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