ieee80211_wds.c revision 191547
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 191547 2009-04-26 21:50:21Z 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.
254		 */
255		mcopy = m_copypacket(m, M_DONTWAIT);
256		if (mcopy == NULL) {
257			ifp->if_oerrors++;
258			/* XXX stat + msg */
259			continue;
260		}
261		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
262		if (ni == NULL) {
263			/* NB: ieee80211_find_txnode does stat+msg */
264			ifp->if_oerrors++;
265			m_freem(mcopy);
266			continue;
267		}
268		/* calculate priority so drivers can find the tx queue */
269		if (ieee80211_classify(ni, mcopy)) {
270			IEEE80211_DISCARD_MAC(vap,
271			    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS,
272			    eh->ether_dhost, NULL,
273			    "%s", "classification failure");
274			vap->iv_stats.is_tx_classify++;
275			ifp->if_oerrors++;
276			m_freem(mcopy);
277			ieee80211_free_node(ni);
278			continue;
279		}
280
281		BPF_MTAP(ifp, m);		/* 802.3 tx */
282
283		/*
284		 * Encapsulate the packet in prep for transmission.
285		 */
286		mcopy = ieee80211_encap(vap, ni, mcopy);
287		if (m == NULL) {
288			/* NB: stat+msg handled in ieee80211_encap */
289			ieee80211_free_node(ni);
290			continue;
291		}
292		mcopy->m_flags |= M_MCAST;
293		mcopy->m_pkthdr.rcvif = (void *) ni;
294
295		if (bpf_peers_present(vap->iv_rawbpf))
296			bpf_mtap(vap->iv_rawbpf, m);
297
298		err = parent->if_transmit(parent, mcopy);
299		if (err) {
300			/* NB: IFQ_HANDOFF reclaims mbuf */
301			ifp->if_oerrors++;
302			ieee80211_free_node(ni);
303		} else
304			ifp->if_opackets++;
305	}
306}
307
308/*
309 * Handle DWDS discovery on receipt of a 4-address frame in
310 * ap mode.  Queue the frame and post an event for someone
311 * to plumb the necessary WDS vap for this station.  Frames
312 * received prior to the vap set running will then be reprocessed
313 * as if they were just received.
314 */
315void
316ieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
317{
318	struct ieee80211vap *vap = ni->ni_vap;
319	struct ieee80211com *ic = ni->ni_ic;
320	int qlen, age;
321
322	IEEE80211_NODE_WDSQ_LOCK(ni);
323	if (!_IF_QFULL(&ni->ni_wdsq)) {
324		/*
325		 * Tag the frame with it's expiry time and insert
326		 * it in the queue.  The aging interval is 4 times
327		 * the listen interval specified by the station.
328		 * Frames that sit around too long are reclaimed
329		 * using this information.
330		 */
331		/* XXX handle overflow? */
332		/* XXX per/vap beacon interval? */
333		/* NB: TU -> secs */
334		age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024;
335		_IEEE80211_NODE_WDSQ_ENQUEUE(ni, m, qlen, age);
336		IEEE80211_NODE_WDSQ_UNLOCK(ni);
337
338		IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
339		    "save frame, %u now queued", qlen);
340	} else {
341		vap->iv_stats.is_dwds_qdrop++;
342		_IF_DROP(&ni->ni_wdsq);
343		IEEE80211_NODE_WDSQ_UNLOCK(ni);
344
345		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_WDS,
346		    mtod(m, struct ieee80211_frame *), "wds data",
347		    "pending q overflow, drops %d (len %d)",
348		    ni->ni_wdsq.ifq_drops, ni->ni_wdsq.ifq_len);
349
350#ifdef IEEE80211_DEBUG
351		if (ieee80211_msg_dumppkts(vap))
352			ieee80211_dump_pkt(ic, mtod(m, caddr_t),
353			    m->m_len, -1, -1);
354#endif
355		/* XXX tail drop? */
356		m_freem(m);
357	}
358	ieee80211_notify_wds_discover(ni);
359}
360
361/*
362 * Age frames on the WDS pending queue. The aging interval is
363 * 4 times the listen interval specified by the station.  This
364 * number is factored into the age calculations when the frame
365 * is placed on the queue.  We store ages as time differences
366 * so we can check and/or adjust only the head of the list.
367 * If a frame's age exceeds the threshold then discard it.
368 * The number of frames discarded is returned to the caller.
369 */
370int
371ieee80211_node_wdsq_age(struct ieee80211_node *ni)
372{
373#ifdef IEEE80211_DEBUG
374	struct ieee80211vap *vap = ni->ni_vap;
375#endif
376	struct mbuf *m;
377	int discard = 0;
378
379	IEEE80211_NODE_WDSQ_LOCK(ni);
380	while (_IF_POLL(&ni->ni_wdsq, m) != NULL &&
381	     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
382		IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
383			"discard frame, age %u", M_AGE_GET(m));
384
385		/* XXX could be optimized */
386		_IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m);
387		m_freem(m);
388		discard++;
389	}
390	if (m != NULL)
391		M_AGE_SUB(m, IEEE80211_INACT_WAIT);
392	IEEE80211_NODE_WDSQ_UNLOCK(ni);
393
394	IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
395	    "discard %u frames for age", discard);
396#if 0
397	IEEE80211_NODE_STAT_ADD(ni, wds_discard, discard);
398#endif
399	return discard;
400}
401
402/*
403 * IEEE80211_M_WDS vap state machine handler.
404 */
405static int
406wds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
407{
408	struct ieee80211com *ic = vap->iv_ic;
409	struct ieee80211_node *ni;
410	enum ieee80211_state ostate;
411	int error;
412
413	IEEE80211_LOCK_ASSERT(ic);
414
415	ostate = vap->iv_state;
416	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
417		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
418	vap->iv_state = nstate;			/* state transition */
419	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
420	if (ostate != IEEE80211_S_SCAN)
421		ieee80211_cancel_scan(vap);	/* background scan */
422	ni = vap->iv_bss;			/* NB: no reference held */
423	error = 0;
424	switch (nstate) {
425	case IEEE80211_S_INIT:
426		switch (ostate) {
427		case IEEE80211_S_SCAN:
428			ieee80211_cancel_scan(vap);
429			break;
430		default:
431			break;
432		}
433		if (ostate != IEEE80211_S_INIT) {
434			/* NB: optimize INIT -> INIT case */
435			ieee80211_reset_bss(vap);
436		}
437		break;
438	case IEEE80211_S_SCAN:
439		switch (ostate) {
440		case IEEE80211_S_INIT:
441			ieee80211_check_scan_current(vap);
442			break;
443		default:
444			break;
445		}
446		break;
447	case IEEE80211_S_RUN:
448		if (ostate == IEEE80211_S_INIT) {
449			/*
450			 * Already have a channel; bypass the scan
451			 * and startup immediately.
452			 */
453			error = ieee80211_create_wds(vap, ic->ic_curchan);
454		}
455		break;
456	default:
457		break;
458	}
459	return error;
460}
461
462/*
463 * Process a received frame.  The node associated with the sender
464 * should be supplied.  If nothing was found in the node table then
465 * the caller is assumed to supply a reference to iv_bss instead.
466 * The RSSI and a timestamp are also supplied.  The RSSI data is used
467 * during AP scanning to select a AP to associate with; it can have
468 * any units so long as values have consistent units and higher values
469 * mean ``better signal''.  The receive timestamp is currently not used
470 * by the 802.11 layer.
471 */
472static int
473wds_input(struct ieee80211_node *ni, struct mbuf *m,
474	int rssi, int noise, uint32_t rstamp)
475{
476#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
477#define	HAS_SEQ(type)	((type & 0x4) == 0)
478	struct ieee80211vap *vap = ni->ni_vap;
479	struct ieee80211com *ic = ni->ni_ic;
480	struct ifnet *ifp = vap->iv_ifp;
481	struct ieee80211_frame *wh;
482	struct ieee80211_key *key;
483	struct ether_header *eh;
484	int hdrspace, need_tap;
485	uint8_t dir, type, subtype, qos;
486	uint16_t rxseq;
487
488	if (m->m_flags & M_AMPDU_MPDU) {
489		/*
490		 * Fastpath for A-MPDU reorder q resubmission.  Frames
491		 * w/ M_AMPDU_MPDU marked have already passed through
492		 * here but were received out of order and been held on
493		 * the reorder queue.  When resubmitted they are marked
494		 * with the M_AMPDU_MPDU flag and we can bypass most of
495		 * the normal processing.
496		 */
497		wh = mtod(m, struct ieee80211_frame *);
498		type = IEEE80211_FC0_TYPE_DATA;
499		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
500		subtype = IEEE80211_FC0_SUBTYPE_QOS;
501		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
502		goto resubmit_ampdu;
503	}
504
505	KASSERT(ni != NULL, ("null node"));
506
507	need_tap = 1;			/* mbuf need to be tapped. */
508	type = -1;			/* undefined */
509
510	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
511		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
512		    ni->ni_macaddr, NULL,
513		    "too short (1): len %u", m->m_pkthdr.len);
514		vap->iv_stats.is_rx_tooshort++;
515		goto out;
516	}
517	/*
518	 * Bit of a cheat here, we use a pointer for a 3-address
519	 * frame format but don't reference fields past outside
520	 * ieee80211_frame_min w/o first validating the data is
521	 * present.
522	 */
523	wh = mtod(m, struct ieee80211_frame *);
524
525	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
526	    IEEE80211_FC0_VERSION_0) {
527		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
528		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
529		    wh->i_fc[0], wh->i_fc[1]);
530		vap->iv_stats.is_rx_badversion++;
531		goto err;
532	}
533
534	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
535	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
536	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
537
538	/* NB: WDS vap's do not scan */
539	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
540		IEEE80211_DISCARD_MAC(vap,
541		    IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
542		    "too short (3): len %u", m->m_pkthdr.len);
543		vap->iv_stats.is_rx_tooshort++;
544		goto out;
545	}
546	/* NB: the TA is implicitly verified by finding the wds peer node */
547	if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
548	    !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
549		/* not interested in */
550		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
551		    wh->i_addr1, NULL, "%s", "not to bss");
552		vap->iv_stats.is_rx_wrongbss++;
553		goto out;
554	}
555	IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
556	ni->ni_noise = noise;
557	ni->ni_rstamp = rstamp;
558	if (HAS_SEQ(type)) {
559		uint8_t tid = ieee80211_gettid(wh);
560		if (IEEE80211_QOS_HAS_SEQ(wh) &&
561		    TID_TO_WME_AC(tid) >= WME_AC_VI)
562			ic->ic_wme.wme_hipri_traffic++;
563		rxseq = le16toh(*(uint16_t *)wh->i_seq);
564		if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
565		    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
566		    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
567			/* duplicate, discard */
568			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
569			    wh->i_addr1, "duplicate",
570			    "seqno <%u,%u> fragno <%u,%u> tid %u",
571			    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
572			    ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
573			    rxseq & IEEE80211_SEQ_FRAG_MASK,
574			    ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
575			    tid);
576			vap->iv_stats.is_rx_dup++;
577			IEEE80211_NODE_STAT(ni, rx_dup);
578			goto out;
579		}
580		ni->ni_rxseqs[tid] = rxseq;
581	}
582	switch (type) {
583	case IEEE80211_FC0_TYPE_DATA:
584		hdrspace = ieee80211_hdrspace(ic, wh);
585		if (m->m_len < hdrspace &&
586		    (m = m_pullup(m, hdrspace)) == NULL) {
587			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
588			    ni->ni_macaddr, NULL,
589			    "data too short: expecting %u", hdrspace);
590			vap->iv_stats.is_rx_tooshort++;
591			goto out;		/* XXX */
592		}
593		if (dir != IEEE80211_FC1_DIR_DSTODS) {
594			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
595			    wh, "data", "incorrect dir 0x%x", dir);
596			vap->iv_stats.is_rx_wrongdir++;
597			goto out;
598		}
599		/*
600		 * Only legacy WDS traffic should take this path.
601		 */
602		if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
603			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
604			    wh, "data", "%s", "not legacy wds");
605			vap->iv_stats.is_rx_wrongdir++;/*XXX*/
606			goto out;
607		}
608		if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
609			ni->ni_inact = ni->ni_inact_reload;
610		/*
611		 * Handle A-MPDU re-ordering.  If the frame is to be
612		 * processed directly then ieee80211_ampdu_reorder
613		 * will return 0; otherwise it has consumed the mbuf
614		 * and we should do nothing more with it.
615		 */
616		if ((m->m_flags & M_AMPDU) &&
617		    ieee80211_ampdu_reorder(ni, m) != 0) {
618			m = NULL;
619			goto out;
620		}
621	resubmit_ampdu:
622
623		/*
624		 * Handle privacy requirements.  Note that we
625		 * must not be preempted from here until after
626		 * we (potentially) call ieee80211_crypto_demic;
627		 * otherwise we may violate assumptions in the
628		 * crypto cipher modules used to do delayed update
629		 * of replay sequence numbers.
630		 */
631		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
632			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
633				/*
634				 * Discard encrypted frames when privacy is off.
635				 */
636				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
637				    wh, "WEP", "%s", "PRIVACY off");
638				vap->iv_stats.is_rx_noprivacy++;
639				IEEE80211_NODE_STAT(ni, rx_noprivacy);
640				goto out;
641			}
642			key = ieee80211_crypto_decap(ni, m, hdrspace);
643			if (key == NULL) {
644				/* NB: stats+msgs handled in crypto_decap */
645				IEEE80211_NODE_STAT(ni, rx_wepfail);
646				goto out;
647			}
648			wh = mtod(m, struct ieee80211_frame *);
649			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
650		} else {
651			/* XXX M_WEP and IEEE80211_F_PRIVACY */
652			key = NULL;
653		}
654
655		/*
656		 * Save QoS bits for use below--before we strip the header.
657		 */
658		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
659			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
660			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
661			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
662		} else
663			qos = 0;
664
665		/*
666		 * Next up, any fragmentation.
667		 */
668		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
669			m = ieee80211_defrag(ni, m, hdrspace);
670			if (m == NULL) {
671				/* Fragment dropped or frame not complete yet */
672				goto out;
673			}
674		}
675		wh = NULL;		/* no longer valid, catch any uses */
676
677		/*
678		 * Next strip any MSDU crypto bits.
679		 */
680		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
681			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
682			    ni->ni_macaddr, "data", "%s", "demic error");
683			vap->iv_stats.is_rx_demicfail++;
684			IEEE80211_NODE_STAT(ni, rx_demicfail);
685			goto out;
686		}
687
688		/* copy to listener after decrypt */
689		if (bpf_peers_present(vap->iv_rawbpf))
690			bpf_mtap(vap->iv_rawbpf, m);
691		need_tap = 0;
692
693		/*
694		 * Finally, strip the 802.11 header.
695		 */
696		m = ieee80211_decap(vap, m, hdrspace);
697		if (m == NULL) {
698			/* XXX mask bit to check for both */
699			/* don't count Null data frames as errors */
700			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
701			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
702				goto out;
703			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
704			    ni->ni_macaddr, "data", "%s", "decap error");
705			vap->iv_stats.is_rx_decap++;
706			IEEE80211_NODE_STAT(ni, rx_decap);
707			goto err;
708		}
709		eh = mtod(m, struct ether_header *);
710		if (!ieee80211_node_is_authorized(ni)) {
711			/*
712			 * Deny any non-PAE frames received prior to
713			 * authorization.  For open/shared-key
714			 * authentication the port is mark authorized
715			 * after authentication completes.  For 802.1x
716			 * the port is not marked authorized by the
717			 * authenticator until the handshake has completed.
718			 */
719			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
720				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
721				    eh->ether_shost, "data",
722				    "unauthorized port: ether type 0x%x len %u",
723				    eh->ether_type, m->m_pkthdr.len);
724				vap->iv_stats.is_rx_unauth++;
725				IEEE80211_NODE_STAT(ni, rx_unauth);
726				goto err;
727			}
728		} else {
729			/*
730			 * When denying unencrypted frames, discard
731			 * any non-PAE frames received without encryption.
732			 */
733			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
734			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
735			    eh->ether_type != htons(ETHERTYPE_PAE)) {
736				/*
737				 * Drop unencrypted frames.
738				 */
739				vap->iv_stats.is_rx_unencrypted++;
740				IEEE80211_NODE_STAT(ni, rx_unencrypted);
741				goto out;
742			}
743		}
744		/* XXX require HT? */
745		if (qos & IEEE80211_QOS_AMSDU) {
746			m = ieee80211_decap_amsdu(ni, m);
747			if (m == NULL)
748				return IEEE80211_FC0_TYPE_DATA;
749		} else {
750#ifdef IEEE80211_SUPPORT_SUPERG
751			m = ieee80211_decap_fastframe(vap, ni, m);
752			if (m == NULL)
753				return IEEE80211_FC0_TYPE_DATA;
754#endif
755		}
756		ieee80211_deliver_data(vap, ni, m);
757		return IEEE80211_FC0_TYPE_DATA;
758
759	case IEEE80211_FC0_TYPE_MGT:
760		vap->iv_stats.is_rx_mgmt++;
761		IEEE80211_NODE_STAT(ni, rx_mgmt);
762		if (dir != IEEE80211_FC1_DIR_NODS) {
763			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
764			    wh, "data", "incorrect dir 0x%x", dir);
765			vap->iv_stats.is_rx_wrongdir++;
766			goto err;
767		}
768		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
769			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
770			    ni->ni_macaddr, "mgt", "too short: len %u",
771			    m->m_pkthdr.len);
772			vap->iv_stats.is_rx_tooshort++;
773			goto out;
774		}
775#ifdef IEEE80211_DEBUG
776		if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
777			if_printf(ifp, "received %s from %s rssi %d\n",
778			    ieee80211_mgt_subtype_name[subtype >>
779				IEEE80211_FC0_SUBTYPE_SHIFT],
780			    ether_sprintf(wh->i_addr2), rssi);
781		}
782#endif
783		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
784			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
785			    wh, NULL, "%s", "WEP set but not permitted");
786			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
787			goto out;
788		}
789		if (bpf_peers_present(vap->iv_rawbpf))
790			bpf_mtap(vap->iv_rawbpf, m);
791		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
792		m_freem(m);
793		return IEEE80211_FC0_TYPE_MGT;
794
795	case IEEE80211_FC0_TYPE_CTL:
796		vap->iv_stats.is_rx_ctl++;
797		IEEE80211_NODE_STAT(ni, rx_ctrl);
798		goto out;
799	default:
800		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
801		    wh, "bad", "frame type 0x%x", type);
802		/* should not come here */
803		break;
804	}
805err:
806	ifp->if_ierrors++;
807out:
808	if (m != NULL) {
809		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
810			bpf_mtap(vap->iv_rawbpf, m);
811		m_freem(m);
812	}
813	return type;
814#undef SEQ_LEQ
815}
816
817static void
818wds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
819	int subtype, int rssi, int noise, u_int32_t rstamp)
820{
821	struct ieee80211vap *vap = ni->ni_vap;
822	struct ieee80211com *ic = ni->ni_ic;
823	struct ieee80211_frame *wh;
824	u_int8_t *frm, *efrm;
825
826	wh = mtod(m0, struct ieee80211_frame *);
827	frm = (u_int8_t *)&wh[1];
828	efrm = mtod(m0, u_int8_t *) + m0->m_len;
829	switch (subtype) {
830	case IEEE80211_FC0_SUBTYPE_DEAUTH:
831	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
832	case IEEE80211_FC0_SUBTYPE_BEACON:
833	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
834	case IEEE80211_FC0_SUBTYPE_AUTH:
835	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
836	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
837	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
838	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
839	case IEEE80211_FC0_SUBTYPE_DISASSOC:
840		vap->iv_stats.is_rx_mgtdiscard++;
841		break;
842	case IEEE80211_FC0_SUBTYPE_ACTION:
843		if (vap->iv_state != IEEE80211_S_RUN ||
844		    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
845			vap->iv_stats.is_rx_mgtdiscard++;
846			break;
847		}
848		ni->ni_inact = ni->ni_inact_reload;
849		if (ieee80211_parse_action(ni, m0) == 0)
850			ic->ic_recv_action(ni, frm, efrm);
851		break;
852	default:
853		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
854		     wh, "mgt", "subtype 0x%x not handled", subtype);
855		vap->iv_stats.is_rx_badsubtype++;
856		break;
857	}
858}
859