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