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