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