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