ieee80211_adhoc.c revision 188466
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 188466 2009-02-10 23:51:18Z 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_TDMA
61#include <net80211/ieee80211_tdma.h>
62#endif
63
64#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
65
66static	void adhoc_vattach(struct ieee80211vap *);
67static	int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
68static int adhoc_input(struct ieee80211_node *, struct mbuf *,
69	int rssi, int noise, uint32_t rstamp);
70static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
71	int subtype, int rssi, int noise, uint32_t rstamp);
72static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
73	int subtype, int rssi, int noise, uint32_t rstamp);
74
75void
76ieee80211_adhoc_attach(struct ieee80211com *ic)
77{
78	ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
79	ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
80}
81
82void
83ieee80211_adhoc_detach(struct ieee80211com *ic)
84{
85}
86
87static void
88adhoc_vdetach(struct ieee80211vap *vap)
89{
90}
91
92static void
93adhoc_vattach(struct ieee80211vap *vap)
94{
95	vap->iv_newstate = adhoc_newstate;
96	vap->iv_input = adhoc_input;
97	if (vap->iv_opmode == IEEE80211_M_IBSS)
98		vap->iv_recv_mgmt = adhoc_recv_mgmt;
99	else
100		vap->iv_recv_mgmt = ahdemo_recv_mgmt;
101	vap->iv_opdetach = adhoc_vdetach;
102#ifdef IEEE80211_SUPPORT_TDMA
103	/*
104	 * Throw control to tdma support.  Note we do this
105	 * after setting up our callbacks so it can piggyback
106	 * on top of us.
107	 */
108	if (vap->iv_caps & IEEE80211_C_TDMA)
109		ieee80211_tdma_vattach(vap);
110#endif
111}
112
113static void
114sta_leave(void *arg, struct ieee80211_node *ni)
115{
116	struct ieee80211vap *vap = arg;
117
118	if (ni->ni_vap == vap && ni != vap->iv_bss)
119		ieee80211_node_leave(ni);
120}
121
122/*
123 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
124 */
125static int
126adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
127{
128	struct ieee80211com *ic = vap->iv_ic;
129	struct ieee80211_node *ni;
130	enum ieee80211_state ostate;
131
132	IEEE80211_LOCK_ASSERT(vap->iv_ic);
133
134	ostate = vap->iv_state;
135	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
136	    __func__, ieee80211_state_name[ostate],
137	    ieee80211_state_name[nstate], arg);
138	vap->iv_state = nstate;			/* state transition */
139	if (ostate != IEEE80211_S_SCAN)
140		ieee80211_cancel_scan(vap);	/* background scan */
141	ni = vap->iv_bss;			/* NB: no reference held */
142	switch (nstate) {
143	case IEEE80211_S_INIT:
144		switch (ostate) {
145		case IEEE80211_S_SCAN:
146			ieee80211_cancel_scan(vap);
147			break;
148		default:
149			break;
150		}
151		if (ostate != IEEE80211_S_INIT) {
152			/* NB: optimize INIT -> INIT case */
153			ieee80211_reset_bss(vap);
154		}
155		break;
156	case IEEE80211_S_SCAN:
157		switch (ostate) {
158		case IEEE80211_S_RUN:		/* beacon miss */
159			/* purge station table; entries are stale */
160			ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
161			/* fall thru... */
162		case IEEE80211_S_INIT:
163			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
164			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
165				/*
166				 * Already have a channel; bypass the
167				 * scan and startup immediately.
168				 */
169				ieee80211_create_ibss(vap, vap->iv_des_chan);
170				break;
171			}
172			/*
173			 * Initiate a scan.  We can come here as a result
174			 * of an IEEE80211_IOC_SCAN_REQ too in which case
175			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
176			 * and the scan request parameters will be present
177			 * in iv_scanreq.  Otherwise we do the default.
178			 */
179			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
180				ieee80211_check_scan(vap,
181				    vap->iv_scanreq_flags,
182				    vap->iv_scanreq_duration,
183				    vap->iv_scanreq_mindwell,
184				    vap->iv_scanreq_maxdwell,
185				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
186				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
187			} else
188				ieee80211_check_scan_current(vap);
189			break;
190		case IEEE80211_S_SCAN:
191			/*
192			 * This can happen because of a change in state
193			 * that requires a reset.  Trigger a new scan
194			 * unless we're in manual roaming mode in which
195			 * case an application must issue an explicit request.
196			 */
197			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
198				ieee80211_check_scan_current(vap);
199			break;
200		default:
201			goto invalid;
202		}
203		break;
204	case IEEE80211_S_RUN:
205		if (vap->iv_flags & IEEE80211_F_WPA) {
206			/* XXX validate prerequisites */
207		}
208		switch (ostate) {
209		case IEEE80211_S_SCAN:
210#ifdef IEEE80211_DEBUG
211			if (ieee80211_msg_debug(vap)) {
212				ieee80211_note(vap,
213				    "synchronized with %s ssid ",
214				    ether_sprintf(ni->ni_bssid));
215				ieee80211_print_essid(vap->iv_bss->ni_essid,
216				    ni->ni_esslen);
217				/* XXX MCS/HT */
218				printf(" channel %d start %uMb\n",
219				    ieee80211_chan2ieee(ic, ic->ic_curchan),
220				    IEEE80211_RATE2MBS(ni->ni_txrate));
221			}
222#endif
223			break;
224		default:
225			goto invalid;
226		}
227		/*
228		 * When 802.1x is not in use mark the port authorized
229		 * at this point so traffic can flow.
230		 */
231		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
232			ieee80211_node_authorize(ni);
233		/*
234		 * Fake association when joining an existing bss.
235		 */
236		if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
237		    ic->ic_newassoc != NULL)
238			ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
239		break;
240	case IEEE80211_S_SLEEP:
241		ieee80211_sta_pwrsave(vap, 0);
242		break;
243	default:
244	invalid:
245		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
246		    "%s: unexpected state transition %s -> %s\n", __func__,
247		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
248		break;
249	}
250	return 0;
251}
252
253/*
254 * Decide if a received management frame should be
255 * printed when debugging is enabled.  This filters some
256 * of the less interesting frames that come frequently
257 * (e.g. beacons).
258 */
259static __inline int
260doprint(struct ieee80211vap *vap, int subtype)
261{
262	switch (subtype) {
263	case IEEE80211_FC0_SUBTYPE_BEACON:
264		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
265	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
266		return 1;
267	}
268	return 1;
269}
270
271/*
272 * Process a received frame.  The node associated with the sender
273 * should be supplied.  If nothing was found in the node table then
274 * the caller is assumed to supply a reference to iv_bss instead.
275 * The RSSI and a timestamp are also supplied.  The RSSI data is used
276 * during AP scanning to select a AP to associate with; it can have
277 * any units so long as values have consistent units and higher values
278 * mean ``better signal''.  The receive timestamp is currently not used
279 * by the 802.11 layer.
280 */
281static int
282adhoc_input(struct ieee80211_node *ni, struct mbuf *m,
283	int rssi, int noise, uint32_t rstamp)
284{
285#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
286#define	HAS_SEQ(type)	((type & 0x4) == 0)
287	struct ieee80211vap *vap = ni->ni_vap;
288	struct ieee80211com *ic = ni->ni_ic;
289	struct ifnet *ifp = vap->iv_ifp;
290	struct ieee80211_frame *wh;
291	struct ieee80211_key *key;
292	struct ether_header *eh;
293	int hdrspace, need_tap;
294	uint8_t dir, type, subtype, qos;
295	uint8_t *bssid;
296	uint16_t rxseq;
297
298	if (m->m_flags & M_AMPDU_MPDU) {
299		/*
300		 * Fastpath for A-MPDU reorder q resubmission.  Frames
301		 * w/ M_AMPDU_MPDU marked have already passed through
302		 * here but were received out of order and been held on
303		 * the reorder queue.  When resubmitted they are marked
304		 * with the M_AMPDU_MPDU flag and we can bypass most of
305		 * the normal processing.
306		 */
307		wh = mtod(m, struct ieee80211_frame *);
308		type = IEEE80211_FC0_TYPE_DATA;
309		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
310		subtype = IEEE80211_FC0_SUBTYPE_QOS;
311		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
312		goto resubmit_ampdu;
313	}
314
315	KASSERT(ni != NULL, ("null node"));
316	ni->ni_inact = ni->ni_inact_reload;
317
318	need_tap = 1;			/* mbuf need to be tapped. */
319	type = -1;			/* undefined */
320
321	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
322		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
323		    ni->ni_macaddr, NULL,
324		    "too short (1): len %u", m->m_pkthdr.len);
325		vap->iv_stats.is_rx_tooshort++;
326		goto out;
327	}
328	/*
329	 * Bit of a cheat here, we use a pointer for a 3-address
330	 * frame format but don't reference fields past outside
331	 * ieee80211_frame_min w/o first validating the data is
332	 * present.
333	 */
334	wh = mtod(m, struct ieee80211_frame *);
335
336	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
337	    IEEE80211_FC0_VERSION_0) {
338		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
339		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
340		vap->iv_stats.is_rx_badversion++;
341		goto err;
342	}
343
344	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
345	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
346	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
347	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
348		if (dir != IEEE80211_FC1_DIR_NODS)
349			bssid = wh->i_addr1;
350		else if (type == IEEE80211_FC0_TYPE_CTL)
351			bssid = wh->i_addr1;
352		else {
353			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
354				IEEE80211_DISCARD_MAC(vap,
355				    IEEE80211_MSG_ANY, ni->ni_macaddr,
356				    NULL, "too short (2): len %u",
357				    m->m_pkthdr.len);
358				vap->iv_stats.is_rx_tooshort++;
359				goto out;
360			}
361			bssid = wh->i_addr3;
362		}
363		/*
364		 * Validate the bssid.
365		 */
366		if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
367		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
368			/* not interested in */
369			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
370			    bssid, NULL, "%s", "not to bss");
371			vap->iv_stats.is_rx_wrongbss++;
372			goto out;
373		}
374		/*
375		 * Data frame, cons up a node when it doesn't
376		 * exist. This should probably done after an ACL check.
377		 */
378		if (type == IEEE80211_FC0_TYPE_DATA &&
379		    ni == vap->iv_bss &&
380		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
381			/*
382			 * Beware of frames that come in too early; we
383			 * can receive broadcast frames and creating sta
384			 * entries will blow up because there is no bss
385			 * channel yet.
386			 */
387			if (vap->iv_state != IEEE80211_S_RUN) {
388				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
389				    wh, "data", "not in RUN state (%s)",
390				    ieee80211_state_name[vap->iv_state]);
391				vap->iv_stats.is_rx_badstate++;
392				goto err;
393			}
394			/*
395			 * Fake up a node for this newly
396			 * discovered member of the IBSS.
397			 */
398			ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
399			if (ni == NULL) {
400				/* NB: stat kept for alloc failure */
401				goto err;
402			}
403		}
404		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
405		ni->ni_noise = noise;
406		ni->ni_rstamp = rstamp;
407		if (HAS_SEQ(type)) {
408			uint8_t tid = ieee80211_gettid(wh);
409			if (IEEE80211_QOS_HAS_SEQ(wh) &&
410			    TID_TO_WME_AC(tid) >= WME_AC_VI)
411				ic->ic_wme.wme_hipri_traffic++;
412			rxseq = le16toh(*(uint16_t *)wh->i_seq);
413			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
414			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
415			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
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 (bpf_peers_present(vap->iv_rawbpf))
534			bpf_mtap(vap->iv_rawbpf, 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 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) &&
594#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
595		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
596			struct llc *llc;
597
598			/*
599			 * Check for fast-frame tunnel encapsulation.
600			 */
601			if (m->m_len < FF_LLC_SIZE &&
602			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
603				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
604				    ni->ni_macaddr, "fast-frame",
605				    "%s", "m_pullup(llc) failed");
606				vap->iv_stats.is_rx_tooshort++;
607				return IEEE80211_FC0_TYPE_DATA;
608			}
609			llc = (struct llc *)(mtod(m, uint8_t *) +
610				sizeof(struct ether_header));
611			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
612				m_adj(m, FF_LLC_SIZE);
613				m = ieee80211_decap_fastframe(ni, m);
614				if (m == NULL)
615					return IEEE80211_FC0_TYPE_DATA;
616			}
617		}
618#undef FF_LLC_SIZE
619		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
620			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
621		else
622			ieee80211_deliver_data(vap, ni, m);
623		return IEEE80211_FC0_TYPE_DATA;
624
625	case IEEE80211_FC0_TYPE_MGT:
626		vap->iv_stats.is_rx_mgmt++;
627		IEEE80211_NODE_STAT(ni, rx_mgmt);
628		if (dir != IEEE80211_FC1_DIR_NODS) {
629			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
630			    wh, "data", "incorrect dir 0x%x", dir);
631			vap->iv_stats.is_rx_wrongdir++;
632			goto err;
633		}
634		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
635			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
636			    ni->ni_macaddr, "mgt", "too short: len %u",
637			    m->m_pkthdr.len);
638			vap->iv_stats.is_rx_tooshort++;
639			goto out;
640		}
641#ifdef IEEE80211_DEBUG
642		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
643		    ieee80211_msg_dumppkts(vap)) {
644			if_printf(ifp, "received %s from %s rssi %d\n",
645			    ieee80211_mgt_subtype_name[subtype >>
646				IEEE80211_FC0_SUBTYPE_SHIFT],
647			    ether_sprintf(wh->i_addr2), rssi);
648		}
649#endif
650		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
651			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
652			    wh, NULL, "%s", "WEP set but not permitted");
653			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
654			goto out;
655		}
656		if (bpf_peers_present(vap->iv_rawbpf))
657			bpf_mtap(vap->iv_rawbpf, m);
658		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
659		m_freem(m);
660		return IEEE80211_FC0_TYPE_MGT;
661
662	case IEEE80211_FC0_TYPE_CTL:
663		vap->iv_stats.is_rx_ctl++;
664		IEEE80211_NODE_STAT(ni, rx_ctrl);
665		goto out;
666	default:
667		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
668		    wh, "bad", "frame type 0x%x", type);
669		/* should not come here */
670		break;
671	}
672err:
673	ifp->if_ierrors++;
674out:
675	if (m != NULL) {
676		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
677			bpf_mtap(vap->iv_rawbpf, m);
678		m_freem(m);
679	}
680	return type;
681#undef SEQ_LEQ
682}
683
684static int
685is11bclient(const uint8_t *rates, const uint8_t *xrates)
686{
687	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
688	int i;
689
690	/* NB: the 11b clients we care about will not have xrates */
691	if (xrates != NULL || rates == NULL)
692		return 0;
693	for (i = 0; i < rates[1]; i++) {
694		int r = rates[2+i] & IEEE80211_RATE_VAL;
695		if (r > 2*11 || ((1<<r) & brates) == 0)
696			return 0;
697	}
698	return 1;
699}
700
701static void
702adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
703	int subtype, int rssi, int noise, uint32_t rstamp)
704{
705	struct ieee80211vap *vap = ni->ni_vap;
706	struct ieee80211com *ic = ni->ni_ic;
707	struct ieee80211_frame *wh;
708	uint8_t *frm, *efrm, *sfrm;
709	uint8_t *ssid, *rates, *xrates;
710
711	wh = mtod(m0, struct ieee80211_frame *);
712	frm = (uint8_t *)&wh[1];
713	efrm = mtod(m0, uint8_t *) + m0->m_len;
714	switch (subtype) {
715	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
716	case IEEE80211_FC0_SUBTYPE_BEACON: {
717		struct ieee80211_scanparams scan;
718		/*
719		 * We process beacon/probe response
720		 * frames to discover neighbors.
721		 */
722		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
723			return;
724		/*
725		 * Count frame now that we know it's to be processed.
726		 */
727		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
728			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
729			IEEE80211_NODE_STAT(ni, rx_beacons);
730		} else
731			IEEE80211_NODE_STAT(ni, rx_proberesp);
732		/*
733		 * If scanning, just pass information to the scan module.
734		 */
735		if (ic->ic_flags & IEEE80211_F_SCAN) {
736			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
737				/*
738				 * Actively scanning a channel marked passive;
739				 * send a probe request now that we know there
740				 * is 802.11 traffic present.
741				 *
742				 * XXX check if the beacon we recv'd gives
743				 * us what we need and suppress the probe req
744				 */
745				ieee80211_probe_curchan(vap, 1);
746				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
747			}
748			ieee80211_add_scan(vap, &scan, wh,
749				subtype, rssi, noise, rstamp);
750			return;
751		}
752		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
753			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
754				/*
755				 * Create a new entry in the neighbor table.
756				 */
757				ni = ieee80211_add_neighbor(vap, wh, &scan);
758			} else if (ni->ni_capinfo == 0) {
759				/*
760				 * Update faked node created on transmit.
761				 * Note this also updates the tsf.
762				 */
763				ieee80211_init_neighbor(ni, wh, &scan);
764			} else {
765				/*
766				 * Record tsf for potential resync.
767				 */
768				memcpy(ni->ni_tstamp.data, scan.tstamp,
769					sizeof(ni->ni_tstamp));
770			}
771			if (ni != NULL) {
772				IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
773				ni->ni_noise = noise;
774				ni->ni_rstamp = rstamp;
775			}
776		}
777		break;
778	}
779
780	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
781		if (vap->iv_state != IEEE80211_S_RUN) {
782			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
783			    wh, NULL, "wrong state %s",
784			    ieee80211_state_name[vap->iv_state]);
785			vap->iv_stats.is_rx_mgtdiscard++;
786			return;
787		}
788		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
789			/* frame must be directed */
790			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
791			    wh, NULL, "%s", "not unicast");
792			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
793			return;
794		}
795
796		/*
797		 * prreq frame format
798		 *	[tlv] ssid
799		 *	[tlv] supported rates
800		 *	[tlv] extended supported rates
801		 */
802		ssid = rates = xrates = NULL;
803		sfrm = frm;
804		while (efrm - frm > 1) {
805			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
806			switch (*frm) {
807			case IEEE80211_ELEMID_SSID:
808				ssid = frm;
809				break;
810			case IEEE80211_ELEMID_RATES:
811				rates = frm;
812				break;
813			case IEEE80211_ELEMID_XRATES:
814				xrates = frm;
815				break;
816			}
817			frm += frm[1] + 2;
818		}
819		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
820		if (xrates != NULL)
821			IEEE80211_VERIFY_ELEMENT(xrates,
822				IEEE80211_RATE_MAXSIZE - rates[1], return);
823		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
824		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
825		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
826			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827			    wh, NULL,
828			    "%s", "no ssid with ssid suppression enabled");
829			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
830			return;
831		}
832
833		/* XXX find a better class or define it's own */
834		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
835		    "%s", "recv probe req");
836		/*
837		 * Some legacy 11b clients cannot hack a complete
838		 * probe response frame.  When the request includes
839		 * only a bare-bones rate set, communicate this to
840		 * the transmit side.
841		 */
842		ieee80211_send_proberesp(vap, wh->i_addr2,
843		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
844		break;
845
846	case IEEE80211_FC0_SUBTYPE_ACTION: {
847		const struct ieee80211_action *ia;
848
849		if (vap->iv_state != IEEE80211_S_RUN) {
850			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
851			    wh, NULL, "wrong state %s",
852			    ieee80211_state_name[vap->iv_state]);
853			vap->iv_stats.is_rx_mgtdiscard++;
854			return;
855		}
856		/*
857		 * action frame format:
858		 *	[1] category
859		 *	[1] action
860		 *	[tlv] parameters
861		 */
862		IEEE80211_VERIFY_LENGTH(efrm - frm,
863			sizeof(struct ieee80211_action), return);
864		ia = (const struct ieee80211_action *) frm;
865
866		vap->iv_stats.is_rx_action++;
867		IEEE80211_NODE_STAT(ni, rx_action);
868
869		/* verify frame payloads but defer processing */
870		/* XXX maybe push this to method */
871		switch (ia->ia_category) {
872		case IEEE80211_ACTION_CAT_BA:
873			switch (ia->ia_action) {
874			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
875				IEEE80211_VERIFY_LENGTH(efrm - frm,
876				    sizeof(struct ieee80211_action_ba_addbarequest),
877				    return);
878				break;
879			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
880				IEEE80211_VERIFY_LENGTH(efrm - frm,
881				    sizeof(struct ieee80211_action_ba_addbaresponse),
882				    return);
883				break;
884			case IEEE80211_ACTION_BA_DELBA:
885				IEEE80211_VERIFY_LENGTH(efrm - frm,
886				    sizeof(struct ieee80211_action_ba_delba),
887				    return);
888				break;
889			}
890			break;
891		case IEEE80211_ACTION_CAT_HT:
892			switch (ia->ia_action) {
893			case IEEE80211_ACTION_HT_TXCHWIDTH:
894				IEEE80211_VERIFY_LENGTH(efrm - frm,
895				    sizeof(struct ieee80211_action_ht_txchwidth),
896				    return);
897				break;
898			}
899			break;
900		}
901		ic->ic_recv_action(ni, frm, efrm);
902		break;
903	}
904
905	case IEEE80211_FC0_SUBTYPE_AUTH:
906	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
907	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
908	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
909	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
910	case IEEE80211_FC0_SUBTYPE_DEAUTH:
911	case IEEE80211_FC0_SUBTYPE_DISASSOC:
912		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
913		     wh, NULL, "%s", "not handled");
914		vap->iv_stats.is_rx_mgtdiscard++;
915		return;
916
917	default:
918		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
919		     wh, "mgt", "subtype 0x%x not handled", subtype);
920		vap->iv_stats.is_rx_badsubtype++;
921		break;
922	}
923}
924#undef IEEE80211_VERIFY_LENGTH
925#undef IEEE80211_VERIFY_ELEMENT
926
927static void
928ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
929	int subtype, int rssi, int noise, uint32_t rstamp)
930{
931	struct ieee80211vap *vap = ni->ni_vap;
932	struct ieee80211com *ic = ni->ni_ic;
933
934	/*
935	 * Process management frames when scanning; useful for doing
936	 * a site-survey.
937	 */
938	if (ic->ic_flags & IEEE80211_F_SCAN)
939		adhoc_recv_mgmt(ni, m0, subtype, rssi, noise, rstamp);
940	else
941		vap->iv_stats.is_rx_mgtdiscard++;
942}
943