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