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