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