blob: 972895e9f22dc5e4b977087bba880f9a40fcdcdb [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
Ilan Peerc4d800d2020-01-31 13:12:53 +02009 * Copyright (C) 2018-2020 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>
Sara Sharon06470f72016-01-28 16:19:25 +020020#include <linux/bitops.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020021#include <net/mac80211.h>
22#include <net/ieee80211_radiotap.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010023#include <asm/unaligned.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020024
25#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020026#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040027#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010028#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020029#include "wep.h"
30#include "wpa.h"
31#include "tkip.h"
32#include "wme.h"
Johannes Berg1d8d3de2011-12-16 15:28:57 +010033#include "rate.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020034
Johannes Bergb2e77712007-09-26 15:19:39 +020035/*
36 * monitor mode reception
37 *
38 * This function cleans up the SKB, i.e. it removes all the stuff
39 * only useful for monitoring.
40 */
Johannes Bergc1129922020-05-26 14:33:48 +020041static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
42 unsigned int present_fcs_len,
43 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020044{
Johannes Bergc1129922020-05-26 14:33:48 +020045 struct ieee80211_hdr *hdr;
46 unsigned int hdrlen;
47 __le16 fc;
48
Johannes Berg30841f52017-04-11 15:38:56 +020049 if (present_fcs_len)
50 __pskb_trim(skb, skb->len - present_fcs_len);
Johannes Bergc096b922018-04-20 13:49:18 +030051 __pskb_pull(skb, rtap_space);
Johannes Bergc1129922020-05-26 14:33:48 +020052
53 hdr = (void *)skb->data;
54 fc = hdr->frame_control;
55
56 /*
57 * Remove the HT-Control field (if present) on management
58 * frames after we've sent the frame to monitoring. We
59 * (currently) don't need it, and don't properly parse
60 * frames with it present, due to the assumption of a
61 * fixed management header length.
62 */
63 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
64 return skb;
65
66 hdrlen = ieee80211_hdrlen(fc);
67 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
68
69 if (!pskb_may_pull(skb, hdrlen)) {
70 dev_kfree_skb(skb);
71 return NULL;
72 }
73
74 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
75 hdrlen - IEEE80211_HT_CTL_LEN);
76 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
77
78 return skb;
Johannes Bergb2e77712007-09-26 15:19:39 +020079}
80
Johannes Berg1f7bba72014-11-06 22:56:36 +010081static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
Johannes Bergc096b922018-04-20 13:49:18 +030082 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020083{
Johannes Bergf1d58c22009-06-17 13:13:00 +020084 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +010085 struct ieee80211_hdr *hdr;
86
Johannes Bergc096b922018-04-20 13:49:18 +030087 hdr = (void *)(skb->data + rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +020088
Johannes Berg4c298672012-07-05 11:34:31 +020089 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +010090 RX_FLAG_FAILED_PLCP_CRC |
Shaul Triebitzc3d1f872018-09-05 08:06:06 +030091 RX_FLAG_ONLY_MONITOR |
92 RX_FLAG_NO_PSDU))
Zhao, Gang6b59db72014-04-21 12:52:59 +080093 return true;
94
Johannes Bergc096b922018-04-20 13:49:18 +030095 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +080096 return true;
97
Harvey Harrison87228f52008-06-11 14:21:59 -070098 if (ieee80211_is_ctl(hdr->frame_control) &&
99 !ieee80211_is_pspoll(hdr->frame_control) &&
100 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800101 return true;
102
103 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200104}
105
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200106static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100107ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
108 struct ieee80211_rx_status *status,
109 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200110{
111 int len;
112
113 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200114 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200115
Johannes Berga144f372013-07-03 13:34:02 +0200116 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200117 if (status->chains)
118 len += 4 * hweight8(status->chains);
Johannes Bergefc38dd2018-12-15 11:03:12 +0200119 /* vendor presence bitmap */
120 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
121 len += 4;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100122
123 if (ieee80211_have_rx_timestamp(status)) {
124 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200125 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100126 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200127 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200128 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200129
Johannes Berga144f372013-07-03 13:34:02 +0200130 /* antenna field, if we don't have per-chain info */
131 if (!status->chains)
132 len += 1;
133
Johannes Berg90b9e4462012-11-16 10:09:08 +0100134 /* padding for RX_FLAGS if necessary */
135 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200136
Johannes Bergda6a4352017-04-26 12:14:59 +0200137 if (status->encoding == RX_ENC_HT) /* HT info */
Johannes Berg6d744ba2011-01-27 14:13:17 +0100138 len += 3;
139
Johannes Berg4c298672012-07-05 11:34:31 +0200140 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100141 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200142 len += 8;
143 }
144
Johannes Bergda6a4352017-04-26 12:14:59 +0200145 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100146 len = ALIGN(len, 2);
147 len += 12;
148 }
149
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300150 if (local->hw.radiotap_timestamp.units_pos >= 0) {
151 len = ALIGN(len, 8);
152 len += 12;
153 }
154
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300155 if (status->encoding == RX_ENC_HE &&
156 status->flag & RX_FLAG_RADIOTAP_HE) {
157 len = ALIGN(len, 2);
158 len += 12;
159 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
160 }
161
162 if (status->encoding == RX_ENC_HE &&
163 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
164 len = ALIGN(len, 2);
165 len += 12;
166 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
167 }
168
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300169 if (status->flag & RX_FLAG_NO_PSDU)
170 len += 1;
171
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300172 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
173 len = ALIGN(len, 2);
174 len += 4;
175 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
176 }
177
Johannes Berga144f372013-07-03 13:34:02 +0200178 if (status->chains) {
179 /* antenna and antenna signal fields */
180 len += 2 * hweight8(status->chains);
181 }
182
Johannes Berg1f7bba72014-11-06 22:56:36 +0100183 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
Liad Kaufmanc15353b2019-02-06 13:17:13 +0200184 struct ieee80211_vendor_radiotap *rtap;
185 int vendor_data_offset = 0;
186
187 /*
188 * The position to look at depends on the existence (or non-
189 * existence) of other elements, so take that into account...
190 */
191 if (status->flag & RX_FLAG_RADIOTAP_HE)
192 vendor_data_offset +=
193 sizeof(struct ieee80211_radiotap_he);
194 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
195 vendor_data_offset +=
196 sizeof(struct ieee80211_radiotap_he_mu);
197 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
198 vendor_data_offset +=
199 sizeof(struct ieee80211_radiotap_lsig);
200
201 rtap = (void *)&skb->data[vendor_data_offset];
Johannes Berg1f7bba72014-11-06 22:56:36 +0100202
Johannes Berg1f7bba72014-11-06 22:56:36 +0100203 /* alignment for fixed 6-byte vendor data header */
204 len = ALIGN(len, 2);
205 /* vendor data header */
206 len += 6;
207 if (WARN_ON(rtap->align == 0))
208 rtap->align = 1;
209 len = ALIGN(len, rtap->align);
210 len += rtap->len + rtap->pad;
211 }
212
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200213 return len;
214}
215
Johannes Berg9e478062017-04-13 14:23:49 +0200216static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
217 struct sk_buff *skb,
Johannes Bergc096b922018-04-20 13:49:18 +0300218 int rtap_space)
Johannes Berg9e478062017-04-13 14:23:49 +0200219{
220 struct {
221 struct ieee80211_hdr_3addr hdr;
222 u8 category;
223 u8 action_code;
Mathieu Malaterre7c53eb52019-01-24 19:19:57 +0100224 } __packed __aligned(2) action;
Johannes Berg9e478062017-04-13 14:23:49 +0200225
226 if (!sdata)
227 return;
228
229 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
230
Johannes Bergc096b922018-04-20 13:49:18 +0300231 if (skb->len < rtap_space + sizeof(action) +
Johannes Berg9e478062017-04-13 14:23:49 +0200232 VHT_MUMIMO_GROUPS_DATA_LEN)
233 return;
234
235 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
236 return;
237
Johannes Bergc096b922018-04-20 13:49:18 +0300238 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
Johannes Berg9e478062017-04-13 14:23:49 +0200239
240 if (!ieee80211_is_action(action.hdr.frame_control))
241 return;
242
243 if (action.category != WLAN_CATEGORY_VHT)
244 return;
245
246 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
247 return;
248
249 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
250 return;
251
252 skb = skb_copy(skb, GFP_ATOMIC);
253 if (!skb)
254 return;
255
Johannes Berg9e478062017-04-13 14:23:49 +0200256 skb_queue_tail(&sdata->skb_queue, skb);
257 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
258}
259
Johannes Berg00ea6de2012-09-05 15:54:51 +0200260/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200261 * ieee80211_add_rx_radiotap_header - add radiotap header
262 *
263 * add a radiotap header containing all the fields which the hardware provided.
264 */
265static void
266ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
267 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200268 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200269 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200270{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200271 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200272 struct ieee80211_radiotap_header *rthdr;
273 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200274 __le32 *it_present;
275 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100276 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200277 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200278 int mpdulen, chain;
279 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100280 struct ieee80211_vendor_radiotap rtap = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300281 struct ieee80211_radiotap_he he = {};
282 struct ieee80211_radiotap_he_mu he_mu = {};
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300283 struct ieee80211_radiotap_lsig lsig = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300284
285 if (status->flag & RX_FLAG_RADIOTAP_HE) {
286 he = *(struct ieee80211_radiotap_he *)skb->data;
287 skb_pull(skb, sizeof(he));
288 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
289 }
290
291 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
292 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
293 skb_pull(skb, sizeof(he_mu));
294 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100295
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300296 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
297 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
298 skb_pull(skb, sizeof(lsig));
299 }
300
Johannes Berg1f7bba72014-11-06 22:56:36 +0100301 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
302 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
303 /* rtap.len and rtap.pad are undone immediately */
304 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
305 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800306
307 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200308 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800309 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200310
Johannes Bergd58ff352017-06-16 14:29:23 +0200311 rthdr = skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100312 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200313 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200314
315 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200316 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200317 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
318 BIT(IEEE80211_RADIOTAP_CHANNEL) |
319 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200320
Johannes Berga144f372013-07-03 13:34:02 +0200321 if (!status->chains)
322 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
323
324 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
325 it_present_val |=
326 BIT(IEEE80211_RADIOTAP_EXT) |
327 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
328 put_unaligned_le32(it_present_val, it_present);
329 it_present++;
330 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
331 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
332 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200333
Johannes Berg1f7bba72014-11-06 22:56:36 +0100334 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
335 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
336 BIT(IEEE80211_RADIOTAP_EXT);
337 put_unaligned_le32(it_present_val, it_present);
338 it_present++;
339 it_present_val = rtap.present;
340 }
341
Johannes Berga144f372013-07-03 13:34:02 +0200342 put_unaligned_le32(it_present_val, it_present);
343
344 pos = (void *)(it_present + 1);
345
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200346 /* the order of the following fields is important */
347
348 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800349 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100350 /* padding */
351 while ((pos - (u8 *)rthdr) & 7)
352 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800353 put_unaligned_le64(
354 ieee80211_calculate_rx_timestamp(local, status,
355 mpdulen, 0),
356 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200357 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200358 pos += 8;
359 }
360
361 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200362 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200363 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100364 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
365 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200366 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200367 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200368 pos++;
369
370 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergda6a4352017-04-26 12:14:59 +0200371 if (!rate || status->encoding != RX_ENC_LEGACY) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200372 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100373 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530374 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200375 * added below. The byte here is needed as padding
376 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200377 */
378 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200379 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200380 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200381 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Johannes Bergda6a4352017-04-26 12:14:59 +0200382 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200383 shift = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200384 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200385 shift = 2;
386 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200387 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200388 pos++;
389
390 /* IEEE80211_RADIOTAP_CHANNEL */
Thomas Pedersen3b23c1842020-04-01 18:18:05 -0700391 /* TODO: frequency offset in KHz */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100392 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200393 pos += 2;
Johannes Bergda6a4352017-04-26 12:14:59 +0200394 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200395 channel_flags |= IEEE80211_CHAN_HALF;
Johannes Bergda6a4352017-04-26 12:14:59 +0200396 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200397 channel_flags |= IEEE80211_CHAN_QUARTER;
398
Aloka Dixit412a84b2020-09-11 01:11:35 +0000399 if (status->band == NL80211_BAND_5GHZ ||
400 status->band == NL80211_BAND_6GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200401 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Bergda6a4352017-04-26 12:14:59 +0200402 else if (status->encoding != RX_ENC_LEGACY)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200403 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100404 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200405 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100406 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100407 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100408 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200409 channel_flags |= IEEE80211_CHAN_2GHZ;
410 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200411 pos += 2;
412
413 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200414 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100415 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200416 *pos = status->signal;
417 rthdr->it_present |=
418 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
419 pos++;
420 }
421
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200422 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
423
Johannes Berga144f372013-07-03 13:34:02 +0200424 if (!status->chains) {
425 /* IEEE80211_RADIOTAP_ANTENNA */
426 *pos = status->antenna;
427 pos++;
428 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200429
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200430 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
431
432 /* IEEE80211_RADIOTAP_RX_FLAGS */
433 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100434 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100435 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100436 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100437 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
438 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200439 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100440
Johannes Bergda6a4352017-04-26 12:14:59 +0200441 if (status->encoding == RX_ENC_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200442 unsigned int stbc;
443
Johannes Berg6d744ba2011-01-27 14:13:17 +0100444 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200445 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100446 *pos = 0;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200447 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100448 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
Johannes Bergda6a4352017-04-26 12:14:59 +0200449 if (status->bw == RATE_INFO_BW_40)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100450 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200451 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
Johannes Bergac55d2f2012-05-10 09:09:10 +0200452 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200453 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200454 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200455 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200456 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100457 pos++;
458 *pos++ = status->rate_idx;
459 }
Johannes Berg4c298672012-07-05 11:34:31 +0200460
461 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
462 u16 flags = 0;
463
464 /* ensure 4 byte alignment */
465 while ((pos - (u8 *)rthdr) & 3)
466 pos++;
467 rthdr->it_present |=
468 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
469 put_unaligned_le32(status->ampdu_reference, pos);
470 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200471 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
472 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
473 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
474 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
475 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
476 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
477 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
478 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
Johannes Berg7299d6f2018-02-19 14:48:39 +0200479 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
480 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
481 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
482 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
Johannes Berg4c298672012-07-05 11:34:31 +0200483 put_unaligned_le16(flags, pos);
484 pos += 2;
485 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
486 *pos++ = status->ampdu_delimiter_crc;
487 else
488 *pos++ = 0;
489 *pos++ = 0;
490 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100491
Johannes Bergda6a4352017-04-26 12:14:59 +0200492 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100493 u16 known = local->hw.radiotap_vht_details;
494
495 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100496 put_unaligned_le16(known, pos);
497 pos += 2;
498 /* flags */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200499 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg51648922012-11-22 23:00:18 +0100500 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200501 /* in VHT, STBC is binary */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200502 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200503 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200504 if (status->enc_flags & RX_ENC_FLAG_BF)
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200505 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100506 pos++;
507 /* bandwidth */
Johannes Bergda6a4352017-04-26 12:14:59 +0200508 switch (status->bw) {
509 case RATE_INFO_BW_80:
Johannes Berg51648922012-11-22 23:00:18 +0100510 *pos++ = 4;
Johannes Bergda6a4352017-04-26 12:14:59 +0200511 break;
512 case RATE_INFO_BW_160:
Johannes Berg51648922012-11-22 23:00:18 +0100513 *pos++ = 11;
Johannes Bergda6a4352017-04-26 12:14:59 +0200514 break;
515 case RATE_INFO_BW_40:
Johannes Berg51648922012-11-22 23:00:18 +0100516 *pos++ = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200517 break;
518 default:
Johannes Berg51648922012-11-22 23:00:18 +0100519 *pos++ = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200520 }
Johannes Berg51648922012-11-22 23:00:18 +0100521 /* MCS/NSS */
Johannes Berg8613c942017-04-26 13:51:41 +0200522 *pos = (status->rate_idx << 4) | status->nss;
Johannes Berg51648922012-11-22 23:00:18 +0100523 pos += 4;
524 /* coding field */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200525 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200526 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100527 pos++;
528 /* group ID */
529 pos++;
530 /* partial_aid */
531 pos += 2;
532 }
533
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300534 if (local->hw.radiotap_timestamp.units_pos >= 0) {
535 u16 accuracy = 0;
536 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
537
538 rthdr->it_present |=
539 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
540
541 /* ensure 8 byte alignment */
542 while ((pos - (u8 *)rthdr) & 7)
543 pos++;
544
545 put_unaligned_le64(status->device_timestamp, pos);
546 pos += sizeof(u64);
547
548 if (local->hw.radiotap_timestamp.accuracy >= 0) {
549 accuracy = local->hw.radiotap_timestamp.accuracy;
550 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
551 }
552 put_unaligned_le16(accuracy, pos);
553 pos += sizeof(u16);
554
555 *pos++ = local->hw.radiotap_timestamp.units_pos;
556 *pos++ = flags;
557 }
558
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300559 if (status->encoding == RX_ENC_HE &&
560 status->flag & RX_FLAG_RADIOTAP_HE) {
Johannes Berg331aead2018-08-31 11:31:07 +0300561#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300562
563 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
564 he.data6 |= HE_PREP(DATA6_NSTS,
565 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
566 status->enc_flags));
567 he.data3 |= HE_PREP(DATA3_STBC, 1);
568 } else {
569 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
570 }
571
572#define CHECK_GI(s) \
573 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
574 (int)NL80211_RATE_INFO_HE_GI_##s)
575
576 CHECK_GI(0_8);
577 CHECK_GI(1_6);
578 CHECK_GI(3_2);
579
580 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
581 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
582 he.data3 |= HE_PREP(DATA3_CODING,
583 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
584
585 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
586
587 switch (status->bw) {
588 case RATE_INFO_BW_20:
589 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
590 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
591 break;
592 case RATE_INFO_BW_40:
593 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
594 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
595 break;
596 case RATE_INFO_BW_80:
597 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
598 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
599 break;
600 case RATE_INFO_BW_160:
601 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
602 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
603 break;
604 case RATE_INFO_BW_HE_RU:
605#define CHECK_RU_ALLOC(s) \
606 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
607 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
608
609 CHECK_RU_ALLOC(26);
610 CHECK_RU_ALLOC(52);
611 CHECK_RU_ALLOC(106);
612 CHECK_RU_ALLOC(242);
613 CHECK_RU_ALLOC(484);
614 CHECK_RU_ALLOC(996);
615 CHECK_RU_ALLOC(2x996);
616
617 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
618 status->he_ru + 4);
619 break;
620 default:
621 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
622 }
623
624 /* ensure 2 byte alignment */
625 while ((pos - (u8 *)rthdr) & 1)
626 pos++;
627 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
628 memcpy(pos, &he, sizeof(he));
629 pos += sizeof(he);
630 }
631
632 if (status->encoding == RX_ENC_HE &&
633 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
634 /* ensure 2 byte alignment */
635 while ((pos - (u8 *)rthdr) & 1)
636 pos++;
637 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
638 memcpy(pos, &he_mu, sizeof(he_mu));
639 pos += sizeof(he_mu);
640 }
641
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300642 if (status->flag & RX_FLAG_NO_PSDU) {
643 rthdr->it_present |=
644 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
645 *pos++ = status->zero_length_psdu_type;
646 }
647
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300648 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
649 /* ensure 2 byte alignment */
650 while ((pos - (u8 *)rthdr) & 1)
651 pos++;
652 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
653 memcpy(pos, &lsig, sizeof(lsig));
654 pos += sizeof(lsig);
655 }
656
Johannes Berga144f372013-07-03 13:34:02 +0200657 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
658 *pos++ = status->chain_signal[chain];
659 *pos++ = chain;
660 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100661
662 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
663 /* ensure 2 byte alignment for the vendor field as required */
664 if ((pos - (u8 *)rthdr) & 1)
665 *pos++ = 0;
666 *pos++ = rtap.oui[0];
667 *pos++ = rtap.oui[1];
668 *pos++ = rtap.oui[2];
669 *pos++ = rtap.subns;
670 put_unaligned_le16(rtap.len, pos);
671 pos += 2;
672 /* align the actual payload as requested */
673 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
674 *pos++ = 0;
675 /* data (and possible padding) already follows */
676 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200677}
678
Johannes Berg127f60b2017-04-13 15:50:27 +0200679static struct sk_buff *
680ieee80211_make_monitor_skb(struct ieee80211_local *local,
681 struct sk_buff **origskb,
682 struct ieee80211_rate *rate,
Johannes Bergc096b922018-04-20 13:49:18 +0300683 int rtap_space, bool use_origskb)
Johannes Berg127f60b2017-04-13 15:50:27 +0200684{
685 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
686 int rt_hdrlen, needed_headroom;
687 struct sk_buff *skb;
688
689 /* room for the radiotap header based on driver features */
690 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
Johannes Bergc096b922018-04-20 13:49:18 +0300691 needed_headroom = rt_hdrlen - rtap_space;
Johannes Berg127f60b2017-04-13 15:50:27 +0200692
693 if (use_origskb) {
694 /* only need to expand headroom if necessary */
695 skb = *origskb;
696 *origskb = NULL;
697
698 /*
699 * This shouldn't trigger often because most devices have an
700 * RX header they pull before we get here, and that should
701 * be big enough for our radiotap information. We should
702 * probably export the length to drivers so that we can have
703 * them allocate enough headroom to start with.
704 */
705 if (skb_headroom(skb) < needed_headroom &&
706 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
707 dev_kfree_skb(skb);
708 return NULL;
709 }
710 } else {
711 /*
712 * Need to make a copy and possibly remove radiotap header
713 * and FCS from the original.
714 */
715 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
716
717 if (!skb)
718 return NULL;
719 }
720
721 /* prepend radiotap information */
722 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
723
724 skb_reset_mac_header(skb);
725 skb->ip_summed = CHECKSUM_UNNECESSARY;
726 skb->pkt_type = PACKET_OTHERHOST;
727 skb->protocol = htons(ETH_P_802_2);
728
729 return skb;
730}
731
Johannes Bergb2e77712007-09-26 15:19:39 +0200732/*
733 * This function copies a received frame to all monitor interfaces and
734 * returns a cleaned-up SKB that no longer includes the FCS nor the
735 * radiotap header the driver might have added.
736 */
737static struct sk_buff *
738ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100739 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200740{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200741 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200742 struct ieee80211_sub_if_data *sdata;
Johannes Berg127f60b2017-04-13 15:50:27 +0200743 struct sk_buff *monskb = NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200744 int present_fcs_len = 0;
Johannes Bergc096b922018-04-20 13:49:18 +0300745 unsigned int rtap_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300746 struct ieee80211_sub_if_data *monitor_sdata =
747 rcu_dereference(local->monitor_sdata);
Johannes Berg127f60b2017-04-13 15:50:27 +0200748 bool only_monitor = false;
Ilan Peer80209192018-12-15 11:03:17 +0200749 unsigned int min_head_len;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100750
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300751 if (status->flag & RX_FLAG_RADIOTAP_HE)
752 rtap_space += sizeof(struct ieee80211_radiotap_he);
753
754 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
755 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
756
Ilan Peerd359bbc2018-12-15 11:03:25 +0200757 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
758 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
759
Johannes Berg1f7bba72014-11-06 22:56:36 +0100760 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
Ilan Peerd359bbc2018-12-15 11:03:25 +0200761 struct ieee80211_vendor_radiotap *rtap =
762 (void *)(origskb->data + rtap_space);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100763
Johannes Bergc096b922018-04-20 13:49:18 +0300764 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100765 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200766
Ilan Peer80209192018-12-15 11:03:17 +0200767 min_head_len = rtap_space;
768
Johannes Bergb2e77712007-09-26 15:19:39 +0200769 /*
770 * First, we may need to make a copy of the skb because
771 * (1) we need to modify it for radiotap (if not present), and
772 * (2) the other RX handlers will modify the skb we got.
773 *
774 * We don't need to, of course, if we aren't going to return
775 * the SKB because it has a bad FCS/PLCP checksum.
776 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100777
Ilan Peer80209192018-12-15 11:03:17 +0200778 if (!(status->flag & RX_FLAG_NO_PSDU)) {
779 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
780 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
781 /* driver bug */
782 WARN_ON(1);
783 dev_kfree_skb(origskb);
784 return NULL;
785 }
786 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200787 }
Ilan Peer80209192018-12-15 11:03:17 +0200788
789 /* also consider the hdr->frame_control */
790 min_head_len += 2;
Johannes Berg30841f52017-04-11 15:38:56 +0200791 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200792
Ilan Peer80209192018-12-15 11:03:17 +0200793 /* ensure that the expected data elements are in skb head */
794 if (!pskb_may_pull(origskb, min_head_len)) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800795 dev_kfree_skb(origskb);
796 return NULL;
797 }
798
Johannes Bergc096b922018-04-20 13:49:18 +0300799 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
Johannes Berg127f60b2017-04-13 15:50:27 +0200800
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100801 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200802 if (only_monitor) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200803 dev_kfree_skb(origskb);
804 return NULL;
805 }
806
Johannes Bergc1129922020-05-26 14:33:48 +0200807 return ieee80211_clean_skb(origskb, present_fcs_len,
808 rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200809 }
810
Johannes Bergc096b922018-04-20 13:49:18 +0300811 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
Johannes Berg9e478062017-04-13 14:23:49 +0200812
Johannes Bergf64331d2017-04-13 13:28:18 +0200813 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200814 bool last_monitor = list_is_last(&sdata->u.mntr.list,
815 &local->mon_list);
816
817 if (!monskb)
818 monskb = ieee80211_make_monitor_skb(local, &origskb,
Johannes Bergc096b922018-04-20 13:49:18 +0300819 rate, rtap_space,
Johannes Berg127f60b2017-04-13 15:50:27 +0200820 only_monitor &&
821 last_monitor);
822
823 if (monskb) {
824 struct sk_buff *skb;
825
826 if (last_monitor) {
827 skb = monskb;
828 monskb = NULL;
829 } else {
830 skb = skb_clone(monskb, GFP_ATOMIC);
831 }
832
833 if (skb) {
834 skb->dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +0200835 dev_sw_netstats_rx_add(skb->dev, skb->len);
Johannes Berg127f60b2017-04-13 15:50:27 +0200836 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200837 }
838 }
839
Johannes Berg127f60b2017-04-13 15:50:27 +0200840 if (last_monitor)
841 break;
Johannes Bergb2e77712007-09-26 15:19:39 +0200842 }
843
Johannes Berg127f60b2017-04-13 15:50:27 +0200844 /* this happens if last_monitor was erroneously false */
845 dev_kfree_skb(monskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200846
Johannes Berg127f60b2017-04-13 15:50:27 +0200847 /* ditto */
848 if (!origskb)
849 return NULL;
850
Johannes Bergc1129922020-05-26 14:33:48 +0200851 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200852}
853
Johannes Berg5cf121c2008-02-25 16:27:43 +0100854static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200855{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700856 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200857 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200858 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200859
860 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700861 if (ieee80211_is_data_qos(hdr->frame_control)) {
862 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200863 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700864 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200865 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200866 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200867
868 seqno_idx = tid;
869 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200870 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200871 /*
872 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
873 *
874 * Sequence numbers for management frames, QoS data
875 * frames with a broadcast/multicast address in the
876 * Address 1 field, and all non-QoS data frames sent
877 * by QoS STAs are assigned using an additional single
878 * modulo-4096 counter, [...]
879 *
880 * We also use that counter for non-QoS STAs.
881 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100882 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200883 security_idx = 0;
884 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100885 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200886 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200887 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200888
Johannes Berg9e262972011-07-07 18:45:03 +0200889 rx->seqno_idx = seqno_idx;
890 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200891 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
892 * For now, set skb->priority to 0 for other cases. */
893 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100894}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200895
Johannes Bergd1c3a372009-01-07 00:26:10 +0100896/**
897 * DOC: Packet alignment
898 *
899 * Drivers always need to pass packets that are aligned to two-byte boundaries
900 * to the stack.
901 *
902 * Additionally, should, if possible, align the payload data in a way that
903 * guarantees that the contained IP header is aligned to a four-byte
904 * boundary. In the case of regular frames, this simply means aligning the
905 * payload to a four-byte boundary (because either the IP header is directly
906 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100907 * in front of it). If the payload data is not properly aligned and the
908 * architecture doesn't support efficient unaligned operations, mac80211
909 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100910 *
911 * With A-MSDU frames, however, the payload data address must yield two modulo
912 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
913 * push the IP header further back to a multiple of four again. Thankfully, the
914 * specs were sane enough this time around to require padding each A-MSDU
915 * subframe to a length that is a multiple of four.
916 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300917 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100918 * the payload is not supported, the driver is required to move the 802.11
919 * header to be directly in front of the payload in that case.
920 */
921static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100922{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100923#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100924 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100925#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200926}
927
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200928
Johannes Berg571ecf62007-07-27 15:43:22 +0200929/* rx handlers */
930
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200931static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
932{
933 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
934
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100935 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200936 return 0;
937
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100938 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200939}
940
941
942static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
943{
944 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
945
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100946 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200947 return 0;
948
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100949 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200950}
951
952
953/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
954static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
955{
956 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
957 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200958 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200959
Johannes Berg1df332e2012-10-26 00:09:11 +0200960 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200961 return -1;
962
Jouni Malinenaf2d14b2020-02-22 15:25:47 +0200963 if (!ieee80211_is_robust_mgmt_frame(skb) &&
964 !ieee80211_is_beacon(hdr->frame_control))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200965 return -1; /* not a robust management frame */
966
967 mmie = (struct ieee80211_mmie *)
968 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200969 if (mmie->element_id == WLAN_EID_MMIE &&
970 mmie->length == sizeof(*mmie) - 2)
971 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200972
Jouni Malinen56c52da2015-01-24 19:52:08 +0200973 mmie16 = (struct ieee80211_mmie_16 *)
974 (skb->data + skb->len - sizeof(*mmie16));
975 if (skb->len >= 24 + sizeof(*mmie16) &&
976 mmie16->element_id == WLAN_EID_MMIE &&
977 mmie16->length == sizeof(*mmie16) - 2)
978 return le16_to_cpu(mmie16->key_id);
979
980 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200981}
982
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100983static int ieee80211_get_keyid(struct sk_buff *skb,
984 const struct ieee80211_cipher_scheme *cs)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200985{
986 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
987 __le16 fc;
988 int hdrlen;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100989 int minlen;
990 u8 key_idx_off;
991 u8 key_idx_shift;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200992 u8 keyid;
993
994 fc = hdr->frame_control;
995 hdrlen = ieee80211_hdrlen(fc);
996
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100997 if (cs) {
998 minlen = hdrlen + cs->hdr_len;
999 key_idx_off = hdrlen + cs->key_idx_off;
1000 key_idx_shift = cs->key_idx_shift;
1001 } else {
1002 /* WEP, TKIP, CCMP and GCMP */
1003 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1004 key_idx_off = hdrlen + 3;
1005 key_idx_shift = 6;
1006 }
1007
1008 if (unlikely(skb->len < minlen))
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001009 return -EINVAL;
1010
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001011 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1012
1013 if (cs)
1014 keyid &= cs->key_idx_mask;
1015 keyid >>= key_idx_shift;
1016
1017 /* cs could use more than the usual two bits for the keyid */
1018 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1019 return -EINVAL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001020
1021 return keyid;
1022}
1023
Johannes Berg1df332e2012-10-26 00:09:11 +02001024static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001025{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001026 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +01001027 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001028
Harvey Harrisona7767f92008-07-02 16:30:51 -07001029 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001030 if (is_multicast_ether_addr(hdr->addr1)) {
1031 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +02001032 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001033 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001034 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001035 return RX_DROP_MONITOR;
1036 } else {
1037 if (!ieee80211_has_a4(hdr->frame_control))
1038 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001039 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001040 return RX_DROP_MONITOR;
1041 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001042 }
1043
1044 /* If there is not an established peer link and this is not a peer link
1045 * establisment frame, beacon or probe, drop the frame.
1046 */
1047
Javier Cardona57cf8042011-05-13 10:45:43 -07001048 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001049 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001050
Harvey Harrisona7767f92008-07-02 16:30:51 -07001051 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001052 return RX_DROP_MONITOR;
1053
Harvey Harrisona7767f92008-07-02 16:30:51 -07001054 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001055 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001056
1057 /* make sure category field is present */
1058 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1059 return RX_DROP_MONITOR;
1060
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001061 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001062 category = mgmt->u.action.category;
1063 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +02001064 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001065 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001066 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001067 }
1068
Harvey Harrisona7767f92008-07-02 16:30:51 -07001069 if (ieee80211_is_probe_req(hdr->frame_control) ||
1070 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -07001071 ieee80211_is_beacon(hdr->frame_control) ||
1072 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -07001073 return RX_CONTINUE;
1074
1075 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001076 }
1077
Johannes Berg902acc72008-02-23 15:17:19 +01001078 return RX_CONTINUE;
1079}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001080
Johannes Bergfb4ea052016-01-28 16:19:24 +02001081static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1082 int index)
1083{
1084 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1085 struct sk_buff *tail = skb_peek_tail(frames);
1086 struct ieee80211_rx_status *status;
1087
Sara Sharon06470f72016-01-28 16:19:25 +02001088 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1089 return true;
1090
Johannes Bergfb4ea052016-01-28 16:19:24 +02001091 if (!tail)
1092 return false;
1093
1094 status = IEEE80211_SKB_RXCB(tail);
1095 if (status->flag & RX_FLAG_AMSDU_MORE)
1096 return false;
1097
1098 return true;
1099}
1100
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001101static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001102 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001103 int index,
1104 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001105{
Michal Kazior83eb9352014-07-16 12:09:31 +02001106 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1107 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +01001108 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001109
Johannes Bergdd318572010-11-29 11:09:16 +01001110 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1111
Michal Kazior83eb9352014-07-16 12:09:31 +02001112 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001113 goto no_frame;
1114
Johannes Bergfb4ea052016-01-28 16:19:24 +02001115 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001116 __skb_queue_purge(skb_list);
1117 goto no_frame;
1118 }
1119
1120 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001121 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +02001122 while ((skb = __skb_dequeue(skb_list))) {
1123 status = IEEE80211_SKB_RXCB(skb);
1124 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1125 __skb_queue_tail(frames, skb);
1126 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001127
1128no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +02001129 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +01001130 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001131}
1132
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001133static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001134 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001135 u16 head_seq_num,
1136 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001137{
1138 int index;
1139
Johannes Bergdd318572010-11-29 11:09:16 +01001140 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1141
Johannes Berg9a886582013-02-15 19:25:00 +01001142 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001143 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001144 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1145 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001146 }
1147}
1148
1149/*
1150 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1151 * the skb was added to the buffer longer than this time ago, the earlier
1152 * frames that have not yet been received are assumed to be lost and the skb
1153 * can be released for processing. This may also release other skb's from the
1154 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001155 *
1156 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001157 */
1158#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1159
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001160static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001161 struct tid_ampdu_rx *tid_agg_rx,
1162 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001163{
Michal Kazior83eb9352014-07-16 12:09:31 +02001164 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001165
Johannes Bergdd318572010-11-29 11:09:16 +01001166 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1167
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001168 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +02001169 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +02001170 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +02001171 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001172 /*
1173 * No buffers ready to be released, but check whether any
1174 * frames in the reorder buffer have timed out.
1175 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001176 int skipped = 1;
1177 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1178 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001179 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001180 skipped++;
1181 continue;
1182 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -07001183 if (skipped &&
1184 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001185 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001186 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001187
Michal Kazior83eb9352014-07-16 12:09:31 +02001188 /* don't leave incomplete A-MSDUs around */
1189 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1190 i = (i + 1) % tid_agg_rx->buf_size)
1191 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1192
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001193 ht_dbg_ratelimited(sdata,
1194 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001195 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1196 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001197
1198 /*
1199 * Increment the head seq# also for the skipped slots.
1200 */
1201 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +01001202 (tid_agg_rx->head_seq_num +
1203 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001204 skipped = 0;
1205 }
Johannes Bergfb4ea052016-01-28 16:19:24 +02001206 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001207 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1208 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +02001209 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001210 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001211
1212 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001213 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001214
1215 for (; j != (index - 1) % tid_agg_rx->buf_size;
1216 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001217 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001218 break;
1219 }
1220
1221 set_release_timer:
1222
Johannes Berg788211d2015-04-01 14:20:42 +02001223 if (!tid_agg_rx->removed)
1224 mod_timer(&tid_agg_rx->reorder_timer,
1225 tid_agg_rx->reorder_time[j] + 1 +
1226 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001227 } else {
1228 del_timer(&tid_agg_rx->reorder_timer);
1229 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001230}
1231
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001232/*
1233 * As this function belongs to the RX path it must be under
1234 * rcu_read_lock protection. It returns false if the frame
1235 * can be processed immediately, true if it was consumed.
1236 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001237static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001238 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001239 struct sk_buff *skb,
1240 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001241{
1242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001243 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001244 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1245 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1246 u16 head_seq_num, buf_size;
1247 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001248 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001249
Johannes Bergdd318572010-11-29 11:09:16 +01001250 spin_lock(&tid_agg_rx->reorder_lock);
1251
Michal Kazior4549cf22014-09-02 14:05:10 +02001252 /*
1253 * Offloaded BA sessions have no known starting sequence number so pick
1254 * one from first Rxed frame for this tid after BA was started.
1255 */
1256 if (unlikely(tid_agg_rx->auto_seq)) {
1257 tid_agg_rx->auto_seq = false;
1258 tid_agg_rx->ssn = mpdu_seq_num;
1259 tid_agg_rx->head_seq_num = mpdu_seq_num;
1260 }
1261
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001262 buf_size = tid_agg_rx->buf_size;
1263 head_seq_num = tid_agg_rx->head_seq_num;
1264
Sara Sharonb7540d82017-02-06 15:28:42 +02001265 /*
1266 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1267 * be reordered.
1268 */
1269 if (unlikely(!tid_agg_rx->started)) {
1270 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1271 ret = false;
1272 goto out;
1273 }
1274 tid_agg_rx->started = true;
1275 }
1276
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001277 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001278 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001279 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001280 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001281 }
1282
1283 /*
1284 * If frame the sequence number exceeds our buffering window
1285 * size release some previous frames to make room for this one.
1286 */
Johannes Berg9a886582013-02-15 19:25:00 +01001287 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1288 head_seq_num = ieee80211_sn_inc(
1289 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001290 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001291 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001292 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001293 }
1294
1295 /* Now the new frame is always in the range of the reordering buffer */
1296
Karl Beldan2e3049b2013-10-24 15:53:32 +02001297 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001298
1299 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001300 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001301 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001302 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001303 }
1304
1305 /*
1306 * If the current MPDU is in the right order and nothing else
1307 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001308 * If it is first but there's something stored, we may be able
1309 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001310 */
1311 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1312 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001313 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1314 tid_agg_rx->head_seq_num =
1315 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001316 ret = false;
1317 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001318 }
1319
1320 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001321 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1322 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1323 tid_agg_rx->reorder_time[index] = jiffies;
1324 tid_agg_rx->stored_mpdu_num++;
1325 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1326 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001327
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001328 out:
1329 spin_unlock(&tid_agg_rx->reorder_lock);
1330 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001331}
1332
1333/*
1334 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1335 * true if the MPDU was buffered, false if it should be processed.
1336 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001337static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1338 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001339{
Johannes Berg2569a822009-11-25 17:46:17 +01001340 struct sk_buff *skb = rx->skb;
1341 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001342 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001343 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001344 struct tid_ampdu_rx *tid_agg_rx;
1345 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001346 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001347
Johannes Berg051a41fa2013-11-20 11:28:27 +01001348 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1349 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001350 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001351
1352 /*
1353 * filter the QoS data rx stream according to
1354 * STA/TID and check if this STA/TID is on aggregation
1355 */
1356
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001357 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001358 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001359
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001360 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1361 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001362 tid = ieee80211_get_tid(hdr);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001363
Johannes Berga87f7362010-06-10 10:21:38 +02001364 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001365 if (!tid_agg_rx) {
1366 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1367 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1368 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1369 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1370 WLAN_BACK_RECIPIENT,
1371 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001372 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001373 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001374
1375 /* qos null data frames are excluded */
1376 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001377 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001378
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001379 /* not part of a BA session */
1380 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1381 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1382 goto dont_reorder;
1383
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001384 /* new, potentially un-ordered, ampdu frame - process it */
1385
1386 /* reset session timer */
1387 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001388 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001389
1390 /* if this mpdu is fragmented - terminate rx aggregation session */
1391 sc = le16_to_cpu(hdr->seq_ctrl);
1392 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg344eec62010-06-10 10:21:36 +02001393 skb_queue_tail(&rx->sdata->skb_queue, skb);
1394 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001395 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001396 }
1397
Johannes Berga87f7362010-06-10 10:21:38 +02001398 /*
1399 * No locking needed -- we will only ever process one
1400 * RX packet at a time, and thus own tid_agg_rx. All
1401 * other code manipulating it needs to (and does) make
1402 * sure that we cannot get to it any more before doing
1403 * anything with it.
1404 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001405 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1406 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001407 return;
1408
1409 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001410 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001411}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001412
Johannes Berg49461622008-06-30 15:10:45 +02001413static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001414ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001415{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001416 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001417 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001418
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001419 if (status->flag & RX_FLAG_DUP_VALIDATED)
1420 return RX_CONTINUE;
1421
Johannes Berg6b0f3272013-07-11 22:33:26 +02001422 /*
1423 * Drop duplicate 802.11 retransmissions
1424 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1425 */
Johannes Berg03954422014-11-11 16:49:25 +01001426
1427 if (rx->skb->len < 24)
1428 return RX_CONTINUE;
1429
1430 if (ieee80211_is_ctl(hdr->frame_control) ||
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001431 ieee80211_is_any_nullfunc(hdr->frame_control) ||
Johannes Berg03954422014-11-11 16:49:25 +01001432 is_multicast_ether_addr(hdr->addr1))
1433 return RX_CONTINUE;
1434
Johannes Berga732fa72015-10-14 18:27:07 +02001435 if (!rx->sta)
1436 return RX_CONTINUE;
1437
1438 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1439 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1440 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001441 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001442 return RX_DROP_UNUSABLE;
1443 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1444 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001445 }
1446
Johannes Berg03954422014-11-11 16:49:25 +01001447 return RX_CONTINUE;
1448}
1449
1450static ieee80211_rx_result debug_noinline
1451ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1452{
1453 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1454
Johannes Berg571ecf62007-07-27 15:43:22 +02001455 /* Drop disallowed frame classes based on STA auth/assoc state;
1456 * IEEE 802.11, Chap 5.5.
1457 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001458 * mac80211 filters only based on association state, i.e. it drops
1459 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001460 * deauth/disassoc frames when needed. In addition, hostapd is
1461 * responsible for filtering on both auth and assoc states.
1462 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001463
Johannes Berg902acc72008-02-23 15:17:19 +01001464 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001465 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001466
Harvey Harrisona7767f92008-07-02 16:30:51 -07001467 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1468 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001469 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001470 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001471 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001472 /*
1473 * accept port control frames from the AP even when it's not
1474 * yet marked ASSOC to prevent a race where we don't set the
1475 * assoc bit quickly enough before it sends the first frame
1476 */
1477 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001478 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001479 unsigned int hdrlen;
1480 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001481
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001482 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1483
1484 if (rx->skb->len < hdrlen + 8)
1485 return RX_DROP_MONITOR;
1486
1487 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1488 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001489 return RX_CONTINUE;
1490 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001491
1492 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1493 cfg80211_rx_spurious_frame(rx->sdata->dev,
1494 hdr->addr2,
1495 GFP_ATOMIC))
1496 return RX_DROP_UNUSABLE;
1497
Johannes Berge4c26ad2008-01-31 19:48:21 +01001498 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001499 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001500
Johannes Berg9ae54c82008-01-31 19:48:20 +01001501 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001502}
1503
1504
Johannes Berg49461622008-06-30 15:10:45 +02001505static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001506ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1507{
1508 struct ieee80211_local *local;
1509 struct ieee80211_hdr *hdr;
1510 struct sk_buff *skb;
1511
1512 local = rx->local;
1513 skb = rx->skb;
1514 hdr = (struct ieee80211_hdr *) skb->data;
1515
1516 if (!local->pspolling)
1517 return RX_CONTINUE;
1518
1519 if (!ieee80211_has_fromds(hdr->frame_control))
1520 /* this is not from AP */
1521 return RX_CONTINUE;
1522
1523 if (!ieee80211_is_data(hdr->frame_control))
1524 return RX_CONTINUE;
1525
1526 if (!ieee80211_has_moredata(hdr->frame_control)) {
1527 /* AP has no more frames buffered for us */
1528 local->pspolling = false;
1529 return RX_CONTINUE;
1530 }
1531
1532 /* more data bit is set, let's request a new frame from the AP */
1533 ieee80211_send_pspoll(local, rx->sdata);
1534
1535 return RX_CONTINUE;
1536}
1537
Marco Porschd012a602012-10-10 12:39:50 -07001538static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001539{
Johannes Berg133b8222008-09-16 14:18:59 +02001540 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001541 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001542 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001543 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001544
Marco Porschd012a602012-10-10 12:39:50 -07001545 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1546 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1547 ps = &sdata->bss->ps;
1548 else
1549 return;
1550
1551 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001552 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001553 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001554 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001555 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1556 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001557
Johannes Berg17c18bf2015-03-21 15:25:43 +01001558 ieee80211_clear_fast_xmit(sta);
1559
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001560 if (!sta->sta.txq[0])
1561 return;
1562
Johannes Bergadf8ed02018-08-31 11:31:08 +03001563 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001564 struct ieee80211_txq *txq = sta->sta.txq[tid];
1565 struct txq_info *txqi = to_txq_info(txq);
1566
1567 spin_lock(&local->active_txq_lock[txq->ac]);
1568 if (!list_empty(&txqi->schedule_order))
1569 list_del_init(&txqi->schedule_order);
1570 spin_unlock(&local->active_txq_lock[txq->ac]);
1571
1572 if (txq_has_queue(txq))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001573 set_bit(tid, &sta->txq_buffered_tids);
1574 else
1575 clear_bit(tid, &sta->txq_buffered_tids);
1576 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001577}
1578
Marco Porschd012a602012-10-10 12:39:50 -07001579static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001580{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001581 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1582 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001583
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001584 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001585 /*
1586 * Clear the flag only if the other one is still set
1587 * so that the TX path won't start TX'ing new frames
1588 * directly ... In the case that the driver flag isn't
1589 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1590 */
1591 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001592 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1593 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001594 return;
1595 }
1596
Johannes Berg5ac2e352014-05-27 16:32:27 +02001597 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1598 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001599 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001600}
1601
Johannes Bergcf471612015-06-16 16:16:38 +02001602int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001603{
Johannes Bergcf471612015-06-16 16:16:38 +02001604 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001605 bool in_ps;
1606
Johannes Bergcf471612015-06-16 16:16:38 +02001607 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001608
1609 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001610 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001611 if ((start && in_ps) || (!start && !in_ps))
1612 return -EINVAL;
1613
1614 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001615 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001616 else
Johannes Bergcf471612015-06-16 16:16:38 +02001617 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001618
1619 return 0;
1620}
1621EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1622
Johannes Berg46fa38e2016-05-03 16:58:00 +03001623void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1624{
1625 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1626
1627 if (test_sta_flag(sta, WLAN_STA_SP))
1628 return;
1629
1630 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1631 ieee80211_sta_ps_deliver_poll_response(sta);
1632 else
1633 set_sta_flag(sta, WLAN_STA_PSPOLL);
1634}
1635EXPORT_SYMBOL(ieee80211_sta_pspoll);
1636
1637void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1638{
1639 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001640 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001641
1642 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001643 * If this AC is not trigger-enabled do nothing unless the
1644 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001645 *
1646 * NB: This could/should check a separate bitmap of trigger-
1647 * enabled queues, but for now we only implement uAPSD w/o
1648 * TSPEC changes to the ACs, so they're always the same.
1649 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001650 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1651 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001652 return;
1653
1654 /* if we are in a service period, do nothing */
1655 if (test_sta_flag(sta, WLAN_STA_SP))
1656 return;
1657
1658 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1659 ieee80211_sta_ps_deliver_uapsd(sta);
1660 else
1661 set_sta_flag(sta, WLAN_STA_UAPSD);
1662}
1663EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1664
Johannes Berg49461622008-06-30 15:10:45 +02001665static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001666ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1667{
1668 struct ieee80211_sub_if_data *sdata = rx->sdata;
1669 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1670 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001671
Johannes Berg5c900672015-04-22 14:48:34 +02001672 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001673 return RX_CONTINUE;
1674
1675 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1676 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1677 return RX_CONTINUE;
1678
1679 /*
1680 * The device handles station powersave, so don't do anything about
1681 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1682 * it to mac80211 since they're handled.)
1683 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001684 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001685 return RX_CONTINUE;
1686
1687 /*
1688 * Don't do anything if the station isn't already asleep. In
1689 * the uAPSD case, the station will probably be marked asleep,
1690 * in the PS-Poll case the station must be confused ...
1691 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001692 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001693 return RX_CONTINUE;
1694
1695 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001696 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001697
1698 /* Free PS Poll skb here instead of returning RX_DROP that would
1699 * count as an dropped frame. */
1700 dev_kfree_skb(rx->skb);
1701
1702 return RX_QUEUED;
1703 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1704 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1705 ieee80211_has_pm(hdr->frame_control) &&
1706 (ieee80211_is_data_qos(hdr->frame_control) ||
1707 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001708 u8 tid = ieee80211_get_tid(hdr);
Johannes Berg47086fc2011-09-29 16:04:33 +02001709
Johannes Berg46fa38e2016-05-03 16:58:00 +03001710 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001711 }
1712
1713 return RX_CONTINUE;
1714}
1715
1716static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001717ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001718{
1719 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001720 struct sk_buff *skb = rx->skb;
1721 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1722 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001723 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001724
1725 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001726 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001727
Johannes Bergb291ba12009-07-10 15:29:03 +02001728 /*
1729 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001730 * BSSID and for station already AUTHORIZED to avoid keeping the
1731 * current IBSS network alive in cases where other STAs start
1732 * using different BSSID. This will also give the station another
1733 * chance to restart the authentication/authorization in case
1734 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001735 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001736 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001737 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001738 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001739 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1740 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001741 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001742 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001743 !is_multicast_ether_addr(hdr->addr1))
1744 sta->rx_stats.last_rate =
1745 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001746 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001747 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001748 sta->rx_stats.last_rx = jiffies;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001749 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
Wen Gongf879ac82020-12-09 11:06:29 +08001750 !is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb291ba12009-07-10 15:29:03 +02001751 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001752 * Mesh beacons will update last_rx when if they are found to
1753 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001754 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001755 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001756 if (ieee80211_is_data(hdr->frame_control))
1757 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001758 }
1759
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001760 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001761
1762 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001763 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001764 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1765
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001766 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001767 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001768 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001769 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001770
Felix Fietkauef0621e2013-04-22 16:29:31 +02001771 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001772 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001773 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1774 int signal = status->chain_signal[i];
1775
1776 if (!(status->chains & BIT(i)))
1777 continue;
1778
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001779 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001780 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001781 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001782 }
1783 }
1784
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001785 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1786 return RX_CONTINUE;
1787
Johannes Berg72eaa432008-11-26 15:02:58 +01001788 /*
1789 * Change STA power saving mode only at the end of a frame
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001790 * exchange sequence, and only for a data or management
1791 * frame as specified in IEEE 802.11-2016 11.2.3.2
Johannes Berg72eaa432008-11-26 15:02:58 +01001792 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001793 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001794 !ieee80211_has_morefrags(hdr->frame_control) &&
Emmanuel Grumbach209327502018-08-20 13:56:07 +03001795 !is_multicast_ether_addr(hdr->addr1) &&
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001796 (ieee80211_is_mgmt(hdr->frame_control) ||
1797 ieee80211_is_data(hdr->frame_control)) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001798 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001799 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001800 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001801 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001802 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001803 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001804 } else {
1805 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001806 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001807 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001808 }
1809
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001810 /* mesh power save support */
1811 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1812 ieee80211_mps_rx_h_sta_process(sta, hdr);
1813
Johannes Berg22403de2009-10-30 12:55:03 +01001814 /*
1815 * Drop (qos-)data::nullfunc frames silently, since they
1816 * are used only to control station power saving mode.
1817 */
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001818 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001819 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001820
1821 /*
1822 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001823 * that was not moved to a 4-addr STA vlan yet send
1824 * the event to userspace and for older hostapd drop
1825 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001826 */
1827 if (ieee80211_has_a4(hdr->frame_control) &&
1828 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1829 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001830 !rx->sdata->u.vlan.sta))) {
1831 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1832 cfg80211_rx_unexpected_4addr_frame(
1833 rx->sdata->dev, sta->sta.addr,
1834 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001835 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001836 }
Johannes Berg22403de2009-10-30 12:55:03 +01001837 /*
1838 * Update counter and free packet here to avoid
1839 * counting this as a dropped packed.
1840 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001841 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001842 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001843 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001844 }
1845
Johannes Berg9ae54c82008-01-31 19:48:20 +01001846 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001847} /* ieee80211_rx_h_sta_process */
1848
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001849static struct ieee80211_key *
1850ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1851{
1852 struct ieee80211_key *key = NULL;
1853 struct ieee80211_sub_if_data *sdata = rx->sdata;
1854 int idx2;
1855
1856 /* Make sure key gets set if either BIGTK key index is set so that
1857 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1858 * Beacon frames and Beacon frames that claim to use another BIGTK key
1859 * index (i.e., a key that we do not have).
1860 */
1861
1862 if (idx < 0) {
1863 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1864 idx2 = idx + 1;
1865 } else {
1866 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1867 idx2 = idx + 1;
1868 else
1869 idx2 = idx - 1;
1870 }
1871
1872 if (rx->sta)
1873 key = rcu_dereference(rx->sta->gtk[idx]);
1874 if (!key)
1875 key = rcu_dereference(sdata->keys[idx]);
1876 if (!key && rx->sta)
1877 key = rcu_dereference(rx->sta->gtk[idx2]);
1878 if (!key)
1879 key = rcu_dereference(sdata->keys[idx2]);
1880
1881 return key;
1882}
1883
Johan Almbladh86c228a2013-08-14 15:29:46 +02001884static ieee80211_rx_result debug_noinline
1885ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1886{
1887 struct sk_buff *skb = rx->skb;
1888 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1889 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1890 int keyidx;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001891 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1892 struct ieee80211_key *sta_ptk = NULL;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001893 struct ieee80211_key *ptk_idx = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001894 int mmie_keyidx = -1;
1895 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001896 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001897
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001898 if (ieee80211_is_ext(hdr->frame_control))
1899 return RX_CONTINUE;
1900
Johan Almbladh86c228a2013-08-14 15:29:46 +02001901 /*
1902 * Key selection 101
1903 *
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001904 * There are five types of keys:
Johan Almbladh86c228a2013-08-14 15:29:46 +02001905 * - GTK (group keys)
1906 * - IGTK (group keys for management frames)
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001907 * - BIGTK (group keys for Beacon frames)
Johan Almbladh86c228a2013-08-14 15:29:46 +02001908 * - PTK (pairwise keys)
1909 * - STK (station-to-station pairwise keys)
1910 *
1911 * When selecting a key, we have to distinguish between multicast
1912 * (including broadcast) and unicast frames, the latter can only
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001913 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1914 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1915 * then unicast frames can also use key indices like GTKs. Hence, if we
Johan Almbladh86c228a2013-08-14 15:29:46 +02001916 * don't have a PTK/STK we check the key index for a WEP key.
1917 *
1918 * Note that in a regular BSS, multicast frames are sent by the
1919 * AP only, associated stations unicast the frame to the AP first
1920 * which then multicasts it on their behalf.
1921 *
1922 * There is also a slight problem in IBSS mode: GTKs are negotiated
1923 * with each station, that is something we don't currently handle.
1924 * The spec seems to expect that one negotiates the same key with
1925 * every station but there's no such requirement; VLANs could be
1926 * possible.
1927 */
1928
Johan Almbladh86c228a2013-08-14 15:29:46 +02001929 /* start without a key */
1930 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001931 fc = hdr->frame_control;
1932
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001933 if (rx->sta) {
1934 int keyid = rx->sta->ptk_idx;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001935 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001936
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001937 if (ieee80211_has_protected(fc)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001938 cs = rx->sta->cipher_scheme;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001939 keyid = ieee80211_get_keyid(rx->skb, cs);
1940
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001941 if (unlikely(keyid < 0))
1942 return RX_DROP_UNUSABLE;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001943
1944 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001945 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001946 }
1947
Johan Almbladh86c228a2013-08-14 15:29:46 +02001948 if (!ieee80211_has_protected(fc))
1949 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1950
1951 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001952 rx->key = ptk_idx ? ptk_idx : sta_ptk;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001953 if ((status->flag & RX_FLAG_DECRYPTED) &&
1954 (status->flag & RX_FLAG_IV_STRIPPED))
1955 return RX_CONTINUE;
1956 /* Skip decryption if the frame is not protected. */
1957 if (!ieee80211_has_protected(fc))
1958 return RX_CONTINUE;
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001959 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1960 /* Broadcast/multicast robust management frame / BIP */
1961 if ((status->flag & RX_FLAG_DECRYPTED) &&
1962 (status->flag & RX_FLAG_IV_STRIPPED))
1963 return RX_CONTINUE;
1964
1965 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1966 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001967 NUM_DEFAULT_BEACON_KEYS) {
1968 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1969 skb->data,
1970 skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001971 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001972 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001973
1974 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1975 if (!rx->key)
1976 return RX_CONTINUE; /* Beacon protection not in use */
Johan Almbladh86c228a2013-08-14 15:29:46 +02001977 } else if (mmie_keyidx >= 0) {
1978 /* Broadcast/multicast robust management frame / BIP */
1979 if ((status->flag & RX_FLAG_DECRYPTED) &&
1980 (status->flag & RX_FLAG_IV_STRIPPED))
1981 return RX_CONTINUE;
1982
1983 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1984 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1985 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09001986 if (rx->sta) {
1987 if (ieee80211_is_group_privacy_action(skb) &&
1988 test_sta_flag(rx->sta, WLAN_STA_MFP))
1989 return RX_DROP_MONITOR;
1990
Johan Almbladh86c228a2013-08-14 15:29:46 +02001991 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09001992 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001993 if (!rx->key)
1994 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1995 } else if (!ieee80211_has_protected(fc)) {
1996 /*
1997 * The frame was not protected, so skip decryption. However, we
1998 * need to set rx->key if there is a key that could have been
1999 * used so that the frame may be dropped if encryption would
2000 * have been expected.
2001 */
2002 struct ieee80211_key *key = NULL;
2003 struct ieee80211_sub_if_data *sdata = rx->sdata;
2004 int i;
2005
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002006 if (ieee80211_is_beacon(fc)) {
2007 key = ieee80211_rx_get_bigtk(rx, -1);
2008 } else if (ieee80211_is_mgmt(fc) &&
2009 is_multicast_ether_addr(hdr->addr1)) {
2010 key = rcu_dereference(rx->sdata->default_mgmt_key);
2011 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002012 if (rx->sta) {
2013 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2014 key = rcu_dereference(rx->sta->gtk[i]);
2015 if (key)
2016 break;
2017 }
2018 }
2019 if (!key) {
2020 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2021 key = rcu_dereference(sdata->keys[i]);
2022 if (key)
2023 break;
2024 }
2025 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002026 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002027 if (key)
2028 rx->key = key;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002029 return RX_CONTINUE;
2030 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002031 /*
2032 * The device doesn't give us the IV so we won't be
2033 * able to look up the key. That's ok though, we
2034 * don't need to decrypt the frame, we just won't
2035 * be able to keep statistics accurate.
2036 * Except for key threshold notifications, should
2037 * we somehow allow the driver to tell us which key
2038 * the hardware used if this flag is set?
2039 */
2040 if ((status->flag & RX_FLAG_DECRYPTED) &&
2041 (status->flag & RX_FLAG_IV_STRIPPED))
2042 return RX_CONTINUE;
2043
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002044 keyidx = ieee80211_get_keyid(rx->skb, cs);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002045
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002046 if (unlikely(keyidx < 0))
2047 return RX_DROP_UNUSABLE;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002048
2049 /* check per-station GTK first, if multicast packet */
2050 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2051 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2052
2053 /* if not found, try default key */
2054 if (!rx->key) {
2055 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2056
2057 /*
2058 * RSNA-protected unicast frames should always be
2059 * sent with pairwise or station-to-station keys,
2060 * but for WEP we allow using a key index as well.
2061 */
2062 if (rx->key &&
2063 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2064 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2065 !is_multicast_ether_addr(hdr->addr1))
2066 rx->key = NULL;
2067 }
2068 }
2069
2070 if (rx->key) {
2071 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2072 return RX_DROP_MONITOR;
2073
Johan Almbladh86c228a2013-08-14 15:29:46 +02002074 /* TODO: add threshold stuff again */
2075 } else {
2076 return RX_DROP_MONITOR;
2077 }
2078
2079 switch (rx->key->conf.cipher) {
2080 case WLAN_CIPHER_SUITE_WEP40:
2081 case WLAN_CIPHER_SUITE_WEP104:
2082 result = ieee80211_crypto_wep_decrypt(rx);
2083 break;
2084 case WLAN_CIPHER_SUITE_TKIP:
2085 result = ieee80211_crypto_tkip_decrypt(rx);
2086 break;
2087 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002088 result = ieee80211_crypto_ccmp_decrypt(
2089 rx, IEEE80211_CCMP_MIC_LEN);
2090 break;
2091 case WLAN_CIPHER_SUITE_CCMP_256:
2092 result = ieee80211_crypto_ccmp_decrypt(
2093 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002094 break;
2095 case WLAN_CIPHER_SUITE_AES_CMAC:
2096 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2097 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02002098 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2099 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2100 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02002101 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2102 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2103 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2104 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02002105 case WLAN_CIPHER_SUITE_GCMP:
2106 case WLAN_CIPHER_SUITE_GCMP_256:
2107 result = ieee80211_crypto_gcmp_decrypt(rx);
2108 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002109 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002110 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002111 }
2112
2113 /* the hdr variable is invalid after the decrypt handlers */
2114
2115 /* either the frame has been decrypted or will be dropped */
2116 status->flag |= RX_FLAG_DECRYPTED;
2117
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002118 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2119 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2120 skb->data, skb->len);
2121
Johan Almbladh86c228a2013-08-14 15:29:46 +02002122 return result;
2123}
2124
Johannes Berg571ecf62007-07-27 15:43:22 +02002125static inline struct ieee80211_fragment_entry *
2126ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
2127 unsigned int frag, unsigned int seq, int rx_queue,
2128 struct sk_buff **skb)
2129{
2130 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02002131
Johannes Berg571ecf62007-07-27 15:43:22 +02002132 entry = &sdata->fragments[sdata->fragment_next++];
2133 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
2134 sdata->fragment_next = 0;
2135
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002136 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02002137 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02002138
2139 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2140 *skb = NULL;
2141 entry->first_frag_time = jiffies;
2142 entry->seq = seq;
2143 entry->rx_queue = rx_queue;
2144 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002145 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02002146 entry->extra_len = 0;
2147
2148 return entry;
2149}
2150
2151static inline struct ieee80211_fragment_entry *
2152ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002153 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02002154 int rx_queue, struct ieee80211_hdr *hdr)
2155{
2156 struct ieee80211_fragment_entry *entry;
2157 int i, idx;
2158
2159 idx = sdata->fragment_next;
2160 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2161 struct ieee80211_hdr *f_hdr;
David S. Miller7957a9d2018-08-06 22:49:13 -07002162 struct sk_buff *f_skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002163
2164 idx--;
2165 if (idx < 0)
2166 idx = IEEE80211_FRAGMENT_MAX - 1;
2167
2168 entry = &sdata->fragments[idx];
2169 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2170 entry->rx_queue != rx_queue ||
2171 entry->last_frag + 1 != frag)
2172 continue;
2173
David S. Miller7957a9d2018-08-06 22:49:13 -07002174 f_skb = __skb_peek(&entry->skb_list);
2175 f_hdr = (struct ieee80211_hdr *) f_skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002176
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002177 /*
2178 * Check ftype and addresses are equal, else check next fragment
2179 */
2180 if (((hdr->frame_control ^ f_hdr->frame_control) &
2181 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002182 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2183 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02002184 continue;
2185
S.Çağlar Onurab466232008-02-14 17:36:47 +02002186 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002187 __skb_queue_purge(&entry->skb_list);
2188 continue;
2189 }
2190 return entry;
2191 }
2192
2193 return NULL;
2194}
2195
Johannes Berg49461622008-06-30 15:10:45 +02002196static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002197ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002198{
2199 struct ieee80211_hdr *hdr;
2200 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002201 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02002202 unsigned int frag, seq;
2203 struct ieee80211_fragment_entry *entry;
2204 struct sk_buff *skb;
2205
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002206 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002207 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002208
Thomas Pedersen09a740c2020-09-21 19:28:14 -07002209 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002210 return RX_CONTINUE;
2211
Johannes Berg571ecf62007-07-27 15:43:22 +02002212 sc = le16_to_cpu(hdr->seq_ctrl);
2213 frag = sc & IEEE80211_SCTL_FRAG;
2214
Johannes Bergb8fff402014-11-03 13:57:46 +01002215 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02002216 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01002217 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02002218 }
Johannes Bergb8fff402014-11-03 13:57:46 +01002219
Andreas Müllerd0259332014-12-12 12:11:11 +01002220 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2221 goto out;
2222
Johannes Berg571ecf62007-07-27 15:43:22 +02002223 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2224
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08002225 if (skb_linearize(rx->skb))
2226 return RX_DROP_UNUSABLE;
2227
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07002228 /*
2229 * skb_linearize() might change the skb->data and
2230 * previously cached variables (in this case, hdr) need to
2231 * be refreshed with the new data.
2232 */
2233 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002234 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2235
2236 if (frag == 0) {
2237 /* This is the first fragment of a new frame. */
2238 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002239 rx->seqno_idx, &(rx->skb));
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002240 if (rx->key &&
2241 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
Johannes Berg9acc54b2016-02-26 22:13:40 +01002242 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2243 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2244 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07002245 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02002246 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002247
2248 /* Store CCMP/GCMP PN so that we can verify that the
2249 * next fragment has a sequential PN value.
2250 */
2251 entry->check_sequential_pn = true;
Johannes Berg571ecf62007-07-27 15:43:22 +02002252 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07002253 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02002254 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01002255 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2256 u.ccmp.rx_pn) !=
2257 offsetof(struct ieee80211_key,
2258 u.gcmp.rx_pn));
2259 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2260 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2261 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2262 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002263 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002264 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002265 }
2266
2267 /* This is a fragment for a frame that should already be pending in
2268 * fragment cache. Add this fragment to the end of the pending entry.
2269 */
Johannes Berg9e262972011-07-07 18:45:03 +02002270 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
2271 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02002272 if (!entry) {
2273 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002274 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002275 }
2276
Johannes Berg9acc54b2016-02-26 22:13:40 +01002277 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2278 * MPDU PN values are not incrementing in steps of 1."
2279 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2280 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2281 */
2282 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002283 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002284 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07002285 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002286
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002287 if (!rx->key ||
2288 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
Johannes Berg9acc54b2016-02-26 22:13:40 +01002289 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2290 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2291 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002292 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002293 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2294 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002295 pn[i]++;
2296 if (pn[i])
2297 break;
2298 }
Johannes Berg9e262972011-07-07 18:45:03 +02002299 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07002300 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02002301 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002302 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002303 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002304 }
2305
Harvey Harrison358c8d92008-07-15 18:44:13 -07002306 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002307 __skb_queue_tail(&entry->skb_list, rx->skb);
2308 entry->last_frag = frag;
2309 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002310 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002311 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002312 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002313 }
2314
2315 rx->skb = __skb_dequeue(&entry->skb_list);
2316 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002317 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002318 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2319 GFP_ATOMIC))) {
2320 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2321 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002322 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002323 }
2324 }
2325 while ((skb = __skb_dequeue(&entry->skb_list))) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02002326 skb_put_data(rx->skb, skb->data, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02002327 dev_kfree_skb(skb);
2328 }
2329
Johannes Berg571ecf62007-07-27 15:43:22 +02002330 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002331 ieee80211_led_rx(rx->local);
2332 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002333 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002334 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002335 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002336}
2337
Johannes Berg1df332e2012-10-26 00:09:11 +02002338static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002339{
Johannes Berg1df332e2012-10-26 00:09:11 +02002340 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002341 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002342
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002343 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002344}
2345
Johannes Berg1df332e2012-10-26 00:09:11 +02002346static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002347{
Markus Theil0b467b632020-06-25 12:42:14 +02002348 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002349 struct sk_buff *skb = rx->skb;
2350 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2351
Johannes Berg3017b802007-08-28 17:01:53 -04002352 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002353 * Pass through unencrypted frames if the hardware has
2354 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002355 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002356 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002357 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002358
Markus Theil0b467b632020-06-25 12:42:14 +02002359 /* check mesh EAPOL frames first */
2360 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2361 ieee80211_is_data(fc))) {
2362 struct ieee80211s_hdr *mesh_hdr;
2363 u16 hdr_len = ieee80211_hdrlen(fc);
2364 u16 ethertype_offset;
2365 __be16 ethertype;
2366
2367 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2368 goto drop_check;
2369
2370 /* make sure fixed part of mesh header is there, also checks skb len */
2371 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2372 goto drop_check;
2373
2374 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2375 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2376 sizeof(rfc1042_header);
2377
2378 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2379 ethertype == rx->sdata->control_port_protocol)
2380 return 0;
2381 }
2382
2383drop_check:
Johannes Berg571ecf62007-07-27 15:43:22 +02002384 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002385 if (unlikely(!ieee80211_has_protected(fc) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002386 !ieee80211_is_any_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002387 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002388 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002389
2390 return 0;
2391}
2392
Johannes Berg1df332e2012-10-26 00:09:11 +02002393static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002394{
2395 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002396 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002397 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002398
Jouni Malinene3efca02010-03-28 22:31:15 -07002399 /*
2400 * Pass through unencrypted frames if the hardware has
2401 * decrypted them already.
2402 */
2403 if (status->flag & RX_FLAG_DECRYPTED)
2404 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002405
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002406 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002407 if (unlikely(!ieee80211_has_protected(fc) &&
2408 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002409 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002410 if (ieee80211_is_deauth(fc) ||
2411 ieee80211_is_disassoc(fc))
2412 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2413 rx->skb->data,
2414 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002415 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002416 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002417 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002418 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002419 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002420 if (ieee80211_is_deauth(fc) ||
2421 ieee80211_is_disassoc(fc))
2422 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2423 rx->skb->data,
2424 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002425 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002426 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002427 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002428 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2429 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2430 rx->skb->data,
2431 rx->skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002432 return -EACCES;
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002433 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002434 /*
2435 * When using MFP, Action frames are not allowed prior to
2436 * having configured keys.
2437 */
2438 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002439 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002440 return -EACCES;
2441 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002442
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002443 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002444}
2445
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002446static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002447__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002448{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002449 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002450 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002451 bool check_port_control = false;
2452 struct ethhdr *ehdr;
2453 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002454
Felix Fietkau4114fa22011-04-12 19:15:22 +02002455 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002456 if (ieee80211_has_a4(hdr->frame_control) &&
2457 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002458 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002459
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002460 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2461 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2462
2463 if (!sdata->u.mgd.use_4addr)
2464 return -1;
Sathishkumar Muruganandam1ecef202018-08-28 17:51:38 +05302465 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002466 check_port_control = true;
2467 }
2468
Johannes Berg9bc383d2009-11-19 11:55:19 +01002469 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002470 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002471 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002472
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002473 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002474 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002475 return ret;
2476
2477 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002478 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2479 *port_control = true;
2480 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002481 return -1;
2482
2483 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002484}
Johannes Berg571ecf62007-07-27 15:43:22 +02002485
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002486/*
2487 * requires that rx->skb is a frame with ethernet header
2488 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002489static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002490{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302491 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002492 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2493 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2494
2495 /*
2496 * Allow EAPOL frames to us/the PAE group address regardless
2497 * of whether the frame was encrypted or not.
2498 */
Johannes Berga621fa42010-08-27 14:26:54 +03002499 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002500 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2501 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002502 return true;
2503
2504 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002505 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002506 return false;
2507
2508 return true;
2509}
2510
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002511static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2512 struct ieee80211_rx_data *rx)
2513{
2514 struct ieee80211_sub_if_data *sdata = rx->sdata;
2515 struct net_device *dev = sdata->dev;
2516
2517 if (unlikely((skb->protocol == sdata->control_port_protocol ||
Markus Theil7f3f96c2020-03-12 10:10:54 +01002518 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2519 !sdata->control_port_no_preauth)) &&
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002520 sdata->control_port_over_nl80211)) {
2521 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Denis Kenziorf8b43c52019-08-27 17:41:20 -05002522 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002523
Denis Kenziora948f712018-07-03 15:05:48 -05002524 cfg80211_rx_control_port(dev, skb, noencrypt);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002525 dev_kfree_skb(skb);
2526 } else {
Denis Kenziorc8a41c62019-08-27 17:41:19 -05002527 memset(skb->cb, 0, sizeof(skb->cb));
2528
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002529 /* deliver to local stack */
Felix Fietkauc5d16862020-07-26 13:06:11 +02002530 if (rx->list)
2531 list_add_tail(&skb->list, rx->list);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002532 else
2533 netif_receive_skb(skb);
2534 }
2535}
2536
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002537/*
2538 * requires that rx->skb is a frame with ethernet header
2539 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002540static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002541ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002542{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002543 struct ieee80211_sub_if_data *sdata = rx->sdata;
2544 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002545 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002546 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2547 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002548
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002549 skb = rx->skb;
2550 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002551
Lev Stipakov36ec1442020-11-13 23:46:24 +02002552 dev_sw_netstats_rx_add(dev, skb->len);
Johannes Berg5a490512015-04-22 17:10:38 +02002553
Johannes Bergde8f18d2016-03-31 20:02:03 +03002554 if (rx->sta) {
2555 /* The seqno index has the same property as needed
2556 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2557 * for non-QoS-data frames. Here we know it's a data
2558 * frame, so count MSDUs.
2559 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002560 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002561 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002562 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002563 }
2564
Johannes Berg05c914f2008-09-11 00:01:58 +02002565 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2566 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002567 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002568 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002569 if (is_multicast_ether_addr(ehdr->h_dest) &&
2570 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002571 /*
2572 * send multicast frames both to higher layers in
2573 * local net stack and back to the wireless medium
2574 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002575 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002576 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002577 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002578 dev->name);
Johannes Berg42dca5e2018-10-09 10:00:21 +02002579 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2580 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2581 dsta = sta_info_get(sdata, ehdr->h_dest);
Johannes Bergabe60632009-11-25 17:46:18 +01002582 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002583 /*
2584 * The destination station is associated to
2585 * this AP (in this VLAN), so send the frame
2586 * directly to it and do not pass it to local
2587 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002588 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002589 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002590 skb = NULL;
2591 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002592 }
2593 }
2594
Kalle Valo59d9cb02009-12-17 13:54:57 +01002595#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002596 if (skb) {
2597 /* 'align' will only take the values 0 or 2 here since all
2598 * frames are required to be aligned to 2-byte boundaries
2599 * when being passed to mac80211; the code here works just
2600 * as well if that isn't true, but mac80211 assumes it can
2601 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002602 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002603 int align;
2604
2605 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002606 if (align) {
2607 if (WARN_ON(skb_headroom(skb) < 3)) {
2608 dev_kfree_skb(skb);
2609 skb = NULL;
2610 } else {
2611 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002612 size_t len = skb_headlen(skb);
2613 skb->data -= align;
2614 memmove(skb->data, data, len);
2615 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002616 }
2617 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002618 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002619#endif
2620
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002621 if (skb) {
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002622 skb->protocol = eth_type_trans(skb, dev);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002623 ieee80211_deliver_skb_to_local_stack(skb, rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002624 }
2625
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002626 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002627 /*
2628 * Send to wireless media and increase priority by 256 to
2629 * keep the received priority instead of reclassifying
2630 * the frame (see cfg80211_classify8021d).
2631 */
2632 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002633 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002634 skb_reset_network_header(xmit_skb);
2635 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002636 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002637 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002638}
2639
Johannes Berg49461622008-06-30 15:10:45 +02002640static ieee80211_rx_result debug_noinline
Felix Fietkau24bba072018-02-27 13:03:07 +01002641__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002642{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002643 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002644 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002645 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2646 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002647 struct sk_buff_head frame_list;
Johannes Berg7f6990c2016-10-05 15:29:49 +02002648 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002649 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002650
Johannes Bergea720932016-10-05 10:14:42 +02002651 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
Johannes Berge2b52272016-10-05 16:42:06 +02002652 check_da = NULL;
2653 check_sa = NULL;
2654 } else switch (rx->sdata->vif.type) {
2655 case NL80211_IFTYPE_AP:
2656 case NL80211_IFTYPE_AP_VLAN:
2657 check_da = NULL;
2658 break;
2659 case NL80211_IFTYPE_STATION:
2660 if (!rx->sta ||
2661 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2662 check_sa = NULL;
2663 break;
2664 case NL80211_IFTYPE_MESH_POINT:
2665 check_sa = NULL;
2666 break;
2667 default:
2668 break;
Johannes Bergea720932016-10-05 10:14:42 +02002669 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002670
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002671 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002672 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002673
Johannes Berg7f6990c2016-10-05 15:29:49 +02002674 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2675 rx->sdata->vif.addr,
Felix Fietkau24bba072018-02-27 13:03:07 +01002676 rx->sdata->vif.type,
2677 data_offset))
Johannes Berg7f6990c2016-10-05 15:29:49 +02002678 return RX_DROP_UNUSABLE;
2679
Zhu Yieaf85ca2009-12-01 10:18:37 +08002680 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2681 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002682 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002683 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002684
Zhu Yieaf85ca2009-12-01 10:18:37 +08002685 while (!skb_queue_empty(&frame_list)) {
2686 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002687
Harvey Harrison358c8d92008-07-15 18:44:13 -07002688 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002689 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002690 continue;
2691 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002692
2693 ieee80211_deliver_skb(rx);
2694 }
2695
Johannes Berg9ae54c82008-01-31 19:48:20 +01002696 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002697}
2698
Felix Fietkau24bba072018-02-27 13:03:07 +01002699static ieee80211_rx_result debug_noinline
2700ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2701{
2702 struct sk_buff *skb = rx->skb;
2703 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2704 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2705 __le16 fc = hdr->frame_control;
2706
2707 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2708 return RX_CONTINUE;
2709
2710 if (unlikely(!ieee80211_is_data(fc)))
2711 return RX_CONTINUE;
2712
2713 if (unlikely(!ieee80211_is_data_present(fc)))
2714 return RX_DROP_MONITOR;
2715
2716 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2717 switch (rx->sdata->vif.type) {
2718 case NL80211_IFTYPE_AP_VLAN:
2719 if (!rx->sdata->u.vlan.sta)
2720 return RX_DROP_UNUSABLE;
2721 break;
2722 case NL80211_IFTYPE_STATION:
2723 if (!rx->sdata->u.mgd.use_4addr)
2724 return RX_DROP_UNUSABLE;
2725 break;
2726 default:
2727 return RX_DROP_UNUSABLE;
2728 }
2729 }
2730
2731 if (is_multicast_ether_addr(hdr->addr1))
2732 return RX_DROP_UNUSABLE;
2733
2734 return __ieee80211_rx_h_amsdu(rx, 0);
2735}
2736
Ingo Molnarbf94e172008-10-12 23:51:38 -07002737#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002738static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002739ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2740{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002741 struct ieee80211_hdr *fwd_hdr, *hdr;
2742 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002743 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002744 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002745 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002746 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002747 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002748 u16 ac, q, hdrlen;
Felix Fietkau51d0af22019-02-22 13:21:15 +01002749 int tailroom = 0;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002750
2751 hdr = (struct ieee80211_hdr *) skb->data;
2752 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002753
2754 /* make sure fixed part of mesh header is there, also checks skb len */
2755 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2756 return RX_DROP_MONITOR;
2757
2758 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2759
2760 /* make sure full mesh header is there, also checks skb len */
2761 if (!pskb_may_pull(rx->skb,
2762 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2763 return RX_DROP_MONITOR;
2764
2765 /* reload pointers */
2766 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002767 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2768
Bob Copelandd0c22112015-03-02 14:28:52 -05002769 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2770 return RX_DROP_MONITOR;
2771
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002772 /* frame is in RMC, don't forward */
2773 if (ieee80211_is_data(hdr->frame_control) &&
2774 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002775 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002776 return RX_DROP_MONITOR;
2777
Johannes Berg5c900672015-04-22 14:48:34 +02002778 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002779 return RX_CONTINUE;
2780
2781 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002782 return RX_DROP_MONITOR;
2783
Javier Cardona43b7b312009-10-15 18:10:51 -07002784 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002785 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002786 char *proxied_addr;
2787 char *mpp_addr;
2788
2789 if (is_multicast_ether_addr(hdr->addr1)) {
2790 mpp_addr = hdr->addr3;
2791 proxied_addr = mesh_hdr->eaddr1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -07002792 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2793 MESH_FLAGS_AE_A5_A6) {
Johannes Berg9b395bc2012-10-26 00:36:40 +02002794 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002795 mpp_addr = hdr->addr4;
2796 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002797 } else {
2798 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002799 }
YanBo79617de2008-09-22 13:30:32 +08002800
YanBo79617de2008-09-22 13:30:32 +08002801 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002802 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002803 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002804 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002805 } else {
2806 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002807 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002808 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002809 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002810 spin_unlock_bh(&mppath->state_lock);
2811 }
2812 rcu_read_unlock();
2813 }
2814
Javier Cardona3c5772a2009-08-10 12:15:48 -07002815 /* Frame has reached destination. Don't forward */
2816 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002817 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002818 return RX_CONTINUE;
2819
Michal Kaziorcf440122016-01-25 14:43:24 +01002820 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2821 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002822 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002823 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002824 return RX_DROP_MONITOR;
2825 }
2826 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002827
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002828 if (!--mesh_hdr->ttl) {
Bob Copelanda0dc0202019-01-17 16:32:42 -05002829 if (!is_multicast_ether_addr(hdr->addr1))
2830 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2831 dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002832 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002833 }
2834
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002835 if (!ifmsh->mshcfg.dot11MeshForwarding)
2836 goto out;
2837
Felix Fietkau51d0af22019-02-22 13:21:15 +01002838 if (sdata->crypto_tx_tailroom_needed_cnt)
2839 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2840
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002841 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
Felix Fietkau51d0af22019-02-22 13:21:15 +01002842 sdata->encrypt_headroom,
2843 tailroom, GFP_ATOMIC);
Joe Perches0c3d5a92018-03-12 08:07:12 -07002844 if (!fwd_skb)
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002845 goto out;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002846
2847 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002848 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002849 info = IEEE80211_SKB_CB(fwd_skb);
2850 memset(info, 0, sizeof(*info));
Felix Fietkaucc20ff22020-09-08 14:36:57 +02002851 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002852 info->control.vif = &rx->sdata->vif;
2853 info->control.jiffies = jiffies;
2854 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2855 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2856 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002857 /* update power mode indication when forwarding */
2858 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002859 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002860 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002861 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2862 } else {
2863 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002864 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002865 fwd_hdr->addr3, 0,
2866 WLAN_REASON_MESH_PATH_NOFORWARD,
2867 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002868 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002869 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002870 return RX_DROP_MONITOR;
2871 }
2872
2873 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2874 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002875 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002876 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002877 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002878 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002879}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002880#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002881
2882static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002883ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002884{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002885 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302886 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002887 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002888 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2889 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002890 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002891 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002892
Harvey Harrison358c8d92008-07-15 18:44:13 -07002893 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002894 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002895
Harvey Harrison358c8d92008-07-15 18:44:13 -07002896 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002897 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002898
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002899 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002900 * Send unexpected-4addr-frame event to hostapd. For older versions,
2901 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002902 */
2903 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002904 sdata->vif.type == NL80211_IFTYPE_AP) {
2905 if (rx->sta &&
2906 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2907 cfg80211_rx_unexpected_4addr_frame(
2908 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002909 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002910 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002911
Felix Fietkau4114fa22011-04-12 19:15:22 +02002912 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002913 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002914 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002915
Harvey Harrison358c8d92008-07-15 18:44:13 -07002916 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002917 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002918
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002919 /* directly handle TDLS channel switch requests/responses */
2920 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2921 cpu_to_be16(ETH_P_TDLS))) {
2922 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2923
2924 if (pskb_may_pull(rx->skb,
2925 offsetof(struct ieee80211_tdls_data, u)) &&
2926 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2927 tf->category == WLAN_CATEGORY_TDLS &&
2928 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2929 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002930 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2931 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002932 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002933 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002934
2935 return RX_QUEUED;
2936 }
2937 }
2938
Felix Fietkau4114fa22011-04-12 19:15:22 +02002939 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2940 unlikely(port_control) && sdata->bss) {
2941 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2942 u.ap);
2943 dev = sdata->dev;
2944 rx->sdata = sdata;
2945 }
2946
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002947 rx->skb->dev = dev;
2948
Johannes Berg602fae42016-03-17 15:02:52 +02002949 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2950 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302951 !is_multicast_ether_addr(
2952 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2953 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002954 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2955 mod_timer(&local->dynamic_ps_timer, jiffies +
2956 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05302957
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002958 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002959
Johannes Berg9ae54c82008-01-31 19:48:20 +01002960 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002961}
2962
Johannes Berg49461622008-06-30 15:10:45 +02002963static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002964ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002965{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002966 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002967 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002968 struct tid_ampdu_rx *tid_agg_rx;
2969 u16 start_seq_num;
2970 u16 tid;
2971
Harvey Harrisona7767f92008-07-02 16:30:51 -07002972 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002973 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002974
Harvey Harrisona7767f92008-07-02 16:30:51 -07002975 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002976 struct {
2977 __le16 control, start_seq_num;
2978 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002979 struct ieee80211_event event = {
2980 .type = BAR_RX_EVENT,
2981 };
Johannes Berg8ae59772010-05-30 14:52:58 +02002982
Ron Rindjunsky71364712007-12-25 17:00:36 +02002983 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002984 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002985
2986 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2987 &bar_data, sizeof(bar_data)))
2988 return RX_DROP_MONITOR;
2989
Johannes Berg8ae59772010-05-30 14:52:58 +02002990 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02002991
Johannes Berg53f24972016-08-29 23:25:19 +03002992 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2993 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2994 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2995 WLAN_BACK_RECIPIENT,
2996 WLAN_REASON_QSTA_REQUIRE_SETUP);
2997
Johannes Berga87f7362010-06-10 10:21:38 +02002998 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2999 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01003000 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003001
Johannes Berg8ae59772010-05-30 14:52:58 +02003002 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003003 event.u.ba.tid = tid;
3004 event.u.ba.ssn = start_seq_num;
3005 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003006
3007 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01003008 if (tid_agg_rx->timeout)
3009 mod_timer(&tid_agg_rx->session_timer,
3010 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02003011
Johannes Bergdd318572010-11-29 11:09:16 +01003012 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01003013 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02003014 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003015 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01003016 spin_unlock(&tid_agg_rx->reorder_lock);
3017
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003018 drv_event_callback(rx->local, rx->sdata, &event);
3019
Johannes Berga02ae752009-11-16 12:00:40 +01003020 kfree_skb(skb);
3021 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003022 }
3023
Johannes Berg08daeca2010-05-30 14:53:43 +02003024 /*
3025 * After this point, we only want management frames,
3026 * so we can drop all remaining control frames to
3027 * cooked monitor interfaces.
3028 */
3029 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003030}
3031
Jouni Malinenf4f727a2009-01-10 11:46:53 +02003032static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3033 struct ieee80211_mgmt *mgmt,
3034 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02003035{
3036 struct ieee80211_local *local = sdata->local;
3037 struct sk_buff *skb;
3038 struct ieee80211_mgmt *resp;
3039
Joe Perchesb203ca32012-05-08 18:56:52 +00003040 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02003041 /* Not to own unicast address */
3042 return;
3043 }
3044
Joe Perchesb203ca32012-05-08 18:56:52 +00003045 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3046 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003047 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02003048 return;
3049 }
3050
3051 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3052 /* Too short SA Query request frame */
3053 return;
3054 }
3055
3056 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3057 if (skb == NULL)
3058 return;
3059
3060 skb_reserve(skb, local->hw.extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02003061 resp = skb_put_zero(skb, 24);
Jouni Malinenfea14732009-01-08 13:32:06 +02003062 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01003063 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01003064 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02003065 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3066 IEEE80211_STYPE_ACTION);
3067 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3068 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3069 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3070 memcpy(resp->u.action.u.sa_query.trans_id,
3071 mgmt->u.action.u.sa_query.trans_id,
3072 WLAN_SA_QUERY_TR_ID_LEN);
3073
Johannes Berg62ae67b2009-11-18 18:42:05 +01003074 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02003075}
3076
Johannes Berg49461622008-06-30 15:10:45 +02003077static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003078ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3079{
3080 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003081 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003082
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003083 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3084 return RX_CONTINUE;
3085
Johannes Berg2e161f782010-08-12 15:38:38 +02003086 /*
3087 * From here on, look only at management frames.
3088 * Data and control frames are already handled,
3089 * and unknown (reserved) frames are useless.
3090 */
3091 if (rx->skb->len < 24)
3092 return RX_DROP_MONITOR;
3093
3094 if (!ieee80211_is_mgmt(mgmt->frame_control))
3095 return RX_DROP_MONITOR;
3096
Johannes Bergee971922011-11-04 11:18:18 +01003097 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3098 ieee80211_is_beacon(mgmt->frame_control) &&
3099 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01003100 int sig = 0;
3101
Tosoni1ad22fb2018-03-14 16:58:34 +00003102 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3103 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003104 sig = status->signal;
3105
Thomas Pedersene76fede2020-04-30 10:25:50 -07003106 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3107 rx->skb->data, rx->skb->len,
3108 ieee80211_rx_status_to_khz(status),
3109 sig);
Johannes Bergee971922011-11-04 11:18:18 +01003110 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3111 }
3112
Johannes Berg2e161f782010-08-12 15:38:38 +02003113 if (ieee80211_drop_unencrypted_mgmt(rx))
3114 return RX_DROP_UNUSABLE;
3115
3116 return RX_CONTINUE;
3117}
3118
3119static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02003120ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3121{
3122 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003123 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02003124 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003125 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02003126 int len = rx->skb->len;
3127
3128 if (!ieee80211_is_action(mgmt->frame_control))
3129 return RX_CONTINUE;
3130
Jouni Malinen026331c2010-02-15 12:53:10 +02003131 /* drop too small frames */
3132 if (len < IEEE80211_MIN_ACTION_SIZE)
3133 return RX_DROP_UNUSABLE;
3134
Marco Porsch815b8092012-12-05 15:04:26 -08003135 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003136 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3137 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02003138 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02003139
Johannes Bergde1ede72008-09-09 14:42:50 +02003140 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003141 case WLAN_CATEGORY_HT:
3142 /* reject HT action frames from stations not supporting HT */
3143 if (!rx->sta->sta.ht_cap.ht_supported)
3144 goto invalid;
3145
3146 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3147 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3148 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3149 sdata->vif.type != NL80211_IFTYPE_AP &&
3150 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3151 break;
3152
Johannes Bergec61cd62012-12-28 12:12:10 +01003153 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003154 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3155 goto invalid;
3156
3157 switch (mgmt->u.action.u.ht_smps.action) {
3158 case WLAN_HT_ACTION_SMPS: {
3159 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003160 enum ieee80211_smps_mode smps_mode;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303161 struct sta_opmode_info sta_opmode = {};
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003162
Ilan Peerc4d800d2020-01-31 13:12:53 +02003163 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3164 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3165 goto handled;
3166
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003167 /* convert to HT capability */
3168 switch (mgmt->u.action.u.ht_smps.smps_control) {
3169 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003170 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003171 break;
3172 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003173 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003174 break;
3175 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003176 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003177 break;
3178 default:
3179 goto invalid;
3180 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003181
3182 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01003183 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003184 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003185 rx->sta->sta.smps_mode = smps_mode;
tamizhr@codeaurora.org57566b22018-03-27 19:16:16 +05303186 sta_opmode.smps_mode =
3187 ieee80211_smps_mode_to_smps_mode(smps_mode);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303188 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003189
3190 sband = rx->local->hw.wiphy->bands[status->band];
3191
Johannes Berg64f68e52012-03-28 10:58:37 +02003192 rate_control_rate_update(local, sband, rx->sta,
3193 IEEE80211_RC_SMPS_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303194 cfg80211_sta_opmode_change_notify(sdata->dev,
3195 rx->sta->addr,
3196 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003197 GFP_ATOMIC);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003198 goto handled;
3199 }
Johannes Bergec61cd62012-12-28 12:12:10 +01003200 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3201 struct ieee80211_supported_band *sband;
3202 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003203 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303204 struct sta_opmode_info sta_opmode = {};
Johannes Bergec61cd62012-12-28 12:12:10 +01003205
3206 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003207 if (!(rx->sta->sta.ht_cap.cap &
3208 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01003209 goto handled;
3210
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003211 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003212 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01003213 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003214 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3215
3216 /* set cur_max_bandwidth and recalc sta bw */
3217 rx->sta->cur_max_bandwidth = max_bw;
3218 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003219
3220 if (rx->sta->sta.bandwidth == new_bw)
3221 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01003222
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003223 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01003224 sband = rx->local->hw.wiphy->bands[status->band];
tamizhr@codeaurora.org97f5f422018-03-27 19:16:17 +05303225 sta_opmode.bw =
3226 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303227 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
Johannes Bergec61cd62012-12-28 12:12:10 +01003228
3229 rate_control_rate_update(local, sband, rx->sta,
3230 IEEE80211_RC_BW_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303231 cfg80211_sta_opmode_change_notify(sdata->dev,
3232 rx->sta->addr,
3233 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003234 GFP_ATOMIC);
Johannes Bergec61cd62012-12-28 12:12:10 +01003235 goto handled;
3236 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003237 default:
3238 goto invalid;
3239 }
3240
3241 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003242 case WLAN_CATEGORY_PUBLIC:
3243 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3244 goto invalid;
3245 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3246 break;
3247 if (!rx->sta)
3248 break;
3249 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3250 break;
3251 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3252 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3253 break;
3254 if (len < offsetof(struct ieee80211_mgmt,
3255 u.action.u.ext_chan_switch.variable))
3256 goto invalid;
3257 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003258 case WLAN_CATEGORY_VHT:
3259 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3260 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3261 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3262 sdata->vif.type != NL80211_IFTYPE_AP &&
3263 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3264 break;
3265
3266 /* verify action code is present */
3267 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3268 goto invalid;
3269
3270 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3271 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01003272 /* verify opmode is present */
3273 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3274 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02003275 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003276 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02003277 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3278 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3279 goto invalid;
3280 goto queue;
3281 }
Johannes Berg0af83d32012-12-27 18:55:36 +01003282 default:
3283 break;
3284 }
3285 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003286 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01003287 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07003288 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01003289 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01003290 sdata->vif.type != NL80211_IFTYPE_AP &&
3291 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02003292 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01003293
Jouni Malinen026331c2010-02-15 12:53:10 +02003294 /* verify action_code is present */
3295 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3296 break;
3297
Johannes Bergde1ede72008-09-09 14:42:50 +02003298 switch (mgmt->u.action.u.addba_req.action_code) {
3299 case WLAN_ACTION_ADDBA_REQ:
3300 if (len < (IEEE80211_MIN_ACTION_SIZE +
3301 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003302 goto invalid;
3303 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003304 case WLAN_ACTION_ADDBA_RESP:
3305 if (len < (IEEE80211_MIN_ACTION_SIZE +
3306 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003307 goto invalid;
3308 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003309 case WLAN_ACTION_DELBA:
3310 if (len < (IEEE80211_MIN_ACTION_SIZE +
3311 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003312 goto invalid;
3313 break;
3314 default:
3315 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02003316 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003317
Johannes Berg8b58ff82010-06-10 10:21:51 +02003318 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02003319 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02003320 /* verify action_code is present */
3321 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3322 break;
3323
Johannes Berg39192c02008-09-09 14:49:03 +02003324 switch (mgmt->u.action.u.measurement.action_code) {
3325 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02003326 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003327 break;
3328
Johannes Berg39192c02008-09-09 14:49:03 +02003329 if (len < (IEEE80211_MIN_ACTION_SIZE +
3330 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02003331 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003332
Johannes Bergcc32abd2009-05-15 11:52:31 +02003333 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003334 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02003335
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003336 ieee80211_process_measurement_req(sdata, mgmt, len);
3337 goto handled;
3338 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3339 u8 *bssid;
3340 if (len < (IEEE80211_MIN_ACTION_SIZE +
3341 sizeof(mgmt->u.action.u.chan_switch)))
3342 break;
3343
3344 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003345 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3346 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003347 break;
3348
3349 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3350 bssid = sdata->u.mgd.bssid;
3351 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3352 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003353 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3354 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003355 else
3356 break;
3357
3358 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02003359 break;
Sujithc481ec92009-01-06 09:28:37 +05303360
Johannes Berg8b58ff82010-06-10 10:21:51 +02003361 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003362 }
Johannes Berg39192c02008-09-09 14:49:03 +02003363 }
3364 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07003365 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003366 if (len < (IEEE80211_MIN_ACTION_SIZE +
3367 sizeof(mgmt->u.action.u.self_prot.action_code)))
3368 break;
3369
Thomas Pedersen8db09852011-08-12 20:01:00 -07003370 switch (mgmt->u.action.u.self_prot.action_code) {
3371 case WLAN_SP_MESH_PEERING_OPEN:
3372 case WLAN_SP_MESH_PEERING_CLOSE:
3373 case WLAN_SP_MESH_PEERING_CONFIRM:
3374 if (!ieee80211_vif_is_mesh(&sdata->vif))
3375 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003376 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003377 /* userspace handles this frame */
3378 break;
3379 goto queue;
3380 case WLAN_SP_MGK_INFORM:
3381 case WLAN_SP_MGK_ACK:
3382 if (!ieee80211_vif_is_mesh(&sdata->vif))
3383 goto invalid;
3384 break;
3385 }
3386 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003387 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003388 if (len < (IEEE80211_MIN_ACTION_SIZE +
3389 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3390 break;
3391
Johannes Berg77a121c2010-06-10 10:21:34 +02003392 if (!ieee80211_vif_is_mesh(&sdata->vif))
3393 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003394 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003395 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003396 break;
3397 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02003398 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003399
Johannes Berg2e161f782010-08-12 15:38:38 +02003400 return RX_CONTINUE;
3401
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003402 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003403 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f782010-08-12 15:38:38 +02003404 /* will return in the next handlers */
3405 return RX_CONTINUE;
3406
3407 handled:
3408 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003409 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003410 dev_kfree_skb(rx->skb);
3411 return RX_QUEUED;
3412
3413 queue:
Johannes Berg2e161f782010-08-12 15:38:38 +02003414 skb_queue_tail(&sdata->skb_queue, rx->skb);
3415 ieee80211_queue_work(&local->hw, &sdata->work);
3416 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003417 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003418 return RX_QUEUED;
3419}
3420
3421static ieee80211_rx_result debug_noinline
3422ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3423{
Johannes Berg554891e2010-09-24 12:38:25 +02003424 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003425 int sig = 0;
Johannes Berg2e161f782010-08-12 15:38:38 +02003426
3427 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003428 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f782010-08-12 15:38:38 +02003429 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003430
Jouni Malinen026331c2010-02-15 12:53:10 +02003431 /*
3432 * Getting here means the kernel doesn't know how to handle
3433 * it, but maybe userspace does ... include returned frames
3434 * so userspace can register for those to know whether ones
3435 * it transmitted were processed or returned.
3436 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003437
Tosoni1ad22fb2018-03-14 16:58:34 +00003438 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3439 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003440 sig = status->signal;
3441
Thomas Pedersene76fede2020-04-30 10:25:50 -07003442 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3443 ieee80211_rx_status_to_khz(status), sig,
3444 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f782010-08-12 15:38:38 +02003445 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003446 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003447 dev_kfree_skb(rx->skb);
3448 return RX_QUEUED;
3449 }
3450
Johannes Berg2e161f782010-08-12 15:38:38 +02003451 return RX_CONTINUE;
3452}
3453
3454static ieee80211_rx_result debug_noinline
Johannes Berg1ea02222020-05-26 10:31:33 +02003455ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3456{
3457 struct ieee80211_sub_if_data *sdata = rx->sdata;
3458 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3459 int len = rx->skb->len;
3460
3461 if (!ieee80211_is_action(mgmt->frame_control))
3462 return RX_CONTINUE;
3463
3464 switch (mgmt->u.action.category) {
3465 case WLAN_CATEGORY_SA_QUERY:
3466 if (len < (IEEE80211_MIN_ACTION_SIZE +
3467 sizeof(mgmt->u.action.u.sa_query)))
3468 break;
3469
3470 switch (mgmt->u.action.u.sa_query.action) {
3471 case WLAN_ACTION_SA_QUERY_REQUEST:
3472 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3473 break;
3474 ieee80211_process_sa_query_req(sdata, mgmt, len);
3475 goto handled;
3476 }
3477 break;
3478 }
3479
3480 return RX_CONTINUE;
3481
3482 handled:
3483 if (rx->sta)
3484 rx->sta->rx_stats.packets++;
3485 dev_kfree_skb(rx->skb);
3486 return RX_QUEUED;
3487}
3488
3489static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003490ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3491{
3492 struct ieee80211_local *local = rx->local;
3493 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3494 struct sk_buff *nskb;
3495 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003496 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003497
3498 if (!ieee80211_is_action(mgmt->frame_control))
3499 return RX_CONTINUE;
3500
3501 /*
3502 * For AP mode, hostapd is responsible for handling any action
3503 * frames that we didn't handle, including returning unknown
3504 * ones. For all other modes we will return them to the sender,
3505 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003506 * 802.11-2012 9.24.4.
Johannes Berg2e161f782010-08-12 15:38:38 +02003507 * Newer versions of hostapd shall also use the management frame
3508 * registration mechanisms, but older ones still use cooked
3509 * monitor interfaces so push all frames there.
3510 */
Johannes Berg554891e2010-09-24 12:38:25 +02003511 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f782010-08-12 15:38:38 +02003512 (sdata->vif.type == NL80211_IFTYPE_AP ||
3513 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3514 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003515
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003516 if (is_multicast_ether_addr(mgmt->da))
3517 return RX_DROP_MONITOR;
3518
Johannes Berg84040802010-02-15 12:46:39 +02003519 /* do not return rejected action frames */
3520 if (mgmt->u.action.category & 0x80)
3521 return RX_DROP_UNUSABLE;
3522
3523 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3524 GFP_ATOMIC);
3525 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003526 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003527
John W. Linville292b4df2010-06-24 11:13:56 -04003528 nmgmt->u.action.category |= 0x80;
3529 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3530 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003531
3532 memset(nskb->cb, 0, sizeof(nskb->cb));
3533
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003534 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3535 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3536
3537 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3538 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3539 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003540 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003541 info->hw_queue =
3542 local->hw.offchannel_tx_hw_queue;
3543 }
3544
3545 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04003546 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003547 }
Johannes Berg39192c02008-09-09 14:49:03 +02003548 dev_kfree_skb(rx->skb);
3549 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003550}
3551
3552static ieee80211_rx_result debug_noinline
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003553ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3554{
3555 struct ieee80211_sub_if_data *sdata = rx->sdata;
3556 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3557
3558 if (!ieee80211_is_ext(hdr->frame_control))
3559 return RX_CONTINUE;
3560
3561 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3562 return RX_DROP_MONITOR;
3563
3564 /* for now only beacons are ext, so queue them */
3565 skb_queue_tail(&sdata->skb_queue, rx->skb);
3566 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3567 if (rx->sta)
3568 rx->sta->rx_stats.packets++;
3569
3570 return RX_QUEUED;
3571}
3572
3573static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003574ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003575{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003576 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003577 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3578 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003579
Johannes Berg77a121c2010-06-10 10:21:34 +02003580 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003581
Johannes Berg77a121c2010-06-10 10:21:34 +02003582 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3583 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003584 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003585 sdata->vif.type != NL80211_IFTYPE_STATION)
3586 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003587
Johannes Berg77a121c2010-06-10 10:21:34 +02003588 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003589 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003590 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3591 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3592 /* process for all: mesh, mlme, ibss */
3593 break;
Johannes Berg95697f92019-10-04 15:37:05 +03003594 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3595 if (is_multicast_ether_addr(mgmt->da) &&
3596 !is_broadcast_ether_addr(mgmt->da))
3597 return RX_DROP_MONITOR;
3598
3599 /* process only for station/IBSS */
3600 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3601 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3602 return RX_DROP_MONITOR;
3603 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003604 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3605 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003606 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003607 if (is_multicast_ether_addr(mgmt->da) &&
3608 !is_broadcast_ether_addr(mgmt->da))
3609 return RX_DROP_MONITOR;
3610
Johannes Berg77a121c2010-06-10 10:21:34 +02003611 /* process only for station */
3612 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3613 return RX_DROP_MONITOR;
3614 break;
3615 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003616 /* process only for ibss and mesh */
3617 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3618 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003619 return RX_DROP_MONITOR;
3620 break;
3621 default:
3622 return RX_DROP_MONITOR;
3623 }
Johannes Berg46900292009-02-15 12:44:28 +01003624
Johannes Berg77a121c2010-06-10 10:21:34 +02003625 /* queue up frame and kick off work to process it */
3626 skb_queue_tail(&sdata->skb_queue, rx->skb);
3627 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003628 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003629 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003630
3631 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003632}
3633
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003634static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3635 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003636{
3637 struct ieee80211_sub_if_data *sdata;
3638 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003639 struct sk_buff *skb = rx->skb, *skb2;
3640 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003641 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003642 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003643
Johannes Berg554891e2010-09-24 12:38:25 +02003644 /*
3645 * If cooked monitor has been processed already, then
3646 * don't do it again. If not, set the flag.
3647 */
3648 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003649 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003650 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003651
Johannes Berg152c4772011-10-21 10:22:22 +02003652 /* If there are no cooked monitor interfaces, just free the SKB */
3653 if (!local->cooked_mntrs)
3654 goto out_free_skb;
3655
Johannes Berg1f7bba72014-11-06 22:56:36 +01003656 /* vendor data is long removed here */
3657 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003658 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003659 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003660
3661 if (skb_headroom(skb) < needed_headroom &&
3662 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003663 goto out_free_skb;
3664
Johannes Berg293702a2012-03-02 13:18:19 +01003665 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003666 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3667 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003668
Zhang Shengjud57a5442016-03-03 01:16:56 +00003669 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003670 skb->ip_summed = CHECKSUM_UNNECESSARY;
3671 skb->pkt_type = PACKET_OTHERHOST;
3672 skb->protocol = htons(ETH_P_802_2);
3673
3674 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01003675 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003676 continue;
3677
Johannes Berg05c914f2008-09-11 00:01:58 +02003678 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003679 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003680 continue;
3681
3682 if (prev_dev) {
3683 skb2 = skb_clone(skb, GFP_ATOMIC);
3684 if (skb2) {
3685 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003686 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003687 }
3688 }
3689
3690 prev_dev = sdata->dev;
Lev Stipakov36ec1442020-11-13 23:46:24 +02003691 dev_sw_netstats_rx_add(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003692 }
3693
3694 if (prev_dev) {
3695 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003696 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003697 return;
3698 }
Michael Wu3d30d942008-01-31 19:48:27 +01003699
3700 out_free_skb:
3701 dev_kfree_skb(skb);
3702}
3703
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003704static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3705 ieee80211_rx_result res)
3706{
3707 switch (res) {
3708 case RX_DROP_MONITOR:
3709 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3710 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003711 rx->sta->rx_stats.dropped++;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003712 fallthrough;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003713 case RX_CONTINUE: {
3714 struct ieee80211_rate *rate = NULL;
3715 struct ieee80211_supported_band *sband;
3716 struct ieee80211_rx_status *status;
3717
3718 status = IEEE80211_SKB_RXCB((rx->skb));
3719
3720 sband = rx->local->hw.wiphy->bands[status->band];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003721 if (status->encoding == RX_ENC_LEGACY)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003722 rate = &sband->bitrates[status->rate_idx];
3723
3724 ieee80211_rx_cooked_monitor(rx, rate);
3725 break;
3726 }
3727 case RX_DROP_UNUSABLE:
3728 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3729 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003730 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003731 dev_kfree_skb(rx->skb);
3732 break;
3733 case RX_QUEUED:
3734 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3735 break;
3736 }
3737}
3738
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003739static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3740 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003741{
3742 ieee80211_rx_result res = RX_DROP_MONITOR;
3743 struct sk_buff *skb;
3744
3745#define CALL_RXH(rxh) \
3746 do { \
3747 res = rxh(rx); \
3748 if (res != RX_CONTINUE) \
3749 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003750 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003751
Johannes Berg45ceeee2015-03-16 09:08:20 +01003752 /* Lock here to avoid hitting all of the data used in the RX
3753 * path (e.g. key data, station data, ...) concurrently when
3754 * a frame is released from the reorder buffer due to timeout
3755 * from the timer, potentially concurrently with RX from the
3756 * driver.
3757 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003758 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003759
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003760 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003761 /*
3762 * all the other fields are valid across frames
3763 * that belong to an aMPDU since they are on the
3764 * same TID from the same station
3765 */
3766 rx->skb = skb;
3767
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003768 CALL_RXH(ieee80211_rx_h_check_more_data);
3769 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3770 CALL_RXH(ieee80211_rx_h_sta_process);
3771 CALL_RXH(ieee80211_rx_h_decrypt);
3772 CALL_RXH(ieee80211_rx_h_defragment);
3773 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003774 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003775#ifdef CONFIG_MAC80211_MESH
3776 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3777 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3778#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003779 CALL_RXH(ieee80211_rx_h_amsdu);
3780 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003781
3782 /* special treatment -- needs the queue */
3783 res = ieee80211_rx_h_ctrl(rx, frames);
3784 if (res != RX_CONTINUE)
3785 goto rxh_next;
3786
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003787 CALL_RXH(ieee80211_rx_h_mgmt_check);
3788 CALL_RXH(ieee80211_rx_h_action);
3789 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
Johannes Berg1ea02222020-05-26 10:31:33 +02003790 CALL_RXH(ieee80211_rx_h_action_post_userspace);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003791 CALL_RXH(ieee80211_rx_h_action_return);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003792 CALL_RXH(ieee80211_rx_h_ext);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003793 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003794
3795 rxh_next:
3796 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003797
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003798#undef CALL_RXH
3799 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003800
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003801 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003802}
Johannes Berg571ecf62007-07-27 15:43:22 +02003803
Johannes Berg4406c372010-09-24 11:21:06 +02003804static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003805{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003806 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003807 ieee80211_rx_result res = RX_DROP_MONITOR;
3808
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003809 __skb_queue_head_init(&reorder_release);
3810
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003811#define CALL_RXH(rxh) \
3812 do { \
3813 res = rxh(rx); \
3814 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003815 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003816 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003817
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003818 CALL_RXH(ieee80211_rx_h_check_dup);
3819 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003820
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003821 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003822
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003823 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003824 return;
Johannes Berg49461622008-06-30 15:10:45 +02003825
Johannes Berg2569a822009-11-25 17:46:17 +01003826 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003827 ieee80211_rx_handlers_result(rx, res);
3828
3829#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003830}
3831
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003832/*
Johannes Bergdd318572010-11-29 11:09:16 +01003833 * This function makes calls into the RX path, therefore
3834 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003835 */
3836void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3837{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003838 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003839 struct ieee80211_rx_data rx = {
3840 .sta = sta,
3841 .sdata = sta->sdata,
3842 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003843 /* This is OK -- must be QoS data frame */
3844 .security_idx = tid,
3845 .seqno_idx = tid,
Johannes Berg554891e2010-09-24 12:38:25 +02003846 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003847 struct tid_ampdu_rx *tid_agg_rx;
3848
3849 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3850 if (!tid_agg_rx)
3851 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003852
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003853 __skb_queue_head_init(&frames);
3854
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003855 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003856 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003857 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003858
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003859 if (!skb_queue_empty(&frames)) {
3860 struct ieee80211_event event = {
3861 .type = BA_FRAME_TIMEOUT,
3862 .u.ba.tid = tid,
3863 .u.ba.sta = &sta->sta,
3864 };
3865 drv_event_callback(rx.local, rx.sdata, &event);
3866 }
3867
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003868 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003869}
3870
Sara Sharon06470f72016-01-28 16:19:25 +02003871void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3872 u16 ssn, u64 filtered,
3873 u16 received_mpdus)
3874{
3875 struct sta_info *sta;
3876 struct tid_ampdu_rx *tid_agg_rx;
3877 struct sk_buff_head frames;
3878 struct ieee80211_rx_data rx = {
3879 /* This is OK -- must be QoS data frame */
3880 .security_idx = tid,
3881 .seqno_idx = tid,
3882 };
3883 int i, diff;
3884
3885 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3886 return;
3887
3888 __skb_queue_head_init(&frames);
3889
3890 sta = container_of(pubsta, struct sta_info, sta);
3891
3892 rx.sta = sta;
3893 rx.sdata = sta->sdata;
3894 rx.local = sta->local;
3895
3896 rcu_read_lock();
3897 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3898 if (!tid_agg_rx)
3899 goto out;
3900
3901 spin_lock_bh(&tid_agg_rx->reorder_lock);
3902
3903 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3904 int release;
3905
3906 /* release all frames in the reorder buffer */
3907 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3908 IEEE80211_SN_MODULO;
3909 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3910 release, &frames);
3911 /* update ssn to match received ssn */
3912 tid_agg_rx->head_seq_num = ssn;
3913 } else {
3914 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3915 &frames);
3916 }
3917
3918 /* handle the case that received ssn is behind the mac ssn.
3919 * it can be tid_agg_rx->buf_size behind and still be valid */
3920 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3921 if (diff >= tid_agg_rx->buf_size) {
3922 tid_agg_rx->reorder_buf_filtered = 0;
3923 goto release;
3924 }
3925 filtered = filtered >> diff;
3926 ssn += diff;
3927
3928 /* update bitmap */
3929 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3930 int index = (ssn + i) % tid_agg_rx->buf_size;
3931
3932 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3933 if (filtered & BIT_ULL(i))
3934 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3935 }
3936
3937 /* now process also frames that the filter marking released */
3938 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3939
3940release:
3941 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3942
3943 ieee80211_rx_handlers(&rx, &frames);
3944
3945 out:
3946 rcu_read_unlock();
3947}
3948EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3949
Johannes Berg571ecf62007-07-27 15:43:22 +02003950/* main receive path */
3951
Johannes Berga58fbe12015-04-22 15:08:39 +02003952static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003953{
Johannes Berg20b01f82010-09-24 11:21:05 +02003954 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003955 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02003956 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003957 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3958 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003959 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
3960 ieee80211_is_s1g_beacon(hdr->frame_control);
Johannes Berg23a24de2007-07-27 15:43:22 +02003961
Johannes Berg51fb61e2007-12-19 01:31:27 +01003962 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003963 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01003964 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003965 return false;
Johannes Berg588f7d32019-02-13 15:13:30 +01003966 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3967 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003968 if (multicast)
3969 return true;
3970 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003971 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02003972 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003973 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02003974 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3975 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003976 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003977 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003978 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02003979 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003980 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003981 if (!multicast &&
3982 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003983 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003984 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003985 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02003986 if (status->encoding != RX_ENC_LEGACY)
Johannes Berg56146182012-11-09 15:07:02 +01003987 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003988 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003989 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01003990 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3991 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003992 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003993 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003994 case NL80211_IFTYPE_OCB:
3995 if (!bssid)
3996 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02003997 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003998 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003999 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004000 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004001 if (!multicast &&
4002 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004003 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004004 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004005 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004006 if (status->encoding != RX_ENC_LEGACY)
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004007 rate_idx = 0; /* TODO: HT rates */
4008 else
4009 rate_idx = status->rate_idx;
4010 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4011 BIT(rate_idx));
4012 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004013 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004014 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg736a80b2018-01-04 15:51:53 +01004015 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4016 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004017 if (multicast)
4018 return true;
4019 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004020 case NL80211_IFTYPE_AP_VLAN:
4021 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02004022 if (!bssid)
4023 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4024
4025 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01004026 /*
4027 * Accept public action frames even when the
4028 * BSSID doesn't match, this is used for P2P
4029 * and location updates. Note that mac80211
4030 * itself never looks at these frames.
4031 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02004032 if (!multicast &&
4033 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004034 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02004035 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004036 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02004037 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02004038 }
4039
4040 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004041 /* ignore data frames to TDLS-peers */
4042 if (ieee80211_is_data(hdr->frame_control))
4043 return false;
4044 /* ignore action frames to TDLS-peers */
4045 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02004046 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004047 !ether_addr_equal(bssid, hdr->addr1))
4048 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004049 }
Johannes Berg3018e942017-04-20 21:32:16 +02004050
4051 /*
4052 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4053 * the BSSID - we've checked that already but may have accepted
4054 * the wildcard (ff:ff:ff:ff:ff:ff).
4055 *
4056 * It also says:
4057 * The BSSID of the Data frame is determined as follows:
4058 * a) If the STA is contained within an AP or is associated
4059 * with an AP, the BSSID is the address currently in use
4060 * by the STA contained in the AP.
4061 *
4062 * So we should not accept data frames with an address that's
4063 * multicast.
4064 *
4065 * Accepting it also opens a security problem because stations
4066 * could encrypt it with the GTK and inject traffic that way.
4067 */
4068 if (ieee80211_is_data(hdr->frame_control) && multicast)
4069 return false;
4070
Johannes Berga58fbe12015-04-22 15:08:39 +02004071 return true;
Johannes Bergf142c6b2012-06-18 20:07:15 +02004072 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02004073 return ieee80211_is_public_action(hdr, skb->len) ||
4074 ieee80211_is_probe_req(hdr->frame_control) ||
4075 ieee80211_is_probe_resp(hdr->frame_control) ||
4076 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03004077 case NL80211_IFTYPE_NAN:
4078 /* Currently no frames on NAN interface are allowed */
4079 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02004080 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02004081 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02004082 }
4083
Johannes Berga58fbe12015-04-22 15:08:39 +02004084 WARN_ON_ONCE(1);
4085 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004086}
4087
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004088void ieee80211_check_fast_rx(struct sta_info *sta)
4089{
4090 struct ieee80211_sub_if_data *sdata = sta->sdata;
4091 struct ieee80211_local *local = sdata->local;
4092 struct ieee80211_key *key;
4093 struct ieee80211_fast_rx fastrx = {
4094 .dev = sdata->dev,
4095 .vif_type = sdata->vif.type,
4096 .control_port_protocol = sdata->control_port_protocol,
4097 }, *old, *new = NULL;
4098 bool assign = false;
4099
4100 /* use sparse to check that we don't return without updating */
4101 __acquire(check_fast_rx);
4102
4103 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4104 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4105 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4106 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4107
Johannes Bergc9c59622016-03-31 20:02:11 +03004108 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4109
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004110 /* fast-rx doesn't do reordering */
4111 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4112 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4113 goto clear;
4114
4115 switch (sdata->vif.type) {
4116 case NL80211_IFTYPE_STATION:
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004117 if (sta->sta.tdls) {
4118 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4119 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4120 fastrx.expected_ds_bits = 0;
4121 } else {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004122 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4123 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4124 fastrx.expected_ds_bits =
4125 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4126 }
Felix Fietkau1d870162018-02-23 10:06:05 +01004127
Felix Fietkau9251a472018-02-23 13:55:50 +01004128 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4129 fastrx.expected_ds_bits |=
4130 cpu_to_le16(IEEE80211_FCTL_TODS);
4131 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4132 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4133 }
4134
Felix Fietkau1d870162018-02-23 10:06:05 +01004135 if (!sdata->u.mgd.powersave)
4136 break;
4137
4138 /* software powersave is a huge mess, avoid all of it */
4139 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4140 goto clear;
4141 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4142 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4143 goto clear;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004144 break;
4145 case NL80211_IFTYPE_AP_VLAN:
4146 case NL80211_IFTYPE_AP:
4147 /* parallel-rx requires this, at least with calls to
4148 * ieee80211_sta_ps_transition()
4149 */
4150 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4151 goto clear;
4152 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4153 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4154 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4155
4156 fastrx.internal_forward =
4157 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4158 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4159 !sdata->u.vlan.sta);
Felix Fietkau59cae5b2018-02-23 10:06:04 +01004160
4161 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4162 sdata->u.vlan.sta) {
4163 fastrx.expected_ds_bits |=
4164 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4165 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4166 fastrx.internal_forward = 0;
4167 }
4168
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004169 break;
4170 default:
4171 goto clear;
4172 }
4173
4174 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4175 goto clear;
4176
4177 rcu_read_lock();
4178 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
Felix Fietkau622d3b42020-12-18 19:47:17 +01004179 if (!key)
4180 key = rcu_dereference(sdata->default_unicast_key);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004181 if (key) {
4182 switch (key->conf.cipher) {
4183 case WLAN_CIPHER_SUITE_TKIP:
4184 /* we don't want to deal with MMIC in fast-rx */
4185 goto clear_rcu;
4186 case WLAN_CIPHER_SUITE_CCMP:
4187 case WLAN_CIPHER_SUITE_CCMP_256:
4188 case WLAN_CIPHER_SUITE_GCMP:
4189 case WLAN_CIPHER_SUITE_GCMP_256:
4190 break;
4191 default:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01004192 /* We also don't want to deal with
4193 * WEP or cipher scheme.
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004194 */
4195 goto clear_rcu;
4196 }
4197
4198 fastrx.key = true;
4199 fastrx.icv_len = key->conf.icv_len;
4200 }
4201
4202 assign = true;
4203 clear_rcu:
4204 rcu_read_unlock();
4205 clear:
4206 __release(check_fast_rx);
4207
4208 if (assign)
4209 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4210
4211 spin_lock_bh(&sta->lock);
4212 old = rcu_dereference_protected(sta->fast_rx, true);
4213 rcu_assign_pointer(sta->fast_rx, new);
4214 spin_unlock_bh(&sta->lock);
4215
4216 if (old)
4217 kfree_rcu(old, rcu_head);
4218}
4219
4220void ieee80211_clear_fast_rx(struct sta_info *sta)
4221{
4222 struct ieee80211_fast_rx *old;
4223
4224 spin_lock_bh(&sta->lock);
4225 old = rcu_dereference_protected(sta->fast_rx, true);
4226 RCU_INIT_POINTER(sta->fast_rx, NULL);
4227 spin_unlock_bh(&sta->lock);
4228
4229 if (old)
4230 kfree_rcu(old, rcu_head);
4231}
4232
4233void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4234{
4235 struct ieee80211_local *local = sdata->local;
4236 struct sta_info *sta;
4237
4238 lockdep_assert_held(&local->sta_mtx);
4239
Madhuparna Bhowmik253216f2020-02-23 20:03:02 +05304240 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004241 if (sdata != sta->sdata &&
4242 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4243 continue;
4244 ieee80211_check_fast_rx(sta);
4245 }
4246}
4247
4248void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4249{
4250 struct ieee80211_local *local = sdata->local;
4251
4252 mutex_lock(&local->sta_mtx);
4253 __ieee80211_check_fast_rx_iface(sdata);
4254 mutex_unlock(&local->sta_mtx);
4255}
4256
4257static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4258 struct ieee80211_fast_rx *fast_rx)
4259{
4260 struct sk_buff *skb = rx->skb;
4261 struct ieee80211_hdr *hdr = (void *)skb->data;
4262 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4263 struct sta_info *sta = rx->sta;
4264 int orig_len = skb->len;
Felix Fietkau24bba072018-02-27 13:03:07 +01004265 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4266 int snap_offs = hdrlen;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004267 struct {
4268 u8 snap[sizeof(rfc1042_header)];
4269 __be16 proto;
4270 } *payload __aligned(2);
4271 struct {
4272 u8 da[ETH_ALEN];
4273 u8 sa[ETH_ALEN];
4274 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03004275 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4276
4277 if (fast_rx->uses_rss)
4278 stats = this_cpu_ptr(sta->pcpu_rx_stats);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004279
4280 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4281 * to a common data structure; drivers can implement that per queue
4282 * but we don't have that information in mac80211
4283 */
4284 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4285 return false;
4286
4287#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4288
4289 /* If using encryption, we also need to have:
4290 * - PN_VALIDATED: similar, but the implementation is tricky
4291 * - DECRYPTED: necessary for PN_VALIDATED
4292 */
4293 if (fast_rx->key &&
4294 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4295 return false;
4296
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004297 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4298 return false;
4299
4300 if (unlikely(ieee80211_is_frag(hdr)))
4301 return false;
4302
4303 /* Since our interface address cannot be multicast, this
4304 * implicitly also rejects multicast frames without the
4305 * explicit check.
4306 *
4307 * We shouldn't get any *data* frames not addressed to us
4308 * (AP mode will accept multicast *management* frames), but
4309 * punting here will make it go through the full checks in
4310 * ieee80211_accept_frame().
4311 */
4312 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4313 return false;
4314
4315 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4316 IEEE80211_FCTL_TODS)) !=
4317 fast_rx->expected_ds_bits)
Felix Fietkaub323ac12018-02-23 10:06:03 +01004318 return false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004319
4320 /* assign the key to drop unencrypted frames (later)
4321 * and strip the IV/MIC if necessary
4322 */
4323 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4324 /* GCMP header length is the same */
4325 snap_offs += IEEE80211_CCMP_HDR_LEN;
4326 }
4327
Felix Fietkau24bba072018-02-27 13:03:07 +01004328 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4329 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4330 goto drop;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004331
Felix Fietkau24bba072018-02-27 13:03:07 +01004332 payload = (void *)(skb->data + snap_offs);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004333
Felix Fietkau24bba072018-02-27 13:03:07 +01004334 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4335 return false;
4336
4337 /* Don't handle these here since they require special code.
4338 * Accept AARP and IPX even though they should come with a
4339 * bridge-tunnel header - but if we get them this way then
4340 * there's little point in discarding them.
4341 */
4342 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4343 payload->proto == fast_rx->control_port_protocol))
4344 return false;
4345 }
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004346
4347 /* after this point, don't punt to the slowpath! */
4348
4349 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4350 pskb_trim(skb, skb->len - fast_rx->icv_len))
4351 goto drop;
4352
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004353 /* statistics part of ieee80211_rx_h_sta_process() */
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004354 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Bergc9c59622016-03-31 20:02:11 +03004355 stats->last_signal = status->signal;
4356 if (!fast_rx->uses_rss)
4357 ewma_signal_add(&sta->rx_stats_avg.signal,
4358 -status->signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004359 }
4360
4361 if (status->chains) {
4362 int i;
4363
Johannes Bergc9c59622016-03-31 20:02:11 +03004364 stats->chains = status->chains;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004365 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4366 int signal = status->chain_signal[i];
4367
4368 if (!(status->chains & BIT(i)))
4369 continue;
4370
Johannes Bergc9c59622016-03-31 20:02:11 +03004371 stats->chain_signal_last[i] = signal;
4372 if (!fast_rx->uses_rss)
4373 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4374 -signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004375 }
4376 }
4377 /* end of statistics */
4378
4379 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4380 goto drop;
4381
Felix Fietkau24bba072018-02-27 13:03:07 +01004382 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4383 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4384 RX_QUEUED)
4385 goto drop;
4386
4387 return true;
4388 }
4389
4390 stats->last_rx = jiffies;
4391 stats->last_rate = sta_stats_encode_rate(status);
4392
4393 stats->fragments++;
4394 stats->packets++;
4395
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004396 /* do the header conversion - first grab the addresses */
4397 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4398 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4399 /* remove the SNAP but leave the ethertype */
4400 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4401 /* push the addresses in front */
4402 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4403
4404 skb->dev = fast_rx->dev;
4405
Lev Stipakov36ec1442020-11-13 23:46:24 +02004406 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004407
4408 /* The seqno index has the same property as needed
4409 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4410 * for non-QoS-data frames. Here we know it's a data
4411 * frame, so count MSDUs.
4412 */
Johannes Bergc9c59622016-03-31 20:02:11 +03004413 u64_stats_update_begin(&stats->syncp);
4414 stats->msdu[rx->seqno_idx]++;
4415 stats->bytes += orig_len;
4416 u64_stats_update_end(&stats->syncp);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004417
4418 if (fast_rx->internal_forward) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004419 struct sk_buff *xmit_skb = NULL;
Johannes Berg42dca5e2018-10-09 10:00:21 +02004420 if (is_multicast_ether_addr(addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004421 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Johannes Berg42dca5e2018-10-09 10:00:21 +02004422 } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4423 sta_info_get(rx->sdata, addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004424 xmit_skb = skb;
4425 skb = NULL;
4426 }
4427
4428 if (xmit_skb) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004429 /*
4430 * Send to wireless media and increase priority by 256
4431 * to keep the received priority instead of
4432 * reclassifying the frame (see cfg80211_classify8021d).
4433 */
Johannes Bergeeb0d562016-12-14 16:47:43 +01004434 xmit_skb->priority += 256;
4435 xmit_skb->protocol = htons(ETH_P_802_3);
4436 skb_reset_network_header(xmit_skb);
4437 skb_reset_mac_header(xmit_skb);
4438 dev_queue_xmit(xmit_skb);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004439 }
Johannes Bergeeb0d562016-12-14 16:47:43 +01004440
4441 if (!skb)
4442 return true;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004443 }
4444
4445 /* deliver to local stack */
4446 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4447 memset(skb->cb, 0, sizeof(skb->cb));
Felix Fietkauc5d16862020-07-26 13:06:11 +02004448 if (rx->list)
4449 list_add_tail(&skb->list, rx->list);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004450 else
4451 netif_receive_skb(skb);
4452
4453 return true;
4454 drop:
4455 dev_kfree_skb(skb);
Johannes Bergc9c59622016-03-31 20:02:11 +03004456 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004457 return true;
4458}
4459
Johannes Berg571ecf62007-07-27 15:43:22 +02004460/*
Johannes Berg4406c372010-09-24 11:21:06 +02004461 * This function returns whether or not the SKB
4462 * was destined for RX processing or not, which,
4463 * if consume is true, is equivalent to whether
4464 * or not the skb was consumed.
4465 */
4466static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4467 struct sk_buff *skb, bool consume)
4468{
4469 struct ieee80211_local *local = rx->local;
4470 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004471
4472 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004473
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004474 /* See if we can do fast-rx; if we have to copy we already lost,
4475 * so punt in that case. We should never have to deliver a data
4476 * frame to multiple interfaces anyway.
4477 *
4478 * We skip the ieee80211_accept_frame() call and do the necessary
4479 * checking inside ieee80211_invoke_fast_rx().
4480 */
4481 if (consume && rx->sta) {
4482 struct ieee80211_fast_rx *fast_rx;
4483
4484 fast_rx = rcu_dereference(rx->sta->fast_rx);
4485 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4486 return true;
4487 }
4488
Johannes Berga58fbe12015-04-22 15:08:39 +02004489 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004490 return false;
4491
Johannes Berg4406c372010-09-24 11:21:06 +02004492 if (!consume) {
4493 skb = skb_copy(skb, GFP_ATOMIC);
4494 if (!skb) {
4495 if (net_ratelimit())
4496 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004497 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004498 sdata->name);
4499 return true;
4500 }
4501
4502 rx->skb = skb;
4503 }
4504
4505 ieee80211_invoke_rx_handlers(rx);
4506 return true;
4507}
4508
4509/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004510 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004511 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004512 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004513static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004514 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004515 struct sk_buff *skb,
Felix Fietkauc5d16862020-07-26 13:06:11 +02004516 struct list_head *list)
Johannes Berg571ecf62007-07-27 15:43:22 +02004517{
4518 struct ieee80211_local *local = hw_to_local(hw);
4519 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004520 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004521 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004522 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004523 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004524 struct rhlist_head *tmp;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004525 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004526
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004527 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004528 memset(&rx, 0, sizeof(rx));
4529 rx.skb = skb;
4530 rx.local = local;
Felix Fietkauc5d16862020-07-26 13:06:11 +02004531 rx.list = list;
Johannes Berg72abd812007-09-17 01:29:22 -04004532
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004533 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004534 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004535
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004536 if (ieee80211_is_mgmt(fc)) {
4537 /* drop frame if too short for header */
4538 if (skb->len < ieee80211_hdrlen(fc))
4539 err = -ENOBUFS;
4540 else
4541 err = skb_linearize(skb);
4542 } else {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004543 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004544 }
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004545
4546 if (err) {
4547 dev_kfree_skb(skb);
4548 return;
4549 }
4550
4551 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004552 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004553 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004554
Johannes Bergd48b2962012-07-06 22:19:27 +02004555 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
Thomas Pedersencd418ba2020-09-21 19:28:08 -07004556 ieee80211_is_beacon(hdr->frame_control) ||
4557 ieee80211_is_s1g_beacon(hdr->frame_control)))
Johannes Bergd48b2962012-07-06 22:19:27 +02004558 ieee80211_scan_rx(local, skb);
4559
Johannes Berg19d19e92017-02-27 09:38:11 +01004560 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004561 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004562
Johannes Berg19d19e92017-02-27 09:38:11 +01004563 if (pubsta) {
4564 rx.sta = container_of(pubsta, struct sta_info, sta);
4565 rx.sdata = rx.sta->sdata;
4566 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4567 return;
4568 goto out;
4569 }
4570
Ben Greear56af3262010-09-23 10:22:24 -07004571 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004572
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004573 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004574 if (!prev_sta) {
4575 prev_sta = sta;
4576 continue;
4577 }
4578
4579 rx.sta = prev_sta;
4580 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004581 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004582
Ben Greear56af3262010-09-23 10:22:24 -07004583 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004584 }
Ben Greear56af3262010-09-23 10:22:24 -07004585
4586 if (prev_sta) {
4587 rx.sta = prev_sta;
4588 rx.sdata = prev_sta->sdata;
4589
Johannes Berg4406c372010-09-24 11:21:06 +02004590 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004591 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304592 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004593 }
Johannes Berg4406c372010-09-24 11:21:06 +02004594 }
4595
Johannes Berg4b0dd982010-09-24 11:21:07 +02004596 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004597
Johannes Berg4b0dd982010-09-24 11:21:07 +02004598 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4599 if (!ieee80211_sdata_running(sdata))
4600 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004601
Johannes Berg4b0dd982010-09-24 11:21:07 +02004602 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4603 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4604 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004605
Johannes Berg4b0dd982010-09-24 11:21:07 +02004606 /*
4607 * frame is destined for this interface, but if it's
4608 * not also for the previous one we handle that after
4609 * the loop to avoid copying the SKB once too much
4610 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004611
Johannes Berg4b0dd982010-09-24 11:21:07 +02004612 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004613 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004614 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004615 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004616
Johannes Berg7852e362012-01-20 13:55:24 +01004617 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004618 rx.sdata = prev;
4619 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004620
Johannes Berg4b0dd982010-09-24 11:21:07 +02004621 prev = sdata;
4622 }
Johannes Berg4406c372010-09-24 11:21:06 +02004623
Johannes Berg4b0dd982010-09-24 11:21:07 +02004624 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004625 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004626 rx.sdata = prev;
4627
4628 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4629 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004630 }
Johannes Berg4406c372010-09-24 11:21:06 +02004631
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304632 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004633 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004634}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004635
4636/*
4637 * This is the receive path handler. It is called by a low level driver when an
4638 * 802.11 MPDU is received from the hardware.
4639 */
Felix Fietkauc5d16862020-07-26 13:06:11 +02004640void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4641 struct sk_buff *skb, struct list_head *list)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004642{
4643 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004644 struct ieee80211_rate *rate = NULL;
4645 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004646 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004647
Johannes Bergd20ef632009-10-11 15:10:40 +02004648 WARN_ON_ONCE(softirq_count() == 0);
4649
Johannes Berg57fbcce2016-04-12 15:56:15 +02004650 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004651 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004652
4653 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004654 if (WARN_ON(!sband))
4655 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004656
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004657 /*
4658 * If we're suspending, it is possible although not too likely
4659 * that we'd be receiving frames after having already partially
4660 * quiesced the stack. We can't process such frames then since
4661 * that might, for example, cause stations to be added or other
4662 * driver callbacks be invoked.
4663 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004664 if (unlikely(local->quiescing || local->suspended))
4665 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004666
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004667 /* We might be during a HW reconfig, prevent Rx for the same reason */
4668 if (unlikely(local->in_reconfig))
4669 goto drop;
4670
Johannes Bergea77f122009-08-21 14:44:45 +02004671 /*
4672 * The same happens when we're not even started,
4673 * but that's worth a warning.
4674 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004675 if (WARN_ON(!local->started))
4676 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004677
Johannes Bergfc885182010-07-30 13:23:12 +02004678 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004679 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004680 * Validate the rate, unless a PLCP error means that
4681 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004682 */
Johannes Bergfc885182010-07-30 13:23:12 +02004683
Johannes Bergda6a4352017-04-26 12:14:59 +02004684 switch (status->encoding) {
4685 case RX_ENC_HT:
Johannes Bergfc885182010-07-30 13:23:12 +02004686 /*
4687 * rate_idx is MCS index, which can be [0-76]
4688 * as documented on:
4689 *
Flavio Suligoi59d4bfc2020-06-05 17:41:12 +02004690 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
Johannes Bergfc885182010-07-30 13:23:12 +02004691 *
4692 * Anything else would be some sort of driver or
4693 * hardware error. The driver should catch hardware
4694 * errors.
4695 */
Johannes Berg444e3802012-09-30 17:08:35 +02004696 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004697 "Rate marked as an HT rate but passed "
4698 "status->rate_idx is not "
4699 "an MCS index [0-76]: %d (0x%02x)\n",
4700 status->rate_idx,
4701 status->rate_idx))
4702 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004703 break;
4704 case RX_ENC_VHT:
Johannes Berg56146182012-11-09 15:07:02 +01004705 if (WARN_ONCE(status->rate_idx > 9 ||
Johannes Berg8613c942017-04-26 13:51:41 +02004706 !status->nss ||
4707 status->nss > 8,
Johannes Berg56146182012-11-09 15:07:02 +01004708 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
Johannes Berg8613c942017-04-26 13:51:41 +02004709 status->rate_idx, status->nss))
Johannes Berg56146182012-11-09 15:07:02 +01004710 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004711 break;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004712 case RX_ENC_HE:
4713 if (WARN_ONCE(status->rate_idx > 11 ||
4714 !status->nss ||
4715 status->nss > 8,
4716 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4717 status->rate_idx, status->nss))
4718 goto drop;
4719 break;
Johannes Bergda6a4352017-04-26 12:14:59 +02004720 default:
4721 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004722 fallthrough;
Johannes Bergda6a4352017-04-26 12:14:59 +02004723 case RX_ENC_LEGACY:
Johannes Berg444e3802012-09-30 17:08:35 +02004724 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004725 goto drop;
4726 rate = &sband->bitrates[status->rate_idx];
4727 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004728 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004729
Johannes Berg554891e2010-09-24 12:38:25 +02004730 status->rx_flags = 0;
4731
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004732 kcov_remote_start_common(skb_get_kcov_handle(skb));
4733
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004734 /*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004735 * Frames with failed FCS/PLCP checksum are not returned,
4736 * all other frames are returned without radiotap header
4737 * if it was previously present.
4738 * Also, frames with less than 16 bytes are dropped.
4739 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004740 skb = ieee80211_rx_monitor(local, skb, rate);
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004741 if (skb) {
4742 ieee80211_tpt_led_trig_rx(local,
4743 ((struct ieee80211_hdr *)skb->data)->frame_control,
4744 skb->len);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004745
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004746 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
4747 }
Johannes Bergd63b5482016-03-31 20:02:02 +03004748
Aleksandr Nogikh261e4112020-10-29 17:36:20 +00004749 kcov_remote_stop();
Johannes Berg77a980d2009-08-24 11:46:30 +02004750 return;
4751 drop:
4752 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004753}
Felix Fietkauc5d16862020-07-26 13:06:11 +02004754EXPORT_SYMBOL(ieee80211_rx_list);
4755
4756void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4757 struct sk_buff *skb, struct napi_struct *napi)
4758{
4759 struct sk_buff *tmp;
4760 LIST_HEAD(list);
4761
4762
4763 /*
4764 * key references and virtual interfaces are protected using RCU
4765 * and this requires that we are in a read-side RCU section during
4766 * receive processing
4767 */
4768 rcu_read_lock();
4769 ieee80211_rx_list(hw, pubsta, skb, &list);
4770 rcu_read_unlock();
4771
4772 if (!napi) {
4773 netif_receive_skb_list(&list);
4774 return;
4775 }
4776
4777 list_for_each_entry_safe(skb, tmp, &list, list) {
4778 skb_list_del_init(skb);
4779 napi_gro_receive(napi, skb);
4780 }
4781}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004782EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004783
4784/* This is a version of the rx handler that can be called from hard irq
4785 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004786void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004787{
4788 struct ieee80211_local *local = hw_to_local(hw);
4789
4790 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4791
Johannes Berg571ecf62007-07-27 15:43:22 +02004792 skb->pkt_type = IEEE80211_RX_MSG;
4793 skb_queue_tail(&local->skb_queue, skb);
4794 tasklet_schedule(&local->tasklet);
4795}
4796EXPORT_SYMBOL(ieee80211_rx_irqsafe);