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