blob: 9541a4c30aca7c071d254c987839f723728312eb [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 Bergf057d142021-05-17 23:07:56 +0200217static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
218 struct sta_info *sta,
219 struct sk_buff *skb)
Johannes Berg0044cc12021-05-17 23:07:54 +0200220{
221 skb_queue_tail(&sdata->skb_queue, skb);
222 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
223 if (sta)
224 sta->rx_stats.packets++;
225}
226
Johannes Bergf057d142021-05-17 23:07:56 +0200227static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
228 struct sta_info *sta,
229 struct sk_buff *skb)
230{
231 skb->protocol = 0;
232 __ieee80211_queue_skb_to_iface(sdata, sta, skb);
233}
234
Johannes Berg9e478062017-04-13 14:23:49 +0200235static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
236 struct sk_buff *skb,
Johannes Bergc096b922018-04-20 13:49:18 +0300237 int rtap_space)
Johannes Berg9e478062017-04-13 14:23:49 +0200238{
239 struct {
240 struct ieee80211_hdr_3addr hdr;
241 u8 category;
242 u8 action_code;
Mathieu Malaterre7c53eb52019-01-24 19:19:57 +0100243 } __packed __aligned(2) action;
Johannes Berg9e478062017-04-13 14:23:49 +0200244
245 if (!sdata)
246 return;
247
248 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
249
Johannes Bergc096b922018-04-20 13:49:18 +0300250 if (skb->len < rtap_space + sizeof(action) +
Johannes Berg9e478062017-04-13 14:23:49 +0200251 VHT_MUMIMO_GROUPS_DATA_LEN)
252 return;
253
254 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
255 return;
256
Johannes Bergc096b922018-04-20 13:49:18 +0300257 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
Johannes Berg9e478062017-04-13 14:23:49 +0200258
259 if (!ieee80211_is_action(action.hdr.frame_control))
260 return;
261
262 if (action.category != WLAN_CATEGORY_VHT)
263 return;
264
265 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
266 return;
267
268 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
269 return;
270
271 skb = skb_copy(skb, GFP_ATOMIC);
272 if (!skb)
273 return;
274
Johannes Berg0044cc12021-05-17 23:07:54 +0200275 ieee80211_queue_skb_to_iface(sdata, NULL, skb);
Johannes Berg9e478062017-04-13 14:23:49 +0200276}
277
Johannes Berg00ea6de2012-09-05 15:54:51 +0200278/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200279 * ieee80211_add_rx_radiotap_header - add radiotap header
280 *
281 * add a radiotap header containing all the fields which the hardware provided.
282 */
283static void
284ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
285 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200286 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200287 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200288{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200289 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200290 struct ieee80211_radiotap_header *rthdr;
291 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200292 __le32 *it_present;
293 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100294 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200295 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200296 int mpdulen, chain;
297 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100298 struct ieee80211_vendor_radiotap rtap = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300299 struct ieee80211_radiotap_he he = {};
300 struct ieee80211_radiotap_he_mu he_mu = {};
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300301 struct ieee80211_radiotap_lsig lsig = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300302
303 if (status->flag & RX_FLAG_RADIOTAP_HE) {
304 he = *(struct ieee80211_radiotap_he *)skb->data;
305 skb_pull(skb, sizeof(he));
306 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
307 }
308
309 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
310 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
311 skb_pull(skb, sizeof(he_mu));
312 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100313
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300314 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
315 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
316 skb_pull(skb, sizeof(lsig));
317 }
318
Johannes Berg1f7bba72014-11-06 22:56:36 +0100319 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
320 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
321 /* rtap.len and rtap.pad are undone immediately */
322 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
323 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800324
325 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200326 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800327 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200328
Johannes Bergd58ff352017-06-16 14:29:23 +0200329 rthdr = skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100330 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200331 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200332
333 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200334 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200335 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
336 BIT(IEEE80211_RADIOTAP_CHANNEL) |
337 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200338
Johannes Berga144f372013-07-03 13:34:02 +0200339 if (!status->chains)
340 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
341
342 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
343 it_present_val |=
344 BIT(IEEE80211_RADIOTAP_EXT) |
345 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
346 put_unaligned_le32(it_present_val, it_present);
347 it_present++;
348 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
349 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
350 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200351
Johannes Berg1f7bba72014-11-06 22:56:36 +0100352 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
353 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
354 BIT(IEEE80211_RADIOTAP_EXT);
355 put_unaligned_le32(it_present_val, it_present);
356 it_present++;
357 it_present_val = rtap.present;
358 }
359
Johannes Berga144f372013-07-03 13:34:02 +0200360 put_unaligned_le32(it_present_val, it_present);
361
Kees Cook8c89f7b2021-08-06 14:53:05 -0700362 /* This references through an offset into it_optional[] rather
363 * than via it_present otherwise later uses of pos will cause
364 * the compiler to think we have walked past the end of the
365 * struct member.
366 */
Johannes Bergc033a382021-11-09 10:02:04 +0100367 pos = (void *)&rthdr->it_optional[it_present + 1 - rthdr->it_optional];
Johannes Berga144f372013-07-03 13:34:02 +0200368
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200369 /* the order of the following fields is important */
370
371 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800372 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100373 /* padding */
374 while ((pos - (u8 *)rthdr) & 7)
375 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800376 put_unaligned_le64(
377 ieee80211_calculate_rx_timestamp(local, status,
378 mpdulen, 0),
379 pos);
Kees Cook5cafd372021-08-06 14:51:12 -0700380 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_TSFT));
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200381 pos += 8;
382 }
383
384 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200385 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200386 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100387 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
388 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200389 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200390 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200391 pos++;
392
393 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergda6a4352017-04-26 12:14:59 +0200394 if (!rate || status->encoding != RX_ENC_LEGACY) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200395 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100396 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530397 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200398 * added below. The byte here is needed as padding
399 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200400 */
401 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200402 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200403 int shift = 0;
Kees Cook5cafd372021-08-06 14:51:12 -0700404 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_RATE));
Johannes Bergda6a4352017-04-26 12:14:59 +0200405 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200406 shift = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200407 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200408 shift = 2;
409 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200410 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200411 pos++;
412
413 /* IEEE80211_RADIOTAP_CHANNEL */
Thomas Pedersen3b23c1842020-04-01 18:18:05 -0700414 /* TODO: frequency offset in KHz */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100415 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200416 pos += 2;
Johannes Bergda6a4352017-04-26 12:14:59 +0200417 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200418 channel_flags |= IEEE80211_CHAN_HALF;
Johannes Bergda6a4352017-04-26 12:14:59 +0200419 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200420 channel_flags |= IEEE80211_CHAN_QUARTER;
421
Aloka Dixit412a84b2020-09-11 01:11:35 +0000422 if (status->band == NL80211_BAND_5GHZ ||
423 status->band == NL80211_BAND_6GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200424 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Bergda6a4352017-04-26 12:14:59 +0200425 else if (status->encoding != RX_ENC_LEGACY)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200426 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100427 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200428 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100429 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100430 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100431 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200432 channel_flags |= IEEE80211_CHAN_2GHZ;
433 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200434 pos += 2;
435
436 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200437 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100438 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200439 *pos = status->signal;
440 rthdr->it_present |=
Kees Cook5cafd372021-08-06 14:51:12 -0700441 cpu_to_le32(BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL));
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200442 pos++;
443 }
444
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200445 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
446
Johannes Berga144f372013-07-03 13:34:02 +0200447 if (!status->chains) {
448 /* IEEE80211_RADIOTAP_ANTENNA */
449 *pos = status->antenna;
450 pos++;
451 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200452
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200453 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
454
455 /* IEEE80211_RADIOTAP_RX_FLAGS */
456 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100457 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100458 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100459 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100460 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
461 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200462 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100463
Johannes Bergda6a4352017-04-26 12:14:59 +0200464 if (status->encoding == RX_ENC_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200465 unsigned int stbc;
466
Kees Cook5cafd372021-08-06 14:51:12 -0700467 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_MCS));
Johannes Bergac55d2f2012-05-10 09:09:10 +0200468 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100469 *pos = 0;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200470 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100471 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
Johannes Bergda6a4352017-04-26 12:14:59 +0200472 if (status->bw == RATE_INFO_BW_40)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100473 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200474 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
Johannes Bergac55d2f2012-05-10 09:09:10 +0200475 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200476 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200477 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200478 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200479 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100480 pos++;
481 *pos++ = status->rate_idx;
482 }
Johannes Berg4c298672012-07-05 11:34:31 +0200483
484 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
485 u16 flags = 0;
486
487 /* ensure 4 byte alignment */
488 while ((pos - (u8 *)rthdr) & 3)
489 pos++;
490 rthdr->it_present |=
Kees Cook5cafd372021-08-06 14:51:12 -0700491 cpu_to_le32(BIT(IEEE80211_RADIOTAP_AMPDU_STATUS));
Johannes Berg4c298672012-07-05 11:34:31 +0200492 put_unaligned_le32(status->ampdu_reference, pos);
493 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200494 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
495 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
496 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
497 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
498 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
499 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
500 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
501 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
Johannes Berg7299d6f2018-02-19 14:48:39 +0200502 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
503 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
504 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
505 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
Johannes Berg4c298672012-07-05 11:34:31 +0200506 put_unaligned_le16(flags, pos);
507 pos += 2;
508 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
509 *pos++ = status->ampdu_delimiter_crc;
510 else
511 *pos++ = 0;
512 *pos++ = 0;
513 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100514
Johannes Bergda6a4352017-04-26 12:14:59 +0200515 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100516 u16 known = local->hw.radiotap_vht_details;
517
Kees Cook5cafd372021-08-06 14:51:12 -0700518 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_VHT));
Johannes Berg51648922012-11-22 23:00:18 +0100519 put_unaligned_le16(known, pos);
520 pos += 2;
521 /* flags */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200522 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg51648922012-11-22 23:00:18 +0100523 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200524 /* in VHT, STBC is binary */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200525 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200526 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200527 if (status->enc_flags & RX_ENC_FLAG_BF)
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200528 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100529 pos++;
530 /* bandwidth */
Johannes Bergda6a4352017-04-26 12:14:59 +0200531 switch (status->bw) {
532 case RATE_INFO_BW_80:
Johannes Berg51648922012-11-22 23:00:18 +0100533 *pos++ = 4;
Johannes Bergda6a4352017-04-26 12:14:59 +0200534 break;
535 case RATE_INFO_BW_160:
Johannes Berg51648922012-11-22 23:00:18 +0100536 *pos++ = 11;
Johannes Bergda6a4352017-04-26 12:14:59 +0200537 break;
538 case RATE_INFO_BW_40:
Johannes Berg51648922012-11-22 23:00:18 +0100539 *pos++ = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200540 break;
541 default:
Johannes Berg51648922012-11-22 23:00:18 +0100542 *pos++ = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200543 }
Johannes Berg51648922012-11-22 23:00:18 +0100544 /* MCS/NSS */
Johannes Berg8613c942017-04-26 13:51:41 +0200545 *pos = (status->rate_idx << 4) | status->nss;
Johannes Berg51648922012-11-22 23:00:18 +0100546 pos += 4;
547 /* coding field */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200548 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200549 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100550 pos++;
551 /* group ID */
552 pos++;
553 /* partial_aid */
554 pos += 2;
555 }
556
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300557 if (local->hw.radiotap_timestamp.units_pos >= 0) {
558 u16 accuracy = 0;
559 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
560
561 rthdr->it_present |=
Kees Cook5cafd372021-08-06 14:51:12 -0700562 cpu_to_le32(BIT(IEEE80211_RADIOTAP_TIMESTAMP));
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300563
564 /* ensure 8 byte alignment */
565 while ((pos - (u8 *)rthdr) & 7)
566 pos++;
567
568 put_unaligned_le64(status->device_timestamp, pos);
569 pos += sizeof(u64);
570
571 if (local->hw.radiotap_timestamp.accuracy >= 0) {
572 accuracy = local->hw.radiotap_timestamp.accuracy;
573 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
574 }
575 put_unaligned_le16(accuracy, pos);
576 pos += sizeof(u16);
577
578 *pos++ = local->hw.radiotap_timestamp.units_pos;
579 *pos++ = flags;
580 }
581
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300582 if (status->encoding == RX_ENC_HE &&
583 status->flag & RX_FLAG_RADIOTAP_HE) {
Johannes Berg331aead2018-08-31 11:31:07 +0300584#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300585
586 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
587 he.data6 |= HE_PREP(DATA6_NSTS,
588 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
589 status->enc_flags));
590 he.data3 |= HE_PREP(DATA3_STBC, 1);
591 } else {
592 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
593 }
594
595#define CHECK_GI(s) \
596 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
597 (int)NL80211_RATE_INFO_HE_GI_##s)
598
599 CHECK_GI(0_8);
600 CHECK_GI(1_6);
601 CHECK_GI(3_2);
602
603 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
604 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
605 he.data3 |= HE_PREP(DATA3_CODING,
606 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
607
608 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
609
610 switch (status->bw) {
611 case RATE_INFO_BW_20:
612 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
613 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
614 break;
615 case RATE_INFO_BW_40:
616 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
617 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
618 break;
619 case RATE_INFO_BW_80:
620 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
621 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
622 break;
623 case RATE_INFO_BW_160:
624 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
625 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
626 break;
627 case RATE_INFO_BW_HE_RU:
628#define CHECK_RU_ALLOC(s) \
629 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
630 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
631
632 CHECK_RU_ALLOC(26);
633 CHECK_RU_ALLOC(52);
634 CHECK_RU_ALLOC(106);
635 CHECK_RU_ALLOC(242);
636 CHECK_RU_ALLOC(484);
637 CHECK_RU_ALLOC(996);
638 CHECK_RU_ALLOC(2x996);
639
640 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
641 status->he_ru + 4);
642 break;
643 default:
644 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
645 }
646
647 /* ensure 2 byte alignment */
648 while ((pos - (u8 *)rthdr) & 1)
649 pos++;
Kees Cook5cafd372021-08-06 14:51:12 -0700650 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE));
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300651 memcpy(pos, &he, sizeof(he));
652 pos += sizeof(he);
653 }
654
655 if (status->encoding == RX_ENC_HE &&
656 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
657 /* ensure 2 byte alignment */
658 while ((pos - (u8 *)rthdr) & 1)
659 pos++;
Kees Cook5cafd372021-08-06 14:51:12 -0700660 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE_MU));
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300661 memcpy(pos, &he_mu, sizeof(he_mu));
662 pos += sizeof(he_mu);
663 }
664
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300665 if (status->flag & RX_FLAG_NO_PSDU) {
666 rthdr->it_present |=
Kees Cook5cafd372021-08-06 14:51:12 -0700667 cpu_to_le32(BIT(IEEE80211_RADIOTAP_ZERO_LEN_PSDU));
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300668 *pos++ = status->zero_length_psdu_type;
669 }
670
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300671 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
672 /* ensure 2 byte alignment */
673 while ((pos - (u8 *)rthdr) & 1)
674 pos++;
Kees Cook5cafd372021-08-06 14:51:12 -0700675 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_LSIG));
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300676 memcpy(pos, &lsig, sizeof(lsig));
677 pos += sizeof(lsig);
678 }
679
Johannes Berga144f372013-07-03 13:34:02 +0200680 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
681 *pos++ = status->chain_signal[chain];
682 *pos++ = chain;
683 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100684
685 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
686 /* ensure 2 byte alignment for the vendor field as required */
687 if ((pos - (u8 *)rthdr) & 1)
688 *pos++ = 0;
689 *pos++ = rtap.oui[0];
690 *pos++ = rtap.oui[1];
691 *pos++ = rtap.oui[2];
692 *pos++ = rtap.subns;
693 put_unaligned_le16(rtap.len, pos);
694 pos += 2;
695 /* align the actual payload as requested */
696 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
697 *pos++ = 0;
698 /* data (and possible padding) already follows */
699 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200700}
701
Johannes Berg127f60b2017-04-13 15:50:27 +0200702static struct sk_buff *
703ieee80211_make_monitor_skb(struct ieee80211_local *local,
704 struct sk_buff **origskb,
705 struct ieee80211_rate *rate,
Johannes Bergc096b922018-04-20 13:49:18 +0300706 int rtap_space, bool use_origskb)
Johannes Berg127f60b2017-04-13 15:50:27 +0200707{
708 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
709 int rt_hdrlen, needed_headroom;
710 struct sk_buff *skb;
711
712 /* room for the radiotap header based on driver features */
713 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
Johannes Bergc096b922018-04-20 13:49:18 +0300714 needed_headroom = rt_hdrlen - rtap_space;
Johannes Berg127f60b2017-04-13 15:50:27 +0200715
716 if (use_origskb) {
717 /* only need to expand headroom if necessary */
718 skb = *origskb;
719 *origskb = NULL;
720
721 /*
722 * This shouldn't trigger often because most devices have an
723 * RX header they pull before we get here, and that should
724 * be big enough for our radiotap information. We should
725 * probably export the length to drivers so that we can have
726 * them allocate enough headroom to start with.
727 */
728 if (skb_headroom(skb) < needed_headroom &&
729 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
730 dev_kfree_skb(skb);
731 return NULL;
732 }
733 } else {
734 /*
735 * Need to make a copy and possibly remove radiotap header
736 * and FCS from the original.
737 */
Johan Almbladhec61cd42021-06-28 14:37:13 +0200738 skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD,
739 0, GFP_ATOMIC);
Johannes Berg127f60b2017-04-13 15:50:27 +0200740
741 if (!skb)
742 return NULL;
743 }
744
745 /* prepend radiotap information */
746 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
747
748 skb_reset_mac_header(skb);
749 skb->ip_summed = CHECKSUM_UNNECESSARY;
750 skb->pkt_type = PACKET_OTHERHOST;
751 skb->protocol = htons(ETH_P_802_2);
752
753 return skb;
754}
755
Johannes Bergb2e77712007-09-26 15:19:39 +0200756/*
757 * This function copies a received frame to all monitor interfaces and
758 * returns a cleaned-up SKB that no longer includes the FCS nor the
759 * radiotap header the driver might have added.
760 */
761static struct sk_buff *
762ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100763 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200764{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200765 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200766 struct ieee80211_sub_if_data *sdata;
Johannes Berg127f60b2017-04-13 15:50:27 +0200767 struct sk_buff *monskb = NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200768 int present_fcs_len = 0;
Johannes Bergc096b922018-04-20 13:49:18 +0300769 unsigned int rtap_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300770 struct ieee80211_sub_if_data *monitor_sdata =
771 rcu_dereference(local->monitor_sdata);
Johannes Berg127f60b2017-04-13 15:50:27 +0200772 bool only_monitor = false;
Ilan Peer80209192018-12-15 11:03:17 +0200773 unsigned int min_head_len;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100774
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300775 if (status->flag & RX_FLAG_RADIOTAP_HE)
776 rtap_space += sizeof(struct ieee80211_radiotap_he);
777
778 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
779 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
780
Ilan Peerd359bbc2018-12-15 11:03:25 +0200781 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
782 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
783
Johannes Berg1f7bba72014-11-06 22:56:36 +0100784 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
Ilan Peerd359bbc2018-12-15 11:03:25 +0200785 struct ieee80211_vendor_radiotap *rtap =
786 (void *)(origskb->data + rtap_space);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100787
Johannes Bergc096b922018-04-20 13:49:18 +0300788 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100789 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200790
Ilan Peer80209192018-12-15 11:03:17 +0200791 min_head_len = rtap_space;
792
Johannes Bergb2e77712007-09-26 15:19:39 +0200793 /*
794 * First, we may need to make a copy of the skb because
795 * (1) we need to modify it for radiotap (if not present), and
796 * (2) the other RX handlers will modify the skb we got.
797 *
798 * We don't need to, of course, if we aren't going to return
799 * the SKB because it has a bad FCS/PLCP checksum.
800 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100801
Ilan Peer80209192018-12-15 11:03:17 +0200802 if (!(status->flag & RX_FLAG_NO_PSDU)) {
803 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
804 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
805 /* driver bug */
806 WARN_ON(1);
807 dev_kfree_skb(origskb);
808 return NULL;
809 }
810 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200811 }
Ilan Peer80209192018-12-15 11:03:17 +0200812
813 /* also consider the hdr->frame_control */
814 min_head_len += 2;
Johannes Berg30841f52017-04-11 15:38:56 +0200815 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200816
Ilan Peer80209192018-12-15 11:03:17 +0200817 /* ensure that the expected data elements are in skb head */
818 if (!pskb_may_pull(origskb, min_head_len)) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800819 dev_kfree_skb(origskb);
820 return NULL;
821 }
822
Johannes Bergc096b922018-04-20 13:49:18 +0300823 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
Johannes Berg127f60b2017-04-13 15:50:27 +0200824
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100825 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200826 if (only_monitor) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200827 dev_kfree_skb(origskb);
828 return NULL;
829 }
830
Johannes Bergc1129922020-05-26 14:33:48 +0200831 return ieee80211_clean_skb(origskb, present_fcs_len,
832 rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200833 }
834
Johannes Bergc096b922018-04-20 13:49:18 +0300835 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
Johannes Berg9e478062017-04-13 14:23:49 +0200836
Johannes Bergf64331d2017-04-13 13:28:18 +0200837 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200838 bool last_monitor = list_is_last(&sdata->u.mntr.list,
839 &local->mon_list);
840
841 if (!monskb)
842 monskb = ieee80211_make_monitor_skb(local, &origskb,
Johannes Bergc096b922018-04-20 13:49:18 +0300843 rate, rtap_space,
Johannes Berg127f60b2017-04-13 15:50:27 +0200844 only_monitor &&
845 last_monitor);
846
847 if (monskb) {
848 struct sk_buff *skb;
849
850 if (last_monitor) {
851 skb = monskb;
852 monskb = NULL;
853 } else {
854 skb = skb_clone(monskb, GFP_ATOMIC);
855 }
856
857 if (skb) {
858 skb->dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +0200859 dev_sw_netstats_rx_add(skb->dev, skb->len);
Johannes Berg127f60b2017-04-13 15:50:27 +0200860 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200861 }
862 }
863
Johannes Berg127f60b2017-04-13 15:50:27 +0200864 if (last_monitor)
865 break;
Johannes Bergb2e77712007-09-26 15:19:39 +0200866 }
867
Johannes Berg127f60b2017-04-13 15:50:27 +0200868 /* this happens if last_monitor was erroneously false */
869 dev_kfree_skb(monskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200870
Johannes Berg127f60b2017-04-13 15:50:27 +0200871 /* ditto */
872 if (!origskb)
873 return NULL;
874
Johannes Bergc1129922020-05-26 14:33:48 +0200875 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200876}
877
Johannes Berg5cf121c2008-02-25 16:27:43 +0100878static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200879{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700880 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200881 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200882 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200883
884 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700885 if (ieee80211_is_data_qos(hdr->frame_control)) {
886 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200887 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700888 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200889 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200890 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200891
892 seqno_idx = tid;
893 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200894 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200895 /*
896 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
897 *
898 * Sequence numbers for management frames, QoS data
899 * frames with a broadcast/multicast address in the
900 * Address 1 field, and all non-QoS data frames sent
901 * by QoS STAs are assigned using an additional single
902 * modulo-4096 counter, [...]
903 *
904 * We also use that counter for non-QoS STAs.
905 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100906 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200907 security_idx = 0;
908 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100909 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200910 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200911 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200912
Johannes Berg9e262972011-07-07 18:45:03 +0200913 rx->seqno_idx = seqno_idx;
914 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200915 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
916 * For now, set skb->priority to 0 for other cases. */
917 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100918}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200919
Johannes Bergd1c3a372009-01-07 00:26:10 +0100920/**
921 * DOC: Packet alignment
922 *
923 * Drivers always need to pass packets that are aligned to two-byte boundaries
924 * to the stack.
925 *
926 * Additionally, should, if possible, align the payload data in a way that
927 * guarantees that the contained IP header is aligned to a four-byte
928 * boundary. In the case of regular frames, this simply means aligning the
929 * payload to a four-byte boundary (because either the IP header is directly
930 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100931 * in front of it). If the payload data is not properly aligned and the
932 * architecture doesn't support efficient unaligned operations, mac80211
933 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100934 *
935 * With A-MSDU frames, however, the payload data address must yield two modulo
936 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
937 * push the IP header further back to a multiple of four again. Thankfully, the
938 * specs were sane enough this time around to require padding each A-MSDU
939 * subframe to a length that is a multiple of four.
940 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300941 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100942 * the payload is not supported, the driver is required to move the 802.11
943 * header to be directly in front of the payload in that case.
944 */
945static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100946{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100947#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100948 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100949#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200950}
951
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200952
Johannes Berg571ecf62007-07-27 15:43:22 +0200953/* rx handlers */
954
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200955static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
956{
957 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
958
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100959 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200960 return 0;
961
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100962 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200963}
964
965
966static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
967{
968 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
969
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100970 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200971 return 0;
972
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100973 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200974}
975
976
977/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
978static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
979{
980 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
981 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200982 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200983
Johannes Berg1df332e2012-10-26 00:09:11 +0200984 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200985 return -1;
986
Jouni Malinenaf2d14b2020-02-22 15:25:47 +0200987 if (!ieee80211_is_robust_mgmt_frame(skb) &&
988 !ieee80211_is_beacon(hdr->frame_control))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200989 return -1; /* not a robust management frame */
990
991 mmie = (struct ieee80211_mmie *)
992 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200993 if (mmie->element_id == WLAN_EID_MMIE &&
994 mmie->length == sizeof(*mmie) - 2)
995 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200996
Jouni Malinen56c52da2015-01-24 19:52:08 +0200997 mmie16 = (struct ieee80211_mmie_16 *)
998 (skb->data + skb->len - sizeof(*mmie16));
999 if (skb->len >= 24 + sizeof(*mmie16) &&
1000 mmie16->element_id == WLAN_EID_MMIE &&
1001 mmie16->length == sizeof(*mmie16) - 2)
1002 return le16_to_cpu(mmie16->key_id);
1003
1004 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001005}
1006
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001007static int ieee80211_get_keyid(struct sk_buff *skb,
1008 const struct ieee80211_cipher_scheme *cs)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001009{
1010 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1011 __le16 fc;
1012 int hdrlen;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001013 int minlen;
1014 u8 key_idx_off;
1015 u8 key_idx_shift;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001016 u8 keyid;
1017
1018 fc = hdr->frame_control;
1019 hdrlen = ieee80211_hdrlen(fc);
1020
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001021 if (cs) {
1022 minlen = hdrlen + cs->hdr_len;
1023 key_idx_off = hdrlen + cs->key_idx_off;
1024 key_idx_shift = cs->key_idx_shift;
1025 } else {
1026 /* WEP, TKIP, CCMP and GCMP */
1027 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1028 key_idx_off = hdrlen + 3;
1029 key_idx_shift = 6;
1030 }
1031
1032 if (unlikely(skb->len < minlen))
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001033 return -EINVAL;
1034
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001035 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1036
1037 if (cs)
1038 keyid &= cs->key_idx_mask;
1039 keyid >>= key_idx_shift;
1040
1041 /* cs could use more than the usual two bits for the keyid */
1042 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1043 return -EINVAL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001044
1045 return keyid;
1046}
1047
Johannes Berg1df332e2012-10-26 00:09:11 +02001048static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001049{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001050 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +01001051 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001052
Harvey Harrisona7767f92008-07-02 16:30:51 -07001053 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001054 if (is_multicast_ether_addr(hdr->addr1)) {
1055 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +02001056 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001057 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001058 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001059 return RX_DROP_MONITOR;
1060 } else {
1061 if (!ieee80211_has_a4(hdr->frame_control))
1062 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001063 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001064 return RX_DROP_MONITOR;
1065 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001066 }
1067
1068 /* If there is not an established peer link and this is not a peer link
1069 * establisment frame, beacon or probe, drop the frame.
1070 */
1071
Javier Cardona57cf8042011-05-13 10:45:43 -07001072 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001073 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001074
Harvey Harrisona7767f92008-07-02 16:30:51 -07001075 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001076 return RX_DROP_MONITOR;
1077
Harvey Harrisona7767f92008-07-02 16:30:51 -07001078 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001079 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001080
1081 /* make sure category field is present */
1082 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1083 return RX_DROP_MONITOR;
1084
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001085 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001086 category = mgmt->u.action.category;
1087 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +02001088 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001089 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001090 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001091 }
1092
Harvey Harrisona7767f92008-07-02 16:30:51 -07001093 if (ieee80211_is_probe_req(hdr->frame_control) ||
1094 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -07001095 ieee80211_is_beacon(hdr->frame_control) ||
1096 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -07001097 return RX_CONTINUE;
1098
1099 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001100 }
1101
Johannes Berg902acc72008-02-23 15:17:19 +01001102 return RX_CONTINUE;
1103}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001104
Johannes Bergfb4ea052016-01-28 16:19:24 +02001105static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1106 int index)
1107{
1108 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1109 struct sk_buff *tail = skb_peek_tail(frames);
1110 struct ieee80211_rx_status *status;
1111
Sara Sharon06470f72016-01-28 16:19:25 +02001112 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1113 return true;
1114
Johannes Bergfb4ea052016-01-28 16:19:24 +02001115 if (!tail)
1116 return false;
1117
1118 status = IEEE80211_SKB_RXCB(tail);
1119 if (status->flag & RX_FLAG_AMSDU_MORE)
1120 return false;
1121
1122 return true;
1123}
1124
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001125static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001126 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001127 int index,
1128 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001129{
Michal Kazior83eb9352014-07-16 12:09:31 +02001130 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1131 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +01001132 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001133
Johannes Bergdd318572010-11-29 11:09:16 +01001134 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1135
Michal Kazior83eb9352014-07-16 12:09:31 +02001136 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001137 goto no_frame;
1138
Johannes Bergfb4ea052016-01-28 16:19:24 +02001139 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001140 __skb_queue_purge(skb_list);
1141 goto no_frame;
1142 }
1143
1144 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001145 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +02001146 while ((skb = __skb_dequeue(skb_list))) {
1147 status = IEEE80211_SKB_RXCB(skb);
1148 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1149 __skb_queue_tail(frames, skb);
1150 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001151
1152no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +02001153 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +01001154 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001155}
1156
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001157static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001158 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001159 u16 head_seq_num,
1160 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001161{
1162 int index;
1163
Johannes Bergdd318572010-11-29 11:09:16 +01001164 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1165
Johannes Berg9a886582013-02-15 19:25:00 +01001166 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001167 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001168 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1169 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001170 }
1171}
1172
1173/*
1174 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1175 * the skb was added to the buffer longer than this time ago, the earlier
1176 * frames that have not yet been received are assumed to be lost and the skb
1177 * can be released for processing. This may also release other skb's from the
1178 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001179 *
1180 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001181 */
1182#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1183
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001184static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001185 struct tid_ampdu_rx *tid_agg_rx,
1186 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001187{
Michal Kazior83eb9352014-07-16 12:09:31 +02001188 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001189
Johannes Bergdd318572010-11-29 11:09:16 +01001190 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1191
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001192 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +02001193 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +02001194 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +02001195 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001196 /*
1197 * No buffers ready to be released, but check whether any
1198 * frames in the reorder buffer have timed out.
1199 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001200 int skipped = 1;
1201 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1202 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001203 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001204 skipped++;
1205 continue;
1206 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -07001207 if (skipped &&
1208 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001209 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001210 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001211
Michal Kazior83eb9352014-07-16 12:09:31 +02001212 /* don't leave incomplete A-MSDUs around */
1213 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1214 i = (i + 1) % tid_agg_rx->buf_size)
1215 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1216
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001217 ht_dbg_ratelimited(sdata,
1218 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001219 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1220 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001221
1222 /*
1223 * Increment the head seq# also for the skipped slots.
1224 */
1225 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +01001226 (tid_agg_rx->head_seq_num +
1227 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001228 skipped = 0;
1229 }
Johannes Bergfb4ea052016-01-28 16:19:24 +02001230 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001231 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1232 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +02001233 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001234 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001235
1236 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001237 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001238
1239 for (; j != (index - 1) % tid_agg_rx->buf_size;
1240 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001241 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001242 break;
1243 }
1244
1245 set_release_timer:
1246
Johannes Berg788211d2015-04-01 14:20:42 +02001247 if (!tid_agg_rx->removed)
1248 mod_timer(&tid_agg_rx->reorder_timer,
1249 tid_agg_rx->reorder_time[j] + 1 +
1250 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001251 } else {
1252 del_timer(&tid_agg_rx->reorder_timer);
1253 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001254}
1255
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001256/*
1257 * As this function belongs to the RX path it must be under
1258 * rcu_read_lock protection. It returns false if the frame
1259 * can be processed immediately, true if it was consumed.
1260 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001261static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001262 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001263 struct sk_buff *skb,
1264 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001265{
1266 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001267 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001268 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1269 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1270 u16 head_seq_num, buf_size;
1271 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001272 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001273
Johannes Bergdd318572010-11-29 11:09:16 +01001274 spin_lock(&tid_agg_rx->reorder_lock);
1275
Michal Kazior4549cf22014-09-02 14:05:10 +02001276 /*
1277 * Offloaded BA sessions have no known starting sequence number so pick
1278 * one from first Rxed frame for this tid after BA was started.
1279 */
1280 if (unlikely(tid_agg_rx->auto_seq)) {
1281 tid_agg_rx->auto_seq = false;
1282 tid_agg_rx->ssn = mpdu_seq_num;
1283 tid_agg_rx->head_seq_num = mpdu_seq_num;
1284 }
1285
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001286 buf_size = tid_agg_rx->buf_size;
1287 head_seq_num = tid_agg_rx->head_seq_num;
1288
Sara Sharonb7540d82017-02-06 15:28:42 +02001289 /*
1290 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1291 * be reordered.
1292 */
1293 if (unlikely(!tid_agg_rx->started)) {
1294 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1295 ret = false;
1296 goto out;
1297 }
1298 tid_agg_rx->started = true;
1299 }
1300
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001301 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001302 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001303 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001304 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001305 }
1306
1307 /*
1308 * If frame the sequence number exceeds our buffering window
1309 * size release some previous frames to make room for this one.
1310 */
Johannes Berg9a886582013-02-15 19:25:00 +01001311 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1312 head_seq_num = ieee80211_sn_inc(
1313 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001314 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001315 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001316 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001317 }
1318
1319 /* Now the new frame is always in the range of the reordering buffer */
1320
Karl Beldan2e3049b2013-10-24 15:53:32 +02001321 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001322
1323 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001324 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001325 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001326 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001327 }
1328
1329 /*
1330 * If the current MPDU is in the right order and nothing else
1331 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001332 * If it is first but there's something stored, we may be able
1333 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001334 */
1335 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1336 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001337 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1338 tid_agg_rx->head_seq_num =
1339 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001340 ret = false;
1341 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001342 }
1343
1344 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001345 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1346 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1347 tid_agg_rx->reorder_time[index] = jiffies;
1348 tid_agg_rx->stored_mpdu_num++;
1349 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1350 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001351
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001352 out:
1353 spin_unlock(&tid_agg_rx->reorder_lock);
1354 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001355}
1356
1357/*
1358 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1359 * true if the MPDU was buffered, false if it should be processed.
1360 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001361static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1362 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001363{
Johannes Berg2569a822009-11-25 17:46:17 +01001364 struct sk_buff *skb = rx->skb;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001365 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001366 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001367 struct tid_ampdu_rx *tid_agg_rx;
1368 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001369 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001370
Johannes Berg051a41fa2013-11-20 11:28:27 +01001371 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1372 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001373 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001374
1375 /*
1376 * filter the QoS data rx stream according to
1377 * STA/TID and check if this STA/TID is on aggregation
1378 */
1379
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001380 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001381 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001382
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001383 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1384 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001385 tid = ieee80211_get_tid(hdr);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001386
Johannes Berga87f7362010-06-10 10:21:38 +02001387 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001388 if (!tid_agg_rx) {
1389 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1390 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1391 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1392 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1393 WLAN_BACK_RECIPIENT,
1394 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001395 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001396 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001397
1398 /* qos null data frames are excluded */
1399 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001400 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001401
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001402 /* not part of a BA session */
1403 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1404 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1405 goto dont_reorder;
1406
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001407 /* new, potentially un-ordered, ampdu frame - process it */
1408
1409 /* reset session timer */
1410 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001411 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001412
1413 /* if this mpdu is fragmented - terminate rx aggregation session */
1414 sc = le16_to_cpu(hdr->seq_ctrl);
1415 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg0044cc12021-05-17 23:07:54 +02001416 ieee80211_queue_skb_to_iface(rx->sdata, NULL, skb);
Johannes Berg2569a822009-11-25 17:46:17 +01001417 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001418 }
1419
Johannes Berga87f7362010-06-10 10:21:38 +02001420 /*
1421 * No locking needed -- we will only ever process one
1422 * RX packet at a time, and thus own tid_agg_rx. All
1423 * other code manipulating it needs to (and does) make
1424 * sure that we cannot get to it any more before doing
1425 * anything with it.
1426 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001427 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1428 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001429 return;
1430
1431 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001432 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001433}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001434
Johannes Berg49461622008-06-30 15:10:45 +02001435static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001436ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001437{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001438 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001439 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001440
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001441 if (status->flag & RX_FLAG_DUP_VALIDATED)
1442 return RX_CONTINUE;
1443
Johannes Berg6b0f3272013-07-11 22:33:26 +02001444 /*
1445 * Drop duplicate 802.11 retransmissions
1446 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1447 */
Johannes Berg03954422014-11-11 16:49:25 +01001448
1449 if (rx->skb->len < 24)
1450 return RX_CONTINUE;
1451
1452 if (ieee80211_is_ctl(hdr->frame_control) ||
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001453 ieee80211_is_any_nullfunc(hdr->frame_control) ||
Johannes Berg03954422014-11-11 16:49:25 +01001454 is_multicast_ether_addr(hdr->addr1))
1455 return RX_CONTINUE;
1456
Johannes Berga732fa72015-10-14 18:27:07 +02001457 if (!rx->sta)
1458 return RX_CONTINUE;
1459
1460 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1461 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1462 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001463 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001464 return RX_DROP_UNUSABLE;
1465 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1466 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001467 }
1468
Johannes Berg03954422014-11-11 16:49:25 +01001469 return RX_CONTINUE;
1470}
1471
1472static ieee80211_rx_result debug_noinline
1473ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1474{
1475 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1476
Johannes Berg571ecf62007-07-27 15:43:22 +02001477 /* Drop disallowed frame classes based on STA auth/assoc state;
1478 * IEEE 802.11, Chap 5.5.
1479 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001480 * mac80211 filters only based on association state, i.e. it drops
1481 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001482 * deauth/disassoc frames when needed. In addition, hostapd is
1483 * responsible for filtering on both auth and assoc states.
1484 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001485
Johannes Berg902acc72008-02-23 15:17:19 +01001486 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001487 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001488
Harvey Harrisona7767f92008-07-02 16:30:51 -07001489 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1490 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001491 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001492 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001493 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001494 /*
1495 * accept port control frames from the AP even when it's not
1496 * yet marked ASSOC to prevent a race where we don't set the
1497 * assoc bit quickly enough before it sends the first frame
1498 */
1499 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001500 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001501 unsigned int hdrlen;
1502 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001503
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001504 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1505
1506 if (rx->skb->len < hdrlen + 8)
1507 return RX_DROP_MONITOR;
1508
1509 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1510 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001511 return RX_CONTINUE;
1512 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001513
1514 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1515 cfg80211_rx_spurious_frame(rx->sdata->dev,
1516 hdr->addr2,
1517 GFP_ATOMIC))
1518 return RX_DROP_UNUSABLE;
1519
Johannes Berge4c26ad2008-01-31 19:48:21 +01001520 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001521 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001522
Johannes Berg9ae54c82008-01-31 19:48:20 +01001523 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001524}
1525
1526
Johannes Berg49461622008-06-30 15:10:45 +02001527static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001528ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1529{
1530 struct ieee80211_local *local;
1531 struct ieee80211_hdr *hdr;
1532 struct sk_buff *skb;
1533
1534 local = rx->local;
1535 skb = rx->skb;
1536 hdr = (struct ieee80211_hdr *) skb->data;
1537
1538 if (!local->pspolling)
1539 return RX_CONTINUE;
1540
1541 if (!ieee80211_has_fromds(hdr->frame_control))
1542 /* this is not from AP */
1543 return RX_CONTINUE;
1544
1545 if (!ieee80211_is_data(hdr->frame_control))
1546 return RX_CONTINUE;
1547
1548 if (!ieee80211_has_moredata(hdr->frame_control)) {
1549 /* AP has no more frames buffered for us */
1550 local->pspolling = false;
1551 return RX_CONTINUE;
1552 }
1553
1554 /* more data bit is set, let's request a new frame from the AP */
1555 ieee80211_send_pspoll(local, rx->sdata);
1556
1557 return RX_CONTINUE;
1558}
1559
Marco Porschd012a602012-10-10 12:39:50 -07001560static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001561{
Johannes Berg133b8222008-09-16 14:18:59 +02001562 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001563 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001564 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001565 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001566
Marco Porschd012a602012-10-10 12:39:50 -07001567 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1568 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1569 ps = &sdata->bss->ps;
1570 else
1571 return;
1572
1573 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001574 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001575 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001576 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001577 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1578 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001579
Johannes Berg17c18bf2015-03-21 15:25:43 +01001580 ieee80211_clear_fast_xmit(sta);
1581
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001582 if (!sta->sta.txq[0])
1583 return;
1584
Johannes Bergadf8ed02018-08-31 11:31:08 +03001585 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001586 struct ieee80211_txq *txq = sta->sta.txq[tid];
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001587
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02001588 ieee80211_unschedule_txq(&local->hw, txq, false);
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001589
1590 if (txq_has_queue(txq))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001591 set_bit(tid, &sta->txq_buffered_tids);
1592 else
1593 clear_bit(tid, &sta->txq_buffered_tids);
1594 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001595}
1596
Marco Porschd012a602012-10-10 12:39:50 -07001597static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001598{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001599 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1600 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001601
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001602 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001603 /*
1604 * Clear the flag only if the other one is still set
1605 * so that the TX path won't start TX'ing new frames
1606 * directly ... In the case that the driver flag isn't
1607 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1608 */
1609 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001610 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1611 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001612 return;
1613 }
1614
Johannes Berg5ac2e352014-05-27 16:32:27 +02001615 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1616 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001617 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001618}
1619
Johannes Bergcf471612015-06-16 16:16:38 +02001620int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001621{
Johannes Bergcf471612015-06-16 16:16:38 +02001622 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001623 bool in_ps;
1624
Johannes Bergcf471612015-06-16 16:16:38 +02001625 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001626
1627 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001628 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001629 if ((start && in_ps) || (!start && !in_ps))
1630 return -EINVAL;
1631
1632 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001633 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001634 else
Johannes Bergcf471612015-06-16 16:16:38 +02001635 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001636
1637 return 0;
1638}
1639EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1640
Johannes Berg46fa38e2016-05-03 16:58:00 +03001641void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1642{
1643 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1644
1645 if (test_sta_flag(sta, WLAN_STA_SP))
1646 return;
1647
1648 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1649 ieee80211_sta_ps_deliver_poll_response(sta);
1650 else
1651 set_sta_flag(sta, WLAN_STA_PSPOLL);
1652}
1653EXPORT_SYMBOL(ieee80211_sta_pspoll);
1654
1655void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1656{
1657 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001658 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001659
1660 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001661 * If this AC is not trigger-enabled do nothing unless the
1662 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001663 *
1664 * NB: This could/should check a separate bitmap of trigger-
1665 * enabled queues, but for now we only implement uAPSD w/o
1666 * TSPEC changes to the ACs, so they're always the same.
1667 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001668 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1669 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001670 return;
1671
1672 /* if we are in a service period, do nothing */
1673 if (test_sta_flag(sta, WLAN_STA_SP))
1674 return;
1675
1676 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1677 ieee80211_sta_ps_deliver_uapsd(sta);
1678 else
1679 set_sta_flag(sta, WLAN_STA_UAPSD);
1680}
1681EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1682
Johannes Berg49461622008-06-30 15:10:45 +02001683static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001684ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1685{
1686 struct ieee80211_sub_if_data *sdata = rx->sdata;
1687 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1688 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001689
Johannes Berg5c900672015-04-22 14:48:34 +02001690 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001691 return RX_CONTINUE;
1692
1693 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1694 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1695 return RX_CONTINUE;
1696
1697 /*
1698 * The device handles station powersave, so don't do anything about
1699 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1700 * it to mac80211 since they're handled.)
1701 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001702 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001703 return RX_CONTINUE;
1704
1705 /*
1706 * Don't do anything if the station isn't already asleep. In
1707 * the uAPSD case, the station will probably be marked asleep,
1708 * in the PS-Poll case the station must be confused ...
1709 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001710 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001711 return RX_CONTINUE;
1712
1713 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001714 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001715
1716 /* Free PS Poll skb here instead of returning RX_DROP that would
1717 * count as an dropped frame. */
1718 dev_kfree_skb(rx->skb);
1719
1720 return RX_QUEUED;
1721 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1722 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1723 ieee80211_has_pm(hdr->frame_control) &&
1724 (ieee80211_is_data_qos(hdr->frame_control) ||
1725 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001726 u8 tid = ieee80211_get_tid(hdr);
Johannes Berg47086fc2011-09-29 16:04:33 +02001727
Johannes Berg46fa38e2016-05-03 16:58:00 +03001728 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001729 }
1730
1731 return RX_CONTINUE;
1732}
1733
1734static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001735ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001736{
1737 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001738 struct sk_buff *skb = rx->skb;
1739 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1740 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001741 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001742
1743 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001744 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001745
Johannes Bergb291ba12009-07-10 15:29:03 +02001746 /*
1747 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001748 * BSSID and for station already AUTHORIZED to avoid keeping the
1749 * current IBSS network alive in cases where other STAs start
1750 * using different BSSID. This will also give the station another
1751 * chance to restart the authentication/authorization in case
1752 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001753 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001754 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001755 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001756 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001757 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1758 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001759 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001760 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001761 !is_multicast_ether_addr(hdr->addr1))
1762 sta->rx_stats.last_rate =
1763 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001764 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001765 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001766 sta->rx_stats.last_rx = jiffies;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001767 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
Wen Gongf879ac82020-12-09 11:06:29 +08001768 !is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb291ba12009-07-10 15:29:03 +02001769 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001770 * Mesh beacons will update last_rx when if they are found to
1771 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001772 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001773 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001774 if (ieee80211_is_data(hdr->frame_control))
1775 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001776 }
1777
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001778 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001779
1780 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001781 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001782 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1783
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001784 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001785 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001786 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001787 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001788
Felix Fietkauef0621e2013-04-22 16:29:31 +02001789 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001790 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001791 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1792 int signal = status->chain_signal[i];
1793
1794 if (!(status->chains & BIT(i)))
1795 continue;
1796
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001797 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001798 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001799 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001800 }
1801 }
1802
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001803 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1804 return RX_CONTINUE;
1805
Johannes Berg72eaa432008-11-26 15:02:58 +01001806 /*
1807 * Change STA power saving mode only at the end of a frame
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001808 * exchange sequence, and only for a data or management
1809 * frame as specified in IEEE 802.11-2016 11.2.3.2
Johannes Berg72eaa432008-11-26 15:02:58 +01001810 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001811 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001812 !ieee80211_has_morefrags(hdr->frame_control) &&
Emmanuel Grumbach209327502018-08-20 13:56:07 +03001813 !is_multicast_ether_addr(hdr->addr1) &&
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001814 (ieee80211_is_mgmt(hdr->frame_control) ||
1815 ieee80211_is_data(hdr->frame_control)) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001816 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001817 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001818 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001819 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001820 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001821 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001822 } else {
1823 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001824 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001825 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001826 }
1827
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001828 /* mesh power save support */
1829 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1830 ieee80211_mps_rx_h_sta_process(sta, hdr);
1831
Johannes Berg22403de2009-10-30 12:55:03 +01001832 /*
1833 * Drop (qos-)data::nullfunc frames silently, since they
1834 * are used only to control station power saving mode.
1835 */
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001836 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001837 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001838
1839 /*
1840 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001841 * that was not moved to a 4-addr STA vlan yet send
1842 * the event to userspace and for older hostapd drop
1843 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001844 */
1845 if (ieee80211_has_a4(hdr->frame_control) &&
1846 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1847 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001848 !rx->sdata->u.vlan.sta))) {
1849 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1850 cfg80211_rx_unexpected_4addr_frame(
1851 rx->sdata->dev, sta->sta.addr,
1852 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001853 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001854 }
Johannes Berg22403de2009-10-30 12:55:03 +01001855 /*
1856 * Update counter and free packet here to avoid
1857 * counting this as a dropped packed.
1858 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001859 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001860 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001861 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001862 }
1863
Johannes Berg9ae54c82008-01-31 19:48:20 +01001864 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001865} /* ieee80211_rx_h_sta_process */
1866
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001867static struct ieee80211_key *
1868ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1869{
1870 struct ieee80211_key *key = NULL;
1871 struct ieee80211_sub_if_data *sdata = rx->sdata;
1872 int idx2;
1873
1874 /* Make sure key gets set if either BIGTK key index is set so that
1875 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1876 * Beacon frames and Beacon frames that claim to use another BIGTK key
1877 * index (i.e., a key that we do not have).
1878 */
1879
1880 if (idx < 0) {
1881 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1882 idx2 = idx + 1;
1883 } else {
1884 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1885 idx2 = idx + 1;
1886 else
1887 idx2 = idx - 1;
1888 }
1889
1890 if (rx->sta)
1891 key = rcu_dereference(rx->sta->gtk[idx]);
1892 if (!key)
1893 key = rcu_dereference(sdata->keys[idx]);
1894 if (!key && rx->sta)
1895 key = rcu_dereference(rx->sta->gtk[idx2]);
1896 if (!key)
1897 key = rcu_dereference(sdata->keys[idx2]);
1898
1899 return key;
1900}
1901
Johan Almbladh86c228a2013-08-14 15:29:46 +02001902static ieee80211_rx_result debug_noinline
1903ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1904{
1905 struct sk_buff *skb = rx->skb;
1906 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1907 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1908 int keyidx;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001909 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1910 struct ieee80211_key *sta_ptk = NULL;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001911 struct ieee80211_key *ptk_idx = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001912 int mmie_keyidx = -1;
1913 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001914 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001915
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001916 if (ieee80211_is_ext(hdr->frame_control))
1917 return RX_CONTINUE;
1918
Johan Almbladh86c228a2013-08-14 15:29:46 +02001919 /*
1920 * Key selection 101
1921 *
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001922 * There are five types of keys:
Johan Almbladh86c228a2013-08-14 15:29:46 +02001923 * - GTK (group keys)
1924 * - IGTK (group keys for management frames)
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001925 * - BIGTK (group keys for Beacon frames)
Johan Almbladh86c228a2013-08-14 15:29:46 +02001926 * - PTK (pairwise keys)
1927 * - STK (station-to-station pairwise keys)
1928 *
1929 * When selecting a key, we have to distinguish between multicast
1930 * (including broadcast) and unicast frames, the latter can only
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001931 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1932 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1933 * then unicast frames can also use key indices like GTKs. Hence, if we
Johan Almbladh86c228a2013-08-14 15:29:46 +02001934 * don't have a PTK/STK we check the key index for a WEP key.
1935 *
1936 * Note that in a regular BSS, multicast frames are sent by the
1937 * AP only, associated stations unicast the frame to the AP first
1938 * which then multicasts it on their behalf.
1939 *
1940 * There is also a slight problem in IBSS mode: GTKs are negotiated
1941 * with each station, that is something we don't currently handle.
1942 * The spec seems to expect that one negotiates the same key with
1943 * every station but there's no such requirement; VLANs could be
1944 * possible.
1945 */
1946
Johan Almbladh86c228a2013-08-14 15:29:46 +02001947 /* start without a key */
1948 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001949 fc = hdr->frame_control;
1950
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001951 if (rx->sta) {
1952 int keyid = rx->sta->ptk_idx;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001953 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001954
Xing Song77dfc2b2021-11-01 10:46:57 +08001955 if (ieee80211_has_protected(fc) &&
1956 !(status->flag & RX_FLAG_IV_STRIPPED)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001957 cs = rx->sta->cipher_scheme;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001958 keyid = ieee80211_get_keyid(rx->skb, cs);
1959
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001960 if (unlikely(keyid < 0))
1961 return RX_DROP_UNUSABLE;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001962
1963 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001964 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001965 }
1966
Johan Almbladh86c228a2013-08-14 15:29:46 +02001967 if (!ieee80211_has_protected(fc))
1968 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1969
1970 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001971 rx->key = ptk_idx ? ptk_idx : sta_ptk;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001972 if ((status->flag & RX_FLAG_DECRYPTED) &&
1973 (status->flag & RX_FLAG_IV_STRIPPED))
1974 return RX_CONTINUE;
1975 /* Skip decryption if the frame is not protected. */
1976 if (!ieee80211_has_protected(fc))
1977 return RX_CONTINUE;
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001978 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
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 + NUM_DEFAULT_MGMT_KEYS ||
1985 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001986 NUM_DEFAULT_BEACON_KEYS) {
1987 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1988 skb->data,
1989 skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001990 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001991 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001992
1993 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1994 if (!rx->key)
1995 return RX_CONTINUE; /* Beacon protection not in use */
Johan Almbladh86c228a2013-08-14 15:29:46 +02001996 } else if (mmie_keyidx >= 0) {
1997 /* Broadcast/multicast robust management frame / BIP */
1998 if ((status->flag & RX_FLAG_DECRYPTED) &&
1999 (status->flag & RX_FLAG_IV_STRIPPED))
2000 return RX_CONTINUE;
2001
2002 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
2003 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
2004 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09002005 if (rx->sta) {
2006 if (ieee80211_is_group_privacy_action(skb) &&
2007 test_sta_flag(rx->sta, WLAN_STA_MFP))
2008 return RX_DROP_MONITOR;
2009
Johan Almbladh86c228a2013-08-14 15:29:46 +02002010 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09002011 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002012 if (!rx->key)
2013 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
2014 } else if (!ieee80211_has_protected(fc)) {
2015 /*
2016 * The frame was not protected, so skip decryption. However, we
2017 * need to set rx->key if there is a key that could have been
2018 * used so that the frame may be dropped if encryption would
2019 * have been expected.
2020 */
2021 struct ieee80211_key *key = NULL;
2022 struct ieee80211_sub_if_data *sdata = rx->sdata;
2023 int i;
2024
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002025 if (ieee80211_is_beacon(fc)) {
2026 key = ieee80211_rx_get_bigtk(rx, -1);
2027 } else if (ieee80211_is_mgmt(fc) &&
2028 is_multicast_ether_addr(hdr->addr1)) {
2029 key = rcu_dereference(rx->sdata->default_mgmt_key);
2030 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002031 if (rx->sta) {
2032 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2033 key = rcu_dereference(rx->sta->gtk[i]);
2034 if (key)
2035 break;
2036 }
2037 }
2038 if (!key) {
2039 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2040 key = rcu_dereference(sdata->keys[i]);
2041 if (key)
2042 break;
2043 }
2044 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002045 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002046 if (key)
2047 rx->key = key;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002048 return RX_CONTINUE;
2049 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002050 /*
2051 * The device doesn't give us the IV so we won't be
2052 * able to look up the key. That's ok though, we
2053 * don't need to decrypt the frame, we just won't
2054 * be able to keep statistics accurate.
2055 * Except for key threshold notifications, should
2056 * we somehow allow the driver to tell us which key
2057 * the hardware used if this flag is set?
2058 */
2059 if ((status->flag & RX_FLAG_DECRYPTED) &&
2060 (status->flag & RX_FLAG_IV_STRIPPED))
2061 return RX_CONTINUE;
2062
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002063 keyidx = ieee80211_get_keyid(rx->skb, cs);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002064
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002065 if (unlikely(keyidx < 0))
2066 return RX_DROP_UNUSABLE;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002067
2068 /* check per-station GTK first, if multicast packet */
2069 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2070 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2071
2072 /* if not found, try default key */
2073 if (!rx->key) {
2074 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2075
2076 /*
2077 * RSNA-protected unicast frames should always be
2078 * sent with pairwise or station-to-station keys,
2079 * but for WEP we allow using a key index as well.
2080 */
2081 if (rx->key &&
2082 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2083 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2084 !is_multicast_ether_addr(hdr->addr1))
2085 rx->key = NULL;
2086 }
2087 }
2088
2089 if (rx->key) {
2090 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2091 return RX_DROP_MONITOR;
2092
Johan Almbladh86c228a2013-08-14 15:29:46 +02002093 /* TODO: add threshold stuff again */
2094 } else {
2095 return RX_DROP_MONITOR;
2096 }
2097
2098 switch (rx->key->conf.cipher) {
2099 case WLAN_CIPHER_SUITE_WEP40:
2100 case WLAN_CIPHER_SUITE_WEP104:
2101 result = ieee80211_crypto_wep_decrypt(rx);
2102 break;
2103 case WLAN_CIPHER_SUITE_TKIP:
2104 result = ieee80211_crypto_tkip_decrypt(rx);
2105 break;
2106 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002107 result = ieee80211_crypto_ccmp_decrypt(
2108 rx, IEEE80211_CCMP_MIC_LEN);
2109 break;
2110 case WLAN_CIPHER_SUITE_CCMP_256:
2111 result = ieee80211_crypto_ccmp_decrypt(
2112 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002113 break;
2114 case WLAN_CIPHER_SUITE_AES_CMAC:
2115 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2116 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02002117 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2118 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2119 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02002120 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2121 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2122 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2123 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02002124 case WLAN_CIPHER_SUITE_GCMP:
2125 case WLAN_CIPHER_SUITE_GCMP_256:
2126 result = ieee80211_crypto_gcmp_decrypt(rx);
2127 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002128 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002129 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002130 }
2131
2132 /* the hdr variable is invalid after the decrypt handlers */
2133
2134 /* either the frame has been decrypted or will be dropped */
2135 status->flag |= RX_FLAG_DECRYPTED;
2136
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002137 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2138 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2139 skb->data, skb->len);
2140
Johan Almbladh86c228a2013-08-14 15:29:46 +02002141 return result;
2142}
2143
Johannes Berg3a11ce02021-05-11 20:02:47 +02002144void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2145{
2146 int i;
2147
2148 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2149 skb_queue_head_init(&cache->entries[i].skb_list);
2150}
2151
2152void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2153{
2154 int i;
2155
2156 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2157 __skb_queue_purge(&cache->entries[i].skb_list);
2158}
2159
Johannes Berg571ecf62007-07-27 15:43:22 +02002160static inline struct ieee80211_fragment_entry *
Johannes Berg3a11ce02021-05-11 20:02:47 +02002161ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
Johannes Berg571ecf62007-07-27 15:43:22 +02002162 unsigned int frag, unsigned int seq, int rx_queue,
2163 struct sk_buff **skb)
2164{
2165 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02002166
Johannes Berg3a11ce02021-05-11 20:02:47 +02002167 entry = &cache->entries[cache->next++];
2168 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2169 cache->next = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002170
Johannes Berg3a11ce02021-05-11 20:02:47 +02002171 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02002172
2173 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2174 *skb = NULL;
2175 entry->first_frag_time = jiffies;
2176 entry->seq = seq;
2177 entry->rx_queue = rx_queue;
2178 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002179 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02002180 entry->extra_len = 0;
2181
2182 return entry;
2183}
2184
2185static inline struct ieee80211_fragment_entry *
Johannes Berg3a11ce02021-05-11 20:02:47 +02002186ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002187 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02002188 int rx_queue, struct ieee80211_hdr *hdr)
2189{
2190 struct ieee80211_fragment_entry *entry;
2191 int i, idx;
2192
Johannes Berg3a11ce02021-05-11 20:02:47 +02002193 idx = cache->next;
Johannes Berg571ecf62007-07-27 15:43:22 +02002194 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2195 struct ieee80211_hdr *f_hdr;
David S. Miller7957a9d2018-08-06 22:49:13 -07002196 struct sk_buff *f_skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002197
2198 idx--;
2199 if (idx < 0)
2200 idx = IEEE80211_FRAGMENT_MAX - 1;
2201
Johannes Berg3a11ce02021-05-11 20:02:47 +02002202 entry = &cache->entries[idx];
Johannes Berg571ecf62007-07-27 15:43:22 +02002203 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2204 entry->rx_queue != rx_queue ||
2205 entry->last_frag + 1 != frag)
2206 continue;
2207
David S. Miller7957a9d2018-08-06 22:49:13 -07002208 f_skb = __skb_peek(&entry->skb_list);
2209 f_hdr = (struct ieee80211_hdr *) f_skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002210
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002211 /*
2212 * Check ftype and addresses are equal, else check next fragment
2213 */
2214 if (((hdr->frame_control ^ f_hdr->frame_control) &
2215 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002216 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2217 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02002218 continue;
2219
S.Çağlar Onurab466232008-02-14 17:36:47 +02002220 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002221 __skb_queue_purge(&entry->skb_list);
2222 continue;
2223 }
2224 return entry;
2225 }
2226
2227 return NULL;
2228}
2229
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002230static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2231{
2232 return rx->key &&
2233 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2234 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2235 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2236 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2237 ieee80211_has_protected(fc);
2238}
2239
Johannes Berg49461622008-06-30 15:10:45 +02002240static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002241ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002242{
Johannes Berg3a11ce02021-05-11 20:02:47 +02002243 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
Johannes Berg571ecf62007-07-27 15:43:22 +02002244 struct ieee80211_hdr *hdr;
2245 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002246 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02002247 unsigned int frag, seq;
2248 struct ieee80211_fragment_entry *entry;
2249 struct sk_buff *skb;
Wen Gong3edc6b02021-05-11 20:02:51 +02002250 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002251
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002252 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002253 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002254
Thomas Pedersen09a740c2020-09-21 19:28:14 -07002255 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002256 return RX_CONTINUE;
2257
Johannes Berg571ecf62007-07-27 15:43:22 +02002258 sc = le16_to_cpu(hdr->seq_ctrl);
2259 frag = sc & IEEE80211_SCTL_FRAG;
2260
Johannes Berg3a11ce02021-05-11 20:02:47 +02002261 if (rx->sta)
2262 cache = &rx->sta->frags;
2263
Andreas Müllerd0259332014-12-12 12:11:11 +01002264 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2265 goto out;
2266
Johannes Berga9799542021-06-09 16:13:06 +02002267 if (is_multicast_ether_addr(hdr->addr1))
2268 return RX_DROP_MONITOR;
2269
Johannes Berg571ecf62007-07-27 15:43:22 +02002270 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2271
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08002272 if (skb_linearize(rx->skb))
2273 return RX_DROP_UNUSABLE;
2274
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07002275 /*
2276 * skb_linearize() might change the skb->data and
2277 * previously cached variables (in this case, hdr) need to
2278 * be refreshed with the new data.
2279 */
2280 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002281 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2282
2283 if (frag == 0) {
2284 /* This is the first fragment of a new frame. */
Johannes Berg3a11ce02021-05-11 20:02:47 +02002285 entry = ieee80211_reassemble_add(cache, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002286 rx->seqno_idx, &(rx->skb));
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002287 if (requires_sequential_pn(rx, fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02002288 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002289
2290 /* Store CCMP/GCMP PN so that we can verify that the
2291 * next fragment has a sequential PN value.
2292 */
2293 entry->check_sequential_pn = true;
Johannes Berg7e44a0b2021-05-11 20:02:49 +02002294 entry->is_protected = true;
Mathy Vanhoef94034c42021-05-11 20:02:43 +02002295 entry->key_color = rx->key->color;
Johannes Berg571ecf62007-07-27 15:43:22 +02002296 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07002297 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02002298 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01002299 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2300 u.ccmp.rx_pn) !=
2301 offsetof(struct ieee80211_key,
2302 u.gcmp.rx_pn));
2303 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2304 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2305 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2306 IEEE80211_GCMP_PN_LEN);
Wen Gong3edc6b02021-05-11 20:02:51 +02002307 } else if (rx->key &&
2308 (ieee80211_has_protected(fc) ||
2309 (status->flag & RX_FLAG_DECRYPTED))) {
Johannes Berg7e44a0b2021-05-11 20:02:49 +02002310 entry->is_protected = true;
2311 entry->key_color = rx->key->color;
Johannes Berg571ecf62007-07-27 15:43:22 +02002312 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002313 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002314 }
2315
2316 /* This is a fragment for a frame that should already be pending in
2317 * fragment cache. Add this fragment to the end of the pending entry.
2318 */
Johannes Berg3a11ce02021-05-11 20:02:47 +02002319 entry = ieee80211_reassemble_find(cache, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002320 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02002321 if (!entry) {
2322 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002323 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002324 }
2325
Johannes Berg9acc54b2016-02-26 22:13:40 +01002326 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2327 * MPDU PN values are not incrementing in steps of 1."
2328 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2329 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2330 */
2331 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002332 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002333 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002334
Mathy Vanhoef965a7d72021-05-11 20:02:42 +02002335 if (!requires_sequential_pn(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002336 return RX_DROP_UNUSABLE;
Mathy Vanhoef94034c42021-05-11 20:02:43 +02002337
2338 /* Prevent mixed key and fragment cache attacks */
2339 if (entry->key_color != rx->key->color)
2340 return RX_DROP_UNUSABLE;
2341
Johannes Berg4325f6c2013-05-08 13:09:08 +02002342 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2343 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002344 pn[i]++;
2345 if (pn[i])
2346 break;
2347 }
Johannes Bergbf30ca92021-05-11 20:02:48 +02002348
2349 rpn = rx->ccm_gcm.pn;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002350 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002351 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002352 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg7e44a0b2021-05-11 20:02:49 +02002353 } else if (entry->is_protected &&
Wen Gong3edc6b02021-05-11 20:02:51 +02002354 (!rx->key ||
2355 (!ieee80211_has_protected(fc) &&
2356 !(status->flag & RX_FLAG_DECRYPTED)) ||
Johannes Berg7e44a0b2021-05-11 20:02:49 +02002357 rx->key->color != entry->key_color)) {
2358 /* Drop this as a mixed key or fragment cache attack, even
2359 * if for TKIP Michael MIC should protect us, and WEP is a
2360 * lost cause anyway.
2361 */
2362 return RX_DROP_UNUSABLE;
Wen Gong3edc6b02021-05-11 20:02:51 +02002363 } else if (entry->is_protected && rx->key &&
2364 entry->key_color != rx->key->color &&
2365 (status->flag & RX_FLAG_DECRYPTED)) {
2366 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002367 }
2368
Harvey Harrison358c8d92008-07-15 18:44:13 -07002369 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002370 __skb_queue_tail(&entry->skb_list, rx->skb);
2371 entry->last_frag = frag;
2372 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002373 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002374 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002375 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002376 }
2377
2378 rx->skb = __skb_dequeue(&entry->skb_list);
2379 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002380 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002381 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2382 GFP_ATOMIC))) {
2383 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2384 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002385 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002386 }
2387 }
2388 while ((skb = __skb_dequeue(&entry->skb_list))) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02002389 skb_put_data(rx->skb, skb->data, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02002390 dev_kfree_skb(skb);
2391 }
2392
Johannes Berg571ecf62007-07-27 15:43:22 +02002393 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002394 ieee80211_led_rx(rx->local);
Johannes Berg571ecf62007-07-27 15:43:22 +02002395 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002396 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002397 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002398}
2399
Johannes Berg1df332e2012-10-26 00:09:11 +02002400static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002401{
Johannes Berg1df332e2012-10-26 00:09:11 +02002402 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002403 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002404
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002405 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002406}
2407
Johannes Berg1df332e2012-10-26 00:09:11 +02002408static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002409{
Markus Theil0b467b632020-06-25 12:42:14 +02002410 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002411 struct sk_buff *skb = rx->skb;
2412 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2413
Johannes Berg3017b802007-08-28 17:01:53 -04002414 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002415 * Pass through unencrypted frames if the hardware has
2416 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002417 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002418 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002419 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002420
Markus Theil0b467b632020-06-25 12:42:14 +02002421 /* check mesh EAPOL frames first */
2422 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2423 ieee80211_is_data(fc))) {
2424 struct ieee80211s_hdr *mesh_hdr;
2425 u16 hdr_len = ieee80211_hdrlen(fc);
2426 u16 ethertype_offset;
2427 __be16 ethertype;
2428
2429 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2430 goto drop_check;
2431
2432 /* make sure fixed part of mesh header is there, also checks skb len */
2433 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2434 goto drop_check;
2435
2436 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2437 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2438 sizeof(rfc1042_header);
2439
2440 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2441 ethertype == rx->sdata->control_port_protocol)
2442 return 0;
2443 }
2444
2445drop_check:
Johannes Berg571ecf62007-07-27 15:43:22 +02002446 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002447 if (unlikely(!ieee80211_has_protected(fc) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002448 !ieee80211_is_any_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002449 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002450 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002451
2452 return 0;
2453}
2454
Johannes Berg1df332e2012-10-26 00:09:11 +02002455static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002456{
2457 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002458 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002459 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002460
Jouni Malinene3efca02010-03-28 22:31:15 -07002461 /*
2462 * Pass through unencrypted frames if the hardware has
2463 * decrypted them already.
2464 */
2465 if (status->flag & RX_FLAG_DECRYPTED)
2466 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002467
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002468 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002469 if (unlikely(!ieee80211_has_protected(fc) &&
2470 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002471 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002472 if (ieee80211_is_deauth(fc) ||
2473 ieee80211_is_disassoc(fc))
2474 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2475 rx->skb->data,
2476 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002477 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002478 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002479 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002480 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002481 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002482 if (ieee80211_is_deauth(fc) ||
2483 ieee80211_is_disassoc(fc))
2484 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2485 rx->skb->data,
2486 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002487 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002488 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002489 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002490 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2491 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2492 rx->skb->data,
2493 rx->skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002494 return -EACCES;
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002495 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002496 /*
2497 * When using MFP, Action frames are not allowed prior to
2498 * having configured keys.
2499 */
2500 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002501 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002502 return -EACCES;
2503 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002504
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002505 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002506}
2507
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002508static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002509__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002510{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002511 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002512 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002513 bool check_port_control = false;
2514 struct ethhdr *ehdr;
2515 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002516
Felix Fietkau4114fa22011-04-12 19:15:22 +02002517 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002518 if (ieee80211_has_a4(hdr->frame_control) &&
2519 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002520 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002521
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002522 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2523 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2524
2525 if (!sdata->u.mgd.use_4addr)
2526 return -1;
Sathishkumar Muruganandam1ecef202018-08-28 17:51:38 +05302527 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002528 check_port_control = true;
2529 }
2530
Johannes Berg9bc383d2009-11-19 11:55:19 +01002531 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002532 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002533 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002534
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002535 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002536 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002537 return ret;
2538
2539 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002540 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2541 *port_control = true;
2542 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002543 return -1;
2544
2545 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002546}
Johannes Berg571ecf62007-07-27 15:43:22 +02002547
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002548/*
2549 * requires that rx->skb is a frame with ethernet header
2550 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002551static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002552{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302553 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002554 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2555 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2556
2557 /*
Johannes Berga8c4d762021-05-11 20:02:50 +02002558 * Allow EAPOL frames to us/the PAE group address regardless of
2559 * whether the frame was encrypted or not, and always disallow
2560 * all other destination addresses for them.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002561 */
Johannes Berga8c4d762021-05-11 20:02:50 +02002562 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2563 return ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2564 ether_addr_equal(ehdr->h_dest, pae_group_addr);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002565
2566 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002567 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002568 return false;
2569
2570 return true;
2571}
2572
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002573static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2574 struct ieee80211_rx_data *rx)
2575{
2576 struct ieee80211_sub_if_data *sdata = rx->sdata;
2577 struct net_device *dev = sdata->dev;
2578
2579 if (unlikely((skb->protocol == sdata->control_port_protocol ||
Markus Theil7f3f96c2020-03-12 10:10:54 +01002580 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2581 !sdata->control_port_no_preauth)) &&
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002582 sdata->control_port_over_nl80211)) {
2583 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Denis Kenziorf8b43c52019-08-27 17:41:20 -05002584 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002585
Denis Kenziora948f712018-07-03 15:05:48 -05002586 cfg80211_rx_control_port(dev, skb, noencrypt);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002587 dev_kfree_skb(skb);
2588 } else {
Johannes Berga8c4d762021-05-11 20:02:50 +02002589 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2590
Denis Kenziorc8a41c62019-08-27 17:41:19 -05002591 memset(skb->cb, 0, sizeof(skb->cb));
2592
Johannes Berga8c4d762021-05-11 20:02:50 +02002593 /*
2594 * 802.1X over 802.11 requires that the authenticator address
2595 * be used for EAPOL frames. However, 802.1X allows the use of
2596 * the PAE group address instead. If the interface is part of
2597 * a bridge and we pass the frame with the PAE group address,
2598 * then the bridge will forward it to the network (even if the
2599 * client was not associated yet), which isn't supposed to
2600 * happen.
2601 * To avoid that, rewrite the destination address to our own
2602 * address, so that the authenticator (e.g. hostapd) will see
2603 * the frame, but bridge won't forward it anywhere else. Note
2604 * that due to earlier filtering, the only other address can
2605 * be the PAE group address.
2606 */
2607 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2608 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2609 ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2610
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002611 /* deliver to local stack */
Felix Fietkauc5d16862020-07-26 13:06:11 +02002612 if (rx->list)
2613 list_add_tail(&skb->list, rx->list);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002614 else
2615 netif_receive_skb(skb);
2616 }
2617}
2618
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002619/*
2620 * requires that rx->skb is a frame with ethernet header
2621 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002622static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002623ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002624{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002625 struct ieee80211_sub_if_data *sdata = rx->sdata;
2626 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002627 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002628 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2629 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002630
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002631 skb = rx->skb;
2632 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002633
Lev Stipakov36ec1442020-11-13 23:46:24 +02002634 dev_sw_netstats_rx_add(dev, skb->len);
Johannes Berg5a490512015-04-22 17:10:38 +02002635
Johannes Bergde8f18d2016-03-31 20:02:03 +03002636 if (rx->sta) {
2637 /* The seqno index has the same property as needed
2638 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2639 * for non-QoS-data frames. Here we know it's a data
2640 * frame, so count MSDUs.
2641 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002642 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002643 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002644 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002645 }
2646
Johannes Berg05c914f2008-09-11 00:01:58 +02002647 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2648 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002649 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berga8c4d762021-05-11 20:02:50 +02002650 ehdr->h_proto != rx->sdata->control_port_protocol &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002651 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002652 if (is_multicast_ether_addr(ehdr->h_dest) &&
2653 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002654 /*
2655 * send multicast frames both to higher layers in
2656 * local net stack and back to the wireless medium
2657 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002658 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002659 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002660 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002661 dev->name);
Johannes Berg42dca5e2018-10-09 10:00:21 +02002662 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2663 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2664 dsta = sta_info_get(sdata, ehdr->h_dest);
Johannes Bergabe60632009-11-25 17:46:18 +01002665 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002666 /*
2667 * The destination station is associated to
2668 * this AP (in this VLAN), so send the frame
2669 * directly to it and do not pass it to local
2670 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002671 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002672 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002673 skb = NULL;
2674 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002675 }
2676 }
2677
Kalle Valo59d9cb02009-12-17 13:54:57 +01002678#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002679 if (skb) {
2680 /* 'align' will only take the values 0 or 2 here since all
2681 * frames are required to be aligned to 2-byte boundaries
2682 * when being passed to mac80211; the code here works just
2683 * as well if that isn't true, but mac80211 assumes it can
2684 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002685 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002686 int align;
2687
2688 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002689 if (align) {
2690 if (WARN_ON(skb_headroom(skb) < 3)) {
2691 dev_kfree_skb(skb);
2692 skb = NULL;
2693 } else {
2694 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002695 size_t len = skb_headlen(skb);
2696 skb->data -= align;
2697 memmove(skb->data, data, len);
2698 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002699 }
2700 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002701 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002702#endif
2703
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002704 if (skb) {
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002705 skb->protocol = eth_type_trans(skb, dev);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002706 ieee80211_deliver_skb_to_local_stack(skb, rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002707 }
2708
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002709 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002710 /*
2711 * Send to wireless media and increase priority by 256 to
2712 * keep the received priority instead of reclassifying
2713 * the frame (see cfg80211_classify8021d).
2714 */
2715 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002716 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002717 skb_reset_network_header(xmit_skb);
2718 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002719 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002720 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002721}
2722
Johannes Berg49461622008-06-30 15:10:45 +02002723static ieee80211_rx_result debug_noinline
Felix Fietkau24bba072018-02-27 13:03:07 +01002724__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002725{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002726 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002727 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002728 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2729 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002730 struct sk_buff_head frame_list;
Johannes Berg7f6990c2016-10-05 15:29:49 +02002731 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002732 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002733
Johannes Bergea720932016-10-05 10:14:42 +02002734 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
Johannes Berge2b52272016-10-05 16:42:06 +02002735 check_da = NULL;
2736 check_sa = NULL;
2737 } else switch (rx->sdata->vif.type) {
2738 case NL80211_IFTYPE_AP:
2739 case NL80211_IFTYPE_AP_VLAN:
2740 check_da = NULL;
2741 break;
2742 case NL80211_IFTYPE_STATION:
2743 if (!rx->sta ||
2744 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2745 check_sa = NULL;
2746 break;
2747 case NL80211_IFTYPE_MESH_POINT:
2748 check_sa = NULL;
2749 break;
2750 default:
2751 break;
Johannes Bergea720932016-10-05 10:14:42 +02002752 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002753
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002754 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002755 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002756
Johannes Berg7f6990c2016-10-05 15:29:49 +02002757 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2758 rx->sdata->vif.addr,
Felix Fietkau24bba072018-02-27 13:03:07 +01002759 rx->sdata->vif.type,
Mathy Vanhoefa1d5ff52021-05-11 20:02:44 +02002760 data_offset, true))
Johannes Berg7f6990c2016-10-05 15:29:49 +02002761 return RX_DROP_UNUSABLE;
2762
Zhu Yieaf85ca2009-12-01 10:18:37 +08002763 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2764 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002765 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002766 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002767
Zhu Yieaf85ca2009-12-01 10:18:37 +08002768 while (!skb_queue_empty(&frame_list)) {
2769 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002770
Harvey Harrison358c8d92008-07-15 18:44:13 -07002771 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002772 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002773 continue;
2774 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002775
2776 ieee80211_deliver_skb(rx);
2777 }
2778
Johannes Berg9ae54c82008-01-31 19:48:20 +01002779 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002780}
2781
Felix Fietkau24bba072018-02-27 13:03:07 +01002782static ieee80211_rx_result debug_noinline
2783ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2784{
2785 struct sk_buff *skb = rx->skb;
2786 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2787 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2788 __le16 fc = hdr->frame_control;
2789
2790 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2791 return RX_CONTINUE;
2792
2793 if (unlikely(!ieee80211_is_data(fc)))
2794 return RX_CONTINUE;
2795
2796 if (unlikely(!ieee80211_is_data_present(fc)))
2797 return RX_DROP_MONITOR;
2798
2799 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2800 switch (rx->sdata->vif.type) {
2801 case NL80211_IFTYPE_AP_VLAN:
2802 if (!rx->sdata->u.vlan.sta)
2803 return RX_DROP_UNUSABLE;
2804 break;
2805 case NL80211_IFTYPE_STATION:
2806 if (!rx->sdata->u.mgd.use_4addr)
2807 return RX_DROP_UNUSABLE;
2808 break;
2809 default:
2810 return RX_DROP_UNUSABLE;
2811 }
2812 }
2813
2814 if (is_multicast_ether_addr(hdr->addr1))
2815 return RX_DROP_UNUSABLE;
2816
Johannes Berg270032a2021-05-11 20:02:46 +02002817 if (rx->key) {
2818 /*
2819 * We should not receive A-MSDUs on pre-HT connections,
2820 * and HT connections cannot use old ciphers. Thus drop
2821 * them, as in those cases we couldn't even have SPP
2822 * A-MSDUs or such.
2823 */
2824 switch (rx->key->conf.cipher) {
2825 case WLAN_CIPHER_SUITE_WEP40:
2826 case WLAN_CIPHER_SUITE_WEP104:
2827 case WLAN_CIPHER_SUITE_TKIP:
2828 return RX_DROP_UNUSABLE;
2829 default:
2830 break;
2831 }
2832 }
2833
Felix Fietkau24bba072018-02-27 13:03:07 +01002834 return __ieee80211_rx_h_amsdu(rx, 0);
2835}
2836
Ingo Molnarbf94e172008-10-12 23:51:38 -07002837#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002838static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002839ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2840{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002841 struct ieee80211_hdr *fwd_hdr, *hdr;
2842 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002843 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002844 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002845 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002846 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002847 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002848 u16 ac, q, hdrlen;
Felix Fietkau51d0af22019-02-22 13:21:15 +01002849 int tailroom = 0;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002850
2851 hdr = (struct ieee80211_hdr *) skb->data;
2852 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002853
2854 /* make sure fixed part of mesh header is there, also checks skb len */
2855 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2856 return RX_DROP_MONITOR;
2857
2858 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2859
2860 /* make sure full mesh header is there, also checks skb len */
2861 if (!pskb_may_pull(rx->skb,
2862 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2863 return RX_DROP_MONITOR;
2864
2865 /* reload pointers */
2866 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002867 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2868
Bob Copelandd0c22112015-03-02 14:28:52 -05002869 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2870 return RX_DROP_MONITOR;
2871
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002872 /* frame is in RMC, don't forward */
2873 if (ieee80211_is_data(hdr->frame_control) &&
2874 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002875 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002876 return RX_DROP_MONITOR;
2877
Johannes Berg5c900672015-04-22 14:48:34 +02002878 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002879 return RX_CONTINUE;
2880
2881 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002882 return RX_DROP_MONITOR;
2883
Javier Cardona43b7b312009-10-15 18:10:51 -07002884 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002885 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002886 char *proxied_addr;
2887 char *mpp_addr;
2888
2889 if (is_multicast_ether_addr(hdr->addr1)) {
2890 mpp_addr = hdr->addr3;
2891 proxied_addr = mesh_hdr->eaddr1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -07002892 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2893 MESH_FLAGS_AE_A5_A6) {
Johannes Berg9b395bc2012-10-26 00:36:40 +02002894 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002895 mpp_addr = hdr->addr4;
2896 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002897 } else {
2898 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002899 }
YanBo79617de2008-09-22 13:30:32 +08002900
YanBo79617de2008-09-22 13:30:32 +08002901 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002902 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002903 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002904 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002905 } else {
2906 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002907 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002908 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002909 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002910 spin_unlock_bh(&mppath->state_lock);
2911 }
2912 rcu_read_unlock();
2913 }
2914
Javier Cardona3c5772a2009-08-10 12:15:48 -07002915 /* Frame has reached destination. Don't forward */
2916 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002917 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002918 return RX_CONTINUE;
2919
Michal Kaziorcf440122016-01-25 14:43:24 +01002920 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2921 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002922 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002923 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002924 return RX_DROP_MONITOR;
2925 }
2926 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002927
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002928 if (!--mesh_hdr->ttl) {
Bob Copelanda0dc0202019-01-17 16:32:42 -05002929 if (!is_multicast_ether_addr(hdr->addr1))
2930 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2931 dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002932 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002933 }
2934
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002935 if (!ifmsh->mshcfg.dot11MeshForwarding)
2936 goto out;
2937
Felix Fietkau51d0af22019-02-22 13:21:15 +01002938 if (sdata->crypto_tx_tailroom_needed_cnt)
2939 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2940
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002941 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
Felix Fietkau51d0af22019-02-22 13:21:15 +01002942 sdata->encrypt_headroom,
2943 tailroom, GFP_ATOMIC);
Joe Perches0c3d5a92018-03-12 08:07:12 -07002944 if (!fwd_skb)
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002945 goto out;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002946
2947 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002948 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002949 info = IEEE80211_SKB_CB(fwd_skb);
2950 memset(info, 0, sizeof(*info));
Felix Fietkaucc20ff22020-09-08 14:36:57 +02002951 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002952 info->control.vif = &rx->sdata->vif;
2953 info->control.jiffies = jiffies;
2954 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2955 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2956 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002957 /* update power mode indication when forwarding */
2958 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002959 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002960 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002961 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2962 } else {
2963 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002964 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002965 fwd_hdr->addr3, 0,
2966 WLAN_REASON_MESH_PATH_NOFORWARD,
2967 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002968 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002969 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002970 return RX_DROP_MONITOR;
2971 }
2972
2973 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2974 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002975 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002976 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002977 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002978 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002979}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002980#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002981
2982static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002983ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002984{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002985 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302986 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002987 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002988 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2989 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002990 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002991 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002992
Harvey Harrison358c8d92008-07-15 18:44:13 -07002993 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002994 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002995
Harvey Harrison358c8d92008-07-15 18:44:13 -07002996 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002997 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002998
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002999 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01003000 * Send unexpected-4addr-frame event to hostapd. For older versions,
3001 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01003002 */
3003 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01003004 sdata->vif.type == NL80211_IFTYPE_AP) {
3005 if (rx->sta &&
3006 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3007 cfg80211_rx_unexpected_4addr_frame(
3008 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01003009 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01003010 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01003011
Felix Fietkau4114fa22011-04-12 19:15:22 +02003012 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02003013 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01003014 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02003015
Harvey Harrison358c8d92008-07-15 18:44:13 -07003016 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01003017 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01003018
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02003019 /* directly handle TDLS channel switch requests/responses */
3020 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3021 cpu_to_be16(ETH_P_TDLS))) {
3022 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3023
3024 if (pskb_may_pull(rx->skb,
3025 offsetof(struct ieee80211_tdls_data, u)) &&
3026 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3027 tf->category == WLAN_CATEGORY_TDLS &&
3028 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3029 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Johannes Bergf057d142021-05-17 23:07:56 +02003030 rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3031 __ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02003032 return RX_QUEUED;
3033 }
3034 }
3035
Felix Fietkau4114fa22011-04-12 19:15:22 +02003036 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3037 unlikely(port_control) && sdata->bss) {
3038 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3039 u.ap);
3040 dev = sdata->dev;
3041 rx->sdata = sdata;
3042 }
3043
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02003044 rx->skb->dev = dev;
3045
Johannes Berg602fae42016-03-17 15:02:52 +02003046 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3047 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05303048 !is_multicast_ether_addr(
3049 ((struct ethhdr *)rx->skb->data)->h_dest) &&
3050 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02003051 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3052 mod_timer(&local->dynamic_ps_timer, jiffies +
3053 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05303054
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02003055 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02003056
Johannes Berg9ae54c82008-01-31 19:48:20 +01003057 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003058}
3059
Johannes Berg49461622008-06-30 15:10:45 +02003060static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003061ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02003062{
Ron Rindjunsky71364712007-12-25 17:00:36 +02003063 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07003064 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003065 struct tid_ampdu_rx *tid_agg_rx;
3066 u16 start_seq_num;
3067 u16 tid;
3068
Harvey Harrisona7767f92008-07-02 16:30:51 -07003069 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003070 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003071
Harvey Harrisona7767f92008-07-02 16:30:51 -07003072 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02003073 struct {
3074 __le16 control, start_seq_num;
3075 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003076 struct ieee80211_event event = {
3077 .type = BAR_RX_EVENT,
3078 };
Johannes Berg8ae59772010-05-30 14:52:58 +02003079
Ron Rindjunsky71364712007-12-25 17:00:36 +02003080 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01003081 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02003082
3083 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3084 &bar_data, sizeof(bar_data)))
3085 return RX_DROP_MONITOR;
3086
Johannes Berg8ae59772010-05-30 14:52:58 +02003087 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02003088
Johannes Berg53f24972016-08-29 23:25:19 +03003089 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3090 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3091 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3092 WLAN_BACK_RECIPIENT,
3093 WLAN_REASON_QSTA_REQUIRE_SETUP);
3094
Johannes Berga87f7362010-06-10 10:21:38 +02003095 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3096 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01003097 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003098
Johannes Berg8ae59772010-05-30 14:52:58 +02003099 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003100 event.u.ba.tid = tid;
3101 event.u.ba.ssn = start_seq_num;
3102 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003103
3104 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01003105 if (tid_agg_rx->timeout)
3106 mod_timer(&tid_agg_rx->session_timer,
3107 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02003108
Johannes Bergdd318572010-11-29 11:09:16 +01003109 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01003110 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02003111 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003112 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01003113 spin_unlock(&tid_agg_rx->reorder_lock);
3114
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003115 drv_event_callback(rx->local, rx->sdata, &event);
3116
Johannes Berga02ae752009-11-16 12:00:40 +01003117 kfree_skb(skb);
3118 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003119 }
3120
Johannes Berg08daeca2010-05-30 14:53:43 +02003121 /*
3122 * After this point, we only want management frames,
3123 * so we can drop all remaining control frames to
3124 * cooked monitor interfaces.
3125 */
3126 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003127}
3128
Jouni Malinenf4f727a2009-01-10 11:46:53 +02003129static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3130 struct ieee80211_mgmt *mgmt,
3131 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02003132{
3133 struct ieee80211_local *local = sdata->local;
3134 struct sk_buff *skb;
3135 struct ieee80211_mgmt *resp;
3136
Joe Perchesb203ca32012-05-08 18:56:52 +00003137 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02003138 /* Not to own unicast address */
3139 return;
3140 }
3141
Joe Perchesb203ca32012-05-08 18:56:52 +00003142 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3143 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003144 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02003145 return;
3146 }
3147
3148 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3149 /* Too short SA Query request frame */
3150 return;
3151 }
3152
3153 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3154 if (skb == NULL)
3155 return;
3156
3157 skb_reserve(skb, local->hw.extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02003158 resp = skb_put_zero(skb, 24);
Jouni Malinenfea14732009-01-08 13:32:06 +02003159 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01003160 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01003161 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02003162 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3163 IEEE80211_STYPE_ACTION);
3164 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3165 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3166 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3167 memcpy(resp->u.action.u.sa_query.trans_id,
3168 mgmt->u.action.u.sa_query.trans_id,
3169 WLAN_SA_QUERY_TR_ID_LEN);
3170
Johannes Berg62ae67b2009-11-18 18:42:05 +01003171 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02003172}
3173
Johannes Berg49461622008-06-30 15:10:45 +02003174static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003175ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3176{
3177 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003178 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003179
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003180 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3181 return RX_CONTINUE;
3182
Johannes Berg2e161f782010-08-12 15:38:38 +02003183 /*
3184 * From here on, look only at management frames.
3185 * Data and control frames are already handled,
3186 * and unknown (reserved) frames are useless.
3187 */
3188 if (rx->skb->len < 24)
3189 return RX_DROP_MONITOR;
3190
3191 if (!ieee80211_is_mgmt(mgmt->frame_control))
3192 return RX_DROP_MONITOR;
3193
Johannes Bergee971922011-11-04 11:18:18 +01003194 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3195 ieee80211_is_beacon(mgmt->frame_control) &&
3196 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01003197 int sig = 0;
3198
Tosoni1ad22fb2018-03-14 16:58:34 +00003199 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3200 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003201 sig = status->signal;
3202
Thomas Pedersene76fede2020-04-30 10:25:50 -07003203 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3204 rx->skb->data, rx->skb->len,
3205 ieee80211_rx_status_to_khz(status),
3206 sig);
Johannes Bergee971922011-11-04 11:18:18 +01003207 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3208 }
3209
Johannes Berg2e161f782010-08-12 15:38:38 +02003210 if (ieee80211_drop_unencrypted_mgmt(rx))
3211 return RX_DROP_UNUSABLE;
3212
3213 return RX_CONTINUE;
3214}
3215
Lorenzo Bianconif5a4c242021-08-23 20:02:39 +02003216static bool
3217ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3218{
3219 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
Lorenzo Bianconif5a4c242021-08-23 20:02:39 +02003220 struct ieee80211_sub_if_data *sdata = rx->sdata;
Lorenzo Bianconif5a4c242021-08-23 20:02:39 +02003221
3222 /* TWT actions are only supported in AP for the moment */
3223 if (sdata->vif.type != NL80211_IFTYPE_AP)
3224 return false;
3225
3226 if (!rx->local->ops->add_twt_setup)
3227 return false;
3228
Lorenzo Bianconi68ba1132021-08-27 10:07:19 +02003229 if (!sdata->vif.bss_conf.twt_responder)
Lorenzo Bianconif5a4c242021-08-23 20:02:39 +02003230 return false;
3231
3232 if (!rx->sta)
3233 return false;
3234
3235 switch (mgmt->u.action.u.s1g.action_code) {
3236 case WLAN_S1G_TWT_SETUP: {
3237 struct ieee80211_twt_setup *twt;
3238
3239 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3240 1 + /* action code */
3241 sizeof(struct ieee80211_twt_setup) +
3242 2 /* TWT req_type agrt */)
3243 break;
3244
3245 twt = (void *)mgmt->u.action.u.s1g.variable;
3246 if (twt->element_id != WLAN_EID_S1G_TWT)
3247 break;
3248
3249 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3250 4 + /* action code + token + tlv */
3251 twt->length)
3252 break;
3253
3254 return true; /* queue the frame */
3255 }
3256 case WLAN_S1G_TWT_TEARDOWN:
3257 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE + 2)
3258 break;
3259
3260 return true; /* queue the frame */
3261 default:
3262 break;
3263 }
3264
3265 return false;
3266}
3267
Johannes Berg2e161f782010-08-12 15:38:38 +02003268static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02003269ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3270{
3271 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003272 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02003273 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003274 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02003275 int len = rx->skb->len;
3276
3277 if (!ieee80211_is_action(mgmt->frame_control))
3278 return RX_CONTINUE;
3279
Jouni Malinen026331c2010-02-15 12:53:10 +02003280 /* drop too small frames */
3281 if (len < IEEE80211_MIN_ACTION_SIZE)
3282 return RX_DROP_UNUSABLE;
3283
Marco Porsch815b8092012-12-05 15:04:26 -08003284 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003285 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3286 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02003287 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02003288
Johannes Bergde1ede72008-09-09 14:42:50 +02003289 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003290 case WLAN_CATEGORY_HT:
3291 /* reject HT action frames from stations not supporting HT */
3292 if (!rx->sta->sta.ht_cap.ht_supported)
3293 goto invalid;
3294
3295 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3296 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3297 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3298 sdata->vif.type != NL80211_IFTYPE_AP &&
3299 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3300 break;
3301
Johannes Bergec61cd62012-12-28 12:12:10 +01003302 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003303 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3304 goto invalid;
3305
3306 switch (mgmt->u.action.u.ht_smps.action) {
3307 case WLAN_HT_ACTION_SMPS: {
3308 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003309 enum ieee80211_smps_mode smps_mode;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303310 struct sta_opmode_info sta_opmode = {};
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003311
Ilan Peerc4d800d2020-01-31 13:12:53 +02003312 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3313 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3314 goto handled;
3315
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003316 /* convert to HT capability */
3317 switch (mgmt->u.action.u.ht_smps.smps_control) {
3318 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003319 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003320 break;
3321 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003322 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003323 break;
3324 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003325 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003326 break;
3327 default:
3328 goto invalid;
3329 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003330
3331 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01003332 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003333 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003334 rx->sta->sta.smps_mode = smps_mode;
tamizhr@codeaurora.org57566b22018-03-27 19:16:16 +05303335 sta_opmode.smps_mode =
3336 ieee80211_smps_mode_to_smps_mode(smps_mode);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303337 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003338
3339 sband = rx->local->hw.wiphy->bands[status->band];
3340
Johannes Berg64f68e52012-03-28 10:58:37 +02003341 rate_control_rate_update(local, sband, rx->sta,
3342 IEEE80211_RC_SMPS_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303343 cfg80211_sta_opmode_change_notify(sdata->dev,
3344 rx->sta->addr,
3345 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003346 GFP_ATOMIC);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003347 goto handled;
3348 }
Johannes Bergec61cd62012-12-28 12:12:10 +01003349 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3350 struct ieee80211_supported_band *sband;
3351 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003352 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303353 struct sta_opmode_info sta_opmode = {};
Johannes Bergec61cd62012-12-28 12:12:10 +01003354
3355 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003356 if (!(rx->sta->sta.ht_cap.cap &
3357 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01003358 goto handled;
3359
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003360 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003361 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01003362 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003363 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3364
3365 /* set cur_max_bandwidth and recalc sta bw */
3366 rx->sta->cur_max_bandwidth = max_bw;
3367 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003368
3369 if (rx->sta->sta.bandwidth == new_bw)
3370 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01003371
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003372 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01003373 sband = rx->local->hw.wiphy->bands[status->band];
tamizhr@codeaurora.org97f5f422018-03-27 19:16:17 +05303374 sta_opmode.bw =
3375 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303376 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
Johannes Bergec61cd62012-12-28 12:12:10 +01003377
3378 rate_control_rate_update(local, sband, rx->sta,
3379 IEEE80211_RC_BW_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303380 cfg80211_sta_opmode_change_notify(sdata->dev,
3381 rx->sta->addr,
3382 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003383 GFP_ATOMIC);
Johannes Bergec61cd62012-12-28 12:12:10 +01003384 goto handled;
3385 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003386 default:
3387 goto invalid;
3388 }
3389
3390 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003391 case WLAN_CATEGORY_PUBLIC:
3392 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3393 goto invalid;
3394 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3395 break;
3396 if (!rx->sta)
3397 break;
3398 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3399 break;
3400 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3401 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3402 break;
3403 if (len < offsetof(struct ieee80211_mgmt,
3404 u.action.u.ext_chan_switch.variable))
3405 goto invalid;
3406 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003407 case WLAN_CATEGORY_VHT:
3408 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3409 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3410 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3411 sdata->vif.type != NL80211_IFTYPE_AP &&
3412 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3413 break;
3414
3415 /* verify action code is present */
3416 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3417 goto invalid;
3418
3419 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3420 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01003421 /* verify opmode is present */
3422 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3423 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02003424 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003425 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02003426 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3427 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3428 goto invalid;
3429 goto queue;
3430 }
Johannes Berg0af83d32012-12-27 18:55:36 +01003431 default:
3432 break;
3433 }
3434 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003435 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01003436 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07003437 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01003438 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01003439 sdata->vif.type != NL80211_IFTYPE_AP &&
3440 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02003441 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01003442
Jouni Malinen026331c2010-02-15 12:53:10 +02003443 /* verify action_code is present */
3444 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3445 break;
3446
Johannes Bergde1ede72008-09-09 14:42:50 +02003447 switch (mgmt->u.action.u.addba_req.action_code) {
3448 case WLAN_ACTION_ADDBA_REQ:
3449 if (len < (IEEE80211_MIN_ACTION_SIZE +
3450 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003451 goto invalid;
3452 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003453 case WLAN_ACTION_ADDBA_RESP:
3454 if (len < (IEEE80211_MIN_ACTION_SIZE +
3455 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003456 goto invalid;
3457 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003458 case WLAN_ACTION_DELBA:
3459 if (len < (IEEE80211_MIN_ACTION_SIZE +
3460 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003461 goto invalid;
3462 break;
3463 default:
3464 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02003465 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003466
Johannes Berg8b58ff82010-06-10 10:21:51 +02003467 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02003468 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02003469 /* verify action_code is present */
3470 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3471 break;
3472
Johannes Berg39192c02008-09-09 14:49:03 +02003473 switch (mgmt->u.action.u.measurement.action_code) {
3474 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02003475 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003476 break;
3477
Johannes Berg39192c02008-09-09 14:49:03 +02003478 if (len < (IEEE80211_MIN_ACTION_SIZE +
3479 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02003480 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003481
Johannes Bergcc32abd2009-05-15 11:52:31 +02003482 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003483 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02003484
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003485 ieee80211_process_measurement_req(sdata, mgmt, len);
3486 goto handled;
3487 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3488 u8 *bssid;
3489 if (len < (IEEE80211_MIN_ACTION_SIZE +
3490 sizeof(mgmt->u.action.u.chan_switch)))
3491 break;
3492
3493 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003494 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3495 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003496 break;
3497
3498 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3499 bssid = sdata->u.mgd.bssid;
3500 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3501 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003502 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3503 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003504 else
3505 break;
3506
3507 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02003508 break;
Sujithc481ec92009-01-06 09:28:37 +05303509
Johannes Berg8b58ff82010-06-10 10:21:51 +02003510 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003511 }
Johannes Berg39192c02008-09-09 14:49:03 +02003512 }
3513 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07003514 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003515 if (len < (IEEE80211_MIN_ACTION_SIZE +
3516 sizeof(mgmt->u.action.u.self_prot.action_code)))
3517 break;
3518
Thomas Pedersen8db09852011-08-12 20:01:00 -07003519 switch (mgmt->u.action.u.self_prot.action_code) {
3520 case WLAN_SP_MESH_PEERING_OPEN:
3521 case WLAN_SP_MESH_PEERING_CLOSE:
3522 case WLAN_SP_MESH_PEERING_CONFIRM:
3523 if (!ieee80211_vif_is_mesh(&sdata->vif))
3524 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003525 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003526 /* userspace handles this frame */
3527 break;
3528 goto queue;
3529 case WLAN_SP_MGK_INFORM:
3530 case WLAN_SP_MGK_ACK:
3531 if (!ieee80211_vif_is_mesh(&sdata->vif))
3532 goto invalid;
3533 break;
3534 }
3535 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003536 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003537 if (len < (IEEE80211_MIN_ACTION_SIZE +
3538 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3539 break;
3540
Johannes Berg77a121c2010-06-10 10:21:34 +02003541 if (!ieee80211_vif_is_mesh(&sdata->vif))
3542 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003543 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003544 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003545 break;
3546 goto queue;
Lorenzo Bianconif5a4c242021-08-23 20:02:39 +02003547 case WLAN_CATEGORY_S1G:
3548 switch (mgmt->u.action.u.s1g.action_code) {
3549 case WLAN_S1G_TWT_SETUP:
3550 case WLAN_S1G_TWT_TEARDOWN:
3551 if (ieee80211_process_rx_twt_action(rx))
3552 goto queue;
3553 break;
3554 default:
3555 break;
3556 }
3557 break;
Johannes Berg84040802010-02-15 12:46:39 +02003558 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003559
Johannes Berg2e161f782010-08-12 15:38:38 +02003560 return RX_CONTINUE;
3561
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003562 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003563 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f782010-08-12 15:38:38 +02003564 /* will return in the next handlers */
3565 return RX_CONTINUE;
3566
3567 handled:
3568 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003569 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003570 dev_kfree_skb(rx->skb);
3571 return RX_QUEUED;
3572
3573 queue:
Johannes Berg0044cc12021-05-17 23:07:54 +02003574 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003575 return RX_QUEUED;
3576}
3577
3578static ieee80211_rx_result debug_noinline
3579ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3580{
Johannes Berg554891e2010-09-24 12:38:25 +02003581 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003582 int sig = 0;
Johannes Berg2e161f782010-08-12 15:38:38 +02003583
3584 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003585 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f782010-08-12 15:38:38 +02003586 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003587
Jouni Malinen026331c2010-02-15 12:53:10 +02003588 /*
3589 * Getting here means the kernel doesn't know how to handle
3590 * it, but maybe userspace does ... include returned frames
3591 * so userspace can register for those to know whether ones
3592 * it transmitted were processed or returned.
3593 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003594
Tosoni1ad22fb2018-03-14 16:58:34 +00003595 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3596 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003597 sig = status->signal;
3598
Thomas Pedersene76fede2020-04-30 10:25:50 -07003599 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3600 ieee80211_rx_status_to_khz(status), sig,
3601 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f782010-08-12 15:38:38 +02003602 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003603 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003604 dev_kfree_skb(rx->skb);
3605 return RX_QUEUED;
3606 }
3607
Johannes Berg2e161f782010-08-12 15:38:38 +02003608 return RX_CONTINUE;
3609}
3610
3611static ieee80211_rx_result debug_noinline
Johannes Berg1ea02222020-05-26 10:31:33 +02003612ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3613{
3614 struct ieee80211_sub_if_data *sdata = rx->sdata;
3615 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3616 int len = rx->skb->len;
3617
3618 if (!ieee80211_is_action(mgmt->frame_control))
3619 return RX_CONTINUE;
3620
3621 switch (mgmt->u.action.category) {
3622 case WLAN_CATEGORY_SA_QUERY:
3623 if (len < (IEEE80211_MIN_ACTION_SIZE +
3624 sizeof(mgmt->u.action.u.sa_query)))
3625 break;
3626
3627 switch (mgmt->u.action.u.sa_query.action) {
3628 case WLAN_ACTION_SA_QUERY_REQUEST:
3629 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3630 break;
3631 ieee80211_process_sa_query_req(sdata, mgmt, len);
3632 goto handled;
3633 }
3634 break;
3635 }
3636
3637 return RX_CONTINUE;
3638
3639 handled:
3640 if (rx->sta)
3641 rx->sta->rx_stats.packets++;
3642 dev_kfree_skb(rx->skb);
3643 return RX_QUEUED;
3644}
3645
3646static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003647ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3648{
3649 struct ieee80211_local *local = rx->local;
3650 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3651 struct sk_buff *nskb;
3652 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003653 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003654
3655 if (!ieee80211_is_action(mgmt->frame_control))
3656 return RX_CONTINUE;
3657
3658 /*
3659 * For AP mode, hostapd is responsible for handling any action
3660 * frames that we didn't handle, including returning unknown
3661 * ones. For all other modes we will return them to the sender,
3662 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003663 * 802.11-2012 9.24.4.
Johannes Berg2e161f782010-08-12 15:38:38 +02003664 * Newer versions of hostapd shall also use the management frame
3665 * registration mechanisms, but older ones still use cooked
3666 * monitor interfaces so push all frames there.
3667 */
Johannes Berg554891e2010-09-24 12:38:25 +02003668 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f782010-08-12 15:38:38 +02003669 (sdata->vif.type == NL80211_IFTYPE_AP ||
3670 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3671 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003672
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003673 if (is_multicast_ether_addr(mgmt->da))
3674 return RX_DROP_MONITOR;
3675
Johannes Berg84040802010-02-15 12:46:39 +02003676 /* do not return rejected action frames */
3677 if (mgmt->u.action.category & 0x80)
3678 return RX_DROP_UNUSABLE;
3679
3680 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3681 GFP_ATOMIC);
3682 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003683 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003684
John W. Linville292b4df2010-06-24 11:13:56 -04003685 nmgmt->u.action.category |= 0x80;
3686 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3687 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003688
3689 memset(nskb->cb, 0, sizeof(nskb->cb));
3690
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003691 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3692 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3693
3694 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3695 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3696 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003697 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003698 info->hw_queue =
3699 local->hw.offchannel_tx_hw_queue;
3700 }
3701
3702 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04003703 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003704 }
Johannes Berg39192c02008-09-09 14:49:03 +02003705 dev_kfree_skb(rx->skb);
3706 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003707}
3708
3709static ieee80211_rx_result debug_noinline
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003710ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3711{
3712 struct ieee80211_sub_if_data *sdata = rx->sdata;
3713 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3714
3715 if (!ieee80211_is_ext(hdr->frame_control))
3716 return RX_CONTINUE;
3717
3718 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3719 return RX_DROP_MONITOR;
3720
3721 /* for now only beacons are ext, so queue them */
Johannes Berg0044cc12021-05-17 23:07:54 +02003722 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003723
3724 return RX_QUEUED;
3725}
3726
3727static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003728ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003729{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003730 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003731 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3732 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003733
Johannes Berg77a121c2010-06-10 10:21:34 +02003734 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003735
Johannes Berg77a121c2010-06-10 10:21:34 +02003736 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3737 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003738 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003739 sdata->vif.type != NL80211_IFTYPE_STATION)
3740 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003741
Johannes Berg77a121c2010-06-10 10:21:34 +02003742 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003743 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003744 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3745 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3746 /* process for all: mesh, mlme, ibss */
3747 break;
Johannes Berg95697f92019-10-04 15:37:05 +03003748 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3749 if (is_multicast_ether_addr(mgmt->da) &&
3750 !is_broadcast_ether_addr(mgmt->da))
3751 return RX_DROP_MONITOR;
3752
3753 /* process only for station/IBSS */
3754 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3755 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3756 return RX_DROP_MONITOR;
3757 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003758 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3759 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003760 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003761 if (is_multicast_ether_addr(mgmt->da) &&
3762 !is_broadcast_ether_addr(mgmt->da))
3763 return RX_DROP_MONITOR;
3764
Johannes Berg77a121c2010-06-10 10:21:34 +02003765 /* process only for station */
3766 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3767 return RX_DROP_MONITOR;
3768 break;
3769 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003770 /* process only for ibss and mesh */
3771 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3772 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003773 return RX_DROP_MONITOR;
3774 break;
3775 default:
3776 return RX_DROP_MONITOR;
3777 }
Johannes Berg46900292009-02-15 12:44:28 +01003778
Johannes Berg0044cc12021-05-17 23:07:54 +02003779 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
Johannes Berg77a121c2010-06-10 10:21:34 +02003780
3781 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003782}
3783
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003784static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3785 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003786{
3787 struct ieee80211_sub_if_data *sdata;
3788 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003789 struct sk_buff *skb = rx->skb, *skb2;
3790 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003791 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003792 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003793
Johannes Berg554891e2010-09-24 12:38:25 +02003794 /*
3795 * If cooked monitor has been processed already, then
3796 * don't do it again. If not, set the flag.
3797 */
3798 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003799 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003800 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003801
Johannes Berg152c4772011-10-21 10:22:22 +02003802 /* If there are no cooked monitor interfaces, just free the SKB */
3803 if (!local->cooked_mntrs)
3804 goto out_free_skb;
3805
Johannes Berg1f7bba72014-11-06 22:56:36 +01003806 /* vendor data is long removed here */
3807 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003808 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003809 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003810
3811 if (skb_headroom(skb) < needed_headroom &&
3812 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003813 goto out_free_skb;
3814
Johannes Berg293702a2012-03-02 13:18:19 +01003815 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003816 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3817 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003818
Zhang Shengjud57a5442016-03-03 01:16:56 +00003819 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003820 skb->ip_summed = CHECKSUM_UNNECESSARY;
3821 skb->pkt_type = PACKET_OTHERHOST;
3822 skb->protocol = htons(ETH_P_802_2);
3823
3824 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01003825 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003826 continue;
3827
Johannes Berg05c914f2008-09-11 00:01:58 +02003828 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003829 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003830 continue;
3831
3832 if (prev_dev) {
3833 skb2 = skb_clone(skb, GFP_ATOMIC);
3834 if (skb2) {
3835 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003836 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003837 }
3838 }
3839
3840 prev_dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +02003841 dev_sw_netstats_rx_add(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003842 }
3843
3844 if (prev_dev) {
3845 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003846 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003847 return;
3848 }
Michael Wu3d30d942008-01-31 19:48:27 +01003849
3850 out_free_skb:
3851 dev_kfree_skb(skb);
3852}
3853
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003854static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3855 ieee80211_rx_result res)
3856{
3857 switch (res) {
3858 case RX_DROP_MONITOR:
3859 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3860 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003861 rx->sta->rx_stats.dropped++;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003862 fallthrough;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003863 case RX_CONTINUE: {
3864 struct ieee80211_rate *rate = NULL;
3865 struct ieee80211_supported_band *sband;
3866 struct ieee80211_rx_status *status;
3867
3868 status = IEEE80211_SKB_RXCB((rx->skb));
3869
3870 sband = rx->local->hw.wiphy->bands[status->band];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003871 if (status->encoding == RX_ENC_LEGACY)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003872 rate = &sband->bitrates[status->rate_idx];
3873
3874 ieee80211_rx_cooked_monitor(rx, rate);
3875 break;
3876 }
3877 case RX_DROP_UNUSABLE:
3878 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3879 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003880 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003881 dev_kfree_skb(rx->skb);
3882 break;
3883 case RX_QUEUED:
3884 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3885 break;
3886 }
3887}
3888
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003889static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3890 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003891{
3892 ieee80211_rx_result res = RX_DROP_MONITOR;
3893 struct sk_buff *skb;
3894
3895#define CALL_RXH(rxh) \
3896 do { \
3897 res = rxh(rx); \
3898 if (res != RX_CONTINUE) \
3899 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003900 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003901
Johannes Berg45ceeee2015-03-16 09:08:20 +01003902 /* Lock here to avoid hitting all of the data used in the RX
3903 * path (e.g. key data, station data, ...) concurrently when
3904 * a frame is released from the reorder buffer due to timeout
3905 * from the timer, potentially concurrently with RX from the
3906 * driver.
3907 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003908 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003909
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003910 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003911 /*
3912 * all the other fields are valid across frames
3913 * that belong to an aMPDU since they are on the
3914 * same TID from the same station
3915 */
3916 rx->skb = skb;
3917
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003918 CALL_RXH(ieee80211_rx_h_check_more_data);
3919 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3920 CALL_RXH(ieee80211_rx_h_sta_process);
3921 CALL_RXH(ieee80211_rx_h_decrypt);
3922 CALL_RXH(ieee80211_rx_h_defragment);
3923 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003924 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003925#ifdef CONFIG_MAC80211_MESH
3926 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3927 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3928#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003929 CALL_RXH(ieee80211_rx_h_amsdu);
3930 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003931
3932 /* special treatment -- needs the queue */
3933 res = ieee80211_rx_h_ctrl(rx, frames);
3934 if (res != RX_CONTINUE)
3935 goto rxh_next;
3936
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003937 CALL_RXH(ieee80211_rx_h_mgmt_check);
3938 CALL_RXH(ieee80211_rx_h_action);
3939 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
Johannes Berg1ea02222020-05-26 10:31:33 +02003940 CALL_RXH(ieee80211_rx_h_action_post_userspace);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003941 CALL_RXH(ieee80211_rx_h_action_return);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003942 CALL_RXH(ieee80211_rx_h_ext);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003943 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003944
3945 rxh_next:
3946 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003947
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003948#undef CALL_RXH
3949 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003950
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003951 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003952}
Johannes Berg571ecf62007-07-27 15:43:22 +02003953
Johannes Berg4406c372010-09-24 11:21:06 +02003954static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003955{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003956 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003957 ieee80211_rx_result res = RX_DROP_MONITOR;
3958
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003959 __skb_queue_head_init(&reorder_release);
3960
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003961#define CALL_RXH(rxh) \
3962 do { \
3963 res = rxh(rx); \
3964 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003965 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003966 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003967
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003968 CALL_RXH(ieee80211_rx_h_check_dup);
3969 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003970
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003971 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003972
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003973 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003974 return;
Johannes Berg49461622008-06-30 15:10:45 +02003975
Johannes Berg2569a822009-11-25 17:46:17 +01003976 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003977 ieee80211_rx_handlers_result(rx, res);
3978
3979#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003980}
3981
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003982/*
Johannes Bergdd318572010-11-29 11:09:16 +01003983 * This function makes calls into the RX path, therefore
3984 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003985 */
3986void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3987{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003988 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003989 struct ieee80211_rx_data rx = {
3990 .sta = sta,
3991 .sdata = sta->sdata,
3992 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003993 /* This is OK -- must be QoS data frame */
3994 .security_idx = tid,
3995 .seqno_idx = tid,
Johannes Berg554891e2010-09-24 12:38:25 +02003996 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003997 struct tid_ampdu_rx *tid_agg_rx;
3998
3999 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4000 if (!tid_agg_rx)
4001 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02004002
Christian Lamparterf9e124f2013-02-04 17:44:44 +00004003 __skb_queue_head_init(&frames);
4004
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02004005 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00004006 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02004007 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02004008
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03004009 if (!skb_queue_empty(&frames)) {
4010 struct ieee80211_event event = {
4011 .type = BA_FRAME_TIMEOUT,
4012 .u.ba.tid = tid,
4013 .u.ba.sta = &sta->sta,
4014 };
4015 drv_event_callback(rx.local, rx.sdata, &event);
4016 }
4017
Christian Lamparterf9e124f2013-02-04 17:44:44 +00004018 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02004019}
4020
Sara Sharon06470f72016-01-28 16:19:25 +02004021void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4022 u16 ssn, u64 filtered,
4023 u16 received_mpdus)
4024{
4025 struct sta_info *sta;
4026 struct tid_ampdu_rx *tid_agg_rx;
4027 struct sk_buff_head frames;
4028 struct ieee80211_rx_data rx = {
4029 /* This is OK -- must be QoS data frame */
4030 .security_idx = tid,
4031 .seqno_idx = tid,
4032 };
4033 int i, diff;
4034
4035 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4036 return;
4037
4038 __skb_queue_head_init(&frames);
4039
4040 sta = container_of(pubsta, struct sta_info, sta);
4041
4042 rx.sta = sta;
4043 rx.sdata = sta->sdata;
4044 rx.local = sta->local;
4045
4046 rcu_read_lock();
4047 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4048 if (!tid_agg_rx)
4049 goto out;
4050
4051 spin_lock_bh(&tid_agg_rx->reorder_lock);
4052
4053 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4054 int release;
4055
4056 /* release all frames in the reorder buffer */
4057 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4058 IEEE80211_SN_MODULO;
4059 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4060 release, &frames);
4061 /* update ssn to match received ssn */
4062 tid_agg_rx->head_seq_num = ssn;
4063 } else {
4064 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4065 &frames);
4066 }
4067
4068 /* handle the case that received ssn is behind the mac ssn.
4069 * it can be tid_agg_rx->buf_size behind and still be valid */
4070 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4071 if (diff >= tid_agg_rx->buf_size) {
4072 tid_agg_rx->reorder_buf_filtered = 0;
4073 goto release;
4074 }
4075 filtered = filtered >> diff;
4076 ssn += diff;
4077
4078 /* update bitmap */
4079 for (i = 0; i < tid_agg_rx->buf_size; i++) {
4080 int index = (ssn + i) % tid_agg_rx->buf_size;
4081
4082 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4083 if (filtered & BIT_ULL(i))
4084 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4085 }
4086
4087 /* now process also frames that the filter marking released */
4088 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4089
4090release:
4091 spin_unlock_bh(&tid_agg_rx->reorder_lock);
4092
4093 ieee80211_rx_handlers(&rx, &frames);
4094
4095 out:
4096 rcu_read_unlock();
4097}
4098EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4099
Johannes Berg571ecf62007-07-27 15:43:22 +02004100/* main receive path */
4101
Johannes Berga58fbe12015-04-22 15:08:39 +02004102static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02004103{
Johannes Berg20b01f82010-09-24 11:21:05 +02004104 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004105 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02004106 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004107 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4108 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07004109 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4110 ieee80211_is_s1g_beacon(hdr->frame_control);
Johannes Berg23a24de2007-07-27 15:43:22 +02004111
Johannes Berg51fb61e2007-12-19 01:31:27 +01004112 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02004113 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01004114 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01004115 return false;
Johannes Berg588f7d32019-02-13 15:13:30 +01004116 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4117 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004118 if (multicast)
4119 return true;
4120 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004121 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02004122 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01004123 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02004124 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
YueHaibinga6555f82021-08-27 22:42:30 +08004125 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4126 !is_valid_ether_addr(hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004127 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004128 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004129 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02004130 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004131 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004132 if (!multicast &&
4133 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004134 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004135 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004136 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004137 if (status->encoding != RX_ENC_LEGACY)
Johannes Berg56146182012-11-09 15:07:02 +01004138 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004139 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004140 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01004141 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4142 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004143 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004144 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004145 case NL80211_IFTYPE_OCB:
4146 if (!bssid)
4147 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02004148 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004149 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004150 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004151 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004152 if (!multicast &&
4153 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004154 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004155 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004156 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004157 if (status->encoding != RX_ENC_LEGACY)
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004158 rate_idx = 0; /* TODO: HT rates */
4159 else
4160 rate_idx = status->rate_idx;
4161 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4162 BIT(rate_idx));
4163 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004164 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004165 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg736a80b2018-01-04 15:51:53 +01004166 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4167 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004168 if (multicast)
4169 return true;
4170 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004171 case NL80211_IFTYPE_AP_VLAN:
4172 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02004173 if (!bssid)
4174 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4175
4176 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01004177 /*
4178 * Accept public action frames even when the
4179 * BSSID doesn't match, this is used for P2P
4180 * and location updates. Note that mac80211
4181 * itself never looks at these frames.
4182 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02004183 if (!multicast &&
4184 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004185 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02004186 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004187 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02004188 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02004189 }
4190
4191 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004192 /* ignore data frames to TDLS-peers */
4193 if (ieee80211_is_data(hdr->frame_control))
4194 return false;
4195 /* ignore action frames to TDLS-peers */
4196 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02004197 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004198 !ether_addr_equal(bssid, hdr->addr1))
4199 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004200 }
Johannes Berg3018e942017-04-20 21:32:16 +02004201
4202 /*
4203 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4204 * the BSSID - we've checked that already but may have accepted
4205 * the wildcard (ff:ff:ff:ff:ff:ff).
4206 *
4207 * It also says:
4208 * The BSSID of the Data frame is determined as follows:
4209 * a) If the STA is contained within an AP or is associated
4210 * with an AP, the BSSID is the address currently in use
4211 * by the STA contained in the AP.
4212 *
4213 * So we should not accept data frames with an address that's
4214 * multicast.
4215 *
4216 * Accepting it also opens a security problem because stations
4217 * could encrypt it with the GTK and inject traffic that way.
4218 */
4219 if (ieee80211_is_data(hdr->frame_control) && multicast)
4220 return false;
4221
Johannes Berga58fbe12015-04-22 15:08:39 +02004222 return true;
Johannes Bergf142c6b2012-06-18 20:07:15 +02004223 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02004224 return ieee80211_is_public_action(hdr, skb->len) ||
4225 ieee80211_is_probe_req(hdr->frame_control) ||
4226 ieee80211_is_probe_resp(hdr->frame_control) ||
4227 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03004228 case NL80211_IFTYPE_NAN:
4229 /* Currently no frames on NAN interface are allowed */
4230 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02004231 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02004232 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02004233 }
4234
Johannes Berga58fbe12015-04-22 15:08:39 +02004235 WARN_ON_ONCE(1);
4236 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004237}
4238
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004239void ieee80211_check_fast_rx(struct sta_info *sta)
4240{
4241 struct ieee80211_sub_if_data *sdata = sta->sdata;
4242 struct ieee80211_local *local = sdata->local;
4243 struct ieee80211_key *key;
4244 struct ieee80211_fast_rx fastrx = {
4245 .dev = sdata->dev,
4246 .vif_type = sdata->vif.type,
4247 .control_port_protocol = sdata->control_port_protocol,
4248 }, *old, *new = NULL;
Felix Fietkau80a915e2020-12-18 19:47:18 +01004249 bool set_offload = false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004250 bool assign = false;
Felix Fietkau80a915e2020-12-18 19:47:18 +01004251 bool offload;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004252
4253 /* use sparse to check that we don't return without updating */
4254 __acquire(check_fast_rx);
4255
4256 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4257 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4258 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4259 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4260
Johannes Bergc9c59622016-03-31 20:02:11 +03004261 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4262
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004263 /* fast-rx doesn't do reordering */
4264 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4265 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4266 goto clear;
4267
4268 switch (sdata->vif.type) {
4269 case NL80211_IFTYPE_STATION:
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004270 if (sta->sta.tdls) {
4271 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4272 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4273 fastrx.expected_ds_bits = 0;
4274 } else {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004275 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4276 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4277 fastrx.expected_ds_bits =
4278 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4279 }
Felix Fietkau1d870162018-02-23 10:06:05 +01004280
Felix Fietkau9251a472018-02-23 13:55:50 +01004281 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4282 fastrx.expected_ds_bits |=
4283 cpu_to_le16(IEEE80211_FCTL_TODS);
4284 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4285 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4286 }
4287
Felix Fietkau1d870162018-02-23 10:06:05 +01004288 if (!sdata->u.mgd.powersave)
4289 break;
4290
4291 /* software powersave is a huge mess, avoid all of it */
4292 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4293 goto clear;
4294 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4295 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4296 goto clear;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004297 break;
4298 case NL80211_IFTYPE_AP_VLAN:
4299 case NL80211_IFTYPE_AP:
4300 /* parallel-rx requires this, at least with calls to
4301 * ieee80211_sta_ps_transition()
4302 */
4303 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4304 goto clear;
4305 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4306 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4307 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4308
4309 fastrx.internal_forward =
4310 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4311 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4312 !sdata->u.vlan.sta);
Felix Fietkau59cae5b2018-02-23 10:06:04 +01004313
4314 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4315 sdata->u.vlan.sta) {
4316 fastrx.expected_ds_bits |=
4317 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4318 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4319 fastrx.internal_forward = 0;
4320 }
4321
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004322 break;
4323 default:
4324 goto clear;
4325 }
4326
4327 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4328 goto clear;
4329
4330 rcu_read_lock();
4331 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
Felix Fietkau622d3b42020-12-18 19:47:17 +01004332 if (!key)
4333 key = rcu_dereference(sdata->default_unicast_key);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004334 if (key) {
4335 switch (key->conf.cipher) {
4336 case WLAN_CIPHER_SUITE_TKIP:
4337 /* we don't want to deal with MMIC in fast-rx */
4338 goto clear_rcu;
4339 case WLAN_CIPHER_SUITE_CCMP:
4340 case WLAN_CIPHER_SUITE_CCMP_256:
4341 case WLAN_CIPHER_SUITE_GCMP:
4342 case WLAN_CIPHER_SUITE_GCMP_256:
4343 break;
4344 default:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01004345 /* We also don't want to deal with
4346 * WEP or cipher scheme.
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004347 */
4348 goto clear_rcu;
4349 }
4350
4351 fastrx.key = true;
4352 fastrx.icv_len = key->conf.icv_len;
4353 }
4354
4355 assign = true;
4356 clear_rcu:
4357 rcu_read_unlock();
4358 clear:
4359 __release(check_fast_rx);
4360
4361 if (assign)
4362 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4363
Felix Fietkau80a915e2020-12-18 19:47:18 +01004364 offload = assign &&
4365 (sdata->vif.offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED);
4366
4367 if (offload)
4368 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4369 else
4370 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4371
4372 if (set_offload)
4373 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4374
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004375 spin_lock_bh(&sta->lock);
4376 old = rcu_dereference_protected(sta->fast_rx, true);
4377 rcu_assign_pointer(sta->fast_rx, new);
4378 spin_unlock_bh(&sta->lock);
4379
4380 if (old)
4381 kfree_rcu(old, rcu_head);
4382}
4383
4384void ieee80211_clear_fast_rx(struct sta_info *sta)
4385{
4386 struct ieee80211_fast_rx *old;
4387
4388 spin_lock_bh(&sta->lock);
4389 old = rcu_dereference_protected(sta->fast_rx, true);
4390 RCU_INIT_POINTER(sta->fast_rx, NULL);
4391 spin_unlock_bh(&sta->lock);
4392
4393 if (old)
4394 kfree_rcu(old, rcu_head);
4395}
4396
4397void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4398{
4399 struct ieee80211_local *local = sdata->local;
4400 struct sta_info *sta;
4401
4402 lockdep_assert_held(&local->sta_mtx);
4403
Madhuparna Bhowmik253216f2020-02-23 20:03:02 +05304404 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004405 if (sdata != sta->sdata &&
4406 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4407 continue;
4408 ieee80211_check_fast_rx(sta);
4409 }
4410}
4411
4412void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4413{
4414 struct ieee80211_local *local = sdata->local;
4415
4416 mutex_lock(&local->sta_mtx);
4417 __ieee80211_check_fast_rx_iface(sdata);
4418 mutex_unlock(&local->sta_mtx);
4419}
4420
Felix Fietkau80a915e2020-12-18 19:47:18 +01004421static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4422 struct ieee80211_fast_rx *fast_rx,
4423 int orig_len)
4424{
4425 struct ieee80211_sta_rx_stats *stats;
4426 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4427 struct sta_info *sta = rx->sta;
4428 struct sk_buff *skb = rx->skb;
4429 void *sa = skb->data + ETH_ALEN;
4430 void *da = skb->data;
4431
4432 stats = &sta->rx_stats;
4433 if (fast_rx->uses_rss)
4434 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4435
4436 /* statistics part of ieee80211_rx_h_sta_process() */
4437 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4438 stats->last_signal = status->signal;
4439 if (!fast_rx->uses_rss)
4440 ewma_signal_add(&sta->rx_stats_avg.signal,
4441 -status->signal);
4442 }
4443
4444 if (status->chains) {
4445 int i;
4446
4447 stats->chains = status->chains;
4448 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4449 int signal = status->chain_signal[i];
4450
4451 if (!(status->chains & BIT(i)))
4452 continue;
4453
4454 stats->chain_signal_last[i] = signal;
4455 if (!fast_rx->uses_rss)
4456 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4457 -signal);
4458 }
4459 }
4460 /* end of statistics */
4461
4462 stats->last_rx = jiffies;
4463 stats->last_rate = sta_stats_encode_rate(status);
4464
4465 stats->fragments++;
4466 stats->packets++;
4467
4468 skb->dev = fast_rx->dev;
4469
4470 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4471
4472 /* The seqno index has the same property as needed
4473 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4474 * for non-QoS-data frames. Here we know it's a data
4475 * frame, so count MSDUs.
4476 */
4477 u64_stats_update_begin(&stats->syncp);
4478 stats->msdu[rx->seqno_idx]++;
4479 stats->bytes += orig_len;
4480 u64_stats_update_end(&stats->syncp);
4481
4482 if (fast_rx->internal_forward) {
4483 struct sk_buff *xmit_skb = NULL;
4484 if (is_multicast_ether_addr(da)) {
4485 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4486 } else if (!ether_addr_equal(da, sa) &&
4487 sta_info_get(rx->sdata, da)) {
4488 xmit_skb = skb;
4489 skb = NULL;
4490 }
4491
4492 if (xmit_skb) {
4493 /*
4494 * Send to wireless media and increase priority by 256
4495 * to keep the received priority instead of
4496 * reclassifying the frame (see cfg80211_classify8021d).
4497 */
4498 xmit_skb->priority += 256;
4499 xmit_skb->protocol = htons(ETH_P_802_3);
4500 skb_reset_network_header(xmit_skb);
4501 skb_reset_mac_header(xmit_skb);
4502 dev_queue_xmit(xmit_skb);
4503 }
4504
4505 if (!skb)
4506 return;
4507 }
4508
4509 /* deliver to local stack */
4510 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4511 memset(skb->cb, 0, sizeof(skb->cb));
4512 if (rx->list)
4513 list_add_tail(&skb->list, rx->list);
4514 else
4515 netif_receive_skb(skb);
4516
4517}
4518
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004519static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4520 struct ieee80211_fast_rx *fast_rx)
4521{
4522 struct sk_buff *skb = rx->skb;
4523 struct ieee80211_hdr *hdr = (void *)skb->data;
4524 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4525 struct sta_info *sta = rx->sta;
4526 int orig_len = skb->len;
Felix Fietkau24bba072018-02-27 13:03:07 +01004527 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4528 int snap_offs = hdrlen;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004529 struct {
4530 u8 snap[sizeof(rfc1042_header)];
4531 __be16 proto;
4532 } *payload __aligned(2);
4533 struct {
4534 u8 da[ETH_ALEN];
4535 u8 sa[ETH_ALEN];
4536 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03004537 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4538
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004539 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4540 * to a common data structure; drivers can implement that per queue
4541 * but we don't have that information in mac80211
4542 */
4543 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4544 return false;
4545
4546#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4547
4548 /* If using encryption, we also need to have:
4549 * - PN_VALIDATED: similar, but the implementation is tricky
4550 * - DECRYPTED: necessary for PN_VALIDATED
4551 */
4552 if (fast_rx->key &&
4553 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4554 return false;
4555
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004556 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4557 return false;
4558
4559 if (unlikely(ieee80211_is_frag(hdr)))
4560 return false;
4561
4562 /* Since our interface address cannot be multicast, this
4563 * implicitly also rejects multicast frames without the
4564 * explicit check.
4565 *
4566 * We shouldn't get any *data* frames not addressed to us
4567 * (AP mode will accept multicast *management* frames), but
4568 * punting here will make it go through the full checks in
4569 * ieee80211_accept_frame().
4570 */
4571 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4572 return false;
4573
4574 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4575 IEEE80211_FCTL_TODS)) !=
4576 fast_rx->expected_ds_bits)
Felix Fietkaub323ac12018-02-23 10:06:03 +01004577 return false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004578
4579 /* assign the key to drop unencrypted frames (later)
4580 * and strip the IV/MIC if necessary
4581 */
4582 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4583 /* GCMP header length is the same */
4584 snap_offs += IEEE80211_CCMP_HDR_LEN;
4585 }
4586
Felix Fietkau24bba072018-02-27 13:03:07 +01004587 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4588 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4589 goto drop;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004590
Felix Fietkau24bba072018-02-27 13:03:07 +01004591 payload = (void *)(skb->data + snap_offs);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004592
Felix Fietkau24bba072018-02-27 13:03:07 +01004593 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4594 return false;
4595
4596 /* Don't handle these here since they require special code.
4597 * Accept AARP and IPX even though they should come with a
4598 * bridge-tunnel header - but if we get them this way then
4599 * there's little point in discarding them.
4600 */
4601 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4602 payload->proto == fast_rx->control_port_protocol))
4603 return false;
4604 }
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004605
4606 /* after this point, don't punt to the slowpath! */
4607
4608 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4609 pskb_trim(skb, skb->len - fast_rx->icv_len))
4610 goto drop;
4611
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004612 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4613 goto drop;
4614
Felix Fietkau24bba072018-02-27 13:03:07 +01004615 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4616 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4617 RX_QUEUED)
4618 goto drop;
4619
4620 return true;
4621 }
4622
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004623 /* do the header conversion - first grab the addresses */
4624 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4625 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4626 /* remove the SNAP but leave the ethertype */
4627 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4628 /* push the addresses in front */
4629 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4630
Felix Fietkau80a915e2020-12-18 19:47:18 +01004631 ieee80211_rx_8023(rx, fast_rx, orig_len);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004632
4633 return true;
4634 drop:
4635 dev_kfree_skb(skb);
Felix Fietkau80a915e2020-12-18 19:47:18 +01004636 if (fast_rx->uses_rss)
4637 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4638
Johannes Bergc9c59622016-03-31 20:02:11 +03004639 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004640 return true;
4641}
4642
Johannes Berg571ecf62007-07-27 15:43:22 +02004643/*
Johannes Berg4406c372010-09-24 11:21:06 +02004644 * This function returns whether or not the SKB
4645 * was destined for RX processing or not, which,
4646 * if consume is true, is equivalent to whether
4647 * or not the skb was consumed.
4648 */
4649static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4650 struct sk_buff *skb, bool consume)
4651{
4652 struct ieee80211_local *local = rx->local;
4653 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004654
4655 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004656
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004657 /* See if we can do fast-rx; if we have to copy we already lost,
4658 * so punt in that case. We should never have to deliver a data
4659 * frame to multiple interfaces anyway.
4660 *
4661 * We skip the ieee80211_accept_frame() call and do the necessary
4662 * checking inside ieee80211_invoke_fast_rx().
4663 */
4664 if (consume && rx->sta) {
4665 struct ieee80211_fast_rx *fast_rx;
4666
4667 fast_rx = rcu_dereference(rx->sta->fast_rx);
4668 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4669 return true;
4670 }
4671
Johannes Berga58fbe12015-04-22 15:08:39 +02004672 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004673 return false;
4674
Johannes Berg4406c372010-09-24 11:21:06 +02004675 if (!consume) {
4676 skb = skb_copy(skb, GFP_ATOMIC);
4677 if (!skb) {
4678 if (net_ratelimit())
4679 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004680 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004681 sdata->name);
4682 return true;
4683 }
4684
4685 rx->skb = skb;
4686 }
4687
4688 ieee80211_invoke_rx_handlers(rx);
4689 return true;
4690}
4691
Felix Fietkau80a915e2020-12-18 19:47:18 +01004692static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
4693 struct ieee80211_sta *pubsta,
4694 struct sk_buff *skb,
4695 struct list_head *list)
4696{
4697 struct ieee80211_local *local = hw_to_local(hw);
4698 struct ieee80211_fast_rx *fast_rx;
4699 struct ieee80211_rx_data rx;
4700
4701 memset(&rx, 0, sizeof(rx));
4702 rx.skb = skb;
4703 rx.local = local;
4704 rx.list = list;
4705
4706 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4707
4708 /* drop frame if too short for header */
4709 if (skb->len < sizeof(struct ethhdr))
4710 goto drop;
4711
4712 if (!pubsta)
4713 goto drop;
4714
4715 rx.sta = container_of(pubsta, struct sta_info, sta);
4716 rx.sdata = rx.sta->sdata;
4717
4718 fast_rx = rcu_dereference(rx.sta->fast_rx);
4719 if (!fast_rx)
4720 goto drop;
4721
4722 ieee80211_rx_8023(&rx, fast_rx, skb->len);
4723 return;
4724
4725drop:
4726 dev_kfree_skb(skb);
4727}
4728
Johannes Berg4406c372010-09-24 11:21:06 +02004729/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004730 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004731 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004732 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004733static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004734 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004735 struct sk_buff *skb,
Felix Fietkauc5d16862020-07-26 13:06:11 +02004736 struct list_head *list)
Johannes Berg571ecf62007-07-27 15:43:22 +02004737{
4738 struct ieee80211_local *local = hw_to_local(hw);
4739 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004740 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004741 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004742 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004743 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004744 struct rhlist_head *tmp;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004745 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004746
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004747 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004748 memset(&rx, 0, sizeof(rx));
4749 rx.skb = skb;
4750 rx.local = local;
Felix Fietkauc5d16862020-07-26 13:06:11 +02004751 rx.list = list;
Johannes Berg72abd812007-09-17 01:29:22 -04004752
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004753 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004754 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004755
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004756 if (ieee80211_is_mgmt(fc)) {
4757 /* drop frame if too short for header */
4758 if (skb->len < ieee80211_hdrlen(fc))
4759 err = -ENOBUFS;
4760 else
4761 err = skb_linearize(skb);
4762 } else {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004763 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004764 }
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004765
4766 if (err) {
4767 dev_kfree_skb(skb);
4768 return;
4769 }
4770
4771 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004772 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004773 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004774
Johannes Bergd48b2962012-07-06 22:19:27 +02004775 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
Thomas Pedersencd418ba2020-09-21 19:28:08 -07004776 ieee80211_is_beacon(hdr->frame_control) ||
4777 ieee80211_is_s1g_beacon(hdr->frame_control)))
Johannes Bergd48b2962012-07-06 22:19:27 +02004778 ieee80211_scan_rx(local, skb);
4779
Johannes Berg19d19e92017-02-27 09:38:11 +01004780 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004781 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004782
Johannes Berg19d19e92017-02-27 09:38:11 +01004783 if (pubsta) {
4784 rx.sta = container_of(pubsta, struct sta_info, sta);
4785 rx.sdata = rx.sta->sdata;
4786 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4787 return;
4788 goto out;
4789 }
4790
Ben Greear56af3262010-09-23 10:22:24 -07004791 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004792
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004793 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004794 if (!prev_sta) {
4795 prev_sta = sta;
4796 continue;
4797 }
4798
4799 rx.sta = prev_sta;
4800 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004801 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004802
Ben Greear56af3262010-09-23 10:22:24 -07004803 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004804 }
Ben Greear56af3262010-09-23 10:22:24 -07004805
4806 if (prev_sta) {
4807 rx.sta = prev_sta;
4808 rx.sdata = prev_sta->sdata;
4809
Johannes Berg4406c372010-09-24 11:21:06 +02004810 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004811 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304812 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004813 }
Johannes Berg4406c372010-09-24 11:21:06 +02004814 }
4815
Johannes Berg4b0dd982010-09-24 11:21:07 +02004816 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004817
Johannes Berg4b0dd982010-09-24 11:21:07 +02004818 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4819 if (!ieee80211_sdata_running(sdata))
4820 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004821
Johannes Berg4b0dd982010-09-24 11:21:07 +02004822 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4823 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4824 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004825
Johannes Berg4b0dd982010-09-24 11:21:07 +02004826 /*
4827 * frame is destined for this interface, but if it's
4828 * not also for the previous one we handle that after
4829 * the loop to avoid copying the SKB once too much
4830 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004831
Johannes Berg4b0dd982010-09-24 11:21:07 +02004832 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004833 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004834 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004835 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004836
Johannes Berg7852e362012-01-20 13:55:24 +01004837 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004838 rx.sdata = prev;
4839 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004840
Johannes Berg4b0dd982010-09-24 11:21:07 +02004841 prev = sdata;
4842 }
Johannes Berg4406c372010-09-24 11:21:06 +02004843
Johannes Berg4b0dd982010-09-24 11:21:07 +02004844 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004845 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004846 rx.sdata = prev;
4847
4848 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4849 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004850 }
Johannes Berg4406c372010-09-24 11:21:06 +02004851
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304852 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004853 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004854}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004855
4856/*
4857 * This is the receive path handler. It is called by a low level driver when an
4858 * 802.11 MPDU is received from the hardware.
4859 */
Felix Fietkauc5d16862020-07-26 13:06:11 +02004860void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4861 struct sk_buff *skb, struct list_head *list)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004862{
4863 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004864 struct ieee80211_rate *rate = NULL;
4865 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004866 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004867 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg8318d782008-01-24 19:38:38 +01004868
Johannes Bergd20ef632009-10-11 15:10:40 +02004869 WARN_ON_ONCE(softirq_count() == 0);
4870
Johannes Berg57fbcce2016-04-12 15:56:15 +02004871 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004872 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004873
4874 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004875 if (WARN_ON(!sband))
4876 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004877
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004878 /*
4879 * If we're suspending, it is possible although not too likely
4880 * that we'd be receiving frames after having already partially
4881 * quiesced the stack. We can't process such frames then since
4882 * that might, for example, cause stations to be added or other
4883 * driver callbacks be invoked.
4884 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004885 if (unlikely(local->quiescing || local->suspended))
4886 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004887
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004888 /* We might be during a HW reconfig, prevent Rx for the same reason */
4889 if (unlikely(local->in_reconfig))
4890 goto drop;
4891
Johannes Bergea77f122009-08-21 14:44:45 +02004892 /*
4893 * The same happens when we're not even started,
4894 * but that's worth a warning.
4895 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004896 if (WARN_ON(!local->started))
4897 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004898
Johannes Bergfc885182010-07-30 13:23:12 +02004899 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004900 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004901 * Validate the rate, unless a PLCP error means that
4902 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004903 */
Johannes Bergfc885182010-07-30 13:23:12 +02004904
Johannes Bergda6a4352017-04-26 12:14:59 +02004905 switch (status->encoding) {
4906 case RX_ENC_HT:
Johannes Bergfc885182010-07-30 13:23:12 +02004907 /*
4908 * rate_idx is MCS index, which can be [0-76]
4909 * as documented on:
4910 *
Flavio Suligoi59d4bfc2020-06-05 17:41:12 +02004911 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
Johannes Bergfc885182010-07-30 13:23:12 +02004912 *
4913 * Anything else would be some sort of driver or
4914 * hardware error. The driver should catch hardware
4915 * errors.
4916 */
Johannes Berg444e3802012-09-30 17:08:35 +02004917 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004918 "Rate marked as an HT rate but passed "
4919 "status->rate_idx is not "
4920 "an MCS index [0-76]: %d (0x%02x)\n",
4921 status->rate_idx,
4922 status->rate_idx))
4923 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004924 break;
4925 case RX_ENC_VHT:
Johannes Berg56146182012-11-09 15:07:02 +01004926 if (WARN_ONCE(status->rate_idx > 9 ||
Johannes Berg8613c942017-04-26 13:51:41 +02004927 !status->nss ||
4928 status->nss > 8,
Johannes Berg56146182012-11-09 15:07:02 +01004929 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
Johannes Berg8613c942017-04-26 13:51:41 +02004930 status->rate_idx, status->nss))
Johannes Berg56146182012-11-09 15:07:02 +01004931 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004932 break;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004933 case RX_ENC_HE:
4934 if (WARN_ONCE(status->rate_idx > 11 ||
4935 !status->nss ||
4936 status->nss > 8,
4937 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4938 status->rate_idx, status->nss))
4939 goto drop;
4940 break;
Johannes Bergda6a4352017-04-26 12:14:59 +02004941 default:
4942 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004943 fallthrough;
Johannes Bergda6a4352017-04-26 12:14:59 +02004944 case RX_ENC_LEGACY:
Johannes Berg444e3802012-09-30 17:08:35 +02004945 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004946 goto drop;
4947 rate = &sband->bitrates[status->rate_idx];
4948 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004949 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004950
Johannes Berg554891e2010-09-24 12:38:25 +02004951 status->rx_flags = 0;
4952
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004953 kcov_remote_start_common(skb_get_kcov_handle(skb));
4954
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004955 /*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004956 * Frames with failed FCS/PLCP checksum are not returned,
4957 * all other frames are returned without radiotap header
4958 * if it was previously present.
4959 * Also, frames with less than 16 bytes are dropped.
4960 */
Felix Fietkau80a915e2020-12-18 19:47:18 +01004961 if (!(status->flag & RX_FLAG_8023))
4962 skb = ieee80211_rx_monitor(local, skb, rate);
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004963 if (skb) {
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004964 if ((status->flag & RX_FLAG_8023) ||
4965 ieee80211_is_data_present(hdr->frame_control))
4966 ieee80211_tpt_led_trig_rx(local, skb->len);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004967
Felix Fietkau80a915e2020-12-18 19:47:18 +01004968 if (status->flag & RX_FLAG_8023)
4969 __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
4970 else
4971 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004972 }
Johannes Bergd63b5482016-03-31 20:02:02 +03004973
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004974 kcov_remote_stop();
Johannes Berg77a980d2009-08-24 11:46:30 +02004975 return;
4976 drop:
4977 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004978}
Felix Fietkauc5d16862020-07-26 13:06:11 +02004979EXPORT_SYMBOL(ieee80211_rx_list);
4980
4981void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4982 struct sk_buff *skb, struct napi_struct *napi)
4983{
4984 struct sk_buff *tmp;
4985 LIST_HEAD(list);
4986
4987
4988 /*
4989 * key references and virtual interfaces are protected using RCU
4990 * and this requires that we are in a read-side RCU section during
4991 * receive processing
4992 */
4993 rcu_read_lock();
4994 ieee80211_rx_list(hw, pubsta, skb, &list);
4995 rcu_read_unlock();
4996
4997 if (!napi) {
4998 netif_receive_skb_list(&list);
4999 return;
5000 }
5001
5002 list_for_each_entry_safe(skb, tmp, &list, list) {
5003 skb_list_del_init(skb);
5004 napi_gro_receive(napi, skb);
5005 }
5006}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02005007EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02005008
5009/* This is a version of the rx handler that can be called from hard irq
5010 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02005011void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02005012{
5013 struct ieee80211_local *local = hw_to_local(hw);
5014
5015 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5016
Johannes Berg571ecf62007-07-27 15:43:22 +02005017 skb->pkt_type = IEEE80211_RX_MSG;
5018 skb_queue_tail(&local->skb_queue, skb);
5019 tasklet_schedule(&local->tasklet);
5020}
5021EXPORT_SYMBOL(ieee80211_rx_irqsafe);