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