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