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