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