ieee80211_adhoc.c revision 191546
1/*-
2 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27#ifdef __FreeBSD__
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_adhoc.c 191546 2009-04-26 21:46:04Z sam $");
29#endif
30
31/*
32 * IEEE 802.11 IBSS mode support.
33 */
34#include "opt_inet.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/mbuf.h>
40#include <sys/malloc.h>
41#include <sys/kernel.h>
42
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/endian.h>
46#include <sys/errno.h>
47#include <sys/proc.h>
48#include <sys/sysctl.h>
49
50#include <net/if.h>
51#include <net/if_media.h>
52#include <net/if_llc.h>
53#include <net/ethernet.h>
54
55#include <net/bpf.h>
56
57#include <net80211/ieee80211_var.h>
58#include <net80211/ieee80211_adhoc.h>
59#include <net80211/ieee80211_input.h>
60#ifdef IEEE80211_SUPPORT_SUPERG
61#include <net80211/ieee80211_superg.h>
62#endif
63#ifdef IEEE80211_SUPPORT_TDMA
64#include <net80211/ieee80211_tdma.h>
65#endif
66
67#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
68
69static	void adhoc_vattach(struct ieee80211vap *);
70static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
71static int adhoc_input(struct ieee80211_node *, struct mbuf *,
72	int rssi, int noise, uint32_t rstamp);
73static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74	int subtype, int rssi, int noise, uint32_t rstamp);
75static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76	int subtype, int rssi, int noise, uint32_t rstamp);
77static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78
79void
80ieee80211_adhoc_attach(struct ieee80211com *ic)
81{
82	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
83	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
84}
85
86void
87ieee80211_adhoc_detach(struct ieee80211com *ic)
88{
89}
90
91static void
92adhoc_vdetach(struct ieee80211vap *vap)
93{
94}
95
96static void
97adhoc_vattach(struct ieee80211vap *vap)
98{
99	vap->iv_newstate = adhoc_newstate;
100	vap->iv_input = adhoc_input;
101	if (vap->iv_opmode == IEEE80211_M_IBSS)
102		vap->iv_recv_mgmt = adhoc_recv_mgmt;
103	else
104		vap->iv_recv_mgmt = ahdemo_recv_mgmt;
105	vap->iv_recv_ctl = adhoc_recv_ctl;
106	vap->iv_opdetach = adhoc_vdetach;
107#ifdef IEEE80211_SUPPORT_TDMA
108	/*
109	 * Throw control to tdma support.  Note we do this
110	 * after setting up our callbacks so it can piggyback
111	 * on top of us.
112	 */
113	if (vap->iv_caps & IEEE80211_C_TDMA)
114		ieee80211_tdma_vattach(vap);
115#endif
116}
117
118static void
119sta_leave(void *arg, struct ieee80211_node *ni)
120{
121	struct ieee80211vap *vap = arg;
122
123	if (ni->ni_vap == vap && ni != vap->iv_bss)
124		ieee80211_node_leave(ni);
125}
126
127/*
128 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
129 */
130static int
131adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
132{
133	struct ieee80211com *ic = vap->iv_ic;
134	struct ieee80211_node *ni;
135	enum ieee80211_state ostate;
136
137	IEEE80211_LOCK_ASSERT(vap->iv_ic);
138
139	ostate = vap->iv_state;
140	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
141	    __func__, ieee80211_state_name[ostate],
142	    ieee80211_state_name[nstate], arg);
143	vap->iv_state = nstate;			/* state transition */
144	if (ostate != IEEE80211_S_SCAN)
145		ieee80211_cancel_scan(vap);	/* background scan */
146	ni = vap->iv_bss;			/* NB: no reference held */
147	switch (nstate) {
148	case IEEE80211_S_INIT:
149		switch (ostate) {
150		case IEEE80211_S_SCAN:
151			ieee80211_cancel_scan(vap);
152			break;
153		default:
154			break;
155		}
156		if (ostate != IEEE80211_S_INIT) {
157			/* NB: optimize INIT -> INIT case */
158			ieee80211_reset_bss(vap);
159		}
160		break;
161	case IEEE80211_S_SCAN:
162		switch (ostate) {
163		case IEEE80211_S_RUN:		/* beacon miss */
164			/* purge station table; entries are stale */
165			ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
166			/* fall thru... */
167		case IEEE80211_S_INIT:
168			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
169			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
170				/*
171				 * Already have a channel; bypass the
172				 * scan and startup immediately.
173				 */
174				ieee80211_create_ibss(vap, vap->iv_des_chan);
175				break;
176			}
177			/*
178			 * Initiate a scan.  We can come here as a result
179			 * of an IEEE80211_IOC_SCAN_REQ too in which case
180			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
181			 * and the scan request parameters will be present
182			 * in iv_scanreq.  Otherwise we do the default.
183			 */
184			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
185				ieee80211_check_scan(vap,
186				    vap->iv_scanreq_flags,
187				    vap->iv_scanreq_duration,
188				    vap->iv_scanreq_mindwell,
189				    vap->iv_scanreq_maxdwell,
190				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
191				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
192			} else
193				ieee80211_check_scan_current(vap);
194			break;
195		case IEEE80211_S_SCAN:
196			/*
197			 * This can happen because of a change in state
198			 * that requires a reset.  Trigger a new scan
199			 * unless we're in manual roaming mode in which
200			 * case an application must issue an explicit request.
201			 */
202			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203				ieee80211_check_scan_current(vap);
204			break;
205		default:
206			goto invalid;
207		}
208		break;
209	case IEEE80211_S_RUN:
210		if (vap->iv_flags & IEEE80211_F_WPA) {
211			/* XXX validate prerequisites */
212		}
213		switch (ostate) {
214		case IEEE80211_S_SCAN:
215#ifdef IEEE80211_DEBUG
216			if (ieee80211_msg_debug(vap)) {
217				ieee80211_note(vap,
218				    "synchronized with %s ssid ",
219				    ether_sprintf(ni->ni_bssid));
220				ieee80211_print_essid(vap->iv_bss->ni_essid,
221				    ni->ni_esslen);
222				/* XXX MCS/HT */
223				printf(" channel %d start %uMb\n",
224				    ieee80211_chan2ieee(ic, ic->ic_curchan),
225				    IEEE80211_RATE2MBS(ni->ni_txrate));
226			}
227#endif
228			break;
229		default:
230			goto invalid;
231		}
232		/*
233		 * When 802.1x is not in use mark the port authorized
234		 * at this point so traffic can flow.
235		 */
236		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
237			ieee80211_node_authorize(ni);
238		/*
239		 * Fake association when joining an existing bss.
240		 */
241		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
242		    ic->ic_newassoc != NULL)
243			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
244		break;
245	case IEEE80211_S_SLEEP:
246		ieee80211_sta_pwrsave(vap, 0);
247		break;
248	default:
249	invalid:
250		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
251		    "%s: unexpected state transition %s -> %s\n", __func__,
252		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
253		break;
254	}
255	return 0;
256}
257
258/*
259 * Decide if a received management frame should be
260 * printed when debugging is enabled.  This filters some
261 * of the less interesting frames that come frequently
262 * (e.g. beacons).
263 */
264static __inline int
265doprint(struct ieee80211vap *vap, int subtype)
266{
267	switch (subtype) {
268	case IEEE80211_FC0_SUBTYPE_BEACON:
269		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
270	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
271		return 1;
272	}
273	return 1;
274}
275
276/*
277 * Process a received frame.  The node associated with the sender
278 * should be supplied.  If nothing was found in the node table then
279 * the caller is assumed to supply a reference to iv_bss instead.
280 * The RSSI and a timestamp are also supplied.  The RSSI data is used
281 * during AP scanning to select a AP to associate with; it can have
282 * any units so long as values have consistent units and higher values
283 * mean ``better signal''.  The receive timestamp is currently not used
284 * by the 802.11 layer.
285 */
286static int
287adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
288	int rssi, int noise, uint32_t rstamp)
289{
290#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
291#define	HAS_SEQ(type)	((type & 0x4) == 0)
292	struct ieee80211vap *vap = ni->ni_vap;
293	struct ieee80211com *ic = ni->ni_ic;
294	struct ifnet *ifp = vap->iv_ifp;
295	struct ieee80211_frame *wh;
296	struct ieee80211_key *key;
297	struct ether_header *eh;
298	int hdrspace, need_tap;
299	uint8_t dir, type, subtype, qos;
300	uint8_t *bssid;
301	uint16_t rxseq;
302
303	if (m->m_flags & M_AMPDU_MPDU) {
304		/*
305		 * Fastpath for A-MPDU reorder q resubmission.  Frames
306		 * w/ M_AMPDU_MPDU marked have already passed through
307		 * here but were received out of order and been held on
308		 * the reorder queue.  When resubmitted they are marked
309		 * with the M_AMPDU_MPDU flag and we can bypass most of
310		 * the normal processing.
311		 */
312		wh = mtod(m, struct ieee80211_frame *);
313		type = IEEE80211_FC0_TYPE_DATA;
314		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
315		subtype = IEEE80211_FC0_SUBTYPE_QOS;
316		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
317		goto resubmit_ampdu;
318	}
319
320	KASSERT(ni != NULL, ("null node"));
321	ni->ni_inact = ni->ni_inact_reload;
322
323	need_tap = 1;			/* mbuf need to be tapped. */
324	type = -1;			/* undefined */
325
326	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
327		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
328		    ni->ni_macaddr, NULL,
329		    "too short (1): len %u", m->m_pkthdr.len);
330		vap->iv_stats.is_rx_tooshort++;
331		goto out;
332	}
333	/*
334	 * Bit of a cheat here, we use a pointer for a 3-address
335	 * frame format but don't reference fields past outside
336	 * ieee80211_frame_min w/o first validating the data is
337	 * present.
338	 */
339	wh = mtod(m, struct ieee80211_frame *);
340
341	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
342	    IEEE80211_FC0_VERSION_0) {
343		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
344		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
345		vap->iv_stats.is_rx_badversion++;
346		goto err;
347	}
348
349	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
350	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
351	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
352	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
353		if (dir != IEEE80211_FC1_DIR_NODS)
354			bssid = wh->i_addr1;
355		else if (type == IEEE80211_FC0_TYPE_CTL)
356			bssid = wh->i_addr1;
357		else {
358			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
359				IEEE80211_DISCARD_MAC(vap,
360				    IEEE80211_MSG_ANY, ni->ni_macaddr,
361				    NULL, "too short (2): len %u",
362				    m->m_pkthdr.len);
363				vap->iv_stats.is_rx_tooshort++;
364				goto out;
365			}
366			bssid = wh->i_addr3;
367		}
368		/*
369		 * Validate the bssid.
370		 */
371		if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
372		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
373			/* not interested in */
374			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
375			    bssid, NULL, "%s", "not to bss");
376			vap->iv_stats.is_rx_wrongbss++;
377			goto out;
378		}
379		/*
380		 * Data frame, cons up a node when it doesn't
381		 * exist. This should probably done after an ACL check.
382		 */
383		if (type == IEEE80211_FC0_TYPE_DATA &&
384		    ni == vap->iv_bss &&
385		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
386			/*
387			 * Beware of frames that come in too early; we
388			 * can receive broadcast frames and creating sta
389			 * entries will blow up because there is no bss
390			 * channel yet.
391			 */
392			if (vap->iv_state != IEEE80211_S_RUN) {
393				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
394				    wh, "data", "not in RUN state (%s)",
395				    ieee80211_state_name[vap->iv_state]);
396				vap->iv_stats.is_rx_badstate++;
397				goto err;
398			}
399			/*
400			 * Fake up a node for this newly
401			 * discovered member of the IBSS.
402			 */
403			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
404			if (ni == NULL) {
405				/* NB: stat kept for alloc failure */
406				goto err;
407			}
408		}
409		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
410		ni->ni_noise = noise;
411		ni->ni_rstamp = rstamp;
412		if (HAS_SEQ(type)) {
413			uint8_t tid = ieee80211_gettid(wh);
414			if (IEEE80211_QOS_HAS_SEQ(wh) &&
415			    TID_TO_WME_AC(tid) >= WME_AC_VI)
416				ic->ic_wme.wme_hipri_traffic++;
417			rxseq = le16toh(*(uint16_t *)wh->i_seq);
418			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
419			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
420			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
421				/* duplicate, discard */
422				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
423				    bssid, "duplicate",
424				    "seqno <%u,%u> fragno <%u,%u> tid %u",
425				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
426				    ni->ni_rxseqs[tid] >>
427					IEEE80211_SEQ_SEQ_SHIFT,
428				    rxseq & IEEE80211_SEQ_FRAG_MASK,
429				    ni->ni_rxseqs[tid] &
430					IEEE80211_SEQ_FRAG_MASK,
431				    tid);
432				vap->iv_stats.is_rx_dup++;
433				IEEE80211_NODE_STAT(ni, rx_dup);
434				goto out;
435			}
436			ni->ni_rxseqs[tid] = rxseq;
437		}
438	}
439
440	switch (type) {
441	case IEEE80211_FC0_TYPE_DATA:
442		hdrspace = ieee80211_hdrspace(ic, wh);
443		if (m->m_len < hdrspace &&
444		    (m = m_pullup(m, hdrspace)) == NULL) {
445			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
446			    ni->ni_macaddr, NULL,
447			    "data too short: expecting %u", hdrspace);
448			vap->iv_stats.is_rx_tooshort++;
449			goto out;		/* XXX */
450		}
451		if (dir != IEEE80211_FC1_DIR_NODS) {
452			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
453			    wh, "data", "incorrect dir 0x%x", dir);
454			vap->iv_stats.is_rx_wrongdir++;
455			goto out;
456		}
457		/* XXX no power-save support */
458
459		/*
460		 * Handle A-MPDU re-ordering.  If the frame is to be
461		 * processed directly then ieee80211_ampdu_reorder
462		 * will return 0; otherwise it has consumed the mbuf
463		 * and we should do nothing more with it.
464		 */
465		if ((m->m_flags & M_AMPDU) &&
466		    ieee80211_ampdu_reorder(ni, m) != 0) {
467			m = NULL;
468			goto out;
469		}
470	resubmit_ampdu:
471
472		/*
473		 * Handle privacy requirements.  Note that we
474		 * must not be preempted from here until after
475		 * we (potentially) call ieee80211_crypto_demic;
476		 * otherwise we may violate assumptions in the
477		 * crypto cipher modules used to do delayed update
478		 * of replay sequence numbers.
479		 */
480		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
481			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
482				/*
483				 * Discard encrypted frames when privacy is off.
484				 */
485				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
486				    wh, "WEP", "%s", "PRIVACY off");
487				vap->iv_stats.is_rx_noprivacy++;
488				IEEE80211_NODE_STAT(ni, rx_noprivacy);
489				goto out;
490			}
491			key = ieee80211_crypto_decap(ni, m, hdrspace);
492			if (key == NULL) {
493				/* NB: stats+msgs handled in crypto_decap */
494				IEEE80211_NODE_STAT(ni, rx_wepfail);
495				goto out;
496			}
497			wh = mtod(m, struct ieee80211_frame *);
498			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
499		} else {
500			/* XXX M_WEP and IEEE80211_F_PRIVACY */
501			key = NULL;
502		}
503
504		/*
505		 * Save QoS bits for use below--before we strip the header.
506		 */
507		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
508			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
509			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
510			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
511		} else
512			qos = 0;
513
514		/*
515		 * Next up, any fragmentation.
516		 */
517		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
518			m = ieee80211_defrag(ni, m, hdrspace);
519			if (m == NULL) {
520				/* Fragment dropped or frame not complete yet */
521				goto out;
522			}
523		}
524		wh = NULL;		/* no longer valid, catch any uses */
525
526		/*
527		 * Next strip any MSDU crypto bits.
528		 */
529		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
530			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
531			    ni->ni_macaddr, "data", "%s", "demic error");
532			vap->iv_stats.is_rx_demicfail++;
533			IEEE80211_NODE_STAT(ni, rx_demicfail);
534			goto out;
535		}
536
537		/* copy to listener after decrypt */
538		if (bpf_peers_present(vap->iv_rawbpf))
539			bpf_mtap(vap->iv_rawbpf, m);
540		need_tap = 0;
541
542		/*
543		 * Finally, strip the 802.11 header.
544		 */
545		m = ieee80211_decap(vap, m, hdrspace);
546		if (m == NULL) {
547			/* XXX mask bit to check for both */
548			/* don't count Null data frames as errors */
549			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
550			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
551				goto out;
552			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
553			    ni->ni_macaddr, "data", "%s", "decap error");
554			vap->iv_stats.is_rx_decap++;
555			IEEE80211_NODE_STAT(ni, rx_decap);
556			goto err;
557		}
558		eh = mtod(m, struct ether_header *);
559		if (!ieee80211_node_is_authorized(ni)) {
560			/*
561			 * Deny any non-PAE frames received prior to
562			 * authorization.  For open/shared-key
563			 * authentication the port is mark authorized
564			 * after authentication completes.  For 802.1x
565			 * the port is not marked authorized by the
566			 * authenticator until the handshake has completed.
567			 */
568			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
569				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
570				    eh->ether_shost, "data",
571				    "unauthorized port: ether type 0x%x len %u",
572				    eh->ether_type, m->m_pkthdr.len);
573				vap->iv_stats.is_rx_unauth++;
574				IEEE80211_NODE_STAT(ni, rx_unauth);
575				goto err;
576			}
577		} else {
578			/*
579			 * When denying unencrypted frames, discard
580			 * any non-PAE frames received without encryption.
581			 */
582			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
583			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
584			    eh->ether_type != htons(ETHERTYPE_PAE)) {
585				/*
586				 * Drop unencrypted frames.
587				 */
588				vap->iv_stats.is_rx_unencrypted++;
589				IEEE80211_NODE_STAT(ni, rx_unencrypted);
590				goto out;
591			}
592		}
593		/* XXX require HT? */
594		if (qos & IEEE80211_QOS_AMSDU) {
595			m = ieee80211_decap_amsdu(ni, m);
596			if (m == NULL)
597				return IEEE80211_FC0_TYPE_DATA;
598		} else {
599#ifdef IEEE80211_SUPPORT_SUPERG
600			m = ieee80211_decap_fastframe(vap, ni, m);
601			if (m == NULL)
602				return IEEE80211_FC0_TYPE_DATA;
603#endif
604		}
605		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
606			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
607		else
608			ieee80211_deliver_data(vap, ni, m);
609		return IEEE80211_FC0_TYPE_DATA;
610
611	case IEEE80211_FC0_TYPE_MGT:
612		vap->iv_stats.is_rx_mgmt++;
613		IEEE80211_NODE_STAT(ni, rx_mgmt);
614		if (dir != IEEE80211_FC1_DIR_NODS) {
615			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
616			    wh, "data", "incorrect dir 0x%x", dir);
617			vap->iv_stats.is_rx_wrongdir++;
618			goto err;
619		}
620		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
621			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
622			    ni->ni_macaddr, "mgt", "too short: len %u",
623			    m->m_pkthdr.len);
624			vap->iv_stats.is_rx_tooshort++;
625			goto out;
626		}
627#ifdef IEEE80211_DEBUG
628		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
629		    ieee80211_msg_dumppkts(vap)) {
630			if_printf(ifp, "received %s from %s rssi %d\n",
631			    ieee80211_mgt_subtype_name[subtype >>
632				IEEE80211_FC0_SUBTYPE_SHIFT],
633			    ether_sprintf(wh->i_addr2), rssi);
634		}
635#endif
636		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
637			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
638			    wh, NULL, "%s", "WEP set but not permitted");
639			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
640			goto out;
641		}
642		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
643		goto out;
644
645	case IEEE80211_FC0_TYPE_CTL:
646		vap->iv_stats.is_rx_ctl++;
647		IEEE80211_NODE_STAT(ni, rx_ctrl);
648		vap->iv_recv_ctl(ni, m, subtype);
649		goto out;
650	default:
651		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
652		    wh, "bad", "frame type 0x%x", type);
653		/* should not come here */
654		break;
655	}
656err:
657	ifp->if_ierrors++;
658out:
659	if (m != NULL) {
660		if (need_tap && bpf_peers_present(vap->iv_rawbpf))
661			bpf_mtap(vap->iv_rawbpf, m);
662		m_freem(m);
663	}
664	return type;
665#undef SEQ_LEQ
666}
667
668static int
669is11bclient(const uint8_t *rates, const uint8_t *xrates)
670{
671	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
672	int i;
673
674	/* NB: the 11b clients we care about will not have xrates */
675	if (xrates != NULL || rates == NULL)
676		return 0;
677	for (i = 0; i < rates[1]; i++) {
678		int r = rates[2+i] & IEEE80211_RATE_VAL;
679		if (r > 2*11 || ((1<<r) & brates) == 0)
680			return 0;
681	}
682	return 1;
683}
684
685static void
686adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
687	int subtype, int rssi, int noise, uint32_t rstamp)
688{
689	struct ieee80211vap *vap = ni->ni_vap;
690	struct ieee80211com *ic = ni->ni_ic;
691	struct ieee80211_frame *wh;
692	uint8_t *frm, *efrm, *sfrm;
693	uint8_t *ssid, *rates, *xrates;
694
695	wh = mtod(m0, struct ieee80211_frame *);
696	frm = (uint8_t *)&wh[1];
697	efrm = mtod(m0, uint8_t *) + m0->m_len;
698	switch (subtype) {
699	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
700	case IEEE80211_FC0_SUBTYPE_BEACON: {
701		struct ieee80211_scanparams scan;
702		/*
703		 * We process beacon/probe response
704		 * frames to discover neighbors.
705		 */
706		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
707			return;
708		/*
709		 * Count frame now that we know it's to be processed.
710		 */
711		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
712			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
713			IEEE80211_NODE_STAT(ni, rx_beacons);
714		} else
715			IEEE80211_NODE_STAT(ni, rx_proberesp);
716		/*
717		 * If scanning, just pass information to the scan module.
718		 */
719		if (ic->ic_flags & IEEE80211_F_SCAN) {
720			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
721				/*
722				 * Actively scanning a channel marked passive;
723				 * send a probe request now that we know there
724				 * is 802.11 traffic present.
725				 *
726				 * XXX check if the beacon we recv'd gives
727				 * us what we need and suppress the probe req
728				 */
729				ieee80211_probe_curchan(vap, 1);
730				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
731			}
732			ieee80211_add_scan(vap, &scan, wh,
733				subtype, rssi, noise, rstamp);
734			return;
735		}
736		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
737			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
738				/*
739				 * Create a new entry in the neighbor table.
740				 */
741				ni = ieee80211_add_neighbor(vap, wh, &scan);
742			} else if (ni->ni_capinfo == 0) {
743				/*
744				 * Update faked node created on transmit.
745				 * Note this also updates the tsf.
746				 */
747				ieee80211_init_neighbor(ni, wh, &scan);
748			} else {
749				/*
750				 * Record tsf for potential resync.
751				 */
752				memcpy(ni->ni_tstamp.data, scan.tstamp,
753					sizeof(ni->ni_tstamp));
754			}
755			if (ni != NULL) {
756				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
757				ni->ni_noise = noise;
758				ni->ni_rstamp = rstamp;
759			}
760		}
761		break;
762	}
763
764	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
765		if (vap->iv_state != IEEE80211_S_RUN) {
766			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
767			    wh, NULL, "wrong state %s",
768			    ieee80211_state_name[vap->iv_state]);
769			vap->iv_stats.is_rx_mgtdiscard++;
770			return;
771		}
772		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
773			/* frame must be directed */
774			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
775			    wh, NULL, "%s", "not unicast");
776			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
777			return;
778		}
779
780		/*
781		 * prreq frame format
782		 *	[tlv] ssid
783		 *	[tlv] supported rates
784		 *	[tlv] extended supported rates
785		 */
786		ssid = rates = xrates = NULL;
787		sfrm = frm;
788		while (efrm - frm > 1) {
789			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
790			switch (*frm) {
791			case IEEE80211_ELEMID_SSID:
792				ssid = frm;
793				break;
794			case IEEE80211_ELEMID_RATES:
795				rates = frm;
796				break;
797			case IEEE80211_ELEMID_XRATES:
798				xrates = frm;
799				break;
800			}
801			frm += frm[1] + 2;
802		}
803		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
804		if (xrates != NULL)
805			IEEE80211_VERIFY_ELEMENT(xrates,
806				IEEE80211_RATE_MAXSIZE - rates[1], return);
807		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
808		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
809		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
810			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
811			    wh, NULL,
812			    "%s", "no ssid with ssid suppression enabled");
813			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
814			return;
815		}
816
817		/* XXX find a better class or define it's own */
818		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
819		    "%s", "recv probe req");
820		/*
821		 * Some legacy 11b clients cannot hack a complete
822		 * probe response frame.  When the request includes
823		 * only a bare-bones rate set, communicate this to
824		 * the transmit side.
825		 */
826		ieee80211_send_proberesp(vap, wh->i_addr2,
827		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
828		break;
829
830	case IEEE80211_FC0_SUBTYPE_ACTION: {
831		const struct ieee80211_action *ia;
832
833		if (vap->iv_state != IEEE80211_S_RUN) {
834			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
835			    wh, NULL, "wrong state %s",
836			    ieee80211_state_name[vap->iv_state]);
837			vap->iv_stats.is_rx_mgtdiscard++;
838			return;
839		}
840		/*
841		 * action frame format:
842		 *	[1] category
843		 *	[1] action
844		 *	[tlv] parameters
845		 */
846		IEEE80211_VERIFY_LENGTH(efrm - frm,
847			sizeof(struct ieee80211_action), return);
848		ia = (const struct ieee80211_action *) frm;
849
850		vap->iv_stats.is_rx_action++;
851		IEEE80211_NODE_STAT(ni, rx_action);
852
853		/* verify frame payloads but defer processing */
854		/* XXX maybe push this to method */
855		switch (ia->ia_category) {
856		case IEEE80211_ACTION_CAT_BA:
857			switch (ia->ia_action) {
858			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
859				IEEE80211_VERIFY_LENGTH(efrm - frm,
860				    sizeof(struct ieee80211_action_ba_addbarequest),
861				    return);
862				break;
863			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
864				IEEE80211_VERIFY_LENGTH(efrm - frm,
865				    sizeof(struct ieee80211_action_ba_addbaresponse),
866				    return);
867				break;
868			case IEEE80211_ACTION_BA_DELBA:
869				IEEE80211_VERIFY_LENGTH(efrm - frm,
870				    sizeof(struct ieee80211_action_ba_delba),
871				    return);
872				break;
873			}
874			break;
875		case IEEE80211_ACTION_CAT_HT:
876			switch (ia->ia_action) {
877			case IEEE80211_ACTION_HT_TXCHWIDTH:
878				IEEE80211_VERIFY_LENGTH(efrm - frm,
879				    sizeof(struct ieee80211_action_ht_txchwidth),
880				    return);
881				break;
882			}
883			break;
884		}
885		ic->ic_recv_action(ni, frm, efrm);
886		break;
887	}
888
889	case IEEE80211_FC0_SUBTYPE_AUTH:
890	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
891	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
892	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
893	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
894	case IEEE80211_FC0_SUBTYPE_DEAUTH:
895	case IEEE80211_FC0_SUBTYPE_DISASSOC:
896		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
897		     wh, NULL, "%s", "not handled");
898		vap->iv_stats.is_rx_mgtdiscard++;
899		return;
900
901	default:
902		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
903		     wh, "mgt", "subtype 0x%x not handled", subtype);
904		vap->iv_stats.is_rx_badsubtype++;
905		break;
906	}
907}
908#undef IEEE80211_VERIFY_LENGTH
909#undef IEEE80211_VERIFY_ELEMENT
910
911static void
912ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
913	int subtype, int rssi, int noise, uint32_t rstamp)
914{
915	struct ieee80211vap *vap = ni->ni_vap;
916	struct ieee80211com *ic = ni->ni_ic;
917
918	/*
919	 * Process management frames when scanning; useful for doing
920	 * a site-survey.
921	 */
922	if (ic->ic_flags & IEEE80211_F_SCAN)
923		adhoc_recv_mgmt(ni, m0, subtype, rssi, noise, rstamp);
924	else
925		vap->iv_stats.is_rx_mgtdiscard++;
926}
927
928static void
929adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
930{
931}
932