xref: /linux/net/mac80211/rx.c (revision dfecb0c5af3b07ebfa84be63a7a21bfc9e29a872)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
6  * Copyright 2007-2010	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
9  * Copyright (C) 2018-2026 Intel Corporation
10  */
11 
12 #include <linux/jiffies.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rcupdate.h>
19 #include <linux/export.h>
20 #include <linux/kcov.h>
21 #include <linux/bitops.h>
22 #include <kunit/visibility.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <linux/unaligned.h>
26 
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
29 #include "led.h"
30 #include "mesh.h"
31 #include "wep.h"
32 #include "wpa.h"
33 #include "tkip.h"
34 #include "wme.h"
35 #include "rate.h"
36 
37 /*
38  * monitor mode reception
39  *
40  * This function cleans up the SKB, i.e. it removes all the stuff
41  * only useful for monitoring.
42  */
43 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
44 					   unsigned int present_fcs_len,
45 					   unsigned int rtap_space)
46 {
47 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
48 	struct ieee80211_hdr *hdr;
49 	unsigned int hdrlen;
50 	__le16 fc;
51 
52 	if (present_fcs_len)
53 		__pskb_trim(skb, skb->len - present_fcs_len);
54 	pskb_pull(skb, rtap_space);
55 
56 	/* After pulling radiotap header, clear all flags that indicate
57 	 * info in skb->data.
58 	 */
59 	status->flag &= ~(RX_FLAG_RADIOTAP_TLV_AT_END |
60 			  RX_FLAG_RADIOTAP_LSIG |
61 			  RX_FLAG_RADIOTAP_HE_MU |
62 			  RX_FLAG_RADIOTAP_HE |
63 			  RX_FLAG_RADIOTAP_VHT);
64 
65 	hdr = (void *)skb->data;
66 	fc = hdr->frame_control;
67 
68 	/*
69 	 * Remove the HT-Control field (if present) on management
70 	 * frames after we've sent the frame to monitoring. We
71 	 * (currently) don't need it, and don't properly parse
72 	 * frames with it present, due to the assumption of a
73 	 * fixed management header length.
74 	 */
75 	if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
76 		return skb;
77 
78 	hdrlen = ieee80211_hdrlen(fc);
79 	hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
80 
81 	if (!pskb_may_pull(skb, hdrlen)) {
82 		dev_kfree_skb(skb);
83 		return NULL;
84 	}
85 
86 	memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
87 		hdrlen - IEEE80211_HT_CTL_LEN);
88 	pskb_pull(skb, IEEE80211_HT_CTL_LEN);
89 
90 	return skb;
91 }
92 
93 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
94 				     unsigned int rtap_space)
95 {
96 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
97 	struct ieee80211_hdr *hdr;
98 
99 	hdr = (void *)(skb->data + rtap_space);
100 
101 	if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
102 			    RX_FLAG_FAILED_PLCP_CRC |
103 			    RX_FLAG_ONLY_MONITOR |
104 			    RX_FLAG_NO_PSDU))
105 		return true;
106 
107 	if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
108 		return true;
109 
110 	if (ieee80211_is_ctl(hdr->frame_control) &&
111 	    !ieee80211_is_pspoll(hdr->frame_control) &&
112 	    !ieee80211_is_back_req(hdr->frame_control))
113 		return true;
114 
115 	return false;
116 }
117 
118 static int
119 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
120 			     struct ieee80211_rx_status *status,
121 			     struct sk_buff *skb)
122 {
123 	int len;
124 
125 	/* always present fields */
126 	len = sizeof(struct ieee80211_radiotap_header) + 8;
127 
128 	/* allocate extra bitmaps */
129 	if (status->chains)
130 		len += 4 * hweight8(status->chains);
131 
132 	if (ieee80211_have_rx_timestamp(status)) {
133 		len = ALIGN(len, 8);
134 		len += 8;
135 	}
136 	if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
137 		len += 1;
138 
139 	/* antenna field, if we don't have per-chain info */
140 	if (!status->chains)
141 		len += 1;
142 
143 	/* padding for RX_FLAGS if necessary */
144 	len = ALIGN(len, 2);
145 
146 	if (status->encoding == RX_ENC_HT) /* HT info */
147 		len += 3;
148 
149 	if (status->flag & RX_FLAG_AMPDU_DETAILS) {
150 		len = ALIGN(len, 4);
151 		len += 8;
152 	}
153 
154 	if (status->encoding == RX_ENC_VHT) {
155 		/* Included even if RX_FLAG_RADIOTAP_VHT is not set */
156 		len = ALIGN(len, 2);
157 		len += 12;
158 		BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_vht) != 12);
159 	}
160 
161 	if (local->hw.radiotap_timestamp.units_pos >= 0) {
162 		len = ALIGN(len, 8);
163 		len += 12;
164 	}
165 
166 	if (status->encoding == RX_ENC_HE &&
167 	    status->flag & RX_FLAG_RADIOTAP_HE) {
168 		len = ALIGN(len, 2);
169 		len += 12;
170 		BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
171 	}
172 
173 	if (status->encoding == RX_ENC_HE &&
174 	    status->flag & RX_FLAG_RADIOTAP_HE_MU) {
175 		len = ALIGN(len, 2);
176 		len += 12;
177 		BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
178 	}
179 
180 	if (status->flag & RX_FLAG_NO_PSDU)
181 		len += 1;
182 
183 	if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
184 		len = ALIGN(len, 2);
185 		len += 4;
186 		BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
187 	}
188 
189 	if (status->chains) {
190 		/* antenna and antenna signal fields */
191 		len += 2 * hweight8(status->chains);
192 	}
193 
194 	if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
195 		int tlv_offset = 0;
196 
197 		/*
198 		 * The position to look at depends on the existence (or non-
199 		 * existence) of other elements, so take that into account...
200 		 */
201 		if (status->flag & RX_FLAG_RADIOTAP_VHT)
202 			tlv_offset +=
203 				sizeof(struct ieee80211_radiotap_vht);
204 		if (status->flag & RX_FLAG_RADIOTAP_HE)
205 			tlv_offset +=
206 				sizeof(struct ieee80211_radiotap_he);
207 		if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
208 			tlv_offset +=
209 				sizeof(struct ieee80211_radiotap_he_mu);
210 		if (status->flag & RX_FLAG_RADIOTAP_LSIG)
211 			tlv_offset +=
212 				sizeof(struct ieee80211_radiotap_lsig);
213 
214 		/* ensure 4 byte alignment for TLV */
215 		len = ALIGN(len, 4);
216 
217 		/* TLVs until the mac header */
218 		len += skb_mac_header(skb) - &skb->data[tlv_offset];
219 	}
220 
221 	return len;
222 }
223 
224 static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
225 					   int link_id,
226 					   struct sta_info *sta,
227 					   struct sk_buff *skb)
228 {
229 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
230 
231 	if (link_id >= 0) {
232 		status->link_valid = 1;
233 		status->link_id = link_id;
234 	} else {
235 		status->link_valid = 0;
236 	}
237 
238 	skb_queue_tail(&sdata->skb_queue, skb);
239 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
240 	if (sta) {
241 		struct link_sta_info *link_sta_info;
242 
243 		if (link_id >= 0) {
244 			link_sta_info = rcu_dereference(sta->link[link_id]);
245 			if (!link_sta_info)
246 				return;
247 		} else {
248 			link_sta_info = &sta->deflink;
249 		}
250 
251 		link_sta_info->rx_stats.packets++;
252 	}
253 }
254 
255 static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
256 					 int link_id,
257 					 struct sta_info *sta,
258 					 struct sk_buff *skb)
259 {
260 	skb->protocol = 0;
261 	__ieee80211_queue_skb_to_iface(sdata, link_id, sta, skb);
262 }
263 
264 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
265 					 struct sk_buff *skb,
266 					 int rtap_space)
267 {
268 	struct {
269 		struct ieee80211_hdr_3addr hdr;
270 		u8 category;
271 		u8 action_code;
272 	} __packed __aligned(2) action;
273 
274 	if (!sdata)
275 		return;
276 
277 	BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE(action_code));
278 
279 	if (skb->len < rtap_space + sizeof(action) +
280 		       VHT_MUMIMO_GROUPS_DATA_LEN)
281 		return;
282 
283 	if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
284 		return;
285 
286 	skb_copy_bits(skb, rtap_space, &action, sizeof(action));
287 
288 	if (!ieee80211_is_action(action.hdr.frame_control))
289 		return;
290 
291 	if (action.category != WLAN_CATEGORY_VHT)
292 		return;
293 
294 	if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
295 		return;
296 
297 	if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
298 		return;
299 
300 	skb = skb_copy(skb, GFP_ATOMIC);
301 	if (!skb)
302 		return;
303 
304 	ieee80211_queue_skb_to_iface(sdata, -1, NULL, skb);
305 }
306 
307 /*
308  * ieee80211_add_rx_radiotap_header - add radiotap header
309  *
310  * add a radiotap header containing all the fields which the hardware provided.
311  */
312 static void
313 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
314 				 struct sk_buff *skb,
315 				 struct ieee80211_rate *rate,
316 				 int rtap_len, bool has_fcs)
317 {
318 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
319 	struct ieee80211_radiotap_header *rthdr;
320 	unsigned char *pos;
321 	__le32 *it_present;
322 	u32 it_present_val;
323 	u16 rx_flags = 0;
324 	u16 channel_flags = 0;
325 	u32 tlvs_len = 0;
326 	int mpdulen, chain;
327 	unsigned long chains = status->chains;
328 	struct ieee80211_radiotap_vht vht = {};
329 	struct ieee80211_radiotap_he he = {};
330 	struct ieee80211_radiotap_he_mu he_mu = {};
331 	struct ieee80211_radiotap_lsig lsig = {};
332 
333 	if (status->flag & RX_FLAG_RADIOTAP_VHT) {
334 		vht = *(struct ieee80211_radiotap_vht *)skb->data;
335 		skb_pull(skb, sizeof(vht));
336 		WARN_ON_ONCE(status->encoding != RX_ENC_VHT);
337 	}
338 
339 	if (status->flag & RX_FLAG_RADIOTAP_HE) {
340 		he = *(struct ieee80211_radiotap_he *)skb->data;
341 		skb_pull(skb, sizeof(he));
342 		WARN_ON_ONCE(status->encoding != RX_ENC_HE);
343 	}
344 
345 	if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
346 		he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
347 		skb_pull(skb, sizeof(he_mu));
348 	}
349 
350 	if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
351 		lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
352 		skb_pull(skb, sizeof(lsig));
353 	}
354 
355 	if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
356 		/* data is pointer at tlv all other info was pulled off */
357 		tlvs_len = skb_mac_header(skb) - skb->data;
358 	}
359 
360 	mpdulen = skb->len;
361 	if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
362 		mpdulen += FCS_LEN;
363 
364 	rthdr = skb_push(skb, rtap_len - tlvs_len);
365 	memset(rthdr, 0, rtap_len - tlvs_len);
366 	it_present = &rthdr->it_present;
367 
368 	/* radiotap header, set always present flags */
369 	rthdr->it_len = cpu_to_le16(rtap_len);
370 	it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
371 			 BIT(IEEE80211_RADIOTAP_CHANNEL) |
372 			 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
373 
374 	if (!status->chains)
375 		it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
376 
377 	for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
378 		it_present_val |=
379 			BIT(IEEE80211_RADIOTAP_EXT) |
380 			BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
381 		put_unaligned_le32(it_present_val, it_present);
382 		it_present++;
383 		it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
384 				 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
385 	}
386 
387 	if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
388 		it_present_val |= BIT(IEEE80211_RADIOTAP_TLV);
389 
390 	put_unaligned_le32(it_present_val, it_present);
391 
392 	/* This references through an offset into it_optional[] rather
393 	 * than via it_present otherwise later uses of pos will cause
394 	 * the compiler to think we have walked past the end of the
395 	 * struct member.
396 	 */
397 	pos = (void *)&rthdr->it_optional[it_present + 1 - rthdr->it_optional];
398 
399 	/* the order of the following fields is important */
400 
401 	/* IEEE80211_RADIOTAP_TSFT */
402 	if (ieee80211_have_rx_timestamp(status)) {
403 		/* padding */
404 		while ((pos - (u8 *)rthdr) & 7)
405 			*pos++ = 0;
406 		put_unaligned_le64(
407 			ieee80211_calculate_rx_timestamp(local, status,
408 							 mpdulen, 0),
409 			pos);
410 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_TSFT));
411 		pos += 8;
412 	}
413 
414 	/* IEEE80211_RADIOTAP_FLAGS */
415 	if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
416 		*pos |= IEEE80211_RADIOTAP_F_FCS;
417 	if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
418 		*pos |= IEEE80211_RADIOTAP_F_BADFCS;
419 	if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
420 		*pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
421 	pos++;
422 
423 	/* IEEE80211_RADIOTAP_RATE */
424 	if (!rate || status->encoding != RX_ENC_LEGACY) {
425 		/*
426 		 * Without rate information don't add it. If we have,
427 		 * MCS information is a separate field in radiotap,
428 		 * added below. The byte here is needed as padding
429 		 * for the channel though, so initialise it to 0.
430 		 */
431 		*pos = 0;
432 	} else {
433 		int shift = 0;
434 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_RATE));
435 		if (status->bw == RATE_INFO_BW_10)
436 			shift = 1;
437 		else if (status->bw == RATE_INFO_BW_5)
438 			shift = 2;
439 		*pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
440 	}
441 	pos++;
442 
443 	/* IEEE80211_RADIOTAP_CHANNEL */
444 	/* TODO: frequency offset in KHz */
445 	put_unaligned_le16(status->freq, pos);
446 	pos += 2;
447 	if (status->bw == RATE_INFO_BW_10)
448 		channel_flags |= IEEE80211_CHAN_HALF;
449 	else if (status->bw == RATE_INFO_BW_5)
450 		channel_flags |= IEEE80211_CHAN_QUARTER;
451 
452 	if (status->band == NL80211_BAND_5GHZ ||
453 	    status->band == NL80211_BAND_6GHZ)
454 		channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
455 	else if (status->encoding != RX_ENC_LEGACY)
456 		channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
457 	else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
458 		channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
459 	else if (rate)
460 		channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
461 	else
462 		channel_flags |= IEEE80211_CHAN_2GHZ;
463 	put_unaligned_le16(channel_flags, pos);
464 	pos += 2;
465 
466 	/* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
467 	if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
468 	    !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
469 		*pos = status->signal;
470 		rthdr->it_present |=
471 			cpu_to_le32(BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL));
472 		pos++;
473 	}
474 
475 	/* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
476 
477 	if (!status->chains) {
478 		/* IEEE80211_RADIOTAP_ANTENNA */
479 		*pos = status->antenna;
480 		pos++;
481 	}
482 
483 	/* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
484 
485 	/* IEEE80211_RADIOTAP_RX_FLAGS */
486 	/* ensure 2 byte alignment for the 2 byte field as required */
487 	if ((pos - (u8 *)rthdr) & 1)
488 		*pos++ = 0;
489 	if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
490 		rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
491 	put_unaligned_le16(rx_flags, pos);
492 	pos += 2;
493 
494 	if (status->encoding == RX_ENC_HT) {
495 		unsigned int stbc;
496 
497 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_MCS));
498 		*pos = local->hw.radiotap_mcs_details;
499 		if (status->enc_flags & RX_ENC_FLAG_HT_GF)
500 			*pos |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
501 		if (status->enc_flags & RX_ENC_FLAG_LDPC)
502 			*pos |= IEEE80211_RADIOTAP_MCS_HAVE_FEC;
503 		pos++;
504 		*pos = 0;
505 		if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
506 			*pos |= IEEE80211_RADIOTAP_MCS_SGI;
507 		if (status->bw == RATE_INFO_BW_40)
508 			*pos |= IEEE80211_RADIOTAP_MCS_BW_40;
509 		if (status->enc_flags & RX_ENC_FLAG_HT_GF)
510 			*pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
511 		if (status->enc_flags & RX_ENC_FLAG_LDPC)
512 			*pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
513 		stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
514 		*pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
515 		pos++;
516 		*pos++ = status->rate_idx;
517 	}
518 
519 	if (status->flag & RX_FLAG_AMPDU_DETAILS) {
520 		u16 flags = 0;
521 
522 		/* ensure 4 byte alignment */
523 		while ((pos - (u8 *)rthdr) & 3)
524 			pos++;
525 		rthdr->it_present |=
526 			cpu_to_le32(BIT(IEEE80211_RADIOTAP_AMPDU_STATUS));
527 		put_unaligned_le32(status->ampdu_reference, pos);
528 		pos += 4;
529 		if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
530 			flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
531 		if (status->flag & RX_FLAG_AMPDU_IS_LAST)
532 			flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
533 		if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
534 			flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
535 		if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
536 			flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
537 		if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
538 			flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
539 		put_unaligned_le16(flags, pos);
540 		pos += 2;
541 		*pos++ = 0;
542 		*pos++ = 0;
543 	}
544 
545 	if (status->encoding == RX_ENC_VHT) {
546 		u16 fill = local->hw.radiotap_vht_details;
547 
548 		/* Leave driver filled fields alone */
549 		fill &= ~le16_to_cpu(vht.known);
550 		vht.known |= cpu_to_le16(fill);
551 
552 		if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_GI &&
553 		    status->enc_flags & RX_ENC_FLAG_SHORT_GI)
554 			vht.flags |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
555 		/* in VHT, STBC is binary */
556 		if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_STBC &&
557 		    status->enc_flags & RX_ENC_FLAG_STBC_MASK)
558 			vht.flags |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
559 		if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED &&
560 		    status->enc_flags & RX_ENC_FLAG_BF)
561 			*pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
562 
563 		if (fill & IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
564 			switch (status->bw) {
565 			case RATE_INFO_BW_40:
566 				vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_40;
567 				break;
568 			case RATE_INFO_BW_80:
569 				vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_80;
570 				break;
571 			case RATE_INFO_BW_160:
572 				vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_160;
573 				break;
574 			default:
575 				vht.bandwidth = IEEE80211_RADIOTAP_VHT_BW_20;
576 				break;
577 			}
578 		}
579 
580 		/*
581 		 * If the driver filled in mcs_nss[0], then do not touch it.
582 		 *
583 		 * Otherwise, put some information about MCS/NSS into the
584 		 * user 0 field. Note that this is not technically correct for
585 		 * an MU frame as we might have decoded a different user.
586 		 */
587 		if (!vht.mcs_nss[0]) {
588 			vht.mcs_nss[0] = (status->rate_idx << 4) | status->nss;
589 
590 			/* coding field */
591 			if (status->enc_flags & RX_ENC_FLAG_LDPC)
592 				vht.coding |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
593 		}
594 
595 		/* ensure 2 byte alignment */
596 		while ((pos - (u8 *)rthdr) & 1)
597 			pos++;
598 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_VHT));
599 		memcpy(pos, &vht, sizeof(vht));
600 		pos += sizeof(vht);
601 	}
602 
603 	if (local->hw.radiotap_timestamp.units_pos >= 0) {
604 		u16 accuracy = 0;
605 		u8 flags;
606 		u64 ts;
607 
608 		rthdr->it_present |=
609 			cpu_to_le32(BIT(IEEE80211_RADIOTAP_TIMESTAMP));
610 
611 		/* ensure 8 byte alignment */
612 		while ((pos - (u8 *)rthdr) & 7)
613 			pos++;
614 
615 		if (status->flag & RX_FLAG_MACTIME_IS_RTAP_TS64) {
616 			flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_64BIT;
617 			ts = status->mactime;
618 		} else {
619 			flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
620 			ts = status->device_timestamp;
621 		}
622 
623 		put_unaligned_le64(ts, pos);
624 		pos += sizeof(u64);
625 
626 		if (local->hw.radiotap_timestamp.accuracy >= 0) {
627 			accuracy = local->hw.radiotap_timestamp.accuracy;
628 			flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
629 		}
630 		put_unaligned_le16(accuracy, pos);
631 		pos += sizeof(u16);
632 
633 		*pos++ = local->hw.radiotap_timestamp.units_pos;
634 		*pos++ = flags;
635 	}
636 
637 	if (status->encoding == RX_ENC_HE &&
638 	    status->flag & RX_FLAG_RADIOTAP_HE) {
639 #define HE_PREP(f, val)	le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
640 
641 		if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
642 			he.data6 |= HE_PREP(DATA6_NSTS,
643 					    FIELD_GET(RX_ENC_FLAG_STBC_MASK,
644 						      status->enc_flags));
645 			he.data3 |= HE_PREP(DATA3_STBC, 1);
646 		} else {
647 			he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
648 		}
649 
650 #define CHECK_GI(s) \
651 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
652 		     (int)NL80211_RATE_INFO_HE_GI_##s)
653 
654 		CHECK_GI(0_8);
655 		CHECK_GI(1_6);
656 		CHECK_GI(3_2);
657 
658 		he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
659 		he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
660 		he.data3 |= HE_PREP(DATA3_CODING,
661 				    !!(status->enc_flags & RX_ENC_FLAG_LDPC));
662 
663 		he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
664 
665 		switch (status->bw) {
666 		case RATE_INFO_BW_20:
667 			he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
668 					    IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
669 			break;
670 		case RATE_INFO_BW_40:
671 			he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
672 					    IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
673 			break;
674 		case RATE_INFO_BW_80:
675 			he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
676 					    IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
677 			break;
678 		case RATE_INFO_BW_160:
679 			he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
680 					    IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
681 			break;
682 		case RATE_INFO_BW_HE_RU:
683 #define CHECK_RU_ALLOC(s) \
684 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
685 		     NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
686 
687 			CHECK_RU_ALLOC(26);
688 			CHECK_RU_ALLOC(52);
689 			CHECK_RU_ALLOC(106);
690 			CHECK_RU_ALLOC(242);
691 			CHECK_RU_ALLOC(484);
692 			CHECK_RU_ALLOC(996);
693 			CHECK_RU_ALLOC(2x996);
694 
695 			he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
696 					    status->he_ru + 4);
697 			break;
698 		default:
699 			WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
700 		}
701 
702 		/* ensure 2 byte alignment */
703 		while ((pos - (u8 *)rthdr) & 1)
704 			pos++;
705 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE));
706 		memcpy(pos, &he, sizeof(he));
707 		pos += sizeof(he);
708 	}
709 
710 	if (status->encoding == RX_ENC_HE &&
711 	    status->flag & RX_FLAG_RADIOTAP_HE_MU) {
712 		/* ensure 2 byte alignment */
713 		while ((pos - (u8 *)rthdr) & 1)
714 			pos++;
715 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE_MU));
716 		memcpy(pos, &he_mu, sizeof(he_mu));
717 		pos += sizeof(he_mu);
718 	}
719 
720 	if (status->flag & RX_FLAG_NO_PSDU) {
721 		rthdr->it_present |=
722 			cpu_to_le32(BIT(IEEE80211_RADIOTAP_ZERO_LEN_PSDU));
723 		*pos++ = status->zero_length_psdu_type;
724 	}
725 
726 	if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
727 		/* ensure 2 byte alignment */
728 		while ((pos - (u8 *)rthdr) & 1)
729 			pos++;
730 		rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_LSIG));
731 		memcpy(pos, &lsig, sizeof(lsig));
732 		pos += sizeof(lsig);
733 	}
734 
735 	for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
736 		*pos++ = status->chain_signal[chain];
737 		*pos++ = chain;
738 	}
739 }
740 
741 static struct sk_buff *
742 ieee80211_make_monitor_skb(struct ieee80211_local *local,
743 			   struct sk_buff **origskb,
744 			   struct ieee80211_rate *rate,
745 			   int rtap_space, bool use_origskb)
746 {
747 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
748 	int rt_hdrlen, needed_headroom;
749 	struct sk_buff *skb;
750 
751 	/* room for the radiotap header based on driver features */
752 	rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
753 	needed_headroom = rt_hdrlen - rtap_space;
754 
755 	if (use_origskb) {
756 		/* only need to expand headroom if necessary */
757 		skb = *origskb;
758 		*origskb = NULL;
759 
760 		/*
761 		 * This shouldn't trigger often because most devices have an
762 		 * RX header they pull before we get here, and that should
763 		 * be big enough for our radiotap information. We should
764 		 * probably export the length to drivers so that we can have
765 		 * them allocate enough headroom to start with.
766 		 */
767 		if (skb_headroom(skb) < needed_headroom &&
768 		    pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
769 			dev_kfree_skb(skb);
770 			return NULL;
771 		}
772 	} else {
773 		/*
774 		 * Need to make a copy and possibly remove radiotap header
775 		 * and FCS from the original.
776 		 */
777 		skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD,
778 				      0, GFP_ATOMIC);
779 
780 		if (!skb)
781 			return NULL;
782 	}
783 
784 	/* prepend radiotap information */
785 	ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
786 
787 	skb_reset_mac_header(skb);
788 	skb->ip_summed = CHECKSUM_UNNECESSARY;
789 	skb->pkt_type = PACKET_OTHERHOST;
790 	skb->protocol = htons(ETH_P_802_2);
791 
792 	return skb;
793 }
794 
795 static bool
796 ieee80211_validate_monitor_radio(struct ieee80211_sub_if_data *sdata,
797 				 struct ieee80211_local *local,
798 				 struct ieee80211_rx_status *status)
799 {
800 	struct wiphy *wiphy = local->hw.wiphy;
801 	int i, freq, bw;
802 
803 	if (!wiphy->n_radio)
804 		return true;
805 
806 	switch (status->bw) {
807 	case RATE_INFO_BW_20:
808 		bw = 20000;
809 		break;
810 	case RATE_INFO_BW_40:
811 		bw = 40000;
812 		break;
813 	case RATE_INFO_BW_80:
814 		bw = 80000;
815 		break;
816 	case RATE_INFO_BW_160:
817 		bw = 160000;
818 		break;
819 	case RATE_INFO_BW_320:
820 		bw = 320000;
821 		break;
822 	default:
823 		return false;
824 	}
825 
826 	freq = MHZ_TO_KHZ(status->freq);
827 
828 	for (i = 0; i < wiphy->n_radio; i++) {
829 		if (!(sdata->wdev.radio_mask & BIT(i)))
830 			continue;
831 
832 		if (!ieee80211_radio_freq_range_valid(&wiphy->radio[i], freq, bw))
833 			continue;
834 
835 		return true;
836 	}
837 	return false;
838 }
839 
840 /*
841  * This function copies a received frame to all monitor interfaces and
842  * returns a cleaned-up SKB that no longer includes the FCS nor the
843  * radiotap header the driver might have added.
844  */
845 static struct sk_buff *
846 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
847 		     struct ieee80211_rate *rate)
848 {
849 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
850 	struct ieee80211_sub_if_data *sdata, *prev_sdata = NULL;
851 	struct sk_buff *skb, *monskb = NULL;
852 	int present_fcs_len = 0;
853 	unsigned int rtap_space = 0;
854 	struct ieee80211_sub_if_data *monitor_sdata =
855 		rcu_dereference(local->monitor_sdata);
856 	bool only_monitor = false;
857 	unsigned int min_head_len;
858 
859 	if (WARN_ON_ONCE(status->flag & RX_FLAG_RADIOTAP_TLV_AT_END &&
860 			 !skb_mac_header_was_set(origskb))) {
861 		/* with this skb no way to know where frame payload starts */
862 		dev_kfree_skb(origskb);
863 		return NULL;
864 	}
865 
866 	if (status->flag & RX_FLAG_RADIOTAP_VHT)
867 		rtap_space += sizeof(struct ieee80211_radiotap_vht);
868 
869 	if (status->flag & RX_FLAG_RADIOTAP_HE)
870 		rtap_space += sizeof(struct ieee80211_radiotap_he);
871 
872 	if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
873 		rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
874 
875 	if (status->flag & RX_FLAG_RADIOTAP_LSIG)
876 		rtap_space += sizeof(struct ieee80211_radiotap_lsig);
877 
878 	if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
879 		rtap_space += skb_mac_header(origskb) - &origskb->data[rtap_space];
880 
881 	min_head_len = rtap_space;
882 
883 	/*
884 	 * First, we may need to make a copy of the skb because
885 	 *  (1) we need to modify it for radiotap (if not present), and
886 	 *  (2) the other RX handlers will modify the skb we got.
887 	 *
888 	 * We don't need to, of course, if we aren't going to return
889 	 * the SKB because it has a bad FCS/PLCP checksum.
890 	 */
891 
892 	if (!(status->flag & RX_FLAG_NO_PSDU)) {
893 		if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
894 			if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
895 				/* driver bug */
896 				WARN_ON(1);
897 				dev_kfree_skb(origskb);
898 				return NULL;
899 			}
900 			present_fcs_len = FCS_LEN;
901 		}
902 
903 		/* also consider the hdr->frame_control */
904 		min_head_len += 2;
905 	}
906 
907 	/* ensure that the expected data elements are in skb head */
908 	if (!pskb_may_pull(origskb, min_head_len)) {
909 		dev_kfree_skb(origskb);
910 		return NULL;
911 	}
912 
913 	only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
914 
915 	if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
916 		if (only_monitor) {
917 			dev_kfree_skb(origskb);
918 			return NULL;
919 		}
920 
921 		return ieee80211_clean_skb(origskb, present_fcs_len,
922 					   rtap_space);
923 	}
924 
925 	ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
926 
927 	list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
928 		struct cfg80211_chan_def *chandef;
929 
930 		chandef = &sdata->vif.bss_conf.chanreq.oper;
931 		if (chandef->chan &&
932 		    chandef->chan->center_freq != status->freq)
933 			continue;
934 
935 		if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR) &&
936 		    !ieee80211_validate_monitor_radio(sdata, local, status))
937 			continue;
938 
939 		if (!prev_sdata) {
940 			prev_sdata = sdata;
941 			continue;
942 		}
943 
944 		if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
945 			ieee80211_handle_mu_mimo_mon(sdata, origskb, rtap_space);
946 
947 		if (!monskb)
948 			monskb = ieee80211_make_monitor_skb(local, &origskb,
949 							    rate, rtap_space,
950 							    false);
951 		if (!monskb)
952 			continue;
953 
954 		skb = skb_clone(monskb, GFP_ATOMIC);
955 		if (!skb)
956 			continue;
957 
958 		skb->dev = prev_sdata->dev;
959 		dev_sw_netstats_rx_add(skb->dev, skb->len);
960 		netif_receive_skb(skb);
961 		prev_sdata = sdata;
962 	}
963 
964 	if (prev_sdata) {
965 		if (monskb)
966 			skb = monskb;
967 		else
968 			skb = ieee80211_make_monitor_skb(local, &origskb,
969 							 rate, rtap_space,
970 							 only_monitor);
971 		if (skb) {
972 			skb->dev = prev_sdata->dev;
973 			dev_sw_netstats_rx_add(skb->dev, skb->len);
974 			netif_receive_skb(skb);
975 		}
976 	}
977 
978 	if (!origskb)
979 		return NULL;
980 
981 	return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
982 }
983 
984 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
985 {
986 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
987 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
988 	int tid, seqno_idx, security_idx;
989 
990 	/* does the frame have a qos control field? */
991 	if (ieee80211_is_data_qos(hdr->frame_control)) {
992 		u8 *qc = ieee80211_get_qos_ctl(hdr);
993 		/* frame has qos control */
994 		tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
995 		if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
996 			status->rx_flags |= IEEE80211_RX_AMSDU;
997 
998 		seqno_idx = tid;
999 		security_idx = tid;
1000 	} else {
1001 		/*
1002 		 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
1003 		 *
1004 		 *	Sequence numbers for management frames, QoS data
1005 		 *	frames with a broadcast/multicast address in the
1006 		 *	Address 1 field, and all non-QoS data frames sent
1007 		 *	by QoS STAs are assigned using an additional single
1008 		 *	modulo-4096 counter, [...]
1009 		 *
1010 		 * We also use that counter for non-QoS STAs.
1011 		 */
1012 		seqno_idx = IEEE80211_NUM_TIDS;
1013 		security_idx = 0;
1014 		if (ieee80211_is_mgmt(hdr->frame_control))
1015 			security_idx = IEEE80211_NUM_TIDS;
1016 		tid = 0;
1017 	}
1018 
1019 	rx->seqno_idx = seqno_idx;
1020 	rx->security_idx = security_idx;
1021 	/* Set skb->priority to 1d tag if highest order bit of TID is not set.
1022 	 * For now, set skb->priority to 0 for other cases. */
1023 	rx->skb->priority = (tid > 7) ? 0 : tid;
1024 }
1025 
1026 /**
1027  * DOC: Packet alignment
1028  *
1029  * Drivers always need to pass packets that are aligned to two-byte boundaries
1030  * to the stack.
1031  *
1032  * Additionally, they should, if possible, align the payload data in a way that
1033  * guarantees that the contained IP header is aligned to a four-byte
1034  * boundary. In the case of regular frames, this simply means aligning the
1035  * payload to a four-byte boundary (because either the IP header is directly
1036  * contained, or IV/RFC1042 headers that have a length divisible by four are
1037  * in front of it).  If the payload data is not properly aligned and the
1038  * architecture doesn't support efficient unaligned operations, mac80211
1039  * will align the data.
1040  *
1041  * With A-MSDU frames, however, the payload data address must yield two modulo
1042  * four because there are 14-byte 802.3 headers within the A-MSDU frames that
1043  * push the IP header further back to a multiple of four again. Thankfully, the
1044  * specs were sane enough this time around to require padding each A-MSDU
1045  * subframe to a length that is a multiple of four.
1046  *
1047  * Padding like Atheros hardware adds which is between the 802.11 header and
1048  * the payload is not supported; the driver is required to move the 802.11
1049  * header to be directly in front of the payload in that case.
1050  */
1051 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
1052 {
1053 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1054 	WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
1055 #endif
1056 }
1057 
1058 
1059 /* rx handlers */
1060 
1061 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
1062 {
1063 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1064 
1065 	if (is_multicast_ether_addr(hdr->addr1))
1066 		return 0;
1067 
1068 	return ieee80211_is_robust_mgmt_frame(skb);
1069 }
1070 
1071 
1072 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
1073 {
1074 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1075 
1076 	if (!is_multicast_ether_addr(hdr->addr1))
1077 		return 0;
1078 
1079 	return ieee80211_is_robust_mgmt_frame(skb);
1080 }
1081 
1082 
1083 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
1084 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
1085 {
1086 	struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
1087 	struct ieee80211_mmie *mmie;
1088 	struct ieee80211_mmie_16 *mmie16;
1089 
1090 	if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
1091 		return -1;
1092 
1093 	if (!ieee80211_is_robust_mgmt_frame(skb) &&
1094 	    !ieee80211_is_beacon(hdr->frame_control))
1095 		return -1; /* not a robust management frame */
1096 
1097 	mmie = (struct ieee80211_mmie *)
1098 		(skb->data + skb->len - sizeof(*mmie));
1099 	if (mmie->element_id == WLAN_EID_MMIE &&
1100 	    mmie->length == sizeof(*mmie) - 2)
1101 		return le16_to_cpu(mmie->key_id);
1102 
1103 	mmie16 = (struct ieee80211_mmie_16 *)
1104 		(skb->data + skb->len - sizeof(*mmie16));
1105 	if (skb->len >= 24 + sizeof(*mmie16) &&
1106 	    mmie16->element_id == WLAN_EID_MMIE &&
1107 	    mmie16->length == sizeof(*mmie16) - 2)
1108 		return le16_to_cpu(mmie16->key_id);
1109 
1110 	return -1;
1111 }
1112 
1113 static int ieee80211_get_keyid(struct sk_buff *skb)
1114 {
1115 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1116 	__le16 fc = hdr->frame_control;
1117 	int hdrlen = ieee80211_hdrlen(fc);
1118 	u8 keyid;
1119 
1120 	/* WEP, TKIP, CCMP and GCMP */
1121 	if (unlikely(skb->len < hdrlen + IEEE80211_WEP_IV_LEN))
1122 		return -EINVAL;
1123 
1124 	skb_copy_bits(skb, hdrlen + 3, &keyid, 1);
1125 
1126 	keyid >>= 6;
1127 
1128 	return keyid;
1129 }
1130 
1131 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
1132 {
1133 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1134 	char *dev_addr = rx->sdata->vif.addr;
1135 
1136 	if (ieee80211_is_data(hdr->frame_control)) {
1137 		if (is_multicast_ether_addr(hdr->addr1)) {
1138 			if (ieee80211_has_tods(hdr->frame_control) ||
1139 			    !ieee80211_has_fromds(hdr->frame_control))
1140 				return RX_DROP_U_MESH_DS_BITS;
1141 			if (ether_addr_equal(hdr->addr3, dev_addr))
1142 				return RX_DROP_U_MESH_A3_MISMATCH;
1143 		} else {
1144 			if (!ieee80211_has_a4(hdr->frame_control))
1145 				return RX_DROP_U_MESH_NO_A4;
1146 			if (ether_addr_equal(hdr->addr4, dev_addr))
1147 				return RX_DROP_U_MESH_A4_MISMATCH;
1148 		}
1149 	}
1150 
1151 	/* If there is not an established peer link and this is not a peer link
1152 	 * establisment frame, beacon or probe, drop the frame.
1153 	 */
1154 
1155 	if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
1156 		struct ieee80211_mgmt *mgmt;
1157 
1158 		if (!ieee80211_is_mgmt(hdr->frame_control))
1159 			return RX_DROP_U_MESH_UNEXP_DATA;
1160 
1161 		if (ieee80211_is_action(hdr->frame_control)) {
1162 			u8 category;
1163 
1164 			/* make sure category field is present */
1165 			if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(category))
1166 				return RX_DROP_U_RUNT_ACTION;
1167 
1168 			mgmt = (struct ieee80211_mgmt *)hdr;
1169 			category = mgmt->u.action.category;
1170 			if (category != WLAN_CATEGORY_MESH_ACTION &&
1171 			    category != WLAN_CATEGORY_SELF_PROTECTED)
1172 				return RX_DROP_U_MESH_WRONG_ACTION;
1173 			return RX_CONTINUE;
1174 		}
1175 
1176 		if (ieee80211_is_probe_req(hdr->frame_control) ||
1177 		    ieee80211_is_probe_resp(hdr->frame_control) ||
1178 		    ieee80211_is_beacon(hdr->frame_control) ||
1179 		    ieee80211_is_auth(hdr->frame_control))
1180 			return RX_CONTINUE;
1181 
1182 		return RX_DROP_U_MESH_UNEXP_MGMT;
1183 	}
1184 
1185 	return RX_CONTINUE;
1186 }
1187 
1188 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1189 					      int index)
1190 {
1191 	struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1192 	struct sk_buff *tail = skb_peek_tail(frames);
1193 	struct ieee80211_rx_status *status;
1194 
1195 	if (tid_agg_rx->reorder_buf_filtered &&
1196 	    tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1197 		return true;
1198 
1199 	if (!tail)
1200 		return false;
1201 
1202 	status = IEEE80211_SKB_RXCB(tail);
1203 	if (status->flag & RX_FLAG_AMSDU_MORE)
1204 		return false;
1205 
1206 	return true;
1207 }
1208 
1209 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1210 					    struct tid_ampdu_rx *tid_agg_rx,
1211 					    int index,
1212 					    struct sk_buff_head *frames)
1213 {
1214 	struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1215 	struct sk_buff *skb;
1216 	struct ieee80211_rx_status *status;
1217 
1218 	lockdep_assert_held(&tid_agg_rx->reorder_lock);
1219 
1220 	if (skb_queue_empty(skb_list))
1221 		goto no_frame;
1222 
1223 	if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1224 		__skb_queue_purge(skb_list);
1225 		goto no_frame;
1226 	}
1227 
1228 	/* release frames from the reorder ring buffer */
1229 	tid_agg_rx->stored_mpdu_num--;
1230 	while ((skb = __skb_dequeue(skb_list))) {
1231 		status = IEEE80211_SKB_RXCB(skb);
1232 		status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1233 		__skb_queue_tail(frames, skb);
1234 	}
1235 
1236 no_frame:
1237 	if (tid_agg_rx->reorder_buf_filtered)
1238 		tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
1239 	tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1240 }
1241 
1242 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1243 					     struct tid_ampdu_rx *tid_agg_rx,
1244 					     u16 head_seq_num,
1245 					     struct sk_buff_head *frames)
1246 {
1247 	int index;
1248 
1249 	lockdep_assert_held(&tid_agg_rx->reorder_lock);
1250 
1251 	while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1252 		index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1253 		ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1254 						frames);
1255 	}
1256 }
1257 
1258 /*
1259  * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1260  * the skb was added to the buffer longer than this time ago, the earlier
1261  * frames that have not yet been received are assumed to be lost and the skb
1262  * can be released for processing. This may also release other skb's from the
1263  * reorder buffer if there are no additional gaps between the frames.
1264  *
1265  * Callers must hold tid_agg_rx->reorder_lock.
1266  */
1267 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1268 
1269 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
1270 					  struct tid_ampdu_rx *tid_agg_rx,
1271 					  struct sk_buff_head *frames)
1272 {
1273 	int index, i, j;
1274 
1275 	lockdep_assert_held(&tid_agg_rx->reorder_lock);
1276 
1277 	/* release the buffer until next missing frame */
1278 	index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1279 	if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
1280 	    tid_agg_rx->stored_mpdu_num) {
1281 		/*
1282 		 * No buffers ready to be released, but check whether any
1283 		 * frames in the reorder buffer have timed out.
1284 		 */
1285 		int skipped = 1;
1286 		for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1287 		     j = (j + 1) % tid_agg_rx->buf_size) {
1288 			if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
1289 				skipped++;
1290 				continue;
1291 			}
1292 			if (skipped &&
1293 			    !time_after(jiffies, tid_agg_rx->reorder_time[j] +
1294 					HT_RX_REORDER_BUF_TIMEOUT))
1295 				goto set_release_timer;
1296 
1297 			/* don't leave incomplete A-MSDUs around */
1298 			for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1299 			     i = (i + 1) % tid_agg_rx->buf_size)
1300 				__skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1301 
1302 			ht_dbg_ratelimited(sdata,
1303 					   "release an RX reorder frame due to timeout on earlier frames\n");
1304 			ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1305 							frames);
1306 
1307 			/*
1308 			 * Increment the head seq# also for the skipped slots.
1309 			 */
1310 			tid_agg_rx->head_seq_num =
1311 				(tid_agg_rx->head_seq_num +
1312 				 skipped) & IEEE80211_SN_MASK;
1313 			skipped = 0;
1314 		}
1315 	} else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1316 		ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1317 						frames);
1318 		index =	tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1319 	}
1320 
1321 	if (tid_agg_rx->stored_mpdu_num) {
1322 		j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1323 
1324 		for (; j != (index - 1) % tid_agg_rx->buf_size;
1325 		     j = (j + 1) % tid_agg_rx->buf_size) {
1326 			if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
1327 				break;
1328 		}
1329 
1330  set_release_timer:
1331 
1332 		if (!tid_agg_rx->removed)
1333 			mod_timer(&tid_agg_rx->reorder_timer,
1334 				  tid_agg_rx->reorder_time[j] + 1 +
1335 				  HT_RX_REORDER_BUF_TIMEOUT);
1336 	} else {
1337 		timer_delete(&tid_agg_rx->reorder_timer);
1338 	}
1339 }
1340 
1341 /*
1342  * As this function belongs to the RX path it must be under
1343  * rcu_read_lock protection. It returns false if the frame
1344  * can be processed immediately, true if it was consumed.
1345  */
1346 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1347 					     struct tid_ampdu_rx *tid_agg_rx,
1348 					     struct sk_buff *skb,
1349 					     struct sk_buff_head *frames)
1350 {
1351 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1352 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1353 	u16 mpdu_seq_num = ieee80211_get_sn(hdr);
1354 	u16 head_seq_num, buf_size;
1355 	int index;
1356 	bool ret = true;
1357 
1358 	spin_lock(&tid_agg_rx->reorder_lock);
1359 
1360 	/*
1361 	 * Offloaded BA sessions have no known starting sequence number so pick
1362 	 * one from first Rxed frame for this tid after BA was started.
1363 	 */
1364 	if (unlikely(tid_agg_rx->auto_seq)) {
1365 		tid_agg_rx->auto_seq = false;
1366 		tid_agg_rx->ssn = mpdu_seq_num;
1367 		tid_agg_rx->head_seq_num = mpdu_seq_num;
1368 	}
1369 
1370 	buf_size = tid_agg_rx->buf_size;
1371 	head_seq_num = tid_agg_rx->head_seq_num;
1372 
1373 	/*
1374 	 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1375 	 * be reordered.
1376 	 */
1377 	if (unlikely(!tid_agg_rx->started)) {
1378 		if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1379 			ret = false;
1380 			goto out;
1381 		}
1382 		tid_agg_rx->started = true;
1383 	}
1384 
1385 	/* frame with out of date sequence number */
1386 	if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1387 		dev_kfree_skb(skb);
1388 		goto out;
1389 	}
1390 
1391 	/*
1392 	 * If frame the sequence number exceeds our buffering window
1393 	 * size release some previous frames to make room for this one.
1394 	 */
1395 	if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1396 		head_seq_num = ieee80211_sn_inc(
1397 				ieee80211_sn_sub(mpdu_seq_num, buf_size));
1398 		/* release stored frames up to new head to stack */
1399 		ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1400 						 head_seq_num, frames);
1401 	}
1402 
1403 	/* Now the new frame is always in the range of the reordering buffer */
1404 
1405 	index = mpdu_seq_num % tid_agg_rx->buf_size;
1406 
1407 	/* check if we already stored this frame */
1408 	if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1409 		dev_kfree_skb(skb);
1410 		goto out;
1411 	}
1412 
1413 	/*
1414 	 * If the current MPDU is in the right order and nothing else
1415 	 * is stored we can process it directly, no need to buffer it.
1416 	 * If it is first but there's something stored, we may be able
1417 	 * to release frames after this one.
1418 	 */
1419 	if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1420 	    tid_agg_rx->stored_mpdu_num == 0) {
1421 		if (!(status->flag & RX_FLAG_AMSDU_MORE))
1422 			tid_agg_rx->head_seq_num =
1423 				ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1424 		ret = false;
1425 		goto out;
1426 	}
1427 
1428 	/* put the frame in the reordering buffer */
1429 	__skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1430 	if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1431 		tid_agg_rx->reorder_time[index] = jiffies;
1432 		tid_agg_rx->stored_mpdu_num++;
1433 		ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1434 	}
1435 
1436  out:
1437 	spin_unlock(&tid_agg_rx->reorder_lock);
1438 	return ret;
1439 }
1440 
1441 /*
1442  * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1443  * true if the MPDU was buffered, false if it should be processed.
1444  */
1445 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1446 				       struct sk_buff_head *frames)
1447 {
1448 	struct sk_buff *skb = rx->skb;
1449 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1450 	struct sta_info *sta = rx->sta;
1451 	struct tid_ampdu_rx *tid_agg_rx;
1452 	u16 sc;
1453 	u8 tid, ack_policy;
1454 
1455 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
1456 	    is_multicast_ether_addr(hdr->addr1))
1457 		goto dont_reorder;
1458 
1459 	/*
1460 	 * filter the QoS data rx stream according to
1461 	 * STA/TID and check if this STA/TID is on aggregation
1462 	 */
1463 
1464 	if (!sta)
1465 		goto dont_reorder;
1466 
1467 	ack_policy = *ieee80211_get_qos_ctl(hdr) &
1468 		     IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1469 	tid = ieee80211_get_tid(hdr);
1470 
1471 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1472 	if (!tid_agg_rx) {
1473 		if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1474 		    !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1475 		    !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1476 			ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1477 					     WLAN_BACK_RECIPIENT,
1478 					     WLAN_REASON_QSTA_REQUIRE_SETUP,
1479 					     ieee80211_s1g_use_ndp_ba(rx->sdata,
1480 								      rx->sta));
1481 		goto dont_reorder;
1482 	}
1483 
1484 	/* qos null data frames are excluded */
1485 	if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1486 		goto dont_reorder;
1487 
1488 	/* not part of a BA session */
1489 	if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
1490 		goto dont_reorder;
1491 
1492 	/* new, potentially un-ordered, ampdu frame - process it */
1493 
1494 	/* reset session timer */
1495 	if (tid_agg_rx->timeout)
1496 		tid_agg_rx->last_rx = jiffies;
1497 
1498 	/* if this mpdu is fragmented - terminate rx aggregation session */
1499 	sc = le16_to_cpu(hdr->seq_ctrl);
1500 	if (sc & IEEE80211_SCTL_FRAG) {
1501 		ieee80211_queue_skb_to_iface(rx->sdata, rx->link_id, NULL, skb);
1502 		return;
1503 	}
1504 
1505 	/*
1506 	 * No locking needed -- we will only ever process one
1507 	 * RX packet at a time, and thus own tid_agg_rx. All
1508 	 * other code manipulating it needs to (and does) make
1509 	 * sure that we cannot get to it any more before doing
1510 	 * anything with it.
1511 	 */
1512 	if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1513 					     frames))
1514 		return;
1515 
1516  dont_reorder:
1517 	__skb_queue_tail(frames, skb);
1518 }
1519 
1520 static ieee80211_rx_result debug_noinline
1521 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1522 {
1523 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1524 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1525 
1526 	if (status->flag & RX_FLAG_DUP_VALIDATED)
1527 		return RX_CONTINUE;
1528 
1529 	/*
1530 	 * Drop duplicate 802.11 retransmissions
1531 	 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1532 	 */
1533 
1534 	if (rx->skb->len < 24)
1535 		return RX_CONTINUE;
1536 
1537 	if (ieee80211_is_ctl(hdr->frame_control) ||
1538 	    ieee80211_is_any_nullfunc(hdr->frame_control))
1539 		return RX_CONTINUE;
1540 
1541 	if (!rx->sta)
1542 		return RX_CONTINUE;
1543 
1544 	if (unlikely(is_multicast_ether_addr(hdr->addr1))) {
1545 		struct ieee80211_sub_if_data *sdata = rx->sdata;
1546 		u16 sn = ieee80211_get_sn(hdr);
1547 
1548 		if (!ieee80211_is_data_present(hdr->frame_control))
1549 			return RX_CONTINUE;
1550 
1551 		if (!ieee80211_vif_is_mld(&sdata->vif) ||
1552 		    sdata->vif.type != NL80211_IFTYPE_STATION)
1553 			return RX_CONTINUE;
1554 
1555 		if (sdata->u.mgd.mcast_seq_last != IEEE80211_SN_MODULO &&
1556 		    ieee80211_sn_less_eq(sn, sdata->u.mgd.mcast_seq_last))
1557 			return RX_DROP_U_DUP;
1558 
1559 		sdata->u.mgd.mcast_seq_last = sn;
1560 		return RX_CONTINUE;
1561 	}
1562 
1563 	if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1564 		     rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1565 		I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1566 		rx->link_sta->rx_stats.num_duplicates++;
1567 		return RX_DROP_U_DUP;
1568 	} else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1569 		rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1570 	}
1571 
1572 	return RX_CONTINUE;
1573 }
1574 
1575 static ieee80211_rx_result debug_noinline
1576 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1577 {
1578 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1579 
1580 	/* Drop disallowed frame classes based on STA auth/assoc state;
1581 	 * IEEE 802.11, Chap 5.5.
1582 	 *
1583 	 * mac80211 filters only based on association state, i.e. it drops
1584 	 * Class 3 frames from not associated stations. hostapd sends
1585 	 * deauth/disassoc frames when needed. In addition, hostapd is
1586 	 * responsible for filtering on both auth and assoc states.
1587 	 */
1588 
1589 	if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1590 		return ieee80211_rx_mesh_check(rx);
1591 
1592 	if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1593 		      ieee80211_is_pspoll(hdr->frame_control)) &&
1594 		     rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1595 		     rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1596 		     (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1597 		/*
1598 		 * accept port control frames from the AP even when it's not
1599 		 * yet marked ASSOC to prevent a race where we don't set the
1600 		 * assoc bit quickly enough before it sends the first frame
1601 		 */
1602 		if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1603 		    ieee80211_is_data_present(hdr->frame_control)) {
1604 			unsigned int hdrlen;
1605 			__be16 ethertype;
1606 
1607 			hdrlen = ieee80211_hdrlen(hdr->frame_control);
1608 
1609 			if (rx->skb->len < hdrlen + 8)
1610 				return RX_DROP_U_RUNT_DATA;
1611 
1612 			skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1613 			if (ethertype == rx->sdata->control_port_protocol)
1614 				return RX_CONTINUE;
1615 		}
1616 
1617 		if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1618 		    cfg80211_rx_spurious_frame(rx->sdata->dev, hdr->addr2,
1619 					       rx->link_id, GFP_ATOMIC))
1620 			return RX_DROP_U_SPURIOUS_NOTIF;
1621 
1622 		return RX_DROP_U_SPURIOUS;
1623 	}
1624 
1625 	return RX_CONTINUE;
1626 }
1627 
1628 
1629 static ieee80211_rx_result debug_noinline
1630 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1631 {
1632 	struct ieee80211_local *local;
1633 	struct ieee80211_hdr *hdr;
1634 	struct sk_buff *skb;
1635 
1636 	local = rx->local;
1637 	skb = rx->skb;
1638 	hdr = (struct ieee80211_hdr *) skb->data;
1639 
1640 	if (!local->pspolling)
1641 		return RX_CONTINUE;
1642 
1643 	if (!ieee80211_has_fromds(hdr->frame_control))
1644 		/* this is not from AP */
1645 		return RX_CONTINUE;
1646 
1647 	if (!ieee80211_is_data(hdr->frame_control))
1648 		return RX_CONTINUE;
1649 
1650 	if (!ieee80211_has_moredata(hdr->frame_control)) {
1651 		/* AP has no more frames buffered for us */
1652 		local->pspolling = false;
1653 		return RX_CONTINUE;
1654 	}
1655 
1656 	/* more data bit is set, let's request a new frame from the AP */
1657 	ieee80211_send_pspoll(local, rx->sdata);
1658 
1659 	return RX_CONTINUE;
1660 }
1661 
1662 static void sta_ps_start(struct sta_info *sta)
1663 {
1664 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1665 	struct ieee80211_local *local = sdata->local;
1666 	struct ps_data *ps;
1667 	int tid;
1668 
1669 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1670 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1671 		ps = &sdata->bss->ps;
1672 	else
1673 		return;
1674 
1675 	atomic_inc(&ps->num_sta_ps);
1676 	set_sta_flag(sta, WLAN_STA_PS_STA);
1677 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1678 		drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1679 	ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1680 	       sta->sta.addr, sta->sta.aid);
1681 
1682 	ieee80211_clear_fast_xmit(sta);
1683 
1684 	for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
1685 		struct ieee80211_txq *txq = sta->sta.txq[tid];
1686 		struct txq_info *txqi = to_txq_info(txq);
1687 
1688 		spin_lock(&local->active_txq_lock[txq->ac]);
1689 		if (!list_empty(&txqi->schedule_order))
1690 			list_del_init(&txqi->schedule_order);
1691 		spin_unlock(&local->active_txq_lock[txq->ac]);
1692 
1693 		if (txq_has_queue(txq))
1694 			set_bit(tid, &sta->txq_buffered_tids);
1695 		else
1696 			clear_bit(tid, &sta->txq_buffered_tids);
1697 	}
1698 }
1699 
1700 static void sta_ps_end(struct sta_info *sta)
1701 {
1702 	ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1703 	       sta->sta.addr, sta->sta.aid);
1704 
1705 	if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1706 		/*
1707 		 * Clear the flag only if the other one is still set
1708 		 * so that the TX path won't start TX'ing new frames
1709 		 * directly ... In the case that the driver flag isn't
1710 		 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1711 		 */
1712 		clear_sta_flag(sta, WLAN_STA_PS_STA);
1713 		ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1714 		       sta->sta.addr, sta->sta.aid);
1715 		return;
1716 	}
1717 
1718 	set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1719 	clear_sta_flag(sta, WLAN_STA_PS_STA);
1720 	ieee80211_sta_ps_deliver_wakeup(sta);
1721 }
1722 
1723 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1724 {
1725 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1726 	bool in_ps;
1727 
1728 	WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1729 
1730 	/* Don't let the same PS state be set twice */
1731 	in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1732 	if ((start && in_ps) || (!start && !in_ps))
1733 		return -EINVAL;
1734 
1735 	if (start)
1736 		sta_ps_start(sta);
1737 	else
1738 		sta_ps_end(sta);
1739 
1740 	return 0;
1741 }
1742 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1743 
1744 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1745 {
1746 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1747 
1748 	if (test_sta_flag(sta, WLAN_STA_SP))
1749 		return;
1750 
1751 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1752 		ieee80211_sta_ps_deliver_poll_response(sta);
1753 	else
1754 		set_sta_flag(sta, WLAN_STA_PSPOLL);
1755 }
1756 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1757 
1758 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1759 {
1760 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1761 	int ac = ieee80211_ac_from_tid(tid);
1762 
1763 	/*
1764 	 * If this AC is not trigger-enabled do nothing unless the
1765 	 * driver is calling us after it already checked.
1766 	 *
1767 	 * NB: This could/should check a separate bitmap of trigger-
1768 	 * enabled queues, but for now we only implement uAPSD w/o
1769 	 * TSPEC changes to the ACs, so they're always the same.
1770 	 */
1771 	if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1772 	    tid != IEEE80211_NUM_TIDS)
1773 		return;
1774 
1775 	/* if we are in a service period, do nothing */
1776 	if (test_sta_flag(sta, WLAN_STA_SP))
1777 		return;
1778 
1779 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1780 		ieee80211_sta_ps_deliver_uapsd(sta);
1781 	else
1782 		set_sta_flag(sta, WLAN_STA_UAPSD);
1783 }
1784 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1785 
1786 static ieee80211_rx_result debug_noinline
1787 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1788 {
1789 	struct ieee80211_sub_if_data *sdata = rx->sdata;
1790 	struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1791 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1792 
1793 	if (!rx->sta)
1794 		return RX_CONTINUE;
1795 
1796 	if (sdata->vif.type != NL80211_IFTYPE_AP &&
1797 	    sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1798 		return RX_CONTINUE;
1799 
1800 	/*
1801 	 * The device handles station powersave, so don't do anything about
1802 	 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1803 	 * it to mac80211 since they're handled.)
1804 	 */
1805 	if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1806 		return RX_CONTINUE;
1807 
1808 	/*
1809 	 * Don't do anything if the station isn't already asleep. In
1810 	 * the uAPSD case, the station will probably be marked asleep,
1811 	 * in the PS-Poll case the station must be confused ...
1812 	 */
1813 	if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1814 		return RX_CONTINUE;
1815 
1816 	if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1817 		ieee80211_sta_pspoll(&rx->sta->sta);
1818 
1819 		/* Free PS Poll skb here instead of returning RX_DROP that would
1820 		 * count as an dropped frame. */
1821 		dev_kfree_skb(rx->skb);
1822 
1823 		return RX_QUEUED;
1824 	} else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1825 		   !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1826 		   ieee80211_has_pm(hdr->frame_control) &&
1827 		   (ieee80211_is_data_qos(hdr->frame_control) ||
1828 		    ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1829 		u8 tid = ieee80211_get_tid(hdr);
1830 
1831 		ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1832 	}
1833 
1834 	return RX_CONTINUE;
1835 }
1836 
1837 static ieee80211_rx_result debug_noinline
1838 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1839 {
1840 	struct sta_info *sta = rx->sta;
1841 	struct link_sta_info *link_sta = rx->link_sta;
1842 	struct sk_buff *skb = rx->skb;
1843 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1844 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1845 	int i;
1846 
1847 	if (!sta || !link_sta)
1848 		return RX_CONTINUE;
1849 
1850 	/*
1851 	 * Update last_rx only for IBSS packets which are for the current
1852 	 * BSSID and for station already AUTHORIZED to avoid keeping the
1853 	 * current IBSS network alive in cases where other STAs start
1854 	 * using different BSSID. This will also give the station another
1855 	 * chance to restart the authentication/authorization in case
1856 	 * something went wrong the first time.
1857 	 */
1858 	if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1859 		u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1860 						NL80211_IFTYPE_ADHOC);
1861 		if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1862 		    test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1863 			link_sta->rx_stats.last_rx = jiffies;
1864 			if (ieee80211_is_data_present(hdr->frame_control) &&
1865 			    !is_multicast_ether_addr(hdr->addr1))
1866 				link_sta->rx_stats.last_rate =
1867 					sta_stats_encode_rate(status);
1868 		}
1869 	} else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1870 		link_sta->rx_stats.last_rx = jiffies;
1871 	} else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1872 		   !is_multicast_ether_addr(hdr->addr1)) {
1873 		/*
1874 		 * Mesh beacons will update last_rx when if they are found to
1875 		 * match the current local configuration when processed.
1876 		 */
1877 		link_sta->rx_stats.last_rx = jiffies;
1878 		if (ieee80211_is_data_present(hdr->frame_control))
1879 			link_sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1880 	}
1881 
1882 	link_sta->rx_stats.fragments++;
1883 
1884 	u64_stats_update_begin(&link_sta->rx_stats.syncp);
1885 	u64_stats_add(&link_sta->rx_stats.bytes, rx->skb->len);
1886 	u64_stats_update_end(&link_sta->rx_stats.syncp);
1887 
1888 	if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1889 		link_sta->rx_stats.last_signal = status->signal;
1890 		ewma_signal_add(&link_sta->rx_stats_avg.signal,
1891 				-status->signal);
1892 	}
1893 
1894 	if (status->chains) {
1895 		link_sta->rx_stats.chains = status->chains;
1896 		for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1897 			int signal = status->chain_signal[i];
1898 
1899 			if (!(status->chains & BIT(i)))
1900 				continue;
1901 
1902 			link_sta->rx_stats.chain_signal_last[i] = signal;
1903 			ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
1904 					-signal);
1905 		}
1906 	}
1907 
1908 	if (ieee80211_is_s1g_beacon(hdr->frame_control))
1909 		return RX_CONTINUE;
1910 
1911 	/*
1912 	 * Change STA power saving mode only at the end of a frame
1913 	 * exchange sequence, and only for a data or management
1914 	 * frame as specified in IEEE 802.11-2016 11.2.3.2
1915 	 */
1916 	if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1917 	    !ieee80211_has_morefrags(hdr->frame_control) &&
1918 	    !is_multicast_ether_addr(hdr->addr1) &&
1919 	    (ieee80211_is_mgmt(hdr->frame_control) ||
1920 	     ieee80211_is_data(hdr->frame_control)) &&
1921 	    !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1922 	    (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1923 	     rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1924 		if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1925 			if (!ieee80211_has_pm(hdr->frame_control))
1926 				sta_ps_end(sta);
1927 		} else {
1928 			if (ieee80211_has_pm(hdr->frame_control))
1929 				sta_ps_start(sta);
1930 		}
1931 	}
1932 
1933 	/* mesh power save support */
1934 	if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1935 		ieee80211_mps_rx_h_sta_process(sta, hdr);
1936 
1937 	/*
1938 	 * Drop (qos-)data::nullfunc frames silently, since they
1939 	 * are used only to control station power saving mode.
1940 	 */
1941 	if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1942 		I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1943 
1944 		/*
1945 		 * If we receive a 4-addr nullfunc frame from a STA
1946 		 * that was not moved to a 4-addr STA vlan yet send
1947 		 * the event to userspace and for older hostapd drop
1948 		 * the frame to the monitor interface.
1949 		 */
1950 		if (ieee80211_has_a4(hdr->frame_control) &&
1951 		    (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1952 		     (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1953 		      !rx->sdata->u.vlan.sta))) {
1954 			if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1955 				cfg80211_rx_unexpected_4addr_frame(
1956 					rx->sdata->dev, sta->sta.addr,
1957 					rx->link_id, GFP_ATOMIC);
1958 			return RX_DROP_U_UNEXPECTED_4ADDR_FRAME;
1959 		}
1960 		/*
1961 		 * Update counter and free packet here to avoid
1962 		 * counting this as a dropped packed.
1963 		 */
1964 		link_sta->rx_stats.packets++;
1965 		dev_kfree_skb(rx->skb);
1966 		return RX_QUEUED;
1967 	}
1968 
1969 	return RX_CONTINUE;
1970 } /* ieee80211_rx_h_sta_process */
1971 
1972 static struct ieee80211_key *
1973 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1974 {
1975 	struct ieee80211_key *key = NULL;
1976 	int idx2;
1977 
1978 	/* Make sure key gets set if either BIGTK key index is set so that
1979 	 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1980 	 * Beacon frames and Beacon frames that claim to use another BIGTK key
1981 	 * index (i.e., a key that we do not have).
1982 	 */
1983 
1984 	if (idx < 0) {
1985 		idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1986 		idx2 = idx + 1;
1987 	} else {
1988 		if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1989 			idx2 = idx + 1;
1990 		else
1991 			idx2 = idx - 1;
1992 	}
1993 
1994 	if (rx->link_sta)
1995 		key = rcu_dereference(rx->link_sta->gtk[idx]);
1996 	if (!key)
1997 		key = rcu_dereference(rx->link->gtk[idx]);
1998 	if (!key && rx->link_sta)
1999 		key = rcu_dereference(rx->link_sta->gtk[idx2]);
2000 	if (!key)
2001 		key = rcu_dereference(rx->link->gtk[idx2]);
2002 
2003 	return key;
2004 }
2005 
2006 static ieee80211_rx_result debug_noinline
2007 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
2008 {
2009 	struct sk_buff *skb = rx->skb;
2010 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2011 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2012 	int keyidx;
2013 	ieee80211_rx_result result = RX_DROP_U_DECRYPT_FAIL;
2014 	struct ieee80211_key *sta_ptk = NULL;
2015 	struct ieee80211_key *ptk_idx = NULL;
2016 	int mmie_keyidx = -1;
2017 	__le16 fc;
2018 
2019 	if (ieee80211_is_ext(hdr->frame_control))
2020 		return RX_CONTINUE;
2021 
2022 	/*
2023 	 * Key selection 101
2024 	 *
2025 	 * There are five types of keys:
2026 	 *  - GTK (group keys)
2027 	 *  - IGTK (group keys for management frames)
2028 	 *  - BIGTK (group keys for Beacon frames)
2029 	 *  - PTK (pairwise keys)
2030 	 *  - STK (station-to-station pairwise keys)
2031 	 *
2032 	 * When selecting a key, we have to distinguish between multicast
2033 	 * (including broadcast) and unicast frames, the latter can only
2034 	 * use PTKs and STKs while the former always use GTKs, IGTKs, and
2035 	 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
2036 	 * then unicast frames can also use key indices like GTKs. Hence, if we
2037 	 * don't have a PTK/STK we check the key index for a WEP key.
2038 	 *
2039 	 * Note that in a regular BSS, multicast frames are sent by the
2040 	 * AP only, associated stations unicast the frame to the AP first
2041 	 * which then multicasts it on their behalf.
2042 	 *
2043 	 * There is also a slight problem in IBSS mode: GTKs are negotiated
2044 	 * with each station, that is something we don't currently handle.
2045 	 * The spec seems to expect that one negotiates the same key with
2046 	 * every station but there's no such requirement; VLANs could be
2047 	 * possible.
2048 	 */
2049 
2050 	/* start without a key */
2051 	rx->key = NULL;
2052 	fc = hdr->frame_control;
2053 
2054 	if (rx->sta) {
2055 		int keyid = rx->sta->ptk_idx;
2056 		sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
2057 
2058 		if (ieee80211_has_protected(fc) &&
2059 		    !(status->flag & RX_FLAG_IV_STRIPPED)) {
2060 			keyid = ieee80211_get_keyid(rx->skb);
2061 
2062 			if (unlikely(keyid < 0))
2063 				return RX_DROP_U_NO_KEY_ID;
2064 
2065 			ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
2066 		}
2067 	}
2068 
2069 	if (!ieee80211_has_protected(fc))
2070 		mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
2071 
2072 	if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
2073 		rx->key = ptk_idx ? ptk_idx : sta_ptk;
2074 		if ((status->flag & RX_FLAG_DECRYPTED) &&
2075 		    (status->flag & RX_FLAG_IV_STRIPPED))
2076 			return RX_CONTINUE;
2077 		/* Skip decryption if the frame is not protected. */
2078 		if (!ieee80211_has_protected(fc))
2079 			return RX_CONTINUE;
2080 	} else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
2081 		/* Broadcast/multicast robust management frame / BIP */
2082 		if ((status->flag & RX_FLAG_DECRYPTED) &&
2083 		    (status->flag & RX_FLAG_IV_STRIPPED))
2084 			return RX_CONTINUE;
2085 
2086 		if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
2087 		    mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
2088 				   NUM_DEFAULT_BEACON_KEYS) {
2089 			if (rx->sdata->dev)
2090 				cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2091 							     skb->data,
2092 							     skb->len);
2093 			return RX_DROP_U_BAD_BCN_KEYIDX;
2094 		}
2095 
2096 		rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
2097 		if (!rx->key)
2098 			return RX_CONTINUE; /* Beacon protection not in use */
2099 	} else if (mmie_keyidx >= 0) {
2100 		/* Broadcast/multicast robust management frame / BIP */
2101 		if ((status->flag & RX_FLAG_DECRYPTED) &&
2102 		    (status->flag & RX_FLAG_IV_STRIPPED))
2103 			return RX_CONTINUE;
2104 
2105 		if (mmie_keyidx < NUM_DEFAULT_KEYS ||
2106 		    mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
2107 			return RX_DROP_U_BAD_MGMT_KEYIDX; /* unexpected BIP keyidx */
2108 		if (rx->link_sta) {
2109 			if (ieee80211_is_group_privacy_action(skb) &&
2110 			    test_sta_flag(rx->sta, WLAN_STA_MFP))
2111 				return RX_DROP_U_UNPROTECTED;
2112 
2113 			rx->key = rcu_dereference(rx->link_sta->gtk[mmie_keyidx]);
2114 		}
2115 		if (!rx->key)
2116 			rx->key = rcu_dereference(rx->link->gtk[mmie_keyidx]);
2117 	} else if (!ieee80211_has_protected(fc)) {
2118 		/*
2119 		 * The frame was not protected, so skip decryption. However, we
2120 		 * need to set rx->key if there is a key that could have been
2121 		 * used so that the frame may be dropped if encryption would
2122 		 * have been expected.
2123 		 */
2124 		struct ieee80211_key *key = NULL;
2125 		int i;
2126 
2127 		if (ieee80211_is_beacon(fc)) {
2128 			key = ieee80211_rx_get_bigtk(rx, -1);
2129 		} else if (ieee80211_is_mgmt(fc) &&
2130 			   is_multicast_ether_addr(hdr->addr1)) {
2131 			key = rcu_dereference(rx->link->default_mgmt_key);
2132 		} else {
2133 			if (rx->link_sta) {
2134 				for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2135 					key = rcu_dereference(rx->link_sta->gtk[i]);
2136 					if (key)
2137 						break;
2138 				}
2139 			}
2140 			if (!key) {
2141 				for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2142 					key = rcu_dereference(rx->link->gtk[i]);
2143 					if (key)
2144 						break;
2145 				}
2146 			}
2147 		}
2148 		if (key)
2149 			rx->key = key;
2150 		return RX_CONTINUE;
2151 	} else {
2152 		/*
2153 		 * The device doesn't give us the IV so we won't be
2154 		 * able to look up the key. That's ok though, we
2155 		 * don't need to decrypt the frame, we just won't
2156 		 * be able to keep statistics accurate.
2157 		 * Except for key threshold notifications, should
2158 		 * we somehow allow the driver to tell us which key
2159 		 * the hardware used if this flag is set?
2160 		 */
2161 		if ((status->flag & RX_FLAG_DECRYPTED) &&
2162 		    (status->flag & RX_FLAG_IV_STRIPPED))
2163 			return RX_CONTINUE;
2164 
2165 		keyidx = ieee80211_get_keyid(rx->skb);
2166 
2167 		if (unlikely(keyidx < 0))
2168 			return RX_DROP_U_NO_KEY_ID;
2169 
2170 		/* check per-station GTK first, if multicast packet */
2171 		if (is_multicast_ether_addr(hdr->addr1) && rx->link_sta)
2172 			rx->key = rcu_dereference(rx->link_sta->gtk[keyidx]);
2173 
2174 		/* if not found, try default key */
2175 		if (!rx->key) {
2176 			if (is_multicast_ether_addr(hdr->addr1))
2177 				rx->key = rcu_dereference(rx->link->gtk[keyidx]);
2178 			if (!rx->key)
2179 				rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2180 
2181 			/*
2182 			 * RSNA-protected unicast frames should always be
2183 			 * sent with pairwise or station-to-station keys,
2184 			 * but for WEP we allow using a key index as well.
2185 			 */
2186 			if (rx->key &&
2187 			    rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2188 			    rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2189 			    !is_multicast_ether_addr(hdr->addr1))
2190 				rx->key = NULL;
2191 		}
2192 	}
2193 
2194 	if (rx->key) {
2195 		if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2196 			return RX_DROP_U_KEY_TAINTED;
2197 
2198 		/* TODO: add threshold stuff again */
2199 	} else {
2200 		return RX_DROP_U_UNPROTECTED;
2201 	}
2202 
2203 	switch (rx->key->conf.cipher) {
2204 	case WLAN_CIPHER_SUITE_WEP40:
2205 	case WLAN_CIPHER_SUITE_WEP104:
2206 		result = ieee80211_crypto_wep_decrypt(rx);
2207 		break;
2208 	case WLAN_CIPHER_SUITE_TKIP:
2209 		result = ieee80211_crypto_tkip_decrypt(rx);
2210 		break;
2211 	case WLAN_CIPHER_SUITE_CCMP:
2212 		result = ieee80211_crypto_ccmp_decrypt(
2213 			rx, IEEE80211_CCMP_MIC_LEN);
2214 		break;
2215 	case WLAN_CIPHER_SUITE_CCMP_256:
2216 		result = ieee80211_crypto_ccmp_decrypt(
2217 			rx, IEEE80211_CCMP_256_MIC_LEN);
2218 		break;
2219 	case WLAN_CIPHER_SUITE_AES_CMAC:
2220 		result = ieee80211_crypto_aes_cmac_decrypt(
2221 			rx, IEEE80211_CMAC_128_MIC_LEN);
2222 		break;
2223 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2224 		result = ieee80211_crypto_aes_cmac_decrypt(
2225 			rx, IEEE80211_CMAC_256_MIC_LEN);
2226 		break;
2227 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2228 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2229 		result = ieee80211_crypto_aes_gmac_decrypt(rx);
2230 		break;
2231 	case WLAN_CIPHER_SUITE_GCMP:
2232 	case WLAN_CIPHER_SUITE_GCMP_256:
2233 		result = ieee80211_crypto_gcmp_decrypt(rx);
2234 		break;
2235 	default:
2236 		result = RX_DROP_U_BAD_CIPHER;
2237 	}
2238 
2239 	/* the hdr variable is invalid after the decrypt handlers */
2240 
2241 	/* either the frame has been decrypted or will be dropped */
2242 	status->flag |= RX_FLAG_DECRYPTED;
2243 
2244 	if (unlikely(ieee80211_is_beacon(fc) && RX_RES_IS_UNUSABLE(result) &&
2245 		     rx->sdata->dev))
2246 		cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2247 					     skb->data, skb->len);
2248 
2249 	return result;
2250 }
2251 
2252 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2253 {
2254 	int i;
2255 
2256 	for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2257 		skb_queue_head_init(&cache->entries[i].skb_list);
2258 }
2259 
2260 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2261 {
2262 	int i;
2263 
2264 	for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2265 		__skb_queue_purge(&cache->entries[i].skb_list);
2266 }
2267 
2268 static inline struct ieee80211_fragment_entry *
2269 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
2270 			 unsigned int frag, unsigned int seq, int rx_queue,
2271 			 struct sk_buff **skb)
2272 {
2273 	struct ieee80211_fragment_entry *entry;
2274 
2275 	entry = &cache->entries[cache->next++];
2276 	if (cache->next >= IEEE80211_FRAGMENT_MAX)
2277 		cache->next = 0;
2278 
2279 	__skb_queue_purge(&entry->skb_list);
2280 
2281 	__skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2282 	*skb = NULL;
2283 	entry->first_frag_time = jiffies;
2284 	entry->seq = seq;
2285 	entry->rx_queue = rx_queue;
2286 	entry->last_frag = frag;
2287 	entry->check_sequential_pn = false;
2288 	entry->extra_len = 0;
2289 
2290 	return entry;
2291 }
2292 
2293 static inline struct ieee80211_fragment_entry *
2294 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2295 			  unsigned int frag, unsigned int seq,
2296 			  int rx_queue, struct ieee80211_hdr *hdr)
2297 {
2298 	struct ieee80211_fragment_entry *entry;
2299 	int i, idx;
2300 
2301 	idx = cache->next;
2302 	for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2303 		struct ieee80211_hdr *f_hdr;
2304 		struct sk_buff *f_skb;
2305 
2306 		idx--;
2307 		if (idx < 0)
2308 			idx = IEEE80211_FRAGMENT_MAX - 1;
2309 
2310 		entry = &cache->entries[idx];
2311 		if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2312 		    entry->rx_queue != rx_queue ||
2313 		    entry->last_frag + 1 != frag)
2314 			continue;
2315 
2316 		f_skb = __skb_peek(&entry->skb_list);
2317 		f_hdr = (struct ieee80211_hdr *) f_skb->data;
2318 
2319 		/*
2320 		 * Check ftype and addresses are equal, else check next fragment
2321 		 */
2322 		if (((hdr->frame_control ^ f_hdr->frame_control) &
2323 		     cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2324 		    !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2325 		    !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2326 			continue;
2327 
2328 		if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2329 			__skb_queue_purge(&entry->skb_list);
2330 			continue;
2331 		}
2332 		return entry;
2333 	}
2334 
2335 	return NULL;
2336 }
2337 
2338 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2339 {
2340 	return rx->key &&
2341 		(rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2342 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2343 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2344 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2345 		ieee80211_has_protected(fc);
2346 }
2347 
2348 static ieee80211_rx_result debug_noinline
2349 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2350 {
2351 	struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2352 	struct ieee80211_hdr *hdr;
2353 	u16 sc;
2354 	__le16 fc;
2355 	unsigned int frag, seq;
2356 	struct ieee80211_fragment_entry *entry;
2357 	struct sk_buff *skb;
2358 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2359 
2360 	hdr = (struct ieee80211_hdr *)rx->skb->data;
2361 	fc = hdr->frame_control;
2362 
2363 	if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2364 		return RX_CONTINUE;
2365 
2366 	sc = le16_to_cpu(hdr->seq_ctrl);
2367 	frag = sc & IEEE80211_SCTL_FRAG;
2368 
2369 	if (rx->sta)
2370 		cache = &rx->sta->frags;
2371 
2372 	if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2373 		goto out;
2374 
2375 	if (is_multicast_ether_addr(hdr->addr1))
2376 		return RX_DROP_U_MCAST_FRAGMENT;
2377 
2378 	I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2379 
2380 	if (skb_linearize(rx->skb))
2381 		return RX_DROP_U_OOM;
2382 
2383 	/*
2384 	 *  skb_linearize() might change the skb->data and
2385 	 *  previously cached variables (in this case, hdr) need to
2386 	 *  be refreshed with the new data.
2387 	 */
2388 	hdr = (struct ieee80211_hdr *)rx->skb->data;
2389 	seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2390 
2391 	if (frag == 0) {
2392 		/* This is the first fragment of a new frame. */
2393 		entry = ieee80211_reassemble_add(cache, frag, seq,
2394 						 rx->seqno_idx, &(rx->skb));
2395 		if (requires_sequential_pn(rx, fc)) {
2396 			int queue = rx->security_idx;
2397 
2398 			/* Store CCMP/GCMP PN so that we can verify that the
2399 			 * next fragment has a sequential PN value.
2400 			 */
2401 			entry->check_sequential_pn = true;
2402 			entry->is_protected = true;
2403 			entry->key_color = rx->key->color;
2404 			memcpy(entry->last_pn,
2405 			       rx->key->u.ccmp.rx_pn[queue],
2406 			       IEEE80211_CCMP_PN_LEN);
2407 			BUILD_BUG_ON(offsetof(struct ieee80211_key,
2408 					      u.ccmp.rx_pn) !=
2409 				     offsetof(struct ieee80211_key,
2410 					      u.gcmp.rx_pn));
2411 			BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2412 				     sizeof(rx->key->u.gcmp.rx_pn[queue]));
2413 			BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2414 				     IEEE80211_GCMP_PN_LEN);
2415 		} else if (rx->key &&
2416 			   (ieee80211_has_protected(fc) ||
2417 			    (status->flag & RX_FLAG_DECRYPTED))) {
2418 			entry->is_protected = true;
2419 			entry->key_color = rx->key->color;
2420 		}
2421 		return RX_QUEUED;
2422 	}
2423 
2424 	/* This is a fragment for a frame that should already be pending in
2425 	 * fragment cache. Add this fragment to the end of the pending entry.
2426 	 */
2427 	entry = ieee80211_reassemble_find(cache, frag, seq,
2428 					  rx->seqno_idx, hdr);
2429 	if (!entry) {
2430 		I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2431 		return RX_DROP_U_DEFRAG_MISMATCH;
2432 	}
2433 
2434 	/* "The receiver shall discard MSDUs and MMPDUs whose constituent
2435 	 *  MPDU PN values are not incrementing in steps of 1."
2436 	 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2437 	 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2438 	 */
2439 	if (entry->check_sequential_pn) {
2440 		int i;
2441 		u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2442 
2443 		if (!requires_sequential_pn(rx, fc))
2444 			return RX_DROP_U_NONSEQ_PN;
2445 
2446 		/* Prevent mixed key and fragment cache attacks */
2447 		if (entry->key_color != rx->key->color)
2448 			return RX_DROP_U_BAD_KEY_COLOR;
2449 
2450 		memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2451 		for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2452 			pn[i]++;
2453 			if (pn[i])
2454 				break;
2455 		}
2456 
2457 		rpn = rx->ccm_gcm.pn;
2458 		if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2459 			return RX_DROP_U_REPLAY;
2460 		memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2461 	} else if (entry->is_protected &&
2462 		   (!rx->key ||
2463 		    (!ieee80211_has_protected(fc) &&
2464 		     !(status->flag & RX_FLAG_DECRYPTED)) ||
2465 		    rx->key->color != entry->key_color)) {
2466 		/* Drop this as a mixed key or fragment cache attack, even
2467 		 * if for TKIP Michael MIC should protect us, and WEP is a
2468 		 * lost cause anyway.
2469 		 */
2470 		return RX_DROP_U_EXPECT_DEFRAG_PROT;
2471 	} else if (entry->is_protected && rx->key &&
2472 		   entry->key_color != rx->key->color &&
2473 		   (status->flag & RX_FLAG_DECRYPTED)) {
2474 		return RX_DROP_U_BAD_KEY_COLOR;
2475 	}
2476 
2477 	skb_pull(rx->skb, ieee80211_hdrlen(fc));
2478 	__skb_queue_tail(&entry->skb_list, rx->skb);
2479 	entry->last_frag = frag;
2480 	entry->extra_len += rx->skb->len;
2481 	if (ieee80211_has_morefrags(fc)) {
2482 		rx->skb = NULL;
2483 		return RX_QUEUED;
2484 	}
2485 
2486 	rx->skb = __skb_dequeue(&entry->skb_list);
2487 	if (skb_tailroom(rx->skb) < entry->extra_len) {
2488 		I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2489 		if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2490 					      GFP_ATOMIC))) {
2491 			I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2492 			__skb_queue_purge(&entry->skb_list);
2493 			return RX_DROP_U_OOM;
2494 		}
2495 	}
2496 	while ((skb = __skb_dequeue(&entry->skb_list))) {
2497 		skb_put_data(rx->skb, skb->data, skb->len);
2498 		dev_kfree_skb(skb);
2499 	}
2500 
2501  out:
2502 	ieee80211_led_rx(rx->local);
2503 	if (rx->sta)
2504 		rx->link_sta->rx_stats.packets++;
2505 	return RX_CONTINUE;
2506 }
2507 
2508 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2509 {
2510 	if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2511 		return -EACCES;
2512 
2513 	return 0;
2514 }
2515 
2516 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2517 {
2518 	struct sk_buff *skb = rx->skb;
2519 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2520 
2521 	/*
2522 	 * Pass through unencrypted frames if the hardware has
2523 	 * decrypted them already.
2524 	 */
2525 	if (status->flag & RX_FLAG_DECRYPTED)
2526 		return 0;
2527 
2528 	/* Drop unencrypted frames if key is set. */
2529 	if (unlikely(!ieee80211_has_protected(fc) &&
2530 		     !ieee80211_is_any_nullfunc(fc) &&
2531 		     ieee80211_is_data(fc) && rx->key))
2532 		return -EACCES;
2533 
2534 	return 0;
2535 }
2536 
2537 VISIBLE_IF_MAC80211_KUNIT ieee80211_rx_result
2538 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2539 {
2540 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2541 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2542 	__le16 fc = mgmt->frame_control;
2543 
2544 	/*
2545 	 * Pass through unencrypted frames if the hardware has
2546 	 * decrypted them already.
2547 	 */
2548 	if (status->flag & RX_FLAG_DECRYPTED)
2549 		return RX_CONTINUE;
2550 
2551 	/* drop unicast protected dual (that wasn't protected) */
2552 	if (ieee80211_is_action(fc) &&
2553 	    mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
2554 		return RX_DROP_U_UNPROT_DUAL;
2555 
2556 	if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2557 		if (unlikely(!ieee80211_has_protected(fc) &&
2558 			     ieee80211_is_unicast_robust_mgmt_frame(rx->skb))) {
2559 			if (ieee80211_is_deauth(fc) ||
2560 			    ieee80211_is_disassoc(fc)) {
2561 				/*
2562 				 * Permit unprotected deauth/disassoc frames
2563 				 * during 4-way-HS (key is installed after HS).
2564 				 */
2565 				if (!rx->key)
2566 					return RX_CONTINUE;
2567 
2568 				cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2569 							     rx->skb->data,
2570 							     rx->skb->len);
2571 			}
2572 			return RX_DROP_U_UNPROT_UCAST_MGMT;
2573 		}
2574 		/* BIP does not use Protected field, so need to check MMIE */
2575 		if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2576 			     ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2577 			if (ieee80211_is_deauth(fc) ||
2578 			    ieee80211_is_disassoc(fc))
2579 				cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2580 							     rx->skb->data,
2581 							     rx->skb->len);
2582 			return RX_DROP_U_UNPROT_MCAST_MGMT;
2583 		}
2584 		if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2585 			     ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2586 			cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2587 						     rx->skb->data,
2588 						     rx->skb->len);
2589 			return RX_DROP_U_UNPROT_BEACON;
2590 		}
2591 		/*
2592 		 * When using MFP, Action frames are not allowed prior to
2593 		 * having configured keys.
2594 		 */
2595 		if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2596 			     ieee80211_is_robust_mgmt_frame(rx->skb)))
2597 			return RX_DROP_U_UNPROT_ACTION;
2598 
2599 		/* drop unicast public action frames when using MPF */
2600 		if (is_unicast_ether_addr(mgmt->da) &&
2601 		    ieee80211_is_protected_dual_of_public_action(rx->skb))
2602 			return RX_DROP_U_UNPROT_UNICAST_PUB_ACTION;
2603 	}
2604 
2605 	/*
2606 	 * Drop robust action frames before assoc regardless of MFP state,
2607 	 * after assoc we also have decided on MFP or not.
2608 	 */
2609 	if (ieee80211_is_action(fc) &&
2610 	    ieee80211_is_robust_mgmt_frame(rx->skb) &&
2611 	    (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))
2612 		return RX_DROP_U_UNPROT_ROBUST_ACTION;
2613 
2614 	/*
2615 	 * Drop unprotected (Re)Association Request/Response frame received from
2616 	 * an EPP Peer.
2617 	 */
2618 	if (!ieee80211_has_protected(fc) &&
2619 	    ieee80211_require_encrypted_assoc(fc, rx->sta))
2620 		return RX_DROP_U_UNPROT_UCAST_MGMT;
2621 
2622 	return RX_CONTINUE;
2623 }
2624 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_drop_unencrypted_mgmt);
2625 
2626 static ieee80211_rx_result
2627 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2628 {
2629 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2630 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2631 	bool check_port_control = false;
2632 	struct ethhdr *ehdr;
2633 	int ret;
2634 
2635 	*port_control = false;
2636 	if (ieee80211_has_a4(hdr->frame_control) &&
2637 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2638 		return RX_DROP_U_UNEXPECTED_VLAN_4ADDR;
2639 
2640 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2641 	    !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2642 		if (!sdata->u.mgd.use_4addr)
2643 			return RX_DROP_U_UNEXPECTED_STA_4ADDR;
2644 		else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2645 			check_port_control = true;
2646 	}
2647 
2648 	if (is_multicast_ether_addr(hdr->addr1) &&
2649 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2650 		return RX_DROP_U_UNEXPECTED_VLAN_MCAST;
2651 
2652 	ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2653 	if (ret < 0)
2654 		return RX_DROP_U_INVALID_8023;
2655 
2656 	ehdr = (struct ethhdr *) rx->skb->data;
2657 	if (ehdr->h_proto == rx->sdata->control_port_protocol)
2658 		*port_control = true;
2659 	else if (check_port_control)
2660 		return RX_DROP_U_NOT_PORT_CONTROL;
2661 
2662 	return RX_CONTINUE;
2663 }
2664 
2665 bool ieee80211_is_our_addr(struct ieee80211_sub_if_data *sdata,
2666 			   const u8 *addr, int *out_link_id)
2667 {
2668 	unsigned int link_id;
2669 
2670 	/* non-MLO, or MLD address replaced by hardware */
2671 	if (ether_addr_equal(sdata->vif.addr, addr))
2672 		return true;
2673 
2674 	if (!ieee80211_vif_is_mld(&sdata->vif))
2675 		return false;
2676 
2677 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->vif.link_conf); link_id++) {
2678 		struct ieee80211_bss_conf *conf;
2679 
2680 		conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2681 
2682 		if (!conf)
2683 			continue;
2684 		if (ether_addr_equal(conf->addr, addr)) {
2685 			if (out_link_id)
2686 				*out_link_id = link_id;
2687 			return true;
2688 		}
2689 	}
2690 
2691 	return false;
2692 }
2693 
2694 /*
2695  * requires that rx->skb is a frame with ethernet header
2696  */
2697 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2698 {
2699 	static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2700 		= { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2701 	struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2702 
2703 	/*
2704 	 * Allow EAPOL frames to us/the PAE group address regardless of
2705 	 * whether the frame was encrypted or not, and always disallow
2706 	 * all other destination addresses for them.
2707 	 */
2708 	if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2709 		return ieee80211_is_our_addr(rx->sdata, ehdr->h_dest, NULL) ||
2710 		       ether_addr_equal(ehdr->h_dest, pae_group_addr);
2711 
2712 	if (ieee80211_802_1x_port_control(rx) ||
2713 	    ieee80211_drop_unencrypted(rx, fc))
2714 		return false;
2715 
2716 	return true;
2717 }
2718 
2719 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2720 						 struct ieee80211_rx_data *rx)
2721 {
2722 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2723 	struct net_device *dev = sdata->dev;
2724 
2725 	if (unlikely((skb->protocol == sdata->control_port_protocol ||
2726 		     (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2727 		      !sdata->control_port_no_preauth)) &&
2728 		     sdata->control_port_over_nl80211)) {
2729 		struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2730 		bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2731 
2732 		cfg80211_rx_control_port(dev, skb, noencrypt, rx->link_id);
2733 		dev_kfree_skb(skb);
2734 	} else {
2735 		struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2736 
2737 		memset(skb->cb, 0, sizeof(skb->cb));
2738 
2739 		/*
2740 		 * 802.1X over 802.11 requires that the authenticator address
2741 		 * be used for EAPOL frames. However, 802.1X allows the use of
2742 		 * the PAE group address instead. If the interface is part of
2743 		 * a bridge and we pass the frame with the PAE group address,
2744 		 * then the bridge will forward it to the network (even if the
2745 		 * client was not associated yet), which isn't supposed to
2746 		 * happen.
2747 		 * To avoid that, rewrite the destination address to our own
2748 		 * address, so that the authenticator (e.g. hostapd) will see
2749 		 * the frame, but bridge won't forward it anywhere else. Note
2750 		 * that due to earlier filtering, the only other address can
2751 		 * be the PAE group address, unless the hardware allowed them
2752 		 * through in 802.3 offloaded mode.
2753 		 */
2754 		if (unlikely(skb->protocol == sdata->control_port_protocol &&
2755 			     !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2756 			ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2757 
2758 		/* deliver to local stack */
2759 		if (rx->list)
2760 			list_add_tail(&skb->list, rx->list);
2761 		else
2762 			netif_receive_skb(skb);
2763 	}
2764 }
2765 
2766 /*
2767  * requires that rx->skb is a frame with ethernet header
2768  */
2769 static void
2770 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2771 {
2772 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2773 	struct net_device *dev = sdata->dev;
2774 	struct sk_buff *skb, *xmit_skb;
2775 	struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2776 	struct sta_info *dsta;
2777 
2778 	skb = rx->skb;
2779 	xmit_skb = NULL;
2780 
2781 	dev_sw_netstats_rx_add(dev, skb->len);
2782 
2783 	if (rx->sta) {
2784 		/* The seqno index has the same property as needed
2785 		 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2786 		 * for non-QoS-data frames. Here we know it's a data
2787 		 * frame, so count MSDUs.
2788 		 */
2789 		u64_stats_update_begin(&rx->link_sta->rx_stats.syncp);
2790 		u64_stats_inc(&rx->link_sta->rx_stats.msdu[rx->seqno_idx]);
2791 		u64_stats_update_end(&rx->link_sta->rx_stats.syncp);
2792 	}
2793 
2794 	if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2795 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2796 	    !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2797 	    ehdr->h_proto != rx->sdata->control_port_protocol &&
2798 	    (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2799 		if (is_multicast_ether_addr(ehdr->h_dest) &&
2800 		    ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2801 			/*
2802 			 * send multicast frames both to higher layers in
2803 			 * local net stack and back to the wireless medium
2804 			 */
2805 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
2806 			if (!xmit_skb)
2807 				net_info_ratelimited("%s: failed to clone multicast frame\n",
2808 						    dev->name);
2809 		} else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2810 			   !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2811 			dsta = sta_info_get(sdata, ehdr->h_dest);
2812 			if (dsta) {
2813 				/*
2814 				 * The destination station is associated to
2815 				 * this AP (in this VLAN), so send the frame
2816 				 * directly to it and do not pass it to local
2817 				 * net stack.
2818 				 */
2819 				xmit_skb = skb;
2820 				skb = NULL;
2821 			}
2822 		}
2823 	}
2824 
2825 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2826 	if (skb) {
2827 		/* 'align' will only take the values 0 or 2 here since all
2828 		 * frames are required to be aligned to 2-byte boundaries
2829 		 * when being passed to mac80211; the code here works just
2830 		 * as well if that isn't true, but mac80211 assumes it can
2831 		 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2832 		 */
2833 		int align;
2834 
2835 		align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2836 		if (align) {
2837 			if (WARN_ON(skb_headroom(skb) < 3)) {
2838 				dev_kfree_skb(skb);
2839 				skb = NULL;
2840 			} else {
2841 				u8 *data = skb->data;
2842 				size_t len = skb_headlen(skb);
2843 				skb->data -= align;
2844 				memmove(skb->data, data, len);
2845 				skb_set_tail_pointer(skb, len);
2846 			}
2847 		}
2848 	}
2849 #endif
2850 
2851 	if (skb) {
2852 		skb->protocol = eth_type_trans(skb, dev);
2853 		ieee80211_deliver_skb_to_local_stack(skb, rx);
2854 	}
2855 
2856 	if (xmit_skb) {
2857 		/*
2858 		 * Send to wireless media and increase priority by 256 to
2859 		 * keep the received priority instead of reclassifying
2860 		 * the frame (see cfg80211_classify8021d).
2861 		 */
2862 		xmit_skb->priority += 256;
2863 		xmit_skb->protocol = htons(ETH_P_802_3);
2864 		skb_reset_network_header(xmit_skb);
2865 		skb_reset_mac_header(xmit_skb);
2866 		dev_queue_xmit(xmit_skb);
2867 	}
2868 }
2869 
2870 #ifdef CONFIG_MAC80211_MESH
2871 static bool
2872 ieee80211_rx_mesh_fast_forward(struct ieee80211_sub_if_data *sdata,
2873 			       struct sk_buff *skb, int hdrlen)
2874 {
2875 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2876 	struct ieee80211_mesh_fast_tx_key key = {
2877 		.type = MESH_FAST_TX_TYPE_FORWARDED
2878 	};
2879 	struct ieee80211_mesh_fast_tx *entry;
2880 	struct ieee80211s_hdr *mesh_hdr;
2881 	struct tid_ampdu_tx *tid_tx;
2882 	struct sta_info *sta;
2883 	struct ethhdr eth;
2884 	u8 tid;
2885 
2886 	mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(eth));
2887 	if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2888 		ether_addr_copy(key.addr, mesh_hdr->eaddr1);
2889 	else if (!(mesh_hdr->flags & MESH_FLAGS_AE))
2890 		ether_addr_copy(key.addr, skb->data);
2891 	else
2892 		return false;
2893 
2894 	entry = mesh_fast_tx_get(sdata, &key);
2895 	if (!entry)
2896 		return false;
2897 
2898 	sta = rcu_dereference(entry->mpath->next_hop);
2899 	if (!sta)
2900 		return false;
2901 
2902 	if (skb_linearize(skb))
2903 		return false;
2904 
2905 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2906 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
2907 	if (tid_tx) {
2908 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
2909 			return false;
2910 
2911 		if (tid_tx->timeout)
2912 			tid_tx->last_tx = jiffies;
2913 	}
2914 
2915 	ieee80211_aggr_check(sdata, sta, skb);
2916 
2917 	if (ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
2918 					    &skb->protocol))
2919 		hdrlen += ETH_ALEN;
2920 	else
2921 		skb->protocol = htons(skb->len - hdrlen);
2922 	skb_set_network_header(skb, hdrlen + 2);
2923 
2924 	skb->dev = sdata->dev;
2925 	memcpy(&eth, skb->data, ETH_HLEN - 2);
2926 	skb_pull(skb, 2);
2927 	__ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
2928 			      eth.h_dest, eth.h_source);
2929 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2930 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2931 
2932 	return true;
2933 }
2934 #endif
2935 
2936 static ieee80211_rx_result
2937 ieee80211_rx_mesh_data(struct ieee80211_sub_if_data *sdata, struct sta_info *sta,
2938 		       struct sk_buff *skb)
2939 {
2940 #ifdef CONFIG_MAC80211_MESH
2941 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2942 	struct ieee80211_local *local = sdata->local;
2943 	uint16_t fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA;
2944 	struct ieee80211_hdr hdr = {
2945 		.frame_control = cpu_to_le16(fc)
2946 	};
2947 	struct ieee80211_hdr *fwd_hdr;
2948 	struct ieee80211s_hdr *mesh_hdr;
2949 	struct ieee80211_tx_info *info;
2950 	struct sk_buff *fwd_skb;
2951 	struct ethhdr *eth;
2952 	bool multicast;
2953 	int tailroom = 0;
2954 	int hdrlen, mesh_hdrlen;
2955 	u8 *qos;
2956 
2957 	if (!ieee80211_vif_is_mesh(&sdata->vif))
2958 		return RX_CONTINUE;
2959 
2960 	if (!pskb_may_pull(skb, sizeof(*eth) + 6))
2961 		return RX_DROP_U_RUNT_MESH_DATA;
2962 
2963 	mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(*eth));
2964 	mesh_hdrlen = ieee80211_get_mesh_hdrlen(mesh_hdr);
2965 
2966 	if (!pskb_may_pull(skb, sizeof(*eth) + mesh_hdrlen))
2967 		return RX_DROP_U_RUNT_MESH_DATA;
2968 
2969 	eth = (struct ethhdr *)skb->data;
2970 	multicast = is_multicast_ether_addr(eth->h_dest);
2971 
2972 	mesh_hdr = (struct ieee80211s_hdr *)(eth + 1);
2973 	if (!mesh_hdr->ttl)
2974 		return RX_DROP_U_MESH_NO_TTL;
2975 
2976 	/* frame is in RMC, don't forward */
2977 	if (is_multicast_ether_addr(eth->h_dest) &&
2978 	    mesh_rmc_check(sdata, eth->h_source, mesh_hdr))
2979 		return RX_DROP_U_MESH_RMC;
2980 
2981 	/* forward packet */
2982 	if (sdata->crypto_tx_tailroom_needed_cnt)
2983 		tailroom = IEEE80211_ENCRYPT_TAILROOM;
2984 
2985 	if (mesh_hdr->flags & MESH_FLAGS_AE) {
2986 		struct mesh_path *mppath;
2987 		char *proxied_addr;
2988 		bool update = false;
2989 
2990 		if (multicast)
2991 			proxied_addr = mesh_hdr->eaddr1;
2992 		else if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2993 			/* has_a4 already checked in ieee80211_rx_mesh_check */
2994 			proxied_addr = mesh_hdr->eaddr2;
2995 		else
2996 			return RX_DROP_U_MESH_BAD_AE;
2997 
2998 		rcu_read_lock();
2999 		mppath = mpp_path_lookup(sdata, proxied_addr);
3000 		if (!mppath) {
3001 			mpp_path_add(sdata, proxied_addr, eth->h_source);
3002 		} else {
3003 			spin_lock_bh(&mppath->state_lock);
3004 			if (!ether_addr_equal(mppath->mpp, eth->h_source)) {
3005 				memcpy(mppath->mpp, eth->h_source, ETH_ALEN);
3006 				update = true;
3007 			}
3008 			mppath->exp_time = jiffies;
3009 			spin_unlock_bh(&mppath->state_lock);
3010 		}
3011 
3012 		/* flush fast xmit cache if the address path changed */
3013 		if (update)
3014 			mesh_fast_tx_flush_addr(sdata, proxied_addr);
3015 
3016 		rcu_read_unlock();
3017 	}
3018 
3019 	/* Frame has reached destination.  Don't forward */
3020 	if (ether_addr_equal(sdata->vif.addr, eth->h_dest))
3021 		goto rx_accept;
3022 
3023 	if (!--mesh_hdr->ttl) {
3024 		if (multicast)
3025 			goto rx_accept;
3026 
3027 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
3028 		return RX_DROP_U_MESH_TTL_EXPIRED;
3029 	}
3030 
3031 	if (!ifmsh->mshcfg.dot11MeshForwarding) {
3032 		if (is_multicast_ether_addr(eth->h_dest))
3033 			goto rx_accept;
3034 
3035 		return RX_DROP_U_MESH_NOT_FORWARDING;
3036 	}
3037 
3038 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[skb->priority]);
3039 
3040 	if (!multicast &&
3041 	    ieee80211_rx_mesh_fast_forward(sdata, skb, mesh_hdrlen))
3042 		return RX_QUEUED;
3043 
3044 	ieee80211_fill_mesh_addresses(&hdr, &hdr.frame_control,
3045 				      eth->h_dest, eth->h_source);
3046 	hdrlen = ieee80211_hdrlen(hdr.frame_control);
3047 	if (multicast) {
3048 		int extra_head = sizeof(struct ieee80211_hdr) - sizeof(*eth);
3049 
3050 		fwd_skb = skb_copy_expand(skb, local->tx_headroom + extra_head +
3051 					       IEEE80211_ENCRYPT_HEADROOM,
3052 					  tailroom, GFP_ATOMIC);
3053 		if (!fwd_skb)
3054 			goto rx_accept;
3055 	} else {
3056 		fwd_skb = skb;
3057 		skb = NULL;
3058 
3059 		if (skb_cow_head(fwd_skb, hdrlen - sizeof(struct ethhdr)))
3060 			return RX_DROP_U_OOM;
3061 
3062 		if (skb_linearize(fwd_skb))
3063 			return RX_DROP_U_OOM;
3064 	}
3065 
3066 	fwd_hdr = skb_push(fwd_skb, hdrlen - sizeof(struct ethhdr));
3067 	memcpy(fwd_hdr, &hdr, hdrlen - 2);
3068 	qos = ieee80211_get_qos_ctl(fwd_hdr);
3069 	qos[0] = qos[1] = 0;
3070 
3071 	skb_reset_mac_header(fwd_skb);
3072 	hdrlen += mesh_hdrlen;
3073 	if (ieee80211_get_8023_tunnel_proto(fwd_skb->data + hdrlen,
3074 					    &fwd_skb->protocol))
3075 		hdrlen += ETH_ALEN;
3076 	else
3077 		fwd_skb->protocol = htons(fwd_skb->len - hdrlen);
3078 	skb_set_network_header(fwd_skb, hdrlen + 2);
3079 
3080 	info = IEEE80211_SKB_CB(fwd_skb);
3081 	memset(info, 0, sizeof(*info));
3082 	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3083 	info->control.vif = &sdata->vif;
3084 	info->control.jiffies = jiffies;
3085 	fwd_skb->dev = sdata->dev;
3086 	if (multicast) {
3087 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
3088 		memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
3089 		/* update power mode indication when forwarding */
3090 		ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
3091 	} else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
3092 		/* mesh power mode flags updated in mesh_nexthop_lookup */
3093 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
3094 	} else {
3095 		/* unable to resolve next hop */
3096 		if (sta)
3097 			mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
3098 					   hdr.addr3, 0,
3099 					   WLAN_REASON_MESH_PATH_NOFORWARD,
3100 					   sta->sta.addr);
3101 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
3102 		kfree_skb(fwd_skb);
3103 		goto rx_accept;
3104 	}
3105 
3106 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
3107 	ieee80211_set_qos_hdr(sdata, fwd_skb);
3108 	ieee80211_add_pending_skb(local, fwd_skb);
3109 
3110 rx_accept:
3111 	if (!skb)
3112 		return RX_QUEUED;
3113 
3114 	ieee80211_strip_8023_mesh_hdr(skb);
3115 #endif
3116 
3117 	return RX_CONTINUE;
3118 }
3119 
3120 static ieee80211_rx_result debug_noinline
3121 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
3122 {
3123 	struct net_device *dev = rx->sdata->dev;
3124 	struct sk_buff *skb = rx->skb;
3125 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3126 	__le16 fc = hdr->frame_control;
3127 	struct sk_buff_head frame_list;
3128 	struct ethhdr ethhdr;
3129 	const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
3130 
3131 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3132 		check_da = NULL;
3133 		check_sa = NULL;
3134 	} else switch (rx->sdata->vif.type) {
3135 		case NL80211_IFTYPE_AP:
3136 		case NL80211_IFTYPE_AP_VLAN:
3137 			check_da = NULL;
3138 			break;
3139 		case NL80211_IFTYPE_STATION:
3140 			if (!test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
3141 				check_sa = NULL;
3142 			break;
3143 		case NL80211_IFTYPE_MESH_POINT:
3144 			check_sa = NULL;
3145 			check_da = NULL;
3146 			break;
3147 		default:
3148 			break;
3149 	}
3150 
3151 	skb->dev = dev;
3152 	__skb_queue_head_init(&frame_list);
3153 
3154 	if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
3155 					  rx->sdata->vif.addr,
3156 					  rx->sdata->vif.type,
3157 					  data_offset, true))
3158 		return RX_DROP_U_BAD_AMSDU;
3159 
3160 	if (rx->sta->amsdu_mesh_control < 0) {
3161 		s8 valid = -1;
3162 		int i;
3163 
3164 		for (i = 0; i <= 2; i++) {
3165 			if (!ieee80211_is_valid_amsdu(skb, i))
3166 				continue;
3167 
3168 			if (valid >= 0) {
3169 				/* ambiguous */
3170 				valid = -1;
3171 				break;
3172 			}
3173 
3174 			valid = i;
3175 		}
3176 
3177 		rx->sta->amsdu_mesh_control = valid;
3178 	}
3179 
3180 	ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
3181 				 rx->sdata->vif.type,
3182 				 rx->local->hw.extra_tx_headroom,
3183 				 check_da, check_sa,
3184 				 rx->sta->amsdu_mesh_control);
3185 
3186 	while (!skb_queue_empty(&frame_list)) {
3187 		rx->skb = __skb_dequeue(&frame_list);
3188 
3189 		switch (ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb)) {
3190 		case RX_QUEUED:
3191 			break;
3192 		case RX_CONTINUE:
3193 			if (ieee80211_frame_allowed(rx, fc)) {
3194 				ieee80211_deliver_skb(rx);
3195 				break;
3196 			}
3197 			fallthrough;
3198 		default:
3199 			dev_kfree_skb(rx->skb);
3200 		}
3201 	}
3202 
3203 	return RX_QUEUED;
3204 }
3205 
3206 static ieee80211_rx_result debug_noinline
3207 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
3208 {
3209 	struct sk_buff *skb = rx->skb;
3210 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3211 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3212 	__le16 fc = hdr->frame_control;
3213 
3214 	if (!(status->rx_flags & IEEE80211_RX_AMSDU))
3215 		return RX_CONTINUE;
3216 
3217 	if (unlikely(!ieee80211_is_data(fc)))
3218 		return RX_CONTINUE;
3219 
3220 	if (unlikely(!ieee80211_is_data_present(fc)))
3221 		return RX_DROP_U_AMSDU_WITHOUT_DATA;
3222 
3223 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3224 		switch (rx->sdata->vif.type) {
3225 		case NL80211_IFTYPE_AP_VLAN:
3226 			if (!rx->sdata->u.vlan.sta)
3227 				return RX_DROP_U_BAD_4ADDR;
3228 			break;
3229 		case NL80211_IFTYPE_STATION:
3230 			if (!rx->sdata->u.mgd.use_4addr)
3231 				return RX_DROP_U_BAD_4ADDR;
3232 			break;
3233 		case NL80211_IFTYPE_MESH_POINT:
3234 			break;
3235 		default:
3236 			return RX_DROP_U_BAD_4ADDR;
3237 		}
3238 	}
3239 
3240 	if (is_multicast_ether_addr(hdr->addr1) || !rx->sta)
3241 		return RX_DROP_U_BAD_AMSDU;
3242 
3243 	if (rx->key) {
3244 		/*
3245 		 * We should not receive A-MSDUs on pre-HT connections,
3246 		 * and HT connections cannot use old ciphers. Thus drop
3247 		 * them, as in those cases we couldn't even have SPP
3248 		 * A-MSDUs or such.
3249 		 */
3250 		switch (rx->key->conf.cipher) {
3251 		case WLAN_CIPHER_SUITE_WEP40:
3252 		case WLAN_CIPHER_SUITE_WEP104:
3253 		case WLAN_CIPHER_SUITE_TKIP:
3254 			return RX_DROP_U_BAD_AMSDU_CIPHER;
3255 		default:
3256 			break;
3257 		}
3258 	}
3259 
3260 	return __ieee80211_rx_h_amsdu(rx, 0);
3261 }
3262 
3263 static ieee80211_rx_result debug_noinline
3264 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
3265 {
3266 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3267 	struct ieee80211_local *local = rx->local;
3268 	struct net_device *dev = sdata->dev;
3269 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
3270 	__le16 fc = hdr->frame_control;
3271 	ieee80211_rx_result res;
3272 	bool port_control;
3273 
3274 	if (unlikely(!ieee80211_is_data(hdr->frame_control)))
3275 		return RX_CONTINUE;
3276 
3277 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3278 		return RX_DROP_U_NULL_DATA;
3279 
3280 	/* Send unexpected-4addr-frame event to hostapd */
3281 	if (ieee80211_has_a4(hdr->frame_control) &&
3282 	    sdata->vif.type == NL80211_IFTYPE_AP) {
3283 		if (rx->sta &&
3284 		    !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3285 			cfg80211_rx_unexpected_4addr_frame(
3286 				rx->sdata->dev, rx->sta->sta.addr, rx->link_id,
3287 				GFP_ATOMIC);
3288 		return RX_DROP_U_UNEXPECTED_4ADDR;
3289 	}
3290 
3291 	res = __ieee80211_data_to_8023(rx, &port_control);
3292 	if (unlikely(res != RX_CONTINUE))
3293 		return res;
3294 
3295 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3296 	if (res != RX_CONTINUE)
3297 		return res;
3298 
3299 	if (!ieee80211_frame_allowed(rx, fc))
3300 		return RX_DROP_U_PORT_CONTROL;
3301 
3302 	/* directly handle TDLS channel switch requests/responses */
3303 	if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3304 						cpu_to_be16(ETH_P_TDLS))) {
3305 		struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3306 
3307 		if (pskb_may_pull(rx->skb,
3308 				  offsetof(struct ieee80211_tdls_data, u)) &&
3309 		    tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3310 		    tf->category == WLAN_CATEGORY_TDLS &&
3311 		    (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3312 		     tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3313 			rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3314 			__ieee80211_queue_skb_to_iface(sdata, rx->link_id,
3315 						       rx->sta, rx->skb);
3316 			return RX_QUEUED;
3317 		}
3318 	}
3319 
3320 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3321 	    unlikely(port_control) && sdata->bss) {
3322 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3323 				     u.ap);
3324 		dev = sdata->dev;
3325 		rx->sdata = sdata;
3326 	}
3327 
3328 	rx->skb->dev = dev;
3329 
3330 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3331 	    local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3332 	    !is_multicast_ether_addr(
3333 		    ((struct ethhdr *)rx->skb->data)->h_dest) &&
3334 	    (!local->scanning &&
3335 	     !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3336 		mod_timer(&local->dynamic_ps_timer, jiffies +
3337 			  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3338 
3339 	ieee80211_deliver_skb(rx);
3340 
3341 	return RX_QUEUED;
3342 }
3343 
3344 static ieee80211_rx_result debug_noinline
3345 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3346 {
3347 	struct sk_buff *skb = rx->skb;
3348 	struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3349 	struct tid_ampdu_rx *tid_agg_rx;
3350 	u16 start_seq_num;
3351 	u16 tid;
3352 
3353 	if (likely(!ieee80211_is_ctl(bar->frame_control)))
3354 		return RX_CONTINUE;
3355 
3356 	if (ieee80211_is_back_req(bar->frame_control)) {
3357 		struct {
3358 			__le16 control, start_seq_num;
3359 		} __packed bar_data;
3360 		struct ieee80211_event event = {
3361 			.type = BAR_RX_EVENT,
3362 		};
3363 
3364 		if (!rx->sta)
3365 			return RX_DROP_U_UNKNOWN_STA;
3366 
3367 		if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3368 				  &bar_data, sizeof(bar_data)))
3369 			return RX_DROP_U_RUNT_BAR;
3370 
3371 		tid = le16_to_cpu(bar_data.control) >> 12;
3372 
3373 		if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3374 		    !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3375 			ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3376 					     WLAN_BACK_RECIPIENT,
3377 					     WLAN_REASON_QSTA_REQUIRE_SETUP,
3378 					     ieee80211_s1g_use_ndp_ba(rx->sdata,
3379 								      rx->sta));
3380 
3381 		tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3382 		if (!tid_agg_rx)
3383 			return RX_DROP_U_BAR_OUTSIDE_SESSION;
3384 
3385 		start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3386 		event.u.ba.tid = tid;
3387 		event.u.ba.ssn = start_seq_num;
3388 		event.u.ba.sta = &rx->sta->sta;
3389 
3390 		/* reset session timer */
3391 		if (tid_agg_rx->timeout)
3392 			mod_timer(&tid_agg_rx->session_timer,
3393 				  TU_TO_EXP_TIME(tid_agg_rx->timeout));
3394 
3395 		spin_lock(&tid_agg_rx->reorder_lock);
3396 		/* release stored frames up to start of BAR */
3397 		ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3398 						 start_seq_num, frames);
3399 		spin_unlock(&tid_agg_rx->reorder_lock);
3400 
3401 		drv_event_callback(rx->local, rx->sdata, &event);
3402 
3403 		kfree_skb(skb);
3404 		return RX_QUEUED;
3405 	}
3406 
3407 	return RX_DROP_U_CTRL_FRAME;
3408 }
3409 
3410 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3411 					   struct ieee80211_mgmt *mgmt,
3412 					   size_t len)
3413 {
3414 	struct ieee80211_local *local = sdata->local;
3415 	struct sk_buff *skb;
3416 	struct ieee80211_mgmt *resp;
3417 
3418 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3419 		/* Not to own unicast address */
3420 		return;
3421 	}
3422 
3423 	if (!ether_addr_equal(mgmt->sa, sdata->vif.cfg.ap_addr) ||
3424 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
3425 		/* Not from the current AP or not associated yet. */
3426 		return;
3427 	}
3428 
3429 	if (len < IEEE80211_MIN_ACTION_SIZE(sa_query)) {
3430 		/* Too short SA Query request frame */
3431 		return;
3432 	}
3433 
3434 	skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3435 	if (skb == NULL)
3436 		return;
3437 
3438 	skb_reserve(skb, local->hw.extra_tx_headroom);
3439 	resp = skb_put_zero(skb, IEEE80211_MIN_ACTION_SIZE(sa_query));
3440 	memcpy(resp->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
3441 	memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3442 	memcpy(resp->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
3443 	resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3444 					  IEEE80211_STYPE_ACTION);
3445 	resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3446 	resp->u.action.action_code = WLAN_ACTION_SA_QUERY_RESPONSE;
3447 	memcpy(resp->u.action.sa_query.trans_id,
3448 	       mgmt->u.action.sa_query.trans_id,
3449 	       WLAN_SA_QUERY_TR_ID_LEN);
3450 
3451 	ieee80211_tx_skb(sdata, skb);
3452 }
3453 
3454 static void
3455 ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx)
3456 {
3457 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3458 	struct ieee80211_bss_conf *bss_conf;
3459 	const struct element *ie;
3460 	size_t baselen;
3461 
3462 	if (!wiphy_ext_feature_isset(rx->local->hw.wiphy,
3463 				     NL80211_EXT_FEATURE_BSS_COLOR))
3464 		return;
3465 
3466 	if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION))
3467 		return;
3468 
3469 	bss_conf = rx->link->conf;
3470 	if (bss_conf->csa_active || bss_conf->color_change_active ||
3471 	    !bss_conf->he_bss_color.enabled)
3472 		return;
3473 
3474 	baselen = mgmt->u.beacon.variable - rx->skb->data;
3475 	if (baselen > rx->skb->len)
3476 		return;
3477 
3478 	ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
3479 				    mgmt->u.beacon.variable,
3480 				    rx->skb->len - baselen);
3481 	if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) &&
3482 	    ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) {
3483 		const struct ieee80211_he_operation *he_oper;
3484 		u8 color;
3485 
3486 		he_oper = (void *)(ie->data + 1);
3487 		if (le32_get_bits(he_oper->he_oper_params,
3488 				  IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED))
3489 			return;
3490 
3491 		color = le32_get_bits(he_oper->he_oper_params,
3492 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3493 		if (color == bss_conf->he_bss_color.color)
3494 			ieee80211_obss_color_collision_notify(&rx->sdata->vif,
3495 							      BIT_ULL(color),
3496 							      bss_conf->link_id);
3497 	}
3498 }
3499 
3500 static ieee80211_rx_result debug_noinline
3501 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3502 {
3503 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3504 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3505 
3506 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3507 		return RX_CONTINUE;
3508 
3509 	/*
3510 	 * From here on, look only at management frames.
3511 	 * Data and control frames are already handled,
3512 	 * and unknown (reserved) frames are useless.
3513 	 */
3514 	if (rx->skb->len < 24)
3515 		return RX_DROP_U_RUNT_MGMT;
3516 
3517 	if (!ieee80211_is_mgmt(mgmt->frame_control))
3518 		return RX_DROP_U_EXPECTED_MGMT;
3519 
3520 	/* drop too small action frames */
3521 	if (ieee80211_is_action(mgmt->frame_control) &&
3522 	    rx->skb->len < IEEE80211_MIN_ACTION_SIZE(category))
3523 		return RX_DROP_U_RUNT_ACTION;
3524 
3525 	/* Drop non-broadcast Beacon frames */
3526 	if (ieee80211_is_beacon(mgmt->frame_control) &&
3527 	    !is_broadcast_ether_addr(mgmt->da))
3528 		return RX_DROP_U_NONBCAST_BEACON;
3529 
3530 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3531 	    ieee80211_is_beacon(mgmt->frame_control) &&
3532 	    !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3533 		int sig = 0;
3534 
3535 		/* sw bss color collision detection */
3536 		ieee80211_rx_check_bss_color_collision(rx);
3537 
3538 		if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3539 		    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3540 			sig = status->signal;
3541 
3542 		cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3543 						rx->skb->data, rx->skb->len,
3544 						ieee80211_rx_status_to_khz(status),
3545 						sig);
3546 		rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3547 	}
3548 
3549 	return ieee80211_drop_unencrypted_mgmt(rx);
3550 }
3551 
3552 static bool
3553 ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3554 {
3555 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
3556 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3557 
3558 	/* TWT actions are only supported in AP for the moment */
3559 	if (sdata->vif.type != NL80211_IFTYPE_AP)
3560 		return false;
3561 
3562 	if (!rx->local->ops->add_twt_setup)
3563 		return false;
3564 
3565 	if (!sdata->vif.bss_conf.twt_responder)
3566 		return false;
3567 
3568 	if (!rx->sta)
3569 		return false;
3570 
3571 	switch (mgmt->u.action.action_code) {
3572 	case WLAN_S1G_TWT_SETUP: {
3573 		struct ieee80211_twt_setup *twt;
3574 
3575 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) +
3576 				   sizeof(struct ieee80211_twt_setup) +
3577 				   2 /* TWT req_type agrt */)
3578 			break;
3579 
3580 		twt = (void *)mgmt->u.action.s1g.variable;
3581 		if (twt->element_id != WLAN_EID_S1G_TWT)
3582 			break;
3583 
3584 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) +
3585 				   3 + /* token + tlv */
3586 				   twt->length)
3587 			break;
3588 
3589 		return true; /* queue the frame */
3590 	}
3591 	case WLAN_S1G_TWT_TEARDOWN:
3592 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) + 1)
3593 			break;
3594 
3595 		return true; /* queue the frame */
3596 	default:
3597 		break;
3598 	}
3599 
3600 	return false;
3601 }
3602 
3603 static ieee80211_rx_result debug_noinline
3604 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3605 {
3606 	struct ieee80211_local *local = rx->local;
3607 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3608 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3609 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3610 	int len = rx->skb->len;
3611 
3612 	if (!ieee80211_is_action(mgmt->frame_control))
3613 		return RX_CONTINUE;
3614 
3615 	if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3616 	    mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3617 	    mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3618 		return RX_DROP_U_ACTION_UNKNOWN_SRC;
3619 
3620 	switch (mgmt->u.action.category) {
3621 	case WLAN_CATEGORY_HT:
3622 		/* reject HT action frames from stations not supporting HT
3623 		 * or not HE Capable
3624 		 */
3625 		if (!rx->link_sta->pub->ht_cap.ht_supported &&
3626 		    !rx->link_sta->pub->he_cap.has_he)
3627 			goto invalid;
3628 
3629 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3630 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3631 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3632 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3633 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3634 			break;
3635 
3636 		/* verify action & smps_control/chanwidth are present */
3637 		if (len < IEEE80211_MIN_ACTION_SIZE(ht_smps))
3638 			goto invalid;
3639 
3640 		switch (mgmt->u.action.action_code) {
3641 		case WLAN_HT_ACTION_SMPS: {
3642 			struct ieee80211_supported_band *sband;
3643 			enum ieee80211_smps_mode smps_mode;
3644 			struct sta_opmode_info sta_opmode = {};
3645 
3646 			if (sdata->vif.type != NL80211_IFTYPE_AP &&
3647 			    sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3648 				goto handled;
3649 
3650 			/* convert to HT capability */
3651 			switch (mgmt->u.action.ht_smps.smps_control) {
3652 			case WLAN_HT_SMPS_CONTROL_DISABLED:
3653 				smps_mode = IEEE80211_SMPS_OFF;
3654 				break;
3655 			case WLAN_HT_SMPS_CONTROL_STATIC:
3656 				smps_mode = IEEE80211_SMPS_STATIC;
3657 				break;
3658 			case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3659 				smps_mode = IEEE80211_SMPS_DYNAMIC;
3660 				break;
3661 			default:
3662 				goto invalid;
3663 			}
3664 
3665 			/* if no change do nothing */
3666 			if (rx->link_sta->pub->smps_mode == smps_mode)
3667 				goto handled;
3668 			rx->link_sta->pub->smps_mode = smps_mode;
3669 			sta_opmode.smps_mode =
3670 				ieee80211_smps_mode_to_smps_mode(smps_mode);
3671 			sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3672 
3673 			sband = rx->local->hw.wiphy->bands[status->band];
3674 
3675 			rate_control_rate_update(local, sband, rx->link_sta,
3676 						 IEEE80211_RC_SMPS_CHANGED);
3677 			cfg80211_sta_opmode_change_notify(sdata->dev,
3678 							  rx->sta->addr,
3679 							  &sta_opmode,
3680 							  GFP_ATOMIC);
3681 			goto handled;
3682 		}
3683 		case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3684 			u8 chanwidth = mgmt->u.action.ht_notify_cw.chanwidth;
3685 
3686 			if (chanwidth != IEEE80211_HT_CHANWIDTH_20MHZ &&
3687 			    chanwidth != IEEE80211_HT_CHANWIDTH_ANY)
3688 				goto invalid;
3689 
3690 			/* If it doesn't support 40 MHz it can't change ... */
3691 			if (!(rx->link_sta->pub->ht_cap.cap &
3692 				IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3693 				goto handled;
3694 
3695 			goto queue;
3696 		}
3697 		default:
3698 			goto invalid;
3699 		}
3700 
3701 		break;
3702 	case WLAN_CATEGORY_PUBLIC:
3703 	case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
3704 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3705 			goto invalid;
3706 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3707 			break;
3708 		if (!rx->sta)
3709 			break;
3710 		if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid))
3711 			break;
3712 		if (mgmt->u.action.action_code !=
3713 				WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3714 			break;
3715 		if (len < IEEE80211_MIN_ACTION_SIZE(ext_chan_switch))
3716 			goto invalid;
3717 		goto queue;
3718 	case WLAN_CATEGORY_VHT:
3719 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3720 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3721 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3722 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3723 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3724 			break;
3725 
3726 		/* verify action code is present */
3727 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3728 			goto invalid;
3729 
3730 		switch (mgmt->u.action.action_code) {
3731 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3732 			/* verify opmode is present */
3733 			if (len < IEEE80211_MIN_ACTION_SIZE(vht_opmode_notif))
3734 				goto invalid;
3735 			goto queue;
3736 		}
3737 		case WLAN_VHT_ACTION_GROUPID_MGMT: {
3738 			if (len < IEEE80211_MIN_ACTION_SIZE(vht_group_notif))
3739 				goto invalid;
3740 			goto queue;
3741 		}
3742 		default:
3743 			break;
3744 		}
3745 		break;
3746 	case WLAN_CATEGORY_BACK:
3747 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3748 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3749 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3750 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3751 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3752 			break;
3753 
3754 		/* verify action_code is present */
3755 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3756 			break;
3757 
3758 		switch (mgmt->u.action.action_code) {
3759 		case WLAN_ACTION_ADDBA_REQ:
3760 		case WLAN_ACTION_NDP_ADDBA_REQ:
3761 			if (len < IEEE80211_MIN_ACTION_SIZE(addba_req))
3762 				goto invalid;
3763 			break;
3764 		case WLAN_ACTION_ADDBA_RESP:
3765 		case WLAN_ACTION_NDP_ADDBA_RESP:
3766 			if (len < IEEE80211_MIN_ACTION_SIZE(addba_resp))
3767 				goto invalid;
3768 			break;
3769 		case WLAN_ACTION_DELBA:
3770 		case WLAN_ACTION_NDP_DELBA:
3771 			if (len < IEEE80211_MIN_ACTION_SIZE(delba))
3772 				goto invalid;
3773 			break;
3774 		default:
3775 			goto invalid;
3776 		}
3777 
3778 		goto queue;
3779 	case WLAN_CATEGORY_SPECTRUM_MGMT:
3780 		/* verify action_code is present */
3781 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3782 			break;
3783 
3784 		switch (mgmt->u.action.action_code) {
3785 		case WLAN_ACTION_SPCT_MSR_REQ:
3786 			if (status->band != NL80211_BAND_5GHZ)
3787 				break;
3788 
3789 			if (len < IEEE80211_MIN_ACTION_SIZE(measurement))
3790 				break;
3791 
3792 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3793 				break;
3794 
3795 			ieee80211_process_measurement_req(sdata, mgmt, len);
3796 			goto handled;
3797 		case WLAN_ACTION_SPCT_CHL_SWITCH: {
3798 			u8 *bssid;
3799 			if (len < IEEE80211_MIN_ACTION_SIZE(chan_switch))
3800 				break;
3801 
3802 			if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3803 			    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3804 			    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3805 				break;
3806 
3807 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
3808 				bssid = sdata->deflink.u.mgd.bssid;
3809 			else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3810 				bssid = sdata->u.ibss.bssid;
3811 			else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3812 				bssid = mgmt->sa;
3813 			else
3814 				break;
3815 
3816 			if (!ether_addr_equal(mgmt->bssid, bssid))
3817 				break;
3818 
3819 			goto queue;
3820 			}
3821 		}
3822 		break;
3823 	case WLAN_CATEGORY_SELF_PROTECTED:
3824 		if (len < IEEE80211_MIN_ACTION_SIZE(self_prot))
3825 			break;
3826 
3827 		switch (mgmt->u.action.action_code) {
3828 		case WLAN_SP_MESH_PEERING_OPEN:
3829 		case WLAN_SP_MESH_PEERING_CLOSE:
3830 		case WLAN_SP_MESH_PEERING_CONFIRM:
3831 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3832 				goto invalid;
3833 			if (sdata->u.mesh.user_mpm)
3834 				/* userspace handles this frame */
3835 				break;
3836 			goto queue;
3837 		case WLAN_SP_MGK_INFORM:
3838 		case WLAN_SP_MGK_ACK:
3839 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3840 				goto invalid;
3841 			break;
3842 		}
3843 		break;
3844 	case WLAN_CATEGORY_MESH_ACTION:
3845 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3846 			break;
3847 
3848 		if (!ieee80211_vif_is_mesh(&sdata->vif))
3849 			break;
3850 		if (mesh_action_is_path_sel(mgmt) &&
3851 		    !mesh_path_sel_is_hwmp(sdata))
3852 			break;
3853 		goto queue;
3854 	case WLAN_CATEGORY_S1G:
3855 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3856 			break;
3857 
3858 		switch (mgmt->u.action.action_code) {
3859 		case WLAN_S1G_TWT_SETUP:
3860 		case WLAN_S1G_TWT_TEARDOWN:
3861 			if (ieee80211_process_rx_twt_action(rx))
3862 				goto queue;
3863 			break;
3864 		default:
3865 			break;
3866 		}
3867 		break;
3868 	case WLAN_CATEGORY_PROTECTED_EHT:
3869 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3870 			break;
3871 
3872 		switch (mgmt->u.action.action_code) {
3873 		case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
3874 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3875 				break;
3876 
3877 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_req))
3878 				goto invalid;
3879 			goto queue;
3880 		case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
3881 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3882 				break;
3883 
3884 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_res))
3885 				goto invalid;
3886 			goto queue;
3887 		case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
3888 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3889 				break;
3890 
3891 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_tear_down))
3892 				goto invalid;
3893 			goto queue;
3894 		case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
3895 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3896 				break;
3897 
3898 			/* The reconfiguration response action frame must
3899 			 * least one 'Status Duple' entry (3 octets)
3900 			 */
3901 			if (len < IEEE80211_MIN_ACTION_SIZE(ml_reconf_resp) + 3)
3902 				goto invalid;
3903 			goto queue;
3904 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
3905 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3906 				break;
3907 
3908 			if (len < IEEE80211_MIN_ACTION_SIZE(epcs) +
3909 				  IEEE80211_EPCS_ENA_RESP_BODY_LEN)
3910 				goto invalid;
3911 			goto queue;
3912 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
3913 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3914 				break;
3915 
3916 			if (len < IEEE80211_MIN_ACTION_SIZE(epcs))
3917 				goto invalid;
3918 			goto queue;
3919 		case WLAN_PROTECTED_EHT_ACTION_EML_OP_MODE_NOTIF:
3920 			if (sdata->vif.type != NL80211_IFTYPE_AP)
3921 				break;
3922 
3923 			if (len < IEEE80211_MIN_ACTION_SIZE(eml_omn))
3924 				goto invalid;
3925 			goto queue;
3926 		default:
3927 			break;
3928 		}
3929 		break;
3930 	}
3931 
3932 	return RX_CONTINUE;
3933 
3934  invalid:
3935 	status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3936 	/* will return in the next handlers */
3937 	return RX_CONTINUE;
3938 
3939  handled:
3940 	if (rx->sta)
3941 		rx->link_sta->rx_stats.packets++;
3942 	dev_kfree_skb(rx->skb);
3943 	return RX_QUEUED;
3944 
3945  queue:
3946 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
3947 	return RX_QUEUED;
3948 }
3949 
3950 static ieee80211_rx_result debug_noinline
3951 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3952 {
3953 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3954 	struct cfg80211_rx_info info = {
3955 		.freq = ieee80211_rx_status_to_khz(status),
3956 		.buf = rx->skb->data,
3957 		.len = rx->skb->len,
3958 		.link_id = rx->link_id,
3959 		.have_link_id = rx->link_id >= 0,
3960 	};
3961 
3962 	/* skip known-bad action frames and return them in the next handler */
3963 	if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3964 		return RX_CONTINUE;
3965 
3966 	/*
3967 	 * Getting here means the kernel doesn't know how to handle
3968 	 * it, but maybe userspace does ... include returned frames
3969 	 * so userspace can register for those to know whether ones
3970 	 * it transmitted were processed or returned.
3971 	 */
3972 
3973 	if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3974 	    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3975 		info.sig_dbm = status->signal;
3976 
3977 	if (ieee80211_is_timing_measurement(rx->skb) ||
3978 	    ieee80211_is_ftm(rx->skb)) {
3979 		info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp);
3980 		info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp);
3981 	}
3982 
3983 	if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) {
3984 		if (rx->sta)
3985 			rx->link_sta->rx_stats.packets++;
3986 		dev_kfree_skb(rx->skb);
3987 		return RX_QUEUED;
3988 	}
3989 
3990 	return RX_CONTINUE;
3991 }
3992 
3993 static ieee80211_rx_result debug_noinline
3994 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3995 {
3996 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3997 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3998 	int len = rx->skb->len;
3999 
4000 	if (!ieee80211_is_action(mgmt->frame_control))
4001 		return RX_CONTINUE;
4002 
4003 	switch (mgmt->u.action.category) {
4004 	case WLAN_CATEGORY_SA_QUERY:
4005 		if (len < IEEE80211_MIN_ACTION_SIZE(sa_query))
4006 			break;
4007 
4008 		switch (mgmt->u.action.action_code) {
4009 		case WLAN_ACTION_SA_QUERY_REQUEST:
4010 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
4011 				break;
4012 			ieee80211_process_sa_query_req(sdata, mgmt, len);
4013 			goto handled;
4014 		}
4015 		break;
4016 	}
4017 
4018 	return RX_CONTINUE;
4019 
4020  handled:
4021 	if (rx->sta)
4022 		rx->link_sta->rx_stats.packets++;
4023 	dev_kfree_skb(rx->skb);
4024 	return RX_QUEUED;
4025 }
4026 
4027 static ieee80211_rx_result debug_noinline
4028 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
4029 {
4030 	struct ieee80211_local *local = rx->local;
4031 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
4032 	struct sk_buff *nskb;
4033 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4034 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4035 
4036 	if (!ieee80211_is_action(mgmt->frame_control))
4037 		return RX_CONTINUE;
4038 
4039 	/*
4040 	 * For AP mode, hostapd is responsible for handling any action
4041 	 * frames that we didn't handle, including returning unknown
4042 	 * ones. For all other modes we will return them to the sender,
4043 	 * setting the 0x80 bit in the action category, as required by
4044 	 * 802.11-2012 9.24.4.
4045 	 * Newer versions of hostapd use the management frame registration
4046 	 * mechanisms and old cooked monitor interface is no longer supported.
4047 	 */
4048 	if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
4049 	    (sdata->vif.type == NL80211_IFTYPE_AP ||
4050 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
4051 		return RX_DROP_U_MALFORMED_ACTION;
4052 
4053 	if (is_multicast_ether_addr(mgmt->da))
4054 		return RX_DROP_U_UNKNOWN_MCAST_ACTION;
4055 
4056 	/* do not return rejected action frames */
4057 	if (mgmt->u.action.category & 0x80)
4058 		return RX_DROP_U_REJECTED_ACTION_RESPONSE;
4059 
4060 	nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
4061 			       GFP_ATOMIC);
4062 	if (nskb) {
4063 		struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
4064 
4065 		nmgmt->u.action.category |= 0x80;
4066 		memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
4067 		memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
4068 
4069 		memset(nskb->cb, 0, sizeof(nskb->cb));
4070 
4071 		if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
4072 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
4073 
4074 			info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
4075 				      IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
4076 				      IEEE80211_TX_CTL_NO_CCK_RATE;
4077 			if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
4078 				info->hw_queue =
4079 					local->hw.offchannel_tx_hw_queue;
4080 		}
4081 
4082 		__ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7, -1,
4083 					    status->band);
4084 	}
4085 
4086 	return RX_DROP_U_UNKNOWN_ACTION_REJECTED;
4087 }
4088 
4089 static ieee80211_rx_result debug_noinline
4090 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
4091 {
4092 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4093 	struct ieee80211_hdr *hdr = (void *)rx->skb->data;
4094 
4095 	if (!ieee80211_is_ext(hdr->frame_control))
4096 		return RX_CONTINUE;
4097 
4098 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
4099 		return RX_DROP_U_UNEXPECTED_EXT_FRAME;
4100 
4101 	/* for now only beacons are ext, so queue them */
4102 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
4103 
4104 	return RX_QUEUED;
4105 }
4106 
4107 static ieee80211_rx_result debug_noinline
4108 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
4109 {
4110 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4111 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
4112 	__le16 stype;
4113 
4114 	stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
4115 
4116 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
4117 	    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4118 	    sdata->vif.type != NL80211_IFTYPE_OCB &&
4119 	    sdata->vif.type != NL80211_IFTYPE_STATION)
4120 		return RX_DROP_U_UNHANDLED_MGMT;
4121 
4122 	switch (stype) {
4123 	case cpu_to_le16(IEEE80211_STYPE_AUTH):
4124 	case cpu_to_le16(IEEE80211_STYPE_BEACON):
4125 	case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
4126 		/* process for all: mesh, mlme, ibss */
4127 		break;
4128 	case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
4129 		if (is_multicast_ether_addr(mgmt->da) &&
4130 		    !is_broadcast_ether_addr(mgmt->da))
4131 			return RX_DROP_U_MCAST_DEAUTH;
4132 
4133 		/* process only for station/IBSS */
4134 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
4135 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
4136 			return RX_DROP_U_UNHANDLED_DEAUTH;
4137 		break;
4138 	case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
4139 	case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
4140 	case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
4141 		if (is_multicast_ether_addr(mgmt->da) &&
4142 		    !is_broadcast_ether_addr(mgmt->da))
4143 			return RX_DROP_U_MCAST_DISASSOC;
4144 
4145 		/* process only for station */
4146 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
4147 			return RX_DROP_U_UNHANDLED_DISASSOC;
4148 		break;
4149 	case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
4150 		/* process only for ibss and mesh */
4151 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4152 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4153 			return RX_DROP_U_UNHANDLED_PREQ;
4154 		break;
4155 	default:
4156 		return RX_DROP_U_UNHANDLED_MGMT_STYPE;
4157 	}
4158 
4159 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
4160 
4161 	return RX_QUEUED;
4162 }
4163 
4164 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
4165 					 ieee80211_rx_result res)
4166 {
4167 	if (res == RX_QUEUED) {
4168 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
4169 		return;
4170 	}
4171 
4172 	if (res != RX_CONTINUE) {
4173 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
4174 		if (rx->sta)
4175 			rx->link_sta->rx_stats.dropped++;
4176 	}
4177 
4178 	kfree_skb_reason(rx->skb, (__force u32)res);
4179 }
4180 
4181 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
4182 				  struct sk_buff_head *frames)
4183 {
4184 	ieee80211_rx_result res;
4185 	struct sk_buff *skb;
4186 
4187 #define CALL_RXH(rxh)			\
4188 	do {				\
4189 		res = rxh(rx);		\
4190 		if (res != RX_CONTINUE)	\
4191 			goto rxh_next;  \
4192 	} while (0)
4193 
4194 	/* Lock here to avoid hitting all of the data used in the RX
4195 	 * path (e.g. key data, station data, ...) concurrently when
4196 	 * a frame is released from the reorder buffer due to timeout
4197 	 * from the timer, potentially concurrently with RX from the
4198 	 * driver.
4199 	 */
4200 	spin_lock_bh(&rx->local->rx_path_lock);
4201 
4202 	while ((skb = __skb_dequeue(frames))) {
4203 		/*
4204 		 * all the other fields are valid across frames
4205 		 * that belong to an aMPDU since they are on the
4206 		 * same TID from the same station
4207 		 */
4208 		rx->skb = skb;
4209 
4210 		if (WARN_ON_ONCE(!rx->link)) {
4211 			res = RX_DROP_U_NO_LINK;
4212 			goto rxh_next;
4213 		}
4214 
4215 		CALL_RXH(ieee80211_rx_h_check_more_data);
4216 		CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
4217 		CALL_RXH(ieee80211_rx_h_sta_process);
4218 		CALL_RXH(ieee80211_rx_h_decrypt);
4219 		CALL_RXH(ieee80211_rx_h_defragment);
4220 		CALL_RXH(ieee80211_rx_h_michael_mic_verify);
4221 		/* must be after MMIC verify so header is counted in MPDU mic */
4222 		CALL_RXH(ieee80211_rx_h_amsdu);
4223 		CALL_RXH(ieee80211_rx_h_data);
4224 
4225 		/* special treatment -- needs the queue */
4226 		res = ieee80211_rx_h_ctrl(rx, frames);
4227 		if (res != RX_CONTINUE)
4228 			goto rxh_next;
4229 
4230 		CALL_RXH(ieee80211_rx_h_mgmt_check);
4231 		CALL_RXH(ieee80211_rx_h_action);
4232 		CALL_RXH(ieee80211_rx_h_userspace_mgmt);
4233 		CALL_RXH(ieee80211_rx_h_action_post_userspace);
4234 		CALL_RXH(ieee80211_rx_h_action_return);
4235 		CALL_RXH(ieee80211_rx_h_ext);
4236 		CALL_RXH(ieee80211_rx_h_mgmt);
4237 
4238  rxh_next:
4239 		ieee80211_rx_handlers_result(rx, res);
4240 
4241 #undef CALL_RXH
4242 	}
4243 
4244 	spin_unlock_bh(&rx->local->rx_path_lock);
4245 }
4246 
4247 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
4248 {
4249 	struct sk_buff_head reorder_release;
4250 	ieee80211_rx_result res;
4251 
4252 	__skb_queue_head_init(&reorder_release);
4253 
4254 #define CALL_RXH(rxh)			\
4255 	do {				\
4256 		res = rxh(rx);		\
4257 		if (res != RX_CONTINUE)	\
4258 			goto rxh_next;  \
4259 	} while (0)
4260 
4261 	CALL_RXH(ieee80211_rx_h_check_dup);
4262 	CALL_RXH(ieee80211_rx_h_check);
4263 
4264 	ieee80211_rx_reorder_ampdu(rx, &reorder_release);
4265 
4266 	ieee80211_rx_handlers(rx, &reorder_release);
4267 	return;
4268 
4269  rxh_next:
4270 	ieee80211_rx_handlers_result(rx, res);
4271 
4272 #undef CALL_RXH
4273 }
4274 
4275 static bool
4276 ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
4277 {
4278 	return !!(sta->valid_links & BIT(link_id));
4279 }
4280 
4281 static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
4282 				       u8 link_id)
4283 {
4284 	rx->link_id = link_id;
4285 	rx->link = rcu_dereference(rx->sdata->link[link_id]);
4286 
4287 	if (!rx->sta)
4288 		return rx->link;
4289 
4290 	if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
4291 		return false;
4292 
4293 	rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
4294 
4295 	return rx->link && rx->link_sta;
4296 }
4297 
4298 static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
4299 				      struct sta_info *sta, int link_id)
4300 {
4301 	rx->link_id = link_id;
4302 	rx->sta = sta;
4303 
4304 	if (sta) {
4305 		rx->local = sta->sdata->local;
4306 		if (!rx->sdata)
4307 			rx->sdata = sta->sdata;
4308 		rx->link_sta = &sta->deflink;
4309 	} else {
4310 		rx->link_sta = NULL;
4311 	}
4312 
4313 	if (link_id < 0) {
4314 		if (ieee80211_vif_is_mld(&rx->sdata->vif) &&
4315 		    sta && !sta->sta.valid_links)
4316 			rx->link =
4317 				rcu_dereference(rx->sdata->link[sta->deflink.link_id]);
4318 		else
4319 			rx->link = &rx->sdata->deflink;
4320 	} else if (!ieee80211_rx_data_set_link(rx, link_id)) {
4321 		return false;
4322 	}
4323 
4324 	return true;
4325 }
4326 
4327 /*
4328  * This function makes calls into the RX path, therefore
4329  * it has to be invoked under RCU read lock.
4330  */
4331 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
4332 {
4333 	struct sk_buff_head frames;
4334 	struct ieee80211_rx_data rx = {
4335 		/* This is OK -- must be QoS data frame */
4336 		.security_idx = tid,
4337 		.seqno_idx = tid,
4338 	};
4339 	struct tid_ampdu_rx *tid_agg_rx;
4340 	int link_id = -1;
4341 
4342 	/* FIXME: statistics won't be right with this */
4343 	if (sta->sta.valid_links)
4344 		link_id = ffs(sta->sta.valid_links) - 1;
4345 
4346 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
4347 		return;
4348 
4349 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4350 	if (!tid_agg_rx)
4351 		return;
4352 
4353 	__skb_queue_head_init(&frames);
4354 
4355 	spin_lock(&tid_agg_rx->reorder_lock);
4356 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4357 	spin_unlock(&tid_agg_rx->reorder_lock);
4358 
4359 	if (!skb_queue_empty(&frames)) {
4360 		struct ieee80211_event event = {
4361 			.type = BA_FRAME_TIMEOUT,
4362 			.u.ba.tid = tid,
4363 			.u.ba.sta = &sta->sta,
4364 		};
4365 		drv_event_callback(rx.local, rx.sdata, &event);
4366 	}
4367 
4368 	ieee80211_rx_handlers(&rx, &frames);
4369 }
4370 
4371 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4372 					  u16 ssn, u64 filtered,
4373 					  u16 received_mpdus)
4374 {
4375 	struct ieee80211_local *local;
4376 	struct sta_info *sta;
4377 	struct tid_ampdu_rx *tid_agg_rx;
4378 	struct sk_buff_head frames;
4379 	struct ieee80211_rx_data rx = {
4380 		/* This is OK -- must be QoS data frame */
4381 		.security_idx = tid,
4382 		.seqno_idx = tid,
4383 	};
4384 	int i, diff;
4385 
4386 	if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4387 		return;
4388 
4389 	__skb_queue_head_init(&frames);
4390 
4391 	sta = container_of(pubsta, struct sta_info, sta);
4392 
4393 	local = sta->sdata->local;
4394 	WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64,
4395 		  "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n",
4396 		  local->hw.max_rx_aggregation_subframes);
4397 
4398 	if (!ieee80211_rx_data_set_sta(&rx, sta, -1))
4399 		return;
4400 
4401 	rcu_read_lock();
4402 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4403 	if (!tid_agg_rx)
4404 		goto out;
4405 
4406 	spin_lock_bh(&tid_agg_rx->reorder_lock);
4407 
4408 	if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4409 		int release;
4410 
4411 		/* release all frames in the reorder buffer */
4412 		release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4413 			   IEEE80211_SN_MODULO;
4414 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4415 						 release, &frames);
4416 		/* update ssn to match received ssn */
4417 		tid_agg_rx->head_seq_num = ssn;
4418 	} else {
4419 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4420 						 &frames);
4421 	}
4422 
4423 	/* handle the case that received ssn is behind the mac ssn.
4424 	 * it can be tid_agg_rx->buf_size behind and still be valid */
4425 	diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4426 	if (diff >= tid_agg_rx->buf_size) {
4427 		tid_agg_rx->reorder_buf_filtered = 0;
4428 		goto release;
4429 	}
4430 	filtered = filtered >> diff;
4431 	ssn += diff;
4432 
4433 	/* update bitmap */
4434 	for (i = 0; i < tid_agg_rx->buf_size; i++) {
4435 		int index = (ssn + i) % tid_agg_rx->buf_size;
4436 
4437 		tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4438 		if (filtered & BIT_ULL(i))
4439 			tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4440 	}
4441 
4442 	/* now process also frames that the filter marking released */
4443 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4444 
4445 release:
4446 	spin_unlock_bh(&tid_agg_rx->reorder_lock);
4447 
4448 	ieee80211_rx_handlers(&rx, &frames);
4449 
4450  out:
4451 	rcu_read_unlock();
4452 }
4453 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4454 
4455 /* main receive path */
4456 
4457 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
4458 {
4459 	return ether_addr_equal(raddr, addr) ||
4460 	       is_broadcast_ether_addr(raddr);
4461 }
4462 
4463 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4464 {
4465 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4466 	struct sk_buff *skb = rx->skb;
4467 	struct ieee80211_hdr *hdr = (void *)skb->data;
4468 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4469 	u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4470 	bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4471 			 ieee80211_is_s1g_beacon(hdr->frame_control);
4472 
4473 	switch (sdata->vif.type) {
4474 	case NL80211_IFTYPE_STATION:
4475 		if (!bssid && !sdata->u.mgd.use_4addr)
4476 			return false;
4477 		if (ieee80211_is_first_frag(hdr->seq_ctrl) &&
4478 		    ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4479 			return false;
4480 		if (multicast)
4481 			return true;
4482 		return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id);
4483 	case NL80211_IFTYPE_ADHOC:
4484 		if (!bssid)
4485 			return false;
4486 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4487 		    ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4488 		    !is_valid_ether_addr(hdr->addr2))
4489 			return false;
4490 		if (ieee80211_is_beacon(hdr->frame_control))
4491 			return true;
4492 		if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4493 			return false;
4494 		if (!multicast &&
4495 		    !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4496 			return false;
4497 		if (!rx->sta) {
4498 			int rate_idx;
4499 			if (status->encoding != RX_ENC_LEGACY)
4500 				rate_idx = 0; /* TODO: HT/VHT rates */
4501 			else
4502 				rate_idx = status->rate_idx;
4503 			ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4504 						 BIT(rate_idx));
4505 		}
4506 		return true;
4507 	case NL80211_IFTYPE_OCB:
4508 		if (!bssid)
4509 			return false;
4510 		if (!ieee80211_is_data_present(hdr->frame_control))
4511 			return false;
4512 		if (!is_broadcast_ether_addr(bssid))
4513 			return false;
4514 		if (!multicast &&
4515 		    !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4516 			return false;
4517 		/* reject invalid/our STA address */
4518 		if (!is_valid_ether_addr(hdr->addr2) ||
4519 		    ether_addr_equal(sdata->dev->dev_addr, hdr->addr2))
4520 			return false;
4521 		if (!rx->sta) {
4522 			int rate_idx;
4523 			if (status->encoding != RX_ENC_LEGACY)
4524 				rate_idx = 0; /* TODO: HT rates */
4525 			else
4526 				rate_idx = status->rate_idx;
4527 			ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4528 						BIT(rate_idx));
4529 		}
4530 		return true;
4531 	case NL80211_IFTYPE_MESH_POINT:
4532 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4533 			return false;
4534 		if (multicast)
4535 			return true;
4536 		return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4537 	case NL80211_IFTYPE_AP_VLAN:
4538 	case NL80211_IFTYPE_AP:
4539 		if (!bssid)
4540 			return ieee80211_is_our_addr(sdata, hdr->addr1,
4541 						     &rx->link_id);
4542 
4543 		if (!is_broadcast_ether_addr(bssid) &&
4544 		    !ieee80211_is_our_addr(sdata, bssid, NULL)) {
4545 			/*
4546 			 * Accept public action frames even when the
4547 			 * BSSID doesn't match, this is used for P2P
4548 			 * and location updates. Note that mac80211
4549 			 * itself never looks at these frames.
4550 			 */
4551 			if (!multicast &&
4552 			    !ieee80211_is_our_addr(sdata, hdr->addr1,
4553 						   &rx->link_id))
4554 				return false;
4555 			if (ieee80211_is_public_action(hdr, skb->len))
4556 				return true;
4557 			return ieee80211_is_beacon(hdr->frame_control);
4558 		}
4559 
4560 		if (!ieee80211_has_tods(hdr->frame_control)) {
4561 			/* ignore data frames to TDLS-peers */
4562 			if (ieee80211_is_data(hdr->frame_control))
4563 				return false;
4564 			/* ignore action frames to TDLS-peers */
4565 			if (ieee80211_is_action(hdr->frame_control) &&
4566 			    !is_broadcast_ether_addr(bssid) &&
4567 			    !ether_addr_equal(bssid, hdr->addr1))
4568 				return false;
4569 		}
4570 
4571 		/*
4572 		 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4573 		 * the BSSID - we've checked that already but may have accepted
4574 		 * the wildcard (ff:ff:ff:ff:ff:ff).
4575 		 *
4576 		 * It also says:
4577 		 *	The BSSID of the Data frame is determined as follows:
4578 		 *	a) If the STA is contained within an AP or is associated
4579 		 *	   with an AP, the BSSID is the address currently in use
4580 		 *	   by the STA contained in the AP.
4581 		 *
4582 		 * So we should not accept data frames with an address that's
4583 		 * multicast.
4584 		 *
4585 		 * Accepting it also opens a security problem because stations
4586 		 * could encrypt it with the GTK and inject traffic that way.
4587 		 */
4588 		if (ieee80211_is_data(hdr->frame_control) && multicast)
4589 			return false;
4590 
4591 		return true;
4592 	case NL80211_IFTYPE_P2P_DEVICE:
4593 		return ieee80211_is_public_action(hdr, skb->len) ||
4594 		       ieee80211_is_probe_req(hdr->frame_control) ||
4595 		       ieee80211_is_probe_resp(hdr->frame_control) ||
4596 		       ieee80211_is_beacon(hdr->frame_control) ||
4597 		       (ieee80211_is_auth(hdr->frame_control) &&
4598 			ether_addr_equal(sdata->vif.addr, hdr->addr1));
4599 	case NL80211_IFTYPE_NAN:
4600 		/* Accept only frames that are addressed to the NAN cluster
4601 		 * (based on the Cluster ID). From these frames, accept only
4602 		 * action frames or authentication frames that are addressed to
4603 		 * the local NAN interface.
4604 		 */
4605 		return memcmp(sdata->wdev.u.nan.cluster_id,
4606 			      hdr->addr3, ETH_ALEN) == 0 &&
4607 			(ieee80211_is_public_action(hdr, skb->len) ||
4608 			 (ieee80211_is_auth(hdr->frame_control) &&
4609 			  ether_addr_equal(sdata->vif.addr, hdr->addr1)));
4610 	case NL80211_IFTYPE_NAN_DATA:
4611 		return false;
4612 	default:
4613 		break;
4614 	}
4615 
4616 	WARN_ON_ONCE(1);
4617 	return false;
4618 }
4619 
4620 void ieee80211_check_fast_rx(struct sta_info *sta)
4621 {
4622 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4623 	struct ieee80211_local *local = sdata->local;
4624 	struct ieee80211_key *key;
4625 	struct ieee80211_fast_rx fastrx = {
4626 		.dev = sdata->dev,
4627 		.vif_type = sdata->vif.type,
4628 		.control_port_protocol = sdata->control_port_protocol,
4629 	}, *old, *new = NULL;
4630 	u32 offload_flags;
4631 	bool set_offload = false;
4632 	bool assign = false;
4633 	bool offload;
4634 
4635 	/* use sparse to check that we don't return without updating */
4636 	__acquire(check_fast_rx);
4637 
4638 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4639 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4640 	ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4641 	ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4642 
4643 	fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4644 
4645 	/* fast-rx doesn't do reordering */
4646 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4647 	    !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4648 		goto clear;
4649 
4650 	switch (sdata->vif.type) {
4651 	case NL80211_IFTYPE_STATION:
4652 		if (sta->sta.tdls) {
4653 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4654 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4655 			fastrx.expected_ds_bits = 0;
4656 		} else {
4657 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4658 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4659 			fastrx.expected_ds_bits =
4660 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4661 		}
4662 
4663 		if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4664 			fastrx.expected_ds_bits |=
4665 				cpu_to_le16(IEEE80211_FCTL_TODS);
4666 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4667 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4668 		}
4669 
4670 		if (!sdata->u.mgd.powersave)
4671 			break;
4672 
4673 		/* software powersave is a huge mess, avoid all of it */
4674 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4675 			goto clear;
4676 		if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4677 		    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4678 			goto clear;
4679 		break;
4680 	case NL80211_IFTYPE_AP_VLAN:
4681 	case NL80211_IFTYPE_AP:
4682 		/* parallel-rx requires this, at least with calls to
4683 		 * ieee80211_sta_ps_transition()
4684 		 */
4685 		if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4686 			goto clear;
4687 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4688 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4689 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4690 
4691 		fastrx.internal_forward =
4692 			!(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4693 			(sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4694 			 !sdata->u.vlan.sta);
4695 
4696 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4697 		    sdata->u.vlan.sta) {
4698 			fastrx.expected_ds_bits |=
4699 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4700 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4701 			fastrx.internal_forward = 0;
4702 		}
4703 
4704 		break;
4705 	case NL80211_IFTYPE_MESH_POINT:
4706 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS |
4707 						      IEEE80211_FCTL_TODS);
4708 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4709 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4710 		break;
4711 	default:
4712 		goto clear;
4713 	}
4714 
4715 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4716 		goto clear;
4717 
4718 	rcu_read_lock();
4719 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4720 	if (!key)
4721 		key = rcu_dereference(sdata->default_unicast_key);
4722 	if (key) {
4723 		switch (key->conf.cipher) {
4724 		case WLAN_CIPHER_SUITE_TKIP:
4725 			/* we don't want to deal with MMIC in fast-rx */
4726 			goto clear_rcu;
4727 		case WLAN_CIPHER_SUITE_CCMP:
4728 		case WLAN_CIPHER_SUITE_CCMP_256:
4729 		case WLAN_CIPHER_SUITE_GCMP:
4730 		case WLAN_CIPHER_SUITE_GCMP_256:
4731 			break;
4732 		default:
4733 			/* We also don't want to deal with
4734 			 * WEP or cipher scheme.
4735 			 */
4736 			goto clear_rcu;
4737 		}
4738 
4739 		fastrx.key = true;
4740 		fastrx.icv_len = key->conf.icv_len;
4741 	}
4742 
4743 	assign = true;
4744  clear_rcu:
4745 	rcu_read_unlock();
4746  clear:
4747 	__release(check_fast_rx);
4748 
4749 	if (assign)
4750 		new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4751 
4752 	offload_flags = get_bss_sdata(sdata)->vif.offload_flags;
4753 	offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED;
4754 
4755 	if (assign && offload)
4756 		set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4757 	else
4758 		set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4759 
4760 	if (set_offload)
4761 		drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4762 
4763 	spin_lock_bh(&sta->lock);
4764 	old = rcu_dereference_protected(sta->fast_rx, true);
4765 	rcu_assign_pointer(sta->fast_rx, new);
4766 	spin_unlock_bh(&sta->lock);
4767 
4768 	if (old)
4769 		kfree_rcu(old, rcu_head);
4770 }
4771 
4772 void ieee80211_clear_fast_rx(struct sta_info *sta)
4773 {
4774 	struct ieee80211_fast_rx *old;
4775 
4776 	spin_lock_bh(&sta->lock);
4777 	old = rcu_dereference_protected(sta->fast_rx, true);
4778 	RCU_INIT_POINTER(sta->fast_rx, NULL);
4779 	spin_unlock_bh(&sta->lock);
4780 
4781 	if (old)
4782 		kfree_rcu(old, rcu_head);
4783 }
4784 
4785 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4786 {
4787 	struct ieee80211_local *local = sdata->local;
4788 	struct sta_info *sta;
4789 
4790 	lockdep_assert_wiphy(local->hw.wiphy);
4791 
4792 	list_for_each_entry(sta, &local->sta_list, list) {
4793 		if (sdata != sta->sdata &&
4794 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4795 			continue;
4796 		ieee80211_check_fast_rx(sta);
4797 	}
4798 }
4799 
4800 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4801 {
4802 	struct ieee80211_local *local = sdata->local;
4803 
4804 	lockdep_assert_wiphy(local->hw.wiphy);
4805 
4806 	__ieee80211_check_fast_rx_iface(sdata);
4807 }
4808 
4809 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4810 			      struct ieee80211_fast_rx *fast_rx,
4811 			      int orig_len)
4812 {
4813 	struct ieee80211_sta_rx_stats *stats;
4814 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4815 	struct sta_info *sta = rx->sta;
4816 	struct link_sta_info *link_sta;
4817 	struct sk_buff *skb = rx->skb;
4818 	void *sa = skb->data + ETH_ALEN;
4819 	void *da = skb->data;
4820 
4821 	if (rx->link_id >= 0) {
4822 		link_sta = rcu_dereference(sta->link[rx->link_id]);
4823 		if (WARN_ON_ONCE(!link_sta)) {
4824 			dev_kfree_skb(rx->skb);
4825 			return;
4826 		}
4827 	} else {
4828 		link_sta = &sta->deflink;
4829 	}
4830 
4831 	stats = &link_sta->rx_stats;
4832 	if (fast_rx->uses_rss)
4833 		stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
4834 
4835 	/* statistics part of ieee80211_rx_h_sta_process() */
4836 	if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4837 		stats->last_signal = status->signal;
4838 		if (!fast_rx->uses_rss)
4839 			ewma_signal_add(&link_sta->rx_stats_avg.signal,
4840 					-status->signal);
4841 	}
4842 
4843 	if (status->chains) {
4844 		int i;
4845 
4846 		stats->chains = status->chains;
4847 		for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4848 			int signal = status->chain_signal[i];
4849 
4850 			if (!(status->chains & BIT(i)))
4851 				continue;
4852 
4853 			stats->chain_signal_last[i] = signal;
4854 			if (!fast_rx->uses_rss)
4855 				ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
4856 						-signal);
4857 		}
4858 	}
4859 	/* end of statistics */
4860 
4861 	stats->last_rx = jiffies;
4862 	stats->last_rate = sta_stats_encode_rate(status);
4863 
4864 	stats->fragments++;
4865 	stats->packets++;
4866 
4867 	skb->dev = fast_rx->dev;
4868 
4869 	dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4870 
4871 	/* The seqno index has the same property as needed
4872 	 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4873 	 * for non-QoS-data frames. Here we know it's a data
4874 	 * frame, so count MSDUs.
4875 	 */
4876 	u64_stats_update_begin(&stats->syncp);
4877 	u64_stats_inc(&stats->msdu[rx->seqno_idx]);
4878 	u64_stats_add(&stats->bytes, orig_len);
4879 	u64_stats_update_end(&stats->syncp);
4880 
4881 	if (fast_rx->internal_forward) {
4882 		struct sk_buff *xmit_skb = NULL;
4883 		if (is_multicast_ether_addr(da)) {
4884 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
4885 		} else if (!ether_addr_equal(da, sa) &&
4886 			   sta_info_get(rx->sdata, da)) {
4887 			xmit_skb = skb;
4888 			skb = NULL;
4889 		}
4890 
4891 		if (xmit_skb) {
4892 			/*
4893 			 * Send to wireless media and increase priority by 256
4894 			 * to keep the received priority instead of
4895 			 * reclassifying the frame (see cfg80211_classify8021d).
4896 			 */
4897 			xmit_skb->priority += 256;
4898 			xmit_skb->protocol = htons(ETH_P_802_3);
4899 			skb_reset_network_header(xmit_skb);
4900 			skb_reset_mac_header(xmit_skb);
4901 			dev_queue_xmit(xmit_skb);
4902 		}
4903 
4904 		if (!skb)
4905 			return;
4906 	}
4907 
4908 	/* deliver to local stack */
4909 	skb->protocol = eth_type_trans(skb, fast_rx->dev);
4910 	ieee80211_deliver_skb_to_local_stack(skb, rx);
4911 }
4912 
4913 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4914 				     struct ieee80211_fast_rx *fast_rx)
4915 {
4916 	struct sk_buff *skb = rx->skb;
4917 	struct ieee80211_hdr *hdr = (void *)skb->data;
4918 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4919 	static ieee80211_rx_result res;
4920 	int orig_len = skb->len;
4921 	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4922 	int snap_offs = hdrlen;
4923 	struct {
4924 		u8 snap[sizeof(rfc1042_header)];
4925 		__be16 proto;
4926 	} *payload __aligned(2);
4927 	struct {
4928 		u8 da[ETH_ALEN];
4929 		u8 sa[ETH_ALEN];
4930 	} addrs __aligned(2);
4931 	struct ieee80211_sta_rx_stats *stats;
4932 
4933 	/* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4934 	 * to a common data structure; drivers can implement that per queue
4935 	 * but we don't have that information in mac80211
4936 	 */
4937 	if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4938 		return false;
4939 
4940 #define FAST_RX_CRYPT_FLAGS	(RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4941 
4942 	/* If using encryption, we also need to have:
4943 	 *  - PN_VALIDATED: similar, but the implementation is tricky
4944 	 *  - DECRYPTED: necessary for PN_VALIDATED
4945 	 */
4946 	if (fast_rx->key &&
4947 	    (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4948 		return false;
4949 
4950 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4951 		return false;
4952 
4953 	if (unlikely(ieee80211_is_frag(hdr)))
4954 		return false;
4955 
4956 	/* Since our interface address cannot be multicast, this
4957 	 * implicitly also rejects multicast frames without the
4958 	 * explicit check.
4959 	 *
4960 	 * We shouldn't get any *data* frames not addressed to us
4961 	 * (AP mode will accept multicast *management* frames), but
4962 	 * punting here will make it go through the full checks in
4963 	 * ieee80211_accept_frame().
4964 	 */
4965 	if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4966 		return false;
4967 
4968 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4969 					      IEEE80211_FCTL_TODS)) !=
4970 	    fast_rx->expected_ds_bits)
4971 		return false;
4972 
4973 	/* assign the key to drop unencrypted frames (later)
4974 	 * and strip the IV/MIC if necessary
4975 	 */
4976 	if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4977 		/* GCMP header length is the same */
4978 		snap_offs += IEEE80211_CCMP_HDR_LEN;
4979 	}
4980 
4981 	if (!ieee80211_vif_is_mesh(&rx->sdata->vif) &&
4982 	    !(status->rx_flags & IEEE80211_RX_AMSDU)) {
4983 		if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4984 			return false;
4985 
4986 		payload = (void *)(skb->data + snap_offs);
4987 
4988 		if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4989 			return false;
4990 
4991 		/* Don't handle these here since they require special code.
4992 		 * Accept AARP and IPX even though they should come with a
4993 		 * bridge-tunnel header - but if we get them this way then
4994 		 * there's little point in discarding them.
4995 		 */
4996 		if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4997 			     payload->proto == fast_rx->control_port_protocol))
4998 			return false;
4999 	}
5000 
5001 	/* after this point, don't punt to the slowpath! */
5002 
5003 	if (fast_rx->uses_rss)
5004 		stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
5005 	else
5006 		stats = &rx->link_sta->rx_stats;
5007 
5008 	if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
5009 	    pskb_trim(skb, skb->len - fast_rx->icv_len))
5010 		goto drop;
5011 
5012 	if (rx->key && !ieee80211_has_protected(hdr->frame_control))
5013 		goto drop;
5014 
5015 	if (status->rx_flags & IEEE80211_RX_AMSDU) {
5016 		if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
5017 		    RX_QUEUED)
5018 			goto drop;
5019 
5020 		return true;
5021 	}
5022 
5023 	/* do the header conversion - first grab the addresses */
5024 	ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
5025 	ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
5026 	if (ieee80211_vif_is_mesh(&rx->sdata->vif)) {
5027 	    skb_pull(skb, snap_offs - 2);
5028 	    put_unaligned_be16(skb->len - 2, skb->data);
5029 	} else {
5030 	    skb_postpull_rcsum(skb, skb->data + snap_offs,
5031 			       sizeof(rfc1042_header) + 2);
5032 
5033 	    /* remove the SNAP but leave the ethertype */
5034 	    skb_pull(skb, snap_offs + sizeof(rfc1042_header));
5035 	}
5036 	/* push the addresses in front */
5037 	memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
5038 
5039 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
5040 	switch (res) {
5041 	case RX_QUEUED:
5042 		stats->last_rx = jiffies;
5043 		stats->last_rate = sta_stats_encode_rate(status);
5044 		return true;
5045 	case RX_CONTINUE:
5046 		break;
5047 	default:
5048 		goto drop;
5049 	}
5050 
5051 	ieee80211_rx_8023(rx, fast_rx, orig_len);
5052 
5053 	return true;
5054  drop:
5055 	dev_kfree_skb(skb);
5056 
5057 	stats->dropped++;
5058 	return true;
5059 }
5060 
5061 /*
5062  * This function returns whether or not the SKB
5063  * was destined for RX processing or not, which,
5064  * if consume is true, is equivalent to whether
5065  * or not the skb was consumed.
5066  */
5067 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
5068 					    struct sk_buff *skb, bool consume)
5069 {
5070 	struct ieee80211_local *local = rx->local;
5071 	struct ieee80211_sub_if_data *sdata = rx->sdata;
5072 	struct ieee80211_hdr *hdr = (void *)skb->data;
5073 	struct link_sta_info *link_sta = rx->link_sta;
5074 	struct ieee80211_link_data *link = rx->link;
5075 
5076 	rx->skb = skb;
5077 
5078 	/* See if we can do fast-rx; if we have to copy we already lost,
5079 	 * so punt in that case. We should never have to deliver a data
5080 	 * frame to multiple interfaces anyway.
5081 	 *
5082 	 * We skip the ieee80211_accept_frame() call and do the necessary
5083 	 * checking inside ieee80211_invoke_fast_rx().
5084 	 */
5085 	if (consume && rx->sta) {
5086 		struct ieee80211_fast_rx *fast_rx;
5087 
5088 		fast_rx = rcu_dereference(rx->sta->fast_rx);
5089 		if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
5090 			return true;
5091 	}
5092 
5093 	if (!ieee80211_accept_frame(rx))
5094 		return false;
5095 
5096 	if (!consume) {
5097 		struct skb_shared_hwtstamps *shwt;
5098 
5099 		rx->skb = skb_copy(skb, GFP_ATOMIC);
5100 		if (!rx->skb) {
5101 			if (net_ratelimit())
5102 				wiphy_debug(local->hw.wiphy,
5103 					"failed to copy skb for %s\n",
5104 					sdata->name);
5105 			return true;
5106 		}
5107 
5108 		/* skb_copy() does not copy the hw timestamps, so copy it
5109 		 * explicitly
5110 		 */
5111 		shwt = skb_hwtstamps(rx->skb);
5112 		shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
5113 
5114 		/* Update the hdr pointer to the new skb for translation below */
5115 		hdr = (struct ieee80211_hdr *)rx->skb->data;
5116 	}
5117 
5118 	/* Store a copy of the pre-translated link addresses for SW crypto */
5119 	if (unlikely(is_unicast_ether_addr(hdr->addr1) &&
5120 		     !ieee80211_is_data(hdr->frame_control)))
5121 		memcpy(rx->link_addrs, &hdr->addrs, 3 * ETH_ALEN);
5122 
5123 	if (unlikely(rx->sta && rx->sta->sta.mlo) &&
5124 	    is_unicast_ether_addr(hdr->addr1) &&
5125 	    !ieee80211_is_probe_resp(hdr->frame_control) &&
5126 	    !ieee80211_is_beacon(hdr->frame_control)) {
5127 		/* translate to MLD addresses */
5128 		if (ether_addr_equal(link->conf->addr, hdr->addr1))
5129 			ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
5130 		if (ether_addr_equal(link_sta->addr, hdr->addr2))
5131 			ether_addr_copy(hdr->addr2, rx->sta->addr);
5132 		/* translate A3 only if it's the BSSID */
5133 		if (!ieee80211_has_tods(hdr->frame_control) &&
5134 		    !ieee80211_has_fromds(hdr->frame_control)) {
5135 			if (ether_addr_equal(link_sta->addr, hdr->addr3))
5136 				ether_addr_copy(hdr->addr3, rx->sta->addr);
5137 			else if (ether_addr_equal(link->conf->addr, hdr->addr3))
5138 				ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
5139 		}
5140 		/* not needed for A4 since it can only carry the SA */
5141 	}
5142 
5143 	ieee80211_invoke_rx_handlers(rx);
5144 	return true;
5145 }
5146 
5147 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
5148 				       struct ieee80211_sta *pubsta,
5149 				       struct sk_buff *skb,
5150 				       struct list_head *list)
5151 {
5152 	struct ieee80211_local *local = hw_to_local(hw);
5153 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5154 	struct ieee80211_fast_rx *fast_rx;
5155 	struct ieee80211_rx_data rx;
5156 	struct sta_info *sta;
5157 	int link_id = -1;
5158 
5159 	memset(&rx, 0, sizeof(rx));
5160 	rx.skb = skb;
5161 	rx.local = local;
5162 	rx.list = list;
5163 	rx.link_id = -1;
5164 
5165 	I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5166 
5167 	/* drop frame if too short for header */
5168 	if (skb->len < sizeof(struct ethhdr))
5169 		goto drop;
5170 
5171 	if (!pubsta)
5172 		goto drop;
5173 
5174 	if (status->link_valid)
5175 		link_id = status->link_id;
5176 
5177 	/*
5178 	 * TODO: Should the frame be dropped if the right link_id is not
5179 	 * available? Or may be it is fine in the current form to proceed with
5180 	 * the frame processing because with frame being in 802.3 format,
5181 	 * link_id is used only for stats purpose and updating the stats on
5182 	 * the deflink is fine?
5183 	 */
5184 	sta = container_of(pubsta, struct sta_info, sta);
5185 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5186 		goto drop;
5187 
5188 	fast_rx = rcu_dereference(rx.sta->fast_rx);
5189 	if (!fast_rx)
5190 		goto drop;
5191 
5192 	ieee80211_rx_8023(&rx, fast_rx, skb->len);
5193 	return;
5194 
5195 drop:
5196 	dev_kfree_skb(skb);
5197 }
5198 
5199 static bool ieee80211_rx_for_interface(struct ieee80211_rx_data *rx,
5200 				       struct sk_buff *skb, bool consume)
5201 {
5202 	struct link_sta_info *link_sta;
5203 	struct ieee80211_hdr *hdr = (void *)skb->data;
5204 	struct sta_info *sta;
5205 	int link_id = -1;
5206 
5207 	/*
5208 	 * Look up link station first, in case there's a
5209 	 * chance that they might have a link address that
5210 	 * is identical to the MLD address, that way we'll
5211 	 * have the link information if needed.
5212 	 */
5213 	link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
5214 	if (link_sta) {
5215 		sta = link_sta->sta;
5216 		link_id = link_sta->link_id;
5217 	} else {
5218 		struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5219 
5220 		sta = sta_info_get_bss(rx->sdata, hdr->addr2);
5221 		if (status->link_valid) {
5222 			link_id = status->link_id;
5223 		} else if (ieee80211_vif_is_mld(&rx->sdata->vif) &&
5224 			   status->freq) {
5225 			struct ieee80211_link_data *link;
5226 			struct ieee80211_chanctx_conf *conf;
5227 
5228 			for_each_link_data_rcu(rx->sdata, link) {
5229 				conf = rcu_dereference(link->conf->chanctx_conf);
5230 				if (!conf || !conf->def.chan)
5231 					continue;
5232 
5233 				if (status->freq == conf->def.chan->center_freq) {
5234 					link_id = link->link_id;
5235 					break;
5236 				}
5237 			}
5238 		}
5239 	}
5240 
5241 	if (!ieee80211_rx_data_set_sta(rx, sta, link_id))
5242 		return false;
5243 
5244 	return ieee80211_prepare_and_rx_handle(rx, skb, consume);
5245 }
5246 
5247 /*
5248  * This is the actual Rx frames handler. as it belongs to Rx path it must
5249  * be called with rcu_read_lock protection.
5250  */
5251 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
5252 					 struct ieee80211_sta *pubsta,
5253 					 struct sk_buff *skb,
5254 					 struct list_head *list)
5255 {
5256 	struct ieee80211_local *local = hw_to_local(hw);
5257 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5258 	struct ieee80211_sub_if_data *sdata;
5259 	struct ieee80211_hdr *hdr;
5260 	__le16 fc;
5261 	struct ieee80211_rx_data rx;
5262 	struct ieee80211_sub_if_data *prev;
5263 	struct rhlist_head *tmp;
5264 	int err = 0;
5265 
5266 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
5267 	memset(&rx, 0, sizeof(rx));
5268 	rx.skb = skb;
5269 	rx.local = local;
5270 	rx.list = list;
5271 	rx.link_id = -1;
5272 
5273 	if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
5274 		I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5275 
5276 	if (ieee80211_is_mgmt(fc)) {
5277 		/* drop frame if too short for header */
5278 		if (skb->len < ieee80211_hdrlen(fc))
5279 			err = -ENOBUFS;
5280 		else
5281 			err = skb_linearize(skb);
5282 	} else {
5283 		err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
5284 	}
5285 
5286 	if (err) {
5287 		dev_kfree_skb(skb);
5288 		return;
5289 	}
5290 
5291 	hdr = (struct ieee80211_hdr *)skb->data;
5292 	ieee80211_parse_qos(&rx);
5293 	ieee80211_verify_alignment(&rx);
5294 
5295 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
5296 		     ieee80211_is_beacon(hdr->frame_control) ||
5297 		     ieee80211_is_s1g_beacon(hdr->frame_control)))
5298 		ieee80211_scan_rx(local, skb);
5299 
5300 	if (ieee80211_is_data(fc)) {
5301 		struct sta_info *sta, *prev_sta;
5302 		int link_id = -1;
5303 
5304 		if (status->link_valid)
5305 			link_id = status->link_id;
5306 
5307 		if (pubsta) {
5308 			sta = container_of(pubsta, struct sta_info, sta);
5309 			if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5310 				goto out;
5311 
5312 			/*
5313 			 * In MLO connection, fetch the link_id using addr2
5314 			 * when the driver does not pass link_id in status.
5315 			 * When the address translation is already performed by
5316 			 * driver/hw, the valid link_id must be passed in
5317 			 * status.
5318 			 */
5319 
5320 			if (!status->link_valid && pubsta->mlo) {
5321 				struct link_sta_info *link_sta;
5322 
5323 				link_sta = link_sta_info_get_bss(rx.sdata,
5324 								 hdr->addr2);
5325 				if (!link_sta)
5326 					goto out;
5327 
5328 				ieee80211_rx_data_set_link(&rx, link_sta->link_id);
5329 			}
5330 
5331 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5332 				return;
5333 			goto out;
5334 		}
5335 
5336 		prev_sta = NULL;
5337 
5338 		for_each_sta_info(local, hdr->addr2, sta, tmp) {
5339 			if (!prev_sta) {
5340 				prev_sta = sta;
5341 				continue;
5342 			}
5343 
5344 			rx.sdata = prev_sta->sdata;
5345 			if (!status->link_valid && prev_sta->sta.mlo) {
5346 				struct link_sta_info *link_sta;
5347 
5348 				link_sta = link_sta_info_get_bss(rx.sdata,
5349 								 hdr->addr2);
5350 				if (!link_sta)
5351 					continue;
5352 
5353 				link_id = link_sta->link_id;
5354 			}
5355 
5356 			if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5357 				goto out;
5358 
5359 			ieee80211_prepare_and_rx_handle(&rx, skb, false);
5360 
5361 			prev_sta = sta;
5362 		}
5363 
5364 		if (prev_sta) {
5365 			rx.sdata = prev_sta->sdata;
5366 			if (!status->link_valid && prev_sta->sta.mlo) {
5367 				struct link_sta_info *link_sta;
5368 
5369 				link_sta = link_sta_info_get_bss(rx.sdata,
5370 								 hdr->addr2);
5371 				if (!link_sta)
5372 					goto out;
5373 
5374 				link_id = link_sta->link_id;
5375 			}
5376 
5377 			if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5378 				goto out;
5379 
5380 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5381 				return;
5382 			goto out;
5383 		}
5384 	}
5385 
5386 	prev = NULL;
5387 
5388 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
5389 		if (!ieee80211_sdata_running(sdata))
5390 			continue;
5391 
5392 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
5393 		    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5394 			continue;
5395 
5396 		/*
5397 		 * frame is destined for this interface, but if it's
5398 		 * not also for the previous one we handle that after
5399 		 * the loop to avoid copying the SKB once too much
5400 		 */
5401 
5402 		if (!prev) {
5403 			prev = sdata;
5404 			continue;
5405 		}
5406 
5407 		rx.sdata = prev;
5408 		ieee80211_rx_for_interface(&rx, skb, false);
5409 
5410 		prev = sdata;
5411 	}
5412 
5413 	if (prev) {
5414 		rx.sdata = prev;
5415 
5416 		if (ieee80211_rx_for_interface(&rx, skb, true))
5417 			return;
5418 	}
5419 
5420  out:
5421 	dev_kfree_skb(skb);
5422 }
5423 
5424 /*
5425  * This is the receive path handler. It is called by a low level driver when an
5426  * 802.11 MPDU is received from the hardware.
5427  */
5428 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5429 		       struct sk_buff *skb, struct list_head *list)
5430 {
5431 	struct ieee80211_local *local = hw_to_local(hw);
5432 	struct ieee80211_rate *rate = NULL;
5433 	struct ieee80211_supported_band *sband;
5434 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5435 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5436 
5437 	WARN_ON_ONCE(softirq_count() == 0);
5438 
5439 	if (WARN_ON(status->band >= NUM_NL80211_BANDS))
5440 		goto drop;
5441 
5442 	sband = local->hw.wiphy->bands[status->band];
5443 	if (WARN_ON(!sband))
5444 		goto drop;
5445 
5446 	/*
5447 	 * If we're suspending, it is possible although not too likely
5448 	 * that we'd be receiving frames after having already partially
5449 	 * quiesced the stack. We can't process such frames then since
5450 	 * that might, for example, cause stations to be added or other
5451 	 * driver callbacks be invoked.
5452 	 */
5453 	if (unlikely(local->quiescing || local->suspended))
5454 		goto drop;
5455 
5456 	/* We might be during a HW reconfig, prevent Rx for the same reason */
5457 	if (unlikely(local->in_reconfig))
5458 		goto drop;
5459 
5460 	/*
5461 	 * The same happens when we're not even started,
5462 	 * but that's worth a warning.
5463 	 */
5464 	if (WARN_ON(!local->started))
5465 		goto drop;
5466 
5467 	if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC) &&
5468 		   !(status->flag & RX_FLAG_NO_PSDU &&
5469 		     status->zero_length_psdu_type ==
5470 		     IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED))) {
5471 		/*
5472 		 * Validate the rate, unless there was a PLCP error which may
5473 		 * have an invalid rate or the PSDU was not capture and may be
5474 		 * missing rate information.
5475 		 */
5476 
5477 		switch (status->encoding) {
5478 		case RX_ENC_HT:
5479 			/*
5480 			 * rate_idx is MCS index, which can be [0-76]
5481 			 * as documented on:
5482 			 *
5483 			 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
5484 			 *
5485 			 * Anything else would be some sort of driver or
5486 			 * hardware error. The driver should catch hardware
5487 			 * errors.
5488 			 */
5489 			if (WARN(status->rate_idx > 76,
5490 				 "Rate marked as an HT rate but passed "
5491 				 "status->rate_idx is not "
5492 				 "an MCS index [0-76]: %d (0x%02x)\n",
5493 				 status->rate_idx,
5494 				 status->rate_idx))
5495 				goto drop;
5496 			break;
5497 		case RX_ENC_VHT:
5498 			if (WARN_ONCE(status->rate_idx > 11 ||
5499 				      !status->nss ||
5500 				      status->nss > 8,
5501 				      "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
5502 				      status->rate_idx, status->nss))
5503 				goto drop;
5504 			break;
5505 		case RX_ENC_HE:
5506 			if (WARN_ONCE(status->rate_idx > 11 ||
5507 				      !status->nss ||
5508 				      status->nss > 8,
5509 				      "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
5510 				      status->rate_idx, status->nss))
5511 				goto drop;
5512 			break;
5513 		case RX_ENC_EHT:
5514 			if (WARN_ONCE(status->rate_idx > 15 ||
5515 				      !status->nss ||
5516 				      status->nss > 8 ||
5517 				      status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5518 				      "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5519 				      status->rate_idx, status->nss, status->eht.gi))
5520 				goto drop;
5521 			break;
5522 		case RX_ENC_UHR:
5523 			if (WARN_ONCE(!(status->rate_idx <= 15 ||
5524 					status->rate_idx == 17 ||
5525 					status->rate_idx == 19 ||
5526 					status->rate_idx == 20 ||
5527 					status->rate_idx == 23) ||
5528 				      !status->nss ||
5529 				      status->nss > 8 ||
5530 				      status->uhr.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5531 				      "Rate marked as a UHR rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5532 				      status->rate_idx, status->nss, status->uhr.gi))
5533 				goto drop;
5534 			if (WARN_ONCE(status->uhr.elr &&
5535 				      (status->nss != 1 || status->rate_idx > 1 ||
5536 				       status->uhr.gi != NL80211_RATE_INFO_EHT_GI_1_6 ||
5537 				       status->bw != RATE_INFO_BW_20 || status->uhr.im),
5538 				      "bad UHR ELR MCS MCS:%d, NSS:%d, GI:%d, BW:%d, IM:%d\n",
5539 				      status->rate_idx, status->nss, status->uhr.gi,
5540 				      status->bw, status->uhr.im))
5541 				goto drop;
5542 			if (WARN_ONCE(status->uhr.im &&
5543 				      (status->nss != 1 || status->rate_idx == 15),
5544 				      "bad UHR IM MCS MCS:%d, NSS:%d\n",
5545 				      status->rate_idx, status->nss))
5546 				goto drop;
5547 			break;
5548 		default:
5549 			WARN_ON_ONCE(1);
5550 			fallthrough;
5551 		case RX_ENC_LEGACY:
5552 			if (WARN_ON(status->rate_idx >= sband->n_bitrates))
5553 				goto drop;
5554 			rate = &sband->bitrates[status->rate_idx];
5555 		}
5556 	}
5557 
5558 	if (WARN_ON_ONCE(status->link_id >= IEEE80211_LINK_UNSPECIFIED))
5559 		goto drop;
5560 
5561 	status->rx_flags = 0;
5562 
5563 	kcov_remote_start_common(skb_get_kcov_handle(skb));
5564 
5565 	/*
5566 	 * Frames with failed FCS/PLCP checksum are not returned,
5567 	 * all other frames are returned without radiotap header
5568 	 * if it was previously present.
5569 	 * Also, frames with less than 16 bytes are dropped.
5570 	 */
5571 	if (!(status->flag & RX_FLAG_8023))
5572 		skb = ieee80211_rx_monitor(local, skb, rate);
5573 	if (skb) {
5574 		if ((status->flag & RX_FLAG_8023) ||
5575 			ieee80211_is_data_present(hdr->frame_control))
5576 			ieee80211_tpt_led_trig_rx(local, skb->len);
5577 
5578 		if (status->flag & RX_FLAG_8023)
5579 			__ieee80211_rx_handle_8023(hw, pubsta, skb, list);
5580 		else
5581 			__ieee80211_rx_handle_packet(hw, pubsta, skb, list);
5582 	}
5583 
5584 	kcov_remote_stop();
5585 	return;
5586  drop:
5587 	kfree_skb(skb);
5588 }
5589 EXPORT_SYMBOL(ieee80211_rx_list);
5590 
5591 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5592 		       struct sk_buff *skb, struct napi_struct *napi)
5593 {
5594 	struct sk_buff *tmp;
5595 	LIST_HEAD(list);
5596 
5597 
5598 	/*
5599 	 * key references and virtual interfaces are protected using RCU
5600 	 * and this requires that we are in a read-side RCU section during
5601 	 * receive processing
5602 	 */
5603 	rcu_read_lock();
5604 	ieee80211_rx_list(hw, pubsta, skb, &list);
5605 	rcu_read_unlock();
5606 
5607 	if (!napi) {
5608 		netif_receive_skb_list(&list);
5609 		return;
5610 	}
5611 
5612 	list_for_each_entry_safe(skb, tmp, &list, list) {
5613 		skb_list_del_init(skb);
5614 		napi_gro_receive(napi, skb);
5615 	}
5616 }
5617 EXPORT_SYMBOL(ieee80211_rx_napi);
5618 
5619 /* This is a version of the rx handler that can be called from hard irq
5620  * context. Post the skb on the queue and schedule the tasklet */
5621 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
5622 {
5623 	struct ieee80211_local *local = hw_to_local(hw);
5624 
5625 	BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5626 
5627 	skb->pkt_type = IEEE80211_RX_MSG;
5628 	skb_queue_tail(&local->skb_queue, skb);
5629 	tasklet_schedule(&local->tasklet);
5630 }
5631 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
5632