xref: /freebsd/sys/net80211/ieee80211_hostap.c (revision 70dc8e5e7f5cfdb393d38d8a0304590f1196dfda)
1b032f27cSSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4b032f27cSSam Leffler  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5b032f27cSSam Leffler  * All rights reserved.
6b032f27cSSam Leffler  *
7b032f27cSSam Leffler  * Redistribution and use in source and binary forms, with or without
8b032f27cSSam Leffler  * modification, are permitted provided that the following conditions
9b032f27cSSam Leffler  * are met:
10b032f27cSSam Leffler  * 1. Redistributions of source code must retain the above copyright
11b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer.
12b032f27cSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
13b032f27cSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
14b032f27cSSam Leffler  *    documentation and/or other materials provided with the distribution.
15b032f27cSSam Leffler  *
16b032f27cSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17b032f27cSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18b032f27cSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19b032f27cSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20b032f27cSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21b032f27cSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22b032f27cSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23b032f27cSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24b032f27cSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25b032f27cSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b032f27cSSam Leffler  */
27b032f27cSSam Leffler 
28b032f27cSSam Leffler /*
29b032f27cSSam Leffler  * IEEE 802.11 HOSTAP mode support.
30b032f27cSSam Leffler  */
31b032f27cSSam Leffler #include "opt_inet.h"
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler 
34b032f27cSSam Leffler #include <sys/param.h>
35b032f27cSSam Leffler #include <sys/systm.h>
36b032f27cSSam Leffler #include <sys/mbuf.h>
37b032f27cSSam Leffler #include <sys/malloc.h>
38b032f27cSSam Leffler #include <sys/kernel.h>
39b032f27cSSam Leffler 
40b032f27cSSam Leffler #include <sys/socket.h>
41b032f27cSSam Leffler #include <sys/sockio.h>
42b032f27cSSam Leffler #include <sys/endian.h>
43b032f27cSSam Leffler #include <sys/errno.h>
44b032f27cSSam Leffler #include <sys/proc.h>
45b032f27cSSam Leffler #include <sys/sysctl.h>
46b032f27cSSam Leffler 
47b032f27cSSam Leffler #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
49b032f27cSSam Leffler #include <net/if_media.h>
50b032f27cSSam Leffler #include <net/if_llc.h>
513d0d5b21SJustin Hibbits #include <net/if_private.h>
52b032f27cSSam Leffler #include <net/ethernet.h>
53b032f27cSSam Leffler 
54b032f27cSSam Leffler #include <net/bpf.h>
55b032f27cSSam Leffler 
56b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
57b032f27cSSam Leffler #include <net80211/ieee80211_hostap.h>
58b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
59616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
60616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
61616190d0SSam Leffler #endif
62b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
6351172f62SAdrian Chadd #include <net80211/ieee80211_vht.h>
648379e8dbSAdrian Chadd #include <net80211/ieee80211_sta.h> /* for parse_wmeie */
65b032f27cSSam Leffler 
66b032f27cSSam Leffler static	void hostap_vattach(struct ieee80211vap *);
67b032f27cSSam Leffler static	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
68b032f27cSSam Leffler static	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
69c79f192cSAdrian Chadd 	    const struct ieee80211_rx_stats *,
705463c4a4SSam Leffler 	    int rssi, int nf);
71b032f27cSSam Leffler static void hostap_deliver_data(struct ieee80211vap *,
72b032f27cSSam Leffler 	    struct ieee80211_node *, struct mbuf *);
73b032f27cSSam Leffler static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74c79f192cSAdrian Chadd 	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
7549eae5f7SSam Leffler static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
76b032f27cSSam Leffler 
77b032f27cSSam Leffler void
ieee80211_hostap_attach(struct ieee80211com * ic)78b032f27cSSam Leffler ieee80211_hostap_attach(struct ieee80211com *ic)
79b032f27cSSam Leffler {
80b032f27cSSam Leffler 	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
81b032f27cSSam Leffler }
82b032f27cSSam Leffler 
83b032f27cSSam Leffler void
ieee80211_hostap_detach(struct ieee80211com * ic)84b032f27cSSam Leffler ieee80211_hostap_detach(struct ieee80211com *ic)
85b032f27cSSam Leffler {
86b032f27cSSam Leffler }
87b032f27cSSam Leffler 
88b032f27cSSam Leffler static void
hostap_vdetach(struct ieee80211vap * vap)89b032f27cSSam Leffler hostap_vdetach(struct ieee80211vap *vap)
90b032f27cSSam Leffler {
91b032f27cSSam Leffler }
92b032f27cSSam Leffler 
93b032f27cSSam Leffler static void
hostap_vattach(struct ieee80211vap * vap)94b032f27cSSam Leffler hostap_vattach(struct ieee80211vap *vap)
95b032f27cSSam Leffler {
96b032f27cSSam Leffler 	vap->iv_newstate = hostap_newstate;
97b032f27cSSam Leffler 	vap->iv_input = hostap_input;
98b032f27cSSam Leffler 	vap->iv_recv_mgmt = hostap_recv_mgmt;
9949eae5f7SSam Leffler 	vap->iv_recv_ctl = hostap_recv_ctl;
100b032f27cSSam Leffler 	vap->iv_opdetach = hostap_vdetach;
101b032f27cSSam Leffler 	vap->iv_deliver_data = hostap_deliver_data;
102e7f0d7cfSAdrian Chadd 	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
103b032f27cSSam Leffler }
104b032f27cSSam Leffler 
105b032f27cSSam Leffler static void
sta_disassoc(void * arg,struct ieee80211_node * ni)106b032f27cSSam Leffler sta_disassoc(void *arg, struct ieee80211_node *ni)
107b032f27cSSam Leffler {
108b032f27cSSam Leffler 
1097db788c6SAndriy Voskoboinyk 	if (ni->ni_associd != 0) {
110b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
111b032f27cSSam Leffler 			IEEE80211_REASON_ASSOC_LEAVE);
112b032f27cSSam Leffler 		ieee80211_node_leave(ni);
113b032f27cSSam Leffler 	}
114b032f27cSSam Leffler }
115b032f27cSSam Leffler 
1164e150988SSam Leffler static void
sta_csa(void * arg,struct ieee80211_node * ni)1174e150988SSam Leffler sta_csa(void *arg, struct ieee80211_node *ni)
1184e150988SSam Leffler {
1197db788c6SAndriy Voskoboinyk 	struct ieee80211vap *vap = ni->ni_vap;
1204e150988SSam Leffler 
1217db788c6SAndriy Voskoboinyk 	if (ni->ni_associd != 0)
1224e150988SSam Leffler 		if (ni->ni_inact > vap->iv_inact_init) {
1234e150988SSam Leffler 			ni->ni_inact = vap->iv_inact_init;
1244e150988SSam Leffler 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
1254e150988SSam Leffler 			    "%s: inact %u", __func__, ni->ni_inact);
1264e150988SSam Leffler 		}
1274e150988SSam Leffler }
1284e150988SSam Leffler 
1297131987dSSam Leffler static void
sta_drop(void * arg,struct ieee80211_node * ni)1307131987dSSam Leffler sta_drop(void *arg, struct ieee80211_node *ni)
1317131987dSSam Leffler {
1327131987dSSam Leffler 
1337db788c6SAndriy Voskoboinyk 	if (ni->ni_associd != 0)
1347131987dSSam Leffler 		ieee80211_node_leave(ni);
1357131987dSSam Leffler }
1367131987dSSam Leffler 
1377131987dSSam Leffler /*
1387131987dSSam Leffler  * Does a channel change require associated stations to re-associate
1397131987dSSam Leffler  * so protocol state is correct.  This is used when doing CSA across
1407131987dSSam Leffler  * bands or similar (e.g. HT -> legacy).
1417131987dSSam Leffler  */
1427131987dSSam Leffler static int
isbandchange(struct ieee80211com * ic)1437131987dSSam Leffler isbandchange(struct ieee80211com *ic)
1447131987dSSam Leffler {
1457131987dSSam Leffler 	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
1467131987dSSam Leffler 	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
1477131987dSSam Leffler 	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
1487131987dSSam Leffler }
1497131987dSSam Leffler 
150b032f27cSSam Leffler /*
151b032f27cSSam Leffler  * IEEE80211_M_HOSTAP vap state machine handler.
152b032f27cSSam Leffler  */
153b032f27cSSam Leffler static int
hostap_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)154b032f27cSSam Leffler hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
155b032f27cSSam Leffler {
156b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
157b032f27cSSam Leffler 	enum ieee80211_state ostate;
158b032f27cSSam Leffler 
159b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
160b032f27cSSam Leffler 
161b032f27cSSam Leffler 	ostate = vap->iv_state;
162b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
163b032f27cSSam Leffler 	    __func__, ieee80211_state_name[ostate],
164b032f27cSSam Leffler 	    ieee80211_state_name[nstate], arg);
165b032f27cSSam Leffler 	vap->iv_state = nstate;			/* state transition */
166b032f27cSSam Leffler 	if (ostate != IEEE80211_S_SCAN)
167b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);	/* background scan */
168b032f27cSSam Leffler 	switch (nstate) {
169b032f27cSSam Leffler 	case IEEE80211_S_INIT:
170b032f27cSSam Leffler 		switch (ostate) {
171b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
172b032f27cSSam Leffler 			ieee80211_cancel_scan(vap);
173b032f27cSSam Leffler 			break;
174b032f27cSSam Leffler 		case IEEE80211_S_CAC:
175b032f27cSSam Leffler 			ieee80211_dfs_cac_stop(vap);
176b032f27cSSam Leffler 			break;
177b032f27cSSam Leffler 		case IEEE80211_S_RUN:
1787db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
1797db788c6SAndriy Voskoboinyk 			    sta_disassoc, NULL);
180b032f27cSSam Leffler 			break;
181b032f27cSSam Leffler 		default:
182b032f27cSSam Leffler 			break;
183b032f27cSSam Leffler 		}
184b032f27cSSam Leffler 		if (ostate != IEEE80211_S_INIT) {
185b032f27cSSam Leffler 			/* NB: optimize INIT -> INIT case */
186b032f27cSSam Leffler 			ieee80211_reset_bss(vap);
187b032f27cSSam Leffler 		}
188b032f27cSSam Leffler 		if (vap->iv_auth->ia_detach != NULL)
189b032f27cSSam Leffler 			vap->iv_auth->ia_detach(vap);
190b032f27cSSam Leffler 		break;
191b032f27cSSam Leffler 	case IEEE80211_S_SCAN:
192b032f27cSSam Leffler 		switch (ostate) {
193b032f27cSSam Leffler 		case IEEE80211_S_CSA:
194b032f27cSSam Leffler 		case IEEE80211_S_RUN:
1957db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
1967db788c6SAndriy Voskoboinyk 			    sta_disassoc, NULL);
197b032f27cSSam Leffler 			/*
198b032f27cSSam Leffler 			 * Clear overlapping BSS state; the beacon frame
199b032f27cSSam Leffler 			 * will be reconstructed on transition to the RUN
200b032f27cSSam Leffler 			 * state and the timeout routines check if the flag
201b032f27cSSam Leffler 			 * is set before doing anything so this is sufficient.
202b032f27cSSam Leffler 			 */
203f1481c8dSAdrian Chadd 			vap->iv_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
204f1481c8dSAdrian Chadd 			vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
205f1481c8dSAdrian Chadd 			/* XXX TODO: schedule deferred update? */
206b032f27cSSam Leffler 			/* fall thru... */
207b032f27cSSam Leffler 		case IEEE80211_S_CAC:
208b032f27cSSam Leffler 			/*
209b032f27cSSam Leffler 			 * NB: We may get here because of a manual channel
210b032f27cSSam Leffler 			 *     change in which case we need to stop CAC
211b032f27cSSam Leffler 			 * XXX no need to stop if ostate RUN but it's ok
212b032f27cSSam Leffler 			 */
213b032f27cSSam Leffler 			ieee80211_dfs_cac_stop(vap);
214b032f27cSSam Leffler 			/* fall thru... */
215b032f27cSSam Leffler 		case IEEE80211_S_INIT:
216b032f27cSSam Leffler 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
217b032f27cSSam Leffler 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
218b032f27cSSam Leffler 				/*
219b032f27cSSam Leffler 				 * Already have a channel; bypass the
220b032f27cSSam Leffler 				 * scan and startup immediately.
221b032f27cSSam Leffler 				 * ieee80211_create_ibss will call back to
222b032f27cSSam Leffler 				 * move us to RUN state.
223b032f27cSSam Leffler 				 */
224b032f27cSSam Leffler 				ieee80211_create_ibss(vap, vap->iv_des_chan);
225b032f27cSSam Leffler 				break;
226b032f27cSSam Leffler 			}
227b032f27cSSam Leffler 			/*
228b032f27cSSam Leffler 			 * Initiate a scan.  We can come here as a result
229b032f27cSSam Leffler 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
230b032f27cSSam Leffler 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
231b032f27cSSam Leffler 			 * and the scan request parameters will be present
232b032f27cSSam Leffler 			 * in iv_scanreq.  Otherwise we do the default.
233b032f27cSSam Leffler 			 */
234b032f27cSSam Leffler 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
235b032f27cSSam Leffler 				ieee80211_check_scan(vap,
236b032f27cSSam Leffler 				    vap->iv_scanreq_flags,
237b032f27cSSam Leffler 				    vap->iv_scanreq_duration,
238b032f27cSSam Leffler 				    vap->iv_scanreq_mindwell,
239b032f27cSSam Leffler 				    vap->iv_scanreq_maxdwell,
240b032f27cSSam Leffler 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
241b032f27cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
242b032f27cSSam Leffler 			} else
243b032f27cSSam Leffler 				ieee80211_check_scan_current(vap);
244b032f27cSSam Leffler 			break;
245b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
246b032f27cSSam Leffler 			/*
247b032f27cSSam Leffler 			 * A state change requires a reset; scan.
248b032f27cSSam Leffler 			 */
249b032f27cSSam Leffler 			ieee80211_check_scan_current(vap);
250b032f27cSSam Leffler 			break;
251b032f27cSSam Leffler 		default:
252b032f27cSSam Leffler 			break;
253b032f27cSSam Leffler 		}
254b032f27cSSam Leffler 		break;
255b032f27cSSam Leffler 	case IEEE80211_S_CAC:
256b032f27cSSam Leffler 		/*
257b032f27cSSam Leffler 		 * Start CAC on a DFS channel.  We come here when starting
258b032f27cSSam Leffler 		 * a bss on a DFS channel (see ieee80211_create_ibss).
259b032f27cSSam Leffler 		 */
260b032f27cSSam Leffler 		ieee80211_dfs_cac_start(vap);
261b032f27cSSam Leffler 		break;
262b032f27cSSam Leffler 	case IEEE80211_S_RUN:
263b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA) {
264b032f27cSSam Leffler 			/* XXX validate prerequisites */
265b032f27cSSam Leffler 		}
266b032f27cSSam Leffler 		switch (ostate) {
267b032f27cSSam Leffler 		case IEEE80211_S_INIT:
268b032f27cSSam Leffler 			/*
269b032f27cSSam Leffler 			 * Already have a channel; bypass the
270b032f27cSSam Leffler 			 * scan and startup immediately.
271b032f27cSSam Leffler 			 * Note that ieee80211_create_ibss will call
272b032f27cSSam Leffler 			 * back to do a RUN->RUN state change.
273b032f27cSSam Leffler 			 */
274b032f27cSSam Leffler 			ieee80211_create_ibss(vap,
275b032f27cSSam Leffler 			    ieee80211_ht_adjust_channel(ic,
2762bfc8a91SSam Leffler 				ic->ic_curchan, vap->iv_flags_ht));
277b032f27cSSam Leffler 			/* NB: iv_bss is changed on return */
278b032f27cSSam Leffler 			break;
279b032f27cSSam Leffler 		case IEEE80211_S_CAC:
280b032f27cSSam Leffler 			/*
281b032f27cSSam Leffler 			 * NB: This is the normal state change when CAC
282b032f27cSSam Leffler 			 * expires and no radar was detected; no need to
283b032f27cSSam Leffler 			 * clear the CAC timer as it's already expired.
284b032f27cSSam Leffler 			 */
285b032f27cSSam Leffler 			/* fall thru... */
286b032f27cSSam Leffler 		case IEEE80211_S_CSA:
287b032f27cSSam Leffler 			/*
2884e150988SSam Leffler 			 * Shorten inactivity timer of associated stations
2894e150988SSam Leffler 			 * to weed out sta's that don't follow a CSA.
2904e150988SSam Leffler 			 */
2917db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
2927db788c6SAndriy Voskoboinyk 			    sta_csa, NULL);
2934e150988SSam Leffler 			/*
294b032f27cSSam Leffler 			 * Update bss node channel to reflect where
295b032f27cSSam Leffler 			 * we landed after CSA.
296b032f27cSSam Leffler 			 */
297b032f27cSSam Leffler 			ieee80211_node_set_chan(vap->iv_bss,
298b032f27cSSam Leffler 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
299b032f27cSSam Leffler 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
300b032f27cSSam Leffler 			/* XXX bypass debug msgs */
301b032f27cSSam Leffler 			break;
302b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
303b032f27cSSam Leffler 		case IEEE80211_S_RUN:
304b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
305b032f27cSSam Leffler 			if (ieee80211_msg_debug(vap)) {
306b032f27cSSam Leffler 				struct ieee80211_node *ni = vap->iv_bss;
307b032f27cSSam Leffler 				ieee80211_note(vap,
308b032f27cSSam Leffler 				    "synchronized with %s ssid ",
309b032f27cSSam Leffler 				    ether_sprintf(ni->ni_bssid));
310b032f27cSSam Leffler 				ieee80211_print_essid(ni->ni_essid,
311b032f27cSSam Leffler 				    ni->ni_esslen);
31238075f7dSAdrian Chadd 				printf(" channel %d start %uMbit/s\n",
313b032f27cSSam Leffler 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
31438075f7dSAdrian Chadd 				    ieee80211_node_get_txrate_kbit(ni) / 1000);
315b032f27cSSam Leffler 			}
316b032f27cSSam Leffler #endif
317b032f27cSSam Leffler 			break;
318b032f27cSSam Leffler 		default:
319b032f27cSSam Leffler 			break;
320b032f27cSSam Leffler 		}
321b032f27cSSam Leffler 		/*
322b032f27cSSam Leffler 		 * Start/stop the authenticator.  We delay until here
323b032f27cSSam Leffler 		 * to allow configuration to happen out of order.
324b032f27cSSam Leffler 		 */
325b032f27cSSam Leffler 		if (vap->iv_auth->ia_attach != NULL) {
326b032f27cSSam Leffler 			/* XXX check failure */
327b032f27cSSam Leffler 			vap->iv_auth->ia_attach(vap);
328b032f27cSSam Leffler 		} else if (vap->iv_auth->ia_detach != NULL) {
329b032f27cSSam Leffler 			vap->iv_auth->ia_detach(vap);
330b032f27cSSam Leffler 		}
331b032f27cSSam Leffler 		ieee80211_node_authorize(vap->iv_bss);
332b032f27cSSam Leffler 		break;
3337131987dSSam Leffler 	case IEEE80211_S_CSA:
3347131987dSSam Leffler 		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
3357131987dSSam Leffler 			/*
3367131987dSSam Leffler 			 * On a ``band change'' silently drop associated
3377131987dSSam Leffler 			 * stations as they must re-associate before they
3387131987dSSam Leffler 			 * can pass traffic (as otherwise protocol state
3397131987dSSam Leffler 			 * such as capabilities and the negotiated rate
3407131987dSSam Leffler 			 * set may/will be wrong).
3417131987dSSam Leffler 			 */
3427db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
3437db788c6SAndriy Voskoboinyk 			    sta_drop, NULL);
3447131987dSSam Leffler 		}
3457131987dSSam Leffler 		break;
346b032f27cSSam Leffler 	default:
347b032f27cSSam Leffler 		break;
348b032f27cSSam Leffler 	}
349b032f27cSSam Leffler 	return 0;
350b032f27cSSam Leffler }
351b032f27cSSam Leffler 
352b032f27cSSam Leffler static void
hostap_deliver_data(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m)353b032f27cSSam Leffler hostap_deliver_data(struct ieee80211vap *vap,
354b032f27cSSam Leffler 	struct ieee80211_node *ni, struct mbuf *m)
355b032f27cSSam Leffler {
356b032f27cSSam Leffler 	struct ether_header *eh = mtod(m, struct ether_header *);
357b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
358b032f27cSSam Leffler 
359e1cfcbcbSSam Leffler 	/* clear driver/net80211 flags before passing up */
36086bd0491SAndre Oppermann 	m->m_flags &= ~(M_MCAST | M_BCAST);
36186bd0491SAndre Oppermann 	m_clrprotoflags(m);
362e1cfcbcbSSam Leffler 
363b032f27cSSam Leffler 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
364b032f27cSSam Leffler 	    ("gack, opmode %d", vap->iv_opmode));
365b032f27cSSam Leffler 	/*
366b032f27cSSam Leffler 	 * Do accounting.
367b032f27cSSam Leffler 	 */
368dea45121SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
369b032f27cSSam Leffler 	IEEE80211_NODE_STAT(ni, rx_data);
370b032f27cSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
371b032f27cSSam Leffler 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
372b032f27cSSam Leffler 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
373b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mcast);
374b032f27cSSam Leffler 	} else
375b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ucast);
376b032f27cSSam Leffler 
377b032f27cSSam Leffler 	/* perform as a bridge within the AP */
378b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
379b032f27cSSam Leffler 		struct mbuf *mcopy = NULL;
380b032f27cSSam Leffler 
381b032f27cSSam Leffler 		if (m->m_flags & M_MCAST) {
382bd29f817SBjoern A. Zeeb 			mcopy = m_dup(m, IEEE80211_M_NOWAIT);
383b032f27cSSam Leffler 			if (mcopy == NULL)
384dea45121SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
385b032f27cSSam Leffler 			else
386b032f27cSSam Leffler 				mcopy->m_flags |= M_MCAST;
387b032f27cSSam Leffler 		} else {
388b032f27cSSam Leffler 			/*
389b032f27cSSam Leffler 			 * Check if the destination is associated with the
390b032f27cSSam Leffler 			 * same vap and authorized to receive traffic.
391b032f27cSSam Leffler 			 * Beware of traffic destined for the vap itself;
392b032f27cSSam Leffler 			 * sending it will not work; just let it be delivered
393b032f27cSSam Leffler 			 * normally.
394b032f27cSSam Leffler 			 */
395b032f27cSSam Leffler 			struct ieee80211_node *sta = ieee80211_find_vap_node(
396b032f27cSSam Leffler 			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
397b032f27cSSam Leffler 			if (sta != NULL) {
398b032f27cSSam Leffler 				if (ieee80211_node_is_authorized(sta)) {
399b032f27cSSam Leffler 					/*
400b032f27cSSam Leffler 					 * Beware of sending to ourself; this
401b032f27cSSam Leffler 					 * needs to happen via the normal
402b032f27cSSam Leffler 					 * input path.
403b032f27cSSam Leffler 					 */
404b032f27cSSam Leffler 					if (sta != vap->iv_bss) {
405b032f27cSSam Leffler 						mcopy = m;
406b032f27cSSam Leffler 						m = NULL;
407b032f27cSSam Leffler 					}
408b032f27cSSam Leffler 				} else {
409b032f27cSSam Leffler 					vap->iv_stats.is_rx_unauth++;
410b032f27cSSam Leffler 					IEEE80211_NODE_STAT(sta, rx_unauth);
411b032f27cSSam Leffler 				}
412b032f27cSSam Leffler 				ieee80211_free_node(sta);
413b032f27cSSam Leffler 			}
414b032f27cSSam Leffler 		}
4154d4d5e25SAndriy Voskoboinyk 		if (mcopy != NULL)
4164d4d5e25SAndriy Voskoboinyk 			(void) ieee80211_vap_xmitpkt(vap, mcopy);
417b032f27cSSam Leffler 	}
418b032f27cSSam Leffler 	if (m != NULL) {
419de607e3cSBjoern A. Zeeb 		struct epoch_tracker et;
420de607e3cSBjoern A. Zeeb 
421b032f27cSSam Leffler 		/*
422b032f27cSSam Leffler 		 * Mark frame as coming from vap's interface.
423b032f27cSSam Leffler 		 */
424b032f27cSSam Leffler 		m->m_pkthdr.rcvif = ifp;
425b032f27cSSam Leffler 		if (m->m_flags & M_MCAST) {
426b032f27cSSam Leffler 			/*
427b032f27cSSam Leffler 			 * Spam DWDS vap's w/ multicast traffic.
428b032f27cSSam Leffler 			 */
429b032f27cSSam Leffler 			/* XXX only if dwds in use? */
430b032f27cSSam Leffler 			ieee80211_dwds_mcast(vap, m);
431b032f27cSSam Leffler 		}
432b032f27cSSam Leffler 		if (ni->ni_vlan != 0) {
433b032f27cSSam Leffler 			/* attach vlan tag */
434b032f27cSSam Leffler 			m->m_pkthdr.ether_vtag = ni->ni_vlan;
435b032f27cSSam Leffler 			m->m_flags |= M_VLANTAG;
436b032f27cSSam Leffler 		}
437de607e3cSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
438b032f27cSSam Leffler 		ifp->if_input(ifp, m);
439de607e3cSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
440b032f27cSSam Leffler 	}
441b032f27cSSam Leffler }
442b032f27cSSam Leffler 
443b032f27cSSam Leffler /*
444b032f27cSSam Leffler  * Decide if a received management frame should be
445b032f27cSSam Leffler  * printed when debugging is enabled.  This filters some
446b032f27cSSam Leffler  * of the less interesting frames that come frequently
447b032f27cSSam Leffler  * (e.g. beacons).
448b032f27cSSam Leffler  */
449b032f27cSSam Leffler static __inline int
doprint(struct ieee80211vap * vap,int subtype)450b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
451b032f27cSSam Leffler {
452b032f27cSSam Leffler 	switch (subtype) {
453b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
454b032f27cSSam Leffler 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
455b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
456b032f27cSSam Leffler 		return 0;
457b032f27cSSam Leffler 	}
458b032f27cSSam Leffler 	return 1;
459b032f27cSSam Leffler }
460b032f27cSSam Leffler 
461b032f27cSSam Leffler /*
462b032f27cSSam Leffler  * Process a received frame.  The node associated with the sender
463b032f27cSSam Leffler  * should be supplied.  If nothing was found in the node table then
464b032f27cSSam Leffler  * the caller is assumed to supply a reference to iv_bss instead.
465b032f27cSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
466b032f27cSSam Leffler  * during AP scanning to select a AP to associate with; it can have
467b032f27cSSam Leffler  * any units so long as values have consistent units and higher values
468b032f27cSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
469b032f27cSSam Leffler  * by the 802.11 layer.
470b032f27cSSam Leffler  */
471b032f27cSSam Leffler static int
hostap_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)472c79f192cSAdrian Chadd hostap_input(struct ieee80211_node *ni, struct mbuf *m,
473c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
474b032f27cSSam Leffler {
475b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
476b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
477b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
478b032f27cSSam Leffler 	struct ieee80211_frame *wh;
479b032f27cSSam Leffler 	struct ieee80211_key *key;
480b032f27cSSam Leffler 	struct ether_header *eh;
4812b80a340SRui Paulo 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
482b032f27cSSam Leffler 	uint8_t dir, type, subtype, qos;
483b032f27cSSam Leffler 	uint8_t *bssid;
484fe75b452SAdrian Chadd 	int is_hw_decrypted = 0;
485fe75b452SAdrian Chadd 	int has_decrypted = 0;
486fe75b452SAdrian Chadd 
487fe75b452SAdrian Chadd 	/*
488fe75b452SAdrian Chadd 	 * Some devices do hardware decryption all the way through
489fe75b452SAdrian Chadd 	 * to pretending the frame wasn't encrypted in the first place.
490fe75b452SAdrian Chadd 	 * So, tag it appropriately so it isn't discarded inappropriately.
491fe75b452SAdrian Chadd 	 */
492fe75b452SAdrian Chadd 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
493fe75b452SAdrian Chadd 		is_hw_decrypted = 1;
494b032f27cSSam Leffler 
49545f856e3SSam Leffler 	if (m->m_flags & M_AMPDU_MPDU) {
496b032f27cSSam Leffler 		/*
497b032f27cSSam Leffler 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
49845f856e3SSam Leffler 		 * w/ M_AMPDU_MPDU marked have already passed through
49945f856e3SSam Leffler 		 * here but were received out of order and been held on
50045f856e3SSam Leffler 		 * the reorder queue.  When resubmitted they are marked
50145f856e3SSam Leffler 		 * with the M_AMPDU_MPDU flag and we can bypass most of
50245f856e3SSam Leffler 		 * the normal processing.
503b032f27cSSam Leffler 		 */
504b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
505b032f27cSSam Leffler 		type = IEEE80211_FC0_TYPE_DATA;
506b032f27cSSam Leffler 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
507c9b7e9dfSBjoern A. Zeeb 		subtype = IEEE80211_FC0_SUBTYPE_QOS_DATA;
508b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
509b032f27cSSam Leffler 		goto resubmit_ampdu;
510b032f27cSSam Leffler 	}
511b032f27cSSam Leffler 
512b032f27cSSam Leffler 	KASSERT(ni != NULL, ("null node"));
513b032f27cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
514b032f27cSSam Leffler 
515b032f27cSSam Leffler 	type = -1;			/* undefined */
516b032f27cSSam Leffler 
517b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
518b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
519b032f27cSSam Leffler 		    ni->ni_macaddr, NULL,
520b032f27cSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
521b032f27cSSam Leffler 		vap->iv_stats.is_rx_tooshort++;
522b032f27cSSam Leffler 		goto out;
523b032f27cSSam Leffler 	}
524b032f27cSSam Leffler 	/*
525b032f27cSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
526b032f27cSSam Leffler 	 * frame format but don't reference fields past outside
527b032f27cSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
528b032f27cSSam Leffler 	 * present.
529b032f27cSSam Leffler 	 */
530b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
531b032f27cSSam Leffler 
53237e54466SAdrian Chadd 	if (!IEEE80211_IS_FC0_CHECK_VER(wh, IEEE80211_FC0_VERSION_0)) {
533b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
534dc7bf546SSam Leffler 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
535dc7bf546SSam Leffler 		    wh->i_fc[0], wh->i_fc[1]);
536b032f27cSSam Leffler 		vap->iv_stats.is_rx_badversion++;
537b032f27cSSam Leffler 		goto err;
538b032f27cSSam Leffler 	}
539b032f27cSSam Leffler 
540b032f27cSSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
541b032f27cSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
542b032f27cSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
543b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
544b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS)
545b032f27cSSam Leffler 			bssid = wh->i_addr1;
546b032f27cSSam Leffler 		else if (type == IEEE80211_FC0_TYPE_CTL)
547b032f27cSSam Leffler 			bssid = wh->i_addr1;
548b032f27cSSam Leffler 		else {
549b032f27cSSam Leffler 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
550b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap,
551b032f27cSSam Leffler 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
552b032f27cSSam Leffler 				    NULL, "too short (2): len %u",
553b032f27cSSam Leffler 				    m->m_pkthdr.len);
554b032f27cSSam Leffler 				vap->iv_stats.is_rx_tooshort++;
555b032f27cSSam Leffler 				goto out;
556b032f27cSSam Leffler 			}
557b032f27cSSam Leffler 			bssid = wh->i_addr3;
558b032f27cSSam Leffler 		}
559b032f27cSSam Leffler 		/*
560b032f27cSSam Leffler 		 * Validate the bssid.
561b032f27cSSam Leffler 		 */
562b032f27cSSam Leffler 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
563b032f27cSSam Leffler 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
564b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
565b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
566b032f27cSSam Leffler 			/* not interested in */
567b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
568b032f27cSSam Leffler 			    bssid, NULL, "%s", "not to bss");
569b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongbss++;
570b032f27cSSam Leffler 			goto out;
571b032f27cSSam Leffler 		}
572b032f27cSSam Leffler 
573b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
5745463c4a4SSam Leffler 		ni->ni_noise = nf;
575c3ebe019SAdrian Chadd 		if (IEEE80211_HAS_SEQ(type, subtype)) {
576b032f27cSSam Leffler 			uint8_t tid = ieee80211_gettid(wh);
577b032f27cSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
578b032f27cSSam Leffler 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
579b032f27cSSam Leffler 				ic->ic_wme.wme_hipri_traffic++;
58085c4e670SAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh, bssid, rxs))
581b032f27cSSam Leffler 				goto out;
582b032f27cSSam Leffler 		}
583b032f27cSSam Leffler 	}
584b032f27cSSam Leffler 
585b032f27cSSam Leffler 	switch (type) {
586b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
587b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
588b032f27cSSam Leffler 		if (m->m_len < hdrspace &&
589b032f27cSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
590b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
591b032f27cSSam Leffler 			    ni->ni_macaddr, NULL,
592b032f27cSSam Leffler 			    "data too short: expecting %u", hdrspace);
593b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
594b032f27cSSam Leffler 			goto out;		/* XXX */
595b032f27cSSam Leffler 		}
596b032f27cSSam Leffler 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
597b032f27cSSam Leffler 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
598b032f27cSSam Leffler 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
599b032f27cSSam Leffler 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
600b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
601b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT, wh, "data",
602b032f27cSSam Leffler 				    "incorrect dir 0x%x", dir);
603b032f27cSSam Leffler 			} else {
604b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
605b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT |
606b032f27cSSam Leffler 				    IEEE80211_MSG_WDS, wh,
607b032f27cSSam Leffler 				    "4-address data",
608b032f27cSSam Leffler 				    "%s", "DWDS not enabled");
609b032f27cSSam Leffler 			}
610b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
611b032f27cSSam Leffler 			goto out;
612b032f27cSSam Leffler 		}
613b032f27cSSam Leffler 		/* check if source STA is associated */
614b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
615b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
616b032f27cSSam Leffler 			    wh, "data", "%s", "unknown src");
617b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
618b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
619b032f27cSSam Leffler 			    IEEE80211_REASON_NOT_AUTHED);
620b032f27cSSam Leffler 			vap->iv_stats.is_rx_notassoc++;
621b032f27cSSam Leffler 			goto err;
622b032f27cSSam Leffler 		}
623b032f27cSSam Leffler 		if (ni->ni_associd == 0) {
624b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
625b032f27cSSam Leffler 			    wh, "data", "%s", "unassoc src");
626b032f27cSSam Leffler 			IEEE80211_SEND_MGMT(ni,
627b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
628b032f27cSSam Leffler 			    IEEE80211_REASON_NOT_ASSOCED);
629b032f27cSSam Leffler 			vap->iv_stats.is_rx_notassoc++;
630b032f27cSSam Leffler 			goto err;
631b032f27cSSam Leffler 		}
632b032f27cSSam Leffler 
633b032f27cSSam Leffler 		/*
634b032f27cSSam Leffler 		 * Check for power save state change.
635b032f27cSSam Leffler 		 * XXX out-of-order A-MPDU frames?
636b032f27cSSam Leffler 		 */
637b032f27cSSam Leffler 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
638b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
639e7f0d7cfSAdrian Chadd 			vap->iv_node_ps(ni,
640b032f27cSSam Leffler 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
641b032f27cSSam Leffler 		/*
642b032f27cSSam Leffler 		 * For 4-address packets handle WDS discovery
643b032f27cSSam Leffler 		 * notifications.  Once a WDS link is setup frames
644b032f27cSSam Leffler 		 * are just delivered to the WDS vap (see below).
645b032f27cSSam Leffler 		 */
646b032f27cSSam Leffler 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
647b032f27cSSam Leffler 			if (!ieee80211_node_is_authorized(ni)) {
648b032f27cSSam Leffler 				IEEE80211_DISCARD(vap,
649b032f27cSSam Leffler 				    IEEE80211_MSG_INPUT |
650b032f27cSSam Leffler 				    IEEE80211_MSG_WDS, wh,
651b032f27cSSam Leffler 				    "4-address data",
652b032f27cSSam Leffler 				    "%s", "unauthorized port");
653b032f27cSSam Leffler 				vap->iv_stats.is_rx_unauth++;
654b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
655b032f27cSSam Leffler 				goto err;
656b032f27cSSam Leffler 			}
657b032f27cSSam Leffler 			ieee80211_dwds_discover(ni, m);
658b032f27cSSam Leffler 			return type;
659b032f27cSSam Leffler 		}
660b032f27cSSam Leffler 
661b032f27cSSam Leffler 		/*
66245f856e3SSam Leffler 		 * Handle A-MPDU re-ordering.  If the frame is to be
66345f856e3SSam Leffler 		 * processed directly then ieee80211_ampdu_reorder
664b032f27cSSam Leffler 		 * will return 0; otherwise it has consumed the mbuf
665b032f27cSSam Leffler 		 * and we should do nothing more with it.
666b032f27cSSam Leffler 		 */
66745f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU) &&
66885c4e670SAdrian Chadd 		    ieee80211_ampdu_reorder(ni, m, rxs) != 0) {
669b032f27cSSam Leffler 			m = NULL;
670b032f27cSSam Leffler 			goto out;
671b032f27cSSam Leffler 		}
672b032f27cSSam Leffler 	resubmit_ampdu:
673b032f27cSSam Leffler 
674b032f27cSSam Leffler 		/*
675b032f27cSSam Leffler 		 * Handle privacy requirements.  Note that we
676b032f27cSSam Leffler 		 * must not be preempted from here until after
677b032f27cSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
678b032f27cSSam Leffler 		 * otherwise we may violate assumptions in the
679b032f27cSSam Leffler 		 * crypto cipher modules used to do delayed update
680b032f27cSSam Leffler 		 * of replay sequence numbers.
681b032f27cSSam Leffler 		 */
6822889cbe2SAdrian Chadd 		if (is_hw_decrypted || IEEE80211_IS_PROTECTED(wh)) {
683b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
684b032f27cSSam Leffler 				/*
685b032f27cSSam Leffler 				 * Discard encrypted frames when privacy is off.
686b032f27cSSam Leffler 				 */
687b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
688b032f27cSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
689b032f27cSSam Leffler 				vap->iv_stats.is_rx_noprivacy++;
690b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
691b032f27cSSam Leffler 				goto out;
692b032f27cSSam Leffler 			}
693fe75b452SAdrian Chadd 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
694b032f27cSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
695b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
696b032f27cSSam Leffler 				goto out;
697b032f27cSSam Leffler 			}
698b032f27cSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
6995945b5f5SKevin Lo 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
700fe75b452SAdrian Chadd 			has_decrypted = 1;
701b032f27cSSam Leffler 		} else {
702b032f27cSSam Leffler 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
703b032f27cSSam Leffler 			key = NULL;
704b032f27cSSam Leffler 		}
705b032f27cSSam Leffler 
706b032f27cSSam Leffler 		/*
707b032f27cSSam Leffler 		 * Save QoS bits for use below--before we strip the header.
708b032f27cSSam Leffler 		 */
709c9b7e9dfSBjoern A. Zeeb 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS_DATA)
710f3f08e16SAndriy Voskoboinyk 			qos = ieee80211_getqos(wh)[0];
711f3f08e16SAndriy Voskoboinyk 		else
712b032f27cSSam Leffler 			qos = 0;
713b032f27cSSam Leffler 
714b032f27cSSam Leffler 		/*
715b032f27cSSam Leffler 		 * Next up, any fragmentation.
716b032f27cSSam Leffler 		 */
717b032f27cSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
71811572d7dSMathy Vanhoef 			m = ieee80211_defrag(ni, m, hdrspace, has_decrypted);
719b032f27cSSam Leffler 			if (m == NULL) {
720b032f27cSSam Leffler 				/* Fragment dropped or frame not complete yet */
721b032f27cSSam Leffler 				goto out;
722b032f27cSSam Leffler 			}
723b032f27cSSam Leffler 		}
724b032f27cSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
725b032f27cSSam Leffler 
726b032f27cSSam Leffler 		/*
727b032f27cSSam Leffler 		 * Next strip any MSDU crypto bits.
728b032f27cSSam Leffler 		 */
729b032f27cSSam Leffler 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
730b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
731b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
732b032f27cSSam Leffler 			vap->iv_stats.is_rx_demicfail++;
733b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
734b032f27cSSam Leffler 			goto out;
735b032f27cSSam Leffler 		}
736b032f27cSSam Leffler 		/* copy to listener after decrypt */
7375463c4a4SSam Leffler 		if (ieee80211_radiotap_active_vap(vap))
7385463c4a4SSam Leffler 			ieee80211_radiotap_rx(vap, m);
739b032f27cSSam Leffler 		need_tap = 0;
740b032f27cSSam Leffler 		/*
741b032f27cSSam Leffler 		 * Finally, strip the 802.11 header.
742b032f27cSSam Leffler 		 */
743f024bdf1SMathy Vanhoef 		m = ieee80211_decap(vap, m, hdrspace, qos);
744b032f27cSSam Leffler 		if (m == NULL) {
745b032f27cSSam Leffler 			/* XXX mask bit to check for both */
746b032f27cSSam Leffler 			/* don't count Null data frames as errors */
747b032f27cSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
748b032f27cSSam Leffler 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
749b032f27cSSam Leffler 				goto out;
750b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
751b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
752b032f27cSSam Leffler 			vap->iv_stats.is_rx_decap++;
753b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
754b032f27cSSam Leffler 			goto err;
755b032f27cSSam Leffler 		}
756ffc19cf5SMathy Vanhoef 		if (!(qos & IEEE80211_QOS_AMSDU))
757b032f27cSSam Leffler 			eh = mtod(m, struct ether_header *);
758ffc19cf5SMathy Vanhoef 		else
759ffc19cf5SMathy Vanhoef 			eh = NULL;
760b032f27cSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
761b032f27cSSam Leffler 			/*
762b032f27cSSam Leffler 			 * Deny any non-PAE frames received prior to
763b032f27cSSam Leffler 			 * authorization.  For open/shared-key
764b032f27cSSam Leffler 			 * authentication the port is mark authorized
765b032f27cSSam Leffler 			 * after authentication completes.  For 802.1x
766b032f27cSSam Leffler 			 * the port is not marked authorized by the
767b032f27cSSam Leffler 			 * authenticator until the handshake has completed.
768b032f27cSSam Leffler 			 */
769ffc19cf5SMathy Vanhoef 			if (eh == NULL ||
770ffc19cf5SMathy Vanhoef 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
771b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
772ffc19cf5SMathy Vanhoef 				    ni->ni_macaddr, "data", "unauthorized or "
773ffc19cf5SMathy Vanhoef 				    "unknown port: ether type 0x%x len %u",
774ffc19cf5SMathy Vanhoef 				    eh == NULL ? -1 : eh->ether_type,
775ffc19cf5SMathy Vanhoef 				    m->m_pkthdr.len);
776b032f27cSSam Leffler 				vap->iv_stats.is_rx_unauth++;
777b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
778b032f27cSSam Leffler 				goto err;
779b032f27cSSam Leffler 			}
780b032f27cSSam Leffler 		} else {
781b032f27cSSam Leffler 			/*
782b032f27cSSam Leffler 			 * When denying unencrypted frames, discard
783b032f27cSSam Leffler 			 * any non-PAE frames received without encryption.
784b032f27cSSam Leffler 			 */
785b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
786fe75b452SAdrian Chadd 			    ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
787fe75b452SAdrian Chadd 			    (is_hw_decrypted == 0) &&
788ffc19cf5SMathy Vanhoef 			    (eh == NULL ||
789ffc19cf5SMathy Vanhoef 			     eh->ether_type != htons(ETHERTYPE_PAE))) {
790b032f27cSSam Leffler 				/*
791b032f27cSSam Leffler 				 * Drop unencrypted frames.
792b032f27cSSam Leffler 				 */
793b032f27cSSam Leffler 				vap->iv_stats.is_rx_unencrypted++;
794b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
795b032f27cSSam Leffler 				goto out;
796b032f27cSSam Leffler 			}
797b032f27cSSam Leffler 		}
798b032f27cSSam Leffler 		/* XXX require HT? */
799b032f27cSSam Leffler 		if (qos & IEEE80211_QOS_AMSDU) {
800b032f27cSSam Leffler 			m = ieee80211_decap_amsdu(ni, m);
801b032f27cSSam Leffler 			if (m == NULL)
802b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
803616190d0SSam Leffler 		} else {
804616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
805616190d0SSam Leffler 			m = ieee80211_decap_fastframe(vap, ni, m);
806b032f27cSSam Leffler 			if (m == NULL)
807b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
808616190d0SSam Leffler #endif
809b032f27cSSam Leffler 		}
810b032f27cSSam Leffler 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
811b032f27cSSam Leffler 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
812b032f27cSSam Leffler 		else
813b032f27cSSam Leffler 			hostap_deliver_data(vap, ni, m);
814b032f27cSSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
815b032f27cSSam Leffler 
816b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
817b032f27cSSam Leffler 		vap->iv_stats.is_rx_mgmt++;
818b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
819b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
820b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
821b032f27cSSam Leffler 			    wh, "mgt", "incorrect dir 0x%x", dir);
822b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
823b032f27cSSam Leffler 			goto err;
824b032f27cSSam Leffler 		}
825b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
826b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
827b032f27cSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
828b032f27cSSam Leffler 			    m->m_pkthdr.len);
829b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
830b032f27cSSam Leffler 			goto out;
831b032f27cSSam Leffler 		}
832b032f27cSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
833b032f27cSSam Leffler 			/* ensure return frames are unicast */
834b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
835b032f27cSSam Leffler 			    wh, NULL, "source is multicast: %s",
836b032f27cSSam Leffler 			    ether_sprintf(wh->i_addr2));
837b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
838b032f27cSSam Leffler 			goto out;
839b032f27cSSam Leffler 		}
840b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
841b032f27cSSam Leffler 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
842b032f27cSSam Leffler 		    ieee80211_msg_dumppkts(vap)) {
843b032f27cSSam Leffler 			if_printf(ifp, "received %s from %s rssi %d\n",
8444357a5d1SAndriy Voskoboinyk 			    ieee80211_mgt_subtype_name(subtype),
845b032f27cSSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
846b032f27cSSam Leffler 		}
847b032f27cSSam Leffler #endif
8482889cbe2SAdrian Chadd 		if (IEEE80211_IS_PROTECTED(wh)) {
849b032f27cSSam Leffler 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
850b032f27cSSam Leffler 				/*
851b032f27cSSam Leffler 				 * Only shared key auth frames with a challenge
852b032f27cSSam Leffler 				 * should be encrypted, discard all others.
853b032f27cSSam Leffler 				 */
854b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
855b032f27cSSam Leffler 				    wh, NULL,
856b032f27cSSam Leffler 				    "%s", "WEP set but not permitted");
857b032f27cSSam Leffler 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
858b032f27cSSam Leffler 				goto out;
859b032f27cSSam Leffler 			}
860b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
861b032f27cSSam Leffler 				/*
862b032f27cSSam Leffler 				 * Discard encrypted frames when privacy is off.
863b032f27cSSam Leffler 				 */
864b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
865b032f27cSSam Leffler 				    wh, NULL, "%s", "WEP set but PRIVACY off");
866b032f27cSSam Leffler 				vap->iv_stats.is_rx_noprivacy++;
867b032f27cSSam Leffler 				goto out;
868b032f27cSSam Leffler 			}
869b032f27cSSam Leffler 			hdrspace = ieee80211_hdrspace(ic, wh);
870fe75b452SAdrian Chadd 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
871b032f27cSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
872b032f27cSSam Leffler 				goto out;
873b032f27cSSam Leffler 			}
874b032f27cSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
8755945b5f5SKevin Lo 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
876fe75b452SAdrian Chadd 			has_decrypted = 1;
877b032f27cSSam Leffler 		}
8784e87d54aSRui Paulo 		/*
8794e87d54aSRui Paulo 		 * Pass the packet to radiotap before calling iv_recv_mgmt().
8804e87d54aSRui Paulo 		 * Otherwise iv_recv_mgmt() might pass another packet to
8814e87d54aSRui Paulo 		 * radiotap, resulting in out of order packet captures.
8824e87d54aSRui Paulo 		 */
883323f12abSRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
884323f12abSRui Paulo 			ieee80211_radiotap_rx(vap, m);
885323f12abSRui Paulo 		need_tap = 0;
886c79f192cSAdrian Chadd 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
8878bbd3e41SSam Leffler 		goto out;
888b032f27cSSam Leffler 
889b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
890b032f27cSSam Leffler 		vap->iv_stats.is_rx_ctl++;
891b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
89249eae5f7SSam Leffler 		vap->iv_recv_ctl(ni, m, subtype);
893b032f27cSSam Leffler 		goto out;
894b032f27cSSam Leffler 	default:
895b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
896b032f27cSSam Leffler 		    wh, "bad", "frame type 0x%x", type);
897b032f27cSSam Leffler 		/* should not come here */
898b032f27cSSam Leffler 		break;
899b032f27cSSam Leffler 	}
900b032f27cSSam Leffler err:
901dea45121SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
902b032f27cSSam Leffler out:
903b032f27cSSam Leffler 	if (m != NULL) {
904a6c3cf3eSSam Leffler 		if (need_tap && ieee80211_radiotap_active_vap(vap))
9055463c4a4SSam Leffler 			ieee80211_radiotap_rx(vap, m);
906b032f27cSSam Leffler 		m_freem(m);
907b032f27cSSam Leffler 	}
908b032f27cSSam Leffler 	return type;
909b032f27cSSam Leffler }
910b032f27cSSam Leffler 
911b032f27cSSam Leffler static void
hostap_auth_open(struct ieee80211_node * ni,struct ieee80211_frame * wh,int rssi,int nf,uint16_t seq,uint16_t status)912b032f27cSSam Leffler hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
9135463c4a4SSam Leffler     int rssi, int nf, uint16_t seq, uint16_t status)
914b032f27cSSam Leffler {
915b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
916b032f27cSSam Leffler 
917b032f27cSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
918b032f27cSSam Leffler 
919b032f27cSSam Leffler 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
920b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
921b032f27cSSam Leffler 		    ni->ni_macaddr, "open auth",
922b032f27cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
923b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
924b032f27cSSam Leffler 		/*
925b032f27cSSam Leffler 		 * Clear any challenge text that may be there if
926b032f27cSSam Leffler 		 * a previous shared key auth failed and then an
927b032f27cSSam Leffler 		 * open auth is attempted.
928b032f27cSSam Leffler 		 */
929b032f27cSSam Leffler 		if (ni->ni_challenge != NULL) {
930b9b53389SAdrian Chadd 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
931b032f27cSSam Leffler 			ni->ni_challenge = NULL;
932b032f27cSSam Leffler 		}
933b032f27cSSam Leffler 		/* XXX hack to workaround calling convention */
934b032f27cSSam Leffler 		ieee80211_send_error(ni, wh->i_addr2,
935b032f27cSSam Leffler 		    IEEE80211_FC0_SUBTYPE_AUTH,
936b032f27cSSam Leffler 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
937b032f27cSSam Leffler 		return;
938b032f27cSSam Leffler 	}
939b032f27cSSam Leffler 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
940b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;
941b032f27cSSam Leffler 		return;
942b032f27cSSam Leffler 	}
943b032f27cSSam Leffler 	/* always accept open authentication requests */
944b032f27cSSam Leffler 	if (ni == vap->iv_bss) {
945b032f27cSSam Leffler 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
946b032f27cSSam Leffler 		if (ni == NULL)
947b032f27cSSam Leffler 			return;
948b032f27cSSam Leffler 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
949b032f27cSSam Leffler 		(void) ieee80211_ref_node(ni);
950b032f27cSSam Leffler 	/*
951b032f27cSSam Leffler 	 * Mark the node as referenced to reflect that it's
952b032f27cSSam Leffler 	 * reference count has been bumped to insure it remains
953b032f27cSSam Leffler 	 * after the transaction completes.
954b032f27cSSam Leffler 	 */
955b032f27cSSam Leffler 	ni->ni_flags |= IEEE80211_NODE_AREF;
9561b999d64SSam Leffler 	/*
95712c290feSSam Leffler 	 * Mark the node as requiring a valid association id
9581b999d64SSam Leffler 	 * before outbound traffic is permitted.
9591b999d64SSam Leffler 	 */
9601b999d64SSam Leffler 	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
961b032f27cSSam Leffler 
962b032f27cSSam Leffler 	if (vap->iv_acl != NULL &&
963b032f27cSSam Leffler 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
964b032f27cSSam Leffler 		/*
965b032f27cSSam Leffler 		 * When the ACL policy is set to RADIUS we defer the
966b032f27cSSam Leffler 		 * authorization to a user agent.  Dispatch an event,
967b032f27cSSam Leffler 		 * a subsequent MLME call will decide the fate of the
968b032f27cSSam Leffler 		 * station.  If the user agent is not present then the
969b032f27cSSam Leffler 		 * node will be reclaimed due to inactivity.
970b032f27cSSam Leffler 		 */
971b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap,
972b032f27cSSam Leffler 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
97394fc800fSGordon Bergling 		    "%s", "station authentication deferred (radius acl)");
974b032f27cSSam Leffler 		ieee80211_notify_node_auth(ni);
975b032f27cSSam Leffler 	} else {
976b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
977b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap,
978b032f27cSSam Leffler 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
979b032f27cSSam Leffler 		    "%s", "station authenticated (open)");
980b032f27cSSam Leffler 		/*
981b032f27cSSam Leffler 		 * When 802.1x is not in use mark the port
982b032f27cSSam Leffler 		 * authorized at this point so traffic can flow.
983b032f27cSSam Leffler 		 */
984b032f27cSSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
985b032f27cSSam Leffler 			ieee80211_node_authorize(ni);
986b032f27cSSam Leffler 	}
987b032f27cSSam Leffler }
988b032f27cSSam Leffler 
989b032f27cSSam Leffler static void
hostap_auth_shared(struct ieee80211_node * ni,struct ieee80211_frame * wh,uint8_t * frm,uint8_t * efrm,int rssi,int nf,uint16_t seq,uint16_t status)990b032f27cSSam Leffler hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
9915463c4a4SSam Leffler     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
992b032f27cSSam Leffler     uint16_t seq, uint16_t status)
993b032f27cSSam Leffler {
994b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
995b032f27cSSam Leffler 	uint8_t *challenge;
99605ea7a3eSBjoern A. Zeeb 	int estatus;
997b032f27cSSam Leffler 
998b032f27cSSam Leffler 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
999b032f27cSSam Leffler 
1000b032f27cSSam Leffler 	/*
1001b032f27cSSam Leffler 	 * NB: this can happen as we allow pre-shared key
1002b032f27cSSam Leffler 	 * authentication to be enabled w/o wep being turned
1003b032f27cSSam Leffler 	 * on so that configuration of these can be done
1004b032f27cSSam Leffler 	 * in any order.  It may be better to enforce the
1005b032f27cSSam Leffler 	 * ordering in which case this check would just be
1006b032f27cSSam Leffler 	 * for sanity/consistency.
1007b032f27cSSam Leffler 	 */
1008b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1009b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1010b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
1011b032f27cSSam Leffler 		    "%s", " PRIVACY is disabled");
1012b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
1013b032f27cSSam Leffler 		goto bad;
1014b032f27cSSam Leffler 	}
1015b032f27cSSam Leffler 	/*
1016b032f27cSSam Leffler 	 * Pre-shared key authentication is evil; accept
1017b032f27cSSam Leffler 	 * it only if explicitly configured (it is supported
1018b032f27cSSam Leffler 	 * mainly for compatibility with clients like Mac OS X).
1019b032f27cSSam Leffler 	 */
1020b032f27cSSam Leffler 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1021b032f27cSSam Leffler 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1022b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1023b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
1024b032f27cSSam Leffler 		    "bad sta auth mode %u", ni->ni_authmode);
1025b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1026b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_ALG;
1027b032f27cSSam Leffler 		goto bad;
1028b032f27cSSam Leffler 	}
1029b032f27cSSam Leffler 
1030b032f27cSSam Leffler 	challenge = NULL;
1031b032f27cSSam Leffler 	if (frm + 1 < efrm) {
1032b032f27cSSam Leffler 		if ((frm[1] + 2) > (efrm - frm)) {
1033b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1034b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1035b032f27cSSam Leffler 			    "ie %d/%d too long",
1036b032f27cSSam Leffler 			    frm[0], (frm[1] + 2) - (efrm - frm));
1037b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1038b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1039b032f27cSSam Leffler 			goto bad;
1040b032f27cSSam Leffler 		}
1041b032f27cSSam Leffler 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1042b032f27cSSam Leffler 			challenge = frm;
1043b032f27cSSam Leffler 		frm += frm[1] + 2;
1044b032f27cSSam Leffler 	}
1045b032f27cSSam Leffler 	switch (seq) {
1046b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1047b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
1048b032f27cSSam Leffler 		if (challenge == NULL) {
1049b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1050b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1051b032f27cSSam Leffler 			    "%s", "no challenge");
1052b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1053b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1054b032f27cSSam Leffler 			goto bad;
1055b032f27cSSam Leffler 		}
1056b032f27cSSam Leffler 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1057b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key auth",
1059b032f27cSSam Leffler 			    "bad challenge len %d", challenge[1]);
1060b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1061b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1062b032f27cSSam Leffler 			goto bad;
1063b032f27cSSam Leffler 		}
1064b032f27cSSam Leffler 	default:
1065b032f27cSSam Leffler 		break;
1066b032f27cSSam Leffler 	}
1067b032f27cSSam Leffler 	switch (seq) {
1068b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_REQUEST:
106905ea7a3eSBjoern A. Zeeb 	{
107005ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
107105ea7a3eSBjoern A. Zeeb 		bool allocbs;
107205ea7a3eSBjoern A. Zeeb #endif
107305ea7a3eSBjoern A. Zeeb 
1074b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
1075b032f27cSSam Leffler 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1076b032f27cSSam Leffler 			if (ni == NULL) {
1077b032f27cSSam Leffler 				/* NB: no way to return an error */
1078b032f27cSSam Leffler 				return;
1079b032f27cSSam Leffler 			}
108005ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
1081b032f27cSSam Leffler 			allocbs = 1;
108205ea7a3eSBjoern A. Zeeb #endif
1083b032f27cSSam Leffler 		} else {
1084b032f27cSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1085b032f27cSSam Leffler 				(void) ieee80211_ref_node(ni);
108605ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
1087b032f27cSSam Leffler 			allocbs = 0;
108805ea7a3eSBjoern A. Zeeb #endif
1089b032f27cSSam Leffler 		}
1090b032f27cSSam Leffler 		/*
1091b032f27cSSam Leffler 		 * Mark the node as referenced to reflect that it's
1092b032f27cSSam Leffler 		 * reference count has been bumped to insure it remains
1093b032f27cSSam Leffler 		 * after the transaction completes.
1094b032f27cSSam Leffler 		 */
1095b032f27cSSam Leffler 		ni->ni_flags |= IEEE80211_NODE_AREF;
10961b999d64SSam Leffler 		/*
10976dbbec93SAndriy Voskoboinyk 		 * Mark the node as requiring a valid association id
10981b999d64SSam Leffler 		 * before outbound traffic is permitted.
10991b999d64SSam Leffler 		 */
11001b999d64SSam Leffler 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1101b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
11025463c4a4SSam Leffler 		ni->ni_noise = nf;
1103b032f27cSSam Leffler 		if (!ieee80211_alloc_challenge(ni)) {
1104b032f27cSSam Leffler 			/* NB: don't return error so they rexmit */
1105b032f27cSSam Leffler 			return;
1106b032f27cSSam Leffler 		}
1107af7d9f8eSBjoern A. Zeeb 		net80211_get_random_bytes(ni->ni_challenge,
1108b032f27cSSam Leffler 			IEEE80211_CHALLENGE_LEN);
1109b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1110b032f27cSSam Leffler 		    ni, "shared key %sauth request", allocbs ? "" : "re");
1111b032f27cSSam Leffler 		/*
1112b032f27cSSam Leffler 		 * When the ACL policy is set to RADIUS we defer the
1113b032f27cSSam Leffler 		 * authorization to a user agent.  Dispatch an event,
1114b032f27cSSam Leffler 		 * a subsequent MLME call will decide the fate of the
1115b032f27cSSam Leffler 		 * station.  If the user agent is not present then the
1116b032f27cSSam Leffler 		 * node will be reclaimed due to inactivity.
1117b032f27cSSam Leffler 		 */
1118b032f27cSSam Leffler 		if (vap->iv_acl != NULL &&
1119b032f27cSSam Leffler 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1120b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap,
1121b032f27cSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1122b032f27cSSam Leffler 			    ni->ni_macaddr,
112394fc800fSGordon Bergling 			    "%s", "station authentication deferred (radius acl)");
1124b032f27cSSam Leffler 			ieee80211_notify_node_auth(ni);
1125b032f27cSSam Leffler 			return;
1126b032f27cSSam Leffler 		}
1127b032f27cSSam Leffler 		break;
112805ea7a3eSBjoern A. Zeeb 	}
1129b032f27cSSam Leffler 	case IEEE80211_AUTH_SHARED_RESPONSE:
1130b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
1131b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1132b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1133b032f27cSSam Leffler 			    "%s", "unknown station");
1134b032f27cSSam Leffler 			/* NB: don't send a response */
1135b032f27cSSam Leffler 			return;
1136b032f27cSSam Leffler 		}
1137b032f27cSSam Leffler 		if (ni->ni_challenge == NULL) {
1138b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1139b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1140b032f27cSSam Leffler 			    "%s", "no challenge recorded");
1141b032f27cSSam Leffler 			vap->iv_stats.is_rx_bad_auth++;
1142b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1143b032f27cSSam Leffler 			goto bad;
1144b032f27cSSam Leffler 		}
1145b032f27cSSam Leffler 		if (memcmp(ni->ni_challenge, &challenge[2],
1146b032f27cSSam Leffler 			   challenge[1]) != 0) {
1147b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1148b032f27cSSam Leffler 			    ni->ni_macaddr, "shared key response",
1149b032f27cSSam Leffler 			    "%s", "challenge mismatch");
1150b032f27cSSam Leffler 			vap->iv_stats.is_rx_auth_fail++;
1151b032f27cSSam Leffler 			estatus = IEEE80211_STATUS_CHALLENGE;
1152b032f27cSSam Leffler 			goto bad;
1153b032f27cSSam Leffler 		}
1154b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1155b032f27cSSam Leffler 		    ni, "%s", "station authenticated (shared key)");
1156b032f27cSSam Leffler 		ieee80211_node_authorize(ni);
1157b032f27cSSam Leffler 		break;
1158b032f27cSSam Leffler 	default:
1159b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1160b032f27cSSam Leffler 		    ni->ni_macaddr, "shared key auth",
1161b032f27cSSam Leffler 		    "bad seq %d", seq);
1162b032f27cSSam Leffler 		vap->iv_stats.is_rx_bad_auth++;
1163b032f27cSSam Leffler 		estatus = IEEE80211_STATUS_SEQUENCE;
1164b032f27cSSam Leffler 		goto bad;
1165b032f27cSSam Leffler 	}
1166b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1167b032f27cSSam Leffler 	return;
1168b032f27cSSam Leffler bad:
1169b032f27cSSam Leffler 	/*
1170b032f27cSSam Leffler 	 * Send an error response; but only when operating as an AP.
1171b032f27cSSam Leffler 	 */
1172b032f27cSSam Leffler 	/* XXX hack to workaround calling convention */
1173b032f27cSSam Leffler 	ieee80211_send_error(ni, wh->i_addr2,
1174b032f27cSSam Leffler 	    IEEE80211_FC0_SUBTYPE_AUTH,
1175b032f27cSSam Leffler 	    (seq + 1) | (estatus<<16));
1176b032f27cSSam Leffler }
1177b032f27cSSam Leffler 
1178b032f27cSSam Leffler /*
1179b032f27cSSam Leffler  * Convert a WPA cipher selector OUI to an internal
1180b032f27cSSam Leffler  * cipher algorithm.  Where appropriate we also
1181b032f27cSSam Leffler  * record any key length.
1182b032f27cSSam Leffler  */
1183b032f27cSSam Leffler static int
wpa_cipher(const uint8_t * sel,uint8_t * keylen,uint8_t * cipher)118495d9a127SAndriy Voskoboinyk wpa_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1185b032f27cSSam Leffler {
1186b032f27cSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
118731021a2bSAndriy Voskoboinyk 	uint32_t w = le32dec(sel);
1188b032f27cSSam Leffler 
1189b032f27cSSam Leffler 	switch (w) {
1190b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_NULL):
119195d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_NONE;
119295d9a127SAndriy Voskoboinyk 		break;
1193b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_WEP40):
1194b032f27cSSam Leffler 		if (keylen)
1195b032f27cSSam Leffler 			*keylen = 40 / NBBY;
119695d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_WEP;
119795d9a127SAndriy Voskoboinyk 		break;
1198b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_WEP104):
1199b032f27cSSam Leffler 		if (keylen)
1200b032f27cSSam Leffler 			*keylen = 104 / NBBY;
120195d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_WEP;
120295d9a127SAndriy Voskoboinyk 		break;
1203b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_TKIP):
120495d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_TKIP;
120595d9a127SAndriy Voskoboinyk 		break;
1206b032f27cSSam Leffler 	case WPA_SEL(WPA_CSE_CCMP):
120795d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_AES_CCM;
120895d9a127SAndriy Voskoboinyk 		break;
1209*70dc8e5eSAdrian Chadd 	/* Note: no GCM cipher in the legacy WPA1 OUI */
121095d9a127SAndriy Voskoboinyk 	default:
121195d9a127SAndriy Voskoboinyk 		return (EINVAL);
1212b032f27cSSam Leffler 	}
121395d9a127SAndriy Voskoboinyk 
121495d9a127SAndriy Voskoboinyk 	return (0);
1215b032f27cSSam Leffler #undef WPA_SEL
1216b032f27cSSam Leffler }
1217b032f27cSSam Leffler 
1218b032f27cSSam Leffler /*
1219b032f27cSSam Leffler  * Convert a WPA key management/authentication algorithm
1220b032f27cSSam Leffler  * to an internal code.
1221b032f27cSSam Leffler  */
1222b032f27cSSam Leffler static int
wpa_keymgmt(const uint8_t * sel)1223b032f27cSSam Leffler wpa_keymgmt(const uint8_t *sel)
1224b032f27cSSam Leffler {
1225b032f27cSSam Leffler #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
122631021a2bSAndriy Voskoboinyk 	uint32_t w = le32dec(sel);
1227b032f27cSSam Leffler 
1228b032f27cSSam Leffler 	switch (w) {
1229b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1230b032f27cSSam Leffler 		return WPA_ASE_8021X_UNSPEC;
1231b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_8021X_PSK):
1232b032f27cSSam Leffler 		return WPA_ASE_8021X_PSK;
1233b032f27cSSam Leffler 	case WPA_SEL(WPA_ASE_NONE):
1234b032f27cSSam Leffler 		return WPA_ASE_NONE;
1235b032f27cSSam Leffler 	}
1236b032f27cSSam Leffler 	return 0;		/* NB: so is discarded */
1237b032f27cSSam Leffler #undef WPA_SEL
1238b032f27cSSam Leffler }
1239b032f27cSSam Leffler 
1240b032f27cSSam Leffler /*
1241b032f27cSSam Leffler  * Parse a WPA information element to collect parameters.
1242b032f27cSSam Leffler  * Note that we do not validate security parameters; that
1243b032f27cSSam Leffler  * is handled by the authenticator; the parsing done here
1244b032f27cSSam Leffler  * is just for internal use in making operational decisions.
1245b032f27cSSam Leffler  */
1246b032f27cSSam Leffler static int
ieee80211_parse_wpa(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)1247b032f27cSSam Leffler ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1248b032f27cSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1249b032f27cSSam Leffler {
1250b032f27cSSam Leffler 	uint8_t len = frm[1];
1251b032f27cSSam Leffler 	uint32_t w;
125295d9a127SAndriy Voskoboinyk 	int error, n;
1253b032f27cSSam Leffler 
1254b032f27cSSam Leffler 	/*
1255b032f27cSSam Leffler 	 * Check the length once for fixed parts: OUI, type,
1256b032f27cSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
1257b032f27cSSam Leffler 	 * Other, variable-length data, must be checked separately.
1258b032f27cSSam Leffler 	 */
1259b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1260b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1261b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1262b032f27cSSam Leffler 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1263b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1264b032f27cSSam Leffler 	}
1265b032f27cSSam Leffler 	if (len < 14) {
1266b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1267b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1268b032f27cSSam Leffler 		    wh, "WPA", "too short, len %u", len);
1269b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1270b032f27cSSam Leffler 	}
1271b032f27cSSam Leffler 	frm += 6, len -= 4;		/* NB: len is payload only */
1272f59310a1SRui Paulo 	/* NB: iswpaoui already validated the OUI and type */
127331021a2bSAndriy Voskoboinyk 	w = le16dec(frm);
1274b032f27cSSam Leffler 	if (w != WPA_VERSION) {
1275b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1276b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1277b032f27cSSam Leffler 		    wh, "WPA", "bad version %u", w);
1278b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1279b032f27cSSam Leffler 	}
1280b032f27cSSam Leffler 	frm += 2, len -= 2;
1281b032f27cSSam Leffler 
1282b032f27cSSam Leffler 	memset(rsn, 0, sizeof(*rsn));
1283b032f27cSSam Leffler 
1284b032f27cSSam Leffler 	/* multicast/group cipher */
128595d9a127SAndriy Voskoboinyk 	error = wpa_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
128695d9a127SAndriy Voskoboinyk 	if (error != 0) {
128795d9a127SAndriy Voskoboinyk 		IEEE80211_DISCARD_IE(vap,
128895d9a127SAndriy Voskoboinyk 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
128995d9a127SAndriy Voskoboinyk 		    wh, "WPA", "unknown mcast cipher suite %08X",
129095d9a127SAndriy Voskoboinyk 		    le32dec(frm));
129195d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
129295d9a127SAndriy Voskoboinyk 	}
1293b032f27cSSam Leffler 	frm += 4, len -= 4;
1294b032f27cSSam Leffler 
1295b032f27cSSam Leffler 	/* unicast ciphers */
129631021a2bSAndriy Voskoboinyk 	n = le16dec(frm);
1297b032f27cSSam Leffler 	frm += 2, len -= 2;
1298b032f27cSSam Leffler 	if (len < n*4+2) {
1299b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1300b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1301b032f27cSSam Leffler 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1302b032f27cSSam Leffler 		    len, n);
1303b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1304b032f27cSSam Leffler 	}
1305b032f27cSSam Leffler 	w = 0;
1306b032f27cSSam Leffler 	for (; n > 0; n--) {
130795d9a127SAndriy Voskoboinyk 		uint8_t cipher;
130895d9a127SAndriy Voskoboinyk 
130995d9a127SAndriy Voskoboinyk 		error = wpa_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
131095d9a127SAndriy Voskoboinyk 		if (error == 0)
131195d9a127SAndriy Voskoboinyk 			w |= 1 << cipher;
131295d9a127SAndriy Voskoboinyk 
1313b032f27cSSam Leffler 		frm += 4, len -= 4;
1314b032f27cSSam Leffler 	}
131595d9a127SAndriy Voskoboinyk 	if (w == 0) {
131695d9a127SAndriy Voskoboinyk 		IEEE80211_DISCARD_IE(vap,
131795d9a127SAndriy Voskoboinyk 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
131895d9a127SAndriy Voskoboinyk 		    wh, "WPA", "no usable pairwise cipher suite found (w=%d)",
131995d9a127SAndriy Voskoboinyk 		    w);
132095d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
132195d9a127SAndriy Voskoboinyk 	}
132295d9a127SAndriy Voskoboinyk 	/* XXX other? */
132395d9a127SAndriy Voskoboinyk 	if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1324b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
132595d9a127SAndriy Voskoboinyk 	else
132695d9a127SAndriy Voskoboinyk 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1327b032f27cSSam Leffler 
1328b032f27cSSam Leffler 	/* key management algorithms */
132931021a2bSAndriy Voskoboinyk 	n = le16dec(frm);
1330b032f27cSSam Leffler 	frm += 2, len -= 2;
1331b032f27cSSam Leffler 	if (len < n*4) {
1332b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1333b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1334b032f27cSSam Leffler 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1335b032f27cSSam Leffler 		    len, n);
1336b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1337b032f27cSSam Leffler 	}
1338b032f27cSSam Leffler 	w = 0;
1339b032f27cSSam Leffler 	for (; n > 0; n--) {
1340b032f27cSSam Leffler 		w |= wpa_keymgmt(frm);
1341b032f27cSSam Leffler 		frm += 4, len -= 4;
1342b032f27cSSam Leffler 	}
1343b032f27cSSam Leffler 	if (w & WPA_ASE_8021X_UNSPEC)
1344b032f27cSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1345b032f27cSSam Leffler 	else
1346b032f27cSSam Leffler 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1347b032f27cSSam Leffler 
1348b032f27cSSam Leffler 	if (len > 2)		/* optional capabilities */
134931021a2bSAndriy Voskoboinyk 		rsn->rsn_caps = le16dec(frm);
1350b032f27cSSam Leffler 
1351b032f27cSSam Leffler 	return 0;
1352b032f27cSSam Leffler }
1353b032f27cSSam Leffler 
1354b032f27cSSam Leffler /*
1355b032f27cSSam Leffler  * Convert an RSN cipher selector OUI to an internal
1356b032f27cSSam Leffler  * cipher algorithm.  Where appropriate we also
1357b032f27cSSam Leffler  * record any key length.
1358b032f27cSSam Leffler  */
1359b032f27cSSam Leffler static int
rsn_cipher(const uint8_t * sel,uint8_t * keylen,uint8_t * cipher)136095d9a127SAndriy Voskoboinyk rsn_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1361b032f27cSSam Leffler {
1362b032f27cSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
136331021a2bSAndriy Voskoboinyk 	uint32_t w = le32dec(sel);
1364b032f27cSSam Leffler 
1365b032f27cSSam Leffler 	switch (w) {
1366b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_NULL):
136795d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_NONE;
136895d9a127SAndriy Voskoboinyk 		break;
1369b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WEP40):
1370b032f27cSSam Leffler 		if (keylen)
1371b032f27cSSam Leffler 			*keylen = 40 / NBBY;
137295d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_WEP;
137395d9a127SAndriy Voskoboinyk 		break;
1374b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WEP104):
1375b032f27cSSam Leffler 		if (keylen)
1376b032f27cSSam Leffler 			*keylen = 104 / NBBY;
137795d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_WEP;
137895d9a127SAndriy Voskoboinyk 		break;
1379b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_TKIP):
138095d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_TKIP;
138195d9a127SAndriy Voskoboinyk 		break;
1382b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_CCMP):
138395d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_AES_CCM;
138495d9a127SAndriy Voskoboinyk 		break;
1385b032f27cSSam Leffler 	case RSN_SEL(RSN_CSE_WRAP):
138695d9a127SAndriy Voskoboinyk 		*cipher = IEEE80211_CIPHER_AES_OCB;
138795d9a127SAndriy Voskoboinyk 		break;
1388*70dc8e5eSAdrian Chadd 	case RSN_SEL(RSN_CSE_GCMP_128):
1389*70dc8e5eSAdrian Chadd 		*cipher = IEEE80211_CIPHER_AES_GCM_128;
1390*70dc8e5eSAdrian Chadd 		break;
139195d9a127SAndriy Voskoboinyk 	default:
139295d9a127SAndriy Voskoboinyk 		return (EINVAL);
1393b032f27cSSam Leffler 	}
139495d9a127SAndriy Voskoboinyk 
139595d9a127SAndriy Voskoboinyk 	return (0);
1396b032f27cSSam Leffler #undef WPA_SEL
1397b032f27cSSam Leffler }
1398b032f27cSSam Leffler 
1399b032f27cSSam Leffler /*
1400b032f27cSSam Leffler  * Convert an RSN key management/authentication algorithm
1401b032f27cSSam Leffler  * to an internal code.
1402b032f27cSSam Leffler  */
1403b032f27cSSam Leffler static int
rsn_keymgmt(const uint8_t * sel)1404b032f27cSSam Leffler rsn_keymgmt(const uint8_t *sel)
1405b032f27cSSam Leffler {
1406b032f27cSSam Leffler #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
140731021a2bSAndriy Voskoboinyk 	uint32_t w = le32dec(sel);
1408b032f27cSSam Leffler 
1409b032f27cSSam Leffler 	switch (w) {
1410b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1411b032f27cSSam Leffler 		return RSN_ASE_8021X_UNSPEC;
1412b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_8021X_PSK):
1413b032f27cSSam Leffler 		return RSN_ASE_8021X_PSK;
1414b032f27cSSam Leffler 	case RSN_SEL(RSN_ASE_NONE):
1415b032f27cSSam Leffler 		return RSN_ASE_NONE;
1416b032f27cSSam Leffler 	}
1417b032f27cSSam Leffler 	return 0;		/* NB: so is discarded */
1418b032f27cSSam Leffler #undef RSN_SEL
1419b032f27cSSam Leffler }
1420b032f27cSSam Leffler 
1421b032f27cSSam Leffler /*
1422b032f27cSSam Leffler  * Parse a WPA/RSN information element to collect parameters
1423b032f27cSSam Leffler  * and validate the parameters against what has been
1424b032f27cSSam Leffler  * configured for the system.
1425b032f27cSSam Leffler  */
1426b032f27cSSam Leffler static int
ieee80211_parse_rsn(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)1427b032f27cSSam Leffler ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1428b032f27cSSam Leffler 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1429b032f27cSSam Leffler {
1430b032f27cSSam Leffler 	uint8_t len = frm[1];
1431b032f27cSSam Leffler 	uint32_t w;
143295d9a127SAndriy Voskoboinyk 	int error, n;
1433b032f27cSSam Leffler 
1434b032f27cSSam Leffler 	/*
1435b032f27cSSam Leffler 	 * Check the length once for fixed parts:
1436b032f27cSSam Leffler 	 * version, mcast cipher, and 2 selector counts.
1437b032f27cSSam Leffler 	 * Other, variable-length data, must be checked separately.
1438b032f27cSSam Leffler 	 */
1439b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1440b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1441b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1442b032f27cSSam Leffler 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1443b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1444b032f27cSSam Leffler 	}
144595d9a127SAndriy Voskoboinyk 	/* XXX may be shorter */
1446b032f27cSSam Leffler 	if (len < 10) {
1447b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1448b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1449b032f27cSSam Leffler 		    wh, "RSN", "too short, len %u", len);
1450b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1451b032f27cSSam Leffler 	}
1452b032f27cSSam Leffler 	frm += 2;
145331021a2bSAndriy Voskoboinyk 	w = le16dec(frm);
1454b032f27cSSam Leffler 	if (w != RSN_VERSION) {
1455b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1456b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1457b032f27cSSam Leffler 		    wh, "RSN", "bad version %u", w);
145895d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_UNSUPP_RSN_IE_VERSION;
1459b032f27cSSam Leffler 	}
1460b032f27cSSam Leffler 	frm += 2, len -= 2;
1461b032f27cSSam Leffler 
1462b032f27cSSam Leffler 	memset(rsn, 0, sizeof(*rsn));
1463b032f27cSSam Leffler 
1464b032f27cSSam Leffler 	/* multicast/group cipher */
146595d9a127SAndriy Voskoboinyk 	error = rsn_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
146695d9a127SAndriy Voskoboinyk 	if (error != 0) {
146795d9a127SAndriy Voskoboinyk 		IEEE80211_DISCARD_IE(vap,
146895d9a127SAndriy Voskoboinyk 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
146995d9a127SAndriy Voskoboinyk 		    wh, "RSN", "unknown mcast cipher suite %08X",
147095d9a127SAndriy Voskoboinyk 		    le32dec(frm));
147195d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
147295d9a127SAndriy Voskoboinyk 	}
147395d9a127SAndriy Voskoboinyk 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_NONE) {
147495d9a127SAndriy Voskoboinyk 		IEEE80211_DISCARD_IE(vap,
147595d9a127SAndriy Voskoboinyk 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
147695d9a127SAndriy Voskoboinyk 		    wh, "RSN", "invalid mcast cipher suite %d",
147795d9a127SAndriy Voskoboinyk 		    rsn->rsn_mcastcipher);
147895d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_GROUP_CIPHER_INVALID;
147995d9a127SAndriy Voskoboinyk 	}
1480b032f27cSSam Leffler 	frm += 4, len -= 4;
1481b032f27cSSam Leffler 
1482b032f27cSSam Leffler 	/* unicast ciphers */
148331021a2bSAndriy Voskoboinyk 	n = le16dec(frm);
1484b032f27cSSam Leffler 	frm += 2, len -= 2;
1485b032f27cSSam Leffler 	if (len < n*4+2) {
1486b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1487b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1488b032f27cSSam Leffler 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1489b032f27cSSam Leffler 		    len, n);
1490b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1491b032f27cSSam Leffler 	}
1492b032f27cSSam Leffler 	w = 0;
149395d9a127SAndriy Voskoboinyk 
1494b032f27cSSam Leffler 	for (; n > 0; n--) {
149595d9a127SAndriy Voskoboinyk 		uint8_t cipher;
149695d9a127SAndriy Voskoboinyk 
149795d9a127SAndriy Voskoboinyk 		error = rsn_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
149895d9a127SAndriy Voskoboinyk 		if (error == 0)
149995d9a127SAndriy Voskoboinyk 			w |= 1 << cipher;
150095d9a127SAndriy Voskoboinyk 
1501b032f27cSSam Leffler 		frm += 4, len -= 4;
1502b032f27cSSam Leffler 	}
1503*70dc8e5eSAdrian Chadd 	if (w & (1 << IEEE80211_CIPHER_AES_GCM_128))
1504*70dc8e5eSAdrian Chadd 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_GCM_128;
1505*70dc8e5eSAdrian Chadd 	else if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1506b032f27cSSam Leffler 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
150795d9a127SAndriy Voskoboinyk 	else if (w & (1 << IEEE80211_CIPHER_AES_OCB))
150895d9a127SAndriy Voskoboinyk 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_OCB;
150995d9a127SAndriy Voskoboinyk 	else if (w & (1 << IEEE80211_CIPHER_TKIP))
151095d9a127SAndriy Voskoboinyk 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
151195d9a127SAndriy Voskoboinyk 	else if ((w & (1 << IEEE80211_CIPHER_NONE)) &&
151295d9a127SAndriy Voskoboinyk 	    (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP ||
151395d9a127SAndriy Voskoboinyk 	     rsn->rsn_mcastcipher == IEEE80211_CIPHER_TKIP))
151495d9a127SAndriy Voskoboinyk 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_NONE;
151595d9a127SAndriy Voskoboinyk 	else {
151695d9a127SAndriy Voskoboinyk 		IEEE80211_DISCARD_IE(vap,
151795d9a127SAndriy Voskoboinyk 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
151895d9a127SAndriy Voskoboinyk 		    wh, "RSN", "no usable pairwise cipher suite found (w=%d)",
151995d9a127SAndriy Voskoboinyk 		    w);
152095d9a127SAndriy Voskoboinyk 		return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
152195d9a127SAndriy Voskoboinyk 	}
1522b032f27cSSam Leffler 
1523b032f27cSSam Leffler 	/* key management algorithms */
152431021a2bSAndriy Voskoboinyk 	n = le16dec(frm);
1525b032f27cSSam Leffler 	frm += 2, len -= 2;
1526b032f27cSSam Leffler 	if (len < n*4) {
1527b032f27cSSam Leffler 		IEEE80211_DISCARD_IE(vap,
1528b032f27cSSam Leffler 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1529b032f27cSSam Leffler 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1530b032f27cSSam Leffler 		    len, n);
1531b032f27cSSam Leffler 		return IEEE80211_REASON_IE_INVALID;
1532b032f27cSSam Leffler 	}
1533b032f27cSSam Leffler 	w = 0;
1534b032f27cSSam Leffler 	for (; n > 0; n--) {
1535b032f27cSSam Leffler 		w |= rsn_keymgmt(frm);
1536b032f27cSSam Leffler 		frm += 4, len -= 4;
1537b032f27cSSam Leffler 	}
1538b032f27cSSam Leffler 	if (w & RSN_ASE_8021X_UNSPEC)
1539b032f27cSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1540b032f27cSSam Leffler 	else
1541b032f27cSSam Leffler 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1542b032f27cSSam Leffler 
1543b032f27cSSam Leffler 	/* optional RSN capabilities */
15443ab5e297SAdrian Chadd 	if (len >= 2) {
154531021a2bSAndriy Voskoboinyk 		rsn->rsn_caps = le16dec(frm);
15463ab5e297SAdrian Chadd 		frm += 2, len -= 2;
15473ab5e297SAdrian Chadd 	}
15483ab5e297SAdrian Chadd 
15493ab5e297SAdrian Chadd 	/* XXX PMK Count / PMKID */
15503ab5e297SAdrian Chadd 
15513ab5e297SAdrian Chadd 	/* XXX Group Cipher Management Suite */
1552b032f27cSSam Leffler 
1553b032f27cSSam Leffler 	return 0;
1554b032f27cSSam Leffler }
1555b032f27cSSam Leffler 
1556b032f27cSSam Leffler /*
1557a4641f4eSPedro F. Giffuni  * WPA/802.11i association request processing.
1558b032f27cSSam Leffler  */
1559b032f27cSSam Leffler static int
wpa_assocreq(struct ieee80211_node * ni,struct ieee80211_rsnparms * rsnparms,const struct ieee80211_frame * wh,const uint8_t * wpa,const uint8_t * rsn,uint16_t capinfo)1560b032f27cSSam Leffler wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1561b032f27cSSam Leffler 	const struct ieee80211_frame *wh, const uint8_t *wpa,
1562b032f27cSSam Leffler 	const uint8_t *rsn, uint16_t capinfo)
1563b032f27cSSam Leffler {
1564b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1565b032f27cSSam Leffler 	uint8_t reason;
1566b032f27cSSam Leffler 	int badwparsn;
1567b032f27cSSam Leffler 
1568b032f27cSSam Leffler 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1569b032f27cSSam Leffler 	if (wpa == NULL && rsn == NULL) {
1570b032f27cSSam Leffler 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1571b032f27cSSam Leffler 			/*
1572b032f27cSSam Leffler 			 * W-Fi Protected Setup (WPS) permits
1573b032f27cSSam Leffler 			 * clients to associate and pass EAPOL frames
1574b032f27cSSam Leffler 			 * to establish initial credentials.
1575b032f27cSSam Leffler 			 */
1576b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_WPS;
1577b032f27cSSam Leffler 			return 1;
1578b032f27cSSam Leffler 		}
1579b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1580b032f27cSSam Leffler 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1581b032f27cSSam Leffler 			/*
1582b032f27cSSam Leffler 			 * Transitional Security Network.  Permits clients
1583b032f27cSSam Leffler 			 * to associate and use WEP while WPA is configured.
1584b032f27cSSam Leffler 			 */
1585b032f27cSSam Leffler 			ni->ni_flags |= IEEE80211_NODE_TSN;
1586b032f27cSSam Leffler 			return 1;
1587b032f27cSSam Leffler 		}
1588b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1589b032f27cSSam Leffler 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1590b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1591b032f27cSSam Leffler 		reason = IEEE80211_REASON_IE_INVALID;
1592b032f27cSSam Leffler 		goto bad;
1593b032f27cSSam Leffler 	}
1594b032f27cSSam Leffler 	/* assert right association security credentials */
1595b032f27cSSam Leffler 	badwparsn = 0;			/* NB: to silence compiler */
1596b032f27cSSam Leffler 	switch (vap->iv_flags & IEEE80211_F_WPA) {
1597b032f27cSSam Leffler 	case IEEE80211_F_WPA1:
1598b032f27cSSam Leffler 		badwparsn = (wpa == NULL);
1599b032f27cSSam Leffler 		break;
1600b032f27cSSam Leffler 	case IEEE80211_F_WPA2:
1601b032f27cSSam Leffler 		badwparsn = (rsn == NULL);
1602b032f27cSSam Leffler 		break;
1603b032f27cSSam Leffler 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1604b032f27cSSam Leffler 		badwparsn = (wpa == NULL && rsn == NULL);
1605b032f27cSSam Leffler 		break;
1606b032f27cSSam Leffler 	}
1607b032f27cSSam Leffler 	if (badwparsn) {
1608b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1609b032f27cSSam Leffler 		    wh, NULL,
1610b032f27cSSam Leffler 		    "%s", "missing WPA/RSN IE in association request");
1611b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1612b032f27cSSam Leffler 		reason = IEEE80211_REASON_IE_INVALID;
1613b032f27cSSam Leffler 		goto bad;
1614b032f27cSSam Leffler 	}
1615b032f27cSSam Leffler 	/*
1616b032f27cSSam Leffler 	 * Parse WPA/RSN information element.
1617b032f27cSSam Leffler 	 */
1618b032f27cSSam Leffler 	if (wpa != NULL)
1619b032f27cSSam Leffler 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1620b032f27cSSam Leffler 	else
1621b032f27cSSam Leffler 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1622b032f27cSSam Leffler 	if (reason != 0) {
162395d9a127SAndriy Voskoboinyk 		/* XXX wpa->rsn fallback? */
1624b032f27cSSam Leffler 		/* XXX distinguish WPA/RSN? */
1625b032f27cSSam Leffler 		vap->iv_stats.is_rx_assoc_badwpaie++;
1626b032f27cSSam Leffler 		goto bad;
1627b032f27cSSam Leffler 	}
1628b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1629b032f27cSSam Leffler 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1630b032f27cSSam Leffler 	    wpa != NULL ? "WPA" : "RSN",
1631b032f27cSSam Leffler 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1632b032f27cSSam Leffler 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1633b032f27cSSam Leffler 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1634b032f27cSSam Leffler 
1635b032f27cSSam Leffler 	return 1;
1636b032f27cSSam Leffler bad:
1637b032f27cSSam Leffler 	ieee80211_node_deauth(ni, reason);
1638b032f27cSSam Leffler 	return 0;
1639b032f27cSSam Leffler }
1640b032f27cSSam Leffler 
1641b032f27cSSam Leffler /* XXX find a better place for definition */
1642b032f27cSSam Leffler struct l2_update_frame {
1643b032f27cSSam Leffler 	struct ether_header eh;
1644b032f27cSSam Leffler 	uint8_t dsap;
1645b032f27cSSam Leffler 	uint8_t ssap;
1646b032f27cSSam Leffler 	uint8_t control;
1647b032f27cSSam Leffler 	uint8_t xid[3];
1648b032f27cSSam Leffler }  __packed;
1649b032f27cSSam Leffler 
1650b032f27cSSam Leffler /*
1651b032f27cSSam Leffler  * Deliver a TGf L2UF frame on behalf of a station.
1652b032f27cSSam Leffler  * This primes any bridge when the station is roaming
1653b032f27cSSam Leffler  * between ap's on the same wired network.
1654b032f27cSSam Leffler  */
1655b032f27cSSam Leffler static void
ieee80211_deliver_l2uf(struct ieee80211_node * ni)1656b032f27cSSam Leffler ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1657b032f27cSSam Leffler {
1658b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1659b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
1660b032f27cSSam Leffler 	struct mbuf *m;
1661b032f27cSSam Leffler 	struct l2_update_frame *l2uf;
1662b032f27cSSam Leffler 	struct ether_header *eh;
1663b032f27cSSam Leffler 
1664bd29f817SBjoern A. Zeeb 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
1665b032f27cSSam Leffler 	if (m == NULL) {
1666b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1667b032f27cSSam Leffler 		    "%s", "no mbuf for l2uf frame");
1668b032f27cSSam Leffler 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1669b032f27cSSam Leffler 		return;
1670b032f27cSSam Leffler 	}
1671b032f27cSSam Leffler 	l2uf = mtod(m, struct l2_update_frame *);
1672b032f27cSSam Leffler 	eh = &l2uf->eh;
1673b032f27cSSam Leffler 	/* dst: Broadcast address */
1674b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1675b032f27cSSam Leffler 	/* src: associated STA */
1676b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1677b032f27cSSam Leffler 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1678b032f27cSSam Leffler 
1679b032f27cSSam Leffler 	l2uf->dsap = 0;
1680b032f27cSSam Leffler 	l2uf->ssap = 0;
1681b032f27cSSam Leffler 	l2uf->control = 0xf5;
1682b032f27cSSam Leffler 	l2uf->xid[0] = 0x81;
1683b032f27cSSam Leffler 	l2uf->xid[1] = 0x80;
1684b032f27cSSam Leffler 	l2uf->xid[2] = 0x00;
1685b032f27cSSam Leffler 
1686b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1687b032f27cSSam Leffler 	hostap_deliver_data(vap, ni, m);
1688b032f27cSSam Leffler }
1689b032f27cSSam Leffler 
1690b032f27cSSam Leffler static void
ratesetmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int rate)1691b032f27cSSam Leffler ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1692b032f27cSSam Leffler 	int reassoc, int resp, const char *tag, int rate)
1693b032f27cSSam Leffler {
1694b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1695b032f27cSSam Leffler 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1696b032f27cSSam Leffler 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1697b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1698b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1699b032f27cSSam Leffler }
1700b032f27cSSam Leffler 
1701b032f27cSSam Leffler static void
capinfomismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int capinfo)1702b032f27cSSam Leffler capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1703b032f27cSSam Leffler 	int reassoc, int resp, const char *tag, int capinfo)
1704b032f27cSSam Leffler {
1705b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1706b032f27cSSam Leffler 
1707b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1708b032f27cSSam Leffler 	    "deny %s request, %s mismatch 0x%x",
1709b032f27cSSam Leffler 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1710b032f27cSSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1711b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1712b032f27cSSam Leffler 	vap->iv_stats.is_rx_assoc_capmismatch++;
1713b032f27cSSam Leffler }
1714b032f27cSSam Leffler 
1715b032f27cSSam Leffler static void
htcapmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp)1716b032f27cSSam Leffler htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1717b032f27cSSam Leffler 	int reassoc, int resp)
1718b032f27cSSam Leffler {
1719b032f27cSSam Leffler 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1720b032f27cSSam Leffler 	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1721b032f27cSSam Leffler 	/* XXX no better code */
1722b8ee2a22SSam Leffler 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1723b032f27cSSam Leffler 	ieee80211_node_leave(ni);
1724b032f27cSSam Leffler }
1725b032f27cSSam Leffler 
1726b032f27cSSam Leffler static void
authalgreject(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int algo,int seq,int status)1727b032f27cSSam Leffler authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1728b032f27cSSam Leffler 	int algo, int seq, int status)
1729b032f27cSSam Leffler {
1730b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1731b032f27cSSam Leffler 
1732b032f27cSSam Leffler 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1733b032f27cSSam Leffler 	    wh, NULL, "unsupported alg %d", algo);
1734b032f27cSSam Leffler 	vap->iv_stats.is_rx_auth_unsupported++;
1735b032f27cSSam Leffler 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1736b032f27cSSam Leffler 	    seq | (status << 16));
1737b032f27cSSam Leffler }
1738b032f27cSSam Leffler 
1739b032f27cSSam Leffler static __inline int
ishtmixed(const uint8_t * ie)1740b032f27cSSam Leffler ishtmixed(const uint8_t *ie)
1741b032f27cSSam Leffler {
1742b032f27cSSam Leffler 	const struct ieee80211_ie_htinfo *ht =
1743b032f27cSSam Leffler 	    (const struct ieee80211_ie_htinfo *) ie;
1744b032f27cSSam Leffler 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1745b032f27cSSam Leffler 	    IEEE80211_HTINFO_OPMODE_MIXED;
1746b032f27cSSam Leffler }
1747b032f27cSSam Leffler 
1748b032f27cSSam Leffler static int
is11bclient(const uint8_t * rates,const uint8_t * xrates)1749b032f27cSSam Leffler is11bclient(const uint8_t *rates, const uint8_t *xrates)
1750b032f27cSSam Leffler {
1751b032f27cSSam Leffler 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1752b032f27cSSam Leffler 	int i;
1753b032f27cSSam Leffler 
1754b032f27cSSam Leffler 	/* NB: the 11b clients we care about will not have xrates */
1755b032f27cSSam Leffler 	if (xrates != NULL || rates == NULL)
1756b032f27cSSam Leffler 		return 0;
1757b032f27cSSam Leffler 	for (i = 0; i < rates[1]; i++) {
1758b032f27cSSam Leffler 		int r = rates[2+i] & IEEE80211_RATE_VAL;
1759b032f27cSSam Leffler 		if (r > 2*11 || ((1<<r) & brates) == 0)
1760b032f27cSSam Leffler 			return 0;
1761b032f27cSSam Leffler 	}
1762b032f27cSSam Leffler 	return 1;
1763b032f27cSSam Leffler }
1764b032f27cSSam Leffler 
1765*70dc8e5eSAdrian Chadd /**
1766*70dc8e5eSAdrian Chadd  * Check if the given cipher is valid for 802.11 HT operation.
1767*70dc8e5eSAdrian Chadd  *
1768*70dc8e5eSAdrian Chadd  * The 802.11 specification only allows HT A-MPDU to be performed
1769*70dc8e5eSAdrian Chadd  * on CCMP / GCMP encrypted frames.  The WEP/TKIP hardware crypto
1770*70dc8e5eSAdrian Chadd  * implementations may not meet the timing required for A-MPDU
1771*70dc8e5eSAdrian Chadd  * operation.
1772*70dc8e5eSAdrian Chadd  *
1773*70dc8e5eSAdrian Chadd  * @param cipher	the IEEE80211_CIPHER_ value to check
1774*70dc8e5eSAdrian Chadd  * @returns	true if the cipher is valid for HT A-MPDU, false otherwise
1775*70dc8e5eSAdrian Chadd  */
1776*70dc8e5eSAdrian Chadd static bool
hostapd_validate_cipher_for_ht_ampdu(uint8_t cipher)1777*70dc8e5eSAdrian Chadd hostapd_validate_cipher_for_ht_ampdu(uint8_t cipher)
1778*70dc8e5eSAdrian Chadd {
1779*70dc8e5eSAdrian Chadd 	switch (cipher) {
1780*70dc8e5eSAdrian Chadd 	case IEEE80211_CIPHER_AES_CCM:
1781*70dc8e5eSAdrian Chadd 	case IEEE80211_CIPHER_AES_GCM_128:
1782*70dc8e5eSAdrian Chadd 		return true;
1783*70dc8e5eSAdrian Chadd 	default:
1784*70dc8e5eSAdrian Chadd 		return false;
1785*70dc8e5eSAdrian Chadd 	}
1786*70dc8e5eSAdrian Chadd }
1787*70dc8e5eSAdrian Chadd 
1788b032f27cSSam Leffler static void
hostap_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)1789b032f27cSSam Leffler hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1790c79f192cSAdrian Chadd 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1791b032f27cSSam Leffler {
1792b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1793b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1794b032f27cSSam Leffler 	struct ieee80211_frame *wh;
1795b032f27cSSam Leffler 	uint8_t *frm, *efrm, *sfrm;
1796b032f27cSSam Leffler 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
179751172f62SAdrian Chadd 	uint8_t *vhtcap, *vhtinfo;
1798b032f27cSSam Leffler 	int reassoc, resp;
1799b032f27cSSam Leffler 	uint8_t rate;
1800b032f27cSSam Leffler 
1801b032f27cSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
1802b032f27cSSam Leffler 	frm = (uint8_t *)&wh[1];
1803b032f27cSSam Leffler 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1804b032f27cSSam Leffler 	switch (subtype) {
1805b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1806b032f27cSSam Leffler 		/*
1807b032f27cSSam Leffler 		 * We process beacon/probe response frames when scanning;
1808b032f27cSSam Leffler 		 * otherwise we check beacon frames for overlapping non-ERP
1809b032f27cSSam Leffler 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1810b032f27cSSam Leffler 		 */
18114ba33fd1SAndriy Voskoboinyk 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1812b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1813b032f27cSSam Leffler 			return;
1814b032f27cSSam Leffler 		}
18154ba33fd1SAndriy Voskoboinyk 		/* FALLTHROUGH */
18164ba33fd1SAndriy Voskoboinyk 	case IEEE80211_FC0_SUBTYPE_BEACON: {
18174ba33fd1SAndriy Voskoboinyk 		struct ieee80211_scanparams scan;
18184ba33fd1SAndriy Voskoboinyk 
1819b032f27cSSam Leffler 		/* NB: accept off-channel frames */
1820c79f192cSAdrian Chadd 		/* XXX TODO: use rxstatus to determine off-channel details */
1821c79f192cSAdrian Chadd 		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1822b032f27cSSam Leffler 			return;
1823b032f27cSSam Leffler 		/*
1824b032f27cSSam Leffler 		 * Count frame now that we know it's to be processed.
1825b032f27cSSam Leffler 		 */
1826b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1827b032f27cSSam Leffler 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1828b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
1829b032f27cSSam Leffler 		} else
1830b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1831b032f27cSSam Leffler 		/*
1832b032f27cSSam Leffler 		 * If scanning, just pass information to the scan module.
1833b032f27cSSam Leffler 		 */
1834b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1835b032f27cSSam Leffler 			if (scan.status == 0 &&		/* NB: on channel */
1836b032f27cSSam Leffler 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1837b032f27cSSam Leffler 				/*
1838b032f27cSSam Leffler 				 * Actively scanning a channel marked passive;
1839b032f27cSSam Leffler 				 * send a probe request now that we know there
1840b032f27cSSam Leffler 				 * is 802.11 traffic present.
1841b032f27cSSam Leffler 				 *
1842b032f27cSSam Leffler 				 * XXX check if the beacon we recv'd gives
1843b032f27cSSam Leffler 				 * us what we need and suppress the probe req
1844b032f27cSSam Leffler 				 */
18459776aba3SBjoern A. Zeeb 				ieee80211_probe_curchan(vap, true);
1846b032f27cSSam Leffler 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1847b032f27cSSam Leffler 			}
18482808a02bSAdrian Chadd 			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
18492808a02bSAdrian Chadd 			    subtype, rssi, nf);
1850b032f27cSSam Leffler 			return;
1851b032f27cSSam Leffler 		}
1852b032f27cSSam Leffler 		/*
1853b032f27cSSam Leffler 		 * Check beacon for overlapping bss w/ non ERP stations.
1854b032f27cSSam Leffler 		 * If we detect one and protection is configured but not
1855b032f27cSSam Leffler 		 * enabled, enable it and start a timer that'll bring us
1856b032f27cSSam Leffler 		 * out if we stop seeing the bss.
1857b032f27cSSam Leffler 		 */
1858b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1859b032f27cSSam Leffler 		    scan.status == 0 &&			/* NB: on-channel */
1860b032f27cSSam Leffler 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1861b032f27cSSam Leffler 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1862f1481c8dSAdrian Chadd 			vap->iv_lastnonerp = ticks;
1863f1481c8dSAdrian Chadd 			vap->iv_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1864f1481c8dSAdrian Chadd 			/*
1865f1481c8dSAdrian Chadd 			 * XXX TODO: this may need to check all VAPs?
1866f1481c8dSAdrian Chadd 			 */
1867f1481c8dSAdrian Chadd 			if (vap->iv_protmode != IEEE80211_PROT_NONE &&
1868f1481c8dSAdrian Chadd 			    (vap->iv_flags & IEEE80211_F_USEPROT) == 0) {
1869b032f27cSSam Leffler 				IEEE80211_NOTE_FRAME(vap,
1870b032f27cSSam Leffler 				    IEEE80211_MSG_ASSOC, wh,
1871b032f27cSSam Leffler 				    "non-ERP present on channel %d "
1872b032f27cSSam Leffler 				    "(saw erp 0x%x from channel %d), "
1873b032f27cSSam Leffler 				    "enable use of protection",
1874b032f27cSSam Leffler 				    ic->ic_curchan->ic_ieee,
1875b032f27cSSam Leffler 				    scan.erp, scan.chan);
1876f1481c8dSAdrian Chadd 				vap->iv_flags |= IEEE80211_F_USEPROT;
1877f1481c8dSAdrian Chadd 				ieee80211_vap_update_erp_protmode(vap);
1878b032f27cSSam Leffler 			}
1879b032f27cSSam Leffler 		}
1880b032f27cSSam Leffler 		/*
1881b032f27cSSam Leffler 		 * Check beacon for non-HT station on HT channel
1882b032f27cSSam Leffler 		 * and update HT BSS occupancy as appropriate.
1883b032f27cSSam Leffler 		 */
1884b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1885b032f27cSSam Leffler 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1886b032f27cSSam Leffler 				/*
1887b032f27cSSam Leffler 				 * Off control channel; only check frames
1888b032f27cSSam Leffler 				 * that come in the extension channel when
1889b032f27cSSam Leffler 				 * operating w/ HT40.
1890b032f27cSSam Leffler 				 */
1891b032f27cSSam Leffler 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1892b032f27cSSam Leffler 					break;
1893b032f27cSSam Leffler 				if (scan.chan != ic->ic_curchan->ic_extieee)
1894b032f27cSSam Leffler 					break;
1895b032f27cSSam Leffler 			}
1896b032f27cSSam Leffler 			if (scan.htinfo == NULL) {
1897f1481c8dSAdrian Chadd 				ieee80211_htprot_update(vap,
1898b032f27cSSam Leffler 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1899b032f27cSSam Leffler 				    IEEE80211_HTINFO_NONHT_PRESENT);
1900b032f27cSSam Leffler 			} else if (ishtmixed(scan.htinfo)) {
1901b032f27cSSam Leffler 				/* XXX? take NONHT_PRESENT from beacon? */
1902f1481c8dSAdrian Chadd 				ieee80211_htprot_update(vap,
1903b032f27cSSam Leffler 				    IEEE80211_HTINFO_OPMODE_MIXED |
1904b032f27cSSam Leffler 				    IEEE80211_HTINFO_NONHT_PRESENT);
1905b032f27cSSam Leffler 			}
1906b032f27cSSam Leffler 		}
1907b032f27cSSam Leffler 		break;
1908b032f27cSSam Leffler 	}
1909b032f27cSSam Leffler 
1910b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1911b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
1912b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1913b032f27cSSam Leffler 			return;
1914b032f27cSSam Leffler 		}
1915b032f27cSSam Leffler 		/*
1916957458a8SAdrian Chadd 		 * Consult the ACL policy module if setup.
1917957458a8SAdrian Chadd 		 */
19185a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1919957458a8SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1920957458a8SAdrian Chadd 			    wh, NULL, "%s", "disallowed by ACL");
1921957458a8SAdrian Chadd 			vap->iv_stats.is_rx_acl++;
1922957458a8SAdrian Chadd 			return;
1923957458a8SAdrian Chadd 		}
1924957458a8SAdrian Chadd 		/*
1925b032f27cSSam Leffler 		 * prreq frame format
1926b032f27cSSam Leffler 		 *	[tlv] ssid
1927b032f27cSSam Leffler 		 *	[tlv] supported rates
1928b032f27cSSam Leffler 		 *	[tlv] extended supported rates
1929b032f27cSSam Leffler 		 */
1930b032f27cSSam Leffler 		ssid = rates = xrates = NULL;
1931b032f27cSSam Leffler 		while (efrm - frm > 1) {
1932b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1933b032f27cSSam Leffler 			switch (*frm) {
1934b032f27cSSam Leffler 			case IEEE80211_ELEMID_SSID:
1935b032f27cSSam Leffler 				ssid = frm;
1936b032f27cSSam Leffler 				break;
1937b032f27cSSam Leffler 			case IEEE80211_ELEMID_RATES:
1938b032f27cSSam Leffler 				rates = frm;
1939b032f27cSSam Leffler 				break;
1940b032f27cSSam Leffler 			case IEEE80211_ELEMID_XRATES:
1941b032f27cSSam Leffler 				xrates = frm;
1942b032f27cSSam Leffler 				break;
1943b032f27cSSam Leffler 			}
1944b032f27cSSam Leffler 			frm += frm[1] + 2;
1945b032f27cSSam Leffler 		}
1946b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1947b032f27cSSam Leffler 		if (xrates != NULL)
1948b032f27cSSam Leffler 			IEEE80211_VERIFY_ELEMENT(xrates,
1949b032f27cSSam Leffler 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1950b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1951b032f27cSSam Leffler 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1952b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1953b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1954b032f27cSSam Leffler 			    wh, NULL,
1955b032f27cSSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
1956b032f27cSSam Leffler 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1957b032f27cSSam Leffler 			return;
1958b032f27cSSam Leffler 		}
1959b032f27cSSam Leffler 
1960b032f27cSSam Leffler 		/* XXX find a better class or define it's own */
1961b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1962b032f27cSSam Leffler 		    "%s", "recv probe req");
1963b032f27cSSam Leffler 		/*
1964b032f27cSSam Leffler 		 * Some legacy 11b clients cannot hack a complete
1965b032f27cSSam Leffler 		 * probe response frame.  When the request includes
1966b032f27cSSam Leffler 		 * only a bare-bones rate set, communicate this to
1967b032f27cSSam Leffler 		 * the transmit side.
1968b032f27cSSam Leffler 		 */
1969b032f27cSSam Leffler 		ieee80211_send_proberesp(vap, wh->i_addr2,
1970b032f27cSSam Leffler 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1971b032f27cSSam Leffler 		break;
1972b032f27cSSam Leffler 
1973b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1974b032f27cSSam Leffler 		uint16_t algo, seq, status;
1975b032f27cSSam Leffler 
1976b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
1977b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
1978b032f27cSSam Leffler 			return;
1979b032f27cSSam Leffler 		}
1980b032f27cSSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1981b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1982b032f27cSSam Leffler 			    wh, NULL, "%s", "wrong bssid");
1983b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1984b032f27cSSam Leffler 			return;
1985b032f27cSSam Leffler 		}
1986b032f27cSSam Leffler 		/*
1987b032f27cSSam Leffler 		 * auth frame format
1988b032f27cSSam Leffler 		 *	[2] algorithm
1989b032f27cSSam Leffler 		 *	[2] sequence
1990b032f27cSSam Leffler 		 *	[2] status
1991b032f27cSSam Leffler 		 *	[tlv*] challenge
1992b032f27cSSam Leffler 		 */
1993b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1994b032f27cSSam Leffler 		algo   = le16toh(*(uint16_t *)frm);
1995b032f27cSSam Leffler 		seq    = le16toh(*(uint16_t *)(frm + 2));
1996b032f27cSSam Leffler 		status = le16toh(*(uint16_t *)(frm + 4));
1997b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1998b032f27cSSam Leffler 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1999b032f27cSSam Leffler 		/*
2000b032f27cSSam Leffler 		 * Consult the ACL policy module if setup.
2001b032f27cSSam Leffler 		 */
20025a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
2003b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
2004b032f27cSSam Leffler 			    wh, NULL, "%s", "disallowed by ACL");
2005b032f27cSSam Leffler 			vap->iv_stats.is_rx_acl++;
2006b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
2007b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_AUTH,
2008b032f27cSSam Leffler 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
2009b032f27cSSam Leffler 			return;
2010b032f27cSSam Leffler 		}
2011b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
2012b032f27cSSam Leffler 			IEEE80211_DISCARD(vap,
2013b032f27cSSam Leffler 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
2014b032f27cSSam Leffler 			    wh, NULL, "%s", "TKIP countermeasures enabled");
2015b032f27cSSam Leffler 			vap->iv_stats.is_rx_auth_countermeasures++;
2016b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
2017b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_AUTH,
2018b032f27cSSam Leffler 				IEEE80211_REASON_MIC_FAILURE);
2019b032f27cSSam Leffler 			return;
2020b032f27cSSam Leffler 		}
2021b032f27cSSam Leffler 		if (algo == IEEE80211_AUTH_ALG_SHARED)
20225463c4a4SSam Leffler 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
2023b032f27cSSam Leffler 			    seq, status);
20245463c4a4SSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
20255463c4a4SSam Leffler 			hostap_auth_open(ni, wh, rssi, nf, seq, status);
2026b032f27cSSam Leffler 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
2027b032f27cSSam Leffler 			authalgreject(ni, wh, algo,
2028b032f27cSSam Leffler 			    seq+1, IEEE80211_STATUS_ALG);
2029b032f27cSSam Leffler 			return;
2030b032f27cSSam Leffler 		} else {
2031b032f27cSSam Leffler 			/*
2032b032f27cSSam Leffler 			 * We assume that an unknown algorithm is the result
2033b032f27cSSam Leffler 			 * of a decryption failure on a shared key auth frame;
2034b032f27cSSam Leffler 			 * return a status code appropriate for that instead
2035b032f27cSSam Leffler 			 * of IEEE80211_STATUS_ALG.
2036b032f27cSSam Leffler 			 *
2037b032f27cSSam Leffler 			 * NB: a seq# of 4 is intentional; the decrypted
2038b032f27cSSam Leffler 			 *     frame likely has a bogus seq value.
2039b032f27cSSam Leffler 			 */
2040b032f27cSSam Leffler 			authalgreject(ni, wh, algo,
2041b032f27cSSam Leffler 			    4, IEEE80211_STATUS_CHALLENGE);
2042b032f27cSSam Leffler 			return;
2043b032f27cSSam Leffler 		}
2044b032f27cSSam Leffler 		break;
2045b032f27cSSam Leffler 	}
2046b032f27cSSam Leffler 
2047b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2048b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
2049b032f27cSSam Leffler 		uint16_t capinfo, lintval;
2050b032f27cSSam Leffler 		struct ieee80211_rsnparms rsnparms;
2051b032f27cSSam Leffler 
2052b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
2053b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
2054b032f27cSSam Leffler 			return;
2055b032f27cSSam Leffler 		}
2056b032f27cSSam Leffler 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
2057b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2058b032f27cSSam Leffler 			    wh, NULL, "%s", "wrong bssid");
2059b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_bss++;
2060b032f27cSSam Leffler 			return;
2061b032f27cSSam Leffler 		}
2062b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2063b032f27cSSam Leffler 			reassoc = 1;
2064b032f27cSSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
2065b032f27cSSam Leffler 		} else {
2066b032f27cSSam Leffler 			reassoc = 0;
2067b032f27cSSam Leffler 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
2068b032f27cSSam Leffler 		}
2069b032f27cSSam Leffler 		if (ni == vap->iv_bss) {
2070b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
2071b032f27cSSam Leffler 			    "deny %s request, sta not authenticated",
2072b032f27cSSam Leffler 			    reassoc ? "reassoc" : "assoc");
2073b032f27cSSam Leffler 			ieee80211_send_error(ni, wh->i_addr2,
2074b032f27cSSam Leffler 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2075b032f27cSSam Leffler 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
2076b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_notauth++;
2077b032f27cSSam Leffler 			return;
2078b032f27cSSam Leffler 		}
2079b032f27cSSam Leffler 
2080b032f27cSSam Leffler 		/*
2081b032f27cSSam Leffler 		 * asreq frame format
2082b032f27cSSam Leffler 		 *	[2] capability information
2083b032f27cSSam Leffler 		 *	[2] listen interval
2084b032f27cSSam Leffler 		 *	[6*] current AP address (reassoc only)
2085b032f27cSSam Leffler 		 *	[tlv] ssid
2086b032f27cSSam Leffler 		 *	[tlv] supported rates
2087b032f27cSSam Leffler 		 *	[tlv] extended supported rates
2088b032f27cSSam Leffler 		 *	[tlv] WPA or RSN
2089b032f27cSSam Leffler 		 *	[tlv] HT capabilities
2090b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities
2091b032f27cSSam Leffler 		 */
2092b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
2093b032f27cSSam Leffler 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
2094b032f27cSSam Leffler 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
2095b032f27cSSam Leffler 		if (reassoc)
2096b032f27cSSam Leffler 			frm += 6;	/* ignore current AP info */
2097b032f27cSSam Leffler 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
209851172f62SAdrian Chadd 		vhtcap = vhtinfo = NULL;
2099b032f27cSSam Leffler 		sfrm = frm;
2100b032f27cSSam Leffler 		while (efrm - frm > 1) {
2101b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2102b032f27cSSam Leffler 			switch (*frm) {
2103b032f27cSSam Leffler 			case IEEE80211_ELEMID_SSID:
2104b032f27cSSam Leffler 				ssid = frm;
2105b032f27cSSam Leffler 				break;
2106b032f27cSSam Leffler 			case IEEE80211_ELEMID_RATES:
2107b032f27cSSam Leffler 				rates = frm;
2108b032f27cSSam Leffler 				break;
2109b032f27cSSam Leffler 			case IEEE80211_ELEMID_XRATES:
2110b032f27cSSam Leffler 				xrates = frm;
2111b032f27cSSam Leffler 				break;
2112b032f27cSSam Leffler 			case IEEE80211_ELEMID_RSN:
2113b032f27cSSam Leffler 				rsn = frm;
2114b032f27cSSam Leffler 				break;
2115b032f27cSSam Leffler 			case IEEE80211_ELEMID_HTCAP:
2116b032f27cSSam Leffler 				htcap = frm;
2117b032f27cSSam Leffler 				break;
211851172f62SAdrian Chadd 			case IEEE80211_ELEMID_VHT_CAP:
211951172f62SAdrian Chadd 				vhtcap = frm;
212051172f62SAdrian Chadd 				break;
212151172f62SAdrian Chadd 			case IEEE80211_ELEMID_VHT_OPMODE:
212251172f62SAdrian Chadd 				vhtinfo = frm;
212351172f62SAdrian Chadd 				break;
2124b032f27cSSam Leffler 			case IEEE80211_ELEMID_VENDOR:
2125b032f27cSSam Leffler 				if (iswpaoui(frm))
2126b032f27cSSam Leffler 					wpa = frm;
2127b032f27cSSam Leffler 				else if (iswmeinfo(frm))
2128b032f27cSSam Leffler 					wme = frm;
2129616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2130b032f27cSSam Leffler 				else if (isatherosoui(frm))
2131b032f27cSSam Leffler 					ath = frm;
2132616190d0SSam Leffler #endif
21332bfc8a91SSam Leffler 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2134b032f27cSSam Leffler 					if (ishtcapoui(frm) && htcap == NULL)
2135b032f27cSSam Leffler 						htcap = frm;
2136b032f27cSSam Leffler 				}
2137b032f27cSSam Leffler 				break;
2138b032f27cSSam Leffler 			}
2139b032f27cSSam Leffler 			frm += frm[1] + 2;
2140b032f27cSSam Leffler 		}
2141b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2142b032f27cSSam Leffler 		if (xrates != NULL)
2143b032f27cSSam Leffler 			IEEE80211_VERIFY_ELEMENT(xrates,
2144b032f27cSSam Leffler 				IEEE80211_RATE_MAXSIZE - rates[1], return);
2145b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2146b032f27cSSam Leffler 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2147b032f27cSSam Leffler 		if (htcap != NULL) {
2148b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(htcap[1],
2149b032f27cSSam Leffler 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2150b032f27cSSam Leffler 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2151b032f27cSSam Leffler 			         sizeof(struct ieee80211_ie_htcap)-2,
2152b032f27cSSam Leffler 			     return);		/* XXX just NULL out? */
2153b032f27cSSam Leffler 		}
2154b032f27cSSam Leffler 
215546788bb1SAdrian Chadd 		/* Validate VHT IEs */
215646788bb1SAdrian Chadd 		if (vhtcap != NULL) {
215746788bb1SAdrian Chadd 			IEEE80211_VERIFY_LENGTH(vhtcap[1],
2158e85eb4c8SBjoern A. Zeeb 			    sizeof(struct ieee80211_vht_cap),
215946788bb1SAdrian Chadd 			    return);
216046788bb1SAdrian Chadd 		}
216146788bb1SAdrian Chadd 		if (vhtinfo != NULL) {
216246788bb1SAdrian Chadd 			IEEE80211_VERIFY_LENGTH(vhtinfo[1],
2163e85eb4c8SBjoern A. Zeeb 			    sizeof(struct ieee80211_vht_operation),
216446788bb1SAdrian Chadd 			    return);
216546788bb1SAdrian Chadd 		}
21667f19273cSAdrian Chadd 
2167b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2168b032f27cSSam Leffler 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2169b032f27cSSam Leffler 			return;
2170b032f27cSSam Leffler 		/* discard challenge after association */
2171b032f27cSSam Leffler 		if (ni->ni_challenge != NULL) {
2172b9b53389SAdrian Chadd 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2173b032f27cSSam Leffler 			ni->ni_challenge = NULL;
2174b032f27cSSam Leffler 		}
2175b032f27cSSam Leffler 		/* NB: 802.11 spec says to ignore station's privacy bit */
2176b032f27cSSam Leffler 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2177b032f27cSSam Leffler 			capinfomismatch(ni, wh, reassoc, resp,
2178b032f27cSSam Leffler 			    "capability", capinfo);
2179b032f27cSSam Leffler 			return;
2180b032f27cSSam Leffler 		}
2181b032f27cSSam Leffler 		/*
2182b032f27cSSam Leffler 		 * Disallow re-associate w/ invalid slot time setting.
2183b032f27cSSam Leffler 		 */
2184b032f27cSSam Leffler 		if (ni->ni_associd != 0 &&
2185b032f27cSSam Leffler 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2186b032f27cSSam Leffler 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2187b032f27cSSam Leffler 			capinfomismatch(ni, wh, reassoc, resp,
2188b032f27cSSam Leffler 			    "slot time", capinfo);
2189b032f27cSSam Leffler 			return;
2190b032f27cSSam Leffler 		}
2191b032f27cSSam Leffler 		rate = ieee80211_setup_rates(ni, rates, xrates,
2192b032f27cSSam Leffler 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2193b032f27cSSam Leffler 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2194b032f27cSSam Leffler 		if (rate & IEEE80211_RATE_BASIC) {
2195b032f27cSSam Leffler 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2196b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_norate++;
2197b032f27cSSam Leffler 			return;
2198b032f27cSSam Leffler 		}
2199b032f27cSSam Leffler 		/*
2200b032f27cSSam Leffler 		 * If constrained to 11g-only stations reject an
2201b032f27cSSam Leffler 		 * 11b-only station.  We cheat a bit here by looking
2202b032f27cSSam Leffler 		 * at the max negotiated xmit rate and assuming anyone
2203b032f27cSSam Leffler 		 * with a best rate <24Mb/s is an 11b station.
2204b032f27cSSam Leffler 		 */
2205b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2206b032f27cSSam Leffler 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2207b032f27cSSam Leffler 			vap->iv_stats.is_rx_assoc_norate++;
2208b032f27cSSam Leffler 			return;
2209b032f27cSSam Leffler 		}
221051172f62SAdrian Chadd 
2211b032f27cSSam Leffler 		/*
2212b032f27cSSam Leffler 		 * Do HT rate set handling and setup HT node state.
2213b032f27cSSam Leffler 		 */
2214b032f27cSSam Leffler 		ni->ni_chan = vap->iv_bss->ni_chan;
221551172f62SAdrian Chadd 
221651172f62SAdrian Chadd 		/* VHT */
221720235662SAdrian Chadd 		if (IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
22187f19273cSAdrian Chadd 		    vhtcap != NULL &&
221920235662SAdrian Chadd 		    vhtinfo != NULL) {
222051172f62SAdrian Chadd 			/* XXX TODO; see below */
222151172f62SAdrian Chadd 			printf("%s: VHT TODO!\n", __func__);
222251172f62SAdrian Chadd 			ieee80211_vht_node_init(ni);
222351172f62SAdrian Chadd 			ieee80211_vht_update_cap(ni, vhtcap, vhtinfo);
222451172f62SAdrian Chadd 		} else if (ni->ni_flags & IEEE80211_NODE_VHT)
222551172f62SAdrian Chadd 			ieee80211_vht_node_cleanup(ni);
222651172f62SAdrian Chadd 
222751172f62SAdrian Chadd 		/* HT */
2228b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2229b032f27cSSam Leffler 			rate = ieee80211_setup_htrates(ni, htcap,
2230b032f27cSSam Leffler 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2231b032f27cSSam Leffler 				IEEE80211_F_DOBRS);
2232b032f27cSSam Leffler 			if (rate & IEEE80211_RATE_BASIC) {
2233b032f27cSSam Leffler 				ratesetmismatch(ni, wh, reassoc, resp,
2234b032f27cSSam Leffler 				    "HT", rate);
2235b032f27cSSam Leffler 				vap->iv_stats.is_ht_assoc_norate++;
2236b032f27cSSam Leffler 				return;
2237b032f27cSSam Leffler 			}
2238fdabd982SSam Leffler 			ieee80211_ht_node_init(ni);
2239fdabd982SSam Leffler 			ieee80211_ht_updatehtcap(ni, htcap);
2240b032f27cSSam Leffler 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2241b032f27cSSam Leffler 			ieee80211_ht_node_cleanup(ni);
224251172f62SAdrian Chadd 
224351172f62SAdrian Chadd 		/* Finally - this will use HT/VHT info to change node channel */
224451172f62SAdrian Chadd 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
224551172f62SAdrian Chadd 			ieee80211_ht_updatehtcap_final(ni);
224651172f62SAdrian Chadd 		}
224751172f62SAdrian Chadd 
2248339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
22490e6cbef2SAdrian Chadd 		/* Always do ff node cleanup; for A-MSDU */
2250339ccfb3SSam Leffler 		ieee80211_ff_node_cleanup(ni);
2251339ccfb3SSam Leffler #endif
2252b032f27cSSam Leffler 		/*
2253b032f27cSSam Leffler 		 * Allow AMPDU operation only with unencrypted traffic
2254*70dc8e5eSAdrian Chadd 		 * or AES-CCM / AES-GCM; the 802.11n spec only specifies these
2255*70dc8e5eSAdrian Chadd 		 * ciphers so permitting any others is undefined and can lead
2256b032f27cSSam Leffler 		 * to interoperability problems.
2257*70dc8e5eSAdrian Chadd 		 *
2258*70dc8e5eSAdrian Chadd 		 * TODO: before landing, find exactly where in 802.11-2020 this
2259*70dc8e5eSAdrian Chadd 		 * is called out!
2260b032f27cSSam Leffler 		 */
2261b032f27cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2262b032f27cSSam Leffler 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2263*70dc8e5eSAdrian Chadd 		    !hostapd_validate_cipher_for_ht_ampdu(rsnparms.rsn_ucastcipher)) ||
2264b032f27cSSam Leffler 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2265b032f27cSSam Leffler 			IEEE80211_NOTE(vap,
2266b032f27cSSam Leffler 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2267b032f27cSSam Leffler 			    "disallow HT use because WEP or TKIP requested, "
2268b032f27cSSam Leffler 			    "capinfo 0x%x ucastcipher %d", capinfo,
2269b032f27cSSam Leffler 			    rsnparms.rsn_ucastcipher);
2270b032f27cSSam Leffler 			ieee80211_ht_node_cleanup(ni);
22710e6cbef2SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
22720e6cbef2SAdrian Chadd 			/* Always do ff node cleanup; for A-MSDU */
22730e6cbef2SAdrian Chadd 			ieee80211_ff_node_cleanup(ni);
22740e6cbef2SAdrian Chadd #endif
2275b032f27cSSam Leffler 			vap->iv_stats.is_ht_assoc_downgrade++;
2276b032f27cSSam Leffler 		}
2277b032f27cSSam Leffler 		/*
2278b032f27cSSam Leffler 		 * If constrained to 11n-only stations reject legacy stations.
2279b032f27cSSam Leffler 		 */
22802bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2281b032f27cSSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2282b032f27cSSam Leffler 			htcapmismatch(ni, wh, reassoc, resp);
2283b032f27cSSam Leffler 			vap->iv_stats.is_ht_assoc_nohtcap++;
2284b032f27cSSam Leffler 			return;
2285b032f27cSSam Leffler 		}
2286b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
22875463c4a4SSam Leffler 		ni->ni_noise = nf;
2288b032f27cSSam Leffler 		ni->ni_intval = lintval;
2289b032f27cSSam Leffler 		ni->ni_capinfo = capinfo;
2290b032f27cSSam Leffler 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2291b032f27cSSam Leffler 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2292b032f27cSSam Leffler 		/*
2293b032f27cSSam Leffler 		 * Store the IEs.
2294b032f27cSSam Leffler 		 * XXX maybe better to just expand
2295b032f27cSSam Leffler 		 */
2296b032f27cSSam Leffler 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2297b032f27cSSam Leffler #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2298b032f27cSSam Leffler 			if (wpa != NULL)
2299b032f27cSSam Leffler 				setie(wpa_ie, wpa - sfrm);
2300b032f27cSSam Leffler 			if (rsn != NULL)
2301b032f27cSSam Leffler 				setie(rsn_ie, rsn - sfrm);
2302b032f27cSSam Leffler 			if (htcap != NULL)
2303b032f27cSSam Leffler 				setie(htcap_ie, htcap - sfrm);
2304b032f27cSSam Leffler 			if (wme != NULL) {
2305b032f27cSSam Leffler 				setie(wme_ie, wme - sfrm);
2306b032f27cSSam Leffler 				/*
2307b032f27cSSam Leffler 				 * Mark node as capable of QoS.
2308b032f27cSSam Leffler 				 */
2309b032f27cSSam Leffler 				ni->ni_flags |= IEEE80211_NODE_QOS;
23108379e8dbSAdrian Chadd 				if (ieee80211_parse_wmeie(wme, wh, ni) > 0) {
23118379e8dbSAdrian Chadd 					if (ni->ni_uapsd != 0)
23128379e8dbSAdrian Chadd 						ni->ni_flags |=
23138379e8dbSAdrian Chadd 						    IEEE80211_NODE_UAPSD;
23148379e8dbSAdrian Chadd 					else
23158379e8dbSAdrian Chadd 						ni->ni_flags &=
23168379e8dbSAdrian Chadd 						    ~IEEE80211_NODE_UAPSD;
23178379e8dbSAdrian Chadd 				}
2318b032f27cSSam Leffler 			} else
23198379e8dbSAdrian Chadd 				ni->ni_flags &=
23208379e8dbSAdrian Chadd 				    ~(IEEE80211_NODE_QOS |
23218379e8dbSAdrian Chadd 				      IEEE80211_NODE_UAPSD);
2322616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2323b032f27cSSam Leffler 			if (ath != NULL) {
2324b032f27cSSam Leffler 				setie(ath_ie, ath - sfrm);
2325b032f27cSSam Leffler 				/*
2326b032f27cSSam Leffler 				 * Parse ATH station parameters.
2327b032f27cSSam Leffler 				 */
2328b032f27cSSam Leffler 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2329b032f27cSSam Leffler 			} else
2330616190d0SSam Leffler #endif
2331b032f27cSSam Leffler 				ni->ni_ath_flags = 0;
2332b032f27cSSam Leffler #undef setie
2333b032f27cSSam Leffler 		} else {
2334b032f27cSSam Leffler 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
23358379e8dbSAdrian Chadd 			ni->ni_flags &= ~IEEE80211_NODE_UAPSD;
2336b032f27cSSam Leffler 			ni->ni_ath_flags = 0;
2337b032f27cSSam Leffler 		}
2338b032f27cSSam Leffler 		ieee80211_node_join(ni, resp);
2339b032f27cSSam Leffler 		ieee80211_deliver_l2uf(ni);
2340b032f27cSSam Leffler 		break;
2341b032f27cSSam Leffler 	}
2342b032f27cSSam Leffler 
2343b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2344b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
234505ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
2346b032f27cSSam Leffler 		uint16_t reason;
234705ea7a3eSBjoern A. Zeeb #endif
2348b032f27cSSam Leffler 
2349b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN ||
2350b032f27cSSam Leffler 		    /* NB: can happen when in promiscuous mode */
2351b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2352b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
2353b032f27cSSam Leffler 			break;
2354b032f27cSSam Leffler 		}
2355b032f27cSSam Leffler 		/*
2356b032f27cSSam Leffler 		 * deauth/disassoc frame format
2357b032f27cSSam Leffler 		 *	[2] reason
2358b032f27cSSam Leffler 		 */
2359b032f27cSSam Leffler 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
236005ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
2361b032f27cSSam Leffler 		reason = le16toh(*(uint16_t *)frm);
236205ea7a3eSBjoern A. Zeeb #endif
2363b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2364b032f27cSSam Leffler 			vap->iv_stats.is_rx_deauth++;
2365b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_deauth);
2366b032f27cSSam Leffler 		} else {
2367b032f27cSSam Leffler 			vap->iv_stats.is_rx_disassoc++;
2368b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_disassoc);
2369b032f27cSSam Leffler 		}
2370b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2371d72d72d3SAndriy Voskoboinyk 		    "recv %s (reason: %d (%s))",
23724357a5d1SAndriy Voskoboinyk 		    ieee80211_mgt_subtype_name(subtype),
2373d72d72d3SAndriy Voskoboinyk 		    reason, ieee80211_reason_to_string(reason));
2374b032f27cSSam Leffler 		if (ni != vap->iv_bss)
2375b032f27cSSam Leffler 			ieee80211_node_leave(ni);
2376b032f27cSSam Leffler 		break;
2377b032f27cSSam Leffler 	}
2378b032f27cSSam Leffler 
2379b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ACTION:
238096283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2381893c4d6eSBernhard Schmidt 		if (ni == vap->iv_bss) {
2382893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2383893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "unknown node");
2384893c4d6eSBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
2385893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2386893c4d6eSBernhard Schmidt 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2387893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2388893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
2389893c4d6eSBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
2390893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
239196283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
239296283082SBernhard Schmidt 			    wh, NULL, "wrong state %s",
239396283082SBernhard Schmidt 			    ieee80211_state_name[vap->iv_state]);
2394b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
2395893c4d6eSBernhard Schmidt 		} else {
2396893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
2397893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
239896283082SBernhard Schmidt 		}
2399b032f27cSSam Leffler 		break;
2400b032f27cSSam Leffler 
2401b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2402b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2403665d5ae9SAndriy Voskoboinyk 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
240496283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
240596283082SBernhard Schmidt 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
240696283082SBernhard Schmidt 		    wh, NULL, "%s", "not handled");
240796283082SBernhard Schmidt 		vap->iv_stats.is_rx_mgtdiscard++;
240896283082SBernhard Schmidt 		break;
240996283082SBernhard Schmidt 
2410b032f27cSSam Leffler 	default:
2411b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2412b032f27cSSam Leffler 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2413b032f27cSSam Leffler 		vap->iv_stats.is_rx_badsubtype++;
2414b032f27cSSam Leffler 		break;
2415b032f27cSSam Leffler 	}
2416b032f27cSSam Leffler }
2417b032f27cSSam Leffler 
241849eae5f7SSam Leffler static void
hostap_recv_ctl(struct ieee80211_node * ni,struct mbuf * m,int subtype)241949eae5f7SSam Leffler hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
242049eae5f7SSam Leffler {
242149eae5f7SSam Leffler 	switch (subtype) {
242249eae5f7SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2423e7f0d7cfSAdrian Chadd 		ni->ni_vap->iv_recv_pspoll(ni, m);
242449eae5f7SSam Leffler 		break;
242549eae5f7SSam Leffler 	case IEEE80211_FC0_SUBTYPE_BAR:
242649eae5f7SSam Leffler 		ieee80211_recv_bar(ni, m);
242749eae5f7SSam Leffler 		break;
242849eae5f7SSam Leffler 	}
242949eae5f7SSam Leffler }
243049eae5f7SSam Leffler 
2431b032f27cSSam Leffler /*
2432b032f27cSSam Leffler  * Process a received ps-poll frame.
2433b032f27cSSam Leffler  */
2434e7f0d7cfSAdrian Chadd void
ieee80211_recv_pspoll(struct ieee80211_node * ni,struct mbuf * m0)2435e7f0d7cfSAdrian Chadd ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2436b032f27cSSam Leffler {
2437b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
24385cda6006SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
2439b032f27cSSam Leffler 	struct ieee80211_frame_min *wh;
2440b032f27cSSam Leffler 	struct mbuf *m;
2441b032f27cSSam Leffler 	uint16_t aid;
2442b032f27cSSam Leffler 	int qlen;
2443b032f27cSSam Leffler 
2444b032f27cSSam Leffler 	wh = mtod(m0, struct ieee80211_frame_min *);
2445b032f27cSSam Leffler 	if (ni->ni_associd == 0) {
2446b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
2447b032f27cSSam Leffler 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2448b032f27cSSam Leffler 		    (struct ieee80211_frame *) wh, NULL,
2449b032f27cSSam Leffler 		    "%s", "unassociated station");
2450b032f27cSSam Leffler 		vap->iv_stats.is_ps_unassoc++;
2451b032f27cSSam Leffler 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2452b032f27cSSam Leffler 			IEEE80211_REASON_NOT_ASSOCED);
2453b032f27cSSam Leffler 		return;
2454b032f27cSSam Leffler 	}
2455b032f27cSSam Leffler 
2456b032f27cSSam Leffler 	aid = le16toh(*(uint16_t *)wh->i_dur);
2457b032f27cSSam Leffler 	if (aid != ni->ni_associd) {
2458b032f27cSSam Leffler 		IEEE80211_DISCARD(vap,
2459b032f27cSSam Leffler 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2460b032f27cSSam Leffler 		    (struct ieee80211_frame *) wh, NULL,
2461b032f27cSSam Leffler 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2462b032f27cSSam Leffler 		    ni->ni_associd, aid);
2463b032f27cSSam Leffler 		vap->iv_stats.is_ps_badaid++;
2464693e3122SSam Leffler 		/*
2465693e3122SSam Leffler 		 * NB: We used to deauth the station but it turns out
2466693e3122SSam Leffler 		 * the Blackberry Curve 8230 (and perhaps other devices)
2467693e3122SSam Leffler 		 * sometimes send the wrong AID when WME is negotiated.
2468693e3122SSam Leffler 		 * Being more lenient here seems ok as we already check
2469693e3122SSam Leffler 		 * the station is associated and we only return frames
2470693e3122SSam Leffler 		 * queued for the station (i.e. we don't use the AID).
2471693e3122SSam Leffler 		 */
2472b032f27cSSam Leffler 		return;
2473b032f27cSSam Leffler 	}
2474b032f27cSSam Leffler 
2475b032f27cSSam Leffler 	/* Okay, take the first queued packet and put it out... */
247663092fceSSam Leffler 	m = ieee80211_node_psq_dequeue(ni, &qlen);
2477b032f27cSSam Leffler 	if (m == NULL) {
2478b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2479b032f27cSSam Leffler 		    "%s", "recv ps-poll, but queue empty");
2480b032f27cSSam Leffler 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2481b032f27cSSam Leffler 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2482b032f27cSSam Leffler 		if (vap->iv_set_tim != NULL)
2483b032f27cSSam Leffler 			vap->iv_set_tim(ni, 0);	/* just in case */
2484b032f27cSSam Leffler 		return;
2485b032f27cSSam Leffler 	}
2486b032f27cSSam Leffler 	/*
2487b032f27cSSam Leffler 	 * If there are more packets, set the more packets bit
2488b032f27cSSam Leffler 	 * in the packet dispatched to the station; otherwise
2489b032f27cSSam Leffler 	 * turn off the TIM bit.
2490b032f27cSSam Leffler 	 */
2491b032f27cSSam Leffler 	if (qlen != 0) {
2492b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2493b032f27cSSam Leffler 		    "recv ps-poll, send packet, %u still queued", qlen);
2494b032f27cSSam Leffler 		m->m_flags |= M_MORE_DATA;
2495b032f27cSSam Leffler 	} else {
2496b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2497b032f27cSSam Leffler 		    "%s", "recv ps-poll, send packet, queue empty");
2498b032f27cSSam Leffler 		if (vap->iv_set_tim != NULL)
2499b032f27cSSam Leffler 			vap->iv_set_tim(ni, 0);
2500b032f27cSSam Leffler 	}
2501b032f27cSSam Leffler 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
250263092fceSSam Leffler 
25037ea3aadaSAdrian Chadd 	/*
25045cda6006SAdrian Chadd 	 * Do the right thing; if it's an encap'ed frame then
2505d3a4ade3SAdrian Chadd 	 * call ieee80211_parent_xmitpkt() else
2506e7495198SAdrian Chadd 	 * call ieee80211_vap_xmitpkt().
25077ea3aadaSAdrian Chadd 	 */
25085cda6006SAdrian Chadd 	if (m->m_flags & M_ENCAP) {
2509d3a4ade3SAdrian Chadd 		(void) ieee80211_parent_xmitpkt(ic, m);
25105cda6006SAdrian Chadd 	} else {
2511e7495198SAdrian Chadd 		(void) ieee80211_vap_xmitpkt(vap, m);
25127ea3aadaSAdrian Chadd 	}
2513b032f27cSSam Leffler }
2514