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