xref: /openbmc/linux/net/mac80211/rx.c (revision 3c6ee770)
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 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 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2727 		goto out;
2728 	}
2729 
2730 	if (!ifmsh->mshcfg.dot11MeshForwarding)
2731 		goto out;
2732 
2733 	fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2734 				       sdata->encrypt_headroom, 0, GFP_ATOMIC);
2735 	if (!fwd_skb)
2736 		goto out;
2737 
2738 	fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
2739 	fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2740 	info = IEEE80211_SKB_CB(fwd_skb);
2741 	memset(info, 0, sizeof(*info));
2742 	info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2743 	info->control.vif = &rx->sdata->vif;
2744 	info->control.jiffies = jiffies;
2745 	if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2746 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2747 		memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2748 		/* update power mode indication when forwarding */
2749 		ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2750 	} else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2751 		/* mesh power mode flags updated in mesh_nexthop_lookup */
2752 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2753 	} else {
2754 		/* unable to resolve next hop */
2755 		mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2756 				   fwd_hdr->addr3, 0,
2757 				   WLAN_REASON_MESH_PATH_NOFORWARD,
2758 				   fwd_hdr->addr2);
2759 		IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2760 		kfree_skb(fwd_skb);
2761 		return RX_DROP_MONITOR;
2762 	}
2763 
2764 	IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2765 	ieee80211_add_pending_skb(local, fwd_skb);
2766  out:
2767 	if (is_multicast_ether_addr(hdr->addr1))
2768 		return RX_CONTINUE;
2769 	return RX_DROP_MONITOR;
2770 }
2771 #endif
2772 
2773 static ieee80211_rx_result debug_noinline
2774 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2775 {
2776 	struct ieee80211_sub_if_data *sdata = rx->sdata;
2777 	struct ieee80211_local *local = rx->local;
2778 	struct net_device *dev = sdata->dev;
2779 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2780 	__le16 fc = hdr->frame_control;
2781 	bool port_control;
2782 	int err;
2783 
2784 	if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2785 		return RX_CONTINUE;
2786 
2787 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2788 		return RX_DROP_MONITOR;
2789 
2790 	/*
2791 	 * Send unexpected-4addr-frame event to hostapd. For older versions,
2792 	 * also drop the frame to cooked monitor interfaces.
2793 	 */
2794 	if (ieee80211_has_a4(hdr->frame_control) &&
2795 	    sdata->vif.type == NL80211_IFTYPE_AP) {
2796 		if (rx->sta &&
2797 		    !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2798 			cfg80211_rx_unexpected_4addr_frame(
2799 				rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2800 		return RX_DROP_MONITOR;
2801 	}
2802 
2803 	err = __ieee80211_data_to_8023(rx, &port_control);
2804 	if (unlikely(err))
2805 		return RX_DROP_UNUSABLE;
2806 
2807 	if (!ieee80211_frame_allowed(rx, fc))
2808 		return RX_DROP_MONITOR;
2809 
2810 	/* directly handle TDLS channel switch requests/responses */
2811 	if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2812 						cpu_to_be16(ETH_P_TDLS))) {
2813 		struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2814 
2815 		if (pskb_may_pull(rx->skb,
2816 				  offsetof(struct ieee80211_tdls_data, u)) &&
2817 		    tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2818 		    tf->category == WLAN_CATEGORY_TDLS &&
2819 		    (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2820 		     tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2821 			skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2822 			schedule_work(&local->tdls_chsw_work);
2823 			if (rx->sta)
2824 				rx->sta->rx_stats.packets++;
2825 
2826 			return RX_QUEUED;
2827 		}
2828 	}
2829 
2830 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2831 	    unlikely(port_control) && sdata->bss) {
2832 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2833 				     u.ap);
2834 		dev = sdata->dev;
2835 		rx->sdata = sdata;
2836 	}
2837 
2838 	rx->skb->dev = dev;
2839 
2840 	if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2841 	    local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2842 	    !is_multicast_ether_addr(
2843 		    ((struct ethhdr *)rx->skb->data)->h_dest) &&
2844 	    (!local->scanning &&
2845 	     !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2846 		mod_timer(&local->dynamic_ps_timer, jiffies +
2847 			  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2848 
2849 	ieee80211_deliver_skb(rx);
2850 
2851 	return RX_QUEUED;
2852 }
2853 
2854 static ieee80211_rx_result debug_noinline
2855 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2856 {
2857 	struct sk_buff *skb = rx->skb;
2858 	struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2859 	struct tid_ampdu_rx *tid_agg_rx;
2860 	u16 start_seq_num;
2861 	u16 tid;
2862 
2863 	if (likely(!ieee80211_is_ctl(bar->frame_control)))
2864 		return RX_CONTINUE;
2865 
2866 	if (ieee80211_is_back_req(bar->frame_control)) {
2867 		struct {
2868 			__le16 control, start_seq_num;
2869 		} __packed bar_data;
2870 		struct ieee80211_event event = {
2871 			.type = BAR_RX_EVENT,
2872 		};
2873 
2874 		if (!rx->sta)
2875 			return RX_DROP_MONITOR;
2876 
2877 		if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2878 				  &bar_data, sizeof(bar_data)))
2879 			return RX_DROP_MONITOR;
2880 
2881 		tid = le16_to_cpu(bar_data.control) >> 12;
2882 
2883 		if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2884 		    !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2885 			ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2886 					     WLAN_BACK_RECIPIENT,
2887 					     WLAN_REASON_QSTA_REQUIRE_SETUP);
2888 
2889 		tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2890 		if (!tid_agg_rx)
2891 			return RX_DROP_MONITOR;
2892 
2893 		start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2894 		event.u.ba.tid = tid;
2895 		event.u.ba.ssn = start_seq_num;
2896 		event.u.ba.sta = &rx->sta->sta;
2897 
2898 		/* reset session timer */
2899 		if (tid_agg_rx->timeout)
2900 			mod_timer(&tid_agg_rx->session_timer,
2901 				  TU_TO_EXP_TIME(tid_agg_rx->timeout));
2902 
2903 		spin_lock(&tid_agg_rx->reorder_lock);
2904 		/* release stored frames up to start of BAR */
2905 		ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2906 						 start_seq_num, frames);
2907 		spin_unlock(&tid_agg_rx->reorder_lock);
2908 
2909 		drv_event_callback(rx->local, rx->sdata, &event);
2910 
2911 		kfree_skb(skb);
2912 		return RX_QUEUED;
2913 	}
2914 
2915 	/*
2916 	 * After this point, we only want management frames,
2917 	 * so we can drop all remaining control frames to
2918 	 * cooked monitor interfaces.
2919 	 */
2920 	return RX_DROP_MONITOR;
2921 }
2922 
2923 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2924 					   struct ieee80211_mgmt *mgmt,
2925 					   size_t len)
2926 {
2927 	struct ieee80211_local *local = sdata->local;
2928 	struct sk_buff *skb;
2929 	struct ieee80211_mgmt *resp;
2930 
2931 	if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2932 		/* Not to own unicast address */
2933 		return;
2934 	}
2935 
2936 	if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2937 	    !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
2938 		/* Not from the current AP or not associated yet. */
2939 		return;
2940 	}
2941 
2942 	if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2943 		/* Too short SA Query request frame */
2944 		return;
2945 	}
2946 
2947 	skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2948 	if (skb == NULL)
2949 		return;
2950 
2951 	skb_reserve(skb, local->hw.extra_tx_headroom);
2952 	resp = skb_put_zero(skb, 24);
2953 	memcpy(resp->da, mgmt->sa, ETH_ALEN);
2954 	memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2955 	memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2956 	resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2957 					  IEEE80211_STYPE_ACTION);
2958 	skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2959 	resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2960 	resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2961 	memcpy(resp->u.action.u.sa_query.trans_id,
2962 	       mgmt->u.action.u.sa_query.trans_id,
2963 	       WLAN_SA_QUERY_TR_ID_LEN);
2964 
2965 	ieee80211_tx_skb(sdata, skb);
2966 }
2967 
2968 static ieee80211_rx_result debug_noinline
2969 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2970 {
2971 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2972 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2973 
2974 	/*
2975 	 * From here on, look only at management frames.
2976 	 * Data and control frames are already handled,
2977 	 * and unknown (reserved) frames are useless.
2978 	 */
2979 	if (rx->skb->len < 24)
2980 		return RX_DROP_MONITOR;
2981 
2982 	if (!ieee80211_is_mgmt(mgmt->frame_control))
2983 		return RX_DROP_MONITOR;
2984 
2985 	if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2986 	    ieee80211_is_beacon(mgmt->frame_control) &&
2987 	    !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
2988 		int sig = 0;
2989 
2990 		if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
2991 		    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
2992 			sig = status->signal;
2993 
2994 		cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2995 					    rx->skb->data, rx->skb->len,
2996 					    status->freq, sig);
2997 		rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2998 	}
2999 
3000 	if (ieee80211_drop_unencrypted_mgmt(rx))
3001 		return RX_DROP_UNUSABLE;
3002 
3003 	return RX_CONTINUE;
3004 }
3005 
3006 static ieee80211_rx_result debug_noinline
3007 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3008 {
3009 	struct ieee80211_local *local = rx->local;
3010 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3011 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3012 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3013 	int len = rx->skb->len;
3014 
3015 	if (!ieee80211_is_action(mgmt->frame_control))
3016 		return RX_CONTINUE;
3017 
3018 	/* drop too small frames */
3019 	if (len < IEEE80211_MIN_ACTION_SIZE)
3020 		return RX_DROP_UNUSABLE;
3021 
3022 	if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3023 	    mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3024 	    mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3025 		return RX_DROP_UNUSABLE;
3026 
3027 	switch (mgmt->u.action.category) {
3028 	case WLAN_CATEGORY_HT:
3029 		/* reject HT action frames from stations not supporting HT */
3030 		if (!rx->sta->sta.ht_cap.ht_supported)
3031 			goto invalid;
3032 
3033 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3034 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3035 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3036 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3037 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3038 			break;
3039 
3040 		/* verify action & smps_control/chanwidth are present */
3041 		if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3042 			goto invalid;
3043 
3044 		switch (mgmt->u.action.u.ht_smps.action) {
3045 		case WLAN_HT_ACTION_SMPS: {
3046 			struct ieee80211_supported_band *sband;
3047 			enum ieee80211_smps_mode smps_mode;
3048 			struct sta_opmode_info sta_opmode = {};
3049 
3050 			/* convert to HT capability */
3051 			switch (mgmt->u.action.u.ht_smps.smps_control) {
3052 			case WLAN_HT_SMPS_CONTROL_DISABLED:
3053 				smps_mode = IEEE80211_SMPS_OFF;
3054 				break;
3055 			case WLAN_HT_SMPS_CONTROL_STATIC:
3056 				smps_mode = IEEE80211_SMPS_STATIC;
3057 				break;
3058 			case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3059 				smps_mode = IEEE80211_SMPS_DYNAMIC;
3060 				break;
3061 			default:
3062 				goto invalid;
3063 			}
3064 
3065 			/* if no change do nothing */
3066 			if (rx->sta->sta.smps_mode == smps_mode)
3067 				goto handled;
3068 			rx->sta->sta.smps_mode = smps_mode;
3069 			sta_opmode.smps_mode =
3070 				ieee80211_smps_mode_to_smps_mode(smps_mode);
3071 			sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3072 
3073 			sband = rx->local->hw.wiphy->bands[status->band];
3074 
3075 			rate_control_rate_update(local, sband, rx->sta,
3076 						 IEEE80211_RC_SMPS_CHANGED);
3077 			cfg80211_sta_opmode_change_notify(sdata->dev,
3078 							  rx->sta->addr,
3079 							  &sta_opmode,
3080 							  GFP_ATOMIC);
3081 			goto handled;
3082 		}
3083 		case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3084 			struct ieee80211_supported_band *sband;
3085 			u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3086 			enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3087 			struct sta_opmode_info sta_opmode = {};
3088 
3089 			/* If it doesn't support 40 MHz it can't change ... */
3090 			if (!(rx->sta->sta.ht_cap.cap &
3091 					IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3092 				goto handled;
3093 
3094 			if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3095 				max_bw = IEEE80211_STA_RX_BW_20;
3096 			else
3097 				max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3098 
3099 			/* set cur_max_bandwidth and recalc sta bw */
3100 			rx->sta->cur_max_bandwidth = max_bw;
3101 			new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
3102 
3103 			if (rx->sta->sta.bandwidth == new_bw)
3104 				goto handled;
3105 
3106 			rx->sta->sta.bandwidth = new_bw;
3107 			sband = rx->local->hw.wiphy->bands[status->band];
3108 			sta_opmode.bw =
3109 				ieee80211_sta_rx_bw_to_chan_width(rx->sta);
3110 			sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3111 
3112 			rate_control_rate_update(local, sband, rx->sta,
3113 						 IEEE80211_RC_BW_CHANGED);
3114 			cfg80211_sta_opmode_change_notify(sdata->dev,
3115 							  rx->sta->addr,
3116 							  &sta_opmode,
3117 							  GFP_ATOMIC);
3118 			goto handled;
3119 		}
3120 		default:
3121 			goto invalid;
3122 		}
3123 
3124 		break;
3125 	case WLAN_CATEGORY_PUBLIC:
3126 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3127 			goto invalid;
3128 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3129 			break;
3130 		if (!rx->sta)
3131 			break;
3132 		if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3133 			break;
3134 		if (mgmt->u.action.u.ext_chan_switch.action_code !=
3135 				WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3136 			break;
3137 		if (len < offsetof(struct ieee80211_mgmt,
3138 				   u.action.u.ext_chan_switch.variable))
3139 			goto invalid;
3140 		goto queue;
3141 	case WLAN_CATEGORY_VHT:
3142 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3143 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3144 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3145 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3146 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3147 			break;
3148 
3149 		/* verify action code is present */
3150 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3151 			goto invalid;
3152 
3153 		switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3154 		case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3155 			/* verify opmode is present */
3156 			if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3157 				goto invalid;
3158 			goto queue;
3159 		}
3160 		case WLAN_VHT_ACTION_GROUPID_MGMT: {
3161 			if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3162 				goto invalid;
3163 			goto queue;
3164 		}
3165 		default:
3166 			break;
3167 		}
3168 		break;
3169 	case WLAN_CATEGORY_BACK:
3170 		if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3171 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3172 		    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3173 		    sdata->vif.type != NL80211_IFTYPE_AP &&
3174 		    sdata->vif.type != NL80211_IFTYPE_ADHOC)
3175 			break;
3176 
3177 		/* verify action_code is present */
3178 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3179 			break;
3180 
3181 		switch (mgmt->u.action.u.addba_req.action_code) {
3182 		case WLAN_ACTION_ADDBA_REQ:
3183 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3184 				   sizeof(mgmt->u.action.u.addba_req)))
3185 				goto invalid;
3186 			break;
3187 		case WLAN_ACTION_ADDBA_RESP:
3188 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3189 				   sizeof(mgmt->u.action.u.addba_resp)))
3190 				goto invalid;
3191 			break;
3192 		case WLAN_ACTION_DELBA:
3193 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3194 				   sizeof(mgmt->u.action.u.delba)))
3195 				goto invalid;
3196 			break;
3197 		default:
3198 			goto invalid;
3199 		}
3200 
3201 		goto queue;
3202 	case WLAN_CATEGORY_SPECTRUM_MGMT:
3203 		/* verify action_code is present */
3204 		if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3205 			break;
3206 
3207 		switch (mgmt->u.action.u.measurement.action_code) {
3208 		case WLAN_ACTION_SPCT_MSR_REQ:
3209 			if (status->band != NL80211_BAND_5GHZ)
3210 				break;
3211 
3212 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3213 				   sizeof(mgmt->u.action.u.measurement)))
3214 				break;
3215 
3216 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3217 				break;
3218 
3219 			ieee80211_process_measurement_req(sdata, mgmt, len);
3220 			goto handled;
3221 		case WLAN_ACTION_SPCT_CHL_SWITCH: {
3222 			u8 *bssid;
3223 			if (len < (IEEE80211_MIN_ACTION_SIZE +
3224 				   sizeof(mgmt->u.action.u.chan_switch)))
3225 				break;
3226 
3227 			if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3228 			    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3229 			    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3230 				break;
3231 
3232 			if (sdata->vif.type == NL80211_IFTYPE_STATION)
3233 				bssid = sdata->u.mgd.bssid;
3234 			else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3235 				bssid = sdata->u.ibss.bssid;
3236 			else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3237 				bssid = mgmt->sa;
3238 			else
3239 				break;
3240 
3241 			if (!ether_addr_equal(mgmt->bssid, bssid))
3242 				break;
3243 
3244 			goto queue;
3245 			}
3246 		}
3247 		break;
3248 	case WLAN_CATEGORY_SA_QUERY:
3249 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3250 			   sizeof(mgmt->u.action.u.sa_query)))
3251 			break;
3252 
3253 		switch (mgmt->u.action.u.sa_query.action) {
3254 		case WLAN_ACTION_SA_QUERY_REQUEST:
3255 			if (sdata->vif.type != NL80211_IFTYPE_STATION)
3256 				break;
3257 			ieee80211_process_sa_query_req(sdata, mgmt, len);
3258 			goto handled;
3259 		}
3260 		break;
3261 	case WLAN_CATEGORY_SELF_PROTECTED:
3262 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3263 			   sizeof(mgmt->u.action.u.self_prot.action_code)))
3264 			break;
3265 
3266 		switch (mgmt->u.action.u.self_prot.action_code) {
3267 		case WLAN_SP_MESH_PEERING_OPEN:
3268 		case WLAN_SP_MESH_PEERING_CLOSE:
3269 		case WLAN_SP_MESH_PEERING_CONFIRM:
3270 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3271 				goto invalid;
3272 			if (sdata->u.mesh.user_mpm)
3273 				/* userspace handles this frame */
3274 				break;
3275 			goto queue;
3276 		case WLAN_SP_MGK_INFORM:
3277 		case WLAN_SP_MGK_ACK:
3278 			if (!ieee80211_vif_is_mesh(&sdata->vif))
3279 				goto invalid;
3280 			break;
3281 		}
3282 		break;
3283 	case WLAN_CATEGORY_MESH_ACTION:
3284 		if (len < (IEEE80211_MIN_ACTION_SIZE +
3285 			   sizeof(mgmt->u.action.u.mesh_action.action_code)))
3286 			break;
3287 
3288 		if (!ieee80211_vif_is_mesh(&sdata->vif))
3289 			break;
3290 		if (mesh_action_is_path_sel(mgmt) &&
3291 		    !mesh_path_sel_is_hwmp(sdata))
3292 			break;
3293 		goto queue;
3294 	}
3295 
3296 	return RX_CONTINUE;
3297 
3298  invalid:
3299 	status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3300 	/* will return in the next handlers */
3301 	return RX_CONTINUE;
3302 
3303  handled:
3304 	if (rx->sta)
3305 		rx->sta->rx_stats.packets++;
3306 	dev_kfree_skb(rx->skb);
3307 	return RX_QUEUED;
3308 
3309  queue:
3310 	skb_queue_tail(&sdata->skb_queue, rx->skb);
3311 	ieee80211_queue_work(&local->hw, &sdata->work);
3312 	if (rx->sta)
3313 		rx->sta->rx_stats.packets++;
3314 	return RX_QUEUED;
3315 }
3316 
3317 static ieee80211_rx_result debug_noinline
3318 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3319 {
3320 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3321 	int sig = 0;
3322 
3323 	/* skip known-bad action frames and return them in the next handler */
3324 	if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3325 		return RX_CONTINUE;
3326 
3327 	/*
3328 	 * Getting here means the kernel doesn't know how to handle
3329 	 * it, but maybe userspace does ... include returned frames
3330 	 * so userspace can register for those to know whether ones
3331 	 * it transmitted were processed or returned.
3332 	 */
3333 
3334 	if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3335 	    !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3336 		sig = status->signal;
3337 
3338 	if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
3339 			     rx->skb->data, rx->skb->len, 0)) {
3340 		if (rx->sta)
3341 			rx->sta->rx_stats.packets++;
3342 		dev_kfree_skb(rx->skb);
3343 		return RX_QUEUED;
3344 	}
3345 
3346 	return RX_CONTINUE;
3347 }
3348 
3349 static ieee80211_rx_result debug_noinline
3350 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3351 {
3352 	struct ieee80211_local *local = rx->local;
3353 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3354 	struct sk_buff *nskb;
3355 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3356 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3357 
3358 	if (!ieee80211_is_action(mgmt->frame_control))
3359 		return RX_CONTINUE;
3360 
3361 	/*
3362 	 * For AP mode, hostapd is responsible for handling any action
3363 	 * frames that we didn't handle, including returning unknown
3364 	 * ones. For all other modes we will return them to the sender,
3365 	 * setting the 0x80 bit in the action category, as required by
3366 	 * 802.11-2012 9.24.4.
3367 	 * Newer versions of hostapd shall also use the management frame
3368 	 * registration mechanisms, but older ones still use cooked
3369 	 * monitor interfaces so push all frames there.
3370 	 */
3371 	if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3372 	    (sdata->vif.type == NL80211_IFTYPE_AP ||
3373 	     sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3374 		return RX_DROP_MONITOR;
3375 
3376 	if (is_multicast_ether_addr(mgmt->da))
3377 		return RX_DROP_MONITOR;
3378 
3379 	/* do not return rejected action frames */
3380 	if (mgmt->u.action.category & 0x80)
3381 		return RX_DROP_UNUSABLE;
3382 
3383 	nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3384 			       GFP_ATOMIC);
3385 	if (nskb) {
3386 		struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3387 
3388 		nmgmt->u.action.category |= 0x80;
3389 		memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3390 		memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3391 
3392 		memset(nskb->cb, 0, sizeof(nskb->cb));
3393 
3394 		if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3395 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3396 
3397 			info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3398 				      IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3399 				      IEEE80211_TX_CTL_NO_CCK_RATE;
3400 			if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3401 				info->hw_queue =
3402 					local->hw.offchannel_tx_hw_queue;
3403 		}
3404 
3405 		__ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3406 					    status->band, 0);
3407 	}
3408 	dev_kfree_skb(rx->skb);
3409 	return RX_QUEUED;
3410 }
3411 
3412 static ieee80211_rx_result debug_noinline
3413 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3414 {
3415 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3416 	struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3417 	__le16 stype;
3418 
3419 	stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3420 
3421 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3422 	    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3423 	    sdata->vif.type != NL80211_IFTYPE_OCB &&
3424 	    sdata->vif.type != NL80211_IFTYPE_STATION)
3425 		return RX_DROP_MONITOR;
3426 
3427 	switch (stype) {
3428 	case cpu_to_le16(IEEE80211_STYPE_AUTH):
3429 	case cpu_to_le16(IEEE80211_STYPE_BEACON):
3430 	case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3431 		/* process for all: mesh, mlme, ibss */
3432 		break;
3433 	case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3434 	case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3435 	case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3436 	case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3437 		if (is_multicast_ether_addr(mgmt->da) &&
3438 		    !is_broadcast_ether_addr(mgmt->da))
3439 			return RX_DROP_MONITOR;
3440 
3441 		/* process only for station */
3442 		if (sdata->vif.type != NL80211_IFTYPE_STATION)
3443 			return RX_DROP_MONITOR;
3444 		break;
3445 	case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3446 		/* process only for ibss and mesh */
3447 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3448 		    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3449 			return RX_DROP_MONITOR;
3450 		break;
3451 	default:
3452 		return RX_DROP_MONITOR;
3453 	}
3454 
3455 	/* queue up frame and kick off work to process it */
3456 	skb_queue_tail(&sdata->skb_queue, rx->skb);
3457 	ieee80211_queue_work(&rx->local->hw, &sdata->work);
3458 	if (rx->sta)
3459 		rx->sta->rx_stats.packets++;
3460 
3461 	return RX_QUEUED;
3462 }
3463 
3464 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3465 					struct ieee80211_rate *rate)
3466 {
3467 	struct ieee80211_sub_if_data *sdata;
3468 	struct ieee80211_local *local = rx->local;
3469 	struct sk_buff *skb = rx->skb, *skb2;
3470 	struct net_device *prev_dev = NULL;
3471 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3472 	int needed_headroom;
3473 
3474 	/*
3475 	 * If cooked monitor has been processed already, then
3476 	 * don't do it again. If not, set the flag.
3477 	 */
3478 	if (rx->flags & IEEE80211_RX_CMNTR)
3479 		goto out_free_skb;
3480 	rx->flags |= IEEE80211_RX_CMNTR;
3481 
3482 	/* If there are no cooked monitor interfaces, just free the SKB */
3483 	if (!local->cooked_mntrs)
3484 		goto out_free_skb;
3485 
3486 	/* vendor data is long removed here */
3487 	status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3488 	/* room for the radiotap header based on driver features */
3489 	needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3490 
3491 	if (skb_headroom(skb) < needed_headroom &&
3492 	    pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3493 		goto out_free_skb;
3494 
3495 	/* prepend radiotap information */
3496 	ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3497 					 false);
3498 
3499 	skb_reset_mac_header(skb);
3500 	skb->ip_summed = CHECKSUM_UNNECESSARY;
3501 	skb->pkt_type = PACKET_OTHERHOST;
3502 	skb->protocol = htons(ETH_P_802_2);
3503 
3504 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3505 		if (!ieee80211_sdata_running(sdata))
3506 			continue;
3507 
3508 		if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3509 		    !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3510 			continue;
3511 
3512 		if (prev_dev) {
3513 			skb2 = skb_clone(skb, GFP_ATOMIC);
3514 			if (skb2) {
3515 				skb2->dev = prev_dev;
3516 				netif_receive_skb(skb2);
3517 			}
3518 		}
3519 
3520 		prev_dev = sdata->dev;
3521 		ieee80211_rx_stats(sdata->dev, skb->len);
3522 	}
3523 
3524 	if (prev_dev) {
3525 		skb->dev = prev_dev;
3526 		netif_receive_skb(skb);
3527 		return;
3528 	}
3529 
3530  out_free_skb:
3531 	dev_kfree_skb(skb);
3532 }
3533 
3534 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3535 					 ieee80211_rx_result res)
3536 {
3537 	switch (res) {
3538 	case RX_DROP_MONITOR:
3539 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3540 		if (rx->sta)
3541 			rx->sta->rx_stats.dropped++;
3542 		/* fall through */
3543 	case RX_CONTINUE: {
3544 		struct ieee80211_rate *rate = NULL;
3545 		struct ieee80211_supported_band *sband;
3546 		struct ieee80211_rx_status *status;
3547 
3548 		status = IEEE80211_SKB_RXCB((rx->skb));
3549 
3550 		sband = rx->local->hw.wiphy->bands[status->band];
3551 		if (status->encoding == RX_ENC_LEGACY)
3552 			rate = &sband->bitrates[status->rate_idx];
3553 
3554 		ieee80211_rx_cooked_monitor(rx, rate);
3555 		break;
3556 		}
3557 	case RX_DROP_UNUSABLE:
3558 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3559 		if (rx->sta)
3560 			rx->sta->rx_stats.dropped++;
3561 		dev_kfree_skb(rx->skb);
3562 		break;
3563 	case RX_QUEUED:
3564 		I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3565 		break;
3566 	}
3567 }
3568 
3569 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3570 				  struct sk_buff_head *frames)
3571 {
3572 	ieee80211_rx_result res = RX_DROP_MONITOR;
3573 	struct sk_buff *skb;
3574 
3575 #define CALL_RXH(rxh)			\
3576 	do {				\
3577 		res = rxh(rx);		\
3578 		if (res != RX_CONTINUE)	\
3579 			goto rxh_next;  \
3580 	} while (0)
3581 
3582 	/* Lock here to avoid hitting all of the data used in the RX
3583 	 * path (e.g. key data, station data, ...) concurrently when
3584 	 * a frame is released from the reorder buffer due to timeout
3585 	 * from the timer, potentially concurrently with RX from the
3586 	 * driver.
3587 	 */
3588 	spin_lock_bh(&rx->local->rx_path_lock);
3589 
3590 	while ((skb = __skb_dequeue(frames))) {
3591 		/*
3592 		 * all the other fields are valid across frames
3593 		 * that belong to an aMPDU since they are on the
3594 		 * same TID from the same station
3595 		 */
3596 		rx->skb = skb;
3597 
3598 		CALL_RXH(ieee80211_rx_h_check_more_data);
3599 		CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3600 		CALL_RXH(ieee80211_rx_h_sta_process);
3601 		CALL_RXH(ieee80211_rx_h_decrypt);
3602 		CALL_RXH(ieee80211_rx_h_defragment);
3603 		CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3604 		/* must be after MMIC verify so header is counted in MPDU mic */
3605 #ifdef CONFIG_MAC80211_MESH
3606 		if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3607 			CALL_RXH(ieee80211_rx_h_mesh_fwding);
3608 #endif
3609 		CALL_RXH(ieee80211_rx_h_amsdu);
3610 		CALL_RXH(ieee80211_rx_h_data);
3611 
3612 		/* special treatment -- needs the queue */
3613 		res = ieee80211_rx_h_ctrl(rx, frames);
3614 		if (res != RX_CONTINUE)
3615 			goto rxh_next;
3616 
3617 		CALL_RXH(ieee80211_rx_h_mgmt_check);
3618 		CALL_RXH(ieee80211_rx_h_action);
3619 		CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3620 		CALL_RXH(ieee80211_rx_h_action_return);
3621 		CALL_RXH(ieee80211_rx_h_mgmt);
3622 
3623  rxh_next:
3624 		ieee80211_rx_handlers_result(rx, res);
3625 
3626 #undef CALL_RXH
3627 	}
3628 
3629 	spin_unlock_bh(&rx->local->rx_path_lock);
3630 }
3631 
3632 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3633 {
3634 	struct sk_buff_head reorder_release;
3635 	ieee80211_rx_result res = RX_DROP_MONITOR;
3636 
3637 	__skb_queue_head_init(&reorder_release);
3638 
3639 #define CALL_RXH(rxh)			\
3640 	do {				\
3641 		res = rxh(rx);		\
3642 		if (res != RX_CONTINUE)	\
3643 			goto rxh_next;  \
3644 	} while (0)
3645 
3646 	CALL_RXH(ieee80211_rx_h_check_dup);
3647 	CALL_RXH(ieee80211_rx_h_check);
3648 
3649 	ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3650 
3651 	ieee80211_rx_handlers(rx, &reorder_release);
3652 	return;
3653 
3654  rxh_next:
3655 	ieee80211_rx_handlers_result(rx, res);
3656 
3657 #undef CALL_RXH
3658 }
3659 
3660 /*
3661  * This function makes calls into the RX path, therefore
3662  * it has to be invoked under RCU read lock.
3663  */
3664 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3665 {
3666 	struct sk_buff_head frames;
3667 	struct ieee80211_rx_data rx = {
3668 		.sta = sta,
3669 		.sdata = sta->sdata,
3670 		.local = sta->local,
3671 		/* This is OK -- must be QoS data frame */
3672 		.security_idx = tid,
3673 		.seqno_idx = tid,
3674 		.napi = NULL, /* must be NULL to not have races */
3675 	};
3676 	struct tid_ampdu_rx *tid_agg_rx;
3677 
3678 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3679 	if (!tid_agg_rx)
3680 		return;
3681 
3682 	__skb_queue_head_init(&frames);
3683 
3684 	spin_lock(&tid_agg_rx->reorder_lock);
3685 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3686 	spin_unlock(&tid_agg_rx->reorder_lock);
3687 
3688 	if (!skb_queue_empty(&frames)) {
3689 		struct ieee80211_event event = {
3690 			.type = BA_FRAME_TIMEOUT,
3691 			.u.ba.tid = tid,
3692 			.u.ba.sta = &sta->sta,
3693 		};
3694 		drv_event_callback(rx.local, rx.sdata, &event);
3695 	}
3696 
3697 	ieee80211_rx_handlers(&rx, &frames);
3698 }
3699 
3700 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3701 					  u16 ssn, u64 filtered,
3702 					  u16 received_mpdus)
3703 {
3704 	struct sta_info *sta;
3705 	struct tid_ampdu_rx *tid_agg_rx;
3706 	struct sk_buff_head frames;
3707 	struct ieee80211_rx_data rx = {
3708 		/* This is OK -- must be QoS data frame */
3709 		.security_idx = tid,
3710 		.seqno_idx = tid,
3711 	};
3712 	int i, diff;
3713 
3714 	if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3715 		return;
3716 
3717 	__skb_queue_head_init(&frames);
3718 
3719 	sta = container_of(pubsta, struct sta_info, sta);
3720 
3721 	rx.sta = sta;
3722 	rx.sdata = sta->sdata;
3723 	rx.local = sta->local;
3724 
3725 	rcu_read_lock();
3726 	tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3727 	if (!tid_agg_rx)
3728 		goto out;
3729 
3730 	spin_lock_bh(&tid_agg_rx->reorder_lock);
3731 
3732 	if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3733 		int release;
3734 
3735 		/* release all frames in the reorder buffer */
3736 		release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3737 			   IEEE80211_SN_MODULO;
3738 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3739 						 release, &frames);
3740 		/* update ssn to match received ssn */
3741 		tid_agg_rx->head_seq_num = ssn;
3742 	} else {
3743 		ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3744 						 &frames);
3745 	}
3746 
3747 	/* handle the case that received ssn is behind the mac ssn.
3748 	 * it can be tid_agg_rx->buf_size behind and still be valid */
3749 	diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3750 	if (diff >= tid_agg_rx->buf_size) {
3751 		tid_agg_rx->reorder_buf_filtered = 0;
3752 		goto release;
3753 	}
3754 	filtered = filtered >> diff;
3755 	ssn += diff;
3756 
3757 	/* update bitmap */
3758 	for (i = 0; i < tid_agg_rx->buf_size; i++) {
3759 		int index = (ssn + i) % tid_agg_rx->buf_size;
3760 
3761 		tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3762 		if (filtered & BIT_ULL(i))
3763 			tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3764 	}
3765 
3766 	/* now process also frames that the filter marking released */
3767 	ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3768 
3769 release:
3770 	spin_unlock_bh(&tid_agg_rx->reorder_lock);
3771 
3772 	ieee80211_rx_handlers(&rx, &frames);
3773 
3774  out:
3775 	rcu_read_unlock();
3776 }
3777 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3778 
3779 /* main receive path */
3780 
3781 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3782 {
3783 	struct ieee80211_sub_if_data *sdata = rx->sdata;
3784 	struct sk_buff *skb = rx->skb;
3785 	struct ieee80211_hdr *hdr = (void *)skb->data;
3786 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3787 	u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3788 	bool multicast = is_multicast_ether_addr(hdr->addr1);
3789 
3790 	switch (sdata->vif.type) {
3791 	case NL80211_IFTYPE_STATION:
3792 		if (!bssid && !sdata->u.mgd.use_4addr)
3793 			return false;
3794 		if (multicast)
3795 			return true;
3796 		return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3797 	case NL80211_IFTYPE_ADHOC:
3798 		if (!bssid)
3799 			return false;
3800 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3801 		    ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3802 			return false;
3803 		if (ieee80211_is_beacon(hdr->frame_control))
3804 			return true;
3805 		if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3806 			return false;
3807 		if (!multicast &&
3808 		    !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3809 			return false;
3810 		if (!rx->sta) {
3811 			int rate_idx;
3812 			if (status->encoding != RX_ENC_LEGACY)
3813 				rate_idx = 0; /* TODO: HT/VHT rates */
3814 			else
3815 				rate_idx = status->rate_idx;
3816 			ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3817 						 BIT(rate_idx));
3818 		}
3819 		return true;
3820 	case NL80211_IFTYPE_OCB:
3821 		if (!bssid)
3822 			return false;
3823 		if (!ieee80211_is_data_present(hdr->frame_control))
3824 			return false;
3825 		if (!is_broadcast_ether_addr(bssid))
3826 			return false;
3827 		if (!multicast &&
3828 		    !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
3829 			return false;
3830 		if (!rx->sta) {
3831 			int rate_idx;
3832 			if (status->encoding != RX_ENC_LEGACY)
3833 				rate_idx = 0; /* TODO: HT rates */
3834 			else
3835 				rate_idx = status->rate_idx;
3836 			ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3837 						BIT(rate_idx));
3838 		}
3839 		return true;
3840 	case NL80211_IFTYPE_MESH_POINT:
3841 		if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
3842 			return false;
3843 		if (multicast)
3844 			return true;
3845 		return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3846 	case NL80211_IFTYPE_AP_VLAN:
3847 	case NL80211_IFTYPE_AP:
3848 		if (!bssid)
3849 			return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3850 
3851 		if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3852 			/*
3853 			 * Accept public action frames even when the
3854 			 * BSSID doesn't match, this is used for P2P
3855 			 * and location updates. Note that mac80211
3856 			 * itself never looks at these frames.
3857 			 */
3858 			if (!multicast &&
3859 			    !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3860 				return false;
3861 			if (ieee80211_is_public_action(hdr, skb->len))
3862 				return true;
3863 			return ieee80211_is_beacon(hdr->frame_control);
3864 		}
3865 
3866 		if (!ieee80211_has_tods(hdr->frame_control)) {
3867 			/* ignore data frames to TDLS-peers */
3868 			if (ieee80211_is_data(hdr->frame_control))
3869 				return false;
3870 			/* ignore action frames to TDLS-peers */
3871 			if (ieee80211_is_action(hdr->frame_control) &&
3872 			    !is_broadcast_ether_addr(bssid) &&
3873 			    !ether_addr_equal(bssid, hdr->addr1))
3874 				return false;
3875 		}
3876 
3877 		/*
3878 		 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
3879 		 * the BSSID - we've checked that already but may have accepted
3880 		 * the wildcard (ff:ff:ff:ff:ff:ff).
3881 		 *
3882 		 * It also says:
3883 		 *	The BSSID of the Data frame is determined as follows:
3884 		 *	a) If the STA is contained within an AP or is associated
3885 		 *	   with an AP, the BSSID is the address currently in use
3886 		 *	   by the STA contained in the AP.
3887 		 *
3888 		 * So we should not accept data frames with an address that's
3889 		 * multicast.
3890 		 *
3891 		 * Accepting it also opens a security problem because stations
3892 		 * could encrypt it with the GTK and inject traffic that way.
3893 		 */
3894 		if (ieee80211_is_data(hdr->frame_control) && multicast)
3895 			return false;
3896 
3897 		return true;
3898 	case NL80211_IFTYPE_WDS:
3899 		if (bssid || !ieee80211_is_data(hdr->frame_control))
3900 			return false;
3901 		return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
3902 	case NL80211_IFTYPE_P2P_DEVICE:
3903 		return ieee80211_is_public_action(hdr, skb->len) ||
3904 		       ieee80211_is_probe_req(hdr->frame_control) ||
3905 		       ieee80211_is_probe_resp(hdr->frame_control) ||
3906 		       ieee80211_is_beacon(hdr->frame_control);
3907 	case NL80211_IFTYPE_NAN:
3908 		/* Currently no frames on NAN interface are allowed */
3909 		return false;
3910 	default:
3911 		break;
3912 	}
3913 
3914 	WARN_ON_ONCE(1);
3915 	return false;
3916 }
3917 
3918 void ieee80211_check_fast_rx(struct sta_info *sta)
3919 {
3920 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3921 	struct ieee80211_local *local = sdata->local;
3922 	struct ieee80211_key *key;
3923 	struct ieee80211_fast_rx fastrx = {
3924 		.dev = sdata->dev,
3925 		.vif_type = sdata->vif.type,
3926 		.control_port_protocol = sdata->control_port_protocol,
3927 	}, *old, *new = NULL;
3928 	bool assign = false;
3929 
3930 	/* use sparse to check that we don't return without updating */
3931 	__acquire(check_fast_rx);
3932 
3933 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3934 	BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3935 	ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3936 	ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3937 
3938 	fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3939 
3940 	/* fast-rx doesn't do reordering */
3941 	if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3942 	    !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3943 		goto clear;
3944 
3945 	switch (sdata->vif.type) {
3946 	case NL80211_IFTYPE_STATION:
3947 		if (sta->sta.tdls) {
3948 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3949 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3950 			fastrx.expected_ds_bits = 0;
3951 		} else {
3952 			fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3953 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3954 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3955 			fastrx.expected_ds_bits =
3956 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
3957 		}
3958 
3959 		if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
3960 			fastrx.expected_ds_bits |=
3961 				cpu_to_le16(IEEE80211_FCTL_TODS);
3962 			fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3963 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3964 		}
3965 
3966 		if (!sdata->u.mgd.powersave)
3967 			break;
3968 
3969 		/* software powersave is a huge mess, avoid all of it */
3970 		if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3971 			goto clear;
3972 		if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3973 		    !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3974 			goto clear;
3975 		break;
3976 	case NL80211_IFTYPE_AP_VLAN:
3977 	case NL80211_IFTYPE_AP:
3978 		/* parallel-rx requires this, at least with calls to
3979 		 * ieee80211_sta_ps_transition()
3980 		 */
3981 		if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3982 			goto clear;
3983 		fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3984 		fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3985 		fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3986 
3987 		fastrx.internal_forward =
3988 			!(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3989 			(sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3990 			 !sdata->u.vlan.sta);
3991 
3992 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3993 		    sdata->u.vlan.sta) {
3994 			fastrx.expected_ds_bits |=
3995 				cpu_to_le16(IEEE80211_FCTL_FROMDS);
3996 			fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3997 			fastrx.internal_forward = 0;
3998 		}
3999 
4000 		break;
4001 	default:
4002 		goto clear;
4003 	}
4004 
4005 	if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4006 		goto clear;
4007 
4008 	rcu_read_lock();
4009 	key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4010 	if (key) {
4011 		switch (key->conf.cipher) {
4012 		case WLAN_CIPHER_SUITE_TKIP:
4013 			/* we don't want to deal with MMIC in fast-rx */
4014 			goto clear_rcu;
4015 		case WLAN_CIPHER_SUITE_CCMP:
4016 		case WLAN_CIPHER_SUITE_CCMP_256:
4017 		case WLAN_CIPHER_SUITE_GCMP:
4018 		case WLAN_CIPHER_SUITE_GCMP_256:
4019 			break;
4020 		default:
4021 			/* we also don't want to deal with WEP or cipher scheme
4022 			 * since those require looking up the key idx in the
4023 			 * frame, rather than assuming the PTK is used
4024 			 * (we need to revisit this once we implement the real
4025 			 * PTK index, which is now valid in the spec, but we
4026 			 * haven't implemented that part yet)
4027 			 */
4028 			goto clear_rcu;
4029 		}
4030 
4031 		fastrx.key = true;
4032 		fastrx.icv_len = key->conf.icv_len;
4033 	}
4034 
4035 	assign = true;
4036  clear_rcu:
4037 	rcu_read_unlock();
4038  clear:
4039 	__release(check_fast_rx);
4040 
4041 	if (assign)
4042 		new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4043 
4044 	spin_lock_bh(&sta->lock);
4045 	old = rcu_dereference_protected(sta->fast_rx, true);
4046 	rcu_assign_pointer(sta->fast_rx, new);
4047 	spin_unlock_bh(&sta->lock);
4048 
4049 	if (old)
4050 		kfree_rcu(old, rcu_head);
4051 }
4052 
4053 void ieee80211_clear_fast_rx(struct sta_info *sta)
4054 {
4055 	struct ieee80211_fast_rx *old;
4056 
4057 	spin_lock_bh(&sta->lock);
4058 	old = rcu_dereference_protected(sta->fast_rx, true);
4059 	RCU_INIT_POINTER(sta->fast_rx, NULL);
4060 	spin_unlock_bh(&sta->lock);
4061 
4062 	if (old)
4063 		kfree_rcu(old, rcu_head);
4064 }
4065 
4066 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4067 {
4068 	struct ieee80211_local *local = sdata->local;
4069 	struct sta_info *sta;
4070 
4071 	lockdep_assert_held(&local->sta_mtx);
4072 
4073 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
4074 		if (sdata != sta->sdata &&
4075 		    (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4076 			continue;
4077 		ieee80211_check_fast_rx(sta);
4078 	}
4079 }
4080 
4081 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4082 {
4083 	struct ieee80211_local *local = sdata->local;
4084 
4085 	mutex_lock(&local->sta_mtx);
4086 	__ieee80211_check_fast_rx_iface(sdata);
4087 	mutex_unlock(&local->sta_mtx);
4088 }
4089 
4090 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4091 				     struct ieee80211_fast_rx *fast_rx)
4092 {
4093 	struct sk_buff *skb = rx->skb;
4094 	struct ieee80211_hdr *hdr = (void *)skb->data;
4095 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4096 	struct sta_info *sta = rx->sta;
4097 	int orig_len = skb->len;
4098 	int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4099 	int snap_offs = hdrlen;
4100 	struct {
4101 		u8 snap[sizeof(rfc1042_header)];
4102 		__be16 proto;
4103 	} *payload __aligned(2);
4104 	struct {
4105 		u8 da[ETH_ALEN];
4106 		u8 sa[ETH_ALEN];
4107 	} addrs __aligned(2);
4108 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4109 
4110 	if (fast_rx->uses_rss)
4111 		stats = this_cpu_ptr(sta->pcpu_rx_stats);
4112 
4113 	/* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4114 	 * to a common data structure; drivers can implement that per queue
4115 	 * but we don't have that information in mac80211
4116 	 */
4117 	if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4118 		return false;
4119 
4120 #define FAST_RX_CRYPT_FLAGS	(RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4121 
4122 	/* If using encryption, we also need to have:
4123 	 *  - PN_VALIDATED: similar, but the implementation is tricky
4124 	 *  - DECRYPTED: necessary for PN_VALIDATED
4125 	 */
4126 	if (fast_rx->key &&
4127 	    (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4128 		return false;
4129 
4130 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4131 		return false;
4132 
4133 	if (unlikely(ieee80211_is_frag(hdr)))
4134 		return false;
4135 
4136 	/* Since our interface address cannot be multicast, this
4137 	 * implicitly also rejects multicast frames without the
4138 	 * explicit check.
4139 	 *
4140 	 * We shouldn't get any *data* frames not addressed to us
4141 	 * (AP mode will accept multicast *management* frames), but
4142 	 * punting here will make it go through the full checks in
4143 	 * ieee80211_accept_frame().
4144 	 */
4145 	if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4146 		return false;
4147 
4148 	if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4149 					      IEEE80211_FCTL_TODS)) !=
4150 	    fast_rx->expected_ds_bits)
4151 		return false;
4152 
4153 	/* assign the key to drop unencrypted frames (later)
4154 	 * and strip the IV/MIC if necessary
4155 	 */
4156 	if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4157 		/* GCMP header length is the same */
4158 		snap_offs += IEEE80211_CCMP_HDR_LEN;
4159 	}
4160 
4161 	if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4162 		if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4163 			goto drop;
4164 
4165 		payload = (void *)(skb->data + snap_offs);
4166 
4167 		if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4168 			return false;
4169 
4170 		/* Don't handle these here since they require special code.
4171 		 * Accept AARP and IPX even though they should come with a
4172 		 * bridge-tunnel header - but if we get them this way then
4173 		 * there's little point in discarding them.
4174 		 */
4175 		if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4176 			     payload->proto == fast_rx->control_port_protocol))
4177 			return false;
4178 	}
4179 
4180 	/* after this point, don't punt to the slowpath! */
4181 
4182 	if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4183 	    pskb_trim(skb, skb->len - fast_rx->icv_len))
4184 		goto drop;
4185 
4186 	if (unlikely(fast_rx->sta_notify)) {
4187 		ieee80211_sta_rx_notify(rx->sdata, hdr);
4188 		fast_rx->sta_notify = false;
4189 	}
4190 
4191 	/* statistics part of ieee80211_rx_h_sta_process() */
4192 	if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4193 		stats->last_signal = status->signal;
4194 		if (!fast_rx->uses_rss)
4195 			ewma_signal_add(&sta->rx_stats_avg.signal,
4196 					-status->signal);
4197 	}
4198 
4199 	if (status->chains) {
4200 		int i;
4201 
4202 		stats->chains = status->chains;
4203 		for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4204 			int signal = status->chain_signal[i];
4205 
4206 			if (!(status->chains & BIT(i)))
4207 				continue;
4208 
4209 			stats->chain_signal_last[i] = signal;
4210 			if (!fast_rx->uses_rss)
4211 				ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4212 						-signal);
4213 		}
4214 	}
4215 	/* end of statistics */
4216 
4217 	if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4218 		goto drop;
4219 
4220 	if (status->rx_flags & IEEE80211_RX_AMSDU) {
4221 		if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4222 		    RX_QUEUED)
4223 			goto drop;
4224 
4225 		return true;
4226 	}
4227 
4228 	stats->last_rx = jiffies;
4229 	stats->last_rate = sta_stats_encode_rate(status);
4230 
4231 	stats->fragments++;
4232 	stats->packets++;
4233 
4234 	/* do the header conversion - first grab the addresses */
4235 	ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4236 	ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4237 	/* remove the SNAP but leave the ethertype */
4238 	skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4239 	/* push the addresses in front */
4240 	memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4241 
4242 	skb->dev = fast_rx->dev;
4243 
4244 	ieee80211_rx_stats(fast_rx->dev, skb->len);
4245 
4246 	/* The seqno index has the same property as needed
4247 	 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4248 	 * for non-QoS-data frames. Here we know it's a data
4249 	 * frame, so count MSDUs.
4250 	 */
4251 	u64_stats_update_begin(&stats->syncp);
4252 	stats->msdu[rx->seqno_idx]++;
4253 	stats->bytes += orig_len;
4254 	u64_stats_update_end(&stats->syncp);
4255 
4256 	if (fast_rx->internal_forward) {
4257 		struct sk_buff *xmit_skb = NULL;
4258 		if (is_multicast_ether_addr(addrs.da)) {
4259 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
4260 		} else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4261 			   sta_info_get(rx->sdata, addrs.da)) {
4262 			xmit_skb = skb;
4263 			skb = NULL;
4264 		}
4265 
4266 		if (xmit_skb) {
4267 			/*
4268 			 * Send to wireless media and increase priority by 256
4269 			 * to keep the received priority instead of
4270 			 * reclassifying the frame (see cfg80211_classify8021d).
4271 			 */
4272 			xmit_skb->priority += 256;
4273 			xmit_skb->protocol = htons(ETH_P_802_3);
4274 			skb_reset_network_header(xmit_skb);
4275 			skb_reset_mac_header(xmit_skb);
4276 			dev_queue_xmit(xmit_skb);
4277 		}
4278 
4279 		if (!skb)
4280 			return true;
4281 	}
4282 
4283 	/* deliver to local stack */
4284 	skb->protocol = eth_type_trans(skb, fast_rx->dev);
4285 	memset(skb->cb, 0, sizeof(skb->cb));
4286 	if (rx->napi)
4287 		napi_gro_receive(rx->napi, skb);
4288 	else
4289 		netif_receive_skb(skb);
4290 
4291 	return true;
4292  drop:
4293 	dev_kfree_skb(skb);
4294 	stats->dropped++;
4295 	return true;
4296 }
4297 
4298 /*
4299  * This function returns whether or not the SKB
4300  * was destined for RX processing or not, which,
4301  * if consume is true, is equivalent to whether
4302  * or not the skb was consumed.
4303  */
4304 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4305 					    struct sk_buff *skb, bool consume)
4306 {
4307 	struct ieee80211_local *local = rx->local;
4308 	struct ieee80211_sub_if_data *sdata = rx->sdata;
4309 
4310 	rx->skb = skb;
4311 
4312 	/* See if we can do fast-rx; if we have to copy we already lost,
4313 	 * so punt in that case. We should never have to deliver a data
4314 	 * frame to multiple interfaces anyway.
4315 	 *
4316 	 * We skip the ieee80211_accept_frame() call and do the necessary
4317 	 * checking inside ieee80211_invoke_fast_rx().
4318 	 */
4319 	if (consume && rx->sta) {
4320 		struct ieee80211_fast_rx *fast_rx;
4321 
4322 		fast_rx = rcu_dereference(rx->sta->fast_rx);
4323 		if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4324 			return true;
4325 	}
4326 
4327 	if (!ieee80211_accept_frame(rx))
4328 		return false;
4329 
4330 	if (!consume) {
4331 		skb = skb_copy(skb, GFP_ATOMIC);
4332 		if (!skb) {
4333 			if (net_ratelimit())
4334 				wiphy_debug(local->hw.wiphy,
4335 					"failed to copy skb for %s\n",
4336 					sdata->name);
4337 			return true;
4338 		}
4339 
4340 		rx->skb = skb;
4341 	}
4342 
4343 	ieee80211_invoke_rx_handlers(rx);
4344 	return true;
4345 }
4346 
4347 /*
4348  * This is the actual Rx frames handler. as it belongs to Rx path it must
4349  * be called with rcu_read_lock protection.
4350  */
4351 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4352 					 struct ieee80211_sta *pubsta,
4353 					 struct sk_buff *skb,
4354 					 struct napi_struct *napi)
4355 {
4356 	struct ieee80211_local *local = hw_to_local(hw);
4357 	struct ieee80211_sub_if_data *sdata;
4358 	struct ieee80211_hdr *hdr;
4359 	__le16 fc;
4360 	struct ieee80211_rx_data rx;
4361 	struct ieee80211_sub_if_data *prev;
4362 	struct rhlist_head *tmp;
4363 	int err = 0;
4364 
4365 	fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4366 	memset(&rx, 0, sizeof(rx));
4367 	rx.skb = skb;
4368 	rx.local = local;
4369 	rx.napi = napi;
4370 
4371 	if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4372 		I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4373 
4374 	if (ieee80211_is_mgmt(fc)) {
4375 		/* drop frame if too short for header */
4376 		if (skb->len < ieee80211_hdrlen(fc))
4377 			err = -ENOBUFS;
4378 		else
4379 			err = skb_linearize(skb);
4380 	} else {
4381 		err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4382 	}
4383 
4384 	if (err) {
4385 		dev_kfree_skb(skb);
4386 		return;
4387 	}
4388 
4389 	hdr = (struct ieee80211_hdr *)skb->data;
4390 	ieee80211_parse_qos(&rx);
4391 	ieee80211_verify_alignment(&rx);
4392 
4393 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4394 		     ieee80211_is_beacon(hdr->frame_control)))
4395 		ieee80211_scan_rx(local, skb);
4396 
4397 	if (ieee80211_is_data(fc)) {
4398 		struct sta_info *sta, *prev_sta;
4399 
4400 		if (pubsta) {
4401 			rx.sta = container_of(pubsta, struct sta_info, sta);
4402 			rx.sdata = rx.sta->sdata;
4403 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4404 				return;
4405 			goto out;
4406 		}
4407 
4408 		prev_sta = NULL;
4409 
4410 		for_each_sta_info(local, hdr->addr2, sta, tmp) {
4411 			if (!prev_sta) {
4412 				prev_sta = sta;
4413 				continue;
4414 			}
4415 
4416 			rx.sta = prev_sta;
4417 			rx.sdata = prev_sta->sdata;
4418 			ieee80211_prepare_and_rx_handle(&rx, skb, false);
4419 
4420 			prev_sta = sta;
4421 		}
4422 
4423 		if (prev_sta) {
4424 			rx.sta = prev_sta;
4425 			rx.sdata = prev_sta->sdata;
4426 
4427 			if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4428 				return;
4429 			goto out;
4430 		}
4431 	}
4432 
4433 	prev = NULL;
4434 
4435 	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4436 		if (!ieee80211_sdata_running(sdata))
4437 			continue;
4438 
4439 		if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4440 		    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4441 			continue;
4442 
4443 		/*
4444 		 * frame is destined for this interface, but if it's
4445 		 * not also for the previous one we handle that after
4446 		 * the loop to avoid copying the SKB once too much
4447 		 */
4448 
4449 		if (!prev) {
4450 			prev = sdata;
4451 			continue;
4452 		}
4453 
4454 		rx.sta = sta_info_get_bss(prev, hdr->addr2);
4455 		rx.sdata = prev;
4456 		ieee80211_prepare_and_rx_handle(&rx, skb, false);
4457 
4458 		prev = sdata;
4459 	}
4460 
4461 	if (prev) {
4462 		rx.sta = sta_info_get_bss(prev, hdr->addr2);
4463 		rx.sdata = prev;
4464 
4465 		if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4466 			return;
4467 	}
4468 
4469  out:
4470 	dev_kfree_skb(skb);
4471 }
4472 
4473 /*
4474  * This is the receive path handler. It is called by a low level driver when an
4475  * 802.11 MPDU is received from the hardware.
4476  */
4477 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4478 		       struct sk_buff *skb, struct napi_struct *napi)
4479 {
4480 	struct ieee80211_local *local = hw_to_local(hw);
4481 	struct ieee80211_rate *rate = NULL;
4482 	struct ieee80211_supported_band *sband;
4483 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4484 
4485 	WARN_ON_ONCE(softirq_count() == 0);
4486 
4487 	if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4488 		goto drop;
4489 
4490 	sband = local->hw.wiphy->bands[status->band];
4491 	if (WARN_ON(!sband))
4492 		goto drop;
4493 
4494 	/*
4495 	 * If we're suspending, it is possible although not too likely
4496 	 * that we'd be receiving frames after having already partially
4497 	 * quiesced the stack. We can't process such frames then since
4498 	 * that might, for example, cause stations to be added or other
4499 	 * driver callbacks be invoked.
4500 	 */
4501 	if (unlikely(local->quiescing || local->suspended))
4502 		goto drop;
4503 
4504 	/* We might be during a HW reconfig, prevent Rx for the same reason */
4505 	if (unlikely(local->in_reconfig))
4506 		goto drop;
4507 
4508 	/*
4509 	 * The same happens when we're not even started,
4510 	 * but that's worth a warning.
4511 	 */
4512 	if (WARN_ON(!local->started))
4513 		goto drop;
4514 
4515 	if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4516 		/*
4517 		 * Validate the rate, unless a PLCP error means that
4518 		 * we probably can't have a valid rate here anyway.
4519 		 */
4520 
4521 		switch (status->encoding) {
4522 		case RX_ENC_HT:
4523 			/*
4524 			 * rate_idx is MCS index, which can be [0-76]
4525 			 * as documented on:
4526 			 *
4527 			 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4528 			 *
4529 			 * Anything else would be some sort of driver or
4530 			 * hardware error. The driver should catch hardware
4531 			 * errors.
4532 			 */
4533 			if (WARN(status->rate_idx > 76,
4534 				 "Rate marked as an HT rate but passed "
4535 				 "status->rate_idx is not "
4536 				 "an MCS index [0-76]: %d (0x%02x)\n",
4537 				 status->rate_idx,
4538 				 status->rate_idx))
4539 				goto drop;
4540 			break;
4541 		case RX_ENC_VHT:
4542 			if (WARN_ONCE(status->rate_idx > 9 ||
4543 				      !status->nss ||
4544 				      status->nss > 8,
4545 				      "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4546 				      status->rate_idx, status->nss))
4547 				goto drop;
4548 			break;
4549 		case RX_ENC_HE:
4550 			if (WARN_ONCE(status->rate_idx > 11 ||
4551 				      !status->nss ||
4552 				      status->nss > 8,
4553 				      "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4554 				      status->rate_idx, status->nss))
4555 				goto drop;
4556 			break;
4557 		default:
4558 			WARN_ON_ONCE(1);
4559 			/* fall through */
4560 		case RX_ENC_LEGACY:
4561 			if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4562 				goto drop;
4563 			rate = &sband->bitrates[status->rate_idx];
4564 		}
4565 	}
4566 
4567 	status->rx_flags = 0;
4568 
4569 	/*
4570 	 * key references and virtual interfaces are protected using RCU
4571 	 * and this requires that we are in a read-side RCU section during
4572 	 * receive processing
4573 	 */
4574 	rcu_read_lock();
4575 
4576 	/*
4577 	 * Frames with failed FCS/PLCP checksum are not returned,
4578 	 * all other frames are returned without radiotap header
4579 	 * if it was previously present.
4580 	 * Also, frames with less than 16 bytes are dropped.
4581 	 */
4582 	skb = ieee80211_rx_monitor(local, skb, rate);
4583 	if (!skb) {
4584 		rcu_read_unlock();
4585 		return;
4586 	}
4587 
4588 	ieee80211_tpt_led_trig_rx(local,
4589 			((struct ieee80211_hdr *)skb->data)->frame_control,
4590 			skb->len);
4591 
4592 	__ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
4593 
4594 	rcu_read_unlock();
4595 
4596 	return;
4597  drop:
4598 	kfree_skb(skb);
4599 }
4600 EXPORT_SYMBOL(ieee80211_rx_napi);
4601 
4602 /* This is a version of the rx handler that can be called from hard irq
4603  * context. Post the skb on the queue and schedule the tasklet */
4604 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4605 {
4606 	struct ieee80211_local *local = hw_to_local(hw);
4607 
4608 	BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4609 
4610 	skb->pkt_type = IEEE80211_RX_MSG;
4611 	skb_queue_tail(&local->skb_queue, skb);
4612 	tasklet_schedule(&local->tasklet);
4613 }
4614 EXPORT_SYMBOL(ieee80211_rx_irqsafe);
4615