blob: 7212a1bebd0c412a3f87d6672955f68dd74a3880 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berg571ecf62007-07-27 15:43:22 +02002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Berg84040802010-02-15 12:46:39 +02006 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03007 * Copyright 2013-2014 Intel Mobile Communications GmbH
Sara Sharonb7540d82017-02-06 15:28:42 +02008 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
Johannes Berg270032a2021-05-11 20:02:46 +02009 * Copyright (C) 2018-2021 Intel Corporation
Johannes Berg571ecf62007-07-27 15:43:22 +020010 */
11
S.Çağlar Onurab466232008-02-14 17:36:47 +020012#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020014#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040018#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040019#include <linux/export.h>
Sebastian Andrzej Siewior183f47f2021-02-18 18:31:24 +010020#include <linux/kcov.h>
Sara Sharon06470f72016-01-28 16:19:25 +020021#include <linux/bitops.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020022#include <net/mac80211.h>
23#include <net/ieee80211_radiotap.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010024#include <asm/unaligned.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020025
26#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020027#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010029#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020030#include "wep.h"
31#include "wpa.h"
32#include "tkip.h"
33#include "wme.h"
Johannes Berg1d8d3de2011-12-16 15:28:57 +010034#include "rate.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020035
Johannes Bergb2e77712007-09-26 15:19:39 +020036/*
37 * monitor mode reception
38 *
39 * This function cleans up the SKB, i.e. it removes all the stuff
40 * only useful for monitoring.
41 */
Johannes Bergc1129922020-05-26 14:33:48 +020042static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
43 unsigned int present_fcs_len,
44 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020045{
Johannes Bergc1129922020-05-26 14:33:48 +020046 struct ieee80211_hdr *hdr;
47 unsigned int hdrlen;
48 __le16 fc;
49
Johannes Berg30841f52017-04-11 15:38:56 +020050 if (present_fcs_len)
51 __pskb_trim(skb, skb->len - present_fcs_len);
Johannes Bergc096b922018-04-20 13:49:18 +030052 __pskb_pull(skb, rtap_space);
Johannes Bergc1129922020-05-26 14:33:48 +020053
54 hdr = (void *)skb->data;
55 fc = hdr->frame_control;
56
57 /*
58 * Remove the HT-Control field (if present) on management
59 * frames after we've sent the frame to monitoring. We
60 * (currently) don't need it, and don't properly parse
61 * frames with it present, due to the assumption of a
62 * fixed management header length.
63 */
64 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
65 return skb;
66
67 hdrlen = ieee80211_hdrlen(fc);
68 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
69
70 if (!pskb_may_pull(skb, hdrlen)) {
71 dev_kfree_skb(skb);
72 return NULL;
73 }
74
75 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
76 hdrlen - IEEE80211_HT_CTL_LEN);
77 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
78
79 return skb;
Johannes Bergb2e77712007-09-26 15:19:39 +020080}
81
Johannes Berg1f7bba72014-11-06 22:56:36 +010082static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
Johannes Bergc096b922018-04-20 13:49:18 +030083 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020084{
Johannes Bergf1d58c22009-06-17 13:13:00 +020085 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +010086 struct ieee80211_hdr *hdr;
87
Johannes Bergc096b922018-04-20 13:49:18 +030088 hdr = (void *)(skb->data + rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +020089
Johannes Berg4c298672012-07-05 11:34:31 +020090 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +010091 RX_FLAG_FAILED_PLCP_CRC |
Shaul Triebitzc3d1f872018-09-05 08:06:06 +030092 RX_FLAG_ONLY_MONITOR |
93 RX_FLAG_NO_PSDU))
Zhao, Gang6b59db72014-04-21 12:52:59 +080094 return true;
95
Johannes Bergc096b922018-04-20 13:49:18 +030096 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +080097 return true;
98
Harvey Harrison87228f52008-06-11 14:21:59 -070099 if (ieee80211_is_ctl(hdr->frame_control) &&
100 !ieee80211_is_pspoll(hdr->frame_control) &&
101 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800102 return true;
103
104 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200105}
106
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200107static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100108ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
109 struct ieee80211_rx_status *status,
110 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200111{
112 int len;
113
114 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200115 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200116
Johannes Berga144f372013-07-03 13:34:02 +0200117 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200118 if (status->chains)
119 len += 4 * hweight8(status->chains);
Johannes Bergefc38dd2018-12-15 11:03:12 +0200120 /* vendor presence bitmap */
121 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
122 len += 4;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100123
124 if (ieee80211_have_rx_timestamp(status)) {
125 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200126 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100127 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200128 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200129 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200130
Johannes Berga144f372013-07-03 13:34:02 +0200131 /* antenna field, if we don't have per-chain info */
132 if (!status->chains)
133 len += 1;
134
Johannes Berg90b9e4462012-11-16 10:09:08 +0100135 /* padding for RX_FLAGS if necessary */
136 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200137
Johannes Bergda6a4352017-04-26 12:14:59 +0200138 if (status->encoding == RX_ENC_HT) /* HT info */
Johannes Berg6d744ba2011-01-27 14:13:17 +0100139 len += 3;
140
Johannes Berg4c298672012-07-05 11:34:31 +0200141 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100142 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200143 len += 8;
144 }
145
Johannes Bergda6a4352017-04-26 12:14:59 +0200146 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100147 len = ALIGN(len, 2);
148 len += 12;
149 }
150
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300151 if (local->hw.radiotap_timestamp.units_pos >= 0) {
152 len = ALIGN(len, 8);
153 len += 12;
154 }
155
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300156 if (status->encoding == RX_ENC_HE &&
157 status->flag & RX_FLAG_RADIOTAP_HE) {
158 len = ALIGN(len, 2);
159 len += 12;
160 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
161 }
162
163 if (status->encoding == RX_ENC_HE &&
164 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
165 len = ALIGN(len, 2);
166 len += 12;
167 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
168 }
169
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300170 if (status->flag & RX_FLAG_NO_PSDU)
171 len += 1;
172
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300173 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
174 len = ALIGN(len, 2);
175 len += 4;
176 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
177 }
178
Johannes Berga144f372013-07-03 13:34:02 +0200179 if (status->chains) {
180 /* antenna and antenna signal fields */
181 len += 2 * hweight8(status->chains);
182 }
183
Johannes Berg1f7bba72014-11-06 22:56:36 +0100184 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
Liad Kaufmanc15353b2019-02-06 13:17:13 +0200185 struct ieee80211_vendor_radiotap *rtap;
186 int vendor_data_offset = 0;
187
188 /*
189 * The position to look at depends on the existence (or non-
190 * existence) of other elements, so take that into account...
191 */
192 if (status->flag & RX_FLAG_RADIOTAP_HE)
193 vendor_data_offset +=
194 sizeof(struct ieee80211_radiotap_he);
195 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
196 vendor_data_offset +=
197 sizeof(struct ieee80211_radiotap_he_mu);
198 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
199 vendor_data_offset +=
200 sizeof(struct ieee80211_radiotap_lsig);
201
202 rtap = (void *)&skb->data[vendor_data_offset];
Johannes Berg1f7bba72014-11-06 22:56:36 +0100203
Johannes Berg1f7bba72014-11-06 22:56:36 +0100204 /* alignment for fixed 6-byte vendor data header */
205 len = ALIGN(len, 2);
206 /* vendor data header */
207 len += 6;
208 if (WARN_ON(rtap->align == 0))
209 rtap->align = 1;
210 len = ALIGN(len, rtap->align);
211 len += rtap->len + rtap->pad;
212 }
213
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200214 return len;
215}
216
Johannes Berg9e478062017-04-13 14:23:49 +0200217static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
218 struct sk_buff *skb,
Johannes Bergc096b922018-04-20 13:49:18 +0300219 int rtap_space)
Johannes Berg9e478062017-04-13 14:23:49 +0200220{
221 struct {
222 struct ieee80211_hdr_3addr hdr;
223 u8 category;
224 u8 action_code;
Mathieu Malaterre7c53eb52019-01-24 19:19:57 +0100225 } __packed __aligned(2) action;
Johannes Berg9e478062017-04-13 14:23:49 +0200226
227 if (!sdata)
228 return;
229
230 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
231
Johannes Bergc096b922018-04-20 13:49:18 +0300232 if (skb->len < rtap_space + sizeof(action) +
Johannes Berg9e478062017-04-13 14:23:49 +0200233 VHT_MUMIMO_GROUPS_DATA_LEN)
234 return;
235
236 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
237 return;
238
Johannes Bergc096b922018-04-20 13:49:18 +0300239 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
Johannes Berg9e478062017-04-13 14:23:49 +0200240
241 if (!ieee80211_is_action(action.hdr.frame_control))
242 return;
243
244 if (action.category != WLAN_CATEGORY_VHT)
245 return;
246
247 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
248 return;
249
250 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
251 return;
252
253 skb = skb_copy(skb, GFP_ATOMIC);
254 if (!skb)
255 return;
256
Johannes Berg9e478062017-04-13 14:23:49 +0200257 skb_queue_tail(&sdata->skb_queue, skb);
258 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
259}
260
Johannes Berg00ea6de2012-09-05 15:54:51 +0200261/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200262 * ieee80211_add_rx_radiotap_header - add radiotap header
263 *
264 * add a radiotap header containing all the fields which the hardware provided.
265 */
266static void
267ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
268 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200269 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200270 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200271{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200272 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200273 struct ieee80211_radiotap_header *rthdr;
274 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200275 __le32 *it_present;
276 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100277 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200278 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200279 int mpdulen, chain;
280 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100281 struct ieee80211_vendor_radiotap rtap = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300282 struct ieee80211_radiotap_he he = {};
283 struct ieee80211_radiotap_he_mu he_mu = {};
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300284 struct ieee80211_radiotap_lsig lsig = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300285
286 if (status->flag & RX_FLAG_RADIOTAP_HE) {
287 he = *(struct ieee80211_radiotap_he *)skb->data;
288 skb_pull(skb, sizeof(he));
289 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
290 }
291
292 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
293 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
294 skb_pull(skb, sizeof(he_mu));
295 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100296
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300297 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
298 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
299 skb_pull(skb, sizeof(lsig));
300 }
301
Johannes Berg1f7bba72014-11-06 22:56:36 +0100302 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
303 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
304 /* rtap.len and rtap.pad are undone immediately */
305 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
306 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800307
308 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200309 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800310 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200311
Johannes Bergd58ff352017-06-16 14:29:23 +0200312 rthdr = skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100313 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200314 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200315
316 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200317 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200318 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
319 BIT(IEEE80211_RADIOTAP_CHANNEL) |
320 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200321
Johannes Berga144f372013-07-03 13:34:02 +0200322 if (!status->chains)
323 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
324
325 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
326 it_present_val |=
327 BIT(IEEE80211_RADIOTAP_EXT) |
328 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
329 put_unaligned_le32(it_present_val, it_present);
330 it_present++;
331 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
332 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
333 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200334
Johannes Berg1f7bba72014-11-06 22:56:36 +0100335 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
336 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
337 BIT(IEEE80211_RADIOTAP_EXT);
338 put_unaligned_le32(it_present_val, it_present);
339 it_present++;
340 it_present_val = rtap.present;
341 }
342
Johannes Berga144f372013-07-03 13:34:02 +0200343 put_unaligned_le32(it_present_val, it_present);
344
345 pos = (void *)(it_present + 1);
346
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200347 /* the order of the following fields is important */
348
349 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800350 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100351 /* padding */
352 while ((pos - (u8 *)rthdr) & 7)
353 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800354 put_unaligned_le64(
355 ieee80211_calculate_rx_timestamp(local, status,
356 mpdulen, 0),
357 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200358 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200359 pos += 8;
360 }
361
362 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200363 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200364 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100365 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
366 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200367 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200368 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200369 pos++;
370
371 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergda6a4352017-04-26 12:14:59 +0200372 if (!rate || status->encoding != RX_ENC_LEGACY) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200373 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100374 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530375 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200376 * added below. The byte here is needed as padding
377 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200378 */
379 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200380 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200381 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200382 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Johannes Bergda6a4352017-04-26 12:14:59 +0200383 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200384 shift = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200385 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200386 shift = 2;
387 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200388 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200389 pos++;
390
391 /* IEEE80211_RADIOTAP_CHANNEL */
Thomas Pedersen3b23c1842020-04-01 18:18:05 -0700392 /* TODO: frequency offset in KHz */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100393 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200394 pos += 2;
Johannes Bergda6a4352017-04-26 12:14:59 +0200395 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200396 channel_flags |= IEEE80211_CHAN_HALF;
Johannes Bergda6a4352017-04-26 12:14:59 +0200397 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200398 channel_flags |= IEEE80211_CHAN_QUARTER;
399
Aloka Dixit412a84b2020-09-11 01:11:35 +0000400 if (status->band == NL80211_BAND_5GHZ ||
401 status->band == NL80211_BAND_6GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200402 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Bergda6a4352017-04-26 12:14:59 +0200403 else if (status->encoding != RX_ENC_LEGACY)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200404 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100405 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200406 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100407 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100408 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100409 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200410 channel_flags |= IEEE80211_CHAN_2GHZ;
411 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200412 pos += 2;
413
414 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200415 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100416 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200417 *pos = status->signal;
418 rthdr->it_present |=
419 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
420 pos++;
421 }
422
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200423 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
424
Johannes Berga144f372013-07-03 13:34:02 +0200425 if (!status->chains) {
426 /* IEEE80211_RADIOTAP_ANTENNA */
427 *pos = status->antenna;
428 pos++;
429 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200430
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200431 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
432
433 /* IEEE80211_RADIOTAP_RX_FLAGS */
434 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100435 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100436 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100437 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100438 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
439 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200440 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100441
Johannes Bergda6a4352017-04-26 12:14:59 +0200442 if (status->encoding == RX_ENC_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200443 unsigned int stbc;
444
Johannes Berg6d744ba2011-01-27 14:13:17 +0100445 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200446 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100447 *pos = 0;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200448 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100449 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
Johannes Bergda6a4352017-04-26 12:14:59 +0200450 if (status->bw == RATE_INFO_BW_40)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100451 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200452 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
Johannes Bergac55d2f2012-05-10 09:09:10 +0200453 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200454 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200455 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200456 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200457 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100458 pos++;
459 *pos++ = status->rate_idx;
460 }
Johannes Berg4c298672012-07-05 11:34:31 +0200461
462 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
463 u16 flags = 0;
464
465 /* ensure 4 byte alignment */
466 while ((pos - (u8 *)rthdr) & 3)
467 pos++;
468 rthdr->it_present |=
469 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
470 put_unaligned_le32(status->ampdu_reference, pos);
471 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200472 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
473 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
474 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
475 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
476 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
477 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
478 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
479 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
Johannes Berg7299d6f2018-02-19 14:48:39 +0200480 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
481 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
482 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
483 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
Johannes Berg4c298672012-07-05 11:34:31 +0200484 put_unaligned_le16(flags, pos);
485 pos += 2;
486 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
487 *pos++ = status->ampdu_delimiter_crc;
488 else
489 *pos++ = 0;
490 *pos++ = 0;
491 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100492
Johannes Bergda6a4352017-04-26 12:14:59 +0200493 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100494 u16 known = local->hw.radiotap_vht_details;
495
496 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100497 put_unaligned_le16(known, pos);
498 pos += 2;
499 /* flags */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200500 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg51648922012-11-22 23:00:18 +0100501 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200502 /* in VHT, STBC is binary */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200503 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200504 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200505 if (status->enc_flags & RX_ENC_FLAG_BF)
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200506 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100507 pos++;
508 /* bandwidth */
Johannes Bergda6a4352017-04-26 12:14:59 +0200509 switch (status->bw) {
510 case RATE_INFO_BW_80:
Johannes Berg51648922012-11-22 23:00:18 +0100511 *pos++ = 4;
Johannes Bergda6a4352017-04-26 12:14:59 +0200512 break;
513 case RATE_INFO_BW_160:
Johannes Berg51648922012-11-22 23:00:18 +0100514 *pos++ = 11;
Johannes Bergda6a4352017-04-26 12:14:59 +0200515 break;
516 case RATE_INFO_BW_40:
Johannes Berg51648922012-11-22 23:00:18 +0100517 *pos++ = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200518 break;
519 default:
Johannes Berg51648922012-11-22 23:00:18 +0100520 *pos++ = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200521 }
Johannes Berg51648922012-11-22 23:00:18 +0100522 /* MCS/NSS */
Johannes Berg8613c942017-04-26 13:51:41 +0200523 *pos = (status->rate_idx << 4) | status->nss;
Johannes Berg51648922012-11-22 23:00:18 +0100524 pos += 4;
525 /* coding field */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200526 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200527 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100528 pos++;
529 /* group ID */
530 pos++;
531 /* partial_aid */
532 pos += 2;
533 }
534
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300535 if (local->hw.radiotap_timestamp.units_pos >= 0) {
536 u16 accuracy = 0;
537 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
538
539 rthdr->it_present |=
540 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
541
542 /* ensure 8 byte alignment */
543 while ((pos - (u8 *)rthdr) & 7)
544 pos++;
545
546 put_unaligned_le64(status->device_timestamp, pos);
547 pos += sizeof(u64);
548
549 if (local->hw.radiotap_timestamp.accuracy >= 0) {
550 accuracy = local->hw.radiotap_timestamp.accuracy;
551 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
552 }
553 put_unaligned_le16(accuracy, pos);
554 pos += sizeof(u16);
555
556 *pos++ = local->hw.radiotap_timestamp.units_pos;
557 *pos++ = flags;
558 }
559
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300560 if (status->encoding == RX_ENC_HE &&
561 status->flag & RX_FLAG_RADIOTAP_HE) {
Johannes Berg331aead2018-08-31 11:31:07 +0300562#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300563
564 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
565 he.data6 |= HE_PREP(DATA6_NSTS,
566 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
567 status->enc_flags));
568 he.data3 |= HE_PREP(DATA3_STBC, 1);
569 } else {
570 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
571 }
572
573#define CHECK_GI(s) \
574 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
575 (int)NL80211_RATE_INFO_HE_GI_##s)
576
577 CHECK_GI(0_8);
578 CHECK_GI(1_6);
579 CHECK_GI(3_2);
580
581 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
582 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
583 he.data3 |= HE_PREP(DATA3_CODING,
584 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
585
586 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
587
588 switch (status->bw) {
589 case RATE_INFO_BW_20:
590 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
591 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
592 break;
593 case RATE_INFO_BW_40:
594 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
595 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
596 break;
597 case RATE_INFO_BW_80:
598 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
599 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
600 break;
601 case RATE_INFO_BW_160:
602 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
603 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
604 break;
605 case RATE_INFO_BW_HE_RU:
606#define CHECK_RU_ALLOC(s) \
607 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
608 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
609
610 CHECK_RU_ALLOC(26);
611 CHECK_RU_ALLOC(52);
612 CHECK_RU_ALLOC(106);
613 CHECK_RU_ALLOC(242);
614 CHECK_RU_ALLOC(484);
615 CHECK_RU_ALLOC(996);
616 CHECK_RU_ALLOC(2x996);
617
618 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
619 status->he_ru + 4);
620 break;
621 default:
622 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
623 }
624
625 /* ensure 2 byte alignment */
626 while ((pos - (u8 *)rthdr) & 1)
627 pos++;
628 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
629 memcpy(pos, &he, sizeof(he));
630 pos += sizeof(he);
631 }
632
633 if (status->encoding == RX_ENC_HE &&
634 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
635 /* ensure 2 byte alignment */
636 while ((pos - (u8 *)rthdr) & 1)
637 pos++;
638 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
639 memcpy(pos, &he_mu, sizeof(he_mu));
640 pos += sizeof(he_mu);
641 }
642
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300643 if (status->flag & RX_FLAG_NO_PSDU) {
644 rthdr->it_present |=
645 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
646 *pos++ = status->zero_length_psdu_type;
647 }
648
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300649 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
650 /* ensure 2 byte alignment */
651 while ((pos - (u8 *)rthdr) & 1)
652 pos++;
653 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
654 memcpy(pos, &lsig, sizeof(lsig));
655 pos += sizeof(lsig);
656 }
657
Johannes Berga144f372013-07-03 13:34:02 +0200658 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
659 *pos++ = status->chain_signal[chain];
660 *pos++ = chain;
661 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100662
663 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
664 /* ensure 2 byte alignment for the vendor field as required */
665 if ((pos - (u8 *)rthdr) & 1)
666 *pos++ = 0;
667 *pos++ = rtap.oui[0];
668 *pos++ = rtap.oui[1];
669 *pos++ = rtap.oui[2];
670 *pos++ = rtap.subns;
671 put_unaligned_le16(rtap.len, pos);
672 pos += 2;
673 /* align the actual payload as requested */
674 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
675 *pos++ = 0;
676 /* data (and possible padding) already follows */
677 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200678}
679
Johannes Berg127f60b2017-04-13 15:50:27 +0200680static struct sk_buff *
681ieee80211_make_monitor_skb(struct ieee80211_local *local,
682 struct sk_buff **origskb,
683 struct ieee80211_rate *rate,
Johannes Bergc096b922018-04-20 13:49:18 +0300684 int rtap_space, bool use_origskb)
Johannes Berg127f60b2017-04-13 15:50:27 +0200685{
686 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
687 int rt_hdrlen, needed_headroom;
688 struct sk_buff *skb;
689
690 /* room for the radiotap header based on driver features */
691 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
Johannes Bergc096b922018-04-20 13:49:18 +0300692 needed_headroom = rt_hdrlen - rtap_space;
Johannes Berg127f60b2017-04-13 15:50:27 +0200693
694 if (use_origskb) {
695 /* only need to expand headroom if necessary */
696 skb = *origskb;
697 *origskb = NULL;
698
699 /*
700 * This shouldn't trigger often because most devices have an
701 * RX header they pull before we get here, and that should
702 * be big enough for our radiotap information. We should
703 * probably export the length to drivers so that we can have
704 * them allocate enough headroom to start with.
705 */
706 if (skb_headroom(skb) < needed_headroom &&
707 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
708 dev_kfree_skb(skb);
709 return NULL;
710 }
711 } else {
712 /*
713 * Need to make a copy and possibly remove radiotap header
714 * and FCS from the original.
715 */
716 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
717
718 if (!skb)
719 return NULL;
720 }
721
722 /* prepend radiotap information */
723 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
724
725 skb_reset_mac_header(skb);
726 skb->ip_summed = CHECKSUM_UNNECESSARY;
727 skb->pkt_type = PACKET_OTHERHOST;
728 skb->protocol = htons(ETH_P_802_2);
729
730 return skb;
731}
732
Johannes Bergb2e77712007-09-26 15:19:39 +0200733/*
734 * This function copies a received frame to all monitor interfaces and
735 * returns a cleaned-up SKB that no longer includes the FCS nor the
736 * radiotap header the driver might have added.
737 */
738static struct sk_buff *
739ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100740 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200741{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200742 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200743 struct ieee80211_sub_if_data *sdata;
Johannes Berg127f60b2017-04-13 15:50:27 +0200744 struct sk_buff *monskb = NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200745 int present_fcs_len = 0;
Johannes Bergc096b922018-04-20 13:49:18 +0300746 unsigned int rtap_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300747 struct ieee80211_sub_if_data *monitor_sdata =
748 rcu_dereference(local->monitor_sdata);
Johannes Berg127f60b2017-04-13 15:50:27 +0200749 bool only_monitor = false;
Ilan Peer80209192018-12-15 11:03:17 +0200750 unsigned int min_head_len;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100751
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300752 if (status->flag & RX_FLAG_RADIOTAP_HE)
753 rtap_space += sizeof(struct ieee80211_radiotap_he);
754
755 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
756 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
757
Ilan Peerd359bbc2018-12-15 11:03:25 +0200758 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
759 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
760
Johannes Berg1f7bba72014-11-06 22:56:36 +0100761 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
Ilan Peerd359bbc2018-12-15 11:03:25 +0200762 struct ieee80211_vendor_radiotap *rtap =
763 (void *)(origskb->data + rtap_space);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100764
Johannes Bergc096b922018-04-20 13:49:18 +0300765 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100766 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200767
Ilan Peer80209192018-12-15 11:03:17 +0200768 min_head_len = rtap_space;
769
Johannes Bergb2e77712007-09-26 15:19:39 +0200770 /*
771 * First, we may need to make a copy of the skb because
772 * (1) we need to modify it for radiotap (if not present), and
773 * (2) the other RX handlers will modify the skb we got.
774 *
775 * We don't need to, of course, if we aren't going to return
776 * the SKB because it has a bad FCS/PLCP checksum.
777 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100778
Ilan Peer80209192018-12-15 11:03:17 +0200779 if (!(status->flag & RX_FLAG_NO_PSDU)) {
780 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
781 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
782 /* driver bug */
783 WARN_ON(1);
784 dev_kfree_skb(origskb);
785 return NULL;
786 }
787 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200788 }
Ilan Peer80209192018-12-15 11:03:17 +0200789
790 /* also consider the hdr->frame_control */
791 min_head_len += 2;
Johannes Berg30841f52017-04-11 15:38:56 +0200792 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200793
Ilan Peer80209192018-12-15 11:03:17 +0200794 /* ensure that the expected data elements are in skb head */
795 if (!pskb_may_pull(origskb, min_head_len)) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800796 dev_kfree_skb(origskb);
797 return NULL;
798 }
799
Johannes Bergc096b922018-04-20 13:49:18 +0300800 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
Johannes Berg127f60b2017-04-13 15:50:27 +0200801
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100802 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200803 if (only_monitor) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200804 dev_kfree_skb(origskb);
805 return NULL;
806 }
807
Johannes Bergc1129922020-05-26 14:33:48 +0200808 return ieee80211_clean_skb(origskb, present_fcs_len,
809 rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200810 }
811
Johannes Bergc096b922018-04-20 13:49:18 +0300812 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
Johannes Berg9e478062017-04-13 14:23:49 +0200813
Johannes Bergf64331d2017-04-13 13:28:18 +0200814 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200815 bool last_monitor = list_is_last(&sdata->u.mntr.list,
816 &local->mon_list);
817
818 if (!monskb)
819 monskb = ieee80211_make_monitor_skb(local, &origskb,
Johannes Bergc096b922018-04-20 13:49:18 +0300820 rate, rtap_space,
Johannes Berg127f60b2017-04-13 15:50:27 +0200821 only_monitor &&
822 last_monitor);
823
824 if (monskb) {
825 struct sk_buff *skb;
826
827 if (last_monitor) {
828 skb = monskb;
829 monskb = NULL;
830 } else {
831 skb = skb_clone(monskb, GFP_ATOMIC);
832 }
833
834 if (skb) {
835 skb->dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +0200836 dev_sw_netstats_rx_add(skb->dev, skb->len);
Johannes Berg127f60b2017-04-13 15:50:27 +0200837 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200838 }
839 }
840
Johannes Berg127f60b2017-04-13 15:50:27 +0200841 if (last_monitor)
842 break;
Johannes Bergb2e77712007-09-26 15:19:39 +0200843 }
844
Johannes Berg127f60b2017-04-13 15:50:27 +0200845 /* this happens if last_monitor was erroneously false */
846 dev_kfree_skb(monskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200847
Johannes Berg127f60b2017-04-13 15:50:27 +0200848 /* ditto */
849 if (!origskb)
850 return NULL;
851
Johannes Bergc1129922020-05-26 14:33:48 +0200852 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200853}
854
Johannes Berg5cf121c2008-02-25 16:27:43 +0100855static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200856{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700857 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200858 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200859 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200860
861 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700862 if (ieee80211_is_data_qos(hdr->frame_control)) {
863 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200864 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700865 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200866 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200867 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200868
869 seqno_idx = tid;
870 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200871 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200872 /*
873 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
874 *
875 * Sequence numbers for management frames, QoS data
876 * frames with a broadcast/multicast address in the
877 * Address 1 field, and all non-QoS data frames sent
878 * by QoS STAs are assigned using an additional single
879 * modulo-4096 counter, [...]
880 *
881 * We also use that counter for non-QoS STAs.
882 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100883 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200884 security_idx = 0;
885 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100886 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200887 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200888 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200889
Johannes Berg9e262972011-07-07 18:45:03 +0200890 rx->seqno_idx = seqno_idx;
891 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200892 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
893 * For now, set skb->priority to 0 for other cases. */
894 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100895}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200896
Johannes Bergd1c3a372009-01-07 00:26:10 +0100897/**
898 * DOC: Packet alignment
899 *
900 * Drivers always need to pass packets that are aligned to two-byte boundaries
901 * to the stack.
902 *
903 * Additionally, should, if possible, align the payload data in a way that
904 * guarantees that the contained IP header is aligned to a four-byte
905 * boundary. In the case of regular frames, this simply means aligning the
906 * payload to a four-byte boundary (because either the IP header is directly
907 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100908 * in front of it). If the payload data is not properly aligned and the
909 * architecture doesn't support efficient unaligned operations, mac80211
910 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100911 *
912 * With A-MSDU frames, however, the payload data address must yield two modulo
913 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
914 * push the IP header further back to a multiple of four again. Thankfully, the
915 * specs were sane enough this time around to require padding each A-MSDU
916 * subframe to a length that is a multiple of four.
917 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300918 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100919 * the payload is not supported, the driver is required to move the 802.11
920 * header to be directly in front of the payload in that case.
921 */
922static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100923{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100924#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100925 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100926#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200927}
928
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200929
Johannes Berg571ecf62007-07-27 15:43:22 +0200930/* rx handlers */
931
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200932static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
933{
934 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
935
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100936 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200937 return 0;
938
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100939 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200940}
941
942
943static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
944{
945 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
946
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100947 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200948 return 0;
949
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100950 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200951}
952
953
954/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
955static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
956{
957 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
958 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200959 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200960
Johannes Berg1df332e2012-10-26 00:09:11 +0200961 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200962 return -1;
963
Jouni Malinenaf2d14b2020-02-22 15:25:47 +0200964 if (!ieee80211_is_robust_mgmt_frame(skb) &&
965 !ieee80211_is_beacon(hdr->frame_control))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200966 return -1; /* not a robust management frame */
967
968 mmie = (struct ieee80211_mmie *)
969 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200970 if (mmie->element_id == WLAN_EID_MMIE &&
971 mmie->length == sizeof(*mmie) - 2)
972 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200973
Jouni Malinen56c52da2015-01-24 19:52:08 +0200974 mmie16 = (struct ieee80211_mmie_16 *)
975 (skb->data + skb->len - sizeof(*mmie16));
976 if (skb->len >= 24 + sizeof(*mmie16) &&
977 mmie16->element_id == WLAN_EID_MMIE &&
978 mmie16->length == sizeof(*mmie16) - 2)
979 return le16_to_cpu(mmie16->key_id);
980
981 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200982}
983
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100984static int ieee80211_get_keyid(struct sk_buff *skb,
985 const struct ieee80211_cipher_scheme *cs)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200986{
987 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
988 __le16 fc;
989 int hdrlen;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100990 int minlen;
991 u8 key_idx_off;
992 u8 key_idx_shift;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200993 u8 keyid;
994
995 fc = hdr->frame_control;
996 hdrlen = ieee80211_hdrlen(fc);
997
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100998 if (cs) {
999 minlen = hdrlen + cs->hdr_len;
1000 key_idx_off = hdrlen + cs->key_idx_off;
1001 key_idx_shift = cs->key_idx_shift;
1002 } else {
1003 /* WEP, TKIP, CCMP and GCMP */
1004 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1005 key_idx_off = hdrlen + 3;
1006 key_idx_shift = 6;
1007 }
1008
1009 if (unlikely(skb->len < minlen))
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001010 return -EINVAL;
1011
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001012 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1013
1014 if (cs)
1015 keyid &= cs->key_idx_mask;
1016 keyid >>= key_idx_shift;
1017
1018 /* cs could use more than the usual two bits for the keyid */
1019 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1020 return -EINVAL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001021
1022 return keyid;
1023}
1024
Johannes Berg1df332e2012-10-26 00:09:11 +02001025static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001026{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001027 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +01001028 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001029
Harvey Harrisona7767f92008-07-02 16:30:51 -07001030 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001031 if (is_multicast_ether_addr(hdr->addr1)) {
1032 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +02001033 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001034 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001035 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001036 return RX_DROP_MONITOR;
1037 } else {
1038 if (!ieee80211_has_a4(hdr->frame_control))
1039 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001040 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001041 return RX_DROP_MONITOR;
1042 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001043 }
1044
1045 /* If there is not an established peer link and this is not a peer link
1046 * establisment frame, beacon or probe, drop the frame.
1047 */
1048
Javier Cardona57cf8042011-05-13 10:45:43 -07001049 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001050 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001051
Harvey Harrisona7767f92008-07-02 16:30:51 -07001052 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001053 return RX_DROP_MONITOR;
1054
Harvey Harrisona7767f92008-07-02 16:30:51 -07001055 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001056 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001057
1058 /* make sure category field is present */
1059 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1060 return RX_DROP_MONITOR;
1061
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001062 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001063 category = mgmt->u.action.category;
1064 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +02001065 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001066 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001067 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001068 }
1069
Harvey Harrisona7767f92008-07-02 16:30:51 -07001070 if (ieee80211_is_probe_req(hdr->frame_control) ||
1071 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -07001072 ieee80211_is_beacon(hdr->frame_control) ||
1073 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -07001074 return RX_CONTINUE;
1075
1076 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001077 }
1078
Johannes Berg902acc72008-02-23 15:17:19 +01001079 return RX_CONTINUE;
1080}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001081
Johannes Bergfb4ea052016-01-28 16:19:24 +02001082static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1083 int index)
1084{
1085 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1086 struct sk_buff *tail = skb_peek_tail(frames);
1087 struct ieee80211_rx_status *status;
1088
Sara Sharon06470f72016-01-28 16:19:25 +02001089 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1090 return true;
1091
Johannes Bergfb4ea052016-01-28 16:19:24 +02001092 if (!tail)
1093 return false;
1094
1095 status = IEEE80211_SKB_RXCB(tail);
1096 if (status->flag & RX_FLAG_AMSDU_MORE)
1097 return false;
1098
1099 return true;
1100}
1101
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001102static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001103 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001104 int index,
1105 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001106{
Michal Kazior83eb9352014-07-16 12:09:31 +02001107 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1108 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +01001109 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001110
Johannes Bergdd318572010-11-29 11:09:16 +01001111 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1112
Michal Kazior83eb9352014-07-16 12:09:31 +02001113 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001114 goto no_frame;
1115
Johannes Bergfb4ea052016-01-28 16:19:24 +02001116 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001117 __skb_queue_purge(skb_list);
1118 goto no_frame;
1119 }
1120
1121 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001122 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +02001123 while ((skb = __skb_dequeue(skb_list))) {
1124 status = IEEE80211_SKB_RXCB(skb);
1125 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1126 __skb_queue_tail(frames, skb);
1127 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001128
1129no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +02001130 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +01001131 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001132}
1133
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001134static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001135 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001136 u16 head_seq_num,
1137 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001138{
1139 int index;
1140
Johannes Bergdd318572010-11-29 11:09:16 +01001141 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1142
Johannes Berg9a886582013-02-15 19:25:00 +01001143 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001144 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001145 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1146 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001147 }
1148}
1149
1150/*
1151 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1152 * the skb was added to the buffer longer than this time ago, the earlier
1153 * frames that have not yet been received are assumed to be lost and the skb
1154 * can be released for processing. This may also release other skb's from the
1155 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001156 *
1157 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001158 */
1159#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1160
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001161static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001162 struct tid_ampdu_rx *tid_agg_rx,
1163 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001164{
Michal Kazior83eb9352014-07-16 12:09:31 +02001165 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001166
Johannes Bergdd318572010-11-29 11:09:16 +01001167 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1168
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001169 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +02001170 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +02001171 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +02001172 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001173 /*
1174 * No buffers ready to be released, but check whether any
1175 * frames in the reorder buffer have timed out.
1176 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001177 int skipped = 1;
1178 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1179 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001180 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001181 skipped++;
1182 continue;
1183 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -07001184 if (skipped &&
1185 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001186 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001187 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001188
Michal Kazior83eb9352014-07-16 12:09:31 +02001189 /* don't leave incomplete A-MSDUs around */
1190 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1191 i = (i + 1) % tid_agg_rx->buf_size)
1192 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1193
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001194 ht_dbg_ratelimited(sdata,
1195 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001196 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1197 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001198
1199 /*
1200 * Increment the head seq# also for the skipped slots.
1201 */
1202 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +01001203 (tid_agg_rx->head_seq_num +
1204 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001205 skipped = 0;
1206 }
Johannes Bergfb4ea052016-01-28 16:19:24 +02001207 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001208 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1209 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +02001210 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001211 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001212
1213 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001214 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001215
1216 for (; j != (index - 1) % tid_agg_rx->buf_size;
1217 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001218 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001219 break;
1220 }
1221
1222 set_release_timer:
1223
Johannes Berg788211d2015-04-01 14:20:42 +02001224 if (!tid_agg_rx->removed)
1225 mod_timer(&tid_agg_rx->reorder_timer,
1226 tid_agg_rx->reorder_time[j] + 1 +
1227 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001228 } else {
1229 del_timer(&tid_agg_rx->reorder_timer);
1230 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001231}
1232
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001233/*
1234 * As this function belongs to the RX path it must be under
1235 * rcu_read_lock protection. It returns false if the frame
1236 * can be processed immediately, true if it was consumed.
1237 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001238static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001239 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001240 struct sk_buff *skb,
1241 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001242{
1243 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001244 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001245 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1246 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1247 u16 head_seq_num, buf_size;
1248 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001249 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001250
Johannes Bergdd318572010-11-29 11:09:16 +01001251 spin_lock(&tid_agg_rx->reorder_lock);
1252
Michal Kazior4549cf22014-09-02 14:05:10 +02001253 /*
1254 * Offloaded BA sessions have no known starting sequence number so pick
1255 * one from first Rxed frame for this tid after BA was started.
1256 */
1257 if (unlikely(tid_agg_rx->auto_seq)) {
1258 tid_agg_rx->auto_seq = false;
1259 tid_agg_rx->ssn = mpdu_seq_num;
1260 tid_agg_rx->head_seq_num = mpdu_seq_num;
1261 }
1262
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001263 buf_size = tid_agg_rx->buf_size;
1264 head_seq_num = tid_agg_rx->head_seq_num;
1265
Sara Sharonb7540d82017-02-06 15:28:42 +02001266 /*
1267 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1268 * be reordered.
1269 */
1270 if (unlikely(!tid_agg_rx->started)) {
1271 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1272 ret = false;
1273 goto out;
1274 }
1275 tid_agg_rx->started = true;
1276 }
1277
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001278 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001279 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001280 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001281 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001282 }
1283
1284 /*
1285 * If frame the sequence number exceeds our buffering window
1286 * size release some previous frames to make room for this one.
1287 */
Johannes Berg9a886582013-02-15 19:25:00 +01001288 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1289 head_seq_num = ieee80211_sn_inc(
1290 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001291 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001292 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001293 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001294 }
1295
1296 /* Now the new frame is always in the range of the reordering buffer */
1297
Karl Beldan2e3049b2013-10-24 15:53:32 +02001298 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001299
1300 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001301 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001302 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001303 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001304 }
1305
1306 /*
1307 * If the current MPDU is in the right order and nothing else
1308 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001309 * If it is first but there's something stored, we may be able
1310 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001311 */
1312 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1313 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001314 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1315 tid_agg_rx->head_seq_num =
1316 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001317 ret = false;
1318 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001319 }
1320
1321 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001322 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1323 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1324 tid_agg_rx->reorder_time[index] = jiffies;
1325 tid_agg_rx->stored_mpdu_num++;
1326 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1327 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001328
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001329 out:
1330 spin_unlock(&tid_agg_rx->reorder_lock);
1331 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001332}
1333
1334/*
1335 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1336 * true if the MPDU was buffered, false if it should be processed.
1337 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001338static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1339 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001340{
Johannes Berg2569a822009-11-25 17:46:17 +01001341 struct sk_buff *skb = rx->skb;
1342 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001343 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001344 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001345 struct tid_ampdu_rx *tid_agg_rx;
1346 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001347 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001348
Johannes Berg051a41fa2013-11-20 11:28:27 +01001349 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1350 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001351 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001352
1353 /*
1354 * filter the QoS data rx stream according to
1355 * STA/TID and check if this STA/TID is on aggregation
1356 */
1357
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001358 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001359 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001360
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001361 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1362 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001363 tid = ieee80211_get_tid(hdr);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001364
Johannes Berga87f7362010-06-10 10:21:38 +02001365 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001366 if (!tid_agg_rx) {
1367 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1368 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1369 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1370 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1371 WLAN_BACK_RECIPIENT,
1372 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001373 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001374 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001375
1376 /* qos null data frames are excluded */
1377 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001378 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001379
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001380 /* not part of a BA session */
1381 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1382 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1383 goto dont_reorder;
1384
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001385 /* new, potentially un-ordered, ampdu frame - process it */
1386
1387 /* reset session timer */
1388 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001389 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001390
1391 /* if this mpdu is fragmented - terminate rx aggregation session */
1392 sc = le16_to_cpu(hdr->seq_ctrl);
1393 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg344eec62010-06-10 10:21:36 +02001394 skb_queue_tail(&rx->sdata->skb_queue, skb);
1395 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001396 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001397 }
1398
Johannes Berga87f7362010-06-10 10:21:38 +02001399 /*
1400 * No locking needed -- we will only ever process one
1401 * RX packet at a time, and thus own tid_agg_rx. All
1402 * other code manipulating it needs to (and does) make
1403 * sure that we cannot get to it any more before doing
1404 * anything with it.
1405 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001406 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1407 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001408 return;
1409
1410 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001411 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001412}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001413
Johannes Berg49461622008-06-30 15:10:45 +02001414static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001415ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001416{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001417 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001418 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001419
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001420 if (status->flag & RX_FLAG_DUP_VALIDATED)
1421 return RX_CONTINUE;
1422
Johannes Berg6b0f3272013-07-11 22:33:26 +02001423 /*
1424 * Drop duplicate 802.11 retransmissions
1425 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1426 */
Johannes Berg03954422014-11-11 16:49:25 +01001427
1428 if (rx->skb->len < 24)
1429 return RX_CONTINUE;
1430
1431 if (ieee80211_is_ctl(hdr->frame_control) ||
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001432 ieee80211_is_any_nullfunc(hdr->frame_control) ||
Johannes Berg03954422014-11-11 16:49:25 +01001433 is_multicast_ether_addr(hdr->addr1))
1434 return RX_CONTINUE;
1435
Johannes Berga732fa72015-10-14 18:27:07 +02001436 if (!rx->sta)
1437 return RX_CONTINUE;
1438
1439 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1440 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1441 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001442 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001443 return RX_DROP_UNUSABLE;
1444 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1445 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001446 }
1447
Johannes Berg03954422014-11-11 16:49:25 +01001448 return RX_CONTINUE;
1449}
1450
1451static ieee80211_rx_result debug_noinline
1452ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1453{
1454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1455
Johannes Berg571ecf62007-07-27 15:43:22 +02001456 /* Drop disallowed frame classes based on STA auth/assoc state;
1457 * IEEE 802.11, Chap 5.5.
1458 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001459 * mac80211 filters only based on association state, i.e. it drops
1460 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001461 * deauth/disassoc frames when needed. In addition, hostapd is
1462 * responsible for filtering on both auth and assoc states.
1463 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001464
Johannes Berg902acc72008-02-23 15:17:19 +01001465 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001466 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001467
Harvey Harrisona7767f92008-07-02 16:30:51 -07001468 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1469 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001470 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001471 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001472 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001473 /*
1474 * accept port control frames from the AP even when it's not
1475 * yet marked ASSOC to prevent a race where we don't set the
1476 * assoc bit quickly enough before it sends the first frame
1477 */
1478 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001479 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001480 unsigned int hdrlen;
1481 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001482
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001483 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1484
1485 if (rx->skb->len < hdrlen + 8)
1486 return RX_DROP_MONITOR;
1487
1488 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1489 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001490 return RX_CONTINUE;
1491 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001492
1493 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1494 cfg80211_rx_spurious_frame(rx->sdata->dev,
1495 hdr->addr2,
1496 GFP_ATOMIC))
1497 return RX_DROP_UNUSABLE;
1498
Johannes Berge4c26ad2008-01-31 19:48:21 +01001499 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001500 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001501
Johannes Berg9ae54c82008-01-31 19:48:20 +01001502 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001503}
1504
1505
Johannes Berg49461622008-06-30 15:10:45 +02001506static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001507ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1508{
1509 struct ieee80211_local *local;
1510 struct ieee80211_hdr *hdr;
1511 struct sk_buff *skb;
1512
1513 local = rx->local;
1514 skb = rx->skb;
1515 hdr = (struct ieee80211_hdr *) skb->data;
1516
1517 if (!local->pspolling)
1518 return RX_CONTINUE;
1519
1520 if (!ieee80211_has_fromds(hdr->frame_control))
1521 /* this is not from AP */
1522 return RX_CONTINUE;
1523
1524 if (!ieee80211_is_data(hdr->frame_control))
1525 return RX_CONTINUE;
1526
1527 if (!ieee80211_has_moredata(hdr->frame_control)) {
1528 /* AP has no more frames buffered for us */
1529 local->pspolling = false;
1530 return RX_CONTINUE;
1531 }
1532
1533 /* more data bit is set, let's request a new frame from the AP */
1534 ieee80211_send_pspoll(local, rx->sdata);
1535
1536 return RX_CONTINUE;
1537}
1538
Marco Porschd012a602012-10-10 12:39:50 -07001539static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001540{
Johannes Berg133b8222008-09-16 14:18:59 +02001541 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001542 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001543 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001544 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001545
Marco Porschd012a602012-10-10 12:39:50 -07001546 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1547 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1548 ps = &sdata->bss->ps;
1549 else
1550 return;
1551
1552 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001553 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001554 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001555 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001556 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1557 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001558
Johannes Berg17c18bf2015-03-21 15:25:43 +01001559 ieee80211_clear_fast_xmit(sta);
1560
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001561 if (!sta->sta.txq[0])
1562 return;
1563
Johannes Bergadf8ed02018-08-31 11:31:08 +03001564 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001565 struct ieee80211_txq *txq = sta->sta.txq[tid];
1566 struct txq_info *txqi = to_txq_info(txq);
1567
1568 spin_lock(&local->active_txq_lock[txq->ac]);
1569 if (!list_empty(&txqi->schedule_order))
1570 list_del_init(&txqi->schedule_order);
1571 spin_unlock(&local->active_txq_lock[txq->ac]);
1572
1573 if (txq_has_queue(txq))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001574 set_bit(tid, &sta->txq_buffered_tids);
1575 else
1576 clear_bit(tid, &sta->txq_buffered_tids);
1577 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001578}
1579
Marco Porschd012a602012-10-10 12:39:50 -07001580static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001581{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001582 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1583 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001584
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001585 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001586 /*
1587 * Clear the flag only if the other one is still set
1588 * so that the TX path won't start TX'ing new frames
1589 * directly ... In the case that the driver flag isn't
1590 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1591 */
1592 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001593 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1594 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001595 return;
1596 }
1597
Johannes Berg5ac2e352014-05-27 16:32:27 +02001598 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1599 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001600 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001601}
1602
Johannes Bergcf471612015-06-16 16:16:38 +02001603int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001604{
Johannes Bergcf471612015-06-16 16:16:38 +02001605 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001606 bool in_ps;
1607
Johannes Bergcf471612015-06-16 16:16:38 +02001608 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001609
1610 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001611 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001612 if ((start && in_ps) || (!start && !in_ps))
1613 return -EINVAL;
1614
1615 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001616 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001617 else
Johannes Bergcf471612015-06-16 16:16:38 +02001618 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001619
1620 return 0;
1621}
1622EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1623
Johannes Berg46fa38e2016-05-03 16:58:00 +03001624void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1625{
1626 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1627
1628 if (test_sta_flag(sta, WLAN_STA_SP))
1629 return;
1630
1631 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1632 ieee80211_sta_ps_deliver_poll_response(sta);
1633 else
1634 set_sta_flag(sta, WLAN_STA_PSPOLL);
1635}
1636EXPORT_SYMBOL(ieee80211_sta_pspoll);
1637
1638void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1639{
1640 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001641 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001642
1643 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001644 * If this AC is not trigger-enabled do nothing unless the
1645 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001646 *
1647 * NB: This could/should check a separate bitmap of trigger-
1648 * enabled queues, but for now we only implement uAPSD w/o
1649 * TSPEC changes to the ACs, so they're always the same.
1650 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001651 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1652 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001653 return;
1654
1655 /* if we are in a service period, do nothing */
1656 if (test_sta_flag(sta, WLAN_STA_SP))
1657 return;
1658
1659 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1660 ieee80211_sta_ps_deliver_uapsd(sta);
1661 else
1662 set_sta_flag(sta, WLAN_STA_UAPSD);
1663}
1664EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1665
Johannes Berg49461622008-06-30 15:10:45 +02001666static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001667ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1668{
1669 struct ieee80211_sub_if_data *sdata = rx->sdata;
1670 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1671 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001672
Johannes Berg5c900672015-04-22 14:48:34 +02001673 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001674 return RX_CONTINUE;
1675
1676 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1677 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1678 return RX_CONTINUE;
1679
1680 /*
1681 * The device handles station powersave, so don't do anything about
1682 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1683 * it to mac80211 since they're handled.)
1684 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001685 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001686 return RX_CONTINUE;
1687
1688 /*
1689 * Don't do anything if the station isn't already asleep. In
1690 * the uAPSD case, the station will probably be marked asleep,
1691 * in the PS-Poll case the station must be confused ...
1692 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001693 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001694 return RX_CONTINUE;
1695
1696 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001697 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001698
1699 /* Free PS Poll skb here instead of returning RX_DROP that would
1700 * count as an dropped frame. */
1701 dev_kfree_skb(rx->skb);
1702
1703 return RX_QUEUED;
1704 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1705 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1706 ieee80211_has_pm(hdr->frame_control) &&
1707 (ieee80211_is_data_qos(hdr->frame_control) ||
1708 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001709 u8 tid = ieee80211_get_tid(hdr);
Johannes Berg47086fc2011-09-29 16:04:33 +02001710
Johannes Berg46fa38e2016-05-03 16:58:00 +03001711 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001712 }
1713
1714 return RX_CONTINUE;
1715}
1716
1717static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001718ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001719{
1720 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001721 struct sk_buff *skb = rx->skb;
1722 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1723 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001724 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001725
1726 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001727 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001728
Johannes Bergb291ba12009-07-10 15:29:03 +02001729 /*
1730 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001731 * BSSID and for station already AUTHORIZED to avoid keeping the
1732 * current IBSS network alive in cases where other STAs start
1733 * using different BSSID. This will also give the station another
1734 * chance to restart the authentication/authorization in case
1735 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001736 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001737 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001738 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001739 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001740 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1741 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001742 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001743 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001744 !is_multicast_ether_addr(hdr->addr1))
1745 sta->rx_stats.last_rate =
1746 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001747 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001748 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001749 sta->rx_stats.last_rx = jiffies;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001750 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
Wen Gongf879ac82020-12-09 11:06:29 +08001751 !is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb291ba12009-07-10 15:29:03 +02001752 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001753 * Mesh beacons will update last_rx when if they are found to
1754 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001755 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001756 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001757 if (ieee80211_is_data(hdr->frame_control))
1758 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001759 }
1760
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001761 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001762
1763 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001764 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001765 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1766
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001767 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001768 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001769 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001770 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001771
Felix Fietkauef0621e2013-04-22 16:29:31 +02001772 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001773 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001774 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1775 int signal = status->chain_signal[i];
1776
1777 if (!(status->chains & BIT(i)))
1778 continue;
1779
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001780 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001781 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001782 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001783 }
1784 }
1785
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001786 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1787 return RX_CONTINUE;
1788
Johannes Berg72eaa432008-11-26 15:02:58 +01001789 /*
1790 * Change STA power saving mode only at the end of a frame
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001791 * exchange sequence, and only for a data or management
1792 * frame as specified in IEEE 802.11-2016 11.2.3.2
Johannes Berg72eaa432008-11-26 15:02:58 +01001793 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001794 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001795 !ieee80211_has_morefrags(hdr->frame_control) &&
Emmanuel Grumbach209327502018-08-20 13:56:07 +03001796 !is_multicast_ether_addr(hdr->addr1) &&
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001797 (ieee80211_is_mgmt(hdr->frame_control) ||
1798 ieee80211_is_data(hdr->frame_control)) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001799 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001800 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001801 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001802 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001803 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001804 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001805 } else {
1806 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001807 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001808 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001809 }
1810
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001811 /* mesh power save support */
1812 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1813 ieee80211_mps_rx_h_sta_process(sta, hdr);
1814
Johannes Berg22403de2009-10-30 12:55:03 +01001815 /*
1816 * Drop (qos-)data::nullfunc frames silently, since they
1817 * are used only to control station power saving mode.
1818 */
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001819 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001820 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001821
1822 /*
1823 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001824 * that was not moved to a 4-addr STA vlan yet send
1825 * the event to userspace and for older hostapd drop
1826 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001827 */
1828 if (ieee80211_has_a4(hdr->frame_control) &&
1829 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1830 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001831 !rx->sdata->u.vlan.sta))) {
1832 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1833 cfg80211_rx_unexpected_4addr_frame(
1834 rx->sdata->dev, sta->sta.addr,
1835 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001836 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001837 }
Johannes Berg22403de2009-10-30 12:55:03 +01001838 /*
1839 * Update counter and free packet here to avoid
1840 * counting this as a dropped packed.
1841 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001842 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001843 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001844 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001845 }
1846
Johannes Berg9ae54c82008-01-31 19:48:20 +01001847 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001848} /* ieee80211_rx_h_sta_process */
1849
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001850static struct ieee80211_key *
1851ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1852{
1853 struct ieee80211_key *key = NULL;
1854 struct ieee80211_sub_if_data *sdata = rx->sdata;
1855 int idx2;
1856
1857 /* Make sure key gets set if either BIGTK key index is set so that
1858 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1859 * Beacon frames and Beacon frames that claim to use another BIGTK key
1860 * index (i.e., a key that we do not have).
1861 */
1862
1863 if (idx < 0) {
1864 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1865 idx2 = idx + 1;
1866 } else {
1867 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1868 idx2 = idx + 1;
1869 else
1870 idx2 = idx - 1;
1871 }
1872
1873 if (rx->sta)
1874 key = rcu_dereference(rx->sta->gtk[idx]);
1875 if (!key)
1876 key = rcu_dereference(sdata->keys[idx]);
1877 if (!key && rx->sta)
1878 key = rcu_dereference(rx->sta->gtk[idx2]);
1879 if (!key)
1880 key = rcu_dereference(sdata->keys[idx2]);
1881
1882 return key;
1883}
1884
Johan Almbladh86c228a2013-08-14 15:29:46 +02001885static ieee80211_rx_result debug_noinline
1886ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1887{
1888 struct sk_buff *skb = rx->skb;
1889 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1890 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1891 int keyidx;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001892 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1893 struct ieee80211_key *sta_ptk = NULL;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001894 struct ieee80211_key *ptk_idx = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001895 int mmie_keyidx = -1;
1896 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001897 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001898
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001899 if (ieee80211_is_ext(hdr->frame_control))
1900 return RX_CONTINUE;
1901
Johan Almbladh86c228a2013-08-14 15:29:46 +02001902 /*
1903 * Key selection 101
1904 *
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001905 * There are five types of keys:
Johan Almbladh86c228a2013-08-14 15:29:46 +02001906 * - GTK (group keys)
1907 * - IGTK (group keys for management frames)
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001908 * - BIGTK (group keys for Beacon frames)
Johan Almbladh86c228a2013-08-14 15:29:46 +02001909 * - PTK (pairwise keys)
1910 * - STK (station-to-station pairwise keys)
1911 *
1912 * When selecting a key, we have to distinguish between multicast
1913 * (including broadcast) and unicast frames, the latter can only
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001914 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1915 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1916 * then unicast frames can also use key indices like GTKs. Hence, if we
Johan Almbladh86c228a2013-08-14 15:29:46 +02001917 * don't have a PTK/STK we check the key index for a WEP key.
1918 *
1919 * Note that in a regular BSS, multicast frames are sent by the
1920 * AP only, associated stations unicast the frame to the AP first
1921 * which then multicasts it on their behalf.
1922 *
1923 * There is also a slight problem in IBSS mode: GTKs are negotiated
1924 * with each station, that is something we don't currently handle.
1925 * The spec seems to expect that one negotiates the same key with
1926 * every station but there's no such requirement; VLANs could be
1927 * possible.
1928 */
1929
Johan Almbladh86c228a2013-08-14 15:29:46 +02001930 /* start without a key */
1931 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001932 fc = hdr->frame_control;
1933
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001934 if (rx->sta) {
1935 int keyid = rx->sta->ptk_idx;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001936 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001937
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001938 if (ieee80211_has_protected(fc)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001939 cs = rx->sta->cipher_scheme;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001940 keyid = ieee80211_get_keyid(rx->skb, cs);
1941
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001942 if (unlikely(keyid < 0))
1943 return RX_DROP_UNUSABLE;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001944
1945 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001946 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001947 }
1948
Johan Almbladh86c228a2013-08-14 15:29:46 +02001949 if (!ieee80211_has_protected(fc))
1950 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1951
1952 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001953 rx->key = ptk_idx ? ptk_idx : sta_ptk;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001954 if ((status->flag & RX_FLAG_DECRYPTED) &&
1955 (status->flag & RX_FLAG_IV_STRIPPED))
1956 return RX_CONTINUE;
1957 /* Skip decryption if the frame is not protected. */
1958 if (!ieee80211_has_protected(fc))
1959 return RX_CONTINUE;
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001960 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1961 /* Broadcast/multicast robust management frame / BIP */
1962 if ((status->flag & RX_FLAG_DECRYPTED) &&
1963 (status->flag & RX_FLAG_IV_STRIPPED))
1964 return RX_CONTINUE;
1965
1966 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1967 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001968 NUM_DEFAULT_BEACON_KEYS) {
1969 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1970 skb->data,
1971 skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001972 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001973 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001974
1975 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1976 if (!rx->key)
1977 return RX_CONTINUE; /* Beacon protection not in use */
Johan Almbladh86c228a2013-08-14 15:29:46 +02001978 } else if (mmie_keyidx >= 0) {
1979 /* Broadcast/multicast robust management frame / BIP */
1980 if ((status->flag & RX_FLAG_DECRYPTED) &&
1981 (status->flag & RX_FLAG_IV_STRIPPED))
1982 return RX_CONTINUE;
1983
1984 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1985 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1986 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09001987 if (rx->sta) {
1988 if (ieee80211_is_group_privacy_action(skb) &&
1989 test_sta_flag(rx->sta, WLAN_STA_MFP))
1990 return RX_DROP_MONITOR;
1991
Johan Almbladh86c228a2013-08-14 15:29:46 +02001992 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09001993 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001994 if (!rx->key)
1995 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1996 } else if (!ieee80211_has_protected(fc)) {
1997 /*
1998 * The frame was not protected, so skip decryption. However, we
1999 * need to set rx->key if there is a key that could have been
2000 * used so that the frame may be dropped if encryption would
2001 * have been expected.
2002 */
2003 struct ieee80211_key *key = NULL;
2004 struct ieee80211_sub_if_data *sdata = rx->sdata;
2005 int i;
2006
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002007 if (ieee80211_is_beacon(fc)) {
2008 key = ieee80211_rx_get_bigtk(rx, -1);
2009 } else if (ieee80211_is_mgmt(fc) &&
2010 is_multicast_ether_addr(hdr->addr1)) {
2011 key = rcu_dereference(rx->sdata->default_mgmt_key);
2012 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002013 if (rx->sta) {
2014 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2015 key = rcu_dereference(rx->sta->gtk[i]);
2016 if (key)
2017 break;
2018 }
2019 }
2020 if (!key) {
2021 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2022 key = rcu_dereference(sdata->keys[i]);
2023 if (key)
2024 break;
2025 }
2026 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002027 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002028 if (key)
2029 rx->key = key;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002030 return RX_CONTINUE;
2031 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002032 /*
2033 * The device doesn't give us the IV so we won't be
2034 * able to look up the key. That's ok though, we
2035 * don't need to decrypt the frame, we just won't
2036 * be able to keep statistics accurate.
2037 * Except for key threshold notifications, should
2038 * we somehow allow the driver to tell us which key
2039 * the hardware used if this flag is set?
2040 */
2041 if ((status->flag & RX_FLAG_DECRYPTED) &&
2042 (status->flag & RX_FLAG_IV_STRIPPED))
2043 return RX_CONTINUE;
2044
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002045 keyidx = ieee80211_get_keyid(rx->skb, cs);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002046
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002047 if (unlikely(keyidx < 0))
2048 return RX_DROP_UNUSABLE;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002049
2050 /* check per-station GTK first, if multicast packet */
2051 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2052 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2053
2054 /* if not found, try default key */
2055 if (!rx->key) {
2056 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2057
2058 /*
2059 * RSNA-protected unicast frames should always be
2060 * sent with pairwise or station-to-station keys,
2061 * but for WEP we allow using a key index as well.
2062 */
2063 if (rx->key &&
2064 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2065 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2066 !is_multicast_ether_addr(hdr->addr1))
2067 rx->key = NULL;
2068 }
2069 }
2070
2071 if (rx->key) {
2072 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2073 return RX_DROP_MONITOR;
2074
Johan Almbladh86c228a2013-08-14 15:29:46 +02002075 /* TODO: add threshold stuff again */
2076 } else {
2077 return RX_DROP_MONITOR;
2078 }
2079
2080 switch (rx->key->conf.cipher) {
2081 case WLAN_CIPHER_SUITE_WEP40:
2082 case WLAN_CIPHER_SUITE_WEP104:
2083 result = ieee80211_crypto_wep_decrypt(rx);
2084 break;
2085 case WLAN_CIPHER_SUITE_TKIP:
2086 result = ieee80211_crypto_tkip_decrypt(rx);
2087 break;
2088 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002089 result = ieee80211_crypto_ccmp_decrypt(
2090 rx, IEEE80211_CCMP_MIC_LEN);
2091 break;
2092 case WLAN_CIPHER_SUITE_CCMP_256:
2093 result = ieee80211_crypto_ccmp_decrypt(
2094 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002095 break;
2096 case WLAN_CIPHER_SUITE_AES_CMAC:
2097 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2098 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02002099 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2100 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2101 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02002102 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2103 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2104 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2105 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02002106 case WLAN_CIPHER_SUITE_GCMP:
2107 case WLAN_CIPHER_SUITE_GCMP_256:
2108 result = ieee80211_crypto_gcmp_decrypt(rx);
2109 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002110 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002111 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002112 }
2113
2114 /* the hdr variable is invalid after the decrypt handlers */
2115
2116 /* either the frame has been decrypted or will be dropped */
2117 status->flag |= RX_FLAG_DECRYPTED;
2118
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002119 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2120 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2121 skb->data, skb->len);
2122
Johan Almbladh86c228a2013-08-14 15:29:46 +02002123 return result;
2124}
2125
Johannes Berg3a11ce02021-05-11 20:02:47 +02002126void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2127{
2128 int i;
2129
2130 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2131 skb_queue_head_init(&cache->entries[i].skb_list);
2132}
2133
2134void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2135{
2136 int i;
2137
2138 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2139 __skb_queue_purge(&cache->entries[i].skb_list);
2140}
2141
Johannes Berg571ecf62007-07-27 15:43:22 +02002142static inline struct ieee80211_fragment_entry *
Johannes Berg3a11ce02021-05-11 20:02:47 +02002143ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
Johannes Berg571ecf62007-07-27 15:43:22 +02002144 unsigned int frag, unsigned int seq, int rx_queue,
2145 struct sk_buff **skb)
2146{
2147 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02002148
Johannes Berg3a11ce02021-05-11 20:02:47 +02002149 entry = &cache->entries[cache->next++];
2150 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2151 cache->next = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002152
Johannes Berg3a11ce02021-05-11 20:02:47 +02002153 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02002154
2155 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2156 *skb = NULL;
2157 entry->first_frag_time = jiffies;
2158 entry->seq = seq;
2159 entry->rx_queue = rx_queue;
2160 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002161 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02002162 entry->extra_len = 0;
2163
2164 return entry;
2165}
2166
2167static inline struct ieee80211_fragment_entry *
Johannes Berg3a11ce02021-05-11 20:02:47 +02002168ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002169 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02002170 int rx_queue, struct ieee80211_hdr *hdr)
2171{
2172 struct ieee80211_fragment_entry *entry;
2173 int i, idx;
2174
Johannes Berg3a11ce02021-05-11 20:02:47 +02002175 idx = cache->next;
Johannes Berg571ecf62007-07-27 15:43:22 +02002176 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2177 struct ieee80211_hdr *f_hdr;
David S. Miller7957a9d2018-08-06 22:49:13 -07002178 struct sk_buff *f_skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002179
2180 idx--;
2181 if (idx < 0)
2182 idx = IEEE80211_FRAGMENT_MAX - 1;
2183
Johannes Berg3a11ce02021-05-11 20:02:47 +02002184 entry = &cache->entries[idx];
Johannes Berg571ecf62007-07-27 15:43:22 +02002185 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2186 entry->rx_queue != rx_queue ||
2187 entry->last_frag + 1 != frag)
2188 continue;
2189
David S. Miller7957a9d2018-08-06 22:49:13 -07002190 f_skb = __skb_peek(&entry->skb_list);
2191 f_hdr = (struct ieee80211_hdr *) f_skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002192
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002193 /*
2194 * Check ftype and addresses are equal, else check next fragment
2195 */
2196 if (((hdr->frame_control ^ f_hdr->frame_control) &
2197 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002198 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2199 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02002200 continue;
2201
S.Çağlar Onurab466232008-02-14 17:36:47 +02002202 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002203 __skb_queue_purge(&entry->skb_list);
2204 continue;
2205 }
2206 return entry;
2207 }
2208
2209 return NULL;
2210}
2211
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002212static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2213{
2214 return rx->key &&
2215 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2216 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2217 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2218 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2219 ieee80211_has_protected(fc);
2220}
2221
Johannes Berg49461622008-06-30 15:10:45 +02002222static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002223ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002224{
Johannes Berg3a11ce02021-05-11 20:02:47 +02002225 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
Johannes Berg571ecf62007-07-27 15:43:22 +02002226 struct ieee80211_hdr *hdr;
2227 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002228 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02002229 unsigned int frag, seq;
2230 struct ieee80211_fragment_entry *entry;
2231 struct sk_buff *skb;
2232
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002233 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002234 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002235
Thomas Pedersen09a740c2020-09-21 19:28:14 -07002236 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002237 return RX_CONTINUE;
2238
Johannes Berg571ecf62007-07-27 15:43:22 +02002239 sc = le16_to_cpu(hdr->seq_ctrl);
2240 frag = sc & IEEE80211_SCTL_FRAG;
2241
Johannes Bergb8fff402014-11-03 13:57:46 +01002242 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02002243 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01002244 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02002245 }
Johannes Bergb8fff402014-11-03 13:57:46 +01002246
Johannes Berg3a11ce02021-05-11 20:02:47 +02002247 if (rx->sta)
2248 cache = &rx->sta->frags;
2249
Andreas Müllerd0259332014-12-12 12:11:11 +01002250 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2251 goto out;
2252
Johannes Berg571ecf62007-07-27 15:43:22 +02002253 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2254
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08002255 if (skb_linearize(rx->skb))
2256 return RX_DROP_UNUSABLE;
2257
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07002258 /*
2259 * skb_linearize() might change the skb->data and
2260 * previously cached variables (in this case, hdr) need to
2261 * be refreshed with the new data.
2262 */
2263 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002264 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2265
2266 if (frag == 0) {
2267 /* This is the first fragment of a new frame. */
Johannes Berg3a11ce02021-05-11 20:02:47 +02002268 entry = ieee80211_reassemble_add(cache, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002269 rx->seqno_idx, &(rx->skb));
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002270 if (requires_sequential_pn(rx, fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02002271 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002272
2273 /* Store CCMP/GCMP PN so that we can verify that the
2274 * next fragment has a sequential PN value.
2275 */
2276 entry->check_sequential_pn = true;
Mathy Vanhoef94034c42021-05-11 20:02:43 +02002277 entry->key_color = rx->key->color;
Johannes Berg571ecf62007-07-27 15:43:22 +02002278 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07002279 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02002280 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01002281 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2282 u.ccmp.rx_pn) !=
2283 offsetof(struct ieee80211_key,
2284 u.gcmp.rx_pn));
2285 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2286 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2287 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2288 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002289 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002290 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002291 }
2292
2293 /* This is a fragment for a frame that should already be pending in
2294 * fragment cache. Add this fragment to the end of the pending entry.
2295 */
Johannes Berg3a11ce02021-05-11 20:02:47 +02002296 entry = ieee80211_reassemble_find(cache, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002297 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02002298 if (!entry) {
2299 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002300 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002301 }
2302
Johannes Berg9acc54b2016-02-26 22:13:40 +01002303 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2304 * MPDU PN values are not incrementing in steps of 1."
2305 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2306 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2307 */
2308 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002309 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002310 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07002311 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002312
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002313 if (!requires_sequential_pn(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002314 return RX_DROP_UNUSABLE;
Mathy Vanhoef94034c42021-05-11 20:02:43 +02002315
2316 /* Prevent mixed key and fragment cache attacks */
2317 if (entry->key_color != rx->key->color)
2318 return RX_DROP_UNUSABLE;
2319
Johannes Berg4325f6c2013-05-08 13:09:08 +02002320 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2321 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002322 pn[i]++;
2323 if (pn[i])
2324 break;
2325 }
Johannes Berg9e262972011-07-07 18:45:03 +02002326 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07002327 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02002328 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002329 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002330 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002331 }
2332
Harvey Harrison358c8d92008-07-15 18:44:13 -07002333 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002334 __skb_queue_tail(&entry->skb_list, rx->skb);
2335 entry->last_frag = frag;
2336 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002337 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002338 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002339 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002340 }
2341
2342 rx->skb = __skb_dequeue(&entry->skb_list);
2343 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002344 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002345 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2346 GFP_ATOMIC))) {
2347 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2348 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002349 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002350 }
2351 }
2352 while ((skb = __skb_dequeue(&entry->skb_list))) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02002353 skb_put_data(rx->skb, skb->data, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02002354 dev_kfree_skb(skb);
2355 }
2356
Johannes Berg571ecf62007-07-27 15:43:22 +02002357 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002358 ieee80211_led_rx(rx->local);
2359 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002360 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002361 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002362 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002363}
2364
Johannes Berg1df332e2012-10-26 00:09:11 +02002365static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002366{
Johannes Berg1df332e2012-10-26 00:09:11 +02002367 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002368 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002369
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002370 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002371}
2372
Johannes Berg1df332e2012-10-26 00:09:11 +02002373static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002374{
Markus Theil0b467b632020-06-25 12:42:14 +02002375 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002376 struct sk_buff *skb = rx->skb;
2377 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2378
Johannes Berg3017b802007-08-28 17:01:53 -04002379 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002380 * Pass through unencrypted frames if the hardware has
2381 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002382 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002383 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002384 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002385
Markus Theil0b467b632020-06-25 12:42:14 +02002386 /* check mesh EAPOL frames first */
2387 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2388 ieee80211_is_data(fc))) {
2389 struct ieee80211s_hdr *mesh_hdr;
2390 u16 hdr_len = ieee80211_hdrlen(fc);
2391 u16 ethertype_offset;
2392 __be16 ethertype;
2393
2394 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2395 goto drop_check;
2396
2397 /* make sure fixed part of mesh header is there, also checks skb len */
2398 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2399 goto drop_check;
2400
2401 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2402 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2403 sizeof(rfc1042_header);
2404
2405 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2406 ethertype == rx->sdata->control_port_protocol)
2407 return 0;
2408 }
2409
2410drop_check:
Johannes Berg571ecf62007-07-27 15:43:22 +02002411 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002412 if (unlikely(!ieee80211_has_protected(fc) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002413 !ieee80211_is_any_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002414 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002415 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002416
2417 return 0;
2418}
2419
Johannes Berg1df332e2012-10-26 00:09:11 +02002420static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002421{
2422 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002423 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002424 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002425
Jouni Malinene3efca02010-03-28 22:31:15 -07002426 /*
2427 * Pass through unencrypted frames if the hardware has
2428 * decrypted them already.
2429 */
2430 if (status->flag & RX_FLAG_DECRYPTED)
2431 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002432
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002433 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002434 if (unlikely(!ieee80211_has_protected(fc) &&
2435 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002436 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002437 if (ieee80211_is_deauth(fc) ||
2438 ieee80211_is_disassoc(fc))
2439 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2440 rx->skb->data,
2441 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002442 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002443 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002444 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002445 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002446 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002447 if (ieee80211_is_deauth(fc) ||
2448 ieee80211_is_disassoc(fc))
2449 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2450 rx->skb->data,
2451 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002452 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002453 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002454 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002455 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2456 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2457 rx->skb->data,
2458 rx->skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002459 return -EACCES;
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002460 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002461 /*
2462 * When using MFP, Action frames are not allowed prior to
2463 * having configured keys.
2464 */
2465 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002466 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002467 return -EACCES;
2468 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002469
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002470 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002471}
2472
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002473static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002474__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002475{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002476 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002477 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002478 bool check_port_control = false;
2479 struct ethhdr *ehdr;
2480 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002481
Felix Fietkau4114fa22011-04-12 19:15:22 +02002482 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002483 if (ieee80211_has_a4(hdr->frame_control) &&
2484 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002485 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002486
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002487 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2488 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2489
2490 if (!sdata->u.mgd.use_4addr)
2491 return -1;
Sathishkumar Muruganandam1ecef202018-08-28 17:51:38 +05302492 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002493 check_port_control = true;
2494 }
2495
Johannes Berg9bc383d2009-11-19 11:55:19 +01002496 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002497 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002498 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002499
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002500 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002501 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002502 return ret;
2503
2504 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002505 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2506 *port_control = true;
2507 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002508 return -1;
2509
2510 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002511}
Johannes Berg571ecf62007-07-27 15:43:22 +02002512
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002513/*
2514 * requires that rx->skb is a frame with ethernet header
2515 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002516static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002517{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302518 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002519 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2520 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2521
2522 /*
2523 * Allow EAPOL frames to us/the PAE group address regardless
2524 * of whether the frame was encrypted or not.
2525 */
Johannes Berga621fa42010-08-27 14:26:54 +03002526 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002527 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2528 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002529 return true;
2530
2531 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002532 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002533 return false;
2534
2535 return true;
2536}
2537
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002538static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2539 struct ieee80211_rx_data *rx)
2540{
2541 struct ieee80211_sub_if_data *sdata = rx->sdata;
2542 struct net_device *dev = sdata->dev;
2543
2544 if (unlikely((skb->protocol == sdata->control_port_protocol ||
Markus Theil7f3f96c2020-03-12 10:10:54 +01002545 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2546 !sdata->control_port_no_preauth)) &&
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002547 sdata->control_port_over_nl80211)) {
2548 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Denis Kenziorf8b43c52019-08-27 17:41:20 -05002549 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002550
Denis Kenziora948f712018-07-03 15:05:48 -05002551 cfg80211_rx_control_port(dev, skb, noencrypt);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002552 dev_kfree_skb(skb);
2553 } else {
Denis Kenziorc8a41c62019-08-27 17:41:19 -05002554 memset(skb->cb, 0, sizeof(skb->cb));
2555
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002556 /* deliver to local stack */
Felix Fietkauc5d16862020-07-26 13:06:11 +02002557 if (rx->list)
2558 list_add_tail(&skb->list, rx->list);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002559 else
2560 netif_receive_skb(skb);
2561 }
2562}
2563
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002564/*
2565 * requires that rx->skb is a frame with ethernet header
2566 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002567static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002568ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002569{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002570 struct ieee80211_sub_if_data *sdata = rx->sdata;
2571 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002572 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002573 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2574 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002575
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002576 skb = rx->skb;
2577 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002578
Lev Stipakov36ec1442020-11-13 23:46:24 +02002579 dev_sw_netstats_rx_add(dev, skb->len);
Johannes Berg5a490512015-04-22 17:10:38 +02002580
Johannes Bergde8f18d2016-03-31 20:02:03 +03002581 if (rx->sta) {
2582 /* The seqno index has the same property as needed
2583 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2584 * for non-QoS-data frames. Here we know it's a data
2585 * frame, so count MSDUs.
2586 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002587 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002588 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002589 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002590 }
2591
Johannes Berg05c914f2008-09-11 00:01:58 +02002592 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2593 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002594 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002595 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002596 if (is_multicast_ether_addr(ehdr->h_dest) &&
2597 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002598 /*
2599 * send multicast frames both to higher layers in
2600 * local net stack and back to the wireless medium
2601 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002602 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002603 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002604 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002605 dev->name);
Johannes Berg42dca5e2018-10-09 10:00:21 +02002606 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2607 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2608 dsta = sta_info_get(sdata, ehdr->h_dest);
Johannes Bergabe60632009-11-25 17:46:18 +01002609 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002610 /*
2611 * The destination station is associated to
2612 * this AP (in this VLAN), so send the frame
2613 * directly to it and do not pass it to local
2614 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002615 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002616 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002617 skb = NULL;
2618 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002619 }
2620 }
2621
Kalle Valo59d9cb02009-12-17 13:54:57 +01002622#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002623 if (skb) {
2624 /* 'align' will only take the values 0 or 2 here since all
2625 * frames are required to be aligned to 2-byte boundaries
2626 * when being passed to mac80211; the code here works just
2627 * as well if that isn't true, but mac80211 assumes it can
2628 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002629 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002630 int align;
2631
2632 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002633 if (align) {
2634 if (WARN_ON(skb_headroom(skb) < 3)) {
2635 dev_kfree_skb(skb);
2636 skb = NULL;
2637 } else {
2638 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002639 size_t len = skb_headlen(skb);
2640 skb->data -= align;
2641 memmove(skb->data, data, len);
2642 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002643 }
2644 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002645 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002646#endif
2647
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002648 if (skb) {
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002649 skb->protocol = eth_type_trans(skb, dev);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002650 ieee80211_deliver_skb_to_local_stack(skb, rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002651 }
2652
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002653 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002654 /*
2655 * Send to wireless media and increase priority by 256 to
2656 * keep the received priority instead of reclassifying
2657 * the frame (see cfg80211_classify8021d).
2658 */
2659 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002660 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002661 skb_reset_network_header(xmit_skb);
2662 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002663 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002664 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002665}
2666
Johannes Berg49461622008-06-30 15:10:45 +02002667static ieee80211_rx_result debug_noinline
Felix Fietkau24bba072018-02-27 13:03:07 +01002668__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002669{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002670 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002671 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002672 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2673 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002674 struct sk_buff_head frame_list;
Johannes Berg7f6990c2016-10-05 15:29:49 +02002675 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002676 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002677
Johannes Bergea720932016-10-05 10:14:42 +02002678 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
Johannes Berge2b52272016-10-05 16:42:06 +02002679 check_da = NULL;
2680 check_sa = NULL;
2681 } else switch (rx->sdata->vif.type) {
2682 case NL80211_IFTYPE_AP:
2683 case NL80211_IFTYPE_AP_VLAN:
2684 check_da = NULL;
2685 break;
2686 case NL80211_IFTYPE_STATION:
2687 if (!rx->sta ||
2688 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2689 check_sa = NULL;
2690 break;
2691 case NL80211_IFTYPE_MESH_POINT:
2692 check_sa = NULL;
2693 break;
2694 default:
2695 break;
Johannes Bergea720932016-10-05 10:14:42 +02002696 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002697
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002698 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002699 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002700
Johannes Berg7f6990c2016-10-05 15:29:49 +02002701 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2702 rx->sdata->vif.addr,
Felix Fietkau24bba072018-02-27 13:03:07 +01002703 rx->sdata->vif.type,
Mathy Vanhoefa1d5ff52021-05-11 20:02:44 +02002704 data_offset, true))
Johannes Berg7f6990c2016-10-05 15:29:49 +02002705 return RX_DROP_UNUSABLE;
2706
Zhu Yieaf85ca2009-12-01 10:18:37 +08002707 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2708 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002709 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002710 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002711
Zhu Yieaf85ca2009-12-01 10:18:37 +08002712 while (!skb_queue_empty(&frame_list)) {
2713 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002714
Harvey Harrison358c8d92008-07-15 18:44:13 -07002715 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002716 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002717 continue;
2718 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002719
2720 ieee80211_deliver_skb(rx);
2721 }
2722
Johannes Berg9ae54c82008-01-31 19:48:20 +01002723 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002724}
2725
Felix Fietkau24bba072018-02-27 13:03:07 +01002726static ieee80211_rx_result debug_noinline
2727ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2728{
2729 struct sk_buff *skb = rx->skb;
2730 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2731 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2732 __le16 fc = hdr->frame_control;
2733
2734 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2735 return RX_CONTINUE;
2736
2737 if (unlikely(!ieee80211_is_data(fc)))
2738 return RX_CONTINUE;
2739
2740 if (unlikely(!ieee80211_is_data_present(fc)))
2741 return RX_DROP_MONITOR;
2742
2743 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2744 switch (rx->sdata->vif.type) {
2745 case NL80211_IFTYPE_AP_VLAN:
2746 if (!rx->sdata->u.vlan.sta)
2747 return RX_DROP_UNUSABLE;
2748 break;
2749 case NL80211_IFTYPE_STATION:
2750 if (!rx->sdata->u.mgd.use_4addr)
2751 return RX_DROP_UNUSABLE;
2752 break;
2753 default:
2754 return RX_DROP_UNUSABLE;
2755 }
2756 }
2757
2758 if (is_multicast_ether_addr(hdr->addr1))
2759 return RX_DROP_UNUSABLE;
2760
Johannes Berg270032a2021-05-11 20:02:46 +02002761 if (rx->key) {
2762 /*
2763 * We should not receive A-MSDUs on pre-HT connections,
2764 * and HT connections cannot use old ciphers. Thus drop
2765 * them, as in those cases we couldn't even have SPP
2766 * A-MSDUs or such.
2767 */
2768 switch (rx->key->conf.cipher) {
2769 case WLAN_CIPHER_SUITE_WEP40:
2770 case WLAN_CIPHER_SUITE_WEP104:
2771 case WLAN_CIPHER_SUITE_TKIP:
2772 return RX_DROP_UNUSABLE;
2773 default:
2774 break;
2775 }
2776 }
2777
Felix Fietkau24bba072018-02-27 13:03:07 +01002778 return __ieee80211_rx_h_amsdu(rx, 0);
2779}
2780
Ingo Molnarbf94e172008-10-12 23:51:38 -07002781#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002782static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002783ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2784{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002785 struct ieee80211_hdr *fwd_hdr, *hdr;
2786 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002787 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002788 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002789 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002790 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002791 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002792 u16 ac, q, hdrlen;
Felix Fietkau51d0af22019-02-22 13:21:15 +01002793 int tailroom = 0;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002794
2795 hdr = (struct ieee80211_hdr *) skb->data;
2796 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002797
2798 /* make sure fixed part of mesh header is there, also checks skb len */
2799 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2800 return RX_DROP_MONITOR;
2801
2802 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2803
2804 /* make sure full mesh header is there, also checks skb len */
2805 if (!pskb_may_pull(rx->skb,
2806 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2807 return RX_DROP_MONITOR;
2808
2809 /* reload pointers */
2810 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002811 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2812
Bob Copelandd0c22112015-03-02 14:28:52 -05002813 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2814 return RX_DROP_MONITOR;
2815
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002816 /* frame is in RMC, don't forward */
2817 if (ieee80211_is_data(hdr->frame_control) &&
2818 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002819 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002820 return RX_DROP_MONITOR;
2821
Johannes Berg5c900672015-04-22 14:48:34 +02002822 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002823 return RX_CONTINUE;
2824
2825 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002826 return RX_DROP_MONITOR;
2827
Javier Cardona43b7b312009-10-15 18:10:51 -07002828 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002829 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002830 char *proxied_addr;
2831 char *mpp_addr;
2832
2833 if (is_multicast_ether_addr(hdr->addr1)) {
2834 mpp_addr = hdr->addr3;
2835 proxied_addr = mesh_hdr->eaddr1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -07002836 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2837 MESH_FLAGS_AE_A5_A6) {
Johannes Berg9b395bc2012-10-26 00:36:40 +02002838 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002839 mpp_addr = hdr->addr4;
2840 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002841 } else {
2842 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002843 }
YanBo79617de2008-09-22 13:30:32 +08002844
YanBo79617de2008-09-22 13:30:32 +08002845 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002846 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002847 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002848 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002849 } else {
2850 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002851 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002852 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002853 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002854 spin_unlock_bh(&mppath->state_lock);
2855 }
2856 rcu_read_unlock();
2857 }
2858
Javier Cardona3c5772a2009-08-10 12:15:48 -07002859 /* Frame has reached destination. Don't forward */
2860 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002861 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002862 return RX_CONTINUE;
2863
Michal Kaziorcf440122016-01-25 14:43:24 +01002864 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2865 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002866 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002867 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002868 return RX_DROP_MONITOR;
2869 }
2870 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002871
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002872 if (!--mesh_hdr->ttl) {
Bob Copelanda0dc0202019-01-17 16:32:42 -05002873 if (!is_multicast_ether_addr(hdr->addr1))
2874 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2875 dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002876 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002877 }
2878
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002879 if (!ifmsh->mshcfg.dot11MeshForwarding)
2880 goto out;
2881
Felix Fietkau51d0af22019-02-22 13:21:15 +01002882 if (sdata->crypto_tx_tailroom_needed_cnt)
2883 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2884
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002885 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
Felix Fietkau51d0af22019-02-22 13:21:15 +01002886 sdata->encrypt_headroom,
2887 tailroom, GFP_ATOMIC);
Joe Perches0c3d5a92018-03-12 08:07:12 -07002888 if (!fwd_skb)
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002889 goto out;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002890
2891 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002892 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002893 info = IEEE80211_SKB_CB(fwd_skb);
2894 memset(info, 0, sizeof(*info));
Felix Fietkaucc20ff22020-09-08 14:36:57 +02002895 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002896 info->control.vif = &rx->sdata->vif;
2897 info->control.jiffies = jiffies;
2898 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2899 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2900 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002901 /* update power mode indication when forwarding */
2902 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002903 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002904 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002905 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2906 } else {
2907 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002908 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002909 fwd_hdr->addr3, 0,
2910 WLAN_REASON_MESH_PATH_NOFORWARD,
2911 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002912 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002913 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002914 return RX_DROP_MONITOR;
2915 }
2916
2917 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2918 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002919 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002920 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002921 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002922 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002923}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002924#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002925
2926static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002927ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002928{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002929 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302930 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002931 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002932 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2933 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002934 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002935 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002936
Harvey Harrison358c8d92008-07-15 18:44:13 -07002937 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002938 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002939
Harvey Harrison358c8d92008-07-15 18:44:13 -07002940 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002941 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002942
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002943 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002944 * Send unexpected-4addr-frame event to hostapd. For older versions,
2945 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002946 */
2947 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002948 sdata->vif.type == NL80211_IFTYPE_AP) {
2949 if (rx->sta &&
2950 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2951 cfg80211_rx_unexpected_4addr_frame(
2952 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002953 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002954 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002955
Felix Fietkau4114fa22011-04-12 19:15:22 +02002956 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002957 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002958 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002959
Harvey Harrison358c8d92008-07-15 18:44:13 -07002960 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002961 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002962
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002963 /* directly handle TDLS channel switch requests/responses */
2964 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2965 cpu_to_be16(ETH_P_TDLS))) {
2966 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2967
2968 if (pskb_may_pull(rx->skb,
2969 offsetof(struct ieee80211_tdls_data, u)) &&
2970 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2971 tf->category == WLAN_CATEGORY_TDLS &&
2972 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2973 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002974 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2975 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002976 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002977 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002978
2979 return RX_QUEUED;
2980 }
2981 }
2982
Felix Fietkau4114fa22011-04-12 19:15:22 +02002983 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2984 unlikely(port_control) && sdata->bss) {
2985 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2986 u.ap);
2987 dev = sdata->dev;
2988 rx->sdata = sdata;
2989 }
2990
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002991 rx->skb->dev = dev;
2992
Johannes Berg602fae42016-03-17 15:02:52 +02002993 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2994 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302995 !is_multicast_ether_addr(
2996 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2997 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002998 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2999 mod_timer(&local->dynamic_ps_timer, jiffies +
3000 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05303001
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02003002 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02003003
Johannes Berg9ae54c82008-01-31 19:48:20 +01003004 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003005}
3006
Johannes Berg49461622008-06-30 15:10:45 +02003007static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003008ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02003009{
Ron Rindjunsky71364712007-12-25 17:00:36 +02003010 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07003011 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003012 struct tid_ampdu_rx *tid_agg_rx;
3013 u16 start_seq_num;
3014 u16 tid;
3015
Harvey Harrisona7767f92008-07-02 16:30:51 -07003016 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003017 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003018
Harvey Harrisona7767f92008-07-02 16:30:51 -07003019 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02003020 struct {
3021 __le16 control, start_seq_num;
3022 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003023 struct ieee80211_event event = {
3024 .type = BAR_RX_EVENT,
3025 };
Johannes Berg8ae59772010-05-30 14:52:58 +02003026
Ron Rindjunsky71364712007-12-25 17:00:36 +02003027 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01003028 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02003029
3030 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3031 &bar_data, sizeof(bar_data)))
3032 return RX_DROP_MONITOR;
3033
Johannes Berg8ae59772010-05-30 14:52:58 +02003034 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02003035
Johannes Berg53f24972016-08-29 23:25:19 +03003036 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3037 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3038 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3039 WLAN_BACK_RECIPIENT,
3040 WLAN_REASON_QSTA_REQUIRE_SETUP);
3041
Johannes Berga87f7362010-06-10 10:21:38 +02003042 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3043 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01003044 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003045
Johannes Berg8ae59772010-05-30 14:52:58 +02003046 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003047 event.u.ba.tid = tid;
3048 event.u.ba.ssn = start_seq_num;
3049 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003050
3051 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01003052 if (tid_agg_rx->timeout)
3053 mod_timer(&tid_agg_rx->session_timer,
3054 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02003055
Johannes Bergdd318572010-11-29 11:09:16 +01003056 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01003057 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02003058 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003059 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01003060 spin_unlock(&tid_agg_rx->reorder_lock);
3061
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003062 drv_event_callback(rx->local, rx->sdata, &event);
3063
Johannes Berga02ae752009-11-16 12:00:40 +01003064 kfree_skb(skb);
3065 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003066 }
3067
Johannes Berg08daeca2010-05-30 14:53:43 +02003068 /*
3069 * After this point, we only want management frames,
3070 * so we can drop all remaining control frames to
3071 * cooked monitor interfaces.
3072 */
3073 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003074}
3075
Jouni Malinenf4f727a2009-01-10 11:46:53 +02003076static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3077 struct ieee80211_mgmt *mgmt,
3078 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02003079{
3080 struct ieee80211_local *local = sdata->local;
3081 struct sk_buff *skb;
3082 struct ieee80211_mgmt *resp;
3083
Joe Perchesb203ca32012-05-08 18:56:52 +00003084 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02003085 /* Not to own unicast address */
3086 return;
3087 }
3088
Joe Perchesb203ca32012-05-08 18:56:52 +00003089 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3090 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003091 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02003092 return;
3093 }
3094
3095 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3096 /* Too short SA Query request frame */
3097 return;
3098 }
3099
3100 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3101 if (skb == NULL)
3102 return;
3103
3104 skb_reserve(skb, local->hw.extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02003105 resp = skb_put_zero(skb, 24);
Jouni Malinenfea14732009-01-08 13:32:06 +02003106 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01003107 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01003108 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02003109 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3110 IEEE80211_STYPE_ACTION);
3111 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3112 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3113 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3114 memcpy(resp->u.action.u.sa_query.trans_id,
3115 mgmt->u.action.u.sa_query.trans_id,
3116 WLAN_SA_QUERY_TR_ID_LEN);
3117
Johannes Berg62ae67b2009-11-18 18:42:05 +01003118 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02003119}
3120
Johannes Berg49461622008-06-30 15:10:45 +02003121static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003122ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3123{
3124 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003125 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003126
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003127 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3128 return RX_CONTINUE;
3129
Johannes Berg2e161f782010-08-12 15:38:38 +02003130 /*
3131 * From here on, look only at management frames.
3132 * Data and control frames are already handled,
3133 * and unknown (reserved) frames are useless.
3134 */
3135 if (rx->skb->len < 24)
3136 return RX_DROP_MONITOR;
3137
3138 if (!ieee80211_is_mgmt(mgmt->frame_control))
3139 return RX_DROP_MONITOR;
3140
Johannes Bergee971922011-11-04 11:18:18 +01003141 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3142 ieee80211_is_beacon(mgmt->frame_control) &&
3143 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01003144 int sig = 0;
3145
Tosoni1ad22fb2018-03-14 16:58:34 +00003146 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3147 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003148 sig = status->signal;
3149
Thomas Pedersene76fede2020-04-30 10:25:50 -07003150 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3151 rx->skb->data, rx->skb->len,
3152 ieee80211_rx_status_to_khz(status),
3153 sig);
Johannes Bergee971922011-11-04 11:18:18 +01003154 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3155 }
3156
Johannes Berg2e161f782010-08-12 15:38:38 +02003157 if (ieee80211_drop_unencrypted_mgmt(rx))
3158 return RX_DROP_UNUSABLE;
3159
3160 return RX_CONTINUE;
3161}
3162
3163static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02003164ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3165{
3166 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003167 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02003168 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003169 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02003170 int len = rx->skb->len;
3171
3172 if (!ieee80211_is_action(mgmt->frame_control))
3173 return RX_CONTINUE;
3174
Jouni Malinen026331c2010-02-15 12:53:10 +02003175 /* drop too small frames */
3176 if (len < IEEE80211_MIN_ACTION_SIZE)
3177 return RX_DROP_UNUSABLE;
3178
Marco Porsch815b8092012-12-05 15:04:26 -08003179 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003180 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3181 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02003182 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02003183
Johannes Bergde1ede72008-09-09 14:42:50 +02003184 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003185 case WLAN_CATEGORY_HT:
3186 /* reject HT action frames from stations not supporting HT */
3187 if (!rx->sta->sta.ht_cap.ht_supported)
3188 goto invalid;
3189
3190 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3191 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3192 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3193 sdata->vif.type != NL80211_IFTYPE_AP &&
3194 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3195 break;
3196
Johannes Bergec61cd62012-12-28 12:12:10 +01003197 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003198 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3199 goto invalid;
3200
3201 switch (mgmt->u.action.u.ht_smps.action) {
3202 case WLAN_HT_ACTION_SMPS: {
3203 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003204 enum ieee80211_smps_mode smps_mode;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303205 struct sta_opmode_info sta_opmode = {};
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003206
Ilan Peerc4d800d2020-01-31 13:12:53 +02003207 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3208 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3209 goto handled;
3210
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003211 /* convert to HT capability */
3212 switch (mgmt->u.action.u.ht_smps.smps_control) {
3213 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003214 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003215 break;
3216 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003217 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003218 break;
3219 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003220 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003221 break;
3222 default:
3223 goto invalid;
3224 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003225
3226 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01003227 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003228 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003229 rx->sta->sta.smps_mode = smps_mode;
tamizhr@codeaurora.org57566b22018-03-27 19:16:16 +05303230 sta_opmode.smps_mode =
3231 ieee80211_smps_mode_to_smps_mode(smps_mode);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303232 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003233
3234 sband = rx->local->hw.wiphy->bands[status->band];
3235
Johannes Berg64f68e52012-03-28 10:58:37 +02003236 rate_control_rate_update(local, sband, rx->sta,
3237 IEEE80211_RC_SMPS_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303238 cfg80211_sta_opmode_change_notify(sdata->dev,
3239 rx->sta->addr,
3240 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003241 GFP_ATOMIC);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003242 goto handled;
3243 }
Johannes Bergec61cd62012-12-28 12:12:10 +01003244 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3245 struct ieee80211_supported_band *sband;
3246 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003247 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303248 struct sta_opmode_info sta_opmode = {};
Johannes Bergec61cd62012-12-28 12:12:10 +01003249
3250 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003251 if (!(rx->sta->sta.ht_cap.cap &
3252 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01003253 goto handled;
3254
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003255 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003256 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01003257 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003258 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3259
3260 /* set cur_max_bandwidth and recalc sta bw */
3261 rx->sta->cur_max_bandwidth = max_bw;
3262 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003263
3264 if (rx->sta->sta.bandwidth == new_bw)
3265 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01003266
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003267 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01003268 sband = rx->local->hw.wiphy->bands[status->band];
tamizhr@codeaurora.org97f5f422018-03-27 19:16:17 +05303269 sta_opmode.bw =
3270 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303271 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
Johannes Bergec61cd62012-12-28 12:12:10 +01003272
3273 rate_control_rate_update(local, sband, rx->sta,
3274 IEEE80211_RC_BW_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303275 cfg80211_sta_opmode_change_notify(sdata->dev,
3276 rx->sta->addr,
3277 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003278 GFP_ATOMIC);
Johannes Bergec61cd62012-12-28 12:12:10 +01003279 goto handled;
3280 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003281 default:
3282 goto invalid;
3283 }
3284
3285 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003286 case WLAN_CATEGORY_PUBLIC:
3287 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3288 goto invalid;
3289 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3290 break;
3291 if (!rx->sta)
3292 break;
3293 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3294 break;
3295 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3296 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3297 break;
3298 if (len < offsetof(struct ieee80211_mgmt,
3299 u.action.u.ext_chan_switch.variable))
3300 goto invalid;
3301 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003302 case WLAN_CATEGORY_VHT:
3303 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3304 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3305 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3306 sdata->vif.type != NL80211_IFTYPE_AP &&
3307 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3308 break;
3309
3310 /* verify action code is present */
3311 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3312 goto invalid;
3313
3314 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3315 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01003316 /* verify opmode is present */
3317 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3318 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02003319 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003320 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02003321 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3322 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3323 goto invalid;
3324 goto queue;
3325 }
Johannes Berg0af83d32012-12-27 18:55:36 +01003326 default:
3327 break;
3328 }
3329 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003330 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01003331 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07003332 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01003333 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01003334 sdata->vif.type != NL80211_IFTYPE_AP &&
3335 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02003336 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01003337
Jouni Malinen026331c2010-02-15 12:53:10 +02003338 /* verify action_code is present */
3339 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3340 break;
3341
Johannes Bergde1ede72008-09-09 14:42:50 +02003342 switch (mgmt->u.action.u.addba_req.action_code) {
3343 case WLAN_ACTION_ADDBA_REQ:
3344 if (len < (IEEE80211_MIN_ACTION_SIZE +
3345 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003346 goto invalid;
3347 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003348 case WLAN_ACTION_ADDBA_RESP:
3349 if (len < (IEEE80211_MIN_ACTION_SIZE +
3350 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003351 goto invalid;
3352 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003353 case WLAN_ACTION_DELBA:
3354 if (len < (IEEE80211_MIN_ACTION_SIZE +
3355 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003356 goto invalid;
3357 break;
3358 default:
3359 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02003360 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003361
Johannes Berg8b58ff82010-06-10 10:21:51 +02003362 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02003363 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02003364 /* verify action_code is present */
3365 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3366 break;
3367
Johannes Berg39192c02008-09-09 14:49:03 +02003368 switch (mgmt->u.action.u.measurement.action_code) {
3369 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02003370 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003371 break;
3372
Johannes Berg39192c02008-09-09 14:49:03 +02003373 if (len < (IEEE80211_MIN_ACTION_SIZE +
3374 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02003375 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003376
Johannes Bergcc32abd2009-05-15 11:52:31 +02003377 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003378 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02003379
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003380 ieee80211_process_measurement_req(sdata, mgmt, len);
3381 goto handled;
3382 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3383 u8 *bssid;
3384 if (len < (IEEE80211_MIN_ACTION_SIZE +
3385 sizeof(mgmt->u.action.u.chan_switch)))
3386 break;
3387
3388 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003389 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3390 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003391 break;
3392
3393 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3394 bssid = sdata->u.mgd.bssid;
3395 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3396 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003397 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3398 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003399 else
3400 break;
3401
3402 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02003403 break;
Sujithc481ec92009-01-06 09:28:37 +05303404
Johannes Berg8b58ff82010-06-10 10:21:51 +02003405 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003406 }
Johannes Berg39192c02008-09-09 14:49:03 +02003407 }
3408 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07003409 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003410 if (len < (IEEE80211_MIN_ACTION_SIZE +
3411 sizeof(mgmt->u.action.u.self_prot.action_code)))
3412 break;
3413
Thomas Pedersen8db09852011-08-12 20:01:00 -07003414 switch (mgmt->u.action.u.self_prot.action_code) {
3415 case WLAN_SP_MESH_PEERING_OPEN:
3416 case WLAN_SP_MESH_PEERING_CLOSE:
3417 case WLAN_SP_MESH_PEERING_CONFIRM:
3418 if (!ieee80211_vif_is_mesh(&sdata->vif))
3419 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003420 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003421 /* userspace handles this frame */
3422 break;
3423 goto queue;
3424 case WLAN_SP_MGK_INFORM:
3425 case WLAN_SP_MGK_ACK:
3426 if (!ieee80211_vif_is_mesh(&sdata->vif))
3427 goto invalid;
3428 break;
3429 }
3430 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003431 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003432 if (len < (IEEE80211_MIN_ACTION_SIZE +
3433 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3434 break;
3435
Johannes Berg77a121c2010-06-10 10:21:34 +02003436 if (!ieee80211_vif_is_mesh(&sdata->vif))
3437 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003438 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003439 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003440 break;
3441 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02003442 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003443
Johannes Berg2e161f782010-08-12 15:38:38 +02003444 return RX_CONTINUE;
3445
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003446 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003447 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f782010-08-12 15:38:38 +02003448 /* will return in the next handlers */
3449 return RX_CONTINUE;
3450
3451 handled:
3452 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003453 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003454 dev_kfree_skb(rx->skb);
3455 return RX_QUEUED;
3456
3457 queue:
Johannes Berg2e161f782010-08-12 15:38:38 +02003458 skb_queue_tail(&sdata->skb_queue, rx->skb);
3459 ieee80211_queue_work(&local->hw, &sdata->work);
3460 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003461 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003462 return RX_QUEUED;
3463}
3464
3465static ieee80211_rx_result debug_noinline
3466ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3467{
Johannes Berg554891e2010-09-24 12:38:25 +02003468 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003469 int sig = 0;
Johannes Berg2e161f782010-08-12 15:38:38 +02003470
3471 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003472 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f782010-08-12 15:38:38 +02003473 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003474
Jouni Malinen026331c2010-02-15 12:53:10 +02003475 /*
3476 * Getting here means the kernel doesn't know how to handle
3477 * it, but maybe userspace does ... include returned frames
3478 * so userspace can register for those to know whether ones
3479 * it transmitted were processed or returned.
3480 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003481
Tosoni1ad22fb2018-03-14 16:58:34 +00003482 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3483 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003484 sig = status->signal;
3485
Thomas Pedersene76fede2020-04-30 10:25:50 -07003486 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3487 ieee80211_rx_status_to_khz(status), sig,
3488 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f782010-08-12 15:38:38 +02003489 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003490 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003491 dev_kfree_skb(rx->skb);
3492 return RX_QUEUED;
3493 }
3494
Johannes Berg2e161f782010-08-12 15:38:38 +02003495 return RX_CONTINUE;
3496}
3497
3498static ieee80211_rx_result debug_noinline
Johannes Berg1ea02222020-05-26 10:31:33 +02003499ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3500{
3501 struct ieee80211_sub_if_data *sdata = rx->sdata;
3502 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3503 int len = rx->skb->len;
3504
3505 if (!ieee80211_is_action(mgmt->frame_control))
3506 return RX_CONTINUE;
3507
3508 switch (mgmt->u.action.category) {
3509 case WLAN_CATEGORY_SA_QUERY:
3510 if (len < (IEEE80211_MIN_ACTION_SIZE +
3511 sizeof(mgmt->u.action.u.sa_query)))
3512 break;
3513
3514 switch (mgmt->u.action.u.sa_query.action) {
3515 case WLAN_ACTION_SA_QUERY_REQUEST:
3516 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3517 break;
3518 ieee80211_process_sa_query_req(sdata, mgmt, len);
3519 goto handled;
3520 }
3521 break;
3522 }
3523
3524 return RX_CONTINUE;
3525
3526 handled:
3527 if (rx->sta)
3528 rx->sta->rx_stats.packets++;
3529 dev_kfree_skb(rx->skb);
3530 return RX_QUEUED;
3531}
3532
3533static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003534ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3535{
3536 struct ieee80211_local *local = rx->local;
3537 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3538 struct sk_buff *nskb;
3539 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003540 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003541
3542 if (!ieee80211_is_action(mgmt->frame_control))
3543 return RX_CONTINUE;
3544
3545 /*
3546 * For AP mode, hostapd is responsible for handling any action
3547 * frames that we didn't handle, including returning unknown
3548 * ones. For all other modes we will return them to the sender,
3549 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003550 * 802.11-2012 9.24.4.
Johannes Berg2e161f782010-08-12 15:38:38 +02003551 * Newer versions of hostapd shall also use the management frame
3552 * registration mechanisms, but older ones still use cooked
3553 * monitor interfaces so push all frames there.
3554 */
Johannes Berg554891e2010-09-24 12:38:25 +02003555 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f782010-08-12 15:38:38 +02003556 (sdata->vif.type == NL80211_IFTYPE_AP ||
3557 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3558 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003559
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003560 if (is_multicast_ether_addr(mgmt->da))
3561 return RX_DROP_MONITOR;
3562
Johannes Berg84040802010-02-15 12:46:39 +02003563 /* do not return rejected action frames */
3564 if (mgmt->u.action.category & 0x80)
3565 return RX_DROP_UNUSABLE;
3566
3567 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3568 GFP_ATOMIC);
3569 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003570 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003571
John W. Linville292b4df2010-06-24 11:13:56 -04003572 nmgmt->u.action.category |= 0x80;
3573 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3574 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003575
3576 memset(nskb->cb, 0, sizeof(nskb->cb));
3577
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003578 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3579 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3580
3581 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3582 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3583 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003584 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003585 info->hw_queue =
3586 local->hw.offchannel_tx_hw_queue;
3587 }
3588
3589 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04003590 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003591 }
Johannes Berg39192c02008-09-09 14:49:03 +02003592 dev_kfree_skb(rx->skb);
3593 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003594}
3595
3596static ieee80211_rx_result debug_noinline
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003597ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3598{
3599 struct ieee80211_sub_if_data *sdata = rx->sdata;
3600 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3601
3602 if (!ieee80211_is_ext(hdr->frame_control))
3603 return RX_CONTINUE;
3604
3605 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3606 return RX_DROP_MONITOR;
3607
3608 /* for now only beacons are ext, so queue them */
3609 skb_queue_tail(&sdata->skb_queue, rx->skb);
3610 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3611 if (rx->sta)
3612 rx->sta->rx_stats.packets++;
3613
3614 return RX_QUEUED;
3615}
3616
3617static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003618ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003619{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003620 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003621 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3622 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003623
Johannes Berg77a121c2010-06-10 10:21:34 +02003624 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003625
Johannes Berg77a121c2010-06-10 10:21:34 +02003626 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3627 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003628 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003629 sdata->vif.type != NL80211_IFTYPE_STATION)
3630 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003631
Johannes Berg77a121c2010-06-10 10:21:34 +02003632 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003633 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003634 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3635 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3636 /* process for all: mesh, mlme, ibss */
3637 break;
Johannes Berg95697f92019-10-04 15:37:05 +03003638 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3639 if (is_multicast_ether_addr(mgmt->da) &&
3640 !is_broadcast_ether_addr(mgmt->da))
3641 return RX_DROP_MONITOR;
3642
3643 /* process only for station/IBSS */
3644 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3645 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3646 return RX_DROP_MONITOR;
3647 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003648 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3649 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003650 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003651 if (is_multicast_ether_addr(mgmt->da) &&
3652 !is_broadcast_ether_addr(mgmt->da))
3653 return RX_DROP_MONITOR;
3654
Johannes Berg77a121c2010-06-10 10:21:34 +02003655 /* process only for station */
3656 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3657 return RX_DROP_MONITOR;
3658 break;
3659 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003660 /* process only for ibss and mesh */
3661 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3662 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003663 return RX_DROP_MONITOR;
3664 break;
3665 default:
3666 return RX_DROP_MONITOR;
3667 }
Johannes Berg46900292009-02-15 12:44:28 +01003668
Johannes Berg77a121c2010-06-10 10:21:34 +02003669 /* queue up frame and kick off work to process it */
3670 skb_queue_tail(&sdata->skb_queue, rx->skb);
3671 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003672 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003673 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003674
3675 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003676}
3677
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003678static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3679 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003680{
3681 struct ieee80211_sub_if_data *sdata;
3682 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003683 struct sk_buff *skb = rx->skb, *skb2;
3684 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003685 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003686 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003687
Johannes Berg554891e2010-09-24 12:38:25 +02003688 /*
3689 * If cooked monitor has been processed already, then
3690 * don't do it again. If not, set the flag.
3691 */
3692 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003693 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003694 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003695
Johannes Berg152c4772011-10-21 10:22:22 +02003696 /* If there are no cooked monitor interfaces, just free the SKB */
3697 if (!local->cooked_mntrs)
3698 goto out_free_skb;
3699
Johannes Berg1f7bba72014-11-06 22:56:36 +01003700 /* vendor data is long removed here */
3701 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003702 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003703 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003704
3705 if (skb_headroom(skb) < needed_headroom &&
3706 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003707 goto out_free_skb;
3708
Johannes Berg293702a2012-03-02 13:18:19 +01003709 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003710 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3711 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003712
Zhang Shengjud57a5442016-03-03 01:16:56 +00003713 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003714 skb->ip_summed = CHECKSUM_UNNECESSARY;
3715 skb->pkt_type = PACKET_OTHERHOST;
3716 skb->protocol = htons(ETH_P_802_2);
3717
3718 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01003719 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003720 continue;
3721
Johannes Berg05c914f2008-09-11 00:01:58 +02003722 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003723 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003724 continue;
3725
3726 if (prev_dev) {
3727 skb2 = skb_clone(skb, GFP_ATOMIC);
3728 if (skb2) {
3729 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003730 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003731 }
3732 }
3733
3734 prev_dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +02003735 dev_sw_netstats_rx_add(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003736 }
3737
3738 if (prev_dev) {
3739 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003740 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003741 return;
3742 }
Michael Wu3d30d942008-01-31 19:48:27 +01003743
3744 out_free_skb:
3745 dev_kfree_skb(skb);
3746}
3747
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003748static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3749 ieee80211_rx_result res)
3750{
3751 switch (res) {
3752 case RX_DROP_MONITOR:
3753 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3754 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003755 rx->sta->rx_stats.dropped++;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003756 fallthrough;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003757 case RX_CONTINUE: {
3758 struct ieee80211_rate *rate = NULL;
3759 struct ieee80211_supported_band *sband;
3760 struct ieee80211_rx_status *status;
3761
3762 status = IEEE80211_SKB_RXCB((rx->skb));
3763
3764 sband = rx->local->hw.wiphy->bands[status->band];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003765 if (status->encoding == RX_ENC_LEGACY)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003766 rate = &sband->bitrates[status->rate_idx];
3767
3768 ieee80211_rx_cooked_monitor(rx, rate);
3769 break;
3770 }
3771 case RX_DROP_UNUSABLE:
3772 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3773 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003774 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003775 dev_kfree_skb(rx->skb);
3776 break;
3777 case RX_QUEUED:
3778 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3779 break;
3780 }
3781}
3782
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003783static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3784 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003785{
3786 ieee80211_rx_result res = RX_DROP_MONITOR;
3787 struct sk_buff *skb;
3788
3789#define CALL_RXH(rxh) \
3790 do { \
3791 res = rxh(rx); \
3792 if (res != RX_CONTINUE) \
3793 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003794 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003795
Johannes Berg45ceeee2015-03-16 09:08:20 +01003796 /* Lock here to avoid hitting all of the data used in the RX
3797 * path (e.g. key data, station data, ...) concurrently when
3798 * a frame is released from the reorder buffer due to timeout
3799 * from the timer, potentially concurrently with RX from the
3800 * driver.
3801 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003802 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003803
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003804 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003805 /*
3806 * all the other fields are valid across frames
3807 * that belong to an aMPDU since they are on the
3808 * same TID from the same station
3809 */
3810 rx->skb = skb;
3811
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003812 CALL_RXH(ieee80211_rx_h_check_more_data);
3813 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3814 CALL_RXH(ieee80211_rx_h_sta_process);
3815 CALL_RXH(ieee80211_rx_h_decrypt);
3816 CALL_RXH(ieee80211_rx_h_defragment);
3817 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003818 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003819#ifdef CONFIG_MAC80211_MESH
3820 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3821 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3822#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003823 CALL_RXH(ieee80211_rx_h_amsdu);
3824 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003825
3826 /* special treatment -- needs the queue */
3827 res = ieee80211_rx_h_ctrl(rx, frames);
3828 if (res != RX_CONTINUE)
3829 goto rxh_next;
3830
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003831 CALL_RXH(ieee80211_rx_h_mgmt_check);
3832 CALL_RXH(ieee80211_rx_h_action);
3833 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
Johannes Berg1ea02222020-05-26 10:31:33 +02003834 CALL_RXH(ieee80211_rx_h_action_post_userspace);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003835 CALL_RXH(ieee80211_rx_h_action_return);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003836 CALL_RXH(ieee80211_rx_h_ext);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003837 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003838
3839 rxh_next:
3840 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003841
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003842#undef CALL_RXH
3843 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003844
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003845 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003846}
Johannes Berg571ecf62007-07-27 15:43:22 +02003847
Johannes Berg4406c372010-09-24 11:21:06 +02003848static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003849{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003850 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003851 ieee80211_rx_result res = RX_DROP_MONITOR;
3852
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003853 __skb_queue_head_init(&reorder_release);
3854
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003855#define CALL_RXH(rxh) \
3856 do { \
3857 res = rxh(rx); \
3858 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003859 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003860 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003861
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003862 CALL_RXH(ieee80211_rx_h_check_dup);
3863 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003864
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003865 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003866
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003867 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003868 return;
Johannes Berg49461622008-06-30 15:10:45 +02003869
Johannes Berg2569a822009-11-25 17:46:17 +01003870 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003871 ieee80211_rx_handlers_result(rx, res);
3872
3873#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003874}
3875
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003876/*
Johannes Bergdd318572010-11-29 11:09:16 +01003877 * This function makes calls into the RX path, therefore
3878 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003879 */
3880void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3881{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003882 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003883 struct ieee80211_rx_data rx = {
3884 .sta = sta,
3885 .sdata = sta->sdata,
3886 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003887 /* This is OK -- must be QoS data frame */
3888 .security_idx = tid,
3889 .seqno_idx = tid,
Johannes Berg554891e2010-09-24 12:38:25 +02003890 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003891 struct tid_ampdu_rx *tid_agg_rx;
3892
3893 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3894 if (!tid_agg_rx)
3895 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003896
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003897 __skb_queue_head_init(&frames);
3898
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003899 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003900 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003901 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003902
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003903 if (!skb_queue_empty(&frames)) {
3904 struct ieee80211_event event = {
3905 .type = BA_FRAME_TIMEOUT,
3906 .u.ba.tid = tid,
3907 .u.ba.sta = &sta->sta,
3908 };
3909 drv_event_callback(rx.local, rx.sdata, &event);
3910 }
3911
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003912 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003913}
3914
Sara Sharon06470f72016-01-28 16:19:25 +02003915void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3916 u16 ssn, u64 filtered,
3917 u16 received_mpdus)
3918{
3919 struct sta_info *sta;
3920 struct tid_ampdu_rx *tid_agg_rx;
3921 struct sk_buff_head frames;
3922 struct ieee80211_rx_data rx = {
3923 /* This is OK -- must be QoS data frame */
3924 .security_idx = tid,
3925 .seqno_idx = tid,
3926 };
3927 int i, diff;
3928
3929 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3930 return;
3931
3932 __skb_queue_head_init(&frames);
3933
3934 sta = container_of(pubsta, struct sta_info, sta);
3935
3936 rx.sta = sta;
3937 rx.sdata = sta->sdata;
3938 rx.local = sta->local;
3939
3940 rcu_read_lock();
3941 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3942 if (!tid_agg_rx)
3943 goto out;
3944
3945 spin_lock_bh(&tid_agg_rx->reorder_lock);
3946
3947 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3948 int release;
3949
3950 /* release all frames in the reorder buffer */
3951 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3952 IEEE80211_SN_MODULO;
3953 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3954 release, &frames);
3955 /* update ssn to match received ssn */
3956 tid_agg_rx->head_seq_num = ssn;
3957 } else {
3958 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3959 &frames);
3960 }
3961
3962 /* handle the case that received ssn is behind the mac ssn.
3963 * it can be tid_agg_rx->buf_size behind and still be valid */
3964 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3965 if (diff >= tid_agg_rx->buf_size) {
3966 tid_agg_rx->reorder_buf_filtered = 0;
3967 goto release;
3968 }
3969 filtered = filtered >> diff;
3970 ssn += diff;
3971
3972 /* update bitmap */
3973 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3974 int index = (ssn + i) % tid_agg_rx->buf_size;
3975
3976 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3977 if (filtered & BIT_ULL(i))
3978 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3979 }
3980
3981 /* now process also frames that the filter marking released */
3982 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3983
3984release:
3985 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3986
3987 ieee80211_rx_handlers(&rx, &frames);
3988
3989 out:
3990 rcu_read_unlock();
3991}
3992EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3993
Johannes Berg571ecf62007-07-27 15:43:22 +02003994/* main receive path */
3995
Johannes Berga58fbe12015-04-22 15:08:39 +02003996static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003997{
Johannes Berg20b01f82010-09-24 11:21:05 +02003998 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003999 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02004000 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004001 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4002 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004003 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4004 ieee80211_is_s1g_beacon(hdr->frame_control);
Johannes Berg23a24de2007-07-27 15:43:22 +02004005
Johannes Berg51fb61e2007-12-19 01:31:27 +01004006 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02004007 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01004008 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01004009 return false;
Johannes Berg588f7d32019-02-13 15:13:30 +01004010 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4011 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004012 if (multicast)
4013 return true;
4014 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004015 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02004016 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01004017 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02004018 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4019 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004020 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004021 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004022 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02004023 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004024 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004025 if (!multicast &&
4026 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004027 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004028 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004029 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004030 if (status->encoding != RX_ENC_LEGACY)
Johannes Berg56146182012-11-09 15:07:02 +01004031 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004032 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004033 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01004034 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4035 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004036 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004037 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004038 case NL80211_IFTYPE_OCB:
4039 if (!bssid)
4040 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02004041 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004042 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004043 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004044 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004045 if (!multicast &&
4046 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004047 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004048 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004049 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004050 if (status->encoding != RX_ENC_LEGACY)
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004051 rate_idx = 0; /* TODO: HT rates */
4052 else
4053 rate_idx = status->rate_idx;
4054 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4055 BIT(rate_idx));
4056 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004057 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004058 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg736a80b2018-01-04 15:51:53 +01004059 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4060 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004061 if (multicast)
4062 return true;
4063 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004064 case NL80211_IFTYPE_AP_VLAN:
4065 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02004066 if (!bssid)
4067 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4068
4069 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01004070 /*
4071 * Accept public action frames even when the
4072 * BSSID doesn't match, this is used for P2P
4073 * and location updates. Note that mac80211
4074 * itself never looks at these frames.
4075 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02004076 if (!multicast &&
4077 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004078 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02004079 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004080 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02004081 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02004082 }
4083
4084 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004085 /* ignore data frames to TDLS-peers */
4086 if (ieee80211_is_data(hdr->frame_control))
4087 return false;
4088 /* ignore action frames to TDLS-peers */
4089 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02004090 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004091 !ether_addr_equal(bssid, hdr->addr1))
4092 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004093 }
Johannes Berg3018e942017-04-20 21:32:16 +02004094
4095 /*
4096 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4097 * the BSSID - we've checked that already but may have accepted
4098 * the wildcard (ff:ff:ff:ff:ff:ff).
4099 *
4100 * It also says:
4101 * The BSSID of the Data frame is determined as follows:
4102 * a) If the STA is contained within an AP or is associated
4103 * with an AP, the BSSID is the address currently in use
4104 * by the STA contained in the AP.
4105 *
4106 * So we should not accept data frames with an address that's
4107 * multicast.
4108 *
4109 * Accepting it also opens a security problem because stations
4110 * could encrypt it with the GTK and inject traffic that way.
4111 */
4112 if (ieee80211_is_data(hdr->frame_control) && multicast)
4113 return false;
4114
Johannes Berga58fbe12015-04-22 15:08:39 +02004115 return true;
Johannes Bergf142c6b2012-06-18 20:07:15 +02004116 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02004117 return ieee80211_is_public_action(hdr, skb->len) ||
4118 ieee80211_is_probe_req(hdr->frame_control) ||
4119 ieee80211_is_probe_resp(hdr->frame_control) ||
4120 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03004121 case NL80211_IFTYPE_NAN:
4122 /* Currently no frames on NAN interface are allowed */
4123 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02004124 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02004125 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02004126 }
4127
Johannes Berga58fbe12015-04-22 15:08:39 +02004128 WARN_ON_ONCE(1);
4129 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004130}
4131
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004132void ieee80211_check_fast_rx(struct sta_info *sta)
4133{
4134 struct ieee80211_sub_if_data *sdata = sta->sdata;
4135 struct ieee80211_local *local = sdata->local;
4136 struct ieee80211_key *key;
4137 struct ieee80211_fast_rx fastrx = {
4138 .dev = sdata->dev,
4139 .vif_type = sdata->vif.type,
4140 .control_port_protocol = sdata->control_port_protocol,
4141 }, *old, *new = NULL;
Felix Fietkau80a915e2020-12-18 19:47:18 +01004142 bool set_offload = false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004143 bool assign = false;
Felix Fietkau80a915e2020-12-18 19:47:18 +01004144 bool offload;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004145
4146 /* use sparse to check that we don't return without updating */
4147 __acquire(check_fast_rx);
4148
4149 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4150 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4151 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4152 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4153
Johannes Bergc9c59622016-03-31 20:02:11 +03004154 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4155
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004156 /* fast-rx doesn't do reordering */
4157 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4158 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4159 goto clear;
4160
4161 switch (sdata->vif.type) {
4162 case NL80211_IFTYPE_STATION:
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004163 if (sta->sta.tdls) {
4164 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4165 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4166 fastrx.expected_ds_bits = 0;
4167 } else {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004168 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4169 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4170 fastrx.expected_ds_bits =
4171 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4172 }
Felix Fietkau1d870162018-02-23 10:06:05 +01004173
Felix Fietkau9251a472018-02-23 13:55:50 +01004174 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4175 fastrx.expected_ds_bits |=
4176 cpu_to_le16(IEEE80211_FCTL_TODS);
4177 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4178 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4179 }
4180
Felix Fietkau1d870162018-02-23 10:06:05 +01004181 if (!sdata->u.mgd.powersave)
4182 break;
4183
4184 /* software powersave is a huge mess, avoid all of it */
4185 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4186 goto clear;
4187 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4188 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4189 goto clear;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004190 break;
4191 case NL80211_IFTYPE_AP_VLAN:
4192 case NL80211_IFTYPE_AP:
4193 /* parallel-rx requires this, at least with calls to
4194 * ieee80211_sta_ps_transition()
4195 */
4196 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4197 goto clear;
4198 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4199 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4200 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4201
4202 fastrx.internal_forward =
4203 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4204 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4205 !sdata->u.vlan.sta);
Felix Fietkau59cae5b2018-02-23 10:06:04 +01004206
4207 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4208 sdata->u.vlan.sta) {
4209 fastrx.expected_ds_bits |=
4210 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4211 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4212 fastrx.internal_forward = 0;
4213 }
4214
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004215 break;
4216 default:
4217 goto clear;
4218 }
4219
4220 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4221 goto clear;
4222
4223 rcu_read_lock();
4224 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
Felix Fietkau622d3b42020-12-18 19:47:17 +01004225 if (!key)
4226 key = rcu_dereference(sdata->default_unicast_key);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004227 if (key) {
4228 switch (key->conf.cipher) {
4229 case WLAN_CIPHER_SUITE_TKIP:
4230 /* we don't want to deal with MMIC in fast-rx */
4231 goto clear_rcu;
4232 case WLAN_CIPHER_SUITE_CCMP:
4233 case WLAN_CIPHER_SUITE_CCMP_256:
4234 case WLAN_CIPHER_SUITE_GCMP:
4235 case WLAN_CIPHER_SUITE_GCMP_256:
4236 break;
4237 default:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01004238 /* We also don't want to deal with
4239 * WEP or cipher scheme.
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004240 */
4241 goto clear_rcu;
4242 }
4243
4244 fastrx.key = true;
4245 fastrx.icv_len = key->conf.icv_len;
4246 }
4247
4248 assign = true;
4249 clear_rcu:
4250 rcu_read_unlock();
4251 clear:
4252 __release(check_fast_rx);
4253
4254 if (assign)
4255 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4256
Felix Fietkau80a915e2020-12-18 19:47:18 +01004257 offload = assign &&
4258 (sdata->vif.offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED);
4259
4260 if (offload)
4261 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4262 else
4263 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4264
4265 if (set_offload)
4266 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4267
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004268 spin_lock_bh(&sta->lock);
4269 old = rcu_dereference_protected(sta->fast_rx, true);
4270 rcu_assign_pointer(sta->fast_rx, new);
4271 spin_unlock_bh(&sta->lock);
4272
4273 if (old)
4274 kfree_rcu(old, rcu_head);
4275}
4276
4277void ieee80211_clear_fast_rx(struct sta_info *sta)
4278{
4279 struct ieee80211_fast_rx *old;
4280
4281 spin_lock_bh(&sta->lock);
4282 old = rcu_dereference_protected(sta->fast_rx, true);
4283 RCU_INIT_POINTER(sta->fast_rx, NULL);
4284 spin_unlock_bh(&sta->lock);
4285
4286 if (old)
4287 kfree_rcu(old, rcu_head);
4288}
4289
4290void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4291{
4292 struct ieee80211_local *local = sdata->local;
4293 struct sta_info *sta;
4294
4295 lockdep_assert_held(&local->sta_mtx);
4296
Madhuparna Bhowmik253216f2020-02-23 20:03:02 +05304297 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004298 if (sdata != sta->sdata &&
4299 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4300 continue;
4301 ieee80211_check_fast_rx(sta);
4302 }
4303}
4304
4305void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4306{
4307 struct ieee80211_local *local = sdata->local;
4308
4309 mutex_lock(&local->sta_mtx);
4310 __ieee80211_check_fast_rx_iface(sdata);
4311 mutex_unlock(&local->sta_mtx);
4312}
4313
Felix Fietkau80a915e2020-12-18 19:47:18 +01004314static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4315 struct ieee80211_fast_rx *fast_rx,
4316 int orig_len)
4317{
4318 struct ieee80211_sta_rx_stats *stats;
4319 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4320 struct sta_info *sta = rx->sta;
4321 struct sk_buff *skb = rx->skb;
4322 void *sa = skb->data + ETH_ALEN;
4323 void *da = skb->data;
4324
4325 stats = &sta->rx_stats;
4326 if (fast_rx->uses_rss)
4327 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4328
4329 /* statistics part of ieee80211_rx_h_sta_process() */
4330 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4331 stats->last_signal = status->signal;
4332 if (!fast_rx->uses_rss)
4333 ewma_signal_add(&sta->rx_stats_avg.signal,
4334 -status->signal);
4335 }
4336
4337 if (status->chains) {
4338 int i;
4339
4340 stats->chains = status->chains;
4341 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4342 int signal = status->chain_signal[i];
4343
4344 if (!(status->chains & BIT(i)))
4345 continue;
4346
4347 stats->chain_signal_last[i] = signal;
4348 if (!fast_rx->uses_rss)
4349 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4350 -signal);
4351 }
4352 }
4353 /* end of statistics */
4354
4355 stats->last_rx = jiffies;
4356 stats->last_rate = sta_stats_encode_rate(status);
4357
4358 stats->fragments++;
4359 stats->packets++;
4360
4361 skb->dev = fast_rx->dev;
4362
4363 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4364
4365 /* The seqno index has the same property as needed
4366 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4367 * for non-QoS-data frames. Here we know it's a data
4368 * frame, so count MSDUs.
4369 */
4370 u64_stats_update_begin(&stats->syncp);
4371 stats->msdu[rx->seqno_idx]++;
4372 stats->bytes += orig_len;
4373 u64_stats_update_end(&stats->syncp);
4374
4375 if (fast_rx->internal_forward) {
4376 struct sk_buff *xmit_skb = NULL;
4377 if (is_multicast_ether_addr(da)) {
4378 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4379 } else if (!ether_addr_equal(da, sa) &&
4380 sta_info_get(rx->sdata, da)) {
4381 xmit_skb = skb;
4382 skb = NULL;
4383 }
4384
4385 if (xmit_skb) {
4386 /*
4387 * Send to wireless media and increase priority by 256
4388 * to keep the received priority instead of
4389 * reclassifying the frame (see cfg80211_classify8021d).
4390 */
4391 xmit_skb->priority += 256;
4392 xmit_skb->protocol = htons(ETH_P_802_3);
4393 skb_reset_network_header(xmit_skb);
4394 skb_reset_mac_header(xmit_skb);
4395 dev_queue_xmit(xmit_skb);
4396 }
4397
4398 if (!skb)
4399 return;
4400 }
4401
4402 /* deliver to local stack */
4403 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4404 memset(skb->cb, 0, sizeof(skb->cb));
4405 if (rx->list)
4406 list_add_tail(&skb->list, rx->list);
4407 else
4408 netif_receive_skb(skb);
4409
4410}
4411
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004412static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4413 struct ieee80211_fast_rx *fast_rx)
4414{
4415 struct sk_buff *skb = rx->skb;
4416 struct ieee80211_hdr *hdr = (void *)skb->data;
4417 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4418 struct sta_info *sta = rx->sta;
4419 int orig_len = skb->len;
Felix Fietkau24bba072018-02-27 13:03:07 +01004420 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4421 int snap_offs = hdrlen;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004422 struct {
4423 u8 snap[sizeof(rfc1042_header)];
4424 __be16 proto;
4425 } *payload __aligned(2);
4426 struct {
4427 u8 da[ETH_ALEN];
4428 u8 sa[ETH_ALEN];
4429 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03004430 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4431
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004432 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4433 * to a common data structure; drivers can implement that per queue
4434 * but we don't have that information in mac80211
4435 */
4436 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4437 return false;
4438
4439#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4440
4441 /* If using encryption, we also need to have:
4442 * - PN_VALIDATED: similar, but the implementation is tricky
4443 * - DECRYPTED: necessary for PN_VALIDATED
4444 */
4445 if (fast_rx->key &&
4446 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4447 return false;
4448
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004449 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4450 return false;
4451
4452 if (unlikely(ieee80211_is_frag(hdr)))
4453 return false;
4454
4455 /* Since our interface address cannot be multicast, this
4456 * implicitly also rejects multicast frames without the
4457 * explicit check.
4458 *
4459 * We shouldn't get any *data* frames not addressed to us
4460 * (AP mode will accept multicast *management* frames), but
4461 * punting here will make it go through the full checks in
4462 * ieee80211_accept_frame().
4463 */
4464 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4465 return false;
4466
4467 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4468 IEEE80211_FCTL_TODS)) !=
4469 fast_rx->expected_ds_bits)
Felix Fietkaub323ac12018-02-23 10:06:03 +01004470 return false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004471
4472 /* assign the key to drop unencrypted frames (later)
4473 * and strip the IV/MIC if necessary
4474 */
4475 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4476 /* GCMP header length is the same */
4477 snap_offs += IEEE80211_CCMP_HDR_LEN;
4478 }
4479
Felix Fietkau24bba072018-02-27 13:03:07 +01004480 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4481 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4482 goto drop;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004483
Felix Fietkau24bba072018-02-27 13:03:07 +01004484 payload = (void *)(skb->data + snap_offs);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004485
Felix Fietkau24bba072018-02-27 13:03:07 +01004486 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4487 return false;
4488
4489 /* Don't handle these here since they require special code.
4490 * Accept AARP and IPX even though they should come with a
4491 * bridge-tunnel header - but if we get them this way then
4492 * there's little point in discarding them.
4493 */
4494 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4495 payload->proto == fast_rx->control_port_protocol))
4496 return false;
4497 }
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004498
4499 /* after this point, don't punt to the slowpath! */
4500
4501 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4502 pskb_trim(skb, skb->len - fast_rx->icv_len))
4503 goto drop;
4504
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004505 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4506 goto drop;
4507
Felix Fietkau24bba072018-02-27 13:03:07 +01004508 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4509 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4510 RX_QUEUED)
4511 goto drop;
4512
4513 return true;
4514 }
4515
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004516 /* do the header conversion - first grab the addresses */
4517 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4518 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4519 /* remove the SNAP but leave the ethertype */
4520 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4521 /* push the addresses in front */
4522 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4523
Felix Fietkau80a915e2020-12-18 19:47:18 +01004524 ieee80211_rx_8023(rx, fast_rx, orig_len);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004525
4526 return true;
4527 drop:
4528 dev_kfree_skb(skb);
Felix Fietkau80a915e2020-12-18 19:47:18 +01004529 if (fast_rx->uses_rss)
4530 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4531
Johannes Bergc9c59622016-03-31 20:02:11 +03004532 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004533 return true;
4534}
4535
Johannes Berg571ecf62007-07-27 15:43:22 +02004536/*
Johannes Berg4406c372010-09-24 11:21:06 +02004537 * This function returns whether or not the SKB
4538 * was destined for RX processing or not, which,
4539 * if consume is true, is equivalent to whether
4540 * or not the skb was consumed.
4541 */
4542static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4543 struct sk_buff *skb, bool consume)
4544{
4545 struct ieee80211_local *local = rx->local;
4546 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004547
4548 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004549
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004550 /* See if we can do fast-rx; if we have to copy we already lost,
4551 * so punt in that case. We should never have to deliver a data
4552 * frame to multiple interfaces anyway.
4553 *
4554 * We skip the ieee80211_accept_frame() call and do the necessary
4555 * checking inside ieee80211_invoke_fast_rx().
4556 */
4557 if (consume && rx->sta) {
4558 struct ieee80211_fast_rx *fast_rx;
4559
4560 fast_rx = rcu_dereference(rx->sta->fast_rx);
4561 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4562 return true;
4563 }
4564
Johannes Berga58fbe12015-04-22 15:08:39 +02004565 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004566 return false;
4567
Johannes Berg4406c372010-09-24 11:21:06 +02004568 if (!consume) {
4569 skb = skb_copy(skb, GFP_ATOMIC);
4570 if (!skb) {
4571 if (net_ratelimit())
4572 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004573 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004574 sdata->name);
4575 return true;
4576 }
4577
4578 rx->skb = skb;
4579 }
4580
4581 ieee80211_invoke_rx_handlers(rx);
4582 return true;
4583}
4584
Felix Fietkau80a915e2020-12-18 19:47:18 +01004585static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
4586 struct ieee80211_sta *pubsta,
4587 struct sk_buff *skb,
4588 struct list_head *list)
4589{
4590 struct ieee80211_local *local = hw_to_local(hw);
4591 struct ieee80211_fast_rx *fast_rx;
4592 struct ieee80211_rx_data rx;
4593
4594 memset(&rx, 0, sizeof(rx));
4595 rx.skb = skb;
4596 rx.local = local;
4597 rx.list = list;
4598
4599 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4600
4601 /* drop frame if too short for header */
4602 if (skb->len < sizeof(struct ethhdr))
4603 goto drop;
4604
4605 if (!pubsta)
4606 goto drop;
4607
4608 rx.sta = container_of(pubsta, struct sta_info, sta);
4609 rx.sdata = rx.sta->sdata;
4610
4611 fast_rx = rcu_dereference(rx.sta->fast_rx);
4612 if (!fast_rx)
4613 goto drop;
4614
4615 ieee80211_rx_8023(&rx, fast_rx, skb->len);
4616 return;
4617
4618drop:
4619 dev_kfree_skb(skb);
4620}
4621
Johannes Berg4406c372010-09-24 11:21:06 +02004622/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004623 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004624 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004625 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004626static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004627 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004628 struct sk_buff *skb,
Felix Fietkauc5d16862020-07-26 13:06:11 +02004629 struct list_head *list)
Johannes Berg571ecf62007-07-27 15:43:22 +02004630{
4631 struct ieee80211_local *local = hw_to_local(hw);
4632 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004633 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004634 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004635 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004636 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004637 struct rhlist_head *tmp;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004638 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004639
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004640 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004641 memset(&rx, 0, sizeof(rx));
4642 rx.skb = skb;
4643 rx.local = local;
Felix Fietkauc5d16862020-07-26 13:06:11 +02004644 rx.list = list;
Johannes Berg72abd812007-09-17 01:29:22 -04004645
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004646 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004647 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004648
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004649 if (ieee80211_is_mgmt(fc)) {
4650 /* drop frame if too short for header */
4651 if (skb->len < ieee80211_hdrlen(fc))
4652 err = -ENOBUFS;
4653 else
4654 err = skb_linearize(skb);
4655 } else {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004656 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004657 }
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004658
4659 if (err) {
4660 dev_kfree_skb(skb);
4661 return;
4662 }
4663
4664 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004665 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004666 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004667
Johannes Bergd48b2962012-07-06 22:19:27 +02004668 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
Thomas Pedersencd418ba2020-09-21 19:28:08 -07004669 ieee80211_is_beacon(hdr->frame_control) ||
4670 ieee80211_is_s1g_beacon(hdr->frame_control)))
Johannes Bergd48b2962012-07-06 22:19:27 +02004671 ieee80211_scan_rx(local, skb);
4672
Johannes Berg19d19e92017-02-27 09:38:11 +01004673 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004674 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004675
Johannes Berg19d19e92017-02-27 09:38:11 +01004676 if (pubsta) {
4677 rx.sta = container_of(pubsta, struct sta_info, sta);
4678 rx.sdata = rx.sta->sdata;
4679 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4680 return;
4681 goto out;
4682 }
4683
Ben Greear56af3262010-09-23 10:22:24 -07004684 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004685
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004686 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004687 if (!prev_sta) {
4688 prev_sta = sta;
4689 continue;
4690 }
4691
4692 rx.sta = prev_sta;
4693 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004694 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004695
Ben Greear56af3262010-09-23 10:22:24 -07004696 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004697 }
Ben Greear56af3262010-09-23 10:22:24 -07004698
4699 if (prev_sta) {
4700 rx.sta = prev_sta;
4701 rx.sdata = prev_sta->sdata;
4702
Johannes Berg4406c372010-09-24 11:21:06 +02004703 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004704 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304705 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004706 }
Johannes Berg4406c372010-09-24 11:21:06 +02004707 }
4708
Johannes Berg4b0dd982010-09-24 11:21:07 +02004709 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004710
Johannes Berg4b0dd982010-09-24 11:21:07 +02004711 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4712 if (!ieee80211_sdata_running(sdata))
4713 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004714
Johannes Berg4b0dd982010-09-24 11:21:07 +02004715 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4716 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4717 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004718
Johannes Berg4b0dd982010-09-24 11:21:07 +02004719 /*
4720 * frame is destined for this interface, but if it's
4721 * not also for the previous one we handle that after
4722 * the loop to avoid copying the SKB once too much
4723 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004724
Johannes Berg4b0dd982010-09-24 11:21:07 +02004725 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004726 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004727 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004728 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004729
Johannes Berg7852e362012-01-20 13:55:24 +01004730 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004731 rx.sdata = prev;
4732 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004733
Johannes Berg4b0dd982010-09-24 11:21:07 +02004734 prev = sdata;
4735 }
Johannes Berg4406c372010-09-24 11:21:06 +02004736
Johannes Berg4b0dd982010-09-24 11:21:07 +02004737 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004738 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004739 rx.sdata = prev;
4740
4741 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4742 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004743 }
Johannes Berg4406c372010-09-24 11:21:06 +02004744
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304745 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004746 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004747}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004748
4749/*
4750 * This is the receive path handler. It is called by a low level driver when an
4751 * 802.11 MPDU is received from the hardware.
4752 */
Felix Fietkauc5d16862020-07-26 13:06:11 +02004753void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4754 struct sk_buff *skb, struct list_head *list)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004755{
4756 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004757 struct ieee80211_rate *rate = NULL;
4758 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004759 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004760
Johannes Bergd20ef632009-10-11 15:10:40 +02004761 WARN_ON_ONCE(softirq_count() == 0);
4762
Johannes Berg57fbcce2016-04-12 15:56:15 +02004763 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004764 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004765
4766 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004767 if (WARN_ON(!sband))
4768 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004769
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004770 /*
4771 * If we're suspending, it is possible although not too likely
4772 * that we'd be receiving frames after having already partially
4773 * quiesced the stack. We can't process such frames then since
4774 * that might, for example, cause stations to be added or other
4775 * driver callbacks be invoked.
4776 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004777 if (unlikely(local->quiescing || local->suspended))
4778 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004779
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004780 /* We might be during a HW reconfig, prevent Rx for the same reason */
4781 if (unlikely(local->in_reconfig))
4782 goto drop;
4783
Johannes Bergea77f122009-08-21 14:44:45 +02004784 /*
4785 * The same happens when we're not even started,
4786 * but that's worth a warning.
4787 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004788 if (WARN_ON(!local->started))
4789 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004790
Johannes Bergfc885182010-07-30 13:23:12 +02004791 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004792 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004793 * Validate the rate, unless a PLCP error means that
4794 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004795 */
Johannes Bergfc885182010-07-30 13:23:12 +02004796
Johannes Bergda6a4352017-04-26 12:14:59 +02004797 switch (status->encoding) {
4798 case RX_ENC_HT:
Johannes Bergfc885182010-07-30 13:23:12 +02004799 /*
4800 * rate_idx is MCS index, which can be [0-76]
4801 * as documented on:
4802 *
Flavio Suligoi59d4bfc2020-06-05 17:41:12 +02004803 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
Johannes Bergfc885182010-07-30 13:23:12 +02004804 *
4805 * Anything else would be some sort of driver or
4806 * hardware error. The driver should catch hardware
4807 * errors.
4808 */
Johannes Berg444e3802012-09-30 17:08:35 +02004809 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004810 "Rate marked as an HT rate but passed "
4811 "status->rate_idx is not "
4812 "an MCS index [0-76]: %d (0x%02x)\n",
4813 status->rate_idx,
4814 status->rate_idx))
4815 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004816 break;
4817 case RX_ENC_VHT:
Johannes Berg56146182012-11-09 15:07:02 +01004818 if (WARN_ONCE(status->rate_idx > 9 ||
Johannes Berg8613c942017-04-26 13:51:41 +02004819 !status->nss ||
4820 status->nss > 8,
Johannes Berg56146182012-11-09 15:07:02 +01004821 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
Johannes Berg8613c942017-04-26 13:51:41 +02004822 status->rate_idx, status->nss))
Johannes Berg56146182012-11-09 15:07:02 +01004823 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004824 break;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004825 case RX_ENC_HE:
4826 if (WARN_ONCE(status->rate_idx > 11 ||
4827 !status->nss ||
4828 status->nss > 8,
4829 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4830 status->rate_idx, status->nss))
4831 goto drop;
4832 break;
Johannes Bergda6a4352017-04-26 12:14:59 +02004833 default:
4834 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004835 fallthrough;
Johannes Bergda6a4352017-04-26 12:14:59 +02004836 case RX_ENC_LEGACY:
Johannes Berg444e3802012-09-30 17:08:35 +02004837 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004838 goto drop;
4839 rate = &sband->bitrates[status->rate_idx];
4840 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004841 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004842
Johannes Berg554891e2010-09-24 12:38:25 +02004843 status->rx_flags = 0;
4844
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004845 kcov_remote_start_common(skb_get_kcov_handle(skb));
4846
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004847 /*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004848 * Frames with failed FCS/PLCP checksum are not returned,
4849 * all other frames are returned without radiotap header
4850 * if it was previously present.
4851 * Also, frames with less than 16 bytes are dropped.
4852 */
Felix Fietkau80a915e2020-12-18 19:47:18 +01004853 if (!(status->flag & RX_FLAG_8023))
4854 skb = ieee80211_rx_monitor(local, skb, rate);
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004855 if (skb) {
4856 ieee80211_tpt_led_trig_rx(local,
4857 ((struct ieee80211_hdr *)skb->data)->frame_control,
4858 skb->len);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004859
Felix Fietkau80a915e2020-12-18 19:47:18 +01004860 if (status->flag & RX_FLAG_8023)
4861 __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
4862 else
4863 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004864 }
Johannes Bergd63b5482016-03-31 20:02:02 +03004865
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004866 kcov_remote_stop();
Johannes Berg77a980d2009-08-24 11:46:30 +02004867 return;
4868 drop:
4869 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004870}
Felix Fietkauc5d16862020-07-26 13:06:11 +02004871EXPORT_SYMBOL(ieee80211_rx_list);
4872
4873void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4874 struct sk_buff *skb, struct napi_struct *napi)
4875{
4876 struct sk_buff *tmp;
4877 LIST_HEAD(list);
4878
4879
4880 /*
4881 * key references and virtual interfaces are protected using RCU
4882 * and this requires that we are in a read-side RCU section during
4883 * receive processing
4884 */
4885 rcu_read_lock();
4886 ieee80211_rx_list(hw, pubsta, skb, &list);
4887 rcu_read_unlock();
4888
4889 if (!napi) {
4890 netif_receive_skb_list(&list);
4891 return;
4892 }
4893
4894 list_for_each_entry_safe(skb, tmp, &list, list) {
4895 skb_list_del_init(skb);
4896 napi_gro_receive(napi, skb);
4897 }
4898}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004899EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004900
4901/* This is a version of the rx handler that can be called from hard irq
4902 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004903void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004904{
4905 struct ieee80211_local *local = hw_to_local(hw);
4906
4907 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4908
Johannes Berg571ecf62007-07-27 15:43:22 +02004909 skb->pkt_type = IEEE80211_RX_MSG;
4910 skb_queue_tail(&local->skb_queue, skb);
4911 tasklet_schedule(&local->tasklet);
4912}
4913EXPORT_SYMBOL(ieee80211_rx_irqsafe);