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