xref: /linux/net/mac80211/rx.c (revision af2d6148d2a159e1a0862bce5a2c88c1618a2b27)
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 	struct ethhdr ethhdr;
3037 	const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
3038 
3039 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3040 		check_da = NULL;
3041 		check_sa = NULL;
3042 	} else switch (rx->sdata->vif.type) {
3043 		case NL80211_IFTYPE_AP:
3044 		case NL80211_IFTYPE_AP_VLAN:
3045 			check_da = NULL;
3046 			break;
3047 		case NL80211_IFTYPE_STATION:
3048 			if (!test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
3049 				check_sa = NULL;
3050 			break;
3051 		case NL80211_IFTYPE_MESH_POINT:
3052 			check_sa = NULL;
3053 			check_da = NULL;
3054 			break;
3055 		default:
3056 			break;
3057 	}
3058 
3059 	skb->dev = dev;
3060 	__skb_queue_head_init(&frame_list);
3061 
3062 	if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
3063 					  rx->sdata->vif.addr,
3064 					  rx->sdata->vif.type,
3065 					  data_offset, true))
3066 		return RX_DROP_U_BAD_AMSDU;
3067 
3068 	if (rx->sta->amsdu_mesh_control < 0) {
3069 		s8 valid = -1;
3070 		int i;
3071 
3072 		for (i = 0; i <= 2; i++) {
3073 			if (!ieee80211_is_valid_amsdu(skb, i))
3074 				continue;
3075 
3076 			if (valid >= 0) {
3077 				/* ambiguous */
3078 				valid = -1;
3079 				break;
3080 			}
3081 
3082 			valid = i;
3083 		}
3084 
3085 		rx->sta->amsdu_mesh_control = valid;
3086 	}
3087 
3088 	ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
3089 				 rx->sdata->vif.type,
3090 				 rx->local->hw.extra_tx_headroom,
3091 				 check_da, check_sa,
3092 				 rx->sta->amsdu_mesh_control);
3093 
3094 	while (!skb_queue_empty(&frame_list)) {
3095 		rx->skb = __skb_dequeue(&frame_list);
3096 
3097 		switch (ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb)) {
3098 		case RX_QUEUED:
3099 			break;
3100 		case RX_CONTINUE:
3101 			if (ieee80211_frame_allowed(rx, fc)) {
3102 				ieee80211_deliver_skb(rx);
3103 				break;
3104 			}
3105 			fallthrough;
3106 		default:
3107 			dev_kfree_skb(rx->skb);
3108 		}
3109 	}
3110 
3111 	return RX_QUEUED;
3112 }
3113 
3114 static ieee80211_rx_result debug_noinline
3115 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
3116 {
3117 	struct sk_buff *skb = rx->skb;
3118 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3119 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3120 	__le16 fc = hdr->frame_control;
3121 
3122 	if (!(status->rx_flags & IEEE80211_RX_AMSDU))
3123 		return RX_CONTINUE;
3124 
3125 	if (unlikely(!ieee80211_is_data(fc)))
3126 		return RX_CONTINUE;
3127 
3128 	if (unlikely(!ieee80211_is_data_present(fc)))
3129 		return RX_DROP;
3130 
3131 	if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3132 		switch (rx->sdata->vif.type) {
3133 		case NL80211_IFTYPE_AP_VLAN:
3134 			if (!rx->sdata->u.vlan.sta)
3135 				return RX_DROP_U_BAD_4ADDR;
3136 			break;
3137 		case NL80211_IFTYPE_STATION:
3138 			if (!rx->sdata->u.mgd.use_4addr)
3139 				return RX_DROP_U_BAD_4ADDR;
3140 			break;
3141 		case NL80211_IFTYPE_MESH_POINT:
3142 			break;
3143 		default:
3144 			return RX_DROP_U_BAD_4ADDR;
3145 		}
3146 	}
3147 
3148 	if (is_multicast_ether_addr(hdr->addr1) || !rx->sta)
3149 		return RX_DROP_U_BAD_AMSDU;
3150 
3151 	if (rx->key) {
3152 		/*
3153 		 * We should not receive A-MSDUs on pre-HT connections,
3154 		 * and HT connections cannot use old ciphers. Thus drop
3155 		 * them, as in those cases we couldn't even have SPP
3156 		 * A-MSDUs or such.
3157 		 */
3158 		switch (rx->key->conf.cipher) {
3159 		case WLAN_CIPHER_SUITE_WEP40:
3160 		case WLAN_CIPHER_SUITE_WEP104:
3161 		case WLAN_CIPHER_SUITE_TKIP:
3162 			return RX_DROP_U_BAD_AMSDU_CIPHER;
3163 		default:
3164 			break;
3165 		}
3166 	}
3167 
3168 	return __ieee80211_rx_h_amsdu(rx, 0);
3169 }
3170 
3171 static ieee80211_rx_result debug_noinline
3172 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
3173 {
3174 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3175 	struct ieee80211_local *local = rx->local;
3176 	struct net_device *dev = sdata->dev;
3177 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
3178 	__le16 fc = hdr->frame_control;
3179 	ieee80211_rx_result res;
3180 	bool port_control;
3181 
3182 	if (unlikely(!ieee80211_is_data(hdr->frame_control)))
3183 		return RX_CONTINUE;
3184 
3185 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3186 		return RX_DROP;
3187 
3188 	/* Send unexpected-4addr-frame event to hostapd */
3189 	if (ieee80211_has_a4(hdr->frame_control) &&
3190 	    sdata->vif.type == NL80211_IFTYPE_AP) {
3191 		if (rx->sta &&
3192 		    !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3193 			cfg80211_rx_unexpected_4addr_frame(
3194 				rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
3195 		return RX_DROP;
3196 	}
3197 
3198 	res = __ieee80211_data_to_8023(rx, &port_control);
3199 	if (unlikely(res != RX_CONTINUE))
3200 		return res;
3201 
3202 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3203 	if (res != RX_CONTINUE)
3204 		return res;
3205 
3206 	if (!ieee80211_frame_allowed(rx, fc))
3207 		return RX_DROP;
3208 
3209 	/* directly handle TDLS channel switch requests/responses */
3210 	if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3211 						cpu_to_be16(ETH_P_TDLS))) {
3212 		struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3213 
3214 		if (pskb_may_pull(rx->skb,
3215 				  offsetof(struct ieee80211_tdls_data, u)) &&
3216 		    tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3217 		    tf->category == WLAN_CATEGORY_TDLS &&
3218 		    (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3219 		     tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3220 			rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3221 			__ieee80211_queue_skb_to_iface(sdata, rx->link_id,
3222 						       rx->sta, rx->skb);
3223 			return RX_QUEUED;
3224 		}
3225 	}
3226 
3227 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3228 	    unlikely(port_control) && sdata->bss) {
3229 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3230 				     u.ap);
3231 		dev = sdata->dev;
3232 		rx->sdata = sdata;
3233 	}
3234 
3235 	rx->skb->dev = dev;
3236 
3237 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3238 	    local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3239 	    !is_multicast_ether_addr(
3240 		    ((struct ethhdr *)rx->skb->data)->h_dest) &&
3241 	    (!local->scanning &&
3242 	     !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3243 		mod_timer(&local->dynamic_ps_timer, jiffies +
3244 			  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3245 
3246 	ieee80211_deliver_skb(rx);
3247 
3248 	return RX_QUEUED;
3249 }
3250 
3251 static ieee80211_rx_result debug_noinline
3252 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3253 {
3254 	struct sk_buff *skb = rx->skb;
3255 	struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3256 	struct tid_ampdu_rx *tid_agg_rx;
3257 	u16 start_seq_num;
3258 	u16 tid;
3259 
3260 	if (likely(!ieee80211_is_ctl(bar->frame_control)))
3261 		return RX_CONTINUE;
3262 
3263 	if (ieee80211_is_back_req(bar->frame_control)) {
3264 		struct {
3265 			__le16 control, start_seq_num;
3266 		} __packed bar_data;
3267 		struct ieee80211_event event = {
3268 			.type = BAR_RX_EVENT,
3269 		};
3270 
3271 		if (!rx->sta)
3272 			return RX_DROP;
3273 
3274 		if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3275 				  &bar_data, sizeof(bar_data)))
3276 			return RX_DROP;
3277 
3278 		tid = le16_to_cpu(bar_data.control) >> 12;
3279 
3280 		if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3281 		    !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3282 			ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3283 					     WLAN_BACK_RECIPIENT,
3284 					     WLAN_REASON_QSTA_REQUIRE_SETUP);
3285 
3286 		tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3287 		if (!tid_agg_rx)
3288 			return RX_DROP;
3289 
3290 		start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3291 		event.u.ba.tid = tid;
3292 		event.u.ba.ssn = start_seq_num;
3293 		event.u.ba.sta = &rx->sta->sta;
3294 
3295 		/* reset session timer */
3296 		if (tid_agg_rx->timeout)
3297 			mod_timer(&tid_agg_rx->session_timer,
3298 				  TU_TO_EXP_TIME(tid_agg_rx->timeout));
3299 
3300 		spin_lock(&tid_agg_rx->reorder_lock);
3301 		/* release stored frames up to start of BAR */
3302 		ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3303 						 start_seq_num, frames);
3304 		spin_unlock(&tid_agg_rx->reorder_lock);
3305 
3306 		drv_event_callback(rx->local, rx->sdata, &event);
3307 
3308 		kfree_skb(skb);
3309 		return RX_QUEUED;
3310 	}
3311 
3312 	return RX_DROP;
3313 }
3314 
3315 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3316 					   struct ieee80211_mgmt *mgmt,
3317 					   size_t len)
3318 {
3319 	struct ieee80211_local *local = sdata->local;
3320 	struct sk_buff *skb;
3321 	struct ieee80211_mgmt *resp;
3322 
3323 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3324 		/* Not to own unicast address */
3325 		return;
3326 	}
3327 
3328 	if (!ether_addr_equal(mgmt->sa, sdata->vif.cfg.ap_addr) ||
3329 	    !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
3330 		/* Not from the current AP or not associated yet. */
3331 		return;
3332 	}
3333 
3334 	if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3335 		/* Too short SA Query request frame */
3336 		return;
3337 	}
3338 
3339 	skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3340 	if (skb == NULL)
3341 		return;
3342 
3343 	skb_reserve(skb, local->hw.extra_tx_headroom);
3344 	resp = skb_put_zero(skb, 24);
3345 	memcpy(resp->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
3346 	memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3347 	memcpy(resp->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
3348 	resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3349 					  IEEE80211_STYPE_ACTION);
3350 	skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3351 	resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3352 	resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3353 	memcpy(resp->u.action.u.sa_query.trans_id,
3354 	       mgmt->u.action.u.sa_query.trans_id,
3355 	       WLAN_SA_QUERY_TR_ID_LEN);
3356 
3357 	ieee80211_tx_skb(sdata, skb);
3358 }
3359 
3360 static void
3361 ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx)
3362 {
3363 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3364 	struct ieee80211_bss_conf *bss_conf;
3365 	const struct element *ie;
3366 	size_t baselen;
3367 
3368 	if (!wiphy_ext_feature_isset(rx->local->hw.wiphy,
3369 				     NL80211_EXT_FEATURE_BSS_COLOR))
3370 		return;
3371 
3372 	if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION))
3373 		return;
3374 
3375 	bss_conf = rx->link->conf;
3376 	if (bss_conf->csa_active || bss_conf->color_change_active ||
3377 	    !bss_conf->he_bss_color.enabled)
3378 		return;
3379 
3380 	baselen = mgmt->u.beacon.variable - rx->skb->data;
3381 	if (baselen > rx->skb->len)
3382 		return;
3383 
3384 	ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
3385 				    mgmt->u.beacon.variable,
3386 				    rx->skb->len - baselen);
3387 	if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) &&
3388 	    ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) {
3389 		const struct ieee80211_he_operation *he_oper;
3390 		u8 color;
3391 
3392 		he_oper = (void *)(ie->data + 1);
3393 		if (le32_get_bits(he_oper->he_oper_params,
3394 				  IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED))
3395 			return;
3396 
3397 		color = le32_get_bits(he_oper->he_oper_params,
3398 				      IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3399 		if (color == bss_conf->he_bss_color.color)
3400 			ieee80211_obss_color_collision_notify(&rx->sdata->vif,
3401 							      BIT_ULL(color),
3402 							      bss_conf->link_id);
3403 	}
3404 }
3405 
3406 static ieee80211_rx_result debug_noinline
3407 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3408 {
3409 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3410 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3411 
3412 	if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3413 		return RX_CONTINUE;
3414 
3415 	/*
3416 	 * From here on, look only at management frames.
3417 	 * Data and control frames are already handled,
3418 	 * and unknown (reserved) frames are useless.
3419 	 */
3420 	if (rx->skb->len < 24)
3421 		return RX_DROP;
3422 
3423 	if (!ieee80211_is_mgmt(mgmt->frame_control))
3424 		return RX_DROP;
3425 
3426 	/* drop too small action frames */
3427 	if (ieee80211_is_action(mgmt->frame_control) &&
3428 	    rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
3429 		return RX_DROP_U_RUNT_ACTION;
3430 
3431 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3432 	    ieee80211_is_beacon(mgmt->frame_control) &&
3433 	    !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3434 		int sig = 0;
3435 
3436 		/* sw bss color collision detection */
3437 		ieee80211_rx_check_bss_color_collision(rx);
3438 
3439 		if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3440 		    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3441 			sig = status->signal;
3442 
3443 		cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3444 						rx->skb->data, rx->skb->len,
3445 						ieee80211_rx_status_to_khz(status),
3446 						sig);
3447 		rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3448 	}
3449 
3450 	return ieee80211_drop_unencrypted_mgmt(rx);
3451 }
3452 
3453 static bool
3454 ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3455 {
3456 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
3457 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3458 
3459 	/* TWT actions are only supported in AP for the moment */
3460 	if (sdata->vif.type != NL80211_IFTYPE_AP)
3461 		return false;
3462 
3463 	if (!rx->local->ops->add_twt_setup)
3464 		return false;
3465 
3466 	if (!sdata->vif.bss_conf.twt_responder)
3467 		return false;
3468 
3469 	if (!rx->sta)
3470 		return false;
3471 
3472 	switch (mgmt->u.action.u.s1g.action_code) {
3473 	case WLAN_S1G_TWT_SETUP: {
3474 		struct ieee80211_twt_setup *twt;
3475 
3476 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3477 				   1 + /* action code */
3478 				   sizeof(struct ieee80211_twt_setup) +
3479 				   2 /* TWT req_type agrt */)
3480 			break;
3481 
3482 		twt = (void *)mgmt->u.action.u.s1g.variable;
3483 		if (twt->element_id != WLAN_EID_S1G_TWT)
3484 			break;
3485 
3486 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3487 				   4 + /* action code + token + tlv */
3488 				   twt->length)
3489 			break;
3490 
3491 		return true; /* queue the frame */
3492 	}
3493 	case WLAN_S1G_TWT_TEARDOWN:
3494 		if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE + 2)
3495 			break;
3496 
3497 		return true; /* queue the frame */
3498 	default:
3499 		break;
3500 	}
3501 
3502 	return false;
3503 }
3504 
3505 static ieee80211_rx_result debug_noinline
3506 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3507 {
3508 	struct ieee80211_local *local = rx->local;
3509 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3510 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3511 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3512 	int len = rx->skb->len;
3513 
3514 	if (!ieee80211_is_action(mgmt->frame_control))
3515 		return RX_CONTINUE;
3516 
3517 	if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3518 	    mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3519 	    mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3520 		return RX_DROP_U_ACTION_UNKNOWN_SRC;
3521 
3522 	switch (mgmt->u.action.category) {
3523 	case WLAN_CATEGORY_HT:
3524 		/* reject HT action frames from stations not supporting HT */
3525 		if (!rx->link_sta->pub->ht_cap.ht_supported)
3526 			goto invalid;
3527 
3528 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3529 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3530 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3531 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3532 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3533 			break;
3534 
3535 		/* verify action & smps_control/chanwidth are present */
3536 		if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3537 			goto invalid;
3538 
3539 		switch (mgmt->u.action.u.ht_smps.action) {
3540 		case WLAN_HT_ACTION_SMPS: {
3541 			struct ieee80211_supported_band *sband;
3542 			enum ieee80211_smps_mode smps_mode;
3543 			struct sta_opmode_info sta_opmode = {};
3544 
3545 			if (sdata->vif.type != NL80211_IFTYPE_AP &&
3546 			    sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3547 				goto handled;
3548 
3549 			/* convert to HT capability */
3550 			switch (mgmt->u.action.u.ht_smps.smps_control) {
3551 			case WLAN_HT_SMPS_CONTROL_DISABLED:
3552 				smps_mode = IEEE80211_SMPS_OFF;
3553 				break;
3554 			case WLAN_HT_SMPS_CONTROL_STATIC:
3555 				smps_mode = IEEE80211_SMPS_STATIC;
3556 				break;
3557 			case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3558 				smps_mode = IEEE80211_SMPS_DYNAMIC;
3559 				break;
3560 			default:
3561 				goto invalid;
3562 			}
3563 
3564 			/* if no change do nothing */
3565 			if (rx->link_sta->pub->smps_mode == smps_mode)
3566 				goto handled;
3567 			rx->link_sta->pub->smps_mode = smps_mode;
3568 			sta_opmode.smps_mode =
3569 				ieee80211_smps_mode_to_smps_mode(smps_mode);
3570 			sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3571 
3572 			sband = rx->local->hw.wiphy->bands[status->band];
3573 
3574 			rate_control_rate_update(local, sband, rx->link_sta,
3575 						 IEEE80211_RC_SMPS_CHANGED);
3576 			cfg80211_sta_opmode_change_notify(sdata->dev,
3577 							  rx->sta->addr,
3578 							  &sta_opmode,
3579 							  GFP_ATOMIC);
3580 			goto handled;
3581 		}
3582 		case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3583 			u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3584 
3585 			if (chanwidth != IEEE80211_HT_CHANWIDTH_20MHZ &&
3586 			    chanwidth != IEEE80211_HT_CHANWIDTH_ANY)
3587 				goto invalid;
3588 
3589 			/* If it doesn't support 40 MHz it can't change ... */
3590 			if (!(rx->link_sta->pub->ht_cap.cap &
3591 				IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3592 				goto handled;
3593 
3594 			goto queue;
3595 		}
3596 		default:
3597 			goto invalid;
3598 		}
3599 
3600 		break;
3601 	case WLAN_CATEGORY_PUBLIC:
3602 	case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
3603 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3604 			goto invalid;
3605 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3606 			break;
3607 		if (!rx->sta)
3608 			break;
3609 		if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid))
3610 			break;
3611 		if (mgmt->u.action.u.ext_chan_switch.action_code !=
3612 				WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3613 			break;
3614 		if (len < offsetof(struct ieee80211_mgmt,
3615 				   u.action.u.ext_chan_switch.variable))
3616 			goto invalid;
3617 		goto queue;
3618 	case WLAN_CATEGORY_VHT:
3619 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3620 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3621 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3622 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3623 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3624 			break;
3625 
3626 		/* verify action code is present */
3627 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3628 			goto invalid;
3629 
3630 		switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3631 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3632 			/* verify opmode is present */
3633 			if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3634 				goto invalid;
3635 			goto queue;
3636 		}
3637 		case WLAN_VHT_ACTION_GROUPID_MGMT: {
3638 			if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3639 				goto invalid;
3640 			goto queue;
3641 		}
3642 		default:
3643 			break;
3644 		}
3645 		break;
3646 	case WLAN_CATEGORY_BACK:
3647 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3648 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3649 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3650 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3651 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3652 			break;
3653 
3654 		/* verify action_code is present */
3655 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3656 			break;
3657 
3658 		switch (mgmt->u.action.u.addba_req.action_code) {
3659 		case WLAN_ACTION_ADDBA_REQ:
3660 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3661 				   sizeof(mgmt->u.action.u.addba_req)))
3662 				goto invalid;
3663 			break;
3664 		case WLAN_ACTION_ADDBA_RESP:
3665 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3666 				   sizeof(mgmt->u.action.u.addba_resp)))
3667 				goto invalid;
3668 			break;
3669 		case WLAN_ACTION_DELBA:
3670 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3671 				   sizeof(mgmt->u.action.u.delba)))
3672 				goto invalid;
3673 			break;
3674 		default:
3675 			goto invalid;
3676 		}
3677 
3678 		goto queue;
3679 	case WLAN_CATEGORY_SPECTRUM_MGMT:
3680 		/* verify action_code is present */
3681 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3682 			break;
3683 
3684 		switch (mgmt->u.action.u.measurement.action_code) {
3685 		case WLAN_ACTION_SPCT_MSR_REQ:
3686 			if (status->band != NL80211_BAND_5GHZ)
3687 				break;
3688 
3689 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3690 				   sizeof(mgmt->u.action.u.measurement)))
3691 				break;
3692 
3693 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3694 				break;
3695 
3696 			ieee80211_process_measurement_req(sdata, mgmt, len);
3697 			goto handled;
3698 		case WLAN_ACTION_SPCT_CHL_SWITCH: {
3699 			u8 *bssid;
3700 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3701 				   sizeof(mgmt->u.action.u.chan_switch)))
3702 				break;
3703 
3704 			if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3705 			    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3706 			    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3707 				break;
3708 
3709 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
3710 				bssid = sdata->deflink.u.mgd.bssid;
3711 			else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3712 				bssid = sdata->u.ibss.bssid;
3713 			else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3714 				bssid = mgmt->sa;
3715 			else
3716 				break;
3717 
3718 			if (!ether_addr_equal(mgmt->bssid, bssid))
3719 				break;
3720 
3721 			goto queue;
3722 			}
3723 		}
3724 		break;
3725 	case WLAN_CATEGORY_SELF_PROTECTED:
3726 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3727 			   sizeof(mgmt->u.action.u.self_prot.action_code)))
3728 			break;
3729 
3730 		switch (mgmt->u.action.u.self_prot.action_code) {
3731 		case WLAN_SP_MESH_PEERING_OPEN:
3732 		case WLAN_SP_MESH_PEERING_CLOSE:
3733 		case WLAN_SP_MESH_PEERING_CONFIRM:
3734 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3735 				goto invalid;
3736 			if (sdata->u.mesh.user_mpm)
3737 				/* userspace handles this frame */
3738 				break;
3739 			goto queue;
3740 		case WLAN_SP_MGK_INFORM:
3741 		case WLAN_SP_MGK_ACK:
3742 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3743 				goto invalid;
3744 			break;
3745 		}
3746 		break;
3747 	case WLAN_CATEGORY_MESH_ACTION:
3748 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3749 			   sizeof(mgmt->u.action.u.mesh_action.action_code)))
3750 			break;
3751 
3752 		if (!ieee80211_vif_is_mesh(&sdata->vif))
3753 			break;
3754 		if (mesh_action_is_path_sel(mgmt) &&
3755 		    !mesh_path_sel_is_hwmp(sdata))
3756 			break;
3757 		goto queue;
3758 	case WLAN_CATEGORY_S1G:
3759 		if (len < offsetofend(typeof(*mgmt),
3760 				      u.action.u.s1g.action_code))
3761 			break;
3762 
3763 		switch (mgmt->u.action.u.s1g.action_code) {
3764 		case WLAN_S1G_TWT_SETUP:
3765 		case WLAN_S1G_TWT_TEARDOWN:
3766 			if (ieee80211_process_rx_twt_action(rx))
3767 				goto queue;
3768 			break;
3769 		default:
3770 			break;
3771 		}
3772 		break;
3773 	case WLAN_CATEGORY_PROTECTED_EHT:
3774 		if (len < offsetofend(typeof(*mgmt),
3775 				      u.action.u.ttlm_req.action_code))
3776 			break;
3777 
3778 		switch (mgmt->u.action.u.ttlm_req.action_code) {
3779 		case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
3780 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3781 				break;
3782 
3783 			if (len < offsetofend(typeof(*mgmt),
3784 					      u.action.u.ttlm_req))
3785 				goto invalid;
3786 			goto queue;
3787 		case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
3788 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3789 				break;
3790 
3791 			if (len < offsetofend(typeof(*mgmt),
3792 					      u.action.u.ttlm_res))
3793 				goto invalid;
3794 			goto queue;
3795 		case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
3796 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3797 				break;
3798 
3799 			if (len < offsetofend(typeof(*mgmt),
3800 					      u.action.u.ttlm_tear_down))
3801 				goto invalid;
3802 			goto queue;
3803 		case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
3804 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3805 				break;
3806 
3807 			/* The reconfiguration response action frame must
3808 			 * least one 'Status Duple' entry (3 octets)
3809 			 */
3810 			if (len <
3811 			    offsetofend(typeof(*mgmt),
3812 					u.action.u.ml_reconf_resp) + 3)
3813 				goto invalid;
3814 			goto queue;
3815 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
3816 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3817 				break;
3818 
3819 			if (len < offsetofend(typeof(*mgmt),
3820 					      u.action.u.epcs) +
3821 			    IEEE80211_EPCS_ENA_RESP_BODY_LEN)
3822 				goto invalid;
3823 			goto queue;
3824 		case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
3825 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3826 				break;
3827 
3828 			if (len < offsetofend(typeof(*mgmt),
3829 					      u.action.u.epcs))
3830 				goto invalid;
3831 			goto queue;
3832 		default:
3833 			break;
3834 		}
3835 		break;
3836 	}
3837 
3838 	return RX_CONTINUE;
3839 
3840  invalid:
3841 	status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3842 	/* will return in the next handlers */
3843 	return RX_CONTINUE;
3844 
3845  handled:
3846 	if (rx->sta)
3847 		rx->link_sta->rx_stats.packets++;
3848 	dev_kfree_skb(rx->skb);
3849 	return RX_QUEUED;
3850 
3851  queue:
3852 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
3853 	return RX_QUEUED;
3854 }
3855 
3856 static ieee80211_rx_result debug_noinline
3857 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3858 {
3859 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3860 	struct cfg80211_rx_info info = {
3861 		.freq = ieee80211_rx_status_to_khz(status),
3862 		.buf = rx->skb->data,
3863 		.len = rx->skb->len,
3864 		.link_id = rx->link_id,
3865 		.have_link_id = rx->link_id >= 0,
3866 	};
3867 
3868 	/* skip known-bad action frames and return them in the next handler */
3869 	if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3870 		return RX_CONTINUE;
3871 
3872 	/*
3873 	 * Getting here means the kernel doesn't know how to handle
3874 	 * it, but maybe userspace does ... include returned frames
3875 	 * so userspace can register for those to know whether ones
3876 	 * it transmitted were processed or returned.
3877 	 */
3878 
3879 	if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3880 	    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3881 		info.sig_dbm = status->signal;
3882 
3883 	if (ieee80211_is_timing_measurement(rx->skb) ||
3884 	    ieee80211_is_ftm(rx->skb)) {
3885 		info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp);
3886 		info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp);
3887 	}
3888 
3889 	if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) {
3890 		if (rx->sta)
3891 			rx->link_sta->rx_stats.packets++;
3892 		dev_kfree_skb(rx->skb);
3893 		return RX_QUEUED;
3894 	}
3895 
3896 	return RX_CONTINUE;
3897 }
3898 
3899 static ieee80211_rx_result debug_noinline
3900 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3901 {
3902 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3903 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3904 	int len = rx->skb->len;
3905 
3906 	if (!ieee80211_is_action(mgmt->frame_control))
3907 		return RX_CONTINUE;
3908 
3909 	switch (mgmt->u.action.category) {
3910 	case WLAN_CATEGORY_SA_QUERY:
3911 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3912 			   sizeof(mgmt->u.action.u.sa_query)))
3913 			break;
3914 
3915 		switch (mgmt->u.action.u.sa_query.action) {
3916 		case WLAN_ACTION_SA_QUERY_REQUEST:
3917 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3918 				break;
3919 			ieee80211_process_sa_query_req(sdata, mgmt, len);
3920 			goto handled;
3921 		}
3922 		break;
3923 	}
3924 
3925 	return RX_CONTINUE;
3926 
3927  handled:
3928 	if (rx->sta)
3929 		rx->link_sta->rx_stats.packets++;
3930 	dev_kfree_skb(rx->skb);
3931 	return RX_QUEUED;
3932 }
3933 
3934 static ieee80211_rx_result debug_noinline
3935 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3936 {
3937 	struct ieee80211_local *local = rx->local;
3938 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3939 	struct sk_buff *nskb;
3940 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3941 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3942 
3943 	if (!ieee80211_is_action(mgmt->frame_control))
3944 		return RX_CONTINUE;
3945 
3946 	/*
3947 	 * For AP mode, hostapd is responsible for handling any action
3948 	 * frames that we didn't handle, including returning unknown
3949 	 * ones. For all other modes we will return them to the sender,
3950 	 * setting the 0x80 bit in the action category, as required by
3951 	 * 802.11-2012 9.24.4.
3952 	 * Newer versions of hostapd use the management frame registration
3953 	 * mechanisms and old cooked monitor interface is no longer supported.
3954 	 */
3955 	if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3956 	    (sdata->vif.type == NL80211_IFTYPE_AP ||
3957 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3958 		return RX_DROP;
3959 
3960 	if (is_multicast_ether_addr(mgmt->da))
3961 		return RX_DROP;
3962 
3963 	/* do not return rejected action frames */
3964 	if (mgmt->u.action.category & 0x80)
3965 		return RX_DROP_U_REJECTED_ACTION_RESPONSE;
3966 
3967 	nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3968 			       GFP_ATOMIC);
3969 	if (nskb) {
3970 		struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3971 
3972 		nmgmt->u.action.category |= 0x80;
3973 		memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3974 		memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3975 
3976 		memset(nskb->cb, 0, sizeof(nskb->cb));
3977 
3978 		if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3979 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3980 
3981 			info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3982 				      IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3983 				      IEEE80211_TX_CTL_NO_CCK_RATE;
3984 			if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3985 				info->hw_queue =
3986 					local->hw.offchannel_tx_hw_queue;
3987 		}
3988 
3989 		__ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7, -1,
3990 					    status->band);
3991 	}
3992 
3993 	return RX_DROP_U_UNKNOWN_ACTION_REJECTED;
3994 }
3995 
3996 static ieee80211_rx_result debug_noinline
3997 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3998 {
3999 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4000 	struct ieee80211_hdr *hdr = (void *)rx->skb->data;
4001 
4002 	if (!ieee80211_is_ext(hdr->frame_control))
4003 		return RX_CONTINUE;
4004 
4005 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
4006 		return RX_DROP;
4007 
4008 	/* for now only beacons are ext, so queue them */
4009 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
4010 
4011 	return RX_QUEUED;
4012 }
4013 
4014 static ieee80211_rx_result debug_noinline
4015 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
4016 {
4017 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4018 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
4019 	__le16 stype;
4020 
4021 	stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
4022 
4023 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
4024 	    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4025 	    sdata->vif.type != NL80211_IFTYPE_OCB &&
4026 	    sdata->vif.type != NL80211_IFTYPE_STATION)
4027 		return RX_DROP;
4028 
4029 	switch (stype) {
4030 	case cpu_to_le16(IEEE80211_STYPE_AUTH):
4031 	case cpu_to_le16(IEEE80211_STYPE_BEACON):
4032 	case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
4033 		/* process for all: mesh, mlme, ibss */
4034 		break;
4035 	case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
4036 		if (is_multicast_ether_addr(mgmt->da) &&
4037 		    !is_broadcast_ether_addr(mgmt->da))
4038 			return RX_DROP;
4039 
4040 		/* process only for station/IBSS */
4041 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
4042 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
4043 			return RX_DROP;
4044 		break;
4045 	case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
4046 	case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
4047 	case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
4048 		if (is_multicast_ether_addr(mgmt->da) &&
4049 		    !is_broadcast_ether_addr(mgmt->da))
4050 			return RX_DROP;
4051 
4052 		/* process only for station */
4053 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
4054 			return RX_DROP;
4055 		break;
4056 	case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
4057 		/* process only for ibss and mesh */
4058 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4059 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
4060 			return RX_DROP;
4061 		break;
4062 	default:
4063 		return RX_DROP;
4064 	}
4065 
4066 	ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
4067 
4068 	return RX_QUEUED;
4069 }
4070 
4071 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
4072 					 ieee80211_rx_result res)
4073 {
4074 	if (res == RX_QUEUED) {
4075 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
4076 		return;
4077 	}
4078 
4079 	if (res != RX_CONTINUE) {
4080 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
4081 		if (rx->sta)
4082 			rx->link_sta->rx_stats.dropped++;
4083 	}
4084 
4085 	kfree_skb_reason(rx->skb, (__force u32)res);
4086 }
4087 
4088 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
4089 				  struct sk_buff_head *frames)
4090 {
4091 	ieee80211_rx_result res = RX_DROP;
4092 	struct sk_buff *skb;
4093 
4094 #define CALL_RXH(rxh)			\
4095 	do {				\
4096 		res = rxh(rx);		\
4097 		if (res != RX_CONTINUE)	\
4098 			goto rxh_next;  \
4099 	} while (0)
4100 
4101 	/* Lock here to avoid hitting all of the data used in the RX
4102 	 * path (e.g. key data, station data, ...) concurrently when
4103 	 * a frame is released from the reorder buffer due to timeout
4104 	 * from the timer, potentially concurrently with RX from the
4105 	 * driver.
4106 	 */
4107 	spin_lock_bh(&rx->local->rx_path_lock);
4108 
4109 	while ((skb = __skb_dequeue(frames))) {
4110 		/*
4111 		 * all the other fields are valid across frames
4112 		 * that belong to an aMPDU since they are on the
4113 		 * same TID from the same station
4114 		 */
4115 		rx->skb = skb;
4116 
4117 		if (WARN_ON_ONCE(!rx->link))
4118 			goto rxh_next;
4119 
4120 		CALL_RXH(ieee80211_rx_h_check_more_data);
4121 		CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
4122 		CALL_RXH(ieee80211_rx_h_sta_process);
4123 		CALL_RXH(ieee80211_rx_h_decrypt);
4124 		CALL_RXH(ieee80211_rx_h_defragment);
4125 		CALL_RXH(ieee80211_rx_h_michael_mic_verify);
4126 		/* must be after MMIC verify so header is counted in MPDU mic */
4127 		CALL_RXH(ieee80211_rx_h_amsdu);
4128 		CALL_RXH(ieee80211_rx_h_data);
4129 
4130 		/* special treatment -- needs the queue */
4131 		res = ieee80211_rx_h_ctrl(rx, frames);
4132 		if (res != RX_CONTINUE)
4133 			goto rxh_next;
4134 
4135 		CALL_RXH(ieee80211_rx_h_mgmt_check);
4136 		CALL_RXH(ieee80211_rx_h_action);
4137 		CALL_RXH(ieee80211_rx_h_userspace_mgmt);
4138 		CALL_RXH(ieee80211_rx_h_action_post_userspace);
4139 		CALL_RXH(ieee80211_rx_h_action_return);
4140 		CALL_RXH(ieee80211_rx_h_ext);
4141 		CALL_RXH(ieee80211_rx_h_mgmt);
4142 
4143  rxh_next:
4144 		ieee80211_rx_handlers_result(rx, res);
4145 
4146 #undef CALL_RXH
4147 	}
4148 
4149 	spin_unlock_bh(&rx->local->rx_path_lock);
4150 }
4151 
4152 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
4153 {
4154 	struct sk_buff_head reorder_release;
4155 	ieee80211_rx_result res = RX_DROP;
4156 
4157 	__skb_queue_head_init(&reorder_release);
4158 
4159 #define CALL_RXH(rxh)			\
4160 	do {				\
4161 		res = rxh(rx);		\
4162 		if (res != RX_CONTINUE)	\
4163 			goto rxh_next;  \
4164 	} while (0)
4165 
4166 	CALL_RXH(ieee80211_rx_h_check_dup);
4167 	CALL_RXH(ieee80211_rx_h_check);
4168 
4169 	ieee80211_rx_reorder_ampdu(rx, &reorder_release);
4170 
4171 	ieee80211_rx_handlers(rx, &reorder_release);
4172 	return;
4173 
4174  rxh_next:
4175 	ieee80211_rx_handlers_result(rx, res);
4176 
4177 #undef CALL_RXH
4178 }
4179 
4180 static bool
4181 ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
4182 {
4183 	return !!(sta->valid_links & BIT(link_id));
4184 }
4185 
4186 static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
4187 				       u8 link_id)
4188 {
4189 	rx->link_id = link_id;
4190 	rx->link = rcu_dereference(rx->sdata->link[link_id]);
4191 
4192 	if (!rx->sta)
4193 		return rx->link;
4194 
4195 	if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
4196 		return false;
4197 
4198 	rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
4199 
4200 	return rx->link && rx->link_sta;
4201 }
4202 
4203 static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
4204 				      struct sta_info *sta, int link_id)
4205 {
4206 	rx->link_id = link_id;
4207 	rx->sta = sta;
4208 
4209 	if (sta) {
4210 		rx->local = sta->sdata->local;
4211 		if (!rx->sdata)
4212 			rx->sdata = sta->sdata;
4213 		rx->link_sta = &sta->deflink;
4214 	} else {
4215 		rx->link_sta = NULL;
4216 	}
4217 
4218 	if (link_id < 0) {
4219 		if (ieee80211_vif_is_mld(&rx->sdata->vif) &&
4220 		    sta && !sta->sta.valid_links)
4221 			rx->link =
4222 				rcu_dereference(rx->sdata->link[sta->deflink.link_id]);
4223 		else
4224 			rx->link = &rx->sdata->deflink;
4225 	} else if (!ieee80211_rx_data_set_link(rx, link_id)) {
4226 		return false;
4227 	}
4228 
4229 	return true;
4230 }
4231 
4232 /*
4233  * This function makes calls into the RX path, therefore
4234  * it has to be invoked under RCU read lock.
4235  */
4236 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
4237 {
4238 	struct sk_buff_head frames;
4239 	struct ieee80211_rx_data rx = {
4240 		/* This is OK -- must be QoS data frame */
4241 		.security_idx = tid,
4242 		.seqno_idx = tid,
4243 	};
4244 	struct tid_ampdu_rx *tid_agg_rx;
4245 	int link_id = -1;
4246 
4247 	/* FIXME: statistics won't be right with this */
4248 	if (sta->sta.valid_links)
4249 		link_id = ffs(sta->sta.valid_links) - 1;
4250 
4251 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
4252 		return;
4253 
4254 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4255 	if (!tid_agg_rx)
4256 		return;
4257 
4258 	__skb_queue_head_init(&frames);
4259 
4260 	spin_lock(&tid_agg_rx->reorder_lock);
4261 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4262 	spin_unlock(&tid_agg_rx->reorder_lock);
4263 
4264 	if (!skb_queue_empty(&frames)) {
4265 		struct ieee80211_event event = {
4266 			.type = BA_FRAME_TIMEOUT,
4267 			.u.ba.tid = tid,
4268 			.u.ba.sta = &sta->sta,
4269 		};
4270 		drv_event_callback(rx.local, rx.sdata, &event);
4271 	}
4272 
4273 	ieee80211_rx_handlers(&rx, &frames);
4274 }
4275 
4276 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4277 					  u16 ssn, u64 filtered,
4278 					  u16 received_mpdus)
4279 {
4280 	struct ieee80211_local *local;
4281 	struct sta_info *sta;
4282 	struct tid_ampdu_rx *tid_agg_rx;
4283 	struct sk_buff_head frames;
4284 	struct ieee80211_rx_data rx = {
4285 		/* This is OK -- must be QoS data frame */
4286 		.security_idx = tid,
4287 		.seqno_idx = tid,
4288 	};
4289 	int i, diff;
4290 
4291 	if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4292 		return;
4293 
4294 	__skb_queue_head_init(&frames);
4295 
4296 	sta = container_of(pubsta, struct sta_info, sta);
4297 
4298 	local = sta->sdata->local;
4299 	WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64,
4300 		  "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n",
4301 		  local->hw.max_rx_aggregation_subframes);
4302 
4303 	if (!ieee80211_rx_data_set_sta(&rx, sta, -1))
4304 		return;
4305 
4306 	rcu_read_lock();
4307 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4308 	if (!tid_agg_rx)
4309 		goto out;
4310 
4311 	spin_lock_bh(&tid_agg_rx->reorder_lock);
4312 
4313 	if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4314 		int release;
4315 
4316 		/* release all frames in the reorder buffer */
4317 		release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4318 			   IEEE80211_SN_MODULO;
4319 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4320 						 release, &frames);
4321 		/* update ssn to match received ssn */
4322 		tid_agg_rx->head_seq_num = ssn;
4323 	} else {
4324 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4325 						 &frames);
4326 	}
4327 
4328 	/* handle the case that received ssn is behind the mac ssn.
4329 	 * it can be tid_agg_rx->buf_size behind and still be valid */
4330 	diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4331 	if (diff >= tid_agg_rx->buf_size) {
4332 		tid_agg_rx->reorder_buf_filtered = 0;
4333 		goto release;
4334 	}
4335 	filtered = filtered >> diff;
4336 	ssn += diff;
4337 
4338 	/* update bitmap */
4339 	for (i = 0; i < tid_agg_rx->buf_size; i++) {
4340 		int index = (ssn + i) % tid_agg_rx->buf_size;
4341 
4342 		tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4343 		if (filtered & BIT_ULL(i))
4344 			tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4345 	}
4346 
4347 	/* now process also frames that the filter marking released */
4348 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4349 
4350 release:
4351 	spin_unlock_bh(&tid_agg_rx->reorder_lock);
4352 
4353 	ieee80211_rx_handlers(&rx, &frames);
4354 
4355  out:
4356 	rcu_read_unlock();
4357 }
4358 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4359 
4360 /* main receive path */
4361 
4362 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
4363 {
4364 	return ether_addr_equal(raddr, addr) ||
4365 	       is_broadcast_ether_addr(raddr);
4366 }
4367 
4368 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4369 {
4370 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4371 	struct sk_buff *skb = rx->skb;
4372 	struct ieee80211_hdr *hdr = (void *)skb->data;
4373 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4374 	u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4375 	bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4376 			 ieee80211_is_s1g_beacon(hdr->frame_control);
4377 
4378 	switch (sdata->vif.type) {
4379 	case NL80211_IFTYPE_STATION:
4380 		if (!bssid && !sdata->u.mgd.use_4addr)
4381 			return false;
4382 		if (ieee80211_is_first_frag(hdr->seq_ctrl) &&
4383 		    ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4384 			return false;
4385 		if (multicast)
4386 			return true;
4387 		return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id);
4388 	case NL80211_IFTYPE_ADHOC:
4389 		if (!bssid)
4390 			return false;
4391 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4392 		    ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4393 		    !is_valid_ether_addr(hdr->addr2))
4394 			return false;
4395 		if (ieee80211_is_beacon(hdr->frame_control))
4396 			return true;
4397 		if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4398 			return false;
4399 		if (!multicast &&
4400 		    !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4401 			return false;
4402 		if (!rx->sta) {
4403 			int rate_idx;
4404 			if (status->encoding != RX_ENC_LEGACY)
4405 				rate_idx = 0; /* TODO: HT/VHT rates */
4406 			else
4407 				rate_idx = status->rate_idx;
4408 			ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4409 						 BIT(rate_idx));
4410 		}
4411 		return true;
4412 	case NL80211_IFTYPE_OCB:
4413 		if (!bssid)
4414 			return false;
4415 		if (!ieee80211_is_data_present(hdr->frame_control))
4416 			return false;
4417 		if (!is_broadcast_ether_addr(bssid))
4418 			return false;
4419 		if (!multicast &&
4420 		    !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4421 			return false;
4422 		/* reject invalid/our STA address */
4423 		if (!is_valid_ether_addr(hdr->addr2) ||
4424 		    ether_addr_equal(sdata->dev->dev_addr, hdr->addr2))
4425 			return false;
4426 		if (!rx->sta) {
4427 			int rate_idx;
4428 			if (status->encoding != RX_ENC_LEGACY)
4429 				rate_idx = 0; /* TODO: HT rates */
4430 			else
4431 				rate_idx = status->rate_idx;
4432 			ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4433 						BIT(rate_idx));
4434 		}
4435 		return true;
4436 	case NL80211_IFTYPE_MESH_POINT:
4437 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4438 			return false;
4439 		if (multicast)
4440 			return true;
4441 		return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4442 	case NL80211_IFTYPE_AP_VLAN:
4443 	case NL80211_IFTYPE_AP:
4444 		if (!bssid)
4445 			return ieee80211_is_our_addr(sdata, hdr->addr1,
4446 						     &rx->link_id);
4447 
4448 		if (!is_broadcast_ether_addr(bssid) &&
4449 		    !ieee80211_is_our_addr(sdata, bssid, NULL)) {
4450 			/*
4451 			 * Accept public action frames even when the
4452 			 * BSSID doesn't match, this is used for P2P
4453 			 * and location updates. Note that mac80211
4454 			 * itself never looks at these frames.
4455 			 */
4456 			if (!multicast &&
4457 			    !ieee80211_is_our_addr(sdata, hdr->addr1,
4458 						   &rx->link_id))
4459 				return false;
4460 			if (ieee80211_is_public_action(hdr, skb->len))
4461 				return true;
4462 			return ieee80211_is_beacon(hdr->frame_control);
4463 		}
4464 
4465 		if (!ieee80211_has_tods(hdr->frame_control)) {
4466 			/* ignore data frames to TDLS-peers */
4467 			if (ieee80211_is_data(hdr->frame_control))
4468 				return false;
4469 			/* ignore action frames to TDLS-peers */
4470 			if (ieee80211_is_action(hdr->frame_control) &&
4471 			    !is_broadcast_ether_addr(bssid) &&
4472 			    !ether_addr_equal(bssid, hdr->addr1))
4473 				return false;
4474 		}
4475 
4476 		/*
4477 		 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4478 		 * the BSSID - we've checked that already but may have accepted
4479 		 * the wildcard (ff:ff:ff:ff:ff:ff).
4480 		 *
4481 		 * It also says:
4482 		 *	The BSSID of the Data frame is determined as follows:
4483 		 *	a) If the STA is contained within an AP or is associated
4484 		 *	   with an AP, the BSSID is the address currently in use
4485 		 *	   by the STA contained in the AP.
4486 		 *
4487 		 * So we should not accept data frames with an address that's
4488 		 * multicast.
4489 		 *
4490 		 * Accepting it also opens a security problem because stations
4491 		 * could encrypt it with the GTK and inject traffic that way.
4492 		 */
4493 		if (ieee80211_is_data(hdr->frame_control) && multicast)
4494 			return false;
4495 
4496 		return true;
4497 	case NL80211_IFTYPE_P2P_DEVICE:
4498 		return ieee80211_is_public_action(hdr, skb->len) ||
4499 		       ieee80211_is_probe_req(hdr->frame_control) ||
4500 		       ieee80211_is_probe_resp(hdr->frame_control) ||
4501 		       ieee80211_is_beacon(hdr->frame_control) ||
4502 		       (ieee80211_is_auth(hdr->frame_control) &&
4503 			ether_addr_equal(sdata->vif.addr, hdr->addr1));
4504 	case NL80211_IFTYPE_NAN:
4505 		/* Currently no frames on NAN interface are allowed */
4506 		return false;
4507 	default:
4508 		break;
4509 	}
4510 
4511 	WARN_ON_ONCE(1);
4512 	return false;
4513 }
4514 
4515 void ieee80211_check_fast_rx(struct sta_info *sta)
4516 {
4517 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4518 	struct ieee80211_local *local = sdata->local;
4519 	struct ieee80211_key *key;
4520 	struct ieee80211_fast_rx fastrx = {
4521 		.dev = sdata->dev,
4522 		.vif_type = sdata->vif.type,
4523 		.control_port_protocol = sdata->control_port_protocol,
4524 	}, *old, *new = NULL;
4525 	u32 offload_flags;
4526 	bool set_offload = false;
4527 	bool assign = false;
4528 	bool offload;
4529 
4530 	/* use sparse to check that we don't return without updating */
4531 	__acquire(check_fast_rx);
4532 
4533 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4534 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4535 	ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4536 	ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4537 
4538 	fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4539 
4540 	/* fast-rx doesn't do reordering */
4541 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4542 	    !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4543 		goto clear;
4544 
4545 	switch (sdata->vif.type) {
4546 	case NL80211_IFTYPE_STATION:
4547 		if (sta->sta.tdls) {
4548 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4549 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4550 			fastrx.expected_ds_bits = 0;
4551 		} else {
4552 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4553 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4554 			fastrx.expected_ds_bits =
4555 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4556 		}
4557 
4558 		if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4559 			fastrx.expected_ds_bits |=
4560 				cpu_to_le16(IEEE80211_FCTL_TODS);
4561 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4562 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4563 		}
4564 
4565 		if (!sdata->u.mgd.powersave)
4566 			break;
4567 
4568 		/* software powersave is a huge mess, avoid all of it */
4569 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4570 			goto clear;
4571 		if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4572 		    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4573 			goto clear;
4574 		break;
4575 	case NL80211_IFTYPE_AP_VLAN:
4576 	case NL80211_IFTYPE_AP:
4577 		/* parallel-rx requires this, at least with calls to
4578 		 * ieee80211_sta_ps_transition()
4579 		 */
4580 		if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4581 			goto clear;
4582 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4583 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4584 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4585 
4586 		fastrx.internal_forward =
4587 			!(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4588 			(sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4589 			 !sdata->u.vlan.sta);
4590 
4591 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4592 		    sdata->u.vlan.sta) {
4593 			fastrx.expected_ds_bits |=
4594 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
4595 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4596 			fastrx.internal_forward = 0;
4597 		}
4598 
4599 		break;
4600 	case NL80211_IFTYPE_MESH_POINT:
4601 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS |
4602 						      IEEE80211_FCTL_TODS);
4603 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4604 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4605 		break;
4606 	default:
4607 		goto clear;
4608 	}
4609 
4610 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4611 		goto clear;
4612 
4613 	rcu_read_lock();
4614 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4615 	if (!key)
4616 		key = rcu_dereference(sdata->default_unicast_key);
4617 	if (key) {
4618 		switch (key->conf.cipher) {
4619 		case WLAN_CIPHER_SUITE_TKIP:
4620 			/* we don't want to deal with MMIC in fast-rx */
4621 			goto clear_rcu;
4622 		case WLAN_CIPHER_SUITE_CCMP:
4623 		case WLAN_CIPHER_SUITE_CCMP_256:
4624 		case WLAN_CIPHER_SUITE_GCMP:
4625 		case WLAN_CIPHER_SUITE_GCMP_256:
4626 			break;
4627 		default:
4628 			/* We also don't want to deal with
4629 			 * WEP or cipher scheme.
4630 			 */
4631 			goto clear_rcu;
4632 		}
4633 
4634 		fastrx.key = true;
4635 		fastrx.icv_len = key->conf.icv_len;
4636 	}
4637 
4638 	assign = true;
4639  clear_rcu:
4640 	rcu_read_unlock();
4641  clear:
4642 	__release(check_fast_rx);
4643 
4644 	if (assign)
4645 		new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4646 
4647 	offload_flags = get_bss_sdata(sdata)->vif.offload_flags;
4648 	offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED;
4649 
4650 	if (assign && offload)
4651 		set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4652 	else
4653 		set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4654 
4655 	if (set_offload)
4656 		drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4657 
4658 	spin_lock_bh(&sta->lock);
4659 	old = rcu_dereference_protected(sta->fast_rx, true);
4660 	rcu_assign_pointer(sta->fast_rx, new);
4661 	spin_unlock_bh(&sta->lock);
4662 
4663 	if (old)
4664 		kfree_rcu(old, rcu_head);
4665 }
4666 
4667 void ieee80211_clear_fast_rx(struct sta_info *sta)
4668 {
4669 	struct ieee80211_fast_rx *old;
4670 
4671 	spin_lock_bh(&sta->lock);
4672 	old = rcu_dereference_protected(sta->fast_rx, true);
4673 	RCU_INIT_POINTER(sta->fast_rx, NULL);
4674 	spin_unlock_bh(&sta->lock);
4675 
4676 	if (old)
4677 		kfree_rcu(old, rcu_head);
4678 }
4679 
4680 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4681 {
4682 	struct ieee80211_local *local = sdata->local;
4683 	struct sta_info *sta;
4684 
4685 	lockdep_assert_wiphy(local->hw.wiphy);
4686 
4687 	list_for_each_entry(sta, &local->sta_list, list) {
4688 		if (sdata != sta->sdata &&
4689 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4690 			continue;
4691 		ieee80211_check_fast_rx(sta);
4692 	}
4693 }
4694 
4695 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4696 {
4697 	struct ieee80211_local *local = sdata->local;
4698 
4699 	lockdep_assert_wiphy(local->hw.wiphy);
4700 
4701 	__ieee80211_check_fast_rx_iface(sdata);
4702 }
4703 
4704 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4705 			      struct ieee80211_fast_rx *fast_rx,
4706 			      int orig_len)
4707 {
4708 	struct ieee80211_sta_rx_stats *stats;
4709 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4710 	struct sta_info *sta = rx->sta;
4711 	struct link_sta_info *link_sta;
4712 	struct sk_buff *skb = rx->skb;
4713 	void *sa = skb->data + ETH_ALEN;
4714 	void *da = skb->data;
4715 
4716 	if (rx->link_id >= 0) {
4717 		link_sta = rcu_dereference(sta->link[rx->link_id]);
4718 		if (WARN_ON_ONCE(!link_sta)) {
4719 			dev_kfree_skb(rx->skb);
4720 			return;
4721 		}
4722 	} else {
4723 		link_sta = &sta->deflink;
4724 	}
4725 
4726 	stats = &link_sta->rx_stats;
4727 	if (fast_rx->uses_rss)
4728 		stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
4729 
4730 	/* statistics part of ieee80211_rx_h_sta_process() */
4731 	if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4732 		stats->last_signal = status->signal;
4733 		if (!fast_rx->uses_rss)
4734 			ewma_signal_add(&link_sta->rx_stats_avg.signal,
4735 					-status->signal);
4736 	}
4737 
4738 	if (status->chains) {
4739 		int i;
4740 
4741 		stats->chains = status->chains;
4742 		for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4743 			int signal = status->chain_signal[i];
4744 
4745 			if (!(status->chains & BIT(i)))
4746 				continue;
4747 
4748 			stats->chain_signal_last[i] = signal;
4749 			if (!fast_rx->uses_rss)
4750 				ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
4751 						-signal);
4752 		}
4753 	}
4754 	/* end of statistics */
4755 
4756 	stats->last_rx = jiffies;
4757 	stats->last_rate = sta_stats_encode_rate(status);
4758 
4759 	stats->fragments++;
4760 	stats->packets++;
4761 
4762 	skb->dev = fast_rx->dev;
4763 
4764 	dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4765 
4766 	/* The seqno index has the same property as needed
4767 	 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4768 	 * for non-QoS-data frames. Here we know it's a data
4769 	 * frame, so count MSDUs.
4770 	 */
4771 	u64_stats_update_begin(&stats->syncp);
4772 	stats->msdu[rx->seqno_idx]++;
4773 	stats->bytes += orig_len;
4774 	u64_stats_update_end(&stats->syncp);
4775 
4776 	if (fast_rx->internal_forward) {
4777 		struct sk_buff *xmit_skb = NULL;
4778 		if (is_multicast_ether_addr(da)) {
4779 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
4780 		} else if (!ether_addr_equal(da, sa) &&
4781 			   sta_info_get(rx->sdata, da)) {
4782 			xmit_skb = skb;
4783 			skb = NULL;
4784 		}
4785 
4786 		if (xmit_skb) {
4787 			/*
4788 			 * Send to wireless media and increase priority by 256
4789 			 * to keep the received priority instead of
4790 			 * reclassifying the frame (see cfg80211_classify8021d).
4791 			 */
4792 			xmit_skb->priority += 256;
4793 			xmit_skb->protocol = htons(ETH_P_802_3);
4794 			skb_reset_network_header(xmit_skb);
4795 			skb_reset_mac_header(xmit_skb);
4796 			dev_queue_xmit(xmit_skb);
4797 		}
4798 
4799 		if (!skb)
4800 			return;
4801 	}
4802 
4803 	/* deliver to local stack */
4804 	skb->protocol = eth_type_trans(skb, fast_rx->dev);
4805 	ieee80211_deliver_skb_to_local_stack(skb, rx);
4806 }
4807 
4808 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4809 				     struct ieee80211_fast_rx *fast_rx)
4810 {
4811 	struct sk_buff *skb = rx->skb;
4812 	struct ieee80211_hdr *hdr = (void *)skb->data;
4813 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4814 	static ieee80211_rx_result res;
4815 	int orig_len = skb->len;
4816 	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4817 	int snap_offs = hdrlen;
4818 	struct {
4819 		u8 snap[sizeof(rfc1042_header)];
4820 		__be16 proto;
4821 	} *payload __aligned(2);
4822 	struct {
4823 		u8 da[ETH_ALEN];
4824 		u8 sa[ETH_ALEN];
4825 	} addrs __aligned(2);
4826 	struct ieee80211_sta_rx_stats *stats;
4827 
4828 	/* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4829 	 * to a common data structure; drivers can implement that per queue
4830 	 * but we don't have that information in mac80211
4831 	 */
4832 	if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4833 		return false;
4834 
4835 #define FAST_RX_CRYPT_FLAGS	(RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4836 
4837 	/* If using encryption, we also need to have:
4838 	 *  - PN_VALIDATED: similar, but the implementation is tricky
4839 	 *  - DECRYPTED: necessary for PN_VALIDATED
4840 	 */
4841 	if (fast_rx->key &&
4842 	    (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4843 		return false;
4844 
4845 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4846 		return false;
4847 
4848 	if (unlikely(ieee80211_is_frag(hdr)))
4849 		return false;
4850 
4851 	/* Since our interface address cannot be multicast, this
4852 	 * implicitly also rejects multicast frames without the
4853 	 * explicit check.
4854 	 *
4855 	 * We shouldn't get any *data* frames not addressed to us
4856 	 * (AP mode will accept multicast *management* frames), but
4857 	 * punting here will make it go through the full checks in
4858 	 * ieee80211_accept_frame().
4859 	 */
4860 	if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4861 		return false;
4862 
4863 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4864 					      IEEE80211_FCTL_TODS)) !=
4865 	    fast_rx->expected_ds_bits)
4866 		return false;
4867 
4868 	/* assign the key to drop unencrypted frames (later)
4869 	 * and strip the IV/MIC if necessary
4870 	 */
4871 	if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4872 		/* GCMP header length is the same */
4873 		snap_offs += IEEE80211_CCMP_HDR_LEN;
4874 	}
4875 
4876 	if (!ieee80211_vif_is_mesh(&rx->sdata->vif) &&
4877 	    !(status->rx_flags & IEEE80211_RX_AMSDU)) {
4878 		if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4879 			return false;
4880 
4881 		payload = (void *)(skb->data + snap_offs);
4882 
4883 		if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4884 			return false;
4885 
4886 		/* Don't handle these here since they require special code.
4887 		 * Accept AARP and IPX even though they should come with a
4888 		 * bridge-tunnel header - but if we get them this way then
4889 		 * there's little point in discarding them.
4890 		 */
4891 		if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4892 			     payload->proto == fast_rx->control_port_protocol))
4893 			return false;
4894 	}
4895 
4896 	/* after this point, don't punt to the slowpath! */
4897 
4898 	if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4899 	    pskb_trim(skb, skb->len - fast_rx->icv_len))
4900 		goto drop;
4901 
4902 	if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4903 		goto drop;
4904 
4905 	if (status->rx_flags & IEEE80211_RX_AMSDU) {
4906 		if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4907 		    RX_QUEUED)
4908 			goto drop;
4909 
4910 		return true;
4911 	}
4912 
4913 	/* do the header conversion - first grab the addresses */
4914 	ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4915 	ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4916 	if (ieee80211_vif_is_mesh(&rx->sdata->vif)) {
4917 	    skb_pull(skb, snap_offs - 2);
4918 	    put_unaligned_be16(skb->len - 2, skb->data);
4919 	} else {
4920 	    skb_postpull_rcsum(skb, skb->data + snap_offs,
4921 			       sizeof(rfc1042_header) + 2);
4922 
4923 	    /* remove the SNAP but leave the ethertype */
4924 	    skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4925 	}
4926 	/* push the addresses in front */
4927 	memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4928 
4929 	res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
4930 	switch (res) {
4931 	case RX_QUEUED:
4932 		return true;
4933 	case RX_CONTINUE:
4934 		break;
4935 	default:
4936 		goto drop;
4937 	}
4938 
4939 	ieee80211_rx_8023(rx, fast_rx, orig_len);
4940 
4941 	return true;
4942  drop:
4943 	dev_kfree_skb(skb);
4944 
4945 	if (fast_rx->uses_rss)
4946 		stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
4947 	else
4948 		stats = &rx->link_sta->rx_stats;
4949 
4950 	stats->dropped++;
4951 	return true;
4952 }
4953 
4954 /*
4955  * This function returns whether or not the SKB
4956  * was destined for RX processing or not, which,
4957  * if consume is true, is equivalent to whether
4958  * or not the skb was consumed.
4959  */
4960 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4961 					    struct sk_buff *skb, bool consume)
4962 {
4963 	struct ieee80211_local *local = rx->local;
4964 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4965 	struct ieee80211_hdr *hdr = (void *)skb->data;
4966 	struct link_sta_info *link_sta = rx->link_sta;
4967 	struct ieee80211_link_data *link = rx->link;
4968 
4969 	rx->skb = skb;
4970 
4971 	/* See if we can do fast-rx; if we have to copy we already lost,
4972 	 * so punt in that case. We should never have to deliver a data
4973 	 * frame to multiple interfaces anyway.
4974 	 *
4975 	 * We skip the ieee80211_accept_frame() call and do the necessary
4976 	 * checking inside ieee80211_invoke_fast_rx().
4977 	 */
4978 	if (consume && rx->sta) {
4979 		struct ieee80211_fast_rx *fast_rx;
4980 
4981 		fast_rx = rcu_dereference(rx->sta->fast_rx);
4982 		if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4983 			return true;
4984 	}
4985 
4986 	if (!ieee80211_accept_frame(rx))
4987 		return false;
4988 
4989 	if (!consume) {
4990 		struct skb_shared_hwtstamps *shwt;
4991 
4992 		rx->skb = skb_copy(skb, GFP_ATOMIC);
4993 		if (!rx->skb) {
4994 			if (net_ratelimit())
4995 				wiphy_debug(local->hw.wiphy,
4996 					"failed to copy skb for %s\n",
4997 					sdata->name);
4998 			return true;
4999 		}
5000 
5001 		/* skb_copy() does not copy the hw timestamps, so copy it
5002 		 * explicitly
5003 		 */
5004 		shwt = skb_hwtstamps(rx->skb);
5005 		shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
5006 
5007 		/* Update the hdr pointer to the new skb for translation below */
5008 		hdr = (struct ieee80211_hdr *)rx->skb->data;
5009 	}
5010 
5011 	if (unlikely(rx->sta && rx->sta->sta.mlo) &&
5012 	    is_unicast_ether_addr(hdr->addr1) &&
5013 	    !ieee80211_is_probe_resp(hdr->frame_control) &&
5014 	    !ieee80211_is_beacon(hdr->frame_control)) {
5015 		/* translate to MLD addresses */
5016 		if (ether_addr_equal(link->conf->addr, hdr->addr1))
5017 			ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
5018 		if (ether_addr_equal(link_sta->addr, hdr->addr2))
5019 			ether_addr_copy(hdr->addr2, rx->sta->addr);
5020 		/* translate A3 only if it's the BSSID */
5021 		if (!ieee80211_has_tods(hdr->frame_control) &&
5022 		    !ieee80211_has_fromds(hdr->frame_control)) {
5023 			if (ether_addr_equal(link_sta->addr, hdr->addr3))
5024 				ether_addr_copy(hdr->addr3, rx->sta->addr);
5025 			else if (ether_addr_equal(link->conf->addr, hdr->addr3))
5026 				ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
5027 		}
5028 		/* not needed for A4 since it can only carry the SA */
5029 	}
5030 
5031 	ieee80211_invoke_rx_handlers(rx);
5032 	return true;
5033 }
5034 
5035 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
5036 				       struct ieee80211_sta *pubsta,
5037 				       struct sk_buff *skb,
5038 				       struct list_head *list)
5039 {
5040 	struct ieee80211_local *local = hw_to_local(hw);
5041 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5042 	struct ieee80211_fast_rx *fast_rx;
5043 	struct ieee80211_rx_data rx;
5044 	struct sta_info *sta;
5045 	int link_id = -1;
5046 
5047 	memset(&rx, 0, sizeof(rx));
5048 	rx.skb = skb;
5049 	rx.local = local;
5050 	rx.list = list;
5051 	rx.link_id = -1;
5052 
5053 	I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5054 
5055 	/* drop frame if too short for header */
5056 	if (skb->len < sizeof(struct ethhdr))
5057 		goto drop;
5058 
5059 	if (!pubsta)
5060 		goto drop;
5061 
5062 	if (status->link_valid)
5063 		link_id = status->link_id;
5064 
5065 	/*
5066 	 * TODO: Should the frame be dropped if the right link_id is not
5067 	 * available? Or may be it is fine in the current form to proceed with
5068 	 * the frame processing because with frame being in 802.3 format,
5069 	 * link_id is used only for stats purpose and updating the stats on
5070 	 * the deflink is fine?
5071 	 */
5072 	sta = container_of(pubsta, struct sta_info, sta);
5073 	if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5074 		goto drop;
5075 
5076 	fast_rx = rcu_dereference(rx.sta->fast_rx);
5077 	if (!fast_rx)
5078 		goto drop;
5079 
5080 	ieee80211_rx_8023(&rx, fast_rx, skb->len);
5081 	return;
5082 
5083 drop:
5084 	dev_kfree_skb(skb);
5085 }
5086 
5087 static bool ieee80211_rx_for_interface(struct ieee80211_rx_data *rx,
5088 				       struct sk_buff *skb, bool consume)
5089 {
5090 	struct link_sta_info *link_sta;
5091 	struct ieee80211_hdr *hdr = (void *)skb->data;
5092 	struct sta_info *sta;
5093 	int link_id = -1;
5094 
5095 	/*
5096 	 * Look up link station first, in case there's a
5097 	 * chance that they might have a link address that
5098 	 * is identical to the MLD address, that way we'll
5099 	 * have the link information if needed.
5100 	 */
5101 	link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
5102 	if (link_sta) {
5103 		sta = link_sta->sta;
5104 		link_id = link_sta->link_id;
5105 	} else {
5106 		struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5107 
5108 		sta = sta_info_get_bss(rx->sdata, hdr->addr2);
5109 		if (status->link_valid)
5110 			link_id = status->link_id;
5111 	}
5112 
5113 	if (!ieee80211_rx_data_set_sta(rx, sta, link_id))
5114 		return false;
5115 
5116 	return ieee80211_prepare_and_rx_handle(rx, skb, consume);
5117 }
5118 
5119 /*
5120  * This is the actual Rx frames handler. as it belongs to Rx path it must
5121  * be called with rcu_read_lock protection.
5122  */
5123 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
5124 					 struct ieee80211_sta *pubsta,
5125 					 struct sk_buff *skb,
5126 					 struct list_head *list)
5127 {
5128 	struct ieee80211_local *local = hw_to_local(hw);
5129 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5130 	struct ieee80211_sub_if_data *sdata;
5131 	struct ieee80211_hdr *hdr;
5132 	__le16 fc;
5133 	struct ieee80211_rx_data rx;
5134 	struct ieee80211_sub_if_data *prev;
5135 	struct rhlist_head *tmp;
5136 	int err = 0;
5137 
5138 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
5139 	memset(&rx, 0, sizeof(rx));
5140 	rx.skb = skb;
5141 	rx.local = local;
5142 	rx.list = list;
5143 	rx.link_id = -1;
5144 
5145 	if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
5146 		I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5147 
5148 	if (ieee80211_is_mgmt(fc)) {
5149 		/* drop frame if too short for header */
5150 		if (skb->len < ieee80211_hdrlen(fc))
5151 			err = -ENOBUFS;
5152 		else
5153 			err = skb_linearize(skb);
5154 	} else {
5155 		err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
5156 	}
5157 
5158 	if (err) {
5159 		dev_kfree_skb(skb);
5160 		return;
5161 	}
5162 
5163 	hdr = (struct ieee80211_hdr *)skb->data;
5164 	ieee80211_parse_qos(&rx);
5165 	ieee80211_verify_alignment(&rx);
5166 
5167 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
5168 		     ieee80211_is_beacon(hdr->frame_control) ||
5169 		     ieee80211_is_s1g_beacon(hdr->frame_control)))
5170 		ieee80211_scan_rx(local, skb);
5171 
5172 	if (ieee80211_is_data(fc)) {
5173 		struct sta_info *sta, *prev_sta;
5174 		int link_id = -1;
5175 
5176 		if (status->link_valid)
5177 			link_id = status->link_id;
5178 
5179 		if (pubsta) {
5180 			sta = container_of(pubsta, struct sta_info, sta);
5181 			if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5182 				goto out;
5183 
5184 			/*
5185 			 * In MLO connection, fetch the link_id using addr2
5186 			 * when the driver does not pass link_id in status.
5187 			 * When the address translation is already performed by
5188 			 * driver/hw, the valid link_id must be passed in
5189 			 * status.
5190 			 */
5191 
5192 			if (!status->link_valid && pubsta->mlo) {
5193 				struct link_sta_info *link_sta;
5194 
5195 				link_sta = link_sta_info_get_bss(rx.sdata,
5196 								 hdr->addr2);
5197 				if (!link_sta)
5198 					goto out;
5199 
5200 				ieee80211_rx_data_set_link(&rx, link_sta->link_id);
5201 			}
5202 
5203 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5204 				return;
5205 			goto out;
5206 		}
5207 
5208 		prev_sta = NULL;
5209 
5210 		for_each_sta_info(local, hdr->addr2, sta, tmp) {
5211 			if (!prev_sta) {
5212 				prev_sta = sta;
5213 				continue;
5214 			}
5215 
5216 			rx.sdata = prev_sta->sdata;
5217 			if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5218 				goto out;
5219 
5220 			if (!status->link_valid && prev_sta->sta.mlo)
5221 				continue;
5222 
5223 			ieee80211_prepare_and_rx_handle(&rx, skb, false);
5224 
5225 			prev_sta = sta;
5226 		}
5227 
5228 		if (prev_sta) {
5229 			rx.sdata = prev_sta->sdata;
5230 			if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5231 				goto out;
5232 
5233 			if (!status->link_valid && prev_sta->sta.mlo)
5234 				goto out;
5235 
5236 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5237 				return;
5238 			goto out;
5239 		}
5240 	}
5241 
5242 	prev = NULL;
5243 
5244 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
5245 		if (!ieee80211_sdata_running(sdata))
5246 			continue;
5247 
5248 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
5249 		    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5250 			continue;
5251 
5252 		/*
5253 		 * frame is destined for this interface, but if it's
5254 		 * not also for the previous one we handle that after
5255 		 * the loop to avoid copying the SKB once too much
5256 		 */
5257 
5258 		if (!prev) {
5259 			prev = sdata;
5260 			continue;
5261 		}
5262 
5263 		rx.sdata = prev;
5264 		ieee80211_rx_for_interface(&rx, skb, false);
5265 
5266 		prev = sdata;
5267 	}
5268 
5269 	if (prev) {
5270 		rx.sdata = prev;
5271 
5272 		if (ieee80211_rx_for_interface(&rx, skb, true))
5273 			return;
5274 	}
5275 
5276  out:
5277 	dev_kfree_skb(skb);
5278 }
5279 
5280 /*
5281  * This is the receive path handler. It is called by a low level driver when an
5282  * 802.11 MPDU is received from the hardware.
5283  */
5284 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5285 		       struct sk_buff *skb, struct list_head *list)
5286 {
5287 	struct ieee80211_local *local = hw_to_local(hw);
5288 	struct ieee80211_rate *rate = NULL;
5289 	struct ieee80211_supported_band *sband;
5290 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5291 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5292 
5293 	WARN_ON_ONCE(softirq_count() == 0);
5294 
5295 	if (WARN_ON(status->band >= NUM_NL80211_BANDS))
5296 		goto drop;
5297 
5298 	sband = local->hw.wiphy->bands[status->band];
5299 	if (WARN_ON(!sband))
5300 		goto drop;
5301 
5302 	/*
5303 	 * If we're suspending, it is possible although not too likely
5304 	 * that we'd be receiving frames after having already partially
5305 	 * quiesced the stack. We can't process such frames then since
5306 	 * that might, for example, cause stations to be added or other
5307 	 * driver callbacks be invoked.
5308 	 */
5309 	if (unlikely(local->quiescing || local->suspended))
5310 		goto drop;
5311 
5312 	/* We might be during a HW reconfig, prevent Rx for the same reason */
5313 	if (unlikely(local->in_reconfig))
5314 		goto drop;
5315 
5316 	/*
5317 	 * The same happens when we're not even started,
5318 	 * but that's worth a warning.
5319 	 */
5320 	if (WARN_ON(!local->started))
5321 		goto drop;
5322 
5323 	if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
5324 		/*
5325 		 * Validate the rate, unless a PLCP error means that
5326 		 * we probably can't have a valid rate here anyway.
5327 		 */
5328 
5329 		switch (status->encoding) {
5330 		case RX_ENC_HT:
5331 			/*
5332 			 * rate_idx is MCS index, which can be [0-76]
5333 			 * as documented on:
5334 			 *
5335 			 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
5336 			 *
5337 			 * Anything else would be some sort of driver or
5338 			 * hardware error. The driver should catch hardware
5339 			 * errors.
5340 			 */
5341 			if (WARN(status->rate_idx > 76,
5342 				 "Rate marked as an HT rate but passed "
5343 				 "status->rate_idx is not "
5344 				 "an MCS index [0-76]: %d (0x%02x)\n",
5345 				 status->rate_idx,
5346 				 status->rate_idx))
5347 				goto drop;
5348 			break;
5349 		case RX_ENC_VHT:
5350 			if (WARN_ONCE(status->rate_idx > 11 ||
5351 				      !status->nss ||
5352 				      status->nss > 8,
5353 				      "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
5354 				      status->rate_idx, status->nss))
5355 				goto drop;
5356 			break;
5357 		case RX_ENC_HE:
5358 			if (WARN_ONCE(status->rate_idx > 11 ||
5359 				      !status->nss ||
5360 				      status->nss > 8,
5361 				      "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
5362 				      status->rate_idx, status->nss))
5363 				goto drop;
5364 			break;
5365 		case RX_ENC_EHT:
5366 			if (WARN_ONCE(status->rate_idx > 15 ||
5367 				      !status->nss ||
5368 				      status->nss > 8 ||
5369 				      status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5370 				      "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5371 				      status->rate_idx, status->nss, status->eht.gi))
5372 				goto drop;
5373 			break;
5374 		default:
5375 			WARN_ON_ONCE(1);
5376 			fallthrough;
5377 		case RX_ENC_LEGACY:
5378 			if (WARN_ON(status->rate_idx >= sband->n_bitrates))
5379 				goto drop;
5380 			rate = &sband->bitrates[status->rate_idx];
5381 		}
5382 	}
5383 
5384 	if (WARN_ON_ONCE(status->link_id >= IEEE80211_LINK_UNSPECIFIED))
5385 		goto drop;
5386 
5387 	status->rx_flags = 0;
5388 
5389 	kcov_remote_start_common(skb_get_kcov_handle(skb));
5390 
5391 	/*
5392 	 * Frames with failed FCS/PLCP checksum are not returned,
5393 	 * all other frames are returned without radiotap header
5394 	 * if it was previously present.
5395 	 * Also, frames with less than 16 bytes are dropped.
5396 	 */
5397 	if (!(status->flag & RX_FLAG_8023))
5398 		skb = ieee80211_rx_monitor(local, skb, rate);
5399 	if (skb) {
5400 		if ((status->flag & RX_FLAG_8023) ||
5401 			ieee80211_is_data_present(hdr->frame_control))
5402 			ieee80211_tpt_led_trig_rx(local, skb->len);
5403 
5404 		if (status->flag & RX_FLAG_8023)
5405 			__ieee80211_rx_handle_8023(hw, pubsta, skb, list);
5406 		else
5407 			__ieee80211_rx_handle_packet(hw, pubsta, skb, list);
5408 	}
5409 
5410 	kcov_remote_stop();
5411 	return;
5412  drop:
5413 	kfree_skb(skb);
5414 }
5415 EXPORT_SYMBOL(ieee80211_rx_list);
5416 
5417 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5418 		       struct sk_buff *skb, struct napi_struct *napi)
5419 {
5420 	struct sk_buff *tmp;
5421 	LIST_HEAD(list);
5422 
5423 
5424 	/*
5425 	 * key references and virtual interfaces are protected using RCU
5426 	 * and this requires that we are in a read-side RCU section during
5427 	 * receive processing
5428 	 */
5429 	rcu_read_lock();
5430 	ieee80211_rx_list(hw, pubsta, skb, &list);
5431 	rcu_read_unlock();
5432 
5433 	if (!napi) {
5434 		netif_receive_skb_list(&list);
5435 		return;
5436 	}
5437 
5438 	list_for_each_entry_safe(skb, tmp, &list, list) {
5439 		skb_list_del_init(skb);
5440 		napi_gro_receive(napi, skb);
5441 	}
5442 }
5443 EXPORT_SYMBOL(ieee80211_rx_napi);
5444 
5445 /* This is a version of the rx handler that can be called from hard irq
5446  * context. Post the skb on the queue and schedule the tasklet */
5447 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
5448 {
5449 	struct ieee80211_local *local = hw_to_local(hw);
5450 
5451 	BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5452 
5453 	skb->pkt_type = IEEE80211_RX_MSG;
5454 	skb_queue_tail(&local->skb_queue, skb);
5455 	tasklet_schedule(&local->tasklet);
5456 }
5457 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
5458