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