ieee80211_adhoc.c revision 244061
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 244061 2012-12-09 23:56:29Z adrian $");
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#include <net80211/ieee80211_sta.h>
67
68#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
69
70static	void adhoc_vattach(struct ieee80211vap *);
71static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
72static int adhoc_input(struct ieee80211_node *, struct mbuf *, int, int);
73static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74	int subtype, int, int);
75static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76	int subtype, int, int);
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		vap->iv_sta_ps(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, int rssi, int nf)
288{
289#define	HAS_SEQ(type)	((type & 0x4) == 0)
290	struct ieee80211vap *vap = ni->ni_vap;
291	struct ieee80211com *ic = ni->ni_ic;
292	struct ifnet *ifp = vap->iv_ifp;
293	struct ieee80211_frame *wh;
294	struct ieee80211_key *key;
295	struct ether_header *eh;
296	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
297	uint8_t dir, type, subtype, qos;
298	uint8_t *bssid;
299	uint16_t rxseq;
300
301	if (m->m_flags & M_AMPDU_MPDU) {
302		/*
303		 * Fastpath for A-MPDU reorder q resubmission.  Frames
304		 * w/ M_AMPDU_MPDU marked have already passed through
305		 * here but were received out of order and been held on
306		 * the reorder queue.  When resubmitted they are marked
307		 * with the M_AMPDU_MPDU flag and we can bypass most of
308		 * the normal processing.
309		 */
310		wh = mtod(m, struct ieee80211_frame *);
311		type = IEEE80211_FC0_TYPE_DATA;
312		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
313		subtype = IEEE80211_FC0_SUBTYPE_QOS;
314		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
315		goto resubmit_ampdu;
316	}
317
318	KASSERT(ni != NULL, ("null node"));
319	ni->ni_inact = ni->ni_inact_reload;
320
321	type = -1;			/* undefined */
322
323	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
324		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
325		    ni->ni_macaddr, NULL,
326		    "too short (1): len %u", m->m_pkthdr.len);
327		vap->iv_stats.is_rx_tooshort++;
328		goto out;
329	}
330	/*
331	 * Bit of a cheat here, we use a pointer for a 3-address
332	 * frame format but don't reference fields past outside
333	 * ieee80211_frame_min w/o first validating the data is
334	 * present.
335	 */
336	wh = mtod(m, struct ieee80211_frame *);
337
338	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
339	    IEEE80211_FC0_VERSION_0) {
340		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
341		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
342		    wh->i_fc[0], wh->i_fc[1]);
343		vap->iv_stats.is_rx_badversion++;
344		goto err;
345	}
346
347	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
348	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
349	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
350	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
351		if (dir != IEEE80211_FC1_DIR_NODS)
352			bssid = wh->i_addr1;
353		else if (type == IEEE80211_FC0_TYPE_CTL)
354			bssid = wh->i_addr1;
355		else {
356			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
357				IEEE80211_DISCARD_MAC(vap,
358				    IEEE80211_MSG_ANY, ni->ni_macaddr,
359				    NULL, "too short (2): len %u",
360				    m->m_pkthdr.len);
361				vap->iv_stats.is_rx_tooshort++;
362				goto out;
363			}
364			bssid = wh->i_addr3;
365		}
366		/*
367		 * Validate the bssid.
368		 */
369		if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
370		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
371			/* not interested in */
372			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
373			    bssid, NULL, "%s", "not to bss");
374			vap->iv_stats.is_rx_wrongbss++;
375			goto out;
376		}
377		/*
378		 * Data frame, cons up a node when it doesn't
379		 * exist. This should probably done after an ACL check.
380		 */
381		if (type == IEEE80211_FC0_TYPE_DATA &&
382		    ni == vap->iv_bss &&
383		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
384			/*
385			 * Beware of frames that come in too early; we
386			 * can receive broadcast frames and creating sta
387			 * entries will blow up because there is no bss
388			 * channel yet.
389			 */
390			if (vap->iv_state != IEEE80211_S_RUN) {
391				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
392				    wh, "data", "not in RUN state (%s)",
393				    ieee80211_state_name[vap->iv_state]);
394				vap->iv_stats.is_rx_badstate++;
395				goto err;
396			}
397			/*
398			 * Fake up a node for this newly
399			 * discovered member of the IBSS.
400			 */
401			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
402			if (ni == NULL) {
403				/* NB: stat kept for alloc failure */
404				goto err;
405			}
406		}
407		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
408		ni->ni_noise = nf;
409		if (HAS_SEQ(type)) {
410			uint8_t tid = ieee80211_gettid(wh);
411			if (IEEE80211_QOS_HAS_SEQ(wh) &&
412			    TID_TO_WME_AC(tid) >= WME_AC_VI)
413				ic->ic_wme.wme_hipri_traffic++;
414			rxseq = le16toh(*(uint16_t *)wh->i_seq);
415			if (! ieee80211_check_rxseq(ni, wh)) {
416				/* duplicate, discard */
417				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
418				    bssid, "duplicate",
419				    "seqno <%u,%u> fragno <%u,%u> tid %u",
420				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
421				    ni->ni_rxseqs[tid] >>
422					IEEE80211_SEQ_SEQ_SHIFT,
423				    rxseq & IEEE80211_SEQ_FRAG_MASK,
424				    ni->ni_rxseqs[tid] &
425					IEEE80211_SEQ_FRAG_MASK,
426				    tid);
427				vap->iv_stats.is_rx_dup++;
428				IEEE80211_NODE_STAT(ni, rx_dup);
429				goto out;
430			}
431			ni->ni_rxseqs[tid] = rxseq;
432		}
433	}
434
435	switch (type) {
436	case IEEE80211_FC0_TYPE_DATA:
437		hdrspace = ieee80211_hdrspace(ic, wh);
438		if (m->m_len < hdrspace &&
439		    (m = m_pullup(m, hdrspace)) == NULL) {
440			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
441			    ni->ni_macaddr, NULL,
442			    "data too short: expecting %u", hdrspace);
443			vap->iv_stats.is_rx_tooshort++;
444			goto out;		/* XXX */
445		}
446		if (dir != IEEE80211_FC1_DIR_NODS) {
447			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
448			    wh, "data", "incorrect dir 0x%x", dir);
449			vap->iv_stats.is_rx_wrongdir++;
450			goto out;
451		}
452		/* XXX no power-save support */
453
454		/*
455		 * Handle A-MPDU re-ordering.  If the frame is to be
456		 * processed directly then ieee80211_ampdu_reorder
457		 * will return 0; otherwise it has consumed the mbuf
458		 * and we should do nothing more with it.
459		 */
460		if ((m->m_flags & M_AMPDU) &&
461		    ieee80211_ampdu_reorder(ni, m) != 0) {
462			m = NULL;
463			goto out;
464		}
465	resubmit_ampdu:
466
467		/*
468		 * Handle privacy requirements.  Note that we
469		 * must not be preempted from here until after
470		 * we (potentially) call ieee80211_crypto_demic;
471		 * otherwise we may violate assumptions in the
472		 * crypto cipher modules used to do delayed update
473		 * of replay sequence numbers.
474		 */
475		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
476			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
477				/*
478				 * Discard encrypted frames when privacy is off.
479				 */
480				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
481				    wh, "WEP", "%s", "PRIVACY off");
482				vap->iv_stats.is_rx_noprivacy++;
483				IEEE80211_NODE_STAT(ni, rx_noprivacy);
484				goto out;
485			}
486			key = ieee80211_crypto_decap(ni, m, hdrspace);
487			if (key == NULL) {
488				/* NB: stats+msgs handled in crypto_decap */
489				IEEE80211_NODE_STAT(ni, rx_wepfail);
490				goto out;
491			}
492			wh = mtod(m, struct ieee80211_frame *);
493			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
494		} else {
495			/* XXX M_WEP and IEEE80211_F_PRIVACY */
496			key = NULL;
497		}
498
499		/*
500		 * Save QoS bits for use below--before we strip the header.
501		 */
502		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
503			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
504			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
505			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
506		} else
507			qos = 0;
508
509		/*
510		 * Next up, any fragmentation.
511		 */
512		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
513			m = ieee80211_defrag(ni, m, hdrspace);
514			if (m == NULL) {
515				/* Fragment dropped or frame not complete yet */
516				goto out;
517			}
518		}
519		wh = NULL;		/* no longer valid, catch any uses */
520
521		/*
522		 * Next strip any MSDU crypto bits.
523		 */
524		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
525			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
526			    ni->ni_macaddr, "data", "%s", "demic error");
527			vap->iv_stats.is_rx_demicfail++;
528			IEEE80211_NODE_STAT(ni, rx_demicfail);
529			goto out;
530		}
531
532		/* copy to listener after decrypt */
533		if (ieee80211_radiotap_active_vap(vap))
534			ieee80211_radiotap_rx(vap, m);
535		need_tap = 0;
536
537		/*
538		 * Finally, strip the 802.11 header.
539		 */
540		m = ieee80211_decap(vap, m, hdrspace);
541		if (m == NULL) {
542			/* XXX mask bit to check for both */
543			/* don't count Null data frames as errors */
544			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
545			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
546				goto out;
547			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
548			    ni->ni_macaddr, "data", "%s", "decap error");
549			vap->iv_stats.is_rx_decap++;
550			IEEE80211_NODE_STAT(ni, rx_decap);
551			goto err;
552		}
553		eh = mtod(m, struct ether_header *);
554		if (!ieee80211_node_is_authorized(ni)) {
555			/*
556			 * Deny any non-PAE frames received prior to
557			 * authorization.  For open/shared-key
558			 * authentication the port is mark authorized
559			 * after authentication completes.  For 802.1x
560			 * the port is not marked authorized by the
561			 * authenticator until the handshake has completed.
562			 */
563			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
564				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
565				    eh->ether_shost, "data",
566				    "unauthorized port: ether type 0x%x len %u",
567				    eh->ether_type, m->m_pkthdr.len);
568				vap->iv_stats.is_rx_unauth++;
569				IEEE80211_NODE_STAT(ni, rx_unauth);
570				goto err;
571			}
572		} else {
573			/*
574			 * When denying unencrypted frames, discard
575			 * any non-PAE frames received without encryption.
576			 */
577			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
578			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
579			    eh->ether_type != htons(ETHERTYPE_PAE)) {
580				/*
581				 * Drop unencrypted frames.
582				 */
583				vap->iv_stats.is_rx_unencrypted++;
584				IEEE80211_NODE_STAT(ni, rx_unencrypted);
585				goto out;
586			}
587		}
588		/* XXX require HT? */
589		if (qos & IEEE80211_QOS_AMSDU) {
590			m = ieee80211_decap_amsdu(ni, m);
591			if (m == NULL)
592				return IEEE80211_FC0_TYPE_DATA;
593		} else {
594#ifdef IEEE80211_SUPPORT_SUPERG
595			m = ieee80211_decap_fastframe(vap, ni, m);
596			if (m == NULL)
597				return IEEE80211_FC0_TYPE_DATA;
598#endif
599		}
600		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
601			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
602		else
603			ieee80211_deliver_data(vap, ni, m);
604		return IEEE80211_FC0_TYPE_DATA;
605
606	case IEEE80211_FC0_TYPE_MGT:
607		vap->iv_stats.is_rx_mgmt++;
608		IEEE80211_NODE_STAT(ni, rx_mgmt);
609		if (dir != IEEE80211_FC1_DIR_NODS) {
610			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
611			    wh, "data", "incorrect dir 0x%x", dir);
612			vap->iv_stats.is_rx_wrongdir++;
613			goto err;
614		}
615		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
616			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
617			    ni->ni_macaddr, "mgt", "too short: len %u",
618			    m->m_pkthdr.len);
619			vap->iv_stats.is_rx_tooshort++;
620			goto out;
621		}
622#ifdef IEEE80211_DEBUG
623		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
624		    ieee80211_msg_dumppkts(vap)) {
625			if_printf(ifp, "received %s from %s rssi %d\n",
626			    ieee80211_mgt_subtype_name[subtype >>
627				IEEE80211_FC0_SUBTYPE_SHIFT],
628			    ether_sprintf(wh->i_addr2), rssi);
629		}
630#endif
631		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
632			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
633			    wh, NULL, "%s", "WEP set but not permitted");
634			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
635			goto out;
636		}
637		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
638		goto out;
639
640	case IEEE80211_FC0_TYPE_CTL:
641		vap->iv_stats.is_rx_ctl++;
642		IEEE80211_NODE_STAT(ni, rx_ctrl);
643		vap->iv_recv_ctl(ni, m, subtype);
644		goto out;
645
646	default:
647		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
648		    wh, "bad", "frame type 0x%x", type);
649		/* should not come here */
650		break;
651	}
652err:
653	ifp->if_ierrors++;
654out:
655	if (m != NULL) {
656		if (need_tap && ieee80211_radiotap_active_vap(vap))
657			ieee80211_radiotap_rx(vap, m);
658		m_freem(m);
659	}
660	return type;
661}
662
663static int
664is11bclient(const uint8_t *rates, const uint8_t *xrates)
665{
666	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
667	int i;
668
669	/* NB: the 11b clients we care about will not have xrates */
670	if (xrates != NULL || rates == NULL)
671		return 0;
672	for (i = 0; i < rates[1]; i++) {
673		int r = rates[2+i] & IEEE80211_RATE_VAL;
674		if (r > 2*11 || ((1<<r) & brates) == 0)
675			return 0;
676	}
677	return 1;
678}
679
680static void
681adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
682	int subtype, int rssi, int nf)
683{
684	struct ieee80211vap *vap = ni->ni_vap;
685	struct ieee80211com *ic = ni->ni_ic;
686	struct ieee80211_frame *wh;
687	uint8_t *frm, *efrm, *sfrm;
688	uint8_t *ssid, *rates, *xrates;
689
690	wh = mtod(m0, struct ieee80211_frame *);
691	frm = (uint8_t *)&wh[1];
692	efrm = mtod(m0, uint8_t *) + m0->m_len;
693	switch (subtype) {
694	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
695	case IEEE80211_FC0_SUBTYPE_BEACON: {
696		struct ieee80211_scanparams scan;
697		/*
698		 * We process beacon/probe response
699		 * frames to discover neighbors.
700		 */
701		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
702			return;
703		/*
704		 * Count frame now that we know it's to be processed.
705		 */
706		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
707			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
708			IEEE80211_NODE_STAT(ni, rx_beacons);
709		} else
710			IEEE80211_NODE_STAT(ni, rx_proberesp);
711		/*
712		 * If scanning, just pass information to the scan module.
713		 */
714		if (ic->ic_flags & IEEE80211_F_SCAN) {
715			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
716				/*
717				 * Actively scanning a channel marked passive;
718				 * send a probe request now that we know there
719				 * is 802.11 traffic present.
720				 *
721				 * XXX check if the beacon we recv'd gives
722				 * us what we need and suppress the probe req
723				 */
724				ieee80211_probe_curchan(vap, 1);
725				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
726			}
727			ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
728			return;
729		}
730		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
731			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
732				/*
733				 * Create a new entry in the neighbor table.
734				 */
735				ni = ieee80211_add_neighbor(vap, wh, &scan);
736			} else if (ni->ni_capinfo == 0) {
737				/*
738				 * Update faked node created on transmit.
739				 * Note this also updates the tsf.
740				 */
741				ieee80211_init_neighbor(ni, wh, &scan);
742			} else {
743				/*
744				 * Record tsf for potential resync.
745				 */
746				memcpy(ni->ni_tstamp.data, scan.tstamp,
747					sizeof(ni->ni_tstamp));
748			}
749			if (ni != NULL) {
750				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
751				ni->ni_noise = nf;
752			}
753		}
754		break;
755	}
756
757	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
758		if (vap->iv_state != IEEE80211_S_RUN) {
759			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
760			    wh, NULL, "wrong state %s",
761			    ieee80211_state_name[vap->iv_state]);
762			vap->iv_stats.is_rx_mgtdiscard++;
763			return;
764		}
765		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
766			/* frame must be directed */
767			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
768			    wh, NULL, "%s", "not unicast");
769			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
770			return;
771		}
772
773		/*
774		 * prreq frame format
775		 *	[tlv] ssid
776		 *	[tlv] supported rates
777		 *	[tlv] extended supported rates
778		 */
779		ssid = rates = xrates = NULL;
780		sfrm = frm;
781		while (efrm - frm > 1) {
782			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
783			switch (*frm) {
784			case IEEE80211_ELEMID_SSID:
785				ssid = frm;
786				break;
787			case IEEE80211_ELEMID_RATES:
788				rates = frm;
789				break;
790			case IEEE80211_ELEMID_XRATES:
791				xrates = frm;
792				break;
793			}
794			frm += frm[1] + 2;
795		}
796		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
797		if (xrates != NULL)
798			IEEE80211_VERIFY_ELEMENT(xrates,
799				IEEE80211_RATE_MAXSIZE - rates[1], return);
800		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
801		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
802		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
803			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
804			    wh, NULL,
805			    "%s", "no ssid with ssid suppression enabled");
806			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
807			return;
808		}
809
810		/* XXX find a better class or define it's own */
811		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
812		    "%s", "recv probe req");
813		/*
814		 * Some legacy 11b clients cannot hack a complete
815		 * probe response frame.  When the request includes
816		 * only a bare-bones rate set, communicate this to
817		 * the transmit side.
818		 */
819		ieee80211_send_proberesp(vap, wh->i_addr2,
820		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
821		break;
822
823	case IEEE80211_FC0_SUBTYPE_ACTION:
824	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
825		if (ni == vap->iv_bss) {
826			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827			    wh, NULL, "%s", "unknown node");
828			vap->iv_stats.is_rx_mgtdiscard++;
829		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
830		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
831			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
832			    wh, NULL, "%s", "not for us");
833			vap->iv_stats.is_rx_mgtdiscard++;
834		} else if (vap->iv_state != IEEE80211_S_RUN) {
835			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
836			    wh, NULL, "wrong state %s",
837			    ieee80211_state_name[vap->iv_state]);
838			vap->iv_stats.is_rx_mgtdiscard++;
839		} else {
840			if (ieee80211_parse_action(ni, m0) == 0)
841				(void)ic->ic_recv_action(ni, wh, frm, efrm);
842		}
843		break;
844
845	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
846	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
847	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
848	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
849	case IEEE80211_FC0_SUBTYPE_ATIM:
850	case IEEE80211_FC0_SUBTYPE_DISASSOC:
851	case IEEE80211_FC0_SUBTYPE_AUTH:
852	case IEEE80211_FC0_SUBTYPE_DEAUTH:
853		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
854		    wh, NULL, "%s", "not handled");
855		vap->iv_stats.is_rx_mgtdiscard++;
856		break;
857
858	default:
859		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
860		    wh, "mgt", "subtype 0x%x not handled", subtype);
861		vap->iv_stats.is_rx_badsubtype++;
862		break;
863	}
864}
865#undef IEEE80211_VERIFY_LENGTH
866#undef IEEE80211_VERIFY_ELEMENT
867
868static void
869ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
870	int subtype, int rssi, int nf)
871{
872	struct ieee80211vap *vap = ni->ni_vap;
873	struct ieee80211com *ic = ni->ni_ic;
874	struct ieee80211_frame *wh;
875
876	/*
877	 * Process management frames when scanning; useful for doing
878	 * a site-survey.
879	 */
880	if (ic->ic_flags & IEEE80211_F_SCAN)
881		adhoc_recv_mgmt(ni, m0, subtype, rssi, nf);
882	else {
883		wh = mtod(m0, struct ieee80211_frame *);
884		switch (subtype) {
885		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
886		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
887		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
888		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
889		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
890		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
891		case IEEE80211_FC0_SUBTYPE_BEACON:
892		case IEEE80211_FC0_SUBTYPE_ATIM:
893		case IEEE80211_FC0_SUBTYPE_DISASSOC:
894		case IEEE80211_FC0_SUBTYPE_AUTH:
895		case IEEE80211_FC0_SUBTYPE_DEAUTH:
896		case IEEE80211_FC0_SUBTYPE_ACTION:
897		case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
898			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
899			     wh, NULL, "%s", "not handled");
900			vap->iv_stats.is_rx_mgtdiscard++;
901			break;
902		default:
903			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
904			     wh, "mgt", "subtype 0x%x not handled", subtype);
905			vap->iv_stats.is_rx_badsubtype++;
906			break;
907		}
908	}
909}
910
911static void
912adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
913{
914
915	switch (subtype) {
916	case IEEE80211_FC0_SUBTYPE_BAR:
917		ieee80211_recv_bar(ni, m);
918		break;
919	}
920}
921