ieee80211_wds.c revision 243882
1178354Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3178354Ssam * All rights reserved.
4178354Ssam *
5178354Ssam * Redistribution and use in source and binary forms, with or without
6178354Ssam * modification, are permitted provided that the following conditions
7178354Ssam * are met:
8178354Ssam * 1. Redistributions of source code must retain the above copyright
9178354Ssam *    notice, this list of conditions and the following disclaimer.
10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright
11178354Ssam *    notice, this list of conditions and the following disclaimer in the
12178354Ssam *    documentation and/or other materials provided with the distribution.
13178354Ssam *
14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24178354Ssam */
25178354Ssam
26178354Ssam#include <sys/cdefs.h>
27178354Ssam#ifdef __FreeBSD__
28178354Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_wds.c 243882 2012-12-05 08:04:20Z glebius $");
29178354Ssam#endif
30178354Ssam
31178354Ssam/*
32178354Ssam * IEEE 802.11 WDS mode support.
33178354Ssam */
34178354Ssam#include "opt_inet.h"
35178354Ssam#include "opt_wlan.h"
36178354Ssam
37178354Ssam#include <sys/param.h>
38178354Ssam#include <sys/systm.h>
39178354Ssam#include <sys/mbuf.h>
40178354Ssam#include <sys/malloc.h>
41178354Ssam#include <sys/kernel.h>
42178354Ssam
43178354Ssam#include <sys/socket.h>
44178354Ssam#include <sys/sockio.h>
45178354Ssam#include <sys/endian.h>
46178354Ssam#include <sys/errno.h>
47178354Ssam#include <sys/proc.h>
48178354Ssam#include <sys/sysctl.h>
49178354Ssam
50178354Ssam#include <net/if.h>
51178354Ssam#include <net/if_media.h>
52178354Ssam#include <net/if_llc.h>
53178354Ssam#include <net/ethernet.h>
54178354Ssam
55178354Ssam#include <net/bpf.h>
56178354Ssam
57178354Ssam#include <net80211/ieee80211_var.h>
58178354Ssam#include <net80211/ieee80211_wds.h>
59178354Ssam#include <net80211/ieee80211_input.h>
60190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
61190391Ssam#include <net80211/ieee80211_superg.h>
62190391Ssam#endif
63178354Ssam
64178354Ssamstatic void wds_vattach(struct ieee80211vap *);
65178354Ssamstatic int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66192468Ssamstatic	int wds_input(struct ieee80211_node *ni, struct mbuf *m, int, int);
67178354Ssamstatic void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *,
68192468Ssam	    int subtype, int, int);
69178354Ssam
70178354Ssamvoid
71178354Ssamieee80211_wds_attach(struct ieee80211com *ic)
72178354Ssam{
73178354Ssam	ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach;
74178354Ssam}
75178354Ssam
76178354Ssamvoid
77178354Ssamieee80211_wds_detach(struct ieee80211com *ic)
78178354Ssam{
79178354Ssam}
80178354Ssam
81178354Ssamstatic void
82178354Ssamwds_vdetach(struct ieee80211vap *vap)
83178354Ssam{
84178354Ssam	if (vap->iv_bss != NULL) {
85178354Ssam		/* XXX locking? */
86178354Ssam		if (vap->iv_bss->ni_wdsvap == vap)
87178354Ssam			vap->iv_bss->ni_wdsvap = NULL;
88178354Ssam	}
89178354Ssam}
90178354Ssam
91178354Ssamstatic void
92178354Ssamwds_vattach(struct ieee80211vap *vap)
93178354Ssam{
94178354Ssam	vap->iv_newstate = wds_newstate;
95178354Ssam	vap->iv_input = wds_input;
96178354Ssam	vap->iv_recv_mgmt = wds_recv_mgmt;
97178354Ssam	vap->iv_opdetach = wds_vdetach;
98178354Ssam}
99178354Ssam
100195379Ssamstatic void
101195379Ssamwds_flush(struct ieee80211_node *ni)
102195379Ssam{
103195379Ssam	struct ieee80211com *ic = ni->ni_ic;
104195379Ssam	struct mbuf *m, *next;
105195379Ssam	int8_t rssi, nf;
106195379Ssam
107195379Ssam	m = ieee80211_ageq_remove(&ic->ic_stageq,
108195379Ssam	    (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
109195379Ssam	if (m == NULL)
110195379Ssam		return;
111195379Ssam
112195379Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_WDS, ni,
113195379Ssam	    "%s", "flush wds queue");
114195379Ssam	ic->ic_node_getsignal(ni, &rssi, &nf);
115195379Ssam	for (; m != NULL; m = next) {
116195379Ssam		next = m->m_nextpkt;
117195379Ssam		m->m_nextpkt = NULL;
118195379Ssam		ieee80211_input(ni, m, rssi, nf);
119195379Ssam	}
120195379Ssam}
121195379Ssam
122178354Ssamstatic int
123178354Ssamieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan)
124178354Ssam{
125178354Ssam	struct ieee80211com *ic = vap->iv_ic;
126178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
127178354Ssam	struct ieee80211_node *ni, *obss;
128178354Ssam
129178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
130178354Ssam	     "%s: creating link to %s on channel %u\n", __func__,
131178354Ssam	     ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan));
132178354Ssam
133178354Ssam	/* NB: vap create must specify the bssid for the link */
134178354Ssam	KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid"));
135178354Ssam	/* NB: we should only be called on RUN transition */
136178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state"));
137178354Ssam
138178354Ssam	if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
139178354Ssam		/*
140178354Ssam		 * Dynamic/non-legacy WDS.  Reference the associated
141178354Ssam		 * station specified by the desired bssid setup at vap
142178354Ssam		 * create.  Point ni_wdsvap at the WDS vap so 4-address
143178354Ssam		 * frames received through the associated AP vap will
144178354Ssam		 * be dispatched upward (e.g. to a bridge) as though
145178354Ssam		 * they arrived on the WDS vap.
146178354Ssam		 */
147178354Ssam		IEEE80211_NODE_LOCK(nt);
148178354Ssam		obss = NULL;
149178354Ssam		ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid);
150178354Ssam		if (ni == NULL) {
151178354Ssam			/*
152178354Ssam			 * Node went away before we could hookup.  This
153178354Ssam			 * should be ok; no traffic will flow and a leave
154178354Ssam			 * event will be dispatched that should cause
155178354Ssam			 * the vap to be destroyed.
156178354Ssam			 */
157178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
158178354Ssam			    "%s: station %s went away\n",
159178354Ssam			    __func__, ether_sprintf(vap->iv_des_bssid));
160178354Ssam			/* XXX stat? */
161178354Ssam		} else if (ni->ni_wdsvap != NULL) {
162178354Ssam			/*
163178354Ssam			 * Node already setup with a WDS vap; we cannot
164178354Ssam			 * allow multiple references so disallow.  If
165178354Ssam			 * ni_wdsvap points at us that's ok; we should
166178354Ssam			 * do nothing anyway.
167178354Ssam			 */
168178354Ssam			/* XXX printf instead? */
169178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
170178354Ssam			    "%s: station %s in use with %s\n",
171178354Ssam			    __func__, ether_sprintf(vap->iv_des_bssid),
172178354Ssam			    ni->ni_wdsvap->iv_ifp->if_xname);
173178354Ssam			/* XXX stat? */
174178354Ssam		} else {
175178354Ssam			/*
176178354Ssam			 * Committed to new node, setup state.
177178354Ssam			 */
178178354Ssam			obss = vap->iv_bss;
179178354Ssam			vap->iv_bss = ni;
180178354Ssam			ni->ni_wdsvap = vap;
181178354Ssam		}
182178354Ssam		IEEE80211_NODE_UNLOCK(nt);
183178354Ssam		if (obss != NULL) {
184178354Ssam			/* NB: deferred to avoid recursive lock */
185178354Ssam			ieee80211_free_node(obss);
186178354Ssam		}
187178354Ssam	} else {
188178354Ssam		/*
189178354Ssam		 * Legacy WDS vap setup.
190178354Ssam		 */
191178354Ssam		/*
192178354Ssam		 * The far end does not associate so we just create
193178354Ssam		 * create a new node and install it as the vap's
194178354Ssam		 * bss node.  We must simulate an association and
195178354Ssam		 * authorize the port for traffic to flow.
196178354Ssam		 * XXX check if node already in sta table?
197178354Ssam		 */
198178354Ssam		ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
199178354Ssam		if (ni != NULL) {
200178354Ssam			obss = vap->iv_bss;
201178354Ssam			vap->iv_bss = ieee80211_ref_node(ni);
202178354Ssam			ni->ni_flags |= IEEE80211_NODE_AREF;
203178354Ssam			if (obss != NULL)
204178354Ssam				ieee80211_free_node(obss);
205178354Ssam			/* give driver a chance to setup state like ni_txrate */
206178354Ssam			if (ic->ic_newassoc != NULL)
207178354Ssam				ic->ic_newassoc(ni, 1);
208178354Ssam			/* tell the authenticator about new station */
209178354Ssam			if (vap->iv_auth->ia_node_join != NULL)
210178354Ssam				vap->iv_auth->ia_node_join(ni);
211178354Ssam			if (ni->ni_authmode != IEEE80211_AUTH_8021X)
212178354Ssam				ieee80211_node_authorize(ni);
213178354Ssam
214178354Ssam			ieee80211_notify_node_join(ni, 1 /*newassoc*/);
215178354Ssam			/* XXX inject l2uf frame */
216178354Ssam		}
217178354Ssam	}
218178354Ssam
219178354Ssam	/*
220195379Ssam	 * Flush any pending frames now that were setup.
221178354Ssam	 */
222195379Ssam	if (ni != NULL)
223195379Ssam		wds_flush(ni);
224178354Ssam	return (ni == NULL ? ENOENT : 0);
225178354Ssam}
226178354Ssam
227178354Ssam/*
228178354Ssam * Propagate multicast frames of an ap vap to all DWDS links.
229178354Ssam * The caller is assumed to have verified this frame is multicast.
230178354Ssam */
231178354Ssamvoid
232178354Ssamieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m)
233178354Ssam{
234178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
235178354Ssam	struct ifnet *parent = ic->ic_ifp;
236178354Ssam	const struct ether_header *eh = mtod(m, const struct ether_header *);
237178354Ssam	struct ieee80211_node *ni;
238178354Ssam	struct ieee80211vap *vap;
239178354Ssam	struct ifnet *ifp;
240178354Ssam	struct mbuf *mcopy;
241178354Ssam	int err;
242178354Ssam
243178354Ssam	KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost),
244178354Ssam	    ("%s not mcast", ether_sprintf(eh->ether_dhost)));
245178354Ssam
246178354Ssam	/* XXX locking */
247178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
248178354Ssam		/* only DWDS vaps are interesting */
249178354Ssam		if (vap->iv_opmode != IEEE80211_M_WDS ||
250178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))
251178354Ssam			continue;
252178354Ssam		/* if it came in this interface, don't send it back out */
253178354Ssam		ifp = vap->iv_ifp;
254178354Ssam		if (ifp == m->m_pkthdr.rcvif)
255178354Ssam			continue;
256178354Ssam		/*
257190672Ssam		 * Duplicate the frame and send it.
258178354Ssam		 */
259243882Sglebius		mcopy = m_copypacket(m, M_NOWAIT);
260178354Ssam		if (mcopy == NULL) {
261178354Ssam			ifp->if_oerrors++;
262178354Ssam			/* XXX stat + msg */
263178354Ssam			continue;
264178354Ssam		}
265178354Ssam		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
266178354Ssam		if (ni == NULL) {
267178354Ssam			/* NB: ieee80211_find_txnode does stat+msg */
268178354Ssam			ifp->if_oerrors++;
269178354Ssam			m_freem(mcopy);
270178354Ssam			continue;
271178354Ssam		}
272190672Ssam		/* calculate priority so drivers can find the tx queue */
273178354Ssam		if (ieee80211_classify(ni, mcopy)) {
274178354Ssam			IEEE80211_DISCARD_MAC(vap,
275178354Ssam			    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS,
276178354Ssam			    eh->ether_dhost, NULL,
277178354Ssam			    "%s", "classification failure");
278178354Ssam			vap->iv_stats.is_tx_classify++;
279178354Ssam			ifp->if_oerrors++;
280178354Ssam			m_freem(mcopy);
281178354Ssam			ieee80211_free_node(ni);
282178354Ssam			continue;
283178354Ssam		}
284191542Ssam
285191542Ssam		BPF_MTAP(ifp, m);		/* 802.3 tx */
286191542Ssam
287190672Ssam		/*
288190672Ssam		 * Encapsulate the packet in prep for transmission.
289190672Ssam		 */
290190672Ssam		mcopy = ieee80211_encap(vap, ni, mcopy);
291194461Srpaulo		if (mcopy == NULL) {
292190672Ssam			/* NB: stat+msg handled in ieee80211_encap */
293190672Ssam			ieee80211_free_node(ni);
294190672Ssam			continue;
295190672Ssam		}
296190672Ssam		mcopy->m_flags |= M_MCAST;
297178354Ssam		mcopy->m_pkthdr.rcvif = (void *) ni;
298178354Ssam
299186658Ssam		err = parent->if_transmit(parent, mcopy);
300178354Ssam		if (err) {
301178354Ssam			/* NB: IFQ_HANDOFF reclaims mbuf */
302178354Ssam			ifp->if_oerrors++;
303178354Ssam			ieee80211_free_node(ni);
304178354Ssam		} else
305178354Ssam			ifp->if_opackets++;
306178354Ssam	}
307178354Ssam}
308178354Ssam
309178354Ssam/*
310178354Ssam * Handle DWDS discovery on receipt of a 4-address frame in
311178354Ssam * ap mode.  Queue the frame and post an event for someone
312178354Ssam * to plumb the necessary WDS vap for this station.  Frames
313178354Ssam * received prior to the vap set running will then be reprocessed
314178354Ssam * as if they were just received.
315178354Ssam */
316178354Ssamvoid
317178354Ssamieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
318178354Ssam{
319178354Ssam	struct ieee80211com *ic = ni->ni_ic;
320178354Ssam
321195379Ssam	/*
322195379Ssam	 * Save the frame with an aging interval 4 times
323195379Ssam	 * the listen interval specified by the station.
324195379Ssam	 * Frames that sit around too long are reclaimed
325195379Ssam	 * using this information.
326195379Ssam	 * XXX handle overflow?
327195379Ssam	 * XXX per/vap beacon interval?
328195379Ssam	 */
329195379Ssam	m->m_pkthdr.rcvif = (void *)(uintptr_t)
330195379Ssam	    ieee80211_mac_hash(ic, ni->ni_macaddr);
331195379Ssam	(void) ieee80211_ageq_append(&ic->ic_stageq, m,
332195379Ssam	    ((ni->ni_intval * ic->ic_lintval) << 2) / 1024);
333178354Ssam	ieee80211_notify_wds_discover(ni);
334178354Ssam}
335178354Ssam
336178354Ssam/*
337178354Ssam * IEEE80211_M_WDS vap state machine handler.
338178354Ssam */
339178354Ssamstatic int
340178354Ssamwds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
341178354Ssam{
342178354Ssam	struct ieee80211com *ic = vap->iv_ic;
343178354Ssam	struct ieee80211_node *ni;
344178354Ssam	enum ieee80211_state ostate;
345178354Ssam	int error;
346178354Ssam
347178354Ssam	IEEE80211_LOCK_ASSERT(ic);
348178354Ssam
349178354Ssam	ostate = vap->iv_state;
350178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
351178354Ssam		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
352178354Ssam	vap->iv_state = nstate;			/* state transition */
353178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
354178354Ssam	if (ostate != IEEE80211_S_SCAN)
355178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
356178354Ssam	ni = vap->iv_bss;			/* NB: no reference held */
357178354Ssam	error = 0;
358178354Ssam	switch (nstate) {
359178354Ssam	case IEEE80211_S_INIT:
360178354Ssam		switch (ostate) {
361178354Ssam		case IEEE80211_S_SCAN:
362178354Ssam			ieee80211_cancel_scan(vap);
363178354Ssam			break;
364178354Ssam		default:
365178354Ssam			break;
366178354Ssam		}
367178354Ssam		if (ostate != IEEE80211_S_INIT) {
368178354Ssam			/* NB: optimize INIT -> INIT case */
369178354Ssam			ieee80211_reset_bss(vap);
370178354Ssam		}
371178354Ssam		break;
372178354Ssam	case IEEE80211_S_SCAN:
373178354Ssam		switch (ostate) {
374178354Ssam		case IEEE80211_S_INIT:
375178354Ssam			ieee80211_check_scan_current(vap);
376178354Ssam			break;
377178354Ssam		default:
378178354Ssam			break;
379178354Ssam		}
380178354Ssam		break;
381178354Ssam	case IEEE80211_S_RUN:
382178354Ssam		if (ostate == IEEE80211_S_INIT) {
383178354Ssam			/*
384178354Ssam			 * Already have a channel; bypass the scan
385178354Ssam			 * and startup immediately.
386178354Ssam			 */
387178354Ssam			error = ieee80211_create_wds(vap, ic->ic_curchan);
388178354Ssam		}
389178354Ssam		break;
390178354Ssam	default:
391178354Ssam		break;
392178354Ssam	}
393178354Ssam	return error;
394178354Ssam}
395178354Ssam
396178354Ssam/*
397178354Ssam * Process a received frame.  The node associated with the sender
398178354Ssam * should be supplied.  If nothing was found in the node table then
399178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
400178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
401178354Ssam * during AP scanning to select a AP to associate with; it can have
402178354Ssam * any units so long as values have consistent units and higher values
403178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
404178354Ssam * by the 802.11 layer.
405178354Ssam */
406178354Ssamstatic int
407192468Ssamwds_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
408178354Ssam{
409178354Ssam#define	HAS_SEQ(type)	((type & 0x4) == 0)
410178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
411178354Ssam	struct ieee80211com *ic = ni->ni_ic;
412178354Ssam	struct ifnet *ifp = vap->iv_ifp;
413178354Ssam	struct ieee80211_frame *wh;
414178354Ssam	struct ieee80211_key *key;
415178354Ssam	struct ether_header *eh;
416203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
417178354Ssam	uint8_t dir, type, subtype, qos;
418178354Ssam	uint16_t rxseq;
419178354Ssam
420183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
421178354Ssam		/*
422178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
423183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
424183247Ssam		 * here but were received out of order and been held on
425183247Ssam		 * the reorder queue.  When resubmitted they are marked
426183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
427183247Ssam		 * the normal processing.
428178354Ssam		 */
429178354Ssam		wh = mtod(m, struct ieee80211_frame *);
430178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
431178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
432178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
433178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
434178354Ssam		goto resubmit_ampdu;
435178354Ssam	}
436178354Ssam
437178354Ssam	KASSERT(ni != NULL, ("null node"));
438178354Ssam
439178354Ssam	type = -1;			/* undefined */
440178354Ssam
441178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
442178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
443178354Ssam		    ni->ni_macaddr, NULL,
444178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
445178354Ssam		vap->iv_stats.is_rx_tooshort++;
446178354Ssam		goto out;
447178354Ssam	}
448178354Ssam	/*
449178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
450178354Ssam	 * frame format but don't reference fields past outside
451178354Ssam	 * ieee80211_frame_min w/o first validating the data is
452178354Ssam	 * present.
453178354Ssam	 */
454178354Ssam	wh = mtod(m, struct ieee80211_frame *);
455178354Ssam
456218958Sbschmidt	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
457218958Sbschmidt		ni->ni_inact = ni->ni_inact_reload;
458218958Sbschmidt
459178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
460178354Ssam	    IEEE80211_FC0_VERSION_0) {
461178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
462191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
463191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
464178354Ssam		vap->iv_stats.is_rx_badversion++;
465178354Ssam		goto err;
466178354Ssam	}
467178354Ssam
468178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
469178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
470178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
471178354Ssam
472178354Ssam	/* NB: WDS vap's do not scan */
473178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
474178354Ssam		IEEE80211_DISCARD_MAC(vap,
475178354Ssam		    IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
476178354Ssam		    "too short (3): len %u", m->m_pkthdr.len);
477178354Ssam		vap->iv_stats.is_rx_tooshort++;
478178354Ssam		goto out;
479178354Ssam	}
480178354Ssam	/* NB: the TA is implicitly verified by finding the wds peer node */
481178354Ssam	if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
482178354Ssam	    !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
483178354Ssam		/* not interested in */
484178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
485178354Ssam		    wh->i_addr1, NULL, "%s", "not to bss");
486178354Ssam		vap->iv_stats.is_rx_wrongbss++;
487178354Ssam		goto out;
488178354Ssam	}
489178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
490192468Ssam	ni->ni_noise = nf;
491178354Ssam	if (HAS_SEQ(type)) {
492178354Ssam		uint8_t tid = ieee80211_gettid(wh);
493178354Ssam		if (IEEE80211_QOS_HAS_SEQ(wh) &&
494178354Ssam		    TID_TO_WME_AC(tid) >= WME_AC_VI)
495178354Ssam			ic->ic_wme.wme_hipri_traffic++;
496178354Ssam		rxseq = le16toh(*(uint16_t *)wh->i_seq);
497221418Sadrian		if (! ieee80211_check_rxseq(ni, wh)) {
498178354Ssam			/* duplicate, discard */
499178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
500178354Ssam			    wh->i_addr1, "duplicate",
501178354Ssam			    "seqno <%u,%u> fragno <%u,%u> tid %u",
502178354Ssam			    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
503178354Ssam			    ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
504178354Ssam			    rxseq & IEEE80211_SEQ_FRAG_MASK,
505178354Ssam			    ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
506178354Ssam			    tid);
507178354Ssam			vap->iv_stats.is_rx_dup++;
508178354Ssam			IEEE80211_NODE_STAT(ni, rx_dup);
509178354Ssam			goto out;
510178354Ssam		}
511178354Ssam		ni->ni_rxseqs[tid] = rxseq;
512178354Ssam	}
513178354Ssam	switch (type) {
514178354Ssam	case IEEE80211_FC0_TYPE_DATA:
515178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
516178354Ssam		if (m->m_len < hdrspace &&
517178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
518178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
519178354Ssam			    ni->ni_macaddr, NULL,
520178354Ssam			    "data too short: expecting %u", hdrspace);
521178354Ssam			vap->iv_stats.is_rx_tooshort++;
522178354Ssam			goto out;		/* XXX */
523178354Ssam		}
524178354Ssam		if (dir != IEEE80211_FC1_DIR_DSTODS) {
525178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
526178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
527178354Ssam			vap->iv_stats.is_rx_wrongdir++;
528178354Ssam			goto out;
529178354Ssam		}
530178354Ssam		/*
531178354Ssam		 * Only legacy WDS traffic should take this path.
532178354Ssam		 */
533178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
534178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
535178354Ssam			    wh, "data", "%s", "not legacy wds");
536178354Ssam			vap->iv_stats.is_rx_wrongdir++;/*XXX*/
537178354Ssam			goto out;
538178354Ssam		}
539178354Ssam		/*
540183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
541183247Ssam		 * processed directly then ieee80211_ampdu_reorder
542178354Ssam		 * will return 0; otherwise it has consumed the mbuf
543178354Ssam		 * and we should do nothing more with it.
544178354Ssam		 */
545183247Ssam		if ((m->m_flags & M_AMPDU) &&
546178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
547178354Ssam			m = NULL;
548178354Ssam			goto out;
549178354Ssam		}
550178354Ssam	resubmit_ampdu:
551178354Ssam
552178354Ssam		/*
553178354Ssam		 * Handle privacy requirements.  Note that we
554178354Ssam		 * must not be preempted from here until after
555178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
556178354Ssam		 * otherwise we may violate assumptions in the
557178354Ssam		 * crypto cipher modules used to do delayed update
558178354Ssam		 * of replay sequence numbers.
559178354Ssam		 */
560178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
561178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
562178354Ssam				/*
563178354Ssam				 * Discard encrypted frames when privacy is off.
564178354Ssam				 */
565178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
566178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
567178354Ssam				vap->iv_stats.is_rx_noprivacy++;
568178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
569178354Ssam				goto out;
570178354Ssam			}
571178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
572178354Ssam			if (key == NULL) {
573178354Ssam				/* NB: stats+msgs handled in crypto_decap */
574178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
575178354Ssam				goto out;
576178354Ssam			}
577178354Ssam			wh = mtod(m, struct ieee80211_frame *);
578178354Ssam			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
579178354Ssam		} else {
580178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
581178354Ssam			key = NULL;
582178354Ssam		}
583178354Ssam
584178354Ssam		/*
585178354Ssam		 * Save QoS bits for use below--before we strip the header.
586178354Ssam		 */
587178354Ssam		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
588178354Ssam			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
589178354Ssam			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
590178354Ssam			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
591178354Ssam		} else
592178354Ssam			qos = 0;
593178354Ssam
594178354Ssam		/*
595178354Ssam		 * Next up, any fragmentation.
596178354Ssam		 */
597178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
598178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
599178354Ssam			if (m == NULL) {
600178354Ssam				/* Fragment dropped or frame not complete yet */
601178354Ssam				goto out;
602178354Ssam			}
603178354Ssam		}
604178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
605178354Ssam
606178354Ssam		/*
607178354Ssam		 * Next strip any MSDU crypto bits.
608178354Ssam		 */
609178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
610178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
611178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
612178354Ssam			vap->iv_stats.is_rx_demicfail++;
613178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
614178354Ssam			goto out;
615178354Ssam		}
616178354Ssam
617178354Ssam		/* copy to listener after decrypt */
618192468Ssam		if (ieee80211_radiotap_active_vap(vap))
619192468Ssam			ieee80211_radiotap_rx(vap, m);
620178354Ssam		need_tap = 0;
621178354Ssam
622178354Ssam		/*
623178354Ssam		 * Finally, strip the 802.11 header.
624178354Ssam		 */
625178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
626178354Ssam		if (m == NULL) {
627178354Ssam			/* XXX mask bit to check for both */
628178354Ssam			/* don't count Null data frames as errors */
629178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
630178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
631178354Ssam				goto out;
632178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
633178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
634178354Ssam			vap->iv_stats.is_rx_decap++;
635178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
636178354Ssam			goto err;
637178354Ssam		}
638178354Ssam		eh = mtod(m, struct ether_header *);
639178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
640178354Ssam			/*
641178354Ssam			 * Deny any non-PAE frames received prior to
642178354Ssam			 * authorization.  For open/shared-key
643178354Ssam			 * authentication the port is mark authorized
644178354Ssam			 * after authentication completes.  For 802.1x
645178354Ssam			 * the port is not marked authorized by the
646178354Ssam			 * authenticator until the handshake has completed.
647178354Ssam			 */
648178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
649178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
650178354Ssam				    eh->ether_shost, "data",
651178354Ssam				    "unauthorized port: ether type 0x%x len %u",
652178354Ssam				    eh->ether_type, m->m_pkthdr.len);
653178354Ssam				vap->iv_stats.is_rx_unauth++;
654178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
655178354Ssam				goto err;
656178354Ssam			}
657178354Ssam		} else {
658178354Ssam			/*
659178354Ssam			 * When denying unencrypted frames, discard
660178354Ssam			 * any non-PAE frames received without encryption.
661178354Ssam			 */
662178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
663178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
664178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
665178354Ssam				/*
666178354Ssam				 * Drop unencrypted frames.
667178354Ssam				 */
668178354Ssam				vap->iv_stats.is_rx_unencrypted++;
669178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
670178354Ssam				goto out;
671178354Ssam			}
672178354Ssam		}
673178354Ssam		/* XXX require HT? */
674178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
675178354Ssam			m = ieee80211_decap_amsdu(ni, m);
676178354Ssam			if (m == NULL)
677178354Ssam				return IEEE80211_FC0_TYPE_DATA;
678190391Ssam		} else {
679190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
680190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
681190391Ssam			if (m == NULL)
682178354Ssam				return IEEE80211_FC0_TYPE_DATA;
683190391Ssam#endif
684178354Ssam		}
685178354Ssam		ieee80211_deliver_data(vap, ni, m);
686178354Ssam		return IEEE80211_FC0_TYPE_DATA;
687178354Ssam
688178354Ssam	case IEEE80211_FC0_TYPE_MGT:
689178354Ssam		vap->iv_stats.is_rx_mgmt++;
690178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
691178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
692178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
693178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
694178354Ssam			vap->iv_stats.is_rx_wrongdir++;
695178354Ssam			goto err;
696178354Ssam		}
697178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
698178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
699178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
700178354Ssam			    m->m_pkthdr.len);
701178354Ssam			vap->iv_stats.is_rx_tooshort++;
702178354Ssam			goto out;
703178354Ssam		}
704178354Ssam#ifdef IEEE80211_DEBUG
705178354Ssam		if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
706178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
707178354Ssam			    ieee80211_mgt_subtype_name[subtype >>
708178354Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
709178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
710178354Ssam		}
711178354Ssam#endif
712178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
713178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
714178354Ssam			    wh, NULL, "%s", "WEP set but not permitted");
715178354Ssam			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
716178354Ssam			goto out;
717178354Ssam		}
718192468Ssam		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
719192468Ssam		goto out;
720178354Ssam
721178354Ssam	case IEEE80211_FC0_TYPE_CTL:
722178354Ssam		vap->iv_stats.is_rx_ctl++;
723178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
724178354Ssam		goto out;
725192468Ssam
726178354Ssam	default:
727178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
728178354Ssam		    wh, "bad", "frame type 0x%x", type);
729178354Ssam		/* should not come here */
730178354Ssam		break;
731178354Ssam	}
732178354Ssamerr:
733178354Ssam	ifp->if_ierrors++;
734178354Ssamout:
735178354Ssam	if (m != NULL) {
736192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
737192468Ssam			ieee80211_radiotap_rx(vap, m);
738178354Ssam		m_freem(m);
739178354Ssam	}
740178354Ssam	return type;
741178354Ssam}
742178354Ssam
743178354Ssamstatic void
744178354Ssamwds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
745192468Ssam	int subtype, int rssi, int nf)
746178354Ssam{
747178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
748178354Ssam	struct ieee80211com *ic = ni->ni_ic;
749178354Ssam	struct ieee80211_frame *wh;
750178354Ssam	u_int8_t *frm, *efrm;
751178354Ssam
752178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
753178354Ssam	frm = (u_int8_t *)&wh[1];
754178354Ssam	efrm = mtod(m0, u_int8_t *) + m0->m_len;
755178354Ssam	switch (subtype) {
756218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION:
757218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
758218958Sbschmidt		if (ni == vap->iv_bss) {
759218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
760218958Sbschmidt			    wh, NULL, "%s", "unknown node");
761218927Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
762218958Sbschmidt		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1)) {
763218958Sbschmidt			/* NB: not interested in multicast frames. */
764218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
765218958Sbschmidt			    wh, NULL, "%s", "not for us");
766218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
767218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
768218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
769218927Sbschmidt			    wh, NULL, "wrong state %s",
770218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
771218927Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
772218958Sbschmidt		} else {
773218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
774218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
775218927Sbschmidt		}
776218927Sbschmidt		break;
777218927Sbschmidt
778178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
779218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
780178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
781178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
782218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
783218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
784218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_BEACON:
785218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
786178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC:
787218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_AUTH:
788218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_DEAUTH:
789218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
790218927Sbschmidt		    wh, NULL, "%s", "not handled");
791178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
792178354Ssam		break;
793218927Sbschmidt
794178354Ssam	default:
795178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
796218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
797178354Ssam		vap->iv_stats.is_rx_badsubtype++;
798178354Ssam		break;
799178354Ssam	}
800178354Ssam}
801