xref: /freebsd/sys/net80211/ieee80211_wds.c (revision 4fd0d10e0fe684211328bc148edf89a792425b39)
1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11 WDS mode support.
33  */
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/if_media.h>
52 #include <net/if_llc.h>
53 #include <net/ethernet.h>
54 
55 #include <net/bpf.h>
56 
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_wds.h>
59 #include <net80211/ieee80211_input.h>
60 #ifdef IEEE80211_SUPPORT_SUPERG
61 #include <net80211/ieee80211_superg.h>
62 #endif
63 
64 static void wds_vattach(struct ieee80211vap *);
65 static int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66 static	int wds_input(struct ieee80211_node *ni, struct mbuf *m, int, int);
67 static void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *,
68 	    int subtype, 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_bss;
179 			vap->iv_bss = ni;
180 			ni->ni_wdsvap = vap;
181 		}
182 		IEEE80211_NODE_UNLOCK(nt);
183 		if (obss != NULL) {
184 			/* NB: deferred to avoid recursive lock */
185 			ieee80211_free_node(obss);
186 		}
187 	} else {
188 		/*
189 		 * Legacy WDS vap setup.
190 		 */
191 		/*
192 		 * The far end does not associate so we just create
193 		 * create a new node and install it as the vap's
194 		 * bss node.  We must simulate an association and
195 		 * authorize the port for traffic to flow.
196 		 * XXX check if node already in sta table?
197 		 */
198 		ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
199 		if (ni != NULL) {
200 			obss = vap->iv_bss;
201 			vap->iv_bss = ieee80211_ref_node(ni);
202 			ni->ni_flags |= IEEE80211_NODE_AREF;
203 			if (obss != NULL)
204 				ieee80211_free_node(obss);
205 			/* give driver a chance to setup state like ni_txrate */
206 			if (ic->ic_newassoc != NULL)
207 				ic->ic_newassoc(ni, 1);
208 			/* tell the authenticator about new station */
209 			if (vap->iv_auth->ia_node_join != NULL)
210 				vap->iv_auth->ia_node_join(ni);
211 			if (ni->ni_authmode != IEEE80211_AUTH_8021X)
212 				ieee80211_node_authorize(ni);
213 
214 			ieee80211_notify_node_join(ni, 1 /*newassoc*/);
215 			/* XXX inject l2uf frame */
216 		}
217 	}
218 
219 	/*
220 	 * Flush any pending frames now that were setup.
221 	 */
222 	if (ni != NULL)
223 		wds_flush(ni);
224 	return (ni == NULL ? ENOENT : 0);
225 }
226 
227 /*
228  * Propagate multicast frames of an ap vap to all DWDS links.
229  * The caller is assumed to have verified this frame is multicast.
230  */
231 void
232 ieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m)
233 {
234 	struct ieee80211com *ic = vap0->iv_ic;
235 	const struct ether_header *eh = mtod(m, const struct ether_header *);
236 	struct ieee80211_node *ni;
237 	struct ieee80211vap *vap;
238 	struct ifnet *ifp;
239 	struct mbuf *mcopy;
240 	int err;
241 
242 	KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost),
243 	    ("%s not mcast", ether_sprintf(eh->ether_dhost)));
244 
245 	/* XXX locking */
246 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
247 		/* only DWDS vaps are interesting */
248 		if (vap->iv_opmode != IEEE80211_M_WDS ||
249 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))
250 			continue;
251 		/* if it came in this interface, don't send it back out */
252 		ifp = vap->iv_ifp;
253 		if (ifp == m->m_pkthdr.rcvif)
254 			continue;
255 		/*
256 		 * Duplicate the frame and send it.
257 		 */
258 		mcopy = m_copypacket(m, M_NOWAIT);
259 		if (mcopy == NULL) {
260 			ifp->if_oerrors++;
261 			/* XXX stat + msg */
262 			continue;
263 		}
264 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
265 		if (ni == NULL) {
266 			/* NB: ieee80211_find_txnode does stat+msg */
267 			ifp->if_oerrors++;
268 			m_freem(mcopy);
269 			continue;
270 		}
271 		/* calculate priority so drivers can find the tx queue */
272 		if (ieee80211_classify(ni, mcopy)) {
273 			IEEE80211_DISCARD_MAC(vap,
274 			    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS,
275 			    eh->ether_dhost, NULL,
276 			    "%s", "classification failure");
277 			vap->iv_stats.is_tx_classify++;
278 			ifp->if_oerrors++;
279 			m_freem(mcopy);
280 			ieee80211_free_node(ni);
281 			continue;
282 		}
283 
284 		BPF_MTAP(ifp, m);		/* 802.3 tx */
285 
286 		/*
287 		 * Encapsulate the packet in prep for transmission.
288 		 */
289 		mcopy = ieee80211_encap(vap, ni, mcopy);
290 		if (mcopy == NULL) {
291 			/* NB: stat+msg handled in ieee80211_encap */
292 			ieee80211_free_node(ni);
293 			continue;
294 		}
295 		mcopy->m_flags |= M_MCAST;
296 		mcopy->m_pkthdr.rcvif = (void *) ni;
297 
298 		err = ieee80211_parent_xmitpkt(ic, mcopy);
299 		if (err) {
300 			/* NB: IFQ_HANDOFF reclaims mbuf */
301 			ifp->if_oerrors++;
302 			ieee80211_free_node(ni);
303 		} else
304 			ifp->if_opackets++;
305 	}
306 }
307 
308 /*
309  * Handle DWDS discovery on receipt of a 4-address frame in
310  * ap mode.  Queue the frame and post an event for someone
311  * to plumb the necessary WDS vap for this station.  Frames
312  * received prior to the vap set running will then be reprocessed
313  * as if they were just received.
314  */
315 void
316 ieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
317 {
318 	struct ieee80211com *ic = ni->ni_ic;
319 
320 	/*
321 	 * Save the frame with an aging interval 4 times
322 	 * the listen interval specified by the station.
323 	 * Frames that sit around too long are reclaimed
324 	 * using this information.
325 	 * XXX handle overflow?
326 	 * XXX per/vap beacon interval?
327 	 */
328 	m->m_pkthdr.rcvif = (void *)(uintptr_t)
329 	    ieee80211_mac_hash(ic, ni->ni_macaddr);
330 	(void) ieee80211_ageq_append(&ic->ic_stageq, m,
331 	    ((ni->ni_intval * ic->ic_lintval) << 2) / 1024);
332 	ieee80211_notify_wds_discover(ni);
333 }
334 
335 /*
336  * IEEE80211_M_WDS vap state machine handler.
337  */
338 static int
339 wds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
340 {
341 	struct ieee80211com *ic = vap->iv_ic;
342 	struct ieee80211_node *ni;
343 	enum ieee80211_state ostate;
344 	int error;
345 
346 	IEEE80211_LOCK_ASSERT(ic);
347 
348 	ostate = vap->iv_state;
349 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
350 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
351 	vap->iv_state = nstate;			/* state transition */
352 	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
353 	if (ostate != IEEE80211_S_SCAN)
354 		ieee80211_cancel_scan(vap);	/* background scan */
355 	ni = vap->iv_bss;			/* NB: no reference held */
356 	error = 0;
357 	switch (nstate) {
358 	case IEEE80211_S_INIT:
359 		switch (ostate) {
360 		case IEEE80211_S_SCAN:
361 			ieee80211_cancel_scan(vap);
362 			break;
363 		default:
364 			break;
365 		}
366 		if (ostate != IEEE80211_S_INIT) {
367 			/* NB: optimize INIT -> INIT case */
368 			ieee80211_reset_bss(vap);
369 		}
370 		break;
371 	case IEEE80211_S_SCAN:
372 		switch (ostate) {
373 		case IEEE80211_S_INIT:
374 			ieee80211_check_scan_current(vap);
375 			break;
376 		default:
377 			break;
378 		}
379 		break;
380 	case IEEE80211_S_RUN:
381 		if (ostate == IEEE80211_S_INIT) {
382 			/*
383 			 * Already have a channel; bypass the scan
384 			 * and startup immediately.
385 			 */
386 			error = ieee80211_create_wds(vap, ic->ic_curchan);
387 		}
388 		break;
389 	default:
390 		break;
391 	}
392 	return error;
393 }
394 
395 /*
396  * Process a received frame.  The node associated with the sender
397  * should be supplied.  If nothing was found in the node table then
398  * the caller is assumed to supply a reference to iv_bss instead.
399  * The RSSI and a timestamp are also supplied.  The RSSI data is used
400  * during AP scanning to select a AP to associate with; it can have
401  * any units so long as values have consistent units and higher values
402  * mean ``better signal''.  The receive timestamp is currently not used
403  * by the 802.11 layer.
404  */
405 static int
406 wds_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
407 {
408 #define	HAS_SEQ(type)	((type & 0x4) == 0)
409 	struct ieee80211vap *vap = ni->ni_vap;
410 	struct ieee80211com *ic = ni->ni_ic;
411 	struct ifnet *ifp = vap->iv_ifp;
412 	struct ieee80211_frame *wh;
413 	struct ieee80211_key *key;
414 	struct ether_header *eh;
415 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
416 	uint8_t dir, type, subtype, qos;
417 	uint16_t rxseq;
418 
419 	if (m->m_flags & M_AMPDU_MPDU) {
420 		/*
421 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
422 		 * w/ M_AMPDU_MPDU marked have already passed through
423 		 * here but were received out of order and been held on
424 		 * the reorder queue.  When resubmitted they are marked
425 		 * with the M_AMPDU_MPDU flag and we can bypass most of
426 		 * the normal processing.
427 		 */
428 		wh = mtod(m, struct ieee80211_frame *);
429 		type = IEEE80211_FC0_TYPE_DATA;
430 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
431 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
432 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
433 		goto resubmit_ampdu;
434 	}
435 
436 	KASSERT(ni != NULL, ("null node"));
437 
438 	type = -1;			/* undefined */
439 
440 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
441 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
442 		    ni->ni_macaddr, NULL,
443 		    "too short (1): len %u", m->m_pkthdr.len);
444 		vap->iv_stats.is_rx_tooshort++;
445 		goto out;
446 	}
447 	/*
448 	 * Bit of a cheat here, we use a pointer for a 3-address
449 	 * frame format but don't reference fields past outside
450 	 * ieee80211_frame_min w/o first validating the data is
451 	 * present.
452 	 */
453 	wh = mtod(m, struct ieee80211_frame *);
454 
455 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
456 		ni->ni_inact = ni->ni_inact_reload;
457 
458 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
459 	    IEEE80211_FC0_VERSION_0) {
460 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
461 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
462 		    wh->i_fc[0], wh->i_fc[1]);
463 		vap->iv_stats.is_rx_badversion++;
464 		goto err;
465 	}
466 
467 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
468 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
469 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
470 
471 	/* NB: WDS vap's do not scan */
472 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
473 		IEEE80211_DISCARD_MAC(vap,
474 		    IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
475 		    "too short (3): len %u", m->m_pkthdr.len);
476 		vap->iv_stats.is_rx_tooshort++;
477 		goto out;
478 	}
479 	/* NB: the TA is implicitly verified by finding the wds peer node */
480 	if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
481 	    !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
482 		/* not interested in */
483 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
484 		    wh->i_addr1, NULL, "%s", "not to bss");
485 		vap->iv_stats.is_rx_wrongbss++;
486 		goto out;
487 	}
488 	IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
489 	ni->ni_noise = nf;
490 	if (HAS_SEQ(type)) {
491 		uint8_t tid = ieee80211_gettid(wh);
492 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
493 		    TID_TO_WME_AC(tid) >= WME_AC_VI)
494 			ic->ic_wme.wme_hipri_traffic++;
495 		rxseq = le16toh(*(uint16_t *)wh->i_seq);
496 		if (! ieee80211_check_rxseq(ni, wh)) {
497 			/* duplicate, discard */
498 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
499 			    wh->i_addr1, "duplicate",
500 			    "seqno <%u,%u> fragno <%u,%u> tid %u",
501 			    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
502 			    ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
503 			    rxseq & IEEE80211_SEQ_FRAG_MASK,
504 			    ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
505 			    tid);
506 			vap->iv_stats.is_rx_dup++;
507 			IEEE80211_NODE_STAT(ni, rx_dup);
508 			goto out;
509 		}
510 		ni->ni_rxseqs[tid] = rxseq;
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) != 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 (wh->i_fc[1] & IEEE80211_FC1_WEP) {
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 			key = ieee80211_crypto_decap(ni, m, hdrspace);
571 			if (key == NULL) {
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_WEP;
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) {
587 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
588 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
589 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
590 		} else
591 			qos = 0;
592 
593 		/*
594 		 * Next up, any fragmentation.
595 		 */
596 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
597 			m = ieee80211_defrag(ni, m, hdrspace);
598 			if (m == NULL) {
599 				/* Fragment dropped or frame not complete yet */
600 				goto out;
601 			}
602 		}
603 		wh = NULL;		/* no longer valid, catch any uses */
604 
605 		/*
606 		 * Next strip any MSDU crypto bits.
607 		 */
608 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
609 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
610 			    ni->ni_macaddr, "data", "%s", "demic error");
611 			vap->iv_stats.is_rx_demicfail++;
612 			IEEE80211_NODE_STAT(ni, rx_demicfail);
613 			goto out;
614 		}
615 
616 		/* copy to listener after decrypt */
617 		if (ieee80211_radiotap_active_vap(vap))
618 			ieee80211_radiotap_rx(vap, m);
619 		need_tap = 0;
620 
621 		/*
622 		 * Finally, strip the 802.11 header.
623 		 */
624 		m = ieee80211_decap(vap, m, hdrspace);
625 		if (m == NULL) {
626 			/* XXX mask bit to check for both */
627 			/* don't count Null data frames as errors */
628 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
629 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
630 				goto out;
631 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
632 			    ni->ni_macaddr, "data", "%s", "decap error");
633 			vap->iv_stats.is_rx_decap++;
634 			IEEE80211_NODE_STAT(ni, rx_decap);
635 			goto err;
636 		}
637 		eh = mtod(m, struct ether_header *);
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->ether_type != htons(ETHERTYPE_PAE)) {
648 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
649 				    eh->ether_shost, "data",
650 				    "unauthorized port: ether type 0x%x len %u",
651 				    eh->ether_type, m->m_pkthdr.len);
652 				vap->iv_stats.is_rx_unauth++;
653 				IEEE80211_NODE_STAT(ni, rx_unauth);
654 				goto err;
655 			}
656 		} else {
657 			/*
658 			 * When denying unencrypted frames, discard
659 			 * any non-PAE frames received without encryption.
660 			 */
661 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
662 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
663 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
664 				/*
665 				 * Drop unencrypted frames.
666 				 */
667 				vap->iv_stats.is_rx_unencrypted++;
668 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
669 				goto out;
670 			}
671 		}
672 		/* XXX require HT? */
673 		if (qos & IEEE80211_QOS_AMSDU) {
674 			m = ieee80211_decap_amsdu(ni, m);
675 			if (m == NULL)
676 				return IEEE80211_FC0_TYPE_DATA;
677 		} else {
678 #ifdef IEEE80211_SUPPORT_SUPERG
679 			m = ieee80211_decap_fastframe(vap, ni, m);
680 			if (m == NULL)
681 				return IEEE80211_FC0_TYPE_DATA;
682 #endif
683 		}
684 		ieee80211_deliver_data(vap, ni, m);
685 		return IEEE80211_FC0_TYPE_DATA;
686 
687 	case IEEE80211_FC0_TYPE_MGT:
688 		vap->iv_stats.is_rx_mgmt++;
689 		IEEE80211_NODE_STAT(ni, rx_mgmt);
690 		if (dir != IEEE80211_FC1_DIR_NODS) {
691 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
692 			    wh, "data", "incorrect dir 0x%x", dir);
693 			vap->iv_stats.is_rx_wrongdir++;
694 			goto err;
695 		}
696 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
697 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
698 			    ni->ni_macaddr, "mgt", "too short: len %u",
699 			    m->m_pkthdr.len);
700 			vap->iv_stats.is_rx_tooshort++;
701 			goto out;
702 		}
703 #ifdef IEEE80211_DEBUG
704 		if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
705 			if_printf(ifp, "received %s from %s rssi %d\n",
706 			    ieee80211_mgt_subtype_name[subtype >>
707 				IEEE80211_FC0_SUBTYPE_SHIFT],
708 			    ether_sprintf(wh->i_addr2), rssi);
709 		}
710 #endif
711 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
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, 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 	ifp->if_ierrors++;
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,
744 	int subtype, 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_BEACON:
784 	case IEEE80211_FC0_SUBTYPE_ATIM:
785 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
786 	case IEEE80211_FC0_SUBTYPE_AUTH:
787 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
788 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
789 		    wh, NULL, "%s", "not handled");
790 		vap->iv_stats.is_rx_mgtdiscard++;
791 		break;
792 
793 	default:
794 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
795 		    wh, "mgt", "subtype 0x%x not handled", subtype);
796 		vap->iv_stats.is_rx_badsubtype++;
797 		break;
798 	}
799 }
800