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