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