Merge tag 'io_uring-6.16-20250630' of git://git.kernel.dk/linux
[linux-2.6-block.git] / net / mac80211 / rx.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
571ecf67
JB
2/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
84040805 6 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
d98ad83e 7 * Copyright 2013-2014 Intel Mobile Communications GmbH
b7540d8f 8 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
9a267ce4 9 * Copyright (C) 2018-2025 Intel Corporation
571ecf67
JB
10 */
11
ab46623e 12#include <linux/jiffies.h>
5a0e3ad6 13#include <linux/slab.h>
571ecf67
JB
14#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
d4e46a3d 18#include <linux/rcupdate.h>
bc3b2d7f 19#include <linux/export.h>
183f47fc 20#include <linux/kcov.h>
06470f74 21#include <linux/bitops.h>
0738e55c 22#include <kunit/visibility.h>
571ecf67
JB
23#include <net/mac80211.h>
24#include <net/ieee80211_radiotap.h>
5f60d5f6 25#include <linux/unaligned.h>
571ecf67
JB
26
27#include "ieee80211_i.h"
24487981 28#include "driver-ops.h"
2c8dccc7 29#include "led.h"
33b64eb2 30#include "mesh.h"
571ecf67
JB
31#include "wep.h"
32#include "wpa.h"
33#include "tkip.h"
34#include "wme.h"
1d8d3dec 35#include "rate.h"
571ecf67 36
b2e7771e
JB
37/*
38 * monitor mode reception
39 *
40 * This function cleans up the SKB, i.e. it removes all the stuff
41 * only useful for monitoring.
42 */
c1129924
JB
43static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
44 unsigned int present_fcs_len,
45 unsigned int rtap_space)
b2e7771e 46{
3a867c7e 47 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
c1129924
JB
48 struct ieee80211_hdr *hdr;
49 unsigned int hdrlen;
50 __le16 fc;
51
30841f5c
JB
52 if (present_fcs_len)
53 __pskb_trim(skb, skb->len - present_fcs_len);
d427c899 54 pskb_pull(skb, rtap_space);
c1129924 55
3a867c7e
MG
56 /* After pulling radiotap header, clear all flags that indicate
57 * info in skb->data.
58 */
9179dff8 59 status->flag &= ~(RX_FLAG_RADIOTAP_TLV_AT_END |
3a867c7e
MG
60 RX_FLAG_RADIOTAP_LSIG |
61 RX_FLAG_RADIOTAP_HE_MU |
62 RX_FLAG_RADIOTAP_HE);
63
c1129924
JB
64 hdr = (void *)skb->data;
65 fc = hdr->frame_control;
66
67 /*
68 * Remove the HT-Control field (if present) on management
69 * frames after we've sent the frame to monitoring. We
70 * (currently) don't need it, and don't properly parse
71 * frames with it present, due to the assumption of a
72 * fixed management header length.
73 */
74 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
75 return skb;
76
77 hdrlen = ieee80211_hdrlen(fc);
78 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
79
80 if (!pskb_may_pull(skb, hdrlen)) {
81 dev_kfree_skb(skb);
82 return NULL;
83 }
84
85 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
86 hdrlen - IEEE80211_HT_CTL_LEN);
d427c899 87 pskb_pull(skb, IEEE80211_HT_CTL_LEN);
c1129924
JB
88
89 return skb;
b2e7771e
JB
90}
91
1f7bba79 92static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
c096b92a 93 unsigned int rtap_space)
b2e7771e 94{
f1d58c25 95 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1f7bba79
JB
96 struct ieee80211_hdr *hdr;
97
c096b92a 98 hdr = (void *)(skb->data + rtap_space);
b2e7771e 99
4c298677 100 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
17883048 101 RX_FLAG_FAILED_PLCP_CRC |
c3d1f875
ST
102 RX_FLAG_ONLY_MONITOR |
103 RX_FLAG_NO_PSDU))
6b59db7d
ZG
104 return true;
105
c096b92a 106 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
6b59db7d
ZG
107 return true;
108
87228f57
HH
109 if (ieee80211_is_ctl(hdr->frame_control) &&
110 !ieee80211_is_pspoll(hdr->frame_control) &&
111 !ieee80211_is_back_req(hdr->frame_control))
6b59db7d
ZG
112 return true;
113
114 return false;
b2e7771e
JB
115}
116
601ae7f2 117static int
1f7bba79
JB
118ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
119 struct ieee80211_rx_status *status,
120 struct sk_buff *skb)
601ae7f2
BR
121{
122 int len;
123
124 /* always present fields */
a144f378 125 len = sizeof(struct ieee80211_radiotap_header) + 8;
601ae7f2 126
a144f378 127 /* allocate extra bitmaps */
a144f378
JB
128 if (status->chains)
129 len += 4 * hweight8(status->chains);
90b9e446
JB
130
131 if (ieee80211_have_rx_timestamp(status)) {
132 len = ALIGN(len, 8);
601ae7f2 133 len += 8;
90b9e446 134 }
30686bf7 135 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
601ae7f2 136 len += 1;
601ae7f2 137
a144f378
JB
138 /* antenna field, if we don't have per-chain info */
139 if (!status->chains)
140 len += 1;
141
90b9e446
JB
142 /* padding for RX_FLAGS if necessary */
143 len = ALIGN(len, 2);
601ae7f2 144
da6a4352 145 if (status->encoding == RX_ENC_HT) /* HT info */
6d744bac
JB
146 len += 3;
147
4c298677 148 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
90b9e446 149 len = ALIGN(len, 4);
4c298677
JB
150 len += 8;
151 }
152
da6a4352 153 if (status->encoding == RX_ENC_VHT) {
51648921
JB
154 len = ALIGN(len, 2);
155 len += 12;
156 }
157
99ee7cae
JB
158 if (local->hw.radiotap_timestamp.units_pos >= 0) {
159 len = ALIGN(len, 8);
160 len += 12;
161 }
162
41cbb0f5
LC
163 if (status->encoding == RX_ENC_HE &&
164 status->flag & RX_FLAG_RADIOTAP_HE) {
165 len = ALIGN(len, 2);
166 len += 12;
167 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
168 }
169
170 if (status->encoding == RX_ENC_HE &&
171 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
172 len = ALIGN(len, 2);
173 len += 12;
174 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
175 }
176
c3d1f875
ST
177 if (status->flag & RX_FLAG_NO_PSDU)
178 len += 1;
179
d1332e7b
ST
180 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
181 len = ALIGN(len, 2);
182 len += 4;
183 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
184 }
185
a144f378
JB
186 if (status->chains) {
187 /* antenna and antenna signal fields */
188 len += 2 * hweight8(status->chains);
189 }
190
9179dff8
MG
191 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
192 int tlv_offset = 0;
c15353be
LK
193
194 /*
195 * The position to look at depends on the existence (or non-
196 * existence) of other elements, so take that into account...
197 */
198 if (status->flag & RX_FLAG_RADIOTAP_HE)
9179dff8 199 tlv_offset +=
c15353be
LK
200 sizeof(struct ieee80211_radiotap_he);
201 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
9179dff8 202 tlv_offset +=
c15353be
LK
203 sizeof(struct ieee80211_radiotap_he_mu);
204 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
9179dff8 205 tlv_offset +=
c15353be
LK
206 sizeof(struct ieee80211_radiotap_lsig);
207
9179dff8
MG
208 /* ensure 4 byte alignment for TLV */
209 len = ALIGN(len, 4);
1f7bba79 210
9179dff8
MG
211 /* TLVs until the mac header */
212 len += skb_mac_header(skb) - &skb->data[tlv_offset];
1f7bba79
JB
213 }
214
601ae7f2
BR
215 return len;
216}
217
f057d140 218static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
4f6c78de 219 int link_id,
f057d140
JB
220 struct sta_info *sta,
221 struct sk_buff *skb)
0044cc17 222{
4f6c78de
JB
223 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
224
225 if (link_id >= 0) {
226 status->link_valid = 1;
227 status->link_id = link_id;
228 } else {
229 status->link_valid = 0;
230 }
231
0044cc17 232 skb_queue_tail(&sdata->skb_queue, skb);
16114496 233 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
0044cc17 234 if (sta)
046d2e7c 235 sta->deflink.rx_stats.packets++;
0044cc17
JB
236}
237
f057d140 238static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
4f6c78de 239 int link_id,
f057d140
JB
240 struct sta_info *sta,
241 struct sk_buff *skb)
242{
243 skb->protocol = 0;
4f6c78de 244 __ieee80211_queue_skb_to_iface(sdata, link_id, sta, skb);
f057d140
JB
245}
246
9e478066
JB
247static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
248 struct sk_buff *skb,
c096b92a 249 int rtap_space)
9e478066
JB
250{
251 struct {
252 struct ieee80211_hdr_3addr hdr;
253 u8 category;
254 u8 action_code;
7c53eb5d 255 } __packed __aligned(2) action;
9e478066
JB
256
257 if (!sdata)
258 return;
259
260 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
261
c096b92a 262 if (skb->len < rtap_space + sizeof(action) +
9e478066
JB
263 VHT_MUMIMO_GROUPS_DATA_LEN)
264 return;
265
266 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
267 return;
268
c096b92a 269 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
9e478066
JB
270
271 if (!ieee80211_is_action(action.hdr.frame_control))
272 return;
273
274 if (action.category != WLAN_CATEGORY_VHT)
275 return;
276
277 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
278 return;
279
280 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
281 return;
282
283 skb = skb_copy(skb, GFP_ATOMIC);
284 if (!skb)
285 return;
286
4f6c78de 287 ieee80211_queue_skb_to_iface(sdata, -1, NULL, skb);
9e478066
JB
288}
289
00ea6deb 290/*
601ae7f2
BR
291 * ieee80211_add_rx_radiotap_header - add radiotap header
292 *
293 * add a radiotap header containing all the fields which the hardware provided.
294 */
295static void
296ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
297 struct sk_buff *skb,
601ae7f2 298 struct ieee80211_rate *rate,
973ef21a 299 int rtap_len, bool has_fcs)
601ae7f2 300{
f1d58c25 301 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
601ae7f2
BR
302 struct ieee80211_radiotap_header *rthdr;
303 unsigned char *pos;
a144f378
JB
304 __le32 *it_present;
305 u32 it_present_val;
6a86b9c7 306 u16 rx_flags = 0;
a5e70697 307 u16 channel_flags = 0;
9179dff8 308 u32 tlvs_len = 0;
a144f378
JB
309 int mpdulen, chain;
310 unsigned long chains = status->chains;
41cbb0f5
LC
311 struct ieee80211_radiotap_he he = {};
312 struct ieee80211_radiotap_he_mu he_mu = {};
d1332e7b 313 struct ieee80211_radiotap_lsig lsig = {};
41cbb0f5
LC
314
315 if (status->flag & RX_FLAG_RADIOTAP_HE) {
316 he = *(struct ieee80211_radiotap_he *)skb->data;
317 skb_pull(skb, sizeof(he));
318 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
319 }
320
321 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
322 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
323 skb_pull(skb, sizeof(he_mu));
324 }
1f7bba79 325
d1332e7b
ST
326 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
327 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
328 skb_pull(skb, sizeof(lsig));
329 }
330
9179dff8
MG
331 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
332 /* data is pointer at tlv all other info was pulled off */
333 tlvs_len = skb_mac_header(skb) - skb->data;
1f7bba79 334 }
f4bda337
TP
335
336 mpdulen = skb->len;
30686bf7 337 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
f4bda337 338 mpdulen += FCS_LEN;
601ae7f2 339
9179dff8
MG
340 rthdr = skb_push(skb, rtap_len - tlvs_len);
341 memset(rthdr, 0, rtap_len - tlvs_len);
a144f378 342 it_present = &rthdr->it_present;
601ae7f2
BR
343
344 /* radiotap header, set always present flags */
0059b2b1 345 rthdr->it_len = cpu_to_le16(rtap_len);
a144f378
JB
346 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
347 BIT(IEEE80211_RADIOTAP_CHANNEL) |
348 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
349
350 if (!status->chains)
351 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
352
353 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
354 it_present_val |=
355 BIT(IEEE80211_RADIOTAP_EXT) |
356 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
357 put_unaligned_le32(it_present_val, it_present);
358 it_present++;
359 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
360 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
361 }
601ae7f2 362
9179dff8
MG
363 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
364 it_present_val |= BIT(IEEE80211_RADIOTAP_TLV);
1f7bba79 365
a144f378
JB
366 put_unaligned_le32(it_present_val, it_present);
367
8c89f7b3
KC
368 /* This references through an offset into it_optional[] rather
369 * than via it_present otherwise later uses of pos will cause
370 * the compiler to think we have walked past the end of the
371 * struct member.
372 */
c033a38a 373 pos = (void *)&rthdr->it_optional[it_present + 1 - rthdr->it_optional];
a144f378 374
601ae7f2
BR
375 /* the order of the following fields is important */
376
377 /* IEEE80211_RADIOTAP_TSFT */
f4bda337 378 if (ieee80211_have_rx_timestamp(status)) {
90b9e446
JB
379 /* padding */
380 while ((pos - (u8 *)rthdr) & 7)
381 *pos++ = 0;
f4bda337
TP
382 put_unaligned_le64(
383 ieee80211_calculate_rx_timestamp(local, status,
384 mpdulen, 0),
385 pos);
5cafd378 386 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_TSFT));
601ae7f2
BR
387 pos += 8;
388 }
389
390 /* IEEE80211_RADIOTAP_FLAGS */
30686bf7 391 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
601ae7f2 392 *pos |= IEEE80211_RADIOTAP_F_FCS;
aae89831
JB
393 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
394 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
7fdd69c5 395 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
b4f28bbb 396 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
601ae7f2
BR
397 pos++;
398
399 /* IEEE80211_RADIOTAP_RATE */
da6a4352 400 if (!rate || status->encoding != RX_ENC_LEGACY) {
0fb8ca45 401 /*
f8d1ccf1 402 * Without rate information don't add it. If we have,
38f37be2 403 * MCS information is a separate field in radiotap,
73b48099
JB
404 * added below. The byte here is needed as padding
405 * for the channel though, so initialise it to 0.
0fb8ca45
JM
406 */
407 *pos = 0;
8d6f658e 408 } else {
2103dec1 409 int shift = 0;
5cafd378 410 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_RATE));
da6a4352 411 if (status->bw == RATE_INFO_BW_10)
2103dec1 412 shift = 1;
da6a4352 413 else if (status->bw == RATE_INFO_BW_5)
2103dec1
SW
414 shift = 2;
415 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
8d6f658e 416 }
601ae7f2
BR
417 pos++;
418
419 /* IEEE80211_RADIOTAP_CHANNEL */
3b23c184 420 /* TODO: frequency offset in KHz */
6a86b9c7 421 put_unaligned_le16(status->freq, pos);
601ae7f2 422 pos += 2;
da6a4352 423 if (status->bw == RATE_INFO_BW_10)
a5e70697 424 channel_flags |= IEEE80211_CHAN_HALF;
da6a4352 425 else if (status->bw == RATE_INFO_BW_5)
a5e70697
SW
426 channel_flags |= IEEE80211_CHAN_QUARTER;
427
412a84b5
AD
428 if (status->band == NL80211_BAND_5GHZ ||
429 status->band == NL80211_BAND_6GHZ)
a5e70697 430 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
da6a4352 431 else if (status->encoding != RX_ENC_LEGACY)
a5e70697 432 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
f8d1ccf1 433 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
a5e70697 434 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
f8d1ccf1 435 else if (rate)
3a5c5e81 436 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
f8d1ccf1 437 else
a5e70697
SW
438 channel_flags |= IEEE80211_CHAN_2GHZ;
439 put_unaligned_le16(channel_flags, pos);
601ae7f2
BR
440 pos += 2;
441
442 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
30686bf7 443 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
fe8431f8 444 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
601ae7f2
BR
445 *pos = status->signal;
446 rthdr->it_present |=
5cafd378 447 cpu_to_le32(BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL));
601ae7f2
BR
448 pos++;
449 }
450
601ae7f2
BR
451 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
452
a144f378
JB
453 if (!status->chains) {
454 /* IEEE80211_RADIOTAP_ANTENNA */
455 *pos = status->antenna;
456 pos++;
457 }
601ae7f2 458
601ae7f2
BR
459 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
460
461 /* IEEE80211_RADIOTAP_RX_FLAGS */
462 /* ensure 2 byte alignment for the 2 byte field as required */
6a86b9c7 463 if ((pos - (u8 *)rthdr) & 1)
90b9e446 464 *pos++ = 0;
aae89831 465 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
6a86b9c7
JB
466 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
467 put_unaligned_le16(rx_flags, pos);
601ae7f2 468 pos += 2;
6d744bac 469
da6a4352 470 if (status->encoding == RX_ENC_HT) {
786677d1
OR
471 unsigned int stbc;
472
5cafd378 473 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_MCS));
57553c3a
P
474 *pos = local->hw.radiotap_mcs_details;
475 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
476 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
477 if (status->enc_flags & RX_ENC_FLAG_LDPC)
478 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FEC;
479 pos++;
6d744bac 480 *pos = 0;
7fdd69c5 481 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
6d744bac 482 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
da6a4352 483 if (status->bw == RATE_INFO_BW_40)
6d744bac 484 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
7fdd69c5 485 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
ac55d2fe 486 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
7fdd69c5 487 if (status->enc_flags & RX_ENC_FLAG_LDPC)
63c361f5 488 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
7fdd69c5 489 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
786677d1 490 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
6d744bac
JB
491 pos++;
492 *pos++ = status->rate_idx;
493 }
4c298677
JB
494
495 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
496 u16 flags = 0;
497
498 /* ensure 4 byte alignment */
499 while ((pos - (u8 *)rthdr) & 3)
500 pos++;
501 rthdr->it_present |=
5cafd378 502 cpu_to_le32(BIT(IEEE80211_RADIOTAP_AMPDU_STATUS));
4c298677
JB
503 put_unaligned_le32(status->ampdu_reference, pos);
504 pos += 4;
4c298677
JB
505 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
506 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
507 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
508 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
509 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
510 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
7299d6f7
JB
511 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
512 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
513 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
514 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
4c298677
JB
515 put_unaligned_le16(flags, pos);
516 pos += 2;
006a97ce 517 *pos++ = 0;
4c298677
JB
518 *pos++ = 0;
519 }
90b9e446 520
da6a4352 521 if (status->encoding == RX_ENC_VHT) {
51648921
JB
522 u16 known = local->hw.radiotap_vht_details;
523
5cafd378 524 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_VHT));
51648921
JB
525 put_unaligned_le16(known, pos);
526 pos += 2;
527 /* flags */
7fdd69c5 528 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
51648921 529 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
63c361f5 530 /* in VHT, STBC is binary */
7fdd69c5 531 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
63c361f5 532 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
7fdd69c5 533 if (status->enc_flags & RX_ENC_FLAG_BF)
fb378c23 534 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
51648921
JB
535 pos++;
536 /* bandwidth */
da6a4352
JB
537 switch (status->bw) {
538 case RATE_INFO_BW_80:
51648921 539 *pos++ = 4;
da6a4352
JB
540 break;
541 case RATE_INFO_BW_160:
51648921 542 *pos++ = 11;
da6a4352
JB
543 break;
544 case RATE_INFO_BW_40:
51648921 545 *pos++ = 1;
da6a4352
JB
546 break;
547 default:
51648921 548 *pos++ = 0;
da6a4352 549 }
51648921 550 /* MCS/NSS */
8613c948 551 *pos = (status->rate_idx << 4) | status->nss;
51648921
JB
552 pos += 4;
553 /* coding field */
7fdd69c5 554 if (status->enc_flags & RX_ENC_FLAG_LDPC)
63c361f5 555 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
51648921
JB
556 pos++;
557 /* group ID */
558 pos++;
559 /* partial_aid */
560 pos += 2;
561 }
562
99ee7cae
JB
563 if (local->hw.radiotap_timestamp.units_pos >= 0) {
564 u16 accuracy = 0;
e62c0fcc
JB
565 u8 flags;
566 u64 ts;
99ee7cae
JB
567
568 rthdr->it_present |=
5cafd378 569 cpu_to_le32(BIT(IEEE80211_RADIOTAP_TIMESTAMP));
99ee7cae
JB
570
571 /* ensure 8 byte alignment */
572 while ((pos - (u8 *)rthdr) & 7)
573 pos++;
574
e62c0fcc
JB
575 if (status->flag & RX_FLAG_MACTIME_IS_RTAP_TS64) {
576 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_64BIT;
577 ts = status->mactime;
578 } else {
579 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
580 ts = status->device_timestamp;
581 }
582
583 put_unaligned_le64(ts, pos);
99ee7cae
JB
584 pos += sizeof(u64);
585
586 if (local->hw.radiotap_timestamp.accuracy >= 0) {
587 accuracy = local->hw.radiotap_timestamp.accuracy;
588 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
589 }
590 put_unaligned_le16(accuracy, pos);
591 pos += sizeof(u16);
592
593 *pos++ = local->hw.radiotap_timestamp.units_pos;
594 *pos++ = flags;
595 }
596
41cbb0f5
LC
597 if (status->encoding == RX_ENC_HE &&
598 status->flag & RX_FLAG_RADIOTAP_HE) {
331aead5 599#define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
41cbb0f5
LC
600
601 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
602 he.data6 |= HE_PREP(DATA6_NSTS,
603 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
604 status->enc_flags));
605 he.data3 |= HE_PREP(DATA3_STBC, 1);
606 } else {
607 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
608 }
609
610#define CHECK_GI(s) \
611 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
612 (int)NL80211_RATE_INFO_HE_GI_##s)
613
614 CHECK_GI(0_8);
615 CHECK_GI(1_6);
616 CHECK_GI(3_2);
617
618 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
619 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
620 he.data3 |= HE_PREP(DATA3_CODING,
621 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
622
623 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
624
625 switch (status->bw) {
626 case RATE_INFO_BW_20:
627 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
628 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
629 break;
630 case RATE_INFO_BW_40:
631 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
632 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
633 break;
634 case RATE_INFO_BW_80:
635 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
636 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
637 break;
638 case RATE_INFO_BW_160:
639 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
640 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
641 break;
642 case RATE_INFO_BW_HE_RU:
643#define CHECK_RU_ALLOC(s) \
644 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
645 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
646
647 CHECK_RU_ALLOC(26);
648 CHECK_RU_ALLOC(52);
649 CHECK_RU_ALLOC(106);
650 CHECK_RU_ALLOC(242);
651 CHECK_RU_ALLOC(484);
652 CHECK_RU_ALLOC(996);
653 CHECK_RU_ALLOC(2x996);
654
655 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
656 status->he_ru + 4);
657 break;
658 default:
659 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
660 }
661
662 /* ensure 2 byte alignment */
663 while ((pos - (u8 *)rthdr) & 1)
664 pos++;
5cafd378 665 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE));
41cbb0f5
LC
666 memcpy(pos, &he, sizeof(he));
667 pos += sizeof(he);
668 }
669
670 if (status->encoding == RX_ENC_HE &&
671 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
672 /* ensure 2 byte alignment */
673 while ((pos - (u8 *)rthdr) & 1)
674 pos++;
5cafd378 675 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE_MU));
41cbb0f5
LC
676 memcpy(pos, &he_mu, sizeof(he_mu));
677 pos += sizeof(he_mu);
678 }
679
c3d1f875
ST
680 if (status->flag & RX_FLAG_NO_PSDU) {
681 rthdr->it_present |=
5cafd378 682 cpu_to_le32(BIT(IEEE80211_RADIOTAP_ZERO_LEN_PSDU));
c3d1f875
ST
683 *pos++ = status->zero_length_psdu_type;
684 }
685
d1332e7b
ST
686 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
687 /* ensure 2 byte alignment */
688 while ((pos - (u8 *)rthdr) & 1)
689 pos++;
5cafd378 690 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_LSIG));
d1332e7b
ST
691 memcpy(pos, &lsig, sizeof(lsig));
692 pos += sizeof(lsig);
693 }
694
a144f378
JB
695 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
696 *pos++ = status->chain_signal[chain];
697 *pos++ = chain;
698 }
601ae7f2
BR
699}
700
127f60bf
JB
701static struct sk_buff *
702ieee80211_make_monitor_skb(struct ieee80211_local *local,
703 struct sk_buff **origskb,
704 struct ieee80211_rate *rate,
c096b92a 705 int rtap_space, bool use_origskb)
127f60bf
JB
706{
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);
c096b92a 713 needed_headroom = rt_hdrlen - rtap_space;
127f60bf
JB
714
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 */
ec61cd49
JA
737 skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD,
738 0, GFP_ATOMIC);
127f60bf
JB
739
740 if (!skb)
741 return NULL;
742 }
743
744 /* prepend radiotap information */
745 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
746
747 skb_reset_mac_header(skb);
748 skb->ip_summed = CHECKSUM_UNNECESSARY;
749 skb->pkt_type = PACKET_OTHERHOST;
750 skb->protocol = htons(ETH_P_802_2);
751
752 return skb;
753}
754
b2e7771e
JB
755/*
756 * This function copies a received frame to all monitor interfaces and
757 * returns a cleaned-up SKB that no longer includes the FCS nor the
758 * radiotap header the driver might have added.
759 */
760static struct sk_buff *
761ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a 762 struct ieee80211_rate *rate)
b2e7771e 763{
f1d58c25 764 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
342afe69
FF
765 struct ieee80211_sub_if_data *sdata, *prev_sdata = NULL;
766 struct sk_buff *skb, *monskb = NULL;
b2e7771e 767 int present_fcs_len = 0;
c096b92a 768 unsigned int rtap_space = 0;
42bd20d9
AE
769 struct ieee80211_sub_if_data *monitor_sdata =
770 rcu_dereference(local->monitor_sdata);
127f60bf 771 bool only_monitor = false;
8020919a 772 unsigned int min_head_len;
1f7bba79 773
9179dff8
MG
774 if (WARN_ON_ONCE(status->flag & RX_FLAG_RADIOTAP_TLV_AT_END &&
775 !skb_mac_header_was_set(origskb))) {
776 /* with this skb no way to know where frame payload starts */
777 dev_kfree_skb(origskb);
778 return NULL;
779 }
780
41cbb0f5
LC
781 if (status->flag & RX_FLAG_RADIOTAP_HE)
782 rtap_space += sizeof(struct ieee80211_radiotap_he);
783
784 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
785 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
786
d359bbce
IP
787 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
788 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
789
9179dff8
MG
790 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
791 rtap_space += skb_mac_header(origskb) - &origskb->data[rtap_space];
b2e7771e 792
8020919a
IP
793 min_head_len = rtap_space;
794
b2e7771e
JB
795 /*
796 * First, we may need to make a copy of the skb because
797 * (1) we need to modify it for radiotap (if not present), and
798 * (2) the other RX handlers will modify the skb we got.
799 *
800 * We don't need to, of course, if we aren't going to return
801 * the SKB because it has a bad FCS/PLCP checksum.
802 */
0869aea0 803
8020919a
IP
804 if (!(status->flag & RX_FLAG_NO_PSDU)) {
805 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
806 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
807 /* driver bug */
808 WARN_ON(1);
809 dev_kfree_skb(origskb);
810 return NULL;
811 }
812 present_fcs_len = FCS_LEN;
30841f5c 813 }
8020919a
IP
814
815 /* also consider the hdr->frame_control */
816 min_head_len += 2;
30841f5c 817 }
b2e7771e 818
8020919a
IP
819 /* ensure that the expected data elements are in skb head */
820 if (!pskb_may_pull(origskb, min_head_len)) {
e3cf8b3f
ZY
821 dev_kfree_skb(origskb);
822 return NULL;
823 }
824
c096b92a 825 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
127f60bf 826
17883048 827 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
127f60bf 828 if (only_monitor) {
b2e7771e
JB
829 dev_kfree_skb(origskb);
830 return NULL;
831 }
832
c1129924
JB
833 return ieee80211_clean_skb(origskb, present_fcs_len,
834 rtap_space);
b2e7771e
JB
835 }
836
c096b92a 837 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
9e478066 838
f64331d5 839 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
f92e0cf1
FF
840 struct cfg80211_chan_def *chandef;
841
842 chandef = &sdata->vif.bss_conf.chanreq.oper;
843 if (chandef->chan &&
844 chandef->chan->center_freq != status->freq)
845 continue;
846
342afe69
FF
847 if (!prev_sdata) {
848 prev_sdata = sdata;
849 continue;
850 }
127f60bf 851
9d40f7e3
FF
852 if (ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR))
853 ieee80211_handle_mu_mimo_mon(sdata, origskb, rtap_space);
854
127f60bf
JB
855 if (!monskb)
856 monskb = ieee80211_make_monitor_skb(local, &origskb,
c096b92a 857 rate, rtap_space,
342afe69
FF
858 false);
859 if (!monskb)
860 continue;
127f60bf 861
342afe69
FF
862 skb = skb_clone(monskb, GFP_ATOMIC);
863 if (!skb)
864 continue;
865
866 skb->dev = prev_sdata->dev;
867 dev_sw_netstats_rx_add(skb->dev, skb->len);
868 netif_receive_skb(skb);
869 prev_sdata = sdata;
870 }
871
872 if (prev_sdata) {
873 if (monskb)
874 skb = monskb;
875 else
876 skb = ieee80211_make_monitor_skb(local, &origskb,
877 rate, rtap_space,
878 only_monitor);
879 if (skb) {
880 skb->dev = prev_sdata->dev;
881 dev_sw_netstats_rx_add(skb->dev, skb->len);
882 netif_receive_skb(skb);
b2e7771e 883 }
b2e7771e
JB
884 }
885
127f60bf
JB
886 if (!origskb)
887 return NULL;
b2e7771e 888
c1129924 889 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
b2e7771e
JB
890}
891
5cf121c3 892static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d 893{
238f74a2 894 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 895 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
9e26297a 896 int tid, seqno_idx, security_idx;
6e0d114d
JB
897
898 /* does the frame have a qos control field? */
238f74a2
HH
899 if (ieee80211_is_data_qos(hdr->frame_control)) {
900 u8 *qc = ieee80211_get_qos_ctl(hdr);
6e0d114d 901 /* frame has qos control */
238f74a2 902 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
04b7dcf9 903 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
554891e6 904 status->rx_flags |= IEEE80211_RX_AMSDU;
9e26297a
JB
905
906 seqno_idx = tid;
907 security_idx = tid;
6e0d114d 908 } else {
1411f9b5
JB
909 /*
910 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
911 *
912 * Sequence numbers for management frames, QoS data
913 * frames with a broadcast/multicast address in the
914 * Address 1 field, and all non-QoS data frames sent
915 * by QoS STAs are assigned using an additional single
916 * modulo-4096 counter, [...]
917 *
918 * We also use that counter for non-QoS STAs.
919 */
5a306f58 920 seqno_idx = IEEE80211_NUM_TIDS;
9e26297a
JB
921 security_idx = 0;
922 if (ieee80211_is_mgmt(hdr->frame_control))
5a306f58 923 security_idx = IEEE80211_NUM_TIDS;
9e26297a 924 tid = 0;
6e0d114d 925 }
52865dfd 926
9e26297a
JB
927 rx->seqno_idx = seqno_idx;
928 rx->security_idx = security_idx;
6e0d114d
JB
929 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
930 * For now, set skb->priority to 0 for other cases. */
931 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 932}
6e0d114d 933
d1c3a37c
JB
934/**
935 * DOC: Packet alignment
936 *
937 * Drivers always need to pass packets that are aligned to two-byte boundaries
938 * to the stack.
939 *
cd336152 940 * Additionally, they should, if possible, align the payload data in a way that
d1c3a37c
JB
941 * guarantees that the contained IP header is aligned to a four-byte
942 * boundary. In the case of regular frames, this simply means aligning the
943 * payload to a four-byte boundary (because either the IP header is directly
944 * contained, or IV/RFC1042 headers that have a length divisible by four are
59d9cb07
KV
945 * in front of it). If the payload data is not properly aligned and the
946 * architecture doesn't support efficient unaligned operations, mac80211
947 * will align the data.
d1c3a37c
JB
948 *
949 * With A-MSDU frames, however, the payload data address must yield two modulo
950 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
951 * push the IP header further back to a multiple of four again. Thankfully, the
952 * specs were sane enough this time around to require padding each A-MSDU
953 * subframe to a length that is a multiple of four.
954 *
25985edc 955 * Padding like Atheros hardware adds which is between the 802.11 header and
cd336152 956 * the payload is not supported; the driver is required to move the 802.11
d1c3a37c
JB
957 * header to be directly in front of the payload in that case.
958 */
959static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
38f3714d 960{
59d9cb07 961#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
441275e1 962 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
d1c3a37c 963#endif
6e0d114d
JB
964}
965
6368e4b1 966
571ecf67
JB
967/* rx handlers */
968
3cfcf6ac
JM
969static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
970{
971 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
972
d8ca16db 973 if (is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
974 return 0;
975
d8ca16db 976 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
977}
978
979
980static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
981{
982 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
983
d8ca16db 984 if (!is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
985 return 0;
986
d8ca16db 987 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
988}
989
990
991/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
992static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
993{
994 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
995 struct ieee80211_mmie *mmie;
56c52da2 996 struct ieee80211_mmie_16 *mmie16;
3cfcf6ac 997
1df332e8 998 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
3cfcf6ac
JM
999 return -1;
1000
af2d14b0
JM
1001 if (!ieee80211_is_robust_mgmt_frame(skb) &&
1002 !ieee80211_is_beacon(hdr->frame_control))
3cfcf6ac
JM
1003 return -1; /* not a robust management frame */
1004
1005 mmie = (struct ieee80211_mmie *)
1006 (skb->data + skb->len - sizeof(*mmie));
56c52da2
JM
1007 if (mmie->element_id == WLAN_EID_MMIE &&
1008 mmie->length == sizeof(*mmie) - 2)
1009 return le16_to_cpu(mmie->key_id);
1010
1011 mmie16 = (struct ieee80211_mmie_16 *)
1012 (skb->data + skb->len - sizeof(*mmie16));
1013 if (skb->len >= 24 + sizeof(*mmie16) &&
1014 mmie16->element_id == WLAN_EID_MMIE &&
1015 mmie16->length == sizeof(*mmie16) - 2)
1016 return le16_to_cpu(mmie16->key_id);
1017
1018 return -1;
3cfcf6ac
JM
1019}
1020
23a5f0af 1021static int ieee80211_get_keyid(struct sk_buff *skb)
2475b1cc
MS
1022{
1023 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
23a5f0af
JB
1024 __le16 fc = hdr->frame_control;
1025 int hdrlen = ieee80211_hdrlen(fc);
2475b1cc
MS
1026 u8 keyid;
1027
23a5f0af
JB
1028 /* WEP, TKIP, CCMP and GCMP */
1029 if (unlikely(skb->len < hdrlen + IEEE80211_WEP_IV_LEN))
2475b1cc
MS
1030 return -EINVAL;
1031
23a5f0af 1032 skb_copy_bits(skb, hdrlen + 3, &keyid, 1);
96fc6efb 1033
23a5f0af 1034 keyid >>= 6;
2475b1cc
MS
1035
1036 return keyid;
1037}
1038
1df332e8 1039static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2 1040{
a7767f95 1041 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
47846c9b 1042 char *dev_addr = rx->sdata->vif.addr;
d6d1a5a7 1043
a7767f95 1044 if (ieee80211_is_data(hdr->frame_control)) {
3c5772a5
JC
1045 if (is_multicast_ether_addr(hdr->addr1)) {
1046 if (ieee80211_has_tods(hdr->frame_control) ||
1df332e8 1047 !ieee80211_has_fromds(hdr->frame_control))
286e6967 1048 return RX_DROP;
b203ca39 1049 if (ether_addr_equal(hdr->addr3, dev_addr))
286e6967 1050 return RX_DROP;
3c5772a5
JC
1051 } else {
1052 if (!ieee80211_has_a4(hdr->frame_control))
286e6967 1053 return RX_DROP;
b203ca39 1054 if (ether_addr_equal(hdr->addr4, dev_addr))
286e6967 1055 return RX_DROP;
3c5772a5 1056 }
33b64eb2
LCC
1057 }
1058
1059 /* If there is not an established peer link and this is not a peer link
1060 * establisment frame, beacon or probe, drop the frame.
1061 */
1062
57cf8043 1063 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
33b64eb2 1064 struct ieee80211_mgmt *mgmt;
d6d1a5a7 1065
a7767f95 1066 if (!ieee80211_is_mgmt(hdr->frame_control))
286e6967 1067 return RX_DROP;
33b64eb2 1068
a7767f95 1069 if (ieee80211_is_action(hdr->frame_control)) {
d3aaec8a 1070 u8 category;
9b395bc3
JB
1071
1072 /* make sure category field is present */
1073 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
286e6967 1074 return RX_DROP;
9b395bc3 1075
33b64eb2 1076 mgmt = (struct ieee80211_mgmt *)hdr;
d3aaec8a
JC
1077 category = mgmt->u.action.category;
1078 if (category != WLAN_CATEGORY_MESH_ACTION &&
1df332e8 1079 category != WLAN_CATEGORY_SELF_PROTECTED)
286e6967 1080 return RX_DROP;
33b64eb2 1081 return RX_CONTINUE;
33b64eb2
LCC
1082 }
1083
a7767f95
HH
1084 if (ieee80211_is_probe_req(hdr->frame_control) ||
1085 ieee80211_is_probe_resp(hdr->frame_control) ||
71839121
JC
1086 ieee80211_is_beacon(hdr->frame_control) ||
1087 ieee80211_is_auth(hdr->frame_control))
a7767f95
HH
1088 return RX_CONTINUE;
1089
286e6967 1090 return RX_DROP;
a7767f95
HH
1091 }
1092
902acc78
JB
1093 return RX_CONTINUE;
1094}
33b64eb2 1095
fb4ea054
JB
1096static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1097 int index)
1098{
1099 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1100 struct sk_buff *tail = skb_peek_tail(frames);
1101 struct ieee80211_rx_status *status;
1102
b98c1610
PKS
1103 if (tid_agg_rx->reorder_buf_filtered &&
1104 tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
06470f74
SS
1105 return true;
1106
fb4ea054
JB
1107 if (!tail)
1108 return false;
1109
1110 status = IEEE80211_SKB_RXCB(tail);
1111 if (status->flag & RX_FLAG_AMSDU_MORE)
1112 return false;
1113
1114 return true;
1115}
1116
d3b2fb53 1117static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1edfb1af 1118 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
1119 int index,
1120 struct sk_buff_head *frames)
1edfb1af 1121{
83eb935e
MK
1122 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1123 struct sk_buff *skb;
4cfda47b 1124 struct ieee80211_rx_status *status;
1edfb1af 1125
dd318575
JB
1126 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1127
83eb935e 1128 if (skb_queue_empty(skb_list))
1edfb1af
JB
1129 goto no_frame;
1130
fb4ea054 1131 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
83eb935e
MK
1132 __skb_queue_purge(skb_list);
1133 goto no_frame;
1134 }
1135
1136 /* release frames from the reorder ring buffer */
1edfb1af 1137 tid_agg_rx->stored_mpdu_num--;
83eb935e
MK
1138 while ((skb = __skb_dequeue(skb_list))) {
1139 status = IEEE80211_SKB_RXCB(skb);
1140 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1141 __skb_queue_tail(frames, skb);
1142 }
1edfb1af
JB
1143
1144no_frame:
b98c1610
PKS
1145 if (tid_agg_rx->reorder_buf_filtered)
1146 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
9a886586 1147 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1edfb1af
JB
1148}
1149
d3b2fb53 1150static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1edfb1af 1151 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
1152 u16 head_seq_num,
1153 struct sk_buff_head *frames)
1edfb1af
JB
1154{
1155 int index;
1156
dd318575
JB
1157 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1158
9a886586 1159 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2e3049b7 1160 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
f9e124fb
CL
1161 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1162 frames);
1edfb1af
JB
1163 }
1164}
1165
1166/*
1167 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1168 * the skb was added to the buffer longer than this time ago, the earlier
1169 * frames that have not yet been received are assumed to be lost and the skb
1170 * can be released for processing. This may also release other skb's from the
1171 * reorder buffer if there are no additional gaps between the frames.
2bff8ebf
CL
1172 *
1173 * Callers must hold tid_agg_rx->reorder_lock.
1edfb1af
JB
1174 */
1175#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1176
d3b2fb53 1177static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
f9e124fb
CL
1178 struct tid_ampdu_rx *tid_agg_rx,
1179 struct sk_buff_head *frames)
aa0c8636 1180{
83eb935e 1181 int index, i, j;
aa0c8636 1182
dd318575
JB
1183 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1184
aa0c8636 1185 /* release the buffer until next missing frame */
2e3049b7 1186 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
fb4ea054 1187 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
07ae2dfc 1188 tid_agg_rx->stored_mpdu_num) {
aa0c8636
CL
1189 /*
1190 * No buffers ready to be released, but check whether any
1191 * frames in the reorder buffer have timed out.
1192 */
aa0c8636
CL
1193 int skipped = 1;
1194 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1195 j = (j + 1) % tid_agg_rx->buf_size) {
fb4ea054 1196 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
aa0c8636
CL
1197 skipped++;
1198 continue;
1199 }
499fe9a4
DH
1200 if (skipped &&
1201 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
aa0c8636 1202 HT_RX_REORDER_BUF_TIMEOUT))
2bff8ebf 1203 goto set_release_timer;
aa0c8636 1204
83eb935e
MK
1205 /* don't leave incomplete A-MSDUs around */
1206 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1207 i = (i + 1) % tid_agg_rx->buf_size)
1208 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1209
bdcbd8e0
JB
1210 ht_dbg_ratelimited(sdata,
1211 "release an RX reorder frame due to timeout on earlier frames\n");
f9e124fb
CL
1212 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1213 frames);
aa0c8636
CL
1214
1215 /*
1216 * Increment the head seq# also for the skipped slots.
1217 */
1218 tid_agg_rx->head_seq_num =
9a886586
JB
1219 (tid_agg_rx->head_seq_num +
1220 skipped) & IEEE80211_SN_MASK;
aa0c8636
CL
1221 skipped = 0;
1222 }
fb4ea054 1223 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
f9e124fb
CL
1224 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1225 frames);
2e3049b7 1226 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
aa0c8636 1227 }
2bff8ebf
CL
1228
1229 if (tid_agg_rx->stored_mpdu_num) {
2e3049b7 1230 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
2bff8ebf
CL
1231
1232 for (; j != (index - 1) % tid_agg_rx->buf_size;
1233 j = (j + 1) % tid_agg_rx->buf_size) {
fb4ea054 1234 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
2bff8ebf
CL
1235 break;
1236 }
1237
1238 set_release_timer:
1239
788211d8
JB
1240 if (!tid_agg_rx->removed)
1241 mod_timer(&tid_agg_rx->reorder_timer,
1242 tid_agg_rx->reorder_time[j] + 1 +
1243 HT_RX_REORDER_BUF_TIMEOUT);
2bff8ebf 1244 } else {
8fa7292f 1245 timer_delete(&tid_agg_rx->reorder_timer);
2bff8ebf 1246 }
aa0c8636
CL
1247}
1248
1edfb1af
JB
1249/*
1250 * As this function belongs to the RX path it must be under
1251 * rcu_read_lock protection. It returns false if the frame
1252 * can be processed immediately, true if it was consumed.
1253 */
d3b2fb53 1254static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1edfb1af 1255 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
1256 struct sk_buff *skb,
1257 struct sk_buff_head *frames)
1edfb1af
JB
1258{
1259 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
83eb935e 1260 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
b1344b13 1261 u16 mpdu_seq_num = ieee80211_get_sn(hdr);
1edfb1af
JB
1262 u16 head_seq_num, buf_size;
1263 int index;
2bff8ebf 1264 bool ret = true;
1edfb1af 1265
dd318575
JB
1266 spin_lock(&tid_agg_rx->reorder_lock);
1267
4549cf2b
MK
1268 /*
1269 * Offloaded BA sessions have no known starting sequence number so pick
1270 * one from first Rxed frame for this tid after BA was started.
1271 */
1272 if (unlikely(tid_agg_rx->auto_seq)) {
1273 tid_agg_rx->auto_seq = false;
1274 tid_agg_rx->ssn = mpdu_seq_num;
1275 tid_agg_rx->head_seq_num = mpdu_seq_num;
1276 }
1277
1edfb1af
JB
1278 buf_size = tid_agg_rx->buf_size;
1279 head_seq_num = tid_agg_rx->head_seq_num;
1280
b7540d8f
SS
1281 /*
1282 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1283 * be reordered.
1284 */
1285 if (unlikely(!tid_agg_rx->started)) {
1286 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1287 ret = false;
1288 goto out;
1289 }
1290 tid_agg_rx->started = true;
1291 }
1292
1edfb1af 1293 /* frame with out of date sequence number */
9a886586 1294 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1edfb1af 1295 dev_kfree_skb(skb);
2bff8ebf 1296 goto out;
1edfb1af
JB
1297 }
1298
1299 /*
1300 * If frame the sequence number exceeds our buffering window
1301 * size release some previous frames to make room for this one.
1302 */
9a886586
JB
1303 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1304 head_seq_num = ieee80211_sn_inc(
1305 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1edfb1af 1306 /* release stored frames up to new head to stack */
d3b2fb53 1307 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
f9e124fb 1308 head_seq_num, frames);
1edfb1af
JB
1309 }
1310
1311 /* Now the new frame is always in the range of the reordering buffer */
1312
2e3049b7 1313 index = mpdu_seq_num % tid_agg_rx->buf_size;
1edfb1af
JB
1314
1315 /* check if we already stored this frame */
fb4ea054 1316 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1edfb1af 1317 dev_kfree_skb(skb);
2bff8ebf 1318 goto out;
1edfb1af
JB
1319 }
1320
1321 /*
1322 * If the current MPDU is in the right order and nothing else
1323 * is stored we can process it directly, no need to buffer it.
c835b214
JB
1324 * If it is first but there's something stored, we may be able
1325 * to release frames after this one.
1edfb1af
JB
1326 */
1327 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1328 tid_agg_rx->stored_mpdu_num == 0) {
83eb935e
MK
1329 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1330 tid_agg_rx->head_seq_num =
1331 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
2bff8ebf
CL
1332 ret = false;
1333 goto out;
1edfb1af
JB
1334 }
1335
1336 /* put the frame in the reordering buffer */
83eb935e
MK
1337 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1338 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1339 tid_agg_rx->reorder_time[index] = jiffies;
1340 tid_agg_rx->stored_mpdu_num++;
1341 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1342 }
1edfb1af 1343
2bff8ebf
CL
1344 out:
1345 spin_unlock(&tid_agg_rx->reorder_lock);
1346 return ret;
1edfb1af
JB
1347}
1348
1349/*
1350 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1351 * true if the MPDU was buffered, false if it should be processed.
1352 */
f9e124fb
CL
1353static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1354 struct sk_buff_head *frames)
1edfb1af 1355{
2569a826 1356 struct sk_buff *skb = rx->skb;
1edfb1af 1357 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2569a826 1358 struct sta_info *sta = rx->sta;
1edfb1af
JB
1359 struct tid_ampdu_rx *tid_agg_rx;
1360 u16 sc;
6cc00d54 1361 u8 tid, ack_policy;
1edfb1af 1362
051a41fa
JB
1363 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1364 is_multicast_ether_addr(hdr->addr1))
2569a826 1365 goto dont_reorder;
1edfb1af
JB
1366
1367 /*
1368 * filter the QoS data rx stream according to
1369 * STA/TID and check if this STA/TID is on aggregation
1370 */
1371
1edfb1af 1372 if (!sta)
2569a826 1373 goto dont_reorder;
1edfb1af 1374
6cc00d54
TP
1375 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1376 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
a1f2ba04 1377 tid = ieee80211_get_tid(hdr);
1edfb1af 1378
a87f736d 1379 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
bfe40fa3
JB
1380 if (!tid_agg_rx) {
1381 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1382 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1383 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1384 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1385 WLAN_BACK_RECIPIENT,
1386 WLAN_REASON_QSTA_REQUIRE_SETUP);
a87f736d 1387 goto dont_reorder;
bfe40fa3 1388 }
1edfb1af
JB
1389
1390 /* qos null data frames are excluded */
1391 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
a87f736d 1392 goto dont_reorder;
1edfb1af 1393
6cc00d54 1394 /* not part of a BA session */
5e469ed9 1395 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
6cc00d54
TP
1396 goto dont_reorder;
1397
1edfb1af
JB
1398 /* new, potentially un-ordered, ampdu frame - process it */
1399
1400 /* reset session timer */
1401 if (tid_agg_rx->timeout)
12d3952f 1402 tid_agg_rx->last_rx = jiffies;
1edfb1af
JB
1403
1404 /* if this mpdu is fragmented - terminate rx aggregation session */
1405 sc = le16_to_cpu(hdr->seq_ctrl);
1406 if (sc & IEEE80211_SCTL_FRAG) {
4f6c78de 1407 ieee80211_queue_skb_to_iface(rx->sdata, rx->link_id, NULL, skb);
2569a826 1408 return;
1edfb1af
JB
1409 }
1410
a87f736d
JB
1411 /*
1412 * No locking needed -- we will only ever process one
1413 * RX packet at a time, and thus own tid_agg_rx. All
1414 * other code manipulating it needs to (and does) make
1415 * sure that we cannot get to it any more before doing
1416 * anything with it.
1417 */
f9e124fb
CL
1418 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1419 frames))
2569a826
JB
1420 return;
1421
1422 dont_reorder:
f9e124fb 1423 __skb_queue_tail(frames, skb);
1edfb1af 1424}
33b64eb2 1425
49461622 1426static ieee80211_rx_result debug_noinline
0395442a 1427ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
571ecf67 1428{
a7767f95 1429 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 1430 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67 1431
f9cfa5f3
SS
1432 if (status->flag & RX_FLAG_DUP_VALIDATED)
1433 return RX_CONTINUE;
1434
6b0f3274
JB
1435 /*
1436 * Drop duplicate 802.11 retransmissions
1437 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1438 */
0395442a
JB
1439
1440 if (rx->skb->len < 24)
1441 return RX_CONTINUE;
1442
1443 if (ieee80211_is_ctl(hdr->frame_control) ||
67625910 1444 ieee80211_is_any_nullfunc(hdr->frame_control))
0395442a
JB
1445 return RX_CONTINUE;
1446
a732fa70
JB
1447 if (!rx->sta)
1448 return RX_CONTINUE;
1449
67625910
JB
1450 if (unlikely(is_multicast_ether_addr(hdr->addr1))) {
1451 struct ieee80211_sub_if_data *sdata = rx->sdata;
1452 u16 sn = ieee80211_get_sn(hdr);
1453
1454 if (!ieee80211_is_data_present(hdr->frame_control))
1455 return RX_CONTINUE;
1456
1457 if (!ieee80211_vif_is_mld(&sdata->vif) ||
1458 sdata->vif.type != NL80211_IFTYPE_STATION)
1459 return RX_CONTINUE;
1460
1461 if (sdata->u.mgd.mcast_seq_last != IEEE80211_SN_MODULO &&
1462 ieee80211_sn_less_eq(sn, sdata->u.mgd.mcast_seq_last))
1463 return RX_DROP_U_DUP;
1464
1465 sdata->u.mgd.mcast_seq_last = sn;
1466 return RX_CONTINUE;
1467 }
1468
a732fa70
JB
1469 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1470 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1471 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
b320d6c4 1472 rx->link_sta->rx_stats.num_duplicates++;
dccc9aa7 1473 return RX_DROP_U_DUP;
a732fa70
JB
1474 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1475 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
571ecf67
JB
1476 }
1477
0395442a
JB
1478 return RX_CONTINUE;
1479}
1480
1481static ieee80211_rx_result debug_noinline
1482ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1483{
1484 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1485
571ecf67
JB
1486 /* Drop disallowed frame classes based on STA auth/assoc state;
1487 * IEEE 802.11, Chap 5.5.
1488 *
ccd7b362
JB
1489 * mac80211 filters only based on association state, i.e. it drops
1490 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
1491 * deauth/disassoc frames when needed. In addition, hostapd is
1492 * responsible for filtering on both auth and assoc states.
1493 */
33b64eb2 1494
902acc78 1495 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 1496 return ieee80211_rx_mesh_check(rx);
33b64eb2 1497
a7767f95
HH
1498 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1499 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 1500 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
239281f8 1501 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
c2c98fde 1502 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
7852e361
JB
1503 /*
1504 * accept port control frames from the AP even when it's not
1505 * yet marked ASSOC to prevent a race where we don't set the
1506 * assoc bit quickly enough before it sends the first frame
1507 */
1508 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2a33bee2 1509 ieee80211_is_data_present(hdr->frame_control)) {
6dbda2d0
JB
1510 unsigned int hdrlen;
1511 __be16 ethertype;
1512
1513 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1514
1515 if (rx->skb->len < hdrlen + 8)
286e6967 1516 return RX_DROP;
6dbda2d0
JB
1517
1518 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1519 if (ethertype == rx->sdata->control_port_protocol)
2a33bee2
GE
1520 return RX_CONTINUE;
1521 }
21fc7560
JB
1522
1523 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1524 cfg80211_rx_spurious_frame(rx->sdata->dev,
1525 hdr->addr2,
1526 GFP_ATOMIC))
dccc9aa7 1527 return RX_DROP_U_SPURIOUS;
21fc7560 1528
286e6967 1529 return RX_DROP;
2a33bee2 1530 }
571ecf67 1531
9ae54c84 1532 return RX_CONTINUE;
570bd537
JB
1533}
1534
1535
572e0012
KV
1536static ieee80211_rx_result debug_noinline
1537ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1538{
1539 struct ieee80211_local *local;
1540 struct ieee80211_hdr *hdr;
1541 struct sk_buff *skb;
1542
1543 local = rx->local;
1544 skb = rx->skb;
1545 hdr = (struct ieee80211_hdr *) skb->data;
1546
1547 if (!local->pspolling)
1548 return RX_CONTINUE;
1549
1550 if (!ieee80211_has_fromds(hdr->frame_control))
1551 /* this is not from AP */
1552 return RX_CONTINUE;
1553
1554 if (!ieee80211_is_data(hdr->frame_control))
1555 return RX_CONTINUE;
1556
1557 if (!ieee80211_has_moredata(hdr->frame_control)) {
1558 /* AP has no more frames buffered for us */
1559 local->pspolling = false;
1560 return RX_CONTINUE;
1561 }
1562
1563 /* more data bit is set, let's request a new frame from the AP */
1564 ieee80211_send_pspoll(local, rx->sdata);
1565
1566 return RX_CONTINUE;
1567}
1568
d012a605 1569static void sta_ps_start(struct sta_info *sta)
571ecf67 1570{
133b8226 1571 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1572 struct ieee80211_local *local = sdata->local;
d012a605 1573 struct ps_data *ps;
ba8c3d6f 1574 int tid;
0795af57 1575
d012a605
MP
1576 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1577 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1578 ps = &sdata->bss->ps;
1579 else
1580 return;
1581
1582 atomic_inc(&ps->num_sta_ps);
c2c98fde 1583 set_sta_flag(sta, WLAN_STA_PS_STA);
30686bf7 1584 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
d057e5a3 1585 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
bdcbd8e0
JB
1586 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1587 sta->sta.addr, sta->sta.aid);
ba8c3d6f 1588
17c18bf8
JB
1589 ieee80211_clear_fast_xmit(sta);
1590
adf8ed01 1591 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
b49c15e1 1592 struct ieee80211_txq *txq = sta->sta.txq[tid];
942741da 1593 struct txq_info *txqi = to_txq_info(txq);
b49c15e1 1594
942741da
FF
1595 spin_lock(&local->active_txq_lock[txq->ac]);
1596 if (!list_empty(&txqi->schedule_order))
1597 list_del_init(&txqi->schedule_order);
1598 spin_unlock(&local->active_txq_lock[txq->ac]);
b49c15e1
FF
1599
1600 if (txq_has_queue(txq))
ba8c3d6f
FF
1601 set_bit(tid, &sta->txq_buffered_tids);
1602 else
1603 clear_bit(tid, &sta->txq_buffered_tids);
1604 }
571ecf67
JB
1605}
1606
d012a605 1607static void sta_ps_end(struct sta_info *sta)
571ecf67 1608{
bdcbd8e0
JB
1609 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1610 sta->sta.addr, sta->sta.aid);
004c872e 1611
c2c98fde 1612 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
e3685e03
JB
1613 /*
1614 * Clear the flag only if the other one is still set
1615 * so that the TX path won't start TX'ing new frames
1616 * directly ... In the case that the driver flag isn't
1617 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1618 */
1619 clear_sta_flag(sta, WLAN_STA_PS_STA);
bdcbd8e0
JB
1620 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1621 sta->sta.addr, sta->sta.aid);
af818581
JB
1622 return;
1623 }
1624
5ac2e350
JB
1625 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1626 clear_sta_flag(sta, WLAN_STA_PS_STA);
af818581 1627 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1628}
1629
cf47161a 1630int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
d057e5a3 1631{
cf47161a 1632 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
d057e5a3
AN
1633 bool in_ps;
1634
cf47161a 1635 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
d057e5a3
AN
1636
1637 /* Don't let the same PS state be set twice */
cf47161a 1638 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
d057e5a3
AN
1639 if ((start && in_ps) || (!start && !in_ps))
1640 return -EINVAL;
1641
1642 if (start)
cf47161a 1643 sta_ps_start(sta);
d057e5a3 1644 else
cf47161a 1645 sta_ps_end(sta);
d057e5a3
AN
1646
1647 return 0;
1648}
1649EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1650
46fa38e8
JB
1651void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1652{
1653 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1654
1655 if (test_sta_flag(sta, WLAN_STA_SP))
1656 return;
1657
1658 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1659 ieee80211_sta_ps_deliver_poll_response(sta);
1660 else
1661 set_sta_flag(sta, WLAN_STA_PSPOLL);
1662}
1663EXPORT_SYMBOL(ieee80211_sta_pspoll);
1664
1665void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1666{
1667 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
731977e9 1668 int ac = ieee80211_ac_from_tid(tid);
46fa38e8
JB
1669
1670 /*
0aa419ec
EG
1671 * If this AC is not trigger-enabled do nothing unless the
1672 * driver is calling us after it already checked.
46fa38e8
JB
1673 *
1674 * NB: This could/should check a separate bitmap of trigger-
1675 * enabled queues, but for now we only implement uAPSD w/o
1676 * TSPEC changes to the ACs, so they're always the same.
1677 */
f438ceb8
EG
1678 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1679 tid != IEEE80211_NUM_TIDS)
46fa38e8
JB
1680 return;
1681
1682 /* if we are in a service period, do nothing */
1683 if (test_sta_flag(sta, WLAN_STA_SP))
1684 return;
1685
1686 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1687 ieee80211_sta_ps_deliver_uapsd(sta);
1688 else
1689 set_sta_flag(sta, WLAN_STA_UAPSD);
1690}
1691EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1692
47086fc5
JB
1693static ieee80211_rx_result debug_noinline
1694ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1695{
1696 struct ieee80211_sub_if_data *sdata = rx->sdata;
1697 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1698 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
47086fc5 1699
5c90067c 1700 if (!rx->sta)
47086fc5
JB
1701 return RX_CONTINUE;
1702
1703 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1704 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1705 return RX_CONTINUE;
1706
1707 /*
1708 * The device handles station powersave, so don't do anything about
1709 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1710 * it to mac80211 since they're handled.)
1711 */
30686bf7 1712 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
47086fc5
JB
1713 return RX_CONTINUE;
1714
1715 /*
1716 * Don't do anything if the station isn't already asleep. In
1717 * the uAPSD case, the station will probably be marked asleep,
1718 * in the PS-Poll case the station must be confused ...
1719 */
c2c98fde 1720 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
47086fc5
JB
1721 return RX_CONTINUE;
1722
1723 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
46fa38e8 1724 ieee80211_sta_pspoll(&rx->sta->sta);
47086fc5
JB
1725
1726 /* Free PS Poll skb here instead of returning RX_DROP that would
1727 * count as an dropped frame. */
1728 dev_kfree_skb(rx->skb);
1729
1730 return RX_QUEUED;
1731 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1732 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1733 ieee80211_has_pm(hdr->frame_control) &&
1734 (ieee80211_is_data_qos(hdr->frame_control) ||
1735 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
a1f2ba04 1736 u8 tid = ieee80211_get_tid(hdr);
47086fc5 1737
46fa38e8 1738 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
47086fc5
JB
1739 }
1740
1741 return RX_CONTINUE;
1742}
1743
49461622 1744static ieee80211_rx_result debug_noinline
5cf121c3 1745ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1746{
1747 struct sta_info *sta = rx->sta;
b320d6c4 1748 struct link_sta_info *link_sta = rx->link_sta;
eb9fb5b8
JB
1749 struct sk_buff *skb = rx->skb;
1750 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1751 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
ef0621e8 1752 int i;
571ecf67 1753
b320d6c4 1754 if (!sta || !link_sta)
9ae54c84 1755 return RX_CONTINUE;
571ecf67 1756
b291ba11
JB
1757 /*
1758 * Update last_rx only for IBSS packets which are for the current
e584da5e
AQ
1759 * BSSID and for station already AUTHORIZED to avoid keeping the
1760 * current IBSS network alive in cases where other STAs start
1761 * using different BSSID. This will also give the station another
1762 * chance to restart the authentication/authorization in case
1763 * something went wrong the first time.
b291ba11 1764 */
05c914fe 1765 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1766 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1767 NL80211_IFTYPE_ADHOC);
e584da5e
AQ
1768 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1769 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
b320d6c4 1770 link_sta->rx_stats.last_rx = jiffies;
0e966d9a 1771 if (ieee80211_is_data_present(hdr->frame_control) &&
4f6b1b3d 1772 !is_multicast_ether_addr(hdr->addr1))
b320d6c4 1773 link_sta->rx_stats.last_rate =
4f6b1b3d 1774 sta_stats_encode_rate(status);
3af6334c 1775 }
239281f8 1776 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
b320d6c4 1777 link_sta->rx_stats.last_rx = jiffies;
09a740ce 1778 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
f879ac8e 1779 !is_multicast_ether_addr(hdr->addr1)) {
b291ba11 1780 /*
33b64eb2
LCC
1781 * Mesh beacons will update last_rx when if they are found to
1782 * match the current local configuration when processed.
571ecf67 1783 */
b320d6c4 1784 link_sta->rx_stats.last_rx = jiffies;
0e966d9a 1785 if (ieee80211_is_data_present(hdr->frame_control))
b320d6c4 1786 link_sta->rx_stats.last_rate = sta_stats_encode_rate(status);
571ecf67
JB
1787 }
1788
b320d6c4 1789 link_sta->rx_stats.fragments++;
0f9c5a61 1790
b320d6c4
BB
1791 u64_stats_update_begin(&link_sta->rx_stats.syncp);
1792 link_sta->rx_stats.bytes += rx->skb->len;
1793 u64_stats_update_end(&link_sta->rx_stats.syncp);
0f9c5a61 1794
fe8431f8 1795 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
b320d6c4
BB
1796 link_sta->rx_stats.last_signal = status->signal;
1797 ewma_signal_add(&link_sta->rx_stats_avg.signal,
046d2e7c 1798 -status->signal);
fe8431f8 1799 }
571ecf67 1800
ef0621e8 1801 if (status->chains) {
b320d6c4 1802 link_sta->rx_stats.chains = status->chains;
ef0621e8
FF
1803 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1804 int signal = status->chain_signal[i];
1805
1806 if (!(status->chains & BIT(i)))
1807 continue;
1808
b320d6c4
BB
1809 link_sta->rx_stats.chain_signal_last[i] = signal;
1810 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
e5a9f8d0 1811 -signal);
ef0621e8
FF
1812 }
1813 }
1814
09a740ce
TP
1815 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1816 return RX_CONTINUE;
1817
72eaa43a
JB
1818 /*
1819 * Change STA power saving mode only at the end of a frame
9fef6544
EG
1820 * exchange sequence, and only for a data or management
1821 * frame as specified in IEEE 802.11-2016 11.2.3.2
72eaa43a 1822 */
30686bf7 1823 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
d057e5a3 1824 !ieee80211_has_morefrags(hdr->frame_control) &&
20932750 1825 !is_multicast_ether_addr(hdr->addr1) &&
9fef6544
EG
1826 (ieee80211_is_mgmt(hdr->frame_control) ||
1827 ieee80211_is_data(hdr->frame_control)) &&
4cfda47b 1828 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
05c914fe 1829 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
9fef6544 1830 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
c2c98fde 1831 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
b4ba544c 1832 if (!ieee80211_has_pm(hdr->frame_control))
d012a605 1833 sta_ps_end(sta);
72eaa43a
JB
1834 } else {
1835 if (ieee80211_has_pm(hdr->frame_control))
d012a605 1836 sta_ps_start(sta);
72eaa43a 1837 }
571ecf67
JB
1838 }
1839
3f52b7e3
MP
1840 /* mesh power save support */
1841 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1842 ieee80211_mps_rx_h_sta_process(sta, hdr);
1843
22403def
JB
1844 /*
1845 * Drop (qos-)data::nullfunc frames silently, since they
1846 * are used only to control station power saving mode.
1847 */
30b2f0be 1848 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
571ecf67 1849 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
d524215f
FF
1850
1851 /*
1852 * If we receive a 4-addr nullfunc frame from a STA
e7f4a940
JB
1853 * that was not moved to a 4-addr STA vlan yet send
1854 * the event to userspace and for older hostapd drop
1855 * the frame to the monitor interface.
d524215f
FF
1856 */
1857 if (ieee80211_has_a4(hdr->frame_control) &&
1858 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1859 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
e7f4a940
JB
1860 !rx->sdata->u.vlan.sta))) {
1861 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1862 cfg80211_rx_unexpected_4addr_frame(
1863 rx->sdata->dev, sta->sta.addr,
1864 GFP_ATOMIC);
286e6967 1865 return RX_DROP_U_UNEXPECTED_4ADDR_FRAME;
e7f4a940 1866 }
22403def
JB
1867 /*
1868 * Update counter and free packet here to avoid
1869 * counting this as a dropped packed.
1870 */
b320d6c4 1871 link_sta->rx_stats.packets++;
571ecf67 1872 dev_kfree_skb(rx->skb);
9ae54c84 1873 return RX_QUEUED;
571ecf67
JB
1874 }
1875
9ae54c84 1876 return RX_CONTINUE;
571ecf67
JB
1877} /* ieee80211_rx_h_sta_process */
1878
af2d14b0
JM
1879static struct ieee80211_key *
1880ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1881{
1882 struct ieee80211_key *key = NULL;
af2d14b0
JM
1883 int idx2;
1884
1885 /* Make sure key gets set if either BIGTK key index is set so that
1886 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1887 * Beacon frames and Beacon frames that claim to use another BIGTK key
1888 * index (i.e., a key that we do not have).
1889 */
1890
1891 if (idx < 0) {
1892 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1893 idx2 = idx + 1;
1894 } else {
1895 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1896 idx2 = idx + 1;
1897 else
1898 idx2 = idx - 1;
1899 }
1900
ccdde7c7
JB
1901 if (rx->link_sta)
1902 key = rcu_dereference(rx->link_sta->gtk[idx]);
af2d14b0 1903 if (!key)
ccdde7c7
JB
1904 key = rcu_dereference(rx->link->gtk[idx]);
1905 if (!key && rx->link_sta)
1906 key = rcu_dereference(rx->link_sta->gtk[idx2]);
af2d14b0 1907 if (!key)
ccdde7c7 1908 key = rcu_dereference(rx->link->gtk[idx2]);
af2d14b0
JM
1909
1910 return key;
1911}
1912
86c228a7
JA
1913static ieee80211_rx_result debug_noinline
1914ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1915{
1916 struct sk_buff *skb = rx->skb;
1917 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1918 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1919 int keyidx;
dccc9aa7 1920 ieee80211_rx_result result = RX_DROP_U_DECRYPT_FAIL;
86c228a7 1921 struct ieee80211_key *sta_ptk = NULL;
96fc6efb 1922 struct ieee80211_key *ptk_idx = NULL;
86c228a7
JA
1923 int mmie_keyidx = -1;
1924 __le16 fc;
1925
09a740ce
TP
1926 if (ieee80211_is_ext(hdr->frame_control))
1927 return RX_CONTINUE;
1928
86c228a7
JA
1929 /*
1930 * Key selection 101
1931 *
af2d14b0 1932 * There are five types of keys:
86c228a7
JA
1933 * - GTK (group keys)
1934 * - IGTK (group keys for management frames)
af2d14b0 1935 * - BIGTK (group keys for Beacon frames)
86c228a7
JA
1936 * - PTK (pairwise keys)
1937 * - STK (station-to-station pairwise keys)
1938 *
1939 * When selecting a key, we have to distinguish between multicast
1940 * (including broadcast) and unicast frames, the latter can only
af2d14b0
JM
1941 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1942 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1943 * then unicast frames can also use key indices like GTKs. Hence, if we
86c228a7
JA
1944 * don't have a PTK/STK we check the key index for a WEP key.
1945 *
1946 * Note that in a regular BSS, multicast frames are sent by the
1947 * AP only, associated stations unicast the frame to the AP first
1948 * which then multicasts it on their behalf.
1949 *
1950 * There is also a slight problem in IBSS mode: GTKs are negotiated
1951 * with each station, that is something we don't currently handle.
1952 * The spec seems to expect that one negotiates the same key with
1953 * every station but there's no such requirement; VLANs could be
1954 * possible.
1955 */
1956
86c228a7
JA
1957 /* start without a key */
1958 rx->key = NULL;
2475b1cc 1959 fc = hdr->frame_control;
86c228a7 1960
2475b1cc
MS
1961 if (rx->sta) {
1962 int keyid = rx->sta->ptk_idx;
96fc6efb 1963 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
86c228a7 1964
77dfc2bc
XS
1965 if (ieee80211_has_protected(fc) &&
1966 !(status->flag & RX_FLAG_IV_STRIPPED)) {
23a5f0af 1967 keyid = ieee80211_get_keyid(rx->skb);
96fc6efb 1968
2475b1cc 1969 if (unlikely(keyid < 0))
dccc9aa7 1970 return RX_DROP_U_NO_KEY_ID;
96fc6efb
AW
1971
1972 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
2475b1cc 1973 }
2475b1cc 1974 }
86c228a7
JA
1975
1976 if (!ieee80211_has_protected(fc))
1977 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1978
1979 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
96fc6efb 1980 rx->key = ptk_idx ? ptk_idx : sta_ptk;
86c228a7
JA
1981 if ((status->flag & RX_FLAG_DECRYPTED) &&
1982 (status->flag & RX_FLAG_IV_STRIPPED))
1983 return RX_CONTINUE;
1984 /* Skip decryption if the frame is not protected. */
1985 if (!ieee80211_has_protected(fc))
1986 return RX_CONTINUE;
af2d14b0
JM
1987 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1988 /* Broadcast/multicast robust management frame / BIP */
1989 if ((status->flag & RX_FLAG_DECRYPTED) &&
1990 (status->flag & RX_FLAG_IV_STRIPPED))
1991 return RX_CONTINUE;
1992
1993 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1994 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
b2d03cab
JB
1995 NUM_DEFAULT_BEACON_KEYS) {
1996 if (rx->sdata->dev)
1997 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1998 skb->data,
1999 skb->len);
286e6967 2000 return RX_DROP_U_BAD_BCN_KEYIDX;
9eaf183a 2001 }
af2d14b0
JM
2002
2003 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
2004 if (!rx->key)
2005 return RX_CONTINUE; /* Beacon protection not in use */
86c228a7
JA
2006 } else if (mmie_keyidx >= 0) {
2007 /* Broadcast/multicast robust management frame / BIP */
2008 if ((status->flag & RX_FLAG_DECRYPTED) &&
2009 (status->flag & RX_FLAG_IV_STRIPPED))
2010 return RX_CONTINUE;
2011
2012 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
2013 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
286e6967 2014 return RX_DROP_U_BAD_MGMT_KEYIDX; /* unexpected BIP keyidx */
ccdde7c7 2015 if (rx->link_sta) {
46f6b060
MH
2016 if (ieee80211_is_group_privacy_action(skb) &&
2017 test_sta_flag(rx->sta, WLAN_STA_MFP))
286e6967 2018 return RX_DROP;
46f6b060 2019
ccdde7c7 2020 rx->key = rcu_dereference(rx->link_sta->gtk[mmie_keyidx]);
46f6b060 2021 }
86c228a7 2022 if (!rx->key)
ccdde7c7 2023 rx->key = rcu_dereference(rx->link->gtk[mmie_keyidx]);
86c228a7
JA
2024 } else if (!ieee80211_has_protected(fc)) {
2025 /*
2026 * The frame was not protected, so skip decryption. However, we
2027 * need to set rx->key if there is a key that could have been
2028 * used so that the frame may be dropped if encryption would
2029 * have been expected.
2030 */
2031 struct ieee80211_key *key = NULL;
86c228a7
JA
2032 int i;
2033
af2d14b0
JM
2034 if (ieee80211_is_beacon(fc)) {
2035 key = ieee80211_rx_get_bigtk(rx, -1);
2036 } else if (ieee80211_is_mgmt(fc) &&
2037 is_multicast_ether_addr(hdr->addr1)) {
ccdde7c7 2038 key = rcu_dereference(rx->link->default_mgmt_key);
af2d14b0 2039 } else {
ccdde7c7 2040 if (rx->link_sta) {
86c228a7 2041 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
ccdde7c7 2042 key = rcu_dereference(rx->link_sta->gtk[i]);
86c228a7
JA
2043 if (key)
2044 break;
2045 }
2046 }
2047 if (!key) {
2048 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
ccdde7c7 2049 key = rcu_dereference(rx->link->gtk[i]);
86c228a7
JA
2050 if (key)
2051 break;
2052 }
2053 }
86c228a7 2054 }
af2d14b0
JM
2055 if (key)
2056 rx->key = key;
86c228a7
JA
2057 return RX_CONTINUE;
2058 } else {
86c228a7
JA
2059 /*
2060 * The device doesn't give us the IV so we won't be
2061 * able to look up the key. That's ok though, we
2062 * don't need to decrypt the frame, we just won't
2063 * be able to keep statistics accurate.
2064 * Except for key threshold notifications, should
2065 * we somehow allow the driver to tell us which key
2066 * the hardware used if this flag is set?
2067 */
2068 if ((status->flag & RX_FLAG_DECRYPTED) &&
2069 (status->flag & RX_FLAG_IV_STRIPPED))
2070 return RX_CONTINUE;
2071
23a5f0af 2072 keyidx = ieee80211_get_keyid(rx->skb);
86c228a7 2073
96fc6efb 2074 if (unlikely(keyidx < 0))
dccc9aa7 2075 return RX_DROP_U_NO_KEY_ID;
86c228a7
JA
2076
2077 /* check per-station GTK first, if multicast packet */
ccdde7c7
JB
2078 if (is_multicast_ether_addr(hdr->addr1) && rx->link_sta)
2079 rx->key = rcu_dereference(rx->link_sta->gtk[keyidx]);
86c228a7
JA
2080
2081 /* if not found, try default key */
2082 if (!rx->key) {
bfd8403a 2083 if (is_multicast_ether_addr(hdr->addr1))
ccdde7c7 2084 rx->key = rcu_dereference(rx->link->gtk[keyidx]);
bfd8403a
JB
2085 if (!rx->key)
2086 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
86c228a7
JA
2087
2088 /*
2089 * RSNA-protected unicast frames should always be
2090 * sent with pairwise or station-to-station keys,
2091 * but for WEP we allow using a key index as well.
2092 */
2093 if (rx->key &&
2094 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2095 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2096 !is_multicast_ether_addr(hdr->addr1))
2097 rx->key = NULL;
2098 }
2099 }
2100
2101 if (rx->key) {
2102 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
286e6967 2103 return RX_DROP;
86c228a7 2104
86c228a7
JA
2105 /* TODO: add threshold stuff again */
2106 } else {
286e6967 2107 return RX_DROP;
86c228a7
JA
2108 }
2109
2110 switch (rx->key->conf.cipher) {
2111 case WLAN_CIPHER_SUITE_WEP40:
2112 case WLAN_CIPHER_SUITE_WEP104:
2113 result = ieee80211_crypto_wep_decrypt(rx);
2114 break;
2115 case WLAN_CIPHER_SUITE_TKIP:
2116 result = ieee80211_crypto_tkip_decrypt(rx);
2117 break;
2118 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db
JM
2119 result = ieee80211_crypto_ccmp_decrypt(
2120 rx, IEEE80211_CCMP_MIC_LEN);
2121 break;
2122 case WLAN_CIPHER_SUITE_CCMP_256:
2123 result = ieee80211_crypto_ccmp_decrypt(
2124 rx, IEEE80211_CCMP_256_MIC_LEN);
86c228a7
JA
2125 break;
2126 case WLAN_CIPHER_SUITE_AES_CMAC:
2127 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2128 break;
56c52da2
JM
2129 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2130 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2131 break;
8ade538b
JM
2132 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2133 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2134 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2135 break;
00b9cfa3
JM
2136 case WLAN_CIPHER_SUITE_GCMP:
2137 case WLAN_CIPHER_SUITE_GCMP_256:
2138 result = ieee80211_crypto_gcmp_decrypt(rx);
2139 break;
86c228a7 2140 default:
dccc9aa7 2141 result = RX_DROP_U_BAD_CIPHER;
86c228a7
JA
2142 }
2143
2144 /* the hdr variable is invalid after the decrypt handlers */
2145
2146 /* either the frame has been decrypted or will be dropped */
2147 status->flag |= RX_FLAG_DECRYPTED;
2148
58305854 2149 if (unlikely(ieee80211_is_beacon(fc) && RX_RES_IS_UNUSABLE(result) &&
b2d03cab 2150 rx->sdata->dev))
9eaf183a
JM
2151 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2152 skb->data, skb->len);
2153
86c228a7
JA
2154 return result;
2155}
2156
3a11ce08
JB
2157void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2158{
2159 int i;
2160
2161 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2162 skb_queue_head_init(&cache->entries[i].skb_list);
2163}
2164
2165void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2166{
2167 int i;
2168
2169 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2170 __skb_queue_purge(&cache->entries[i].skb_list);
2171}
2172
571ecf67 2173static inline struct ieee80211_fragment_entry *
3a11ce08 2174ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
571ecf67
JB
2175 unsigned int frag, unsigned int seq, int rx_queue,
2176 struct sk_buff **skb)
2177{
2178 struct ieee80211_fragment_entry *entry;
571ecf67 2179
3a11ce08
JB
2180 entry = &cache->entries[cache->next++];
2181 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2182 cache->next = 0;
571ecf67 2183
3a11ce08 2184 __skb_queue_purge(&entry->skb_list);
571ecf67
JB
2185
2186 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2187 *skb = NULL;
2188 entry->first_frag_time = jiffies;
2189 entry->seq = seq;
2190 entry->rx_queue = rx_queue;
2191 entry->last_frag = frag;
9acc54be 2192 entry->check_sequential_pn = false;
571ecf67
JB
2193 entry->extra_len = 0;
2194
2195 return entry;
2196}
2197
2198static inline struct ieee80211_fragment_entry *
3a11ce08 2199ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
b73d70ad 2200 unsigned int frag, unsigned int seq,
571ecf67
JB
2201 int rx_queue, struct ieee80211_hdr *hdr)
2202{
2203 struct ieee80211_fragment_entry *entry;
2204 int i, idx;
2205
3a11ce08 2206 idx = cache->next;
571ecf67
JB
2207 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2208 struct ieee80211_hdr *f_hdr;
7957a9de 2209 struct sk_buff *f_skb;
571ecf67
JB
2210
2211 idx--;
2212 if (idx < 0)
2213 idx = IEEE80211_FRAGMENT_MAX - 1;
2214
3a11ce08 2215 entry = &cache->entries[idx];
571ecf67
JB
2216 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2217 entry->rx_queue != rx_queue ||
2218 entry->last_frag + 1 != frag)
2219 continue;
2220
7957a9de
DM
2221 f_skb = __skb_peek(&entry->skb_list);
2222 f_hdr = (struct ieee80211_hdr *) f_skb->data;
571ecf67 2223
b73d70ad
HH
2224 /*
2225 * Check ftype and addresses are equal, else check next fragment
2226 */
2227 if (((hdr->frame_control ^ f_hdr->frame_control) &
2228 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
b203ca39
JP
2229 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2230 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
571ecf67
JB
2231 continue;
2232
ab46623e 2233 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
2234 __skb_queue_purge(&entry->skb_list);
2235 continue;
2236 }
2237 return entry;
2238 }
2239
2240 return NULL;
2241}
2242
965a7d72
MV
2243static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2244{
2245 return rx->key &&
2246 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2247 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2248 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2249 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2250 ieee80211_has_protected(fc);
2251}
2252
49461622 2253static ieee80211_rx_result debug_noinline
5cf121c3 2254ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67 2255{
3a11ce08 2256 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
571ecf67
JB
2257 struct ieee80211_hdr *hdr;
2258 u16 sc;
358c8d9d 2259 __le16 fc;
571ecf67
JB
2260 unsigned int frag, seq;
2261 struct ieee80211_fragment_entry *entry;
2262 struct sk_buff *skb;
3edc6b0d 2263 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67 2264
b73d70ad 2265 hdr = (struct ieee80211_hdr *)rx->skb->data;
358c8d9d 2266 fc = hdr->frame_control;
f7fbf70e 2267
09a740ce 2268 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
f7fbf70e
JC
2269 return RX_CONTINUE;
2270
571ecf67
JB
2271 sc = le16_to_cpu(hdr->seq_ctrl);
2272 frag = sc & IEEE80211_SCTL_FRAG;
2273
3a11ce08
JB
2274 if (rx->sta)
2275 cache = &rx->sta->frags;
2276
d025933e
AM
2277 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2278 goto out;
2279
a9799541 2280 if (is_multicast_ether_addr(hdr->addr1))
286e6967 2281 return RX_DROP;
a9799541 2282
571ecf67
JB
2283 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2284
e3cf8b3f 2285 if (skb_linearize(rx->skb))
dccc9aa7 2286 return RX_DROP_U_OOM;
e3cf8b3f 2287
058897a4
AK
2288 /*
2289 * skb_linearize() might change the skb->data and
2290 * previously cached variables (in this case, hdr) need to
2291 * be refreshed with the new data.
2292 */
2293 hdr = (struct ieee80211_hdr *)rx->skb->data;
571ecf67
JB
2294 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2295
2296 if (frag == 0) {
2297 /* This is the first fragment of a new frame. */
3a11ce08 2298 entry = ieee80211_reassemble_add(cache, frag, seq,
9e26297a 2299 rx->seqno_idx, &(rx->skb));
965a7d72 2300 if (requires_sequential_pn(rx, fc)) {
9e26297a 2301 int queue = rx->security_idx;
9acc54be
JB
2302
2303 /* Store CCMP/GCMP PN so that we can verify that the
2304 * next fragment has a sequential PN value.
2305 */
2306 entry->check_sequential_pn = true;
7e44a0b5 2307 entry->is_protected = true;
94034c40 2308 entry->key_color = rx->key->color;
571ecf67 2309 memcpy(entry->last_pn,
9190252c 2310 rx->key->u.ccmp.rx_pn[queue],
4325f6ca 2311 IEEE80211_CCMP_PN_LEN);
9acc54be
JB
2312 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2313 u.ccmp.rx_pn) !=
2314 offsetof(struct ieee80211_key,
2315 u.gcmp.rx_pn));
2316 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2317 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2318 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2319 IEEE80211_GCMP_PN_LEN);
3edc6b0d
WG
2320 } else if (rx->key &&
2321 (ieee80211_has_protected(fc) ||
2322 (status->flag & RX_FLAG_DECRYPTED))) {
7e44a0b5
JB
2323 entry->is_protected = true;
2324 entry->key_color = rx->key->color;
571ecf67 2325 }
9ae54c84 2326 return RX_QUEUED;
571ecf67
JB
2327 }
2328
2329 /* This is a fragment for a frame that should already be pending in
2330 * fragment cache. Add this fragment to the end of the pending entry.
2331 */
3a11ce08 2332 entry = ieee80211_reassemble_find(cache, frag, seq,
9e26297a 2333 rx->seqno_idx, hdr);
571ecf67
JB
2334 if (!entry) {
2335 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
286e6967 2336 return RX_DROP;
571ecf67
JB
2337 }
2338
9acc54be
JB
2339 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2340 * MPDU PN values are not incrementing in steps of 1."
2341 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2342 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2343 */
2344 if (entry->check_sequential_pn) {
571ecf67 2345 int i;
4325f6ca 2346 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
9acc54be 2347
965a7d72 2348 if (!requires_sequential_pn(rx, fc))
dccc9aa7 2349 return RX_DROP_U_NONSEQ_PN;
94034c40
MV
2350
2351 /* Prevent mixed key and fragment cache attacks */
2352 if (entry->key_color != rx->key->color)
dccc9aa7 2353 return RX_DROP_U_BAD_KEY_COLOR;
94034c40 2354
4325f6ca
JB
2355 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2356 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
571ecf67
JB
2357 pn[i]++;
2358 if (pn[i])
2359 break;
2360 }
bf30ca92
JB
2361
2362 rpn = rx->ccm_gcm.pn;
4325f6ca 2363 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
dccc9aa7 2364 return RX_DROP_U_REPLAY;
4325f6ca 2365 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
7e44a0b5 2366 } else if (entry->is_protected &&
3edc6b0d
WG
2367 (!rx->key ||
2368 (!ieee80211_has_protected(fc) &&
2369 !(status->flag & RX_FLAG_DECRYPTED)) ||
7e44a0b5
JB
2370 rx->key->color != entry->key_color)) {
2371 /* Drop this as a mixed key or fragment cache attack, even
2372 * if for TKIP Michael MIC should protect us, and WEP is a
2373 * lost cause anyway.
2374 */
dccc9aa7 2375 return RX_DROP_U_EXPECT_DEFRAG_PROT;
3edc6b0d
WG
2376 } else if (entry->is_protected && rx->key &&
2377 entry->key_color != rx->key->color &&
2378 (status->flag & RX_FLAG_DECRYPTED)) {
dccc9aa7 2379 return RX_DROP_U_BAD_KEY_COLOR;
571ecf67
JB
2380 }
2381
358c8d9d 2382 skb_pull(rx->skb, ieee80211_hdrlen(fc));
571ecf67
JB
2383 __skb_queue_tail(&entry->skb_list, rx->skb);
2384 entry->last_frag = frag;
2385 entry->extra_len += rx->skb->len;
358c8d9d 2386 if (ieee80211_has_morefrags(fc)) {
571ecf67 2387 rx->skb = NULL;
9ae54c84 2388 return RX_QUEUED;
571ecf67
JB
2389 }
2390
2391 rx->skb = __skb_dequeue(&entry->skb_list);
2392 if (skb_tailroom(rx->skb) < entry->extra_len) {
f1160434 2393 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
571ecf67
JB
2394 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2395 GFP_ATOMIC))) {
2396 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2397 __skb_queue_purge(&entry->skb_list);
dccc9aa7 2398 return RX_DROP_U_OOM;
571ecf67
JB
2399 }
2400 }
2401 while ((skb = __skb_dequeue(&entry->skb_list))) {
59ae1d12 2402 skb_put_data(rx->skb, skb->data, skb->len);
571ecf67
JB
2403 dev_kfree_skb(skb);
2404 }
2405
571ecf67 2406 out:
d025933e 2407 ieee80211_led_rx(rx->local);
571ecf67 2408 if (rx->sta)
b320d6c4 2409 rx->link_sta->rx_stats.packets++;
9ae54c84 2410 return RX_CONTINUE;
571ecf67
JB
2411}
2412
1df332e8 2413static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 2414{
1df332e8 2415 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
76ee65bf 2416 return -EACCES;
571ecf67 2417
76ee65bf 2418 return 0;
571ecf67
JB
2419}
2420
1df332e8 2421static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
571ecf67 2422{
eb9fb5b8
JB
2423 struct sk_buff *skb = rx->skb;
2424 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2425
3017b80b 2426 /*
7848ba7d
JB
2427 * Pass through unencrypted frames if the hardware has
2428 * decrypted them already.
3017b80b 2429 */
eb9fb5b8 2430 if (status->flag & RX_FLAG_DECRYPTED)
76ee65bf 2431 return 0;
571ecf67
JB
2432
2433 /* Drop unencrypted frames if key is set. */
358c8d9d 2434 if (unlikely(!ieee80211_has_protected(fc) &&
30b2f0be 2435 !ieee80211_is_any_nullfunc(fc) &&
e8f4fb7c 2436 ieee80211_is_data(fc) && rx->key))
76ee65bf 2437 return -EACCES;
bef5d1c7
JB
2438
2439 return 0;
2440}
2441
0738e55c 2442VISIBLE_IF_MAC80211_KUNIT ieee80211_rx_result
c419d884 2443ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
bef5d1c7 2444{
e3efca0a 2445 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
76a3059c
JB
2446 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2447 __le16 fc = mgmt->frame_control;
bef5d1c7 2448
e3efca0a
JM
2449 /*
2450 * Pass through unencrypted frames if the hardware has
2451 * decrypted them already.
2452 */
2453 if (status->flag & RX_FLAG_DECRYPTED)
6c02fab7 2454 return RX_CONTINUE;
bef5d1c7 2455
76a3059c
JB
2456 /* drop unicast protected dual (that wasn't protected) */
2457 if (ieee80211_is_action(fc) &&
2458 mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
6c02fab7 2459 return RX_DROP_U_UNPROT_DUAL;
76a3059c 2460
c2c98fde 2461 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
d211e90e 2462 if (unlikely(!ieee80211_has_protected(fc) &&
7339e0f2 2463 ieee80211_is_unicast_robust_mgmt_frame(rx->skb))) {
6ff57cf8 2464 if (ieee80211_is_deauth(fc) ||
7339e0f2
AG
2465 ieee80211_is_disassoc(fc)) {
2466 /*
2467 * Permit unprotected deauth/disassoc frames
2468 * during 4-way-HS (key is installed after HS).
2469 */
2470 if (!rx->key)
6c02fab7 2471 return RX_CONTINUE;
7339e0f2 2472
6ff57cf8
JB
2473 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2474 rx->skb->data,
2475 rx->skb->len);
7339e0f2 2476 }
6c02fab7 2477 return RX_DROP_U_UNPROT_UCAST_MGMT;
cf4e594e 2478 }
f2ca3ea4 2479 /* BIP does not use Protected field, so need to check MMIE */
f64f9e71 2480 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 2481 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
6ff57cf8
JB
2482 if (ieee80211_is_deauth(fc) ||
2483 ieee80211_is_disassoc(fc))
2484 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2485 rx->skb->data,
2486 rx->skb->len);
6c02fab7 2487 return RX_DROP_U_UNPROT_MCAST_MGMT;
cf4e594e 2488 }
af2d14b0 2489 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
9eaf183a
JM
2490 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2491 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2492 rx->skb->data,
2493 rx->skb->len);
6c02fab7 2494 return RX_DROP_U_UNPROT_BEACON;
9eaf183a 2495 }
f2ca3ea4
JM
2496 /*
2497 * When using MFP, Action frames are not allowed prior to
2498 * having configured keys.
2499 */
2500 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
d8ca16db 2501 ieee80211_is_robust_mgmt_frame(rx->skb)))
6c02fab7 2502 return RX_DROP_U_UNPROT_ACTION;
76a3059c
JB
2503
2504 /* drop unicast public action frames when using MPF */
2505 if (is_unicast_ether_addr(mgmt->da) &&
91535613 2506 ieee80211_is_protected_dual_of_public_action(rx->skb))
6c02fab7 2507 return RX_DROP_U_UNPROT_UNICAST_PUB_ACTION;
f2ca3ea4 2508 }
b3fc9c6c 2509
f3bd5932
JB
2510 /*
2511 * Drop robust action frames before assoc regardless of MFP state,
2512 * after assoc we also have decided on MFP or not.
2513 */
2514 if (ieee80211_is_action(fc) &&
2515 ieee80211_is_robust_mgmt_frame(rx->skb) &&
2516 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))
2517 return RX_DROP_U_UNPROT_ROBUST_ACTION;
2518
c419d884 2519 return RX_CONTINUE;
571ecf67 2520}
0738e55c 2521EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_drop_unencrypted_mgmt);
571ecf67 2522
2a1c5c7d 2523static ieee80211_rx_result
4114fa21 2524__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
571ecf67 2525{
eb9fb5b8 2526 struct ieee80211_sub_if_data *sdata = rx->sdata;
f14543ee 2527 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
fbb327c5
FF
2528 bool check_port_control = false;
2529 struct ethhdr *ehdr;
2530 int ret;
f14543ee 2531
4114fa21 2532 *port_control = false;
9bc383de
JB
2533 if (ieee80211_has_a4(hdr->frame_control) &&
2534 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2a1c5c7d 2535 return RX_DROP_U_UNEXPECTED_VLAN_4ADDR;
9bc383de 2536
fbb327c5
FF
2537 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2538 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
fbb327c5 2539 if (!sdata->u.mgd.use_4addr)
2a1c5c7d 2540 return RX_DROP_U_UNEXPECTED_STA_4ADDR;
1ecef20c 2541 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
fbb327c5
FF
2542 check_port_control = true;
2543 }
2544
9bc383de 2545 if (is_multicast_ether_addr(hdr->addr1) &&
fbb327c5 2546 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2a1c5c7d 2547 return RX_DROP_U_UNEXPECTED_VLAN_MCAST;
571ecf67 2548
fbb327c5 2549 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
4114fa21 2550 if (ret < 0)
2a1c5c7d 2551 return RX_DROP_U_INVALID_8023;
fbb327c5
FF
2552
2553 ehdr = (struct ethhdr *) rx->skb->data;
4114fa21
FF
2554 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2555 *port_control = true;
2556 else if (check_port_control)
2a1c5c7d 2557 return RX_DROP_U_NOT_PORT_CONTROL;
fbb327c5 2558
2a1c5c7d 2559 return RX_CONTINUE;
76ee65bf 2560}
571ecf67 2561
0d5891e3
AO
2562bool ieee80211_is_our_addr(struct ieee80211_sub_if_data *sdata,
2563 const u8 *addr, int *out_link_id)
d06faef1
AO
2564{
2565 unsigned int link_id;
2566
2567 /* non-MLO, or MLD address replaced by hardware */
2568 if (ether_addr_equal(sdata->vif.addr, addr))
2569 return true;
2570
f1871abd 2571 if (!ieee80211_vif_is_mld(&sdata->vif))
d06faef1
AO
2572 return false;
2573
2574 for (link_id = 0; link_id < ARRAY_SIZE(sdata->vif.link_conf); link_id++) {
2575 struct ieee80211_bss_conf *conf;
2576
2577 conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2578
2579 if (!conf)
2580 continue;
2581 if (ether_addr_equal(conf->addr, addr)) {
2582 if (out_link_id)
2583 *out_link_id = link_id;
2584 return true;
2585 }
2586 }
2587
2588 return false;
2589}
2590
ce3edf6d
JB
2591/*
2592 * requires that rx->skb is a frame with ethernet header
2593 */
358c8d9d 2594static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
ce3edf6d 2595{
c97c23e3 2596 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
ce3edf6d
JB
2597 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2598 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2599
2600 /*
a8c4d76a
JB
2601 * Allow EAPOL frames to us/the PAE group address regardless of
2602 * whether the frame was encrypted or not, and always disallow
2603 * all other destination addresses for them.
ce3edf6d 2604 */
a8c4d76a 2605 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
d06faef1 2606 return ieee80211_is_our_addr(rx->sdata, ehdr->h_dest, NULL) ||
a8c4d76a 2607 ether_addr_equal(ehdr->h_dest, pae_group_addr);
ce3edf6d
JB
2608
2609 if (ieee80211_802_1x_port_control(rx) ||
358c8d9d 2610 ieee80211_drop_unencrypted(rx, fc))
ce3edf6d
JB
2611 return false;
2612
2613 return true;
2614}
2615
018f6fbf
DK
2616static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2617 struct ieee80211_rx_data *rx)
2618{
2619 struct ieee80211_sub_if_data *sdata = rx->sdata;
2620 struct net_device *dev = sdata->dev;
2621
2622 if (unlikely((skb->protocol == sdata->control_port_protocol ||
7f3f96ce
MT
2623 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2624 !sdata->control_port_no_preauth)) &&
018f6fbf
DK
2625 sdata->control_port_over_nl80211)) {
2626 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
f8b43c5c 2627 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
018f6fbf 2628
4c532321 2629 cfg80211_rx_control_port(dev, skb, noencrypt, rx->link_id);
018f6fbf
DK
2630 dev_kfree_skb(skb);
2631 } else {
a8c4d76a
JB
2632 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2633
c8a41c6a
DK
2634 memset(skb->cb, 0, sizeof(skb->cb));
2635
a8c4d76a
JB
2636 /*
2637 * 802.1X over 802.11 requires that the authenticator address
2638 * be used for EAPOL frames. However, 802.1X allows the use of
2639 * the PAE group address instead. If the interface is part of
2640 * a bridge and we pass the frame with the PAE group address,
2641 * then the bridge will forward it to the network (even if the
2642 * client was not associated yet), which isn't supposed to
2643 * happen.
2644 * To avoid that, rewrite the destination address to our own
2645 * address, so that the authenticator (e.g. hostapd) will see
2646 * the frame, but bridge won't forward it anywhere else. Note
2647 * that due to earlier filtering, the only other address can
610d086d
DW
2648 * be the PAE group address, unless the hardware allowed them
2649 * through in 802.3 offloaded mode.
a8c4d76a
JB
2650 */
2651 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2652 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2653 ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2654
018f6fbf 2655 /* deliver to local stack */
c5d1686b
FF
2656 if (rx->list)
2657 list_add_tail(&skb->list, rx->list);
018f6fbf
DK
2658 else
2659 netif_receive_skb(skb);
2660 }
2661}
2662
ce3edf6d
JB
2663/*
2664 * requires that rx->skb is a frame with ethernet header
2665 */
76ee65bf 2666static void
5cf121c3 2667ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf 2668{
eb9fb5b8
JB
2669 struct ieee80211_sub_if_data *sdata = rx->sdata;
2670 struct net_device *dev = sdata->dev;
76ee65bf 2671 struct sk_buff *skb, *xmit_skb;
ce3edf6d
JB
2672 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2673 struct sta_info *dsta;
571ecf67 2674
76ee65bf
RR
2675 skb = rx->skb;
2676 xmit_skb = NULL;
571ecf67 2677
36ec144f 2678 dev_sw_netstats_rx_add(dev, skb->len);
5a490510 2679
de8f18d3
JB
2680 if (rx->sta) {
2681 /* The seqno index has the same property as needed
2682 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2683 * for non-QoS-data frames. Here we know it's a data
2684 * frame, so count MSDUs.
2685 */
b320d6c4
BB
2686 u64_stats_update_begin(&rx->link_sta->rx_stats.syncp);
2687 rx->link_sta->rx_stats.msdu[rx->seqno_idx]++;
2688 u64_stats_update_end(&rx->link_sta->rx_stats.syncp);
de8f18d3
JB
2689 }
2690
05c914fe
JB
2691 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2692 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
213cd118 2693 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
a8c4d76a 2694 ehdr->h_proto != rx->sdata->control_port_protocol &&
9bc383de 2695 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
72f15d53
MB
2696 if (is_multicast_ether_addr(ehdr->h_dest) &&
2697 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
ce3edf6d
JB
2698 /*
2699 * send multicast frames both to higher layers in
2700 * local net stack and back to the wireless medium
2701 */
76ee65bf 2702 xmit_skb = skb_copy(skb, GFP_ATOMIC);
e87cc472 2703 if (!xmit_skb)
bdcbd8e0 2704 net_info_ratelimited("%s: failed to clone multicast frame\n",
e87cc472 2705 dev->name);
42dca5ef
JB
2706 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2707 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2708 dsta = sta_info_get(sdata, ehdr->h_dest);
abe60632 2709 if (dsta) {
ce3edf6d
JB
2710 /*
2711 * The destination station is associated to
2712 * this AP (in this VLAN), so send the frame
2713 * directly to it and do not pass it to local
2714 * net stack.
571ecf67 2715 */
76ee65bf 2716 xmit_skb = skb;
571ecf67
JB
2717 skb = NULL;
2718 }
571ecf67
JB
2719 }
2720 }
2721
59d9cb07 2722#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
9e890a1f
JB
2723 if (skb) {
2724 /* 'align' will only take the values 0 or 2 here since all
2725 * frames are required to be aligned to 2-byte boundaries
2726 * when being passed to mac80211; the code here works just
2727 * as well if that isn't true, but mac80211 assumes it can
2728 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
d1c3a37c 2729 */
9e890a1f
JB
2730 int align;
2731
2732 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
d1c3a37c
JB
2733 if (align) {
2734 if (WARN_ON(skb_headroom(skb) < 3)) {
2735 dev_kfree_skb(skb);
2736 skb = NULL;
2737 } else {
2738 u8 *data = skb->data;
8ce0b589
ZY
2739 size_t len = skb_headlen(skb);
2740 skb->data -= align;
2741 memmove(skb->data, data, len);
2742 skb_set_tail_pointer(skb, len);
d1c3a37c
JB
2743 }
2744 }
9e890a1f 2745 }
d1c3a37c
JB
2746#endif
2747
9e890a1f 2748 if (skb) {
9e890a1f 2749 skb->protocol = eth_type_trans(skb, dev);
018f6fbf 2750 ieee80211_deliver_skb_to_local_stack(skb, rx);
571ecf67
JB
2751 }
2752
76ee65bf 2753 if (xmit_skb) {
aef6c928
HS
2754 /*
2755 * Send to wireless media and increase priority by 256 to
2756 * keep the received priority instead of reclassifying
2757 * the frame (see cfg80211_classify8021d).
2758 */
2759 xmit_skb->priority += 256;
f831e909 2760 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
2761 skb_reset_network_header(xmit_skb);
2762 skb_reset_mac_header(xmit_skb);
76ee65bf 2763 dev_queue_xmit(xmit_skb);
571ecf67 2764 }
76ee65bf
RR
2765}
2766
8b0f5cb6
FF
2767#ifdef CONFIG_MAC80211_MESH
2768static bool
2769ieee80211_rx_mesh_fast_forward(struct ieee80211_sub_if_data *sdata,
2770 struct sk_buff *skb, int hdrlen)
2771{
2772 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
8c75cdcd
FF
2773 struct ieee80211_mesh_fast_tx_key key = {
2774 .type = MESH_FAST_TX_TYPE_FORWARDED
2775 };
2776 struct ieee80211_mesh_fast_tx *entry;
8b0f5cb6
FF
2777 struct ieee80211s_hdr *mesh_hdr;
2778 struct tid_ampdu_tx *tid_tx;
2779 struct sta_info *sta;
2780 struct ethhdr eth;
2781 u8 tid;
2782
2783 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(eth));
2784 if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
8c75cdcd 2785 ether_addr_copy(key.addr, mesh_hdr->eaddr1);
8b0f5cb6 2786 else if (!(mesh_hdr->flags & MESH_FLAGS_AE))
8c75cdcd
FF
2787 ether_addr_copy(key.addr, skb->data);
2788 else
2789 return false;
2790
2791 entry = mesh_fast_tx_get(sdata, &key);
8b0f5cb6
FF
2792 if (!entry)
2793 return false;
2794
2795 sta = rcu_dereference(entry->mpath->next_hop);
2796 if (!sta)
2797 return false;
2798
2799 if (skb_linearize(skb))
2800 return false;
2801
2802 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2803 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
2804 if (tid_tx) {
2805 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
2806 return false;
2807
2808 if (tid_tx->timeout)
2809 tid_tx->last_tx = jiffies;
2810 }
2811
2812 ieee80211_aggr_check(sdata, sta, skb);
2813
2814 if (ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
2815 &skb->protocol))
2816 hdrlen += ETH_ALEN;
2817 else
2818 skb->protocol = htons(skb->len - hdrlen);
2819 skb_set_network_header(skb, hdrlen + 2);
2820
2821 skb->dev = sdata->dev;
2822 memcpy(&eth, skb->data, ETH_HLEN - 2);
2823 skb_pull(skb, 2);
2824 __ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
2825 eth.h_dest, eth.h_source);
2826 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2827 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2828
2829 return true;
2830}
2831#endif
2832
986e43b1
FF
2833static ieee80211_rx_result
2834ieee80211_rx_mesh_data(struct ieee80211_sub_if_data *sdata, struct sta_info *sta,
2835 struct sk_buff *skb)
2836{
2837#ifdef CONFIG_MAC80211_MESH
2838 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2839 struct ieee80211_local *local = sdata->local;
2840 uint16_t fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA;
2841 struct ieee80211_hdr hdr = {
2842 .frame_control = cpu_to_le16(fc)
2843 };
2844 struct ieee80211_hdr *fwd_hdr;
2845 struct ieee80211s_hdr *mesh_hdr;
2846 struct ieee80211_tx_info *info;
2847 struct sk_buff *fwd_skb;
2848 struct ethhdr *eth;
2849 bool multicast;
2850 int tailroom = 0;
2851 int hdrlen, mesh_hdrlen;
2852 u8 *qos;
2853
2854 if (!ieee80211_vif_is_mesh(&sdata->vif))
2855 return RX_CONTINUE;
2856
2857 if (!pskb_may_pull(skb, sizeof(*eth) + 6))
286e6967 2858 return RX_DROP;
986e43b1
FF
2859
2860 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(*eth));
2861 mesh_hdrlen = ieee80211_get_mesh_hdrlen(mesh_hdr);
2862
2863 if (!pskb_may_pull(skb, sizeof(*eth) + mesh_hdrlen))
286e6967 2864 return RX_DROP;
986e43b1
FF
2865
2866 eth = (struct ethhdr *)skb->data;
2867 multicast = is_multicast_ether_addr(eth->h_dest);
2868
2869 mesh_hdr = (struct ieee80211s_hdr *)(eth + 1);
2870 if (!mesh_hdr->ttl)
286e6967 2871 return RX_DROP;
986e43b1
FF
2872
2873 /* frame is in RMC, don't forward */
2874 if (is_multicast_ether_addr(eth->h_dest) &&
2875 mesh_rmc_check(sdata, eth->h_source, mesh_hdr))
286e6967 2876 return RX_DROP;
986e43b1 2877
986e43b1
FF
2878 /* forward packet */
2879 if (sdata->crypto_tx_tailroom_needed_cnt)
2880 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2881
986e43b1
FF
2882 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2883 struct mesh_path *mppath;
2884 char *proxied_addr;
d5edb9ae 2885 bool update = false;
986e43b1
FF
2886
2887 if (multicast)
2888 proxied_addr = mesh_hdr->eaddr1;
2889 else if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2890 /* has_a4 already checked in ieee80211_rx_mesh_check */
2891 proxied_addr = mesh_hdr->eaddr2;
2892 else
286e6967 2893 return RX_DROP;
986e43b1
FF
2894
2895 rcu_read_lock();
2896 mppath = mpp_path_lookup(sdata, proxied_addr);
2897 if (!mppath) {
2898 mpp_path_add(sdata, proxied_addr, eth->h_source);
2899 } else {
2900 spin_lock_bh(&mppath->state_lock);
d5edb9ae 2901 if (!ether_addr_equal(mppath->mpp, eth->h_source)) {
986e43b1 2902 memcpy(mppath->mpp, eth->h_source, ETH_ALEN);
d5edb9ae
FF
2903 update = true;
2904 }
986e43b1
FF
2905 mppath->exp_time = jiffies;
2906 spin_unlock_bh(&mppath->state_lock);
2907 }
d5edb9ae
FF
2908
2909 /* flush fast xmit cache if the address path changed */
2910 if (update)
2911 mesh_fast_tx_flush_addr(sdata, proxied_addr);
2912
986e43b1
FF
2913 rcu_read_unlock();
2914 }
2915
f355f701
FF
2916 /* Frame has reached destination. Don't forward */
2917 if (ether_addr_equal(sdata->vif.addr, eth->h_dest))
2918 goto rx_accept;
2919
e26c0946
FF
2920 if (!--mesh_hdr->ttl) {
2921 if (multicast)
2922 goto rx_accept;
2923
2924 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
286e6967 2925 return RX_DROP;
e26c0946
FF
2926 }
2927
f355f701
FF
2928 if (!ifmsh->mshcfg.dot11MeshForwarding) {
2929 if (is_multicast_ether_addr(eth->h_dest))
2930 goto rx_accept;
2931
286e6967 2932 return RX_DROP;
f355f701
FF
2933 }
2934
986e43b1
FF
2935 skb_set_queue_mapping(skb, ieee802_1d_to_ac[skb->priority]);
2936
8b0f5cb6
FF
2937 if (!multicast &&
2938 ieee80211_rx_mesh_fast_forward(sdata, skb, mesh_hdrlen))
2939 return RX_QUEUED;
2940
986e43b1
FF
2941 ieee80211_fill_mesh_addresses(&hdr, &hdr.frame_control,
2942 eth->h_dest, eth->h_source);
2943 hdrlen = ieee80211_hdrlen(hdr.frame_control);
2944 if (multicast) {
2945 int extra_head = sizeof(struct ieee80211_hdr) - sizeof(*eth);
2946
2947 fwd_skb = skb_copy_expand(skb, local->tx_headroom + extra_head +
2948 IEEE80211_ENCRYPT_HEADROOM,
2949 tailroom, GFP_ATOMIC);
2950 if (!fwd_skb)
2951 goto rx_accept;
2952 } else {
2953 fwd_skb = skb;
2954 skb = NULL;
2955
2956 if (skb_cow_head(fwd_skb, hdrlen - sizeof(struct ethhdr)))
dccc9aa7 2957 return RX_DROP_U_OOM;
8f0149a8
FF
2958
2959 if (skb_linearize(fwd_skb))
dccc9aa7 2960 return RX_DROP_U_OOM;
986e43b1
FF
2961 }
2962
2963 fwd_hdr = skb_push(fwd_skb, hdrlen - sizeof(struct ethhdr));
2964 memcpy(fwd_hdr, &hdr, hdrlen - 2);
2965 qos = ieee80211_get_qos_ctl(fwd_hdr);
2966 qos[0] = qos[1] = 0;
2967
2968 skb_reset_mac_header(fwd_skb);
2969 hdrlen += mesh_hdrlen;
2970 if (ieee80211_get_8023_tunnel_proto(fwd_skb->data + hdrlen,
2971 &fwd_skb->protocol))
2972 hdrlen += ETH_ALEN;
2973 else
2974 fwd_skb->protocol = htons(fwd_skb->len - hdrlen);
899c2c11 2975 skb_set_network_header(fwd_skb, hdrlen + 2);
986e43b1
FF
2976
2977 info = IEEE80211_SKB_CB(fwd_skb);
2978 memset(info, 0, sizeof(*info));
2979 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2980 info->control.vif = &sdata->vif;
2981 info->control.jiffies = jiffies;
8b0f5cb6 2982 fwd_skb->dev = sdata->dev;
986e43b1
FF
2983 if (multicast) {
2984 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2985 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2986 /* update power mode indication when forwarding */
2987 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2988 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2989 /* mesh power mode flags updated in mesh_nexthop_lookup */
2990 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2991 } else {
2992 /* unable to resolve next hop */
2993 if (sta)
2994 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2995 hdr.addr3, 0,
2996 WLAN_REASON_MESH_PATH_NOFORWARD,
2997 sta->sta.addr);
2998 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2999 kfree_skb(fwd_skb);
3000 goto rx_accept;
3001 }
3002
3003 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
3aaa1a5a 3004 ieee80211_set_qos_hdr(sdata, fwd_skb);
986e43b1
FF
3005 ieee80211_add_pending_skb(local, fwd_skb);
3006
3007rx_accept:
3008 if (!skb)
3009 return RX_QUEUED;
3010
3011 ieee80211_strip_8023_mesh_hdr(skb);
3012#endif
3013
3014 return RX_CONTINUE;
3015}
3016
49461622 3017static ieee80211_rx_result debug_noinline
24bba078 3018__ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
fd4c7f2f 3019{
eb9fb5b8 3020 struct net_device *dev = rx->sdata->dev;
eaf85ca7 3021 struct sk_buff *skb = rx->skb;
358c8d9d
HH
3022 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3023 __le16 fc = hdr->frame_control;
eaf85ca7 3024 struct sk_buff_head frame_list;
4d78e032 3025 ieee80211_rx_result res;
7f6990c8 3026 struct ethhdr ethhdr;
e2b5227f 3027 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
fd4c7f2f 3028
ea720935 3029 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
e2b5227f
JB
3030 check_da = NULL;
3031 check_sa = NULL;
3032 } else switch (rx->sdata->vif.type) {
3033 case NL80211_IFTYPE_AP:
3034 case NL80211_IFTYPE_AP_VLAN:
3035 check_da = NULL;
3036 break;
3037 case NL80211_IFTYPE_STATION:
c3219371 3038 if (!test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
e2b5227f
JB
3039 check_sa = NULL;
3040 break;
3041 case NL80211_IFTYPE_MESH_POINT:
3042 check_sa = NULL;
986e43b1 3043 check_da = NULL;
e2b5227f
JB
3044 break;
3045 default:
3046 break;
ea720935 3047 }
fd4c7f2f 3048
eaf85ca7
ZY
3049 skb->dev = dev;
3050 __skb_queue_head_init(&frame_list);
fd4c7f2f 3051
7f6990c8
JB
3052 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
3053 rx->sdata->vif.addr,
24bba078 3054 rx->sdata->vif.type,
a1d5ff56 3055 data_offset, true))
dccc9aa7 3056 return RX_DROP_U_BAD_AMSDU;
7f6990c8 3057
a16fc383 3058 if (rx->sta->amsdu_mesh_control < 0) {
fe4a6d2d
FF
3059 s8 valid = -1;
3060 int i;
3061
3062 for (i = 0; i <= 2; i++) {
3063 if (!ieee80211_is_valid_amsdu(skb, i))
3064 continue;
3065
3066 if (valid >= 0) {
3067 /* ambiguous */
3068 valid = -1;
3069 break;
3070 }
3071
3072 valid = i;
3073 }
6e4c0d04 3074
fe4a6d2d 3075 rx->sta->amsdu_mesh_control = valid;
6e4c0d04
FF
3076 }
3077
eaf85ca7
ZY
3078 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
3079 rx->sdata->vif.type,
8b935ee2 3080 rx->local->hw.extra_tx_headroom,
6e4c0d04
FF
3081 check_da, check_sa,
3082 rx->sta->amsdu_mesh_control);
fd4c7f2f 3083
eaf85ca7
ZY
3084 while (!skb_queue_empty(&frame_list)) {
3085 rx->skb = __skb_dequeue(&frame_list);
fd4c7f2f 3086
986e43b1
FF
3087 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3088 switch (res) {
3089 case RX_QUEUED:
ce3edf6d 3090 continue;
986e43b1
FF
3091 case RX_CONTINUE:
3092 break;
3093 default:
3094 goto free;
ce3edf6d 3095 }
fd4c7f2f 3096
986e43b1
FF
3097 if (!ieee80211_frame_allowed(rx, fc))
3098 goto free;
3099
fd4c7f2f 3100 ieee80211_deliver_skb(rx);
986e43b1
FF
3101 continue;
3102
3103free:
3104 dev_kfree_skb(rx->skb);
fd4c7f2f
RR
3105 }
3106
9ae54c84 3107 return RX_QUEUED;
fd4c7f2f
RR
3108}
3109
24bba078
FF
3110static ieee80211_rx_result debug_noinline
3111ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
3112{
3113 struct sk_buff *skb = rx->skb;
3114 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3115 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3116 __le16 fc = hdr->frame_control;
3117
3118 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
3119 return RX_CONTINUE;
3120
3121 if (unlikely(!ieee80211_is_data(fc)))
3122 return RX_CONTINUE;
3123
3124 if (unlikely(!ieee80211_is_data_present(fc)))
286e6967 3125 return RX_DROP;
24bba078
FF
3126
3127 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3128 switch (rx->sdata->vif.type) {
3129 case NL80211_IFTYPE_AP_VLAN:
3130 if (!rx->sdata->u.vlan.sta)
dccc9aa7 3131 return RX_DROP_U_BAD_4ADDR;
24bba078
FF
3132 break;
3133 case NL80211_IFTYPE_STATION:
3134 if (!rx->sdata->u.mgd.use_4addr)
dccc9aa7 3135 return RX_DROP_U_BAD_4ADDR;
24bba078 3136 break;
986e43b1
FF
3137 case NL80211_IFTYPE_MESH_POINT:
3138 break;
24bba078 3139 default:
dccc9aa7 3140 return RX_DROP_U_BAD_4ADDR;
24bba078
FF
3141 }
3142 }
3143
a16fc383 3144 if (is_multicast_ether_addr(hdr->addr1) || !rx->sta)
dccc9aa7 3145 return RX_DROP_U_BAD_AMSDU;
24bba078 3146
270032a2
JB
3147 if (rx->key) {
3148 /*
3149 * We should not receive A-MSDUs on pre-HT connections,
3150 * and HT connections cannot use old ciphers. Thus drop
3151 * them, as in those cases we couldn't even have SPP
3152 * A-MSDUs or such.
3153 */
3154 switch (rx->key->conf.cipher) {
3155 case WLAN_CIPHER_SUITE_WEP40:
3156 case WLAN_CIPHER_SUITE_WEP104:
3157 case WLAN_CIPHER_SUITE_TKIP:
dccc9aa7 3158 return RX_DROP_U_BAD_AMSDU_CIPHER;
270032a2
JB
3159 default:
3160 break;
3161 }
3162 }
3163
24bba078
FF
3164 return __ieee80211_rx_h_amsdu(rx, 0);
3165}
3166
49461622 3167static ieee80211_rx_result debug_noinline
5cf121c3 3168ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf 3169{
eb9fb5b8 3170 struct ieee80211_sub_if_data *sdata = rx->sdata;
e15276a4 3171 struct ieee80211_local *local = rx->local;
eb9fb5b8 3172 struct net_device *dev = sdata->dev;
358c8d9d
HH
3173 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
3174 __le16 fc = hdr->frame_control;
4d78e032 3175 ieee80211_rx_result res;
4114fa21 3176 bool port_control;
76ee65bf 3177
358c8d9d 3178 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
9ae54c84 3179 return RX_CONTINUE;
76ee65bf 3180
358c8d9d 3181 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
286e6967 3182 return RX_DROP;
76ee65bf 3183
286e6967 3184 /* Send unexpected-4addr-frame event to hostapd */
f14543ee 3185 if (ieee80211_has_a4(hdr->frame_control) &&
e7f4a940
JB
3186 sdata->vif.type == NL80211_IFTYPE_AP) {
3187 if (rx->sta &&
3188 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3189 cfg80211_rx_unexpected_4addr_frame(
3190 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
286e6967 3191 return RX_DROP;
e7f4a940 3192 }
f14543ee 3193
2a1c5c7d
JB
3194 res = __ieee80211_data_to_8023(rx, &port_control);
3195 if (unlikely(res != RX_CONTINUE))
3196 return res;
76ee65bf 3197
986e43b1
FF
3198 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3199 if (res != RX_CONTINUE)
3200 return res;
3201
358c8d9d 3202 if (!ieee80211_frame_allowed(rx, fc))
286e6967 3203 return RX_DROP;
ce3edf6d 3204
8a4d32f3
AN
3205 /* directly handle TDLS channel switch requests/responses */
3206 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3207 cpu_to_be16(ETH_P_TDLS))) {
3208 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3209
3210 if (pskb_may_pull(rx->skb,
3211 offsetof(struct ieee80211_tdls_data, u)) &&
3212 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3213 tf->category == WLAN_CATEGORY_TDLS &&
3214 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3215 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
f057d140 3216 rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
4f6c78de
JB
3217 __ieee80211_queue_skb_to_iface(sdata, rx->link_id,
3218 rx->sta, rx->skb);
8a4d32f3
AN
3219 return RX_QUEUED;
3220 }
3221 }
3222
4114fa21
FF
3223 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3224 unlikely(port_control) && sdata->bss) {
3225 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3226 u.ap);
3227 dev = sdata->dev;
3228 rx->sdata = sdata;
3229 }
3230
76ee65bf
RR
3231 rx->skb->dev = dev;
3232
602fae42
JB
3233 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3234 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
8c99f691
RM
3235 !is_multicast_ether_addr(
3236 ((struct ethhdr *)rx->skb->data)->h_dest) &&
3237 (!local->scanning &&
602fae42
JB
3238 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3239 mod_timer(&local->dynamic_ps_timer, jiffies +
3240 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
e15276a4 3241
76ee65bf 3242 ieee80211_deliver_skb(rx);
571ecf67 3243
9ae54c84 3244 return RX_QUEUED;
571ecf67
JB
3245}
3246
49461622 3247static ieee80211_rx_result debug_noinline
f9e124fb 3248ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
71364716 3249{
71364716 3250 struct sk_buff *skb = rx->skb;
a7767f95 3251 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
71364716
RR
3252 struct tid_ampdu_rx *tid_agg_rx;
3253 u16 start_seq_num;
3254 u16 tid;
3255
a7767f95 3256 if (likely(!ieee80211_is_ctl(bar->frame_control)))
9ae54c84 3257 return RX_CONTINUE;
71364716 3258
a7767f95 3259 if (ieee80211_is_back_req(bar->frame_control)) {
8ae5977f
JB
3260 struct {
3261 __le16 control, start_seq_num;
3262 } __packed bar_data;
6382246e
EG
3263 struct ieee80211_event event = {
3264 .type = BAR_RX_EVENT,
3265 };
8ae5977f 3266
71364716 3267 if (!rx->sta)
286e6967 3268 return RX_DROP;
8ae5977f
JB
3269
3270 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3271 &bar_data, sizeof(bar_data)))
286e6967 3272 return RX_DROP;
8ae5977f 3273
8ae5977f 3274 tid = le16_to_cpu(bar_data.control) >> 12;
a87f736d 3275
53f24974
JB
3276 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3277 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3278 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3279 WLAN_BACK_RECIPIENT,
3280 WLAN_REASON_QSTA_REQUIRE_SETUP);
3281
a87f736d
JB
3282 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3283 if (!tid_agg_rx)
286e6967 3284 return RX_DROP;
71364716 3285
8ae5977f 3286 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
6382246e
EG
3287 event.u.ba.tid = tid;
3288 event.u.ba.ssn = start_seq_num;
3289 event.u.ba.sta = &rx->sta->sta;
71364716
RR
3290
3291 /* reset session timer */
20ad19d0
JB
3292 if (tid_agg_rx->timeout)
3293 mod_timer(&tid_agg_rx->session_timer,
3294 TU_TO_EXP_TIME(tid_agg_rx->timeout));
71364716 3295
dd318575 3296 spin_lock(&tid_agg_rx->reorder_lock);
a02ae758 3297 /* release stored frames up to start of BAR */
d3b2fb53 3298 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
f9e124fb 3299 start_seq_num, frames);
dd318575
JB
3300 spin_unlock(&tid_agg_rx->reorder_lock);
3301
6382246e
EG
3302 drv_event_callback(rx->local, rx->sdata, &event);
3303
a02ae758
JB
3304 kfree_skb(skb);
3305 return RX_QUEUED;
71364716
RR
3306 }
3307
286e6967 3308 return RX_DROP;
71364716
RR
3309}
3310
f4f727a6
JM
3311static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3312 struct ieee80211_mgmt *mgmt,
3313 size_t len)
fea14732
JM
3314{
3315 struct ieee80211_local *local = sdata->local;
3316 struct sk_buff *skb;
3317 struct ieee80211_mgmt *resp;
3318
b203ca39 3319 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
fea14732
JM
3320 /* Not to own unicast address */
3321 return;
3322 }
3323
9a267ce4
JB
3324 if (!ether_addr_equal(mgmt->sa, sdata->vif.cfg.ap_addr) ||
3325 !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
77fdaa12 3326 /* Not from the current AP or not associated yet. */
fea14732
JM
3327 return;
3328 }
3329
3330 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3331 /* Too short SA Query request frame */
3332 return;
3333 }
3334
3335 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3336 if (skb == NULL)
3337 return;
3338
3339 skb_reserve(skb, local->hw.extra_tx_headroom);
b080db58 3340 resp = skb_put_zero(skb, 24);
9a267ce4 3341 memcpy(resp->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
47846c9b 3342 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
9a267ce4 3343 memcpy(resp->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
fea14732
JM
3344 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3345 IEEE80211_STYPE_ACTION);
3346 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3347 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3348 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3349 memcpy(resp->u.action.u.sa_query.trans_id,
3350 mgmt->u.action.u.sa_query.trans_id,
3351 WLAN_SA_QUERY_TR_ID_LEN);
3352
62ae67be 3353 ieee80211_tx_skb(sdata, skb);
fea14732
JM
3354}
3355
6d945a33
LB
3356static void
3357ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx)
3358{
3359 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
4044b237 3360 struct ieee80211_bss_conf *bss_conf;
6d945a33
LB
3361 const struct element *ie;
3362 size_t baselen;
3363
3364 if (!wiphy_ext_feature_isset(rx->local->hw.wiphy,
3365 NL80211_EXT_FEATURE_BSS_COLOR))
3366 return;
3367
3368 if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION))
3369 return;
3370
4044b237
MCL
3371 bss_conf = rx->link->conf;
3372 if (bss_conf->csa_active || bss_conf->color_change_active ||
3373 !bss_conf->he_bss_color.enabled)
6d945a33
LB
3374 return;
3375
3376 baselen = mgmt->u.beacon.variable - rx->skb->data;
3377 if (baselen > rx->skb->len)
3378 return;
3379
3380 ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
3381 mgmt->u.beacon.variable,
3382 rx->skb->len - baselen);
3383 if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) &&
3384 ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) {
6d945a33
LB
3385 const struct ieee80211_he_operation *he_oper;
3386 u8 color;
3387
3388 he_oper = (void *)(ie->data + 1);
3389 if (le32_get_bits(he_oper->he_oper_params,
3390 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED))
3391 return;
3392
3393 color = le32_get_bits(he_oper->he_oper_params,
3394 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3395 if (color == bss_conf->he_bss_color.color)
82253dda 3396 ieee80211_obss_color_collision_notify(&rx->sdata->vif,
414e736c
AKS
3397 BIT_ULL(color),
3398 bss_conf->link_id);
6d945a33
LB
3399 }
3400}
3401
2e161f78
JB
3402static ieee80211_rx_result debug_noinline
3403ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3404{
3405 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 3406 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78 3407
09a740ce
TP
3408 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3409 return RX_CONTINUE;
3410
2e161f78
JB
3411 /*
3412 * From here on, look only at management frames.
3413 * Data and control frames are already handled,
3414 * and unknown (reserved) frames are useless.
3415 */
3416 if (rx->skb->len < 24)
286e6967 3417 return RX_DROP;
2e161f78
JB
3418
3419 if (!ieee80211_is_mgmt(mgmt->frame_control))
286e6967 3420 return RX_DROP;
2e161f78 3421
2cc7add3
JB
3422 /* drop too small action frames */
3423 if (ieee80211_is_action(mgmt->frame_control) &&
3424 rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
dccc9aa7 3425 return RX_DROP_U_RUNT_ACTION;
2cc7add3 3426
ee971924
JB
3427 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3428 ieee80211_is_beacon(mgmt->frame_control) &&
3429 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
804483e9
JB
3430 int sig = 0;
3431
6d945a33
LB
3432 /* sw bss color collision detection */
3433 ieee80211_rx_check_bss_color_collision(rx);
3434
1ad22fb5
T
3435 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3436 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
804483e9
JB
3437 sig = status->signal;
3438
e76fede8
TP
3439 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3440 rx->skb->data, rx->skb->len,
3441 ieee80211_rx_status_to_khz(status),
3442 sig);
ee971924
JB
3443 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3444 }
3445
6c02fab7 3446 return ieee80211_drop_unencrypted_mgmt(rx);
2e161f78
JB
3447}
3448
f5a4c24e
LB
3449static bool
3450ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3451{
3452 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
f5a4c24e 3453 struct ieee80211_sub_if_data *sdata = rx->sdata;
f5a4c24e
LB
3454
3455 /* TWT actions are only supported in AP for the moment */
3456 if (sdata->vif.type != NL80211_IFTYPE_AP)
3457 return false;
3458
3459 if (!rx->local->ops->add_twt_setup)
3460 return false;
3461
68ba1131 3462 if (!sdata->vif.bss_conf.twt_responder)
f5a4c24e
LB
3463 return false;
3464
3465 if (!rx->sta)
3466 return false;
3467
3468 switch (mgmt->u.action.u.s1g.action_code) {
3469 case WLAN_S1G_TWT_SETUP: {
3470 struct ieee80211_twt_setup *twt;
3471
3472 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3473 1 + /* action code */
3474 sizeof(struct ieee80211_twt_setup) +
3475 2 /* TWT req_type agrt */)
3476 break;
3477
3478 twt = (void *)mgmt->u.action.u.s1g.variable;
3479 if (twt->element_id != WLAN_EID_S1G_TWT)
3480 break;
3481
3482 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3483 4 + /* action code + token + tlv */
3484 twt->length)
3485 break;
3486
3487 return true; /* queue the frame */
3488 }
3489 case WLAN_S1G_TWT_TEARDOWN:
3490 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE + 2)
3491 break;
3492
3493 return true; /* queue the frame */
3494 default:
3495 break;
3496 }
3497
3498 return false;
3499}
3500
de1ede7a
JB
3501static ieee80211_rx_result debug_noinline
3502ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3503{
3504 struct ieee80211_local *local = rx->local;
eb9fb5b8 3505 struct ieee80211_sub_if_data *sdata = rx->sdata;
de1ede7a 3506 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 3507 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
de1ede7a
JB
3508 int len = rx->skb->len;
3509
3510 if (!ieee80211_is_action(mgmt->frame_control))
3511 return RX_CONTINUE;
3512
815b8092 3513 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
cd7760e6
SW
3514 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3515 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
dccc9aa7 3516 return RX_DROP_U_ACTION_UNKNOWN_SRC;
de1ede7a 3517
de1ede7a 3518 switch (mgmt->u.action.category) {
1d8d3dec
JB
3519 case WLAN_CATEGORY_HT:
3520 /* reject HT action frames from stations not supporting HT */
b320d6c4 3521 if (!rx->link_sta->pub->ht_cap.ht_supported)
1d8d3dec
JB
3522 goto invalid;
3523
3524 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3525 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3526 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3527 sdata->vif.type != NL80211_IFTYPE_AP &&
3528 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3529 break;
3530
ec61cd63 3531 /* verify action & smps_control/chanwidth are present */
1d8d3dec
JB
3532 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3533 goto invalid;
3534
3535 switch (mgmt->u.action.u.ht_smps.action) {
3536 case WLAN_HT_ACTION_SMPS: {
3537 struct ieee80211_supported_band *sband;
af0ed69b 3538 enum ieee80211_smps_mode smps_mode;
ff84e7bf 3539 struct sta_opmode_info sta_opmode = {};
1d8d3dec 3540
c4d800dc
IP
3541 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3542 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3543 goto handled;
3544
1d8d3dec
JB
3545 /* convert to HT capability */
3546 switch (mgmt->u.action.u.ht_smps.smps_control) {
3547 case WLAN_HT_SMPS_CONTROL_DISABLED:
af0ed69b 3548 smps_mode = IEEE80211_SMPS_OFF;
1d8d3dec
JB
3549 break;
3550 case WLAN_HT_SMPS_CONTROL_STATIC:
af0ed69b 3551 smps_mode = IEEE80211_SMPS_STATIC;
1d8d3dec
JB
3552 break;
3553 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
af0ed69b 3554 smps_mode = IEEE80211_SMPS_DYNAMIC;
1d8d3dec
JB
3555 break;
3556 default:
3557 goto invalid;
3558 }
1d8d3dec
JB
3559
3560 /* if no change do nothing */
261ce887 3561 if (rx->link_sta->pub->smps_mode == smps_mode)
1d8d3dec 3562 goto handled;
261ce887 3563 rx->link_sta->pub->smps_mode = smps_mode;
57566b20 3564 sta_opmode.smps_mode =
3565 ieee80211_smps_mode_to_smps_mode(smps_mode);
ff84e7bf 3566 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
1d8d3dec
JB
3567
3568 sband = rx->local->hw.wiphy->bands[status->band];
3569
88b67e91 3570 rate_control_rate_update(local, sband, rx->link_sta,
64f68e5d 3571 IEEE80211_RC_SMPS_CHANGED);
ff84e7bf 3572 cfg80211_sta_opmode_change_notify(sdata->dev,
3573 rx->sta->addr,
3574 &sta_opmode,
c752cac9 3575 GFP_ATOMIC);
1d8d3dec
JB
3576 goto handled;
3577 }
ec61cd63
JB
3578 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3579 struct ieee80211_supported_band *sband;
3580 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
1c45c5ce 3581 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
ff84e7bf 3582 struct sta_opmode_info sta_opmode = {};
ec61cd63
JB
3583
3584 /* If it doesn't support 40 MHz it can't change ... */
b320d6c4 3585 if (!(rx->link_sta->pub->ht_cap.cap &
e1a0c6b3 3586 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
ec61cd63
JB
3587 goto handled;
3588
e1a0c6b3 3589 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
1c45c5ce 3590 max_bw = IEEE80211_STA_RX_BW_20;
e1a0c6b3 3591 else
b320d6c4 3592 max_bw = ieee80211_sta_cap_rx_bw(rx->link_sta);
1c45c5ce
EP
3593
3594 /* set cur_max_bandwidth and recalc sta bw */
b320d6c4
BB
3595 rx->link_sta->cur_max_bandwidth = max_bw;
3596 new_bw = ieee80211_sta_cur_vht_bw(rx->link_sta);
ec61cd63 3597
b320d6c4 3598 if (rx->link_sta->pub->bandwidth == new_bw)
ec61cd63
JB
3599 goto handled;
3600
b320d6c4 3601 rx->link_sta->pub->bandwidth = new_bw;
ec61cd63 3602 sband = rx->local->hw.wiphy->bands[status->band];
97f5f425 3603 sta_opmode.bw =
b320d6c4 3604 ieee80211_sta_rx_bw_to_chan_width(rx->link_sta);
ff84e7bf 3605 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
ec61cd63 3606
88b67e91 3607 rate_control_rate_update(local, sband, rx->link_sta,
ec61cd63 3608 IEEE80211_RC_BW_CHANGED);
ff84e7bf 3609 cfg80211_sta_opmode_change_notify(sdata->dev,
3610 rx->sta->addr,
3611 &sta_opmode,
c752cac9 3612 GFP_ATOMIC);
ec61cd63
JB
3613 goto handled;
3614 }
1d8d3dec
JB
3615 default:
3616 goto invalid;
3617 }
3618
0af83d3d 3619 break;
1b3a2e49 3620 case WLAN_CATEGORY_PUBLIC:
b777bdfc 3621 case WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION:
1b3a2e49
JB
3622 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3623 goto invalid;
3624 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3625 break;
3626 if (!rx->sta)
3627 break;
bfd8403a 3628 if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid))
1b3a2e49
JB
3629 break;
3630 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3631 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3632 break;
3633 if (len < offsetof(struct ieee80211_mgmt,
3634 u.action.u.ext_chan_switch.variable))
3635 goto invalid;
3636 goto queue;
0af83d3d
JB
3637 case WLAN_CATEGORY_VHT:
3638 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3639 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3640 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3641 sdata->vif.type != NL80211_IFTYPE_AP &&
3642 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3643 break;
3644
3645 /* verify action code is present */
3646 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3647 goto invalid;
3648
3649 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3650 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
0af83d3d
JB
3651 /* verify opmode is present */
3652 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3653 goto invalid;
d2941df8 3654 goto queue;
0af83d3d 3655 }
23a1f8d4
SS
3656 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3657 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3658 goto invalid;
3659 goto queue;
3660 }
0af83d3d
JB
3661 default:
3662 break;
3663 }
1d8d3dec 3664 break;
de1ede7a 3665 case WLAN_CATEGORY_BACK:
8abd3f9b 3666 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
ae2772b3 3667 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
8abd3f9b 3668 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
13c40c54
AS
3669 sdata->vif.type != NL80211_IFTYPE_AP &&
3670 sdata->vif.type != NL80211_IFTYPE_ADHOC)
84040805 3671 break;
8abd3f9b 3672
026331c4
JM
3673 /* verify action_code is present */
3674 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3675 break;
3676
de1ede7a
JB
3677 switch (mgmt->u.action.u.addba_req.action_code) {
3678 case WLAN_ACTION_ADDBA_REQ:
3679 if (len < (IEEE80211_MIN_ACTION_SIZE +
3680 sizeof(mgmt->u.action.u.addba_req)))
bed7ee6e
JB
3681 goto invalid;
3682 break;
de1ede7a
JB
3683 case WLAN_ACTION_ADDBA_RESP:
3684 if (len < (IEEE80211_MIN_ACTION_SIZE +
3685 sizeof(mgmt->u.action.u.addba_resp)))
bed7ee6e
JB
3686 goto invalid;
3687 break;
de1ede7a
JB
3688 case WLAN_ACTION_DELBA:
3689 if (len < (IEEE80211_MIN_ACTION_SIZE +
3690 sizeof(mgmt->u.action.u.delba)))
bed7ee6e
JB
3691 goto invalid;
3692 break;
3693 default:
3694 goto invalid;
de1ede7a 3695 }
bed7ee6e 3696
8b58ff83 3697 goto queue;
39192c0b 3698 case WLAN_CATEGORY_SPECTRUM_MGMT:
026331c4
JM
3699 /* verify action_code is present */
3700 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3701 break;
3702
39192c0b
JB
3703 switch (mgmt->u.action.u.measurement.action_code) {
3704 case WLAN_ACTION_SPCT_MSR_REQ:
57fbcce3 3705 if (status->band != NL80211_BAND_5GHZ)
cd7760e6
SW
3706 break;
3707
39192c0b
JB
3708 if (len < (IEEE80211_MIN_ACTION_SIZE +
3709 sizeof(mgmt->u.action.u.measurement)))
84040805 3710 break;
cd7760e6
SW
3711
3712 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3713 break;
3714
39192c0b 3715 ieee80211_process_measurement_req(sdata, mgmt, len);
84040805 3716 goto handled;
cd7760e6
SW
3717 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3718 u8 *bssid;
3719 if (len < (IEEE80211_MIN_ACTION_SIZE +
3720 sizeof(mgmt->u.action.u.chan_switch)))
84040805 3721 break;
cc32abd4 3722
cd7760e6 3723 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
b8456a14
CYY
3724 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3725 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
cd7760e6
SW
3726 break;
3727
3728 if (sdata->vif.type == NL80211_IFTYPE_STATION)
bfd8403a 3729 bssid = sdata->deflink.u.mgd.bssid;
cd7760e6
SW
3730 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3731 bssid = sdata->u.ibss.bssid;
b8456a14
CYY
3732 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3733 bssid = mgmt->sa;
cd7760e6
SW
3734 else
3735 break;
3736
3737 if (!ether_addr_equal(mgmt->bssid, bssid))
84040805 3738 break;
c481ec97 3739
8b58ff83 3740 goto queue;
cd7760e6 3741 }
39192c0b
JB
3742 }
3743 break;
8db09850 3744 case WLAN_CATEGORY_SELF_PROTECTED:
9b395bc3
JB
3745 if (len < (IEEE80211_MIN_ACTION_SIZE +
3746 sizeof(mgmt->u.action.u.self_prot.action_code)))
3747 break;
3748
8db09850
TP
3749 switch (mgmt->u.action.u.self_prot.action_code) {
3750 case WLAN_SP_MESH_PEERING_OPEN:
3751 case WLAN_SP_MESH_PEERING_CLOSE:
3752 case WLAN_SP_MESH_PEERING_CONFIRM:
3753 if (!ieee80211_vif_is_mesh(&sdata->vif))
3754 goto invalid;
a6dad6a2 3755 if (sdata->u.mesh.user_mpm)
8db09850
TP
3756 /* userspace handles this frame */
3757 break;
3758 goto queue;
3759 case WLAN_SP_MGK_INFORM:
3760 case WLAN_SP_MGK_ACK:
3761 if (!ieee80211_vif_is_mesh(&sdata->vif))
3762 goto invalid;
3763 break;
3764 }
3765 break;
d3aaec8a 3766 case WLAN_CATEGORY_MESH_ACTION:
9b395bc3
JB
3767 if (len < (IEEE80211_MIN_ACTION_SIZE +
3768 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3769 break;
3770
77a121c3
JB
3771 if (!ieee80211_vif_is_mesh(&sdata->vif))
3772 break;
25d49e4d 3773 if (mesh_action_is_path_sel(mgmt) &&
1df332e8 3774 !mesh_path_sel_is_hwmp(sdata))
c7108a71
JC
3775 break;
3776 goto queue;
f5a4c24e 3777 case WLAN_CATEGORY_S1G:
19e4a47e
JB
3778 if (len < offsetofend(typeof(*mgmt),
3779 u.action.u.s1g.action_code))
3780 break;
3781
f5a4c24e
LB
3782 switch (mgmt->u.action.u.s1g.action_code) {
3783 case WLAN_S1G_TWT_SETUP:
3784 case WLAN_S1G_TWT_TEARDOWN:
3785 if (ieee80211_process_rx_twt_action(rx))
3786 goto queue;
3787 break;
3788 default:
3789 break;
3790 }
3791 break;
8f500fbc 3792 case WLAN_CATEGORY_PROTECTED_EHT:
7c1c73bf
JB
3793 if (len < offsetofend(typeof(*mgmt),
3794 u.action.u.ttlm_req.action_code))
3795 break;
3796
8f500fbc
AB
3797 switch (mgmt->u.action.u.ttlm_req.action_code) {
3798 case WLAN_PROTECTED_EHT_ACTION_TTLM_REQ:
3799 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3800 break;
3801
3802 if (len < offsetofend(typeof(*mgmt),
3803 u.action.u.ttlm_req))
3804 goto invalid;
3805 goto queue;
f7660b3f
AB
3806 case WLAN_PROTECTED_EHT_ACTION_TTLM_RES:
3807 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3808 break;
3809
3810 if (len < offsetofend(typeof(*mgmt),
3811 u.action.u.ttlm_res))
3812 goto invalid;
3813 goto queue;
8b8a6731
IP
3814 case WLAN_PROTECTED_EHT_ACTION_TTLM_TEARDOWN:
3815 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3816 break;
3817
3818 if (len < offsetofend(typeof(*mgmt),
3819 u.action.u.ttlm_tear_down))
3820 goto invalid;
3821 goto queue;
36e05b0b
IP
3822 case WLAN_PROTECTED_EHT_ACTION_LINK_RECONFIG_RESP:
3823 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3824 break;
3825
3826 /* The reconfiguration response action frame must
3827 * least one 'Status Duple' entry (3 octets)
3828 */
3829 if (len <
3830 offsetofend(typeof(*mgmt),
3831 u.action.u.ml_reconf_resp) + 3)
3832 goto invalid;
3833 goto queue;
de86c5f6
IP
3834 case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_RESP:
3835 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3836 break;
3837
3838 if (len < offsetofend(typeof(*mgmt),
3839 u.action.u.epcs) +
3840 IEEE80211_EPCS_ENA_RESP_BODY_LEN)
3841 goto invalid;
3842 goto queue;
3843 case WLAN_PROTECTED_EHT_ACTION_EPCS_ENABLE_TEARDOWN:
3844 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3845 break;
3846
3847 if (len < offsetofend(typeof(*mgmt),
3848 u.action.u.epcs))
3849 goto invalid;
3850 goto queue;
8f500fbc
AB
3851 default:
3852 break;
3853 }
3854 break;
84040805 3855 }
026331c4 3856
2e161f78
JB
3857 return RX_CONTINUE;
3858
bed7ee6e 3859 invalid:
554891e6 3860 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2e161f78
JB
3861 /* will return in the next handlers */
3862 return RX_CONTINUE;
3863
3864 handled:
3865 if (rx->sta)
b320d6c4 3866 rx->link_sta->rx_stats.packets++;
2e161f78
JB
3867 dev_kfree_skb(rx->skb);
3868 return RX_QUEUED;
3869
3870 queue:
4f6c78de 3871 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
2e161f78
JB
3872 return RX_QUEUED;
3873}
3874
3875static ieee80211_rx_result debug_noinline
3876ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3877{
554891e6 3878 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
f9202638
AS
3879 struct cfg80211_rx_info info = {
3880 .freq = ieee80211_rx_status_to_khz(status),
3881 .buf = rx->skb->data,
2ec833a5
JB
3882 .len = rx->skb->len,
3883 .link_id = rx->link_id,
3884 .have_link_id = rx->link_id >= 0,
f9202638 3885 };
2e161f78
JB
3886
3887 /* skip known-bad action frames and return them in the next handler */
554891e6 3888 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2e161f78 3889 return RX_CONTINUE;
d7907448 3890
026331c4
JM
3891 /*
3892 * Getting here means the kernel doesn't know how to handle
3893 * it, but maybe userspace does ... include returned frames
3894 * so userspace can register for those to know whether ones
3895 * it transmitted were processed or returned.
3896 */
026331c4 3897
1ad22fb5
T
3898 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3899 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
f9202638
AS
3900 info.sig_dbm = status->signal;
3901
3902 if (ieee80211_is_timing_measurement(rx->skb) ||
3903 ieee80211_is_ftm(rx->skb)) {
3904 info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp);
3905 info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp);
3906 }
804483e9 3907
f9202638 3908 if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) {
2e161f78 3909 if (rx->sta)
b320d6c4 3910 rx->link_sta->rx_stats.packets++;
2e161f78
JB
3911 dev_kfree_skb(rx->skb);
3912 return RX_QUEUED;
3913 }
3914
2e161f78
JB
3915 return RX_CONTINUE;
3916}
3917
1ea02224
JB
3918static ieee80211_rx_result debug_noinline
3919ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3920{
3921 struct ieee80211_sub_if_data *sdata = rx->sdata;
3922 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3923 int len = rx->skb->len;
3924
3925 if (!ieee80211_is_action(mgmt->frame_control))
3926 return RX_CONTINUE;
3927
3928 switch (mgmt->u.action.category) {
3929 case WLAN_CATEGORY_SA_QUERY:
3930 if (len < (IEEE80211_MIN_ACTION_SIZE +
3931 sizeof(mgmt->u.action.u.sa_query)))
3932 break;
3933
3934 switch (mgmt->u.action.u.sa_query.action) {
3935 case WLAN_ACTION_SA_QUERY_REQUEST:
3936 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3937 break;
3938 ieee80211_process_sa_query_req(sdata, mgmt, len);
3939 goto handled;
3940 }
3941 break;
3942 }
3943
3944 return RX_CONTINUE;
3945
3946 handled:
3947 if (rx->sta)
b320d6c4 3948 rx->link_sta->rx_stats.packets++;
1ea02224
JB
3949 dev_kfree_skb(rx->skb);
3950 return RX_QUEUED;
3951}
3952
2e161f78
JB
3953static ieee80211_rx_result debug_noinline
3954ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3955{
3956 struct ieee80211_local *local = rx->local;
3957 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3958 struct sk_buff *nskb;
3959 struct ieee80211_sub_if_data *sdata = rx->sdata;
554891e6 3960 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
3961
3962 if (!ieee80211_is_action(mgmt->frame_control))
3963 return RX_CONTINUE;
3964
3965 /*
3966 * For AP mode, hostapd is responsible for handling any action
3967 * frames that we didn't handle, including returning unknown
3968 * ones. For all other modes we will return them to the sender,
3969 * setting the 0x80 bit in the action category, as required by
4b5ebccc 3970 * 802.11-2012 9.24.4.
286e6967
AW
3971 * Newer versions of hostapd use the management frame registration
3972 * mechanisms and old cooked monitor interface is no longer supported.
2e161f78 3973 */
554891e6 3974 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2e161f78
JB
3975 (sdata->vif.type == NL80211_IFTYPE_AP ||
3976 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
286e6967 3977 return RX_DROP;
026331c4 3978
4b5ebccc 3979 if (is_multicast_ether_addr(mgmt->da))
286e6967 3980 return RX_DROP;
4b5ebccc 3981
84040805
JB
3982 /* do not return rejected action frames */
3983 if (mgmt->u.action.category & 0x80)
dccc9aa7 3984 return RX_DROP_U_REJECTED_ACTION_RESPONSE;
84040805
JB
3985
3986 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3987 GFP_ATOMIC);
3988 if (nskb) {
292b4df6 3989 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
84040805 3990
292b4df6
JL
3991 nmgmt->u.action.category |= 0x80;
3992 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3993 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
84040805
JB
3994
3995 memset(nskb->cb, 0, sizeof(nskb->cb));
3996
07e5a5f5
JB
3997 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3998 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3999
4000 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
4001 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
4002 IEEE80211_TX_CTL_NO_CCK_RATE;
30686bf7 4003 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
07e5a5f5
JB
4004 info->hw_queue =
4005 local->hw.offchannel_tx_hw_queue;
4006 }
4007
e1e68b14 4008 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7, -1,
08aca29a 4009 status->band);
de1ede7a 4010 }
6e02ba7c
JB
4011
4012 return RX_DROP_U_UNKNOWN_ACTION_REJECTED;
de1ede7a
JB
4013}
4014
09a740ce
TP
4015static ieee80211_rx_result debug_noinline
4016ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
4017{
4018 struct ieee80211_sub_if_data *sdata = rx->sdata;
4019 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
4020
4021 if (!ieee80211_is_ext(hdr->frame_control))
4022 return RX_CONTINUE;
4023
4024 if (sdata->vif.type != NL80211_IFTYPE_STATION)
286e6967 4025 return RX_DROP;
09a740ce
TP
4026
4027 /* for now only beacons are ext, so queue them */
4f6c78de 4028 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
09a740ce
TP
4029
4030 return RX_QUEUED;
4031}
4032
49461622 4033static ieee80211_rx_result debug_noinline
5cf121c3 4034ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67 4035{
eb9fb5b8 4036 struct ieee80211_sub_if_data *sdata = rx->sdata;
77a121c3
JB
4037 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
4038 __le16 stype;
571ecf67 4039
77a121c3 4040 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
472dbc45 4041
77a121c3
JB
4042 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
4043 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
239281f8 4044 sdata->vif.type != NL80211_IFTYPE_OCB &&
77a121c3 4045 sdata->vif.type != NL80211_IFTYPE_STATION)
286e6967 4046 return RX_DROP;
472dbc45 4047
77a121c3 4048 switch (stype) {
66e67e41 4049 case cpu_to_le16(IEEE80211_STYPE_AUTH):
77a121c3
JB
4050 case cpu_to_le16(IEEE80211_STYPE_BEACON):
4051 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
4052 /* process for all: mesh, mlme, ibss */
4053 break;
95697f99
JB
4054 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
4055 if (is_multicast_ether_addr(mgmt->da) &&
4056 !is_broadcast_ether_addr(mgmt->da))
286e6967 4057 return RX_DROP;
95697f99
JB
4058
4059 /* process only for station/IBSS */
4060 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
4061 sdata->vif.type != NL80211_IFTYPE_ADHOC)
286e6967 4062 return RX_DROP;
95697f99 4063 break;
66e67e41
JB
4064 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
4065 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
77a121c3 4066 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2c31333a
CL
4067 if (is_multicast_ether_addr(mgmt->da) &&
4068 !is_broadcast_ether_addr(mgmt->da))
286e6967 4069 return RX_DROP;
2c31333a 4070
77a121c3
JB
4071 /* process only for station */
4072 if (sdata->vif.type != NL80211_IFTYPE_STATION)
286e6967 4073 return RX_DROP;
77a121c3
JB
4074 break;
4075 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
9fb04b50
TP
4076 /* process only for ibss and mesh */
4077 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
4078 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
286e6967 4079 return RX_DROP;
77a121c3
JB
4080 break;
4081 default:
286e6967 4082 return RX_DROP;
77a121c3 4083 }
f9d540ee 4084
4f6c78de 4085 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
46900298 4086
77a121c3 4087 return RX_QUEUED;
571ecf67
JB
4088}
4089
aa0c8636
CL
4090static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
4091 ieee80211_rx_result res)
4092{
baa951a1
JB
4093 if (res == RX_QUEUED) {
4094 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
4095 return;
4096 }
aa0c8636 4097
baa951a1 4098 if (res != RX_CONTINUE) {
aa0c8636
CL
4099 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
4100 if (rx->sta)
b320d6c4 4101 rx->link_sta->rx_stats.dropped++;
aa0c8636 4102 }
baa951a1 4103
286e6967 4104 kfree_skb_reason(rx->skb, (__force u32)res);
aa0c8636 4105}
571ecf67 4106
f9e124fb
CL
4107static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
4108 struct sk_buff_head *frames)
58905290 4109{
286e6967 4110 ieee80211_rx_result res = RX_DROP;
aa0c8636 4111 struct sk_buff *skb;
8944b79f 4112
e32f85f7
LCC
4113#define CALL_RXH(rxh) \
4114 do { \
4115 res = rxh(rx); \
4116 if (res != RX_CONTINUE) \
2569a826 4117 goto rxh_next; \
8ebaa5b0 4118 } while (0)
49461622 4119
45ceeee8
JB
4120 /* Lock here to avoid hitting all of the data used in the RX
4121 * path (e.g. key data, station data, ...) concurrently when
4122 * a frame is released from the reorder buffer due to timeout
4123 * from the timer, potentially concurrently with RX from the
4124 * driver.
4125 */
f9e124fb 4126 spin_lock_bh(&rx->local->rx_path_lock);
24a8fdad 4127
f9e124fb 4128 while ((skb = __skb_dequeue(frames))) {
2569a826
JB
4129 /*
4130 * all the other fields are valid across frames
4131 * that belong to an aMPDU since they are on the
4132 * same TID from the same station
4133 */
4134 rx->skb = skb;
4135
56057da4
JB
4136 if (WARN_ON_ONCE(!rx->link))
4137 goto rxh_next;
4138
8ebaa5b0
JB
4139 CALL_RXH(ieee80211_rx_h_check_more_data);
4140 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
4141 CALL_RXH(ieee80211_rx_h_sta_process);
4142 CALL_RXH(ieee80211_rx_h_decrypt);
4143 CALL_RXH(ieee80211_rx_h_defragment);
4144 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
2569a826 4145 /* must be after MMIC verify so header is counted in MPDU mic */
8ebaa5b0
JB
4146 CALL_RXH(ieee80211_rx_h_amsdu);
4147 CALL_RXH(ieee80211_rx_h_data);
f9e124fb
CL
4148
4149 /* special treatment -- needs the queue */
4150 res = ieee80211_rx_h_ctrl(rx, frames);
4151 if (res != RX_CONTINUE)
4152 goto rxh_next;
4153
8ebaa5b0
JB
4154 CALL_RXH(ieee80211_rx_h_mgmt_check);
4155 CALL_RXH(ieee80211_rx_h_action);
4156 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
1ea02224 4157 CALL_RXH(ieee80211_rx_h_action_post_userspace);
8ebaa5b0 4158 CALL_RXH(ieee80211_rx_h_action_return);
09a740ce 4159 CALL_RXH(ieee80211_rx_h_ext);
8ebaa5b0 4160 CALL_RXH(ieee80211_rx_h_mgmt);
49461622 4161
aa0c8636
CL
4162 rxh_next:
4163 ieee80211_rx_handlers_result(rx, res);
f9e124fb 4164
49461622 4165#undef CALL_RXH
aa0c8636 4166 }
24a8fdad 4167
f9e124fb 4168 spin_unlock_bh(&rx->local->rx_path_lock);
aa0c8636
CL
4169}
4170
4406c376 4171static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
aa0c8636 4172{
f9e124fb 4173 struct sk_buff_head reorder_release;
286e6967 4174 ieee80211_rx_result res = RX_DROP;
aa0c8636 4175
f9e124fb
CL
4176 __skb_queue_head_init(&reorder_release);
4177
aa0c8636
CL
4178#define CALL_RXH(rxh) \
4179 do { \
4180 res = rxh(rx); \
4181 if (res != RX_CONTINUE) \
4182 goto rxh_next; \
8ebaa5b0 4183 } while (0)
aa0c8636 4184
8ebaa5b0
JB
4185 CALL_RXH(ieee80211_rx_h_check_dup);
4186 CALL_RXH(ieee80211_rx_h_check);
aa0c8636 4187
f9e124fb 4188 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
aa0c8636 4189
f9e124fb 4190 ieee80211_rx_handlers(rx, &reorder_release);
aa0c8636 4191 return;
49461622 4192
2569a826 4193 rxh_next:
aa0c8636
CL
4194 ieee80211_rx_handlers_result(rx, res);
4195
4196#undef CALL_RXH
58905290
JB
4197}
4198
e66b7920
FF
4199static bool
4200ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
4201{
e66b7920
FF
4202 return !!(sta->valid_links & BIT(link_id));
4203}
4204
4205static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
4206 u8 link_id)
4207{
4208 rx->link_id = link_id;
4209 rx->link = rcu_dereference(rx->sdata->link[link_id]);
4210
4211 if (!rx->sta)
4212 return rx->link;
4213
4214 if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
4215 return false;
4216
4217 rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
4218
4219 return rx->link && rx->link_sta;
4220}
4221
4222static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
0d846bdc 4223 struct sta_info *sta, int link_id)
e66b7920 4224{
e66b7920
FF
4225 rx->link_id = link_id;
4226 rx->sta = sta;
4227
4228 if (sta) {
4229 rx->local = sta->sdata->local;
4230 if (!rx->sdata)
4231 rx->sdata = sta->sdata;
4232 rx->link_sta = &sta->deflink;
ab5f171e
JB
4233 } else {
4234 rx->link_sta = NULL;
e66b7920
FF
4235 }
4236
4237 if (link_id < 0)
4238 rx->link = &rx->sdata->deflink;
4239 else if (!ieee80211_rx_data_set_link(rx, link_id))
4240 return false;
4241
4242 return true;
4243}
4244
2bff8ebf 4245/*
dd318575
JB
4246 * This function makes calls into the RX path, therefore
4247 * it has to be invoked under RCU read lock.
2bff8ebf
CL
4248 */
4249void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
4250{
f9e124fb 4251 struct sk_buff_head frames;
554891e6 4252 struct ieee80211_rx_data rx = {
9e26297a
JB
4253 /* This is OK -- must be QoS data frame */
4254 .security_idx = tid,
4255 .seqno_idx = tid,
554891e6 4256 };
2c15a0cf 4257 struct tid_ampdu_rx *tid_agg_rx;
e66b7920
FF
4258 int link_id = -1;
4259
4260 /* FIXME: statistics won't be right with this */
4261 if (sta->sta.valid_links)
4262 link_id = ffs(sta->sta.valid_links) - 1;
4263
0d846bdc 4264 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
e66b7920 4265 return;
2c15a0cf
CL
4266
4267 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4268 if (!tid_agg_rx)
4269 return;
2bff8ebf 4270
f9e124fb
CL
4271 __skb_queue_head_init(&frames);
4272
2c15a0cf 4273 spin_lock(&tid_agg_rx->reorder_lock);
f9e124fb 4274 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
2c15a0cf 4275 spin_unlock(&tid_agg_rx->reorder_lock);
2bff8ebf 4276
b497de63
EG
4277 if (!skb_queue_empty(&frames)) {
4278 struct ieee80211_event event = {
4279 .type = BA_FRAME_TIMEOUT,
4280 .u.ba.tid = tid,
4281 .u.ba.sta = &sta->sta,
4282 };
4283 drv_event_callback(rx.local, rx.sdata, &event);
4284 }
4285
f9e124fb 4286 ieee80211_rx_handlers(&rx, &frames);
2bff8ebf
CL
4287}
4288
06470f74
SS
4289void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4290 u16 ssn, u64 filtered,
4291 u16 received_mpdus)
4292{
b98c1610 4293 struct ieee80211_local *local;
06470f74
SS
4294 struct sta_info *sta;
4295 struct tid_ampdu_rx *tid_agg_rx;
4296 struct sk_buff_head frames;
4297 struct ieee80211_rx_data rx = {
4298 /* This is OK -- must be QoS data frame */
4299 .security_idx = tid,
4300 .seqno_idx = tid,
4301 };
4302 int i, diff;
4303
4304 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4305 return;
4306
4307 __skb_queue_head_init(&frames);
4308
4309 sta = container_of(pubsta, struct sta_info, sta);
4310
b98c1610
PKS
4311 local = sta->sdata->local;
4312 WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64,
4313 "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n",
4314 local->hw.max_rx_aggregation_subframes);
4315
0d846bdc 4316 if (!ieee80211_rx_data_set_sta(&rx, sta, -1))
e66b7920 4317 return;
06470f74
SS
4318
4319 rcu_read_lock();
4320 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4321 if (!tid_agg_rx)
4322 goto out;
4323
4324 spin_lock_bh(&tid_agg_rx->reorder_lock);
4325
4326 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4327 int release;
4328
4329 /* release all frames in the reorder buffer */
4330 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4331 IEEE80211_SN_MODULO;
4332 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4333 release, &frames);
4334 /* update ssn to match received ssn */
4335 tid_agg_rx->head_seq_num = ssn;
4336 } else {
4337 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4338 &frames);
4339 }
4340
4341 /* handle the case that received ssn is behind the mac ssn.
4342 * it can be tid_agg_rx->buf_size behind and still be valid */
4343 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4344 if (diff >= tid_agg_rx->buf_size) {
4345 tid_agg_rx->reorder_buf_filtered = 0;
4346 goto release;
4347 }
4348 filtered = filtered >> diff;
4349 ssn += diff;
4350
4351 /* update bitmap */
4352 for (i = 0; i < tid_agg_rx->buf_size; i++) {
4353 int index = (ssn + i) % tid_agg_rx->buf_size;
4354
4355 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4356 if (filtered & BIT_ULL(i))
4357 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4358 }
4359
4360 /* now process also frames that the filter marking released */
4361 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4362
4363release:
4364 spin_unlock_bh(&tid_agg_rx->reorder_lock);
4365
4366 ieee80211_rx_handlers(&rx, &frames);
4367
4368 out:
4369 rcu_read_unlock();
4370}
4371EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4372
571ecf67
JB
4373/* main receive path */
4374
7e60096f
JB
4375static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
4376{
4377 return ether_addr_equal(raddr, addr) ||
4378 is_broadcast_ether_addr(raddr);
4379}
4380
a58fbe1a 4381static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
23a24def 4382{
20b01f80 4383 struct ieee80211_sub_if_data *sdata = rx->sdata;
eb9fb5b8 4384 struct sk_buff *skb = rx->skb;
a58fbe1a 4385 struct ieee80211_hdr *hdr = (void *)skb->data;
eb9fb5b8
JB
4386 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4387 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
09a740ce
TP
4388 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4389 ieee80211_is_s1g_beacon(hdr->frame_control);
23a24def 4390
51fb61e7 4391 switch (sdata->vif.type) {
05c914fe 4392 case NL80211_IFTYPE_STATION:
9bc383de 4393 if (!bssid && !sdata->u.mgd.use_4addr)
3c2723f5 4394 return false;
e6f5dcb7
GI
4395 if (ieee80211_is_first_frag(hdr->seq_ctrl) &&
4396 ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
588f7d39 4397 return false;
a58fbe1a
JB
4398 if (multicast)
4399 return true;
892b3bce 4400 return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id);
05c914fe 4401 case NL80211_IFTYPE_ADHOC:
23a24def 4402 if (!bssid)
3c2723f5 4403 return false;
6329b8d9 4404 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
a6555f84
Y
4405 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4406 !is_valid_ether_addr(hdr->addr2))
3c2723f5 4407 return false;
a58fbe1a 4408 if (ieee80211_is_beacon(hdr->frame_control))
3c2723f5 4409 return true;
a58fbe1a 4410 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3c2723f5 4411 return false;
a58fbe1a
JB
4412 if (!multicast &&
4413 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
df140465 4414 return false;
a58fbe1a 4415 if (!rx->sta) {
0fb8ca45 4416 int rate_idx;
da6a4352 4417 if (status->encoding != RX_ENC_LEGACY)
5614618e 4418 rate_idx = 0; /* TODO: HT/VHT rates */
0fb8ca45 4419 else
eb9fb5b8 4420 rate_idx = status->rate_idx;
8bf11d8d
JB
4421 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4422 BIT(rate_idx));
0fb8ca45 4423 }
a58fbe1a 4424 return true;
239281f8
RL
4425 case NL80211_IFTYPE_OCB:
4426 if (!bssid)
4427 return false;
cc117298 4428 if (!ieee80211_is_data_present(hdr->frame_control))
239281f8 4429 return false;
a58fbe1a 4430 if (!is_broadcast_ether_addr(bssid))
239281f8 4431 return false;
a58fbe1a
JB
4432 if (!multicast &&
4433 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
df140465 4434 return false;
d1b1a5eb
JB
4435 /* reject invalid/our STA address */
4436 if (!is_valid_ether_addr(hdr->addr2) ||
4437 ether_addr_equal(sdata->dev->dev_addr, hdr->addr2))
4438 return false;
a58fbe1a 4439 if (!rx->sta) {
239281f8 4440 int rate_idx;
da6a4352 4441 if (status->encoding != RX_ENC_LEGACY)
239281f8
RL
4442 rate_idx = 0; /* TODO: HT rates */
4443 else
4444 rate_idx = status->rate_idx;
4445 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4446 BIT(rate_idx));
4447 }
a58fbe1a 4448 return true;
05c914fe 4449 case NL80211_IFTYPE_MESH_POINT:
736a80bb
JB
4450 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4451 return false;
a58fbe1a
JB
4452 if (multicast)
4453 return true;
4454 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
05c914fe
JB
4455 case NL80211_IFTYPE_AP_VLAN:
4456 case NL80211_IFTYPE_AP:
a58fbe1a 4457 if (!bssid)
892b3bce
JB
4458 return ieee80211_is_our_addr(sdata, hdr->addr1,
4459 &rx->link_id);
a58fbe1a 4460
892b3bce
JB
4461 if (!is_broadcast_ether_addr(bssid) &&
4462 !ieee80211_is_our_addr(sdata, bssid, NULL)) {
3df6eaea
JB
4463 /*
4464 * Accept public action frames even when the
4465 * BSSID doesn't match, this is used for P2P
4466 * and location updates. Note that mac80211
4467 * itself never looks at these frames.
4468 */
2b9ccd4e 4469 if (!multicast &&
892b3bce
JB
4470 !ieee80211_is_our_addr(sdata, hdr->addr1,
4471 &rx->link_id))
3c2723f5 4472 return false;
d48b2968 4473 if (ieee80211_is_public_action(hdr, skb->len))
3c2723f5 4474 return true;
5c90067c 4475 return ieee80211_is_beacon(hdr->frame_control);
a58fbe1a
JB
4476 }
4477
4478 if (!ieee80211_has_tods(hdr->frame_control)) {
db8e1732
AN
4479 /* ignore data frames to TDLS-peers */
4480 if (ieee80211_is_data(hdr->frame_control))
4481 return false;
4482 /* ignore action frames to TDLS-peers */
4483 if (ieee80211_is_action(hdr->frame_control) &&
1ec7bae8 4484 !is_broadcast_ether_addr(bssid) &&
db8e1732
AN
4485 !ether_addr_equal(bssid, hdr->addr1))
4486 return false;
23a24def 4487 }
3018e947
JB
4488
4489 /*
4490 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4491 * the BSSID - we've checked that already but may have accepted
4492 * the wildcard (ff:ff:ff:ff:ff:ff).
4493 *
4494 * It also says:
4495 * The BSSID of the Data frame is determined as follows:
4496 * a) If the STA is contained within an AP or is associated
4497 * with an AP, the BSSID is the address currently in use
4498 * by the STA contained in the AP.
4499 *
4500 * So we should not accept data frames with an address that's
4501 * multicast.
4502 *
4503 * Accepting it also opens a security problem because stations
4504 * could encrypt it with the GTK and inject traffic that way.
4505 */
4506 if (ieee80211_is_data(hdr->frame_control) && multicast)
4507 return false;
4508
a58fbe1a 4509 return true;
f142c6b9 4510 case NL80211_IFTYPE_P2P_DEVICE:
a58fbe1a
JB
4511 return ieee80211_is_public_action(hdr, skb->len) ||
4512 ieee80211_is_probe_req(hdr->frame_control) ||
4513 ieee80211_is_probe_resp(hdr->frame_control) ||
bee404e1
AO
4514 ieee80211_is_beacon(hdr->frame_control) ||
4515 (ieee80211_is_auth(hdr->frame_control) &&
4516 ether_addr_equal(sdata->vif.addr, hdr->addr1));
cb3b7d87
AB
4517 case NL80211_IFTYPE_NAN:
4518 /* Currently no frames on NAN interface are allowed */
4519 return false;
2ca27bcf 4520 default:
fb1c1cd6 4521 break;
23a24def
JB
4522 }
4523
a58fbe1a
JB
4524 WARN_ON_ONCE(1);
4525 return false;
23a24def
JB
4526}
4527
49ddf8e6
JB
4528void ieee80211_check_fast_rx(struct sta_info *sta)
4529{
4530 struct ieee80211_sub_if_data *sdata = sta->sdata;
4531 struct ieee80211_local *local = sdata->local;
4532 struct ieee80211_key *key;
4533 struct ieee80211_fast_rx fastrx = {
4534 .dev = sdata->dev,
4535 .vif_type = sdata->vif.type,
4536 .control_port_protocol = sdata->control_port_protocol,
4537 }, *old, *new = NULL;
3bf9e30e 4538 u32 offload_flags;
80a915ec 4539 bool set_offload = false;
49ddf8e6 4540 bool assign = false;
80a915ec 4541 bool offload;
49ddf8e6
JB
4542
4543 /* use sparse to check that we don't return without updating */
4544 __acquire(check_fast_rx);
4545
4546 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4547 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4548 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4549 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4550
c9c5962b
JB
4551 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4552
49ddf8e6
JB
4553 /* fast-rx doesn't do reordering */
4554 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4555 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4556 goto clear;
4557
4558 switch (sdata->vif.type) {
4559 case NL80211_IFTYPE_STATION:
49ddf8e6
JB
4560 if (sta->sta.tdls) {
4561 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4562 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4563 fastrx.expected_ds_bits = 0;
4564 } else {
49ddf8e6
JB
4565 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4566 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4567 fastrx.expected_ds_bits =
4568 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4569 }
1d870162 4570
9251a473
FF
4571 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4572 fastrx.expected_ds_bits |=
4573 cpu_to_le16(IEEE80211_FCTL_TODS);
4574 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4575 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4576 }
4577
1d870162
FF
4578 if (!sdata->u.mgd.powersave)
4579 break;
4580
4581 /* software powersave is a huge mess, avoid all of it */
4582 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4583 goto clear;
4584 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4585 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4586 goto clear;
49ddf8e6
JB
4587 break;
4588 case NL80211_IFTYPE_AP_VLAN:
4589 case NL80211_IFTYPE_AP:
4590 /* parallel-rx requires this, at least with calls to
4591 * ieee80211_sta_ps_transition()
4592 */
4593 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4594 goto clear;
4595 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4596 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4597 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4598
4599 fastrx.internal_forward =
4600 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4601 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4602 !sdata->u.vlan.sta);
59cae5b9
FF
4603
4604 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4605 sdata->u.vlan.sta) {
4606 fastrx.expected_ds_bits |=
4607 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4608 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4609 fastrx.internal_forward = 0;
4610 }
4611
3468e1e0
FF
4612 break;
4613 case NL80211_IFTYPE_MESH_POINT:
4614 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS |
4615 IEEE80211_FCTL_TODS);
4616 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4617 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
49ddf8e6
JB
4618 break;
4619 default:
4620 goto clear;
4621 }
4622
4623 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4624 goto clear;
4625
4626 rcu_read_lock();
4627 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
622d3b4e
FF
4628 if (!key)
4629 key = rcu_dereference(sdata->default_unicast_key);
49ddf8e6
JB
4630 if (key) {
4631 switch (key->conf.cipher) {
4632 case WLAN_CIPHER_SUITE_TKIP:
4633 /* we don't want to deal with MMIC in fast-rx */
4634 goto clear_rcu;
4635 case WLAN_CIPHER_SUITE_CCMP:
4636 case WLAN_CIPHER_SUITE_CCMP_256:
4637 case WLAN_CIPHER_SUITE_GCMP:
4638 case WLAN_CIPHER_SUITE_GCMP_256:
4639 break;
4640 default:
96fc6efb
AW
4641 /* We also don't want to deal with
4642 * WEP or cipher scheme.
49ddf8e6
JB
4643 */
4644 goto clear_rcu;
4645 }
4646
4647 fastrx.key = true;
4648 fastrx.icv_len = key->conf.icv_len;
4649 }
4650
4651 assign = true;
4652 clear_rcu:
4653 rcu_read_unlock();
4654 clear:
4655 __release(check_fast_rx);
4656
4657 if (assign)
4658 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4659
3bf9e30e
FF
4660 offload_flags = get_bss_sdata(sdata)->vif.offload_flags;
4661 offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED;
80a915ec 4662
3bf9e30e 4663 if (assign && offload)
80a915ec
FF
4664 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4665 else
4666 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4667
4668 if (set_offload)
4669 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4670
49ddf8e6
JB
4671 spin_lock_bh(&sta->lock);
4672 old = rcu_dereference_protected(sta->fast_rx, true);
4673 rcu_assign_pointer(sta->fast_rx, new);
4674 spin_unlock_bh(&sta->lock);
4675
4676 if (old)
4677 kfree_rcu(old, rcu_head);
4678}
4679
4680void ieee80211_clear_fast_rx(struct sta_info *sta)
4681{
4682 struct ieee80211_fast_rx *old;
4683
4684 spin_lock_bh(&sta->lock);
4685 old = rcu_dereference_protected(sta->fast_rx, true);
4686 RCU_INIT_POINTER(sta->fast_rx, NULL);
4687 spin_unlock_bh(&sta->lock);
4688
4689 if (old)
4690 kfree_rcu(old, rcu_head);
4691}
4692
4693void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4694{
4695 struct ieee80211_local *local = sdata->local;
4696 struct sta_info *sta;
4697
4d3acf43 4698 lockdep_assert_wiphy(local->hw.wiphy);
49ddf8e6 4699
253216ff 4700 list_for_each_entry(sta, &local->sta_list, list) {
49ddf8e6
JB
4701 if (sdata != sta->sdata &&
4702 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4703 continue;
4704 ieee80211_check_fast_rx(sta);
4705 }
4706}
4707
4708void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4709{
4710 struct ieee80211_local *local = sdata->local;
4711
4d3acf43
JB
4712 lockdep_assert_wiphy(local->hw.wiphy);
4713
49ddf8e6 4714 __ieee80211_check_fast_rx_iface(sdata);
49ddf8e6
JB
4715}
4716
80a915ec
FF
4717static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4718 struct ieee80211_fast_rx *fast_rx,
4719 int orig_len)
4720{
4721 struct ieee80211_sta_rx_stats *stats;
4722 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4723 struct sta_info *sta = rx->sta;
43635a5a 4724 struct link_sta_info *link_sta;
80a915ec
FF
4725 struct sk_buff *skb = rx->skb;
4726 void *sa = skb->data + ETH_ALEN;
4727 void *da = skb->data;
4728
43635a5a
VT
4729 if (rx->link_id >= 0) {
4730 link_sta = rcu_dereference(sta->link[rx->link_id]);
4731 if (WARN_ON_ONCE(!link_sta)) {
4732 dev_kfree_skb(rx->skb);
4733 return;
4734 }
4735 } else {
4736 link_sta = &sta->deflink;
4737 }
4738
4739 stats = &link_sta->rx_stats;
80a915ec 4740 if (fast_rx->uses_rss)
43635a5a 4741 stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
80a915ec
FF
4742
4743 /* statistics part of ieee80211_rx_h_sta_process() */
4744 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4745 stats->last_signal = status->signal;
4746 if (!fast_rx->uses_rss)
43635a5a 4747 ewma_signal_add(&link_sta->rx_stats_avg.signal,
80a915ec
FF
4748 -status->signal);
4749 }
4750
4751 if (status->chains) {
4752 int i;
4753
4754 stats->chains = status->chains;
4755 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4756 int signal = status->chain_signal[i];
4757
4758 if (!(status->chains & BIT(i)))
4759 continue;
4760
4761 stats->chain_signal_last[i] = signal;
4762 if (!fast_rx->uses_rss)
43635a5a 4763 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
80a915ec
FF
4764 -signal);
4765 }
4766 }
4767 /* end of statistics */
4768
4769 stats->last_rx = jiffies;
4770 stats->last_rate = sta_stats_encode_rate(status);
4771
4772 stats->fragments++;
4773 stats->packets++;
4774
4775 skb->dev = fast_rx->dev;
4776
4777 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4778
4779 /* The seqno index has the same property as needed
4780 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4781 * for non-QoS-data frames. Here we know it's a data
4782 * frame, so count MSDUs.
4783 */
4784 u64_stats_update_begin(&stats->syncp);
4785 stats->msdu[rx->seqno_idx]++;
4786 stats->bytes += orig_len;
4787 u64_stats_update_end(&stats->syncp);
4788
4789 if (fast_rx->internal_forward) {
4790 struct sk_buff *xmit_skb = NULL;
4791 if (is_multicast_ether_addr(da)) {
4792 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4793 } else if (!ether_addr_equal(da, sa) &&
4794 sta_info_get(rx->sdata, da)) {
4795 xmit_skb = skb;
4796 skb = NULL;
4797 }
4798
4799 if (xmit_skb) {
4800 /*
4801 * Send to wireless media and increase priority by 256
4802 * to keep the received priority instead of
4803 * reclassifying the frame (see cfg80211_classify8021d).
4804 */
4805 xmit_skb->priority += 256;
4806 xmit_skb->protocol = htons(ETH_P_802_3);
4807 skb_reset_network_header(xmit_skb);
4808 skb_reset_mac_header(xmit_skb);
4809 dev_queue_xmit(xmit_skb);
4810 }
4811
4812 if (!skb)
4813 return;
4814 }
4815
4816 /* deliver to local stack */
4817 skb->protocol = eth_type_trans(skb, fast_rx->dev);
610d086d 4818 ieee80211_deliver_skb_to_local_stack(skb, rx);
80a915ec
FF
4819}
4820
49ddf8e6
JB
4821static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4822 struct ieee80211_fast_rx *fast_rx)
4823{
4824 struct sk_buff *skb = rx->skb;
4825 struct ieee80211_hdr *hdr = (void *)skb->data;
4826 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3468e1e0 4827 static ieee80211_rx_result res;
49ddf8e6 4828 int orig_len = skb->len;
24bba078
FF
4829 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4830 int snap_offs = hdrlen;
49ddf8e6
JB
4831 struct {
4832 u8 snap[sizeof(rfc1042_header)];
4833 __be16 proto;
4834 } *payload __aligned(2);
4835 struct {
4836 u8 da[ETH_ALEN];
4837 u8 sa[ETH_ALEN];
4838 } addrs __aligned(2);
43635a5a 4839 struct ieee80211_sta_rx_stats *stats;
c9c5962b 4840
49ddf8e6
JB
4841 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4842 * to a common data structure; drivers can implement that per queue
4843 * but we don't have that information in mac80211
4844 */
4845 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4846 return false;
4847
4848#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4849
4850 /* If using encryption, we also need to have:
4851 * - PN_VALIDATED: similar, but the implementation is tricky
4852 * - DECRYPTED: necessary for PN_VALIDATED
4853 */
4854 if (fast_rx->key &&
4855 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4856 return false;
4857
49ddf8e6
JB
4858 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4859 return false;
4860
4861 if (unlikely(ieee80211_is_frag(hdr)))
4862 return false;
4863
4864 /* Since our interface address cannot be multicast, this
4865 * implicitly also rejects multicast frames without the
4866 * explicit check.
4867 *
4868 * We shouldn't get any *data* frames not addressed to us
4869 * (AP mode will accept multicast *management* frames), but
4870 * punting here will make it go through the full checks in
4871 * ieee80211_accept_frame().
4872 */
4873 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4874 return false;
4875
4876 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4877 IEEE80211_FCTL_TODS)) !=
4878 fast_rx->expected_ds_bits)
b323ac19 4879 return false;
49ddf8e6
JB
4880
4881 /* assign the key to drop unencrypted frames (later)
4882 * and strip the IV/MIC if necessary
4883 */
4884 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4885 /* GCMP header length is the same */
4886 snap_offs += IEEE80211_CCMP_HDR_LEN;
4887 }
4888
3468e1e0
FF
4889 if (!ieee80211_vif_is_mesh(&rx->sdata->vif) &&
4890 !(status->rx_flags & IEEE80211_RX_AMSDU)) {
24bba078 4891 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
f5369dcf 4892 return false;
49ddf8e6 4893
24bba078 4894 payload = (void *)(skb->data + snap_offs);
49ddf8e6 4895
24bba078
FF
4896 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4897 return false;
4898
4899 /* Don't handle these here since they require special code.
4900 * Accept AARP and IPX even though they should come with a
4901 * bridge-tunnel header - but if we get them this way then
4902 * there's little point in discarding them.
4903 */
4904 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4905 payload->proto == fast_rx->control_port_protocol))
4906 return false;
4907 }
49ddf8e6
JB
4908
4909 /* after this point, don't punt to the slowpath! */
4910
4911 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4912 pskb_trim(skb, skb->len - fast_rx->icv_len))
4913 goto drop;
4914
49ddf8e6
JB
4915 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4916 goto drop;
4917
24bba078
FF
4918 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4919 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4920 RX_QUEUED)
4921 goto drop;
4922
4923 return true;
4924 }
4925
49ddf8e6
JB
4926 /* do the header conversion - first grab the addresses */
4927 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4928 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
3468e1e0
FF
4929 if (ieee80211_vif_is_mesh(&rx->sdata->vif)) {
4930 skb_pull(skb, snap_offs - 2);
4931 put_unaligned_be16(skb->len - 2, skb->data);
4932 } else {
4933 skb_postpull_rcsum(skb, skb->data + snap_offs,
4934 sizeof(rfc1042_header) + 2);
4935
4936 /* remove the SNAP but leave the ethertype */
4937 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4938 }
49ddf8e6
JB
4939 /* push the addresses in front */
4940 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4941
3468e1e0
FF
4942 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
4943 switch (res) {
4944 case RX_QUEUED:
4945 return true;
4946 case RX_CONTINUE:
4947 break;
4948 default:
4949 goto drop;
4950 }
4951
80a915ec 4952 ieee80211_rx_8023(rx, fast_rx, orig_len);
49ddf8e6
JB
4953
4954 return true;
4955 drop:
4956 dev_kfree_skb(skb);
43635a5a 4957
80a915ec 4958 if (fast_rx->uses_rss)
e66b7920 4959 stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
43635a5a 4960 else
e66b7920 4961 stats = &rx->link_sta->rx_stats;
80a915ec 4962
c9c5962b 4963 stats->dropped++;
49ddf8e6
JB
4964 return true;
4965}
4966
4406c376
JB
4967/*
4968 * This function returns whether or not the SKB
4969 * was destined for RX processing or not, which,
4970 * if consume is true, is equivalent to whether
4971 * or not the skb was consumed.
4972 */
4973static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4974 struct sk_buff *skb, bool consume)
4975{
4976 struct ieee80211_local *local = rx->local;
4977 struct ieee80211_sub_if_data *sdata = rx->sdata;
42fb9148 4978 struct ieee80211_hdr *hdr = (void *)skb->data;
e66b7920
FF
4979 struct link_sta_info *link_sta = rx->link_sta;
4980 struct ieee80211_link_data *link = rx->link;
4406c376
JB
4981
4982 rx->skb = skb;
4406c376 4983
49ddf8e6
JB
4984 /* See if we can do fast-rx; if we have to copy we already lost,
4985 * so punt in that case. We should never have to deliver a data
4986 * frame to multiple interfaces anyway.
4987 *
4988 * We skip the ieee80211_accept_frame() call and do the necessary
4989 * checking inside ieee80211_invoke_fast_rx().
4990 */
4991 if (consume && rx->sta) {
4992 struct ieee80211_fast_rx *fast_rx;
4993
4994 fast_rx = rcu_dereference(rx->sta->fast_rx);
4995 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4996 return true;
4997 }
4998
a58fbe1a 4999 if (!ieee80211_accept_frame(rx))
4406c376
JB
5000 return false;
5001
4406c376 5002 if (!consume) {
f9202638
AS
5003 struct skb_shared_hwtstamps *shwt;
5004
5005 rx->skb = skb_copy(skb, GFP_ATOMIC);
5006 if (!rx->skb) {
4406c376
JB
5007 if (net_ratelimit())
5008 wiphy_debug(local->hw.wiphy,
b305dae4 5009 "failed to copy skb for %s\n",
4406c376
JB
5010 sdata->name);
5011 return true;
5012 }
5013
f9202638
AS
5014 /* skb_copy() does not copy the hw timestamps, so copy it
5015 * explicitly
5016 */
5017 shwt = skb_hwtstamps(rx->skb);
5018 shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
fa22b51a
S
5019
5020 /* Update the hdr pointer to the new skb for translation below */
5021 hdr = (struct ieee80211_hdr *)rx->skb->data;
4406c376
JB
5022 }
5023
daf8fb42 5024 if (unlikely(rx->sta && rx->sta->sta.mlo) &&
47c171a4
JB
5025 is_unicast_ether_addr(hdr->addr1) &&
5026 !ieee80211_is_probe_resp(hdr->frame_control) &&
5027 !ieee80211_is_beacon(hdr->frame_control)) {
42fb9148
JB
5028 /* translate to MLD addresses */
5029 if (ether_addr_equal(link->conf->addr, hdr->addr1))
5030 ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
5031 if (ether_addr_equal(link_sta->addr, hdr->addr2))
5032 ether_addr_copy(hdr->addr2, rx->sta->addr);
1f638944
JB
5033 /* translate A3 only if it's the BSSID */
5034 if (!ieee80211_has_tods(hdr->frame_control) &&
5035 !ieee80211_has_fromds(hdr->frame_control)) {
5036 if (ether_addr_equal(link_sta->addr, hdr->addr3))
5037 ether_addr_copy(hdr->addr3, rx->sta->addr);
5038 else if (ether_addr_equal(link->conf->addr, hdr->addr3))
5039 ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
5040 }
42fb9148
JB
5041 /* not needed for A4 since it can only carry the SA */
5042 }
5043
4406c376
JB
5044 ieee80211_invoke_rx_handlers(rx);
5045 return true;
5046}
5047
80a915ec
FF
5048static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
5049 struct ieee80211_sta *pubsta,
5050 struct sk_buff *skb,
5051 struct list_head *list)
5052{
5053 struct ieee80211_local *local = hw_to_local(hw);
ea9d807b 5054 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
80a915ec
FF
5055 struct ieee80211_fast_rx *fast_rx;
5056 struct ieee80211_rx_data rx;
0d846bdc 5057 struct sta_info *sta;
e66b7920 5058 int link_id = -1;
80a915ec
FF
5059
5060 memset(&rx, 0, sizeof(rx));
5061 rx.skb = skb;
5062 rx.local = local;
5063 rx.list = list;
892b3bce 5064 rx.link_id = -1;
80a915ec
FF
5065
5066 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5067
5068 /* drop frame if too short for header */
5069 if (skb->len < sizeof(struct ethhdr))
5070 goto drop;
5071
5072 if (!pubsta)
5073 goto drop;
5074
e66b7920
FF
5075 if (status->link_valid)
5076 link_id = status->link_id;
ea9d807b
VT
5077
5078 /*
5079 * TODO: Should the frame be dropped if the right link_id is not
5080 * available? Or may be it is fine in the current form to proceed with
5081 * the frame processing because with frame being in 802.3 format,
5082 * link_id is used only for stats purpose and updating the stats on
5083 * the deflink is fine?
5084 */
0d846bdc
JB
5085 sta = container_of(pubsta, struct sta_info, sta);
5086 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
e66b7920 5087 goto drop;
80a915ec
FF
5088
5089 fast_rx = rcu_dereference(rx.sta->fast_rx);
5090 if (!fast_rx)
5091 goto drop;
5092
5093 ieee80211_rx_8023(&rx, fast_rx, skb->len);
5094 return;
5095
5096drop:
5097 dev_kfree_skb(skb);
5098}
5099
892b3bce
JB
5100static bool ieee80211_rx_for_interface(struct ieee80211_rx_data *rx,
5101 struct sk_buff *skb, bool consume)
5102{
5103 struct link_sta_info *link_sta;
5104 struct ieee80211_hdr *hdr = (void *)skb->data;
e66b7920
FF
5105 struct sta_info *sta;
5106 int link_id = -1;
892b3bce
JB
5107
5108 /*
5109 * Look up link station first, in case there's a
5110 * chance that they might have a link address that
5111 * is identical to the MLD address, that way we'll
5112 * have the link information if needed.
5113 */
5114 link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
5115 if (link_sta) {
e66b7920
FF
5116 sta = link_sta->sta;
5117 link_id = link_sta->link_id;
892b3bce 5118 } else {
ea9d807b
VT
5119 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5120
e66b7920
FF
5121 sta = sta_info_get_bss(rx->sdata, hdr->addr2);
5122 if (status->link_valid)
5123 link_id = status->link_id;
892b3bce
JB
5124 }
5125
0d846bdc 5126 if (!ieee80211_rx_data_set_sta(rx, sta, link_id))
e66b7920
FF
5127 return false;
5128
892b3bce
JB
5129 return ieee80211_prepare_and_rx_handle(rx, skb, consume);
5130}
5131
571ecf67 5132/*
6b59db7d 5133 * This is the actual Rx frames handler. as it belongs to Rx path it must
6368e4b1 5134 * be called with rcu_read_lock protection.
571ecf67 5135 */
71ebb4aa 5136static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
d63b548f 5137 struct ieee80211_sta *pubsta,
af9f9b22 5138 struct sk_buff *skb,
c5d1686b 5139 struct list_head *list)
571ecf67
JB
5140{
5141 struct ieee80211_local *local = hw_to_local(hw);
ea9d807b 5142 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
571ecf67 5143 struct ieee80211_sub_if_data *sdata;
571ecf67 5144 struct ieee80211_hdr *hdr;
e3cf8b3f 5145 __le16 fc;
5cf121c3 5146 struct ieee80211_rx_data rx;
4406c376 5147 struct ieee80211_sub_if_data *prev;
83e7e4ce 5148 struct rhlist_head *tmp;
e3cf8b3f 5149 int err = 0;
571ecf67 5150
e3cf8b3f 5151 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
571ecf67
JB
5152 memset(&rx, 0, sizeof(rx));
5153 rx.skb = skb;
5154 rx.local = local;
c5d1686b 5155 rx.list = list;
892b3bce 5156 rx.link_id = -1;
72abd81b 5157
e3cf8b3f 5158 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
c206ca67 5159 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
571ecf67 5160
4a4f1a58
JB
5161 if (ieee80211_is_mgmt(fc)) {
5162 /* drop frame if too short for header */
5163 if (skb->len < ieee80211_hdrlen(fc))
5164 err = -ENOBUFS;
5165 else
5166 err = skb_linearize(skb);
5167 } else {
e3cf8b3f 5168 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4a4f1a58 5169 }
e3cf8b3f
ZY
5170
5171 if (err) {
5172 dev_kfree_skb(skb);
5173 return;
5174 }
5175
5176 hdr = (struct ieee80211_hdr *)skb->data;
38f3714d 5177 ieee80211_parse_qos(&rx);
d1c3a37c 5178 ieee80211_verify_alignment(&rx);
38f3714d 5179
d48b2968 5180 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
cd418ba6
TP
5181 ieee80211_is_beacon(hdr->frame_control) ||
5182 ieee80211_is_s1g_beacon(hdr->frame_control)))
d48b2968
JB
5183 ieee80211_scan_rx(local, skb);
5184
19d19e96 5185 if (ieee80211_is_data(fc)) {
d63b548f 5186 struct sta_info *sta, *prev_sta;
e66b7920 5187 int link_id = -1;
7bedd0cf 5188
e66b7920
FF
5189 if (status->link_valid)
5190 link_id = status->link_id;
ea9d807b 5191
e66b7920 5192 if (pubsta) {
0d846bdc
JB
5193 sta = container_of(pubsta, struct sta_info, sta);
5194 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
ea9d807b
VT
5195 goto out;
5196
ea9d807b
VT
5197 /*
5198 * In MLO connection, fetch the link_id using addr2
5199 * when the driver does not pass link_id in status.
5200 * When the address translation is already performed by
5201 * driver/hw, the valid link_id must be passed in
5202 * status.
5203 */
5204
5205 if (!status->link_valid && pubsta->mlo) {
ea9d807b
VT
5206 struct link_sta_info *link_sta;
5207
5208 link_sta = link_sta_info_get_bss(rx.sdata,
5209 hdr->addr2);
5210 if (!link_sta)
5211 goto out;
5212
e66b7920 5213 ieee80211_rx_data_set_link(&rx, link_sta->link_id);
ea9d807b
VT
5214 }
5215
19d19e96
JB
5216 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5217 return;
5218 goto out;
5219 }
5220
56af3268 5221 prev_sta = NULL;
4406c376 5222
83e7e4ce 5223 for_each_sta_info(local, hdr->addr2, sta, tmp) {
56af3268
BG
5224 if (!prev_sta) {
5225 prev_sta = sta;
5226 continue;
5227 }
5228
e66b7920 5229 rx.sdata = prev_sta->sdata;
0d846bdc 5230 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
e66b7920
FF
5231 goto out;
5232
5233 if (!status->link_valid && prev_sta->sta.mlo)
ea9d807b
VT
5234 continue;
5235
4406c376 5236 ieee80211_prepare_and_rx_handle(&rx, skb, false);
abe60632 5237
56af3268 5238 prev_sta = sta;
4406c376 5239 }
56af3268
BG
5240
5241 if (prev_sta) {
e66b7920 5242 rx.sdata = prev_sta->sdata;
0d846bdc 5243 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
ea9d807b
VT
5244 goto out;
5245
e66b7920
FF
5246 if (!status->link_valid && prev_sta->sta.mlo)
5247 goto out;
56af3268 5248
4406c376 5249 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
56af3268 5250 return;
8e26d5ad 5251 goto out;
56af3268 5252 }
4406c376
JB
5253 }
5254
4b0dd98e 5255 prev = NULL;
b2e7771e 5256
4b0dd98e
JB
5257 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
5258 if (!ieee80211_sdata_running(sdata))
5259 continue;
340e11f3 5260
4b0dd98e
JB
5261 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
5262 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5263 continue;
340e11f3 5264
4b0dd98e
JB
5265 /*
5266 * frame is destined for this interface, but if it's
5267 * not also for the previous one we handle that after
5268 * the loop to avoid copying the SKB once too much
5269 */
4bb29f8c 5270
4b0dd98e 5271 if (!prev) {
abe60632 5272 prev = sdata;
4b0dd98e 5273 continue;
340e11f3 5274 }
4bb29f8c 5275
4b0dd98e 5276 rx.sdata = prev;
892b3bce 5277 ieee80211_rx_for_interface(&rx, skb, false);
4bb29f8c 5278
4b0dd98e
JB
5279 prev = sdata;
5280 }
5281
5282 if (prev) {
4b0dd98e 5283 rx.sdata = prev;
4406c376 5284
892b3bce 5285 if (ieee80211_rx_for_interface(&rx, skb, true))
4b0dd98e 5286 return;
571ecf67 5287 }
4406c376 5288
8e26d5ad 5289 out:
4406c376 5290 dev_kfree_skb(skb);
571ecf67 5291}
6368e4b1
RR
5292
5293/*
5294 * This is the receive path handler. It is called by a low level driver when an
5295 * 802.11 MPDU is received from the hardware.
5296 */
c5d1686b
FF
5297void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5298 struct sk_buff *skb, struct list_head *list)
6368e4b1
RR
5299{
5300 struct ieee80211_local *local = hw_to_local(hw);
8318d78a
JB
5301 struct ieee80211_rate *rate = NULL;
5302 struct ieee80211_supported_band *sband;
f1d58c25 5303 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
30f6cf96 5304 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8318d78a 5305
d20ef63d
JB
5306 WARN_ON_ONCE(softirq_count() == 0);
5307
57fbcce3 5308 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
77a980dc 5309 goto drop;
8318d78a
JB
5310
5311 sband = local->hw.wiphy->bands[status->band];
77a980dc
JB
5312 if (WARN_ON(!sband))
5313 goto drop;
8318d78a 5314
89c3a8ac
JB
5315 /*
5316 * If we're suspending, it is possible although not too likely
5317 * that we'd be receiving frames after having already partially
5318 * quiesced the stack. We can't process such frames then since
5319 * that might, for example, cause stations to be added or other
5320 * driver callbacks be invoked.
5321 */
77a980dc
JB
5322 if (unlikely(local->quiescing || local->suspended))
5323 goto drop;
89c3a8ac 5324
04800ada
AN
5325 /* We might be during a HW reconfig, prevent Rx for the same reason */
5326 if (unlikely(local->in_reconfig))
5327 goto drop;
5328
ea77f12f
JB
5329 /*
5330 * The same happens when we're not even started,
5331 * but that's worth a warning.
5332 */
77a980dc
JB
5333 if (WARN_ON(!local->started))
5334 goto drop;
ea77f12f 5335
fc885189 5336 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
e5d6eb83 5337 /*
fc885189
JB
5338 * Validate the rate, unless a PLCP error means that
5339 * we probably can't have a valid rate here anyway.
e5d6eb83 5340 */
fc885189 5341
da6a4352
JB
5342 switch (status->encoding) {
5343 case RX_ENC_HT:
fc885189
JB
5344 /*
5345 * rate_idx is MCS index, which can be [0-76]
5346 * as documented on:
5347 *
59d4bfc1 5348 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
fc885189
JB
5349 *
5350 * Anything else would be some sort of driver or
5351 * hardware error. The driver should catch hardware
5352 * errors.
5353 */
444e3803 5354 if (WARN(status->rate_idx > 76,
fc885189
JB
5355 "Rate marked as an HT rate but passed "
5356 "status->rate_idx is not "
5357 "an MCS index [0-76]: %d (0x%02x)\n",
5358 status->rate_idx,
5359 status->rate_idx))
5360 goto drop;
da6a4352
JB
5361 break;
5362 case RX_ENC_VHT:
04be6d33 5363 if (WARN_ONCE(status->rate_idx > 11 ||
8613c948
JB
5364 !status->nss ||
5365 status->nss > 8,
5614618e 5366 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
8613c948 5367 status->rate_idx, status->nss))
5614618e 5368 goto drop;
da6a4352 5369 break;
41cbb0f5
LC
5370 case RX_ENC_HE:
5371 if (WARN_ONCE(status->rate_idx > 11 ||
5372 !status->nss ||
5373 status->nss > 8,
5374 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
5375 status->rate_idx, status->nss))
5376 goto drop;
5377 break;
f66c48af
JB
5378 case RX_ENC_EHT:
5379 if (WARN_ONCE(status->rate_idx > 15 ||
5380 !status->nss ||
5381 status->nss > 8 ||
5382 status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5383 "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5384 status->rate_idx, status->nss, status->eht.gi))
5385 goto drop;
5386 break;
da6a4352
JB
5387 default:
5388 WARN_ON_ONCE(1);
fc0561dc 5389 fallthrough;
da6a4352 5390 case RX_ENC_LEGACY:
444e3803 5391 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
fc885189
JB
5392 goto drop;
5393 rate = &sband->bitrates[status->rate_idx];
5394 }
0fb8ca45 5395 }
6368e4b1 5396
ea9d807b
VT
5397 if (WARN_ON_ONCE(status->link_id >= IEEE80211_LINK_UNSPECIFIED))
5398 goto drop;
5399
554891e6
JB
5400 status->rx_flags = 0;
5401
261e411b
AN
5402 kcov_remote_start_common(skb_get_kcov_handle(skb));
5403
6368e4b1
RR
5404 /*
5405 * Frames with failed FCS/PLCP checksum are not returned,
5406 * all other frames are returned without radiotap header
5407 * if it was previously present.
5408 * Also, frames with less than 16 bytes are dropped.
5409 */
80a915ec
FF
5410 if (!(status->flag & RX_FLAG_8023))
5411 skb = ieee80211_rx_monitor(local, skb, rate);
261e411b 5412 if (skb) {
30f6cf96
FF
5413 if ((status->flag & RX_FLAG_8023) ||
5414 ieee80211_is_data_present(hdr->frame_control))
5415 ieee80211_tpt_led_trig_rx(local, skb->len);
d63b548f 5416
80a915ec
FF
5417 if (status->flag & RX_FLAG_8023)
5418 __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
5419 else
5420 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
261e411b 5421 }
77a980dc 5422
261e411b 5423 kcov_remote_stop();
77a980dc
JB
5424 return;
5425 drop:
5426 kfree_skb(skb);
6368e4b1 5427}
c5d1686b
FF
5428EXPORT_SYMBOL(ieee80211_rx_list);
5429
5430void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5431 struct sk_buff *skb, struct napi_struct *napi)
5432{
5433 struct sk_buff *tmp;
5434 LIST_HEAD(list);
5435
5436
5437 /*
5438 * key references and virtual interfaces are protected using RCU
5439 * and this requires that we are in a read-side RCU section during
5440 * receive processing
5441 */
5442 rcu_read_lock();
5443 ieee80211_rx_list(hw, pubsta, skb, &list);
5444 rcu_read_unlock();
5445
5446 if (!napi) {
5447 netif_receive_skb_list(&list);
5448 return;
5449 }
5450
5451 list_for_each_entry_safe(skb, tmp, &list, list) {
5452 skb_list_del_init(skb);
5453 napi_gro_receive(napi, skb);
5454 }
5455}
af9f9b22 5456EXPORT_SYMBOL(ieee80211_rx_napi);
571ecf67
JB
5457
5458/* This is a version of the rx handler that can be called from hard irq
5459 * context. Post the skb on the queue and schedule the tasklet */
f1d58c25 5460void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
571ecf67
JB
5461{
5462 struct ieee80211_local *local = hw_to_local(hw);
5463
5464 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5465
571ecf67
JB
5466 skb->pkt_type = IEEE80211_RX_MSG;
5467 skb_queue_tail(&local->skb_queue, skb);
5468 tasklet_schedule(&local->tasklet);
5469}
5470EXPORT_SYMBOL(ieee80211_rx_irqsafe);