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