]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/rx.c
mac80211: fix comment referring to RX queue
[mirror_ubuntu-bionic-kernel.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
571ecf67
JB
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
ab46623e 13#include <linux/jiffies.h>
5a0e3ad6 14#include <linux/slab.h>
571ecf67
JB
15#include <linux/kernel.h>
16#include <linux/skbuff.h>
17#include <linux/netdevice.h>
18#include <linux/etherdevice.h>
d4e46a3d 19#include <linux/rcupdate.h>
bc3b2d7f 20#include <linux/export.h>
571ecf67
JB
21#include <net/mac80211.h>
22#include <net/ieee80211_radiotap.h>
d26ad377 23#include <asm/unaligned.h>
571ecf67
JB
24
25#include "ieee80211_i.h"
24487981 26#include "driver-ops.h"
2c8dccc7 27#include "led.h"
33b64eb2 28#include "mesh.h"
571ecf67
JB
29#include "wep.h"
30#include "wpa.h"
31#include "tkip.h"
32#include "wme.h"
1d8d3dec 33#include "rate.h"
571ecf67 34
5a490510
JB
35static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
36{
37 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
38
39 u64_stats_update_begin(&tstats->syncp);
40 tstats->rx_packets++;
41 tstats->rx_bytes += len;
42 u64_stats_update_end(&tstats->syncp);
43}
44
a6828493
JB
45static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
46 enum nl80211_iftype type)
47{
48 __le16 fc = hdr->frame_control;
49
50 if (ieee80211_is_data(fc)) {
51 if (len < 24) /* drop incorrect hdr len (data) */
52 return NULL;
53
54 if (ieee80211_has_a4(fc))
55 return NULL;
56 if (ieee80211_has_tods(fc))
57 return hdr->addr1;
58 if (ieee80211_has_fromds(fc))
59 return hdr->addr2;
60
61 return hdr->addr3;
62 }
63
64 if (ieee80211_is_mgmt(fc)) {
65 if (len < 24) /* drop incorrect hdr len (mgmt) */
66 return NULL;
67 return hdr->addr3;
68 }
69
70 if (ieee80211_is_ctl(fc)) {
71 if (ieee80211_is_pspoll(fc))
72 return hdr->addr1;
73
74 if (ieee80211_is_back_req(fc)) {
75 switch (type) {
76 case NL80211_IFTYPE_STATION:
77 return hdr->addr2;
78 case NL80211_IFTYPE_AP:
79 case NL80211_IFTYPE_AP_VLAN:
80 return hdr->addr1;
81 default:
82 break; /* fall through to the return */
83 }
84 }
85 }
86
87 return NULL;
88}
89
b2e7771e
JB
90/*
91 * monitor mode reception
92 *
93 * This function cleans up the SKB, i.e. it removes all the stuff
94 * only useful for monitoring.
95 */
96static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
1f7bba79
JB
97 struct sk_buff *skb,
98 unsigned int rtap_vendor_space)
b2e7771e 99{
30686bf7 100 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
b2e7771e 101 if (likely(skb->len > FCS_LEN))
e3cf8b3f 102 __pskb_trim(skb, skb->len - FCS_LEN);
b2e7771e
JB
103 else {
104 /* driver bug */
105 WARN_ON(1);
106 dev_kfree_skb(skb);
0624760c 107 return NULL;
b2e7771e
JB
108 }
109 }
110
1f7bba79
JB
111 __pskb_pull(skb, rtap_vendor_space);
112
b2e7771e
JB
113 return skb;
114}
115
1f7bba79
JB
116static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
117 unsigned int rtap_vendor_space)
b2e7771e 118{
f1d58c25 119 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1f7bba79
JB
120 struct ieee80211_hdr *hdr;
121
122 hdr = (void *)(skb->data + rtap_vendor_space);
b2e7771e 123
4c298677
JB
124 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
125 RX_FLAG_FAILED_PLCP_CRC |
126 RX_FLAG_AMPDU_IS_ZEROLEN))
6b59db7d
ZG
127 return true;
128
1f7bba79 129 if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
6b59db7d
ZG
130 return true;
131
87228f57
HH
132 if (ieee80211_is_ctl(hdr->frame_control) &&
133 !ieee80211_is_pspoll(hdr->frame_control) &&
134 !ieee80211_is_back_req(hdr->frame_control))
6b59db7d
ZG
135 return true;
136
137 return false;
b2e7771e
JB
138}
139
601ae7f2 140static int
1f7bba79
JB
141ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
142 struct ieee80211_rx_status *status,
143 struct sk_buff *skb)
601ae7f2
BR
144{
145 int len;
146
147 /* always present fields */
a144f378 148 len = sizeof(struct ieee80211_radiotap_header) + 8;
601ae7f2 149
a144f378 150 /* allocate extra bitmaps */
a144f378
JB
151 if (status->chains)
152 len += 4 * hweight8(status->chains);
90b9e446
JB
153
154 if (ieee80211_have_rx_timestamp(status)) {
155 len = ALIGN(len, 8);
601ae7f2 156 len += 8;
90b9e446 157 }
30686bf7 158 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
601ae7f2 159 len += 1;
601ae7f2 160
a144f378
JB
161 /* antenna field, if we don't have per-chain info */
162 if (!status->chains)
163 len += 1;
164
90b9e446
JB
165 /* padding for RX_FLAGS if necessary */
166 len = ALIGN(len, 2);
601ae7f2 167
6d744bac
JB
168 if (status->flag & RX_FLAG_HT) /* HT info */
169 len += 3;
170
4c298677 171 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
90b9e446 172 len = ALIGN(len, 4);
4c298677
JB
173 len += 8;
174 }
175
51648921
JB
176 if (status->flag & RX_FLAG_VHT) {
177 len = ALIGN(len, 2);
178 len += 12;
179 }
180
a144f378
JB
181 if (status->chains) {
182 /* antenna and antenna signal fields */
183 len += 2 * hweight8(status->chains);
184 }
185
1f7bba79
JB
186 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
187 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
188
189 /* vendor presence bitmap */
190 len += 4;
191 /* alignment for fixed 6-byte vendor data header */
192 len = ALIGN(len, 2);
193 /* vendor data header */
194 len += 6;
195 if (WARN_ON(rtap->align == 0))
196 rtap->align = 1;
197 len = ALIGN(len, rtap->align);
198 len += rtap->len + rtap->pad;
199 }
200
601ae7f2
BR
201 return len;
202}
203
00ea6deb 204/*
601ae7f2
BR
205 * ieee80211_add_rx_radiotap_header - add radiotap header
206 *
207 * add a radiotap header containing all the fields which the hardware provided.
208 */
209static void
210ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
211 struct sk_buff *skb,
601ae7f2 212 struct ieee80211_rate *rate,
973ef21a 213 int rtap_len, bool has_fcs)
601ae7f2 214{
f1d58c25 215 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
601ae7f2
BR
216 struct ieee80211_radiotap_header *rthdr;
217 unsigned char *pos;
a144f378
JB
218 __le32 *it_present;
219 u32 it_present_val;
6a86b9c7 220 u16 rx_flags = 0;
a5e70697 221 u16 channel_flags = 0;
a144f378
JB
222 int mpdulen, chain;
223 unsigned long chains = status->chains;
1f7bba79
JB
224 struct ieee80211_vendor_radiotap rtap = {};
225
226 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
227 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
228 /* rtap.len and rtap.pad are undone immediately */
229 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
230 }
f4bda337
TP
231
232 mpdulen = skb->len;
30686bf7 233 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
f4bda337 234 mpdulen += FCS_LEN;
601ae7f2
BR
235
236 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
1f7bba79 237 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
a144f378 238 it_present = &rthdr->it_present;
601ae7f2
BR
239
240 /* radiotap header, set always present flags */
0059b2b1 241 rthdr->it_len = cpu_to_le16(rtap_len);
a144f378
JB
242 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
243 BIT(IEEE80211_RADIOTAP_CHANNEL) |
244 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
245
246 if (!status->chains)
247 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
248
249 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
250 it_present_val |=
251 BIT(IEEE80211_RADIOTAP_EXT) |
252 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
253 put_unaligned_le32(it_present_val, it_present);
254 it_present++;
255 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
256 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
257 }
601ae7f2 258
1f7bba79
JB
259 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
260 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
261 BIT(IEEE80211_RADIOTAP_EXT);
262 put_unaligned_le32(it_present_val, it_present);
263 it_present++;
264 it_present_val = rtap.present;
265 }
266
a144f378
JB
267 put_unaligned_le32(it_present_val, it_present);
268
269 pos = (void *)(it_present + 1);
270
601ae7f2
BR
271 /* the order of the following fields is important */
272
273 /* IEEE80211_RADIOTAP_TSFT */
f4bda337 274 if (ieee80211_have_rx_timestamp(status)) {
90b9e446
JB
275 /* padding */
276 while ((pos - (u8 *)rthdr) & 7)
277 *pos++ = 0;
f4bda337
TP
278 put_unaligned_le64(
279 ieee80211_calculate_rx_timestamp(local, status,
280 mpdulen, 0),
281 pos);
1df332e8 282 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
601ae7f2
BR
283 pos += 8;
284 }
285
286 /* IEEE80211_RADIOTAP_FLAGS */
30686bf7 287 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
601ae7f2 288 *pos |= IEEE80211_RADIOTAP_F_FCS;
aae89831
JB
289 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
290 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
b4f28bbb
BR
291 if (status->flag & RX_FLAG_SHORTPRE)
292 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
601ae7f2
BR
293 pos++;
294
295 /* IEEE80211_RADIOTAP_RATE */
5614618e 296 if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
0fb8ca45 297 /*
f8d1ccf1 298 * Without rate information don't add it. If we have,
38f37be2 299 * MCS information is a separate field in radiotap,
73b48099
JB
300 * added below. The byte here is needed as padding
301 * for the channel though, so initialise it to 0.
0fb8ca45
JM
302 */
303 *pos = 0;
8d6f658e 304 } else {
2103dec1 305 int shift = 0;
ebe6c7ba 306 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2103dec1
SW
307 if (status->flag & RX_FLAG_10MHZ)
308 shift = 1;
309 else if (status->flag & RX_FLAG_5MHZ)
310 shift = 2;
311 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
8d6f658e 312 }
601ae7f2
BR
313 pos++;
314
315 /* IEEE80211_RADIOTAP_CHANNEL */
6a86b9c7 316 put_unaligned_le16(status->freq, pos);
601ae7f2 317 pos += 2;
a5e70697
SW
318 if (status->flag & RX_FLAG_10MHZ)
319 channel_flags |= IEEE80211_CHAN_HALF;
320 else if (status->flag & RX_FLAG_5MHZ)
321 channel_flags |= IEEE80211_CHAN_QUARTER;
322
601ae7f2 323 if (status->band == IEEE80211_BAND_5GHZ)
a5e70697 324 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
5614618e 325 else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
a5e70697 326 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
f8d1ccf1 327 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
a5e70697 328 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
f8d1ccf1 329 else if (rate)
3a5c5e81 330 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
f8d1ccf1 331 else
a5e70697
SW
332 channel_flags |= IEEE80211_CHAN_2GHZ;
333 put_unaligned_le16(channel_flags, pos);
601ae7f2
BR
334 pos += 2;
335
336 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
30686bf7 337 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
fe8431f8 338 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
601ae7f2
BR
339 *pos = status->signal;
340 rthdr->it_present |=
341 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
342 pos++;
343 }
344
601ae7f2
BR
345 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
346
a144f378
JB
347 if (!status->chains) {
348 /* IEEE80211_RADIOTAP_ANTENNA */
349 *pos = status->antenna;
350 pos++;
351 }
601ae7f2 352
601ae7f2
BR
353 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
354
355 /* IEEE80211_RADIOTAP_RX_FLAGS */
356 /* ensure 2 byte alignment for the 2 byte field as required */
6a86b9c7 357 if ((pos - (u8 *)rthdr) & 1)
90b9e446 358 *pos++ = 0;
aae89831 359 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
6a86b9c7
JB
360 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
361 put_unaligned_le16(rx_flags, pos);
601ae7f2 362 pos += 2;
6d744bac
JB
363
364 if (status->flag & RX_FLAG_HT) {
786677d1
OR
365 unsigned int stbc;
366
6d744bac 367 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
ac55d2fe 368 *pos++ = local->hw.radiotap_mcs_details;
6d744bac
JB
369 *pos = 0;
370 if (status->flag & RX_FLAG_SHORT_GI)
371 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
372 if (status->flag & RX_FLAG_40MHZ)
373 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
ac55d2fe
JB
374 if (status->flag & RX_FLAG_HT_GF)
375 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
63c361f5
EG
376 if (status->flag & RX_FLAG_LDPC)
377 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
786677d1
OR
378 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
379 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
6d744bac
JB
380 pos++;
381 *pos++ = status->rate_idx;
382 }
4c298677
JB
383
384 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
385 u16 flags = 0;
386
387 /* ensure 4 byte alignment */
388 while ((pos - (u8 *)rthdr) & 3)
389 pos++;
390 rthdr->it_present |=
391 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
392 put_unaligned_le32(status->ampdu_reference, pos);
393 pos += 4;
394 if (status->flag & RX_FLAG_AMPDU_REPORT_ZEROLEN)
395 flags |= IEEE80211_RADIOTAP_AMPDU_REPORT_ZEROLEN;
396 if (status->flag & RX_FLAG_AMPDU_IS_ZEROLEN)
397 flags |= IEEE80211_RADIOTAP_AMPDU_IS_ZEROLEN;
398 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
399 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
400 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
401 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
402 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
403 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
404 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
405 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
406 put_unaligned_le16(flags, pos);
407 pos += 2;
408 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
409 *pos++ = status->ampdu_delimiter_crc;
410 else
411 *pos++ = 0;
412 *pos++ = 0;
413 }
90b9e446 414
51648921
JB
415 if (status->flag & RX_FLAG_VHT) {
416 u16 known = local->hw.radiotap_vht_details;
417
418 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
51648921
JB
419 put_unaligned_le16(known, pos);
420 pos += 2;
421 /* flags */
422 if (status->flag & RX_FLAG_SHORT_GI)
423 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
63c361f5
EG
424 /* in VHT, STBC is binary */
425 if (status->flag & RX_FLAG_STBC_MASK)
426 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
fb378c23
EG
427 if (status->vht_flag & RX_VHT_FLAG_BF)
428 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
51648921
JB
429 pos++;
430 /* bandwidth */
1b8d242a 431 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
51648921 432 *pos++ = 4;
1b8d242a 433 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
51648921
JB
434 *pos++ = 11;
435 else if (status->flag & RX_FLAG_40MHZ)
436 *pos++ = 1;
437 else /* 20 MHz */
438 *pos++ = 0;
439 /* MCS/NSS */
440 *pos = (status->rate_idx << 4) | status->vht_nss;
441 pos += 4;
442 /* coding field */
63c361f5
EG
443 if (status->flag & RX_FLAG_LDPC)
444 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
51648921
JB
445 pos++;
446 /* group ID */
447 pos++;
448 /* partial_aid */
449 pos += 2;
450 }
451
a144f378
JB
452 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
453 *pos++ = status->chain_signal[chain];
454 *pos++ = chain;
455 }
1f7bba79
JB
456
457 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
458 /* ensure 2 byte alignment for the vendor field as required */
459 if ((pos - (u8 *)rthdr) & 1)
460 *pos++ = 0;
461 *pos++ = rtap.oui[0];
462 *pos++ = rtap.oui[1];
463 *pos++ = rtap.oui[2];
464 *pos++ = rtap.subns;
465 put_unaligned_le16(rtap.len, pos);
466 pos += 2;
467 /* align the actual payload as requested */
468 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
469 *pos++ = 0;
470 /* data (and possible padding) already follows */
471 }
601ae7f2
BR
472}
473
b2e7771e
JB
474/*
475 * This function copies a received frame to all monitor interfaces and
476 * returns a cleaned-up SKB that no longer includes the FCS nor the
477 * radiotap header the driver might have added.
478 */
479static struct sk_buff *
480ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a 481 struct ieee80211_rate *rate)
b2e7771e 482{
f1d58c25 483 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
b2e7771e 484 struct ieee80211_sub_if_data *sdata;
1f7bba79 485 int rt_hdrlen, needed_headroom;
b2e7771e
JB
486 struct sk_buff *skb, *skb2;
487 struct net_device *prev_dev = NULL;
488 int present_fcs_len = 0;
1f7bba79
JB
489 unsigned int rtap_vendor_space = 0;
490
491 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
492 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
493
494 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
495 }
b2e7771e
JB
496
497 /*
498 * First, we may need to make a copy of the skb because
499 * (1) we need to modify it for radiotap (if not present), and
500 * (2) the other RX handlers will modify the skb we got.
501 *
502 * We don't need to, of course, if we aren't going to return
503 * the SKB because it has a bad FCS/PLCP checksum.
504 */
0869aea0 505
30686bf7 506 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
b2e7771e
JB
507 present_fcs_len = FCS_LEN;
508
1f7bba79
JB
509 /* ensure hdr->frame_control and vendor radiotap data are in skb head */
510 if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
e3cf8b3f
ZY
511 dev_kfree_skb(origskb);
512 return NULL;
513 }
514
b2e7771e 515 if (!local->monitors) {
1f7bba79
JB
516 if (should_drop_frame(origskb, present_fcs_len,
517 rtap_vendor_space)) {
b2e7771e
JB
518 dev_kfree_skb(origskb);
519 return NULL;
520 }
521
1f7bba79 522 return remove_monitor_info(local, origskb, rtap_vendor_space);
b2e7771e
JB
523 }
524
751413ea 525 /* room for the radiotap header based on driver features */
1f7bba79
JB
526 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
527 needed_headroom = rt_hdrlen - rtap_vendor_space;
751413ea 528
1f7bba79 529 if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
b2e7771e
JB
530 /* only need to expand headroom if necessary */
531 skb = origskb;
532 origskb = NULL;
533
534 /*
535 * This shouldn't trigger often because most devices have an
536 * RX header they pull before we get here, and that should
537 * be big enough for our radiotap information. We should
538 * probably export the length to drivers so that we can have
539 * them allocate enough headroom to start with.
540 */
541 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 542 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
543 dev_kfree_skb(skb);
544 return NULL;
545 }
546 } else {
547 /*
548 * Need to make a copy and possibly remove radiotap header
549 * and FCS from the original.
550 */
551 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
552
1f7bba79
JB
553 origskb = remove_monitor_info(local, origskb,
554 rtap_vendor_space);
b2e7771e
JB
555
556 if (!skb)
557 return origskb;
558 }
559
0869aea0 560 /* prepend radiotap information */
1f7bba79 561 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
b2e7771e 562
ce3edf6d 563 skb_reset_mac_header(skb);
b2e7771e
JB
564 skb->ip_summed = CHECKSUM_UNNECESSARY;
565 skb->pkt_type = PACKET_OTHERHOST;
566 skb->protocol = htons(ETH_P_802_2);
567
568 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
05c914fe 569 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
b2e7771e
JB
570 continue;
571
3d30d949
MW
572 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
573 continue;
574
9607e6b6 575 if (!ieee80211_sdata_running(sdata))
47846c9b
JB
576 continue;
577
b2e7771e
JB
578 if (prev_dev) {
579 skb2 = skb_clone(skb, GFP_ATOMIC);
580 if (skb2) {
581 skb2->dev = prev_dev;
5548a8a1 582 netif_receive_skb(skb2);
b2e7771e
JB
583 }
584 }
585
586 prev_dev = sdata->dev;
5a490510 587 ieee80211_rx_stats(sdata->dev, skb->len);
b2e7771e
JB
588 }
589
590 if (prev_dev) {
591 skb->dev = prev_dev;
5548a8a1 592 netif_receive_skb(skb);
b2e7771e
JB
593 } else
594 dev_kfree_skb(skb);
595
596 return origskb;
597}
598
5cf121c3 599static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d 600{
238f74a2 601 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 602 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
9e26297a 603 int tid, seqno_idx, security_idx;
6e0d114d
JB
604
605 /* does the frame have a qos control field? */
238f74a2
HH
606 if (ieee80211_is_data_qos(hdr->frame_control)) {
607 u8 *qc = ieee80211_get_qos_ctl(hdr);
6e0d114d 608 /* frame has qos control */
238f74a2 609 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
04b7dcf9 610 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
554891e6 611 status->rx_flags |= IEEE80211_RX_AMSDU;
9e26297a
JB
612
613 seqno_idx = tid;
614 security_idx = tid;
6e0d114d 615 } else {
1411f9b5
JB
616 /*
617 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
618 *
619 * Sequence numbers for management frames, QoS data
620 * frames with a broadcast/multicast address in the
621 * Address 1 field, and all non-QoS data frames sent
622 * by QoS STAs are assigned using an additional single
623 * modulo-4096 counter, [...]
624 *
625 * We also use that counter for non-QoS STAs.
626 */
5a306f58 627 seqno_idx = IEEE80211_NUM_TIDS;
9e26297a
JB
628 security_idx = 0;
629 if (ieee80211_is_mgmt(hdr->frame_control))
5a306f58 630 security_idx = IEEE80211_NUM_TIDS;
9e26297a 631 tid = 0;
6e0d114d 632 }
52865dfd 633
9e26297a
JB
634 rx->seqno_idx = seqno_idx;
635 rx->security_idx = security_idx;
6e0d114d
JB
636 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
637 * For now, set skb->priority to 0 for other cases. */
638 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 639}
6e0d114d 640
d1c3a37c
JB
641/**
642 * DOC: Packet alignment
643 *
644 * Drivers always need to pass packets that are aligned to two-byte boundaries
645 * to the stack.
646 *
647 * Additionally, should, if possible, align the payload data in a way that
648 * guarantees that the contained IP header is aligned to a four-byte
649 * boundary. In the case of regular frames, this simply means aligning the
650 * payload to a four-byte boundary (because either the IP header is directly
651 * contained, or IV/RFC1042 headers that have a length divisible by four are
59d9cb07
KV
652 * in front of it). If the payload data is not properly aligned and the
653 * architecture doesn't support efficient unaligned operations, mac80211
654 * will align the data.
d1c3a37c
JB
655 *
656 * With A-MSDU frames, however, the payload data address must yield two modulo
657 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
658 * push the IP header further back to a multiple of four again. Thankfully, the
659 * specs were sane enough this time around to require padding each A-MSDU
660 * subframe to a length that is a multiple of four.
661 *
25985edc 662 * Padding like Atheros hardware adds which is between the 802.11 header and
d1c3a37c
JB
663 * the payload is not supported, the driver is required to move the 802.11
664 * header to be directly in front of the payload in that case.
665 */
666static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
38f3714d 667{
59d9cb07
KV
668#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
669 WARN_ONCE((unsigned long)rx->skb->data & 1,
670 "unaligned packet at 0x%p\n", rx->skb->data);
d1c3a37c 671#endif
6e0d114d
JB
672}
673
6368e4b1 674
571ecf67
JB
675/* rx handlers */
676
3cfcf6ac
JM
677static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
678{
679 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
680
d8ca16db 681 if (is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
682 return 0;
683
d8ca16db 684 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
685}
686
687
688static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
689{
690 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
691
d8ca16db 692 if (!is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
693 return 0;
694
d8ca16db 695 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
696}
697
698
699/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
700static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
701{
702 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
703 struct ieee80211_mmie *mmie;
56c52da2 704 struct ieee80211_mmie_16 *mmie16;
3cfcf6ac 705
1df332e8 706 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
3cfcf6ac
JM
707 return -1;
708
d8ca16db 709 if (!ieee80211_is_robust_mgmt_frame(skb))
3cfcf6ac
JM
710 return -1; /* not a robust management frame */
711
712 mmie = (struct ieee80211_mmie *)
713 (skb->data + skb->len - sizeof(*mmie));
56c52da2
JM
714 if (mmie->element_id == WLAN_EID_MMIE &&
715 mmie->length == sizeof(*mmie) - 2)
716 return le16_to_cpu(mmie->key_id);
717
718 mmie16 = (struct ieee80211_mmie_16 *)
719 (skb->data + skb->len - sizeof(*mmie16));
720 if (skb->len >= 24 + sizeof(*mmie16) &&
721 mmie16->element_id == WLAN_EID_MMIE &&
722 mmie16->length == sizeof(*mmie16) - 2)
723 return le16_to_cpu(mmie16->key_id);
724
725 return -1;
3cfcf6ac
JM
726}
727
2475b1cc
MS
728static int iwl80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
729 struct sk_buff *skb)
730{
731 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
732 __le16 fc;
733 int hdrlen;
734 u8 keyid;
735
736 fc = hdr->frame_control;
737 hdrlen = ieee80211_hdrlen(fc);
738
739 if (skb->len < hdrlen + cs->hdr_len)
740 return -EINVAL;
741
742 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
743 keyid &= cs->key_idx_mask;
744 keyid >>= cs->key_idx_shift;
745
746 return keyid;
747}
748
1df332e8 749static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2 750{
a7767f95 751 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
47846c9b 752 char *dev_addr = rx->sdata->vif.addr;
d6d1a5a7 753
a7767f95 754 if (ieee80211_is_data(hdr->frame_control)) {
3c5772a5
JC
755 if (is_multicast_ether_addr(hdr->addr1)) {
756 if (ieee80211_has_tods(hdr->frame_control) ||
1df332e8 757 !ieee80211_has_fromds(hdr->frame_control))
3c5772a5 758 return RX_DROP_MONITOR;
b203ca39 759 if (ether_addr_equal(hdr->addr3, dev_addr))
3c5772a5
JC
760 return RX_DROP_MONITOR;
761 } else {
762 if (!ieee80211_has_a4(hdr->frame_control))
763 return RX_DROP_MONITOR;
b203ca39 764 if (ether_addr_equal(hdr->addr4, dev_addr))
3c5772a5
JC
765 return RX_DROP_MONITOR;
766 }
33b64eb2
LCC
767 }
768
769 /* If there is not an established peer link and this is not a peer link
770 * establisment frame, beacon or probe, drop the frame.
771 */
772
57cf8043 773 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
33b64eb2 774 struct ieee80211_mgmt *mgmt;
d6d1a5a7 775
a7767f95 776 if (!ieee80211_is_mgmt(hdr->frame_control))
33b64eb2
LCC
777 return RX_DROP_MONITOR;
778
a7767f95 779 if (ieee80211_is_action(hdr->frame_control)) {
d3aaec8a 780 u8 category;
9b395bc3
JB
781
782 /* make sure category field is present */
783 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
784 return RX_DROP_MONITOR;
785
33b64eb2 786 mgmt = (struct ieee80211_mgmt *)hdr;
d3aaec8a
JC
787 category = mgmt->u.action.category;
788 if (category != WLAN_CATEGORY_MESH_ACTION &&
1df332e8 789 category != WLAN_CATEGORY_SELF_PROTECTED)
33b64eb2 790 return RX_DROP_MONITOR;
33b64eb2 791 return RX_CONTINUE;
33b64eb2
LCC
792 }
793
a7767f95
HH
794 if (ieee80211_is_probe_req(hdr->frame_control) ||
795 ieee80211_is_probe_resp(hdr->frame_control) ||
71839121
JC
796 ieee80211_is_beacon(hdr->frame_control) ||
797 ieee80211_is_auth(hdr->frame_control))
a7767f95
HH
798 return RX_CONTINUE;
799
800 return RX_DROP_MONITOR;
a7767f95
HH
801 }
802
902acc78
JB
803 return RX_CONTINUE;
804}
33b64eb2 805
d3b2fb53 806static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1edfb1af 807 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
808 int index,
809 struct sk_buff_head *frames)
1edfb1af 810{
83eb935e
MK
811 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
812 struct sk_buff *skb;
4cfda47b 813 struct ieee80211_rx_status *status;
1edfb1af 814
dd318575
JB
815 lockdep_assert_held(&tid_agg_rx->reorder_lock);
816
83eb935e 817 if (skb_queue_empty(skb_list))
1edfb1af
JB
818 goto no_frame;
819
83eb935e
MK
820 if (!ieee80211_rx_reorder_ready(skb_list)) {
821 __skb_queue_purge(skb_list);
822 goto no_frame;
823 }
824
825 /* release frames from the reorder ring buffer */
1edfb1af 826 tid_agg_rx->stored_mpdu_num--;
83eb935e
MK
827 while ((skb = __skb_dequeue(skb_list))) {
828 status = IEEE80211_SKB_RXCB(skb);
829 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
830 __skb_queue_tail(frames, skb);
831 }
1edfb1af
JB
832
833no_frame:
9a886586 834 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1edfb1af
JB
835}
836
d3b2fb53 837static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1edfb1af 838 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
839 u16 head_seq_num,
840 struct sk_buff_head *frames)
1edfb1af
JB
841{
842 int index;
843
dd318575
JB
844 lockdep_assert_held(&tid_agg_rx->reorder_lock);
845
9a886586 846 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2e3049b7 847 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
f9e124fb
CL
848 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
849 frames);
1edfb1af
JB
850 }
851}
852
853/*
854 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
855 * the skb was added to the buffer longer than this time ago, the earlier
856 * frames that have not yet been received are assumed to be lost and the skb
857 * can be released for processing. This may also release other skb's from the
858 * reorder buffer if there are no additional gaps between the frames.
2bff8ebf
CL
859 *
860 * Callers must hold tid_agg_rx->reorder_lock.
1edfb1af
JB
861 */
862#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
863
d3b2fb53 864static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
f9e124fb
CL
865 struct tid_ampdu_rx *tid_agg_rx,
866 struct sk_buff_head *frames)
aa0c8636 867{
83eb935e 868 int index, i, j;
aa0c8636 869
dd318575
JB
870 lockdep_assert_held(&tid_agg_rx->reorder_lock);
871
aa0c8636 872 /* release the buffer until next missing frame */
2e3049b7 873 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
83eb935e 874 if (!ieee80211_rx_reorder_ready(&tid_agg_rx->reorder_buf[index]) &&
07ae2dfc 875 tid_agg_rx->stored_mpdu_num) {
aa0c8636
CL
876 /*
877 * No buffers ready to be released, but check whether any
878 * frames in the reorder buffer have timed out.
879 */
aa0c8636
CL
880 int skipped = 1;
881 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
882 j = (j + 1) % tid_agg_rx->buf_size) {
83eb935e
MK
883 if (!ieee80211_rx_reorder_ready(
884 &tid_agg_rx->reorder_buf[j])) {
aa0c8636
CL
885 skipped++;
886 continue;
887 }
499fe9a4
DH
888 if (skipped &&
889 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
aa0c8636 890 HT_RX_REORDER_BUF_TIMEOUT))
2bff8ebf 891 goto set_release_timer;
aa0c8636 892
83eb935e
MK
893 /* don't leave incomplete A-MSDUs around */
894 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
895 i = (i + 1) % tid_agg_rx->buf_size)
896 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
897
bdcbd8e0
JB
898 ht_dbg_ratelimited(sdata,
899 "release an RX reorder frame due to timeout on earlier frames\n");
f9e124fb
CL
900 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
901 frames);
aa0c8636
CL
902
903 /*
904 * Increment the head seq# also for the skipped slots.
905 */
906 tid_agg_rx->head_seq_num =
9a886586
JB
907 (tid_agg_rx->head_seq_num +
908 skipped) & IEEE80211_SN_MASK;
aa0c8636
CL
909 skipped = 0;
910 }
83eb935e
MK
911 } else while (ieee80211_rx_reorder_ready(
912 &tid_agg_rx->reorder_buf[index])) {
f9e124fb
CL
913 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
914 frames);
2e3049b7 915 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
aa0c8636 916 }
2bff8ebf
CL
917
918 if (tid_agg_rx->stored_mpdu_num) {
2e3049b7 919 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
2bff8ebf
CL
920
921 for (; j != (index - 1) % tid_agg_rx->buf_size;
922 j = (j + 1) % tid_agg_rx->buf_size) {
83eb935e
MK
923 if (ieee80211_rx_reorder_ready(
924 &tid_agg_rx->reorder_buf[j]))
2bff8ebf
CL
925 break;
926 }
927
928 set_release_timer:
929
788211d8
JB
930 if (!tid_agg_rx->removed)
931 mod_timer(&tid_agg_rx->reorder_timer,
932 tid_agg_rx->reorder_time[j] + 1 +
933 HT_RX_REORDER_BUF_TIMEOUT);
2bff8ebf
CL
934 } else {
935 del_timer(&tid_agg_rx->reorder_timer);
936 }
aa0c8636
CL
937}
938
1edfb1af
JB
939/*
940 * As this function belongs to the RX path it must be under
941 * rcu_read_lock protection. It returns false if the frame
942 * can be processed immediately, true if it was consumed.
943 */
d3b2fb53 944static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1edfb1af 945 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
946 struct sk_buff *skb,
947 struct sk_buff_head *frames)
1edfb1af
JB
948{
949 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
83eb935e 950 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1edfb1af
JB
951 u16 sc = le16_to_cpu(hdr->seq_ctrl);
952 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
953 u16 head_seq_num, buf_size;
954 int index;
2bff8ebf 955 bool ret = true;
1edfb1af 956
dd318575
JB
957 spin_lock(&tid_agg_rx->reorder_lock);
958
4549cf2b
MK
959 /*
960 * Offloaded BA sessions have no known starting sequence number so pick
961 * one from first Rxed frame for this tid after BA was started.
962 */
963 if (unlikely(tid_agg_rx->auto_seq)) {
964 tid_agg_rx->auto_seq = false;
965 tid_agg_rx->ssn = mpdu_seq_num;
966 tid_agg_rx->head_seq_num = mpdu_seq_num;
967 }
968
1edfb1af
JB
969 buf_size = tid_agg_rx->buf_size;
970 head_seq_num = tid_agg_rx->head_seq_num;
971
972 /* frame with out of date sequence number */
9a886586 973 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1edfb1af 974 dev_kfree_skb(skb);
2bff8ebf 975 goto out;
1edfb1af
JB
976 }
977
978 /*
979 * If frame the sequence number exceeds our buffering window
980 * size release some previous frames to make room for this one.
981 */
9a886586
JB
982 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
983 head_seq_num = ieee80211_sn_inc(
984 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1edfb1af 985 /* release stored frames up to new head to stack */
d3b2fb53 986 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
f9e124fb 987 head_seq_num, frames);
1edfb1af
JB
988 }
989
990 /* Now the new frame is always in the range of the reordering buffer */
991
2e3049b7 992 index = mpdu_seq_num % tid_agg_rx->buf_size;
1edfb1af
JB
993
994 /* check if we already stored this frame */
83eb935e 995 if (ieee80211_rx_reorder_ready(&tid_agg_rx->reorder_buf[index])) {
1edfb1af 996 dev_kfree_skb(skb);
2bff8ebf 997 goto out;
1edfb1af
JB
998 }
999
1000 /*
1001 * If the current MPDU is in the right order and nothing else
1002 * is stored we can process it directly, no need to buffer it.
c835b214
JB
1003 * If it is first but there's something stored, we may be able
1004 * to release frames after this one.
1edfb1af
JB
1005 */
1006 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1007 tid_agg_rx->stored_mpdu_num == 0) {
83eb935e
MK
1008 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1009 tid_agg_rx->head_seq_num =
1010 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
2bff8ebf
CL
1011 ret = false;
1012 goto out;
1edfb1af
JB
1013 }
1014
1015 /* put the frame in the reordering buffer */
83eb935e
MK
1016 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1017 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1018 tid_agg_rx->reorder_time[index] = jiffies;
1019 tid_agg_rx->stored_mpdu_num++;
1020 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1021 }
1edfb1af 1022
2bff8ebf
CL
1023 out:
1024 spin_unlock(&tid_agg_rx->reorder_lock);
1025 return ret;
1edfb1af
JB
1026}
1027
1028/*
1029 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1030 * true if the MPDU was buffered, false if it should be processed.
1031 */
f9e124fb
CL
1032static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1033 struct sk_buff_head *frames)
1edfb1af 1034{
2569a826
JB
1035 struct sk_buff *skb = rx->skb;
1036 struct ieee80211_local *local = rx->local;
1edfb1af 1037 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2569a826 1038 struct sta_info *sta = rx->sta;
1edfb1af
JB
1039 struct tid_ampdu_rx *tid_agg_rx;
1040 u16 sc;
6cc00d54 1041 u8 tid, ack_policy;
1edfb1af 1042
051a41fa
JB
1043 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1044 is_multicast_ether_addr(hdr->addr1))
2569a826 1045 goto dont_reorder;
1edfb1af
JB
1046
1047 /*
1048 * filter the QoS data rx stream according to
1049 * STA/TID and check if this STA/TID is on aggregation
1050 */
1051
1edfb1af 1052 if (!sta)
2569a826 1053 goto dont_reorder;
1edfb1af 1054
6cc00d54
TP
1055 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1056 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1edfb1af
JB
1057 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1058
a87f736d
JB
1059 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1060 if (!tid_agg_rx)
1061 goto dont_reorder;
1edfb1af
JB
1062
1063 /* qos null data frames are excluded */
1064 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
a87f736d 1065 goto dont_reorder;
1edfb1af 1066
6cc00d54
TP
1067 /* not part of a BA session */
1068 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1069 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1070 goto dont_reorder;
1071
1edfb1af
JB
1072 /* new, potentially un-ordered, ampdu frame - process it */
1073
1074 /* reset session timer */
1075 if (tid_agg_rx->timeout)
12d3952f 1076 tid_agg_rx->last_rx = jiffies;
1edfb1af
JB
1077
1078 /* if this mpdu is fragmented - terminate rx aggregation session */
1079 sc = le16_to_cpu(hdr->seq_ctrl);
1080 if (sc & IEEE80211_SCTL_FRAG) {
c1475ca9 1081 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
344eec67
JB
1082 skb_queue_tail(&rx->sdata->skb_queue, skb);
1083 ieee80211_queue_work(&local->hw, &rx->sdata->work);
2569a826 1084 return;
1edfb1af
JB
1085 }
1086
a87f736d
JB
1087 /*
1088 * No locking needed -- we will only ever process one
1089 * RX packet at a time, and thus own tid_agg_rx. All
1090 * other code manipulating it needs to (and does) make
1091 * sure that we cannot get to it any more before doing
1092 * anything with it.
1093 */
f9e124fb
CL
1094 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1095 frames))
2569a826
JB
1096 return;
1097
1098 dont_reorder:
f9e124fb 1099 __skb_queue_tail(frames, skb);
1edfb1af 1100}
33b64eb2 1101
49461622 1102static ieee80211_rx_result debug_noinline
0395442a 1103ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
571ecf67 1104{
a7767f95 1105 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 1106 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67 1107
6b0f3274
JB
1108 /*
1109 * Drop duplicate 802.11 retransmissions
1110 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1111 */
0395442a
JB
1112
1113 if (rx->skb->len < 24)
1114 return RX_CONTINUE;
1115
1116 if (ieee80211_is_ctl(hdr->frame_control) ||
1117 ieee80211_is_qos_nullfunc(hdr->frame_control) ||
1118 is_multicast_ether_addr(hdr->addr1))
1119 return RX_CONTINUE;
1120
1121 if (rx->sta) {
a7767f95 1122 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
9e26297a 1123 rx->sta->last_seq_ctrl[rx->seqno_idx] ==
571ecf67 1124 hdr->seq_ctrl)) {
c206ca67 1125 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
5c90067c 1126 rx->sta->num_duplicates++;
b1f93314 1127 return RX_DROP_UNUSABLE;
0cfcefef 1128 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
9e26297a 1129 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
0cfcefef 1130 }
571ecf67
JB
1131 }
1132
0395442a
JB
1133 return RX_CONTINUE;
1134}
1135
1136static ieee80211_rx_result debug_noinline
1137ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1138{
1139 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1140
571ecf67
JB
1141 /* Drop disallowed frame classes based on STA auth/assoc state;
1142 * IEEE 802.11, Chap 5.5.
1143 *
ccd7b362
JB
1144 * mac80211 filters only based on association state, i.e. it drops
1145 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
1146 * deauth/disassoc frames when needed. In addition, hostapd is
1147 * responsible for filtering on both auth and assoc states.
1148 */
33b64eb2 1149
902acc78 1150 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 1151 return ieee80211_rx_mesh_check(rx);
33b64eb2 1152
a7767f95
HH
1153 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1154 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 1155 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1be7fe8d 1156 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
239281f8 1157 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
c2c98fde 1158 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
7852e361
JB
1159 /*
1160 * accept port control frames from the AP even when it's not
1161 * yet marked ASSOC to prevent a race where we don't set the
1162 * assoc bit quickly enough before it sends the first frame
1163 */
1164 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2a33bee2 1165 ieee80211_is_data_present(hdr->frame_control)) {
6dbda2d0
JB
1166 unsigned int hdrlen;
1167 __be16 ethertype;
1168
1169 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1170
1171 if (rx->skb->len < hdrlen + 8)
1172 return RX_DROP_MONITOR;
1173
1174 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1175 if (ethertype == rx->sdata->control_port_protocol)
2a33bee2
GE
1176 return RX_CONTINUE;
1177 }
21fc7560
JB
1178
1179 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1180 cfg80211_rx_spurious_frame(rx->sdata->dev,
1181 hdr->addr2,
1182 GFP_ATOMIC))
1183 return RX_DROP_UNUSABLE;
1184
e4c26add 1185 return RX_DROP_MONITOR;
2a33bee2 1186 }
571ecf67 1187
9ae54c84 1188 return RX_CONTINUE;
570bd537
JB
1189}
1190
1191
572e0012
KV
1192static ieee80211_rx_result debug_noinline
1193ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1194{
1195 struct ieee80211_local *local;
1196 struct ieee80211_hdr *hdr;
1197 struct sk_buff *skb;
1198
1199 local = rx->local;
1200 skb = rx->skb;
1201 hdr = (struct ieee80211_hdr *) skb->data;
1202
1203 if (!local->pspolling)
1204 return RX_CONTINUE;
1205
1206 if (!ieee80211_has_fromds(hdr->frame_control))
1207 /* this is not from AP */
1208 return RX_CONTINUE;
1209
1210 if (!ieee80211_is_data(hdr->frame_control))
1211 return RX_CONTINUE;
1212
1213 if (!ieee80211_has_moredata(hdr->frame_control)) {
1214 /* AP has no more frames buffered for us */
1215 local->pspolling = false;
1216 return RX_CONTINUE;
1217 }
1218
1219 /* more data bit is set, let's request a new frame from the AP */
1220 ieee80211_send_pspoll(local, rx->sdata);
1221
1222 return RX_CONTINUE;
1223}
1224
d012a605 1225static void sta_ps_start(struct sta_info *sta)
571ecf67 1226{
133b8226 1227 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1228 struct ieee80211_local *local = sdata->local;
d012a605 1229 struct ps_data *ps;
ba8c3d6f 1230 int tid;
0795af57 1231
d012a605
MP
1232 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1233 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1234 ps = &sdata->bss->ps;
1235 else
1236 return;
1237
1238 atomic_inc(&ps->num_sta_ps);
c2c98fde 1239 set_sta_flag(sta, WLAN_STA_PS_STA);
30686bf7 1240 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
d057e5a3 1241 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
bdcbd8e0
JB
1242 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1243 sta->sta.addr, sta->sta.aid);
ba8c3d6f 1244
17c18bf8
JB
1245 ieee80211_clear_fast_xmit(sta);
1246
ba8c3d6f
FF
1247 if (!sta->sta.txq[0])
1248 return;
1249
1250 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1251 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1252
1253 if (!skb_queue_len(&txqi->queue))
1254 set_bit(tid, &sta->txq_buffered_tids);
1255 else
1256 clear_bit(tid, &sta->txq_buffered_tids);
1257 }
571ecf67
JB
1258}
1259
d012a605 1260static void sta_ps_end(struct sta_info *sta)
571ecf67 1261{
bdcbd8e0
JB
1262 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1263 sta->sta.addr, sta->sta.aid);
004c872e 1264
c2c98fde 1265 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
e3685e03
JB
1266 /*
1267 * Clear the flag only if the other one is still set
1268 * so that the TX path won't start TX'ing new frames
1269 * directly ... In the case that the driver flag isn't
1270 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1271 */
1272 clear_sta_flag(sta, WLAN_STA_PS_STA);
bdcbd8e0
JB
1273 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1274 sta->sta.addr, sta->sta.aid);
af818581
JB
1275 return;
1276 }
1277
5ac2e350
JB
1278 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1279 clear_sta_flag(sta, WLAN_STA_PS_STA);
af818581 1280 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1281}
1282
cf47161a 1283int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
d057e5a3 1284{
cf47161a 1285 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
d057e5a3
AN
1286 bool in_ps;
1287
cf47161a 1288 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
d057e5a3
AN
1289
1290 /* Don't let the same PS state be set twice */
cf47161a 1291 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
d057e5a3
AN
1292 if ((start && in_ps) || (!start && !in_ps))
1293 return -EINVAL;
1294
1295 if (start)
cf47161a 1296 sta_ps_start(sta);
d057e5a3 1297 else
cf47161a 1298 sta_ps_end(sta);
d057e5a3
AN
1299
1300 return 0;
1301}
1302EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1303
47086fc5
JB
1304static ieee80211_rx_result debug_noinline
1305ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1306{
1307 struct ieee80211_sub_if_data *sdata = rx->sdata;
1308 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1309 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1310 int tid, ac;
1311
5c90067c 1312 if (!rx->sta)
47086fc5
JB
1313 return RX_CONTINUE;
1314
1315 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1316 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1317 return RX_CONTINUE;
1318
1319 /*
1320 * The device handles station powersave, so don't do anything about
1321 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1322 * it to mac80211 since they're handled.)
1323 */
30686bf7 1324 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
47086fc5
JB
1325 return RX_CONTINUE;
1326
1327 /*
1328 * Don't do anything if the station isn't already asleep. In
1329 * the uAPSD case, the station will probably be marked asleep,
1330 * in the PS-Poll case the station must be confused ...
1331 */
c2c98fde 1332 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
47086fc5
JB
1333 return RX_CONTINUE;
1334
1335 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
c2c98fde
JB
1336 if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
1337 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
deeaee19
JB
1338 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1339 else
c2c98fde 1340 set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
deeaee19 1341 }
47086fc5
JB
1342
1343 /* Free PS Poll skb here instead of returning RX_DROP that would
1344 * count as an dropped frame. */
1345 dev_kfree_skb(rx->skb);
1346
1347 return RX_QUEUED;
1348 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1349 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1350 ieee80211_has_pm(hdr->frame_control) &&
1351 (ieee80211_is_data_qos(hdr->frame_control) ||
1352 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1353 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1354 ac = ieee802_1d_to_ac[tid & 7];
1355
1356 /*
1357 * If this AC is not trigger-enabled do nothing.
1358 *
1359 * NB: This could/should check a separate bitmap of trigger-
1360 * enabled queues, but for now we only implement uAPSD w/o
1361 * TSPEC changes to the ACs, so they're always the same.
1362 */
1363 if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
1364 return RX_CONTINUE;
1365
1366 /* if we are in a service period, do nothing */
c2c98fde 1367 if (test_sta_flag(rx->sta, WLAN_STA_SP))
47086fc5
JB
1368 return RX_CONTINUE;
1369
c2c98fde 1370 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
47086fc5
JB
1371 ieee80211_sta_ps_deliver_uapsd(rx->sta);
1372 else
c2c98fde 1373 set_sta_flag(rx->sta, WLAN_STA_UAPSD);
47086fc5
JB
1374 }
1375
1376 return RX_CONTINUE;
1377}
1378
49461622 1379static ieee80211_rx_result debug_noinline
5cf121c3 1380ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1381{
1382 struct sta_info *sta = rx->sta;
eb9fb5b8
JB
1383 struct sk_buff *skb = rx->skb;
1384 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1385 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
ef0621e8 1386 int i;
571ecf67
JB
1387
1388 if (!sta)
9ae54c84 1389 return RX_CONTINUE;
571ecf67 1390
b291ba11
JB
1391 /*
1392 * Update last_rx only for IBSS packets which are for the current
e584da5e
AQ
1393 * BSSID and for station already AUTHORIZED to avoid keeping the
1394 * current IBSS network alive in cases where other STAs start
1395 * using different BSSID. This will also give the station another
1396 * chance to restart the authentication/authorization in case
1397 * something went wrong the first time.
b291ba11 1398 */
05c914fe 1399 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1400 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1401 NL80211_IFTYPE_ADHOC);
e584da5e
AQ
1402 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1403 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
571ecf67 1404 sta->last_rx = jiffies;
f4ebddf9
HR
1405 if (ieee80211_is_data(hdr->frame_control) &&
1406 !is_multicast_ether_addr(hdr->addr1)) {
3af6334c
FF
1407 sta->last_rx_rate_idx = status->rate_idx;
1408 sta->last_rx_rate_flag = status->flag;
1b8d242a 1409 sta->last_rx_rate_vht_flag = status->vht_flag;
5614618e 1410 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c
FF
1411 }
1412 }
239281f8 1413 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
6fe3eac7 1414 sta->last_rx = jiffies;
b291ba11
JB
1415 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1416 /*
33b64eb2
LCC
1417 * Mesh beacons will update last_rx when if they are found to
1418 * match the current local configuration when processed.
571ecf67 1419 */
b291ba11 1420 sta->last_rx = jiffies;
3af6334c
FF
1421 if (ieee80211_is_data(hdr->frame_control)) {
1422 sta->last_rx_rate_idx = status->rate_idx;
1423 sta->last_rx_rate_flag = status->flag;
b8ff416b 1424 sta->last_rx_rate_vht_flag = status->vht_flag;
5614618e 1425 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c 1426 }
571ecf67
JB
1427 }
1428
3cf335d5
KV
1429 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1430 ieee80211_sta_rx_notify(rx->sdata, hdr);
1431
571ecf67
JB
1432 sta->rx_fragments++;
1433 sta->rx_bytes += rx->skb->len;
fe8431f8
FF
1434 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1435 sta->last_signal = status->signal;
1436 ewma_add(&sta->avg_signal, -status->signal);
1437 }
571ecf67 1438
ef0621e8
FF
1439 if (status->chains) {
1440 sta->chains = status->chains;
1441 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1442 int signal = status->chain_signal[i];
1443
1444 if (!(status->chains & BIT(i)))
1445 continue;
1446
1447 sta->chain_signal_last[i] = signal;
1448 ewma_add(&sta->chain_signal_avg[i], -signal);
1449 }
1450 }
1451
72eaa43a
JB
1452 /*
1453 * Change STA power saving mode only at the end of a frame
1454 * exchange sequence.
1455 */
30686bf7 1456 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
d057e5a3 1457 !ieee80211_has_morefrags(hdr->frame_control) &&
4cfda47b 1458 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
05c914fe 1459 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
b4ba544c
JB
1460 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1461 /* PM bit is only checked in frames where it isn't reserved,
1462 * in AP mode it's reserved in non-bufferable management frames
1463 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1464 */
1465 (!ieee80211_is_mgmt(hdr->frame_control) ||
1466 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
c2c98fde 1467 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
b4ba544c 1468 if (!ieee80211_has_pm(hdr->frame_control))
d012a605 1469 sta_ps_end(sta);
72eaa43a
JB
1470 } else {
1471 if (ieee80211_has_pm(hdr->frame_control))
d012a605 1472 sta_ps_start(sta);
72eaa43a 1473 }
571ecf67
JB
1474 }
1475
3f52b7e3
MP
1476 /* mesh power save support */
1477 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1478 ieee80211_mps_rx_h_sta_process(sta, hdr);
1479
22403def
JB
1480 /*
1481 * Drop (qos-)data::nullfunc frames silently, since they
1482 * are used only to control station power saving mode.
1483 */
1484 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1485 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
571ecf67 1486 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
d524215f
FF
1487
1488 /*
1489 * If we receive a 4-addr nullfunc frame from a STA
e7f4a940
JB
1490 * that was not moved to a 4-addr STA vlan yet send
1491 * the event to userspace and for older hostapd drop
1492 * the frame to the monitor interface.
d524215f
FF
1493 */
1494 if (ieee80211_has_a4(hdr->frame_control) &&
1495 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1496 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
e7f4a940
JB
1497 !rx->sdata->u.vlan.sta))) {
1498 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1499 cfg80211_rx_unexpected_4addr_frame(
1500 rx->sdata->dev, sta->sta.addr,
1501 GFP_ATOMIC);
d524215f 1502 return RX_DROP_MONITOR;
e7f4a940 1503 }
22403def
JB
1504 /*
1505 * Update counter and free packet here to avoid
1506 * counting this as a dropped packed.
1507 */
571ecf67
JB
1508 sta->rx_packets++;
1509 dev_kfree_skb(rx->skb);
9ae54c84 1510 return RX_QUEUED;
571ecf67
JB
1511 }
1512
9ae54c84 1513 return RX_CONTINUE;
571ecf67
JB
1514} /* ieee80211_rx_h_sta_process */
1515
86c228a7
JA
1516static ieee80211_rx_result debug_noinline
1517ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1518{
1519 struct sk_buff *skb = rx->skb;
1520 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1521 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1522 int keyidx;
1523 int hdrlen;
1524 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1525 struct ieee80211_key *sta_ptk = NULL;
1526 int mmie_keyidx = -1;
1527 __le16 fc;
2475b1cc 1528 const struct ieee80211_cipher_scheme *cs = NULL;
86c228a7
JA
1529
1530 /*
1531 * Key selection 101
1532 *
1533 * There are four types of keys:
1534 * - GTK (group keys)
1535 * - IGTK (group keys for management frames)
1536 * - PTK (pairwise keys)
1537 * - STK (station-to-station pairwise keys)
1538 *
1539 * When selecting a key, we have to distinguish between multicast
1540 * (including broadcast) and unicast frames, the latter can only
1541 * use PTKs and STKs while the former always use GTKs and IGTKs.
1542 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1543 * unicast frames can also use key indices like GTKs. Hence, if we
1544 * don't have a PTK/STK we check the key index for a WEP key.
1545 *
1546 * Note that in a regular BSS, multicast frames are sent by the
1547 * AP only, associated stations unicast the frame to the AP first
1548 * which then multicasts it on their behalf.
1549 *
1550 * There is also a slight problem in IBSS mode: GTKs are negotiated
1551 * with each station, that is something we don't currently handle.
1552 * The spec seems to expect that one negotiates the same key with
1553 * every station but there's no such requirement; VLANs could be
1554 * possible.
1555 */
1556
86c228a7
JA
1557 /* start without a key */
1558 rx->key = NULL;
2475b1cc 1559 fc = hdr->frame_control;
86c228a7 1560
2475b1cc
MS
1561 if (rx->sta) {
1562 int keyid = rx->sta->ptk_idx;
86c228a7 1563
2475b1cc
MS
1564 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1565 cs = rx->sta->cipher_scheme;
1566 keyid = iwl80211_get_cs_keyid(cs, rx->skb);
1567 if (unlikely(keyid < 0))
1568 return RX_DROP_UNUSABLE;
1569 }
1570 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1571 }
86c228a7
JA
1572
1573 if (!ieee80211_has_protected(fc))
1574 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1575
1576 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1577 rx->key = sta_ptk;
1578 if ((status->flag & RX_FLAG_DECRYPTED) &&
1579 (status->flag & RX_FLAG_IV_STRIPPED))
1580 return RX_CONTINUE;
1581 /* Skip decryption if the frame is not protected. */
1582 if (!ieee80211_has_protected(fc))
1583 return RX_CONTINUE;
1584 } else if (mmie_keyidx >= 0) {
1585 /* Broadcast/multicast robust management frame / BIP */
1586 if ((status->flag & RX_FLAG_DECRYPTED) &&
1587 (status->flag & RX_FLAG_IV_STRIPPED))
1588 return RX_CONTINUE;
1589
1590 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1591 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1592 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1593 if (rx->sta)
1594 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1595 if (!rx->key)
1596 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1597 } else if (!ieee80211_has_protected(fc)) {
1598 /*
1599 * The frame was not protected, so skip decryption. However, we
1600 * need to set rx->key if there is a key that could have been
1601 * used so that the frame may be dropped if encryption would
1602 * have been expected.
1603 */
1604 struct ieee80211_key *key = NULL;
1605 struct ieee80211_sub_if_data *sdata = rx->sdata;
1606 int i;
1607
1608 if (ieee80211_is_mgmt(fc) &&
1609 is_multicast_ether_addr(hdr->addr1) &&
1610 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1611 rx->key = key;
1612 else {
1613 if (rx->sta) {
1614 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1615 key = rcu_dereference(rx->sta->gtk[i]);
1616 if (key)
1617 break;
1618 }
1619 }
1620 if (!key) {
1621 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1622 key = rcu_dereference(sdata->keys[i]);
1623 if (key)
1624 break;
1625 }
1626 }
1627 if (key)
1628 rx->key = key;
1629 }
1630 return RX_CONTINUE;
1631 } else {
1632 u8 keyid;
2475b1cc 1633
86c228a7
JA
1634 /*
1635 * The device doesn't give us the IV so we won't be
1636 * able to look up the key. That's ok though, we
1637 * don't need to decrypt the frame, we just won't
1638 * be able to keep statistics accurate.
1639 * Except for key threshold notifications, should
1640 * we somehow allow the driver to tell us which key
1641 * the hardware used if this flag is set?
1642 */
1643 if ((status->flag & RX_FLAG_DECRYPTED) &&
1644 (status->flag & RX_FLAG_IV_STRIPPED))
1645 return RX_CONTINUE;
1646
1647 hdrlen = ieee80211_hdrlen(fc);
1648
2475b1cc
MS
1649 if (cs) {
1650 keyidx = iwl80211_get_cs_keyid(cs, rx->skb);
86c228a7 1651
2475b1cc
MS
1652 if (unlikely(keyidx < 0))
1653 return RX_DROP_UNUSABLE;
1654 } else {
1655 if (rx->skb->len < 8 + hdrlen)
1656 return RX_DROP_UNUSABLE; /* TODO: count this? */
1657 /*
1658 * no need to call ieee80211_wep_get_keyidx,
1659 * it verifies a bunch of things we've done already
1660 */
1661 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1662 keyidx = keyid >> 6;
1663 }
86c228a7
JA
1664
1665 /* check per-station GTK first, if multicast packet */
1666 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1667 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1668
1669 /* if not found, try default key */
1670 if (!rx->key) {
1671 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1672
1673 /*
1674 * RSNA-protected unicast frames should always be
1675 * sent with pairwise or station-to-station keys,
1676 * but for WEP we allow using a key index as well.
1677 */
1678 if (rx->key &&
1679 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1680 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1681 !is_multicast_ether_addr(hdr->addr1))
1682 rx->key = NULL;
1683 }
1684 }
1685
1686 if (rx->key) {
1687 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1688 return RX_DROP_MONITOR;
1689
1690 rx->key->tx_rx_count++;
1691 /* TODO: add threshold stuff again */
1692 } else {
1693 return RX_DROP_MONITOR;
1694 }
1695
1696 switch (rx->key->conf.cipher) {
1697 case WLAN_CIPHER_SUITE_WEP40:
1698 case WLAN_CIPHER_SUITE_WEP104:
1699 result = ieee80211_crypto_wep_decrypt(rx);
1700 break;
1701 case WLAN_CIPHER_SUITE_TKIP:
1702 result = ieee80211_crypto_tkip_decrypt(rx);
1703 break;
1704 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db
JM
1705 result = ieee80211_crypto_ccmp_decrypt(
1706 rx, IEEE80211_CCMP_MIC_LEN);
1707 break;
1708 case WLAN_CIPHER_SUITE_CCMP_256:
1709 result = ieee80211_crypto_ccmp_decrypt(
1710 rx, IEEE80211_CCMP_256_MIC_LEN);
86c228a7
JA
1711 break;
1712 case WLAN_CIPHER_SUITE_AES_CMAC:
1713 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1714 break;
56c52da2
JM
1715 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1716 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1717 break;
8ade538b
JM
1718 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1719 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1720 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1721 break;
00b9cfa3
JM
1722 case WLAN_CIPHER_SUITE_GCMP:
1723 case WLAN_CIPHER_SUITE_GCMP_256:
1724 result = ieee80211_crypto_gcmp_decrypt(rx);
1725 break;
86c228a7 1726 default:
2475b1cc 1727 result = ieee80211_crypto_hw_decrypt(rx);
86c228a7
JA
1728 }
1729
1730 /* the hdr variable is invalid after the decrypt handlers */
1731
1732 /* either the frame has been decrypted or will be dropped */
1733 status->flag |= RX_FLAG_DECRYPTED;
1734
1735 return result;
1736}
1737
571ecf67
JB
1738static inline struct ieee80211_fragment_entry *
1739ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1740 unsigned int frag, unsigned int seq, int rx_queue,
1741 struct sk_buff **skb)
1742{
1743 struct ieee80211_fragment_entry *entry;
571ecf67 1744
571ecf67
JB
1745 entry = &sdata->fragments[sdata->fragment_next++];
1746 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1747 sdata->fragment_next = 0;
1748
bdcbd8e0 1749 if (!skb_queue_empty(&entry->skb_list))
571ecf67 1750 __skb_queue_purge(&entry->skb_list);
571ecf67
JB
1751
1752 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1753 *skb = NULL;
1754 entry->first_frag_time = jiffies;
1755 entry->seq = seq;
1756 entry->rx_queue = rx_queue;
1757 entry->last_frag = frag;
1758 entry->ccmp = 0;
1759 entry->extra_len = 0;
1760
1761 return entry;
1762}
1763
1764static inline struct ieee80211_fragment_entry *
1765ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
b73d70ad 1766 unsigned int frag, unsigned int seq,
571ecf67
JB
1767 int rx_queue, struct ieee80211_hdr *hdr)
1768{
1769 struct ieee80211_fragment_entry *entry;
1770 int i, idx;
1771
1772 idx = sdata->fragment_next;
1773 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1774 struct ieee80211_hdr *f_hdr;
571ecf67
JB
1775
1776 idx--;
1777 if (idx < 0)
1778 idx = IEEE80211_FRAGMENT_MAX - 1;
1779
1780 entry = &sdata->fragments[idx];
1781 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1782 entry->rx_queue != rx_queue ||
1783 entry->last_frag + 1 != frag)
1784 continue;
1785
b73d70ad 1786 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
571ecf67 1787
b73d70ad
HH
1788 /*
1789 * Check ftype and addresses are equal, else check next fragment
1790 */
1791 if (((hdr->frame_control ^ f_hdr->frame_control) &
1792 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
b203ca39
JP
1793 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1794 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
571ecf67
JB
1795 continue;
1796
ab46623e 1797 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
1798 __skb_queue_purge(&entry->skb_list);
1799 continue;
1800 }
1801 return entry;
1802 }
1803
1804 return NULL;
1805}
1806
49461622 1807static ieee80211_rx_result debug_noinline
5cf121c3 1808ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67
JB
1809{
1810 struct ieee80211_hdr *hdr;
1811 u16 sc;
358c8d9d 1812 __le16 fc;
571ecf67
JB
1813 unsigned int frag, seq;
1814 struct ieee80211_fragment_entry *entry;
1815 struct sk_buff *skb;
554891e6 1816 struct ieee80211_rx_status *status;
571ecf67 1817
b73d70ad 1818 hdr = (struct ieee80211_hdr *)rx->skb->data;
358c8d9d 1819 fc = hdr->frame_control;
f7fbf70e
JC
1820
1821 if (ieee80211_is_ctl(fc))
1822 return RX_CONTINUE;
1823
571ecf67
JB
1824 sc = le16_to_cpu(hdr->seq_ctrl);
1825 frag = sc & IEEE80211_SCTL_FRAG;
1826
b8fff407 1827 if (is_multicast_ether_addr(hdr->addr1)) {
c206ca67 1828 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
d025933e 1829 goto out_no_led;
571ecf67 1830 }
b8fff407 1831
d025933e
AM
1832 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1833 goto out;
1834
571ecf67
JB
1835 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1836
e3cf8b3f
ZY
1837 if (skb_linearize(rx->skb))
1838 return RX_DROP_UNUSABLE;
1839
058897a4
AK
1840 /*
1841 * skb_linearize() might change the skb->data and
1842 * previously cached variables (in this case, hdr) need to
1843 * be refreshed with the new data.
1844 */
1845 hdr = (struct ieee80211_hdr *)rx->skb->data;
571ecf67
JB
1846 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1847
1848 if (frag == 0) {
1849 /* This is the first fragment of a new frame. */
1850 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
9e26297a 1851 rx->seqno_idx, &(rx->skb));
2b2ba0db
JM
1852 if (rx->key &&
1853 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
1854 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256) &&
358c8d9d 1855 ieee80211_has_protected(fc)) {
9e26297a 1856 int queue = rx->security_idx;
571ecf67
JB
1857 /* Store CCMP PN so that we can verify that the next
1858 * fragment has a sequential PN value. */
1859 entry->ccmp = 1;
1860 memcpy(entry->last_pn,
9190252c 1861 rx->key->u.ccmp.rx_pn[queue],
4325f6ca 1862 IEEE80211_CCMP_PN_LEN);
571ecf67 1863 }
9ae54c84 1864 return RX_QUEUED;
571ecf67
JB
1865 }
1866
1867 /* This is a fragment for a frame that should already be pending in
1868 * fragment cache. Add this fragment to the end of the pending entry.
1869 */
9e26297a
JB
1870 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1871 rx->seqno_idx, hdr);
571ecf67
JB
1872 if (!entry) {
1873 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
e4c26add 1874 return RX_DROP_MONITOR;
571ecf67
JB
1875 }
1876
1877 /* Verify that MPDUs within one MSDU have sequential PN values.
1878 * (IEEE 802.11i, 8.3.3.4.5) */
1879 if (entry->ccmp) {
1880 int i;
4325f6ca 1881 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
9190252c 1882 int queue;
2b2ba0db
JM
1883 if (!rx->key ||
1884 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
1885 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256))
e4c26add 1886 return RX_DROP_UNUSABLE;
4325f6ca
JB
1887 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1888 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
571ecf67
JB
1889 pn[i]++;
1890 if (pn[i])
1891 break;
1892 }
9e26297a 1893 queue = rx->security_idx;
9190252c 1894 rpn = rx->key->u.ccmp.rx_pn[queue];
4325f6ca 1895 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
e4c26add 1896 return RX_DROP_UNUSABLE;
4325f6ca 1897 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
571ecf67
JB
1898 }
1899
358c8d9d 1900 skb_pull(rx->skb, ieee80211_hdrlen(fc));
571ecf67
JB
1901 __skb_queue_tail(&entry->skb_list, rx->skb);
1902 entry->last_frag = frag;
1903 entry->extra_len += rx->skb->len;
358c8d9d 1904 if (ieee80211_has_morefrags(fc)) {
571ecf67 1905 rx->skb = NULL;
9ae54c84 1906 return RX_QUEUED;
571ecf67
JB
1907 }
1908
1909 rx->skb = __skb_dequeue(&entry->skb_list);
1910 if (skb_tailroom(rx->skb) < entry->extra_len) {
f1160434 1911 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
571ecf67
JB
1912 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1913 GFP_ATOMIC))) {
1914 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1915 __skb_queue_purge(&entry->skb_list);
e4c26add 1916 return RX_DROP_UNUSABLE;
571ecf67
JB
1917 }
1918 }
1919 while ((skb = __skb_dequeue(&entry->skb_list))) {
1920 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1921 dev_kfree_skb(skb);
1922 }
1923
1924 /* Complete frame has been reassembled - process it now */
554891e6
JB
1925 status = IEEE80211_SKB_RXCB(rx->skb);
1926 status->rx_flags |= IEEE80211_RX_FRAGMENTED;
571ecf67
JB
1927
1928 out:
d025933e
AM
1929 ieee80211_led_rx(rx->local);
1930 out_no_led:
571ecf67
JB
1931 if (rx->sta)
1932 rx->sta->rx_packets++;
9ae54c84 1933 return RX_CONTINUE;
571ecf67
JB
1934}
1935
1df332e8 1936static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 1937{
1df332e8 1938 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
76ee65bf 1939 return -EACCES;
571ecf67 1940
76ee65bf 1941 return 0;
571ecf67
JB
1942}
1943
1df332e8 1944static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
571ecf67 1945{
eb9fb5b8
JB
1946 struct sk_buff *skb = rx->skb;
1947 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1948
3017b80b 1949 /*
7848ba7d
JB
1950 * Pass through unencrypted frames if the hardware has
1951 * decrypted them already.
3017b80b 1952 */
eb9fb5b8 1953 if (status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1954 return 0;
571ecf67
JB
1955
1956 /* Drop unencrypted frames if key is set. */
358c8d9d
HH
1957 if (unlikely(!ieee80211_has_protected(fc) &&
1958 !ieee80211_is_nullfunc(fc) &&
e8f4fb7c 1959 ieee80211_is_data(fc) && rx->key))
76ee65bf 1960 return -EACCES;
bef5d1c7
JB
1961
1962 return 0;
1963}
1964
1df332e8 1965static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
bef5d1c7
JB
1966{
1967 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
e3efca0a 1968 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
bef5d1c7 1969 __le16 fc = hdr->frame_control;
bef5d1c7 1970
e3efca0a
JM
1971 /*
1972 * Pass through unencrypted frames if the hardware has
1973 * decrypted them already.
1974 */
1975 if (status->flag & RX_FLAG_DECRYPTED)
1976 return 0;
bef5d1c7 1977
c2c98fde 1978 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
d211e90e
JM
1979 if (unlikely(!ieee80211_has_protected(fc) &&
1980 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1981 rx->key)) {
6ff57cf8
JB
1982 if (ieee80211_is_deauth(fc) ||
1983 ieee80211_is_disassoc(fc))
1984 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1985 rx->skb->data,
1986 rx->skb->len);
f2ca3ea4 1987 return -EACCES;
cf4e594e 1988 }
f2ca3ea4 1989 /* BIP does not use Protected field, so need to check MMIE */
f64f9e71 1990 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1991 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
6ff57cf8
JB
1992 if (ieee80211_is_deauth(fc) ||
1993 ieee80211_is_disassoc(fc))
1994 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1995 rx->skb->data,
1996 rx->skb->len);
f2ca3ea4 1997 return -EACCES;
cf4e594e 1998 }
f2ca3ea4
JM
1999 /*
2000 * When using MFP, Action frames are not allowed prior to
2001 * having configured keys.
2002 */
2003 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
d8ca16db 2004 ieee80211_is_robust_mgmt_frame(rx->skb)))
f2ca3ea4
JM
2005 return -EACCES;
2006 }
b3fc9c6c 2007
76ee65bf 2008 return 0;
571ecf67
JB
2009}
2010
76ee65bf 2011static int
4114fa21 2012__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
571ecf67 2013{
eb9fb5b8 2014 struct ieee80211_sub_if_data *sdata = rx->sdata;
f14543ee 2015 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
fbb327c5
FF
2016 bool check_port_control = false;
2017 struct ethhdr *ehdr;
2018 int ret;
f14543ee 2019
4114fa21 2020 *port_control = false;
9bc383de
JB
2021 if (ieee80211_has_a4(hdr->frame_control) &&
2022 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
f14543ee 2023 return -1;
9bc383de 2024
fbb327c5
FF
2025 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2026 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2027
2028 if (!sdata->u.mgd.use_4addr)
2029 return -1;
2030 else
2031 check_port_control = true;
2032 }
2033
9bc383de 2034 if (is_multicast_ether_addr(hdr->addr1) &&
fbb327c5 2035 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
f14543ee 2036 return -1;
571ecf67 2037
fbb327c5 2038 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
4114fa21 2039 if (ret < 0)
fbb327c5
FF
2040 return ret;
2041
2042 ehdr = (struct ethhdr *) rx->skb->data;
4114fa21
FF
2043 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2044 *port_control = true;
2045 else if (check_port_control)
fbb327c5
FF
2046 return -1;
2047
2048 return 0;
76ee65bf 2049}
571ecf67 2050
ce3edf6d
JB
2051/*
2052 * requires that rx->skb is a frame with ethernet header
2053 */
358c8d9d 2054static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
ce3edf6d 2055{
c97c23e3 2056 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
ce3edf6d
JB
2057 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2058 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2059
2060 /*
2061 * Allow EAPOL frames to us/the PAE group address regardless
2062 * of whether the frame was encrypted or not.
2063 */
a621fa4d 2064 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
3bc7945e
JP
2065 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2066 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
ce3edf6d
JB
2067 return true;
2068
2069 if (ieee80211_802_1x_port_control(rx) ||
358c8d9d 2070 ieee80211_drop_unencrypted(rx, fc))
ce3edf6d
JB
2071 return false;
2072
2073 return true;
2074}
2075
2076/*
2077 * requires that rx->skb is a frame with ethernet header
2078 */
76ee65bf 2079static void
5cf121c3 2080ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf 2081{
eb9fb5b8
JB
2082 struct ieee80211_sub_if_data *sdata = rx->sdata;
2083 struct net_device *dev = sdata->dev;
76ee65bf 2084 struct sk_buff *skb, *xmit_skb;
ce3edf6d
JB
2085 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2086 struct sta_info *dsta;
571ecf67 2087
76ee65bf
RR
2088 skb = rx->skb;
2089 xmit_skb = NULL;
571ecf67 2090
5a490510
JB
2091 ieee80211_rx_stats(dev, skb->len);
2092
05c914fe
JB
2093 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2094 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
213cd118 2095 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
9bc383de 2096 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
ce3edf6d
JB
2097 if (is_multicast_ether_addr(ehdr->h_dest)) {
2098 /*
2099 * send multicast frames both to higher layers in
2100 * local net stack and back to the wireless medium
2101 */
76ee65bf 2102 xmit_skb = skb_copy(skb, GFP_ATOMIC);
e87cc472 2103 if (!xmit_skb)
bdcbd8e0 2104 net_info_ratelimited("%s: failed to clone multicast frame\n",
e87cc472 2105 dev->name);
571ecf67 2106 } else {
abe60632
JB
2107 dsta = sta_info_get(sdata, skb->data);
2108 if (dsta) {
ce3edf6d
JB
2109 /*
2110 * The destination station is associated to
2111 * this AP (in this VLAN), so send the frame
2112 * directly to it and do not pass it to local
2113 * net stack.
571ecf67 2114 */
76ee65bf 2115 xmit_skb = skb;
571ecf67
JB
2116 skb = NULL;
2117 }
571ecf67
JB
2118 }
2119 }
2120
59d9cb07 2121#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
9e890a1f
JB
2122 if (skb) {
2123 /* 'align' will only take the values 0 or 2 here since all
2124 * frames are required to be aligned to 2-byte boundaries
2125 * when being passed to mac80211; the code here works just
2126 * as well if that isn't true, but mac80211 assumes it can
2127 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
d1c3a37c 2128 */
9e890a1f
JB
2129 int align;
2130
2131 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
d1c3a37c
JB
2132 if (align) {
2133 if (WARN_ON(skb_headroom(skb) < 3)) {
2134 dev_kfree_skb(skb);
2135 skb = NULL;
2136 } else {
2137 u8 *data = skb->data;
8ce0b589
ZY
2138 size_t len = skb_headlen(skb);
2139 skb->data -= align;
2140 memmove(skb->data, data, len);
2141 skb_set_tail_pointer(skb, len);
d1c3a37c
JB
2142 }
2143 }
9e890a1f 2144 }
d1c3a37c
JB
2145#endif
2146
9e890a1f
JB
2147 if (skb) {
2148 /* deliver to local stack */
2149 skb->protocol = eth_type_trans(skb, dev);
2150 memset(skb->cb, 0, sizeof(skb->cb));
22d3a3c8
JB
2151 if (!(rx->flags & IEEE80211_RX_REORDER_TIMER) &&
2152 rx->local->napi)
06d181a8
JB
2153 napi_gro_receive(rx->local->napi, skb);
2154 else
2155 netif_receive_skb(skb);
571ecf67
JB
2156 }
2157
76ee65bf 2158 if (xmit_skb) {
aef6c928
HS
2159 /*
2160 * Send to wireless media and increase priority by 256 to
2161 * keep the received priority instead of reclassifying
2162 * the frame (see cfg80211_classify8021d).
2163 */
2164 xmit_skb->priority += 256;
f831e909 2165 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
2166 skb_reset_network_header(xmit_skb);
2167 skb_reset_mac_header(xmit_skb);
76ee65bf 2168 dev_queue_xmit(xmit_skb);
571ecf67 2169 }
76ee65bf
RR
2170}
2171
49461622 2172static ieee80211_rx_result debug_noinline
5cf121c3 2173ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
fd4c7f2f 2174{
eb9fb5b8 2175 struct net_device *dev = rx->sdata->dev;
eaf85ca7 2176 struct sk_buff *skb = rx->skb;
358c8d9d
HH
2177 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2178 __le16 fc = hdr->frame_control;
eaf85ca7 2179 struct sk_buff_head frame_list;
554891e6 2180 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
fd4c7f2f 2181
358c8d9d 2182 if (unlikely(!ieee80211_is_data(fc)))
9ae54c84 2183 return RX_CONTINUE;
fd4c7f2f 2184
358c8d9d 2185 if (unlikely(!ieee80211_is_data_present(fc)))
e4c26add 2186 return RX_DROP_MONITOR;
fd4c7f2f 2187
554891e6 2188 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
9ae54c84 2189 return RX_CONTINUE;
fd4c7f2f 2190
eaf85ca7
ZY
2191 if (ieee80211_has_a4(hdr->frame_control) &&
2192 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2193 !rx->sdata->u.vlan.sta)
e4c26add 2194 return RX_DROP_UNUSABLE;
fd4c7f2f 2195
eaf85ca7
ZY
2196 if (is_multicast_ether_addr(hdr->addr1) &&
2197 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2198 rx->sdata->u.vlan.sta) ||
2199 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2200 rx->sdata->u.mgd.use_4addr)))
e4c26add 2201 return RX_DROP_UNUSABLE;
fd4c7f2f 2202
eaf85ca7
ZY
2203 skb->dev = dev;
2204 __skb_queue_head_init(&frame_list);
fd4c7f2f 2205
e3cf8b3f
ZY
2206 if (skb_linearize(skb))
2207 return RX_DROP_UNUSABLE;
2208
eaf85ca7
ZY
2209 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2210 rx->sdata->vif.type,
8b3becad 2211 rx->local->hw.extra_tx_headroom, true);
fd4c7f2f 2212
eaf85ca7
ZY
2213 while (!skb_queue_empty(&frame_list)) {
2214 rx->skb = __skb_dequeue(&frame_list);
fd4c7f2f 2215
358c8d9d 2216 if (!ieee80211_frame_allowed(rx, fc)) {
eaf85ca7 2217 dev_kfree_skb(rx->skb);
ce3edf6d
JB
2218 continue;
2219 }
fd4c7f2f
RR
2220
2221 ieee80211_deliver_skb(rx);
2222 }
2223
9ae54c84 2224 return RX_QUEUED;
fd4c7f2f
RR
2225}
2226
bf94e17b 2227#ifdef CONFIG_MAC80211_MESH
b0dee578 2228static ieee80211_rx_result
e32f85f7
LCC
2229ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2230{
30789eb6
TP
2231 struct ieee80211_hdr *fwd_hdr, *hdr;
2232 struct ieee80211_tx_info *info;
e32f85f7 2233 struct ieee80211s_hdr *mesh_hdr;
e32f85f7 2234 struct sk_buff *skb = rx->skb, *fwd_skb;
3b8d81e0 2235 struct ieee80211_local *local = rx->local;
eb9fb5b8 2236 struct ieee80211_sub_if_data *sdata = rx->sdata;
30789eb6 2237 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
30789eb6 2238 u16 q, hdrlen;
e32f85f7
LCC
2239
2240 hdr = (struct ieee80211_hdr *) skb->data;
2241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
9b395bc3
JB
2242
2243 /* make sure fixed part of mesh header is there, also checks skb len */
2244 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2245 return RX_DROP_MONITOR;
2246
2247 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2248
2249 /* make sure full mesh header is there, also checks skb len */
2250 if (!pskb_may_pull(rx->skb,
2251 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2252 return RX_DROP_MONITOR;
2253
2254 /* reload pointers */
2255 hdr = (struct ieee80211_hdr *) skb->data;
e32f85f7
LCC
2256 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2257
d0c22119
BC
2258 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2259 return RX_DROP_MONITOR;
2260
2157fdd6
TP
2261 /* frame is in RMC, don't forward */
2262 if (ieee80211_is_data(hdr->frame_control) &&
2263 is_multicast_ether_addr(hdr->addr1) &&
bf7cd94d 2264 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2157fdd6
TP
2265 return RX_DROP_MONITOR;
2266
5c90067c 2267 if (!ieee80211_is_data(hdr->frame_control))
e32f85f7
LCC
2268 return RX_CONTINUE;
2269
2270 if (!mesh_hdr->ttl)
e32f85f7
LCC
2271 return RX_DROP_MONITOR;
2272
43b7b314 2273 if (mesh_hdr->flags & MESH_FLAGS_AE) {
79617dee 2274 struct mesh_path *mppath;
43b7b314
JC
2275 char *proxied_addr;
2276 char *mpp_addr;
2277
2278 if (is_multicast_ether_addr(hdr->addr1)) {
2279 mpp_addr = hdr->addr3;
2280 proxied_addr = mesh_hdr->eaddr1;
9b395bc3
JB
2281 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2282 /* has_a4 already checked in ieee80211_rx_mesh_check */
43b7b314
JC
2283 mpp_addr = hdr->addr4;
2284 proxied_addr = mesh_hdr->eaddr2;
9b395bc3
JB
2285 } else {
2286 return RX_DROP_MONITOR;
43b7b314 2287 }
79617dee 2288
79617dee 2289 rcu_read_lock();
bf7cd94d 2290 mppath = mpp_path_lookup(sdata, proxied_addr);
79617dee 2291 if (!mppath) {
bf7cd94d 2292 mpp_path_add(sdata, proxied_addr, mpp_addr);
79617dee
Y
2293 } else {
2294 spin_lock_bh(&mppath->state_lock);
b203ca39 2295 if (!ether_addr_equal(mppath->mpp, mpp_addr))
43b7b314 2296 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
79617dee
Y
2297 spin_unlock_bh(&mppath->state_lock);
2298 }
2299 rcu_read_unlock();
2300 }
2301
3c5772a5
JC
2302 /* Frame has reached destination. Don't forward */
2303 if (!is_multicast_ether_addr(hdr->addr1) &&
b203ca39 2304 ether_addr_equal(sdata->vif.addr, hdr->addr3))
e32f85f7
LCC
2305 return RX_CONTINUE;
2306
00e96dec 2307 q = ieee80211_select_queue_80211(sdata, skb, hdr);
d3c1597b 2308 if (ieee80211_queue_stopped(&local->hw, q)) {
30789eb6 2309 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
d3c1597b
TP
2310 return RX_DROP_MONITOR;
2311 }
2312 skb_set_queue_mapping(skb, q);
e32f85f7 2313
30789eb6
TP
2314 if (!--mesh_hdr->ttl) {
2315 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2ac64cd1 2316 goto out;
30789eb6
TP
2317 }
2318
d665508b
CYY
2319 if (!ifmsh->mshcfg.dot11MeshForwarding)
2320 goto out;
2321
30789eb6
TP
2322 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2323 if (!fwd_skb) {
bdcbd8e0 2324 net_info_ratelimited("%s: failed to clone mesh frame\n",
e87cc472 2325 sdata->name);
30789eb6
TP
2326 goto out;
2327 }
2328
2329 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
9d6d6f49 2330 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
30789eb6
TP
2331 info = IEEE80211_SKB_CB(fwd_skb);
2332 memset(info, 0, sizeof(*info));
2333 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2334 info->control.vif = &rx->sdata->vif;
2335 info->control.jiffies = jiffies;
2336 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2337 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2338 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
3f52b7e3
MP
2339 /* update power mode indication when forwarding */
2340 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
bf7cd94d 2341 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
3f52b7e3 2342 /* mesh power mode flags updated in mesh_nexthop_lookup */
30789eb6
TP
2343 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2344 } else {
2345 /* unable to resolve next hop */
bf7cd94d 2346 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
f63f8421
CYY
2347 fwd_hdr->addr3, 0,
2348 WLAN_REASON_MESH_PATH_NOFORWARD,
2349 fwd_hdr->addr2);
30789eb6 2350 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
74b8cc3d 2351 kfree_skb(fwd_skb);
30789eb6 2352 return RX_DROP_MONITOR;
e32f85f7
LCC
2353 }
2354
30789eb6
TP
2355 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2356 ieee80211_add_pending_skb(local, fwd_skb);
b51aff05 2357 out:
df140465 2358 if (is_multicast_ether_addr(hdr->addr1))
e32f85f7 2359 return RX_CONTINUE;
df140465 2360 return RX_DROP_MONITOR;
e32f85f7 2361}
bf94e17b 2362#endif
e32f85f7 2363
49461622 2364static ieee80211_rx_result debug_noinline
5cf121c3 2365ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf 2366{
eb9fb5b8 2367 struct ieee80211_sub_if_data *sdata = rx->sdata;
e15276a4 2368 struct ieee80211_local *local = rx->local;
eb9fb5b8 2369 struct net_device *dev = sdata->dev;
358c8d9d
HH
2370 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2371 __le16 fc = hdr->frame_control;
4114fa21 2372 bool port_control;
ce3edf6d 2373 int err;
76ee65bf 2374
358c8d9d 2375 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
9ae54c84 2376 return RX_CONTINUE;
76ee65bf 2377
358c8d9d 2378 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
e4c26add 2379 return RX_DROP_MONITOR;
76ee65bf 2380
79c892b8 2381 if (rx->sta) {
3d6dc343 2382 /* The seqno index has the same property as needed
79c892b8
JB
2383 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2384 * for non-QoS-data frames. Here we know it's a data
2385 * frame, so count MSDUs.
2386 */
3d6dc343 2387 rx->sta->rx_msdu[rx->seqno_idx]++;
79c892b8
JB
2388 }
2389
f14543ee 2390 /*
e7f4a940
JB
2391 * Send unexpected-4addr-frame event to hostapd. For older versions,
2392 * also drop the frame to cooked monitor interfaces.
f14543ee
FF
2393 */
2394 if (ieee80211_has_a4(hdr->frame_control) &&
e7f4a940
JB
2395 sdata->vif.type == NL80211_IFTYPE_AP) {
2396 if (rx->sta &&
2397 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2398 cfg80211_rx_unexpected_4addr_frame(
2399 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
f14543ee 2400 return RX_DROP_MONITOR;
e7f4a940 2401 }
f14543ee 2402
4114fa21 2403 err = __ieee80211_data_to_8023(rx, &port_control);
76ee65bf 2404 if (unlikely(err))
e4c26add 2405 return RX_DROP_UNUSABLE;
76ee65bf 2406
358c8d9d 2407 if (!ieee80211_frame_allowed(rx, fc))
e4c26add 2408 return RX_DROP_MONITOR;
ce3edf6d 2409
8a4d32f3
AN
2410 /* directly handle TDLS channel switch requests/responses */
2411 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2412 cpu_to_be16(ETH_P_TDLS))) {
2413 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2414
2415 if (pskb_may_pull(rx->skb,
2416 offsetof(struct ieee80211_tdls_data, u)) &&
2417 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2418 tf->category == WLAN_CATEGORY_TDLS &&
2419 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2420 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2421 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TDLS_CHSW;
2422 skb_queue_tail(&sdata->skb_queue, rx->skb);
2423 ieee80211_queue_work(&rx->local->hw, &sdata->work);
2424 if (rx->sta)
2425 rx->sta->rx_packets++;
2426
2427 return RX_QUEUED;
2428 }
2429 }
2430
4114fa21
FF
2431 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2432 unlikely(port_control) && sdata->bss) {
2433 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2434 u.ap);
2435 dev = sdata->dev;
2436 rx->sdata = sdata;
2437 }
2438
76ee65bf
RR
2439 rx->skb->dev = dev;
2440
08ca944e 2441 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
8c99f691
RM
2442 !is_multicast_ether_addr(
2443 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2444 (!local->scanning &&
2445 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
e15276a4
VN
2446 mod_timer(&local->dynamic_ps_timer, jiffies +
2447 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2448 }
2449
76ee65bf 2450 ieee80211_deliver_skb(rx);
571ecf67 2451
9ae54c84 2452 return RX_QUEUED;
571ecf67
JB
2453}
2454
49461622 2455static ieee80211_rx_result debug_noinline
f9e124fb 2456ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
71364716 2457{
71364716 2458 struct sk_buff *skb = rx->skb;
a7767f95 2459 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
71364716
RR
2460 struct tid_ampdu_rx *tid_agg_rx;
2461 u16 start_seq_num;
2462 u16 tid;
2463
a7767f95 2464 if (likely(!ieee80211_is_ctl(bar->frame_control)))
9ae54c84 2465 return RX_CONTINUE;
71364716 2466
a7767f95 2467 if (ieee80211_is_back_req(bar->frame_control)) {
8ae5977f
JB
2468 struct {
2469 __le16 control, start_seq_num;
2470 } __packed bar_data;
6382246e
EG
2471 struct ieee80211_event event = {
2472 .type = BAR_RX_EVENT,
2473 };
8ae5977f 2474
71364716 2475 if (!rx->sta)
a02ae758 2476 return RX_DROP_MONITOR;
8ae5977f
JB
2477
2478 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2479 &bar_data, sizeof(bar_data)))
2480 return RX_DROP_MONITOR;
2481
8ae5977f 2482 tid = le16_to_cpu(bar_data.control) >> 12;
a87f736d
JB
2483
2484 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2485 if (!tid_agg_rx)
a02ae758 2486 return RX_DROP_MONITOR;
71364716 2487
8ae5977f 2488 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
6382246e
EG
2489 event.u.ba.tid = tid;
2490 event.u.ba.ssn = start_seq_num;
2491 event.u.ba.sta = &rx->sta->sta;
71364716
RR
2492
2493 /* reset session timer */
20ad19d0
JB
2494 if (tid_agg_rx->timeout)
2495 mod_timer(&tid_agg_rx->session_timer,
2496 TU_TO_EXP_TIME(tid_agg_rx->timeout));
71364716 2497
dd318575 2498 spin_lock(&tid_agg_rx->reorder_lock);
a02ae758 2499 /* release stored frames up to start of BAR */
d3b2fb53 2500 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
f9e124fb 2501 start_seq_num, frames);
dd318575
JB
2502 spin_unlock(&tid_agg_rx->reorder_lock);
2503
6382246e
EG
2504 drv_event_callback(rx->local, rx->sdata, &event);
2505
a02ae758
JB
2506 kfree_skb(skb);
2507 return RX_QUEUED;
71364716
RR
2508 }
2509
08daecae
JB
2510 /*
2511 * After this point, we only want management frames,
2512 * so we can drop all remaining control frames to
2513 * cooked monitor interfaces.
2514 */
2515 return RX_DROP_MONITOR;
71364716
RR
2516}
2517
f4f727a6
JM
2518static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2519 struct ieee80211_mgmt *mgmt,
2520 size_t len)
fea14732
JM
2521{
2522 struct ieee80211_local *local = sdata->local;
2523 struct sk_buff *skb;
2524 struct ieee80211_mgmt *resp;
2525
b203ca39 2526 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
fea14732
JM
2527 /* Not to own unicast address */
2528 return;
2529 }
2530
b203ca39
JP
2531 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2532 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
77fdaa12 2533 /* Not from the current AP or not associated yet. */
fea14732
JM
2534 return;
2535 }
2536
2537 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2538 /* Too short SA Query request frame */
2539 return;
2540 }
2541
2542 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2543 if (skb == NULL)
2544 return;
2545
2546 skb_reserve(skb, local->hw.extra_tx_headroom);
2547 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2548 memset(resp, 0, 24);
2549 memcpy(resp->da, mgmt->sa, ETH_ALEN);
47846c9b 2550 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
46900298 2551 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
fea14732
JM
2552 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2553 IEEE80211_STYPE_ACTION);
2554 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2555 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2556 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2557 memcpy(resp->u.action.u.sa_query.trans_id,
2558 mgmt->u.action.u.sa_query.trans_id,
2559 WLAN_SA_QUERY_TR_ID_LEN);
2560
62ae67be 2561 ieee80211_tx_skb(sdata, skb);
fea14732
JM
2562}
2563
2e161f78
JB
2564static ieee80211_rx_result debug_noinline
2565ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2566{
2567 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2568 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2569
2570 /*
2571 * From here on, look only at management frames.
2572 * Data and control frames are already handled,
2573 * and unknown (reserved) frames are useless.
2574 */
2575 if (rx->skb->len < 24)
2576 return RX_DROP_MONITOR;
2577
2578 if (!ieee80211_is_mgmt(mgmt->frame_control))
2579 return RX_DROP_MONITOR;
2580
ee971924
JB
2581 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2582 ieee80211_is_beacon(mgmt->frame_control) &&
2583 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
804483e9
JB
2584 int sig = 0;
2585
30686bf7 2586 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
804483e9
JB
2587 sig = status->signal;
2588
ee971924
JB
2589 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2590 rx->skb->data, rx->skb->len,
37c73b5f 2591 status->freq, sig);
ee971924
JB
2592 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2593 }
2594
2e161f78
JB
2595 if (ieee80211_drop_unencrypted_mgmt(rx))
2596 return RX_DROP_UNUSABLE;
2597
2598 return RX_CONTINUE;
2599}
2600
de1ede7a
JB
2601static ieee80211_rx_result debug_noinline
2602ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2603{
2604 struct ieee80211_local *local = rx->local;
eb9fb5b8 2605 struct ieee80211_sub_if_data *sdata = rx->sdata;
de1ede7a 2606 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2607 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
de1ede7a
JB
2608 int len = rx->skb->len;
2609
2610 if (!ieee80211_is_action(mgmt->frame_control))
2611 return RX_CONTINUE;
2612
026331c4
JM
2613 /* drop too small frames */
2614 if (len < IEEE80211_MIN_ACTION_SIZE)
84040805 2615 return RX_DROP_UNUSABLE;
de1ede7a 2616
815b8092 2617 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
cd7760e6
SW
2618 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2619 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
84040805 2620 return RX_DROP_UNUSABLE;
de1ede7a 2621
de1ede7a 2622 switch (mgmt->u.action.category) {
1d8d3dec
JB
2623 case WLAN_CATEGORY_HT:
2624 /* reject HT action frames from stations not supporting HT */
2625 if (!rx->sta->sta.ht_cap.ht_supported)
2626 goto invalid;
2627
2628 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2629 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2630 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2631 sdata->vif.type != NL80211_IFTYPE_AP &&
2632 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2633 break;
2634
ec61cd63 2635 /* verify action & smps_control/chanwidth are present */
1d8d3dec
JB
2636 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2637 goto invalid;
2638
2639 switch (mgmt->u.action.u.ht_smps.action) {
2640 case WLAN_HT_ACTION_SMPS: {
2641 struct ieee80211_supported_band *sband;
af0ed69b 2642 enum ieee80211_smps_mode smps_mode;
1d8d3dec
JB
2643
2644 /* convert to HT capability */
2645 switch (mgmt->u.action.u.ht_smps.smps_control) {
2646 case WLAN_HT_SMPS_CONTROL_DISABLED:
af0ed69b 2647 smps_mode = IEEE80211_SMPS_OFF;
1d8d3dec
JB
2648 break;
2649 case WLAN_HT_SMPS_CONTROL_STATIC:
af0ed69b 2650 smps_mode = IEEE80211_SMPS_STATIC;
1d8d3dec
JB
2651 break;
2652 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
af0ed69b 2653 smps_mode = IEEE80211_SMPS_DYNAMIC;
1d8d3dec
JB
2654 break;
2655 default:
2656 goto invalid;
2657 }
1d8d3dec
JB
2658
2659 /* if no change do nothing */
af0ed69b 2660 if (rx->sta->sta.smps_mode == smps_mode)
1d8d3dec 2661 goto handled;
af0ed69b 2662 rx->sta->sta.smps_mode = smps_mode;
1d8d3dec
JB
2663
2664 sband = rx->local->hw.wiphy->bands[status->band];
2665
64f68e5d
JB
2666 rate_control_rate_update(local, sband, rx->sta,
2667 IEEE80211_RC_SMPS_CHANGED);
1d8d3dec
JB
2668 goto handled;
2669 }
ec61cd63
JB
2670 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2671 struct ieee80211_supported_band *sband;
2672 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
1c45c5ce 2673 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
ec61cd63
JB
2674
2675 /* If it doesn't support 40 MHz it can't change ... */
e1a0c6b3
JB
2676 if (!(rx->sta->sta.ht_cap.cap &
2677 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
ec61cd63
JB
2678 goto handled;
2679
e1a0c6b3 2680 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
1c45c5ce 2681 max_bw = IEEE80211_STA_RX_BW_20;
e1a0c6b3 2682 else
1c45c5ce
EP
2683 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2684
2685 /* set cur_max_bandwidth and recalc sta bw */
2686 rx->sta->cur_max_bandwidth = max_bw;
2687 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
ec61cd63 2688
e1a0c6b3 2689 if (rx->sta->sta.bandwidth == new_bw)
ec61cd63
JB
2690 goto handled;
2691
1c45c5ce 2692 rx->sta->sta.bandwidth = new_bw;
ec61cd63
JB
2693 sband = rx->local->hw.wiphy->bands[status->band];
2694
2695 rate_control_rate_update(local, sband, rx->sta,
2696 IEEE80211_RC_BW_CHANGED);
2697 goto handled;
2698 }
1d8d3dec
JB
2699 default:
2700 goto invalid;
2701 }
2702
0af83d3d 2703 break;
1b3a2e49
JB
2704 case WLAN_CATEGORY_PUBLIC:
2705 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2706 goto invalid;
2707 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2708 break;
2709 if (!rx->sta)
2710 break;
2711 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2712 break;
2713 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2714 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2715 break;
2716 if (len < offsetof(struct ieee80211_mgmt,
2717 u.action.u.ext_chan_switch.variable))
2718 goto invalid;
2719 goto queue;
0af83d3d
JB
2720 case WLAN_CATEGORY_VHT:
2721 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2722 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2723 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2724 sdata->vif.type != NL80211_IFTYPE_AP &&
2725 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2726 break;
2727
2728 /* verify action code is present */
2729 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2730 goto invalid;
2731
2732 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2733 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2734 u8 opmode;
2735
2736 /* verify opmode is present */
2737 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2738 goto invalid;
2739
2740 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2741
2742 ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
bee7f586
JB
2743 opmode, status->band,
2744 false);
0af83d3d
JB
2745 goto handled;
2746 }
2747 default:
2748 break;
2749 }
1d8d3dec 2750 break;
de1ede7a 2751 case WLAN_CATEGORY_BACK:
8abd3f9b 2752 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
ae2772b3 2753 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
8abd3f9b 2754 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
13c40c54
AS
2755 sdata->vif.type != NL80211_IFTYPE_AP &&
2756 sdata->vif.type != NL80211_IFTYPE_ADHOC)
84040805 2757 break;
8abd3f9b 2758
026331c4
JM
2759 /* verify action_code is present */
2760 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2761 break;
2762
de1ede7a
JB
2763 switch (mgmt->u.action.u.addba_req.action_code) {
2764 case WLAN_ACTION_ADDBA_REQ:
2765 if (len < (IEEE80211_MIN_ACTION_SIZE +
2766 sizeof(mgmt->u.action.u.addba_req)))
bed7ee6e
JB
2767 goto invalid;
2768 break;
de1ede7a
JB
2769 case WLAN_ACTION_ADDBA_RESP:
2770 if (len < (IEEE80211_MIN_ACTION_SIZE +
2771 sizeof(mgmt->u.action.u.addba_resp)))
bed7ee6e
JB
2772 goto invalid;
2773 break;
de1ede7a
JB
2774 case WLAN_ACTION_DELBA:
2775 if (len < (IEEE80211_MIN_ACTION_SIZE +
2776 sizeof(mgmt->u.action.u.delba)))
bed7ee6e
JB
2777 goto invalid;
2778 break;
2779 default:
2780 goto invalid;
de1ede7a 2781 }
bed7ee6e 2782
8b58ff83 2783 goto queue;
39192c0b 2784 case WLAN_CATEGORY_SPECTRUM_MGMT:
026331c4
JM
2785 /* verify action_code is present */
2786 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2787 break;
2788
39192c0b
JB
2789 switch (mgmt->u.action.u.measurement.action_code) {
2790 case WLAN_ACTION_SPCT_MSR_REQ:
cd7760e6
SW
2791 if (status->band != IEEE80211_BAND_5GHZ)
2792 break;
2793
39192c0b
JB
2794 if (len < (IEEE80211_MIN_ACTION_SIZE +
2795 sizeof(mgmt->u.action.u.measurement)))
84040805 2796 break;
cd7760e6
SW
2797
2798 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2799 break;
2800
39192c0b 2801 ieee80211_process_measurement_req(sdata, mgmt, len);
84040805 2802 goto handled;
cd7760e6
SW
2803 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2804 u8 *bssid;
2805 if (len < (IEEE80211_MIN_ACTION_SIZE +
2806 sizeof(mgmt->u.action.u.chan_switch)))
84040805 2807 break;
cc32abd4 2808
cd7760e6 2809 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
b8456a14
CYY
2810 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2811 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
cd7760e6
SW
2812 break;
2813
2814 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2815 bssid = sdata->u.mgd.bssid;
2816 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2817 bssid = sdata->u.ibss.bssid;
b8456a14
CYY
2818 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2819 bssid = mgmt->sa;
cd7760e6
SW
2820 else
2821 break;
2822
2823 if (!ether_addr_equal(mgmt->bssid, bssid))
84040805 2824 break;
c481ec97 2825
8b58ff83 2826 goto queue;
cd7760e6 2827 }
39192c0b
JB
2828 }
2829 break;
fea14732
JM
2830 case WLAN_CATEGORY_SA_QUERY:
2831 if (len < (IEEE80211_MIN_ACTION_SIZE +
2832 sizeof(mgmt->u.action.u.sa_query)))
84040805
JB
2833 break;
2834
fea14732
JM
2835 switch (mgmt->u.action.u.sa_query.action) {
2836 case WLAN_ACTION_SA_QUERY_REQUEST:
2837 if (sdata->vif.type != NL80211_IFTYPE_STATION)
84040805 2838 break;
fea14732 2839 ieee80211_process_sa_query_req(sdata, mgmt, len);
84040805 2840 goto handled;
fea14732
JM
2841 }
2842 break;
8db09850 2843 case WLAN_CATEGORY_SELF_PROTECTED:
9b395bc3
JB
2844 if (len < (IEEE80211_MIN_ACTION_SIZE +
2845 sizeof(mgmt->u.action.u.self_prot.action_code)))
2846 break;
2847
8db09850
TP
2848 switch (mgmt->u.action.u.self_prot.action_code) {
2849 case WLAN_SP_MESH_PEERING_OPEN:
2850 case WLAN_SP_MESH_PEERING_CLOSE:
2851 case WLAN_SP_MESH_PEERING_CONFIRM:
2852 if (!ieee80211_vif_is_mesh(&sdata->vif))
2853 goto invalid;
a6dad6a2 2854 if (sdata->u.mesh.user_mpm)
8db09850
TP
2855 /* userspace handles this frame */
2856 break;
2857 goto queue;
2858 case WLAN_SP_MGK_INFORM:
2859 case WLAN_SP_MGK_ACK:
2860 if (!ieee80211_vif_is_mesh(&sdata->vif))
2861 goto invalid;
2862 break;
2863 }
2864 break;
d3aaec8a 2865 case WLAN_CATEGORY_MESH_ACTION:
9b395bc3
JB
2866 if (len < (IEEE80211_MIN_ACTION_SIZE +
2867 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2868 break;
2869
77a121c3
JB
2870 if (!ieee80211_vif_is_mesh(&sdata->vif))
2871 break;
25d49e4d 2872 if (mesh_action_is_path_sel(mgmt) &&
1df332e8 2873 !mesh_path_sel_is_hwmp(sdata))
c7108a71
JC
2874 break;
2875 goto queue;
84040805 2876 }
026331c4 2877
2e161f78
JB
2878 return RX_CONTINUE;
2879
bed7ee6e 2880 invalid:
554891e6 2881 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2e161f78
JB
2882 /* will return in the next handlers */
2883 return RX_CONTINUE;
2884
2885 handled:
2886 if (rx->sta)
2887 rx->sta->rx_packets++;
2888 dev_kfree_skb(rx->skb);
2889 return RX_QUEUED;
2890
2891 queue:
2892 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2893 skb_queue_tail(&sdata->skb_queue, rx->skb);
2894 ieee80211_queue_work(&local->hw, &sdata->work);
2895 if (rx->sta)
2896 rx->sta->rx_packets++;
2897 return RX_QUEUED;
2898}
2899
2900static ieee80211_rx_result debug_noinline
2901ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2902{
554891e6 2903 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
804483e9 2904 int sig = 0;
2e161f78
JB
2905
2906 /* skip known-bad action frames and return them in the next handler */
554891e6 2907 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2e161f78 2908 return RX_CONTINUE;
d7907448 2909
026331c4
JM
2910 /*
2911 * Getting here means the kernel doesn't know how to handle
2912 * it, but maybe userspace does ... include returned frames
2913 * so userspace can register for those to know whether ones
2914 * it transmitted were processed or returned.
2915 */
026331c4 2916
30686bf7 2917 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
804483e9
JB
2918 sig = status->signal;
2919
71bbc994 2920 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
970fdfa8 2921 rx->skb->data, rx->skb->len, 0)) {
2e161f78
JB
2922 if (rx->sta)
2923 rx->sta->rx_packets++;
2924 dev_kfree_skb(rx->skb);
2925 return RX_QUEUED;
2926 }
2927
2e161f78
JB
2928 return RX_CONTINUE;
2929}
2930
2931static ieee80211_rx_result debug_noinline
2932ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2933{
2934 struct ieee80211_local *local = rx->local;
2935 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2936 struct sk_buff *nskb;
2937 struct ieee80211_sub_if_data *sdata = rx->sdata;
554891e6 2938 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2939
2940 if (!ieee80211_is_action(mgmt->frame_control))
2941 return RX_CONTINUE;
2942
2943 /*
2944 * For AP mode, hostapd is responsible for handling any action
2945 * frames that we didn't handle, including returning unknown
2946 * ones. For all other modes we will return them to the sender,
2947 * setting the 0x80 bit in the action category, as required by
4b5ebccc 2948 * 802.11-2012 9.24.4.
2e161f78
JB
2949 * Newer versions of hostapd shall also use the management frame
2950 * registration mechanisms, but older ones still use cooked
2951 * monitor interfaces so push all frames there.
2952 */
554891e6 2953 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2e161f78
JB
2954 (sdata->vif.type == NL80211_IFTYPE_AP ||
2955 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2956 return RX_DROP_MONITOR;
026331c4 2957
4b5ebccc
JB
2958 if (is_multicast_ether_addr(mgmt->da))
2959 return RX_DROP_MONITOR;
2960
84040805
JB
2961 /* do not return rejected action frames */
2962 if (mgmt->u.action.category & 0x80)
2963 return RX_DROP_UNUSABLE;
2964
2965 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2966 GFP_ATOMIC);
2967 if (nskb) {
292b4df6 2968 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
84040805 2969
292b4df6
JL
2970 nmgmt->u.action.category |= 0x80;
2971 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2972 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
84040805
JB
2973
2974 memset(nskb->cb, 0, sizeof(nskb->cb));
2975
07e5a5f5
JB
2976 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
2977 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
2978
2979 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
2980 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
2981 IEEE80211_TX_CTL_NO_CCK_RATE;
30686bf7 2982 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
07e5a5f5
JB
2983 info->hw_queue =
2984 local->hw.offchannel_tx_hw_queue;
2985 }
2986
2987 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
2988 status->band);
de1ede7a 2989 }
39192c0b
JB
2990 dev_kfree_skb(rx->skb);
2991 return RX_QUEUED;
de1ede7a
JB
2992}
2993
49461622 2994static ieee80211_rx_result debug_noinline
5cf121c3 2995ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67 2996{
eb9fb5b8 2997 struct ieee80211_sub_if_data *sdata = rx->sdata;
77a121c3
JB
2998 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2999 __le16 stype;
571ecf67 3000
77a121c3 3001 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
472dbc45 3002
77a121c3
JB
3003 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3004 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
239281f8 3005 sdata->vif.type != NL80211_IFTYPE_OCB &&
77a121c3
JB
3006 sdata->vif.type != NL80211_IFTYPE_STATION)
3007 return RX_DROP_MONITOR;
472dbc45 3008
77a121c3 3009 switch (stype) {
66e67e41 3010 case cpu_to_le16(IEEE80211_STYPE_AUTH):
77a121c3
JB
3011 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3012 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3013 /* process for all: mesh, mlme, ibss */
3014 break;
66e67e41
JB
3015 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3016 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
77a121c3
JB
3017 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3018 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2c31333a
CL
3019 if (is_multicast_ether_addr(mgmt->da) &&
3020 !is_broadcast_ether_addr(mgmt->da))
3021 return RX_DROP_MONITOR;
3022
77a121c3
JB
3023 /* process only for station */
3024 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3025 return RX_DROP_MONITOR;
3026 break;
3027 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
9fb04b50
TP
3028 /* process only for ibss and mesh */
3029 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3030 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
77a121c3
JB
3031 return RX_DROP_MONITOR;
3032 break;
3033 default:
3034 return RX_DROP_MONITOR;
3035 }
f9d540ee 3036
77a121c3 3037 /* queue up frame and kick off work to process it */
c1475ca9 3038 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
77a121c3
JB
3039 skb_queue_tail(&sdata->skb_queue, rx->skb);
3040 ieee80211_queue_work(&rx->local->hw, &sdata->work);
8b58ff83
JB
3041 if (rx->sta)
3042 rx->sta->rx_packets++;
46900298 3043
77a121c3 3044 return RX_QUEUED;
571ecf67
JB
3045}
3046
5cf121c3 3047/* TODO: use IEEE80211_RX_FRAGMENTED */
5f0b7de5
JB
3048static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3049 struct ieee80211_rate *rate)
3d30d949
MW
3050{
3051 struct ieee80211_sub_if_data *sdata;
3052 struct ieee80211_local *local = rx->local;
3d30d949
MW
3053 struct sk_buff *skb = rx->skb, *skb2;
3054 struct net_device *prev_dev = NULL;
eb9fb5b8 3055 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
293702a3 3056 int needed_headroom;
3d30d949 3057
554891e6
JB
3058 /*
3059 * If cooked monitor has been processed already, then
3060 * don't do it again. If not, set the flag.
3061 */
3062 if (rx->flags & IEEE80211_RX_CMNTR)
7c1e1831 3063 goto out_free_skb;
554891e6 3064 rx->flags |= IEEE80211_RX_CMNTR;
7c1e1831 3065
152c477a
JB
3066 /* If there are no cooked monitor interfaces, just free the SKB */
3067 if (!local->cooked_mntrs)
3068 goto out_free_skb;
3069
1f7bba79
JB
3070 /* vendor data is long removed here */
3071 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
293702a3 3072 /* room for the radiotap header based on driver features */
1f7bba79 3073 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3d30d949 3074
293702a3
JB
3075 if (skb_headroom(skb) < needed_headroom &&
3076 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3077 goto out_free_skb;
3d30d949 3078
293702a3 3079 /* prepend radiotap information */
973ef21a
FF
3080 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3081 false);
3d30d949
MW
3082
3083 skb_set_mac_header(skb, 0);
3084 skb->ip_summed = CHECKSUM_UNNECESSARY;
3085 skb->pkt_type = PACKET_OTHERHOST;
3086 skb->protocol = htons(ETH_P_802_2);
3087
3088 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
9607e6b6 3089 if (!ieee80211_sdata_running(sdata))
3d30d949
MW
3090 continue;
3091
05c914fe 3092 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3d30d949
MW
3093 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
3094 continue;
3095
3096 if (prev_dev) {
3097 skb2 = skb_clone(skb, GFP_ATOMIC);
3098 if (skb2) {
3099 skb2->dev = prev_dev;
5548a8a1 3100 netif_receive_skb(skb2);
3d30d949
MW
3101 }
3102 }
3103
3104 prev_dev = sdata->dev;
5a490510 3105 ieee80211_rx_stats(sdata->dev, skb->len);
3d30d949
MW
3106 }
3107
3108 if (prev_dev) {
3109 skb->dev = prev_dev;
5548a8a1 3110 netif_receive_skb(skb);
554891e6
JB
3111 return;
3112 }
3d30d949
MW
3113
3114 out_free_skb:
3115 dev_kfree_skb(skb);
3116}
3117
aa0c8636
CL
3118static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3119 ieee80211_rx_result res)
3120{
3121 switch (res) {
3122 case RX_DROP_MONITOR:
3123 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3124 if (rx->sta)
3125 rx->sta->rx_dropped++;
3126 /* fall through */
3127 case RX_CONTINUE: {
3128 struct ieee80211_rate *rate = NULL;
3129 struct ieee80211_supported_band *sband;
3130 struct ieee80211_rx_status *status;
3131
3132 status = IEEE80211_SKB_RXCB((rx->skb));
3133
3134 sband = rx->local->hw.wiphy->bands[status->band];
5614618e
JB
3135 if (!(status->flag & RX_FLAG_HT) &&
3136 !(status->flag & RX_FLAG_VHT))
aa0c8636
CL
3137 rate = &sband->bitrates[status->rate_idx];
3138
3139 ieee80211_rx_cooked_monitor(rx, rate);
3140 break;
3141 }
3142 case RX_DROP_UNUSABLE:
3143 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3144 if (rx->sta)
3145 rx->sta->rx_dropped++;
3146 dev_kfree_skb(rx->skb);
3147 break;
3148 case RX_QUEUED:
3149 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3150 break;
3151 }
3152}
571ecf67 3153
f9e124fb
CL
3154static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3155 struct sk_buff_head *frames)
58905290 3156{
58905290 3157 ieee80211_rx_result res = RX_DROP_MONITOR;
aa0c8636 3158 struct sk_buff *skb;
8944b79f 3159
e32f85f7
LCC
3160#define CALL_RXH(rxh) \
3161 do { \
3162 res = rxh(rx); \
3163 if (res != RX_CONTINUE) \
2569a826 3164 goto rxh_next; \
e32f85f7 3165 } while (0);
49461622 3166
45ceeee8
JB
3167 /* Lock here to avoid hitting all of the data used in the RX
3168 * path (e.g. key data, station data, ...) concurrently when
3169 * a frame is released from the reorder buffer due to timeout
3170 * from the timer, potentially concurrently with RX from the
3171 * driver.
3172 */
f9e124fb 3173 spin_lock_bh(&rx->local->rx_path_lock);
24a8fdad 3174
f9e124fb 3175 while ((skb = __skb_dequeue(frames))) {
2569a826
JB
3176 /*
3177 * all the other fields are valid across frames
3178 * that belong to an aMPDU since they are on the
3179 * same TID from the same station
3180 */
3181 rx->skb = skb;
3182
2569a826 3183 CALL_RXH(ieee80211_rx_h_check_more_data)
47086fc5 3184 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
2569a826 3185 CALL_RXH(ieee80211_rx_h_sta_process)
86c228a7 3186 CALL_RXH(ieee80211_rx_h_decrypt)
2569a826 3187 CALL_RXH(ieee80211_rx_h_defragment)
2569a826
JB
3188 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
3189 /* must be after MMIC verify so header is counted in MPDU mic */
bf94e17b 3190#ifdef CONFIG_MAC80211_MESH
aa0c8636 3191 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2569a826 3192 CALL_RXH(ieee80211_rx_h_mesh_fwding);
bf94e17b 3193#endif
2154c81c 3194 CALL_RXH(ieee80211_rx_h_amsdu)
2569a826 3195 CALL_RXH(ieee80211_rx_h_data)
f9e124fb
CL
3196
3197 /* special treatment -- needs the queue */
3198 res = ieee80211_rx_h_ctrl(rx, frames);
3199 if (res != RX_CONTINUE)
3200 goto rxh_next;
3201
2e161f78 3202 CALL_RXH(ieee80211_rx_h_mgmt_check)
2569a826 3203 CALL_RXH(ieee80211_rx_h_action)
2e161f78
JB
3204 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
3205 CALL_RXH(ieee80211_rx_h_action_return)
2569a826 3206 CALL_RXH(ieee80211_rx_h_mgmt)
49461622 3207
aa0c8636
CL
3208 rxh_next:
3209 ieee80211_rx_handlers_result(rx, res);
f9e124fb 3210
49461622 3211#undef CALL_RXH
aa0c8636 3212 }
24a8fdad 3213
f9e124fb 3214 spin_unlock_bh(&rx->local->rx_path_lock);
aa0c8636
CL
3215}
3216
4406c376 3217static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
aa0c8636 3218{
f9e124fb 3219 struct sk_buff_head reorder_release;
aa0c8636
CL
3220 ieee80211_rx_result res = RX_DROP_MONITOR;
3221
f9e124fb
CL
3222 __skb_queue_head_init(&reorder_release);
3223
aa0c8636
CL
3224#define CALL_RXH(rxh) \
3225 do { \
3226 res = rxh(rx); \
3227 if (res != RX_CONTINUE) \
3228 goto rxh_next; \
3229 } while (0);
3230
0395442a 3231 CALL_RXH(ieee80211_rx_h_check_dup)
aa0c8636
CL
3232 CALL_RXH(ieee80211_rx_h_check)
3233
f9e124fb 3234 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
aa0c8636 3235
f9e124fb 3236 ieee80211_rx_handlers(rx, &reorder_release);
aa0c8636 3237 return;
49461622 3238
2569a826 3239 rxh_next:
aa0c8636
CL
3240 ieee80211_rx_handlers_result(rx, res);
3241
3242#undef CALL_RXH
58905290
JB
3243}
3244
2bff8ebf 3245/*
dd318575
JB
3246 * This function makes calls into the RX path, therefore
3247 * it has to be invoked under RCU read lock.
2bff8ebf
CL
3248 */
3249void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3250{
f9e124fb 3251 struct sk_buff_head frames;
554891e6
JB
3252 struct ieee80211_rx_data rx = {
3253 .sta = sta,
3254 .sdata = sta->sdata,
3255 .local = sta->local,
9e26297a
JB
3256 /* This is OK -- must be QoS data frame */
3257 .security_idx = tid,
3258 .seqno_idx = tid,
22d3a3c8 3259 .flags = IEEE80211_RX_REORDER_TIMER,
554891e6 3260 };
2c15a0cf
CL
3261 struct tid_ampdu_rx *tid_agg_rx;
3262
3263 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3264 if (!tid_agg_rx)
3265 return;
2bff8ebf 3266
f9e124fb
CL
3267 __skb_queue_head_init(&frames);
3268
2c15a0cf 3269 spin_lock(&tid_agg_rx->reorder_lock);
f9e124fb 3270 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
2c15a0cf 3271 spin_unlock(&tid_agg_rx->reorder_lock);
2bff8ebf 3272
b497de63
EG
3273 if (!skb_queue_empty(&frames)) {
3274 struct ieee80211_event event = {
3275 .type = BA_FRAME_TIMEOUT,
3276 .u.ba.tid = tid,
3277 .u.ba.sta = &sta->sta,
3278 };
3279 drv_event_callback(rx.local, rx.sdata, &event);
3280 }
3281
f9e124fb 3282 ieee80211_rx_handlers(&rx, &frames);
2bff8ebf
CL
3283}
3284
571ecf67
JB
3285/* main receive path */
3286
a58fbe1a 3287static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
23a24def 3288{
20b01f80 3289 struct ieee80211_sub_if_data *sdata = rx->sdata;
eb9fb5b8 3290 struct sk_buff *skb = rx->skb;
a58fbe1a 3291 struct ieee80211_hdr *hdr = (void *)skb->data;
eb9fb5b8
JB
3292 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3293 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
23a24def
JB
3294 int multicast = is_multicast_ether_addr(hdr->addr1);
3295
51fb61e7 3296 switch (sdata->vif.type) {
05c914fe 3297 case NL80211_IFTYPE_STATION:
9bc383de 3298 if (!bssid && !sdata->u.mgd.use_4addr)
3c2723f5 3299 return false;
a58fbe1a
JB
3300 if (multicast)
3301 return true;
3302 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
05c914fe 3303 case NL80211_IFTYPE_ADHOC:
23a24def 3304 if (!bssid)
3c2723f5 3305 return false;
6329b8d9
FF
3306 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3307 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3c2723f5 3308 return false;
a58fbe1a 3309 if (ieee80211_is_beacon(hdr->frame_control))
3c2723f5 3310 return true;
a58fbe1a 3311 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3c2723f5 3312 return false;
a58fbe1a
JB
3313 if (!multicast &&
3314 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
df140465 3315 return false;
a58fbe1a 3316 if (!rx->sta) {
0fb8ca45 3317 int rate_idx;
5614618e
JB
3318 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3319 rate_idx = 0; /* TODO: HT/VHT rates */
0fb8ca45 3320 else
eb9fb5b8 3321 rate_idx = status->rate_idx;
8bf11d8d
JB
3322 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3323 BIT(rate_idx));
0fb8ca45 3324 }
a58fbe1a 3325 return true;
239281f8
RL
3326 case NL80211_IFTYPE_OCB:
3327 if (!bssid)
3328 return false;
a58fbe1a 3329 if (ieee80211_is_beacon(hdr->frame_control))
239281f8 3330 return false;
a58fbe1a 3331 if (!is_broadcast_ether_addr(bssid))
239281f8 3332 return false;
a58fbe1a
JB
3333 if (!multicast &&
3334 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
df140465 3335 return false;
a58fbe1a 3336 if (!rx->sta) {
239281f8
RL
3337 int rate_idx;
3338 if (status->flag & RX_FLAG_HT)
3339 rate_idx = 0; /* TODO: HT rates */
3340 else
3341 rate_idx = status->rate_idx;
3342 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3343 BIT(rate_idx));
3344 }
a58fbe1a 3345 return true;
05c914fe 3346 case NL80211_IFTYPE_MESH_POINT:
a58fbe1a
JB
3347 if (multicast)
3348 return true;
3349 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
05c914fe
JB
3350 case NL80211_IFTYPE_AP_VLAN:
3351 case NL80211_IFTYPE_AP:
a58fbe1a
JB
3352 if (!bssid)
3353 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3354
3355 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3df6eaea
JB
3356 /*
3357 * Accept public action frames even when the
3358 * BSSID doesn't match, this is used for P2P
3359 * and location updates. Note that mac80211
3360 * itself never looks at these frames.
3361 */
2b9ccd4e
JB
3362 if (!multicast &&
3363 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3c2723f5 3364 return false;
d48b2968 3365 if (ieee80211_is_public_action(hdr, skb->len))
3c2723f5 3366 return true;
5c90067c 3367 return ieee80211_is_beacon(hdr->frame_control);
a58fbe1a
JB
3368 }
3369
3370 if (!ieee80211_has_tods(hdr->frame_control)) {
db8e1732
AN
3371 /* ignore data frames to TDLS-peers */
3372 if (ieee80211_is_data(hdr->frame_control))
3373 return false;
3374 /* ignore action frames to TDLS-peers */
3375 if (ieee80211_is_action(hdr->frame_control) &&
3376 !ether_addr_equal(bssid, hdr->addr1))
3377 return false;
23a24def 3378 }
a58fbe1a 3379 return true;
05c914fe 3380 case NL80211_IFTYPE_WDS:
a7767f95 3381 if (bssid || !ieee80211_is_data(hdr->frame_control))
3c2723f5 3382 return false;
a58fbe1a 3383 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
f142c6b9 3384 case NL80211_IFTYPE_P2P_DEVICE:
a58fbe1a
JB
3385 return ieee80211_is_public_action(hdr, skb->len) ||
3386 ieee80211_is_probe_req(hdr->frame_control) ||
3387 ieee80211_is_probe_resp(hdr->frame_control) ||
3388 ieee80211_is_beacon(hdr->frame_control);
2ca27bcf 3389 default:
fb1c1cd6 3390 break;
23a24def
JB
3391 }
3392
a58fbe1a
JB
3393 WARN_ON_ONCE(1);
3394 return false;
23a24def
JB
3395}
3396
4406c376
JB
3397/*
3398 * This function returns whether or not the SKB
3399 * was destined for RX processing or not, which,
3400 * if consume is true, is equivalent to whether
3401 * or not the skb was consumed.
3402 */
3403static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3404 struct sk_buff *skb, bool consume)
3405{
3406 struct ieee80211_local *local = rx->local;
3407 struct ieee80211_sub_if_data *sdata = rx->sdata;
4406c376
JB
3408
3409 rx->skb = skb;
4406c376 3410
a58fbe1a 3411 if (!ieee80211_accept_frame(rx))
4406c376
JB
3412 return false;
3413
4406c376
JB
3414 if (!consume) {
3415 skb = skb_copy(skb, GFP_ATOMIC);
3416 if (!skb) {
3417 if (net_ratelimit())
3418 wiphy_debug(local->hw.wiphy,
b305dae4 3419 "failed to copy skb for %s\n",
4406c376
JB
3420 sdata->name);
3421 return true;
3422 }
3423
3424 rx->skb = skb;
3425 }
3426
3427 ieee80211_invoke_rx_handlers(rx);
3428 return true;
3429}
3430
571ecf67 3431/*
6b59db7d 3432 * This is the actual Rx frames handler. as it belongs to Rx path it must
6368e4b1 3433 * be called with rcu_read_lock protection.
571ecf67 3434 */
71ebb4aa 3435static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
071d9ac2 3436 struct sk_buff *skb)
571ecf67
JB
3437{
3438 struct ieee80211_local *local = hw_to_local(hw);
3439 struct ieee80211_sub_if_data *sdata;
571ecf67 3440 struct ieee80211_hdr *hdr;
e3cf8b3f 3441 __le16 fc;
5cf121c3 3442 struct ieee80211_rx_data rx;
4406c376 3443 struct ieee80211_sub_if_data *prev;
7bedd0cf
JB
3444 struct sta_info *sta, *prev_sta;
3445 struct rhash_head *tmp;
e3cf8b3f 3446 int err = 0;
571ecf67 3447
e3cf8b3f 3448 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
571ecf67
JB
3449 memset(&rx, 0, sizeof(rx));
3450 rx.skb = skb;
3451 rx.local = local;
72abd81b 3452
e3cf8b3f 3453 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
c206ca67 3454 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
571ecf67 3455
4a4f1a58
JB
3456 if (ieee80211_is_mgmt(fc)) {
3457 /* drop frame if too short for header */
3458 if (skb->len < ieee80211_hdrlen(fc))
3459 err = -ENOBUFS;
3460 else
3461 err = skb_linearize(skb);
3462 } else {
e3cf8b3f 3463 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4a4f1a58 3464 }
e3cf8b3f
ZY
3465
3466 if (err) {
3467 dev_kfree_skb(skb);
3468 return;
3469 }
3470
3471 hdr = (struct ieee80211_hdr *)skb->data;
38f3714d 3472 ieee80211_parse_qos(&rx);
d1c3a37c 3473 ieee80211_verify_alignment(&rx);
38f3714d 3474
d48b2968
JB
3475 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3476 ieee80211_is_beacon(hdr->frame_control)))
3477 ieee80211_scan_rx(local, skb);
3478
e3cf8b3f 3479 if (ieee80211_is_data(fc)) {
7bedd0cf
JB
3480 const struct bucket_table *tbl;
3481
56af3268 3482 prev_sta = NULL;
4406c376 3483
7bedd0cf
JB
3484 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
3485
3486 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
56af3268
BG
3487 if (!prev_sta) {
3488 prev_sta = sta;
3489 continue;
3490 }
3491
3492 rx.sta = prev_sta;
3493 rx.sdata = prev_sta->sdata;
4406c376 3494 ieee80211_prepare_and_rx_handle(&rx, skb, false);
abe60632 3495
56af3268 3496 prev_sta = sta;
4406c376 3497 }
56af3268
BG
3498
3499 if (prev_sta) {
3500 rx.sta = prev_sta;
3501 rx.sdata = prev_sta->sdata;
3502
4406c376 3503 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
56af3268 3504 return;
8e26d5ad 3505 goto out;
56af3268 3506 }
4406c376
JB
3507 }
3508
4b0dd98e 3509 prev = NULL;
b2e7771e 3510
4b0dd98e
JB
3511 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3512 if (!ieee80211_sdata_running(sdata))
3513 continue;
340e11f3 3514
4b0dd98e
JB
3515 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
3516 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3517 continue;
340e11f3 3518
4b0dd98e
JB
3519 /*
3520 * frame is destined for this interface, but if it's
3521 * not also for the previous one we handle that after
3522 * the loop to avoid copying the SKB once too much
3523 */
4bb29f8c 3524
4b0dd98e 3525 if (!prev) {
abe60632 3526 prev = sdata;
4b0dd98e 3527 continue;
340e11f3 3528 }
4bb29f8c 3529
7852e361 3530 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e
JB
3531 rx.sdata = prev;
3532 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4bb29f8c 3533
4b0dd98e
JB
3534 prev = sdata;
3535 }
3536
3537 if (prev) {
7852e361 3538 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e 3539 rx.sdata = prev;
4406c376 3540
4b0dd98e
JB
3541 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
3542 return;
571ecf67 3543 }
4406c376 3544
8e26d5ad 3545 out:
4406c376 3546 dev_kfree_skb(skb);
571ecf67 3547}
6368e4b1
RR
3548
3549/*
3550 * This is the receive path handler. It is called by a low level driver when an
3551 * 802.11 MPDU is received from the hardware.
3552 */
103bf9f7 3553void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
6368e4b1
RR
3554{
3555 struct ieee80211_local *local = hw_to_local(hw);
8318d78a
JB
3556 struct ieee80211_rate *rate = NULL;
3557 struct ieee80211_supported_band *sband;
f1d58c25 3558 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
8318d78a 3559
d20ef63d
JB
3560 WARN_ON_ONCE(softirq_count() == 0);
3561
444e3803 3562 if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
77a980dc 3563 goto drop;
8318d78a
JB
3564
3565 sband = local->hw.wiphy->bands[status->band];
77a980dc
JB
3566 if (WARN_ON(!sband))
3567 goto drop;
8318d78a 3568
89c3a8ac
JB
3569 /*
3570 * If we're suspending, it is possible although not too likely
3571 * that we'd be receiving frames after having already partially
3572 * quiesced the stack. We can't process such frames then since
3573 * that might, for example, cause stations to be added or other
3574 * driver callbacks be invoked.
3575 */
77a980dc
JB
3576 if (unlikely(local->quiescing || local->suspended))
3577 goto drop;
89c3a8ac 3578
04800ada
AN
3579 /* We might be during a HW reconfig, prevent Rx for the same reason */
3580 if (unlikely(local->in_reconfig))
3581 goto drop;
3582
ea77f12f
JB
3583 /*
3584 * The same happens when we're not even started,
3585 * but that's worth a warning.
3586 */
77a980dc
JB
3587 if (WARN_ON(!local->started))
3588 goto drop;
ea77f12f 3589
fc885189 3590 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
e5d6eb83 3591 /*
fc885189
JB
3592 * Validate the rate, unless a PLCP error means that
3593 * we probably can't have a valid rate here anyway.
e5d6eb83 3594 */
fc885189
JB
3595
3596 if (status->flag & RX_FLAG_HT) {
3597 /*
3598 * rate_idx is MCS index, which can be [0-76]
3599 * as documented on:
3600 *
3601 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
3602 *
3603 * Anything else would be some sort of driver or
3604 * hardware error. The driver should catch hardware
3605 * errors.
3606 */
444e3803 3607 if (WARN(status->rate_idx > 76,
fc885189
JB
3608 "Rate marked as an HT rate but passed "
3609 "status->rate_idx is not "
3610 "an MCS index [0-76]: %d (0x%02x)\n",
3611 status->rate_idx,
3612 status->rate_idx))
3613 goto drop;
5614618e
JB
3614 } else if (status->flag & RX_FLAG_VHT) {
3615 if (WARN_ONCE(status->rate_idx > 9 ||
3616 !status->vht_nss ||
3617 status->vht_nss > 8,
3618 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
3619 status->rate_idx, status->vht_nss))
3620 goto drop;
fc885189 3621 } else {
444e3803 3622 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
fc885189
JB
3623 goto drop;
3624 rate = &sband->bitrates[status->rate_idx];
3625 }
0fb8ca45 3626 }
6368e4b1 3627
554891e6
JB
3628 status->rx_flags = 0;
3629
6368e4b1
RR
3630 /*
3631 * key references and virtual interfaces are protected using RCU
3632 * and this requires that we are in a read-side RCU section during
3633 * receive processing
3634 */
3635 rcu_read_lock();
3636
3637 /*
3638 * Frames with failed FCS/PLCP checksum are not returned,
3639 * all other frames are returned without radiotap header
3640 * if it was previously present.
3641 * Also, frames with less than 16 bytes are dropped.
3642 */
f1d58c25 3643 skb = ieee80211_rx_monitor(local, skb, rate);
6368e4b1
RR
3644 if (!skb) {
3645 rcu_read_unlock();
3646 return;
3647 }
3648
e1e54068
JB
3649 ieee80211_tpt_led_trig_rx(local,
3650 ((struct ieee80211_hdr *)skb->data)->frame_control,
3651 skb->len);
071d9ac2 3652 __ieee80211_rx_handle_packet(hw, skb);
6368e4b1
RR
3653
3654 rcu_read_unlock();
77a980dc
JB
3655
3656 return;
3657 drop:
3658 kfree_skb(skb);
6368e4b1 3659}
103bf9f7 3660EXPORT_SYMBOL(ieee80211_rx);
571ecf67
JB
3661
3662/* This is a version of the rx handler that can be called from hard irq
3663 * context. Post the skb on the queue and schedule the tasklet */
f1d58c25 3664void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
571ecf67
JB
3665{
3666 struct ieee80211_local *local = hw_to_local(hw);
3667
3668 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
3669
571ecf67
JB
3670 skb->pkt_type = IEEE80211_RX_MSG;
3671 skb_queue_tail(&local->skb_queue, skb);
3672 tasklet_schedule(&local->tasklet);
3673}
3674EXPORT_SYMBOL(ieee80211_rx_irqsafe);