blob: 1e2e5a406d58759433e6f0d3b7e60aaa87744167 [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 Berg5a490512015-04-22 17:10:38 +020035static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
36{
37 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
38
39 u64_stats_update_begin(&tstats->syncp);
40 tstats->rx_packets++;
41 tstats->rx_bytes += len;
42 u64_stats_update_end(&tstats->syncp);
43}
44
Johannes Bergb2e77712007-09-26 15:19:39 +020045/*
46 * monitor mode reception
47 *
48 * This function cleans up the SKB, i.e. it removes all the stuff
49 * only useful for monitoring.
50 */
Johannes Bergc1129922020-05-26 14:33:48 +020051static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
52 unsigned int present_fcs_len,
53 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020054{
Johannes Bergc1129922020-05-26 14:33:48 +020055 struct ieee80211_hdr *hdr;
56 unsigned int hdrlen;
57 __le16 fc;
58
Johannes Berg30841f52017-04-11 15:38:56 +020059 if (present_fcs_len)
60 __pskb_trim(skb, skb->len - present_fcs_len);
Johannes Bergc096b922018-04-20 13:49:18 +030061 __pskb_pull(skb, rtap_space);
Johannes Bergc1129922020-05-26 14:33:48 +020062
63 hdr = (void *)skb->data;
64 fc = hdr->frame_control;
65
66 /*
67 * Remove the HT-Control field (if present) on management
68 * frames after we've sent the frame to monitoring. We
69 * (currently) don't need it, and don't properly parse
70 * frames with it present, due to the assumption of a
71 * fixed management header length.
72 */
73 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
74 return skb;
75
76 hdrlen = ieee80211_hdrlen(fc);
77 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
78
79 if (!pskb_may_pull(skb, hdrlen)) {
80 dev_kfree_skb(skb);
81 return NULL;
82 }
83
84 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
85 hdrlen - IEEE80211_HT_CTL_LEN);
86 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
87
88 return skb;
Johannes Bergb2e77712007-09-26 15:19:39 +020089}
90
Johannes Berg1f7bba72014-11-06 22:56:36 +010091static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
Johannes Bergc096b922018-04-20 13:49:18 +030092 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020093{
Johannes Bergf1d58c22009-06-17 13:13:00 +020094 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +010095 struct ieee80211_hdr *hdr;
96
Johannes Bergc096b922018-04-20 13:49:18 +030097 hdr = (void *)(skb->data + rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +020098
Johannes Berg4c298672012-07-05 11:34:31 +020099 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100100 RX_FLAG_FAILED_PLCP_CRC |
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300101 RX_FLAG_ONLY_MONITOR |
102 RX_FLAG_NO_PSDU))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800103 return true;
104
Johannes Bergc096b922018-04-20 13:49:18 +0300105 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800106 return true;
107
Harvey Harrison87228f52008-06-11 14:21:59 -0700108 if (ieee80211_is_ctl(hdr->frame_control) &&
109 !ieee80211_is_pspoll(hdr->frame_control) &&
110 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800111 return true;
112
113 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200114}
115
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200116static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100117ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
118 struct ieee80211_rx_status *status,
119 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200120{
121 int len;
122
123 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200124 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200125
Johannes Berga144f372013-07-03 13:34:02 +0200126 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200127 if (status->chains)
128 len += 4 * hweight8(status->chains);
Johannes Bergefc38dd2018-12-15 11:03:12 +0200129 /* vendor presence bitmap */
130 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
131 len += 4;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100132
133 if (ieee80211_have_rx_timestamp(status)) {
134 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200135 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100136 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200137 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200138 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200139
Johannes Berga144f372013-07-03 13:34:02 +0200140 /* antenna field, if we don't have per-chain info */
141 if (!status->chains)
142 len += 1;
143
Johannes Berg90b9e4462012-11-16 10:09:08 +0100144 /* padding for RX_FLAGS if necessary */
145 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200146
Johannes Bergda6a4352017-04-26 12:14:59 +0200147 if (status->encoding == RX_ENC_HT) /* HT info */
Johannes Berg6d744ba2011-01-27 14:13:17 +0100148 len += 3;
149
Johannes Berg4c298672012-07-05 11:34:31 +0200150 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100151 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200152 len += 8;
153 }
154
Johannes Bergda6a4352017-04-26 12:14:59 +0200155 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100156 len = ALIGN(len, 2);
157 len += 12;
158 }
159
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300160 if (local->hw.radiotap_timestamp.units_pos >= 0) {
161 len = ALIGN(len, 8);
162 len += 12;
163 }
164
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300165 if (status->encoding == RX_ENC_HE &&
166 status->flag & RX_FLAG_RADIOTAP_HE) {
167 len = ALIGN(len, 2);
168 len += 12;
169 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
170 }
171
172 if (status->encoding == RX_ENC_HE &&
173 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
174 len = ALIGN(len, 2);
175 len += 12;
176 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
177 }
178
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300179 if (status->flag & RX_FLAG_NO_PSDU)
180 len += 1;
181
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300182 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
183 len = ALIGN(len, 2);
184 len += 4;
185 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
186 }
187
Johannes Berga144f372013-07-03 13:34:02 +0200188 if (status->chains) {
189 /* antenna and antenna signal fields */
190 len += 2 * hweight8(status->chains);
191 }
192
Johannes Berg1f7bba72014-11-06 22:56:36 +0100193 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
Liad Kaufmanc15353b2019-02-06 13:17:13 +0200194 struct ieee80211_vendor_radiotap *rtap;
195 int vendor_data_offset = 0;
196
197 /*
198 * The position to look at depends on the existence (or non-
199 * existence) of other elements, so take that into account...
200 */
201 if (status->flag & RX_FLAG_RADIOTAP_HE)
202 vendor_data_offset +=
203 sizeof(struct ieee80211_radiotap_he);
204 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
205 vendor_data_offset +=
206 sizeof(struct ieee80211_radiotap_he_mu);
207 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
208 vendor_data_offset +=
209 sizeof(struct ieee80211_radiotap_lsig);
210
211 rtap = (void *)&skb->data[vendor_data_offset];
Johannes Berg1f7bba72014-11-06 22:56:36 +0100212
Johannes Berg1f7bba72014-11-06 22:56:36 +0100213 /* alignment for fixed 6-byte vendor data header */
214 len = ALIGN(len, 2);
215 /* vendor data header */
216 len += 6;
217 if (WARN_ON(rtap->align == 0))
218 rtap->align = 1;
219 len = ALIGN(len, rtap->align);
220 len += rtap->len + rtap->pad;
221 }
222
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200223 return len;
224}
225
Johannes Berg9e478062017-04-13 14:23:49 +0200226static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
227 struct sk_buff *skb,
Johannes Bergc096b922018-04-20 13:49:18 +0300228 int rtap_space)
Johannes Berg9e478062017-04-13 14:23:49 +0200229{
230 struct {
231 struct ieee80211_hdr_3addr hdr;
232 u8 category;
233 u8 action_code;
Mathieu Malaterre7c53eb52019-01-24 19:19:57 +0100234 } __packed __aligned(2) action;
Johannes Berg9e478062017-04-13 14:23:49 +0200235
236 if (!sdata)
237 return;
238
239 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
240
Johannes Bergc096b922018-04-20 13:49:18 +0300241 if (skb->len < rtap_space + sizeof(action) +
Johannes Berg9e478062017-04-13 14:23:49 +0200242 VHT_MUMIMO_GROUPS_DATA_LEN)
243 return;
244
245 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
246 return;
247
Johannes Bergc096b922018-04-20 13:49:18 +0300248 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
Johannes Berg9e478062017-04-13 14:23:49 +0200249
250 if (!ieee80211_is_action(action.hdr.frame_control))
251 return;
252
253 if (action.category != WLAN_CATEGORY_VHT)
254 return;
255
256 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
257 return;
258
259 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
260 return;
261
262 skb = skb_copy(skb, GFP_ATOMIC);
263 if (!skb)
264 return;
265
Johannes Berg9e478062017-04-13 14:23:49 +0200266 skb_queue_tail(&sdata->skb_queue, skb);
267 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
268}
269
Johannes Berg00ea6de2012-09-05 15:54:51 +0200270/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200271 * ieee80211_add_rx_radiotap_header - add radiotap header
272 *
273 * add a radiotap header containing all the fields which the hardware provided.
274 */
275static void
276ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
277 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200278 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200279 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200280{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200281 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200282 struct ieee80211_radiotap_header *rthdr;
283 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200284 __le32 *it_present;
285 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100286 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200287 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200288 int mpdulen, chain;
289 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100290 struct ieee80211_vendor_radiotap rtap = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300291 struct ieee80211_radiotap_he he = {};
292 struct ieee80211_radiotap_he_mu he_mu = {};
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300293 struct ieee80211_radiotap_lsig lsig = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300294
295 if (status->flag & RX_FLAG_RADIOTAP_HE) {
296 he = *(struct ieee80211_radiotap_he *)skb->data;
297 skb_pull(skb, sizeof(he));
298 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
299 }
300
301 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
302 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
303 skb_pull(skb, sizeof(he_mu));
304 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100305
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300306 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
307 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
308 skb_pull(skb, sizeof(lsig));
309 }
310
Johannes Berg1f7bba72014-11-06 22:56:36 +0100311 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
312 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
313 /* rtap.len and rtap.pad are undone immediately */
314 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
315 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800316
317 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200318 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800319 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200320
Johannes Bergd58ff352017-06-16 14:29:23 +0200321 rthdr = skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100322 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200323 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200324
325 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200326 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200327 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
328 BIT(IEEE80211_RADIOTAP_CHANNEL) |
329 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200330
Johannes Berga144f372013-07-03 13:34:02 +0200331 if (!status->chains)
332 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
333
334 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
335 it_present_val |=
336 BIT(IEEE80211_RADIOTAP_EXT) |
337 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
338 put_unaligned_le32(it_present_val, it_present);
339 it_present++;
340 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
341 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
342 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200343
Johannes Berg1f7bba72014-11-06 22:56:36 +0100344 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
345 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
346 BIT(IEEE80211_RADIOTAP_EXT);
347 put_unaligned_le32(it_present_val, it_present);
348 it_present++;
349 it_present_val = rtap.present;
350 }
351
Johannes Berga144f372013-07-03 13:34:02 +0200352 put_unaligned_le32(it_present_val, it_present);
353
354 pos = (void *)(it_present + 1);
355
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200356 /* the order of the following fields is important */
357
358 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800359 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100360 /* padding */
361 while ((pos - (u8 *)rthdr) & 7)
362 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800363 put_unaligned_le64(
364 ieee80211_calculate_rx_timestamp(local, status,
365 mpdulen, 0),
366 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200367 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200368 pos += 8;
369 }
370
371 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200372 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200373 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100374 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
375 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200376 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200377 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200378 pos++;
379
380 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergda6a4352017-04-26 12:14:59 +0200381 if (!rate || status->encoding != RX_ENC_LEGACY) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200382 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100383 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530384 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200385 * added below. The byte here is needed as padding
386 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200387 */
388 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200389 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200390 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200391 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Johannes Bergda6a4352017-04-26 12:14:59 +0200392 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200393 shift = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200394 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200395 shift = 2;
396 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200397 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200398 pos++;
399
400 /* IEEE80211_RADIOTAP_CHANNEL */
Thomas Pedersen3b23c1842020-04-01 18:18:05 -0700401 /* TODO: frequency offset in KHz */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100402 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200403 pos += 2;
Johannes Bergda6a4352017-04-26 12:14:59 +0200404 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200405 channel_flags |= IEEE80211_CHAN_HALF;
Johannes Bergda6a4352017-04-26 12:14:59 +0200406 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200407 channel_flags |= IEEE80211_CHAN_QUARTER;
408
Aloka Dixit412a84b2020-09-11 01:11:35 +0000409 if (status->band == NL80211_BAND_5GHZ ||
410 status->band == NL80211_BAND_6GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200411 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Bergda6a4352017-04-26 12:14:59 +0200412 else if (status->encoding != RX_ENC_LEGACY)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200413 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100414 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200415 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100416 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100417 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100418 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200419 channel_flags |= IEEE80211_CHAN_2GHZ;
420 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200421 pos += 2;
422
423 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200424 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100425 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200426 *pos = status->signal;
427 rthdr->it_present |=
428 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
429 pos++;
430 }
431
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200432 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
433
Johannes Berga144f372013-07-03 13:34:02 +0200434 if (!status->chains) {
435 /* IEEE80211_RADIOTAP_ANTENNA */
436 *pos = status->antenna;
437 pos++;
438 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200439
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200440 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
441
442 /* IEEE80211_RADIOTAP_RX_FLAGS */
443 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100444 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100445 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100446 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100447 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
448 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200449 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100450
Johannes Bergda6a4352017-04-26 12:14:59 +0200451 if (status->encoding == RX_ENC_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200452 unsigned int stbc;
453
Johannes Berg6d744ba2011-01-27 14:13:17 +0100454 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200455 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100456 *pos = 0;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200457 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100458 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
Johannes Bergda6a4352017-04-26 12:14:59 +0200459 if (status->bw == RATE_INFO_BW_40)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100460 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200461 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
Johannes Bergac55d2f2012-05-10 09:09:10 +0200462 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200463 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200464 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200465 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200466 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100467 pos++;
468 *pos++ = status->rate_idx;
469 }
Johannes Berg4c298672012-07-05 11:34:31 +0200470
471 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
472 u16 flags = 0;
473
474 /* ensure 4 byte alignment */
475 while ((pos - (u8 *)rthdr) & 3)
476 pos++;
477 rthdr->it_present |=
478 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
479 put_unaligned_le32(status->ampdu_reference, pos);
480 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200481 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
482 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
483 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
484 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
485 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
486 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
487 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
488 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
Johannes Berg7299d6f2018-02-19 14:48:39 +0200489 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
490 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
491 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
492 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
Johannes Berg4c298672012-07-05 11:34:31 +0200493 put_unaligned_le16(flags, pos);
494 pos += 2;
495 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
496 *pos++ = status->ampdu_delimiter_crc;
497 else
498 *pos++ = 0;
499 *pos++ = 0;
500 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100501
Johannes Bergda6a4352017-04-26 12:14:59 +0200502 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100503 u16 known = local->hw.radiotap_vht_details;
504
505 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100506 put_unaligned_le16(known, pos);
507 pos += 2;
508 /* flags */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200509 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg51648922012-11-22 23:00:18 +0100510 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200511 /* in VHT, STBC is binary */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200512 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200513 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200514 if (status->enc_flags & RX_ENC_FLAG_BF)
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200515 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100516 pos++;
517 /* bandwidth */
Johannes Bergda6a4352017-04-26 12:14:59 +0200518 switch (status->bw) {
519 case RATE_INFO_BW_80:
Johannes Berg51648922012-11-22 23:00:18 +0100520 *pos++ = 4;
Johannes Bergda6a4352017-04-26 12:14:59 +0200521 break;
522 case RATE_INFO_BW_160:
Johannes Berg51648922012-11-22 23:00:18 +0100523 *pos++ = 11;
Johannes Bergda6a4352017-04-26 12:14:59 +0200524 break;
525 case RATE_INFO_BW_40:
Johannes Berg51648922012-11-22 23:00:18 +0100526 *pos++ = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200527 break;
528 default:
Johannes Berg51648922012-11-22 23:00:18 +0100529 *pos++ = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200530 }
Johannes Berg51648922012-11-22 23:00:18 +0100531 /* MCS/NSS */
Johannes Berg8613c942017-04-26 13:51:41 +0200532 *pos = (status->rate_idx << 4) | status->nss;
Johannes Berg51648922012-11-22 23:00:18 +0100533 pos += 4;
534 /* coding field */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200535 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200536 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100537 pos++;
538 /* group ID */
539 pos++;
540 /* partial_aid */
541 pos += 2;
542 }
543
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300544 if (local->hw.radiotap_timestamp.units_pos >= 0) {
545 u16 accuracy = 0;
546 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
547
548 rthdr->it_present |=
549 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
550
551 /* ensure 8 byte alignment */
552 while ((pos - (u8 *)rthdr) & 7)
553 pos++;
554
555 put_unaligned_le64(status->device_timestamp, pos);
556 pos += sizeof(u64);
557
558 if (local->hw.radiotap_timestamp.accuracy >= 0) {
559 accuracy = local->hw.radiotap_timestamp.accuracy;
560 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
561 }
562 put_unaligned_le16(accuracy, pos);
563 pos += sizeof(u16);
564
565 *pos++ = local->hw.radiotap_timestamp.units_pos;
566 *pos++ = flags;
567 }
568
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300569 if (status->encoding == RX_ENC_HE &&
570 status->flag & RX_FLAG_RADIOTAP_HE) {
Johannes Berg331aead2018-08-31 11:31:07 +0300571#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300572
573 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
574 he.data6 |= HE_PREP(DATA6_NSTS,
575 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
576 status->enc_flags));
577 he.data3 |= HE_PREP(DATA3_STBC, 1);
578 } else {
579 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
580 }
581
582#define CHECK_GI(s) \
583 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
584 (int)NL80211_RATE_INFO_HE_GI_##s)
585
586 CHECK_GI(0_8);
587 CHECK_GI(1_6);
588 CHECK_GI(3_2);
589
590 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
591 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
592 he.data3 |= HE_PREP(DATA3_CODING,
593 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
594
595 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
596
597 switch (status->bw) {
598 case RATE_INFO_BW_20:
599 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
600 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
601 break;
602 case RATE_INFO_BW_40:
603 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
604 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
605 break;
606 case RATE_INFO_BW_80:
607 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
608 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
609 break;
610 case RATE_INFO_BW_160:
611 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
612 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
613 break;
614 case RATE_INFO_BW_HE_RU:
615#define CHECK_RU_ALLOC(s) \
616 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
617 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
618
619 CHECK_RU_ALLOC(26);
620 CHECK_RU_ALLOC(52);
621 CHECK_RU_ALLOC(106);
622 CHECK_RU_ALLOC(242);
623 CHECK_RU_ALLOC(484);
624 CHECK_RU_ALLOC(996);
625 CHECK_RU_ALLOC(2x996);
626
627 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
628 status->he_ru + 4);
629 break;
630 default:
631 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
632 }
633
634 /* ensure 2 byte alignment */
635 while ((pos - (u8 *)rthdr) & 1)
636 pos++;
637 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
638 memcpy(pos, &he, sizeof(he));
639 pos += sizeof(he);
640 }
641
642 if (status->encoding == RX_ENC_HE &&
643 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
644 /* ensure 2 byte alignment */
645 while ((pos - (u8 *)rthdr) & 1)
646 pos++;
647 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
648 memcpy(pos, &he_mu, sizeof(he_mu));
649 pos += sizeof(he_mu);
650 }
651
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300652 if (status->flag & RX_FLAG_NO_PSDU) {
653 rthdr->it_present |=
654 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
655 *pos++ = status->zero_length_psdu_type;
656 }
657
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300658 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
659 /* ensure 2 byte alignment */
660 while ((pos - (u8 *)rthdr) & 1)
661 pos++;
662 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
663 memcpy(pos, &lsig, sizeof(lsig));
664 pos += sizeof(lsig);
665 }
666
Johannes Berga144f372013-07-03 13:34:02 +0200667 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
668 *pos++ = status->chain_signal[chain];
669 *pos++ = chain;
670 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100671
672 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
673 /* ensure 2 byte alignment for the vendor field as required */
674 if ((pos - (u8 *)rthdr) & 1)
675 *pos++ = 0;
676 *pos++ = rtap.oui[0];
677 *pos++ = rtap.oui[1];
678 *pos++ = rtap.oui[2];
679 *pos++ = rtap.subns;
680 put_unaligned_le16(rtap.len, pos);
681 pos += 2;
682 /* align the actual payload as requested */
683 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
684 *pos++ = 0;
685 /* data (and possible padding) already follows */
686 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200687}
688
Johannes Berg127f60b2017-04-13 15:50:27 +0200689static struct sk_buff *
690ieee80211_make_monitor_skb(struct ieee80211_local *local,
691 struct sk_buff **origskb,
692 struct ieee80211_rate *rate,
Johannes Bergc096b922018-04-20 13:49:18 +0300693 int rtap_space, bool use_origskb)
Johannes Berg127f60b2017-04-13 15:50:27 +0200694{
695 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
696 int rt_hdrlen, needed_headroom;
697 struct sk_buff *skb;
698
699 /* room for the radiotap header based on driver features */
700 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
Johannes Bergc096b922018-04-20 13:49:18 +0300701 needed_headroom = rt_hdrlen - rtap_space;
Johannes Berg127f60b2017-04-13 15:50:27 +0200702
703 if (use_origskb) {
704 /* only need to expand headroom if necessary */
705 skb = *origskb;
706 *origskb = NULL;
707
708 /*
709 * This shouldn't trigger often because most devices have an
710 * RX header they pull before we get here, and that should
711 * be big enough for our radiotap information. We should
712 * probably export the length to drivers so that we can have
713 * them allocate enough headroom to start with.
714 */
715 if (skb_headroom(skb) < needed_headroom &&
716 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
717 dev_kfree_skb(skb);
718 return NULL;
719 }
720 } else {
721 /*
722 * Need to make a copy and possibly remove radiotap header
723 * and FCS from the original.
724 */
725 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
726
727 if (!skb)
728 return NULL;
729 }
730
731 /* prepend radiotap information */
732 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
733
734 skb_reset_mac_header(skb);
735 skb->ip_summed = CHECKSUM_UNNECESSARY;
736 skb->pkt_type = PACKET_OTHERHOST;
737 skb->protocol = htons(ETH_P_802_2);
738
739 return skb;
740}
741
Johannes Bergb2e77712007-09-26 15:19:39 +0200742/*
743 * This function copies a received frame to all monitor interfaces and
744 * returns a cleaned-up SKB that no longer includes the FCS nor the
745 * radiotap header the driver might have added.
746 */
747static struct sk_buff *
748ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100749 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200750{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200751 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200752 struct ieee80211_sub_if_data *sdata;
Johannes Berg127f60b2017-04-13 15:50:27 +0200753 struct sk_buff *monskb = NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200754 int present_fcs_len = 0;
Johannes Bergc096b922018-04-20 13:49:18 +0300755 unsigned int rtap_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300756 struct ieee80211_sub_if_data *monitor_sdata =
757 rcu_dereference(local->monitor_sdata);
Johannes Berg127f60b2017-04-13 15:50:27 +0200758 bool only_monitor = false;
Ilan Peer80209192018-12-15 11:03:17 +0200759 unsigned int min_head_len;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100760
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300761 if (status->flag & RX_FLAG_RADIOTAP_HE)
762 rtap_space += sizeof(struct ieee80211_radiotap_he);
763
764 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
765 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
766
Ilan Peerd359bbc2018-12-15 11:03:25 +0200767 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
768 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
769
Johannes Berg1f7bba72014-11-06 22:56:36 +0100770 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
Ilan Peerd359bbc2018-12-15 11:03:25 +0200771 struct ieee80211_vendor_radiotap *rtap =
772 (void *)(origskb->data + rtap_space);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100773
Johannes Bergc096b922018-04-20 13:49:18 +0300774 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100775 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200776
Ilan Peer80209192018-12-15 11:03:17 +0200777 min_head_len = rtap_space;
778
Johannes Bergb2e77712007-09-26 15:19:39 +0200779 /*
780 * First, we may need to make a copy of the skb because
781 * (1) we need to modify it for radiotap (if not present), and
782 * (2) the other RX handlers will modify the skb we got.
783 *
784 * We don't need to, of course, if we aren't going to return
785 * the SKB because it has a bad FCS/PLCP checksum.
786 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100787
Ilan Peer80209192018-12-15 11:03:17 +0200788 if (!(status->flag & RX_FLAG_NO_PSDU)) {
789 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
790 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
791 /* driver bug */
792 WARN_ON(1);
793 dev_kfree_skb(origskb);
794 return NULL;
795 }
796 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200797 }
Ilan Peer80209192018-12-15 11:03:17 +0200798
799 /* also consider the hdr->frame_control */
800 min_head_len += 2;
Johannes Berg30841f52017-04-11 15:38:56 +0200801 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200802
Ilan Peer80209192018-12-15 11:03:17 +0200803 /* ensure that the expected data elements are in skb head */
804 if (!pskb_may_pull(origskb, min_head_len)) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800805 dev_kfree_skb(origskb);
806 return NULL;
807 }
808
Johannes Bergc096b922018-04-20 13:49:18 +0300809 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
Johannes Berg127f60b2017-04-13 15:50:27 +0200810
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100811 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200812 if (only_monitor) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200813 dev_kfree_skb(origskb);
814 return NULL;
815 }
816
Johannes Bergc1129922020-05-26 14:33:48 +0200817 return ieee80211_clean_skb(origskb, present_fcs_len,
818 rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200819 }
820
Johannes Bergc096b922018-04-20 13:49:18 +0300821 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
Johannes Berg9e478062017-04-13 14:23:49 +0200822
Johannes Bergf64331d2017-04-13 13:28:18 +0200823 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200824 bool last_monitor = list_is_last(&sdata->u.mntr.list,
825 &local->mon_list);
826
827 if (!monskb)
828 monskb = ieee80211_make_monitor_skb(local, &origskb,
Johannes Bergc096b922018-04-20 13:49:18 +0300829 rate, rtap_space,
Johannes Berg127f60b2017-04-13 15:50:27 +0200830 only_monitor &&
831 last_monitor);
832
833 if (monskb) {
834 struct sk_buff *skb;
835
836 if (last_monitor) {
837 skb = monskb;
838 monskb = NULL;
839 } else {
840 skb = skb_clone(monskb, GFP_ATOMIC);
841 }
842
843 if (skb) {
844 skb->dev = sdata->dev;
845 ieee80211_rx_stats(skb->dev, skb->len);
846 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200847 }
848 }
849
Johannes Berg127f60b2017-04-13 15:50:27 +0200850 if (last_monitor)
851 break;
Johannes Bergb2e77712007-09-26 15:19:39 +0200852 }
853
Johannes Berg127f60b2017-04-13 15:50:27 +0200854 /* this happens if last_monitor was erroneously false */
855 dev_kfree_skb(monskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200856
Johannes Berg127f60b2017-04-13 15:50:27 +0200857 /* ditto */
858 if (!origskb)
859 return NULL;
860
Johannes Bergc1129922020-05-26 14:33:48 +0200861 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200862}
863
Johannes Berg5cf121c2008-02-25 16:27:43 +0100864static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200865{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200867 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200868 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200869
870 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700871 if (ieee80211_is_data_qos(hdr->frame_control)) {
872 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200873 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700874 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200875 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200876 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200877
878 seqno_idx = tid;
879 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200880 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200881 /*
882 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
883 *
884 * Sequence numbers for management frames, QoS data
885 * frames with a broadcast/multicast address in the
886 * Address 1 field, and all non-QoS data frames sent
887 * by QoS STAs are assigned using an additional single
888 * modulo-4096 counter, [...]
889 *
890 * We also use that counter for non-QoS STAs.
891 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100892 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200893 security_idx = 0;
894 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100895 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200896 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200897 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200898
Johannes Berg9e262972011-07-07 18:45:03 +0200899 rx->seqno_idx = seqno_idx;
900 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200901 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
902 * For now, set skb->priority to 0 for other cases. */
903 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100904}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200905
Johannes Bergd1c3a372009-01-07 00:26:10 +0100906/**
907 * DOC: Packet alignment
908 *
909 * Drivers always need to pass packets that are aligned to two-byte boundaries
910 * to the stack.
911 *
912 * Additionally, should, if possible, align the payload data in a way that
913 * guarantees that the contained IP header is aligned to a four-byte
914 * boundary. In the case of regular frames, this simply means aligning the
915 * payload to a four-byte boundary (because either the IP header is directly
916 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100917 * in front of it). If the payload data is not properly aligned and the
918 * architecture doesn't support efficient unaligned operations, mac80211
919 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100920 *
921 * With A-MSDU frames, however, the payload data address must yield two modulo
922 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
923 * push the IP header further back to a multiple of four again. Thankfully, the
924 * specs were sane enough this time around to require padding each A-MSDU
925 * subframe to a length that is a multiple of four.
926 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300927 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100928 * the payload is not supported, the driver is required to move the 802.11
929 * header to be directly in front of the payload in that case.
930 */
931static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100932{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100933#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100934 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100935#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200936}
937
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200938
Johannes Berg571ecf62007-07-27 15:43:22 +0200939/* rx handlers */
940
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200941static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
942{
943 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
944
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100945 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200946 return 0;
947
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100948 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200949}
950
951
952static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
953{
954 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
955
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100956 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200957 return 0;
958
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100959 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200960}
961
962
963/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
964static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
965{
966 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
967 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200968 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200969
Johannes Berg1df332e2012-10-26 00:09:11 +0200970 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200971 return -1;
972
Jouni Malinenaf2d14b2020-02-22 15:25:47 +0200973 if (!ieee80211_is_robust_mgmt_frame(skb) &&
974 !ieee80211_is_beacon(hdr->frame_control))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200975 return -1; /* not a robust management frame */
976
977 mmie = (struct ieee80211_mmie *)
978 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200979 if (mmie->element_id == WLAN_EID_MMIE &&
980 mmie->length == sizeof(*mmie) - 2)
981 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200982
Jouni Malinen56c52da2015-01-24 19:52:08 +0200983 mmie16 = (struct ieee80211_mmie_16 *)
984 (skb->data + skb->len - sizeof(*mmie16));
985 if (skb->len >= 24 + sizeof(*mmie16) &&
986 mmie16->element_id == WLAN_EID_MMIE &&
987 mmie16->length == sizeof(*mmie16) - 2)
988 return le16_to_cpu(mmie16->key_id);
989
990 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200991}
992
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100993static int ieee80211_get_keyid(struct sk_buff *skb,
994 const struct ieee80211_cipher_scheme *cs)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200995{
996 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
997 __le16 fc;
998 int hdrlen;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +0100999 int minlen;
1000 u8 key_idx_off;
1001 u8 key_idx_shift;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001002 u8 keyid;
1003
1004 fc = hdr->frame_control;
1005 hdrlen = ieee80211_hdrlen(fc);
1006
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001007 if (cs) {
1008 minlen = hdrlen + cs->hdr_len;
1009 key_idx_off = hdrlen + cs->key_idx_off;
1010 key_idx_shift = cs->key_idx_shift;
1011 } else {
1012 /* WEP, TKIP, CCMP and GCMP */
1013 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1014 key_idx_off = hdrlen + 3;
1015 key_idx_shift = 6;
1016 }
1017
1018 if (unlikely(skb->len < minlen))
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001019 return -EINVAL;
1020
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001021 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1022
1023 if (cs)
1024 keyid &= cs->key_idx_mask;
1025 keyid >>= key_idx_shift;
1026
1027 /* cs could use more than the usual two bits for the keyid */
1028 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1029 return -EINVAL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001030
1031 return keyid;
1032}
1033
Johannes Berg1df332e2012-10-26 00:09:11 +02001034static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001035{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001036 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +01001037 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001038
Harvey Harrisona7767f92008-07-02 16:30:51 -07001039 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001040 if (is_multicast_ether_addr(hdr->addr1)) {
1041 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +02001042 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001043 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001044 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001045 return RX_DROP_MONITOR;
1046 } else {
1047 if (!ieee80211_has_a4(hdr->frame_control))
1048 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001049 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001050 return RX_DROP_MONITOR;
1051 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001052 }
1053
1054 /* If there is not an established peer link and this is not a peer link
1055 * establisment frame, beacon or probe, drop the frame.
1056 */
1057
Javier Cardona57cf8042011-05-13 10:45:43 -07001058 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001059 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001060
Harvey Harrisona7767f92008-07-02 16:30:51 -07001061 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001062 return RX_DROP_MONITOR;
1063
Harvey Harrisona7767f92008-07-02 16:30:51 -07001064 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001065 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001066
1067 /* make sure category field is present */
1068 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1069 return RX_DROP_MONITOR;
1070
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001071 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001072 category = mgmt->u.action.category;
1073 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +02001074 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001075 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001076 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001077 }
1078
Harvey Harrisona7767f92008-07-02 16:30:51 -07001079 if (ieee80211_is_probe_req(hdr->frame_control) ||
1080 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -07001081 ieee80211_is_beacon(hdr->frame_control) ||
1082 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -07001083 return RX_CONTINUE;
1084
1085 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001086 }
1087
Johannes Berg902acc72008-02-23 15:17:19 +01001088 return RX_CONTINUE;
1089}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001090
Johannes Bergfb4ea052016-01-28 16:19:24 +02001091static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1092 int index)
1093{
1094 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1095 struct sk_buff *tail = skb_peek_tail(frames);
1096 struct ieee80211_rx_status *status;
1097
Sara Sharon06470f72016-01-28 16:19:25 +02001098 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1099 return true;
1100
Johannes Bergfb4ea052016-01-28 16:19:24 +02001101 if (!tail)
1102 return false;
1103
1104 status = IEEE80211_SKB_RXCB(tail);
1105 if (status->flag & RX_FLAG_AMSDU_MORE)
1106 return false;
1107
1108 return true;
1109}
1110
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001111static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001112 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001113 int index,
1114 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001115{
Michal Kazior83eb9352014-07-16 12:09:31 +02001116 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1117 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +01001118 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001119
Johannes Bergdd318572010-11-29 11:09:16 +01001120 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1121
Michal Kazior83eb9352014-07-16 12:09:31 +02001122 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001123 goto no_frame;
1124
Johannes Bergfb4ea052016-01-28 16:19:24 +02001125 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001126 __skb_queue_purge(skb_list);
1127 goto no_frame;
1128 }
1129
1130 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001131 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +02001132 while ((skb = __skb_dequeue(skb_list))) {
1133 status = IEEE80211_SKB_RXCB(skb);
1134 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1135 __skb_queue_tail(frames, skb);
1136 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001137
1138no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +02001139 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +01001140 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001141}
1142
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001143static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001144 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001145 u16 head_seq_num,
1146 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001147{
1148 int index;
1149
Johannes Bergdd318572010-11-29 11:09:16 +01001150 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1151
Johannes Berg9a886582013-02-15 19:25:00 +01001152 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001153 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001154 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1155 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001156 }
1157}
1158
1159/*
1160 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1161 * the skb was added to the buffer longer than this time ago, the earlier
1162 * frames that have not yet been received are assumed to be lost and the skb
1163 * can be released for processing. This may also release other skb's from the
1164 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001165 *
1166 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001167 */
1168#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1169
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001170static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001171 struct tid_ampdu_rx *tid_agg_rx,
1172 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001173{
Michal Kazior83eb9352014-07-16 12:09:31 +02001174 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001175
Johannes Bergdd318572010-11-29 11:09:16 +01001176 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1177
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001178 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +02001179 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +02001180 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +02001181 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001182 /*
1183 * No buffers ready to be released, but check whether any
1184 * frames in the reorder buffer have timed out.
1185 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001186 int skipped = 1;
1187 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1188 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001189 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001190 skipped++;
1191 continue;
1192 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -07001193 if (skipped &&
1194 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001195 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001196 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001197
Michal Kazior83eb9352014-07-16 12:09:31 +02001198 /* don't leave incomplete A-MSDUs around */
1199 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1200 i = (i + 1) % tid_agg_rx->buf_size)
1201 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1202
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001203 ht_dbg_ratelimited(sdata,
1204 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001205 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1206 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001207
1208 /*
1209 * Increment the head seq# also for the skipped slots.
1210 */
1211 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +01001212 (tid_agg_rx->head_seq_num +
1213 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001214 skipped = 0;
1215 }
Johannes Bergfb4ea052016-01-28 16:19:24 +02001216 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001217 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1218 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +02001219 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001220 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001221
1222 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001223 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001224
1225 for (; j != (index - 1) % tid_agg_rx->buf_size;
1226 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001227 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001228 break;
1229 }
1230
1231 set_release_timer:
1232
Johannes Berg788211d2015-04-01 14:20:42 +02001233 if (!tid_agg_rx->removed)
1234 mod_timer(&tid_agg_rx->reorder_timer,
1235 tid_agg_rx->reorder_time[j] + 1 +
1236 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001237 } else {
1238 del_timer(&tid_agg_rx->reorder_timer);
1239 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001240}
1241
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001242/*
1243 * As this function belongs to the RX path it must be under
1244 * rcu_read_lock protection. It returns false if the frame
1245 * can be processed immediately, true if it was consumed.
1246 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001247static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001248 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001249 struct sk_buff *skb,
1250 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001251{
1252 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001253 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001254 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1255 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1256 u16 head_seq_num, buf_size;
1257 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001258 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001259
Johannes Bergdd318572010-11-29 11:09:16 +01001260 spin_lock(&tid_agg_rx->reorder_lock);
1261
Michal Kazior4549cf22014-09-02 14:05:10 +02001262 /*
1263 * Offloaded BA sessions have no known starting sequence number so pick
1264 * one from first Rxed frame for this tid after BA was started.
1265 */
1266 if (unlikely(tid_agg_rx->auto_seq)) {
1267 tid_agg_rx->auto_seq = false;
1268 tid_agg_rx->ssn = mpdu_seq_num;
1269 tid_agg_rx->head_seq_num = mpdu_seq_num;
1270 }
1271
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001272 buf_size = tid_agg_rx->buf_size;
1273 head_seq_num = tid_agg_rx->head_seq_num;
1274
Sara Sharonb7540d82017-02-06 15:28:42 +02001275 /*
1276 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1277 * be reordered.
1278 */
1279 if (unlikely(!tid_agg_rx->started)) {
1280 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1281 ret = false;
1282 goto out;
1283 }
1284 tid_agg_rx->started = true;
1285 }
1286
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001287 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001288 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001289 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001290 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001291 }
1292
1293 /*
1294 * If frame the sequence number exceeds our buffering window
1295 * size release some previous frames to make room for this one.
1296 */
Johannes Berg9a886582013-02-15 19:25:00 +01001297 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1298 head_seq_num = ieee80211_sn_inc(
1299 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001300 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001301 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001302 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001303 }
1304
1305 /* Now the new frame is always in the range of the reordering buffer */
1306
Karl Beldan2e3049b2013-10-24 15:53:32 +02001307 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001308
1309 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001310 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001311 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001312 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001313 }
1314
1315 /*
1316 * If the current MPDU is in the right order and nothing else
1317 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001318 * If it is first but there's something stored, we may be able
1319 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001320 */
1321 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1322 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001323 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1324 tid_agg_rx->head_seq_num =
1325 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001326 ret = false;
1327 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001328 }
1329
1330 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001331 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1332 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1333 tid_agg_rx->reorder_time[index] = jiffies;
1334 tid_agg_rx->stored_mpdu_num++;
1335 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1336 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001337
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001338 out:
1339 spin_unlock(&tid_agg_rx->reorder_lock);
1340 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001341}
1342
1343/*
1344 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1345 * true if the MPDU was buffered, false if it should be processed.
1346 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001347static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1348 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001349{
Johannes Berg2569a822009-11-25 17:46:17 +01001350 struct sk_buff *skb = rx->skb;
1351 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001352 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001353 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001354 struct tid_ampdu_rx *tid_agg_rx;
1355 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001356 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001357
Johannes Berg051a41fa2013-11-20 11:28:27 +01001358 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1359 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001360 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001361
1362 /*
1363 * filter the QoS data rx stream according to
1364 * STA/TID and check if this STA/TID is on aggregation
1365 */
1366
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001367 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001368 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001369
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001370 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1371 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001372 tid = ieee80211_get_tid(hdr);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001373
Johannes Berga87f7362010-06-10 10:21:38 +02001374 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001375 if (!tid_agg_rx) {
1376 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1377 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1378 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1379 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1380 WLAN_BACK_RECIPIENT,
1381 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001382 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001383 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001384
1385 /* qos null data frames are excluded */
1386 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001387 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001388
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001389 /* not part of a BA session */
1390 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1391 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1392 goto dont_reorder;
1393
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001394 /* new, potentially un-ordered, ampdu frame - process it */
1395
1396 /* reset session timer */
1397 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001398 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001399
1400 /* if this mpdu is fragmented - terminate rx aggregation session */
1401 sc = le16_to_cpu(hdr->seq_ctrl);
1402 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg344eec62010-06-10 10:21:36 +02001403 skb_queue_tail(&rx->sdata->skb_queue, skb);
1404 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001405 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001406 }
1407
Johannes Berga87f7362010-06-10 10:21:38 +02001408 /*
1409 * No locking needed -- we will only ever process one
1410 * RX packet at a time, and thus own tid_agg_rx. All
1411 * other code manipulating it needs to (and does) make
1412 * sure that we cannot get to it any more before doing
1413 * anything with it.
1414 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001415 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1416 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001417 return;
1418
1419 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001420 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001421}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001422
Johannes Berg49461622008-06-30 15:10:45 +02001423static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001424ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001425{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001426 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001427 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001428
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001429 if (status->flag & RX_FLAG_DUP_VALIDATED)
1430 return RX_CONTINUE;
1431
Johannes Berg6b0f3272013-07-11 22:33:26 +02001432 /*
1433 * Drop duplicate 802.11 retransmissions
1434 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1435 */
Johannes Berg03954422014-11-11 16:49:25 +01001436
1437 if (rx->skb->len < 24)
1438 return RX_CONTINUE;
1439
1440 if (ieee80211_is_ctl(hdr->frame_control) ||
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001441 ieee80211_is_any_nullfunc(hdr->frame_control) ||
Johannes Berg03954422014-11-11 16:49:25 +01001442 is_multicast_ether_addr(hdr->addr1))
1443 return RX_CONTINUE;
1444
Johannes Berga732fa72015-10-14 18:27:07 +02001445 if (!rx->sta)
1446 return RX_CONTINUE;
1447
1448 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1449 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1450 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001451 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001452 return RX_DROP_UNUSABLE;
1453 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1454 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001455 }
1456
Johannes Berg03954422014-11-11 16:49:25 +01001457 return RX_CONTINUE;
1458}
1459
1460static ieee80211_rx_result debug_noinline
1461ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1462{
1463 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1464
Johannes Berg571ecf62007-07-27 15:43:22 +02001465 /* Drop disallowed frame classes based on STA auth/assoc state;
1466 * IEEE 802.11, Chap 5.5.
1467 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001468 * mac80211 filters only based on association state, i.e. it drops
1469 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001470 * deauth/disassoc frames when needed. In addition, hostapd is
1471 * responsible for filtering on both auth and assoc states.
1472 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001473
Johannes Berg902acc72008-02-23 15:17:19 +01001474 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001475 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001476
Harvey Harrisona7767f92008-07-02 16:30:51 -07001477 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1478 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001479 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Bill Jordan1be7fe82010-10-01 11:20:41 -04001480 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001481 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001482 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001483 /*
1484 * accept port control frames from the AP even when it's not
1485 * yet marked ASSOC to prevent a race where we don't set the
1486 * assoc bit quickly enough before it sends the first frame
1487 */
1488 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001489 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001490 unsigned int hdrlen;
1491 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001492
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001493 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1494
1495 if (rx->skb->len < hdrlen + 8)
1496 return RX_DROP_MONITOR;
1497
1498 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1499 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001500 return RX_CONTINUE;
1501 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001502
1503 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1504 cfg80211_rx_spurious_frame(rx->sdata->dev,
1505 hdr->addr2,
1506 GFP_ATOMIC))
1507 return RX_DROP_UNUSABLE;
1508
Johannes Berge4c26ad2008-01-31 19:48:21 +01001509 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001510 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001511
Johannes Berg9ae54c82008-01-31 19:48:20 +01001512 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001513}
1514
1515
Johannes Berg49461622008-06-30 15:10:45 +02001516static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001517ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1518{
1519 struct ieee80211_local *local;
1520 struct ieee80211_hdr *hdr;
1521 struct sk_buff *skb;
1522
1523 local = rx->local;
1524 skb = rx->skb;
1525 hdr = (struct ieee80211_hdr *) skb->data;
1526
1527 if (!local->pspolling)
1528 return RX_CONTINUE;
1529
1530 if (!ieee80211_has_fromds(hdr->frame_control))
1531 /* this is not from AP */
1532 return RX_CONTINUE;
1533
1534 if (!ieee80211_is_data(hdr->frame_control))
1535 return RX_CONTINUE;
1536
1537 if (!ieee80211_has_moredata(hdr->frame_control)) {
1538 /* AP has no more frames buffered for us */
1539 local->pspolling = false;
1540 return RX_CONTINUE;
1541 }
1542
1543 /* more data bit is set, let's request a new frame from the AP */
1544 ieee80211_send_pspoll(local, rx->sdata);
1545
1546 return RX_CONTINUE;
1547}
1548
Marco Porschd012a602012-10-10 12:39:50 -07001549static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001550{
Johannes Berg133b8222008-09-16 14:18:59 +02001551 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001552 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001553 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001554 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001555
Marco Porschd012a602012-10-10 12:39:50 -07001556 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1557 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1558 ps = &sdata->bss->ps;
1559 else
1560 return;
1561
1562 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001563 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001564 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001565 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001566 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1567 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001568
Johannes Berg17c18bf2015-03-21 15:25:43 +01001569 ieee80211_clear_fast_xmit(sta);
1570
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001571 if (!sta->sta.txq[0])
1572 return;
1573
Johannes Bergadf8ed02018-08-31 11:31:08 +03001574 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001575 struct ieee80211_txq *txq = sta->sta.txq[tid];
1576 struct txq_info *txqi = to_txq_info(txq);
1577
1578 spin_lock(&local->active_txq_lock[txq->ac]);
1579 if (!list_empty(&txqi->schedule_order))
1580 list_del_init(&txqi->schedule_order);
1581 spin_unlock(&local->active_txq_lock[txq->ac]);
1582
1583 if (txq_has_queue(txq))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001584 set_bit(tid, &sta->txq_buffered_tids);
1585 else
1586 clear_bit(tid, &sta->txq_buffered_tids);
1587 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001588}
1589
Marco Porschd012a602012-10-10 12:39:50 -07001590static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001591{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001592 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1593 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001594
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001595 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001596 /*
1597 * Clear the flag only if the other one is still set
1598 * so that the TX path won't start TX'ing new frames
1599 * directly ... In the case that the driver flag isn't
1600 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1601 */
1602 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001603 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1604 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001605 return;
1606 }
1607
Johannes Berg5ac2e352014-05-27 16:32:27 +02001608 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1609 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001610 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001611}
1612
Johannes Bergcf471612015-06-16 16:16:38 +02001613int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001614{
Johannes Bergcf471612015-06-16 16:16:38 +02001615 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001616 bool in_ps;
1617
Johannes Bergcf471612015-06-16 16:16:38 +02001618 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001619
1620 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001621 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001622 if ((start && in_ps) || (!start && !in_ps))
1623 return -EINVAL;
1624
1625 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001626 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001627 else
Johannes Bergcf471612015-06-16 16:16:38 +02001628 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001629
1630 return 0;
1631}
1632EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1633
Johannes Berg46fa38e2016-05-03 16:58:00 +03001634void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1635{
1636 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1637
1638 if (test_sta_flag(sta, WLAN_STA_SP))
1639 return;
1640
1641 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1642 ieee80211_sta_ps_deliver_poll_response(sta);
1643 else
1644 set_sta_flag(sta, WLAN_STA_PSPOLL);
1645}
1646EXPORT_SYMBOL(ieee80211_sta_pspoll);
1647
1648void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1649{
1650 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001651 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001652
1653 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001654 * If this AC is not trigger-enabled do nothing unless the
1655 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001656 *
1657 * NB: This could/should check a separate bitmap of trigger-
1658 * enabled queues, but for now we only implement uAPSD w/o
1659 * TSPEC changes to the ACs, so they're always the same.
1660 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001661 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1662 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001663 return;
1664
1665 /* if we are in a service period, do nothing */
1666 if (test_sta_flag(sta, WLAN_STA_SP))
1667 return;
1668
1669 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1670 ieee80211_sta_ps_deliver_uapsd(sta);
1671 else
1672 set_sta_flag(sta, WLAN_STA_UAPSD);
1673}
1674EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1675
Johannes Berg49461622008-06-30 15:10:45 +02001676static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001677ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1678{
1679 struct ieee80211_sub_if_data *sdata = rx->sdata;
1680 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1681 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001682
Johannes Berg5c900672015-04-22 14:48:34 +02001683 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001684 return RX_CONTINUE;
1685
1686 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1687 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1688 return RX_CONTINUE;
1689
1690 /*
1691 * The device handles station powersave, so don't do anything about
1692 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1693 * it to mac80211 since they're handled.)
1694 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001695 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001696 return RX_CONTINUE;
1697
1698 /*
1699 * Don't do anything if the station isn't already asleep. In
1700 * the uAPSD case, the station will probably be marked asleep,
1701 * in the PS-Poll case the station must be confused ...
1702 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001703 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001704 return RX_CONTINUE;
1705
1706 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001707 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001708
1709 /* Free PS Poll skb here instead of returning RX_DROP that would
1710 * count as an dropped frame. */
1711 dev_kfree_skb(rx->skb);
1712
1713 return RX_QUEUED;
1714 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1715 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1716 ieee80211_has_pm(hdr->frame_control) &&
1717 (ieee80211_is_data_qos(hdr->frame_control) ||
1718 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001719 u8 tid = ieee80211_get_tid(hdr);
Johannes Berg47086fc2011-09-29 16:04:33 +02001720
Johannes Berg46fa38e2016-05-03 16:58:00 +03001721 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001722 }
1723
1724 return RX_CONTINUE;
1725}
1726
1727static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001728ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001729{
1730 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001731 struct sk_buff *skb = rx->skb;
1732 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1733 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001734 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001735
1736 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001737 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001738
Johannes Bergb291ba12009-07-10 15:29:03 +02001739 /*
1740 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001741 * BSSID and for station already AUTHORIZED to avoid keeping the
1742 * current IBSS network alive in cases where other STAs start
1743 * using different BSSID. This will also give the station another
1744 * chance to restart the authentication/authorization in case
1745 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001746 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001747 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001748 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001749 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001750 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1751 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001752 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001753 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001754 !is_multicast_ether_addr(hdr->addr1))
1755 sta->rx_stats.last_rate =
1756 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001757 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001758 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001759 sta->rx_stats.last_rx = jiffies;
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001760 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1761 is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb291ba12009-07-10 15:29:03 +02001762 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001763 * Mesh beacons will update last_rx when if they are found to
1764 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001765 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001766 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001767 if (ieee80211_is_data(hdr->frame_control))
1768 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001769 }
1770
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001771 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001772
1773 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001774 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001775 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1776
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001777 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001778 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001779 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001780 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001781
Felix Fietkauef0621e2013-04-22 16:29:31 +02001782 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001783 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001784 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1785 int signal = status->chain_signal[i];
1786
1787 if (!(status->chains & BIT(i)))
1788 continue;
1789
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001790 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001791 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001792 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001793 }
1794 }
1795
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001796 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1797 return RX_CONTINUE;
1798
Johannes Berg72eaa432008-11-26 15:02:58 +01001799 /*
1800 * Change STA power saving mode only at the end of a frame
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001801 * exchange sequence, and only for a data or management
1802 * frame as specified in IEEE 802.11-2016 11.2.3.2
Johannes Berg72eaa432008-11-26 15:02:58 +01001803 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001804 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001805 !ieee80211_has_morefrags(hdr->frame_control) &&
Emmanuel Grumbach209327502018-08-20 13:56:07 +03001806 !is_multicast_ether_addr(hdr->addr1) &&
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001807 (ieee80211_is_mgmt(hdr->frame_control) ||
1808 ieee80211_is_data(hdr->frame_control)) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001809 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001810 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001811 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001812 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001813 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001814 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001815 } else {
1816 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001817 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001818 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001819 }
1820
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001821 /* mesh power save support */
1822 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1823 ieee80211_mps_rx_h_sta_process(sta, hdr);
1824
Johannes Berg22403de2009-10-30 12:55:03 +01001825 /*
1826 * Drop (qos-)data::nullfunc frames silently, since they
1827 * are used only to control station power saving mode.
1828 */
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001829 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001830 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001831
1832 /*
1833 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001834 * that was not moved to a 4-addr STA vlan yet send
1835 * the event to userspace and for older hostapd drop
1836 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001837 */
1838 if (ieee80211_has_a4(hdr->frame_control) &&
1839 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1840 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001841 !rx->sdata->u.vlan.sta))) {
1842 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1843 cfg80211_rx_unexpected_4addr_frame(
1844 rx->sdata->dev, sta->sta.addr,
1845 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001846 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001847 }
Johannes Berg22403de2009-10-30 12:55:03 +01001848 /*
1849 * Update counter and free packet here to avoid
1850 * counting this as a dropped packed.
1851 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001852 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001853 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001854 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001855 }
1856
Johannes Berg9ae54c82008-01-31 19:48:20 +01001857 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001858} /* ieee80211_rx_h_sta_process */
1859
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001860static struct ieee80211_key *
1861ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1862{
1863 struct ieee80211_key *key = NULL;
1864 struct ieee80211_sub_if_data *sdata = rx->sdata;
1865 int idx2;
1866
1867 /* Make sure key gets set if either BIGTK key index is set so that
1868 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1869 * Beacon frames and Beacon frames that claim to use another BIGTK key
1870 * index (i.e., a key that we do not have).
1871 */
1872
1873 if (idx < 0) {
1874 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1875 idx2 = idx + 1;
1876 } else {
1877 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1878 idx2 = idx + 1;
1879 else
1880 idx2 = idx - 1;
1881 }
1882
1883 if (rx->sta)
1884 key = rcu_dereference(rx->sta->gtk[idx]);
1885 if (!key)
1886 key = rcu_dereference(sdata->keys[idx]);
1887 if (!key && rx->sta)
1888 key = rcu_dereference(rx->sta->gtk[idx2]);
1889 if (!key)
1890 key = rcu_dereference(sdata->keys[idx2]);
1891
1892 return key;
1893}
1894
Johan Almbladh86c228a2013-08-14 15:29:46 +02001895static ieee80211_rx_result debug_noinline
1896ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1897{
1898 struct sk_buff *skb = rx->skb;
1899 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1900 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1901 int keyidx;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001902 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1903 struct ieee80211_key *sta_ptk = NULL;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001904 struct ieee80211_key *ptk_idx = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001905 int mmie_keyidx = -1;
1906 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001907 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001908
Thomas Pedersen09a740c2020-09-21 19:28:14 -07001909 if (ieee80211_is_ext(hdr->frame_control))
1910 return RX_CONTINUE;
1911
Johan Almbladh86c228a2013-08-14 15:29:46 +02001912 /*
1913 * Key selection 101
1914 *
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001915 * There are five types of keys:
Johan Almbladh86c228a2013-08-14 15:29:46 +02001916 * - GTK (group keys)
1917 * - IGTK (group keys for management frames)
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001918 * - BIGTK (group keys for Beacon frames)
Johan Almbladh86c228a2013-08-14 15:29:46 +02001919 * - PTK (pairwise keys)
1920 * - STK (station-to-station pairwise keys)
1921 *
1922 * When selecting a key, we have to distinguish between multicast
1923 * (including broadcast) and unicast frames, the latter can only
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001924 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1925 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1926 * then unicast frames can also use key indices like GTKs. Hence, if we
Johan Almbladh86c228a2013-08-14 15:29:46 +02001927 * don't have a PTK/STK we check the key index for a WEP key.
1928 *
1929 * Note that in a regular BSS, multicast frames are sent by the
1930 * AP only, associated stations unicast the frame to the AP first
1931 * which then multicasts it on their behalf.
1932 *
1933 * There is also a slight problem in IBSS mode: GTKs are negotiated
1934 * with each station, that is something we don't currently handle.
1935 * The spec seems to expect that one negotiates the same key with
1936 * every station but there's no such requirement; VLANs could be
1937 * possible.
1938 */
1939
Johan Almbladh86c228a2013-08-14 15:29:46 +02001940 /* start without a key */
1941 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001942 fc = hdr->frame_control;
1943
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001944 if (rx->sta) {
1945 int keyid = rx->sta->ptk_idx;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001946 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001947
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001948 if (ieee80211_has_protected(fc)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001949 cs = rx->sta->cipher_scheme;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001950 keyid = ieee80211_get_keyid(rx->skb, cs);
1951
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001952 if (unlikely(keyid < 0))
1953 return RX_DROP_UNUSABLE;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001954
1955 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001956 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001957 }
1958
Johan Almbladh86c228a2013-08-14 15:29:46 +02001959 if (!ieee80211_has_protected(fc))
1960 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1961
1962 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001963 rx->key = ptk_idx ? ptk_idx : sta_ptk;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001964 if ((status->flag & RX_FLAG_DECRYPTED) &&
1965 (status->flag & RX_FLAG_IV_STRIPPED))
1966 return RX_CONTINUE;
1967 /* Skip decryption if the frame is not protected. */
1968 if (!ieee80211_has_protected(fc))
1969 return RX_CONTINUE;
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001970 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1971 /* Broadcast/multicast robust management frame / BIP */
1972 if ((status->flag & RX_FLAG_DECRYPTED) &&
1973 (status->flag & RX_FLAG_IV_STRIPPED))
1974 return RX_CONTINUE;
1975
1976 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1977 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001978 NUM_DEFAULT_BEACON_KEYS) {
1979 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1980 skb->data,
1981 skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001982 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Jouni Malinen9eaf1832020-04-01 17:25:48 +03001983 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001984
1985 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1986 if (!rx->key)
1987 return RX_CONTINUE; /* Beacon protection not in use */
Johan Almbladh86c228a2013-08-14 15:29:46 +02001988 } else if (mmie_keyidx >= 0) {
1989 /* Broadcast/multicast robust management frame / BIP */
1990 if ((status->flag & RX_FLAG_DECRYPTED) &&
1991 (status->flag & RX_FLAG_IV_STRIPPED))
1992 return RX_CONTINUE;
1993
1994 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1995 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1996 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09001997 if (rx->sta) {
1998 if (ieee80211_is_group_privacy_action(skb) &&
1999 test_sta_flag(rx->sta, WLAN_STA_MFP))
2000 return RX_DROP_MONITOR;
2001
Johan Almbladh86c228a2013-08-14 15:29:46 +02002002 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09002003 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002004 if (!rx->key)
2005 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
2006 } else if (!ieee80211_has_protected(fc)) {
2007 /*
2008 * The frame was not protected, so skip decryption. However, we
2009 * need to set rx->key if there is a key that could have been
2010 * used so that the frame may be dropped if encryption would
2011 * have been expected.
2012 */
2013 struct ieee80211_key *key = NULL;
2014 struct ieee80211_sub_if_data *sdata = rx->sdata;
2015 int i;
2016
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002017 if (ieee80211_is_beacon(fc)) {
2018 key = ieee80211_rx_get_bigtk(rx, -1);
2019 } else if (ieee80211_is_mgmt(fc) &&
2020 is_multicast_ether_addr(hdr->addr1)) {
2021 key = rcu_dereference(rx->sdata->default_mgmt_key);
2022 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002023 if (rx->sta) {
2024 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2025 key = rcu_dereference(rx->sta->gtk[i]);
2026 if (key)
2027 break;
2028 }
2029 }
2030 if (!key) {
2031 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2032 key = rcu_dereference(sdata->keys[i]);
2033 if (key)
2034 break;
2035 }
2036 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002037 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002038 if (key)
2039 rx->key = key;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002040 return RX_CONTINUE;
2041 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002042 /*
2043 * The device doesn't give us the IV so we won't be
2044 * able to look up the key. That's ok though, we
2045 * don't need to decrypt the frame, we just won't
2046 * be able to keep statistics accurate.
2047 * Except for key threshold notifications, should
2048 * we somehow allow the driver to tell us which key
2049 * the hardware used if this flag is set?
2050 */
2051 if ((status->flag & RX_FLAG_DECRYPTED) &&
2052 (status->flag & RX_FLAG_IV_STRIPPED))
2053 return RX_CONTINUE;
2054
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002055 keyidx = ieee80211_get_keyid(rx->skb, cs);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002056
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002057 if (unlikely(keyidx < 0))
2058 return RX_DROP_UNUSABLE;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002059
2060 /* check per-station GTK first, if multicast packet */
2061 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2062 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2063
2064 /* if not found, try default key */
2065 if (!rx->key) {
2066 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2067
2068 /*
2069 * RSNA-protected unicast frames should always be
2070 * sent with pairwise or station-to-station keys,
2071 * but for WEP we allow using a key index as well.
2072 */
2073 if (rx->key &&
2074 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2075 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2076 !is_multicast_ether_addr(hdr->addr1))
2077 rx->key = NULL;
2078 }
2079 }
2080
2081 if (rx->key) {
2082 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2083 return RX_DROP_MONITOR;
2084
Johan Almbladh86c228a2013-08-14 15:29:46 +02002085 /* TODO: add threshold stuff again */
2086 } else {
2087 return RX_DROP_MONITOR;
2088 }
2089
2090 switch (rx->key->conf.cipher) {
2091 case WLAN_CIPHER_SUITE_WEP40:
2092 case WLAN_CIPHER_SUITE_WEP104:
2093 result = ieee80211_crypto_wep_decrypt(rx);
2094 break;
2095 case WLAN_CIPHER_SUITE_TKIP:
2096 result = ieee80211_crypto_tkip_decrypt(rx);
2097 break;
2098 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002099 result = ieee80211_crypto_ccmp_decrypt(
2100 rx, IEEE80211_CCMP_MIC_LEN);
2101 break;
2102 case WLAN_CIPHER_SUITE_CCMP_256:
2103 result = ieee80211_crypto_ccmp_decrypt(
2104 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002105 break;
2106 case WLAN_CIPHER_SUITE_AES_CMAC:
2107 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2108 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02002109 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2110 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2111 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02002112 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2113 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2114 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2115 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02002116 case WLAN_CIPHER_SUITE_GCMP:
2117 case WLAN_CIPHER_SUITE_GCMP_256:
2118 result = ieee80211_crypto_gcmp_decrypt(rx);
2119 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002120 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002121 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002122 }
2123
2124 /* the hdr variable is invalid after the decrypt handlers */
2125
2126 /* either the frame has been decrypted or will be dropped */
2127 status->flag |= RX_FLAG_DECRYPTED;
2128
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002129 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2130 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2131 skb->data, skb->len);
2132
Johan Almbladh86c228a2013-08-14 15:29:46 +02002133 return result;
2134}
2135
Johannes Berg571ecf62007-07-27 15:43:22 +02002136static inline struct ieee80211_fragment_entry *
2137ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
2138 unsigned int frag, unsigned int seq, int rx_queue,
2139 struct sk_buff **skb)
2140{
2141 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02002142
Johannes Berg571ecf62007-07-27 15:43:22 +02002143 entry = &sdata->fragments[sdata->fragment_next++];
2144 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
2145 sdata->fragment_next = 0;
2146
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002147 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02002148 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02002149
2150 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2151 *skb = NULL;
2152 entry->first_frag_time = jiffies;
2153 entry->seq = seq;
2154 entry->rx_queue = rx_queue;
2155 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002156 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02002157 entry->extra_len = 0;
2158
2159 return entry;
2160}
2161
2162static inline struct ieee80211_fragment_entry *
2163ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002164 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02002165 int rx_queue, struct ieee80211_hdr *hdr)
2166{
2167 struct ieee80211_fragment_entry *entry;
2168 int i, idx;
2169
2170 idx = sdata->fragment_next;
2171 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2172 struct ieee80211_hdr *f_hdr;
David S. Miller7957a9d2018-08-06 22:49:13 -07002173 struct sk_buff *f_skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002174
2175 idx--;
2176 if (idx < 0)
2177 idx = IEEE80211_FRAGMENT_MAX - 1;
2178
2179 entry = &sdata->fragments[idx];
2180 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2181 entry->rx_queue != rx_queue ||
2182 entry->last_frag + 1 != frag)
2183 continue;
2184
David S. Miller7957a9d2018-08-06 22:49:13 -07002185 f_skb = __skb_peek(&entry->skb_list);
2186 f_hdr = (struct ieee80211_hdr *) f_skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002187
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002188 /*
2189 * Check ftype and addresses are equal, else check next fragment
2190 */
2191 if (((hdr->frame_control ^ f_hdr->frame_control) &
2192 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002193 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2194 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02002195 continue;
2196
S.Çağlar Onurab466232008-02-14 17:36:47 +02002197 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002198 __skb_queue_purge(&entry->skb_list);
2199 continue;
2200 }
2201 return entry;
2202 }
2203
2204 return NULL;
2205}
2206
Johannes Berg49461622008-06-30 15:10:45 +02002207static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002208ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002209{
2210 struct ieee80211_hdr *hdr;
2211 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002212 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02002213 unsigned int frag, seq;
2214 struct ieee80211_fragment_entry *entry;
2215 struct sk_buff *skb;
2216
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002217 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002218 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002219
Thomas Pedersen09a740c2020-09-21 19:28:14 -07002220 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002221 return RX_CONTINUE;
2222
Johannes Berg571ecf62007-07-27 15:43:22 +02002223 sc = le16_to_cpu(hdr->seq_ctrl);
2224 frag = sc & IEEE80211_SCTL_FRAG;
2225
Johannes Bergb8fff402014-11-03 13:57:46 +01002226 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02002227 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01002228 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02002229 }
Johannes Bergb8fff402014-11-03 13:57:46 +01002230
Andreas Müllerd0259332014-12-12 12:11:11 +01002231 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2232 goto out;
2233
Johannes Berg571ecf62007-07-27 15:43:22 +02002234 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2235
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08002236 if (skb_linearize(rx->skb))
2237 return RX_DROP_UNUSABLE;
2238
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07002239 /*
2240 * skb_linearize() might change the skb->data and
2241 * previously cached variables (in this case, hdr) need to
2242 * be refreshed with the new data.
2243 */
2244 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002245 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2246
2247 if (frag == 0) {
2248 /* This is the first fragment of a new frame. */
2249 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002250 rx->seqno_idx, &(rx->skb));
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002251 if (rx->key &&
2252 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
Johannes Berg9acc54b2016-02-26 22:13:40 +01002253 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2254 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2255 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07002256 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02002257 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002258
2259 /* Store CCMP/GCMP PN so that we can verify that the
2260 * next fragment has a sequential PN value.
2261 */
2262 entry->check_sequential_pn = true;
Johannes Berg571ecf62007-07-27 15:43:22 +02002263 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07002264 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02002265 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01002266 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2267 u.ccmp.rx_pn) !=
2268 offsetof(struct ieee80211_key,
2269 u.gcmp.rx_pn));
2270 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2271 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2272 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2273 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002274 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002275 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002276 }
2277
2278 /* This is a fragment for a frame that should already be pending in
2279 * fragment cache. Add this fragment to the end of the pending entry.
2280 */
Johannes Berg9e262972011-07-07 18:45:03 +02002281 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
2282 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02002283 if (!entry) {
2284 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002285 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002286 }
2287
Johannes Berg9acc54b2016-02-26 22:13:40 +01002288 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2289 * MPDU PN values are not incrementing in steps of 1."
2290 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2291 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2292 */
2293 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002294 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002295 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07002296 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002297
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002298 if (!rx->key ||
2299 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
Johannes Berg9acc54b2016-02-26 22:13:40 +01002300 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2301 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2302 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002303 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002304 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2305 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002306 pn[i]++;
2307 if (pn[i])
2308 break;
2309 }
Johannes Berg9e262972011-07-07 18:45:03 +02002310 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07002311 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02002312 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002313 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002314 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002315 }
2316
Harvey Harrison358c8d92008-07-15 18:44:13 -07002317 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002318 __skb_queue_tail(&entry->skb_list, rx->skb);
2319 entry->last_frag = frag;
2320 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002321 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002322 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002323 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002324 }
2325
2326 rx->skb = __skb_dequeue(&entry->skb_list);
2327 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002328 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002329 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2330 GFP_ATOMIC))) {
2331 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2332 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002333 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002334 }
2335 }
2336 while ((skb = __skb_dequeue(&entry->skb_list))) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02002337 skb_put_data(rx->skb, skb->data, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02002338 dev_kfree_skb(skb);
2339 }
2340
Johannes Berg571ecf62007-07-27 15:43:22 +02002341 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002342 ieee80211_led_rx(rx->local);
2343 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002344 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002345 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002346 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002347}
2348
Johannes Berg1df332e2012-10-26 00:09:11 +02002349static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002350{
Johannes Berg1df332e2012-10-26 00:09:11 +02002351 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002352 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002353
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002354 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002355}
2356
Johannes Berg1df332e2012-10-26 00:09:11 +02002357static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002358{
Markus Theil0b467b632020-06-25 12:42:14 +02002359 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002360 struct sk_buff *skb = rx->skb;
2361 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2362
Johannes Berg3017b802007-08-28 17:01:53 -04002363 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002364 * Pass through unencrypted frames if the hardware has
2365 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002366 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002367 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002368 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002369
Markus Theil0b467b632020-06-25 12:42:14 +02002370 /* check mesh EAPOL frames first */
2371 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2372 ieee80211_is_data(fc))) {
2373 struct ieee80211s_hdr *mesh_hdr;
2374 u16 hdr_len = ieee80211_hdrlen(fc);
2375 u16 ethertype_offset;
2376 __be16 ethertype;
2377
2378 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2379 goto drop_check;
2380
2381 /* make sure fixed part of mesh header is there, also checks skb len */
2382 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2383 goto drop_check;
2384
2385 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2386 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2387 sizeof(rfc1042_header);
2388
2389 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2390 ethertype == rx->sdata->control_port_protocol)
2391 return 0;
2392 }
2393
2394drop_check:
Johannes Berg571ecf62007-07-27 15:43:22 +02002395 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002396 if (unlikely(!ieee80211_has_protected(fc) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002397 !ieee80211_is_any_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002398 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002399 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002400
2401 return 0;
2402}
2403
Johannes Berg1df332e2012-10-26 00:09:11 +02002404static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002405{
2406 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002407 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002408 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002409
Jouni Malinene3efca02010-03-28 22:31:15 -07002410 /*
2411 * Pass through unencrypted frames if the hardware has
2412 * decrypted them already.
2413 */
2414 if (status->flag & RX_FLAG_DECRYPTED)
2415 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002416
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002417 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002418 if (unlikely(!ieee80211_has_protected(fc) &&
2419 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002420 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002421 if (ieee80211_is_deauth(fc) ||
2422 ieee80211_is_disassoc(fc))
2423 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2424 rx->skb->data,
2425 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002426 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002427 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002428 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002429 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002430 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002431 if (ieee80211_is_deauth(fc) ||
2432 ieee80211_is_disassoc(fc))
2433 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2434 rx->skb->data,
2435 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002436 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002437 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002438 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002439 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2440 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2441 rx->skb->data,
2442 rx->skb->len);
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002443 return -EACCES;
Jouni Malinen9eaf1832020-04-01 17:25:48 +03002444 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002445 /*
2446 * When using MFP, Action frames are not allowed prior to
2447 * having configured keys.
2448 */
2449 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002450 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002451 return -EACCES;
2452 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002453
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002454 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002455}
2456
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002457static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002458__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002459{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002460 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002462 bool check_port_control = false;
2463 struct ethhdr *ehdr;
2464 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002465
Felix Fietkau4114fa22011-04-12 19:15:22 +02002466 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002467 if (ieee80211_has_a4(hdr->frame_control) &&
2468 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002469 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002470
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002471 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2472 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2473
2474 if (!sdata->u.mgd.use_4addr)
2475 return -1;
Sathishkumar Muruganandam1ecef202018-08-28 17:51:38 +05302476 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002477 check_port_control = true;
2478 }
2479
Johannes Berg9bc383d2009-11-19 11:55:19 +01002480 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002481 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002482 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002483
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002484 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002485 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002486 return ret;
2487
2488 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002489 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2490 *port_control = true;
2491 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002492 return -1;
2493
2494 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002495}
Johannes Berg571ecf62007-07-27 15:43:22 +02002496
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002497/*
2498 * requires that rx->skb is a frame with ethernet header
2499 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002500static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002501{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302502 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002503 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2504 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2505
2506 /*
2507 * Allow EAPOL frames to us/the PAE group address regardless
2508 * of whether the frame was encrypted or not.
2509 */
Johannes Berga621fa42010-08-27 14:26:54 +03002510 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002511 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2512 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002513 return true;
2514
2515 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002516 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002517 return false;
2518
2519 return true;
2520}
2521
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002522static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2523 struct ieee80211_rx_data *rx)
2524{
2525 struct ieee80211_sub_if_data *sdata = rx->sdata;
2526 struct net_device *dev = sdata->dev;
2527
2528 if (unlikely((skb->protocol == sdata->control_port_protocol ||
Markus Theil7f3f96c2020-03-12 10:10:54 +01002529 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2530 !sdata->control_port_no_preauth)) &&
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002531 sdata->control_port_over_nl80211)) {
2532 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Denis Kenziorf8b43c52019-08-27 17:41:20 -05002533 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002534
Denis Kenziora948f712018-07-03 15:05:48 -05002535 cfg80211_rx_control_port(dev, skb, noencrypt);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002536 dev_kfree_skb(skb);
2537 } else {
Denis Kenziorc8a41c62019-08-27 17:41:19 -05002538 memset(skb->cb, 0, sizeof(skb->cb));
2539
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002540 /* deliver to local stack */
Felix Fietkauc5d16862020-07-26 13:06:11 +02002541 if (rx->list)
2542 list_add_tail(&skb->list, rx->list);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002543 else
2544 netif_receive_skb(skb);
2545 }
2546}
2547
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002548/*
2549 * requires that rx->skb is a frame with ethernet header
2550 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002551static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002552ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002553{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002554 struct ieee80211_sub_if_data *sdata = rx->sdata;
2555 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002556 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002557 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2558 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002559
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002560 skb = rx->skb;
2561 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002562
Johannes Berg5a490512015-04-22 17:10:38 +02002563 ieee80211_rx_stats(dev, skb->len);
2564
Johannes Bergde8f18d2016-03-31 20:02:03 +03002565 if (rx->sta) {
2566 /* The seqno index has the same property as needed
2567 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2568 * for non-QoS-data frames. Here we know it's a data
2569 * frame, so count MSDUs.
2570 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002571 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002572 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002573 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002574 }
2575
Johannes Berg05c914f2008-09-11 00:01:58 +02002576 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2577 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002578 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002579 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002580 if (is_multicast_ether_addr(ehdr->h_dest) &&
2581 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002582 /*
2583 * send multicast frames both to higher layers in
2584 * local net stack and back to the wireless medium
2585 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002586 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002587 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002588 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002589 dev->name);
Johannes Berg42dca5e2018-10-09 10:00:21 +02002590 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2591 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2592 dsta = sta_info_get(sdata, ehdr->h_dest);
Johannes Bergabe60632009-11-25 17:46:18 +01002593 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002594 /*
2595 * The destination station is associated to
2596 * this AP (in this VLAN), so send the frame
2597 * directly to it and do not pass it to local
2598 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002599 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002600 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002601 skb = NULL;
2602 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002603 }
2604 }
2605
Kalle Valo59d9cb02009-12-17 13:54:57 +01002606#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002607 if (skb) {
2608 /* 'align' will only take the values 0 or 2 here since all
2609 * frames are required to be aligned to 2-byte boundaries
2610 * when being passed to mac80211; the code here works just
2611 * as well if that isn't true, but mac80211 assumes it can
2612 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002613 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002614 int align;
2615
2616 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002617 if (align) {
2618 if (WARN_ON(skb_headroom(skb) < 3)) {
2619 dev_kfree_skb(skb);
2620 skb = NULL;
2621 } else {
2622 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002623 size_t len = skb_headlen(skb);
2624 skb->data -= align;
2625 memmove(skb->data, data, len);
2626 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002627 }
2628 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002629 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002630#endif
2631
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002632 if (skb) {
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002633 skb->protocol = eth_type_trans(skb, dev);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002634 ieee80211_deliver_skb_to_local_stack(skb, rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002635 }
2636
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002637 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002638 /*
2639 * Send to wireless media and increase priority by 256 to
2640 * keep the received priority instead of reclassifying
2641 * the frame (see cfg80211_classify8021d).
2642 */
2643 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002644 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002645 skb_reset_network_header(xmit_skb);
2646 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002647 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002648 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002649}
2650
Johannes Berg49461622008-06-30 15:10:45 +02002651static ieee80211_rx_result debug_noinline
Felix Fietkau24bba072018-02-27 13:03:07 +01002652__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002653{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002654 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002655 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002656 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2657 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002658 struct sk_buff_head frame_list;
Johannes Berg7f6990c2016-10-05 15:29:49 +02002659 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002660 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002661
Johannes Bergea720932016-10-05 10:14:42 +02002662 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
Johannes Berge2b52272016-10-05 16:42:06 +02002663 check_da = NULL;
2664 check_sa = NULL;
2665 } else switch (rx->sdata->vif.type) {
2666 case NL80211_IFTYPE_AP:
2667 case NL80211_IFTYPE_AP_VLAN:
2668 check_da = NULL;
2669 break;
2670 case NL80211_IFTYPE_STATION:
2671 if (!rx->sta ||
2672 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2673 check_sa = NULL;
2674 break;
2675 case NL80211_IFTYPE_MESH_POINT:
2676 check_sa = NULL;
2677 break;
2678 default:
2679 break;
Johannes Bergea720932016-10-05 10:14:42 +02002680 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002681
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002682 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002683 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002684
Johannes Berg7f6990c2016-10-05 15:29:49 +02002685 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2686 rx->sdata->vif.addr,
Felix Fietkau24bba072018-02-27 13:03:07 +01002687 rx->sdata->vif.type,
2688 data_offset))
Johannes Berg7f6990c2016-10-05 15:29:49 +02002689 return RX_DROP_UNUSABLE;
2690
Zhu Yieaf85ca2009-12-01 10:18:37 +08002691 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2692 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002693 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002694 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002695
Zhu Yieaf85ca2009-12-01 10:18:37 +08002696 while (!skb_queue_empty(&frame_list)) {
2697 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002698
Harvey Harrison358c8d92008-07-15 18:44:13 -07002699 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002700 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002701 continue;
2702 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002703
2704 ieee80211_deliver_skb(rx);
2705 }
2706
Johannes Berg9ae54c82008-01-31 19:48:20 +01002707 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002708}
2709
Felix Fietkau24bba072018-02-27 13:03:07 +01002710static ieee80211_rx_result debug_noinline
2711ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2712{
2713 struct sk_buff *skb = rx->skb;
2714 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2715 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2716 __le16 fc = hdr->frame_control;
2717
2718 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2719 return RX_CONTINUE;
2720
2721 if (unlikely(!ieee80211_is_data(fc)))
2722 return RX_CONTINUE;
2723
2724 if (unlikely(!ieee80211_is_data_present(fc)))
2725 return RX_DROP_MONITOR;
2726
2727 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2728 switch (rx->sdata->vif.type) {
2729 case NL80211_IFTYPE_AP_VLAN:
2730 if (!rx->sdata->u.vlan.sta)
2731 return RX_DROP_UNUSABLE;
2732 break;
2733 case NL80211_IFTYPE_STATION:
2734 if (!rx->sdata->u.mgd.use_4addr)
2735 return RX_DROP_UNUSABLE;
2736 break;
2737 default:
2738 return RX_DROP_UNUSABLE;
2739 }
2740 }
2741
2742 if (is_multicast_ether_addr(hdr->addr1))
2743 return RX_DROP_UNUSABLE;
2744
2745 return __ieee80211_rx_h_amsdu(rx, 0);
2746}
2747
Ingo Molnarbf94e172008-10-12 23:51:38 -07002748#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002749static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002750ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2751{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002752 struct ieee80211_hdr *fwd_hdr, *hdr;
2753 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002754 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002755 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002756 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002757 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002758 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002759 u16 ac, q, hdrlen;
Felix Fietkau51d0af22019-02-22 13:21:15 +01002760 int tailroom = 0;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002761
2762 hdr = (struct ieee80211_hdr *) skb->data;
2763 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002764
2765 /* make sure fixed part of mesh header is there, also checks skb len */
2766 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2767 return RX_DROP_MONITOR;
2768
2769 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2770
2771 /* make sure full mesh header is there, also checks skb len */
2772 if (!pskb_may_pull(rx->skb,
2773 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2774 return RX_DROP_MONITOR;
2775
2776 /* reload pointers */
2777 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002778 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2779
Bob Copelandd0c22112015-03-02 14:28:52 -05002780 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2781 return RX_DROP_MONITOR;
2782
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002783 /* frame is in RMC, don't forward */
2784 if (ieee80211_is_data(hdr->frame_control) &&
2785 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002786 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002787 return RX_DROP_MONITOR;
2788
Johannes Berg5c900672015-04-22 14:48:34 +02002789 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002790 return RX_CONTINUE;
2791
2792 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002793 return RX_DROP_MONITOR;
2794
Javier Cardona43b7b312009-10-15 18:10:51 -07002795 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002796 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002797 char *proxied_addr;
2798 char *mpp_addr;
2799
2800 if (is_multicast_ether_addr(hdr->addr1)) {
2801 mpp_addr = hdr->addr3;
2802 proxied_addr = mesh_hdr->eaddr1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -07002803 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2804 MESH_FLAGS_AE_A5_A6) {
Johannes Berg9b395bc2012-10-26 00:36:40 +02002805 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002806 mpp_addr = hdr->addr4;
2807 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002808 } else {
2809 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002810 }
YanBo79617de2008-09-22 13:30:32 +08002811
YanBo79617de2008-09-22 13:30:32 +08002812 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002813 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002814 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002815 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002816 } else {
2817 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002818 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002819 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002820 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002821 spin_unlock_bh(&mppath->state_lock);
2822 }
2823 rcu_read_unlock();
2824 }
2825
Javier Cardona3c5772a2009-08-10 12:15:48 -07002826 /* Frame has reached destination. Don't forward */
2827 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002828 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002829 return RX_CONTINUE;
2830
Michal Kaziorcf440122016-01-25 14:43:24 +01002831 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2832 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002833 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002834 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002835 return RX_DROP_MONITOR;
2836 }
2837 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002838
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002839 if (!--mesh_hdr->ttl) {
Bob Copelanda0dc0202019-01-17 16:32:42 -05002840 if (!is_multicast_ether_addr(hdr->addr1))
2841 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2842 dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002843 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002844 }
2845
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002846 if (!ifmsh->mshcfg.dot11MeshForwarding)
2847 goto out;
2848
Felix Fietkau51d0af22019-02-22 13:21:15 +01002849 if (sdata->crypto_tx_tailroom_needed_cnt)
2850 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2851
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002852 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
Felix Fietkau51d0af22019-02-22 13:21:15 +01002853 sdata->encrypt_headroom,
2854 tailroom, GFP_ATOMIC);
Joe Perches0c3d5a92018-03-12 08:07:12 -07002855 if (!fwd_skb)
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002856 goto out;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002857
2858 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002859 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002860 info = IEEE80211_SKB_CB(fwd_skb);
2861 memset(info, 0, sizeof(*info));
Felix Fietkaucc20ff22020-09-08 14:36:57 +02002862 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002863 info->control.vif = &rx->sdata->vif;
2864 info->control.jiffies = jiffies;
2865 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2866 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2867 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002868 /* update power mode indication when forwarding */
2869 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002870 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002871 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002872 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2873 } else {
2874 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002875 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002876 fwd_hdr->addr3, 0,
2877 WLAN_REASON_MESH_PATH_NOFORWARD,
2878 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002879 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002880 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002881 return RX_DROP_MONITOR;
2882 }
2883
2884 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2885 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002886 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002887 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002888 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002889 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002890}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002891#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002892
2893static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002894ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002895{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002896 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302897 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002898 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002899 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2900 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002901 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002902 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002903
Harvey Harrison358c8d92008-07-15 18:44:13 -07002904 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002905 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002906
Harvey Harrison358c8d92008-07-15 18:44:13 -07002907 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002908 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002909
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002910 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002911 * Send unexpected-4addr-frame event to hostapd. For older versions,
2912 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002913 */
2914 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002915 sdata->vif.type == NL80211_IFTYPE_AP) {
2916 if (rx->sta &&
2917 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2918 cfg80211_rx_unexpected_4addr_frame(
2919 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002920 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002921 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002922
Felix Fietkau4114fa22011-04-12 19:15:22 +02002923 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002924 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002925 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002926
Harvey Harrison358c8d92008-07-15 18:44:13 -07002927 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002928 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002929
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002930 /* directly handle TDLS channel switch requests/responses */
2931 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2932 cpu_to_be16(ETH_P_TDLS))) {
2933 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2934
2935 if (pskb_may_pull(rx->skb,
2936 offsetof(struct ieee80211_tdls_data, u)) &&
2937 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2938 tf->category == WLAN_CATEGORY_TDLS &&
2939 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2940 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002941 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2942 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002943 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002944 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002945
2946 return RX_QUEUED;
2947 }
2948 }
2949
Felix Fietkau4114fa22011-04-12 19:15:22 +02002950 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2951 unlikely(port_control) && sdata->bss) {
2952 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2953 u.ap);
2954 dev = sdata->dev;
2955 rx->sdata = sdata;
2956 }
2957
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002958 rx->skb->dev = dev;
2959
Johannes Berg602fae42016-03-17 15:02:52 +02002960 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2961 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302962 !is_multicast_ether_addr(
2963 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2964 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002965 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2966 mod_timer(&local->dynamic_ps_timer, jiffies +
2967 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05302968
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002969 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002970
Johannes Berg9ae54c82008-01-31 19:48:20 +01002971 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002972}
2973
Johannes Berg49461622008-06-30 15:10:45 +02002974static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002975ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002976{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002977 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002978 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002979 struct tid_ampdu_rx *tid_agg_rx;
2980 u16 start_seq_num;
2981 u16 tid;
2982
Harvey Harrisona7767f92008-07-02 16:30:51 -07002983 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002984 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002985
Harvey Harrisona7767f92008-07-02 16:30:51 -07002986 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002987 struct {
2988 __le16 control, start_seq_num;
2989 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002990 struct ieee80211_event event = {
2991 .type = BAR_RX_EVENT,
2992 };
Johannes Berg8ae59772010-05-30 14:52:58 +02002993
Ron Rindjunsky71364712007-12-25 17:00:36 +02002994 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002995 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002996
2997 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2998 &bar_data, sizeof(bar_data)))
2999 return RX_DROP_MONITOR;
3000
Johannes Berg8ae59772010-05-30 14:52:58 +02003001 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02003002
Johannes Berg53f24972016-08-29 23:25:19 +03003003 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3004 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3005 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3006 WLAN_BACK_RECIPIENT,
3007 WLAN_REASON_QSTA_REQUIRE_SETUP);
3008
Johannes Berga87f7362010-06-10 10:21:38 +02003009 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3010 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01003011 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003012
Johannes Berg8ae59772010-05-30 14:52:58 +02003013 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003014 event.u.ba.tid = tid;
3015 event.u.ba.ssn = start_seq_num;
3016 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003017
3018 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01003019 if (tid_agg_rx->timeout)
3020 mod_timer(&tid_agg_rx->session_timer,
3021 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02003022
Johannes Bergdd318572010-11-29 11:09:16 +01003023 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01003024 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02003025 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003026 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01003027 spin_unlock(&tid_agg_rx->reorder_lock);
3028
Emmanuel Grumbach63822462015-04-20 22:53:37 +03003029 drv_event_callback(rx->local, rx->sdata, &event);
3030
Johannes Berga02ae752009-11-16 12:00:40 +01003031 kfree_skb(skb);
3032 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003033 }
3034
Johannes Berg08daeca2010-05-30 14:53:43 +02003035 /*
3036 * After this point, we only want management frames,
3037 * so we can drop all remaining control frames to
3038 * cooked monitor interfaces.
3039 */
3040 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003041}
3042
Jouni Malinenf4f727a2009-01-10 11:46:53 +02003043static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3044 struct ieee80211_mgmt *mgmt,
3045 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02003046{
3047 struct ieee80211_local *local = sdata->local;
3048 struct sk_buff *skb;
3049 struct ieee80211_mgmt *resp;
3050
Joe Perchesb203ca32012-05-08 18:56:52 +00003051 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02003052 /* Not to own unicast address */
3053 return;
3054 }
3055
Joe Perchesb203ca32012-05-08 18:56:52 +00003056 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3057 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003058 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02003059 return;
3060 }
3061
3062 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3063 /* Too short SA Query request frame */
3064 return;
3065 }
3066
3067 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3068 if (skb == NULL)
3069 return;
3070
3071 skb_reserve(skb, local->hw.extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02003072 resp = skb_put_zero(skb, 24);
Jouni Malinenfea14732009-01-08 13:32:06 +02003073 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01003074 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01003075 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02003076 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3077 IEEE80211_STYPE_ACTION);
3078 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3079 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3080 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3081 memcpy(resp->u.action.u.sa_query.trans_id,
3082 mgmt->u.action.u.sa_query.trans_id,
3083 WLAN_SA_QUERY_TR_ID_LEN);
3084
Johannes Berg62ae67b2009-11-18 18:42:05 +01003085 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02003086}
3087
Johannes Berg49461622008-06-30 15:10:45 +02003088static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003089ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3090{
3091 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003092 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003093
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003094 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3095 return RX_CONTINUE;
3096
Johannes Berg2e161f782010-08-12 15:38:38 +02003097 /*
3098 * From here on, look only at management frames.
3099 * Data and control frames are already handled,
3100 * and unknown (reserved) frames are useless.
3101 */
3102 if (rx->skb->len < 24)
3103 return RX_DROP_MONITOR;
3104
3105 if (!ieee80211_is_mgmt(mgmt->frame_control))
3106 return RX_DROP_MONITOR;
3107
Johannes Bergee971922011-11-04 11:18:18 +01003108 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3109 ieee80211_is_beacon(mgmt->frame_control) &&
3110 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01003111 int sig = 0;
3112
Tosoni1ad22fb2018-03-14 16:58:34 +00003113 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3114 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003115 sig = status->signal;
3116
Thomas Pedersene76fede2020-04-30 10:25:50 -07003117 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3118 rx->skb->data, rx->skb->len,
3119 ieee80211_rx_status_to_khz(status),
3120 sig);
Johannes Bergee971922011-11-04 11:18:18 +01003121 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3122 }
3123
Johannes Berg2e161f782010-08-12 15:38:38 +02003124 if (ieee80211_drop_unencrypted_mgmt(rx))
3125 return RX_DROP_UNUSABLE;
3126
3127 return RX_CONTINUE;
3128}
3129
3130static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02003131ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3132{
3133 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003134 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02003135 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003136 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02003137 int len = rx->skb->len;
3138
3139 if (!ieee80211_is_action(mgmt->frame_control))
3140 return RX_CONTINUE;
3141
Jouni Malinen026331c2010-02-15 12:53:10 +02003142 /* drop too small frames */
3143 if (len < IEEE80211_MIN_ACTION_SIZE)
3144 return RX_DROP_UNUSABLE;
3145
Marco Porsch815b8092012-12-05 15:04:26 -08003146 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003147 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3148 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02003149 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02003150
Johannes Bergde1ede72008-09-09 14:42:50 +02003151 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003152 case WLAN_CATEGORY_HT:
3153 /* reject HT action frames from stations not supporting HT */
3154 if (!rx->sta->sta.ht_cap.ht_supported)
3155 goto invalid;
3156
3157 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3158 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3159 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3160 sdata->vif.type != NL80211_IFTYPE_AP &&
3161 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3162 break;
3163
Johannes Bergec61cd62012-12-28 12:12:10 +01003164 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003165 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3166 goto invalid;
3167
3168 switch (mgmt->u.action.u.ht_smps.action) {
3169 case WLAN_HT_ACTION_SMPS: {
3170 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003171 enum ieee80211_smps_mode smps_mode;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303172 struct sta_opmode_info sta_opmode = {};
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003173
Ilan Peerc4d800d2020-01-31 13:12:53 +02003174 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3175 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3176 goto handled;
3177
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003178 /* convert to HT capability */
3179 switch (mgmt->u.action.u.ht_smps.smps_control) {
3180 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003181 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003182 break;
3183 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003184 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003185 break;
3186 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003187 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003188 break;
3189 default:
3190 goto invalid;
3191 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003192
3193 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01003194 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003195 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003196 rx->sta->sta.smps_mode = smps_mode;
tamizhr@codeaurora.org57566b22018-03-27 19:16:16 +05303197 sta_opmode.smps_mode =
3198 ieee80211_smps_mode_to_smps_mode(smps_mode);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303199 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003200
3201 sband = rx->local->hw.wiphy->bands[status->band];
3202
Johannes Berg64f68e52012-03-28 10:58:37 +02003203 rate_control_rate_update(local, sband, rx->sta,
3204 IEEE80211_RC_SMPS_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303205 cfg80211_sta_opmode_change_notify(sdata->dev,
3206 rx->sta->addr,
3207 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003208 GFP_ATOMIC);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003209 goto handled;
3210 }
Johannes Bergec61cd62012-12-28 12:12:10 +01003211 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3212 struct ieee80211_supported_band *sband;
3213 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003214 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303215 struct sta_opmode_info sta_opmode = {};
Johannes Bergec61cd62012-12-28 12:12:10 +01003216
3217 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003218 if (!(rx->sta->sta.ht_cap.cap &
3219 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01003220 goto handled;
3221
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003222 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003223 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01003224 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003225 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3226
3227 /* set cur_max_bandwidth and recalc sta bw */
3228 rx->sta->cur_max_bandwidth = max_bw;
3229 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003230
3231 if (rx->sta->sta.bandwidth == new_bw)
3232 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01003233
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003234 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01003235 sband = rx->local->hw.wiphy->bands[status->band];
tamizhr@codeaurora.org97f5f422018-03-27 19:16:17 +05303236 sta_opmode.bw =
3237 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303238 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
Johannes Bergec61cd62012-12-28 12:12:10 +01003239
3240 rate_control_rate_update(local, sband, rx->sta,
3241 IEEE80211_RC_BW_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303242 cfg80211_sta_opmode_change_notify(sdata->dev,
3243 rx->sta->addr,
3244 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003245 GFP_ATOMIC);
Johannes Bergec61cd62012-12-28 12:12:10 +01003246 goto handled;
3247 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003248 default:
3249 goto invalid;
3250 }
3251
3252 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003253 case WLAN_CATEGORY_PUBLIC:
3254 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3255 goto invalid;
3256 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3257 break;
3258 if (!rx->sta)
3259 break;
3260 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3261 break;
3262 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3263 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3264 break;
3265 if (len < offsetof(struct ieee80211_mgmt,
3266 u.action.u.ext_chan_switch.variable))
3267 goto invalid;
3268 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003269 case WLAN_CATEGORY_VHT:
3270 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3271 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3272 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3273 sdata->vif.type != NL80211_IFTYPE_AP &&
3274 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3275 break;
3276
3277 /* verify action code is present */
3278 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3279 goto invalid;
3280
3281 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3282 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01003283 /* verify opmode is present */
3284 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3285 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02003286 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003287 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02003288 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3289 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3290 goto invalid;
3291 goto queue;
3292 }
Johannes Berg0af83d32012-12-27 18:55:36 +01003293 default:
3294 break;
3295 }
3296 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003297 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01003298 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07003299 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01003300 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01003301 sdata->vif.type != NL80211_IFTYPE_AP &&
3302 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02003303 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01003304
Jouni Malinen026331c2010-02-15 12:53:10 +02003305 /* verify action_code is present */
3306 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3307 break;
3308
Johannes Bergde1ede72008-09-09 14:42:50 +02003309 switch (mgmt->u.action.u.addba_req.action_code) {
3310 case WLAN_ACTION_ADDBA_REQ:
3311 if (len < (IEEE80211_MIN_ACTION_SIZE +
3312 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003313 goto invalid;
3314 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003315 case WLAN_ACTION_ADDBA_RESP:
3316 if (len < (IEEE80211_MIN_ACTION_SIZE +
3317 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003318 goto invalid;
3319 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003320 case WLAN_ACTION_DELBA:
3321 if (len < (IEEE80211_MIN_ACTION_SIZE +
3322 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003323 goto invalid;
3324 break;
3325 default:
3326 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02003327 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003328
Johannes Berg8b58ff82010-06-10 10:21:51 +02003329 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02003330 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02003331 /* verify action_code is present */
3332 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3333 break;
3334
Johannes Berg39192c02008-09-09 14:49:03 +02003335 switch (mgmt->u.action.u.measurement.action_code) {
3336 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02003337 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003338 break;
3339
Johannes Berg39192c02008-09-09 14:49:03 +02003340 if (len < (IEEE80211_MIN_ACTION_SIZE +
3341 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02003342 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003343
Johannes Bergcc32abd2009-05-15 11:52:31 +02003344 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003345 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02003346
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003347 ieee80211_process_measurement_req(sdata, mgmt, len);
3348 goto handled;
3349 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3350 u8 *bssid;
3351 if (len < (IEEE80211_MIN_ACTION_SIZE +
3352 sizeof(mgmt->u.action.u.chan_switch)))
3353 break;
3354
3355 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003356 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3357 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003358 break;
3359
3360 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3361 bssid = sdata->u.mgd.bssid;
3362 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3363 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003364 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3365 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003366 else
3367 break;
3368
3369 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02003370 break;
Sujithc481ec92009-01-06 09:28:37 +05303371
Johannes Berg8b58ff82010-06-10 10:21:51 +02003372 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003373 }
Johannes Berg39192c02008-09-09 14:49:03 +02003374 }
3375 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07003376 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003377 if (len < (IEEE80211_MIN_ACTION_SIZE +
3378 sizeof(mgmt->u.action.u.self_prot.action_code)))
3379 break;
3380
Thomas Pedersen8db09852011-08-12 20:01:00 -07003381 switch (mgmt->u.action.u.self_prot.action_code) {
3382 case WLAN_SP_MESH_PEERING_OPEN:
3383 case WLAN_SP_MESH_PEERING_CLOSE:
3384 case WLAN_SP_MESH_PEERING_CONFIRM:
3385 if (!ieee80211_vif_is_mesh(&sdata->vif))
3386 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003387 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003388 /* userspace handles this frame */
3389 break;
3390 goto queue;
3391 case WLAN_SP_MGK_INFORM:
3392 case WLAN_SP_MGK_ACK:
3393 if (!ieee80211_vif_is_mesh(&sdata->vif))
3394 goto invalid;
3395 break;
3396 }
3397 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003398 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003399 if (len < (IEEE80211_MIN_ACTION_SIZE +
3400 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3401 break;
3402
Johannes Berg77a121c2010-06-10 10:21:34 +02003403 if (!ieee80211_vif_is_mesh(&sdata->vif))
3404 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003405 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003406 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003407 break;
3408 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02003409 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003410
Johannes Berg2e161f782010-08-12 15:38:38 +02003411 return RX_CONTINUE;
3412
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003413 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003414 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f782010-08-12 15:38:38 +02003415 /* will return in the next handlers */
3416 return RX_CONTINUE;
3417
3418 handled:
3419 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003420 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003421 dev_kfree_skb(rx->skb);
3422 return RX_QUEUED;
3423
3424 queue:
Johannes Berg2e161f782010-08-12 15:38:38 +02003425 skb_queue_tail(&sdata->skb_queue, rx->skb);
3426 ieee80211_queue_work(&local->hw, &sdata->work);
3427 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003428 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003429 return RX_QUEUED;
3430}
3431
3432static ieee80211_rx_result debug_noinline
3433ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3434{
Johannes Berg554891e2010-09-24 12:38:25 +02003435 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003436 int sig = 0;
Johannes Berg2e161f782010-08-12 15:38:38 +02003437
3438 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003439 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f782010-08-12 15:38:38 +02003440 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003441
Jouni Malinen026331c2010-02-15 12:53:10 +02003442 /*
3443 * Getting here means the kernel doesn't know how to handle
3444 * it, but maybe userspace does ... include returned frames
3445 * so userspace can register for those to know whether ones
3446 * it transmitted were processed or returned.
3447 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003448
Tosoni1ad22fb2018-03-14 16:58:34 +00003449 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3450 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003451 sig = status->signal;
3452
Thomas Pedersene76fede2020-04-30 10:25:50 -07003453 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3454 ieee80211_rx_status_to_khz(status), sig,
3455 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f782010-08-12 15:38:38 +02003456 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003457 rx->sta->rx_stats.packets++;
Johannes Berg2e161f782010-08-12 15:38:38 +02003458 dev_kfree_skb(rx->skb);
3459 return RX_QUEUED;
3460 }
3461
Johannes Berg2e161f782010-08-12 15:38:38 +02003462 return RX_CONTINUE;
3463}
3464
3465static ieee80211_rx_result debug_noinline
Johannes Berg1ea02222020-05-26 10:31:33 +02003466ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3467{
3468 struct ieee80211_sub_if_data *sdata = rx->sdata;
3469 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3470 int len = rx->skb->len;
3471
3472 if (!ieee80211_is_action(mgmt->frame_control))
3473 return RX_CONTINUE;
3474
3475 switch (mgmt->u.action.category) {
3476 case WLAN_CATEGORY_SA_QUERY:
3477 if (len < (IEEE80211_MIN_ACTION_SIZE +
3478 sizeof(mgmt->u.action.u.sa_query)))
3479 break;
3480
3481 switch (mgmt->u.action.u.sa_query.action) {
3482 case WLAN_ACTION_SA_QUERY_REQUEST:
3483 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3484 break;
3485 ieee80211_process_sa_query_req(sdata, mgmt, len);
3486 goto handled;
3487 }
3488 break;
3489 }
3490
3491 return RX_CONTINUE;
3492
3493 handled:
3494 if (rx->sta)
3495 rx->sta->rx_stats.packets++;
3496 dev_kfree_skb(rx->skb);
3497 return RX_QUEUED;
3498}
3499
3500static ieee80211_rx_result debug_noinline
Johannes Berg2e161f782010-08-12 15:38:38 +02003501ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3502{
3503 struct ieee80211_local *local = rx->local;
3504 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3505 struct sk_buff *nskb;
3506 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003507 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f782010-08-12 15:38:38 +02003508
3509 if (!ieee80211_is_action(mgmt->frame_control))
3510 return RX_CONTINUE;
3511
3512 /*
3513 * For AP mode, hostapd is responsible for handling any action
3514 * frames that we didn't handle, including returning unknown
3515 * ones. For all other modes we will return them to the sender,
3516 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003517 * 802.11-2012 9.24.4.
Johannes Berg2e161f782010-08-12 15:38:38 +02003518 * Newer versions of hostapd shall also use the management frame
3519 * registration mechanisms, but older ones still use cooked
3520 * monitor interfaces so push all frames there.
3521 */
Johannes Berg554891e2010-09-24 12:38:25 +02003522 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f782010-08-12 15:38:38 +02003523 (sdata->vif.type == NL80211_IFTYPE_AP ||
3524 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3525 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003526
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003527 if (is_multicast_ether_addr(mgmt->da))
3528 return RX_DROP_MONITOR;
3529
Johannes Berg84040802010-02-15 12:46:39 +02003530 /* do not return rejected action frames */
3531 if (mgmt->u.action.category & 0x80)
3532 return RX_DROP_UNUSABLE;
3533
3534 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3535 GFP_ATOMIC);
3536 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003537 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003538
John W. Linville292b4df2010-06-24 11:13:56 -04003539 nmgmt->u.action.category |= 0x80;
3540 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3541 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003542
3543 memset(nskb->cb, 0, sizeof(nskb->cb));
3544
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003545 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3546 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3547
3548 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3549 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3550 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003551 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003552 info->hw_queue =
3553 local->hw.offchannel_tx_hw_queue;
3554 }
3555
3556 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04003557 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003558 }
Johannes Berg39192c02008-09-09 14:49:03 +02003559 dev_kfree_skb(rx->skb);
3560 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003561}
3562
3563static ieee80211_rx_result debug_noinline
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003564ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3565{
3566 struct ieee80211_sub_if_data *sdata = rx->sdata;
3567 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3568
3569 if (!ieee80211_is_ext(hdr->frame_control))
3570 return RX_CONTINUE;
3571
3572 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3573 return RX_DROP_MONITOR;
3574
3575 /* for now only beacons are ext, so queue them */
3576 skb_queue_tail(&sdata->skb_queue, rx->skb);
3577 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3578 if (rx->sta)
3579 rx->sta->rx_stats.packets++;
3580
3581 return RX_QUEUED;
3582}
3583
3584static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003585ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003586{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003587 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003588 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3589 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003590
Johannes Berg77a121c2010-06-10 10:21:34 +02003591 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003592
Johannes Berg77a121c2010-06-10 10:21:34 +02003593 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3594 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003595 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003596 sdata->vif.type != NL80211_IFTYPE_STATION)
3597 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003598
Johannes Berg77a121c2010-06-10 10:21:34 +02003599 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003600 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003601 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3602 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3603 /* process for all: mesh, mlme, ibss */
3604 break;
Johannes Berg95697f92019-10-04 15:37:05 +03003605 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3606 if (is_multicast_ether_addr(mgmt->da) &&
3607 !is_broadcast_ether_addr(mgmt->da))
3608 return RX_DROP_MONITOR;
3609
3610 /* process only for station/IBSS */
3611 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3612 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3613 return RX_DROP_MONITOR;
3614 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003615 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3616 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003617 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003618 if (is_multicast_ether_addr(mgmt->da) &&
3619 !is_broadcast_ether_addr(mgmt->da))
3620 return RX_DROP_MONITOR;
3621
Johannes Berg77a121c2010-06-10 10:21:34 +02003622 /* process only for station */
3623 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3624 return RX_DROP_MONITOR;
3625 break;
3626 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003627 /* process only for ibss and mesh */
3628 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3629 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003630 return RX_DROP_MONITOR;
3631 break;
3632 default:
3633 return RX_DROP_MONITOR;
3634 }
Johannes Berg46900292009-02-15 12:44:28 +01003635
Johannes Berg77a121c2010-06-10 10:21:34 +02003636 /* queue up frame and kick off work to process it */
3637 skb_queue_tail(&sdata->skb_queue, rx->skb);
3638 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003639 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003640 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003641
3642 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003643}
3644
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003645static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3646 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003647{
3648 struct ieee80211_sub_if_data *sdata;
3649 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003650 struct sk_buff *skb = rx->skb, *skb2;
3651 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003652 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003653 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003654
Johannes Berg554891e2010-09-24 12:38:25 +02003655 /*
3656 * If cooked monitor has been processed already, then
3657 * don't do it again. If not, set the flag.
3658 */
3659 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003660 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003661 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003662
Johannes Berg152c4772011-10-21 10:22:22 +02003663 /* If there are no cooked monitor interfaces, just free the SKB */
3664 if (!local->cooked_mntrs)
3665 goto out_free_skb;
3666
Johannes Berg1f7bba72014-11-06 22:56:36 +01003667 /* vendor data is long removed here */
3668 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003669 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003670 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003671
3672 if (skb_headroom(skb) < needed_headroom &&
3673 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003674 goto out_free_skb;
3675
Johannes Berg293702a2012-03-02 13:18:19 +01003676 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003677 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3678 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003679
Zhang Shengjud57a5442016-03-03 01:16:56 +00003680 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003681 skb->ip_summed = CHECKSUM_UNNECESSARY;
3682 skb->pkt_type = PACKET_OTHERHOST;
3683 skb->protocol = htons(ETH_P_802_2);
3684
3685 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01003686 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003687 continue;
3688
Johannes Berg05c914f2008-09-11 00:01:58 +02003689 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003690 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003691 continue;
3692
3693 if (prev_dev) {
3694 skb2 = skb_clone(skb, GFP_ATOMIC);
3695 if (skb2) {
3696 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003697 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003698 }
3699 }
3700
3701 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +02003702 ieee80211_rx_stats(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003703 }
3704
3705 if (prev_dev) {
3706 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003707 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003708 return;
3709 }
Michael Wu3d30d942008-01-31 19:48:27 +01003710
3711 out_free_skb:
3712 dev_kfree_skb(skb);
3713}
3714
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003715static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3716 ieee80211_rx_result res)
3717{
3718 switch (res) {
3719 case RX_DROP_MONITOR:
3720 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3721 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003722 rx->sta->rx_stats.dropped++;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003723 fallthrough;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003724 case RX_CONTINUE: {
3725 struct ieee80211_rate *rate = NULL;
3726 struct ieee80211_supported_band *sband;
3727 struct ieee80211_rx_status *status;
3728
3729 status = IEEE80211_SKB_RXCB((rx->skb));
3730
3731 sband = rx->local->hw.wiphy->bands[status->band];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003732 if (status->encoding == RX_ENC_LEGACY)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003733 rate = &sband->bitrates[status->rate_idx];
3734
3735 ieee80211_rx_cooked_monitor(rx, rate);
3736 break;
3737 }
3738 case RX_DROP_UNUSABLE:
3739 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3740 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003741 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003742 dev_kfree_skb(rx->skb);
3743 break;
3744 case RX_QUEUED:
3745 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3746 break;
3747 }
3748}
3749
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003750static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3751 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003752{
3753 ieee80211_rx_result res = RX_DROP_MONITOR;
3754 struct sk_buff *skb;
3755
3756#define CALL_RXH(rxh) \
3757 do { \
3758 res = rxh(rx); \
3759 if (res != RX_CONTINUE) \
3760 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003761 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003762
Johannes Berg45ceeee2015-03-16 09:08:20 +01003763 /* Lock here to avoid hitting all of the data used in the RX
3764 * path (e.g. key data, station data, ...) concurrently when
3765 * a frame is released from the reorder buffer due to timeout
3766 * from the timer, potentially concurrently with RX from the
3767 * driver.
3768 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003769 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003770
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003771 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003772 /*
3773 * all the other fields are valid across frames
3774 * that belong to an aMPDU since they are on the
3775 * same TID from the same station
3776 */
3777 rx->skb = skb;
3778
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003779 CALL_RXH(ieee80211_rx_h_check_more_data);
3780 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3781 CALL_RXH(ieee80211_rx_h_sta_process);
3782 CALL_RXH(ieee80211_rx_h_decrypt);
3783 CALL_RXH(ieee80211_rx_h_defragment);
3784 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003785 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003786#ifdef CONFIG_MAC80211_MESH
3787 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3788 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3789#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003790 CALL_RXH(ieee80211_rx_h_amsdu);
3791 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003792
3793 /* special treatment -- needs the queue */
3794 res = ieee80211_rx_h_ctrl(rx, frames);
3795 if (res != RX_CONTINUE)
3796 goto rxh_next;
3797
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003798 CALL_RXH(ieee80211_rx_h_mgmt_check);
3799 CALL_RXH(ieee80211_rx_h_action);
3800 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
Johannes Berg1ea02222020-05-26 10:31:33 +02003801 CALL_RXH(ieee80211_rx_h_action_post_userspace);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003802 CALL_RXH(ieee80211_rx_h_action_return);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003803 CALL_RXH(ieee80211_rx_h_ext);
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003804 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003805
3806 rxh_next:
3807 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003808
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003809#undef CALL_RXH
3810 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003811
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003812 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003813}
Johannes Berg571ecf62007-07-27 15:43:22 +02003814
Johannes Berg4406c372010-09-24 11:21:06 +02003815static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003816{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003817 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003818 ieee80211_rx_result res = RX_DROP_MONITOR;
3819
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003820 __skb_queue_head_init(&reorder_release);
3821
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003822#define CALL_RXH(rxh) \
3823 do { \
3824 res = rxh(rx); \
3825 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003826 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003827 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003828
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003829 CALL_RXH(ieee80211_rx_h_check_dup);
3830 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003831
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003832 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003833
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003834 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003835 return;
Johannes Berg49461622008-06-30 15:10:45 +02003836
Johannes Berg2569a822009-11-25 17:46:17 +01003837 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003838 ieee80211_rx_handlers_result(rx, res);
3839
3840#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003841}
3842
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003843/*
Johannes Bergdd318572010-11-29 11:09:16 +01003844 * This function makes calls into the RX path, therefore
3845 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003846 */
3847void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3848{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003849 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003850 struct ieee80211_rx_data rx = {
3851 .sta = sta,
3852 .sdata = sta->sdata,
3853 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003854 /* This is OK -- must be QoS data frame */
3855 .security_idx = tid,
3856 .seqno_idx = tid,
Johannes Berg554891e2010-09-24 12:38:25 +02003857 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003858 struct tid_ampdu_rx *tid_agg_rx;
3859
3860 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3861 if (!tid_agg_rx)
3862 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003863
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003864 __skb_queue_head_init(&frames);
3865
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003866 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003867 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003868 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003869
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003870 if (!skb_queue_empty(&frames)) {
3871 struct ieee80211_event event = {
3872 .type = BA_FRAME_TIMEOUT,
3873 .u.ba.tid = tid,
3874 .u.ba.sta = &sta->sta,
3875 };
3876 drv_event_callback(rx.local, rx.sdata, &event);
3877 }
3878
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003879 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003880}
3881
Sara Sharon06470f72016-01-28 16:19:25 +02003882void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3883 u16 ssn, u64 filtered,
3884 u16 received_mpdus)
3885{
3886 struct sta_info *sta;
3887 struct tid_ampdu_rx *tid_agg_rx;
3888 struct sk_buff_head frames;
3889 struct ieee80211_rx_data rx = {
3890 /* This is OK -- must be QoS data frame */
3891 .security_idx = tid,
3892 .seqno_idx = tid,
3893 };
3894 int i, diff;
3895
3896 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3897 return;
3898
3899 __skb_queue_head_init(&frames);
3900
3901 sta = container_of(pubsta, struct sta_info, sta);
3902
3903 rx.sta = sta;
3904 rx.sdata = sta->sdata;
3905 rx.local = sta->local;
3906
3907 rcu_read_lock();
3908 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3909 if (!tid_agg_rx)
3910 goto out;
3911
3912 spin_lock_bh(&tid_agg_rx->reorder_lock);
3913
3914 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3915 int release;
3916
3917 /* release all frames in the reorder buffer */
3918 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3919 IEEE80211_SN_MODULO;
3920 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3921 release, &frames);
3922 /* update ssn to match received ssn */
3923 tid_agg_rx->head_seq_num = ssn;
3924 } else {
3925 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3926 &frames);
3927 }
3928
3929 /* handle the case that received ssn is behind the mac ssn.
3930 * it can be tid_agg_rx->buf_size behind and still be valid */
3931 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3932 if (diff >= tid_agg_rx->buf_size) {
3933 tid_agg_rx->reorder_buf_filtered = 0;
3934 goto release;
3935 }
3936 filtered = filtered >> diff;
3937 ssn += diff;
3938
3939 /* update bitmap */
3940 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3941 int index = (ssn + i) % tid_agg_rx->buf_size;
3942
3943 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3944 if (filtered & BIT_ULL(i))
3945 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3946 }
3947
3948 /* now process also frames that the filter marking released */
3949 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3950
3951release:
3952 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3953
3954 ieee80211_rx_handlers(&rx, &frames);
3955
3956 out:
3957 rcu_read_unlock();
3958}
3959EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3960
Johannes Berg571ecf62007-07-27 15:43:22 +02003961/* main receive path */
3962
Johannes Berga58fbe12015-04-22 15:08:39 +02003963static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003964{
Johannes Berg20b01f82010-09-24 11:21:05 +02003965 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003966 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02003967 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003968 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3969 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Thomas Pedersen09a740c2020-09-21 19:28:14 -07003970 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
3971 ieee80211_is_s1g_beacon(hdr->frame_control);
Johannes Berg23a24de2007-07-27 15:43:22 +02003972
Johannes Berg51fb61e2007-12-19 01:31:27 +01003973 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003974 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01003975 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003976 return false;
Johannes Berg588f7d32019-02-13 15:13:30 +01003977 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3978 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003979 if (multicast)
3980 return true;
3981 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003982 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02003983 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003984 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02003985 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3986 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003987 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003988 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003989 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02003990 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003991 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003992 if (!multicast &&
3993 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003994 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003995 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003996 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02003997 if (status->encoding != RX_ENC_LEGACY)
Johannes Berg56146182012-11-09 15:07:02 +01003998 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003999 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01004000 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01004001 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4002 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004003 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004004 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004005 case NL80211_IFTYPE_OCB:
4006 if (!bssid)
4007 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02004008 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004009 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004010 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004011 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004012 if (!multicast &&
4013 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02004014 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004015 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004016 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02004017 if (status->encoding != RX_ENC_LEGACY)
Rostislav Lisovy239281f2014-11-03 10:33:19 +01004018 rate_idx = 0; /* TODO: HT rates */
4019 else
4020 rate_idx = status->rate_idx;
4021 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4022 BIT(rate_idx));
4023 }
Johannes Berga58fbe12015-04-22 15:08:39 +02004024 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004025 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg736a80b2018-01-04 15:51:53 +01004026 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4027 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004028 if (multicast)
4029 return true;
4030 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02004031 case NL80211_IFTYPE_AP_VLAN:
4032 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02004033 if (!bssid)
4034 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4035
4036 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01004037 /*
4038 * Accept public action frames even when the
4039 * BSSID doesn't match, this is used for P2P
4040 * and location updates. Note that mac80211
4041 * itself never looks at these frames.
4042 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02004043 if (!multicast &&
4044 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004045 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02004046 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004047 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02004048 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02004049 }
4050
4051 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004052 /* ignore data frames to TDLS-peers */
4053 if (ieee80211_is_data(hdr->frame_control))
4054 return false;
4055 /* ignore action frames to TDLS-peers */
4056 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02004057 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03004058 !ether_addr_equal(bssid, hdr->addr1))
4059 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004060 }
Johannes Berg3018e942017-04-20 21:32:16 +02004061
4062 /*
4063 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4064 * the BSSID - we've checked that already but may have accepted
4065 * the wildcard (ff:ff:ff:ff:ff:ff).
4066 *
4067 * It also says:
4068 * The BSSID of the Data frame is determined as follows:
4069 * a) If the STA is contained within an AP or is associated
4070 * with an AP, the BSSID is the address currently in use
4071 * by the STA contained in the AP.
4072 *
4073 * So we should not accept data frames with an address that's
4074 * multicast.
4075 *
4076 * Accepting it also opens a security problem because stations
4077 * could encrypt it with the GTK and inject traffic that way.
4078 */
4079 if (ieee80211_is_data(hdr->frame_control) && multicast)
4080 return false;
4081
Johannes Berga58fbe12015-04-22 15:08:39 +02004082 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004083 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07004084 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004085 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004086 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
Johannes Bergf142c6b2012-06-18 20:07:15 +02004087 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02004088 return ieee80211_is_public_action(hdr, skb->len) ||
4089 ieee80211_is_probe_req(hdr->frame_control) ||
4090 ieee80211_is_probe_resp(hdr->frame_control) ||
4091 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03004092 case NL80211_IFTYPE_NAN:
4093 /* Currently no frames on NAN interface are allowed */
4094 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02004095 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02004096 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02004097 }
4098
Johannes Berga58fbe12015-04-22 15:08:39 +02004099 WARN_ON_ONCE(1);
4100 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004101}
4102
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004103void ieee80211_check_fast_rx(struct sta_info *sta)
4104{
4105 struct ieee80211_sub_if_data *sdata = sta->sdata;
4106 struct ieee80211_local *local = sdata->local;
4107 struct ieee80211_key *key;
4108 struct ieee80211_fast_rx fastrx = {
4109 .dev = sdata->dev,
4110 .vif_type = sdata->vif.type,
4111 .control_port_protocol = sdata->control_port_protocol,
4112 }, *old, *new = NULL;
4113 bool assign = false;
4114
4115 /* use sparse to check that we don't return without updating */
4116 __acquire(check_fast_rx);
4117
4118 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4119 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4120 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4121 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4122
Johannes Bergc9c59622016-03-31 20:02:11 +03004123 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4124
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004125 /* fast-rx doesn't do reordering */
4126 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4127 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4128 goto clear;
4129
4130 switch (sdata->vif.type) {
4131 case NL80211_IFTYPE_STATION:
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004132 if (sta->sta.tdls) {
4133 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4134 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4135 fastrx.expected_ds_bits = 0;
4136 } else {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004137 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4138 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4139 fastrx.expected_ds_bits =
4140 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4141 }
Felix Fietkau1d870162018-02-23 10:06:05 +01004142
Felix Fietkau9251a472018-02-23 13:55:50 +01004143 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4144 fastrx.expected_ds_bits |=
4145 cpu_to_le16(IEEE80211_FCTL_TODS);
4146 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4147 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4148 }
4149
Felix Fietkau1d870162018-02-23 10:06:05 +01004150 if (!sdata->u.mgd.powersave)
4151 break;
4152
4153 /* software powersave is a huge mess, avoid all of it */
4154 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4155 goto clear;
4156 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4157 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4158 goto clear;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004159 break;
4160 case NL80211_IFTYPE_AP_VLAN:
4161 case NL80211_IFTYPE_AP:
4162 /* parallel-rx requires this, at least with calls to
4163 * ieee80211_sta_ps_transition()
4164 */
4165 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4166 goto clear;
4167 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4168 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4169 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4170
4171 fastrx.internal_forward =
4172 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4173 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4174 !sdata->u.vlan.sta);
Felix Fietkau59cae5b2018-02-23 10:06:04 +01004175
4176 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4177 sdata->u.vlan.sta) {
4178 fastrx.expected_ds_bits |=
4179 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4180 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4181 fastrx.internal_forward = 0;
4182 }
4183
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004184 break;
4185 default:
4186 goto clear;
4187 }
4188
4189 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4190 goto clear;
4191
4192 rcu_read_lock();
4193 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4194 if (key) {
4195 switch (key->conf.cipher) {
4196 case WLAN_CIPHER_SUITE_TKIP:
4197 /* we don't want to deal with MMIC in fast-rx */
4198 goto clear_rcu;
4199 case WLAN_CIPHER_SUITE_CCMP:
4200 case WLAN_CIPHER_SUITE_CCMP_256:
4201 case WLAN_CIPHER_SUITE_GCMP:
4202 case WLAN_CIPHER_SUITE_GCMP_256:
4203 break;
4204 default:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01004205 /* We also don't want to deal with
4206 * WEP or cipher scheme.
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004207 */
4208 goto clear_rcu;
4209 }
4210
4211 fastrx.key = true;
4212 fastrx.icv_len = key->conf.icv_len;
4213 }
4214
4215 assign = true;
4216 clear_rcu:
4217 rcu_read_unlock();
4218 clear:
4219 __release(check_fast_rx);
4220
4221 if (assign)
4222 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4223
4224 spin_lock_bh(&sta->lock);
4225 old = rcu_dereference_protected(sta->fast_rx, true);
4226 rcu_assign_pointer(sta->fast_rx, new);
4227 spin_unlock_bh(&sta->lock);
4228
4229 if (old)
4230 kfree_rcu(old, rcu_head);
4231}
4232
4233void ieee80211_clear_fast_rx(struct sta_info *sta)
4234{
4235 struct ieee80211_fast_rx *old;
4236
4237 spin_lock_bh(&sta->lock);
4238 old = rcu_dereference_protected(sta->fast_rx, true);
4239 RCU_INIT_POINTER(sta->fast_rx, NULL);
4240 spin_unlock_bh(&sta->lock);
4241
4242 if (old)
4243 kfree_rcu(old, rcu_head);
4244}
4245
4246void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4247{
4248 struct ieee80211_local *local = sdata->local;
4249 struct sta_info *sta;
4250
4251 lockdep_assert_held(&local->sta_mtx);
4252
Madhuparna Bhowmik253216f2020-02-23 20:03:02 +05304253 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004254 if (sdata != sta->sdata &&
4255 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4256 continue;
4257 ieee80211_check_fast_rx(sta);
4258 }
4259}
4260
4261void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4262{
4263 struct ieee80211_local *local = sdata->local;
4264
4265 mutex_lock(&local->sta_mtx);
4266 __ieee80211_check_fast_rx_iface(sdata);
4267 mutex_unlock(&local->sta_mtx);
4268}
4269
4270static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4271 struct ieee80211_fast_rx *fast_rx)
4272{
4273 struct sk_buff *skb = rx->skb;
4274 struct ieee80211_hdr *hdr = (void *)skb->data;
4275 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4276 struct sta_info *sta = rx->sta;
4277 int orig_len = skb->len;
Felix Fietkau24bba072018-02-27 13:03:07 +01004278 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4279 int snap_offs = hdrlen;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004280 struct {
4281 u8 snap[sizeof(rfc1042_header)];
4282 __be16 proto;
4283 } *payload __aligned(2);
4284 struct {
4285 u8 da[ETH_ALEN];
4286 u8 sa[ETH_ALEN];
4287 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03004288 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4289
4290 if (fast_rx->uses_rss)
4291 stats = this_cpu_ptr(sta->pcpu_rx_stats);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004292
4293 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4294 * to a common data structure; drivers can implement that per queue
4295 * but we don't have that information in mac80211
4296 */
4297 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4298 return false;
4299
4300#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4301
4302 /* If using encryption, we also need to have:
4303 * - PN_VALIDATED: similar, but the implementation is tricky
4304 * - DECRYPTED: necessary for PN_VALIDATED
4305 */
4306 if (fast_rx->key &&
4307 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4308 return false;
4309
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004310 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4311 return false;
4312
4313 if (unlikely(ieee80211_is_frag(hdr)))
4314 return false;
4315
4316 /* Since our interface address cannot be multicast, this
4317 * implicitly also rejects multicast frames without the
4318 * explicit check.
4319 *
4320 * We shouldn't get any *data* frames not addressed to us
4321 * (AP mode will accept multicast *management* frames), but
4322 * punting here will make it go through the full checks in
4323 * ieee80211_accept_frame().
4324 */
4325 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4326 return false;
4327
4328 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4329 IEEE80211_FCTL_TODS)) !=
4330 fast_rx->expected_ds_bits)
Felix Fietkaub323ac12018-02-23 10:06:03 +01004331 return false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004332
4333 /* assign the key to drop unencrypted frames (later)
4334 * and strip the IV/MIC if necessary
4335 */
4336 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4337 /* GCMP header length is the same */
4338 snap_offs += IEEE80211_CCMP_HDR_LEN;
4339 }
4340
Felix Fietkau24bba072018-02-27 13:03:07 +01004341 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4342 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4343 goto drop;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004344
Felix Fietkau24bba072018-02-27 13:03:07 +01004345 payload = (void *)(skb->data + snap_offs);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004346
Felix Fietkau24bba072018-02-27 13:03:07 +01004347 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4348 return false;
4349
4350 /* Don't handle these here since they require special code.
4351 * Accept AARP and IPX even though they should come with a
4352 * bridge-tunnel header - but if we get them this way then
4353 * there's little point in discarding them.
4354 */
4355 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4356 payload->proto == fast_rx->control_port_protocol))
4357 return false;
4358 }
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004359
4360 /* after this point, don't punt to the slowpath! */
4361
4362 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4363 pskb_trim(skb, skb->len - fast_rx->icv_len))
4364 goto drop;
4365
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004366 /* statistics part of ieee80211_rx_h_sta_process() */
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004367 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Bergc9c59622016-03-31 20:02:11 +03004368 stats->last_signal = status->signal;
4369 if (!fast_rx->uses_rss)
4370 ewma_signal_add(&sta->rx_stats_avg.signal,
4371 -status->signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004372 }
4373
4374 if (status->chains) {
4375 int i;
4376
Johannes Bergc9c59622016-03-31 20:02:11 +03004377 stats->chains = status->chains;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004378 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4379 int signal = status->chain_signal[i];
4380
4381 if (!(status->chains & BIT(i)))
4382 continue;
4383
Johannes Bergc9c59622016-03-31 20:02:11 +03004384 stats->chain_signal_last[i] = signal;
4385 if (!fast_rx->uses_rss)
4386 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4387 -signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004388 }
4389 }
4390 /* end of statistics */
4391
4392 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4393 goto drop;
4394
Felix Fietkau24bba072018-02-27 13:03:07 +01004395 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4396 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4397 RX_QUEUED)
4398 goto drop;
4399
4400 return true;
4401 }
4402
4403 stats->last_rx = jiffies;
4404 stats->last_rate = sta_stats_encode_rate(status);
4405
4406 stats->fragments++;
4407 stats->packets++;
4408
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004409 /* do the header conversion - first grab the addresses */
4410 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4411 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4412 /* remove the SNAP but leave the ethertype */
4413 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4414 /* push the addresses in front */
4415 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4416
4417 skb->dev = fast_rx->dev;
4418
4419 ieee80211_rx_stats(fast_rx->dev, skb->len);
4420
4421 /* The seqno index has the same property as needed
4422 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4423 * for non-QoS-data frames. Here we know it's a data
4424 * frame, so count MSDUs.
4425 */
Johannes Bergc9c59622016-03-31 20:02:11 +03004426 u64_stats_update_begin(&stats->syncp);
4427 stats->msdu[rx->seqno_idx]++;
4428 stats->bytes += orig_len;
4429 u64_stats_update_end(&stats->syncp);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004430
4431 if (fast_rx->internal_forward) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004432 struct sk_buff *xmit_skb = NULL;
Johannes Berg42dca5e2018-10-09 10:00:21 +02004433 if (is_multicast_ether_addr(addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004434 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Johannes Berg42dca5e2018-10-09 10:00:21 +02004435 } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4436 sta_info_get(rx->sdata, addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004437 xmit_skb = skb;
4438 skb = NULL;
4439 }
4440
4441 if (xmit_skb) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004442 /*
4443 * Send to wireless media and increase priority by 256
4444 * to keep the received priority instead of
4445 * reclassifying the frame (see cfg80211_classify8021d).
4446 */
Johannes Bergeeb0d562016-12-14 16:47:43 +01004447 xmit_skb->priority += 256;
4448 xmit_skb->protocol = htons(ETH_P_802_3);
4449 skb_reset_network_header(xmit_skb);
4450 skb_reset_mac_header(xmit_skb);
4451 dev_queue_xmit(xmit_skb);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004452 }
Johannes Bergeeb0d562016-12-14 16:47:43 +01004453
4454 if (!skb)
4455 return true;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004456 }
4457
4458 /* deliver to local stack */
4459 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4460 memset(skb->cb, 0, sizeof(skb->cb));
Felix Fietkauc5d16862020-07-26 13:06:11 +02004461 if (rx->list)
4462 list_add_tail(&skb->list, rx->list);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004463 else
4464 netif_receive_skb(skb);
4465
4466 return true;
4467 drop:
4468 dev_kfree_skb(skb);
Johannes Bergc9c59622016-03-31 20:02:11 +03004469 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004470 return true;
4471}
4472
Johannes Berg571ecf62007-07-27 15:43:22 +02004473/*
Johannes Berg4406c372010-09-24 11:21:06 +02004474 * This function returns whether or not the SKB
4475 * was destined for RX processing or not, which,
4476 * if consume is true, is equivalent to whether
4477 * or not the skb was consumed.
4478 */
4479static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4480 struct sk_buff *skb, bool consume)
4481{
4482 struct ieee80211_local *local = rx->local;
4483 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004484
4485 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004486
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004487 /* See if we can do fast-rx; if we have to copy we already lost,
4488 * so punt in that case. We should never have to deliver a data
4489 * frame to multiple interfaces anyway.
4490 *
4491 * We skip the ieee80211_accept_frame() call and do the necessary
4492 * checking inside ieee80211_invoke_fast_rx().
4493 */
4494 if (consume && rx->sta) {
4495 struct ieee80211_fast_rx *fast_rx;
4496
4497 fast_rx = rcu_dereference(rx->sta->fast_rx);
4498 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4499 return true;
4500 }
4501
Johannes Berga58fbe12015-04-22 15:08:39 +02004502 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004503 return false;
4504
Johannes Berg4406c372010-09-24 11:21:06 +02004505 if (!consume) {
4506 skb = skb_copy(skb, GFP_ATOMIC);
4507 if (!skb) {
4508 if (net_ratelimit())
4509 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004510 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004511 sdata->name);
4512 return true;
4513 }
4514
4515 rx->skb = skb;
4516 }
4517
4518 ieee80211_invoke_rx_handlers(rx);
4519 return true;
4520}
4521
4522/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004523 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004524 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004525 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004526static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004527 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004528 struct sk_buff *skb,
Felix Fietkauc5d16862020-07-26 13:06:11 +02004529 struct list_head *list)
Johannes Berg571ecf62007-07-27 15:43:22 +02004530{
4531 struct ieee80211_local *local = hw_to_local(hw);
4532 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004533 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004534 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004535 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004536 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004537 struct rhlist_head *tmp;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004538 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004539
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004540 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004541 memset(&rx, 0, sizeof(rx));
4542 rx.skb = skb;
4543 rx.local = local;
Felix Fietkauc5d16862020-07-26 13:06:11 +02004544 rx.list = list;
Johannes Berg72abd812007-09-17 01:29:22 -04004545
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004546 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004547 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004548
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004549 if (ieee80211_is_mgmt(fc)) {
4550 /* drop frame if too short for header */
4551 if (skb->len < ieee80211_hdrlen(fc))
4552 err = -ENOBUFS;
4553 else
4554 err = skb_linearize(skb);
4555 } else {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004556 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004557 }
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004558
4559 if (err) {
4560 dev_kfree_skb(skb);
4561 return;
4562 }
4563
4564 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004565 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004566 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004567
Johannes Bergd48b2962012-07-06 22:19:27 +02004568 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
Thomas Pedersencd418ba2020-09-21 19:28:08 -07004569 ieee80211_is_beacon(hdr->frame_control) ||
4570 ieee80211_is_s1g_beacon(hdr->frame_control)))
Johannes Bergd48b2962012-07-06 22:19:27 +02004571 ieee80211_scan_rx(local, skb);
4572
Johannes Berg19d19e92017-02-27 09:38:11 +01004573 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004574 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004575
Johannes Berg19d19e92017-02-27 09:38:11 +01004576 if (pubsta) {
4577 rx.sta = container_of(pubsta, struct sta_info, sta);
4578 rx.sdata = rx.sta->sdata;
4579 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4580 return;
4581 goto out;
4582 }
4583
Ben Greear56af3262010-09-23 10:22:24 -07004584 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004585
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004586 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004587 if (!prev_sta) {
4588 prev_sta = sta;
4589 continue;
4590 }
4591
4592 rx.sta = prev_sta;
4593 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004594 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004595
Ben Greear56af3262010-09-23 10:22:24 -07004596 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004597 }
Ben Greear56af3262010-09-23 10:22:24 -07004598
4599 if (prev_sta) {
4600 rx.sta = prev_sta;
4601 rx.sdata = prev_sta->sdata;
4602
Johannes Berg4406c372010-09-24 11:21:06 +02004603 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004604 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304605 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004606 }
Johannes Berg4406c372010-09-24 11:21:06 +02004607 }
4608
Johannes Berg4b0dd982010-09-24 11:21:07 +02004609 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004610
Johannes Berg4b0dd982010-09-24 11:21:07 +02004611 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4612 if (!ieee80211_sdata_running(sdata))
4613 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004614
Johannes Berg4b0dd982010-09-24 11:21:07 +02004615 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4616 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4617 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004618
Johannes Berg4b0dd982010-09-24 11:21:07 +02004619 /*
4620 * frame is destined for this interface, but if it's
4621 * not also for the previous one we handle that after
4622 * the loop to avoid copying the SKB once too much
4623 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004624
Johannes Berg4b0dd982010-09-24 11:21:07 +02004625 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004626 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004627 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004628 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004629
Johannes Berg7852e362012-01-20 13:55:24 +01004630 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004631 rx.sdata = prev;
4632 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004633
Johannes Berg4b0dd982010-09-24 11:21:07 +02004634 prev = sdata;
4635 }
Johannes Berg4406c372010-09-24 11:21:06 +02004636
Johannes Berg4b0dd982010-09-24 11:21:07 +02004637 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004638 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004639 rx.sdata = prev;
4640
4641 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4642 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004643 }
Johannes Berg4406c372010-09-24 11:21:06 +02004644
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304645 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004646 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004647}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004648
4649/*
4650 * This is the receive path handler. It is called by a low level driver when an
4651 * 802.11 MPDU is received from the hardware.
4652 */
Felix Fietkauc5d16862020-07-26 13:06:11 +02004653void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4654 struct sk_buff *skb, struct list_head *list)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004655{
4656 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004657 struct ieee80211_rate *rate = NULL;
4658 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004659 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004660
Johannes Bergd20ef632009-10-11 15:10:40 +02004661 WARN_ON_ONCE(softirq_count() == 0);
4662
Johannes Berg57fbcce2016-04-12 15:56:15 +02004663 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004664 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004665
4666 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004667 if (WARN_ON(!sband))
4668 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004669
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004670 /*
4671 * If we're suspending, it is possible although not too likely
4672 * that we'd be receiving frames after having already partially
4673 * quiesced the stack. We can't process such frames then since
4674 * that might, for example, cause stations to be added or other
4675 * driver callbacks be invoked.
4676 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004677 if (unlikely(local->quiescing || local->suspended))
4678 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004679
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004680 /* We might be during a HW reconfig, prevent Rx for the same reason */
4681 if (unlikely(local->in_reconfig))
4682 goto drop;
4683
Johannes Bergea77f122009-08-21 14:44:45 +02004684 /*
4685 * The same happens when we're not even started,
4686 * but that's worth a warning.
4687 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004688 if (WARN_ON(!local->started))
4689 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004690
Johannes Bergfc885182010-07-30 13:23:12 +02004691 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004692 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004693 * Validate the rate, unless a PLCP error means that
4694 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004695 */
Johannes Bergfc885182010-07-30 13:23:12 +02004696
Johannes Bergda6a4352017-04-26 12:14:59 +02004697 switch (status->encoding) {
4698 case RX_ENC_HT:
Johannes Bergfc885182010-07-30 13:23:12 +02004699 /*
4700 * rate_idx is MCS index, which can be [0-76]
4701 * as documented on:
4702 *
Flavio Suligoi59d4bfc2020-06-05 17:41:12 +02004703 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
Johannes Bergfc885182010-07-30 13:23:12 +02004704 *
4705 * Anything else would be some sort of driver or
4706 * hardware error. The driver should catch hardware
4707 * errors.
4708 */
Johannes Berg444e3802012-09-30 17:08:35 +02004709 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004710 "Rate marked as an HT rate but passed "
4711 "status->rate_idx is not "
4712 "an MCS index [0-76]: %d (0x%02x)\n",
4713 status->rate_idx,
4714 status->rate_idx))
4715 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004716 break;
4717 case RX_ENC_VHT:
Johannes Berg56146182012-11-09 15:07:02 +01004718 if (WARN_ONCE(status->rate_idx > 9 ||
Johannes Berg8613c942017-04-26 13:51:41 +02004719 !status->nss ||
4720 status->nss > 8,
Johannes Berg56146182012-11-09 15:07:02 +01004721 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
Johannes Berg8613c942017-04-26 13:51:41 +02004722 status->rate_idx, status->nss))
Johannes Berg56146182012-11-09 15:07:02 +01004723 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004724 break;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004725 case RX_ENC_HE:
4726 if (WARN_ONCE(status->rate_idx > 11 ||
4727 !status->nss ||
4728 status->nss > 8,
4729 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4730 status->rate_idx, status->nss))
4731 goto drop;
4732 break;
Johannes Bergda6a4352017-04-26 12:14:59 +02004733 default:
4734 WARN_ON_ONCE(1);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004735 fallthrough;
Johannes Bergda6a4352017-04-26 12:14:59 +02004736 case RX_ENC_LEGACY:
Johannes Berg444e3802012-09-30 17:08:35 +02004737 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004738 goto drop;
4739 rate = &sband->bitrates[status->rate_idx];
4740 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004741 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004742
Johannes Berg554891e2010-09-24 12:38:25 +02004743 status->rx_flags = 0;
4744
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004745 /*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004746 * Frames with failed FCS/PLCP checksum are not returned,
4747 * all other frames are returned without radiotap header
4748 * if it was previously present.
4749 * Also, frames with less than 16 bytes are dropped.
4750 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004751 skb = ieee80211_rx_monitor(local, skb, rate);
Felix Fietkauc5d16862020-07-26 13:06:11 +02004752 if (!skb)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004753 return;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004754
Johannes Berge1e54062010-11-30 08:58:45 +01004755 ieee80211_tpt_led_trig_rx(local,
4756 ((struct ieee80211_hdr *)skb->data)->frame_control,
4757 skb->len);
Johannes Bergd63b5482016-03-31 20:02:02 +03004758
Felix Fietkauc5d16862020-07-26 13:06:11 +02004759 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
Johannes Berg77a980d2009-08-24 11:46:30 +02004760
4761 return;
4762 drop:
4763 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004764}
Felix Fietkauc5d16862020-07-26 13:06:11 +02004765EXPORT_SYMBOL(ieee80211_rx_list);
4766
4767void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4768 struct sk_buff *skb, struct napi_struct *napi)
4769{
4770 struct sk_buff *tmp;
4771 LIST_HEAD(list);
4772
4773
4774 /*
4775 * key references and virtual interfaces are protected using RCU
4776 * and this requires that we are in a read-side RCU section during
4777 * receive processing
4778 */
4779 rcu_read_lock();
4780 ieee80211_rx_list(hw, pubsta, skb, &list);
4781 rcu_read_unlock();
4782
4783 if (!napi) {
4784 netif_receive_skb_list(&list);
4785 return;
4786 }
4787
4788 list_for_each_entry_safe(skb, tmp, &list, list) {
4789 skb_list_del_init(skb);
4790 napi_gro_receive(napi, skb);
4791 }
4792}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004793EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004794
4795/* This is a version of the rx handler that can be called from hard irq
4796 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004797void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004798{
4799 struct ieee80211_local *local = hw_to_local(hw);
4800
4801 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4802
Johannes Berg571ecf62007-07-27 15:43:22 +02004803 skb->pkt_type = IEEE80211_RX_MSG;
4804 skb_queue_tail(&local->skb_queue, skb);
4805 tasklet_schedule(&local->tasklet);
4806}
4807EXPORT_SYMBOL(ieee80211_rx_irqsafe);