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