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