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