ieee80211_wds.c revision 190391
1/*-
2 * Copyright (c) 2007-2008 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_wds.c 190391 2009-03-24 20:39:08Z sam $");
29#endif
30
31/*
32 * IEEE 802.11 WDS 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_wds.h>
59#include <net80211/ieee80211_input.h>
60#ifdef IEEE80211_SUPPORT_SUPERG
61#include <net80211/ieee80211_superg.h>
62#endif
63
64static void wds_vattach(struct ieee80211vap *);
65static int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66static	int wds_input(struct ieee80211_node *ni, struct mbuf *m,
67	    int rssi, int noise, uint32_t rstamp);
68static void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *,
69	    int subtype, int rssi, int noise, u_int32_t rstamp);
70
71void
72ieee80211_wds_attach(struct ieee80211com *ic)
73{
74	ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach;
75}
76
77void
78ieee80211_wds_detach(struct ieee80211com *ic)
79{
80}
81
82static void
83wds_vdetach(struct ieee80211vap *vap)
84{
85	if (vap->iv_bss != NULL) {
86		/* XXX locking? */
87		if (vap->iv_bss->ni_wdsvap == vap)
88			vap->iv_bss->ni_wdsvap = NULL;
89	}
90}
91
92static void
93wds_vattach(struct ieee80211vap *vap)
94{
95	vap->iv_newstate = wds_newstate;
96	vap->iv_input = wds_input;
97	vap->iv_recv_mgmt = wds_recv_mgmt;
98	vap->iv_opdetach = wds_vdetach;
99}
100
101static int
102ieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan)
103{
104	struct ieee80211com *ic = vap->iv_ic;
105	struct ieee80211_node_table *nt = &ic->ic_sta;
106	struct ieee80211_node *ni, *obss;
107
108	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
109	     "%s: creating link to %s on channel %u\n", __func__,
110	     ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan));
111
112	/* NB: vap create must specify the bssid for the link */
113	KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid"));
114	/* NB: we should only be called on RUN transition */
115	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state"));
116
117	if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
118		/*
119		 * Dynamic/non-legacy WDS.  Reference the associated
120		 * station specified by the desired bssid setup at vap
121		 * create.  Point ni_wdsvap at the WDS vap so 4-address
122		 * frames received through the associated AP vap will
123		 * be dispatched upward (e.g. to a bridge) as though
124		 * they arrived on the WDS vap.
125		 */
126		IEEE80211_NODE_LOCK(nt);
127		obss = NULL;
128		ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid);
129		if (ni == NULL) {
130			/*
131			 * Node went away before we could hookup.  This
132			 * should be ok; no traffic will flow and a leave
133			 * event will be dispatched that should cause
134			 * the vap to be destroyed.
135			 */
136			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
137			    "%s: station %s went away\n",
138			    __func__, ether_sprintf(vap->iv_des_bssid));
139			/* XXX stat? */
140		} else if (ni->ni_wdsvap != NULL) {
141			/*
142			 * Node already setup with a WDS vap; we cannot
143			 * allow multiple references so disallow.  If
144			 * ni_wdsvap points at us that's ok; we should
145			 * do nothing anyway.
146			 */
147			/* XXX printf instead? */
148			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
149			    "%s: station %s in use with %s\n",
150			    __func__, ether_sprintf(vap->iv_des_bssid),
151			    ni->ni_wdsvap->iv_ifp->if_xname);
152			/* XXX stat? */
153		} else {
154			/*
155			 * Committed to new node, setup state.
156			 */
157			obss = vap->iv_bss;
158			vap->iv_bss = ni;
159			ni->ni_wdsvap = vap;
160		}
161		IEEE80211_NODE_UNLOCK(nt);
162		if (obss != NULL) {
163			/* NB: deferred to avoid recursive lock */
164			ieee80211_free_node(obss);
165		}
166	} else {
167		/*
168		 * Legacy WDS vap setup.
169		 */
170		/*
171		 * The far end does not associate so we just create
172		 * create a new node and install it as the vap's
173		 * bss node.  We must simulate an association and
174		 * authorize the port for traffic to flow.
175		 * XXX check if node already in sta table?
176		 */
177		ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
178		if (ni != NULL) {
179			obss = vap->iv_bss;
180			vap->iv_bss = ieee80211_ref_node(ni);
181			ni->ni_flags |= IEEE80211_NODE_AREF;
182			if (obss != NULL)
183				ieee80211_free_node(obss);
184			/* give driver a chance to setup state like ni_txrate */
185			if (ic->ic_newassoc != NULL)
186				ic->ic_newassoc(ni, 1);
187			/* tell the authenticator about new station */
188			if (vap->iv_auth->ia_node_join != NULL)
189				vap->iv_auth->ia_node_join(ni);
190			if (ni->ni_authmode != IEEE80211_AUTH_8021X)
191				ieee80211_node_authorize(ni);
192
193			ieee80211_notify_node_join(ni, 1 /*newassoc*/);
194			/* XXX inject l2uf frame */
195		}
196	}
197
198	/*
199	 * Flush pending frames now that were setup.
200	 */
201	if (ni != NULL && IEEE80211_NODE_WDSQ_QLEN(ni) != 0) {
202		int8_t rssi, noise;
203
204		IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
205		    "flush wds queue, %u packets queued",
206		    IEEE80211_NODE_WDSQ_QLEN(ni));
207		ic->ic_node_getsignal(ni, &rssi, &noise);
208		for (;;) {
209			struct mbuf *m;
210
211			IEEE80211_NODE_WDSQ_LOCK(ni);
212			_IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m);
213			IEEE80211_NODE_WDSQ_UNLOCK(ni);
214			if (m == NULL)
215				break;
216			/* XXX cheat and re-use last rstamp */
217			ieee80211_input(ni, m, rssi, noise, ni->ni_rstamp);
218		}
219	}
220	return (ni == NULL ? ENOENT : 0);
221}
222
223/*
224 * Propagate multicast frames of an ap vap to all DWDS links.
225 * The caller is assumed to have verified this frame is multicast.
226 */
227void
228ieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m)
229{
230	struct ieee80211com *ic = vap0->iv_ic;
231	struct ifnet *parent = ic->ic_ifp;
232	const struct ether_header *eh = mtod(m, const struct ether_header *);
233	struct ieee80211_node *ni;
234	struct ieee80211vap *vap;
235	struct ifnet *ifp;
236	struct mbuf *mcopy;
237	int err;
238
239	KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost),
240	    ("%s not mcast", ether_sprintf(eh->ether_dhost)));
241
242	/* XXX locking */
243	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
244		/* only DWDS vaps are interesting */
245		if (vap->iv_opmode != IEEE80211_M_WDS ||
246		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))
247			continue;
248		/* if it came in this interface, don't send it back out */
249		ifp = vap->iv_ifp;
250		if (ifp == m->m_pkthdr.rcvif)
251			continue;
252		/*
253		 * Duplicate the frame and send it.  We don't need
254		 * to classify or lookup the tx node; this was already
255		 * done by the caller so we can just re-use the info.
256		 */
257		mcopy = m_copypacket(m, M_DONTWAIT);
258		if (mcopy == NULL) {
259			ifp->if_oerrors++;
260			/* XXX stat + msg */
261			continue;
262		}
263		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
264		if (ni == NULL) {
265			/* NB: ieee80211_find_txnode does stat+msg */
266			ifp->if_oerrors++;
267			m_freem(mcopy);
268			continue;
269		}
270		if (ieee80211_classify(ni, mcopy)) {
271			IEEE80211_DISCARD_MAC(vap,
272			    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS,
273			    eh->ether_dhost, NULL,
274			    "%s", "classification failure");
275			vap->iv_stats.is_tx_classify++;
276			ifp->if_oerrors++;
277			m_freem(mcopy);
278			ieee80211_free_node(ni);
279			continue;
280		}
281		mcopy->m_flags |= M_MCAST | M_WDS;
282		mcopy->m_pkthdr.rcvif = (void *) ni;
283
284		err = parent->if_transmit(parent, mcopy);
285		if (err) {
286			/* NB: IFQ_HANDOFF reclaims mbuf */
287			ifp->if_oerrors++;
288			ieee80211_free_node(ni);
289		} else
290			ifp->if_opackets++;
291	}
292}
293
294/*
295 * Handle DWDS discovery on receipt of a 4-address frame in
296 * ap mode.  Queue the frame and post an event for someone
297 * to plumb the necessary WDS vap for this station.  Frames
298 * received prior to the vap set running will then be reprocessed
299 * as if they were just received.
300 */
301void
302ieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
303{
304	struct ieee80211vap *vap = ni->ni_vap;
305	struct ieee80211com *ic = ni->ni_ic;
306	int qlen, age;
307
308	IEEE80211_NODE_WDSQ_LOCK(ni);
309	if (!_IF_QFULL(&ni->ni_wdsq)) {
310		/*
311		 * Tag the frame with it's expiry time and insert
312		 * it in the queue.  The aging interval is 4 times
313		 * the listen interval specified by the station.
314		 * Frames that sit around too long are reclaimed
315		 * using this information.
316		 */
317		/* XXX handle overflow? */
318		/* XXX per/vap beacon interval? */
319		/* NB: TU -> secs */
320		age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024;
321		_IEEE80211_NODE_WDSQ_ENQUEUE(ni, m, qlen, age);
322		IEEE80211_NODE_WDSQ_UNLOCK(ni);
323
324		IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
325		    "save frame, %u now queued", qlen);
326	} else {
327		vap->iv_stats.is_dwds_qdrop++;
328		_IF_DROP(&ni->ni_wdsq);
329		IEEE80211_NODE_WDSQ_UNLOCK(ni);
330
331		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_WDS,
332		    mtod(m, struct ieee80211_frame *), "wds data",
333		    "pending q overflow, drops %d (len %d)",
334		    ni->ni_wdsq.ifq_drops, ni->ni_wdsq.ifq_len);
335
336#ifdef IEEE80211_DEBUG
337		if (ieee80211_msg_dumppkts(vap))
338			ieee80211_dump_pkt(ic, mtod(m, caddr_t),
339			    m->m_len, -1, -1);
340#endif
341		/* XXX tail drop? */
342		m_freem(m);
343	}
344	ieee80211_notify_wds_discover(ni);
345}
346
347/*
348 * Age frames on the WDS pending queue. The aging interval is
349 * 4 times the listen interval specified by the station.  This
350 * number is factored into the age calculations when the frame
351 * is placed on the queue.  We store ages as time differences
352 * so we can check and/or adjust only the head of the list.
353 * If a frame's age exceeds the threshold then discard it.
354 * The number of frames discarded is returned to the caller.
355 */
356int
357ieee80211_node_wdsq_age(struct ieee80211_node *ni)
358{
359#ifdef IEEE80211_DEBUG
360	struct ieee80211vap *vap = ni->ni_vap;
361#endif
362	struct mbuf *m;
363	int discard = 0;
364
365	IEEE80211_NODE_WDSQ_LOCK(ni);
366	while (_IF_POLL(&ni->ni_wdsq, m) != NULL &&
367	     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
368		IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
369			"discard frame, age %u", M_AGE_GET(m));
370
371		/* XXX could be optimized */
372		_IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m);
373		m_freem(m);
374		discard++;
375	}
376	if (m != NULL)
377		M_AGE_SUB(m, IEEE80211_INACT_WAIT);
378	IEEE80211_NODE_WDSQ_UNLOCK(ni);
379
380	IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
381	    "discard %u frames for age", discard);
382#if 0
383	IEEE80211_NODE_STAT_ADD(ni, wds_discard, discard);
384#endif
385	return discard;
386}
387
388/*
389 * IEEE80211_M_WDS vap state machine handler.
390 */
391static int
392wds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
393{
394	struct ieee80211com *ic = vap->iv_ic;
395	struct ieee80211_node *ni;
396	enum ieee80211_state ostate;
397	int error;
398
399	IEEE80211_LOCK_ASSERT(ic);
400
401	ostate = vap->iv_state;
402	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
403		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
404	vap->iv_state = nstate;			/* state transition */
405	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
406	if (ostate != IEEE80211_S_SCAN)
407		ieee80211_cancel_scan(vap);	/* background scan */
408	ni = vap->iv_bss;			/* NB: no reference held */
409	error = 0;
410	switch (nstate) {
411	case IEEE80211_S_INIT:
412		switch (ostate) {
413		case IEEE80211_S_SCAN:
414			ieee80211_cancel_scan(vap);
415			break;
416		default:
417			break;
418		}
419		if (ostate != IEEE80211_S_INIT) {
420			/* NB: optimize INIT -> INIT case */
421			ieee80211_reset_bss(vap);
422		}
423		break;
424	case IEEE80211_S_SCAN:
425		switch (ostate) {
426		case IEEE80211_S_INIT:
427			ieee80211_check_scan_current(vap);
428			break;
429		default:
430			break;
431		}
432		break;
433	case IEEE80211_S_RUN:
434		if (ostate == IEEE80211_S_INIT) {
435			/*
436			 * Already have a channel; bypass the scan
437			 * and startup immediately.
438			 */
439			error = ieee80211_create_wds(vap, ic->ic_curchan);
440		}
441		break;
442	default:
443		break;
444	}
445	return error;
446}
447
448/*
449 * Process a received frame.  The node associated with the sender
450 * should be supplied.  If nothing was found in the node table then
451 * the caller is assumed to supply a reference to iv_bss instead.
452 * The RSSI and a timestamp are also supplied.  The RSSI data is used
453 * during AP scanning to select a AP to associate with; it can have
454 * any units so long as values have consistent units and higher values
455 * mean ``better signal''.  The receive timestamp is currently not used
456 * by the 802.11 layer.
457 */
458static int
459wds_input(struct ieee80211_node *ni, struct mbuf *m,
460	int rssi, int noise, uint32_t rstamp)
461{
462#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
463#define	HAS_SEQ(type)	((type & 0x4) == 0)
464	struct ieee80211vap *vap = ni->ni_vap;
465	struct ieee80211com *ic = ni->ni_ic;
466	struct ifnet *ifp = vap->iv_ifp;
467	struct ieee80211_frame *wh;
468	struct ieee80211_key *key;
469	struct ether_header *eh;
470	int hdrspace, need_tap;
471	uint8_t dir, type, subtype, qos;
472	uint16_t rxseq;
473
474	if (m->m_flags & M_AMPDU_MPDU) {
475		/*
476		 * Fastpath for A-MPDU reorder q resubmission.  Frames
477		 * w/ M_AMPDU_MPDU marked have already passed through
478		 * here but were received out of order and been held on
479		 * the reorder queue.  When resubmitted they are marked
480		 * with the M_AMPDU_MPDU flag and we can bypass most of
481		 * the normal processing.
482		 */
483		wh = mtod(m, struct ieee80211_frame *);
484		type = IEEE80211_FC0_TYPE_DATA;
485		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
486		subtype = IEEE80211_FC0_SUBTYPE_QOS;
487		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
488		goto resubmit_ampdu;
489	}
490
491	KASSERT(ni != NULL, ("null node"));
492
493	need_tap = 1;			/* mbuf need to be tapped. */
494	type = -1;			/* undefined */
495
496	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
497		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
498		    ni->ni_macaddr, NULL,
499		    "too short (1): len %u", m->m_pkthdr.len);
500		vap->iv_stats.is_rx_tooshort++;
501		goto out;
502	}
503	/*
504	 * Bit of a cheat here, we use a pointer for a 3-address
505	 * frame format but don't reference fields past outside
506	 * ieee80211_frame_min w/o first validating the data is
507	 * present.
508	 */
509	wh = mtod(m, struct ieee80211_frame *);
510
511	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
512	    IEEE80211_FC0_VERSION_0) {
513		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
514		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
515		vap->iv_stats.is_rx_badversion++;
516		goto err;
517	}
518
519	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
520	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
521	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
522
523	/* NB: WDS vap's do not scan */
524	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
525		IEEE80211_DISCARD_MAC(vap,
526		    IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
527		    "too short (3): len %u", m->m_pkthdr.len);
528		vap->iv_stats.is_rx_tooshort++;
529		goto out;
530	}
531	/* NB: the TA is implicitly verified by finding the wds peer node */
532	if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
533	    !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
534		/* not interested in */
535		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
536		    wh->i_addr1, NULL, "%s", "not to bss");
537		vap->iv_stats.is_rx_wrongbss++;
538		goto out;
539	}
540	IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
541	ni->ni_noise = noise;
542	ni->ni_rstamp = rstamp;
543	if (HAS_SEQ(type)) {
544		uint8_t tid = ieee80211_gettid(wh);
545		if (IEEE80211_QOS_HAS_SEQ(wh) &&
546		    TID_TO_WME_AC(tid) >= WME_AC_VI)
547			ic->ic_wme.wme_hipri_traffic++;
548		rxseq = le16toh(*(uint16_t *)wh->i_seq);
549		if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
550		    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
551		    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
552			/* duplicate, discard */
553			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
554			    wh->i_addr1, "duplicate",
555			    "seqno <%u,%u> fragno <%u,%u> tid %u",
556			    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
557			    ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
558			    rxseq & IEEE80211_SEQ_FRAG_MASK,
559			    ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
560			    tid);
561			vap->iv_stats.is_rx_dup++;
562			IEEE80211_NODE_STAT(ni, rx_dup);
563			goto out;
564		}
565		ni->ni_rxseqs[tid] = rxseq;
566	}
567	switch (type) {
568	case IEEE80211_FC0_TYPE_DATA:
569		hdrspace = ieee80211_hdrspace(ic, wh);
570		if (m->m_len < hdrspace &&
571		    (m = m_pullup(m, hdrspace)) == NULL) {
572			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
573			    ni->ni_macaddr, NULL,
574			    "data too short: expecting %u", hdrspace);
575			vap->iv_stats.is_rx_tooshort++;
576			goto out;		/* XXX */
577		}
578		if (dir != IEEE80211_FC1_DIR_DSTODS) {
579			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
580			    wh, "data", "incorrect dir 0x%x", dir);
581			vap->iv_stats.is_rx_wrongdir++;
582			goto out;
583		}
584		/*
585		 * Only legacy WDS traffic should take this path.
586		 */
587		if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
588			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
589			    wh, "data", "%s", "not legacy wds");
590			vap->iv_stats.is_rx_wrongdir++;/*XXX*/
591			goto out;
592		}
593		if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
594			ni->ni_inact = ni->ni_inact_reload;
595		/*
596		 * Handle A-MPDU re-ordering.  If the frame is to be
597		 * processed directly then ieee80211_ampdu_reorder
598		 * will return 0; otherwise it has consumed the mbuf
599		 * and we should do nothing more with it.
600		 */
601		if ((m->m_flags & M_AMPDU) &&
602		    ieee80211_ampdu_reorder(ni, m) != 0) {
603			m = NULL;
604			goto out;
605		}
606	resubmit_ampdu:
607
608		/*
609		 * Handle privacy requirements.  Note that we
610		 * must not be preempted from here until after
611		 * we (potentially) call ieee80211_crypto_demic;
612		 * otherwise we may violate assumptions in the
613		 * crypto cipher modules used to do delayed update
614		 * of replay sequence numbers.
615		 */
616		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
617			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
618				/*
619				 * Discard encrypted frames when privacy is off.
620				 */
621				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
622				    wh, "WEP", "%s", "PRIVACY off");
623				vap->iv_stats.is_rx_noprivacy++;
624				IEEE80211_NODE_STAT(ni, rx_noprivacy);
625				goto out;
626			}
627			key = ieee80211_crypto_decap(ni, m, hdrspace);
628			if (key == NULL) {
629				/* NB: stats+msgs handled in crypto_decap */
630				IEEE80211_NODE_STAT(ni, rx_wepfail);
631				goto out;
632			}
633			wh = mtod(m, struct ieee80211_frame *);
634			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
635		} else {
636			/* XXX M_WEP and IEEE80211_F_PRIVACY */
637			key = NULL;
638		}
639
640		/*
641		 * Save QoS bits for use below--before we strip the header.
642		 */
643		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
644			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
645			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
646			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
647		} else
648			qos = 0;
649
650		/*
651		 * Next up, any fragmentation.
652		 */
653		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
654			m = ieee80211_defrag(ni, m, hdrspace);
655			if (m == NULL) {
656				/* Fragment dropped or frame not complete yet */
657				goto out;
658			}
659		}
660		wh = NULL;		/* no longer valid, catch any uses */
661
662		/*
663		 * Next strip any MSDU crypto bits.
664		 */
665		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
666			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
667			    ni->ni_macaddr, "data", "%s", "demic error");
668			vap->iv_stats.is_rx_demicfail++;
669			IEEE80211_NODE_STAT(ni, rx_demicfail);
670			goto out;
671		}
672
673		/* copy to listener after decrypt */
674		if (bpf_peers_present(vap->iv_rawbpf))
675			bpf_mtap(vap->iv_rawbpf, m);
676		need_tap = 0;
677
678		/*
679		 * Finally, strip the 802.11 header.
680		 */
681		m = ieee80211_decap(vap, m, hdrspace);
682		if (m == NULL) {
683			/* XXX mask bit to check for both */
684			/* don't count Null data frames as errors */
685			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
686			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
687				goto out;
688			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
689			    ni->ni_macaddr, "data", "%s", "decap error");
690			vap->iv_stats.is_rx_decap++;
691			IEEE80211_NODE_STAT(ni, rx_decap);
692			goto err;
693		}
694		eh = mtod(m, struct ether_header *);
695		if (!ieee80211_node_is_authorized(ni)) {
696			/*
697			 * Deny any non-PAE frames received prior to
698			 * authorization.  For open/shared-key
699			 * authentication the port is mark authorized
700			 * after authentication completes.  For 802.1x
701			 * the port is not marked authorized by the
702			 * authenticator until the handshake has completed.
703			 */
704			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
705				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
706				    eh->ether_shost, "data",
707				    "unauthorized port: ether type 0x%x len %u",
708				    eh->ether_type, m->m_pkthdr.len);
709				vap->iv_stats.is_rx_unauth++;
710				IEEE80211_NODE_STAT(ni, rx_unauth);
711				goto err;
712			}
713		} else {
714			/*
715			 * When denying unencrypted frames, discard
716			 * any non-PAE frames received without encryption.
717			 */
718			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
719			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
720			    eh->ether_type != htons(ETHERTYPE_PAE)) {
721				/*
722				 * Drop unencrypted frames.
723				 */
724				vap->iv_stats.is_rx_unencrypted++;
725				IEEE80211_NODE_STAT(ni, rx_unencrypted);
726				goto out;
727			}
728		}
729		/* XXX require HT? */
730		if (qos & IEEE80211_QOS_AMSDU) {
731			m = ieee80211_decap_amsdu(ni, m);
732			if (m == NULL)
733				return IEEE80211_FC0_TYPE_DATA;
734		} else {
735#ifdef IEEE80211_SUPPORT_SUPERG
736			m = ieee80211_decap_fastframe(vap, ni, m);
737			if (m == NULL)
738				return IEEE80211_FC0_TYPE_DATA;
739#endif
740		}
741		ieee80211_deliver_data(vap, ni, m);
742		return IEEE80211_FC0_TYPE_DATA;
743
744	case IEEE80211_FC0_TYPE_MGT:
745		vap->iv_stats.is_rx_mgmt++;
746		IEEE80211_NODE_STAT(ni, rx_mgmt);
747		if (dir != IEEE80211_FC1_DIR_NODS) {
748			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
749			    wh, "data", "incorrect dir 0x%x", dir);
750			vap->iv_stats.is_rx_wrongdir++;
751			goto err;
752		}
753		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
754			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
755			    ni->ni_macaddr, "mgt", "too short: len %u",
756			    m->m_pkthdr.len);
757			vap->iv_stats.is_rx_tooshort++;
758			goto out;
759		}
760#ifdef IEEE80211_DEBUG
761		if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
762			if_printf(ifp, "received %s from %s rssi %d\n",
763			    ieee80211_mgt_subtype_name[subtype >>
764				IEEE80211_FC0_SUBTYPE_SHIFT],
765			    ether_sprintf(wh->i_addr2), rssi);
766		}
767#endif
768		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
769			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
770			    wh, NULL, "%s", "WEP set but not permitted");
771			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
772			goto out;
773		}
774		if (bpf_peers_present(vap->iv_rawbpf))
775			bpf_mtap(vap->iv_rawbpf, m);
776		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
777		m_freem(m);
778		return IEEE80211_FC0_TYPE_MGT;
779
780	case IEEE80211_FC0_TYPE_CTL:
781		vap->iv_stats.is_rx_ctl++;
782		IEEE80211_NODE_STAT(ni, rx_ctrl);
783		goto out;
784	default:
785		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
786		    wh, "bad", "frame type 0x%x", type);
787		/* should not come here */
788		break;
789	}
790err:
791	ifp->if_ierrors++;
792out:
793	if (m != NULL) {
794		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
795			bpf_mtap(vap->iv_rawbpf, m);
796		m_freem(m);
797	}
798	return type;
799#undef SEQ_LEQ
800}
801
802static void
803wds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
804	int subtype, int rssi, int noise, u_int32_t rstamp)
805{
806	struct ieee80211vap *vap = ni->ni_vap;
807	struct ieee80211com *ic = ni->ni_ic;
808	struct ieee80211_frame *wh;
809	u_int8_t *frm, *efrm;
810
811	wh = mtod(m0, struct ieee80211_frame *);
812	frm = (u_int8_t *)&wh[1];
813	efrm = mtod(m0, u_int8_t *) + m0->m_len;
814	switch (subtype) {
815	case IEEE80211_FC0_SUBTYPE_DEAUTH:
816	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
817	case IEEE80211_FC0_SUBTYPE_BEACON:
818	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
819	case IEEE80211_FC0_SUBTYPE_AUTH:
820	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
821	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
822	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
823	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
824	case IEEE80211_FC0_SUBTYPE_DISASSOC:
825		vap->iv_stats.is_rx_mgtdiscard++;
826		break;
827	case IEEE80211_FC0_SUBTYPE_ACTION:
828		if (vap->iv_state != IEEE80211_S_RUN ||
829		    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
830			vap->iv_stats.is_rx_mgtdiscard++;
831			break;
832		}
833		ni->ni_inact = ni->ni_inact_reload;
834		if (ieee80211_parse_action(ni, m0) == 0)
835			ic->ic_recv_action(ni, frm, efrm);
836		break;
837	default:
838		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
839		     wh, "mgt", "subtype 0x%x not handled", subtype);
840		vap->iv_stats.is_rx_badsubtype++;
841		break;
842	}
843}
844