xref: /freebsd/sys/net80211/ieee80211_adhoc.c (revision a233c71650a1e5b29723a8547ea160e105aa855e)
1b032f27cSSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
410ad9a77SSam Leffler  * Copyright (c) 2007-2009 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 IBSS 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>
51b032f27cSSam Leffler #include <net/ethernet.h>
52b032f27cSSam Leffler 
53b032f27cSSam Leffler #include <net/bpf.h>
54b032f27cSSam Leffler 
55b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
56b032f27cSSam Leffler #include <net80211/ieee80211_adhoc.h>
57b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
58616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
59616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
60616190d0SSam Leffler #endif
6110ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
6210ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
6310ad9a77SSam Leffler #endif
6424effd11SAdrian Chadd #include <net80211/ieee80211_sta.h>
65b032f27cSSam Leffler 
66b032f27cSSam Leffler #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
67b032f27cSSam Leffler 
68b032f27cSSam Leffler static	void adhoc_vattach(struct ieee80211vap *);
69b032f27cSSam Leffler static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70c79f192cSAdrian Chadd static int adhoc_input(struct ieee80211_node *, struct mbuf *,
71c79f192cSAdrian Chadd 	    const struct ieee80211_rx_stats *, int, int);
72b032f27cSSam Leffler static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
73c79f192cSAdrian Chadd 	int subtype, const struct ieee80211_rx_stats *, int, int);
7452ef06dcSSam Leffler static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
75c79f192cSAdrian Chadd 	    int subtype, const struct ieee80211_rx_stats *rxs, int, int);
7649eae5f7SSam Leffler static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
77b032f27cSSam Leffler 
78b032f27cSSam Leffler void
ieee80211_adhoc_attach(struct ieee80211com * ic)79b032f27cSSam Leffler ieee80211_adhoc_attach(struct ieee80211com *ic)
80b032f27cSSam Leffler {
81b032f27cSSam Leffler 	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
82b032f27cSSam Leffler 	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
83b032f27cSSam Leffler }
84b032f27cSSam Leffler 
85b032f27cSSam Leffler void
ieee80211_adhoc_detach(struct ieee80211com * ic)86b032f27cSSam Leffler ieee80211_adhoc_detach(struct ieee80211com *ic)
87b032f27cSSam Leffler {
88b032f27cSSam Leffler }
89b032f27cSSam Leffler 
90b032f27cSSam Leffler static void
adhoc_vdetach(struct ieee80211vap * vap)91b032f27cSSam Leffler adhoc_vdetach(struct ieee80211vap *vap)
92b032f27cSSam Leffler {
93b032f27cSSam Leffler }
94b032f27cSSam Leffler 
95b032f27cSSam Leffler static void
adhoc_vattach(struct ieee80211vap * vap)96b032f27cSSam Leffler adhoc_vattach(struct ieee80211vap *vap)
97b032f27cSSam Leffler {
98b032f27cSSam Leffler 	vap->iv_newstate = adhoc_newstate;
99b032f27cSSam Leffler 	vap->iv_input = adhoc_input;
10052ef06dcSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS)
101b032f27cSSam Leffler 		vap->iv_recv_mgmt = adhoc_recv_mgmt;
10252ef06dcSSam Leffler 	else
10352ef06dcSSam Leffler 		vap->iv_recv_mgmt = ahdemo_recv_mgmt;
10449eae5f7SSam Leffler 	vap->iv_recv_ctl = adhoc_recv_ctl;
105b032f27cSSam Leffler 	vap->iv_opdetach = adhoc_vdetach;
10610ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
10710ad9a77SSam Leffler 	/*
10810ad9a77SSam Leffler 	 * Throw control to tdma support.  Note we do this
10910ad9a77SSam Leffler 	 * after setting up our callbacks so it can piggyback
11010ad9a77SSam Leffler 	 * on top of us.
11110ad9a77SSam Leffler 	 */
11210ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA)
11310ad9a77SSam Leffler 		ieee80211_tdma_vattach(vap);
11410ad9a77SSam Leffler #endif
115b032f27cSSam Leffler }
116b032f27cSSam Leffler 
117f09cf33fSSam Leffler static void
sta_leave(void * arg,struct ieee80211_node * ni)118f09cf33fSSam Leffler sta_leave(void *arg, struct ieee80211_node *ni)
119f09cf33fSSam Leffler {
1207db788c6SAndriy Voskoboinyk 	struct ieee80211vap *vap = ni->ni_vap;
121f09cf33fSSam Leffler 
1227db788c6SAndriy Voskoboinyk 	if (ni != vap->iv_bss)
123f09cf33fSSam Leffler 		ieee80211_node_leave(ni);
124f09cf33fSSam Leffler }
125f09cf33fSSam Leffler 
126b032f27cSSam Leffler /*
127b032f27cSSam Leffler  * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
128b032f27cSSam Leffler  */
129b032f27cSSam Leffler static int
adhoc_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)130b032f27cSSam Leffler adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
131b032f27cSSam Leffler {
132b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
133b032f27cSSam Leffler 	struct ieee80211_node *ni;
134b032f27cSSam Leffler 	enum ieee80211_state ostate;
135b032f27cSSam Leffler 
136b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
137b032f27cSSam Leffler 
138b032f27cSSam Leffler 	ostate = vap->iv_state;
139b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
140b032f27cSSam Leffler 	    __func__, ieee80211_state_name[ostate],
141b032f27cSSam Leffler 	    ieee80211_state_name[nstate], arg);
142b032f27cSSam Leffler 	vap->iv_state = nstate;			/* state transition */
143b032f27cSSam Leffler 	if (ostate != IEEE80211_S_SCAN)
144b032f27cSSam Leffler 		ieee80211_cancel_scan(vap);	/* background scan */
145b032f27cSSam Leffler 	ni = vap->iv_bss;			/* NB: no reference held */
146b032f27cSSam Leffler 	switch (nstate) {
147b032f27cSSam Leffler 	case IEEE80211_S_INIT:
148b032f27cSSam Leffler 		switch (ostate) {
149b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
150b032f27cSSam Leffler 			ieee80211_cancel_scan(vap);
151b032f27cSSam Leffler 			break;
152b032f27cSSam Leffler 		default:
153b032f27cSSam Leffler 			break;
154b032f27cSSam Leffler 		}
155b032f27cSSam Leffler 		if (ostate != IEEE80211_S_INIT) {
156b032f27cSSam Leffler 			/* NB: optimize INIT -> INIT case */
157b032f27cSSam Leffler 			ieee80211_reset_bss(vap);
158b032f27cSSam Leffler 		}
159b032f27cSSam Leffler 		break;
160b032f27cSSam Leffler 	case IEEE80211_S_SCAN:
161b032f27cSSam Leffler 		switch (ostate) {
162b032f27cSSam Leffler 		case IEEE80211_S_RUN:		/* beacon miss */
163f09cf33fSSam Leffler 			/* purge station table; entries are stale */
1647db788c6SAndriy Voskoboinyk 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
1657db788c6SAndriy Voskoboinyk 			    sta_leave, NULL);
166f09cf33fSSam Leffler 			/* fall thru... */
167f09cf33fSSam Leffler 		case IEEE80211_S_INIT:
168b032f27cSSam Leffler 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
169b032f27cSSam Leffler 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
170b032f27cSSam Leffler 				/*
171b032f27cSSam Leffler 				 * Already have a channel; bypass the
172b032f27cSSam Leffler 				 * scan and startup immediately.
173b032f27cSSam Leffler 				 */
1741a006f7dSAdrian Chadd 				ieee80211_create_ibss(vap,
1751a006f7dSAdrian Chadd 				    ieee80211_ht_adjust_channel(ic,
1761a006f7dSAdrian Chadd 				    vap->iv_des_chan, vap->iv_flags_ht));
177b032f27cSSam Leffler 				break;
178b032f27cSSam Leffler 			}
179b032f27cSSam Leffler 			/*
180b032f27cSSam Leffler 			 * Initiate a scan.  We can come here as a result
181b032f27cSSam Leffler 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
182b032f27cSSam Leffler 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
183b032f27cSSam Leffler 			 * and the scan request parameters will be present
184b032f27cSSam Leffler 			 * in iv_scanreq.  Otherwise we do the default.
185b032f27cSSam Leffler 			 */
186b032f27cSSam Leffler 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
187b032f27cSSam Leffler 				ieee80211_check_scan(vap,
188b032f27cSSam Leffler 				    vap->iv_scanreq_flags,
189b032f27cSSam Leffler 				    vap->iv_scanreq_duration,
190b032f27cSSam Leffler 				    vap->iv_scanreq_mindwell,
191b032f27cSSam Leffler 				    vap->iv_scanreq_maxdwell,
192b032f27cSSam Leffler 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
193b032f27cSSam Leffler 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
194b032f27cSSam Leffler 			} else
195b032f27cSSam Leffler 				ieee80211_check_scan_current(vap);
196b032f27cSSam Leffler 			break;
197b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
198b032f27cSSam Leffler 			/*
199b032f27cSSam Leffler 			 * This can happen because of a change in state
200b032f27cSSam Leffler 			 * that requires a reset.  Trigger a new scan
201b032f27cSSam Leffler 			 * unless we're in manual roaming mode in which
202b032f27cSSam Leffler 			 * case an application must issue an explicit request.
203b032f27cSSam Leffler 			 */
204b032f27cSSam Leffler 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
205b032f27cSSam Leffler 				ieee80211_check_scan_current(vap);
206b032f27cSSam Leffler 			break;
207b032f27cSSam Leffler 		default:
208b032f27cSSam Leffler 			goto invalid;
209b032f27cSSam Leffler 		}
210b032f27cSSam Leffler 		break;
211b032f27cSSam Leffler 	case IEEE80211_S_RUN:
212b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA) {
213b032f27cSSam Leffler 			/* XXX validate prerequisites */
214b032f27cSSam Leffler 		}
215b032f27cSSam Leffler 		switch (ostate) {
21604a5c73bSAndriy Voskoboinyk 		case IEEE80211_S_INIT:
21704a5c73bSAndriy Voskoboinyk 			/*
21804a5c73bSAndriy Voskoboinyk 			 * Already have a channel; bypass the
21904a5c73bSAndriy Voskoboinyk 			 * scan and startup immediately.
22004a5c73bSAndriy Voskoboinyk 			 * Note that ieee80211_create_ibss will call
22104a5c73bSAndriy Voskoboinyk 			 * back to do a RUN->RUN state change.
22204a5c73bSAndriy Voskoboinyk 			 */
22304a5c73bSAndriy Voskoboinyk 			ieee80211_create_ibss(vap,
22404a5c73bSAndriy Voskoboinyk 			    ieee80211_ht_adjust_channel(ic,
22504a5c73bSAndriy Voskoboinyk 				ic->ic_curchan, vap->iv_flags_ht));
22604a5c73bSAndriy Voskoboinyk 			/* NB: iv_bss is changed on return */
22704a5c73bSAndriy Voskoboinyk 			ni = vap->iv_bss;
22804a5c73bSAndriy Voskoboinyk 			break;
229b032f27cSSam Leffler 		case IEEE80211_S_SCAN:
230b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
231b032f27cSSam Leffler 			if (ieee80211_msg_debug(vap)) {
232b032f27cSSam Leffler 				ieee80211_note(vap,
233b032f27cSSam Leffler 				    "synchronized with %s ssid ",
234b032f27cSSam Leffler 				    ether_sprintf(ni->ni_bssid));
235b032f27cSSam Leffler 				ieee80211_print_essid(vap->iv_bss->ni_essid,
236b032f27cSSam Leffler 				    ni->ni_esslen);
237*674362e2SAdrian Chadd 				net80211_printf(" channel %d start %uMbit/s\n",
238b032f27cSSam Leffler 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
23938075f7dSAdrian Chadd 				    ieee80211_node_get_txrate_kbit(ni) / 1000);
240b032f27cSSam Leffler 			}
241b032f27cSSam Leffler #endif
242b032f27cSSam Leffler 			break;
243364ee125SAdrian Chadd 		case IEEE80211_S_RUN:	/* IBSS merge */
244364ee125SAdrian Chadd 			break;
245b032f27cSSam Leffler 		default:
246b032f27cSSam Leffler 			goto invalid;
247b032f27cSSam Leffler 		}
248b032f27cSSam Leffler 		/*
249b032f27cSSam Leffler 		 * When 802.1x is not in use mark the port authorized
250b032f27cSSam Leffler 		 * at this point so traffic can flow.
251b032f27cSSam Leffler 		 */
252b032f27cSSam Leffler 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
253b032f27cSSam Leffler 			ieee80211_node_authorize(ni);
254e66b0905SSam Leffler 		/*
255e66b0905SSam Leffler 		 * Fake association when joining an existing bss.
256e66b0905SSam Leffler 		 */
257e66b0905SSam Leffler 		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
258e66b0905SSam Leffler 		    ic->ic_newassoc != NULL)
259e66b0905SSam Leffler 			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
260b032f27cSSam Leffler 		break;
261b032f27cSSam Leffler 	case IEEE80211_S_SLEEP:
262e7f0d7cfSAdrian Chadd 		vap->iv_sta_ps(vap, 0);
263b032f27cSSam Leffler 		break;
264b032f27cSSam Leffler 	default:
265b032f27cSSam Leffler 	invalid:
26607760642SSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
26707760642SSam Leffler 		    "%s: unexpected state transition %s -> %s\n", __func__,
268b032f27cSSam Leffler 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
269b032f27cSSam Leffler 		break;
270b032f27cSSam Leffler 	}
271b032f27cSSam Leffler 	return 0;
272b032f27cSSam Leffler }
273b032f27cSSam Leffler 
274b032f27cSSam Leffler /*
275b032f27cSSam Leffler  * Decide if a received management frame should be
276b032f27cSSam Leffler  * printed when debugging is enabled.  This filters some
277b032f27cSSam Leffler  * of the less interesting frames that come frequently
278b032f27cSSam Leffler  * (e.g. beacons).
279b032f27cSSam Leffler  */
280b032f27cSSam Leffler static __inline int
doprint(struct ieee80211vap * vap,int subtype)281b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
282b032f27cSSam Leffler {
283b032f27cSSam Leffler 	switch (subtype) {
284b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
285b032f27cSSam Leffler 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
286b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
287b032f27cSSam Leffler 		return 1;
288b032f27cSSam Leffler 	}
289b032f27cSSam Leffler 	return 1;
290b032f27cSSam Leffler }
291b032f27cSSam Leffler 
292b032f27cSSam Leffler /*
293b032f27cSSam Leffler  * Process a received frame.  The node associated with the sender
294b032f27cSSam Leffler  * should be supplied.  If nothing was found in the node table then
295b032f27cSSam Leffler  * the caller is assumed to supply a reference to iv_bss instead.
296b032f27cSSam Leffler  * The RSSI and a timestamp are also supplied.  The RSSI data is used
297b032f27cSSam Leffler  * during AP scanning to select a AP to associate with; it can have
298b032f27cSSam Leffler  * any units so long as values have consistent units and higher values
299b032f27cSSam Leffler  * mean ``better signal''.  The receive timestamp is currently not used
300b032f27cSSam Leffler  * by the 802.11 layer.
301b032f27cSSam Leffler  */
302b032f27cSSam Leffler static int
adhoc_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)303c79f192cSAdrian Chadd adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
304c79f192cSAdrian Chadd     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
305b032f27cSSam Leffler {
306b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
307b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
308b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
309b032f27cSSam Leffler 	struct ieee80211_frame *wh;
310b032f27cSSam Leffler 	struct ieee80211_key *key;
311b032f27cSSam Leffler 	struct ether_header *eh;
3122b80a340SRui Paulo 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
313b032f27cSSam Leffler 	uint8_t dir, type, subtype, qos;
314b032f27cSSam Leffler 	uint8_t *bssid;
315fe75b452SAdrian Chadd 	int is_hw_decrypted = 0;
316fe75b452SAdrian Chadd 	int has_decrypted = 0;
317fe75b452SAdrian Chadd 
318fe75b452SAdrian Chadd 	/*
319fe75b452SAdrian Chadd 	 * Some devices do hardware decryption all the way through
320fe75b452SAdrian Chadd 	 * to pretending the frame wasn't encrypted in the first place.
321fe75b452SAdrian Chadd 	 * So, tag it appropriately so it isn't discarded inappropriately.
322fe75b452SAdrian Chadd 	 */
323fe75b452SAdrian Chadd 	if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
324fe75b452SAdrian Chadd 		is_hw_decrypted = 1;
325b032f27cSSam Leffler 
32645f856e3SSam Leffler 	if (m->m_flags & M_AMPDU_MPDU) {
327b032f27cSSam Leffler 		/*
328b032f27cSSam Leffler 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
32945f856e3SSam Leffler 		 * w/ M_AMPDU_MPDU marked have already passed through
33045f856e3SSam Leffler 		 * here but were received out of order and been held on
33145f856e3SSam Leffler 		 * the reorder queue.  When resubmitted they are marked
33245f856e3SSam Leffler 		 * with the M_AMPDU_MPDU flag and we can bypass most of
33345f856e3SSam Leffler 		 * the normal processing.
334b032f27cSSam Leffler 		 */
335b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
336b032f27cSSam Leffler 		type = IEEE80211_FC0_TYPE_DATA;
337b032f27cSSam Leffler 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
338c9b7e9dfSBjoern A. Zeeb 		subtype = IEEE80211_FC0_SUBTYPE_QOS_DATA;
339b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
340b032f27cSSam Leffler 		goto resubmit_ampdu;
341b032f27cSSam Leffler 	}
342b032f27cSSam Leffler 
343b032f27cSSam Leffler 	KASSERT(ni != NULL, ("null node"));
344b032f27cSSam Leffler 	ni->ni_inact = ni->ni_inact_reload;
345b032f27cSSam Leffler 
346b032f27cSSam Leffler 	type = -1;			/* undefined */
347b032f27cSSam Leffler 
348b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
349b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
350b032f27cSSam Leffler 		    ni->ni_macaddr, NULL,
351b032f27cSSam Leffler 		    "too short (1): len %u", m->m_pkthdr.len);
352b032f27cSSam Leffler 		vap->iv_stats.is_rx_tooshort++;
353b032f27cSSam Leffler 		goto out;
354b032f27cSSam Leffler 	}
355b032f27cSSam Leffler 	/*
356b032f27cSSam Leffler 	 * Bit of a cheat here, we use a pointer for a 3-address
357b032f27cSSam Leffler 	 * frame format but don't reference fields past outside
358b032f27cSSam Leffler 	 * ieee80211_frame_min w/o first validating the data is
359b032f27cSSam Leffler 	 * present.
360b032f27cSSam Leffler 	 */
361b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
362b032f27cSSam Leffler 
36337e54466SAdrian Chadd 	if (!IEEE80211_IS_FC0_CHECK_VER(wh, IEEE80211_FC0_VERSION_0)) {
364b032f27cSSam Leffler 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
365dc7bf546SSam Leffler 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
366dc7bf546SSam Leffler 		    wh->i_fc[0], wh->i_fc[1]);
367b032f27cSSam Leffler 		vap->iv_stats.is_rx_badversion++;
368b032f27cSSam Leffler 		goto err;
369b032f27cSSam Leffler 	}
370b032f27cSSam Leffler 
371b032f27cSSam Leffler 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
372b032f27cSSam Leffler 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
373b032f27cSSam Leffler 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
374b032f27cSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
375b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS)
376b032f27cSSam Leffler 			bssid = wh->i_addr1;
377b032f27cSSam Leffler 		else if (type == IEEE80211_FC0_TYPE_CTL)
378b032f27cSSam Leffler 			bssid = wh->i_addr1;
379b032f27cSSam Leffler 		else {
380b032f27cSSam Leffler 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
381b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap,
382b032f27cSSam Leffler 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
383b032f27cSSam Leffler 				    NULL, "too short (2): len %u",
384b032f27cSSam Leffler 				    m->m_pkthdr.len);
385b032f27cSSam Leffler 				vap->iv_stats.is_rx_tooshort++;
386b032f27cSSam Leffler 				goto out;
387b032f27cSSam Leffler 			}
388b032f27cSSam Leffler 			bssid = wh->i_addr3;
389b032f27cSSam Leffler 		}
390b032f27cSSam Leffler 		/*
391b032f27cSSam Leffler 		 * Validate the bssid.
392b032f27cSSam Leffler 		 */
393a56a0ef3SAdrian Chadd 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
394a56a0ef3SAdrian Chadd 		     (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
395a56a0ef3SAdrian Chadd 		      subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
396a56a0ef3SAdrian Chadd 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
397a278d11aSAdrian Chadd 		    !IEEE80211_ADDR_EQ(bssid,
398a278d11aSAdrian Chadd 		        ieee80211_vap_get_broadcast_address(vap))) {
399b032f27cSSam Leffler 			/* not interested in */
400b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
401b032f27cSSam Leffler 			    bssid, NULL, "%s", "not to bss");
402b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongbss++;
403b032f27cSSam Leffler 			goto out;
404b032f27cSSam Leffler 		}
405b032f27cSSam Leffler 		/*
406b032f27cSSam Leffler 		 * Data frame, cons up a node when it doesn't
407b032f27cSSam Leffler 		 * exist. This should probably done after an ACL check.
408b032f27cSSam Leffler 		 */
409b032f27cSSam Leffler 		if (type == IEEE80211_FC0_TYPE_DATA &&
410b032f27cSSam Leffler 		    ni == vap->iv_bss &&
411b032f27cSSam Leffler 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
412b032f27cSSam Leffler 			/*
41310ad9a77SSam Leffler 			 * Beware of frames that come in too early; we
41410ad9a77SSam Leffler 			 * can receive broadcast frames and creating sta
41510ad9a77SSam Leffler 			 * entries will blow up because there is no bss
41610ad9a77SSam Leffler 			 * channel yet.
41710ad9a77SSam Leffler 			 */
41810ad9a77SSam Leffler 			if (vap->iv_state != IEEE80211_S_RUN) {
41910ad9a77SSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
42010ad9a77SSam Leffler 				    wh, "data", "not in RUN state (%s)",
42110ad9a77SSam Leffler 				    ieee80211_state_name[vap->iv_state]);
42210ad9a77SSam Leffler 				vap->iv_stats.is_rx_badstate++;
42310ad9a77SSam Leffler 				goto err;
42410ad9a77SSam Leffler 			}
42510ad9a77SSam Leffler 			/*
426869897d2SAdrian Chadd 			 * Fake up a node for this newly discovered member
427869897d2SAdrian Chadd 			 * of the IBSS.
428869897d2SAdrian Chadd 			 *
429869897d2SAdrian Chadd 			 * Note: This doesn't "upgrade" the node to 11n;
430869897d2SAdrian Chadd 			 * that will happen after a probe request/response
431869897d2SAdrian Chadd 			 * exchange.
432b032f27cSSam Leffler 			 */
433b032f27cSSam Leffler 			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
434b032f27cSSam Leffler 			if (ni == NULL) {
435b032f27cSSam Leffler 				/* NB: stat kept for alloc failure */
436b032f27cSSam Leffler 				goto err;
437b032f27cSSam Leffler 			}
438b032f27cSSam Leffler 		}
439b032f27cSSam Leffler 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
4405463c4a4SSam Leffler 		ni->ni_noise = nf;
441c3ebe019SAdrian Chadd 		if (IEEE80211_HAS_SEQ(type, subtype) &&
442c3ebe019SAdrian Chadd 		    IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
443b032f27cSSam Leffler 			uint8_t tid = ieee80211_gettid(wh);
444b032f27cSSam Leffler 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
445b032f27cSSam Leffler 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
446b032f27cSSam Leffler 				ic->ic_wme.wme_hipri_traffic++;
44785c4e670SAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh, bssid, rxs))
448b032f27cSSam Leffler 				goto out;
449b032f27cSSam Leffler 		}
450b032f27cSSam Leffler 	}
451b032f27cSSam Leffler 
452b032f27cSSam Leffler 	switch (type) {
453b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
454b032f27cSSam Leffler 		hdrspace = ieee80211_hdrspace(ic, wh);
455b032f27cSSam Leffler 		if (m->m_len < hdrspace &&
456b032f27cSSam Leffler 		    (m = m_pullup(m, hdrspace)) == NULL) {
457b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
458b032f27cSSam Leffler 			    ni->ni_macaddr, NULL,
459b032f27cSSam Leffler 			    "data too short: expecting %u", hdrspace);
460b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
461b032f27cSSam Leffler 			goto out;		/* XXX */
462b032f27cSSam Leffler 		}
463b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
464b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
465b032f27cSSam Leffler 			    wh, "data", "incorrect dir 0x%x", dir);
466b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
467b032f27cSSam Leffler 			goto out;
468b032f27cSSam Leffler 		}
469b032f27cSSam Leffler 		/* XXX no power-save support */
470b032f27cSSam Leffler 
471b032f27cSSam Leffler 		/*
47245f856e3SSam Leffler 		 * Handle A-MPDU re-ordering.  If the frame is to be
47345f856e3SSam Leffler 		 * processed directly then ieee80211_ampdu_reorder
474b032f27cSSam Leffler 		 * will return 0; otherwise it has consumed the mbuf
475b032f27cSSam Leffler 		 * and we should do nothing more with it.
476b032f27cSSam Leffler 		 */
47745f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU) &&
47885c4e670SAdrian Chadd 		    ieee80211_ampdu_reorder(ni, m, rxs) != 0) {
479b032f27cSSam Leffler 			m = NULL;
480b032f27cSSam Leffler 			goto out;
481b032f27cSSam Leffler 		}
482b032f27cSSam Leffler 	resubmit_ampdu:
483b032f27cSSam Leffler 
484b032f27cSSam Leffler 		/*
485b032f27cSSam Leffler 		 * Handle privacy requirements.  Note that we
486b032f27cSSam Leffler 		 * must not be preempted from here until after
487b032f27cSSam Leffler 		 * we (potentially) call ieee80211_crypto_demic;
488b032f27cSSam Leffler 		 * otherwise we may violate assumptions in the
489b032f27cSSam Leffler 		 * crypto cipher modules used to do delayed update
490b032f27cSSam Leffler 		 * of replay sequence numbers.
491b032f27cSSam Leffler 		 */
4922889cbe2SAdrian Chadd 		if (is_hw_decrypted || IEEE80211_IS_PROTECTED(wh)) {
493b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
494b032f27cSSam Leffler 				/*
495b032f27cSSam Leffler 				 * Discard encrypted frames when privacy is off.
496b032f27cSSam Leffler 				 */
497b032f27cSSam Leffler 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
498b032f27cSSam Leffler 				    wh, "WEP", "%s", "PRIVACY off");
499b032f27cSSam Leffler 				vap->iv_stats.is_rx_noprivacy++;
500b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
501b032f27cSSam Leffler 				goto out;
502b032f27cSSam Leffler 			}
503fe75b452SAdrian Chadd 			if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
504b032f27cSSam Leffler 				/* NB: stats+msgs handled in crypto_decap */
505b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_wepfail);
506b032f27cSSam Leffler 				goto out;
507b032f27cSSam Leffler 			}
508b032f27cSSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
5095945b5f5SKevin Lo 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
510fe75b452SAdrian Chadd 			has_decrypted = 1;
511b032f27cSSam Leffler 		} else {
512b032f27cSSam Leffler 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
513b032f27cSSam Leffler 			key = NULL;
514b032f27cSSam Leffler 		}
515b032f27cSSam Leffler 
516b032f27cSSam Leffler 		/*
517b032f27cSSam Leffler 		 * Save QoS bits for use below--before we strip the header.
518b032f27cSSam Leffler 		 */
519c9b7e9dfSBjoern A. Zeeb 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS_DATA)
520f3f08e16SAndriy Voskoboinyk 			qos = ieee80211_getqos(wh)[0];
521f3f08e16SAndriy Voskoboinyk 		else
522b032f27cSSam Leffler 			qos = 0;
523b032f27cSSam Leffler 
524b032f27cSSam Leffler 		/*
525b032f27cSSam Leffler 		 * Next up, any fragmentation.
526b032f27cSSam Leffler 		 */
527b032f27cSSam Leffler 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
52811572d7dSMathy Vanhoef 			m = ieee80211_defrag(ni, m, hdrspace, has_decrypted);
529b032f27cSSam Leffler 			if (m == NULL) {
530b032f27cSSam Leffler 				/* Fragment dropped or frame not complete yet */
531b032f27cSSam Leffler 				goto out;
532b032f27cSSam Leffler 			}
533b032f27cSSam Leffler 		}
534b032f27cSSam Leffler 		wh = NULL;		/* no longer valid, catch any uses */
535b032f27cSSam Leffler 
536b032f27cSSam Leffler 		/*
537b032f27cSSam Leffler 		 * Next strip any MSDU crypto bits.
538b032f27cSSam Leffler 		 */
539fe75b452SAdrian Chadd 		if (!ieee80211_crypto_demic(vap, key, m, 0)) {
540b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
541b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "demic error");
542b032f27cSSam Leffler 			vap->iv_stats.is_rx_demicfail++;
543b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_demicfail);
544b032f27cSSam Leffler 			goto out;
545b032f27cSSam Leffler 		}
546b032f27cSSam Leffler 
547b032f27cSSam Leffler 		/* copy to listener after decrypt */
5485463c4a4SSam Leffler 		if (ieee80211_radiotap_active_vap(vap))
5495463c4a4SSam Leffler 			ieee80211_radiotap_rx(vap, m);
550b032f27cSSam Leffler 		need_tap = 0;
551b032f27cSSam Leffler 
552b032f27cSSam Leffler 		/*
553b032f27cSSam Leffler 		 * Finally, strip the 802.11 header.
554b032f27cSSam Leffler 		 */
555f024bdf1SMathy Vanhoef 		m = ieee80211_decap(vap, m, hdrspace, qos);
556b032f27cSSam Leffler 		if (m == NULL) {
557b032f27cSSam Leffler 			/* XXX mask bit to check for both */
558b032f27cSSam Leffler 			/* don't count Null data frames as errors */
559b032f27cSSam Leffler 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
560b032f27cSSam Leffler 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
561b032f27cSSam Leffler 				goto out;
562b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
563b032f27cSSam Leffler 			    ni->ni_macaddr, "data", "%s", "decap error");
564b032f27cSSam Leffler 			vap->iv_stats.is_rx_decap++;
565b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_decap);
566b032f27cSSam Leffler 			goto err;
567b032f27cSSam Leffler 		}
568ffc19cf5SMathy Vanhoef 		if (!(qos & IEEE80211_QOS_AMSDU))
569b032f27cSSam Leffler 			eh = mtod(m, struct ether_header *);
570ffc19cf5SMathy Vanhoef 		else
571ffc19cf5SMathy Vanhoef 			eh = NULL;
572b032f27cSSam Leffler 		if (!ieee80211_node_is_authorized(ni)) {
573b032f27cSSam Leffler 			/*
574b032f27cSSam Leffler 			 * Deny any non-PAE frames received prior to
575b032f27cSSam Leffler 			 * authorization.  For open/shared-key
576b032f27cSSam Leffler 			 * authentication the port is mark authorized
577b032f27cSSam Leffler 			 * after authentication completes.  For 802.1x
578b032f27cSSam Leffler 			 * the port is not marked authorized by the
579b032f27cSSam Leffler 			 * authenticator until the handshake has completed.
580b032f27cSSam Leffler 			 */
581ffc19cf5SMathy Vanhoef 			if (eh == NULL ||
582ffc19cf5SMathy Vanhoef 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
583b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
584ffc19cf5SMathy Vanhoef 				    ni->ni_macaddr, "data", "unauthorized or "
585ffc19cf5SMathy Vanhoef 				    "unknown port: ether type 0x%x len %u",
586ffc19cf5SMathy Vanhoef 				    eh == NULL ? -1 : eh->ether_type,
587ffc19cf5SMathy Vanhoef 				    m->m_pkthdr.len);
588b032f27cSSam Leffler 				vap->iv_stats.is_rx_unauth++;
589b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unauth);
590b032f27cSSam Leffler 				goto err;
591b032f27cSSam Leffler 			}
592b032f27cSSam Leffler 		} else {
593b032f27cSSam Leffler 			/*
594b032f27cSSam Leffler 			 * When denying unencrypted frames, discard
595b032f27cSSam Leffler 			 * any non-PAE frames received without encryption.
596b032f27cSSam Leffler 			 */
597b032f27cSSam Leffler 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
598fe75b452SAdrian Chadd 			    ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
599fe75b452SAdrian Chadd 			    (is_hw_decrypted == 0) &&
600ffc19cf5SMathy Vanhoef 			    (eh == NULL ||
601ffc19cf5SMathy Vanhoef 			     eh->ether_type != htons(ETHERTYPE_PAE))) {
602b032f27cSSam Leffler 				/*
603b032f27cSSam Leffler 				 * Drop unencrypted frames.
604b032f27cSSam Leffler 				 */
605b032f27cSSam Leffler 				vap->iv_stats.is_rx_unencrypted++;
606b032f27cSSam Leffler 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
607b032f27cSSam Leffler 				goto out;
608b032f27cSSam Leffler 			}
609b032f27cSSam Leffler 		}
610b032f27cSSam Leffler 		/* XXX require HT? */
611b032f27cSSam Leffler 		if (qos & IEEE80211_QOS_AMSDU) {
612b032f27cSSam Leffler 			m = ieee80211_decap_amsdu(ni, m);
613b032f27cSSam Leffler 			if (m == NULL)
614b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
615616190d0SSam Leffler 		} else {
616616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
617616190d0SSam Leffler 			m = ieee80211_decap_fastframe(vap, ni, m);
618b032f27cSSam Leffler 			if (m == NULL)
619b032f27cSSam Leffler 				return IEEE80211_FC0_TYPE_DATA;
620616190d0SSam Leffler #endif
621b032f27cSSam Leffler 		}
622b032f27cSSam Leffler 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
623b032f27cSSam Leffler 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
624b032f27cSSam Leffler 		else
625b032f27cSSam Leffler 			ieee80211_deliver_data(vap, ni, m);
626b032f27cSSam Leffler 		return IEEE80211_FC0_TYPE_DATA;
627b032f27cSSam Leffler 
628b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
629b032f27cSSam Leffler 		vap->iv_stats.is_rx_mgmt++;
630b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_mgmt);
631b032f27cSSam Leffler 		if (dir != IEEE80211_FC1_DIR_NODS) {
632b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
633b032f27cSSam Leffler 			    wh, "data", "incorrect dir 0x%x", dir);
634b032f27cSSam Leffler 			vap->iv_stats.is_rx_wrongdir++;
635b032f27cSSam Leffler 			goto err;
636b032f27cSSam Leffler 		}
637b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
638b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
639b032f27cSSam Leffler 			    ni->ni_macaddr, "mgt", "too short: len %u",
640b032f27cSSam Leffler 			    m->m_pkthdr.len);
641b032f27cSSam Leffler 			vap->iv_stats.is_rx_tooshort++;
642b032f27cSSam Leffler 			goto out;
643b032f27cSSam Leffler 		}
644b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
645b032f27cSSam Leffler 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
646b032f27cSSam Leffler 		    ieee80211_msg_dumppkts(vap)) {
6471a3c03d8SAdrian Chadd 			net80211_vap_printf(vap,
6481a3c03d8SAdrian Chadd 			    "received %s from %s rssi %d\n",
6494357a5d1SAndriy Voskoboinyk 			    ieee80211_mgt_subtype_name(subtype),
650b032f27cSSam Leffler 			    ether_sprintf(wh->i_addr2), rssi);
651b032f27cSSam Leffler 		}
652b032f27cSSam Leffler #endif
6532889cbe2SAdrian Chadd 		if (IEEE80211_IS_PROTECTED(wh)) {
654b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
655b032f27cSSam Leffler 			    wh, NULL, "%s", "WEP set but not permitted");
656b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
657b032f27cSSam Leffler 			goto out;
658b032f27cSSam Leffler 		}
659c79f192cSAdrian Chadd 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
6608bbd3e41SSam Leffler 		goto out;
661b032f27cSSam Leffler 
662b032f27cSSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
663b032f27cSSam Leffler 		vap->iv_stats.is_rx_ctl++;
664b032f27cSSam Leffler 		IEEE80211_NODE_STAT(ni, rx_ctrl);
665267e8f64SAdrian Chadd 		if (ieee80211_is_ctl_frame_for_vap(ni, m))
66649eae5f7SSam Leffler 			vap->iv_recv_ctl(ni, m, subtype);
667b032f27cSSam Leffler 		goto out;
66824a599ddSSam Leffler 
669b032f27cSSam Leffler 	default:
670b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
671b032f27cSSam Leffler 		    wh, "bad", "frame type 0x%x", type);
672b032f27cSSam Leffler 		/* should not come here */
673b032f27cSSam Leffler 		break;
674b032f27cSSam Leffler 	}
675b032f27cSSam Leffler err:
676dea45121SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
677b032f27cSSam Leffler out:
678b032f27cSSam Leffler 	if (m != NULL) {
679a6c3cf3eSSam Leffler 		if (need_tap && ieee80211_radiotap_active_vap(vap))
6805463c4a4SSam Leffler 			ieee80211_radiotap_rx(vap, m);
681b032f27cSSam Leffler 		m_freem(m);
682b032f27cSSam Leffler 	}
683b032f27cSSam Leffler 	return type;
684b032f27cSSam Leffler }
685b032f27cSSam Leffler 
686b032f27cSSam Leffler static int
is11bclient(const uint8_t * rates,const uint8_t * xrates)687b032f27cSSam Leffler is11bclient(const uint8_t *rates, const uint8_t *xrates)
688b032f27cSSam Leffler {
689b032f27cSSam Leffler 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
690b032f27cSSam Leffler 	int i;
691b032f27cSSam Leffler 
692b032f27cSSam Leffler 	/* NB: the 11b clients we care about will not have xrates */
693b032f27cSSam Leffler 	if (xrates != NULL || rates == NULL)
694b032f27cSSam Leffler 		return 0;
695b032f27cSSam Leffler 	for (i = 0; i < rates[1]; i++) {
696b032f27cSSam Leffler 		int r = rates[2+i] & IEEE80211_RATE_VAL;
697b032f27cSSam Leffler 		if (r > 2*11 || ((1<<r) & brates) == 0)
698b032f27cSSam Leffler 			return 0;
699b032f27cSSam Leffler 	}
700b032f27cSSam Leffler 	return 1;
701b032f27cSSam Leffler }
702b032f27cSSam Leffler 
703b032f27cSSam Leffler static void
adhoc_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)704b032f27cSSam Leffler adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
705c79f192cSAdrian Chadd 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
706b032f27cSSam Leffler {
707b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
708b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
709c79f192cSAdrian Chadd 	struct ieee80211_channel *rxchan = ic->ic_curchan;
710b032f27cSSam Leffler 	struct ieee80211_frame *wh;
711601a2543SAndriy Voskoboinyk 	uint8_t *frm, *efrm;
712b032f27cSSam Leffler 	uint8_t *ssid, *rates, *xrates;
713341177e1SAdrian Chadd #if 0
714d71a1f7aSAdrian Chadd 	int ht_state_change = 0;
715341177e1SAdrian Chadd #endif
716b032f27cSSam Leffler 
717b032f27cSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
718b032f27cSSam Leffler 	frm = (uint8_t *)&wh[1];
719b032f27cSSam Leffler 	efrm = mtod(m0, uint8_t *) + m0->m_len;
7200b5aeb89SAdrian Chadd 
7210b5aeb89SAdrian Chadd 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
7220b5aeb89SAdrian Chadd 	    "%s: recv mgmt frame, addr2=%6D, ni=%p (%6D) fc=%.02x %.02x\n",
7230b5aeb89SAdrian Chadd 	    __func__,
7240b5aeb89SAdrian Chadd 	    wh->i_addr2, ":",
7250b5aeb89SAdrian Chadd 	    ni,
7260b5aeb89SAdrian Chadd 	    ni->ni_macaddr, ":",
7270b5aeb89SAdrian Chadd 	    wh->i_fc[0],
7280b5aeb89SAdrian Chadd 	    wh->i_fc[1]);
729b032f27cSSam Leffler 	switch (subtype) {
730b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
731b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON: {
732b032f27cSSam Leffler 		struct ieee80211_scanparams scan;
733c79f192cSAdrian Chadd 		struct ieee80211_channel *c;
734b032f27cSSam Leffler 		/*
735b032f27cSSam Leffler 		 * We process beacon/probe response
736b032f27cSSam Leffler 		 * frames to discover neighbors.
737b032f27cSSam Leffler 		 */
738c79f192cSAdrian Chadd 		if (rxs != NULL) {
739c79f192cSAdrian Chadd 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
740c79f192cSAdrian Chadd 			if (c != NULL)
741c79f192cSAdrian Chadd 				rxchan = c;
742c79f192cSAdrian Chadd 		}
743c79f192cSAdrian Chadd 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
744b032f27cSSam Leffler 			return;
745b032f27cSSam Leffler 		/*
746b032f27cSSam Leffler 		 * Count frame now that we know it's to be processed.
747b032f27cSSam Leffler 		 */
748b032f27cSSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
749b032f27cSSam Leffler 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
750b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_beacons);
751b032f27cSSam Leffler 		} else
752b032f27cSSam Leffler 			IEEE80211_NODE_STAT(ni, rx_proberesp);
753b032f27cSSam Leffler 		/*
754b032f27cSSam Leffler 		 * If scanning, just pass information to the scan module.
755b032f27cSSam Leffler 		 */
756b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN) {
757b032f27cSSam Leffler 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
758b032f27cSSam Leffler 				/*
759b032f27cSSam Leffler 				 * Actively scanning a channel marked passive;
760b032f27cSSam Leffler 				 * send a probe request now that we know there
761b032f27cSSam Leffler 				 * is 802.11 traffic present.
762b032f27cSSam Leffler 				 *
763b032f27cSSam Leffler 				 * XXX check if the beacon we recv'd gives
764b032f27cSSam Leffler 				 * us what we need and suppress the probe req
765b032f27cSSam Leffler 				 */
7669776aba3SBjoern A. Zeeb 				ieee80211_probe_curchan(vap, true);
767b032f27cSSam Leffler 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
768b032f27cSSam Leffler 			}
769c79f192cSAdrian Chadd 			ieee80211_add_scan(vap, rxchan, &scan, wh,
7702808a02bSAdrian Chadd 			    subtype, rssi, nf);
771b032f27cSSam Leffler 			return;
772b032f27cSSam Leffler 		}
773b032f27cSSam Leffler 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
774b032f27cSSam Leffler 			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
775b032f27cSSam Leffler 				/*
776b032f27cSSam Leffler 				 * Create a new entry in the neighbor table.
777172b009aSAdrian Chadd 				 *
778172b009aSAdrian Chadd 				 * XXX TODO:
779172b009aSAdrian Chadd 				 *
780172b009aSAdrian Chadd 				 * Here we're not scanning; so if we have an
781172b009aSAdrian Chadd 				 * SSID then make sure it matches our SSID.
782172b009aSAdrian Chadd 				 * Otherwise this code will match on all IBSS
783172b009aSAdrian Chadd 				 * beacons/probe requests for all SSIDs,
784172b009aSAdrian Chadd 				 * filling the node table with nodes that
785172b009aSAdrian Chadd 				 * aren't ours.
786b032f27cSSam Leffler 				 */
787e65d4e8aSAdrian Chadd 				if (ieee80211_ibss_node_check_new(ni, &scan)) {
788b032f27cSSam Leffler 					ni = ieee80211_add_neighbor(vap, wh, &scan);
789869897d2SAdrian Chadd 					/*
790869897d2SAdrian Chadd 					 * Send a probe request so we announce 11n
791869897d2SAdrian Chadd 					 * capabilities.
792869897d2SAdrian Chadd 					 */
793869897d2SAdrian Chadd 					ieee80211_send_probereq(ni, /* node */
794869897d2SAdrian Chadd 					    vap->iv_myaddr, /* SA */
795869897d2SAdrian Chadd 					    ni->ni_macaddr, /* DA */
796869897d2SAdrian Chadd 					    vap->iv_bss->ni_bssid, /* BSSID */
797869897d2SAdrian Chadd 					    vap->iv_bss->ni_essid,
798869897d2SAdrian Chadd 					    vap->iv_bss->ni_esslen); /* SSID */
799e65d4e8aSAdrian Chadd 				} else
800e65d4e8aSAdrian Chadd 					ni = NULL;
801869897d2SAdrian Chadd 
8020b5aeb89SAdrian Chadd 				/*
8030b5aeb89SAdrian Chadd 				 * Send a probe request so we announce 11n
8040b5aeb89SAdrian Chadd 				 * capabilities.
8050b5aeb89SAdrian Chadd 				 *
8060b5aeb89SAdrian Chadd 				 * Don't do this if we're scanning.
8070b5aeb89SAdrian Chadd 				 */
8080b5aeb89SAdrian Chadd 				if (! (ic->ic_flags & IEEE80211_F_SCAN))
8090b5aeb89SAdrian Chadd 					ieee80211_send_probereq(ni, /* node */
8100b5aeb89SAdrian Chadd 						vap->iv_myaddr, /* SA */
8110b5aeb89SAdrian Chadd 						ni->ni_macaddr, /* DA */
8120b5aeb89SAdrian Chadd 						vap->iv_bss->ni_bssid, /* BSSID */
8130b5aeb89SAdrian Chadd 						vap->iv_bss->ni_essid,
8140b5aeb89SAdrian Chadd 						vap->iv_bss->ni_esslen); /* SSID */
8150b5aeb89SAdrian Chadd 
816b032f27cSSam Leffler 			} else if (ni->ni_capinfo == 0) {
817b032f27cSSam Leffler 				/*
818b032f27cSSam Leffler 				 * Update faked node created on transmit.
819b032f27cSSam Leffler 				 * Note this also updates the tsf.
820b032f27cSSam Leffler 				 */
821b032f27cSSam Leffler 				ieee80211_init_neighbor(ni, wh, &scan);
822869897d2SAdrian Chadd 
823869897d2SAdrian Chadd 				/*
824869897d2SAdrian Chadd 				 * Send a probe request so we announce 11n
825869897d2SAdrian Chadd 				 * capabilities.
826869897d2SAdrian Chadd 				 */
827869897d2SAdrian Chadd 				ieee80211_send_probereq(ni, /* node */
828869897d2SAdrian Chadd 					vap->iv_myaddr, /* SA */
829869897d2SAdrian Chadd 					ni->ni_macaddr, /* DA */
830869897d2SAdrian Chadd 					vap->iv_bss->ni_bssid, /* BSSID */
831869897d2SAdrian Chadd 					vap->iv_bss->ni_essid,
832869897d2SAdrian Chadd 					vap->iv_bss->ni_esslen); /* SSID */
833b032f27cSSam Leffler 			} else {
834b032f27cSSam Leffler 				/*
835b032f27cSSam Leffler 				 * Record tsf for potential resync.
836b032f27cSSam Leffler 				 */
837b032f27cSSam Leffler 				memcpy(ni->ni_tstamp.data, scan.tstamp,
838b032f27cSSam Leffler 					sizeof(ni->ni_tstamp));
839b032f27cSSam Leffler 			}
840d71a1f7aSAdrian Chadd 			/*
841d71a1f7aSAdrian Chadd 			 * This isn't enabled yet - otherwise it would
842d71a1f7aSAdrian Chadd 			 * update the HT parameters and channel width
843d71a1f7aSAdrian Chadd 			 * from any node, which could lead to lots of
844d71a1f7aSAdrian Chadd 			 * strange behaviour if the 11n nodes aren't
845d71a1f7aSAdrian Chadd 			 * exactly configured to match.
846d71a1f7aSAdrian Chadd 			 */
847d71a1f7aSAdrian Chadd #if 0
848d71a1f7aSAdrian Chadd 			if (scan.htcap != NULL && scan.htinfo != NULL &&
849d71a1f7aSAdrian Chadd 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
85051172f62SAdrian Chadd 				ieee80211_ht_updateparams(ni,
85151172f62SAdrian Chadd 				    scan.htcap, scan.htinfo));
85251172f62SAdrian Chadd 				if (ieee80211_ht_updateparams_final(ni,
853d71a1f7aSAdrian Chadd 				    scan.htcap, scan.htinfo))
854d71a1f7aSAdrian Chadd 					ht_state_change = 1;
855d71a1f7aSAdrian Chadd 			}
85651172f62SAdrian Chadd 
85751172f62SAdrian Chadd 			/* XXX same for VHT? */
858d71a1f7aSAdrian Chadd #endif
859b032f27cSSam Leffler 			if (ni != NULL) {
860b032f27cSSam Leffler 				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
8615463c4a4SSam Leffler 				ni->ni_noise = nf;
862b032f27cSSam Leffler 			}
86332d384a4SAdrian Chadd 			/*
86432d384a4SAdrian Chadd 			 * Same here - the channel width change should
86532d384a4SAdrian Chadd 			 * be applied to the specific peer node, not
86632d384a4SAdrian Chadd 			 * to the ic.  Ie, the interface configuration
86732d384a4SAdrian Chadd 			 * should stay in its current channel width;
86832d384a4SAdrian Chadd 			 * but it should change the rate control and
86932d384a4SAdrian Chadd 			 * any queued frames for the given node only.
87032d384a4SAdrian Chadd 			 *
87132d384a4SAdrian Chadd 			 * Since there's no (current) way to inform
87232d384a4SAdrian Chadd 			 * the driver that a channel width change has
873a4641f4eSPedro F. Giffuni 			 * occurred for a single node, just stub this
87432d384a4SAdrian Chadd 			 * out.
87532d384a4SAdrian Chadd 			 */
87632d384a4SAdrian Chadd #if 0
877d71a1f7aSAdrian Chadd 			if (ht_state_change)
878d71a1f7aSAdrian Chadd 				ieee80211_update_chw(ic);
87932d384a4SAdrian Chadd #endif
880b032f27cSSam Leffler 		}
881b032f27cSSam Leffler 		break;
882b032f27cSSam Leffler 	}
883b032f27cSSam Leffler 
884b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
885b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
88607760642SSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
88707760642SSam Leffler 			    wh, NULL, "wrong state %s",
88807760642SSam Leffler 			    ieee80211_state_name[vap->iv_state]);
889b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
890b032f27cSSam Leffler 			return;
891b032f27cSSam Leffler 		}
892b032f27cSSam Leffler 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
893b032f27cSSam Leffler 			/* frame must be directed */
89407760642SSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
89507760642SSam Leffler 			    wh, NULL, "%s", "not unicast");
896b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
897b032f27cSSam Leffler 			return;
898b032f27cSSam Leffler 		}
899b032f27cSSam Leffler 
900b032f27cSSam Leffler 		/*
901b032f27cSSam Leffler 		 * prreq frame format
902b032f27cSSam Leffler 		 *	[tlv] ssid
903b032f27cSSam Leffler 		 *	[tlv] supported rates
904b032f27cSSam Leffler 		 *	[tlv] extended supported rates
905b032f27cSSam Leffler 		 */
906b032f27cSSam Leffler 		ssid = rates = xrates = NULL;
907b032f27cSSam Leffler 		while (efrm - frm > 1) {
908b032f27cSSam Leffler 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
909b032f27cSSam Leffler 			switch (*frm) {
910b032f27cSSam Leffler 			case IEEE80211_ELEMID_SSID:
911b032f27cSSam Leffler 				ssid = frm;
912b032f27cSSam Leffler 				break;
913b032f27cSSam Leffler 			case IEEE80211_ELEMID_RATES:
914b032f27cSSam Leffler 				rates = frm;
915b032f27cSSam Leffler 				break;
916b032f27cSSam Leffler 			case IEEE80211_ELEMID_XRATES:
917b032f27cSSam Leffler 				xrates = frm;
918b032f27cSSam Leffler 				break;
919b032f27cSSam Leffler 			}
920b032f27cSSam Leffler 			frm += frm[1] + 2;
921b032f27cSSam Leffler 		}
922b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
923b032f27cSSam Leffler 		if (xrates != NULL)
924b032f27cSSam Leffler 			IEEE80211_VERIFY_ELEMENT(xrates,
925b032f27cSSam Leffler 				IEEE80211_RATE_MAXSIZE - rates[1], return);
926b032f27cSSam Leffler 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
927b032f27cSSam Leffler 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
928b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
929b032f27cSSam Leffler 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
930b032f27cSSam Leffler 			    wh, NULL,
931b032f27cSSam Leffler 			    "%s", "no ssid with ssid suppression enabled");
932b032f27cSSam Leffler 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
933b032f27cSSam Leffler 			return;
934b032f27cSSam Leffler 		}
935b032f27cSSam Leffler 
936b032f27cSSam Leffler 		/* XXX find a better class or define it's own */
937b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
938b032f27cSSam Leffler 		    "%s", "recv probe req");
939b032f27cSSam Leffler 		/*
940b032f27cSSam Leffler 		 * Some legacy 11b clients cannot hack a complete
941b032f27cSSam Leffler 		 * probe response frame.  When the request includes
942b032f27cSSam Leffler 		 * only a bare-bones rate set, communicate this to
943b032f27cSSam Leffler 		 * the transmit side.
944b032f27cSSam Leffler 		 */
945b032f27cSSam Leffler 		ieee80211_send_proberesp(vap, wh->i_addr2,
946b032f27cSSam Leffler 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
947869897d2SAdrian Chadd 
948869897d2SAdrian Chadd 		/*
949869897d2SAdrian Chadd 		 * Note: we don't benefit from stashing the probe request
950869897d2SAdrian Chadd 		 * IEs away to use for IBSS negotiation, because we
951869897d2SAdrian Chadd 		 * typically don't get all of the IEs.
952869897d2SAdrian Chadd 		 */
953b032f27cSSam Leffler 		break;
954b032f27cSSam Leffler 
95596283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION:
95696283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
9579b405302SAdrian Chadd 		if ((ni == vap->iv_bss) &&
9589b405302SAdrian Chadd 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
959893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
960893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "unknown node");
961893c4d6eSBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
962893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
963893c4d6eSBernhard Schmidt 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
9640b5aeb89SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
965893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
966893c4d6eSBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
967893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
9680b5aeb89SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_DEBUG,
96907760642SSam Leffler 			    wh, NULL, "wrong state %s",
97007760642SSam Leffler 			    ieee80211_state_name[vap->iv_state]);
971b032f27cSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
972893c4d6eSBernhard Schmidt 		} else {
973893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
974893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
975b032f27cSSam Leffler 		}
97696283082SBernhard Schmidt 		break;
977b032f27cSSam Leffler 
978b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
979b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
98096283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
981b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
982665d5ae9SAndriy Voskoboinyk 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
98396283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
984b032f27cSSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
98596283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_AUTH:
98696283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
98707760642SSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
98807760642SSam Leffler 		    wh, NULL, "%s", "not handled");
989b032f27cSSam Leffler 		vap->iv_stats.is_rx_mgtdiscard++;
99096283082SBernhard Schmidt 		break;
991b032f27cSSam Leffler 
992b032f27cSSam Leffler 	default:
993b032f27cSSam Leffler 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
994b032f27cSSam Leffler 		    wh, "mgt", "subtype 0x%x not handled", subtype);
995b032f27cSSam Leffler 		vap->iv_stats.is_rx_badsubtype++;
996b032f27cSSam Leffler 		break;
997b032f27cSSam Leffler 	}
998b032f27cSSam Leffler }
999b032f27cSSam Leffler #undef IEEE80211_VERIFY_LENGTH
1000b032f27cSSam Leffler #undef IEEE80211_VERIFY_ELEMENT
100152ef06dcSSam Leffler 
100252ef06dcSSam Leffler static void
ahdemo_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)100352ef06dcSSam Leffler ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1004c79f192cSAdrian Chadd 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
100552ef06dcSSam Leffler {
100652ef06dcSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
100752ef06dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
100852ef06dcSSam Leffler 
100952ef06dcSSam Leffler 	/*
101052ef06dcSSam Leffler 	 * Process management frames when scanning; useful for doing
101152ef06dcSSam Leffler 	 * a site-survey.
101252ef06dcSSam Leffler 	 */
101352ef06dcSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)
1014c79f192cSAdrian Chadd 		adhoc_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
101596283082SBernhard Schmidt 	else {
101605ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_DEBUG
101705ea7a3eSBjoern A. Zeeb 		struct ieee80211_frame *wh;
101805ea7a3eSBjoern A. Zeeb 
101996283082SBernhard Schmidt 		wh = mtod(m0, struct ieee80211_frame *);
102005ea7a3eSBjoern A. Zeeb #endif
102196283082SBernhard Schmidt 		switch (subtype) {
102296283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
102396283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
102496283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
102596283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
102696283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
102796283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1028665d5ae9SAndriy Voskoboinyk 		case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
102996283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_BEACON:
103096283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_ATIM:
103196283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
103296283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_AUTH:
103396283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
103496283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_ACTION:
103596283082SBernhard Schmidt 		case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
103696283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
103796283082SBernhard Schmidt 			     wh, NULL, "%s", "not handled");
103852ef06dcSSam Leffler 			vap->iv_stats.is_rx_mgtdiscard++;
103996283082SBernhard Schmidt 			break;
104096283082SBernhard Schmidt 		default:
104196283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
104296283082SBernhard Schmidt 			     wh, "mgt", "subtype 0x%x not handled", subtype);
104396283082SBernhard Schmidt 			vap->iv_stats.is_rx_badsubtype++;
104496283082SBernhard Schmidt 			break;
104596283082SBernhard Schmidt 		}
104696283082SBernhard Schmidt 	}
104752ef06dcSSam Leffler }
104849eae5f7SSam Leffler 
104949eae5f7SSam Leffler static void
adhoc_recv_ctl(struct ieee80211_node * ni,struct mbuf * m,int subtype)10500917631fSRui Paulo adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
105149eae5f7SSam Leffler {
10520917631fSRui Paulo 
10530917631fSRui Paulo 	switch (subtype) {
10540917631fSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
10550917631fSRui Paulo 		ieee80211_recv_bar(ni, m);
10560917631fSRui Paulo 		break;
10570917631fSRui Paulo 	}
105849eae5f7SSam Leffler }
1059