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