mac80211: remove short frame test and counter
[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{
30686bf7 55 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
b2e7771e 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 }
30686bf7 113 if (ieee80211_hw_check(&local->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;
30686bf7 188 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
f4bda337 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 */
30686bf7 242 if (has_fcs && ieee80211_hw_check(&local->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 */
30686bf7 292 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
fe8431f8 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
30686bf7 461 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
b2e7771e
JB
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)) {
c206ca67 1080 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
5c90067c 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 /* Drop disallowed frame classes based on STA auth/assoc state;
1097 * IEEE 802.11, Chap 5.5.
1098 *
ccd7b362
JB
1099 * mac80211 filters only based on association state, i.e. it drops
1100 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
1101 * deauth/disassoc frames when needed. In addition, hostapd is
1102 * responsible for filtering on both auth and assoc states.
1103 */
33b64eb2 1104
902acc78 1105 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 1106 return ieee80211_rx_mesh_check(rx);
33b64eb2 1107
a7767f95
HH
1108 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1109 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 1110 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1be7fe8d 1111 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
239281f8 1112 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
c2c98fde 1113 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
7852e361
JB
1114 /*
1115 * accept port control frames from the AP even when it's not
1116 * yet marked ASSOC to prevent a race where we don't set the
1117 * assoc bit quickly enough before it sends the first frame
1118 */
1119 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2a33bee2 1120 ieee80211_is_data_present(hdr->frame_control)) {
6dbda2d0
JB
1121 unsigned int hdrlen;
1122 __be16 ethertype;
1123
1124 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1125
1126 if (rx->skb->len < hdrlen + 8)
1127 return RX_DROP_MONITOR;
1128
1129 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1130 if (ethertype == rx->sdata->control_port_protocol)
2a33bee2
GE
1131 return RX_CONTINUE;
1132 }
21fc7560
JB
1133
1134 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1135 cfg80211_rx_spurious_frame(rx->sdata->dev,
1136 hdr->addr2,
1137 GFP_ATOMIC))
1138 return RX_DROP_UNUSABLE;
1139
e4c26add 1140 return RX_DROP_MONITOR;
2a33bee2 1141 }
571ecf67 1142
9ae54c84 1143 return RX_CONTINUE;
570bd537
JB
1144}
1145
1146
572e0012
KV
1147static ieee80211_rx_result debug_noinline
1148ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1149{
1150 struct ieee80211_local *local;
1151 struct ieee80211_hdr *hdr;
1152 struct sk_buff *skb;
1153
1154 local = rx->local;
1155 skb = rx->skb;
1156 hdr = (struct ieee80211_hdr *) skb->data;
1157
1158 if (!local->pspolling)
1159 return RX_CONTINUE;
1160
1161 if (!ieee80211_has_fromds(hdr->frame_control))
1162 /* this is not from AP */
1163 return RX_CONTINUE;
1164
1165 if (!ieee80211_is_data(hdr->frame_control))
1166 return RX_CONTINUE;
1167
1168 if (!ieee80211_has_moredata(hdr->frame_control)) {
1169 /* AP has no more frames buffered for us */
1170 local->pspolling = false;
1171 return RX_CONTINUE;
1172 }
1173
1174 /* more data bit is set, let's request a new frame from the AP */
1175 ieee80211_send_pspoll(local, rx->sdata);
1176
1177 return RX_CONTINUE;
1178}
1179
d012a605 1180static void sta_ps_start(struct sta_info *sta)
571ecf67 1181{
133b8226 1182 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1183 struct ieee80211_local *local = sdata->local;
d012a605 1184 struct ps_data *ps;
ba8c3d6f 1185 int tid;
0795af57 1186
d012a605
MP
1187 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1188 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1189 ps = &sdata->bss->ps;
1190 else
1191 return;
1192
1193 atomic_inc(&ps->num_sta_ps);
c2c98fde 1194 set_sta_flag(sta, WLAN_STA_PS_STA);
30686bf7 1195 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
d057e5a3 1196 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
bdcbd8e0
JB
1197 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1198 sta->sta.addr, sta->sta.aid);
ba8c3d6f 1199
17c18bf8
JB
1200 ieee80211_clear_fast_xmit(sta);
1201
ba8c3d6f
FF
1202 if (!sta->sta.txq[0])
1203 return;
1204
1205 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1206 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1207
1208 if (!skb_queue_len(&txqi->queue))
1209 set_bit(tid, &sta->txq_buffered_tids);
1210 else
1211 clear_bit(tid, &sta->txq_buffered_tids);
1212 }
571ecf67
JB
1213}
1214
d012a605 1215static void sta_ps_end(struct sta_info *sta)
571ecf67 1216{
bdcbd8e0
JB
1217 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1218 sta->sta.addr, sta->sta.aid);
004c872e 1219
c2c98fde 1220 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
e3685e03
JB
1221 /*
1222 * Clear the flag only if the other one is still set
1223 * so that the TX path won't start TX'ing new frames
1224 * directly ... In the case that the driver flag isn't
1225 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1226 */
1227 clear_sta_flag(sta, WLAN_STA_PS_STA);
bdcbd8e0
JB
1228 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1229 sta->sta.addr, sta->sta.aid);
af818581
JB
1230 return;
1231 }
1232
5ac2e350
JB
1233 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1234 clear_sta_flag(sta, WLAN_STA_PS_STA);
af818581 1235 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1236}
1237
cf47161a 1238int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
d057e5a3 1239{
cf47161a 1240 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
d057e5a3
AN
1241 bool in_ps;
1242
cf47161a 1243 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
d057e5a3
AN
1244
1245 /* Don't let the same PS state be set twice */
cf47161a 1246 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
d057e5a3
AN
1247 if ((start && in_ps) || (!start && !in_ps))
1248 return -EINVAL;
1249
1250 if (start)
cf47161a 1251 sta_ps_start(sta);
d057e5a3 1252 else
cf47161a 1253 sta_ps_end(sta);
d057e5a3
AN
1254
1255 return 0;
1256}
1257EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1258
47086fc5
JB
1259static ieee80211_rx_result debug_noinline
1260ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1261{
1262 struct ieee80211_sub_if_data *sdata = rx->sdata;
1263 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1264 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1265 int tid, ac;
1266
5c90067c 1267 if (!rx->sta)
47086fc5
JB
1268 return RX_CONTINUE;
1269
1270 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1271 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1272 return RX_CONTINUE;
1273
1274 /*
1275 * The device handles station powersave, so don't do anything about
1276 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1277 * it to mac80211 since they're handled.)
1278 */
30686bf7 1279 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
47086fc5
JB
1280 return RX_CONTINUE;
1281
1282 /*
1283 * Don't do anything if the station isn't already asleep. In
1284 * the uAPSD case, the station will probably be marked asleep,
1285 * in the PS-Poll case the station must be confused ...
1286 */
c2c98fde 1287 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
47086fc5
JB
1288 return RX_CONTINUE;
1289
1290 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
c2c98fde
JB
1291 if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
1292 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
deeaee19
JB
1293 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1294 else
c2c98fde 1295 set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
deeaee19 1296 }
47086fc5
JB
1297
1298 /* Free PS Poll skb here instead of returning RX_DROP that would
1299 * count as an dropped frame. */
1300 dev_kfree_skb(rx->skb);
1301
1302 return RX_QUEUED;
1303 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1304 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1305 ieee80211_has_pm(hdr->frame_control) &&
1306 (ieee80211_is_data_qos(hdr->frame_control) ||
1307 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1308 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1309 ac = ieee802_1d_to_ac[tid & 7];
1310
1311 /*
1312 * If this AC is not trigger-enabled do nothing.
1313 *
1314 * NB: This could/should check a separate bitmap of trigger-
1315 * enabled queues, but for now we only implement uAPSD w/o
1316 * TSPEC changes to the ACs, so they're always the same.
1317 */
1318 if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
1319 return RX_CONTINUE;
1320
1321 /* if we are in a service period, do nothing */
c2c98fde 1322 if (test_sta_flag(rx->sta, WLAN_STA_SP))
47086fc5
JB
1323 return RX_CONTINUE;
1324
c2c98fde 1325 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
47086fc5
JB
1326 ieee80211_sta_ps_deliver_uapsd(rx->sta);
1327 else
c2c98fde 1328 set_sta_flag(rx->sta, WLAN_STA_UAPSD);
47086fc5
JB
1329 }
1330
1331 return RX_CONTINUE;
1332}
1333
49461622 1334static ieee80211_rx_result debug_noinline
5cf121c3 1335ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1336{
1337 struct sta_info *sta = rx->sta;
eb9fb5b8
JB
1338 struct sk_buff *skb = rx->skb;
1339 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1340 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
ef0621e8 1341 int i;
571ecf67
JB
1342
1343 if (!sta)
9ae54c84 1344 return RX_CONTINUE;
571ecf67 1345
b291ba11
JB
1346 /*
1347 * Update last_rx only for IBSS packets which are for the current
e584da5e
AQ
1348 * BSSID and for station already AUTHORIZED to avoid keeping the
1349 * current IBSS network alive in cases where other STAs start
1350 * using different BSSID. This will also give the station another
1351 * chance to restart the authentication/authorization in case
1352 * something went wrong the first time.
b291ba11 1353 */
05c914fe 1354 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1355 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1356 NL80211_IFTYPE_ADHOC);
e584da5e
AQ
1357 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1358 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
571ecf67 1359 sta->last_rx = jiffies;
f4ebddf9
HR
1360 if (ieee80211_is_data(hdr->frame_control) &&
1361 !is_multicast_ether_addr(hdr->addr1)) {
3af6334c
FF
1362 sta->last_rx_rate_idx = status->rate_idx;
1363 sta->last_rx_rate_flag = status->flag;
1b8d242a 1364 sta->last_rx_rate_vht_flag = status->vht_flag;
5614618e 1365 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c
FF
1366 }
1367 }
239281f8 1368 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
6fe3eac7 1369 sta->last_rx = jiffies;
b291ba11
JB
1370 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1371 /*
33b64eb2
LCC
1372 * Mesh beacons will update last_rx when if they are found to
1373 * match the current local configuration when processed.
571ecf67 1374 */
b291ba11 1375 sta->last_rx = jiffies;
3af6334c
FF
1376 if (ieee80211_is_data(hdr->frame_control)) {
1377 sta->last_rx_rate_idx = status->rate_idx;
1378 sta->last_rx_rate_flag = status->flag;
b8ff416b 1379 sta->last_rx_rate_vht_flag = status->vht_flag;
5614618e 1380 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c 1381 }
571ecf67
JB
1382 }
1383
3cf335d5
KV
1384 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1385 ieee80211_sta_rx_notify(rx->sdata, hdr);
1386
571ecf67
JB
1387 sta->rx_fragments++;
1388 sta->rx_bytes += rx->skb->len;
fe8431f8
FF
1389 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1390 sta->last_signal = status->signal;
1391 ewma_add(&sta->avg_signal, -status->signal);
1392 }
571ecf67 1393
ef0621e8
FF
1394 if (status->chains) {
1395 sta->chains = status->chains;
1396 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1397 int signal = status->chain_signal[i];
1398
1399 if (!(status->chains & BIT(i)))
1400 continue;
1401
1402 sta->chain_signal_last[i] = signal;
1403 ewma_add(&sta->chain_signal_avg[i], -signal);
1404 }
1405 }
1406
72eaa43a
JB
1407 /*
1408 * Change STA power saving mode only at the end of a frame
1409 * exchange sequence.
1410 */
30686bf7 1411 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
d057e5a3 1412 !ieee80211_has_morefrags(hdr->frame_control) &&
4cfda47b 1413 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
05c914fe 1414 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
b4ba544c
JB
1415 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1416 /* PM bit is only checked in frames where it isn't reserved,
1417 * in AP mode it's reserved in non-bufferable management frames
1418 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1419 */
1420 (!ieee80211_is_mgmt(hdr->frame_control) ||
1421 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
c2c98fde 1422 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
b4ba544c 1423 if (!ieee80211_has_pm(hdr->frame_control))
d012a605 1424 sta_ps_end(sta);
72eaa43a
JB
1425 } else {
1426 if (ieee80211_has_pm(hdr->frame_control))
d012a605 1427 sta_ps_start(sta);
72eaa43a 1428 }
571ecf67
JB
1429 }
1430
3f52b7e3
MP
1431 /* mesh power save support */
1432 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1433 ieee80211_mps_rx_h_sta_process(sta, hdr);
1434
22403def
JB
1435 /*
1436 * Drop (qos-)data::nullfunc frames silently, since they
1437 * are used only to control station power saving mode.
1438 */
1439 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1440 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
571ecf67 1441 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
d524215f
FF
1442
1443 /*
1444 * If we receive a 4-addr nullfunc frame from a STA
e7f4a940
JB
1445 * that was not moved to a 4-addr STA vlan yet send
1446 * the event to userspace and for older hostapd drop
1447 * the frame to the monitor interface.
d524215f
FF
1448 */
1449 if (ieee80211_has_a4(hdr->frame_control) &&
1450 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1451 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
e7f4a940
JB
1452 !rx->sdata->u.vlan.sta))) {
1453 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1454 cfg80211_rx_unexpected_4addr_frame(
1455 rx->sdata->dev, sta->sta.addr,
1456 GFP_ATOMIC);
d524215f 1457 return RX_DROP_MONITOR;
e7f4a940 1458 }
22403def
JB
1459 /*
1460 * Update counter and free packet here to avoid
1461 * counting this as a dropped packed.
1462 */
571ecf67
JB
1463 sta->rx_packets++;
1464 dev_kfree_skb(rx->skb);
9ae54c84 1465 return RX_QUEUED;
571ecf67
JB
1466 }
1467
9ae54c84 1468 return RX_CONTINUE;
571ecf67
JB
1469} /* ieee80211_rx_h_sta_process */
1470
86c228a7
JA
1471static ieee80211_rx_result debug_noinline
1472ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1473{
1474 struct sk_buff *skb = rx->skb;
1475 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1476 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1477 int keyidx;
1478 int hdrlen;
1479 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1480 struct ieee80211_key *sta_ptk = NULL;
1481 int mmie_keyidx = -1;
1482 __le16 fc;
2475b1cc 1483 const struct ieee80211_cipher_scheme *cs = NULL;
86c228a7
JA
1484
1485 /*
1486 * Key selection 101
1487 *
1488 * There are four types of keys:
1489 * - GTK (group keys)
1490 * - IGTK (group keys for management frames)
1491 * - PTK (pairwise keys)
1492 * - STK (station-to-station pairwise keys)
1493 *
1494 * When selecting a key, we have to distinguish between multicast
1495 * (including broadcast) and unicast frames, the latter can only
1496 * use PTKs and STKs while the former always use GTKs and IGTKs.
1497 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1498 * unicast frames can also use key indices like GTKs. Hence, if we
1499 * don't have a PTK/STK we check the key index for a WEP key.
1500 *
1501 * Note that in a regular BSS, multicast frames are sent by the
1502 * AP only, associated stations unicast the frame to the AP first
1503 * which then multicasts it on their behalf.
1504 *
1505 * There is also a slight problem in IBSS mode: GTKs are negotiated
1506 * with each station, that is something we don't currently handle.
1507 * The spec seems to expect that one negotiates the same key with
1508 * every station but there's no such requirement; VLANs could be
1509 * possible.
1510 */
1511
86c228a7
JA
1512 /* start without a key */
1513 rx->key = NULL;
2475b1cc 1514 fc = hdr->frame_control;
86c228a7 1515
2475b1cc
MS
1516 if (rx->sta) {
1517 int keyid = rx->sta->ptk_idx;
86c228a7 1518
2475b1cc
MS
1519 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1520 cs = rx->sta->cipher_scheme;
1521 keyid = iwl80211_get_cs_keyid(cs, rx->skb);
1522 if (unlikely(keyid < 0))
1523 return RX_DROP_UNUSABLE;
1524 }
1525 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1526 }
86c228a7
JA
1527
1528 if (!ieee80211_has_protected(fc))
1529 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1530
1531 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1532 rx->key = sta_ptk;
1533 if ((status->flag & RX_FLAG_DECRYPTED) &&
1534 (status->flag & RX_FLAG_IV_STRIPPED))
1535 return RX_CONTINUE;
1536 /* Skip decryption if the frame is not protected. */
1537 if (!ieee80211_has_protected(fc))
1538 return RX_CONTINUE;
1539 } else if (mmie_keyidx >= 0) {
1540 /* Broadcast/multicast robust management frame / BIP */
1541 if ((status->flag & RX_FLAG_DECRYPTED) &&
1542 (status->flag & RX_FLAG_IV_STRIPPED))
1543 return RX_CONTINUE;
1544
1545 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1546 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1547 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1548 if (rx->sta)
1549 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1550 if (!rx->key)
1551 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1552 } else if (!ieee80211_has_protected(fc)) {
1553 /*
1554 * The frame was not protected, so skip decryption. However, we
1555 * need to set rx->key if there is a key that could have been
1556 * used so that the frame may be dropped if encryption would
1557 * have been expected.
1558 */
1559 struct ieee80211_key *key = NULL;
1560 struct ieee80211_sub_if_data *sdata = rx->sdata;
1561 int i;
1562
1563 if (ieee80211_is_mgmt(fc) &&
1564 is_multicast_ether_addr(hdr->addr1) &&
1565 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1566 rx->key = key;
1567 else {
1568 if (rx->sta) {
1569 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1570 key = rcu_dereference(rx->sta->gtk[i]);
1571 if (key)
1572 break;
1573 }
1574 }
1575 if (!key) {
1576 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1577 key = rcu_dereference(sdata->keys[i]);
1578 if (key)
1579 break;
1580 }
1581 }
1582 if (key)
1583 rx->key = key;
1584 }
1585 return RX_CONTINUE;
1586 } else {
1587 u8 keyid;
2475b1cc 1588
86c228a7
JA
1589 /*
1590 * The device doesn't give us the IV so we won't be
1591 * able to look up the key. That's ok though, we
1592 * don't need to decrypt the frame, we just won't
1593 * be able to keep statistics accurate.
1594 * Except for key threshold notifications, should
1595 * we somehow allow the driver to tell us which key
1596 * the hardware used if this flag is set?
1597 */
1598 if ((status->flag & RX_FLAG_DECRYPTED) &&
1599 (status->flag & RX_FLAG_IV_STRIPPED))
1600 return RX_CONTINUE;
1601
1602 hdrlen = ieee80211_hdrlen(fc);
1603
2475b1cc
MS
1604 if (cs) {
1605 keyidx = iwl80211_get_cs_keyid(cs, rx->skb);
86c228a7 1606
2475b1cc
MS
1607 if (unlikely(keyidx < 0))
1608 return RX_DROP_UNUSABLE;
1609 } else {
1610 if (rx->skb->len < 8 + hdrlen)
1611 return RX_DROP_UNUSABLE; /* TODO: count this? */
1612 /*
1613 * no need to call ieee80211_wep_get_keyidx,
1614 * it verifies a bunch of things we've done already
1615 */
1616 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1617 keyidx = keyid >> 6;
1618 }
86c228a7
JA
1619
1620 /* check per-station GTK first, if multicast packet */
1621 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1622 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1623
1624 /* if not found, try default key */
1625 if (!rx->key) {
1626 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1627
1628 /*
1629 * RSNA-protected unicast frames should always be
1630 * sent with pairwise or station-to-station keys,
1631 * but for WEP we allow using a key index as well.
1632 */
1633 if (rx->key &&
1634 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1635 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1636 !is_multicast_ether_addr(hdr->addr1))
1637 rx->key = NULL;
1638 }
1639 }
1640
1641 if (rx->key) {
1642 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1643 return RX_DROP_MONITOR;
1644
1645 rx->key->tx_rx_count++;
1646 /* TODO: add threshold stuff again */
1647 } else {
1648 return RX_DROP_MONITOR;
1649 }
1650
1651 switch (rx->key->conf.cipher) {
1652 case WLAN_CIPHER_SUITE_WEP40:
1653 case WLAN_CIPHER_SUITE_WEP104:
1654 result = ieee80211_crypto_wep_decrypt(rx);
1655 break;
1656 case WLAN_CIPHER_SUITE_TKIP:
1657 result = ieee80211_crypto_tkip_decrypt(rx);
1658 break;
1659 case WLAN_CIPHER_SUITE_CCMP:
2b2ba0db
JM
1660 result = ieee80211_crypto_ccmp_decrypt(
1661 rx, IEEE80211_CCMP_MIC_LEN);
1662 break;
1663 case WLAN_CIPHER_SUITE_CCMP_256:
1664 result = ieee80211_crypto_ccmp_decrypt(
1665 rx, IEEE80211_CCMP_256_MIC_LEN);
86c228a7
JA
1666 break;
1667 case WLAN_CIPHER_SUITE_AES_CMAC:
1668 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1669 break;
56c52da2
JM
1670 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1671 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1672 break;
8ade538b
JM
1673 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1674 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1675 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1676 break;
00b9cfa3
JM
1677 case WLAN_CIPHER_SUITE_GCMP:
1678 case WLAN_CIPHER_SUITE_GCMP_256:
1679 result = ieee80211_crypto_gcmp_decrypt(rx);
1680 break;
86c228a7 1681 default:
2475b1cc 1682 result = ieee80211_crypto_hw_decrypt(rx);
86c228a7
JA
1683 }
1684
1685 /* the hdr variable is invalid after the decrypt handlers */
1686
1687 /* either the frame has been decrypted or will be dropped */
1688 status->flag |= RX_FLAG_DECRYPTED;
1689
1690 return result;
1691}
1692
571ecf67
JB
1693static inline struct ieee80211_fragment_entry *
1694ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1695 unsigned int frag, unsigned int seq, int rx_queue,
1696 struct sk_buff **skb)
1697{
1698 struct ieee80211_fragment_entry *entry;
571ecf67 1699
571ecf67
JB
1700 entry = &sdata->fragments[sdata->fragment_next++];
1701 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1702 sdata->fragment_next = 0;
1703
bdcbd8e0 1704 if (!skb_queue_empty(&entry->skb_list))
571ecf67 1705 __skb_queue_purge(&entry->skb_list);
571ecf67
JB
1706
1707 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1708 *skb = NULL;
1709 entry->first_frag_time = jiffies;
1710 entry->seq = seq;
1711 entry->rx_queue = rx_queue;
1712 entry->last_frag = frag;
1713 entry->ccmp = 0;
1714 entry->extra_len = 0;
1715
1716 return entry;
1717}
1718
1719static inline struct ieee80211_fragment_entry *
1720ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
b73d70ad 1721 unsigned int frag, unsigned int seq,
571ecf67
JB
1722 int rx_queue, struct ieee80211_hdr *hdr)
1723{
1724 struct ieee80211_fragment_entry *entry;
1725 int i, idx;
1726
1727 idx = sdata->fragment_next;
1728 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1729 struct ieee80211_hdr *f_hdr;
571ecf67
JB
1730
1731 idx--;
1732 if (idx < 0)
1733 idx = IEEE80211_FRAGMENT_MAX - 1;
1734
1735 entry = &sdata->fragments[idx];
1736 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1737 entry->rx_queue != rx_queue ||
1738 entry->last_frag + 1 != frag)
1739 continue;
1740
b73d70ad 1741 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
571ecf67 1742
b73d70ad
HH
1743 /*
1744 * Check ftype and addresses are equal, else check next fragment
1745 */
1746 if (((hdr->frame_control ^ f_hdr->frame_control) &
1747 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
b203ca39
JP
1748 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1749 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
571ecf67
JB
1750 continue;
1751
ab46623e 1752 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
1753 __skb_queue_purge(&entry->skb_list);
1754 continue;
1755 }
1756 return entry;
1757 }
1758
1759 return NULL;
1760}
1761
49461622 1762static ieee80211_rx_result debug_noinline
5cf121c3 1763ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67
JB
1764{
1765 struct ieee80211_hdr *hdr;
1766 u16 sc;
358c8d9d 1767 __le16 fc;
571ecf67
JB
1768 unsigned int frag, seq;
1769 struct ieee80211_fragment_entry *entry;
1770 struct sk_buff *skb;
554891e6 1771 struct ieee80211_rx_status *status;
571ecf67 1772
b73d70ad 1773 hdr = (struct ieee80211_hdr *)rx->skb->data;
358c8d9d 1774 fc = hdr->frame_control;
f7fbf70e
JC
1775
1776 if (ieee80211_is_ctl(fc))
1777 return RX_CONTINUE;
1778
571ecf67
JB
1779 sc = le16_to_cpu(hdr->seq_ctrl);
1780 frag = sc & IEEE80211_SCTL_FRAG;
1781
b8fff407 1782 if (is_multicast_ether_addr(hdr->addr1)) {
c206ca67 1783 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
d025933e 1784 goto out_no_led;
571ecf67 1785 }
b8fff407 1786
d025933e
AM
1787 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1788 goto out;
1789
571ecf67
JB
1790 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1791
e3cf8b3f
ZY
1792 if (skb_linearize(rx->skb))
1793 return RX_DROP_UNUSABLE;
1794
058897a4
AK
1795 /*
1796 * skb_linearize() might change the skb->data and
1797 * previously cached variables (in this case, hdr) need to
1798 * be refreshed with the new data.
1799 */
1800 hdr = (struct ieee80211_hdr *)rx->skb->data;
571ecf67
JB
1801 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1802
1803 if (frag == 0) {
1804 /* This is the first fragment of a new frame. */
1805 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
9e26297a 1806 rx->seqno_idx, &(rx->skb));
2b2ba0db
JM
1807 if (rx->key &&
1808 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
1809 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256) &&
358c8d9d 1810 ieee80211_has_protected(fc)) {
9e26297a 1811 int queue = rx->security_idx;
571ecf67
JB
1812 /* Store CCMP PN so that we can verify that the next
1813 * fragment has a sequential PN value. */
1814 entry->ccmp = 1;
1815 memcpy(entry->last_pn,
9190252c 1816 rx->key->u.ccmp.rx_pn[queue],
4325f6ca 1817 IEEE80211_CCMP_PN_LEN);
571ecf67 1818 }
9ae54c84 1819 return RX_QUEUED;
571ecf67
JB
1820 }
1821
1822 /* This is a fragment for a frame that should already be pending in
1823 * fragment cache. Add this fragment to the end of the pending entry.
1824 */
9e26297a
JB
1825 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1826 rx->seqno_idx, hdr);
571ecf67
JB
1827 if (!entry) {
1828 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
e4c26add 1829 return RX_DROP_MONITOR;
571ecf67
JB
1830 }
1831
1832 /* Verify that MPDUs within one MSDU have sequential PN values.
1833 * (IEEE 802.11i, 8.3.3.4.5) */
1834 if (entry->ccmp) {
1835 int i;
4325f6ca 1836 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
9190252c 1837 int queue;
2b2ba0db
JM
1838 if (!rx->key ||
1839 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
1840 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256))
e4c26add 1841 return RX_DROP_UNUSABLE;
4325f6ca
JB
1842 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1843 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
571ecf67
JB
1844 pn[i]++;
1845 if (pn[i])
1846 break;
1847 }
9e26297a 1848 queue = rx->security_idx;
9190252c 1849 rpn = rx->key->u.ccmp.rx_pn[queue];
4325f6ca 1850 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
e4c26add 1851 return RX_DROP_UNUSABLE;
4325f6ca 1852 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
571ecf67
JB
1853 }
1854
358c8d9d 1855 skb_pull(rx->skb, ieee80211_hdrlen(fc));
571ecf67
JB
1856 __skb_queue_tail(&entry->skb_list, rx->skb);
1857 entry->last_frag = frag;
1858 entry->extra_len += rx->skb->len;
358c8d9d 1859 if (ieee80211_has_morefrags(fc)) {
571ecf67 1860 rx->skb = NULL;
9ae54c84 1861 return RX_QUEUED;
571ecf67
JB
1862 }
1863
1864 rx->skb = __skb_dequeue(&entry->skb_list);
1865 if (skb_tailroom(rx->skb) < entry->extra_len) {
f1160434 1866 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
571ecf67
JB
1867 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1868 GFP_ATOMIC))) {
1869 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1870 __skb_queue_purge(&entry->skb_list);
e4c26add 1871 return RX_DROP_UNUSABLE;
571ecf67
JB
1872 }
1873 }
1874 while ((skb = __skb_dequeue(&entry->skb_list))) {
1875 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1876 dev_kfree_skb(skb);
1877 }
1878
1879 /* Complete frame has been reassembled - process it now */
554891e6
JB
1880 status = IEEE80211_SKB_RXCB(rx->skb);
1881 status->rx_flags |= IEEE80211_RX_FRAGMENTED;
571ecf67
JB
1882
1883 out:
d025933e
AM
1884 ieee80211_led_rx(rx->local);
1885 out_no_led:
571ecf67
JB
1886 if (rx->sta)
1887 rx->sta->rx_packets++;
9ae54c84 1888 return RX_CONTINUE;
571ecf67
JB
1889}
1890
1df332e8 1891static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 1892{
1df332e8 1893 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
76ee65bf 1894 return -EACCES;
571ecf67 1895
76ee65bf 1896 return 0;
571ecf67
JB
1897}
1898
1df332e8 1899static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
571ecf67 1900{
eb9fb5b8
JB
1901 struct sk_buff *skb = rx->skb;
1902 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1903
3017b80b 1904 /*
7848ba7d
JB
1905 * Pass through unencrypted frames if the hardware has
1906 * decrypted them already.
3017b80b 1907 */
eb9fb5b8 1908 if (status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1909 return 0;
571ecf67
JB
1910
1911 /* Drop unencrypted frames if key is set. */
358c8d9d
HH
1912 if (unlikely(!ieee80211_has_protected(fc) &&
1913 !ieee80211_is_nullfunc(fc) &&
e8f4fb7c 1914 ieee80211_is_data(fc) && rx->key))
76ee65bf 1915 return -EACCES;
bef5d1c7
JB
1916
1917 return 0;
1918}
1919
1df332e8 1920static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
bef5d1c7
JB
1921{
1922 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
e3efca0a 1923 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
bef5d1c7 1924 __le16 fc = hdr->frame_control;
bef5d1c7 1925
e3efca0a
JM
1926 /*
1927 * Pass through unencrypted frames if the hardware has
1928 * decrypted them already.
1929 */
1930 if (status->flag & RX_FLAG_DECRYPTED)
1931 return 0;
bef5d1c7 1932
c2c98fde 1933 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
d211e90e
JM
1934 if (unlikely(!ieee80211_has_protected(fc) &&
1935 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1936 rx->key)) {
6ff57cf8
JB
1937 if (ieee80211_is_deauth(fc) ||
1938 ieee80211_is_disassoc(fc))
1939 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1940 rx->skb->data,
1941 rx->skb->len);
f2ca3ea4 1942 return -EACCES;
cf4e594e 1943 }
f2ca3ea4 1944 /* BIP does not use Protected field, so need to check MMIE */
f64f9e71 1945 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1946 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
6ff57cf8
JB
1947 if (ieee80211_is_deauth(fc) ||
1948 ieee80211_is_disassoc(fc))
1949 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1950 rx->skb->data,
1951 rx->skb->len);
f2ca3ea4 1952 return -EACCES;
cf4e594e 1953 }
f2ca3ea4
JM
1954 /*
1955 * When using MFP, Action frames are not allowed prior to
1956 * having configured keys.
1957 */
1958 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
d8ca16db 1959 ieee80211_is_robust_mgmt_frame(rx->skb)))
f2ca3ea4
JM
1960 return -EACCES;
1961 }
b3fc9c6c 1962
76ee65bf 1963 return 0;
571ecf67
JB
1964}
1965
76ee65bf 1966static int
4114fa21 1967__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
571ecf67 1968{
eb9fb5b8 1969 struct ieee80211_sub_if_data *sdata = rx->sdata;
f14543ee 1970 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
fbb327c5
FF
1971 bool check_port_control = false;
1972 struct ethhdr *ehdr;
1973 int ret;
f14543ee 1974
4114fa21 1975 *port_control = false;
9bc383de
JB
1976 if (ieee80211_has_a4(hdr->frame_control) &&
1977 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
f14543ee 1978 return -1;
9bc383de 1979
fbb327c5
FF
1980 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1981 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
1982
1983 if (!sdata->u.mgd.use_4addr)
1984 return -1;
1985 else
1986 check_port_control = true;
1987 }
1988
9bc383de 1989 if (is_multicast_ether_addr(hdr->addr1) &&
fbb327c5 1990 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
f14543ee 1991 return -1;
571ecf67 1992
fbb327c5 1993 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
4114fa21 1994 if (ret < 0)
fbb327c5
FF
1995 return ret;
1996
1997 ehdr = (struct ethhdr *) rx->skb->data;
4114fa21
FF
1998 if (ehdr->h_proto == rx->sdata->control_port_protocol)
1999 *port_control = true;
2000 else if (check_port_control)
fbb327c5
FF
2001 return -1;
2002
2003 return 0;
76ee65bf 2004}
571ecf67 2005
ce3edf6d
JB
2006/*
2007 * requires that rx->skb is a frame with ethernet header
2008 */
358c8d9d 2009static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
ce3edf6d 2010{
c97c23e3 2011 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
ce3edf6d
JB
2012 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2013 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2014
2015 /*
2016 * Allow EAPOL frames to us/the PAE group address regardless
2017 * of whether the frame was encrypted or not.
2018 */
a621fa4d 2019 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
3bc7945e
JP
2020 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2021 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
ce3edf6d
JB
2022 return true;
2023
2024 if (ieee80211_802_1x_port_control(rx) ||
358c8d9d 2025 ieee80211_drop_unencrypted(rx, fc))
ce3edf6d
JB
2026 return false;
2027
2028 return true;
2029}
2030
2031/*
2032 * requires that rx->skb is a frame with ethernet header
2033 */
76ee65bf 2034static void
5cf121c3 2035ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf 2036{
eb9fb5b8
JB
2037 struct ieee80211_sub_if_data *sdata = rx->sdata;
2038 struct net_device *dev = sdata->dev;
76ee65bf 2039 struct sk_buff *skb, *xmit_skb;
ce3edf6d
JB
2040 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2041 struct sta_info *dsta;
571ecf67 2042
76ee65bf
RR
2043 skb = rx->skb;
2044 xmit_skb = NULL;
571ecf67 2045
5a490510
JB
2046 ieee80211_rx_stats(dev, skb->len);
2047
05c914fe
JB
2048 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2049 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
213cd118 2050 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
9bc383de 2051 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
ce3edf6d
JB
2052 if (is_multicast_ether_addr(ehdr->h_dest)) {
2053 /*
2054 * send multicast frames both to higher layers in
2055 * local net stack and back to the wireless medium
2056 */
76ee65bf 2057 xmit_skb = skb_copy(skb, GFP_ATOMIC);
e87cc472 2058 if (!xmit_skb)
bdcbd8e0 2059 net_info_ratelimited("%s: failed to clone multicast frame\n",
e87cc472 2060 dev->name);
571ecf67 2061 } else {
abe60632
JB
2062 dsta = sta_info_get(sdata, skb->data);
2063 if (dsta) {
ce3edf6d
JB
2064 /*
2065 * The destination station is associated to
2066 * this AP (in this VLAN), so send the frame
2067 * directly to it and do not pass it to local
2068 * net stack.
571ecf67 2069 */
76ee65bf 2070 xmit_skb = skb;
571ecf67
JB
2071 skb = NULL;
2072 }
571ecf67
JB
2073 }
2074 }
2075
59d9cb07 2076#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
9e890a1f
JB
2077 if (skb) {
2078 /* 'align' will only take the values 0 or 2 here since all
2079 * frames are required to be aligned to 2-byte boundaries
2080 * when being passed to mac80211; the code here works just
2081 * as well if that isn't true, but mac80211 assumes it can
2082 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
d1c3a37c 2083 */
9e890a1f
JB
2084 int align;
2085
2086 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
d1c3a37c
JB
2087 if (align) {
2088 if (WARN_ON(skb_headroom(skb) < 3)) {
2089 dev_kfree_skb(skb);
2090 skb = NULL;
2091 } else {
2092 u8 *data = skb->data;
8ce0b589
ZY
2093 size_t len = skb_headlen(skb);
2094 skb->data -= align;
2095 memmove(skb->data, data, len);
2096 skb_set_tail_pointer(skb, len);
d1c3a37c
JB
2097 }
2098 }
9e890a1f 2099 }
d1c3a37c
JB
2100#endif
2101
9e890a1f
JB
2102 if (skb) {
2103 /* deliver to local stack */
2104 skb->protocol = eth_type_trans(skb, dev);
2105 memset(skb->cb, 0, sizeof(skb->cb));
22d3a3c8
JB
2106 if (!(rx->flags & IEEE80211_RX_REORDER_TIMER) &&
2107 rx->local->napi)
06d181a8
JB
2108 napi_gro_receive(rx->local->napi, skb);
2109 else
2110 netif_receive_skb(skb);
571ecf67
JB
2111 }
2112
76ee65bf 2113 if (xmit_skb) {
aef6c928
HS
2114 /*
2115 * Send to wireless media and increase priority by 256 to
2116 * keep the received priority instead of reclassifying
2117 * the frame (see cfg80211_classify8021d).
2118 */
2119 xmit_skb->priority += 256;
f831e909 2120 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
2121 skb_reset_network_header(xmit_skb);
2122 skb_reset_mac_header(xmit_skb);
76ee65bf 2123 dev_queue_xmit(xmit_skb);
571ecf67 2124 }
76ee65bf
RR
2125}
2126
49461622 2127static ieee80211_rx_result debug_noinline
5cf121c3 2128ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
fd4c7f2f 2129{
eb9fb5b8 2130 struct net_device *dev = rx->sdata->dev;
eaf85ca7 2131 struct sk_buff *skb = rx->skb;
358c8d9d
HH
2132 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2133 __le16 fc = hdr->frame_control;
eaf85ca7 2134 struct sk_buff_head frame_list;
554891e6 2135 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
fd4c7f2f 2136
358c8d9d 2137 if (unlikely(!ieee80211_is_data(fc)))
9ae54c84 2138 return RX_CONTINUE;
fd4c7f2f 2139
358c8d9d 2140 if (unlikely(!ieee80211_is_data_present(fc)))
e4c26add 2141 return RX_DROP_MONITOR;
fd4c7f2f 2142
554891e6 2143 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
9ae54c84 2144 return RX_CONTINUE;
fd4c7f2f 2145
eaf85ca7
ZY
2146 if (ieee80211_has_a4(hdr->frame_control) &&
2147 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2148 !rx->sdata->u.vlan.sta)
e4c26add 2149 return RX_DROP_UNUSABLE;
fd4c7f2f 2150
eaf85ca7
ZY
2151 if (is_multicast_ether_addr(hdr->addr1) &&
2152 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2153 rx->sdata->u.vlan.sta) ||
2154 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2155 rx->sdata->u.mgd.use_4addr)))
e4c26add 2156 return RX_DROP_UNUSABLE;
fd4c7f2f 2157
eaf85ca7
ZY
2158 skb->dev = dev;
2159 __skb_queue_head_init(&frame_list);
fd4c7f2f 2160
e3cf8b3f
ZY
2161 if (skb_linearize(skb))
2162 return RX_DROP_UNUSABLE;
2163
eaf85ca7
ZY
2164 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2165 rx->sdata->vif.type,
8b3becad 2166 rx->local->hw.extra_tx_headroom, true);
fd4c7f2f 2167
eaf85ca7
ZY
2168 while (!skb_queue_empty(&frame_list)) {
2169 rx->skb = __skb_dequeue(&frame_list);
fd4c7f2f 2170
358c8d9d 2171 if (!ieee80211_frame_allowed(rx, fc)) {
eaf85ca7 2172 dev_kfree_skb(rx->skb);
ce3edf6d
JB
2173 continue;
2174 }
fd4c7f2f
RR
2175
2176 ieee80211_deliver_skb(rx);
2177 }
2178
9ae54c84 2179 return RX_QUEUED;
fd4c7f2f
RR
2180}
2181
bf94e17b 2182#ifdef CONFIG_MAC80211_MESH
b0dee578 2183static ieee80211_rx_result
e32f85f7
LCC
2184ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2185{
30789eb6
TP
2186 struct ieee80211_hdr *fwd_hdr, *hdr;
2187 struct ieee80211_tx_info *info;
e32f85f7 2188 struct ieee80211s_hdr *mesh_hdr;
e32f85f7 2189 struct sk_buff *skb = rx->skb, *fwd_skb;
3b8d81e0 2190 struct ieee80211_local *local = rx->local;
eb9fb5b8 2191 struct ieee80211_sub_if_data *sdata = rx->sdata;
30789eb6 2192 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
30789eb6 2193 u16 q, hdrlen;
e32f85f7
LCC
2194
2195 hdr = (struct ieee80211_hdr *) skb->data;
2196 hdrlen = ieee80211_hdrlen(hdr->frame_control);
9b395bc3
JB
2197
2198 /* make sure fixed part of mesh header is there, also checks skb len */
2199 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2200 return RX_DROP_MONITOR;
2201
2202 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2203
2204 /* make sure full mesh header is there, also checks skb len */
2205 if (!pskb_may_pull(rx->skb,
2206 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2207 return RX_DROP_MONITOR;
2208
2209 /* reload pointers */
2210 hdr = (struct ieee80211_hdr *) skb->data;
e32f85f7
LCC
2211 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2212
d0c22119
BC
2213 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2214 return RX_DROP_MONITOR;
2215
2157fdd6
TP
2216 /* frame is in RMC, don't forward */
2217 if (ieee80211_is_data(hdr->frame_control) &&
2218 is_multicast_ether_addr(hdr->addr1) &&
bf7cd94d 2219 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2157fdd6
TP
2220 return RX_DROP_MONITOR;
2221
5c90067c 2222 if (!ieee80211_is_data(hdr->frame_control))
e32f85f7
LCC
2223 return RX_CONTINUE;
2224
2225 if (!mesh_hdr->ttl)
e32f85f7
LCC
2226 return RX_DROP_MONITOR;
2227
43b7b314 2228 if (mesh_hdr->flags & MESH_FLAGS_AE) {
79617dee 2229 struct mesh_path *mppath;
43b7b314
JC
2230 char *proxied_addr;
2231 char *mpp_addr;
2232
2233 if (is_multicast_ether_addr(hdr->addr1)) {
2234 mpp_addr = hdr->addr3;
2235 proxied_addr = mesh_hdr->eaddr1;
9b395bc3
JB
2236 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2237 /* has_a4 already checked in ieee80211_rx_mesh_check */
43b7b314
JC
2238 mpp_addr = hdr->addr4;
2239 proxied_addr = mesh_hdr->eaddr2;
9b395bc3
JB
2240 } else {
2241 return RX_DROP_MONITOR;
43b7b314 2242 }
79617dee 2243
79617dee 2244 rcu_read_lock();
bf7cd94d 2245 mppath = mpp_path_lookup(sdata, proxied_addr);
79617dee 2246 if (!mppath) {
bf7cd94d 2247 mpp_path_add(sdata, proxied_addr, mpp_addr);
79617dee
Y
2248 } else {
2249 spin_lock_bh(&mppath->state_lock);
b203ca39 2250 if (!ether_addr_equal(mppath->mpp, mpp_addr))
43b7b314 2251 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
79617dee
Y
2252 spin_unlock_bh(&mppath->state_lock);
2253 }
2254 rcu_read_unlock();
2255 }
2256
3c5772a5
JC
2257 /* Frame has reached destination. Don't forward */
2258 if (!is_multicast_ether_addr(hdr->addr1) &&
b203ca39 2259 ether_addr_equal(sdata->vif.addr, hdr->addr3))
e32f85f7
LCC
2260 return RX_CONTINUE;
2261
00e96dec 2262 q = ieee80211_select_queue_80211(sdata, skb, hdr);
d3c1597b 2263 if (ieee80211_queue_stopped(&local->hw, q)) {
30789eb6 2264 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
d3c1597b
TP
2265 return RX_DROP_MONITOR;
2266 }
2267 skb_set_queue_mapping(skb, q);
e32f85f7 2268
30789eb6
TP
2269 if (!--mesh_hdr->ttl) {
2270 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2ac64cd1 2271 goto out;
30789eb6
TP
2272 }
2273
d665508b
CYY
2274 if (!ifmsh->mshcfg.dot11MeshForwarding)
2275 goto out;
2276
30789eb6
TP
2277 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2278 if (!fwd_skb) {
bdcbd8e0 2279 net_info_ratelimited("%s: failed to clone mesh frame\n",
e87cc472 2280 sdata->name);
30789eb6
TP
2281 goto out;
2282 }
2283
2284 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
9d6d6f49 2285 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
30789eb6
TP
2286 info = IEEE80211_SKB_CB(fwd_skb);
2287 memset(info, 0, sizeof(*info));
2288 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2289 info->control.vif = &rx->sdata->vif;
2290 info->control.jiffies = jiffies;
2291 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2292 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2293 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
3f52b7e3
MP
2294 /* update power mode indication when forwarding */
2295 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
bf7cd94d 2296 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
3f52b7e3 2297 /* mesh power mode flags updated in mesh_nexthop_lookup */
30789eb6
TP
2298 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2299 } else {
2300 /* unable to resolve next hop */
bf7cd94d 2301 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
f63f8421
CYY
2302 fwd_hdr->addr3, 0,
2303 WLAN_REASON_MESH_PATH_NOFORWARD,
2304 fwd_hdr->addr2);
30789eb6 2305 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
74b8cc3d 2306 kfree_skb(fwd_skb);
30789eb6 2307 return RX_DROP_MONITOR;
e32f85f7
LCC
2308 }
2309
30789eb6
TP
2310 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2311 ieee80211_add_pending_skb(local, fwd_skb);
b51aff05 2312 out:
df140465 2313 if (is_multicast_ether_addr(hdr->addr1))
e32f85f7 2314 return RX_CONTINUE;
df140465 2315 return RX_DROP_MONITOR;
e32f85f7 2316}
bf94e17b 2317#endif
e32f85f7 2318
49461622 2319static ieee80211_rx_result debug_noinline
5cf121c3 2320ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf 2321{
eb9fb5b8 2322 struct ieee80211_sub_if_data *sdata = rx->sdata;
e15276a4 2323 struct ieee80211_local *local = rx->local;
eb9fb5b8 2324 struct net_device *dev = sdata->dev;
358c8d9d
HH
2325 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2326 __le16 fc = hdr->frame_control;
4114fa21 2327 bool port_control;
ce3edf6d 2328 int err;
76ee65bf 2329
358c8d9d 2330 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
9ae54c84 2331 return RX_CONTINUE;
76ee65bf 2332
358c8d9d 2333 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
e4c26add 2334 return RX_DROP_MONITOR;
76ee65bf 2335
79c892b8 2336 if (rx->sta) {
3d6dc343 2337 /* The seqno index has the same property as needed
79c892b8
JB
2338 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2339 * for non-QoS-data frames. Here we know it's a data
2340 * frame, so count MSDUs.
2341 */
3d6dc343 2342 rx->sta->rx_msdu[rx->seqno_idx]++;
79c892b8
JB
2343 }
2344
f14543ee 2345 /*
e7f4a940
JB
2346 * Send unexpected-4addr-frame event to hostapd. For older versions,
2347 * also drop the frame to cooked monitor interfaces.
f14543ee
FF
2348 */
2349 if (ieee80211_has_a4(hdr->frame_control) &&
e7f4a940
JB
2350 sdata->vif.type == NL80211_IFTYPE_AP) {
2351 if (rx->sta &&
2352 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2353 cfg80211_rx_unexpected_4addr_frame(
2354 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
f14543ee 2355 return RX_DROP_MONITOR;
e7f4a940 2356 }
f14543ee 2357
4114fa21 2358 err = __ieee80211_data_to_8023(rx, &port_control);
76ee65bf 2359 if (unlikely(err))
e4c26add 2360 return RX_DROP_UNUSABLE;
76ee65bf 2361
358c8d9d 2362 if (!ieee80211_frame_allowed(rx, fc))
e4c26add 2363 return RX_DROP_MONITOR;
ce3edf6d 2364
8a4d32f3
AN
2365 /* directly handle TDLS channel switch requests/responses */
2366 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2367 cpu_to_be16(ETH_P_TDLS))) {
2368 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2369
2370 if (pskb_may_pull(rx->skb,
2371 offsetof(struct ieee80211_tdls_data, u)) &&
2372 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2373 tf->category == WLAN_CATEGORY_TDLS &&
2374 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2375 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2376 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TDLS_CHSW;
2377 skb_queue_tail(&sdata->skb_queue, rx->skb);
2378 ieee80211_queue_work(&rx->local->hw, &sdata->work);
2379 if (rx->sta)
2380 rx->sta->rx_packets++;
2381
2382 return RX_QUEUED;
2383 }
2384 }
2385
4114fa21
FF
2386 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2387 unlikely(port_control) && sdata->bss) {
2388 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2389 u.ap);
2390 dev = sdata->dev;
2391 rx->sdata = sdata;
2392 }
2393
76ee65bf
RR
2394 rx->skb->dev = dev;
2395
08ca944e 2396 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
8c99f691
RM
2397 !is_multicast_ether_addr(
2398 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2399 (!local->scanning &&
2400 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
e15276a4
VN
2401 mod_timer(&local->dynamic_ps_timer, jiffies +
2402 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2403 }
2404
76ee65bf 2405 ieee80211_deliver_skb(rx);
571ecf67 2406
9ae54c84 2407 return RX_QUEUED;
571ecf67
JB
2408}
2409
49461622 2410static ieee80211_rx_result debug_noinline
f9e124fb 2411ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
71364716 2412{
71364716 2413 struct sk_buff *skb = rx->skb;
a7767f95 2414 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
71364716
RR
2415 struct tid_ampdu_rx *tid_agg_rx;
2416 u16 start_seq_num;
2417 u16 tid;
2418
a7767f95 2419 if (likely(!ieee80211_is_ctl(bar->frame_control)))
9ae54c84 2420 return RX_CONTINUE;
71364716 2421
a7767f95 2422 if (ieee80211_is_back_req(bar->frame_control)) {
8ae5977f
JB
2423 struct {
2424 __le16 control, start_seq_num;
2425 } __packed bar_data;
6382246e
EG
2426 struct ieee80211_event event = {
2427 .type = BAR_RX_EVENT,
2428 };
8ae5977f 2429
71364716 2430 if (!rx->sta)
a02ae758 2431 return RX_DROP_MONITOR;
8ae5977f
JB
2432
2433 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2434 &bar_data, sizeof(bar_data)))
2435 return RX_DROP_MONITOR;
2436
8ae5977f 2437 tid = le16_to_cpu(bar_data.control) >> 12;
a87f736d
JB
2438
2439 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2440 if (!tid_agg_rx)
a02ae758 2441 return RX_DROP_MONITOR;
71364716 2442
8ae5977f 2443 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
6382246e
EG
2444 event.u.ba.tid = tid;
2445 event.u.ba.ssn = start_seq_num;
2446 event.u.ba.sta = &rx->sta->sta;
71364716
RR
2447
2448 /* reset session timer */
20ad19d0
JB
2449 if (tid_agg_rx->timeout)
2450 mod_timer(&tid_agg_rx->session_timer,
2451 TU_TO_EXP_TIME(tid_agg_rx->timeout));
71364716 2452
dd318575 2453 spin_lock(&tid_agg_rx->reorder_lock);
a02ae758 2454 /* release stored frames up to start of BAR */
d3b2fb53 2455 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
f9e124fb 2456 start_seq_num, frames);
dd318575
JB
2457 spin_unlock(&tid_agg_rx->reorder_lock);
2458
6382246e
EG
2459 drv_event_callback(rx->local, rx->sdata, &event);
2460
a02ae758
JB
2461 kfree_skb(skb);
2462 return RX_QUEUED;
71364716
RR
2463 }
2464
08daecae
JB
2465 /*
2466 * After this point, we only want management frames,
2467 * so we can drop all remaining control frames to
2468 * cooked monitor interfaces.
2469 */
2470 return RX_DROP_MONITOR;
71364716
RR
2471}
2472
f4f727a6
JM
2473static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2474 struct ieee80211_mgmt *mgmt,
2475 size_t len)
fea14732
JM
2476{
2477 struct ieee80211_local *local = sdata->local;
2478 struct sk_buff *skb;
2479 struct ieee80211_mgmt *resp;
2480
b203ca39 2481 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
fea14732
JM
2482 /* Not to own unicast address */
2483 return;
2484 }
2485
b203ca39
JP
2486 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2487 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
77fdaa12 2488 /* Not from the current AP or not associated yet. */
fea14732
JM
2489 return;
2490 }
2491
2492 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2493 /* Too short SA Query request frame */
2494 return;
2495 }
2496
2497 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2498 if (skb == NULL)
2499 return;
2500
2501 skb_reserve(skb, local->hw.extra_tx_headroom);
2502 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2503 memset(resp, 0, 24);
2504 memcpy(resp->da, mgmt->sa, ETH_ALEN);
47846c9b 2505 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
46900298 2506 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
fea14732
JM
2507 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2508 IEEE80211_STYPE_ACTION);
2509 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2510 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2511 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2512 memcpy(resp->u.action.u.sa_query.trans_id,
2513 mgmt->u.action.u.sa_query.trans_id,
2514 WLAN_SA_QUERY_TR_ID_LEN);
2515
62ae67be 2516 ieee80211_tx_skb(sdata, skb);
fea14732
JM
2517}
2518
2e161f78
JB
2519static ieee80211_rx_result debug_noinline
2520ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2521{
2522 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2523 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2524
2525 /*
2526 * From here on, look only at management frames.
2527 * Data and control frames are already handled,
2528 * and unknown (reserved) frames are useless.
2529 */
2530 if (rx->skb->len < 24)
2531 return RX_DROP_MONITOR;
2532
2533 if (!ieee80211_is_mgmt(mgmt->frame_control))
2534 return RX_DROP_MONITOR;
2535
ee971924
JB
2536 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2537 ieee80211_is_beacon(mgmt->frame_control) &&
2538 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
804483e9
JB
2539 int sig = 0;
2540
30686bf7 2541 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
804483e9
JB
2542 sig = status->signal;
2543
ee971924
JB
2544 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2545 rx->skb->data, rx->skb->len,
37c73b5f 2546 status->freq, sig);
ee971924
JB
2547 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2548 }
2549
2e161f78
JB
2550 if (ieee80211_drop_unencrypted_mgmt(rx))
2551 return RX_DROP_UNUSABLE;
2552
2553 return RX_CONTINUE;
2554}
2555
de1ede7a
JB
2556static ieee80211_rx_result debug_noinline
2557ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2558{
2559 struct ieee80211_local *local = rx->local;
eb9fb5b8 2560 struct ieee80211_sub_if_data *sdata = rx->sdata;
de1ede7a 2561 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2562 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
de1ede7a
JB
2563 int len = rx->skb->len;
2564
2565 if (!ieee80211_is_action(mgmt->frame_control))
2566 return RX_CONTINUE;
2567
026331c4
JM
2568 /* drop too small frames */
2569 if (len < IEEE80211_MIN_ACTION_SIZE)
84040805 2570 return RX_DROP_UNUSABLE;
de1ede7a 2571
815b8092 2572 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
cd7760e6
SW
2573 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2574 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
84040805 2575 return RX_DROP_UNUSABLE;
de1ede7a 2576
de1ede7a 2577 switch (mgmt->u.action.category) {
1d8d3dec
JB
2578 case WLAN_CATEGORY_HT:
2579 /* reject HT action frames from stations not supporting HT */
2580 if (!rx->sta->sta.ht_cap.ht_supported)
2581 goto invalid;
2582
2583 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2584 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2585 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2586 sdata->vif.type != NL80211_IFTYPE_AP &&
2587 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2588 break;
2589
ec61cd63 2590 /* verify action & smps_control/chanwidth are present */
1d8d3dec
JB
2591 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2592 goto invalid;
2593
2594 switch (mgmt->u.action.u.ht_smps.action) {
2595 case WLAN_HT_ACTION_SMPS: {
2596 struct ieee80211_supported_band *sband;
af0ed69b 2597 enum ieee80211_smps_mode smps_mode;
1d8d3dec
JB
2598
2599 /* convert to HT capability */
2600 switch (mgmt->u.action.u.ht_smps.smps_control) {
2601 case WLAN_HT_SMPS_CONTROL_DISABLED:
af0ed69b 2602 smps_mode = IEEE80211_SMPS_OFF;
1d8d3dec
JB
2603 break;
2604 case WLAN_HT_SMPS_CONTROL_STATIC:
af0ed69b 2605 smps_mode = IEEE80211_SMPS_STATIC;
1d8d3dec
JB
2606 break;
2607 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
af0ed69b 2608 smps_mode = IEEE80211_SMPS_DYNAMIC;
1d8d3dec
JB
2609 break;
2610 default:
2611 goto invalid;
2612 }
1d8d3dec
JB
2613
2614 /* if no change do nothing */
af0ed69b 2615 if (rx->sta->sta.smps_mode == smps_mode)
1d8d3dec 2616 goto handled;
af0ed69b 2617 rx->sta->sta.smps_mode = smps_mode;
1d8d3dec
JB
2618
2619 sband = rx->local->hw.wiphy->bands[status->band];
2620
64f68e5d
JB
2621 rate_control_rate_update(local, sband, rx->sta,
2622 IEEE80211_RC_SMPS_CHANGED);
1d8d3dec
JB
2623 goto handled;
2624 }
ec61cd63
JB
2625 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2626 struct ieee80211_supported_band *sband;
2627 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
1c45c5ce 2628 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
ec61cd63
JB
2629
2630 /* If it doesn't support 40 MHz it can't change ... */
e1a0c6b3
JB
2631 if (!(rx->sta->sta.ht_cap.cap &
2632 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
ec61cd63
JB
2633 goto handled;
2634
e1a0c6b3 2635 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
1c45c5ce 2636 max_bw = IEEE80211_STA_RX_BW_20;
e1a0c6b3 2637 else
1c45c5ce
EP
2638 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2639
2640 /* set cur_max_bandwidth and recalc sta bw */
2641 rx->sta->cur_max_bandwidth = max_bw;
2642 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
ec61cd63 2643
e1a0c6b3 2644 if (rx->sta->sta.bandwidth == new_bw)
ec61cd63
JB
2645 goto handled;
2646
1c45c5ce 2647 rx->sta->sta.bandwidth = new_bw;
ec61cd63
JB
2648 sband = rx->local->hw.wiphy->bands[status->band];
2649
2650 rate_control_rate_update(local, sband, rx->sta,
2651 IEEE80211_RC_BW_CHANGED);
2652 goto handled;
2653 }
1d8d3dec
JB
2654 default:
2655 goto invalid;
2656 }
2657
0af83d3d 2658 break;
1b3a2e49
JB
2659 case WLAN_CATEGORY_PUBLIC:
2660 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2661 goto invalid;
2662 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2663 break;
2664 if (!rx->sta)
2665 break;
2666 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2667 break;
2668 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2669 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2670 break;
2671 if (len < offsetof(struct ieee80211_mgmt,
2672 u.action.u.ext_chan_switch.variable))
2673 goto invalid;
2674 goto queue;
0af83d3d
JB
2675 case WLAN_CATEGORY_VHT:
2676 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2677 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2678 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2679 sdata->vif.type != NL80211_IFTYPE_AP &&
2680 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2681 break;
2682
2683 /* verify action code is present */
2684 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2685 goto invalid;
2686
2687 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2688 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2689 u8 opmode;
2690
2691 /* verify opmode is present */
2692 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2693 goto invalid;
2694
2695 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2696
2697 ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
bee7f586
JB
2698 opmode, status->band,
2699 false);
0af83d3d
JB
2700 goto handled;
2701 }
2702 default:
2703 break;
2704 }
1d8d3dec 2705 break;
de1ede7a 2706 case WLAN_CATEGORY_BACK:
8abd3f9b 2707 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
ae2772b3 2708 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
8abd3f9b 2709 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
13c40c54
AS
2710 sdata->vif.type != NL80211_IFTYPE_AP &&
2711 sdata->vif.type != NL80211_IFTYPE_ADHOC)
84040805 2712 break;
8abd3f9b 2713
026331c4
JM
2714 /* verify action_code is present */
2715 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2716 break;
2717
de1ede7a
JB
2718 switch (mgmt->u.action.u.addba_req.action_code) {
2719 case WLAN_ACTION_ADDBA_REQ:
2720 if (len < (IEEE80211_MIN_ACTION_SIZE +
2721 sizeof(mgmt->u.action.u.addba_req)))
bed7ee6e
JB
2722 goto invalid;
2723 break;
de1ede7a
JB
2724 case WLAN_ACTION_ADDBA_RESP:
2725 if (len < (IEEE80211_MIN_ACTION_SIZE +
2726 sizeof(mgmt->u.action.u.addba_resp)))
bed7ee6e
JB
2727 goto invalid;
2728 break;
de1ede7a
JB
2729 case WLAN_ACTION_DELBA:
2730 if (len < (IEEE80211_MIN_ACTION_SIZE +
2731 sizeof(mgmt->u.action.u.delba)))
bed7ee6e
JB
2732 goto invalid;
2733 break;
2734 default:
2735 goto invalid;
de1ede7a 2736 }
bed7ee6e 2737
8b58ff83 2738 goto queue;
39192c0b 2739 case WLAN_CATEGORY_SPECTRUM_MGMT:
026331c4
JM
2740 /* verify action_code is present */
2741 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2742 break;
2743
39192c0b
JB
2744 switch (mgmt->u.action.u.measurement.action_code) {
2745 case WLAN_ACTION_SPCT_MSR_REQ:
cd7760e6
SW
2746 if (status->band != IEEE80211_BAND_5GHZ)
2747 break;
2748
39192c0b
JB
2749 if (len < (IEEE80211_MIN_ACTION_SIZE +
2750 sizeof(mgmt->u.action.u.measurement)))
84040805 2751 break;
cd7760e6
SW
2752
2753 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2754 break;
2755
39192c0b 2756 ieee80211_process_measurement_req(sdata, mgmt, len);
84040805 2757 goto handled;
cd7760e6
SW
2758 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2759 u8 *bssid;
2760 if (len < (IEEE80211_MIN_ACTION_SIZE +
2761 sizeof(mgmt->u.action.u.chan_switch)))
84040805 2762 break;
cc32abd4 2763
cd7760e6 2764 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
b8456a14
CYY
2765 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2766 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
cd7760e6
SW
2767 break;
2768
2769 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2770 bssid = sdata->u.mgd.bssid;
2771 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2772 bssid = sdata->u.ibss.bssid;
b8456a14
CYY
2773 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2774 bssid = mgmt->sa;
cd7760e6
SW
2775 else
2776 break;
2777
2778 if (!ether_addr_equal(mgmt->bssid, bssid))
84040805 2779 break;
c481ec97 2780
8b58ff83 2781 goto queue;
cd7760e6 2782 }
39192c0b
JB
2783 }
2784 break;
fea14732
JM
2785 case WLAN_CATEGORY_SA_QUERY:
2786 if (len < (IEEE80211_MIN_ACTION_SIZE +
2787 sizeof(mgmt->u.action.u.sa_query)))
84040805
JB
2788 break;
2789
fea14732
JM
2790 switch (mgmt->u.action.u.sa_query.action) {
2791 case WLAN_ACTION_SA_QUERY_REQUEST:
2792 if (sdata->vif.type != NL80211_IFTYPE_STATION)
84040805 2793 break;
fea14732 2794 ieee80211_process_sa_query_req(sdata, mgmt, len);
84040805 2795 goto handled;
fea14732
JM
2796 }
2797 break;
8db09850 2798 case WLAN_CATEGORY_SELF_PROTECTED:
9b395bc3
JB
2799 if (len < (IEEE80211_MIN_ACTION_SIZE +
2800 sizeof(mgmt->u.action.u.self_prot.action_code)))
2801 break;
2802
8db09850
TP
2803 switch (mgmt->u.action.u.self_prot.action_code) {
2804 case WLAN_SP_MESH_PEERING_OPEN:
2805 case WLAN_SP_MESH_PEERING_CLOSE:
2806 case WLAN_SP_MESH_PEERING_CONFIRM:
2807 if (!ieee80211_vif_is_mesh(&sdata->vif))
2808 goto invalid;
a6dad6a2 2809 if (sdata->u.mesh.user_mpm)
8db09850
TP
2810 /* userspace handles this frame */
2811 break;
2812 goto queue;
2813 case WLAN_SP_MGK_INFORM:
2814 case WLAN_SP_MGK_ACK:
2815 if (!ieee80211_vif_is_mesh(&sdata->vif))
2816 goto invalid;
2817 break;
2818 }
2819 break;
d3aaec8a 2820 case WLAN_CATEGORY_MESH_ACTION:
9b395bc3
JB
2821 if (len < (IEEE80211_MIN_ACTION_SIZE +
2822 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2823 break;
2824
77a121c3
JB
2825 if (!ieee80211_vif_is_mesh(&sdata->vif))
2826 break;
25d49e4d 2827 if (mesh_action_is_path_sel(mgmt) &&
1df332e8 2828 !mesh_path_sel_is_hwmp(sdata))
c7108a71
JC
2829 break;
2830 goto queue;
84040805 2831 }
026331c4 2832
2e161f78
JB
2833 return RX_CONTINUE;
2834
bed7ee6e 2835 invalid:
554891e6 2836 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2e161f78
JB
2837 /* will return in the next handlers */
2838 return RX_CONTINUE;
2839
2840 handled:
2841 if (rx->sta)
2842 rx->sta->rx_packets++;
2843 dev_kfree_skb(rx->skb);
2844 return RX_QUEUED;
2845
2846 queue:
2847 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2848 skb_queue_tail(&sdata->skb_queue, rx->skb);
2849 ieee80211_queue_work(&local->hw, &sdata->work);
2850 if (rx->sta)
2851 rx->sta->rx_packets++;
2852 return RX_QUEUED;
2853}
2854
2855static ieee80211_rx_result debug_noinline
2856ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2857{
554891e6 2858 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
804483e9 2859 int sig = 0;
2e161f78
JB
2860
2861 /* skip known-bad action frames and return them in the next handler */
554891e6 2862 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2e161f78 2863 return RX_CONTINUE;
d7907448 2864
026331c4
JM
2865 /*
2866 * Getting here means the kernel doesn't know how to handle
2867 * it, but maybe userspace does ... include returned frames
2868 * so userspace can register for those to know whether ones
2869 * it transmitted were processed or returned.
2870 */
026331c4 2871
30686bf7 2872 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
804483e9
JB
2873 sig = status->signal;
2874
71bbc994 2875 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
970fdfa8 2876 rx->skb->data, rx->skb->len, 0)) {
2e161f78
JB
2877 if (rx->sta)
2878 rx->sta->rx_packets++;
2879 dev_kfree_skb(rx->skb);
2880 return RX_QUEUED;
2881 }
2882
2e161f78
JB
2883 return RX_CONTINUE;
2884}
2885
2886static ieee80211_rx_result debug_noinline
2887ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2888{
2889 struct ieee80211_local *local = rx->local;
2890 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2891 struct sk_buff *nskb;
2892 struct ieee80211_sub_if_data *sdata = rx->sdata;
554891e6 2893 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2894
2895 if (!ieee80211_is_action(mgmt->frame_control))
2896 return RX_CONTINUE;
2897
2898 /*
2899 * For AP mode, hostapd is responsible for handling any action
2900 * frames that we didn't handle, including returning unknown
2901 * ones. For all other modes we will return them to the sender,
2902 * setting the 0x80 bit in the action category, as required by
4b5ebccc 2903 * 802.11-2012 9.24.4.
2e161f78
JB
2904 * Newer versions of hostapd shall also use the management frame
2905 * registration mechanisms, but older ones still use cooked
2906 * monitor interfaces so push all frames there.
2907 */
554891e6 2908 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2e161f78
JB
2909 (sdata->vif.type == NL80211_IFTYPE_AP ||
2910 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2911 return RX_DROP_MONITOR;
026331c4 2912
4b5ebccc
JB
2913 if (is_multicast_ether_addr(mgmt->da))
2914 return RX_DROP_MONITOR;
2915
84040805
JB
2916 /* do not return rejected action frames */
2917 if (mgmt->u.action.category & 0x80)
2918 return RX_DROP_UNUSABLE;
2919
2920 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2921 GFP_ATOMIC);
2922 if (nskb) {
292b4df6 2923 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
84040805 2924
292b4df6
JL
2925 nmgmt->u.action.category |= 0x80;
2926 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2927 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
84040805
JB
2928
2929 memset(nskb->cb, 0, sizeof(nskb->cb));
2930
07e5a5f5
JB
2931 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
2932 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
2933
2934 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
2935 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
2936 IEEE80211_TX_CTL_NO_CCK_RATE;
30686bf7 2937 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
07e5a5f5
JB
2938 info->hw_queue =
2939 local->hw.offchannel_tx_hw_queue;
2940 }
2941
2942 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
2943 status->band);
de1ede7a 2944 }
39192c0b
JB
2945 dev_kfree_skb(rx->skb);
2946 return RX_QUEUED;
de1ede7a
JB
2947}
2948
49461622 2949static ieee80211_rx_result debug_noinline
5cf121c3 2950ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67 2951{
eb9fb5b8 2952 struct ieee80211_sub_if_data *sdata = rx->sdata;
77a121c3
JB
2953 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2954 __le16 stype;
571ecf67 2955
77a121c3 2956 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
472dbc45 2957
77a121c3
JB
2958 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2959 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
239281f8 2960 sdata->vif.type != NL80211_IFTYPE_OCB &&
77a121c3
JB
2961 sdata->vif.type != NL80211_IFTYPE_STATION)
2962 return RX_DROP_MONITOR;
472dbc45 2963
77a121c3 2964 switch (stype) {
66e67e41 2965 case cpu_to_le16(IEEE80211_STYPE_AUTH):
77a121c3
JB
2966 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2967 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2968 /* process for all: mesh, mlme, ibss */
2969 break;
66e67e41
JB
2970 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2971 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
77a121c3
JB
2972 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2973 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2c31333a
CL
2974 if (is_multicast_ether_addr(mgmt->da) &&
2975 !is_broadcast_ether_addr(mgmt->da))
2976 return RX_DROP_MONITOR;
2977
77a121c3
JB
2978 /* process only for station */
2979 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2980 return RX_DROP_MONITOR;
2981 break;
2982 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
9fb04b50
TP
2983 /* process only for ibss and mesh */
2984 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2985 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
77a121c3
JB
2986 return RX_DROP_MONITOR;
2987 break;
2988 default:
2989 return RX_DROP_MONITOR;
2990 }
f9d540ee 2991
77a121c3 2992 /* queue up frame and kick off work to process it */
c1475ca9 2993 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
77a121c3
JB
2994 skb_queue_tail(&sdata->skb_queue, rx->skb);
2995 ieee80211_queue_work(&rx->local->hw, &sdata->work);
8b58ff83
JB
2996 if (rx->sta)
2997 rx->sta->rx_packets++;
46900298 2998
77a121c3 2999 return RX_QUEUED;
571ecf67
JB
3000}
3001
5cf121c3 3002/* TODO: use IEEE80211_RX_FRAGMENTED */
5f0b7de5
JB
3003static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3004 struct ieee80211_rate *rate)
3d30d949
MW
3005{
3006 struct ieee80211_sub_if_data *sdata;
3007 struct ieee80211_local *local = rx->local;
3d30d949
MW
3008 struct sk_buff *skb = rx->skb, *skb2;
3009 struct net_device *prev_dev = NULL;
eb9fb5b8 3010 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
293702a3 3011 int needed_headroom;
3d30d949 3012
554891e6
JB
3013 /*
3014 * If cooked monitor has been processed already, then
3015 * don't do it again. If not, set the flag.
3016 */
3017 if (rx->flags & IEEE80211_RX_CMNTR)
7c1e1831 3018 goto out_free_skb;
554891e6 3019 rx->flags |= IEEE80211_RX_CMNTR;
7c1e1831 3020
152c477a
JB
3021 /* If there are no cooked monitor interfaces, just free the SKB */
3022 if (!local->cooked_mntrs)
3023 goto out_free_skb;
3024
1f7bba79
JB
3025 /* vendor data is long removed here */
3026 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
293702a3 3027 /* room for the radiotap header based on driver features */
1f7bba79 3028 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3d30d949 3029
293702a3
JB
3030 if (skb_headroom(skb) < needed_headroom &&
3031 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3032 goto out_free_skb;
3d30d949 3033
293702a3 3034 /* prepend radiotap information */
973ef21a
FF
3035 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3036 false);
3d30d949
MW
3037
3038 skb_set_mac_header(skb, 0);
3039 skb->ip_summed = CHECKSUM_UNNECESSARY;
3040 skb->pkt_type = PACKET_OTHERHOST;
3041 skb->protocol = htons(ETH_P_802_2);
3042
3043 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
9607e6b6 3044 if (!ieee80211_sdata_running(sdata))
3d30d949
MW
3045 continue;
3046
05c914fe 3047 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3d30d949
MW
3048 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
3049 continue;
3050
3051 if (prev_dev) {
3052 skb2 = skb_clone(skb, GFP_ATOMIC);
3053 if (skb2) {
3054 skb2->dev = prev_dev;
5548a8a1 3055 netif_receive_skb(skb2);
3d30d949
MW
3056 }
3057 }
3058
3059 prev_dev = sdata->dev;
5a490510 3060 ieee80211_rx_stats(sdata->dev, skb->len);
3d30d949
MW
3061 }
3062
3063 if (prev_dev) {
3064 skb->dev = prev_dev;
5548a8a1 3065 netif_receive_skb(skb);
554891e6
JB
3066 return;
3067 }
3d30d949
MW
3068
3069 out_free_skb:
3070 dev_kfree_skb(skb);
3071}
3072
aa0c8636
CL
3073static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3074 ieee80211_rx_result res)
3075{
3076 switch (res) {
3077 case RX_DROP_MONITOR:
3078 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3079 if (rx->sta)
3080 rx->sta->rx_dropped++;
3081 /* fall through */
3082 case RX_CONTINUE: {
3083 struct ieee80211_rate *rate = NULL;
3084 struct ieee80211_supported_band *sband;
3085 struct ieee80211_rx_status *status;
3086
3087 status = IEEE80211_SKB_RXCB((rx->skb));
3088
3089 sband = rx->local->hw.wiphy->bands[status->band];
5614618e
JB
3090 if (!(status->flag & RX_FLAG_HT) &&
3091 !(status->flag & RX_FLAG_VHT))
aa0c8636
CL
3092 rate = &sband->bitrates[status->rate_idx];
3093
3094 ieee80211_rx_cooked_monitor(rx, rate);
3095 break;
3096 }
3097 case RX_DROP_UNUSABLE:
3098 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3099 if (rx->sta)
3100 rx->sta->rx_dropped++;
3101 dev_kfree_skb(rx->skb);
3102 break;
3103 case RX_QUEUED:
3104 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3105 break;
3106 }
3107}
571ecf67 3108
f9e124fb
CL
3109static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3110 struct sk_buff_head *frames)
58905290 3111{
58905290 3112 ieee80211_rx_result res = RX_DROP_MONITOR;
aa0c8636 3113 struct sk_buff *skb;
8944b79f 3114
e32f85f7
LCC
3115#define CALL_RXH(rxh) \
3116 do { \
3117 res = rxh(rx); \
3118 if (res != RX_CONTINUE) \
2569a826 3119 goto rxh_next; \
e32f85f7 3120 } while (0);
49461622 3121
45ceeee8
JB
3122 /* Lock here to avoid hitting all of the data used in the RX
3123 * path (e.g. key data, station data, ...) concurrently when
3124 * a frame is released from the reorder buffer due to timeout
3125 * from the timer, potentially concurrently with RX from the
3126 * driver.
3127 */
f9e124fb 3128 spin_lock_bh(&rx->local->rx_path_lock);
24a8fdad 3129
f9e124fb 3130 while ((skb = __skb_dequeue(frames))) {
2569a826
JB
3131 /*
3132 * all the other fields are valid across frames
3133 * that belong to an aMPDU since they are on the
3134 * same TID from the same station
3135 */
3136 rx->skb = skb;
3137
2569a826 3138 CALL_RXH(ieee80211_rx_h_check_more_data)
47086fc5 3139 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
2569a826 3140 CALL_RXH(ieee80211_rx_h_sta_process)
86c228a7 3141 CALL_RXH(ieee80211_rx_h_decrypt)
2569a826 3142 CALL_RXH(ieee80211_rx_h_defragment)
2569a826
JB
3143 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
3144 /* must be after MMIC verify so header is counted in MPDU mic */
bf94e17b 3145#ifdef CONFIG_MAC80211_MESH
aa0c8636 3146 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2569a826 3147 CALL_RXH(ieee80211_rx_h_mesh_fwding);
bf94e17b 3148#endif
2154c81c 3149 CALL_RXH(ieee80211_rx_h_amsdu)
2569a826 3150 CALL_RXH(ieee80211_rx_h_data)
f9e124fb
CL
3151
3152 /* special treatment -- needs the queue */
3153 res = ieee80211_rx_h_ctrl(rx, frames);
3154 if (res != RX_CONTINUE)
3155 goto rxh_next;
3156
2e161f78 3157 CALL_RXH(ieee80211_rx_h_mgmt_check)
2569a826 3158 CALL_RXH(ieee80211_rx_h_action)
2e161f78
JB
3159 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
3160 CALL_RXH(ieee80211_rx_h_action_return)
2569a826 3161 CALL_RXH(ieee80211_rx_h_mgmt)
49461622 3162
aa0c8636
CL
3163 rxh_next:
3164 ieee80211_rx_handlers_result(rx, res);
f9e124fb 3165
49461622 3166#undef CALL_RXH
aa0c8636 3167 }
24a8fdad 3168
f9e124fb 3169 spin_unlock_bh(&rx->local->rx_path_lock);
aa0c8636
CL
3170}
3171
4406c376 3172static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
aa0c8636 3173{
f9e124fb 3174 struct sk_buff_head reorder_release;
aa0c8636
CL
3175 ieee80211_rx_result res = RX_DROP_MONITOR;
3176
f9e124fb
CL
3177 __skb_queue_head_init(&reorder_release);
3178
aa0c8636
CL
3179#define CALL_RXH(rxh) \
3180 do { \
3181 res = rxh(rx); \
3182 if (res != RX_CONTINUE) \
3183 goto rxh_next; \
3184 } while (0);
3185
0395442a 3186 CALL_RXH(ieee80211_rx_h_check_dup)
aa0c8636
CL
3187 CALL_RXH(ieee80211_rx_h_check)
3188
f9e124fb 3189 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
aa0c8636 3190
f9e124fb 3191 ieee80211_rx_handlers(rx, &reorder_release);
aa0c8636 3192 return;
49461622 3193
2569a826 3194 rxh_next:
aa0c8636
CL
3195 ieee80211_rx_handlers_result(rx, res);
3196
3197#undef CALL_RXH
58905290
JB
3198}
3199
2bff8ebf 3200/*
dd318575
JB
3201 * This function makes calls into the RX path, therefore
3202 * it has to be invoked under RCU read lock.
2bff8ebf
CL
3203 */
3204void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3205{
f9e124fb 3206 struct sk_buff_head frames;
554891e6
JB
3207 struct ieee80211_rx_data rx = {
3208 .sta = sta,
3209 .sdata = sta->sdata,
3210 .local = sta->local,
9e26297a
JB
3211 /* This is OK -- must be QoS data frame */
3212 .security_idx = tid,
3213 .seqno_idx = tid,
22d3a3c8 3214 .flags = IEEE80211_RX_REORDER_TIMER,
554891e6 3215 };
2c15a0cf
CL
3216 struct tid_ampdu_rx *tid_agg_rx;
3217
3218 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3219 if (!tid_agg_rx)
3220 return;
2bff8ebf 3221
f9e124fb
CL
3222 __skb_queue_head_init(&frames);
3223
2c15a0cf 3224 spin_lock(&tid_agg_rx->reorder_lock);
f9e124fb 3225 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
2c15a0cf 3226 spin_unlock(&tid_agg_rx->reorder_lock);
2bff8ebf 3227
b497de63
EG
3228 if (!skb_queue_empty(&frames)) {
3229 struct ieee80211_event event = {
3230 .type = BA_FRAME_TIMEOUT,
3231 .u.ba.tid = tid,
3232 .u.ba.sta = &sta->sta,
3233 };
3234 drv_event_callback(rx.local, rx.sdata, &event);
3235 }
3236
f9e124fb 3237 ieee80211_rx_handlers(&rx, &frames);
2bff8ebf
CL
3238}
3239
571ecf67
JB
3240/* main receive path */
3241
a58fbe1a 3242static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
23a24def 3243{
20b01f80 3244 struct ieee80211_sub_if_data *sdata = rx->sdata;
eb9fb5b8 3245 struct sk_buff *skb = rx->skb;
a58fbe1a 3246 struct ieee80211_hdr *hdr = (void *)skb->data;
eb9fb5b8
JB
3247 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3248 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
23a24def
JB
3249 int multicast = is_multicast_ether_addr(hdr->addr1);
3250
51fb61e7 3251 switch (sdata->vif.type) {
05c914fe 3252 case NL80211_IFTYPE_STATION:
9bc383de 3253 if (!bssid && !sdata->u.mgd.use_4addr)
3c2723f5 3254 return false;
a58fbe1a
JB
3255 if (multicast)
3256 return true;
3257 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
05c914fe 3258 case NL80211_IFTYPE_ADHOC:
23a24def 3259 if (!bssid)
3c2723f5 3260 return false;
6329b8d9
FF
3261 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3262 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3c2723f5 3263 return false;
a58fbe1a 3264 if (ieee80211_is_beacon(hdr->frame_control))
3c2723f5 3265 return true;
a58fbe1a 3266 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3c2723f5 3267 return false;
a58fbe1a
JB
3268 if (!multicast &&
3269 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
df140465 3270 return false;
a58fbe1a 3271 if (!rx->sta) {
0fb8ca45 3272 int rate_idx;
5614618e
JB
3273 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3274 rate_idx = 0; /* TODO: HT/VHT rates */
0fb8ca45 3275 else
eb9fb5b8 3276 rate_idx = status->rate_idx;
8bf11d8d
JB
3277 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3278 BIT(rate_idx));
0fb8ca45 3279 }
a58fbe1a 3280 return true;
239281f8
RL
3281 case NL80211_IFTYPE_OCB:
3282 if (!bssid)
3283 return false;
a58fbe1a 3284 if (ieee80211_is_beacon(hdr->frame_control))
239281f8 3285 return false;
a58fbe1a 3286 if (!is_broadcast_ether_addr(bssid))
239281f8 3287 return false;
a58fbe1a
JB
3288 if (!multicast &&
3289 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
df140465 3290 return false;
a58fbe1a 3291 if (!rx->sta) {
239281f8
RL
3292 int rate_idx;
3293 if (status->flag & RX_FLAG_HT)
3294 rate_idx = 0; /* TODO: HT rates */
3295 else
3296 rate_idx = status->rate_idx;
3297 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3298 BIT(rate_idx));
3299 }
a58fbe1a 3300 return true;
05c914fe 3301 case NL80211_IFTYPE_MESH_POINT:
a58fbe1a
JB
3302 if (multicast)
3303 return true;
3304 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
05c914fe
JB
3305 case NL80211_IFTYPE_AP_VLAN:
3306 case NL80211_IFTYPE_AP:
a58fbe1a
JB
3307 if (!bssid)
3308 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3309
3310 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3df6eaea
JB
3311 /*
3312 * Accept public action frames even when the
3313 * BSSID doesn't match, this is used for P2P
3314 * and location updates. Note that mac80211
3315 * itself never looks at these frames.
3316 */
2b9ccd4e
JB
3317 if (!multicast &&
3318 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3c2723f5 3319 return false;
d48b2968 3320 if (ieee80211_is_public_action(hdr, skb->len))
3c2723f5 3321 return true;
5c90067c 3322 return ieee80211_is_beacon(hdr->frame_control);
a58fbe1a
JB
3323 }
3324
3325 if (!ieee80211_has_tods(hdr->frame_control)) {
db8e1732
AN
3326 /* ignore data frames to TDLS-peers */
3327 if (ieee80211_is_data(hdr->frame_control))
3328 return false;
3329 /* ignore action frames to TDLS-peers */
3330 if (ieee80211_is_action(hdr->frame_control) &&
3331 !ether_addr_equal(bssid, hdr->addr1))
3332 return false;
23a24def 3333 }
a58fbe1a 3334 return true;
05c914fe 3335 case NL80211_IFTYPE_WDS:
a7767f95 3336 if (bssid || !ieee80211_is_data(hdr->frame_control))
3c2723f5 3337 return false;
a58fbe1a 3338 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
f142c6b9 3339 case NL80211_IFTYPE_P2P_DEVICE:
a58fbe1a
JB
3340 return ieee80211_is_public_action(hdr, skb->len) ||
3341 ieee80211_is_probe_req(hdr->frame_control) ||
3342 ieee80211_is_probe_resp(hdr->frame_control) ||
3343 ieee80211_is_beacon(hdr->frame_control);
2ca27bcf 3344 default:
fb1c1cd6 3345 break;
23a24def
JB
3346 }
3347
a58fbe1a
JB
3348 WARN_ON_ONCE(1);
3349 return false;
23a24def
JB
3350}
3351
4406c376
JB
3352/*
3353 * This function returns whether or not the SKB
3354 * was destined for RX processing or not, which,
3355 * if consume is true, is equivalent to whether
3356 * or not the skb was consumed.
3357 */
3358static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3359 struct sk_buff *skb, bool consume)
3360{
3361 struct ieee80211_local *local = rx->local;
3362 struct ieee80211_sub_if_data *sdata = rx->sdata;
4406c376
JB
3363
3364 rx->skb = skb;
4406c376 3365
a58fbe1a 3366 if (!ieee80211_accept_frame(rx))
4406c376
JB
3367 return false;
3368
4406c376
JB
3369 if (!consume) {
3370 skb = skb_copy(skb, GFP_ATOMIC);
3371 if (!skb) {
3372 if (net_ratelimit())
3373 wiphy_debug(local->hw.wiphy,
b305dae4 3374 "failed to copy skb for %s\n",
4406c376
JB
3375 sdata->name);
3376 return true;
3377 }
3378
3379 rx->skb = skb;
3380 }
3381
3382 ieee80211_invoke_rx_handlers(rx);
3383 return true;
3384}
3385
571ecf67 3386/*
6b59db7d 3387 * This is the actual Rx frames handler. as it belongs to Rx path it must
6368e4b1 3388 * be called with rcu_read_lock protection.
571ecf67 3389 */
71ebb4aa 3390static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
071d9ac2 3391 struct sk_buff *skb)
571ecf67
JB
3392{
3393 struct ieee80211_local *local = hw_to_local(hw);
3394 struct ieee80211_sub_if_data *sdata;
571ecf67 3395 struct ieee80211_hdr *hdr;
e3cf8b3f 3396 __le16 fc;
5cf121c3 3397 struct ieee80211_rx_data rx;
4406c376 3398 struct ieee80211_sub_if_data *prev;
7bedd0cf
JB
3399 struct sta_info *sta, *prev_sta;
3400 struct rhash_head *tmp;
e3cf8b3f 3401 int err = 0;
571ecf67 3402
e3cf8b3f 3403 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
571ecf67
JB
3404 memset(&rx, 0, sizeof(rx));
3405 rx.skb = skb;
3406 rx.local = local;
72abd81b 3407
e3cf8b3f 3408 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
c206ca67 3409 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
571ecf67 3410
4a4f1a58
JB
3411 if (ieee80211_is_mgmt(fc)) {
3412 /* drop frame if too short for header */
3413 if (skb->len < ieee80211_hdrlen(fc))
3414 err = -ENOBUFS;
3415 else
3416 err = skb_linearize(skb);
3417 } else {
e3cf8b3f 3418 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4a4f1a58 3419 }
e3cf8b3f
ZY
3420
3421 if (err) {
3422 dev_kfree_skb(skb);
3423 return;
3424 }
3425
3426 hdr = (struct ieee80211_hdr *)skb->data;
38f3714d 3427 ieee80211_parse_qos(&rx);
d1c3a37c 3428 ieee80211_verify_alignment(&rx);
38f3714d 3429
d48b2968
JB
3430 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3431 ieee80211_is_beacon(hdr->frame_control)))
3432 ieee80211_scan_rx(local, skb);
3433
e3cf8b3f 3434 if (ieee80211_is_data(fc)) {
7bedd0cf
JB
3435 const struct bucket_table *tbl;
3436
56af3268 3437 prev_sta = NULL;
4406c376 3438
7bedd0cf
JB
3439 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
3440
3441 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
56af3268
BG
3442 if (!prev_sta) {
3443 prev_sta = sta;
3444 continue;
3445 }
3446
3447 rx.sta = prev_sta;
3448 rx.sdata = prev_sta->sdata;
4406c376 3449 ieee80211_prepare_and_rx_handle(&rx, skb, false);
abe60632 3450
56af3268 3451 prev_sta = sta;
4406c376 3452 }
56af3268
BG
3453
3454 if (prev_sta) {
3455 rx.sta = prev_sta;
3456 rx.sdata = prev_sta->sdata;
3457
4406c376 3458 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
56af3268 3459 return;
8e26d5ad 3460 goto out;
56af3268 3461 }
4406c376
JB
3462 }
3463
4b0dd98e 3464 prev = NULL;
b2e7771e 3465
4b0dd98e
JB
3466 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3467 if (!ieee80211_sdata_running(sdata))
3468 continue;
340e11f3 3469
4b0dd98e
JB
3470 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
3471 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3472 continue;
340e11f3 3473
4b0dd98e
JB
3474 /*
3475 * frame is destined for this interface, but if it's
3476 * not also for the previous one we handle that after
3477 * the loop to avoid copying the SKB once too much
3478 */
4bb29f8c 3479
4b0dd98e 3480 if (!prev) {
abe60632 3481 prev = sdata;
4b0dd98e 3482 continue;
340e11f3 3483 }
4bb29f8c 3484
7852e361 3485 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e
JB
3486 rx.sdata = prev;
3487 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4bb29f8c 3488
4b0dd98e
JB
3489 prev = sdata;
3490 }
3491
3492 if (prev) {
7852e361 3493 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e 3494 rx.sdata = prev;
4406c376 3495
4b0dd98e
JB
3496 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
3497 return;
571ecf67 3498 }
4406c376 3499
8e26d5ad 3500 out:
4406c376 3501 dev_kfree_skb(skb);
571ecf67 3502}
6368e4b1
RR
3503
3504/*
3505 * This is the receive path handler. It is called by a low level driver when an
3506 * 802.11 MPDU is received from the hardware.
3507 */
103bf9f7 3508void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
6368e4b1
RR
3509{
3510 struct ieee80211_local *local = hw_to_local(hw);
8318d78a
JB
3511 struct ieee80211_rate *rate = NULL;
3512 struct ieee80211_supported_band *sband;
f1d58c25 3513 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
8318d78a 3514
d20ef63d
JB
3515 WARN_ON_ONCE(softirq_count() == 0);
3516
444e3803 3517 if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
77a980dc 3518 goto drop;
8318d78a
JB
3519
3520 sband = local->hw.wiphy->bands[status->band];
77a980dc
JB
3521 if (WARN_ON(!sband))
3522 goto drop;
8318d78a 3523
89c3a8ac
JB
3524 /*
3525 * If we're suspending, it is possible although not too likely
3526 * that we'd be receiving frames after having already partially
3527 * quiesced the stack. We can't process such frames then since
3528 * that might, for example, cause stations to be added or other
3529 * driver callbacks be invoked.
3530 */
77a980dc
JB
3531 if (unlikely(local->quiescing || local->suspended))
3532 goto drop;
89c3a8ac 3533
04800ada
AN
3534 /* We might be during a HW reconfig, prevent Rx for the same reason */
3535 if (unlikely(local->in_reconfig))
3536 goto drop;
3537
ea77f12f
JB
3538 /*
3539 * The same happens when we're not even started,
3540 * but that's worth a warning.
3541 */
77a980dc
JB
3542 if (WARN_ON(!local->started))
3543 goto drop;
ea77f12f 3544
fc885189 3545 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
e5d6eb83 3546 /*
fc885189
JB
3547 * Validate the rate, unless a PLCP error means that
3548 * we probably can't have a valid rate here anyway.
e5d6eb83 3549 */
fc885189
JB
3550
3551 if (status->flag & RX_FLAG_HT) {
3552 /*
3553 * rate_idx is MCS index, which can be [0-76]
3554 * as documented on:
3555 *
3556 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
3557 *
3558 * Anything else would be some sort of driver or
3559 * hardware error. The driver should catch hardware
3560 * errors.
3561 */
444e3803 3562 if (WARN(status->rate_idx > 76,
fc885189
JB
3563 "Rate marked as an HT rate but passed "
3564 "status->rate_idx is not "
3565 "an MCS index [0-76]: %d (0x%02x)\n",
3566 status->rate_idx,
3567 status->rate_idx))
3568 goto drop;
5614618e
JB
3569 } else if (status->flag & RX_FLAG_VHT) {
3570 if (WARN_ONCE(status->rate_idx > 9 ||
3571 !status->vht_nss ||
3572 status->vht_nss > 8,
3573 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
3574 status->rate_idx, status->vht_nss))
3575 goto drop;
fc885189 3576 } else {
444e3803 3577 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
fc885189
JB
3578 goto drop;
3579 rate = &sband->bitrates[status->rate_idx];
3580 }
0fb8ca45 3581 }
6368e4b1 3582
554891e6
JB
3583 status->rx_flags = 0;
3584
6368e4b1
RR
3585 /*
3586 * key references and virtual interfaces are protected using RCU
3587 * and this requires that we are in a read-side RCU section during
3588 * receive processing
3589 */
3590 rcu_read_lock();
3591
3592 /*
3593 * Frames with failed FCS/PLCP checksum are not returned,
3594 * all other frames are returned without radiotap header
3595 * if it was previously present.
3596 * Also, frames with less than 16 bytes are dropped.
3597 */
f1d58c25 3598 skb = ieee80211_rx_monitor(local, skb, rate);
6368e4b1
RR
3599 if (!skb) {
3600 rcu_read_unlock();
3601 return;
3602 }
3603
e1e54068
JB
3604 ieee80211_tpt_led_trig_rx(local,
3605 ((struct ieee80211_hdr *)skb->data)->frame_control,
3606 skb->len);
071d9ac2 3607 __ieee80211_rx_handle_packet(hw, skb);
6368e4b1
RR
3608
3609 rcu_read_unlock();
77a980dc
JB
3610
3611 return;
3612 drop:
3613 kfree_skb(skb);
6368e4b1 3614}
103bf9f7 3615EXPORT_SYMBOL(ieee80211_rx);
571ecf67
JB
3616
3617/* This is a version of the rx handler that can be called from hard irq
3618 * context. Post the skb on the queue and schedule the tasklet */
f1d58c25 3619void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
571ecf67
JB
3620{
3621 struct ieee80211_local *local = hw_to_local(hw);
3622
3623 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
3624
571ecf67
JB
3625 skb->pkt_type = IEEE80211_RX_MSG;
3626 skb_queue_tail(&local->skb_queue, skb);
3627 tasklet_schedule(&local->tasklet);
3628}
3629EXPORT_SYMBOL(ieee80211_rx_irqsafe);