blob: 6bd24123456dacf51fe5067aa8436d2fc978559f [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 Berga6828492015-06-16 15:17:15 +020045static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
46 enum nl80211_iftype type)
47{
48 __le16 fc = hdr->frame_control;
49
50 if (ieee80211_is_data(fc)) {
51 if (len < 24) /* drop incorrect hdr len (data) */
52 return NULL;
53
54 if (ieee80211_has_a4(fc))
55 return NULL;
56 if (ieee80211_has_tods(fc))
57 return hdr->addr1;
58 if (ieee80211_has_fromds(fc))
59 return hdr->addr2;
60
61 return hdr->addr3;
62 }
63
64 if (ieee80211_is_mgmt(fc)) {
65 if (len < 24) /* drop incorrect hdr len (mgmt) */
66 return NULL;
67 return hdr->addr3;
68 }
69
70 if (ieee80211_is_ctl(fc)) {
71 if (ieee80211_is_pspoll(fc))
72 return hdr->addr1;
73
74 if (ieee80211_is_back_req(fc)) {
75 switch (type) {
76 case NL80211_IFTYPE_STATION:
77 return hdr->addr2;
78 case NL80211_IFTYPE_AP:
79 case NL80211_IFTYPE_AP_VLAN:
80 return hdr->addr1;
81 default:
82 break; /* fall through to the return */
83 }
84 }
85 }
86
87 return NULL;
88}
89
Johannes Bergb2e77712007-09-26 15:19:39 +020090/*
91 * monitor mode reception
92 *
93 * This function cleans up the SKB, i.e. it removes all the stuff
94 * only useful for monitoring.
95 */
Johannes Berg30841f52017-04-11 15:38:56 +020096static void remove_monitor_info(struct sk_buff *skb,
97 unsigned int present_fcs_len,
Johannes Bergc096b922018-04-20 13:49:18 +030098 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +020099{
Johannes Berg30841f52017-04-11 15:38:56 +0200100 if (present_fcs_len)
101 __pskb_trim(skb, skb->len - present_fcs_len);
Johannes Bergc096b922018-04-20 13:49:18 +0300102 __pskb_pull(skb, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200103}
104
Johannes Berg1f7bba72014-11-06 22:56:36 +0100105static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
Johannes Bergc096b922018-04-20 13:49:18 +0300106 unsigned int rtap_space)
Johannes Bergb2e77712007-09-26 15:19:39 +0200107{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200108 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100109 struct ieee80211_hdr *hdr;
110
Johannes Bergc096b922018-04-20 13:49:18 +0300111 hdr = (void *)(skb->data + rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200112
Johannes Berg4c298672012-07-05 11:34:31 +0200113 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100114 RX_FLAG_FAILED_PLCP_CRC |
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300115 RX_FLAG_ONLY_MONITOR |
116 RX_FLAG_NO_PSDU))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800117 return true;
118
Johannes Bergc096b922018-04-20 13:49:18 +0300119 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800120 return true;
121
Harvey Harrison87228f52008-06-11 14:21:59 -0700122 if (ieee80211_is_ctl(hdr->frame_control) &&
123 !ieee80211_is_pspoll(hdr->frame_control) &&
124 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800125 return true;
126
127 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200128}
129
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200130static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100131ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
132 struct ieee80211_rx_status *status,
133 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200134{
135 int len;
136
137 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200138 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200139
Johannes Berga144f372013-07-03 13:34:02 +0200140 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200141 if (status->chains)
142 len += 4 * hweight8(status->chains);
Johannes Bergefc38dd2018-12-15 11:03:12 +0200143 /* vendor presence bitmap */
144 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
145 len += 4;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100146
147 if (ieee80211_have_rx_timestamp(status)) {
148 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200149 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100150 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200151 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200152 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200153
Johannes Berga144f372013-07-03 13:34:02 +0200154 /* antenna field, if we don't have per-chain info */
155 if (!status->chains)
156 len += 1;
157
Johannes Berg90b9e4462012-11-16 10:09:08 +0100158 /* padding for RX_FLAGS if necessary */
159 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200160
Johannes Bergda6a4352017-04-26 12:14:59 +0200161 if (status->encoding == RX_ENC_HT) /* HT info */
Johannes Berg6d744ba2011-01-27 14:13:17 +0100162 len += 3;
163
Johannes Berg4c298672012-07-05 11:34:31 +0200164 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100165 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200166 len += 8;
167 }
168
Johannes Bergda6a4352017-04-26 12:14:59 +0200169 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100170 len = ALIGN(len, 2);
171 len += 12;
172 }
173
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300174 if (local->hw.radiotap_timestamp.units_pos >= 0) {
175 len = ALIGN(len, 8);
176 len += 12;
177 }
178
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300179 if (status->encoding == RX_ENC_HE &&
180 status->flag & RX_FLAG_RADIOTAP_HE) {
181 len = ALIGN(len, 2);
182 len += 12;
183 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
184 }
185
186 if (status->encoding == RX_ENC_HE &&
187 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
188 len = ALIGN(len, 2);
189 len += 12;
190 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
191 }
192
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300193 if (status->flag & RX_FLAG_NO_PSDU)
194 len += 1;
195
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300196 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
197 len = ALIGN(len, 2);
198 len += 4;
199 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
200 }
201
Johannes Berga144f372013-07-03 13:34:02 +0200202 if (status->chains) {
203 /* antenna and antenna signal fields */
204 len += 2 * hweight8(status->chains);
205 }
206
Johannes Berg1f7bba72014-11-06 22:56:36 +0100207 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
Liad Kaufmanc15353b2019-02-06 13:17:13 +0200208 struct ieee80211_vendor_radiotap *rtap;
209 int vendor_data_offset = 0;
210
211 /*
212 * The position to look at depends on the existence (or non-
213 * existence) of other elements, so take that into account...
214 */
215 if (status->flag & RX_FLAG_RADIOTAP_HE)
216 vendor_data_offset +=
217 sizeof(struct ieee80211_radiotap_he);
218 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
219 vendor_data_offset +=
220 sizeof(struct ieee80211_radiotap_he_mu);
221 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
222 vendor_data_offset +=
223 sizeof(struct ieee80211_radiotap_lsig);
224
225 rtap = (void *)&skb->data[vendor_data_offset];
Johannes Berg1f7bba72014-11-06 22:56:36 +0100226
Johannes Berg1f7bba72014-11-06 22:56:36 +0100227 /* alignment for fixed 6-byte vendor data header */
228 len = ALIGN(len, 2);
229 /* vendor data header */
230 len += 6;
231 if (WARN_ON(rtap->align == 0))
232 rtap->align = 1;
233 len = ALIGN(len, rtap->align);
234 len += rtap->len + rtap->pad;
235 }
236
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200237 return len;
238}
239
Johannes Berg9e478062017-04-13 14:23:49 +0200240static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
241 struct sk_buff *skb,
Johannes Bergc096b922018-04-20 13:49:18 +0300242 int rtap_space)
Johannes Berg9e478062017-04-13 14:23:49 +0200243{
244 struct {
245 struct ieee80211_hdr_3addr hdr;
246 u8 category;
247 u8 action_code;
Mathieu Malaterre7c53eb52019-01-24 19:19:57 +0100248 } __packed __aligned(2) action;
Johannes Berg9e478062017-04-13 14:23:49 +0200249
250 if (!sdata)
251 return;
252
253 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
254
Johannes Bergc096b922018-04-20 13:49:18 +0300255 if (skb->len < rtap_space + sizeof(action) +
Johannes Berg9e478062017-04-13 14:23:49 +0200256 VHT_MUMIMO_GROUPS_DATA_LEN)
257 return;
258
259 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
260 return;
261
Johannes Bergc096b922018-04-20 13:49:18 +0300262 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
Johannes Berg9e478062017-04-13 14:23:49 +0200263
264 if (!ieee80211_is_action(action.hdr.frame_control))
265 return;
266
267 if (action.category != WLAN_CATEGORY_VHT)
268 return;
269
270 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
271 return;
272
273 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
274 return;
275
276 skb = skb_copy(skb, GFP_ATOMIC);
277 if (!skb)
278 return;
279
Johannes Berg9e478062017-04-13 14:23:49 +0200280 skb_queue_tail(&sdata->skb_queue, skb);
281 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
282}
283
Johannes Berg00ea6de2012-09-05 15:54:51 +0200284/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200285 * ieee80211_add_rx_radiotap_header - add radiotap header
286 *
287 * add a radiotap header containing all the fields which the hardware provided.
288 */
289static void
290ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
291 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200292 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200293 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200294{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200295 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200296 struct ieee80211_radiotap_header *rthdr;
297 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200298 __le32 *it_present;
299 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100300 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200301 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200302 int mpdulen, chain;
303 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100304 struct ieee80211_vendor_radiotap rtap = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300305 struct ieee80211_radiotap_he he = {};
306 struct ieee80211_radiotap_he_mu he_mu = {};
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300307 struct ieee80211_radiotap_lsig lsig = {};
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300308
309 if (status->flag & RX_FLAG_RADIOTAP_HE) {
310 he = *(struct ieee80211_radiotap_he *)skb->data;
311 skb_pull(skb, sizeof(he));
312 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
313 }
314
315 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
316 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
317 skb_pull(skb, sizeof(he_mu));
318 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100319
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300320 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
321 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
322 skb_pull(skb, sizeof(lsig));
323 }
324
Johannes Berg1f7bba72014-11-06 22:56:36 +0100325 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
326 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
327 /* rtap.len and rtap.pad are undone immediately */
328 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
329 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800330
331 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200332 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800333 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200334
Johannes Bergd58ff352017-06-16 14:29:23 +0200335 rthdr = skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100336 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200337 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200338
339 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200340 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200341 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
342 BIT(IEEE80211_RADIOTAP_CHANNEL) |
343 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200344
Johannes Berga144f372013-07-03 13:34:02 +0200345 if (!status->chains)
346 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
347
348 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
349 it_present_val |=
350 BIT(IEEE80211_RADIOTAP_EXT) |
351 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
352 put_unaligned_le32(it_present_val, it_present);
353 it_present++;
354 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
355 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
356 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200357
Johannes Berg1f7bba72014-11-06 22:56:36 +0100358 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
359 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
360 BIT(IEEE80211_RADIOTAP_EXT);
361 put_unaligned_le32(it_present_val, it_present);
362 it_present++;
363 it_present_val = rtap.present;
364 }
365
Johannes Berga144f372013-07-03 13:34:02 +0200366 put_unaligned_le32(it_present_val, it_present);
367
368 pos = (void *)(it_present + 1);
369
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200370 /* the order of the following fields is important */
371
372 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800373 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100374 /* padding */
375 while ((pos - (u8 *)rthdr) & 7)
376 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800377 put_unaligned_le64(
378 ieee80211_calculate_rx_timestamp(local, status,
379 mpdulen, 0),
380 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200381 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200382 pos += 8;
383 }
384
385 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200386 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200387 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100388 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
389 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200390 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200391 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200392 pos++;
393
394 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergda6a4352017-04-26 12:14:59 +0200395 if (!rate || status->encoding != RX_ENC_LEGACY) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200396 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100397 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530398 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200399 * added below. The byte here is needed as padding
400 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200401 */
402 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200403 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200404 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200405 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Johannes Bergda6a4352017-04-26 12:14:59 +0200406 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200407 shift = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200408 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200409 shift = 2;
410 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200411 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200412 pos++;
413
414 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100415 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200416 pos += 2;
Johannes Bergda6a4352017-04-26 12:14:59 +0200417 if (status->bw == RATE_INFO_BW_10)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200418 channel_flags |= IEEE80211_CHAN_HALF;
Johannes Bergda6a4352017-04-26 12:14:59 +0200419 else if (status->bw == RATE_INFO_BW_5)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200420 channel_flags |= IEEE80211_CHAN_QUARTER;
421
Johannes Berg57fbcce2016-04-12 15:56:15 +0200422 if (status->band == NL80211_BAND_5GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200423 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Bergda6a4352017-04-26 12:14:59 +0200424 else if (status->encoding != RX_ENC_LEGACY)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200425 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100426 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200427 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100428 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100429 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100430 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200431 channel_flags |= IEEE80211_CHAN_2GHZ;
432 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200433 pos += 2;
434
435 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200436 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100437 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200438 *pos = status->signal;
439 rthdr->it_present |=
440 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
441 pos++;
442 }
443
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200444 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
445
Johannes Berga144f372013-07-03 13:34:02 +0200446 if (!status->chains) {
447 /* IEEE80211_RADIOTAP_ANTENNA */
448 *pos = status->antenna;
449 pos++;
450 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200451
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200452 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
453
454 /* IEEE80211_RADIOTAP_RX_FLAGS */
455 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100456 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100457 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100458 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100459 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
460 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200461 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100462
Johannes Bergda6a4352017-04-26 12:14:59 +0200463 if (status->encoding == RX_ENC_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200464 unsigned int stbc;
465
Johannes Berg6d744ba2011-01-27 14:13:17 +0100466 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200467 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100468 *pos = 0;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200469 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100470 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
Johannes Bergda6a4352017-04-26 12:14:59 +0200471 if (status->bw == RATE_INFO_BW_40)
Johannes Berg6d744ba2011-01-27 14:13:17 +0100472 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200473 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
Johannes Bergac55d2f2012-05-10 09:09:10 +0200474 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200475 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200476 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200477 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200478 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100479 pos++;
480 *pos++ = status->rate_idx;
481 }
Johannes Berg4c298672012-07-05 11:34:31 +0200482
483 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
484 u16 flags = 0;
485
486 /* ensure 4 byte alignment */
487 while ((pos - (u8 *)rthdr) & 3)
488 pos++;
489 rthdr->it_present |=
490 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
491 put_unaligned_le32(status->ampdu_reference, pos);
492 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200493 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
494 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
495 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
496 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
497 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
498 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
499 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
500 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
Johannes Berg7299d6f2018-02-19 14:48:39 +0200501 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
502 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
503 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
504 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
Johannes Berg4c298672012-07-05 11:34:31 +0200505 put_unaligned_le16(flags, pos);
506 pos += 2;
507 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
508 *pos++ = status->ampdu_delimiter_crc;
509 else
510 *pos++ = 0;
511 *pos++ = 0;
512 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100513
Johannes Bergda6a4352017-04-26 12:14:59 +0200514 if (status->encoding == RX_ENC_VHT) {
Johannes Berg51648922012-11-22 23:00:18 +0100515 u16 known = local->hw.radiotap_vht_details;
516
517 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100518 put_unaligned_le16(known, pos);
519 pos += 2;
520 /* flags */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200521 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
Johannes Berg51648922012-11-22 23:00:18 +0100522 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200523 /* in VHT, STBC is binary */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200524 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200525 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200526 if (status->enc_flags & RX_ENC_FLAG_BF)
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200527 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100528 pos++;
529 /* bandwidth */
Johannes Bergda6a4352017-04-26 12:14:59 +0200530 switch (status->bw) {
531 case RATE_INFO_BW_80:
Johannes Berg51648922012-11-22 23:00:18 +0100532 *pos++ = 4;
Johannes Bergda6a4352017-04-26 12:14:59 +0200533 break;
534 case RATE_INFO_BW_160:
Johannes Berg51648922012-11-22 23:00:18 +0100535 *pos++ = 11;
Johannes Bergda6a4352017-04-26 12:14:59 +0200536 break;
537 case RATE_INFO_BW_40:
Johannes Berg51648922012-11-22 23:00:18 +0100538 *pos++ = 1;
Johannes Bergda6a4352017-04-26 12:14:59 +0200539 break;
540 default:
Johannes Berg51648922012-11-22 23:00:18 +0100541 *pos++ = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200542 }
Johannes Berg51648922012-11-22 23:00:18 +0100543 /* MCS/NSS */
Johannes Berg8613c942017-04-26 13:51:41 +0200544 *pos = (status->rate_idx << 4) | status->nss;
Johannes Berg51648922012-11-22 23:00:18 +0100545 pos += 4;
546 /* coding field */
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200547 if (status->enc_flags & RX_ENC_FLAG_LDPC)
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200548 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100549 pos++;
550 /* group ID */
551 pos++;
552 /* partial_aid */
553 pos += 2;
554 }
555
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300556 if (local->hw.radiotap_timestamp.units_pos >= 0) {
557 u16 accuracy = 0;
558 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
559
560 rthdr->it_present |=
561 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
562
563 /* ensure 8 byte alignment */
564 while ((pos - (u8 *)rthdr) & 7)
565 pos++;
566
567 put_unaligned_le64(status->device_timestamp, pos);
568 pos += sizeof(u64);
569
570 if (local->hw.radiotap_timestamp.accuracy >= 0) {
571 accuracy = local->hw.radiotap_timestamp.accuracy;
572 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
573 }
574 put_unaligned_le16(accuracy, pos);
575 pos += sizeof(u16);
576
577 *pos++ = local->hw.radiotap_timestamp.units_pos;
578 *pos++ = flags;
579 }
580
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300581 if (status->encoding == RX_ENC_HE &&
582 status->flag & RX_FLAG_RADIOTAP_HE) {
Johannes Berg331aead2018-08-31 11:31:07 +0300583#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300584
585 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
586 he.data6 |= HE_PREP(DATA6_NSTS,
587 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
588 status->enc_flags));
589 he.data3 |= HE_PREP(DATA3_STBC, 1);
590 } else {
591 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
592 }
593
594#define CHECK_GI(s) \
595 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
596 (int)NL80211_RATE_INFO_HE_GI_##s)
597
598 CHECK_GI(0_8);
599 CHECK_GI(1_6);
600 CHECK_GI(3_2);
601
602 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
603 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
604 he.data3 |= HE_PREP(DATA3_CODING,
605 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
606
607 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
608
609 switch (status->bw) {
610 case RATE_INFO_BW_20:
611 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
612 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
613 break;
614 case RATE_INFO_BW_40:
615 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
616 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
617 break;
618 case RATE_INFO_BW_80:
619 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
620 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
621 break;
622 case RATE_INFO_BW_160:
623 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
624 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
625 break;
626 case RATE_INFO_BW_HE_RU:
627#define CHECK_RU_ALLOC(s) \
628 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
629 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
630
631 CHECK_RU_ALLOC(26);
632 CHECK_RU_ALLOC(52);
633 CHECK_RU_ALLOC(106);
634 CHECK_RU_ALLOC(242);
635 CHECK_RU_ALLOC(484);
636 CHECK_RU_ALLOC(996);
637 CHECK_RU_ALLOC(2x996);
638
639 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
640 status->he_ru + 4);
641 break;
642 default:
643 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
644 }
645
646 /* ensure 2 byte alignment */
647 while ((pos - (u8 *)rthdr) & 1)
648 pos++;
649 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
650 memcpy(pos, &he, sizeof(he));
651 pos += sizeof(he);
652 }
653
654 if (status->encoding == RX_ENC_HE &&
655 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
656 /* ensure 2 byte alignment */
657 while ((pos - (u8 *)rthdr) & 1)
658 pos++;
659 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
660 memcpy(pos, &he_mu, sizeof(he_mu));
661 pos += sizeof(he_mu);
662 }
663
Shaul Triebitzc3d1f872018-09-05 08:06:06 +0300664 if (status->flag & RX_FLAG_NO_PSDU) {
665 rthdr->it_present |=
666 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
667 *pos++ = status->zero_length_psdu_type;
668 }
669
Shaul Triebitzd1332e72018-08-31 11:31:20 +0300670 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
671 /* ensure 2 byte alignment */
672 while ((pos - (u8 *)rthdr) & 1)
673 pos++;
674 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
675 memcpy(pos, &lsig, sizeof(lsig));
676 pos += sizeof(lsig);
677 }
678
Johannes Berga144f372013-07-03 13:34:02 +0200679 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
680 *pos++ = status->chain_signal[chain];
681 *pos++ = chain;
682 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100683
684 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
685 /* ensure 2 byte alignment for the vendor field as required */
686 if ((pos - (u8 *)rthdr) & 1)
687 *pos++ = 0;
688 *pos++ = rtap.oui[0];
689 *pos++ = rtap.oui[1];
690 *pos++ = rtap.oui[2];
691 *pos++ = rtap.subns;
692 put_unaligned_le16(rtap.len, pos);
693 pos += 2;
694 /* align the actual payload as requested */
695 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
696 *pos++ = 0;
697 /* data (and possible padding) already follows */
698 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200699}
700
Johannes Berg127f60b2017-04-13 15:50:27 +0200701static struct sk_buff *
702ieee80211_make_monitor_skb(struct ieee80211_local *local,
703 struct sk_buff **origskb,
704 struct ieee80211_rate *rate,
Johannes Bergc096b922018-04-20 13:49:18 +0300705 int rtap_space, bool use_origskb)
Johannes Berg127f60b2017-04-13 15:50:27 +0200706{
707 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
708 int rt_hdrlen, needed_headroom;
709 struct sk_buff *skb;
710
711 /* room for the radiotap header based on driver features */
712 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
Johannes Bergc096b922018-04-20 13:49:18 +0300713 needed_headroom = rt_hdrlen - rtap_space;
Johannes Berg127f60b2017-04-13 15:50:27 +0200714
715 if (use_origskb) {
716 /* only need to expand headroom if necessary */
717 skb = *origskb;
718 *origskb = NULL;
719
720 /*
721 * This shouldn't trigger often because most devices have an
722 * RX header they pull before we get here, and that should
723 * be big enough for our radiotap information. We should
724 * probably export the length to drivers so that we can have
725 * them allocate enough headroom to start with.
726 */
727 if (skb_headroom(skb) < needed_headroom &&
728 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
729 dev_kfree_skb(skb);
730 return NULL;
731 }
732 } else {
733 /*
734 * Need to make a copy and possibly remove radiotap header
735 * and FCS from the original.
736 */
737 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
738
739 if (!skb)
740 return NULL;
741 }
742
743 /* prepend radiotap information */
744 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
745
746 skb_reset_mac_header(skb);
747 skb->ip_summed = CHECKSUM_UNNECESSARY;
748 skb->pkt_type = PACKET_OTHERHOST;
749 skb->protocol = htons(ETH_P_802_2);
750
751 return skb;
752}
753
Johannes Bergb2e77712007-09-26 15:19:39 +0200754/*
755 * This function copies a received frame to all monitor interfaces and
756 * returns a cleaned-up SKB that no longer includes the FCS nor the
757 * radiotap header the driver might have added.
758 */
759static struct sk_buff *
760ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100761 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200762{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200763 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200764 struct ieee80211_sub_if_data *sdata;
Johannes Berg127f60b2017-04-13 15:50:27 +0200765 struct sk_buff *monskb = NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200766 int present_fcs_len = 0;
Johannes Bergc096b922018-04-20 13:49:18 +0300767 unsigned int rtap_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300768 struct ieee80211_sub_if_data *monitor_sdata =
769 rcu_dereference(local->monitor_sdata);
Johannes Berg127f60b2017-04-13 15:50:27 +0200770 bool only_monitor = false;
Ilan Peer80209192018-12-15 11:03:17 +0200771 unsigned int min_head_len;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100772
Luca Coelho41cbb0f2018-06-09 09:14:44 +0300773 if (status->flag & RX_FLAG_RADIOTAP_HE)
774 rtap_space += sizeof(struct ieee80211_radiotap_he);
775
776 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
777 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
778
Ilan Peerd359bbc2018-12-15 11:03:25 +0200779 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
780 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
781
Johannes Berg1f7bba72014-11-06 22:56:36 +0100782 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
Ilan Peerd359bbc2018-12-15 11:03:25 +0200783 struct ieee80211_vendor_radiotap *rtap =
784 (void *)(origskb->data + rtap_space);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100785
Johannes Bergc096b922018-04-20 13:49:18 +0300786 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100787 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200788
Ilan Peer80209192018-12-15 11:03:17 +0200789 min_head_len = rtap_space;
790
Johannes Bergb2e77712007-09-26 15:19:39 +0200791 /*
792 * First, we may need to make a copy of the skb because
793 * (1) we need to modify it for radiotap (if not present), and
794 * (2) the other RX handlers will modify the skb we got.
795 *
796 * We don't need to, of course, if we aren't going to return
797 * the SKB because it has a bad FCS/PLCP checksum.
798 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100799
Ilan Peer80209192018-12-15 11:03:17 +0200800 if (!(status->flag & RX_FLAG_NO_PSDU)) {
801 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
802 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
803 /* driver bug */
804 WARN_ON(1);
805 dev_kfree_skb(origskb);
806 return NULL;
807 }
808 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200809 }
Ilan Peer80209192018-12-15 11:03:17 +0200810
811 /* also consider the hdr->frame_control */
812 min_head_len += 2;
Johannes Berg30841f52017-04-11 15:38:56 +0200813 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200814
Ilan Peer80209192018-12-15 11:03:17 +0200815 /* ensure that the expected data elements are in skb head */
816 if (!pskb_may_pull(origskb, min_head_len)) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800817 dev_kfree_skb(origskb);
818 return NULL;
819 }
820
Johannes Bergc096b922018-04-20 13:49:18 +0300821 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
Johannes Berg127f60b2017-04-13 15:50:27 +0200822
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100823 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200824 if (only_monitor) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200825 dev_kfree_skb(origskb);
826 return NULL;
827 }
828
Johannes Bergc096b922018-04-20 13:49:18 +0300829 remove_monitor_info(origskb, present_fcs_len, rtap_space);
Johannes Berg30841f52017-04-11 15:38:56 +0200830 return origskb;
Johannes Bergb2e77712007-09-26 15:19:39 +0200831 }
832
Johannes Bergc096b922018-04-20 13:49:18 +0300833 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
Johannes Berg9e478062017-04-13 14:23:49 +0200834
Johannes Bergf64331d2017-04-13 13:28:18 +0200835 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
Johannes Berg127f60b2017-04-13 15:50:27 +0200836 bool last_monitor = list_is_last(&sdata->u.mntr.list,
837 &local->mon_list);
838
839 if (!monskb)
840 monskb = ieee80211_make_monitor_skb(local, &origskb,
Johannes Bergc096b922018-04-20 13:49:18 +0300841 rate, rtap_space,
Johannes Berg127f60b2017-04-13 15:50:27 +0200842 only_monitor &&
843 last_monitor);
844
845 if (monskb) {
846 struct sk_buff *skb;
847
848 if (last_monitor) {
849 skb = monskb;
850 monskb = NULL;
851 } else {
852 skb = skb_clone(monskb, GFP_ATOMIC);
853 }
854
855 if (skb) {
856 skb->dev = sdata->dev;
857 ieee80211_rx_stats(skb->dev, skb->len);
858 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200859 }
860 }
861
Johannes Berg127f60b2017-04-13 15:50:27 +0200862 if (last_monitor)
863 break;
Johannes Bergb2e77712007-09-26 15:19:39 +0200864 }
865
Johannes Berg127f60b2017-04-13 15:50:27 +0200866 /* this happens if last_monitor was erroneously false */
867 dev_kfree_skb(monskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200868
Johannes Berg127f60b2017-04-13 15:50:27 +0200869 /* ditto */
870 if (!origskb)
871 return NULL;
872
Johannes Bergc096b922018-04-20 13:49:18 +0300873 remove_monitor_info(origskb, present_fcs_len, rtap_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200874 return origskb;
875}
876
Johannes Berg5cf121c2008-02-25 16:27:43 +0100877static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200878{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700879 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200880 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200881 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200882
883 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700884 if (ieee80211_is_data_qos(hdr->frame_control)) {
885 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200886 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700887 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200888 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200889 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200890
891 seqno_idx = tid;
892 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200893 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200894 /*
895 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
896 *
897 * Sequence numbers for management frames, QoS data
898 * frames with a broadcast/multicast address in the
899 * Address 1 field, and all non-QoS data frames sent
900 * by QoS STAs are assigned using an additional single
901 * modulo-4096 counter, [...]
902 *
903 * We also use that counter for non-QoS STAs.
904 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100905 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200906 security_idx = 0;
907 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100908 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200909 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200910 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200911
Johannes Berg9e262972011-07-07 18:45:03 +0200912 rx->seqno_idx = seqno_idx;
913 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200914 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
915 * For now, set skb->priority to 0 for other cases. */
916 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100917}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200918
Johannes Bergd1c3a372009-01-07 00:26:10 +0100919/**
920 * DOC: Packet alignment
921 *
922 * Drivers always need to pass packets that are aligned to two-byte boundaries
923 * to the stack.
924 *
925 * Additionally, should, if possible, align the payload data in a way that
926 * guarantees that the contained IP header is aligned to a four-byte
927 * boundary. In the case of regular frames, this simply means aligning the
928 * payload to a four-byte boundary (because either the IP header is directly
929 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100930 * in front of it). If the payload data is not properly aligned and the
931 * architecture doesn't support efficient unaligned operations, mac80211
932 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100933 *
934 * With A-MSDU frames, however, the payload data address must yield two modulo
935 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
936 * push the IP header further back to a multiple of four again. Thankfully, the
937 * specs were sane enough this time around to require padding each A-MSDU
938 * subframe to a length that is a multiple of four.
939 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300940 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100941 * the payload is not supported, the driver is required to move the 802.11
942 * header to be directly in front of the payload in that case.
943 */
944static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100945{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100946#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100947 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100948#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200949}
950
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200951
Johannes Berg571ecf62007-07-27 15:43:22 +0200952/* rx handlers */
953
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200954static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
955{
956 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
957
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100958 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200959 return 0;
960
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100961 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200962}
963
964
965static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
966{
967 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
968
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100969 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200970 return 0;
971
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100972 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200973}
974
975
976/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
977static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
978{
979 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
980 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200981 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200982
Johannes Berg1df332e2012-10-26 00:09:11 +0200983 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200984 return -1;
985
Jouni Malinenaf2d14b2020-02-22 15:25:47 +0200986 if (!ieee80211_is_robust_mgmt_frame(skb) &&
987 !ieee80211_is_beacon(hdr->frame_control))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200988 return -1; /* not a robust management frame */
989
990 mmie = (struct ieee80211_mmie *)
991 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200992 if (mmie->element_id == WLAN_EID_MMIE &&
993 mmie->length == sizeof(*mmie) - 2)
994 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200995
Jouni Malinen56c52da2015-01-24 19:52:08 +0200996 mmie16 = (struct ieee80211_mmie_16 *)
997 (skb->data + skb->len - sizeof(*mmie16));
998 if (skb->len >= 24 + sizeof(*mmie16) &&
999 mmie16->element_id == WLAN_EID_MMIE &&
1000 mmie16->length == sizeof(*mmie16) - 2)
1001 return le16_to_cpu(mmie16->key_id);
1002
1003 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001004}
1005
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001006static int ieee80211_get_keyid(struct sk_buff *skb,
1007 const struct ieee80211_cipher_scheme *cs)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001008{
1009 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1010 __le16 fc;
1011 int hdrlen;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001012 int minlen;
1013 u8 key_idx_off;
1014 u8 key_idx_shift;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001015 u8 keyid;
1016
1017 fc = hdr->frame_control;
1018 hdrlen = ieee80211_hdrlen(fc);
1019
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001020 if (cs) {
1021 minlen = hdrlen + cs->hdr_len;
1022 key_idx_off = hdrlen + cs->key_idx_off;
1023 key_idx_shift = cs->key_idx_shift;
1024 } else {
1025 /* WEP, TKIP, CCMP and GCMP */
1026 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1027 key_idx_off = hdrlen + 3;
1028 key_idx_shift = 6;
1029 }
1030
1031 if (unlikely(skb->len < minlen))
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001032 return -EINVAL;
1033
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001034 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1035
1036 if (cs)
1037 keyid &= cs->key_idx_mask;
1038 keyid >>= key_idx_shift;
1039
1040 /* cs could use more than the usual two bits for the keyid */
1041 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1042 return -EINVAL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001043
1044 return keyid;
1045}
1046
Johannes Berg1df332e2012-10-26 00:09:11 +02001047static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001048{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001049 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +01001050 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001051
Harvey Harrisona7767f92008-07-02 16:30:51 -07001052 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001053 if (is_multicast_ether_addr(hdr->addr1)) {
1054 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +02001055 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001056 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001057 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001058 return RX_DROP_MONITOR;
1059 } else {
1060 if (!ieee80211_has_a4(hdr->frame_control))
1061 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +00001062 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001063 return RX_DROP_MONITOR;
1064 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001065 }
1066
1067 /* If there is not an established peer link and this is not a peer link
1068 * establisment frame, beacon or probe, drop the frame.
1069 */
1070
Javier Cardona57cf8042011-05-13 10:45:43 -07001071 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001072 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +01001073
Harvey Harrisona7767f92008-07-02 16:30:51 -07001074 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001075 return RX_DROP_MONITOR;
1076
Harvey Harrisona7767f92008-07-02 16:30:51 -07001077 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001078 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001079
1080 /* make sure category field is present */
1081 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1082 return RX_DROP_MONITOR;
1083
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001084 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07001085 category = mgmt->u.action.category;
1086 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +02001087 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001088 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001089 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001090 }
1091
Harvey Harrisona7767f92008-07-02 16:30:51 -07001092 if (ieee80211_is_probe_req(hdr->frame_control) ||
1093 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -07001094 ieee80211_is_beacon(hdr->frame_control) ||
1095 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -07001096 return RX_CONTINUE;
1097
1098 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001099 }
1100
Johannes Berg902acc72008-02-23 15:17:19 +01001101 return RX_CONTINUE;
1102}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001103
Johannes Bergfb4ea052016-01-28 16:19:24 +02001104static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1105 int index)
1106{
1107 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1108 struct sk_buff *tail = skb_peek_tail(frames);
1109 struct ieee80211_rx_status *status;
1110
Sara Sharon06470f72016-01-28 16:19:25 +02001111 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1112 return true;
1113
Johannes Bergfb4ea052016-01-28 16:19:24 +02001114 if (!tail)
1115 return false;
1116
1117 status = IEEE80211_SKB_RXCB(tail);
1118 if (status->flag & RX_FLAG_AMSDU_MORE)
1119 return false;
1120
1121 return true;
1122}
1123
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001124static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001125 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001126 int index,
1127 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001128{
Michal Kazior83eb9352014-07-16 12:09:31 +02001129 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1130 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +01001131 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001132
Johannes Bergdd318572010-11-29 11:09:16 +01001133 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1134
Michal Kazior83eb9352014-07-16 12:09:31 +02001135 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001136 goto no_frame;
1137
Johannes Bergfb4ea052016-01-28 16:19:24 +02001138 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001139 __skb_queue_purge(skb_list);
1140 goto no_frame;
1141 }
1142
1143 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001144 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +02001145 while ((skb = __skb_dequeue(skb_list))) {
1146 status = IEEE80211_SKB_RXCB(skb);
1147 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1148 __skb_queue_tail(frames, skb);
1149 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001150
1151no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +02001152 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +01001153 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001154}
1155
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001156static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001157 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001158 u16 head_seq_num,
1159 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001160{
1161 int index;
1162
Johannes Bergdd318572010-11-29 11:09:16 +01001163 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1164
Johannes Berg9a886582013-02-15 19:25:00 +01001165 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001166 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001167 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1168 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001169 }
1170}
1171
1172/*
1173 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1174 * the skb was added to the buffer longer than this time ago, the earlier
1175 * frames that have not yet been received are assumed to be lost and the skb
1176 * can be released for processing. This may also release other skb's from the
1177 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001178 *
1179 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001180 */
1181#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1182
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001183static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001184 struct tid_ampdu_rx *tid_agg_rx,
1185 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001186{
Michal Kazior83eb9352014-07-16 12:09:31 +02001187 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001188
Johannes Bergdd318572010-11-29 11:09:16 +01001189 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1190
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001191 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +02001192 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +02001193 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +02001194 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001195 /*
1196 * No buffers ready to be released, but check whether any
1197 * frames in the reorder buffer have timed out.
1198 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001199 int skipped = 1;
1200 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1201 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001202 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001203 skipped++;
1204 continue;
1205 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -07001206 if (skipped &&
1207 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001208 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001209 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001210
Michal Kazior83eb9352014-07-16 12:09:31 +02001211 /* don't leave incomplete A-MSDUs around */
1212 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1213 i = (i + 1) % tid_agg_rx->buf_size)
1214 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1215
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001216 ht_dbg_ratelimited(sdata,
1217 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001218 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1219 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001220
1221 /*
1222 * Increment the head seq# also for the skipped slots.
1223 */
1224 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +01001225 (tid_agg_rx->head_seq_num +
1226 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001227 skipped = 0;
1228 }
Johannes Bergfb4ea052016-01-28 16:19:24 +02001229 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001230 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1231 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +02001232 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001233 }
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001234
1235 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +02001236 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001237
1238 for (; j != (index - 1) % tid_agg_rx->buf_size;
1239 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +02001240 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001241 break;
1242 }
1243
1244 set_release_timer:
1245
Johannes Berg788211d2015-04-01 14:20:42 +02001246 if (!tid_agg_rx->removed)
1247 mod_timer(&tid_agg_rx->reorder_timer,
1248 tid_agg_rx->reorder_time[j] + 1 +
1249 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001250 } else {
1251 del_timer(&tid_agg_rx->reorder_timer);
1252 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +02001253}
1254
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001255/*
1256 * As this function belongs to the RX path it must be under
1257 * rcu_read_lock protection. It returns false if the frame
1258 * can be processed immediately, true if it was consumed.
1259 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001260static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001261 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001262 struct sk_buff *skb,
1263 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001264{
1265 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001266 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001267 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1268 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1269 u16 head_seq_num, buf_size;
1270 int index;
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001271 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001272
Johannes Bergdd318572010-11-29 11:09:16 +01001273 spin_lock(&tid_agg_rx->reorder_lock);
1274
Michal Kazior4549cf22014-09-02 14:05:10 +02001275 /*
1276 * Offloaded BA sessions have no known starting sequence number so pick
1277 * one from first Rxed frame for this tid after BA was started.
1278 */
1279 if (unlikely(tid_agg_rx->auto_seq)) {
1280 tid_agg_rx->auto_seq = false;
1281 tid_agg_rx->ssn = mpdu_seq_num;
1282 tid_agg_rx->head_seq_num = mpdu_seq_num;
1283 }
1284
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001285 buf_size = tid_agg_rx->buf_size;
1286 head_seq_num = tid_agg_rx->head_seq_num;
1287
Sara Sharonb7540d82017-02-06 15:28:42 +02001288 /*
1289 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1290 * be reordered.
1291 */
1292 if (unlikely(!tid_agg_rx->started)) {
1293 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1294 ret = false;
1295 goto out;
1296 }
1297 tid_agg_rx->started = true;
1298 }
1299
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001300 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001301 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001302 dev_kfree_skb(skb);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001303 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001304 }
1305
1306 /*
1307 * If frame the sequence number exceeds our buffering window
1308 * size release some previous frames to make room for this one.
1309 */
Johannes Berg9a886582013-02-15 19:25:00 +01001310 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1311 head_seq_num = ieee80211_sn_inc(
1312 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001313 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001314 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001315 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001316 }
1317
1318 /* Now the new frame is always in the range of the reordering buffer */
1319
Karl Beldan2e3049b2013-10-24 15:53:32 +02001320 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001321
1322 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001323 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001324 dev_kfree_skb(skb);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001325 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001326 }
1327
1328 /*
1329 * If the current MPDU is in the right order and nothing else
1330 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001331 * If it is first but there's something stored, we may be able
1332 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001333 */
1334 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1335 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001336 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1337 tid_agg_rx->head_seq_num =
1338 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001339 ret = false;
1340 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001341 }
1342
1343 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001344 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1345 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1346 tid_agg_rx->reorder_time[index] = jiffies;
1347 tid_agg_rx->stored_mpdu_num++;
1348 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1349 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001350
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02001351 out:
1352 spin_unlock(&tid_agg_rx->reorder_lock);
1353 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001354}
1355
1356/*
1357 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1358 * true if the MPDU was buffered, false if it should be processed.
1359 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001360static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1361 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001362{
Johannes Berg2569a822009-11-25 17:46:17 +01001363 struct sk_buff *skb = rx->skb;
1364 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001365 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001366 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001367 struct tid_ampdu_rx *tid_agg_rx;
1368 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001369 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001370
Johannes Berg051a41fa2013-11-20 11:28:27 +01001371 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1372 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001373 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001374
1375 /*
1376 * filter the QoS data rx stream according to
1377 * STA/TID and check if this STA/TID is on aggregation
1378 */
1379
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001380 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001381 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001382
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001383 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1384 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001385 tid = ieee80211_get_tid(hdr);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001386
Johannes Berga87f7362010-06-10 10:21:38 +02001387 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001388 if (!tid_agg_rx) {
1389 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1390 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1391 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1392 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1393 WLAN_BACK_RECIPIENT,
1394 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001395 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001396 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001397
1398 /* qos null data frames are excluded */
1399 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001400 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001401
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001402 /* not part of a BA session */
1403 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1404 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1405 goto dont_reorder;
1406
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001407 /* new, potentially un-ordered, ampdu frame - process it */
1408
1409 /* reset session timer */
1410 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001411 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001412
1413 /* if this mpdu is fragmented - terminate rx aggregation session */
1414 sc = le16_to_cpu(hdr->seq_ctrl);
1415 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg344eec62010-06-10 10:21:36 +02001416 skb_queue_tail(&rx->sdata->skb_queue, skb);
1417 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001418 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001419 }
1420
Johannes Berga87f7362010-06-10 10:21:38 +02001421 /*
1422 * No locking needed -- we will only ever process one
1423 * RX packet at a time, and thus own tid_agg_rx. All
1424 * other code manipulating it needs to (and does) make
1425 * sure that we cannot get to it any more before doing
1426 * anything with it.
1427 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001428 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1429 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001430 return;
1431
1432 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001433 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001434}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001435
Johannes Berg49461622008-06-30 15:10:45 +02001436static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001437ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001438{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001439 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001440 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001441
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001442 if (status->flag & RX_FLAG_DUP_VALIDATED)
1443 return RX_CONTINUE;
1444
Johannes Berg6b0f3272013-07-11 22:33:26 +02001445 /*
1446 * Drop duplicate 802.11 retransmissions
1447 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1448 */
Johannes Berg03954422014-11-11 16:49:25 +01001449
1450 if (rx->skb->len < 24)
1451 return RX_CONTINUE;
1452
1453 if (ieee80211_is_ctl(hdr->frame_control) ||
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001454 ieee80211_is_any_nullfunc(hdr->frame_control) ||
Johannes Berg03954422014-11-11 16:49:25 +01001455 is_multicast_ether_addr(hdr->addr1))
1456 return RX_CONTINUE;
1457
Johannes Berga732fa72015-10-14 18:27:07 +02001458 if (!rx->sta)
1459 return RX_CONTINUE;
1460
1461 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1462 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1463 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001464 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001465 return RX_DROP_UNUSABLE;
1466 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1467 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001468 }
1469
Johannes Berg03954422014-11-11 16:49:25 +01001470 return RX_CONTINUE;
1471}
1472
1473static ieee80211_rx_result debug_noinline
1474ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1475{
1476 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1477
Johannes Berg571ecf62007-07-27 15:43:22 +02001478 /* Drop disallowed frame classes based on STA auth/assoc state;
1479 * IEEE 802.11, Chap 5.5.
1480 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001481 * mac80211 filters only based on association state, i.e. it drops
1482 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001483 * deauth/disassoc frames when needed. In addition, hostapd is
1484 * responsible for filtering on both auth and assoc states.
1485 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001486
Johannes Berg902acc72008-02-23 15:17:19 +01001487 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001488 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001489
Harvey Harrisona7767f92008-07-02 16:30:51 -07001490 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1491 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001492 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Bill Jordan1be7fe82010-10-01 11:20:41 -04001493 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001494 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001495 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001496 /*
1497 * accept port control frames from the AP even when it's not
1498 * yet marked ASSOC to prevent a race where we don't set the
1499 * assoc bit quickly enough before it sends the first frame
1500 */
1501 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001502 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001503 unsigned int hdrlen;
1504 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001505
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001506 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1507
1508 if (rx->skb->len < hdrlen + 8)
1509 return RX_DROP_MONITOR;
1510
1511 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1512 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001513 return RX_CONTINUE;
1514 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001515
1516 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1517 cfg80211_rx_spurious_frame(rx->sdata->dev,
1518 hdr->addr2,
1519 GFP_ATOMIC))
1520 return RX_DROP_UNUSABLE;
1521
Johannes Berge4c26ad2008-01-31 19:48:21 +01001522 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001523 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001524
Johannes Berg9ae54c82008-01-31 19:48:20 +01001525 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001526}
1527
1528
Johannes Berg49461622008-06-30 15:10:45 +02001529static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001530ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1531{
1532 struct ieee80211_local *local;
1533 struct ieee80211_hdr *hdr;
1534 struct sk_buff *skb;
1535
1536 local = rx->local;
1537 skb = rx->skb;
1538 hdr = (struct ieee80211_hdr *) skb->data;
1539
1540 if (!local->pspolling)
1541 return RX_CONTINUE;
1542
1543 if (!ieee80211_has_fromds(hdr->frame_control))
1544 /* this is not from AP */
1545 return RX_CONTINUE;
1546
1547 if (!ieee80211_is_data(hdr->frame_control))
1548 return RX_CONTINUE;
1549
1550 if (!ieee80211_has_moredata(hdr->frame_control)) {
1551 /* AP has no more frames buffered for us */
1552 local->pspolling = false;
1553 return RX_CONTINUE;
1554 }
1555
1556 /* more data bit is set, let's request a new frame from the AP */
1557 ieee80211_send_pspoll(local, rx->sdata);
1558
1559 return RX_CONTINUE;
1560}
1561
Marco Porschd012a602012-10-10 12:39:50 -07001562static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001563{
Johannes Berg133b8222008-09-16 14:18:59 +02001564 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001565 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001566 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001567 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001568
Marco Porschd012a602012-10-10 12:39:50 -07001569 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1570 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1571 ps = &sdata->bss->ps;
1572 else
1573 return;
1574
1575 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001576 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001577 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001578 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001579 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1580 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001581
Johannes Berg17c18bf2015-03-21 15:25:43 +01001582 ieee80211_clear_fast_xmit(sta);
1583
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001584 if (!sta->sta.txq[0])
1585 return;
1586
Johannes Bergadf8ed02018-08-31 11:31:08 +03001587 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
Felix Fietkaub49c15e2019-03-19 12:00:13 +01001588 struct ieee80211_txq *txq = sta->sta.txq[tid];
1589 struct txq_info *txqi = to_txq_info(txq);
1590
1591 spin_lock(&local->active_txq_lock[txq->ac]);
1592 if (!list_empty(&txqi->schedule_order))
1593 list_del_init(&txqi->schedule_order);
1594 spin_unlock(&local->active_txq_lock[txq->ac]);
1595
1596 if (txq_has_queue(txq))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001597 set_bit(tid, &sta->txq_buffered_tids);
1598 else
1599 clear_bit(tid, &sta->txq_buffered_tids);
1600 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001601}
1602
Marco Porschd012a602012-10-10 12:39:50 -07001603static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001604{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001605 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1606 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001607
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001608 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001609 /*
1610 * Clear the flag only if the other one is still set
1611 * so that the TX path won't start TX'ing new frames
1612 * directly ... In the case that the driver flag isn't
1613 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1614 */
1615 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001616 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1617 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001618 return;
1619 }
1620
Johannes Berg5ac2e352014-05-27 16:32:27 +02001621 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1622 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001623 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001624}
1625
Johannes Bergcf471612015-06-16 16:16:38 +02001626int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001627{
Johannes Bergcf471612015-06-16 16:16:38 +02001628 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001629 bool in_ps;
1630
Johannes Bergcf471612015-06-16 16:16:38 +02001631 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001632
1633 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001634 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001635 if ((start && in_ps) || (!start && !in_ps))
1636 return -EINVAL;
1637
1638 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001639 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001640 else
Johannes Bergcf471612015-06-16 16:16:38 +02001641 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001642
1643 return 0;
1644}
1645EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1646
Johannes Berg46fa38e2016-05-03 16:58:00 +03001647void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1648{
1649 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1650
1651 if (test_sta_flag(sta, WLAN_STA_SP))
1652 return;
1653
1654 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1655 ieee80211_sta_ps_deliver_poll_response(sta);
1656 else
1657 set_sta_flag(sta, WLAN_STA_PSPOLL);
1658}
1659EXPORT_SYMBOL(ieee80211_sta_pspoll);
1660
1661void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1662{
1663 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001664 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001665
1666 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001667 * If this AC is not trigger-enabled do nothing unless the
1668 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001669 *
1670 * NB: This could/should check a separate bitmap of trigger-
1671 * enabled queues, but for now we only implement uAPSD w/o
1672 * TSPEC changes to the ACs, so they're always the same.
1673 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001674 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1675 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001676 return;
1677
1678 /* if we are in a service period, do nothing */
1679 if (test_sta_flag(sta, WLAN_STA_SP))
1680 return;
1681
1682 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1683 ieee80211_sta_ps_deliver_uapsd(sta);
1684 else
1685 set_sta_flag(sta, WLAN_STA_UAPSD);
1686}
1687EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1688
Johannes Berg49461622008-06-30 15:10:45 +02001689static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001690ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1691{
1692 struct ieee80211_sub_if_data *sdata = rx->sdata;
1693 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1694 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001695
Johannes Berg5c900672015-04-22 14:48:34 +02001696 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001697 return RX_CONTINUE;
1698
1699 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1700 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1701 return RX_CONTINUE;
1702
1703 /*
1704 * The device handles station powersave, so don't do anything about
1705 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1706 * it to mac80211 since they're handled.)
1707 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001708 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001709 return RX_CONTINUE;
1710
1711 /*
1712 * Don't do anything if the station isn't already asleep. In
1713 * the uAPSD case, the station will probably be marked asleep,
1714 * in the PS-Poll case the station must be confused ...
1715 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001716 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001717 return RX_CONTINUE;
1718
1719 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001720 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001721
1722 /* Free PS Poll skb here instead of returning RX_DROP that would
1723 * count as an dropped frame. */
1724 dev_kfree_skb(rx->skb);
1725
1726 return RX_QUEUED;
1727 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1728 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1729 ieee80211_has_pm(hdr->frame_control) &&
1730 (ieee80211_is_data_qos(hdr->frame_control) ||
1731 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001732 u8 tid = ieee80211_get_tid(hdr);
Johannes Berg47086fc2011-09-29 16:04:33 +02001733
Johannes Berg46fa38e2016-05-03 16:58:00 +03001734 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001735 }
1736
1737 return RX_CONTINUE;
1738}
1739
1740static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001741ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001742{
1743 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001744 struct sk_buff *skb = rx->skb;
1745 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1746 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001747 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001748
1749 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001750 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001751
Johannes Bergb291ba12009-07-10 15:29:03 +02001752 /*
1753 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001754 * BSSID and for station already AUTHORIZED to avoid keeping the
1755 * current IBSS network alive in cases where other STAs start
1756 * using different BSSID. This will also give the station another
1757 * chance to restart the authentication/authorization in case
1758 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001759 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001760 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001761 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001762 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001763 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1764 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001765 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001766 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001767 !is_multicast_ether_addr(hdr->addr1))
1768 sta->rx_stats.last_rate =
1769 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001770 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001771 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001772 sta->rx_stats.last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +02001773 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1774 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001775 * Mesh beacons will update last_rx when if they are found to
1776 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001777 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001778 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001779 if (ieee80211_is_data(hdr->frame_control))
1780 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001781 }
1782
Kalle Valo3cf335d52009-03-22 21:57:06 +02001783 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1784 ieee80211_sta_rx_notify(rx->sdata, hdr);
1785
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001786 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001787
1788 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001789 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001790 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1791
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001792 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001793 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001794 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001795 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001796
Felix Fietkauef0621e2013-04-22 16:29:31 +02001797 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001798 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001799 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1800 int signal = status->chain_signal[i];
1801
1802 if (!(status->chains & BIT(i)))
1803 continue;
1804
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001805 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001806 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001807 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001808 }
1809 }
1810
Johannes Berg72eaa432008-11-26 15:02:58 +01001811 /*
1812 * Change STA power saving mode only at the end of a frame
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001813 * exchange sequence, and only for a data or management
1814 * frame as specified in IEEE 802.11-2016 11.2.3.2
Johannes Berg72eaa432008-11-26 15:02:58 +01001815 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001816 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001817 !ieee80211_has_morefrags(hdr->frame_control) &&
Emmanuel Grumbach20932752018-08-20 13:56:07 +03001818 !is_multicast_ether_addr(hdr->addr1) &&
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001819 (ieee80211_is_mgmt(hdr->frame_control) ||
1820 ieee80211_is_data(hdr->frame_control)) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001821 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001822 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Emmanuel Grumbach9fef6542017-10-29 11:51:07 +02001823 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001824 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001825 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001826 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001827 } else {
1828 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001829 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001830 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001831 }
1832
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001833 /* mesh power save support */
1834 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1835 ieee80211_mps_rx_h_sta_process(sta, hdr);
1836
Johannes Berg22403de2009-10-30 12:55:03 +01001837 /*
1838 * Drop (qos-)data::nullfunc frames silently, since they
1839 * are used only to control station power saving mode.
1840 */
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08001841 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001842 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001843
1844 /*
1845 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001846 * that was not moved to a 4-addr STA vlan yet send
1847 * the event to userspace and for older hostapd drop
1848 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001849 */
1850 if (ieee80211_has_a4(hdr->frame_control) &&
1851 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1852 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001853 !rx->sdata->u.vlan.sta))) {
1854 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1855 cfg80211_rx_unexpected_4addr_frame(
1856 rx->sdata->dev, sta->sta.addr,
1857 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001858 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001859 }
Johannes Berg22403de2009-10-30 12:55:03 +01001860 /*
1861 * Update counter and free packet here to avoid
1862 * counting this as a dropped packed.
1863 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001864 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001865 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001866 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001867 }
1868
Johannes Berg9ae54c82008-01-31 19:48:20 +01001869 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001870} /* ieee80211_rx_h_sta_process */
1871
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001872static struct ieee80211_key *
1873ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1874{
1875 struct ieee80211_key *key = NULL;
1876 struct ieee80211_sub_if_data *sdata = rx->sdata;
1877 int idx2;
1878
1879 /* Make sure key gets set if either BIGTK key index is set so that
1880 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1881 * Beacon frames and Beacon frames that claim to use another BIGTK key
1882 * index (i.e., a key that we do not have).
1883 */
1884
1885 if (idx < 0) {
1886 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1887 idx2 = idx + 1;
1888 } else {
1889 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1890 idx2 = idx + 1;
1891 else
1892 idx2 = idx - 1;
1893 }
1894
1895 if (rx->sta)
1896 key = rcu_dereference(rx->sta->gtk[idx]);
1897 if (!key)
1898 key = rcu_dereference(sdata->keys[idx]);
1899 if (!key && rx->sta)
1900 key = rcu_dereference(rx->sta->gtk[idx2]);
1901 if (!key)
1902 key = rcu_dereference(sdata->keys[idx2]);
1903
1904 return key;
1905}
1906
Johan Almbladh86c228a2013-08-14 15:29:46 +02001907static ieee80211_rx_result debug_noinline
1908ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1909{
1910 struct sk_buff *skb = rx->skb;
1911 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1912 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1913 int keyidx;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001914 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1915 struct ieee80211_key *sta_ptk = NULL;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001916 struct ieee80211_key *ptk_idx = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001917 int mmie_keyidx = -1;
1918 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001919 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001920
1921 /*
1922 * Key selection 101
1923 *
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001924 * There are five types of keys:
Johan Almbladh86c228a2013-08-14 15:29:46 +02001925 * - GTK (group keys)
1926 * - IGTK (group keys for management frames)
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001927 * - BIGTK (group keys for Beacon frames)
Johan Almbladh86c228a2013-08-14 15:29:46 +02001928 * - PTK (pairwise keys)
1929 * - STK (station-to-station pairwise keys)
1930 *
1931 * When selecting a key, we have to distinguish between multicast
1932 * (including broadcast) and unicast frames, the latter can only
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001933 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1934 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1935 * then unicast frames can also use key indices like GTKs. Hence, if we
Johan Almbladh86c228a2013-08-14 15:29:46 +02001936 * don't have a PTK/STK we check the key index for a WEP key.
1937 *
1938 * Note that in a regular BSS, multicast frames are sent by the
1939 * AP only, associated stations unicast the frame to the AP first
1940 * which then multicasts it on their behalf.
1941 *
1942 * There is also a slight problem in IBSS mode: GTKs are negotiated
1943 * with each station, that is something we don't currently handle.
1944 * The spec seems to expect that one negotiates the same key with
1945 * every station but there's no such requirement; VLANs could be
1946 * possible.
1947 */
1948
Johan Almbladh86c228a2013-08-14 15:29:46 +02001949 /* start without a key */
1950 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001951 fc = hdr->frame_control;
1952
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001953 if (rx->sta) {
1954 int keyid = rx->sta->ptk_idx;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001955 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001956
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001957 if (ieee80211_has_protected(fc)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001958 cs = rx->sta->cipher_scheme;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001959 keyid = ieee80211_get_keyid(rx->skb, cs);
1960
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001961 if (unlikely(keyid < 0))
1962 return RX_DROP_UNUSABLE;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001963
1964 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001965 }
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001966 }
1967
Johan Almbladh86c228a2013-08-14 15:29:46 +02001968 if (!ieee80211_has_protected(fc))
1969 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1970
1971 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01001972 rx->key = ptk_idx ? ptk_idx : sta_ptk;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001973 if ((status->flag & RX_FLAG_DECRYPTED) &&
1974 (status->flag & RX_FLAG_IV_STRIPPED))
1975 return RX_CONTINUE;
1976 /* Skip decryption if the frame is not protected. */
1977 if (!ieee80211_has_protected(fc))
1978 return RX_CONTINUE;
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02001979 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1980 /* Broadcast/multicast robust management frame / BIP */
1981 if ((status->flag & RX_FLAG_DECRYPTED) &&
1982 (status->flag & RX_FLAG_IV_STRIPPED))
1983 return RX_CONTINUE;
1984
1985 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1986 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1987 NUM_DEFAULT_BEACON_KEYS)
1988 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1989
1990 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1991 if (!rx->key)
1992 return RX_CONTINUE; /* Beacon protection not in use */
Johan Almbladh86c228a2013-08-14 15:29:46 +02001993 } else if (mmie_keyidx >= 0) {
1994 /* Broadcast/multicast robust management frame / BIP */
1995 if ((status->flag & RX_FLAG_DECRYPTED) &&
1996 (status->flag & RX_FLAG_IV_STRIPPED))
1997 return RX_CONTINUE;
1998
1999 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
2000 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
2001 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09002002 if (rx->sta) {
2003 if (ieee80211_is_group_privacy_action(skb) &&
2004 test_sta_flag(rx->sta, WLAN_STA_MFP))
2005 return RX_DROP_MONITOR;
2006
Johan Almbladh86c228a2013-08-14 15:29:46 +02002007 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09002008 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002009 if (!rx->key)
2010 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
2011 } else if (!ieee80211_has_protected(fc)) {
2012 /*
2013 * The frame was not protected, so skip decryption. However, we
2014 * need to set rx->key if there is a key that could have been
2015 * used so that the frame may be dropped if encryption would
2016 * have been expected.
2017 */
2018 struct ieee80211_key *key = NULL;
2019 struct ieee80211_sub_if_data *sdata = rx->sdata;
2020 int i;
2021
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002022 if (ieee80211_is_beacon(fc)) {
2023 key = ieee80211_rx_get_bigtk(rx, -1);
2024 } else if (ieee80211_is_mgmt(fc) &&
2025 is_multicast_ether_addr(hdr->addr1)) {
2026 key = rcu_dereference(rx->sdata->default_mgmt_key);
2027 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002028 if (rx->sta) {
2029 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2030 key = rcu_dereference(rx->sta->gtk[i]);
2031 if (key)
2032 break;
2033 }
2034 }
2035 if (!key) {
2036 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2037 key = rcu_dereference(sdata->keys[i]);
2038 if (key)
2039 break;
2040 }
2041 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02002042 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002043 if (key)
2044 rx->key = key;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002045 return RX_CONTINUE;
2046 } else {
Johan Almbladh86c228a2013-08-14 15:29:46 +02002047 /*
2048 * The device doesn't give us the IV so we won't be
2049 * able to look up the key. That's ok though, we
2050 * don't need to decrypt the frame, we just won't
2051 * be able to keep statistics accurate.
2052 * Except for key threshold notifications, should
2053 * we somehow allow the driver to tell us which key
2054 * the hardware used if this flag is set?
2055 */
2056 if ((status->flag & RX_FLAG_DECRYPTED) &&
2057 (status->flag & RX_FLAG_IV_STRIPPED))
2058 return RX_CONTINUE;
2059
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002060 keyidx = ieee80211_get_keyid(rx->skb, cs);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002061
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01002062 if (unlikely(keyidx < 0))
2063 return RX_DROP_UNUSABLE;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002064
2065 /* check per-station GTK first, if multicast packet */
2066 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2067 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2068
2069 /* if not found, try default key */
2070 if (!rx->key) {
2071 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2072
2073 /*
2074 * RSNA-protected unicast frames should always be
2075 * sent with pairwise or station-to-station keys,
2076 * but for WEP we allow using a key index as well.
2077 */
2078 if (rx->key &&
2079 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2080 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2081 !is_multicast_ether_addr(hdr->addr1))
2082 rx->key = NULL;
2083 }
2084 }
2085
2086 if (rx->key) {
2087 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2088 return RX_DROP_MONITOR;
2089
Johan Almbladh86c228a2013-08-14 15:29:46 +02002090 /* TODO: add threshold stuff again */
2091 } else {
2092 return RX_DROP_MONITOR;
2093 }
2094
2095 switch (rx->key->conf.cipher) {
2096 case WLAN_CIPHER_SUITE_WEP40:
2097 case WLAN_CIPHER_SUITE_WEP104:
2098 result = ieee80211_crypto_wep_decrypt(rx);
2099 break;
2100 case WLAN_CIPHER_SUITE_TKIP:
2101 result = ieee80211_crypto_tkip_decrypt(rx);
2102 break;
2103 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002104 result = ieee80211_crypto_ccmp_decrypt(
2105 rx, IEEE80211_CCMP_MIC_LEN);
2106 break;
2107 case WLAN_CIPHER_SUITE_CCMP_256:
2108 result = ieee80211_crypto_ccmp_decrypt(
2109 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002110 break;
2111 case WLAN_CIPHER_SUITE_AES_CMAC:
2112 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2113 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02002114 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2115 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2116 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02002117 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2118 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2119 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2120 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02002121 case WLAN_CIPHER_SUITE_GCMP:
2122 case WLAN_CIPHER_SUITE_GCMP_256:
2123 result = ieee80211_crypto_gcmp_decrypt(rx);
2124 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02002125 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002126 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02002127 }
2128
2129 /* the hdr variable is invalid after the decrypt handlers */
2130
2131 /* either the frame has been decrypted or will be dropped */
2132 status->flag |= RX_FLAG_DECRYPTED;
2133
2134 return result;
2135}
2136
Johannes Berg571ecf62007-07-27 15:43:22 +02002137static inline struct ieee80211_fragment_entry *
2138ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
2139 unsigned int frag, unsigned int seq, int rx_queue,
2140 struct sk_buff **skb)
2141{
2142 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02002143
Johannes Berg571ecf62007-07-27 15:43:22 +02002144 entry = &sdata->fragments[sdata->fragment_next++];
2145 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
2146 sdata->fragment_next = 0;
2147
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002148 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02002149 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02002150
2151 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2152 *skb = NULL;
2153 entry->first_frag_time = jiffies;
2154 entry->seq = seq;
2155 entry->rx_queue = rx_queue;
2156 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002157 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02002158 entry->extra_len = 0;
2159
2160 return entry;
2161}
2162
2163static inline struct ieee80211_fragment_entry *
2164ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002165 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02002166 int rx_queue, struct ieee80211_hdr *hdr)
2167{
2168 struct ieee80211_fragment_entry *entry;
2169 int i, idx;
2170
2171 idx = sdata->fragment_next;
2172 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2173 struct ieee80211_hdr *f_hdr;
David S. Miller7957a9d2018-08-06 22:49:13 -07002174 struct sk_buff *f_skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002175
2176 idx--;
2177 if (idx < 0)
2178 idx = IEEE80211_FRAGMENT_MAX - 1;
2179
2180 entry = &sdata->fragments[idx];
2181 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2182 entry->rx_queue != rx_queue ||
2183 entry->last_frag + 1 != frag)
2184 continue;
2185
David S. Miller7957a9d2018-08-06 22:49:13 -07002186 f_skb = __skb_peek(&entry->skb_list);
2187 f_hdr = (struct ieee80211_hdr *) f_skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002188
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002189 /*
2190 * Check ftype and addresses are equal, else check next fragment
2191 */
2192 if (((hdr->frame_control ^ f_hdr->frame_control) &
2193 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002194 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2195 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02002196 continue;
2197
S.Çağlar Onurab466232008-02-14 17:36:47 +02002198 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002199 __skb_queue_purge(&entry->skb_list);
2200 continue;
2201 }
2202 return entry;
2203 }
2204
2205 return NULL;
2206}
2207
Johannes Berg49461622008-06-30 15:10:45 +02002208static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002209ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002210{
2211 struct ieee80211_hdr *hdr;
2212 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002213 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02002214 unsigned int frag, seq;
2215 struct ieee80211_fragment_entry *entry;
2216 struct sk_buff *skb;
2217
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07002218 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002219 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07002220
2221 if (ieee80211_is_ctl(fc))
2222 return RX_CONTINUE;
2223
Johannes Berg571ecf62007-07-27 15:43:22 +02002224 sc = le16_to_cpu(hdr->seq_ctrl);
2225 frag = sc & IEEE80211_SCTL_FRAG;
2226
Johannes Bergb8fff402014-11-03 13:57:46 +01002227 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02002228 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01002229 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02002230 }
Johannes Bergb8fff402014-11-03 13:57:46 +01002231
Andreas Müllerd0259332014-12-12 12:11:11 +01002232 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2233 goto out;
2234
Johannes Berg571ecf62007-07-27 15:43:22 +02002235 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2236
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08002237 if (skb_linearize(rx->skb))
2238 return RX_DROP_UNUSABLE;
2239
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07002240 /*
2241 * skb_linearize() might change the skb->data and
2242 * previously cached variables (in this case, hdr) need to
2243 * be refreshed with the new data.
2244 */
2245 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002246 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2247
2248 if (frag == 0) {
2249 /* This is the first fragment of a new frame. */
2250 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02002251 rx->seqno_idx, &(rx->skb));
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002252 if (rx->key &&
2253 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
Johannes Berg9acc54b2016-02-26 22:13:40 +01002254 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2255 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2256 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07002257 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02002258 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002259
2260 /* Store CCMP/GCMP PN so that we can verify that the
2261 * next fragment has a sequential PN value.
2262 */
2263 entry->check_sequential_pn = true;
Johannes Berg571ecf62007-07-27 15:43:22 +02002264 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07002265 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02002266 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01002267 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2268 u.ccmp.rx_pn) !=
2269 offsetof(struct ieee80211_key,
2270 u.gcmp.rx_pn));
2271 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2272 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2273 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2274 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002275 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002276 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002277 }
2278
2279 /* This is a fragment for a frame that should already be pending in
2280 * fragment cache. Add this fragment to the end of the pending entry.
2281 */
Johannes Berg9e262972011-07-07 18:45:03 +02002282 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
2283 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02002284 if (!entry) {
2285 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002286 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002287 }
2288
Johannes Berg9acc54b2016-02-26 22:13:40 +01002289 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2290 * MPDU PN values are not incrementing in steps of 1."
2291 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2292 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2293 */
2294 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002295 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002296 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07002297 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002298
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002299 if (!rx->key ||
2300 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
Johannes Berg9acc54b2016-02-26 22:13:40 +01002301 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2302 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2303 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002304 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002305 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2306 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002307 pn[i]++;
2308 if (pn[i])
2309 break;
2310 }
Johannes Berg9e262972011-07-07 18:45:03 +02002311 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07002312 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02002313 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002314 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002315 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002316 }
2317
Harvey Harrison358c8d92008-07-15 18:44:13 -07002318 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002319 __skb_queue_tail(&entry->skb_list, rx->skb);
2320 entry->last_frag = frag;
2321 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002322 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002323 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002324 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002325 }
2326
2327 rx->skb = __skb_dequeue(&entry->skb_list);
2328 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002329 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002330 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2331 GFP_ATOMIC))) {
2332 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2333 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002334 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002335 }
2336 }
2337 while ((skb = __skb_dequeue(&entry->skb_list))) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02002338 skb_put_data(rx->skb, skb->data, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02002339 dev_kfree_skb(skb);
2340 }
2341
Johannes Berg571ecf62007-07-27 15:43:22 +02002342 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002343 ieee80211_led_rx(rx->local);
2344 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002345 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002346 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002347 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002348}
2349
Johannes Berg1df332e2012-10-26 00:09:11 +02002350static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002351{
Johannes Berg1df332e2012-10-26 00:09:11 +02002352 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002353 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002354
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002355 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002356}
2357
Johannes Berg1df332e2012-10-26 00:09:11 +02002358static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002359{
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
2370 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002371 if (unlikely(!ieee80211_has_protected(fc) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -08002372 !ieee80211_is_any_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002373 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002374 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002375
2376 return 0;
2377}
2378
Johannes Berg1df332e2012-10-26 00:09:11 +02002379static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002380{
2381 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002382 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002383 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002384
Jouni Malinene3efca02010-03-28 22:31:15 -07002385 /*
2386 * Pass through unencrypted frames if the hardware has
2387 * decrypted them already.
2388 */
2389 if (status->flag & RX_FLAG_DECRYPTED)
2390 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002391
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002392 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002393 if (unlikely(!ieee80211_has_protected(fc) &&
2394 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002395 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002396 if (ieee80211_is_deauth(fc) ||
2397 ieee80211_is_disassoc(fc))
2398 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2399 rx->skb->data,
2400 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002401 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002402 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002403 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002404 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002405 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002406 if (ieee80211_is_deauth(fc) ||
2407 ieee80211_is_disassoc(fc))
2408 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2409 rx->skb->data,
2410 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002411 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002412 }
Jouni Malinenaf2d14b2020-02-22 15:25:47 +02002413 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2414 ieee80211_get_mmie_keyidx(rx->skb) < 0))
2415 return -EACCES;
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002416 /*
2417 * When using MFP, Action frames are not allowed prior to
2418 * having configured keys.
2419 */
2420 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002421 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002422 return -EACCES;
2423 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002424
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002425 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002426}
2427
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002428static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002429__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002430{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002431 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002432 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002433 bool check_port_control = false;
2434 struct ethhdr *ehdr;
2435 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002436
Felix Fietkau4114fa22011-04-12 19:15:22 +02002437 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002438 if (ieee80211_has_a4(hdr->frame_control) &&
2439 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002440 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002441
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002442 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2443 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2444
2445 if (!sdata->u.mgd.use_4addr)
2446 return -1;
Sathishkumar Muruganandam1ecef202018-08-28 17:51:38 +05302447 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002448 check_port_control = true;
2449 }
2450
Johannes Berg9bc383d2009-11-19 11:55:19 +01002451 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002452 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002453 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002454
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002455 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002456 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002457 return ret;
2458
2459 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002460 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2461 *port_control = true;
2462 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002463 return -1;
2464
2465 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002466}
Johannes Berg571ecf62007-07-27 15:43:22 +02002467
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002468/*
2469 * requires that rx->skb is a frame with ethernet header
2470 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002471static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002472{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302473 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002474 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2475 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2476
2477 /*
2478 * Allow EAPOL frames to us/the PAE group address regardless
2479 * of whether the frame was encrypted or not.
2480 */
Johannes Berga621fa42010-08-27 14:26:54 +03002481 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002482 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2483 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002484 return true;
2485
2486 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002487 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002488 return false;
2489
2490 return true;
2491}
2492
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002493static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2494 struct ieee80211_rx_data *rx)
2495{
2496 struct ieee80211_sub_if_data *sdata = rx->sdata;
2497 struct net_device *dev = sdata->dev;
2498
2499 if (unlikely((skb->protocol == sdata->control_port_protocol ||
2500 skb->protocol == cpu_to_be16(ETH_P_PREAUTH)) &&
2501 sdata->control_port_over_nl80211)) {
2502 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Denis Kenziorf8b43c52019-08-27 17:41:20 -05002503 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002504
Denis Kenziora948f712018-07-03 15:05:48 -05002505 cfg80211_rx_control_port(dev, skb, noencrypt);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002506 dev_kfree_skb(skb);
2507 } else {
Denis Kenziorc8a41c62019-08-27 17:41:19 -05002508 memset(skb->cb, 0, sizeof(skb->cb));
2509
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002510 /* deliver to local stack */
2511 if (rx->napi)
2512 napi_gro_receive(rx->napi, skb);
2513 else
2514 netif_receive_skb(skb);
2515 }
2516}
2517
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002518/*
2519 * requires that rx->skb is a frame with ethernet header
2520 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002521static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002522ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002523{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002524 struct ieee80211_sub_if_data *sdata = rx->sdata;
2525 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002526 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002527 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2528 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002529
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002530 skb = rx->skb;
2531 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002532
Johannes Berg5a490512015-04-22 17:10:38 +02002533 ieee80211_rx_stats(dev, skb->len);
2534
Johannes Bergde8f18d2016-03-31 20:02:03 +03002535 if (rx->sta) {
2536 /* The seqno index has the same property as needed
2537 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2538 * for non-QoS-data frames. Here we know it's a data
2539 * frame, so count MSDUs.
2540 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002541 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002542 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002543 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002544 }
2545
Johannes Berg05c914f2008-09-11 00:01:58 +02002546 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2547 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002548 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002549 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002550 if (is_multicast_ether_addr(ehdr->h_dest) &&
2551 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002552 /*
2553 * send multicast frames both to higher layers in
2554 * local net stack and back to the wireless medium
2555 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002556 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002557 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002558 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002559 dev->name);
Johannes Berg42dca5e2018-10-09 10:00:21 +02002560 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2561 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2562 dsta = sta_info_get(sdata, ehdr->h_dest);
Johannes Bergabe60632009-11-25 17:46:18 +01002563 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002564 /*
2565 * The destination station is associated to
2566 * this AP (in this VLAN), so send the frame
2567 * directly to it and do not pass it to local
2568 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002569 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002570 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002571 skb = NULL;
2572 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002573 }
2574 }
2575
Kalle Valo59d9cb02009-12-17 13:54:57 +01002576#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002577 if (skb) {
2578 /* 'align' will only take the values 0 or 2 here since all
2579 * frames are required to be aligned to 2-byte boundaries
2580 * when being passed to mac80211; the code here works just
2581 * as well if that isn't true, but mac80211 assumes it can
2582 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002583 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002584 int align;
2585
2586 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002587 if (align) {
2588 if (WARN_ON(skb_headroom(skb) < 3)) {
2589 dev_kfree_skb(skb);
2590 skb = NULL;
2591 } else {
2592 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002593 size_t len = skb_headlen(skb);
2594 skb->data -= align;
2595 memmove(skb->data, data, len);
2596 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002597 }
2598 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002599 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002600#endif
2601
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002602 if (skb) {
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002603 skb->protocol = eth_type_trans(skb, dev);
Denis Kenzior018f6fb2018-03-26 12:52:51 -05002604 ieee80211_deliver_skb_to_local_stack(skb, rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002605 }
2606
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002607 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002608 /*
2609 * Send to wireless media and increase priority by 256 to
2610 * keep the received priority instead of reclassifying
2611 * the frame (see cfg80211_classify8021d).
2612 */
2613 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002614 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002615 skb_reset_network_header(xmit_skb);
2616 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002617 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002618 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002619}
2620
Johannes Berg49461622008-06-30 15:10:45 +02002621static ieee80211_rx_result debug_noinline
Felix Fietkau24bba072018-02-27 13:03:07 +01002622__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002623{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002624 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002625 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002626 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2627 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002628 struct sk_buff_head frame_list;
Johannes Berg7f6990c2016-10-05 15:29:49 +02002629 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002630 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002631
Johannes Bergea720932016-10-05 10:14:42 +02002632 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
Johannes Berge2b52272016-10-05 16:42:06 +02002633 check_da = NULL;
2634 check_sa = NULL;
2635 } else switch (rx->sdata->vif.type) {
2636 case NL80211_IFTYPE_AP:
2637 case NL80211_IFTYPE_AP_VLAN:
2638 check_da = NULL;
2639 break;
2640 case NL80211_IFTYPE_STATION:
2641 if (!rx->sta ||
2642 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2643 check_sa = NULL;
2644 break;
2645 case NL80211_IFTYPE_MESH_POINT:
2646 check_sa = NULL;
2647 break;
2648 default:
2649 break;
Johannes Bergea720932016-10-05 10:14:42 +02002650 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002651
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002652 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002653 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002654
Johannes Berg7f6990c2016-10-05 15:29:49 +02002655 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2656 rx->sdata->vif.addr,
Felix Fietkau24bba072018-02-27 13:03:07 +01002657 rx->sdata->vif.type,
2658 data_offset))
Johannes Berg7f6990c2016-10-05 15:29:49 +02002659 return RX_DROP_UNUSABLE;
2660
Zhu Yieaf85ca2009-12-01 10:18:37 +08002661 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2662 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002663 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002664 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002665
Zhu Yieaf85ca2009-12-01 10:18:37 +08002666 while (!skb_queue_empty(&frame_list)) {
2667 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002668
Harvey Harrison358c8d92008-07-15 18:44:13 -07002669 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002670 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002671 continue;
2672 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002673
2674 ieee80211_deliver_skb(rx);
2675 }
2676
Johannes Berg9ae54c82008-01-31 19:48:20 +01002677 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002678}
2679
Felix Fietkau24bba072018-02-27 13:03:07 +01002680static ieee80211_rx_result debug_noinline
2681ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2682{
2683 struct sk_buff *skb = rx->skb;
2684 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2685 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2686 __le16 fc = hdr->frame_control;
2687
2688 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2689 return RX_CONTINUE;
2690
2691 if (unlikely(!ieee80211_is_data(fc)))
2692 return RX_CONTINUE;
2693
2694 if (unlikely(!ieee80211_is_data_present(fc)))
2695 return RX_DROP_MONITOR;
2696
2697 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2698 switch (rx->sdata->vif.type) {
2699 case NL80211_IFTYPE_AP_VLAN:
2700 if (!rx->sdata->u.vlan.sta)
2701 return RX_DROP_UNUSABLE;
2702 break;
2703 case NL80211_IFTYPE_STATION:
2704 if (!rx->sdata->u.mgd.use_4addr)
2705 return RX_DROP_UNUSABLE;
2706 break;
2707 default:
2708 return RX_DROP_UNUSABLE;
2709 }
2710 }
2711
2712 if (is_multicast_ether_addr(hdr->addr1))
2713 return RX_DROP_UNUSABLE;
2714
2715 return __ieee80211_rx_h_amsdu(rx, 0);
2716}
2717
Ingo Molnarbf94e172008-10-12 23:51:38 -07002718#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002719static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002720ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2721{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002722 struct ieee80211_hdr *fwd_hdr, *hdr;
2723 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002724 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002725 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002726 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002727 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002728 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002729 u16 ac, q, hdrlen;
Felix Fietkau51d0af22019-02-22 13:21:15 +01002730 int tailroom = 0;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002731
2732 hdr = (struct ieee80211_hdr *) skb->data;
2733 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002734
2735 /* make sure fixed part of mesh header is there, also checks skb len */
2736 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2737 return RX_DROP_MONITOR;
2738
2739 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2740
2741 /* make sure full mesh header is there, also checks skb len */
2742 if (!pskb_may_pull(rx->skb,
2743 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2744 return RX_DROP_MONITOR;
2745
2746 /* reload pointers */
2747 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002748 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2749
Bob Copelandd0c22112015-03-02 14:28:52 -05002750 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2751 return RX_DROP_MONITOR;
2752
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002753 /* frame is in RMC, don't forward */
2754 if (ieee80211_is_data(hdr->frame_control) &&
2755 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002756 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002757 return RX_DROP_MONITOR;
2758
Johannes Berg5c900672015-04-22 14:48:34 +02002759 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002760 return RX_CONTINUE;
2761
2762 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002763 return RX_DROP_MONITOR;
2764
Javier Cardona43b7b312009-10-15 18:10:51 -07002765 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002766 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002767 char *proxied_addr;
2768 char *mpp_addr;
2769
2770 if (is_multicast_ether_addr(hdr->addr1)) {
2771 mpp_addr = hdr->addr3;
2772 proxied_addr = mesh_hdr->eaddr1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -07002773 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2774 MESH_FLAGS_AE_A5_A6) {
Johannes Berg9b395bc2012-10-26 00:36:40 +02002775 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002776 mpp_addr = hdr->addr4;
2777 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002778 } else {
2779 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002780 }
YanBo79617de2008-09-22 13:30:32 +08002781
YanBo79617de2008-09-22 13:30:32 +08002782 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002783 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002784 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002785 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002786 } else {
2787 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002788 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002789 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002790 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002791 spin_unlock_bh(&mppath->state_lock);
2792 }
2793 rcu_read_unlock();
2794 }
2795
Javier Cardona3c5772a2009-08-10 12:15:48 -07002796 /* Frame has reached destination. Don't forward */
2797 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002798 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002799 return RX_CONTINUE;
2800
Michal Kaziorcf440122016-01-25 14:43:24 +01002801 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2802 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002803 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002804 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002805 return RX_DROP_MONITOR;
2806 }
2807 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002808
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002809 if (!--mesh_hdr->ttl) {
Bob Copelanda0dc0202019-01-17 16:32:42 -05002810 if (!is_multicast_ether_addr(hdr->addr1))
2811 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2812 dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002813 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002814 }
2815
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002816 if (!ifmsh->mshcfg.dot11MeshForwarding)
2817 goto out;
2818
Felix Fietkau51d0af22019-02-22 13:21:15 +01002819 if (sdata->crypto_tx_tailroom_needed_cnt)
2820 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2821
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002822 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
Felix Fietkau51d0af22019-02-22 13:21:15 +01002823 sdata->encrypt_headroom,
2824 tailroom, GFP_ATOMIC);
Joe Perches0c3d5a92018-03-12 08:07:12 -07002825 if (!fwd_skb)
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002826 goto out;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002827
2828 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002829 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002830 info = IEEE80211_SKB_CB(fwd_skb);
2831 memset(info, 0, sizeof(*info));
2832 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2833 info->control.vif = &rx->sdata->vif;
2834 info->control.jiffies = jiffies;
2835 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2836 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2837 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002838 /* update power mode indication when forwarding */
2839 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002840 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002841 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002842 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2843 } else {
2844 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002845 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002846 fwd_hdr->addr3, 0,
2847 WLAN_REASON_MESH_PATH_NOFORWARD,
2848 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002849 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002850 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002851 return RX_DROP_MONITOR;
2852 }
2853
2854 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2855 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002856 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002857 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002858 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002859 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002860}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002861#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002862
2863static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002864ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002865{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002866 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302867 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002868 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002869 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2870 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002871 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002872 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002873
Harvey Harrison358c8d92008-07-15 18:44:13 -07002874 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002875 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002876
Harvey Harrison358c8d92008-07-15 18:44:13 -07002877 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002878 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002879
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002880 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002881 * Send unexpected-4addr-frame event to hostapd. For older versions,
2882 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002883 */
2884 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002885 sdata->vif.type == NL80211_IFTYPE_AP) {
2886 if (rx->sta &&
2887 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2888 cfg80211_rx_unexpected_4addr_frame(
2889 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002890 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002891 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002892
Felix Fietkau4114fa22011-04-12 19:15:22 +02002893 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002894 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002895 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002896
Harvey Harrison358c8d92008-07-15 18:44:13 -07002897 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002898 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002899
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002900 /* directly handle TDLS channel switch requests/responses */
2901 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2902 cpu_to_be16(ETH_P_TDLS))) {
2903 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2904
2905 if (pskb_may_pull(rx->skb,
2906 offsetof(struct ieee80211_tdls_data, u)) &&
2907 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2908 tf->category == WLAN_CATEGORY_TDLS &&
2909 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2910 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002911 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2912 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002913 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002914 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002915
2916 return RX_QUEUED;
2917 }
2918 }
2919
Felix Fietkau4114fa22011-04-12 19:15:22 +02002920 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2921 unlikely(port_control) && sdata->bss) {
2922 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2923 u.ap);
2924 dev = sdata->dev;
2925 rx->sdata = sdata;
2926 }
2927
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002928 rx->skb->dev = dev;
2929
Johannes Berg602fae42016-03-17 15:02:52 +02002930 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2931 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302932 !is_multicast_ether_addr(
2933 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2934 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002935 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2936 mod_timer(&local->dynamic_ps_timer, jiffies +
2937 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05302938
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002939 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002940
Johannes Berg9ae54c82008-01-31 19:48:20 +01002941 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002942}
2943
Johannes Berg49461622008-06-30 15:10:45 +02002944static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002945ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002946{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002947 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002948 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002949 struct tid_ampdu_rx *tid_agg_rx;
2950 u16 start_seq_num;
2951 u16 tid;
2952
Harvey Harrisona7767f92008-07-02 16:30:51 -07002953 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002954 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002955
Harvey Harrisona7767f92008-07-02 16:30:51 -07002956 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002957 struct {
2958 __le16 control, start_seq_num;
2959 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002960 struct ieee80211_event event = {
2961 .type = BAR_RX_EVENT,
2962 };
Johannes Berg8ae59772010-05-30 14:52:58 +02002963
Ron Rindjunsky71364712007-12-25 17:00:36 +02002964 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002965 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002966
2967 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2968 &bar_data, sizeof(bar_data)))
2969 return RX_DROP_MONITOR;
2970
Johannes Berg8ae59772010-05-30 14:52:58 +02002971 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02002972
Johannes Berg53f24972016-08-29 23:25:19 +03002973 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2974 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2975 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2976 WLAN_BACK_RECIPIENT,
2977 WLAN_REASON_QSTA_REQUIRE_SETUP);
2978
Johannes Berga87f7362010-06-10 10:21:38 +02002979 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2980 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01002981 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002982
Johannes Berg8ae59772010-05-30 14:52:58 +02002983 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002984 event.u.ba.tid = tid;
2985 event.u.ba.ssn = start_seq_num;
2986 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002987
2988 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002989 if (tid_agg_rx->timeout)
2990 mod_timer(&tid_agg_rx->session_timer,
2991 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02002992
Johannes Bergdd318572010-11-29 11:09:16 +01002993 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01002994 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02002995 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002996 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01002997 spin_unlock(&tid_agg_rx->reorder_lock);
2998
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002999 drv_event_callback(rx->local, rx->sdata, &event);
3000
Johannes Berga02ae752009-11-16 12:00:40 +01003001 kfree_skb(skb);
3002 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003003 }
3004
Johannes Berg08daeca2010-05-30 14:53:43 +02003005 /*
3006 * After this point, we only want management frames,
3007 * so we can drop all remaining control frames to
3008 * cooked monitor interfaces.
3009 */
3010 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02003011}
3012
Jouni Malinenf4f727a2009-01-10 11:46:53 +02003013static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3014 struct ieee80211_mgmt *mgmt,
3015 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02003016{
3017 struct ieee80211_local *local = sdata->local;
3018 struct sk_buff *skb;
3019 struct ieee80211_mgmt *resp;
3020
Joe Perchesb203ca32012-05-08 18:56:52 +00003021 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02003022 /* Not to own unicast address */
3023 return;
3024 }
3025
Joe Perchesb203ca32012-05-08 18:56:52 +00003026 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3027 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003028 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02003029 return;
3030 }
3031
3032 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3033 /* Too short SA Query request frame */
3034 return;
3035 }
3036
3037 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3038 if (skb == NULL)
3039 return;
3040
3041 skb_reserve(skb, local->hw.extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02003042 resp = skb_put_zero(skb, 24);
Jouni Malinenfea14732009-01-08 13:32:06 +02003043 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01003044 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01003045 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02003046 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3047 IEEE80211_STYPE_ACTION);
3048 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3049 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3050 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3051 memcpy(resp->u.action.u.sa_query.trans_id,
3052 mgmt->u.action.u.sa_query.trans_id,
3053 WLAN_SA_QUERY_TR_ID_LEN);
3054
Johannes Berg62ae67b2009-11-18 18:42:05 +01003055 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02003056}
3057
Johannes Berg49461622008-06-30 15:10:45 +02003058static ieee80211_rx_result debug_noinline
Johannes Berg2e161f72010-08-12 15:38:38 +02003059ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3060{
3061 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003062 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02003063
3064 /*
3065 * From here on, look only at management frames.
3066 * Data and control frames are already handled,
3067 * and unknown (reserved) frames are useless.
3068 */
3069 if (rx->skb->len < 24)
3070 return RX_DROP_MONITOR;
3071
3072 if (!ieee80211_is_mgmt(mgmt->frame_control))
3073 return RX_DROP_MONITOR;
3074
Johannes Bergee971922011-11-04 11:18:18 +01003075 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3076 ieee80211_is_beacon(mgmt->frame_control) &&
3077 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01003078 int sig = 0;
3079
Tosoni1ad22fb2018-03-14 16:58:34 +00003080 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3081 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003082 sig = status->signal;
3083
Johannes Bergee971922011-11-04 11:18:18 +01003084 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
3085 rx->skb->data, rx->skb->len,
Ben Greear37c73b52012-10-26 14:49:25 -07003086 status->freq, sig);
Johannes Bergee971922011-11-04 11:18:18 +01003087 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3088 }
3089
Johannes Berg2e161f72010-08-12 15:38:38 +02003090 if (ieee80211_drop_unencrypted_mgmt(rx))
3091 return RX_DROP_UNUSABLE;
3092
3093 return RX_CONTINUE;
3094}
3095
3096static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02003097ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3098{
3099 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003100 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02003101 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02003102 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02003103 int len = rx->skb->len;
3104
3105 if (!ieee80211_is_action(mgmt->frame_control))
3106 return RX_CONTINUE;
3107
Jouni Malinen026331c2010-02-15 12:53:10 +02003108 /* drop too small frames */
3109 if (len < IEEE80211_MIN_ACTION_SIZE)
3110 return RX_DROP_UNUSABLE;
3111
Marco Porsch815b8092012-12-05 15:04:26 -08003112 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003113 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3114 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02003115 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02003116
Johannes Bergde1ede72008-09-09 14:42:50 +02003117 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003118 case WLAN_CATEGORY_HT:
3119 /* reject HT action frames from stations not supporting HT */
3120 if (!rx->sta->sta.ht_cap.ht_supported)
3121 goto invalid;
3122
3123 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3124 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3125 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3126 sdata->vif.type != NL80211_IFTYPE_AP &&
3127 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3128 break;
3129
Johannes Bergec61cd62012-12-28 12:12:10 +01003130 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003131 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3132 goto invalid;
3133
3134 switch (mgmt->u.action.u.ht_smps.action) {
3135 case WLAN_HT_ACTION_SMPS: {
3136 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003137 enum ieee80211_smps_mode smps_mode;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303138 struct sta_opmode_info sta_opmode = {};
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003139
Ilan Peerc4d800d2020-01-31 13:12:53 +02003140 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3141 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3142 goto handled;
3143
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003144 /* convert to HT capability */
3145 switch (mgmt->u.action.u.ht_smps.smps_control) {
3146 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003147 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003148 break;
3149 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003150 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003151 break;
3152 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01003153 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003154 break;
3155 default:
3156 goto invalid;
3157 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003158
3159 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01003160 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003161 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01003162 rx->sta->sta.smps_mode = smps_mode;
tamizhr@codeaurora.org57566b22018-03-27 19:16:16 +05303163 sta_opmode.smps_mode =
3164 ieee80211_smps_mode_to_smps_mode(smps_mode);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303165 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003166
3167 sband = rx->local->hw.wiphy->bands[status->band];
3168
Johannes Berg64f68e52012-03-28 10:58:37 +02003169 rate_control_rate_update(local, sband, rx->sta,
3170 IEEE80211_RC_SMPS_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303171 cfg80211_sta_opmode_change_notify(sdata->dev,
3172 rx->sta->addr,
3173 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003174 GFP_ATOMIC);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003175 goto handled;
3176 }
Johannes Bergec61cd62012-12-28 12:12:10 +01003177 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3178 struct ieee80211_supported_band *sband;
3179 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003180 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303181 struct sta_opmode_info sta_opmode = {};
Johannes Bergec61cd62012-12-28 12:12:10 +01003182
3183 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003184 if (!(rx->sta->sta.ht_cap.cap &
3185 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01003186 goto handled;
3187
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003188 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003189 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01003190 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003191 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3192
3193 /* set cur_max_bandwidth and recalc sta bw */
3194 rx->sta->cur_max_bandwidth = max_bw;
3195 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01003196
3197 if (rx->sta->sta.bandwidth == new_bw)
3198 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01003199
Eliad Peller1c45c5c2014-12-14 11:05:51 +02003200 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01003201 sband = rx->local->hw.wiphy->bands[status->band];
tamizhr@codeaurora.org97f5f422018-03-27 19:16:17 +05303202 sta_opmode.bw =
3203 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303204 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
Johannes Bergec61cd62012-12-28 12:12:10 +01003205
3206 rate_control_rate_update(local, sband, rx->sta,
3207 IEEE80211_RC_BW_CHANGED);
tamizhr@codeaurora.orgff84e7b2018-01-31 16:24:50 +05303208 cfg80211_sta_opmode_change_notify(sdata->dev,
3209 rx->sta->addr,
3210 &sta_opmode,
Yan-Hsuan Chuangc752cac2018-10-23 11:24:44 +08003211 GFP_ATOMIC);
Johannes Bergec61cd62012-12-28 12:12:10 +01003212 goto handled;
3213 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01003214 default:
3215 goto invalid;
3216 }
3217
3218 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003219 case WLAN_CATEGORY_PUBLIC:
3220 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3221 goto invalid;
3222 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3223 break;
3224 if (!rx->sta)
3225 break;
3226 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3227 break;
3228 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3229 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3230 break;
3231 if (len < offsetof(struct ieee80211_mgmt,
3232 u.action.u.ext_chan_switch.variable))
3233 goto invalid;
3234 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003235 case WLAN_CATEGORY_VHT:
3236 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3237 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3238 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3239 sdata->vif.type != NL80211_IFTYPE_AP &&
3240 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3241 break;
3242
3243 /* verify action code is present */
3244 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3245 goto invalid;
3246
3247 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3248 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01003249 /* verify opmode is present */
3250 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3251 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02003252 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01003253 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02003254 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3255 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3256 goto invalid;
3257 goto queue;
3258 }
Johannes Berg0af83d32012-12-27 18:55:36 +01003259 default:
3260 break;
3261 }
3262 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003263 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01003264 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07003265 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01003266 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01003267 sdata->vif.type != NL80211_IFTYPE_AP &&
3268 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02003269 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01003270
Jouni Malinen026331c2010-02-15 12:53:10 +02003271 /* verify action_code is present */
3272 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3273 break;
3274
Johannes Bergde1ede72008-09-09 14:42:50 +02003275 switch (mgmt->u.action.u.addba_req.action_code) {
3276 case WLAN_ACTION_ADDBA_REQ:
3277 if (len < (IEEE80211_MIN_ACTION_SIZE +
3278 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003279 goto invalid;
3280 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003281 case WLAN_ACTION_ADDBA_RESP:
3282 if (len < (IEEE80211_MIN_ACTION_SIZE +
3283 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003284 goto invalid;
3285 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02003286 case WLAN_ACTION_DELBA:
3287 if (len < (IEEE80211_MIN_ACTION_SIZE +
3288 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003289 goto invalid;
3290 break;
3291 default:
3292 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02003293 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003294
Johannes Berg8b58ff82010-06-10 10:21:51 +02003295 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02003296 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02003297 /* verify action_code is present */
3298 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3299 break;
3300
Johannes Berg39192c02008-09-09 14:49:03 +02003301 switch (mgmt->u.action.u.measurement.action_code) {
3302 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02003303 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003304 break;
3305
Johannes Berg39192c02008-09-09 14:49:03 +02003306 if (len < (IEEE80211_MIN_ACTION_SIZE +
3307 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02003308 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003309
Johannes Bergcc32abd2009-05-15 11:52:31 +02003310 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003311 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02003312
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003313 ieee80211_process_measurement_req(sdata, mgmt, len);
3314 goto handled;
3315 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3316 u8 *bssid;
3317 if (len < (IEEE80211_MIN_ACTION_SIZE +
3318 sizeof(mgmt->u.action.u.chan_switch)))
3319 break;
3320
3321 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003322 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3323 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003324 break;
3325
3326 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3327 bssid = sdata->u.mgd.bssid;
3328 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3329 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07003330 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3331 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003332 else
3333 break;
3334
3335 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02003336 break;
Sujithc481ec92009-01-06 09:28:37 +05303337
Johannes Berg8b58ff82010-06-10 10:21:51 +02003338 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02003339 }
Johannes Berg39192c02008-09-09 14:49:03 +02003340 }
3341 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02003342 case WLAN_CATEGORY_SA_QUERY:
3343 if (len < (IEEE80211_MIN_ACTION_SIZE +
3344 sizeof(mgmt->u.action.u.sa_query)))
Johannes Berg84040802010-02-15 12:46:39 +02003345 break;
3346
Jouni Malinenfea14732009-01-08 13:32:06 +02003347 switch (mgmt->u.action.u.sa_query.action) {
3348 case WLAN_ACTION_SA_QUERY_REQUEST:
3349 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02003350 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02003351 ieee80211_process_sa_query_req(sdata, mgmt, len);
Johannes Berg84040802010-02-15 12:46:39 +02003352 goto handled;
Jouni Malinenfea14732009-01-08 13:32:06 +02003353 }
3354 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07003355 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003356 if (len < (IEEE80211_MIN_ACTION_SIZE +
3357 sizeof(mgmt->u.action.u.self_prot.action_code)))
3358 break;
3359
Thomas Pedersen8db09852011-08-12 20:01:00 -07003360 switch (mgmt->u.action.u.self_prot.action_code) {
3361 case WLAN_SP_MESH_PEERING_OPEN:
3362 case WLAN_SP_MESH_PEERING_CLOSE:
3363 case WLAN_SP_MESH_PEERING_CONFIRM:
3364 if (!ieee80211_vif_is_mesh(&sdata->vif))
3365 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003366 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003367 /* userspace handles this frame */
3368 break;
3369 goto queue;
3370 case WLAN_SP_MGK_INFORM:
3371 case WLAN_SP_MGK_ACK:
3372 if (!ieee80211_vif_is_mesh(&sdata->vif))
3373 goto invalid;
3374 break;
3375 }
3376 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003377 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003378 if (len < (IEEE80211_MIN_ACTION_SIZE +
3379 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3380 break;
3381
Johannes Berg77a121c2010-06-10 10:21:34 +02003382 if (!ieee80211_vif_is_mesh(&sdata->vif))
3383 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003384 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003385 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003386 break;
3387 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02003388 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003389
Johannes Berg2e161f72010-08-12 15:38:38 +02003390 return RX_CONTINUE;
3391
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003392 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003393 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f72010-08-12 15:38:38 +02003394 /* will return in the next handlers */
3395 return RX_CONTINUE;
3396
3397 handled:
3398 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003399 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003400 dev_kfree_skb(rx->skb);
3401 return RX_QUEUED;
3402
3403 queue:
Johannes Berg2e161f72010-08-12 15:38:38 +02003404 skb_queue_tail(&sdata->skb_queue, rx->skb);
3405 ieee80211_queue_work(&local->hw, &sdata->work);
3406 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003407 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003408 return RX_QUEUED;
3409}
3410
3411static ieee80211_rx_result debug_noinline
3412ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3413{
Johannes Berg554891e2010-09-24 12:38:25 +02003414 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003415 int sig = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +02003416
3417 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003418 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f72010-08-12 15:38:38 +02003419 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003420
Jouni Malinen026331c2010-02-15 12:53:10 +02003421 /*
3422 * Getting here means the kernel doesn't know how to handle
3423 * it, but maybe userspace does ... include returned frames
3424 * so userspace can register for those to know whether ones
3425 * it transmitted were processed or returned.
3426 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003427
Tosoni1ad22fb2018-03-14 16:58:34 +00003428 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3429 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
Johannes Berg804483e2012-03-05 22:18:41 +01003430 sig = status->signal;
3431
Johannes Berg71bbc992012-06-15 15:30:18 +02003432 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07003433 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f72010-08-12 15:38:38 +02003434 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003435 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003436 dev_kfree_skb(rx->skb);
3437 return RX_QUEUED;
3438 }
3439
Johannes Berg2e161f72010-08-12 15:38:38 +02003440 return RX_CONTINUE;
3441}
3442
3443static ieee80211_rx_result debug_noinline
3444ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3445{
3446 struct ieee80211_local *local = rx->local;
3447 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3448 struct sk_buff *nskb;
3449 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003450 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02003451
3452 if (!ieee80211_is_action(mgmt->frame_control))
3453 return RX_CONTINUE;
3454
3455 /*
3456 * For AP mode, hostapd is responsible for handling any action
3457 * frames that we didn't handle, including returning unknown
3458 * ones. For all other modes we will return them to the sender,
3459 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003460 * 802.11-2012 9.24.4.
Johannes Berg2e161f72010-08-12 15:38:38 +02003461 * Newer versions of hostapd shall also use the management frame
3462 * registration mechanisms, but older ones still use cooked
3463 * monitor interfaces so push all frames there.
3464 */
Johannes Berg554891e2010-09-24 12:38:25 +02003465 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f72010-08-12 15:38:38 +02003466 (sdata->vif.type == NL80211_IFTYPE_AP ||
3467 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3468 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003469
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003470 if (is_multicast_ether_addr(mgmt->da))
3471 return RX_DROP_MONITOR;
3472
Johannes Berg84040802010-02-15 12:46:39 +02003473 /* do not return rejected action frames */
3474 if (mgmt->u.action.category & 0x80)
3475 return RX_DROP_UNUSABLE;
3476
3477 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3478 GFP_ATOMIC);
3479 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003480 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003481
John W. Linville292b4df2010-06-24 11:13:56 -04003482 nmgmt->u.action.category |= 0x80;
3483 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3484 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003485
3486 memset(nskb->cb, 0, sizeof(nskb->cb));
3487
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003488 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3489 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3490
3491 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3492 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3493 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003494 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003495 info->hw_queue =
3496 local->hw.offchannel_tx_hw_queue;
3497 }
3498
3499 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
Johannes Bergb9771d42018-05-28 15:47:41 +02003500 status->band, 0);
Johannes Bergde1ede72008-09-09 14:42:50 +02003501 }
Johannes Berg39192c02008-09-09 14:49:03 +02003502 dev_kfree_skb(rx->skb);
3503 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003504}
3505
3506static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003507ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003508{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003509 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003510 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3511 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003512
Johannes Berg77a121c2010-06-10 10:21:34 +02003513 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003514
Johannes Berg77a121c2010-06-10 10:21:34 +02003515 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3516 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003517 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003518 sdata->vif.type != NL80211_IFTYPE_STATION)
3519 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003520
Johannes Berg77a121c2010-06-10 10:21:34 +02003521 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003522 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003523 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3524 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3525 /* process for all: mesh, mlme, ibss */
3526 break;
Johannes Berg95697f92019-10-04 15:37:05 +03003527 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3528 if (is_multicast_ether_addr(mgmt->da) &&
3529 !is_broadcast_ether_addr(mgmt->da))
3530 return RX_DROP_MONITOR;
3531
3532 /* process only for station/IBSS */
3533 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3534 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3535 return RX_DROP_MONITOR;
3536 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003537 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3538 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003539 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003540 if (is_multicast_ether_addr(mgmt->da) &&
3541 !is_broadcast_ether_addr(mgmt->da))
3542 return RX_DROP_MONITOR;
3543
Johannes Berg77a121c2010-06-10 10:21:34 +02003544 /* process only for station */
3545 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3546 return RX_DROP_MONITOR;
3547 break;
3548 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003549 /* process only for ibss and mesh */
3550 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3551 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003552 return RX_DROP_MONITOR;
3553 break;
3554 default:
3555 return RX_DROP_MONITOR;
3556 }
Johannes Berg46900292009-02-15 12:44:28 +01003557
Johannes Berg77a121c2010-06-10 10:21:34 +02003558 /* queue up frame and kick off work to process it */
3559 skb_queue_tail(&sdata->skb_queue, rx->skb);
3560 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003561 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003562 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003563
3564 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003565}
3566
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003567static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3568 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003569{
3570 struct ieee80211_sub_if_data *sdata;
3571 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003572 struct sk_buff *skb = rx->skb, *skb2;
3573 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003574 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003575 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003576
Johannes Berg554891e2010-09-24 12:38:25 +02003577 /*
3578 * If cooked monitor has been processed already, then
3579 * don't do it again. If not, set the flag.
3580 */
3581 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003582 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003583 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003584
Johannes Berg152c4772011-10-21 10:22:22 +02003585 /* If there are no cooked monitor interfaces, just free the SKB */
3586 if (!local->cooked_mntrs)
3587 goto out_free_skb;
3588
Johannes Berg1f7bba72014-11-06 22:56:36 +01003589 /* vendor data is long removed here */
3590 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003591 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003592 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003593
3594 if (skb_headroom(skb) < needed_headroom &&
3595 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003596 goto out_free_skb;
3597
Johannes Berg293702a2012-03-02 13:18:19 +01003598 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003599 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3600 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003601
Zhang Shengjud57a5442016-03-03 01:16:56 +00003602 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003603 skb->ip_summed = CHECKSUM_UNNECESSARY;
3604 skb->pkt_type = PACKET_OTHERHOST;
3605 skb->protocol = htons(ETH_P_802_2);
3606
3607 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01003608 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003609 continue;
3610
Johannes Berg05c914f2008-09-11 00:01:58 +02003611 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003612 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003613 continue;
3614
3615 if (prev_dev) {
3616 skb2 = skb_clone(skb, GFP_ATOMIC);
3617 if (skb2) {
3618 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003619 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003620 }
3621 }
3622
3623 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +02003624 ieee80211_rx_stats(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003625 }
3626
3627 if (prev_dev) {
3628 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003629 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003630 return;
3631 }
Michael Wu3d30d942008-01-31 19:48:27 +01003632
3633 out_free_skb:
3634 dev_kfree_skb(skb);
3635}
3636
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003637static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3638 ieee80211_rx_result res)
3639{
3640 switch (res) {
3641 case RX_DROP_MONITOR:
3642 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3643 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003644 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003645 /* fall through */
3646 case RX_CONTINUE: {
3647 struct ieee80211_rate *rate = NULL;
3648 struct ieee80211_supported_band *sband;
3649 struct ieee80211_rx_status *status;
3650
3651 status = IEEE80211_SKB_RXCB((rx->skb));
3652
3653 sband = rx->local->hw.wiphy->bands[status->band];
Luca Coelho41cbb0f2018-06-09 09:14:44 +03003654 if (status->encoding == RX_ENC_LEGACY)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003655 rate = &sband->bitrates[status->rate_idx];
3656
3657 ieee80211_rx_cooked_monitor(rx, rate);
3658 break;
3659 }
3660 case RX_DROP_UNUSABLE:
3661 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3662 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003663 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003664 dev_kfree_skb(rx->skb);
3665 break;
3666 case RX_QUEUED:
3667 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3668 break;
3669 }
3670}
3671
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003672static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3673 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003674{
3675 ieee80211_rx_result res = RX_DROP_MONITOR;
3676 struct sk_buff *skb;
3677
3678#define CALL_RXH(rxh) \
3679 do { \
3680 res = rxh(rx); \
3681 if (res != RX_CONTINUE) \
3682 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003683 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003684
Johannes Berg45ceeee2015-03-16 09:08:20 +01003685 /* Lock here to avoid hitting all of the data used in the RX
3686 * path (e.g. key data, station data, ...) concurrently when
3687 * a frame is released from the reorder buffer due to timeout
3688 * from the timer, potentially concurrently with RX from the
3689 * driver.
3690 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003691 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003692
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003693 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003694 /*
3695 * all the other fields are valid across frames
3696 * that belong to an aMPDU since they are on the
3697 * same TID from the same station
3698 */
3699 rx->skb = skb;
3700
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003701 CALL_RXH(ieee80211_rx_h_check_more_data);
3702 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3703 CALL_RXH(ieee80211_rx_h_sta_process);
3704 CALL_RXH(ieee80211_rx_h_decrypt);
3705 CALL_RXH(ieee80211_rx_h_defragment);
3706 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003707 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003708#ifdef CONFIG_MAC80211_MESH
3709 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3710 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3711#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003712 CALL_RXH(ieee80211_rx_h_amsdu);
3713 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003714
3715 /* special treatment -- needs the queue */
3716 res = ieee80211_rx_h_ctrl(rx, frames);
3717 if (res != RX_CONTINUE)
3718 goto rxh_next;
3719
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003720 CALL_RXH(ieee80211_rx_h_mgmt_check);
3721 CALL_RXH(ieee80211_rx_h_action);
3722 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3723 CALL_RXH(ieee80211_rx_h_action_return);
3724 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003725
3726 rxh_next:
3727 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003728
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003729#undef CALL_RXH
3730 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003731
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003732 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003733}
Johannes Berg571ecf62007-07-27 15:43:22 +02003734
Johannes Berg4406c372010-09-24 11:21:06 +02003735static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003736{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003737 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003738 ieee80211_rx_result res = RX_DROP_MONITOR;
3739
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003740 __skb_queue_head_init(&reorder_release);
3741
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003742#define CALL_RXH(rxh) \
3743 do { \
3744 res = rxh(rx); \
3745 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003746 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003747 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003748
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003749 CALL_RXH(ieee80211_rx_h_check_dup);
3750 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003751
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003752 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003753
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003754 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003755 return;
Johannes Berg49461622008-06-30 15:10:45 +02003756
Johannes Berg2569a822009-11-25 17:46:17 +01003757 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003758 ieee80211_rx_handlers_result(rx, res);
3759
3760#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003761}
3762
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02003763/*
Johannes Bergdd318572010-11-29 11:09:16 +01003764 * This function makes calls into the RX path, therefore
3765 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02003766 */
3767void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3768{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003769 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003770 struct ieee80211_rx_data rx = {
3771 .sta = sta,
3772 .sdata = sta->sdata,
3773 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003774 /* This is OK -- must be QoS data frame */
3775 .security_idx = tid,
3776 .seqno_idx = tid,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02003777 .napi = NULL, /* must be NULL to not have races */
Johannes Berg554891e2010-09-24 12:38:25 +02003778 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003779 struct tid_ampdu_rx *tid_agg_rx;
3780
3781 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3782 if (!tid_agg_rx)
3783 return;
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02003784
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003785 __skb_queue_head_init(&frames);
3786
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003787 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003788 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003789 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02003790
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003791 if (!skb_queue_empty(&frames)) {
3792 struct ieee80211_event event = {
3793 .type = BA_FRAME_TIMEOUT,
3794 .u.ba.tid = tid,
3795 .u.ba.sta = &sta->sta,
3796 };
3797 drv_event_callback(rx.local, rx.sdata, &event);
3798 }
3799
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003800 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8ebf2010-08-05 01:36:41 +02003801}
3802
Sara Sharon06470f72016-01-28 16:19:25 +02003803void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3804 u16 ssn, u64 filtered,
3805 u16 received_mpdus)
3806{
3807 struct sta_info *sta;
3808 struct tid_ampdu_rx *tid_agg_rx;
3809 struct sk_buff_head frames;
3810 struct ieee80211_rx_data rx = {
3811 /* This is OK -- must be QoS data frame */
3812 .security_idx = tid,
3813 .seqno_idx = tid,
3814 };
3815 int i, diff;
3816
3817 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3818 return;
3819
3820 __skb_queue_head_init(&frames);
3821
3822 sta = container_of(pubsta, struct sta_info, sta);
3823
3824 rx.sta = sta;
3825 rx.sdata = sta->sdata;
3826 rx.local = sta->local;
3827
3828 rcu_read_lock();
3829 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3830 if (!tid_agg_rx)
3831 goto out;
3832
3833 spin_lock_bh(&tid_agg_rx->reorder_lock);
3834
3835 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3836 int release;
3837
3838 /* release all frames in the reorder buffer */
3839 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3840 IEEE80211_SN_MODULO;
3841 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3842 release, &frames);
3843 /* update ssn to match received ssn */
3844 tid_agg_rx->head_seq_num = ssn;
3845 } else {
3846 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3847 &frames);
3848 }
3849
3850 /* handle the case that received ssn is behind the mac ssn.
3851 * it can be tid_agg_rx->buf_size behind and still be valid */
3852 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3853 if (diff >= tid_agg_rx->buf_size) {
3854 tid_agg_rx->reorder_buf_filtered = 0;
3855 goto release;
3856 }
3857 filtered = filtered >> diff;
3858 ssn += diff;
3859
3860 /* update bitmap */
3861 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3862 int index = (ssn + i) % tid_agg_rx->buf_size;
3863
3864 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3865 if (filtered & BIT_ULL(i))
3866 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3867 }
3868
3869 /* now process also frames that the filter marking released */
3870 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3871
3872release:
3873 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3874
3875 ieee80211_rx_handlers(&rx, &frames);
3876
3877 out:
3878 rcu_read_unlock();
3879}
3880EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3881
Johannes Berg571ecf62007-07-27 15:43:22 +02003882/* main receive path */
3883
Johannes Berga58fbe12015-04-22 15:08:39 +02003884static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003885{
Johannes Berg20b01f82010-09-24 11:21:05 +02003886 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003887 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02003888 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003889 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3890 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Luca Coelhoe5f2e062017-04-26 10:58:54 +03003891 bool multicast = is_multicast_ether_addr(hdr->addr1);
Johannes Berg23a24de2007-07-27 15:43:22 +02003892
Johannes Berg51fb61e2007-12-19 01:31:27 +01003893 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003894 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01003895 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003896 return false;
Johannes Berg588f7d32019-02-13 15:13:30 +01003897 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3898 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003899 if (multicast)
3900 return true;
3901 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003902 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02003903 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003904 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02003905 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3906 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003907 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003908 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003909 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02003910 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003911 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003912 if (!multicast &&
3913 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003914 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003915 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003916 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02003917 if (status->encoding != RX_ENC_LEGACY)
Johannes Berg56146182012-11-09 15:07:02 +01003918 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003919 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003920 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01003921 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3922 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003923 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003924 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003925 case NL80211_IFTYPE_OCB:
3926 if (!bssid)
3927 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02003928 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003929 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003930 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003931 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003932 if (!multicast &&
3933 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003934 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003935 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003936 int rate_idx;
Johannes Bergda6a4352017-04-26 12:14:59 +02003937 if (status->encoding != RX_ENC_LEGACY)
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003938 rate_idx = 0; /* TODO: HT rates */
3939 else
3940 rate_idx = status->rate_idx;
3941 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3942 BIT(rate_idx));
3943 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003944 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02003945 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg736a80b2018-01-04 15:51:53 +01003946 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
3947 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003948 if (multicast)
3949 return true;
3950 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003951 case NL80211_IFTYPE_AP_VLAN:
3952 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02003953 if (!bssid)
3954 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3955
3956 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01003957 /*
3958 * Accept public action frames even when the
3959 * BSSID doesn't match, this is used for P2P
3960 * and location updates. Note that mac80211
3961 * itself never looks at these frames.
3962 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02003963 if (!multicast &&
3964 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003965 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02003966 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003967 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02003968 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02003969 }
3970
3971 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003972 /* ignore data frames to TDLS-peers */
3973 if (ieee80211_is_data(hdr->frame_control))
3974 return false;
3975 /* ignore action frames to TDLS-peers */
3976 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02003977 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003978 !ether_addr_equal(bssid, hdr->addr1))
3979 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02003980 }
Johannes Berg3018e942017-04-20 21:32:16 +02003981
3982 /*
3983 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
3984 * the BSSID - we've checked that already but may have accepted
3985 * the wildcard (ff:ff:ff:ff:ff:ff).
3986 *
3987 * It also says:
3988 * The BSSID of the Data frame is determined as follows:
3989 * a) If the STA is contained within an AP or is associated
3990 * with an AP, the BSSID is the address currently in use
3991 * by the STA contained in the AP.
3992 *
3993 * So we should not accept data frames with an address that's
3994 * multicast.
3995 *
3996 * Accepting it also opens a security problem because stations
3997 * could encrypt it with the GTK and inject traffic that way.
3998 */
3999 if (ieee80211_is_data(hdr->frame_control) && multicast)
4000 return false;
4001
Johannes Berga58fbe12015-04-22 15:08:39 +02004002 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02004003 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07004004 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01004005 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02004006 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
Johannes Bergf142c6b2012-06-18 20:07:15 +02004007 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02004008 return ieee80211_is_public_action(hdr, skb->len) ||
4009 ieee80211_is_probe_req(hdr->frame_control) ||
4010 ieee80211_is_probe_resp(hdr->frame_control) ||
4011 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03004012 case NL80211_IFTYPE_NAN:
4013 /* Currently no frames on NAN interface are allowed */
4014 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02004015 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02004016 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02004017 }
4018
Johannes Berga58fbe12015-04-22 15:08:39 +02004019 WARN_ON_ONCE(1);
4020 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02004021}
4022
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004023void ieee80211_check_fast_rx(struct sta_info *sta)
4024{
4025 struct ieee80211_sub_if_data *sdata = sta->sdata;
4026 struct ieee80211_local *local = sdata->local;
4027 struct ieee80211_key *key;
4028 struct ieee80211_fast_rx fastrx = {
4029 .dev = sdata->dev,
4030 .vif_type = sdata->vif.type,
4031 .control_port_protocol = sdata->control_port_protocol,
4032 }, *old, *new = NULL;
4033 bool assign = false;
4034
4035 /* use sparse to check that we don't return without updating */
4036 __acquire(check_fast_rx);
4037
4038 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4039 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4040 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4041 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4042
Johannes Bergc9c59622016-03-31 20:02:11 +03004043 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4044
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004045 /* fast-rx doesn't do reordering */
4046 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4047 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4048 goto clear;
4049
4050 switch (sdata->vif.type) {
4051 case NL80211_IFTYPE_STATION:
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004052 if (sta->sta.tdls) {
4053 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4054 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4055 fastrx.expected_ds_bits = 0;
4056 } else {
4057 fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
4058 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4059 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4060 fastrx.expected_ds_bits =
4061 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4062 }
Felix Fietkau1d870162018-02-23 10:06:05 +01004063
Felix Fietkau9251a472018-02-23 13:55:50 +01004064 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4065 fastrx.expected_ds_bits |=
4066 cpu_to_le16(IEEE80211_FCTL_TODS);
4067 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4068 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4069 }
4070
Felix Fietkau1d870162018-02-23 10:06:05 +01004071 if (!sdata->u.mgd.powersave)
4072 break;
4073
4074 /* software powersave is a huge mess, avoid all of it */
4075 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4076 goto clear;
4077 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4078 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4079 goto clear;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004080 break;
4081 case NL80211_IFTYPE_AP_VLAN:
4082 case NL80211_IFTYPE_AP:
4083 /* parallel-rx requires this, at least with calls to
4084 * ieee80211_sta_ps_transition()
4085 */
4086 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4087 goto clear;
4088 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4089 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4090 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4091
4092 fastrx.internal_forward =
4093 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4094 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4095 !sdata->u.vlan.sta);
Felix Fietkau59cae5b2018-02-23 10:06:04 +01004096
4097 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4098 sdata->u.vlan.sta) {
4099 fastrx.expected_ds_bits |=
4100 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4101 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4102 fastrx.internal_forward = 0;
4103 }
4104
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004105 break;
4106 default:
4107 goto clear;
4108 }
4109
4110 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4111 goto clear;
4112
4113 rcu_read_lock();
4114 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4115 if (key) {
4116 switch (key->conf.cipher) {
4117 case WLAN_CIPHER_SUITE_TKIP:
4118 /* we don't want to deal with MMIC in fast-rx */
4119 goto clear_rcu;
4120 case WLAN_CIPHER_SUITE_CCMP:
4121 case WLAN_CIPHER_SUITE_CCMP_256:
4122 case WLAN_CIPHER_SUITE_GCMP:
4123 case WLAN_CIPHER_SUITE_GCMP_256:
4124 break;
4125 default:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01004126 /* We also don't want to deal with
4127 * WEP or cipher scheme.
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004128 */
4129 goto clear_rcu;
4130 }
4131
4132 fastrx.key = true;
4133 fastrx.icv_len = key->conf.icv_len;
4134 }
4135
4136 assign = true;
4137 clear_rcu:
4138 rcu_read_unlock();
4139 clear:
4140 __release(check_fast_rx);
4141
4142 if (assign)
4143 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4144
4145 spin_lock_bh(&sta->lock);
4146 old = rcu_dereference_protected(sta->fast_rx, true);
4147 rcu_assign_pointer(sta->fast_rx, new);
4148 spin_unlock_bh(&sta->lock);
4149
4150 if (old)
4151 kfree_rcu(old, rcu_head);
4152}
4153
4154void ieee80211_clear_fast_rx(struct sta_info *sta)
4155{
4156 struct ieee80211_fast_rx *old;
4157
4158 spin_lock_bh(&sta->lock);
4159 old = rcu_dereference_protected(sta->fast_rx, true);
4160 RCU_INIT_POINTER(sta->fast_rx, NULL);
4161 spin_unlock_bh(&sta->lock);
4162
4163 if (old)
4164 kfree_rcu(old, rcu_head);
4165}
4166
4167void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4168{
4169 struct ieee80211_local *local = sdata->local;
4170 struct sta_info *sta;
4171
4172 lockdep_assert_held(&local->sta_mtx);
4173
4174 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4175 if (sdata != sta->sdata &&
4176 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4177 continue;
4178 ieee80211_check_fast_rx(sta);
4179 }
4180}
4181
4182void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4183{
4184 struct ieee80211_local *local = sdata->local;
4185
4186 mutex_lock(&local->sta_mtx);
4187 __ieee80211_check_fast_rx_iface(sdata);
4188 mutex_unlock(&local->sta_mtx);
4189}
4190
4191static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4192 struct ieee80211_fast_rx *fast_rx)
4193{
4194 struct sk_buff *skb = rx->skb;
4195 struct ieee80211_hdr *hdr = (void *)skb->data;
4196 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4197 struct sta_info *sta = rx->sta;
4198 int orig_len = skb->len;
Felix Fietkau24bba072018-02-27 13:03:07 +01004199 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4200 int snap_offs = hdrlen;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004201 struct {
4202 u8 snap[sizeof(rfc1042_header)];
4203 __be16 proto;
4204 } *payload __aligned(2);
4205 struct {
4206 u8 da[ETH_ALEN];
4207 u8 sa[ETH_ALEN];
4208 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03004209 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4210
4211 if (fast_rx->uses_rss)
4212 stats = this_cpu_ptr(sta->pcpu_rx_stats);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004213
4214 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4215 * to a common data structure; drivers can implement that per queue
4216 * but we don't have that information in mac80211
4217 */
4218 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4219 return false;
4220
4221#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4222
4223 /* If using encryption, we also need to have:
4224 * - PN_VALIDATED: similar, but the implementation is tricky
4225 * - DECRYPTED: necessary for PN_VALIDATED
4226 */
4227 if (fast_rx->key &&
4228 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4229 return false;
4230
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004231 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4232 return false;
4233
4234 if (unlikely(ieee80211_is_frag(hdr)))
4235 return false;
4236
4237 /* Since our interface address cannot be multicast, this
4238 * implicitly also rejects multicast frames without the
4239 * explicit check.
4240 *
4241 * We shouldn't get any *data* frames not addressed to us
4242 * (AP mode will accept multicast *management* frames), but
4243 * punting here will make it go through the full checks in
4244 * ieee80211_accept_frame().
4245 */
4246 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4247 return false;
4248
4249 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4250 IEEE80211_FCTL_TODS)) !=
4251 fast_rx->expected_ds_bits)
Felix Fietkaub323ac12018-02-23 10:06:03 +01004252 return false;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004253
4254 /* assign the key to drop unencrypted frames (later)
4255 * and strip the IV/MIC if necessary
4256 */
4257 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4258 /* GCMP header length is the same */
4259 snap_offs += IEEE80211_CCMP_HDR_LEN;
4260 }
4261
Felix Fietkau24bba072018-02-27 13:03:07 +01004262 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4263 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4264 goto drop;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004265
Felix Fietkau24bba072018-02-27 13:03:07 +01004266 payload = (void *)(skb->data + snap_offs);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004267
Felix Fietkau24bba072018-02-27 13:03:07 +01004268 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4269 return false;
4270
4271 /* Don't handle these here since they require special code.
4272 * Accept AARP and IPX even though they should come with a
4273 * bridge-tunnel header - but if we get them this way then
4274 * there's little point in discarding them.
4275 */
4276 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4277 payload->proto == fast_rx->control_port_protocol))
4278 return false;
4279 }
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004280
4281 /* after this point, don't punt to the slowpath! */
4282
4283 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4284 pskb_trim(skb, skb->len - fast_rx->icv_len))
4285 goto drop;
4286
4287 if (unlikely(fast_rx->sta_notify)) {
4288 ieee80211_sta_rx_notify(rx->sdata, hdr);
4289 fast_rx->sta_notify = false;
4290 }
4291
4292 /* statistics part of ieee80211_rx_h_sta_process() */
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004293 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Bergc9c59622016-03-31 20:02:11 +03004294 stats->last_signal = status->signal;
4295 if (!fast_rx->uses_rss)
4296 ewma_signal_add(&sta->rx_stats_avg.signal,
4297 -status->signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004298 }
4299
4300 if (status->chains) {
4301 int i;
4302
Johannes Bergc9c59622016-03-31 20:02:11 +03004303 stats->chains = status->chains;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004304 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4305 int signal = status->chain_signal[i];
4306
4307 if (!(status->chains & BIT(i)))
4308 continue;
4309
Johannes Bergc9c59622016-03-31 20:02:11 +03004310 stats->chain_signal_last[i] = signal;
4311 if (!fast_rx->uses_rss)
4312 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4313 -signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004314 }
4315 }
4316 /* end of statistics */
4317
4318 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4319 goto drop;
4320
Felix Fietkau24bba072018-02-27 13:03:07 +01004321 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4322 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4323 RX_QUEUED)
4324 goto drop;
4325
4326 return true;
4327 }
4328
4329 stats->last_rx = jiffies;
4330 stats->last_rate = sta_stats_encode_rate(status);
4331
4332 stats->fragments++;
4333 stats->packets++;
4334
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004335 /* do the header conversion - first grab the addresses */
4336 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4337 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4338 /* remove the SNAP but leave the ethertype */
4339 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4340 /* push the addresses in front */
4341 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4342
4343 skb->dev = fast_rx->dev;
4344
4345 ieee80211_rx_stats(fast_rx->dev, skb->len);
4346
4347 /* The seqno index has the same property as needed
4348 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4349 * for non-QoS-data frames. Here we know it's a data
4350 * frame, so count MSDUs.
4351 */
Johannes Bergc9c59622016-03-31 20:02:11 +03004352 u64_stats_update_begin(&stats->syncp);
4353 stats->msdu[rx->seqno_idx]++;
4354 stats->bytes += orig_len;
4355 u64_stats_update_end(&stats->syncp);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004356
4357 if (fast_rx->internal_forward) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004358 struct sk_buff *xmit_skb = NULL;
Johannes Berg42dca5e2018-10-09 10:00:21 +02004359 if (is_multicast_ether_addr(addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004360 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Johannes Berg42dca5e2018-10-09 10:00:21 +02004361 } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4362 sta_info_get(rx->sdata, addrs.da)) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01004363 xmit_skb = skb;
4364 skb = NULL;
4365 }
4366
4367 if (xmit_skb) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004368 /*
4369 * Send to wireless media and increase priority by 256
4370 * to keep the received priority instead of
4371 * reclassifying the frame (see cfg80211_classify8021d).
4372 */
Johannes Bergeeb0d562016-12-14 16:47:43 +01004373 xmit_skb->priority += 256;
4374 xmit_skb->protocol = htons(ETH_P_802_3);
4375 skb_reset_network_header(xmit_skb);
4376 skb_reset_mac_header(xmit_skb);
4377 dev_queue_xmit(xmit_skb);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004378 }
Johannes Bergeeb0d562016-12-14 16:47:43 +01004379
4380 if (!skb)
4381 return true;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004382 }
4383
4384 /* deliver to local stack */
4385 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4386 memset(skb->cb, 0, sizeof(skb->cb));
4387 if (rx->napi)
4388 napi_gro_receive(rx->napi, skb);
4389 else
4390 netif_receive_skb(skb);
4391
4392 return true;
4393 drop:
4394 dev_kfree_skb(skb);
Johannes Bergc9c59622016-03-31 20:02:11 +03004395 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004396 return true;
4397}
4398
Johannes Berg571ecf62007-07-27 15:43:22 +02004399/*
Johannes Berg4406c372010-09-24 11:21:06 +02004400 * This function returns whether or not the SKB
4401 * was destined for RX processing or not, which,
4402 * if consume is true, is equivalent to whether
4403 * or not the skb was consumed.
4404 */
4405static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4406 struct sk_buff *skb, bool consume)
4407{
4408 struct ieee80211_local *local = rx->local;
4409 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004410
4411 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004412
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004413 /* See if we can do fast-rx; if we have to copy we already lost,
4414 * so punt in that case. We should never have to deliver a data
4415 * frame to multiple interfaces anyway.
4416 *
4417 * We skip the ieee80211_accept_frame() call and do the necessary
4418 * checking inside ieee80211_invoke_fast_rx().
4419 */
4420 if (consume && rx->sta) {
4421 struct ieee80211_fast_rx *fast_rx;
4422
4423 fast_rx = rcu_dereference(rx->sta->fast_rx);
4424 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4425 return true;
4426 }
4427
Johannes Berga58fbe12015-04-22 15:08:39 +02004428 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004429 return false;
4430
Johannes Berg4406c372010-09-24 11:21:06 +02004431 if (!consume) {
4432 skb = skb_copy(skb, GFP_ATOMIC);
4433 if (!skb) {
4434 if (net_ratelimit())
4435 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004436 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004437 sdata->name);
4438 return true;
4439 }
4440
4441 rx->skb = skb;
4442 }
4443
4444 ieee80211_invoke_rx_handlers(rx);
4445 return true;
4446}
4447
4448/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004449 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004450 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004451 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004452static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004453 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004454 struct sk_buff *skb,
4455 struct napi_struct *napi)
Johannes Berg571ecf62007-07-27 15:43:22 +02004456{
4457 struct ieee80211_local *local = hw_to_local(hw);
4458 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004459 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004460 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004461 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004462 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004463 struct rhlist_head *tmp;
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004464 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004465
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004466 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004467 memset(&rx, 0, sizeof(rx));
4468 rx.skb = skb;
4469 rx.local = local;
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004470 rx.napi = napi;
Johannes Berg72abd812007-09-17 01:29:22 -04004471
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004472 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004473 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004474
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004475 if (ieee80211_is_mgmt(fc)) {
4476 /* drop frame if too short for header */
4477 if (skb->len < ieee80211_hdrlen(fc))
4478 err = -ENOBUFS;
4479 else
4480 err = skb_linearize(skb);
4481 } else {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004482 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004483 }
Zhu Yie3cf8b3f2010-03-29 17:35:07 +08004484
4485 if (err) {
4486 dev_kfree_skb(skb);
4487 return;
4488 }
4489
4490 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004491 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004492 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004493
Johannes Bergd48b2962012-07-06 22:19:27 +02004494 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4495 ieee80211_is_beacon(hdr->frame_control)))
4496 ieee80211_scan_rx(local, skb);
4497
Johannes Berg19d19e92017-02-27 09:38:11 +01004498 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004499 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004500
Johannes Berg19d19e92017-02-27 09:38:11 +01004501 if (pubsta) {
4502 rx.sta = container_of(pubsta, struct sta_info, sta);
4503 rx.sdata = rx.sta->sdata;
4504 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4505 return;
4506 goto out;
4507 }
4508
Ben Greear56af3262010-09-23 10:22:24 -07004509 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004510
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004511 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004512 if (!prev_sta) {
4513 prev_sta = sta;
4514 continue;
4515 }
4516
4517 rx.sta = prev_sta;
4518 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004519 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004520
Ben Greear56af3262010-09-23 10:22:24 -07004521 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004522 }
Ben Greear56af3262010-09-23 10:22:24 -07004523
4524 if (prev_sta) {
4525 rx.sta = prev_sta;
4526 rx.sdata = prev_sta->sdata;
4527
Johannes Berg4406c372010-09-24 11:21:06 +02004528 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004529 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304530 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004531 }
Johannes Berg4406c372010-09-24 11:21:06 +02004532 }
4533
Johannes Berg4b0dd982010-09-24 11:21:07 +02004534 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004535
Johannes Berg4b0dd982010-09-24 11:21:07 +02004536 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4537 if (!ieee80211_sdata_running(sdata))
4538 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004539
Johannes Berg4b0dd982010-09-24 11:21:07 +02004540 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4541 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4542 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004543
Johannes Berg4b0dd982010-09-24 11:21:07 +02004544 /*
4545 * frame is destined for this interface, but if it's
4546 * not also for the previous one we handle that after
4547 * the loop to avoid copying the SKB once too much
4548 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004549
Johannes Berg4b0dd982010-09-24 11:21:07 +02004550 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004551 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004552 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004553 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004554
Johannes Berg7852e362012-01-20 13:55:24 +01004555 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004556 rx.sdata = prev;
4557 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004558
Johannes Berg4b0dd982010-09-24 11:21:07 +02004559 prev = sdata;
4560 }
Johannes Berg4406c372010-09-24 11:21:06 +02004561
Johannes Berg4b0dd982010-09-24 11:21:07 +02004562 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004563 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004564 rx.sdata = prev;
4565
4566 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4567 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004568 }
Johannes Berg4406c372010-09-24 11:21:06 +02004569
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304570 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004571 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004572}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004573
4574/*
4575 * This is the receive path handler. It is called by a low level driver when an
4576 * 802.11 MPDU is received from the hardware.
4577 */
Johannes Bergd63b5482016-03-31 20:02:02 +03004578void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4579 struct sk_buff *skb, struct napi_struct *napi)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004580{
4581 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004582 struct ieee80211_rate *rate = NULL;
4583 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004584 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004585
Johannes Bergd20ef632009-10-11 15:10:40 +02004586 WARN_ON_ONCE(softirq_count() == 0);
4587
Johannes Berg57fbcce2016-04-12 15:56:15 +02004588 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004589 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004590
4591 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004592 if (WARN_ON(!sband))
4593 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004594
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004595 /*
4596 * If we're suspending, it is possible although not too likely
4597 * that we'd be receiving frames after having already partially
4598 * quiesced the stack. We can't process such frames then since
4599 * that might, for example, cause stations to be added or other
4600 * driver callbacks be invoked.
4601 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004602 if (unlikely(local->quiescing || local->suspended))
4603 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004604
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004605 /* We might be during a HW reconfig, prevent Rx for the same reason */
4606 if (unlikely(local->in_reconfig))
4607 goto drop;
4608
Johannes Bergea77f122009-08-21 14:44:45 +02004609 /*
4610 * The same happens when we're not even started,
4611 * but that's worth a warning.
4612 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004613 if (WARN_ON(!local->started))
4614 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004615
Johannes Bergfc885182010-07-30 13:23:12 +02004616 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004617 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004618 * Validate the rate, unless a PLCP error means that
4619 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004620 */
Johannes Bergfc885182010-07-30 13:23:12 +02004621
Johannes Bergda6a4352017-04-26 12:14:59 +02004622 switch (status->encoding) {
4623 case RX_ENC_HT:
Johannes Bergfc885182010-07-30 13:23:12 +02004624 /*
4625 * rate_idx is MCS index, which can be [0-76]
4626 * as documented on:
4627 *
4628 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4629 *
4630 * Anything else would be some sort of driver or
4631 * hardware error. The driver should catch hardware
4632 * errors.
4633 */
Johannes Berg444e3802012-09-30 17:08:35 +02004634 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004635 "Rate marked as an HT rate but passed "
4636 "status->rate_idx is not "
4637 "an MCS index [0-76]: %d (0x%02x)\n",
4638 status->rate_idx,
4639 status->rate_idx))
4640 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004641 break;
4642 case RX_ENC_VHT:
Johannes Berg56146182012-11-09 15:07:02 +01004643 if (WARN_ONCE(status->rate_idx > 9 ||
Johannes Berg8613c942017-04-26 13:51:41 +02004644 !status->nss ||
4645 status->nss > 8,
Johannes Berg56146182012-11-09 15:07:02 +01004646 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
Johannes Berg8613c942017-04-26 13:51:41 +02004647 status->rate_idx, status->nss))
Johannes Berg56146182012-11-09 15:07:02 +01004648 goto drop;
Johannes Bergda6a4352017-04-26 12:14:59 +02004649 break;
Luca Coelho41cbb0f2018-06-09 09:14:44 +03004650 case RX_ENC_HE:
4651 if (WARN_ONCE(status->rate_idx > 11 ||
4652 !status->nss ||
4653 status->nss > 8,
4654 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4655 status->rate_idx, status->nss))
4656 goto drop;
4657 break;
Johannes Bergda6a4352017-04-26 12:14:59 +02004658 default:
4659 WARN_ON_ONCE(1);
4660 /* fall through */
4661 case RX_ENC_LEGACY:
Johannes Berg444e3802012-09-30 17:08:35 +02004662 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004663 goto drop;
4664 rate = &sband->bitrates[status->rate_idx];
4665 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004666 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004667
Johannes Berg554891e2010-09-24 12:38:25 +02004668 status->rx_flags = 0;
4669
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004670 /*
4671 * key references and virtual interfaces are protected using RCU
4672 * and this requires that we are in a read-side RCU section during
4673 * receive processing
4674 */
4675 rcu_read_lock();
4676
4677 /*
4678 * Frames with failed FCS/PLCP checksum are not returned,
4679 * all other frames are returned without radiotap header
4680 * if it was previously present.
4681 * Also, frames with less than 16 bytes are dropped.
4682 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004683 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004684 if (!skb) {
4685 rcu_read_unlock();
4686 return;
4687 }
4688
Johannes Berge1e54062010-11-30 08:58:45 +01004689 ieee80211_tpt_led_trig_rx(local,
4690 ((struct ieee80211_hdr *)skb->data)->frame_control,
4691 skb->len);
Johannes Bergd63b5482016-03-31 20:02:02 +03004692
4693 __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004694
4695 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02004696
4697 return;
4698 drop:
4699 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004700}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004701EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004702
4703/* This is a version of the rx handler that can be called from hard irq
4704 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004705void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004706{
4707 struct ieee80211_local *local = hw_to_local(hw);
4708
4709 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4710
Johannes Berg571ecf62007-07-27 15:43:22 +02004711 skb->pkt_type = IEEE80211_RX_MSG;
4712 skb_queue_tail(&local->skb_queue, skb);
4713 tasklet_schedule(&local->tasklet);
4714}
4715EXPORT_SYMBOL(ieee80211_rx_irqsafe);