xref: /linux/net/mac80211/rx.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
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->extra_len = 0;
2288 
2289 	return entry;
2290 }
2291 
2292 static inline struct ieee80211_fragment_entry *
2293 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2294 			  unsigned int frag, unsigned int seq,
2295 			  int rx_queue, struct ieee80211_hdr *hdr)
2296 {
2297 	struct ieee80211_fragment_entry *entry;
2298 	int i, idx;
2299 
2300 	idx = cache->next;
2301 	for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2302 		struct ieee80211_hdr *f_hdr;
2303 		struct sk_buff *f_skb;
2304 
2305 		idx--;
2306 		if (idx < 0)
2307 			idx = IEEE80211_FRAGMENT_MAX - 1;
2308 
2309 		entry = &cache->entries[idx];
2310 		if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2311 		    entry->rx_queue != rx_queue ||
2312 		    entry->last_frag + 1 != frag)
2313 			continue;
2314 
2315 		f_skb = __skb_peek(&entry->skb_list);
2316 		f_hdr = (struct ieee80211_hdr *) f_skb->data;
2317 
2318 		/*
2319 		 * Check ftype and addresses are equal, else check next fragment
2320 		 */
2321 		if (((hdr->frame_control ^ f_hdr->frame_control) &
2322 		     cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2323 		    !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2324 		    !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2325 			continue;
2326 
2327 		if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2328 			__skb_queue_purge(&entry->skb_list);
2329 			continue;
2330 		}
2331 		return entry;
2332 	}
2333 
2334 	return NULL;
2335 }
2336 
2337 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2338 {
2339 	return rx->key &&
2340 		(rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2341 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2342 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2343 		 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2344 		ieee80211_has_protected(fc);
2345 }
2346 
2347 static ieee80211_rx_result debug_noinline
2348 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2349 {
2350 	struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2351 	struct ieee80211_hdr *hdr;
2352 	u16 sc;
2353 	__le16 fc;
2354 	unsigned int frag, seq;
2355 	struct ieee80211_fragment_entry *entry;
2356 	struct sk_buff *skb;
2357 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2358 
2359 	hdr = (struct ieee80211_hdr *)rx->skb->data;
2360 	fc = hdr->frame_control;
2361 
2362 	if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2363 		return RX_CONTINUE;
2364 
2365 	sc = le16_to_cpu(hdr->seq_ctrl);
2366 	frag = sc & IEEE80211_SCTL_FRAG;
2367 
2368 	if (rx->sta)
2369 		cache = &rx->sta->frags;
2370 
2371 	if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2372 		goto out;
2373 
2374 	if (is_multicast_ether_addr(hdr->addr1))
2375 		return RX_DROP_U_MCAST_FRAGMENT;
2376 
2377 	I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2378 
2379 	if (skb_linearize(rx->skb))
2380 		return RX_DROP_U_OOM;
2381 
2382 	/*
2383 	 *  skb_linearize() might change the skb->data and
2384 	 *  previously cached variables (in this case, hdr) need to
2385 	 *  be refreshed with the new data.
2386 	 */
2387 	hdr = (struct ieee80211_hdr *)rx->skb->data;
2388 	seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2389 
2390 	if (frag == 0) {
2391 		/* This is the first fragment of a new frame. */
2392 		entry = ieee80211_reassemble_add(cache, frag, seq,
2393 						 rx->seqno_idx, &(rx->skb));
2394 		if (requires_sequential_pn(rx, fc)) {
2395 			int queue = rx->security_idx;
2396 
2397 			/* Store CCMP/GCMP PN so that we can verify that the
2398 			 * next fragment has a sequential PN value.
2399 			 */
2400 			entry->check_sequential_pn = true;
2401 			entry->is_protected = true;
2402 			entry->key_color = rx->key->color;
2403 			memcpy(entry->last_pn,
2404 			       rx->key->u.ccmp.rx_pn[queue],
2405 			       IEEE80211_CCMP_PN_LEN);
2406 			BUILD_BUG_ON(offsetof(struct ieee80211_key,
2407 					      u.ccmp.rx_pn) !=
2408 				     offsetof(struct ieee80211_key,
2409 					      u.gcmp.rx_pn));
2410 			BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2411 				     sizeof(rx->key->u.gcmp.rx_pn[queue]));
2412 			BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2413 				     IEEE80211_GCMP_PN_LEN);
2414 		} else if (rx->key &&
2415 			   (ieee80211_has_protected(fc) ||
2416 			    (status->flag & RX_FLAG_DECRYPTED))) {
2417 			entry->is_protected = true;
2418 			entry->key_color = rx->key->color;
2419 		}
2420 		return RX_QUEUED;
2421 	}
2422 
2423 	/* This is a fragment for a frame that should already be pending in
2424 	 * fragment cache. Add this fragment to the end of the pending entry.
2425 	 */
2426 	entry = ieee80211_reassemble_find(cache, frag, seq,
2427 					  rx->seqno_idx, hdr);
2428 	if (!entry) {
2429 		I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2430 		return RX_DROP_U_DEFRAG_MISMATCH;
2431 	}
2432 
2433 	/* "The receiver shall discard MSDUs and MMPDUs whose constituent
2434 	 *  MPDU PN values are not incrementing in steps of 1."
2435 	 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2436 	 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2437 	 */
2438 	if (entry->check_sequential_pn) {
2439 		int i;
2440 		u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2441 
2442 		if (!requires_sequential_pn(rx, fc))
2443 			return RX_DROP_U_NONSEQ_PN;
2444 
2445 		/* Prevent mixed key and fragment cache attacks */
2446 		if (entry->key_color != rx->key->color)
2447 			return RX_DROP_U_BAD_KEY_COLOR;
2448 
2449 		memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2450 		for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2451 			pn[i]++;
2452 			if (pn[i])
2453 				break;
2454 		}
2455 
2456 		rpn = rx->ccm_gcm.pn;
2457 		if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2458 			return RX_DROP_U_REPLAY;
2459 		memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2460 	} else if (entry->is_protected &&
2461 		   (!rx->key ||
2462 		    (!ieee80211_has_protected(fc) &&
2463 		     !(status->flag & RX_FLAG_DECRYPTED)) ||
2464 		    rx->key->color != entry->key_color)) {
2465 		/* Drop this as a mixed key or fragment cache attack, even
2466 		 * if for TKIP Michael MIC should protect us, and WEP is a
2467 		 * lost cause anyway.
2468 		 */
2469 		return RX_DROP_U_EXPECT_DEFRAG_PROT;
2470 	} else if (entry->is_protected && rx->key &&
2471 		   entry->key_color != rx->key->color &&
2472 		   (status->flag & RX_FLAG_DECRYPTED)) {
2473 		return RX_DROP_U_BAD_KEY_COLOR;
2474 	}
2475 
2476 	skb_pull(rx->skb, ieee80211_hdrlen(fc));
2477 	__skb_queue_tail(&entry->skb_list, rx->skb);
2478 	entry->last_frag = frag;
2479 	entry->extra_len += rx->skb->len;
2480 	if (ieee80211_has_morefrags(fc)) {
2481 		rx->skb = NULL;
2482 		return RX_QUEUED;
2483 	}
2484 
2485 	rx->skb = __skb_dequeue(&entry->skb_list);
2486 	if (skb_tailroom(rx->skb) < entry->extra_len) {
2487 		I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2488 		if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2489 					      GFP_ATOMIC))) {
2490 			I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2491 			__skb_queue_purge(&entry->skb_list);
2492 			return RX_DROP_U_OOM;
2493 		}
2494 	}
2495 	while ((skb = __skb_dequeue(&entry->skb_list))) {
2496 		skb_put_data(rx->skb, skb->data, skb->len);
2497 		dev_kfree_skb(skb);
2498 	}
2499 
2500  out:
2501 	ieee80211_led_rx(rx->local);
2502 	if (rx->link_sta)
2503 		rx->link_sta->rx_stats.packets++;
2504 	return RX_CONTINUE;
2505 }
2506 
2507 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2508 {
2509 	if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2510 		return -EACCES;
2511 
2512 	return 0;
2513 }
2514 
2515 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2516 {
2517 	struct sk_buff *skb = rx->skb;
2518 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2519 
2520 	/*
2521 	 * Pass through unencrypted frames if the hardware has
2522 	 * decrypted them already.
2523 	 */
2524 	if (status->flag & RX_FLAG_DECRYPTED)
2525 		return 0;
2526 
2527 	/* Drop unencrypted frames if key is set. */
2528 	if (unlikely(!ieee80211_has_protected(fc) &&
2529 		     !ieee80211_is_any_nullfunc(fc) &&
2530 		     ieee80211_is_data(fc) && rx->key))
2531 		return -EACCES;
2532 
2533 	return 0;
2534 }
2535 
2536 VISIBLE_IF_MAC80211_KUNIT ieee80211_rx_result
2537 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2538 {
2539 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2540 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2541 	__le16 fc = mgmt->frame_control;
2542 
2543 	/*
2544 	 * Pass through unencrypted frames if the hardware has
2545 	 * decrypted them already.
2546 	 */
2547 	if (status->flag & RX_FLAG_DECRYPTED)
2548 		return RX_CONTINUE;
2549 
2550 	/* drop unicast protected dual (that wasn't protected) */
2551 	if (ieee80211_is_action(fc) &&
2552 	    mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
2553 		return RX_DROP_U_UNPROT_DUAL;
2554 
2555 	if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2556 		if (unlikely(!ieee80211_has_protected(fc) &&
2557 			     ieee80211_is_unicast_robust_mgmt_frame(rx->skb))) {
2558 			if (ieee80211_is_deauth(fc) ||
2559 			    ieee80211_is_disassoc(fc)) {
2560 				/*
2561 				 * Permit unprotected deauth/disassoc frames
2562 				 * during 4-way-HS (key is installed after HS).
2563 				 */
2564 				if (!rx->key)
2565 					return RX_CONTINUE;
2566 
2567 				cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2568 							     rx->skb->data,
2569 							     rx->skb->len);
2570 			}
2571 			return RX_DROP_U_UNPROT_UCAST_MGMT;
2572 		}
2573 		/* BIP does not use Protected field, so need to check MMIE */
2574 		if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2575 			     ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2576 			if (ieee80211_is_deauth(fc) ||
2577 			    ieee80211_is_disassoc(fc))
2578 				cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2579 							     rx->skb->data,
2580 							     rx->skb->len);
2581 			return RX_DROP_U_UNPROT_MCAST_MGMT;
2582 		}
2583 		if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2584 			     ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2585 			cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2586 						     rx->skb->data,
2587 						     rx->skb->len);
2588 			return RX_DROP_U_UNPROT_BEACON;
2589 		}
2590 		/*
2591 		 * When using MFP, Action frames are not allowed prior to
2592 		 * having configured keys.
2593 		 */
2594 		if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2595 			     ieee80211_is_robust_mgmt_frame(rx->skb)))
2596 			return RX_DROP_U_UNPROT_ACTION;
2597 
2598 		/* drop unicast public action frames when using MPF */
2599 		if (is_unicast_ether_addr(mgmt->da) &&
2600 		    ieee80211_is_protected_dual_of_public_action(rx->skb))
2601 			return RX_DROP_U_UNPROT_UNICAST_PUB_ACTION;
2602 	}
2603 
2604 	/*
2605 	 * Drop robust action frames before assoc regardless of MFP state,
2606 	 * after assoc we also have decided on MFP or not.
2607 	 */
2608 	if (ieee80211_is_action(fc) &&
2609 	    ieee80211_is_robust_mgmt_frame(rx->skb) &&
2610 	    (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))
2611 		return RX_DROP_U_UNPROT_ROBUST_ACTION;
2612 
2613 	/*
2614 	 * Drop unprotected (Re)Association Request/Response frame received from
2615 	 * an EPP Peer.
2616 	 */
2617 	if (!ieee80211_has_protected(fc) &&
2618 	    ieee80211_require_encrypted_assoc(fc, rx->sta))
2619 		return RX_DROP_U_UNPROT_UCAST_MGMT;
2620 
2621 	return RX_CONTINUE;
2622 }
2623 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_drop_unencrypted_mgmt);
2624 
2625 static ieee80211_rx_result
2626 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2627 {
2628 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2629 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2630 	bool check_port_control = false;
2631 	struct ethhdr *ehdr;
2632 	int ret;
2633 
2634 	*port_control = false;
2635 	if (ieee80211_has_a4(hdr->frame_control) &&
2636 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2637 		return RX_DROP_U_UNEXPECTED_VLAN_4ADDR;
2638 
2639 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2640 	    !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2641 		if (!sdata->u.mgd.use_4addr)
2642 			return RX_DROP_U_UNEXPECTED_STA_4ADDR;
2643 		else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2644 			check_port_control = true;
2645 	}
2646 
2647 	if (is_multicast_ether_addr(hdr->addr1) &&
2648 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2649 		return RX_DROP_U_UNEXPECTED_VLAN_MCAST;
2650 
2651 	ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2652 	if (ret < 0)
2653 		return RX_DROP_U_INVALID_8023;
2654 
2655 	ehdr = (struct ethhdr *) rx->skb->data;
2656 	if (ehdr->h_proto == rx->sdata->control_port_protocol)
2657 		*port_control = true;
2658 	else if (check_port_control)
2659 		return RX_DROP_U_NOT_PORT_CONTROL;
2660 
2661 	return RX_CONTINUE;
2662 }
2663 
2664 bool ieee80211_is_our_addr(struct ieee80211_sub_if_data *sdata,
2665 			   const u8 *addr, int *out_link_id)
2666 {
2667 	unsigned int link_id;
2668 
2669 	/* non-MLO, or MLD address replaced by hardware */
2670 	if (ether_addr_equal(sdata->vif.addr, addr))
2671 		return true;
2672 
2673 	if (!ieee80211_vif_is_mld(&sdata->vif))
2674 		return false;
2675 
2676 	for (link_id = 0; link_id < ARRAY_SIZE(sdata->vif.link_conf); link_id++) {
2677 		struct ieee80211_bss_conf *conf;
2678 
2679 		conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2680 
2681 		if (!conf)
2682 			continue;
2683 		if (ether_addr_equal(conf->addr, addr)) {
2684 			if (out_link_id)
2685 				*out_link_id = link_id;
2686 			return true;
2687 		}
2688 	}
2689 
2690 	return false;
2691 }
2692 
2693 /*
2694  * requires that rx->skb is a frame with ethernet header
2695  */
2696 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2697 {
2698 	static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2699 		= { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2700 	struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2701 
2702 	/*
2703 	 * Allow EAPOL frames to us/the PAE group address regardless of
2704 	 * whether the frame was encrypted or not, and always disallow
2705 	 * all other destination addresses for them.
2706 	 */
2707 	if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2708 		return ieee80211_is_our_addr(rx->sdata, ehdr->h_dest, NULL) ||
2709 		       ether_addr_equal(ehdr->h_dest, pae_group_addr);
2710 
2711 	if (ieee80211_802_1x_port_control(rx) ||
2712 	    ieee80211_drop_unencrypted(rx, fc))
2713 		return false;
2714 
2715 	return true;
2716 }
2717 
2718 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2719 						 struct ieee80211_rx_data *rx)
2720 {
2721 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2722 	struct net_device *dev = sdata->dev;
2723 
2724 	if (unlikely((skb->protocol == sdata->control_port_protocol ||
2725 		     (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2726 		      !sdata->control_port_no_preauth)) &&
2727 		     sdata->control_port_over_nl80211)) {
2728 		struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2729 		bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2730 
2731 		cfg80211_rx_control_port(dev, skb, noencrypt, rx->link_id);
2732 		dev_kfree_skb(skb);
2733 	} else {
2734 		struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2735 
2736 		memset(skb->cb, 0, sizeof(skb->cb));
2737 
2738 		/*
2739 		 * 802.1X over 802.11 requires that the authenticator address
2740 		 * be used for EAPOL frames. However, 802.1X allows the use of
2741 		 * the PAE group address instead. If the interface is part of
2742 		 * a bridge and we pass the frame with the PAE group address,
2743 		 * then the bridge will forward it to the network (even if the
2744 		 * client was not associated yet), which isn't supposed to
2745 		 * happen.
2746 		 * To avoid that, rewrite the destination address to our own
2747 		 * address, so that the authenticator (e.g. hostapd) will see
2748 		 * the frame, but bridge won't forward it anywhere else. Note
2749 		 * that due to earlier filtering, the only other address can
2750 		 * be the PAE group address, unless the hardware allowed them
2751 		 * through in 802.3 offloaded mode.
2752 		 */
2753 		if (unlikely(skb->protocol == sdata->control_port_protocol &&
2754 			     !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2755 			ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2756 
2757 		/* deliver to local stack */
2758 		if (rx->list)
2759 			list_add_tail(&skb->list, rx->list);
2760 		else
2761 			netif_receive_skb(skb);
2762 	}
2763 }
2764 
2765 /*
2766  * requires that rx->skb is a frame with ethernet header
2767  */
2768 static void
2769 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2770 {
2771 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2772 	struct net_device *dev = sdata->dev;
2773 	struct sk_buff *skb, *xmit_skb;
2774 	struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2775 	struct sta_info *dsta;
2776 
2777 	skb = rx->skb;
2778 	xmit_skb = NULL;
2779 
2780 	dev_sw_netstats_rx_add(dev, skb->len);
2781 
2782 	if (rx->sta) {
2783 		/* The seqno index has the same property as needed
2784 		 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2785 		 * for non-QoS-data frames. Here we know it's a data
2786 		 * frame, so count MSDUs.
2787 		 */
2788 		u64_stats_update_begin(&rx->link_sta->rx_stats.syncp);
2789 		u64_stats_inc(&rx->link_sta->rx_stats.msdu[rx->seqno_idx]);
2790 		u64_stats_update_end(&rx->link_sta->rx_stats.syncp);
2791 	}
2792 
2793 	if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2794 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2795 	    !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2796 	    ehdr->h_proto != rx->sdata->control_port_protocol &&
2797 	    (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2798 		if (is_multicast_ether_addr(ehdr->h_dest) &&
2799 		    ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2800 			/*
2801 			 * send multicast frames both to higher layers in
2802 			 * local net stack and back to the wireless medium
2803 			 */
2804 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
2805 			if (!xmit_skb)
2806 				net_info_ratelimited("%s: failed to clone multicast frame\n",
2807 						    dev->name);
2808 		} else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2809 			   !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2810 			dsta = sta_info_get(sdata, ehdr->h_dest);
2811 			if (dsta) {
2812 				/*
2813 				 * The destination station is associated to
2814 				 * this AP (in this VLAN), so send the frame
2815 				 * directly to it and do not pass it to local
2816 				 * net stack.
2817 				 */
2818 				xmit_skb = skb;
2819 				skb = NULL;
2820 			}
2821 		}
2822 	}
2823 
2824 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2825 	if (skb) {
2826 		/* 'align' will only take the values 0 or 2 here since all
2827 		 * frames are required to be aligned to 2-byte boundaries
2828 		 * when being passed to mac80211; the code here works just
2829 		 * as well if that isn't true, but mac80211 assumes it can
2830 		 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2831 		 */
2832 		int align;
2833 
2834 		align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2835 		if (align) {
2836 			if (WARN_ON(skb_headroom(skb) < 3)) {
2837 				dev_kfree_skb(skb);
2838 				skb = NULL;
2839 			} else {
2840 				u8 *data = skb->data;
2841 				size_t len = skb_headlen(skb);
2842 				skb->data -= align;
2843 				memmove(skb->data, data, len);
2844 				skb_set_tail_pointer(skb, len);
2845 			}
2846 		}
2847 	}
2848 #endif
2849 
2850 	if (skb) {
2851 		skb->protocol = eth_type_trans(skb, dev);
2852 		ieee80211_deliver_skb_to_local_stack(skb, rx);
2853 	}
2854 
2855 	if (xmit_skb) {
2856 		/*
2857 		 * Send to wireless media and increase priority by 256 to
2858 		 * keep the received priority instead of reclassifying
2859 		 * the frame (see cfg80211_classify8021d).
2860 		 */
2861 		xmit_skb->priority += 256;
2862 		xmit_skb->protocol = htons(ETH_P_802_3);
2863 		skb_reset_network_header(xmit_skb);
2864 		skb_reset_mac_header(xmit_skb);
2865 		dev_queue_xmit(xmit_skb);
2866 	}
2867 }
2868 
2869 #ifdef CONFIG_MAC80211_MESH
2870 static bool
2871 ieee80211_rx_mesh_fast_forward(struct ieee80211_sub_if_data *sdata,
2872 			       struct sk_buff *skb, int hdrlen)
2873 {
2874 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2875 	struct ieee80211_mesh_fast_tx_key key = {
2876 		.type = MESH_FAST_TX_TYPE_FORWARDED
2877 	};
2878 	struct ieee80211_mesh_fast_tx *entry;
2879 	struct ieee80211s_hdr *mesh_hdr;
2880 	struct tid_ampdu_tx *tid_tx;
2881 	struct sta_info *sta;
2882 	struct ethhdr eth;
2883 	u8 tid;
2884 
2885 	mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(eth));
2886 	if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2887 		ether_addr_copy(key.addr, mesh_hdr->eaddr1);
2888 	else if (!(mesh_hdr->flags & MESH_FLAGS_AE))
2889 		ether_addr_copy(key.addr, skb->data);
2890 	else
2891 		return false;
2892 
2893 	entry = mesh_fast_tx_get(sdata, &key);
2894 	if (!entry)
2895 		return false;
2896 
2897 	sta = rcu_dereference(entry->mpath->next_hop);
2898 	if (!sta)
2899 		return false;
2900 
2901 	if (skb_linearize(skb))
2902 		return false;
2903 
2904 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2905 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
2906 	if (tid_tx) {
2907 		if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
2908 			return false;
2909 
2910 		if (tid_tx->timeout)
2911 			tid_tx->last_tx = jiffies;
2912 	}
2913 
2914 	ieee80211_aggr_check(sdata, sta, skb);
2915 
2916 	if (ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
2917 					    &skb->protocol))
2918 		hdrlen += ETH_ALEN;
2919 	else
2920 		skb->protocol = htons(skb->len - hdrlen);
2921 	skb_set_network_header(skb, hdrlen + 2);
2922 
2923 	skb->dev = sdata->dev;
2924 	memcpy(&eth, skb->data, ETH_HLEN - 2);
2925 	skb_pull(skb, 2);
2926 	__ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
2927 			      eth.h_dest, eth.h_source);
2928 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2929 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2930 
2931 	return true;
2932 }
2933 #endif
2934 
2935 static ieee80211_rx_result
2936 ieee80211_rx_mesh_data(struct ieee80211_sub_if_data *sdata, struct sta_info *sta,
2937 		       struct sk_buff *skb)
2938 {
2939 #ifdef CONFIG_MAC80211_MESH
2940 	struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2941 	struct ieee80211_local *local = sdata->local;
2942 	uint16_t fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA;
2943 	struct ieee80211_hdr hdr = {
2944 		.frame_control = cpu_to_le16(fc)
2945 	};
2946 	struct ieee80211_hdr *fwd_hdr;
2947 	struct ieee80211s_hdr *mesh_hdr;
2948 	struct ieee80211_tx_info *info;
2949 	struct sk_buff *fwd_skb;
2950 	struct ethhdr *eth;
2951 	bool multicast;
2952 	int tailroom = 0;
2953 	int hdrlen, mesh_hdrlen;
2954 	u8 *qos;
2955 
2956 	if (!ieee80211_vif_is_mesh(&sdata->vif))
2957 		return RX_CONTINUE;
2958 
2959 	if (!pskb_may_pull(skb, sizeof(*eth) + 6))
2960 		return RX_DROP_U_RUNT_MESH_DATA;
2961 
2962 	mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(*eth));
2963 	mesh_hdrlen = ieee80211_get_mesh_hdrlen(mesh_hdr);
2964 
2965 	if (!pskb_may_pull(skb, sizeof(*eth) + mesh_hdrlen))
2966 		return RX_DROP_U_RUNT_MESH_DATA;
2967 
2968 	eth = (struct ethhdr *)skb->data;
2969 	multicast = is_multicast_ether_addr(eth->h_dest);
2970 
2971 	mesh_hdr = (struct ieee80211s_hdr *)(eth + 1);
2972 	if (!mesh_hdr->ttl)
2973 		return RX_DROP_U_MESH_NO_TTL;
2974 
2975 	/* frame is in RMC, don't forward */
2976 	if (is_multicast_ether_addr(eth->h_dest) &&
2977 	    mesh_rmc_check(sdata, eth->h_source, mesh_hdr))
2978 		return RX_DROP_U_MESH_RMC;
2979 
2980 	/* forward packet */
2981 	if (sdata->crypto_tx_tailroom_needed_cnt)
2982 		tailroom = IEEE80211_ENCRYPT_TAILROOM;
2983 
2984 	if (mesh_hdr->flags & MESH_FLAGS_AE) {
2985 		struct mesh_path *mppath;
2986 		char *proxied_addr;
2987 		bool update = false;
2988 
2989 		if (multicast)
2990 			proxied_addr = mesh_hdr->eaddr1;
2991 		else if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2992 			/* has_a4 already checked in ieee80211_rx_mesh_check */
2993 			proxied_addr = mesh_hdr->eaddr2;
2994 		else
2995 			return RX_DROP_U_MESH_BAD_AE;
2996 
2997 		rcu_read_lock();
2998 		mppath = mpp_path_lookup(sdata, proxied_addr);
2999 		if (!mppath) {
3000 			mpp_path_add(sdata, proxied_addr, eth->h_source);
3001 		} else {
3002 			spin_lock_bh(&mppath->state_lock);
3003 			if (!ether_addr_equal(mppath->mpp, eth->h_source)) {
3004 				memcpy(mppath->mpp, eth->h_source, ETH_ALEN);
3005 				update = true;
3006 			}
3007 			mppath->exp_time = jiffies;
3008 			spin_unlock_bh(&mppath->state_lock);
3009 		}
3010 
3011 		/* flush fast xmit cache if the address path changed */
3012 		if (update)
3013 			mesh_fast_tx_flush_addr(sdata, proxied_addr);
3014 
3015 		rcu_read_unlock();
3016 	}
3017 
3018 	/* Frame has reached destination.  Don't forward */
3019 	if (ether_addr_equal(sdata->vif.addr, eth->h_dest))
3020 		goto rx_accept;
3021 
3022 	if (!--mesh_hdr->ttl) {
3023 		if (multicast)
3024 			goto rx_accept;
3025 
3026 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
3027 		return RX_DROP_U_MESH_TTL_EXPIRED;
3028 	}
3029 
3030 	if (!ifmsh->mshcfg.dot11MeshForwarding) {
3031 		if (is_multicast_ether_addr(eth->h_dest))
3032 			goto rx_accept;
3033 
3034 		return RX_DROP_U_MESH_NOT_FORWARDING;
3035 	}
3036 
3037 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[skb->priority]);
3038 
3039 	if (!multicast &&
3040 	    ieee80211_rx_mesh_fast_forward(sdata, skb, mesh_hdrlen))
3041 		return RX_QUEUED;
3042 
3043 	ieee80211_fill_mesh_addresses(&hdr, &hdr.frame_control,
3044 				      eth->h_dest, eth->h_source);
3045 	hdrlen = ieee80211_hdrlen(hdr.frame_control);
3046 	if (multicast) {
3047 		int extra_head = sizeof(struct ieee80211_hdr) - sizeof(*eth);
3048 
3049 		fwd_skb = skb_copy_expand(skb, local->tx_headroom + extra_head +
3050 					       IEEE80211_ENCRYPT_HEADROOM,
3051 					  tailroom, GFP_ATOMIC);
3052 		if (!fwd_skb)
3053 			goto rx_accept;
3054 	} else {
3055 		fwd_skb = skb;
3056 		skb = NULL;
3057 
3058 		if (skb_cow_head(fwd_skb, hdrlen - sizeof(struct ethhdr)))
3059 			return RX_DROP_U_OOM;
3060 
3061 		if (skb_linearize(fwd_skb))
3062 			return RX_DROP_U_OOM;
3063 	}
3064 
3065 	fwd_hdr = skb_push(fwd_skb, hdrlen - sizeof(struct ethhdr));
3066 	memcpy(fwd_hdr, &hdr, hdrlen - 2);
3067 	qos = ieee80211_get_qos_ctl(fwd_hdr);
3068 	qos[0] = qos[1] = 0;
3069 
3070 	skb_reset_mac_header(fwd_skb);
3071 	hdrlen += mesh_hdrlen;
3072 	if (ieee80211_get_8023_tunnel_proto(fwd_skb->data + hdrlen,
3073 					    &fwd_skb->protocol))
3074 		hdrlen += ETH_ALEN;
3075 	else
3076 		fwd_skb->protocol = htons(fwd_skb->len - hdrlen);
3077 	skb_set_network_header(fwd_skb, hdrlen + 2);
3078 
3079 	info = IEEE80211_SKB_CB(fwd_skb);
3080 	memset(info, 0, sizeof(*info));
3081 	info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3082 	info->control.vif = &sdata->vif;
3083 	info->control.jiffies = jiffies;
3084 	fwd_skb->dev = sdata->dev;
3085 	if (multicast) {
3086 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
3087 		memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
3088 		/* update power mode indication when forwarding */
3089 		ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
3090 	} else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
3091 		/* mesh power mode flags updated in mesh_nexthop_lookup */
3092 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
3093 	} else {
3094 		/* unable to resolve next hop */
3095 		if (sta)
3096 			mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
3097 					   hdr.addr3, 0,
3098 					   WLAN_REASON_MESH_PATH_NOFORWARD,
3099 					   sta->sta.addr);
3100 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
3101 		kfree_skb(fwd_skb);
3102 		goto rx_accept;
3103 	}
3104 
3105 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
3106 	ieee80211_set_qos_hdr(sdata, fwd_skb);
3107 	ieee80211_add_pending_skb(local, fwd_skb);
3108 
3109 rx_accept:
3110 	if (!skb)
3111 		return RX_QUEUED;
3112 
3113 	ieee80211_strip_8023_mesh_hdr(skb);
3114 #endif
3115 
3116 	return RX_CONTINUE;
3117 }
3118 
3119 static ieee80211_rx_result debug_noinline
3120 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
3121 {
3122 	struct net_device *dev = rx->sdata->dev;
3123 	struct sk_buff *skb = rx->skb;
3124 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3125 	__le16 fc = hdr->frame_control;
3126 	struct sk_buff_head frame_list;
3127 	struct ethhdr ethhdr;
3128 	const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
3129 
3130 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3131 		check_da = NULL;
3132 		check_sa = NULL;
3133 	} else switch (rx->sdata->vif.type) {
3134 		case NL80211_IFTYPE_AP:
3135 		case NL80211_IFTYPE_AP_VLAN:
3136 			check_da = NULL;
3137 			break;
3138 		case NL80211_IFTYPE_STATION:
3139 			if (!test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
3140 				check_sa = NULL;
3141 			break;
3142 		case NL80211_IFTYPE_MESH_POINT:
3143 			check_sa = NULL;
3144 			check_da = NULL;
3145 			break;
3146 		default:
3147 			break;
3148 	}
3149 
3150 	skb->dev = dev;
3151 	__skb_queue_head_init(&frame_list);
3152 
3153 	if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
3154 					  rx->sdata->vif.addr,
3155 					  rx->sdata->vif.type,
3156 					  data_offset, true))
3157 		return RX_DROP_U_BAD_AMSDU;
3158 
3159 	if (rx->sta->amsdu_mesh_control < 0) {
3160 		s8 valid = -1;
3161 		int i;
3162 
3163 		for (i = 0; i <= 2; i++) {
3164 			if (!ieee80211_is_valid_amsdu(skb, i))
3165 				continue;
3166 
3167 			if (valid >= 0) {
3168 				/* ambiguous */
3169 				valid = -1;
3170 				break;
3171 			}
3172 
3173 			valid = i;
3174 		}
3175 
3176 		rx->sta->amsdu_mesh_control = valid;
3177 	}
3178 
3179 	ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
3180 				 rx->sdata->vif.type,
3181 				 rx->local->hw.extra_tx_headroom,
3182 				 check_da, check_sa,
3183 				 rx->sta->amsdu_mesh_control);
3184 
3185 	while (!skb_queue_empty(&frame_list)) {
3186 		rx->skb = __skb_dequeue(&frame_list);
3187 
3188 		switch (ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb)) {
3189 		case RX_QUEUED:
3190 			break;
3191 		case RX_CONTINUE:
3192 			if (ieee80211_frame_allowed(rx, fc)) {
3193 				ieee80211_deliver_skb(rx);
3194 				break;
3195 			}
3196 			fallthrough;
3197 		default:
3198 			dev_kfree_skb(rx->skb);
3199 		}
3200 	}
3201 
3202 	return RX_QUEUED;
3203 }
3204 
3205 static ieee80211_rx_result debug_noinline
3206 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
3207 {
3208 	struct sk_buff *skb = rx->skb;
3209 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3210 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3211 	__le16 fc = hdr->frame_control;
3212 
3213 	if (!(status->rx_flags & IEEE80211_RX_AMSDU))
3214 		return RX_CONTINUE;
3215 
3216 	if (unlikely(!ieee80211_is_data(fc)))
3217 		return RX_CONTINUE;
3218 
3219 	if (unlikely(!ieee80211_is_data_present(fc)))
3220 		return RX_DROP_U_AMSDU_WITHOUT_DATA;
3221 
3222 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3223 		switch (rx->sdata->vif.type) {
3224 		case NL80211_IFTYPE_AP_VLAN:
3225 			if (!rx->sdata->u.vlan.sta)
3226 				return RX_DROP_U_BAD_4ADDR;
3227 			break;
3228 		case NL80211_IFTYPE_STATION:
3229 			if (!rx->sdata->u.mgd.use_4addr)
3230 				return RX_DROP_U_BAD_4ADDR;
3231 			break;
3232 		case NL80211_IFTYPE_MESH_POINT:
3233 			break;
3234 		default:
3235 			return RX_DROP_U_BAD_4ADDR;
3236 		}
3237 	}
3238 
3239 	if (is_multicast_ether_addr(hdr->addr1) || !rx->sta)
3240 		return RX_DROP_U_BAD_AMSDU;
3241 
3242 	if (rx->key) {
3243 		/*
3244 		 * We should not receive A-MSDUs on pre-HT connections,
3245 		 * and HT connections cannot use old ciphers. Thus drop
3246 		 * them, as in those cases we couldn't even have SPP
3247 		 * A-MSDUs or such.
3248 		 */
3249 		switch (rx->key->conf.cipher) {
3250 		case WLAN_CIPHER_SUITE_WEP40:
3251 		case WLAN_CIPHER_SUITE_WEP104:
3252 		case WLAN_CIPHER_SUITE_TKIP:
3253 			return RX_DROP_U_BAD_AMSDU_CIPHER;
3254 		default:
3255 			break;
3256 		}
3257 	}
3258 
3259 	return __ieee80211_rx_h_amsdu(rx, 0);
3260 }
3261 
3262 static ieee80211_rx_result debug_noinline
3263 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
3264 {
3265 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3266 	struct ieee80211_local *local = rx->local;
3267 	struct net_device *dev = sdata->dev;
3268 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
3269 	__le16 fc = hdr->frame_control;
3270 	ieee80211_rx_result res;
3271 	bool port_control;
3272 
3273 	if (unlikely(!ieee80211_is_data(hdr->frame_control)))
3274 		return RX_CONTINUE;
3275 
3276 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3277 		return RX_DROP_U_NULL_DATA;
3278 
3279 	/* Send unexpected-4addr-frame event to hostapd */
3280 	if (ieee80211_has_a4(hdr->frame_control) &&
3281 	    sdata->vif.type == NL80211_IFTYPE_AP) {
3282 		if (rx->sta &&
3283 		    !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3284 			cfg80211_rx_unexpected_4addr_frame(
3285 				rx->sdata->dev, rx->sta->sta.addr, rx->link_id,
3286 				GFP_ATOMIC);
3287 		return RX_DROP_U_UNEXPECTED_4ADDR;
3288 	}
3289 
3290 	res = __ieee80211_data_to_8023(rx, &port_control);
3291 	if (unlikely(res != RX_CONTINUE))
3292 		return res;
3293 
3294 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3295 	if (res != RX_CONTINUE)
3296 		return res;
3297 
3298 	if (!ieee80211_frame_allowed(rx, fc))
3299 		return RX_DROP_U_PORT_CONTROL;
3300 
3301 	/* directly handle TDLS channel switch requests/responses */
3302 	if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3303 						cpu_to_be16(ETH_P_TDLS))) {
3304 		struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3305 
3306 		if (pskb_may_pull(rx->skb,
3307 				  offsetof(struct ieee80211_tdls_data, u)) &&
3308 		    tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3309 		    tf->category == WLAN_CATEGORY_TDLS &&
3310 		    (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3311 		     tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3312 			rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3313 			__ieee80211_queue_skb_to_iface(sdata, rx->link_sta,
3314 						       rx->skb);
3315 			return RX_QUEUED;
3316 		}
3317 	}
3318 
3319 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3320 	    unlikely(port_control) && sdata->bss) {
3321 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3322 				     u.ap);
3323 		dev = sdata->dev;
3324 		rx->sdata = sdata;
3325 	}
3326 
3327 	rx->skb->dev = dev;
3328 
3329 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3330 	    local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3331 	    !is_multicast_ether_addr(
3332 		    ((struct ethhdr *)rx->skb->data)->h_dest) &&
3333 	    (!local->scanning &&
3334 	     !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3335 		mod_timer(&local->dynamic_ps_timer, jiffies +
3336 			  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3337 
3338 	ieee80211_deliver_skb(rx);
3339 
3340 	return RX_QUEUED;
3341 }
3342 
3343 static ieee80211_rx_result debug_noinline
3344 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3345 {
3346 	struct sk_buff *skb = rx->skb;
3347 	struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3348 	struct tid_ampdu_rx *tid_agg_rx;
3349 	u16 start_seq_num;
3350 	u16 tid;
3351 
3352 	if (likely(!ieee80211_is_ctl(bar->frame_control)))
3353 		return RX_CONTINUE;
3354 
3355 	if (ieee80211_is_back_req(bar->frame_control)) {
3356 		struct {
3357 			__le16 control, start_seq_num;
3358 		} __packed bar_data;
3359 		struct ieee80211_event event = {
3360 			.type = BAR_RX_EVENT,
3361 		};
3362 
3363 		if (!rx->sta)
3364 			return RX_DROP_U_UNKNOWN_STA;
3365 
3366 		if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3367 				  &bar_data, sizeof(bar_data)))
3368 			return RX_DROP_U_RUNT_BAR;
3369 
3370 		tid = le16_to_cpu(bar_data.control) >> 12;
3371 
3372 		if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3373 		    !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3374 			ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3375 					     WLAN_BACK_RECIPIENT,
3376 					     WLAN_REASON_QSTA_REQUIRE_SETUP,
3377 					     ieee80211_s1g_use_ndp_ba(rx->sdata,
3378 								      rx->sta));
3379 
3380 		tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3381 		if (!tid_agg_rx)
3382 			return RX_DROP_U_BAR_OUTSIDE_SESSION;
3383 
3384 		start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3385 		event.u.ba.tid = tid;
3386 		event.u.ba.ssn = start_seq_num;
3387 		event.u.ba.sta = &rx->sta->sta;
3388 
3389 		/* reset session timer */
3390 		if (tid_agg_rx->timeout)
3391 			mod_timer(&tid_agg_rx->session_timer,
3392 				  TU_TO_EXP_TIME(tid_agg_rx->timeout));
3393 
3394 		spin_lock(&tid_agg_rx->reorder_lock);
3395 		/* release stored frames up to start of BAR */
3396 		ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3397 						 start_seq_num, frames);
3398 		spin_unlock(&tid_agg_rx->reorder_lock);
3399 
3400 		drv_event_callback(rx->local, rx->sdata, &event);
3401 
3402 		kfree_skb(skb);
3403 		return RX_QUEUED;
3404 	}
3405 
3406 	return RX_DROP_U_CTRL_FRAME;
3407 }
3408 
3409 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3410 					   struct ieee80211_mgmt *mgmt,
3411 					   size_t len)
3412 {
3413 	struct ieee80211_local *local = sdata->local;
3414 	struct sk_buff *skb;
3415 	struct ieee80211_mgmt *resp;
3416 
3417 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3418 		/* Not to own unicast address */
3419 		return;
3420 	}
3421 
3422 	if (!ether_addr_equal(mgmt->sa, sdata->vif.cfg.ap_addr) ||
3423 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
3424 		/* Not from the current AP or not associated yet. */
3425 		return;
3426 	}
3427 
3428 	if (len < IEEE80211_MIN_ACTION_SIZE(sa_query)) {
3429 		/* Too short SA Query request frame */
3430 		return;
3431 	}
3432 
3433 	skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3434 	if (skb == NULL)
3435 		return;
3436 
3437 	skb_reserve(skb, local->hw.extra_tx_headroom);
3438 	resp = skb_put_zero(skb, IEEE80211_MIN_ACTION_SIZE(sa_query));
3439 	memcpy(resp->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
3440 	memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3441 	memcpy(resp->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
3442 	resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3443 					  IEEE80211_STYPE_ACTION);
3444 	resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3445 	resp->u.action.action_code = WLAN_ACTION_SA_QUERY_RESPONSE;
3446 	memcpy(resp->u.action.sa_query.trans_id,
3447 	       mgmt->u.action.sa_query.trans_id,
3448 	       WLAN_SA_QUERY_TR_ID_LEN);
3449 
3450 	ieee80211_tx_skb(sdata, skb);
3451 }
3452 
3453 static void
3454 ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx)
3455 {
3456 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3457 	struct ieee80211_bss_conf *bss_conf;
3458 	const struct element *ie;
3459 	size_t baselen;
3460 
3461 	if (!wiphy_ext_feature_isset(rx->local->hw.wiphy,
3462 				     NL80211_EXT_FEATURE_BSS_COLOR))
3463 		return;
3464 
3465 	if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION))
3466 		return;
3467 
3468 	bss_conf = rx->link->conf;
3469 	if (bss_conf->csa_active || bss_conf->color_change_active ||
3470 	    !bss_conf->he_bss_color.enabled)
3471 		return;
3472 
3473 	baselen = mgmt->u.beacon.variable - rx->skb->data;
3474 	if (baselen > rx->skb->len)
3475 		return;
3476 
3477 	ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
3478 				    mgmt->u.beacon.variable,
3479 				    rx->skb->len - baselen);
3480 	if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) &&
3481 	    ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) {
3482 		const struct ieee80211_he_operation *he_oper;
3483 		u8 color;
3484 
3485 		he_oper = (void *)(ie->data + 1);
3486 		if (le32_get_bits(he_oper->he_oper_params,
3487 				  IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED))
3488 			return;
3489 
3490 		color = le32_get_bits(he_oper->he_oper_params,
3491 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3492 		if (color == bss_conf->he_bss_color.color)
3493 			ieee80211_obss_color_collision_notify(&rx->sdata->vif,
3494 							      BIT_ULL(color),
3495 							      bss_conf->link_id);
3496 	}
3497 }
3498 
3499 static ieee80211_rx_result debug_noinline
3500 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3501 {
3502 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3503 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3504 
3505 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3506 		return RX_CONTINUE;
3507 
3508 	/*
3509 	 * From here on, look only at management frames.
3510 	 * Data and control frames are already handled,
3511 	 * and unknown (reserved) frames are useless.
3512 	 */
3513 	if (rx->skb->len < 24)
3514 		return RX_DROP_U_RUNT_MGMT;
3515 
3516 	if (!ieee80211_is_mgmt(mgmt->frame_control))
3517 		return RX_DROP_U_EXPECTED_MGMT;
3518 
3519 	/* drop too small action frames */
3520 	if (ieee80211_is_action(mgmt->frame_control) &&
3521 	    rx->skb->len < IEEE80211_MIN_ACTION_SIZE(category))
3522 		return RX_DROP_U_RUNT_ACTION;
3523 
3524 	/* Drop non-broadcast Beacon frames */
3525 	if (ieee80211_is_beacon(mgmt->frame_control) &&
3526 	    !is_broadcast_ether_addr(mgmt->da))
3527 		return RX_DROP_U_NONBCAST_BEACON;
3528 
3529 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3530 	    ieee80211_is_beacon(mgmt->frame_control) &&
3531 	    !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3532 		int sig = 0;
3533 
3534 		/* sw bss color collision detection */
3535 		ieee80211_rx_check_bss_color_collision(rx);
3536 
3537 		if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3538 		    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3539 			sig = status->signal;
3540 
3541 		cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3542 						rx->skb->data, rx->skb->len,
3543 						ieee80211_rx_status_to_khz(status),
3544 						sig);
3545 		rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3546 	}
3547 
3548 	return ieee80211_drop_unencrypted_mgmt(rx);
3549 }
3550 
3551 static bool
3552 ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3553 {
3554 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
3555 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3556 
3557 	/* TWT actions are only supported in AP for the moment */
3558 	if (sdata->vif.type != NL80211_IFTYPE_AP)
3559 		return false;
3560 
3561 	if (!rx->local->ops->add_twt_setup)
3562 		return false;
3563 
3564 	if (!sdata->vif.bss_conf.twt_responder)
3565 		return false;
3566 
3567 	if (!rx->sta)
3568 		return false;
3569 
3570 	switch (mgmt->u.action.action_code) {
3571 	case WLAN_S1G_TWT_SETUP: {
3572 		struct ieee80211_twt_setup *twt;
3573 
3574 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) +
3575 				   sizeof(struct ieee80211_twt_setup) +
3576 				   2 /* TWT req_type agrt */)
3577 			break;
3578 
3579 		twt = (void *)mgmt->u.action.s1g.variable;
3580 		if (twt->element_id != WLAN_EID_S1G_TWT)
3581 			break;
3582 
3583 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) +
3584 				   3 + /* token + tlv */
3585 				   twt->length)
3586 			break;
3587 
3588 		return true; /* queue the frame */
3589 	}
3590 	case WLAN_S1G_TWT_TEARDOWN:
3591 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE(action_code) + 1)
3592 			break;
3593 
3594 		return true; /* queue the frame */
3595 	default:
3596 		break;
3597 	}
3598 
3599 	return false;
3600 }
3601 
3602 static ieee80211_rx_result debug_noinline
3603 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3604 {
3605 	struct ieee80211_local *local = rx->local;
3606 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3607 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3608 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3609 	int len = rx->skb->len;
3610 
3611 	if (!ieee80211_is_action(mgmt->frame_control))
3612 		return RX_CONTINUE;
3613 
3614 	if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3615 	    mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3616 	    mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3617 		return RX_DROP_U_ACTION_UNKNOWN_SRC;
3618 
3619 	switch (mgmt->u.action.category) {
3620 	case WLAN_CATEGORY_HT:
3621 		/* reject HT action frames from stations not supporting HT
3622 		 * or not HE Capable
3623 		 */
3624 		if (!rx->link_sta->pub->ht_cap.ht_supported &&
3625 		    !rx->link_sta->pub->he_cap.has_he)
3626 			goto invalid;
3627 
3628 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3629 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3630 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3631 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3632 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3633 			break;
3634 
3635 		/* verify action & smps_control/chanwidth are present */
3636 		if (len < IEEE80211_MIN_ACTION_SIZE(ht_smps))
3637 			goto invalid;
3638 
3639 		switch (mgmt->u.action.action_code) {
3640 		case WLAN_HT_ACTION_SMPS: {
3641 			struct ieee80211_supported_band *sband;
3642 			enum ieee80211_smps_mode smps_mode;
3643 			struct sta_opmode_info sta_opmode = {};
3644 
3645 			if (sdata->vif.type != NL80211_IFTYPE_AP &&
3646 			    sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3647 				goto handled;
3648 
3649 			/* convert to HT capability */
3650 			switch (mgmt->u.action.ht_smps.smps_control) {
3651 			case WLAN_HT_SMPS_CONTROL_DISABLED:
3652 				smps_mode = IEEE80211_SMPS_OFF;
3653 				break;
3654 			case WLAN_HT_SMPS_CONTROL_STATIC:
3655 				smps_mode = IEEE80211_SMPS_STATIC;
3656 				break;
3657 			case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3658 				smps_mode = IEEE80211_SMPS_DYNAMIC;
3659 				break;
3660 			default:
3661 				goto invalid;
3662 			}
3663 
3664 			/* if no change do nothing */
3665 			if (rx->link_sta->pub->smps_mode == smps_mode)
3666 				goto handled;
3667 			rx->link_sta->pub->smps_mode = smps_mode;
3668 			sta_opmode.smps_mode =
3669 				ieee80211_smps_mode_to_smps_mode(smps_mode);
3670 			sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3671 
3672 			sband = rx->local->hw.wiphy->bands[status->band];
3673 
3674 			rate_control_rate_update(local, sband, rx->link_sta,
3675 						 IEEE80211_RC_SMPS_CHANGED);
3676 			cfg80211_sta_opmode_change_notify(sdata->dev,
3677 							  rx->sta->addr,
3678 							  &sta_opmode,
3679 							  GFP_ATOMIC);
3680 			goto handled;
3681 		}
3682 		case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3683 			u8 chanwidth = mgmt->u.action.ht_notify_cw.chanwidth;
3684 
3685 			if (chanwidth != IEEE80211_HT_CHANWIDTH_20MHZ &&
3686 			    chanwidth != IEEE80211_HT_CHANWIDTH_ANY)
3687 				goto invalid;
3688 
3689 			/* If it doesn't support 40 MHz it can't change ... */
3690 			if (!(rx->link_sta->pub->ht_cap.cap &
3691 				IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3692 				goto handled;
3693 
3694 			goto queue;
3695 		}
3696 		default:
3697 			goto invalid;
3698 		}
3699 
3700 		break;
3701 	case WLAN_CATEGORY_PUBLIC:
3702 	case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
3703 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3704 			goto invalid;
3705 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3706 			break;
3707 		if (!rx->sta)
3708 			break;
3709 		if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid))
3710 			break;
3711 		if (mgmt->u.action.action_code !=
3712 				WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3713 			break;
3714 		if (len < IEEE80211_MIN_ACTION_SIZE(ext_chan_switch))
3715 			goto invalid;
3716 		goto queue;
3717 	case WLAN_CATEGORY_VHT:
3718 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3719 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3720 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3721 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3722 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3723 			break;
3724 
3725 		/* verify action code is present */
3726 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3727 			goto invalid;
3728 
3729 		switch (mgmt->u.action.action_code) {
3730 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3731 			/* verify opmode is present */
3732 			if (len < IEEE80211_MIN_ACTION_SIZE(vht_opmode_notif))
3733 				goto invalid;
3734 			goto queue;
3735 		}
3736 		case WLAN_VHT_ACTION_GROUPID_MGMT: {
3737 			if (len < IEEE80211_MIN_ACTION_SIZE(vht_group_notif))
3738 				goto invalid;
3739 			goto queue;
3740 		}
3741 		default:
3742 			break;
3743 		}
3744 		break;
3745 	case WLAN_CATEGORY_BACK:
3746 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3747 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3748 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3749 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3750 		    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3751 		    sdata->vif.type != NL80211_IFTYPE_NAN_DATA)
3752 			break;
3753 
3754 		/* verify action_code is present */
3755 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3756 			break;
3757 
3758 		switch (mgmt->u.action.action_code) {
3759 		case WLAN_ACTION_ADDBA_REQ:
3760 		case WLAN_ACTION_NDP_ADDBA_REQ:
3761 			if (len < IEEE80211_MIN_ACTION_SIZE(addba_req))
3762 				goto invalid;
3763 			break;
3764 		case WLAN_ACTION_ADDBA_RESP:
3765 		case WLAN_ACTION_NDP_ADDBA_RESP:
3766 			if (len < IEEE80211_MIN_ACTION_SIZE(addba_resp))
3767 				goto invalid;
3768 			break;
3769 		case WLAN_ACTION_DELBA:
3770 		case WLAN_ACTION_NDP_DELBA:
3771 			if (len < IEEE80211_MIN_ACTION_SIZE(delba))
3772 				goto invalid;
3773 			break;
3774 		default:
3775 			goto invalid;
3776 		}
3777 
3778 		goto queue;
3779 	case WLAN_CATEGORY_SPECTRUM_MGMT:
3780 		/* verify action_code is present */
3781 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3782 			break;
3783 
3784 		switch (mgmt->u.action.action_code) {
3785 		case WLAN_ACTION_SPCT_MSR_REQ:
3786 			if (status->band != NL80211_BAND_5GHZ)
3787 				break;
3788 
3789 			if (len < IEEE80211_MIN_ACTION_SIZE(measurement))
3790 				break;
3791 
3792 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3793 				break;
3794 
3795 			ieee80211_process_measurement_req(sdata, mgmt, len);
3796 			goto handled;
3797 		case WLAN_ACTION_SPCT_CHL_SWITCH: {
3798 			u8 *bssid;
3799 			if (len < IEEE80211_MIN_ACTION_SIZE(chan_switch))
3800 				break;
3801 
3802 			if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3803 			    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3804 			    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3805 				break;
3806 
3807 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
3808 				bssid = sdata->deflink.u.mgd.bssid;
3809 			else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3810 				bssid = sdata->u.ibss.bssid;
3811 			else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3812 				bssid = mgmt->sa;
3813 			else
3814 				break;
3815 
3816 			if (!ether_addr_equal(mgmt->bssid, bssid))
3817 				break;
3818 
3819 			goto queue;
3820 			}
3821 		}
3822 		break;
3823 	case WLAN_CATEGORY_SELF_PROTECTED:
3824 		if (len < IEEE80211_MIN_ACTION_SIZE(self_prot))
3825 			break;
3826 
3827 		switch (mgmt->u.action.action_code) {
3828 		case WLAN_SP_MESH_PEERING_OPEN:
3829 		case WLAN_SP_MESH_PEERING_CLOSE:
3830 		case WLAN_SP_MESH_PEERING_CONFIRM:
3831 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3832 				goto invalid;
3833 			if (sdata->u.mesh.user_mpm)
3834 				/* userspace handles this frame */
3835 				break;
3836 			goto queue;
3837 		case WLAN_SP_MGK_INFORM:
3838 		case WLAN_SP_MGK_ACK:
3839 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3840 				goto invalid;
3841 			break;
3842 		}
3843 		break;
3844 	case WLAN_CATEGORY_MESH_ACTION:
3845 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3846 			break;
3847 
3848 		if (!ieee80211_vif_is_mesh(&sdata->vif))
3849 			break;
3850 		if (mesh_action_is_path_sel(mgmt) &&
3851 		    !mesh_path_sel_is_hwmp(sdata))
3852 			break;
3853 		goto queue;
3854 	case WLAN_CATEGORY_S1G:
3855 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3856 			break;
3857 
3858 		switch (mgmt->u.action.action_code) {
3859 		case WLAN_S1G_TWT_SETUP:
3860 		case WLAN_S1G_TWT_TEARDOWN:
3861 			if (ieee80211_process_rx_twt_action(rx))
3862 				goto queue;
3863 			break;
3864 		default:
3865 			break;
3866 		}
3867 		break;
3868 	case WLAN_CATEGORY_PROTECTED_EHT:
3869 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3870 			break;
3871 
3872 		switch (mgmt->u.action.action_code) {
3873 		case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
3874 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3875 				break;
3876 
3877 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_req))
3878 				goto invalid;
3879 			goto queue;
3880 		case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
3881 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3882 				break;
3883 
3884 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_res))
3885 				goto invalid;
3886 			goto queue;
3887 		case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
3888 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3889 				break;
3890 
3891 			if (len < IEEE80211_MIN_ACTION_SIZE(ttlm_tear_down))
3892 				goto invalid;
3893 			goto queue;
3894 		case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
3895 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3896 				break;
3897 
3898 			/* The reconfiguration response action frame must
3899 			 * least one 'Status Duple' entry (3 octets)
3900 			 */
3901 			if (len < IEEE80211_MIN_ACTION_SIZE(ml_reconf_resp) + 3)
3902 				goto invalid;
3903 			goto queue;
3904 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
3905 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3906 				break;
3907 
3908 			if (len < IEEE80211_MIN_ACTION_SIZE(epcs) +
3909 				  IEEE80211_EPCS_ENA_RESP_BODY_LEN)
3910 				goto invalid;
3911 			goto queue;
3912 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
3913 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3914 				break;
3915 
3916 			if (len < IEEE80211_MIN_ACTION_SIZE(epcs))
3917 				goto invalid;
3918 			goto queue;
3919 		case WLAN_PROTECTED_EHT_ACTION_EML_OP_MODE_NOTIF:
3920 			if (sdata->vif.type != NL80211_IFTYPE_AP)
3921 				break;
3922 
3923 			if (len < IEEE80211_MIN_ACTION_SIZE(eml_omn))
3924 				goto invalid;
3925 			goto queue;
3926 		default:
3927 			break;
3928 		}
3929 		break;
3930 	case WLAN_CATEGORY_PROTECTED_UHR:
3931 		if (len < IEEE80211_MIN_ACTION_SIZE(action_code))
3932 			break;
3933 
3934 		switch (mgmt->u.action.action_code) {
3935 		case IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_REQUEST:
3936 			if (sdata->vif.type != NL80211_IFTYPE_AP)
3937 				break;
3938 			if (len < IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_req))
3939 				goto invalid;
3940 			if (mgmt->u.action.uhr_link_reconf_req.type !=
3941 			    IEEE80211_UHR_LINK_RECONFIG_REQUEST_OMP_REQUEST)
3942 				break;
3943 			goto queue;
3944 		case IEEE80211_PROTECTED_UHR_ACTION_LINK_RECONFIG_NOTIFY:
3945 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3946 				break;
3947 
3948 			if (len < IEEE80211_MIN_ACTION_SIZE(uhr_link_reconf_notif))
3949 				goto invalid;
3950 			goto queue;
3951 		}
3952 		break;
3953 	}
3954 
3955 	return RX_CONTINUE;
3956 
3957  invalid:
3958 	status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3959 	/* will return in the next handlers */
3960 	return RX_CONTINUE;
3961 
3962  handled:
3963 	if (rx->sta)
3964 		rx->link_sta->rx_stats.packets++;
3965 	dev_kfree_skb(rx->skb);
3966 	return RX_QUEUED;
3967 
3968  queue:
3969 	ieee80211_queue_skb_to_iface(sdata, rx->link_sta, rx->skb);
3970 	return RX_QUEUED;
3971 }
3972 
3973 static ieee80211_rx_result debug_noinline
3974 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3975 {
3976 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3977 	struct cfg80211_rx_info info = {
3978 		.freq = ieee80211_rx_status_to_khz(status),
3979 		.buf = rx->skb->data,
3980 		.len = rx->skb->len,
3981 		.link_id = rx->link_id,
3982 		.have_link_id = rx->link_id >= 0,
3983 		.no_sta = !rx->sta,
3984 	};
3985 
3986 	/* skip known-bad action frames and return them in the next handler */
3987 	if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3988 		return RX_CONTINUE;
3989 
3990 	/*
3991 	 * Getting here means the kernel doesn't know how to handle
3992 	 * it, but maybe userspace does ... include returned frames
3993 	 * so userspace can register for those to know whether ones
3994 	 * it transmitted were processed or returned.
3995 	 */
3996 
3997 	if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3998 	    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3999 		info.sig_dbm = status->signal;
4000 
4001 	if (ieee80211_is_timing_measurement(rx->skb) ||
4002 	    ieee80211_is_ftm(rx->skb)) {
4003 		info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp);
4004 		info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp);
4005 	}
4006 
4007 	if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) {
4008 		if (rx->sta)
4009 			rx->link_sta->rx_stats.packets++;
4010 		dev_kfree_skb(rx->skb);
4011 		return RX_QUEUED;
4012 	}
4013 
4014 	return RX_CONTINUE;
4015 }
4016 
4017 static ieee80211_rx_result debug_noinline
4018 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
4019 {
4020 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4021 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
4022 	int len = rx->skb->len;
4023 
4024 	if (!ieee80211_is_action(mgmt->frame_control))
4025 		return RX_CONTINUE;
4026 
4027 	switch (mgmt->u.action.category) {
4028 	case WLAN_CATEGORY_SA_QUERY:
4029 		if (len < IEEE80211_MIN_ACTION_SIZE(sa_query))
4030 			break;
4031 
4032 		switch (mgmt->u.action.action_code) {
4033 		case WLAN_ACTION_SA_QUERY_REQUEST:
4034 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
4035 				break;
4036 			ieee80211_process_sa_query_req(sdata, mgmt, len);
4037 			goto handled;
4038 		}
4039 		break;
4040 	}
4041 
4042 	return RX_CONTINUE;
4043 
4044  handled:
4045 	if (rx->sta)
4046 		rx->link_sta->rx_stats.packets++;
4047 	dev_kfree_skb(rx->skb);
4048 	return RX_QUEUED;
4049 }
4050 
4051 static ieee80211_rx_result debug_noinline
4052 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
4053 {
4054 	struct ieee80211_local *local = rx->local;
4055 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
4056 	struct sk_buff *nskb;
4057 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4058 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4059 
4060 	if (!ieee80211_is_action(mgmt->frame_control))
4061 		return RX_CONTINUE;
4062 
4063 	/*
4064 	 * For AP mode, hostapd is responsible for handling any action
4065 	 * frames that we didn't handle, including returning unknown
4066 	 * ones. For all other modes we will return them to the sender,
4067 	 * setting the 0x80 bit in the action category, as required by
4068 	 * 802.11-2012 9.24.4.
4069 	 * Newer versions of hostapd use the management frame registration
4070 	 * mechanisms and old cooked monitor interface is no longer supported.
4071 	 */
4072 	if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
4073 	    (sdata->vif.type == NL80211_IFTYPE_AP ||
4074 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
4075 		return RX_DROP_U_MALFORMED_ACTION;
4076 
4077 	if (is_multicast_ether_addr(mgmt->da))
4078 		return RX_DROP_U_UNKNOWN_MCAST_ACTION;
4079 
4080 	/* do not return rejected action frames */
4081 	if (mgmt->u.action.category & 0x80)
4082 		return RX_DROP_U_REJECTED_ACTION_RESPONSE;
4083 
4084 	nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
4085 			       GFP_ATOMIC);
4086 	if (nskb) {
4087 		struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
4088 
4089 		nmgmt->u.action.category |= 0x80;
4090 		memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
4091 		memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
4092 
4093 		memset(nskb->cb, 0, sizeof(nskb->cb));
4094 
4095 		if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
4096 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
4097 
4098 			info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
4099 				      IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
4100 				      IEEE80211_TX_CTL_NO_CCK_RATE;
4101 			if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
4102 				info->hw_queue =
4103 					local->hw.offchannel_tx_hw_queue;
4104 		}
4105 
4106 		__ieee80211_tx_skb_tid_band(rx->sdata, nskb, rx->sta, 7, -1,
4107 					    status->band);
4108 	}
4109 
4110 	return RX_DROP_U_UNKNOWN_ACTION_REJECTED;
4111 }
4112 
4113 static ieee80211_rx_result debug_noinline
4114 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
4115 {
4116 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4117 	struct ieee80211_hdr *hdr = (void *)rx->skb->data;
4118 
4119 	if (!ieee80211_is_ext(hdr->frame_control))
4120 		return RX_CONTINUE;
4121 
4122 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
4123 		return RX_DROP_U_UNEXPECTED_EXT_FRAME;
4124 
4125 	/* for now only beacons are ext, so queue them */
4126 	ieee80211_queue_skb_to_iface(sdata, rx->link_sta, rx->skb);
4127 
4128 	return RX_QUEUED;
4129 }
4130 
4131 static ieee80211_rx_result debug_noinline
4132 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
4133 {
4134 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4135 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
4136 	__le16 stype;
4137 
4138 	stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
4139 
4140 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
4141 	    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4142 	    sdata->vif.type != NL80211_IFTYPE_OCB &&
4143 	    sdata->vif.type != NL80211_IFTYPE_STATION)
4144 		return RX_DROP_U_UNHANDLED_MGMT;
4145 
4146 	switch (stype) {
4147 	case cpu_to_le16(IEEE80211_STYPE_AUTH):
4148 	case cpu_to_le16(IEEE80211_STYPE_BEACON):
4149 	case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
4150 		/* process for all: mesh, mlme, ibss */
4151 		break;
4152 	case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
4153 		if (is_multicast_ether_addr(mgmt->da) &&
4154 		    !is_broadcast_ether_addr(mgmt->da))
4155 			return RX_DROP_U_MCAST_DEAUTH;
4156 
4157 		/* process only for station/IBSS */
4158 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
4159 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
4160 			return RX_DROP_U_UNHANDLED_DEAUTH;
4161 		break;
4162 	case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
4163 	case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
4164 	case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
4165 		if (is_multicast_ether_addr(mgmt->da) &&
4166 		    !is_broadcast_ether_addr(mgmt->da))
4167 			return RX_DROP_U_MCAST_DISASSOC;
4168 
4169 		/* process only for station */
4170 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
4171 			return RX_DROP_U_UNHANDLED_DISASSOC;
4172 		break;
4173 	case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
4174 		/* process only for ibss and mesh */
4175 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4176 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4177 			return RX_DROP_U_UNHANDLED_PREQ;
4178 		break;
4179 	default:
4180 		return RX_DROP_U_UNHANDLED_MGMT_STYPE;
4181 	}
4182 
4183 	ieee80211_queue_skb_to_iface(sdata, rx->link_sta, rx->skb);
4184 
4185 	return RX_QUEUED;
4186 }
4187 
4188 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
4189 					 ieee80211_rx_result res)
4190 {
4191 	if (res == RX_QUEUED) {
4192 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
4193 		return;
4194 	}
4195 
4196 	if (res != RX_CONTINUE) {
4197 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
4198 		if (rx->sta)
4199 			rx->link_sta->rx_stats.dropped++;
4200 	}
4201 
4202 	kfree_skb_reason(rx->skb, (__force u32)res);
4203 }
4204 
4205 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
4206 				  struct sk_buff_head *frames)
4207 {
4208 	ieee80211_rx_result res;
4209 	struct sk_buff *skb;
4210 
4211 #define CALL_RXH(rxh)			\
4212 	do {				\
4213 		res = rxh(rx);		\
4214 		if (res != RX_CONTINUE)	\
4215 			goto rxh_next;  \
4216 	} while (0)
4217 
4218 	/* Lock here to avoid hitting all of the data used in the RX
4219 	 * path (e.g. key data, station data, ...) concurrently when
4220 	 * a frame is released from the reorder buffer due to timeout
4221 	 * from the timer, potentially concurrently with RX from the
4222 	 * driver.
4223 	 */
4224 	spin_lock_bh(&rx->local->rx_path_lock);
4225 
4226 	while ((skb = __skb_dequeue(frames))) {
4227 		struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4228 
4229 		/*
4230 		 * Most fields are valid across frames. However,
4231 		 * in the case of reordering frames may have arrived
4232 		 * on different links, so adjust the link_id, link
4233 		 * and link_sta accordingly.
4234 		 */
4235 		rx->skb = skb;
4236 		if (ieee80211_vif_is_mld(&rx->sdata->vif) &&
4237 		    unlikely(rx->link_id != status->link_id)) {
4238 			if (rx->sta) {
4239 				rx->link_sta =
4240 					rcu_dereference(rx->sta->link[rx->link_id]);
4241 
4242 				/* Link got disabled, just use deflink */
4243 				if (!rx->link_sta)
4244 					rx->link_sta = &rx->sta->deflink;
4245 			}
4246 			rx->link_id = status->link_id;
4247 			rx->link = rcu_dereference(rx->sdata->link[status->link_id]);
4248 		}
4249 
4250 		if (WARN_ON_ONCE(!rx->link)) {
4251 			res = RX_DROP_U_NO_LINK;
4252 			goto rxh_next;
4253 		}
4254 
4255 		CALL_RXH(ieee80211_rx_h_check_more_data);
4256 		CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
4257 		CALL_RXH(ieee80211_rx_h_sta_process);
4258 		CALL_RXH(ieee80211_rx_h_decrypt);
4259 		CALL_RXH(ieee80211_rx_h_defragment);
4260 		CALL_RXH(ieee80211_rx_h_michael_mic_verify);
4261 		/* must be after MMIC verify so header is counted in MPDU mic */
4262 		CALL_RXH(ieee80211_rx_h_amsdu);
4263 		CALL_RXH(ieee80211_rx_h_data);
4264 
4265 		/* special treatment -- needs the queue */
4266 		res = ieee80211_rx_h_ctrl(rx, frames);
4267 		if (res != RX_CONTINUE)
4268 			goto rxh_next;
4269 
4270 		CALL_RXH(ieee80211_rx_h_mgmt_check);
4271 		CALL_RXH(ieee80211_rx_h_action);
4272 		CALL_RXH(ieee80211_rx_h_userspace_mgmt);
4273 		CALL_RXH(ieee80211_rx_h_action_post_userspace);
4274 		CALL_RXH(ieee80211_rx_h_action_return);
4275 		CALL_RXH(ieee80211_rx_h_ext);
4276 		CALL_RXH(ieee80211_rx_h_mgmt);
4277 
4278  rxh_next:
4279 		ieee80211_rx_handlers_result(rx, res);
4280 
4281 #undef CALL_RXH
4282 	}
4283 
4284 	spin_unlock_bh(&rx->local->rx_path_lock);
4285 }
4286 
4287 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
4288 {
4289 	struct sk_buff_head reorder_release;
4290 	ieee80211_rx_result res;
4291 
4292 	__skb_queue_head_init(&reorder_release);
4293 
4294 #define CALL_RXH(rxh)			\
4295 	do {				\
4296 		res = rxh(rx);		\
4297 		if (res != RX_CONTINUE)	\
4298 			goto rxh_next;  \
4299 	} while (0)
4300 
4301 	CALL_RXH(ieee80211_rx_h_check_dup);
4302 	CALL_RXH(ieee80211_rx_h_check);
4303 
4304 	ieee80211_rx_reorder_ampdu(rx, &reorder_release);
4305 
4306 	ieee80211_rx_handlers(rx, &reorder_release);
4307 	return;
4308 
4309  rxh_next:
4310 	ieee80211_rx_handlers_result(rx, res);
4311 
4312 #undef CALL_RXH
4313 }
4314 
4315 static bool
4316 ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
4317 {
4318 	if (sta->mlo)
4319 		return sta->valid_links & BIT(link_id);
4320 
4321 	return sta->deflink.link_id == link_id;
4322 }
4323 
4324 static bool ieee80211_rx_data_set_link_sta(struct ieee80211_rx_data *rx,
4325 					   struct link_sta_info *link_sta)
4326 {
4327 	struct sta_info *sta;
4328 
4329 	if (WARN_ON_ONCE(!link_sta) ||
4330 	    !ieee80211_rx_is_valid_sta_link_id(&link_sta->sta->sta,
4331 					       link_sta->link_id))
4332 		return false;
4333 
4334 	sta = link_sta->sta;
4335 
4336 	rx->sta = sta;
4337 	rx->local = sta->sdata->local;
4338 	rx->link = rcu_dereference(sta->sdata->link[link_sta->link_id]);
4339 	if (!rx->link)
4340 		return false;
4341 
4342 	rx->link_id = rx->link->link_id;
4343 	rx->link_sta = link_sta;
4344 
4345 	return true;
4346 }
4347 
4348 static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
4349 				       u8 link_id)
4350 {
4351 	rx->link_id = link_id;
4352 	rx->link = rcu_dereference(rx->sdata->link[link_id]);
4353 
4354 	if (!rx->sta)
4355 		return rx->link;
4356 
4357 	if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
4358 		return false;
4359 
4360 	rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
4361 
4362 	return rx->link && rx->link_sta;
4363 }
4364 
4365 static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
4366 				      struct sta_info *sta, int link_id)
4367 {
4368 	rx->link_id = link_id;
4369 	rx->sta = sta;
4370 
4371 	if (sta) {
4372 		rx->local = sta->sdata->local;
4373 		if (!rx->sdata)
4374 			rx->sdata = sta->sdata;
4375 		rx->link_sta = &sta->deflink;
4376 	} else {
4377 		rx->link_sta = NULL;
4378 	}
4379 
4380 	if (link_id < 0) {
4381 		if (ieee80211_vif_is_mld(&rx->sdata->vif) &&
4382 		    sta && !sta->sta.valid_links) {
4383 			rx->link =
4384 				rcu_dereference(rx->sdata->link[sta->deflink.link_id]);
4385 			rx->link_sta = &sta->deflink;
4386 		} else {
4387 			rx->link = &rx->sdata->deflink;
4388 		}
4389 	} else if (!ieee80211_rx_data_set_link(rx, link_id)) {
4390 		return false;
4391 	}
4392 
4393 	return true;
4394 }
4395 
4396 /*
4397  * This function makes calls into the RX path, therefore
4398  * it has to be invoked under RCU read lock.
4399  */
4400 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
4401 {
4402 	struct sk_buff_head frames;
4403 	struct ieee80211_rx_data rx = {
4404 		/* This is OK -- must be QoS data frame */
4405 		.security_idx = tid,
4406 		.seqno_idx = tid,
4407 	};
4408 	struct tid_ampdu_rx *tid_agg_rx;
4409 	int link_id = -1;
4410 
4411 	/* NOTE: the correct link STA for the frame is set later */
4412 	if (sta->sta.valid_links)
4413 		link_id = ffs(sta->sta.valid_links) - 1;
4414 
4415 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
4416 		return;
4417 
4418 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4419 	if (!tid_agg_rx)
4420 		return;
4421 
4422 	__skb_queue_head_init(&frames);
4423 
4424 	spin_lock(&tid_agg_rx->reorder_lock);
4425 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4426 	spin_unlock(&tid_agg_rx->reorder_lock);
4427 
4428 	if (!skb_queue_empty(&frames)) {
4429 		struct ieee80211_event event = {
4430 			.type = BA_FRAME_TIMEOUT,
4431 			.u.ba.tid = tid,
4432 			.u.ba.sta = &sta->sta,
4433 		};
4434 		drv_event_callback(rx.local, rx.sdata, &event);
4435 	}
4436 
4437 	ieee80211_rx_handlers(&rx, &frames);
4438 }
4439 
4440 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4441 					  u16 ssn, u64 filtered,
4442 					  u16 received_mpdus)
4443 {
4444 	struct ieee80211_local *local;
4445 	struct sta_info *sta;
4446 	struct tid_ampdu_rx *tid_agg_rx;
4447 	struct sk_buff_head frames;
4448 	struct ieee80211_rx_data rx = {
4449 		/* This is OK -- must be QoS data frame */
4450 		.security_idx = tid,
4451 		.seqno_idx = tid,
4452 	};
4453 	int i, diff;
4454 
4455 	if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4456 		return;
4457 
4458 	__skb_queue_head_init(&frames);
4459 
4460 	sta = container_of(pubsta, struct sta_info, sta);
4461 
4462 	local = sta->sdata->local;
4463 	WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64,
4464 		  "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n",
4465 		  local->hw.max_rx_aggregation_subframes);
4466 
4467 	if (!ieee80211_rx_data_set_sta(&rx, sta, -1))
4468 		return;
4469 
4470 	rcu_read_lock();
4471 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4472 	if (!tid_agg_rx)
4473 		goto out;
4474 
4475 	spin_lock_bh(&tid_agg_rx->reorder_lock);
4476 
4477 	if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4478 		int release;
4479 
4480 		/* release all frames in the reorder buffer */
4481 		release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4482 			   IEEE80211_SN_MODULO;
4483 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4484 						 release, &frames);
4485 		/* update ssn to match received ssn */
4486 		tid_agg_rx->head_seq_num = ssn;
4487 	} else {
4488 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4489 						 &frames);
4490 	}
4491 
4492 	/* handle the case that received ssn is behind the mac ssn.
4493 	 * it can be tid_agg_rx->buf_size behind and still be valid */
4494 	diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4495 	if (diff >= tid_agg_rx->buf_size) {
4496 		tid_agg_rx->reorder_buf_filtered = 0;
4497 		goto release;
4498 	}
4499 	filtered = filtered >> diff;
4500 	ssn += diff;
4501 
4502 	/* update bitmap */
4503 	for (i = 0; i < tid_agg_rx->buf_size; i++) {
4504 		int index = (ssn + i) % tid_agg_rx->buf_size;
4505 
4506 		tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4507 		if (filtered & BIT_ULL(i))
4508 			tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4509 	}
4510 
4511 	/* now process also frames that the filter marking released */
4512 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4513 
4514 release:
4515 	spin_unlock_bh(&tid_agg_rx->reorder_lock);
4516 
4517 	ieee80211_rx_handlers(&rx, &frames);
4518 
4519  out:
4520 	rcu_read_unlock();
4521 }
4522 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4523 
4524 /* main receive path */
4525 
4526 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
4527 {
4528 	return ether_addr_equal(raddr, addr) ||
4529 	       is_broadcast_ether_addr(raddr);
4530 }
4531 
4532 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4533 {
4534 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4535 	struct sk_buff *skb = rx->skb;
4536 	struct ieee80211_hdr *hdr = (void *)skb->data;
4537 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4538 	u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4539 	bool multicast;
4540 	static const u8 nan_network_id[ETH_ALEN] __aligned(2) = {
4541 		0x51, 0x6F, 0x9A, 0x01, 0x00, 0x00
4542 	};
4543 
4544 	if (ieee80211_is_s1g_beacon(hdr->frame_control))
4545 		return sdata->vif.type == NL80211_IFTYPE_STATION && bssid;
4546 
4547 	multicast = is_multicast_ether_addr(hdr->addr1);
4548 
4549 	switch (sdata->vif.type) {
4550 	case NL80211_IFTYPE_STATION:
4551 		if (!bssid && !sdata->u.mgd.use_4addr)
4552 			return false;
4553 		if (ieee80211_is_first_frag(hdr->seq_ctrl) &&
4554 		    ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4555 			return false;
4556 		if (multicast)
4557 			return true;
4558 		return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id);
4559 	case NL80211_IFTYPE_ADHOC:
4560 		if (!bssid)
4561 			return false;
4562 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4563 		    ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4564 		    !is_valid_ether_addr(hdr->addr2))
4565 			return false;
4566 		if (ieee80211_is_beacon(hdr->frame_control))
4567 			return true;
4568 		if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4569 			return false;
4570 		if (!multicast &&
4571 		    !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4572 			return false;
4573 		if (!rx->sta) {
4574 			int rate_idx;
4575 			if (status->encoding != RX_ENC_LEGACY)
4576 				rate_idx = 0; /* TODO: HT/VHT rates */
4577 			else
4578 				rate_idx = status->rate_idx;
4579 			ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4580 						 BIT(rate_idx));
4581 		}
4582 		return true;
4583 	case NL80211_IFTYPE_OCB:
4584 		if (!bssid)
4585 			return false;
4586 		if (!ieee80211_is_data_present(hdr->frame_control))
4587 			return false;
4588 		if (!is_broadcast_ether_addr(bssid))
4589 			return false;
4590 		if (!multicast &&
4591 		    !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4592 			return false;
4593 		/* reject invalid/our STA address */
4594 		if (!is_valid_ether_addr(hdr->addr2) ||
4595 		    ether_addr_equal(sdata->dev->dev_addr, hdr->addr2))
4596 			return false;
4597 		if (!rx->sta) {
4598 			int rate_idx;
4599 			if (status->encoding != RX_ENC_LEGACY)
4600 				rate_idx = 0; /* TODO: HT rates */
4601 			else
4602 				rate_idx = status->rate_idx;
4603 			ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4604 						BIT(rate_idx));
4605 		}
4606 		return true;
4607 	case NL80211_IFTYPE_MESH_POINT:
4608 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4609 			return false;
4610 		if (multicast)
4611 			return true;
4612 		return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4613 	case NL80211_IFTYPE_AP_VLAN:
4614 	case NL80211_IFTYPE_AP:
4615 		if (!bssid)
4616 			return ieee80211_is_our_addr(sdata, hdr->addr1,
4617 						     &rx->link_id);
4618 
4619 		if (!is_broadcast_ether_addr(bssid) &&
4620 		    !ieee80211_is_our_addr(sdata, bssid, NULL)) {
4621 			/*
4622 			 * Accept public action frames even when the
4623 			 * BSSID doesn't match, this is used for P2P
4624 			 * and location updates. Note that mac80211
4625 			 * itself never looks at these frames.
4626 			 */
4627 			if (!multicast &&
4628 			    !ieee80211_is_our_addr(sdata, hdr->addr1,
4629 						   &rx->link_id))
4630 				return false;
4631 			if (ieee80211_is_public_action(hdr, skb->len))
4632 				return true;
4633 			return ieee80211_is_beacon(hdr->frame_control);
4634 		}
4635 
4636 		if (!ieee80211_has_tods(hdr->frame_control)) {
4637 			/* ignore data frames to TDLS-peers */
4638 			if (ieee80211_is_data(hdr->frame_control))
4639 				return false;
4640 			/* ignore action frames to TDLS-peers */
4641 			if (ieee80211_is_action(hdr->frame_control) &&
4642 			    !is_broadcast_ether_addr(bssid) &&
4643 			    !ether_addr_equal(bssid, hdr->addr1))
4644 				return false;
4645 		}
4646 
4647 		/*
4648 		 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4649 		 * the BSSID - we've checked that already but may have accepted
4650 		 * the wildcard (ff:ff:ff:ff:ff:ff).
4651 		 *
4652 		 * It also says:
4653 		 *	The BSSID of the Data frame is determined as follows:
4654 		 *	a) If the STA is contained within an AP or is associated
4655 		 *	   with an AP, the BSSID is the address currently in use
4656 		 *	   by the STA contained in the AP.
4657 		 *
4658 		 * So we should not accept data frames with an address that's
4659 		 * multicast.
4660 		 *
4661 		 * Accepting it also opens a security problem because stations
4662 		 * could encrypt it with the GTK and inject traffic that way.
4663 		 */
4664 		if (ieee80211_is_data(hdr->frame_control) && multicast)
4665 			return false;
4666 
4667 		return true;
4668 	case NL80211_IFTYPE_P2P_DEVICE:
4669 		return ieee80211_is_public_action(hdr, skb->len) ||
4670 		       ieee80211_is_probe_req(hdr->frame_control) ||
4671 		       ieee80211_is_probe_resp(hdr->frame_control) ||
4672 		       ieee80211_is_beacon(hdr->frame_control) ||
4673 		       (ieee80211_is_auth(hdr->frame_control) &&
4674 			ether_addr_equal(sdata->vif.addr, hdr->addr1));
4675 	case NL80211_IFTYPE_NAN:
4676 		if (ieee80211_has_tods(hdr->frame_control) ||
4677 		    ieee80211_has_fromds(hdr->frame_control))
4678 			return false;
4679 
4680 		/*
4681 		 * Accept only frames that are addressed to the NAN cluster
4682 		 * (based on the Cluster ID). From these frames, accept only
4683 		 *  - public action frames,
4684 		 *  - authentication frames to the local address, and
4685 		 *  - robust management frames except disassoc.
4686 		 */
4687 		if (!ether_addr_equal(sdata->u.nan.conf.cluster_id, hdr->addr3))
4688 			return false;
4689 		if (ieee80211_is_public_action(hdr, skb->len))
4690 			return true;
4691 		if (ieee80211_is_auth(hdr->frame_control) &&
4692 		    ether_addr_equal(sdata->vif.addr, hdr->addr1))
4693 			return true;
4694 		if (!ieee80211_is_disassoc(hdr->frame_control) &&
4695 		    ieee80211_is_robust_mgmt_frame(skb))
4696 			return true;
4697 		return false;
4698 	case NL80211_IFTYPE_NAN_DATA:
4699 		if (ieee80211_has_tods(hdr->frame_control) ||
4700 		    ieee80211_has_fromds(hdr->frame_control))
4701 			return false;
4702 
4703 		if (ieee80211_is_data(hdr->frame_control)) {
4704 			struct ieee80211_sub_if_data *nmi;
4705 
4706 			nmi = rcu_dereference(sdata->u.nan_data.nmi);
4707 			if (!nmi)
4708 				return false;
4709 
4710 			if (!ether_addr_equal(nmi->u.nan.conf.cluster_id,
4711 					      hdr->addr3))
4712 				return false;
4713 
4714 			return multicast ||
4715 			       ether_addr_equal(sdata->vif.addr, hdr->addr1);
4716 		}
4717 
4718 		/* Non-public action frames (unicast or multicast) */
4719 		if (ieee80211_is_action(hdr->frame_control) &&
4720 		    !ieee80211_is_public_action(hdr, skb->len) &&
4721 		    (ether_addr_equal(nan_network_id, hdr->addr1) ||
4722 		     ether_addr_equal(sdata->vif.addr, hdr->addr1)))
4723 			return true;
4724 
4725 		/* Unicast secure management frames */
4726 		return ether_addr_equal(sdata->vif.addr, hdr->addr1) &&
4727 		       ieee80211_is_unicast_robust_mgmt_frame(skb);
4728 	case NL80211_IFTYPE_PD:
4729 		/*
4730 		 * Accept only authentication frames (PASN) addressed to
4731 		 * this interface.
4732 		 */
4733 		return ieee80211_is_auth(hdr->frame_control) &&
4734 		       ether_addr_equal(sdata->vif.addr, hdr->addr1);
4735 	default:
4736 		break;
4737 	}
4738 
4739 	WARN_ON_ONCE(1);
4740 	return false;
4741 }
4742 
4743 void ieee80211_check_fast_rx(struct sta_info *sta)
4744 {
4745 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4746 	struct ieee80211_local *local = sdata->local;
4747 	struct ieee80211_key *key;
4748 	struct ieee80211_fast_rx fastrx = {
4749 		.dev = sdata->dev,
4750 		.vif_type = sdata->vif.type,
4751 		.control_port_protocol = sdata->control_port_protocol,
4752 	}, *old, *new = NULL;
4753 	u32 offload_flags;
4754 	bool set_offload = false;
4755 	bool assign = false;
4756 	bool offload;
4757 
4758 	/* use sparse to check that we don't return without updating */
4759 	__acquire(check_fast_rx);
4760 
4761 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4762 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4763 	ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4764 	ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4765 
4766 	fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4767 
4768 	/* fast-rx doesn't do reordering */
4769 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4770 	    !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4771 		goto clear;
4772 
4773 	switch (sdata->vif.type) {
4774 	case NL80211_IFTYPE_STATION:
4775 		if (sta->sta.tdls) {
4776 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4777 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4778 			fastrx.expected_ds_bits = 0;
4779 		} else {
4780 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4781 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4782 			fastrx.expected_ds_bits =
4783 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4784 		}
4785 
4786 		if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4787 			fastrx.expected_ds_bits |=
4788 				cpu_to_le16(IEEE80211_FCTL_TODS);
4789 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4790 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4791 		}
4792 
4793 		if (!sdata->u.mgd.powersave)
4794 			break;
4795 
4796 		/* software powersave is a huge mess, avoid all of it */
4797 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4798 			goto clear;
4799 		if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4800 		    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4801 			goto clear;
4802 		break;
4803 	case NL80211_IFTYPE_AP_VLAN:
4804 	case NL80211_IFTYPE_AP:
4805 		/* parallel-rx requires this, at least with calls to
4806 		 * ieee80211_sta_ps_transition()
4807 		 */
4808 		if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4809 			goto clear;
4810 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4811 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4812 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4813 
4814 		fastrx.internal_forward =
4815 			!(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4816 			(sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4817 			 !sdata->u.vlan.sta);
4818 
4819 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4820 		    sdata->u.vlan.sta) {
4821 			fastrx.expected_ds_bits |=
4822 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4823 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4824 			fastrx.internal_forward = 0;
4825 		}
4826 
4827 		break;
4828 	case NL80211_IFTYPE_MESH_POINT:
4829 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS |
4830 						      IEEE80211_FCTL_TODS);
4831 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4832 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4833 		break;
4834 	default:
4835 		goto clear;
4836 	}
4837 
4838 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4839 		goto clear;
4840 
4841 	rcu_read_lock();
4842 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4843 	if (!key)
4844 		key = rcu_dereference(sdata->default_unicast_key);
4845 	if (key) {
4846 		switch (key->conf.cipher) {
4847 		case WLAN_CIPHER_SUITE_TKIP:
4848 			/* we don't want to deal with MMIC in fast-rx */
4849 			goto clear_rcu;
4850 		case WLAN_CIPHER_SUITE_CCMP:
4851 		case WLAN_CIPHER_SUITE_CCMP_256:
4852 		case WLAN_CIPHER_SUITE_GCMP:
4853 		case WLAN_CIPHER_SUITE_GCMP_256:
4854 			break;
4855 		default:
4856 			/* We also don't want to deal with
4857 			 * WEP or cipher scheme.
4858 			 */
4859 			goto clear_rcu;
4860 		}
4861 
4862 		fastrx.key = true;
4863 		fastrx.icv_len = key->conf.icv_len;
4864 	}
4865 
4866 	assign = true;
4867  clear_rcu:
4868 	rcu_read_unlock();
4869  clear:
4870 	__release(check_fast_rx);
4871 
4872 	if (assign)
4873 		new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4874 
4875 	offload_flags = get_bss_sdata(sdata)->vif.offload_flags;
4876 	offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED;
4877 
4878 	if (assign && offload)
4879 		set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4880 	else
4881 		set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4882 
4883 	if (set_offload)
4884 		drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4885 
4886 	spin_lock_bh(&sta->lock);
4887 	old = rcu_dereference_protected(sta->fast_rx, true);
4888 	rcu_assign_pointer(sta->fast_rx, new);
4889 	spin_unlock_bh(&sta->lock);
4890 
4891 	if (old)
4892 		kfree_rcu(old, rcu_head);
4893 }
4894 
4895 void ieee80211_clear_fast_rx(struct sta_info *sta)
4896 {
4897 	struct ieee80211_fast_rx *old;
4898 
4899 	spin_lock_bh(&sta->lock);
4900 	old = rcu_dereference_protected(sta->fast_rx, true);
4901 	RCU_INIT_POINTER(sta->fast_rx, NULL);
4902 	spin_unlock_bh(&sta->lock);
4903 
4904 	if (old)
4905 		kfree_rcu(old, rcu_head);
4906 }
4907 
4908 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4909 {
4910 	struct ieee80211_local *local = sdata->local;
4911 	struct sta_info *sta;
4912 
4913 	lockdep_assert_wiphy(local->hw.wiphy);
4914 
4915 	list_for_each_entry(sta, &local->sta_list, list) {
4916 		if (sdata != sta->sdata &&
4917 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4918 			continue;
4919 		ieee80211_check_fast_rx(sta);
4920 	}
4921 }
4922 
4923 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4924 {
4925 	struct ieee80211_local *local = sdata->local;
4926 
4927 	lockdep_assert_wiphy(local->hw.wiphy);
4928 
4929 	__ieee80211_check_fast_rx_iface(sdata);
4930 }
4931 
4932 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4933 			      struct ieee80211_fast_rx *fast_rx,
4934 			      int orig_len)
4935 {
4936 	struct ieee80211_sta_rx_stats *stats;
4937 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4938 	struct link_sta_info *link_sta = rx->link_sta;
4939 	struct sk_buff *skb = rx->skb;
4940 	void *sa = skb->data + ETH_ALEN;
4941 	void *da = skb->data;
4942 
4943 	if (WARN_ON_ONCE(!link_sta)) {
4944 		dev_kfree_skb(rx->skb);
4945 		return;
4946 	}
4947 
4948 	stats = &link_sta->rx_stats;
4949 	if (fast_rx->uses_rss)
4950 		stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
4951 
4952 	/* statistics part of ieee80211_rx_h_sta_process() */
4953 	if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4954 		stats->last_signal = status->signal;
4955 		if (!fast_rx->uses_rss)
4956 			ewma_signal_add(&link_sta->rx_stats_avg.signal,
4957 					-status->signal);
4958 	}
4959 
4960 	if (status->chains) {
4961 		int i;
4962 
4963 		stats->chains = status->chains;
4964 		for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4965 			int signal = status->chain_signal[i];
4966 
4967 			if (!(status->chains & BIT(i)))
4968 				continue;
4969 
4970 			stats->chain_signal_last[i] = signal;
4971 			if (!fast_rx->uses_rss)
4972 				ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
4973 						-signal);
4974 		}
4975 	}
4976 	/* end of statistics */
4977 
4978 	stats->last_rx = jiffies;
4979 	stats->last_rate = sta_stats_encode_rate(status);
4980 
4981 	stats->fragments++;
4982 	stats->packets++;
4983 
4984 	skb->dev = fast_rx->dev;
4985 
4986 	dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4987 
4988 	/* The seqno index has the same property as needed
4989 	 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4990 	 * for non-QoS-data frames. Here we know it's a data
4991 	 * frame, so count MSDUs.
4992 	 */
4993 	u64_stats_update_begin(&stats->syncp);
4994 	u64_stats_inc(&stats->msdu[rx->seqno_idx]);
4995 	u64_stats_add(&stats->bytes, orig_len);
4996 	u64_stats_update_end(&stats->syncp);
4997 
4998 	if (fast_rx->internal_forward) {
4999 		struct sk_buff *xmit_skb = NULL;
5000 		if (is_multicast_ether_addr(da)) {
5001 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
5002 		} else if (!ether_addr_equal(da, sa) &&
5003 			   sta_info_get(rx->sdata, da)) {
5004 			xmit_skb = skb;
5005 			skb = NULL;
5006 		}
5007 
5008 		if (xmit_skb) {
5009 			/*
5010 			 * Send to wireless media and increase priority by 256
5011 			 * to keep the received priority instead of
5012 			 * reclassifying the frame (see cfg80211_classify8021d).
5013 			 */
5014 			xmit_skb->priority += 256;
5015 			xmit_skb->protocol = htons(ETH_P_802_3);
5016 			skb_reset_network_header(xmit_skb);
5017 			skb_reset_mac_header(xmit_skb);
5018 			dev_queue_xmit(xmit_skb);
5019 		}
5020 
5021 		if (!skb)
5022 			return;
5023 	}
5024 
5025 	/* deliver to local stack */
5026 	skb->protocol = eth_type_trans(skb, fast_rx->dev);
5027 	ieee80211_deliver_skb_to_local_stack(skb, rx);
5028 }
5029 
5030 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
5031 				     struct ieee80211_fast_rx *fast_rx)
5032 {
5033 	struct sk_buff *skb = rx->skb;
5034 	struct ieee80211_hdr *hdr = (void *)skb->data;
5035 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5036 	ieee80211_rx_result res;
5037 	int orig_len = skb->len;
5038 	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
5039 	int snap_offs = hdrlen;
5040 	struct {
5041 		u8 snap[sizeof(rfc1042_header)];
5042 		__be16 proto;
5043 	} *payload __aligned(2);
5044 	struct {
5045 		u8 da[ETH_ALEN];
5046 		u8 sa[ETH_ALEN];
5047 	} addrs __aligned(2);
5048 	struct ieee80211_sta_rx_stats *stats;
5049 	u32 encoded_rate;
5050 
5051 	/* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
5052 	 * to a common data structure; drivers can implement that per queue
5053 	 * but we don't have that information in mac80211
5054 	 */
5055 	if (!(status->flag & RX_FLAG_DUP_VALIDATED))
5056 		return false;
5057 
5058 #define FAST_RX_CRYPT_FLAGS	(RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
5059 
5060 	/* If using encryption, we also need to have:
5061 	 *  - PN_VALIDATED: similar, but the implementation is tricky
5062 	 *  - DECRYPTED: necessary for PN_VALIDATED
5063 	 */
5064 	if (fast_rx->key &&
5065 	    (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
5066 		return false;
5067 
5068 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
5069 		return false;
5070 
5071 	if (unlikely(ieee80211_is_frag(hdr)))
5072 		return false;
5073 
5074 	/* Since our interface address cannot be multicast, this
5075 	 * implicitly also rejects multicast frames without the
5076 	 * explicit check.
5077 	 *
5078 	 * We shouldn't get any *data* frames not addressed to us
5079 	 * (AP mode will accept multicast *management* frames), but
5080 	 * punting here will make it go through the full checks in
5081 	 * ieee80211_accept_frame().
5082 	 */
5083 	if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
5084 		return false;
5085 
5086 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
5087 					      IEEE80211_FCTL_TODS)) !=
5088 	    fast_rx->expected_ds_bits)
5089 		return false;
5090 
5091 	/* assign the key to drop unencrypted frames (later)
5092 	 * and strip the IV/MIC if necessary
5093 	 */
5094 	if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
5095 		/* GCMP header length is the same */
5096 		snap_offs += IEEE80211_CCMP_HDR_LEN;
5097 	}
5098 
5099 	if (!ieee80211_vif_is_mesh(&rx->sdata->vif) &&
5100 	    !(status->rx_flags & IEEE80211_RX_AMSDU)) {
5101 		if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
5102 			return false;
5103 
5104 		payload = (void *)(skb->data + snap_offs);
5105 
5106 		if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
5107 			return false;
5108 
5109 		/* Don't handle these here since they require special code.
5110 		 * Accept AARP and IPX even though they should come with a
5111 		 * bridge-tunnel header - but if we get them this way then
5112 		 * there's little point in discarding them.
5113 		 */
5114 		if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
5115 			     payload->proto == fast_rx->control_port_protocol))
5116 			return false;
5117 	}
5118 
5119 	/* after this point, don't punt to the slowpath! */
5120 
5121 	if (fast_rx->uses_rss)
5122 		stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
5123 	else
5124 		stats = &rx->link_sta->rx_stats;
5125 
5126 	if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
5127 	    pskb_trim(skb, skb->len - fast_rx->icv_len))
5128 		goto drop;
5129 
5130 	if (rx->key && !ieee80211_has_protected(hdr->frame_control))
5131 		goto drop;
5132 
5133 	if (status->rx_flags & IEEE80211_RX_AMSDU) {
5134 		if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
5135 		    RX_QUEUED)
5136 			goto drop;
5137 
5138 		return true;
5139 	}
5140 
5141 	/* do the header conversion - first grab the addresses */
5142 	ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
5143 	ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
5144 	if (ieee80211_vif_is_mesh(&rx->sdata->vif)) {
5145 	    skb_pull(skb, snap_offs - 2);
5146 	    put_unaligned_be16(skb->len - 2, skb->data);
5147 	} else {
5148 	    skb_postpull_rcsum(skb, skb->data + snap_offs,
5149 			       sizeof(rfc1042_header) + 2);
5150 
5151 	    /* remove the SNAP but leave the ethertype */
5152 	    skb_pull(skb, snap_offs + sizeof(rfc1042_header));
5153 	}
5154 	/* push the addresses in front */
5155 	memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
5156 
5157 	/* capture before mesh forward may memset or free skb->cb */
5158 	encoded_rate = sta_stats_encode_rate(status);
5159 
5160 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
5161 	switch (res) {
5162 	case RX_QUEUED:
5163 		stats->last_rx = jiffies;
5164 		stats->last_rate = encoded_rate;
5165 		return true;
5166 	case RX_CONTINUE:
5167 		break;
5168 	default:
5169 		goto drop;
5170 	}
5171 
5172 	status = IEEE80211_SKB_RXCB(skb);
5173 	status->link_id = rx->link_id < 0 ? 0 : rx->link_id;
5174 
5175 	ieee80211_rx_8023(rx, fast_rx, orig_len);
5176 
5177 	return true;
5178  drop:
5179 	dev_kfree_skb(skb);
5180 
5181 	stats->dropped++;
5182 	return true;
5183 }
5184 
5185 /*
5186  * This function returns whether or not the SKB
5187  * was destined for RX processing or not, which,
5188  * if consume is true, is equivalent to whether
5189  * or not the skb was consumed.
5190  */
5191 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
5192 					    struct sk_buff *skb, bool consume)
5193 {
5194 	struct ieee80211_local *local = rx->local;
5195 	struct ieee80211_sub_if_data *sdata = rx->sdata;
5196 	struct ieee80211_hdr *hdr = (void *)skb->data;
5197 	struct link_sta_info *link_sta = rx->link_sta;
5198 	struct ieee80211_link_data *link = rx->link;
5199 	struct ieee80211_rx_status *status;
5200 
5201 	rx->skb = skb;
5202 
5203 	/* See if we can do fast-rx; if we have to copy we already lost,
5204 	 * so punt in that case. We should never have to deliver a data
5205 	 * frame to multiple interfaces anyway.
5206 	 *
5207 	 * We skip the ieee80211_accept_frame() call and do the necessary
5208 	 * checking inside ieee80211_invoke_fast_rx().
5209 	 */
5210 	if (consume && rx->sta) {
5211 		struct ieee80211_fast_rx *fast_rx;
5212 
5213 		fast_rx = rcu_dereference(rx->sta->fast_rx);
5214 		if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
5215 			return true;
5216 	}
5217 
5218 	if (!ieee80211_accept_frame(rx))
5219 		return false;
5220 
5221 	if (!consume) {
5222 		struct skb_shared_hwtstamps *shwt;
5223 
5224 		rx->skb = skb_copy(skb, GFP_ATOMIC);
5225 		if (!rx->skb) {
5226 			if (net_ratelimit())
5227 				wiphy_debug(local->hw.wiphy,
5228 					"failed to copy skb for %s\n",
5229 					sdata->name);
5230 			return true;
5231 		}
5232 
5233 		/* skb_copy() does not copy the hw timestamps, so copy it
5234 		 * explicitly
5235 		 */
5236 		shwt = skb_hwtstamps(rx->skb);
5237 		shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
5238 
5239 		/* Update the hdr pointer to the new skb for translation below */
5240 		hdr = (struct ieee80211_hdr *)rx->skb->data;
5241 	}
5242 
5243 	status = IEEE80211_SKB_RXCB(rx->skb);
5244 	status->link_id = rx->link_id < 0 ? 0 : rx->link_id;
5245 
5246 	if (ieee80211_is_s1g_beacon(hdr->frame_control)) {
5247 		ieee80211_invoke_rx_handlers(rx);
5248 		return true;
5249 	}
5250 
5251 	/* Store a copy of the pre-translated link addresses for SW crypto */
5252 	if (unlikely(is_unicast_ether_addr(hdr->addr1) &&
5253 		     !ieee80211_is_data(hdr->frame_control)))
5254 		memcpy(rx->link_addrs, &hdr->addrs, 3 * ETH_ALEN);
5255 
5256 	if (unlikely(rx->sta && rx->sta->sta.mlo) &&
5257 	    is_unicast_ether_addr(hdr->addr1) &&
5258 	    !ieee80211_is_probe_resp(hdr->frame_control) &&
5259 	    !ieee80211_is_beacon(hdr->frame_control)) {
5260 		/* translate to MLD addresses */
5261 		if (ether_addr_equal(link->conf->addr, hdr->addr1))
5262 			ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
5263 		if (ether_addr_equal(link_sta->addr, hdr->addr2))
5264 			ether_addr_copy(hdr->addr2, rx->sta->addr);
5265 		/* translate A3 only if it's the BSSID */
5266 		if (!ieee80211_has_tods(hdr->frame_control) &&
5267 		    !ieee80211_has_fromds(hdr->frame_control)) {
5268 			if (ether_addr_equal(link_sta->addr, hdr->addr3))
5269 				ether_addr_copy(hdr->addr3, rx->sta->addr);
5270 			else if (ether_addr_equal(link->conf->addr, hdr->addr3))
5271 				ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
5272 		}
5273 		/* not needed for A4 since it can only carry the SA */
5274 	}
5275 
5276 	ieee80211_invoke_rx_handlers(rx);
5277 	return true;
5278 }
5279 
5280 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
5281 				       struct ieee80211_link_sta *link_pubsta,
5282 				       struct sk_buff *skb,
5283 				       struct list_head *list)
5284 {
5285 	struct ieee80211_local *local = hw_to_local(hw);
5286 	struct ieee80211_fast_rx *fast_rx;
5287 	struct ieee80211_rx_data rx;
5288 	struct sta_info *sta;
5289 
5290 	memset(&rx, 0, sizeof(rx));
5291 	rx.skb = skb;
5292 	rx.local = local;
5293 	rx.list = list;
5294 
5295 	I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5296 
5297 	/* drop frame if too short for header */
5298 	if (skb->len < sizeof(struct ethhdr))
5299 		goto drop;
5300 
5301 	if (!link_pubsta)
5302 		goto drop;
5303 
5304 	sta = container_of(link_pubsta->sta, struct sta_info, sta);
5305 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_pubsta->link_id))
5306 		goto drop;
5307 
5308 	rx.link_id = link_pubsta->link_id;
5309 
5310 	fast_rx = rcu_dereference(rx.sta->fast_rx);
5311 	if (!fast_rx)
5312 		goto drop;
5313 
5314 	ieee80211_rx_8023(&rx, fast_rx, skb->len);
5315 	return;
5316 
5317 drop:
5318 	dev_kfree_skb(skb);
5319 }
5320 
5321 static bool ieee80211_rx_valid_freq(int freq, struct ieee80211_link_data *link)
5322 {
5323 	struct ieee80211_chanctx_conf *conf;
5324 
5325 	if (!freq || link->sdata->vif.type == NL80211_IFTYPE_NAN ||
5326 	    link->sdata->vif.type == NL80211_IFTYPE_NAN_DATA)
5327 		return true;
5328 
5329 	conf = rcu_dereference(link->conf->chanctx_conf);
5330 	if (!conf || !conf->def.chan)
5331 		return false;
5332 
5333 	return freq == conf->def.chan->center_freq;
5334 }
5335 
5336 /*
5337  * This is the actual Rx frames handler. as it belongs to Rx path it must
5338  * be called with rcu_read_lock protection.
5339  */
5340 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
5341 					 struct ieee80211_link_sta *link_pubsta,
5342 					 struct sk_buff *skb,
5343 					 struct list_head *list)
5344 {
5345 	struct ieee80211_local *local = hw_to_local(hw);
5346 	struct ieee80211_sub_if_data *sdata;
5347 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5348 	struct ieee80211_hdr *hdr;
5349 	struct link_sta_info *link_sta;
5350 	struct sta_info *sta;
5351 	__le16 fc;
5352 	struct ieee80211_rx_data rx;
5353 	struct rhlist_head *tmp;
5354 	bool rx_data_pending;
5355 	int err = 0;
5356 
5357 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
5358 	memset(&rx, 0, sizeof(rx));
5359 	rx.skb = skb;
5360 	rx.local = local;
5361 	rx.list = list;
5362 
5363 	if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
5364 		I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5365 
5366 	if (ieee80211_is_mgmt(fc)) {
5367 		/* drop frame if too short for header */
5368 		if (skb->len < ieee80211_hdrlen(fc))
5369 			err = -ENOBUFS;
5370 		else
5371 			err = skb_linearize(skb);
5372 	} else if (ieee80211_is_s1g_beacon(fc)) {
5373 		size_t s1g_hdr_len = offsetof(struct ieee80211_ext,
5374 					      u.s1g_beacon.variable) +
5375 				     ieee80211_s1g_optional_len(fc);
5376 
5377 		if (skb->len < s1g_hdr_len)
5378 			err = -ENOBUFS;
5379 		else
5380 			err = skb_linearize(skb);
5381 	} else if (ieee80211_is_ext(fc)) {
5382 		err = -EINVAL;
5383 	} else {
5384 		err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
5385 	}
5386 
5387 	if (err) {
5388 		dev_kfree_skb(skb);
5389 		return;
5390 	}
5391 
5392 	hdr = (struct ieee80211_hdr *)skb->data;
5393 	ieee80211_parse_qos(&rx);
5394 	ieee80211_verify_alignment(&rx);
5395 
5396 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
5397 		     ieee80211_is_beacon(hdr->frame_control) ||
5398 		     ieee80211_is_s1g_beacon(hdr->frame_control)))
5399 		ieee80211_scan_rx(local, skb);
5400 
5401 	/*
5402 	 * RX of a data frame should only happen to existing stations.
5403 	 * It is therefore more efficient to use the one provided by the driver
5404 	 * or search based on the station's address.
5405 	 *
5406 	 * Note we will fall through and handle a spurious data frame in the
5407 	 * same way as a management frame.
5408 	 */
5409 	if (ieee80211_is_data(fc)) {
5410 		if (link_pubsta) {
5411 			sta = container_of(link_pubsta->sta, struct sta_info,
5412 					   sta);
5413 			link_sta = rcu_dereference(sta->link[link_pubsta->link_id]);
5414 
5415 			rx.sdata = sta->sdata;
5416 			if (ieee80211_rx_data_set_link_sta(&rx, link_sta) &&
5417 			    ieee80211_prepare_and_rx_handle(&rx, skb, true))
5418 				return;
5419 
5420 			goto out;
5421 		}
5422 
5423 		rx_data_pending = false;
5424 
5425 		/* Search for stations on non-MLD interfaces */
5426 		for_each_sta_info(local, hdr->addr2, sta, tmp) {
5427 			struct ieee80211_link_data *link;
5428 
5429 			if (ieee80211_vif_is_mld(&sta->sdata->vif))
5430 				continue;
5431 
5432 			link = &sta->sdata->deflink;
5433 			if (!ieee80211_rx_valid_freq(status->freq, link))
5434 				continue;
5435 
5436 			if (rx_data_pending) {
5437 				ieee80211_prepare_and_rx_handle(&rx, skb,
5438 								false);
5439 				rx_data_pending = false;
5440 			}
5441 
5442 			rx.sdata = sta->sdata;
5443 			if (!ieee80211_rx_data_set_link_sta(&rx, &sta->deflink))
5444 				continue;
5445 
5446 			rx_data_pending = true;
5447 		}
5448 
5449 		/* And search stations on MLD interfaces */
5450 		for_each_link_sta_info(local, hdr->addr2, link_sta, tmp) {
5451 			struct ieee80211_link_data *link;
5452 
5453 			sta = link_sta->sta;
5454 			sdata =	sta->sdata;
5455 			link = rcu_dereference(sdata->link[link_sta->link_id]);
5456 
5457 			if (!ieee80211_rx_valid_freq(status->freq, link))
5458 				continue;
5459 
5460 			if (rx_data_pending) {
5461 				ieee80211_prepare_and_rx_handle(&rx, skb,
5462 								false);
5463 				rx_data_pending = false;
5464 			}
5465 
5466 			rx.sdata = sta->sdata;
5467 			if (!ieee80211_rx_data_set_link_sta(&rx, link_sta))
5468 				continue;
5469 
5470 			rx_data_pending = true;
5471 		}
5472 
5473 		if (rx_data_pending) {
5474 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5475 				return;
5476 
5477 			goto out;
5478 		}
5479 
5480 		/* fall through, e.g. for spurious frame notification */
5481 	}
5482 
5483 	/*
5484 	 * This is a management frame (or a data frame without a station) and
5485 	 * it will be delivered independent of whether a station exists,
5486 	 * so iterate the interfaces.
5487 	 */
5488 	rx_data_pending = false;
5489 
5490 	/* We expect the driver to provide frequency information */
5491 	if (WARN_ON_ONCE(!local->emulate_chanctx && !status->freq))
5492 		goto out;
5493 
5494 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
5495 		struct ieee80211_link_data *link = NULL;
5496 
5497 		if (!ieee80211_sdata_running(sdata))
5498 			continue;
5499 
5500 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
5501 		    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5502 			continue;
5503 
5504 		link_sta = NULL;
5505 		link = NULL;
5506 
5507 		/* Try to resolve the station */
5508 		if (!ieee80211_vif_is_mld(&sdata->vif)) {
5509 			sta = sta_info_get_bss(sdata, hdr->addr2);
5510 
5511 			if (sta) {
5512 				link_sta = &sta->deflink;
5513 				link = &sdata->deflink;
5514 			}
5515 		} else {
5516 			link_sta = link_sta_info_get_bss(sdata, hdr->addr2);
5517 
5518 			if (link_sta)
5519 				link = rcu_dereference(sdata->link[link_sta->link_id]);
5520 		}
5521 
5522 		/*
5523 		 * If we found a STA and it has a valid link information, then
5524 		 * RX using the station.
5525 		 */
5526 		if (link_sta && link &&
5527 		    ieee80211_rx_valid_freq(status->freq, link)) {
5528 			if (rx_data_pending) {
5529 				ieee80211_prepare_and_rx_handle(&rx, skb, false);
5530 				rx_data_pending = false;
5531 			}
5532 
5533 			/* No valid_links check as we need to RX beacons */
5534 
5535 			rx.sdata = sdata;
5536 			if (ieee80211_rx_data_set_link_sta(&rx, link_sta))
5537 				rx_data_pending = true;
5538 
5539 			continue;
5540 		}
5541 
5542 		/* No station, try to resolve the link and RX */
5543 		if (ieee80211_vif_is_mld(&sdata->vif)) {
5544 			struct ieee80211_chanctx_conf *conf;
5545 			bool found = false;
5546 
5547 			for_each_link_data_rcu(sdata, link) {
5548 				conf = rcu_dereference(link->conf->chanctx_conf);
5549 				if (!conf || !conf->def.chan)
5550 					continue;
5551 
5552 				if (status->freq == conf->def.chan->center_freq) {
5553 					found = true;
5554 					break;
5555 				}
5556 			}
5557 
5558 			if (!found)
5559 				link = &sdata->deflink;
5560 		} else {
5561 			link = &sdata->deflink;
5562 		}
5563 
5564 		if (rx_data_pending) {
5565 			ieee80211_prepare_and_rx_handle(&rx, skb, false);
5566 			rx_data_pending = false;
5567 		}
5568 
5569 		rx.sdata = sdata;
5570 		rx.local = sdata->local;
5571 		rx.link = link;
5572 		rx.link_id = link->link_id;
5573 		rx.sta = NULL;
5574 		rx.link_sta = NULL;
5575 
5576 		rx_data_pending = true;
5577 	}
5578 
5579 	if (rx_data_pending &&
5580 	    ieee80211_prepare_and_rx_handle(&rx, skb, true))
5581 		return;
5582 
5583  out:
5584 	dev_kfree_skb(skb);
5585 }
5586 
5587 /*
5588  * This is the receive path handler. It is called by a low level driver when an
5589  * 802.11 MPDU is received from the hardware.
5590  */
5591 void ieee80211_rx_list(struct ieee80211_hw *hw,
5592 		       struct ieee80211_link_sta *link_pubsta,
5593 		       struct sk_buff *skb, struct list_head *list)
5594 {
5595 	struct ieee80211_local *local = hw_to_local(hw);
5596 	struct ieee80211_rate *rate = NULL;
5597 	struct ieee80211_supported_band *sband;
5598 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5599 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5600 
5601 	WARN_ON_ONCE(softirq_count() == 0);
5602 
5603 	if (WARN_ON(status->band >= NUM_NL80211_BANDS))
5604 		goto drop;
5605 
5606 	sband = local->hw.wiphy->bands[status->band];
5607 	if (WARN_ON(!sband))
5608 		goto drop;
5609 
5610 	/*
5611 	 * If we're suspending, it is possible although not too likely
5612 	 * that we'd be receiving frames after having already partially
5613 	 * quiesced the stack. We can't process such frames then since
5614 	 * that might, for example, cause stations to be added or other
5615 	 * driver callbacks be invoked.
5616 	 */
5617 	if (unlikely(local->quiescing || local->suspended))
5618 		goto drop;
5619 
5620 	/* We might be during a HW reconfig, prevent Rx for the same reason */
5621 	if (unlikely(local->in_reconfig))
5622 		goto drop;
5623 
5624 	/*
5625 	 * The same happens when we're not even started,
5626 	 * but that's worth a warning.
5627 	 */
5628 	if (WARN_ON(!local->started))
5629 		goto drop;
5630 
5631 	if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC) &&
5632 		   !(status->flag & RX_FLAG_NO_PSDU &&
5633 		     status->zero_length_psdu_type ==
5634 		     IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED))) {
5635 		/*
5636 		 * Validate the rate, unless there was a PLCP error which may
5637 		 * have an invalid rate or the PSDU was not capture and may be
5638 		 * missing rate information.
5639 		 */
5640 
5641 		switch (status->encoding) {
5642 		case RX_ENC_HT:
5643 			/*
5644 			 * rate_idx is MCS index, which can be [0-76]
5645 			 * as documented on:
5646 			 *
5647 			 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
5648 			 *
5649 			 * Anything else would be some sort of driver or
5650 			 * hardware error. The driver should catch hardware
5651 			 * errors.
5652 			 */
5653 			if (WARN(status->rate_idx > 76,
5654 				 "Rate marked as an HT rate but passed "
5655 				 "status->rate_idx is not "
5656 				 "an MCS index [0-76]: %d (0x%02x)\n",
5657 				 status->rate_idx,
5658 				 status->rate_idx))
5659 				goto drop;
5660 			break;
5661 		case RX_ENC_VHT:
5662 			if (WARN_ONCE(status->rate_idx > 11 ||
5663 				      !status->nss ||
5664 				      status->nss > 8,
5665 				      "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
5666 				      status->rate_idx, status->nss))
5667 				goto drop;
5668 			break;
5669 		case RX_ENC_HE:
5670 			if (WARN_ONCE(status->rate_idx > 11 ||
5671 				      !status->nss ||
5672 				      status->nss > 8,
5673 				      "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
5674 				      status->rate_idx, status->nss))
5675 				goto drop;
5676 			break;
5677 		case RX_ENC_EHT:
5678 			if (WARN_ONCE(status->rate_idx > 15 ||
5679 				      !status->nss ||
5680 				      status->nss > 8 ||
5681 				      status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5682 				      "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5683 				      status->rate_idx, status->nss, status->eht.gi))
5684 				goto drop;
5685 			break;
5686 		case RX_ENC_UHR:
5687 			if (WARN_ONCE(!(status->rate_idx <= 15 ||
5688 					status->rate_idx == 17 ||
5689 					status->rate_idx == 19 ||
5690 					status->rate_idx == 20 ||
5691 					status->rate_idx == 23) ||
5692 				      !status->nss ||
5693 				      status->nss > 8 ||
5694 				      status->uhr.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5695 				      "Rate marked as a UHR rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5696 				      status->rate_idx, status->nss, status->uhr.gi))
5697 				goto drop;
5698 			if (WARN_ONCE(status->uhr.elr &&
5699 				      (status->nss != 1 || status->rate_idx > 1 ||
5700 				       status->uhr.gi != NL80211_RATE_INFO_EHT_GI_1_6 ||
5701 				       status->bw != RATE_INFO_BW_20 || status->uhr.im),
5702 				      "bad UHR ELR MCS MCS:%d, NSS:%d, GI:%d, BW:%d, IM:%d\n",
5703 				      status->rate_idx, status->nss, status->uhr.gi,
5704 				      status->bw, status->uhr.im))
5705 				goto drop;
5706 			if (WARN_ONCE(status->uhr.im &&
5707 				      (status->nss != 1 || status->rate_idx == 15),
5708 				      "bad UHR IM MCS MCS:%d, NSS:%d\n",
5709 				      status->rate_idx, status->nss))
5710 				goto drop;
5711 			break;
5712 		case RX_ENC_S1G:
5713 			if (WARN_ONCE(status->rate_idx > 12 ||
5714 				      !status->nss ||
5715 				      status->nss > 4,
5716 				      "Rate marked as an S1G rate but data is invalid: MCS: %d, NSS: %d\n",
5717 				      status->rate_idx, status->nss))
5718 				goto drop;
5719 			break;
5720 		default:
5721 			WARN_ON_ONCE(1);
5722 			fallthrough;
5723 		case RX_ENC_LEGACY:
5724 			if (WARN_ON(status->rate_idx >= sband->n_bitrates))
5725 				goto drop;
5726 			rate = &sband->bitrates[status->rate_idx];
5727 		}
5728 	}
5729 
5730 	status->rx_flags = 0;
5731 
5732 	kcov_remote_start_common(skb_get_kcov_handle(skb));
5733 
5734 	/*
5735 	 * Frames with failed FCS/PLCP checksum are not returned,
5736 	 * all other frames are returned without radiotap header
5737 	 * if it was previously present.
5738 	 * Also, frames with less than 16 bytes are dropped.
5739 	 */
5740 	if (!(status->flag & RX_FLAG_8023))
5741 		skb = ieee80211_rx_monitor(local, skb, rate);
5742 	if (skb) {
5743 		if ((status->flag & RX_FLAG_8023) ||
5744 			ieee80211_is_data_present(hdr->frame_control))
5745 			ieee80211_tpt_led_trig_rx(local, skb->len);
5746 
5747 		if (status->flag & RX_FLAG_8023)
5748 			__ieee80211_rx_handle_8023(hw, link_pubsta, skb, list);
5749 		else
5750 			__ieee80211_rx_handle_packet(hw, link_pubsta, skb,
5751 						     list);
5752 	}
5753 
5754 	kcov_remote_stop();
5755 	return;
5756  drop:
5757 	kfree_skb(skb);
5758 }
5759 EXPORT_SYMBOL(ieee80211_rx_list);
5760 
5761 void ieee80211_rx_napi(struct ieee80211_hw *hw,
5762 		       struct ieee80211_link_sta *link_pubsta,
5763 		       struct sk_buff *skb, struct napi_struct *napi)
5764 {
5765 	struct sk_buff *tmp;
5766 	LIST_HEAD(list);
5767 
5768 
5769 	/*
5770 	 * key references and virtual interfaces are protected using RCU
5771 	 * and this requires that we are in a read-side RCU section during
5772 	 * receive processing
5773 	 */
5774 	rcu_read_lock();
5775 	ieee80211_rx_list(hw, link_pubsta, skb, &list);
5776 	rcu_read_unlock();
5777 
5778 	if (!napi) {
5779 		netif_receive_skb_list(&list);
5780 		return;
5781 	}
5782 
5783 	list_for_each_entry_safe(skb, tmp, &list, list) {
5784 		skb_list_del_init(skb);
5785 		napi_gro_receive(napi, skb);
5786 	}
5787 }
5788 EXPORT_SYMBOL(ieee80211_rx_napi);
5789 
5790 /* This is a version of the rx handler that can be called from hard irq
5791  * context. Post the skb on the queue and schedule the tasklet */
5792 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
5793 {
5794 	struct ieee80211_local *local = hw_to_local(hw);
5795 
5796 	BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5797 
5798 	skb->pkt_type = IEEE80211_RX_MSG;
5799 	skb_queue_tail(&local->skb_queue, skb);
5800 	tasklet_schedule(&local->tasklet);
5801 }
5802 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
5803