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