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