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: stable/11/sys/net80211/ieee80211_wds.c 344969 2019-03-09 12:54:10Z avos $");
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>
51257176Sglebius#include <net/if_var.h>
52178354Ssam#include <net/if_media.h>
53178354Ssam#include <net/if_llc.h>
54178354Ssam#include <net/ethernet.h>
55178354Ssam
56178354Ssam#include <net/bpf.h>
57178354Ssam
58178354Ssam#include <net80211/ieee80211_var.h>
59178354Ssam#include <net80211/ieee80211_wds.h>
60178354Ssam#include <net80211/ieee80211_input.h>
61190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
62190391Ssam#include <net80211/ieee80211_superg.h>
63190391Ssam#endif
64178354Ssam
65178354Ssamstatic void wds_vattach(struct ieee80211vap *);
66178354Ssamstatic int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
67283535Sadrianstatic	int wds_input(struct ieee80211_node *ni, struct mbuf *m,
68283535Sadrian	    const struct ieee80211_rx_stats *rxs, int, int);
69283535Sadrianstatic void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *, int subtype,
70283535Sadrian	const struct ieee80211_rx_stats *, int, int);
71178354Ssam
72178354Ssamvoid
73178354Ssamieee80211_wds_attach(struct ieee80211com *ic)
74178354Ssam{
75178354Ssam	ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach;
76178354Ssam}
77178354Ssam
78178354Ssamvoid
79178354Ssamieee80211_wds_detach(struct ieee80211com *ic)
80178354Ssam{
81178354Ssam}
82178354Ssam
83178354Ssamstatic void
84178354Ssamwds_vdetach(struct ieee80211vap *vap)
85178354Ssam{
86178354Ssam	if (vap->iv_bss != NULL) {
87178354Ssam		/* XXX locking? */
88178354Ssam		if (vap->iv_bss->ni_wdsvap == vap)
89178354Ssam			vap->iv_bss->ni_wdsvap = NULL;
90178354Ssam	}
91178354Ssam}
92178354Ssam
93178354Ssamstatic void
94178354Ssamwds_vattach(struct ieee80211vap *vap)
95178354Ssam{
96178354Ssam	vap->iv_newstate = wds_newstate;
97178354Ssam	vap->iv_input = wds_input;
98178354Ssam	vap->iv_recv_mgmt = wds_recv_mgmt;
99178354Ssam	vap->iv_opdetach = wds_vdetach;
100178354Ssam}
101178354Ssam
102195379Ssamstatic void
103195379Ssamwds_flush(struct ieee80211_node *ni)
104195379Ssam{
105195379Ssam	struct ieee80211com *ic = ni->ni_ic;
106195379Ssam	struct mbuf *m, *next;
107195379Ssam	int8_t rssi, nf;
108195379Ssam
109195379Ssam	m = ieee80211_ageq_remove(&ic->ic_stageq,
110195379Ssam	    (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
111195379Ssam	if (m == NULL)
112195379Ssam		return;
113195379Ssam
114195379Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_WDS, ni,
115195379Ssam	    "%s", "flush wds queue");
116195379Ssam	ic->ic_node_getsignal(ni, &rssi, &nf);
117195379Ssam	for (; m != NULL; m = next) {
118195379Ssam		next = m->m_nextpkt;
119195379Ssam		m->m_nextpkt = NULL;
120195379Ssam		ieee80211_input(ni, m, rssi, nf);
121195379Ssam	}
122195379Ssam}
123195379Ssam
124178354Ssamstatic int
125178354Ssamieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan)
126178354Ssam{
127178354Ssam	struct ieee80211com *ic = vap->iv_ic;
128178354Ssam	struct ieee80211_node_table *nt = &ic->ic_sta;
129178354Ssam	struct ieee80211_node *ni, *obss;
130178354Ssam
131178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
132178354Ssam	     "%s: creating link to %s on channel %u\n", __func__,
133178354Ssam	     ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan));
134178354Ssam
135178354Ssam	/* NB: vap create must specify the bssid for the link */
136178354Ssam	KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid"));
137178354Ssam	/* NB: we should only be called on RUN transition */
138178354Ssam	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state"));
139178354Ssam
140178354Ssam	if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
141178354Ssam		/*
142178354Ssam		 * Dynamic/non-legacy WDS.  Reference the associated
143178354Ssam		 * station specified by the desired bssid setup at vap
144178354Ssam		 * create.  Point ni_wdsvap at the WDS vap so 4-address
145178354Ssam		 * frames received through the associated AP vap will
146178354Ssam		 * be dispatched upward (e.g. to a bridge) as though
147178354Ssam		 * they arrived on the WDS vap.
148178354Ssam		 */
149178354Ssam		IEEE80211_NODE_LOCK(nt);
150178354Ssam		obss = NULL;
151178354Ssam		ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid);
152178354Ssam		if (ni == NULL) {
153178354Ssam			/*
154178354Ssam			 * Node went away before we could hookup.  This
155178354Ssam			 * should be ok; no traffic will flow and a leave
156178354Ssam			 * event will be dispatched that should cause
157178354Ssam			 * the vap to be destroyed.
158178354Ssam			 */
159178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
160178354Ssam			    "%s: station %s went away\n",
161178354Ssam			    __func__, ether_sprintf(vap->iv_des_bssid));
162178354Ssam			/* XXX stat? */
163178354Ssam		} else if (ni->ni_wdsvap != NULL) {
164178354Ssam			/*
165178354Ssam			 * Node already setup with a WDS vap; we cannot
166178354Ssam			 * allow multiple references so disallow.  If
167178354Ssam			 * ni_wdsvap points at us that's ok; we should
168178354Ssam			 * do nothing anyway.
169178354Ssam			 */
170178354Ssam			/* XXX printf instead? */
171178354Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
172178354Ssam			    "%s: station %s in use with %s\n",
173178354Ssam			    __func__, ether_sprintf(vap->iv_des_bssid),
174178354Ssam			    ni->ni_wdsvap->iv_ifp->if_xname);
175178354Ssam			/* XXX stat? */
176178354Ssam		} else {
177178354Ssam			/*
178178354Ssam			 * Committed to new node, setup state.
179178354Ssam			 */
180178354Ssam			obss = vap->iv_bss;
181178354Ssam			vap->iv_bss = ni;
182178354Ssam			ni->ni_wdsvap = vap;
183178354Ssam		}
184178354Ssam		IEEE80211_NODE_UNLOCK(nt);
185178354Ssam		if (obss != NULL) {
186178354Ssam			/* NB: deferred to avoid recursive lock */
187178354Ssam			ieee80211_free_node(obss);
188178354Ssam		}
189178354Ssam	} else {
190178354Ssam		/*
191178354Ssam		 * Legacy WDS vap setup.
192178354Ssam		 */
193178354Ssam		/*
194178354Ssam		 * The far end does not associate so we just create
195178354Ssam		 * create a new node and install it as the vap's
196178354Ssam		 * bss node.  We must simulate an association and
197178354Ssam		 * authorize the port for traffic to flow.
198178354Ssam		 * XXX check if node already in sta table?
199178354Ssam		 */
200178354Ssam		ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
201178354Ssam		if (ni != NULL) {
202178354Ssam			obss = vap->iv_bss;
203178354Ssam			vap->iv_bss = ieee80211_ref_node(ni);
204178354Ssam			ni->ni_flags |= IEEE80211_NODE_AREF;
205178354Ssam			if (obss != NULL)
206178354Ssam				ieee80211_free_node(obss);
207178354Ssam			/* give driver a chance to setup state like ni_txrate */
208178354Ssam			if (ic->ic_newassoc != NULL)
209178354Ssam				ic->ic_newassoc(ni, 1);
210178354Ssam			/* tell the authenticator about new station */
211178354Ssam			if (vap->iv_auth->ia_node_join != NULL)
212178354Ssam				vap->iv_auth->ia_node_join(ni);
213178354Ssam			if (ni->ni_authmode != IEEE80211_AUTH_8021X)
214178354Ssam				ieee80211_node_authorize(ni);
215178354Ssam
216178354Ssam			ieee80211_notify_node_join(ni, 1 /*newassoc*/);
217178354Ssam			/* XXX inject l2uf frame */
218178354Ssam		}
219178354Ssam	}
220178354Ssam
221178354Ssam	/*
222195379Ssam	 * Flush any pending frames now that were setup.
223178354Ssam	 */
224195379Ssam	if (ni != NULL)
225195379Ssam		wds_flush(ni);
226178354Ssam	return (ni == NULL ? ENOENT : 0);
227178354Ssam}
228178354Ssam
229178354Ssam/*
230178354Ssam * Propagate multicast frames of an ap vap to all DWDS links.
231178354Ssam * The caller is assumed to have verified this frame is multicast.
232178354Ssam */
233178354Ssamvoid
234178354Ssamieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m)
235178354Ssam{
236178354Ssam	struct ieee80211com *ic = vap0->iv_ic;
237178354Ssam	const struct ether_header *eh = mtod(m, const struct ether_header *);
238178354Ssam	struct ieee80211_node *ni;
239178354Ssam	struct ieee80211vap *vap;
240178354Ssam	struct ifnet *ifp;
241178354Ssam	struct mbuf *mcopy;
242178354Ssam	int err;
243178354Ssam
244178354Ssam	KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost),
245178354Ssam	    ("%s not mcast", ether_sprintf(eh->ether_dhost)));
246178354Ssam
247178354Ssam	/* XXX locking */
248178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
249178354Ssam		/* only DWDS vaps are interesting */
250178354Ssam		if (vap->iv_opmode != IEEE80211_M_WDS ||
251178354Ssam		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))
252178354Ssam			continue;
253178354Ssam		/* if it came in this interface, don't send it back out */
254178354Ssam		ifp = vap->iv_ifp;
255178354Ssam		if (ifp == m->m_pkthdr.rcvif)
256178354Ssam			continue;
257178354Ssam		/*
258190672Ssam		 * Duplicate the frame and send it.
259178354Ssam		 */
260243882Sglebius		mcopy = m_copypacket(m, M_NOWAIT);
261178354Ssam		if (mcopy == NULL) {
262271861Sglebius			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
263178354Ssam			/* XXX stat + msg */
264178354Ssam			continue;
265178354Ssam		}
266178354Ssam		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
267178354Ssam		if (ni == NULL) {
268178354Ssam			/* NB: ieee80211_find_txnode does stat+msg */
269271861Sglebius			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
270178354Ssam			m_freem(mcopy);
271178354Ssam			continue;
272178354Ssam		}
273190672Ssam		/* calculate priority so drivers can find the tx queue */
274178354Ssam		if (ieee80211_classify(ni, mcopy)) {
275178354Ssam			IEEE80211_DISCARD_MAC(vap,
276178354Ssam			    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS,
277178354Ssam			    eh->ether_dhost, NULL,
278178354Ssam			    "%s", "classification failure");
279178354Ssam			vap->iv_stats.is_tx_classify++;
280271861Sglebius			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
281178354Ssam			m_freem(mcopy);
282178354Ssam			ieee80211_free_node(ni);
283178354Ssam			continue;
284178354Ssam		}
285191542Ssam
286191542Ssam		BPF_MTAP(ifp, m);		/* 802.3 tx */
287191542Ssam
288190672Ssam		/*
289190672Ssam		 * Encapsulate the packet in prep for transmission.
290190672Ssam		 */
291297287Savos		IEEE80211_TX_LOCK(ic);
292190672Ssam		mcopy = ieee80211_encap(vap, ni, mcopy);
293194461Srpaulo		if (mcopy == NULL) {
294190672Ssam			/* NB: stat+msg handled in ieee80211_encap */
295297287Savos			IEEE80211_TX_UNLOCK(ic);
296190672Ssam			ieee80211_free_node(ni);
297190672Ssam			continue;
298190672Ssam		}
299190672Ssam		mcopy->m_flags |= M_MCAST;
300178354Ssam		mcopy->m_pkthdr.rcvif = (void *) ni;
301178354Ssam
302254082Sadrian		err = ieee80211_parent_xmitpkt(ic, mcopy);
303297287Savos		IEEE80211_TX_UNLOCK(ic);
304289164Sadrian		if (!err) {
305271861Sglebius			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
306283313Sae			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
307283313Sae			if_inc_counter(ifp, IFCOUNTER_OBYTES,
308283313Sae			    m->m_pkthdr.len);
309283313Sae		}
310178354Ssam	}
311178354Ssam}
312178354Ssam
313178354Ssam/*
314178354Ssam * Handle DWDS discovery on receipt of a 4-address frame in
315178354Ssam * ap mode.  Queue the frame and post an event for someone
316178354Ssam * to plumb the necessary WDS vap for this station.  Frames
317178354Ssam * received prior to the vap set running will then be reprocessed
318178354Ssam * as if they were just received.
319178354Ssam */
320178354Ssamvoid
321178354Ssamieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
322178354Ssam{
323178354Ssam	struct ieee80211com *ic = ni->ni_ic;
324178354Ssam
325195379Ssam	/*
326195379Ssam	 * Save the frame with an aging interval 4 times
327195379Ssam	 * the listen interval specified by the station.
328195379Ssam	 * Frames that sit around too long are reclaimed
329195379Ssam	 * using this information.
330195379Ssam	 * XXX handle overflow?
331195379Ssam	 * XXX per/vap beacon interval?
332195379Ssam	 */
333195379Ssam	m->m_pkthdr.rcvif = (void *)(uintptr_t)
334195379Ssam	    ieee80211_mac_hash(ic, ni->ni_macaddr);
335195379Ssam	(void) ieee80211_ageq_append(&ic->ic_stageq, m,
336195379Ssam	    ((ni->ni_intval * ic->ic_lintval) << 2) / 1024);
337178354Ssam	ieee80211_notify_wds_discover(ni);
338178354Ssam}
339178354Ssam
340178354Ssam/*
341178354Ssam * IEEE80211_M_WDS vap state machine handler.
342178354Ssam */
343178354Ssamstatic int
344178354Ssamwds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
345178354Ssam{
346178354Ssam	struct ieee80211com *ic = vap->iv_ic;
347178354Ssam	enum ieee80211_state ostate;
348178354Ssam	int error;
349178354Ssam
350178354Ssam	IEEE80211_LOCK_ASSERT(ic);
351178354Ssam
352178354Ssam	ostate = vap->iv_state;
353178354Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
354178354Ssam		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
355178354Ssam	vap->iv_state = nstate;			/* state transition */
356178354Ssam	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
357178354Ssam	if (ostate != IEEE80211_S_SCAN)
358178354Ssam		ieee80211_cancel_scan(vap);	/* background scan */
359178354Ssam	error = 0;
360178354Ssam	switch (nstate) {
361178354Ssam	case IEEE80211_S_INIT:
362178354Ssam		switch (ostate) {
363178354Ssam		case IEEE80211_S_SCAN:
364178354Ssam			ieee80211_cancel_scan(vap);
365178354Ssam			break;
366178354Ssam		default:
367178354Ssam			break;
368178354Ssam		}
369178354Ssam		if (ostate != IEEE80211_S_INIT) {
370178354Ssam			/* NB: optimize INIT -> INIT case */
371178354Ssam			ieee80211_reset_bss(vap);
372178354Ssam		}
373178354Ssam		break;
374178354Ssam	case IEEE80211_S_SCAN:
375178354Ssam		switch (ostate) {
376178354Ssam		case IEEE80211_S_INIT:
377178354Ssam			ieee80211_check_scan_current(vap);
378178354Ssam			break;
379178354Ssam		default:
380178354Ssam			break;
381178354Ssam		}
382178354Ssam		break;
383178354Ssam	case IEEE80211_S_RUN:
384178354Ssam		if (ostate == IEEE80211_S_INIT) {
385178354Ssam			/*
386178354Ssam			 * Already have a channel; bypass the scan
387178354Ssam			 * and startup immediately.
388178354Ssam			 */
389178354Ssam			error = ieee80211_create_wds(vap, ic->ic_curchan);
390178354Ssam		}
391178354Ssam		break;
392178354Ssam	default:
393178354Ssam		break;
394178354Ssam	}
395178354Ssam	return error;
396178354Ssam}
397178354Ssam
398178354Ssam/*
399178354Ssam * Process a received frame.  The node associated with the sender
400178354Ssam * should be supplied.  If nothing was found in the node table then
401178354Ssam * the caller is assumed to supply a reference to iv_bss instead.
402178354Ssam * The RSSI and a timestamp are also supplied.  The RSSI data is used
403178354Ssam * during AP scanning to select a AP to associate with; it can have
404178354Ssam * any units so long as values have consistent units and higher values
405178354Ssam * mean ``better signal''.  The receive timestamp is currently not used
406178354Ssam * by the 802.11 layer.
407178354Ssam */
408178354Ssamstatic int
409283535Sadrianwds_input(struct ieee80211_node *ni, struct mbuf *m,
410283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
411178354Ssam{
412178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
413178354Ssam	struct ieee80211com *ic = ni->ni_ic;
414178354Ssam	struct ifnet *ifp = vap->iv_ifp;
415178354Ssam	struct ieee80211_frame *wh;
416178354Ssam	struct ieee80211_key *key;
417178354Ssam	struct ether_header *eh;
418203422Srpaulo	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
419178354Ssam	uint8_t dir, type, subtype, qos;
420178354Ssam
421183247Ssam	if (m->m_flags & M_AMPDU_MPDU) {
422178354Ssam		/*
423178354Ssam		 * Fastpath for A-MPDU reorder q resubmission.  Frames
424183247Ssam		 * w/ M_AMPDU_MPDU marked have already passed through
425183247Ssam		 * here but were received out of order and been held on
426183247Ssam		 * the reorder queue.  When resubmitted they are marked
427183247Ssam		 * with the M_AMPDU_MPDU flag and we can bypass most of
428183247Ssam		 * the normal processing.
429178354Ssam		 */
430178354Ssam		wh = mtod(m, struct ieee80211_frame *);
431178354Ssam		type = IEEE80211_FC0_TYPE_DATA;
432178354Ssam		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
433178354Ssam		subtype = IEEE80211_FC0_SUBTYPE_QOS;
434178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
435178354Ssam		goto resubmit_ampdu;
436178354Ssam	}
437178354Ssam
438178354Ssam	KASSERT(ni != NULL, ("null node"));
439178354Ssam
440178354Ssam	type = -1;			/* undefined */
441178354Ssam
442178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
443178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
444178354Ssam		    ni->ni_macaddr, NULL,
445178354Ssam		    "too short (1): len %u", m->m_pkthdr.len);
446178354Ssam		vap->iv_stats.is_rx_tooshort++;
447178354Ssam		goto out;
448178354Ssam	}
449178354Ssam	/*
450178354Ssam	 * Bit of a cheat here, we use a pointer for a 3-address
451178354Ssam	 * frame format but don't reference fields past outside
452178354Ssam	 * ieee80211_frame_min w/o first validating the data is
453178354Ssam	 * present.
454178354Ssam	 */
455178354Ssam	wh = mtod(m, struct ieee80211_frame *);
456178354Ssam
457218958Sbschmidt	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
458218958Sbschmidt		ni->ni_inact = ni->ni_inact_reload;
459218958Sbschmidt
460178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
461178354Ssam	    IEEE80211_FC0_VERSION_0) {
462178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
463191547Ssam		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
464191547Ssam		    wh->i_fc[0], wh->i_fc[1]);
465178354Ssam		vap->iv_stats.is_rx_badversion++;
466178354Ssam		goto err;
467178354Ssam	}
468178354Ssam
469178354Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
470178354Ssam	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
471178354Ssam	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
472178354Ssam
473178354Ssam	/* NB: WDS vap's do not scan */
474178354Ssam	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
475178354Ssam		IEEE80211_DISCARD_MAC(vap,
476178354Ssam		    IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
477178354Ssam		    "too short (3): len %u", m->m_pkthdr.len);
478178354Ssam		vap->iv_stats.is_rx_tooshort++;
479178354Ssam		goto out;
480178354Ssam	}
481178354Ssam	/* NB: the TA is implicitly verified by finding the wds peer node */
482178354Ssam	if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
483178354Ssam	    !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
484178354Ssam		/* not interested in */
485178354Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
486178354Ssam		    wh->i_addr1, NULL, "%s", "not to bss");
487178354Ssam		vap->iv_stats.is_rx_wrongbss++;
488178354Ssam		goto out;
489178354Ssam	}
490178354Ssam	IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
491192468Ssam	ni->ni_noise = nf;
492282820Sadrian	if (IEEE80211_HAS_SEQ(type, subtype)) {
493178354Ssam		uint8_t tid = ieee80211_gettid(wh);
494178354Ssam		if (IEEE80211_QOS_HAS_SEQ(wh) &&
495178354Ssam		    TID_TO_WME_AC(tid) >= WME_AC_VI)
496178354Ssam			ic->ic_wme.wme_hipri_traffic++;
497296254Savos		if (! ieee80211_check_rxseq(ni, wh, wh->i_addr1))
498178354Ssam			goto out;
499178354Ssam	}
500178354Ssam	switch (type) {
501178354Ssam	case IEEE80211_FC0_TYPE_DATA:
502178354Ssam		hdrspace = ieee80211_hdrspace(ic, wh);
503178354Ssam		if (m->m_len < hdrspace &&
504178354Ssam		    (m = m_pullup(m, hdrspace)) == NULL) {
505178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
506178354Ssam			    ni->ni_macaddr, NULL,
507178354Ssam			    "data too short: expecting %u", hdrspace);
508178354Ssam			vap->iv_stats.is_rx_tooshort++;
509178354Ssam			goto out;		/* XXX */
510178354Ssam		}
511178354Ssam		if (dir != IEEE80211_FC1_DIR_DSTODS) {
512178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
513178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
514178354Ssam			vap->iv_stats.is_rx_wrongdir++;
515178354Ssam			goto out;
516178354Ssam		}
517178354Ssam		/*
518178354Ssam		 * Only legacy WDS traffic should take this path.
519178354Ssam		 */
520178354Ssam		if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
521178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
522178354Ssam			    wh, "data", "%s", "not legacy wds");
523178354Ssam			vap->iv_stats.is_rx_wrongdir++;/*XXX*/
524178354Ssam			goto out;
525178354Ssam		}
526178354Ssam		/*
527183247Ssam		 * Handle A-MPDU re-ordering.  If the frame is to be
528183247Ssam		 * processed directly then ieee80211_ampdu_reorder
529178354Ssam		 * will return 0; otherwise it has consumed the mbuf
530178354Ssam		 * and we should do nothing more with it.
531178354Ssam		 */
532183247Ssam		if ((m->m_flags & M_AMPDU) &&
533178354Ssam		    ieee80211_ampdu_reorder(ni, m) != 0) {
534178354Ssam			m = NULL;
535178354Ssam			goto out;
536178354Ssam		}
537178354Ssam	resubmit_ampdu:
538178354Ssam
539178354Ssam		/*
540178354Ssam		 * Handle privacy requirements.  Note that we
541178354Ssam		 * must not be preempted from here until after
542178354Ssam		 * we (potentially) call ieee80211_crypto_demic;
543178354Ssam		 * otherwise we may violate assumptions in the
544178354Ssam		 * crypto cipher modules used to do delayed update
545178354Ssam		 * of replay sequence numbers.
546178354Ssam		 */
547260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
548178354Ssam			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
549178354Ssam				/*
550178354Ssam				 * Discard encrypted frames when privacy is off.
551178354Ssam				 */
552178354Ssam				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
553178354Ssam				    wh, "WEP", "%s", "PRIVACY off");
554178354Ssam				vap->iv_stats.is_rx_noprivacy++;
555178354Ssam				IEEE80211_NODE_STAT(ni, rx_noprivacy);
556178354Ssam				goto out;
557178354Ssam			}
558178354Ssam			key = ieee80211_crypto_decap(ni, m, hdrspace);
559178354Ssam			if (key == NULL) {
560178354Ssam				/* NB: stats+msgs handled in crypto_decap */
561178354Ssam				IEEE80211_NODE_STAT(ni, rx_wepfail);
562178354Ssam				goto out;
563178354Ssam			}
564178354Ssam			wh = mtod(m, struct ieee80211_frame *);
565260444Skevlo			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
566178354Ssam		} else {
567178354Ssam			/* XXX M_WEP and IEEE80211_F_PRIVACY */
568178354Ssam			key = NULL;
569178354Ssam		}
570178354Ssam
571178354Ssam		/*
572178354Ssam		 * Save QoS bits for use below--before we strip the header.
573178354Ssam		 */
574344969Savos		if (subtype == IEEE80211_FC0_SUBTYPE_QOS)
575344969Savos			qos = ieee80211_getqos(wh)[0];
576344969Savos		else
577178354Ssam			qos = 0;
578178354Ssam
579178354Ssam		/*
580178354Ssam		 * Next up, any fragmentation.
581178354Ssam		 */
582178354Ssam		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
583178354Ssam			m = ieee80211_defrag(ni, m, hdrspace);
584178354Ssam			if (m == NULL) {
585178354Ssam				/* Fragment dropped or frame not complete yet */
586178354Ssam				goto out;
587178354Ssam			}
588178354Ssam		}
589178354Ssam		wh = NULL;		/* no longer valid, catch any uses */
590178354Ssam
591178354Ssam		/*
592178354Ssam		 * Next strip any MSDU crypto bits.
593178354Ssam		 */
594178354Ssam		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
595178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
596178354Ssam			    ni->ni_macaddr, "data", "%s", "demic error");
597178354Ssam			vap->iv_stats.is_rx_demicfail++;
598178354Ssam			IEEE80211_NODE_STAT(ni, rx_demicfail);
599178354Ssam			goto out;
600178354Ssam		}
601178354Ssam
602178354Ssam		/* copy to listener after decrypt */
603192468Ssam		if (ieee80211_radiotap_active_vap(vap))
604192468Ssam			ieee80211_radiotap_rx(vap, m);
605178354Ssam		need_tap = 0;
606178354Ssam
607178354Ssam		/*
608178354Ssam		 * Finally, strip the 802.11 header.
609178354Ssam		 */
610178354Ssam		m = ieee80211_decap(vap, m, hdrspace);
611178354Ssam		if (m == NULL) {
612178354Ssam			/* XXX mask bit to check for both */
613178354Ssam			/* don't count Null data frames as errors */
614178354Ssam			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
615178354Ssam			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
616178354Ssam				goto out;
617178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
618178354Ssam			    ni->ni_macaddr, "data", "%s", "decap error");
619178354Ssam			vap->iv_stats.is_rx_decap++;
620178354Ssam			IEEE80211_NODE_STAT(ni, rx_decap);
621178354Ssam			goto err;
622178354Ssam		}
623178354Ssam		eh = mtod(m, struct ether_header *);
624178354Ssam		if (!ieee80211_node_is_authorized(ni)) {
625178354Ssam			/*
626178354Ssam			 * Deny any non-PAE frames received prior to
627178354Ssam			 * authorization.  For open/shared-key
628178354Ssam			 * authentication the port is mark authorized
629178354Ssam			 * after authentication completes.  For 802.1x
630178354Ssam			 * the port is not marked authorized by the
631178354Ssam			 * authenticator until the handshake has completed.
632178354Ssam			 */
633178354Ssam			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
634178354Ssam				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
635178354Ssam				    eh->ether_shost, "data",
636178354Ssam				    "unauthorized port: ether type 0x%x len %u",
637178354Ssam				    eh->ether_type, m->m_pkthdr.len);
638178354Ssam				vap->iv_stats.is_rx_unauth++;
639178354Ssam				IEEE80211_NODE_STAT(ni, rx_unauth);
640178354Ssam				goto err;
641178354Ssam			}
642178354Ssam		} else {
643178354Ssam			/*
644178354Ssam			 * When denying unencrypted frames, discard
645178354Ssam			 * any non-PAE frames received without encryption.
646178354Ssam			 */
647178354Ssam			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
648178354Ssam			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
649178354Ssam			    eh->ether_type != htons(ETHERTYPE_PAE)) {
650178354Ssam				/*
651178354Ssam				 * Drop unencrypted frames.
652178354Ssam				 */
653178354Ssam				vap->iv_stats.is_rx_unencrypted++;
654178354Ssam				IEEE80211_NODE_STAT(ni, rx_unencrypted);
655178354Ssam				goto out;
656178354Ssam			}
657178354Ssam		}
658178354Ssam		/* XXX require HT? */
659178354Ssam		if (qos & IEEE80211_QOS_AMSDU) {
660178354Ssam			m = ieee80211_decap_amsdu(ni, m);
661178354Ssam			if (m == NULL)
662178354Ssam				return IEEE80211_FC0_TYPE_DATA;
663190391Ssam		} else {
664190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG
665190391Ssam			m = ieee80211_decap_fastframe(vap, ni, m);
666190391Ssam			if (m == NULL)
667178354Ssam				return IEEE80211_FC0_TYPE_DATA;
668190391Ssam#endif
669178354Ssam		}
670178354Ssam		ieee80211_deliver_data(vap, ni, m);
671178354Ssam		return IEEE80211_FC0_TYPE_DATA;
672178354Ssam
673178354Ssam	case IEEE80211_FC0_TYPE_MGT:
674178354Ssam		vap->iv_stats.is_rx_mgmt++;
675178354Ssam		IEEE80211_NODE_STAT(ni, rx_mgmt);
676178354Ssam		if (dir != IEEE80211_FC1_DIR_NODS) {
677178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
678178354Ssam			    wh, "data", "incorrect dir 0x%x", dir);
679178354Ssam			vap->iv_stats.is_rx_wrongdir++;
680178354Ssam			goto err;
681178354Ssam		}
682178354Ssam		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
683178354Ssam			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
684178354Ssam			    ni->ni_macaddr, "mgt", "too short: len %u",
685178354Ssam			    m->m_pkthdr.len);
686178354Ssam			vap->iv_stats.is_rx_tooshort++;
687178354Ssam			goto out;
688178354Ssam		}
689178354Ssam#ifdef IEEE80211_DEBUG
690178354Ssam		if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
691178354Ssam			if_printf(ifp, "received %s from %s rssi %d\n",
692298376Savos			    ieee80211_mgt_subtype_name(subtype),
693178354Ssam			    ether_sprintf(wh->i_addr2), rssi);
694178354Ssam		}
695178354Ssam#endif
696260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
697178354Ssam			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
698178354Ssam			    wh, NULL, "%s", "WEP set but not permitted");
699178354Ssam			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
700178354Ssam			goto out;
701178354Ssam		}
702283535Sadrian		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
703192468Ssam		goto out;
704178354Ssam
705178354Ssam	case IEEE80211_FC0_TYPE_CTL:
706178354Ssam		vap->iv_stats.is_rx_ctl++;
707178354Ssam		IEEE80211_NODE_STAT(ni, rx_ctrl);
708178354Ssam		goto out;
709192468Ssam
710178354Ssam	default:
711178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
712178354Ssam		    wh, "bad", "frame type 0x%x", type);
713178354Ssam		/* should not come here */
714178354Ssam		break;
715178354Ssam	}
716178354Ssamerr:
717271861Sglebius	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
718178354Ssamout:
719178354Ssam	if (m != NULL) {
720192765Ssam		if (need_tap && ieee80211_radiotap_active_vap(vap))
721192468Ssam			ieee80211_radiotap_rx(vap, m);
722178354Ssam		m_freem(m);
723178354Ssam	}
724178354Ssam	return type;
725178354Ssam}
726178354Ssam
727178354Ssamstatic void
728283535Sadrianwds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
729283535Sadrian    const struct ieee80211_rx_stats *rxs, int rssi, int nf)
730178354Ssam{
731178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
732178354Ssam	struct ieee80211com *ic = ni->ni_ic;
733178354Ssam	struct ieee80211_frame *wh;
734178354Ssam	u_int8_t *frm, *efrm;
735178354Ssam
736178354Ssam	wh = mtod(m0, struct ieee80211_frame *);
737178354Ssam	frm = (u_int8_t *)&wh[1];
738178354Ssam	efrm = mtod(m0, u_int8_t *) + m0->m_len;
739178354Ssam	switch (subtype) {
740218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION:
741218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
742218958Sbschmidt		if (ni == vap->iv_bss) {
743218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
744218958Sbschmidt			    wh, NULL, "%s", "unknown node");
745218927Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
746218958Sbschmidt		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1)) {
747218958Sbschmidt			/* NB: not interested in multicast frames. */
748218927Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
749218958Sbschmidt			    wh, NULL, "%s", "not for us");
750218958Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
751218958Sbschmidt		} else if (vap->iv_state != IEEE80211_S_RUN) {
752218958Sbschmidt			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
753218927Sbschmidt			    wh, NULL, "wrong state %s",
754218927Sbschmidt			    ieee80211_state_name[vap->iv_state]);
755218927Sbschmidt			vap->iv_stats.is_rx_mgtdiscard++;
756218958Sbschmidt		} else {
757218958Sbschmidt			if (ieee80211_parse_action(ni, m0) == 0)
758218958Sbschmidt				(void)ic->ic_recv_action(ni, wh, frm, efrm);
759218927Sbschmidt		}
760218927Sbschmidt		break;
761218927Sbschmidt
762178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
763218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
764178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
765178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
766218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
767218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
768295795Savos	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
769218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_BEACON:
770218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_ATIM:
771178354Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC:
772218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_AUTH:
773218927Sbschmidt	case IEEE80211_FC0_SUBTYPE_DEAUTH:
774218927Sbschmidt		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
775218927Sbschmidt		    wh, NULL, "%s", "not handled");
776178354Ssam		vap->iv_stats.is_rx_mgtdiscard++;
777178354Ssam		break;
778218927Sbschmidt
779178354Ssam	default:
780178354Ssam		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
781218927Sbschmidt		    wh, "mgt", "subtype 0x%x not handled", subtype);
782178354Ssam		vap->iv_stats.is_rx_badsubtype++;
783178354Ssam		break;
784178354Ssam	}
785178354Ssam}
786