]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/mac80211/rx.c
mac80211: move aMPDU RX reorder code
[mirror_ubuntu-zesty-kernel.git] / net / mac80211 / rx.c
CommitLineData
571ecf67
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
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>
571ecf67
JB
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
d4e46a3d 17#include <linux/rcupdate.h>
571ecf67
JB
18#include <net/mac80211.h>
19#include <net/ieee80211_radiotap.h>
20
21#include "ieee80211_i.h"
24487981 22#include "driver-ops.h"
2c8dccc7 23#include "led.h"
33b64eb2 24#include "mesh.h"
571ecf67
JB
25#include "wep.h"
26#include "wpa.h"
27#include "tkip.h"
28#include "wme.h"
29
1edfb1af
JB
30static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
31 struct sk_buff *skb,
32 struct ieee80211_rate *rate);
a02ae758 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))
45 skb_trim(skb, skb->len - FCS_LEN);
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
JB
62
63 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
64 return 1;
0869aea0 65 if (unlikely(skb->len < 16 + present_fcs_len))
b2e7771e 66 return 1;
87228f57
HH
67 if (ieee80211_is_ctl(hdr->frame_control) &&
68 !ieee80211_is_pspoll(hdr->frame_control) &&
69 !ieee80211_is_back_req(hdr->frame_control))
b2e7771e
JB
70 return 1;
71 return 0;
72}
73
601ae7f2
BR
74static int
75ieee80211_rx_radiotap_len(struct ieee80211_local *local,
76 struct ieee80211_rx_status *status)
77{
78 int len;
79
80 /* always present fields */
81 len = sizeof(struct ieee80211_radiotap_header) + 9;
82
83 if (status->flag & RX_FLAG_TSFT)
84 len += 8;
7fee5372 85 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
601ae7f2
BR
86 len += 1;
87 if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
88 len += 1;
89
90 if (len & 1) /* padding for RX_FLAGS if necessary */
91 len++;
92
601ae7f2
BR
93 return len;
94}
95
d1c3a37c 96/*
601ae7f2
BR
97 * ieee80211_add_rx_radiotap_header - add radiotap header
98 *
99 * add a radiotap header containing all the fields which the hardware provided.
100 */
101static void
102ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
103 struct sk_buff *skb,
601ae7f2
BR
104 struct ieee80211_rate *rate,
105 int rtap_len)
106{
f1d58c25 107 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
601ae7f2
BR
108 struct ieee80211_radiotap_header *rthdr;
109 unsigned char *pos;
6a86b9c7 110 u16 rx_flags = 0;
601ae7f2
BR
111
112 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
113 memset(rthdr, 0, rtap_len);
114
115 /* radiotap header, set always present flags */
116 rthdr->it_present =
117 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
601ae7f2
BR
118 (1 << IEEE80211_RADIOTAP_CHANNEL) |
119 (1 << IEEE80211_RADIOTAP_ANTENNA) |
120 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
121 rthdr->it_len = cpu_to_le16(rtap_len);
122
123 pos = (unsigned char *)(rthdr+1);
124
125 /* the order of the following fields is important */
126
127 /* IEEE80211_RADIOTAP_TSFT */
128 if (status->flag & RX_FLAG_TSFT) {
6a86b9c7 129 put_unaligned_le64(status->mactime, pos);
601ae7f2
BR
130 rthdr->it_present |=
131 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
132 pos += 8;
133 }
134
135 /* IEEE80211_RADIOTAP_FLAGS */
136 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
137 *pos |= IEEE80211_RADIOTAP_F_FCS;
aae89831
JB
138 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
139 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
b4f28bbb
BR
140 if (status->flag & RX_FLAG_SHORTPRE)
141 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
601ae7f2
BR
142 pos++;
143
144 /* IEEE80211_RADIOTAP_RATE */
0fb8ca45
JM
145 if (status->flag & RX_FLAG_HT) {
146 /*
147 * TODO: add following information into radiotap header once
148 * suitable fields are defined for it:
149 * - MCS index (status->rate_idx)
150 * - HT40 (status->flag & RX_FLAG_40MHZ)
151 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
152 */
153 *pos = 0;
8d6f658e 154 } else {
ebe6c7ba 155 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
0fb8ca45 156 *pos = rate->bitrate / 5;
8d6f658e 157 }
601ae7f2
BR
158 pos++;
159
160 /* IEEE80211_RADIOTAP_CHANNEL */
6a86b9c7 161 put_unaligned_le16(status->freq, pos);
601ae7f2
BR
162 pos += 2;
163 if (status->band == IEEE80211_BAND_5GHZ)
6a86b9c7
JB
164 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
165 pos);
5f0b7de5
JB
166 else if (status->flag & RX_FLAG_HT)
167 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
168 pos);
9deb1ae5 169 else if (rate->flags & IEEE80211_RATE_ERP_G)
6a86b9c7
JB
170 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
171 pos);
601ae7f2 172 else
6a86b9c7
JB
173 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
174 pos);
601ae7f2
BR
175 pos += 2;
176
177 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
178 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
179 *pos = status->signal;
180 rthdr->it_present |=
181 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
182 pos++;
183 }
184
185 /* IEEE80211_RADIOTAP_DBM_ANTNOISE */
186 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
187 *pos = status->noise;
188 rthdr->it_present |=
189 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTNOISE);
190 pos++;
191 }
192
193 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
194
195 /* IEEE80211_RADIOTAP_ANTENNA */
196 *pos = status->antenna;
197 pos++;
198
601ae7f2
BR
199 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
200
201 /* IEEE80211_RADIOTAP_RX_FLAGS */
202 /* ensure 2 byte alignment for the 2 byte field as required */
6a86b9c7 203 if ((pos - (u8 *)rthdr) & 1)
601ae7f2 204 pos++;
aae89831 205 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
6a86b9c7
JB
206 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
207 put_unaligned_le16(rx_flags, pos);
601ae7f2
BR
208 pos += 2;
209}
210
b2e7771e
JB
211/*
212 * This function copies a received frame to all monitor interfaces and
213 * returns a cleaned-up SKB that no longer includes the FCS nor the
214 * radiotap header the driver might have added.
215 */
216static struct sk_buff *
217ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a 218 struct ieee80211_rate *rate)
b2e7771e 219{
f1d58c25 220 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
b2e7771e 221 struct ieee80211_sub_if_data *sdata;
b2e7771e 222 int needed_headroom = 0;
b2e7771e
JB
223 struct sk_buff *skb, *skb2;
224 struct net_device *prev_dev = NULL;
225 int present_fcs_len = 0;
b2e7771e
JB
226
227 /*
228 * First, we may need to make a copy of the skb because
229 * (1) we need to modify it for radiotap (if not present), and
230 * (2) the other RX handlers will modify the skb we got.
231 *
232 * We don't need to, of course, if we aren't going to return
233 * the SKB because it has a bad FCS/PLCP checksum.
234 */
0869aea0
JB
235
236 /* room for the radiotap header based on driver features */
237 needed_headroom = ieee80211_rx_radiotap_len(local, status);
b2e7771e
JB
238
239 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
240 present_fcs_len = FCS_LEN;
241
242 if (!local->monitors) {
0869aea0 243 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
244 dev_kfree_skb(origskb);
245 return NULL;
246 }
247
0869aea0 248 return remove_monitor_info(local, origskb);
b2e7771e
JB
249 }
250
0869aea0 251 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
252 /* only need to expand headroom if necessary */
253 skb = origskb;
254 origskb = NULL;
255
256 /*
257 * This shouldn't trigger often because most devices have an
258 * RX header they pull before we get here, and that should
259 * be big enough for our radiotap information. We should
260 * probably export the length to drivers so that we can have
261 * them allocate enough headroom to start with.
262 */
263 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 264 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
265 dev_kfree_skb(skb);
266 return NULL;
267 }
268 } else {
269 /*
270 * Need to make a copy and possibly remove radiotap header
271 * and FCS from the original.
272 */
273 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
274
0869aea0 275 origskb = remove_monitor_info(local, origskb);
b2e7771e
JB
276
277 if (!skb)
278 return origskb;
279 }
280
0869aea0
JB
281 /* prepend radiotap information */
282 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
b2e7771e 283
ce3edf6d 284 skb_reset_mac_header(skb);
b2e7771e
JB
285 skb->ip_summed = CHECKSUM_UNNECESSARY;
286 skb->pkt_type = PACKET_OTHERHOST;
287 skb->protocol = htons(ETH_P_802_2);
288
289 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
290 if (!netif_running(sdata->dev))
291 continue;
292
05c914fe 293 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
b2e7771e
JB
294 continue;
295
3d30d949
MW
296 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
297 continue;
298
b2e7771e
JB
299 if (prev_dev) {
300 skb2 = skb_clone(skb, GFP_ATOMIC);
301 if (skb2) {
302 skb2->dev = prev_dev;
303 netif_rx(skb2);
304 }
305 }
306
307 prev_dev = sdata->dev;
308 sdata->dev->stats.rx_packets++;
309 sdata->dev->stats.rx_bytes += skb->len;
310 }
311
312 if (prev_dev) {
313 skb->dev = prev_dev;
314 netif_rx(skb);
315 } else
316 dev_kfree_skb(skb);
317
318 return origskb;
319}
320
321
5cf121c3 322static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d 323{
238f74a2 324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
6e0d114d
JB
325 int tid;
326
327 /* does the frame have a qos control field? */
238f74a2
HH
328 if (ieee80211_is_data_qos(hdr->frame_control)) {
329 u8 *qc = ieee80211_get_qos_ctl(hdr);
6e0d114d 330 /* frame has qos control */
238f74a2
HH
331 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
332 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
5cf121c3 333 rx->flags |= IEEE80211_RX_AMSDU;
fd4c7f2f 334 else
5cf121c3 335 rx->flags &= ~IEEE80211_RX_AMSDU;
6e0d114d 336 } else {
1411f9b5
JB
337 /*
338 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
339 *
340 * Sequence numbers for management frames, QoS data
341 * frames with a broadcast/multicast address in the
342 * Address 1 field, and all non-QoS data frames sent
343 * by QoS STAs are assigned using an additional single
344 * modulo-4096 counter, [...]
345 *
346 * We also use that counter for non-QoS STAs.
347 */
348 tid = NUM_RX_DATA_QUEUES - 1;
6e0d114d 349 }
52865dfd 350
5cf121c3 351 rx->queue = tid;
6e0d114d
JB
352 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
353 * For now, set skb->priority to 0 for other cases. */
354 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 355}
6e0d114d 356
d1c3a37c
JB
357/**
358 * DOC: Packet alignment
359 *
360 * Drivers always need to pass packets that are aligned to two-byte boundaries
361 * to the stack.
362 *
363 * Additionally, should, if possible, align the payload data in a way that
364 * guarantees that the contained IP header is aligned to a four-byte
365 * boundary. In the case of regular frames, this simply means aligning the
366 * payload to a four-byte boundary (because either the IP header is directly
367 * contained, or IV/RFC1042 headers that have a length divisible by four are
368 * in front of it).
369 *
370 * With A-MSDU frames, however, the payload data address must yield two modulo
371 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
372 * push the IP header further back to a multiple of four again. Thankfully, the
373 * specs were sane enough this time around to require padding each A-MSDU
374 * subframe to a length that is a multiple of four.
375 *
376 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
377 * the payload is not supported, the driver is required to move the 802.11
378 * header to be directly in front of the payload in that case.
379 */
380static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
38f3714d 381{
a7767f95 382 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
38f3714d
JB
383 int hdrlen;
384
d1c3a37c
JB
385#ifndef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
386 return;
387#endif
388
389 if (WARN_ONCE((unsigned long)rx->skb->data & 1,
390 "unaligned packet at 0x%p\n", rx->skb->data))
391 return;
392
a7767f95 393 if (!ieee80211_is_data_present(hdr->frame_control))
38f3714d
JB
394 return;
395
a7767f95 396 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5cf121c3 397 if (rx->flags & IEEE80211_RX_AMSDU)
38f3714d 398 hdrlen += ETH_HLEN;
d1c3a37c
JB
399 WARN_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3,
400 "unaligned IP payload at 0x%p\n", rx->skb->data + hdrlen);
6e0d114d
JB
401}
402
6368e4b1 403
571ecf67
JB
404/* rx handlers */
405
49461622 406static ieee80211_rx_result debug_noinline
5cf121c3 407ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
571ecf67
JB
408{
409 struct ieee80211_local *local = rx->local;
410 struct sk_buff *skb = rx->skb;
411
fbe9c429 412 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
f1d58c25 413 return ieee80211_scan_rx(rx->sdata, skb);
ece8eddd 414
142b9f50
HS
415 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
416 (rx->flags & IEEE80211_RX_IN_SCAN))) {
ece8eddd 417 /* drop all the other packets during a software scan anyway */
f1d58c25 418 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
ece8eddd 419 dev_kfree_skb(skb);
9ae54c84 420 return RX_QUEUED;
571ecf67
JB
421 }
422
5cf121c3 423 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
571ecf67
JB
424 /* scanning finished during invoking of handlers */
425 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
e4c26add 426 return RX_DROP_UNUSABLE;
571ecf67
JB
427 }
428
9ae54c84 429 return RX_CONTINUE;
571ecf67
JB
430}
431
3cfcf6ac
JM
432
433static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
434{
435 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
436
437 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
438 return 0;
439
440 return ieee80211_is_robust_mgmt_frame(hdr);
441}
442
443
444static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
445{
446 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
447
448 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
449 return 0;
450
451 return ieee80211_is_robust_mgmt_frame(hdr);
452}
453
454
455/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
456static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
457{
458 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
459 struct ieee80211_mmie *mmie;
460
461 if (skb->len < 24 + sizeof(*mmie) ||
462 !is_multicast_ether_addr(hdr->da))
463 return -1;
464
465 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
466 return -1; /* not a robust management frame */
467
468 mmie = (struct ieee80211_mmie *)
469 (skb->data + skb->len - sizeof(*mmie));
470 if (mmie->element_id != WLAN_EID_MMIE ||
471 mmie->length != sizeof(*mmie) - 2)
472 return -1;
473
474 return le16_to_cpu(mmie->key_id);
475}
476
477
33b64eb2 478static ieee80211_rx_result
5cf121c3 479ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2 480{
a7767f95
HH
481 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
482 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
eb9fb5b8 483 char *dev_addr = rx->sdata->dev->dev_addr;
d6d1a5a7 484
a7767f95 485 if (ieee80211_is_data(hdr->frame_control)) {
3c5772a5
JC
486 if (is_multicast_ether_addr(hdr->addr1)) {
487 if (ieee80211_has_tods(hdr->frame_control) ||
488 !ieee80211_has_fromds(hdr->frame_control))
489 return RX_DROP_MONITOR;
490 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
491 return RX_DROP_MONITOR;
492 } else {
493 if (!ieee80211_has_a4(hdr->frame_control))
494 return RX_DROP_MONITOR;
495 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
496 return RX_DROP_MONITOR;
497 }
33b64eb2
LCC
498 }
499
500 /* If there is not an established peer link and this is not a peer link
501 * establisment frame, beacon or probe, drop the frame.
502 */
503
b4e08ea1 504 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
33b64eb2 505 struct ieee80211_mgmt *mgmt;
d6d1a5a7 506
a7767f95 507 if (!ieee80211_is_mgmt(hdr->frame_control))
33b64eb2
LCC
508 return RX_DROP_MONITOR;
509
a7767f95 510 if (ieee80211_is_action(hdr->frame_control)) {
33b64eb2 511 mgmt = (struct ieee80211_mgmt *)hdr;
0938393f 512 if (mgmt->u.action.category != MESH_PLINK_CATEGORY)
33b64eb2 513 return RX_DROP_MONITOR;
33b64eb2 514 return RX_CONTINUE;
33b64eb2
LCC
515 }
516
a7767f95
HH
517 if (ieee80211_is_probe_req(hdr->frame_control) ||
518 ieee80211_is_probe_resp(hdr->frame_control) ||
519 ieee80211_is_beacon(hdr->frame_control))
520 return RX_CONTINUE;
521
522 return RX_DROP_MONITOR;
523
524 }
525
526#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
527
528 if (ieee80211_is_data(hdr->frame_control) &&
529 is_multicast_ether_addr(hdr->addr1) &&
3c5772a5 530 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
33b64eb2 531 return RX_DROP_MONITOR;
902acc78 532#undef msh_h_get
d6d1a5a7 533
902acc78
JB
534 return RX_CONTINUE;
535}
33b64eb2 536
1edfb1af
JB
537#define SEQ_MODULO 0x1000
538#define SEQ_MASK 0xfff
539
540static inline int seq_less(u16 sq1, u16 sq2)
541{
542 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
543}
544
545static inline u16 seq_inc(u16 sq)
546{
547 return (sq + 1) & SEQ_MASK;
548}
549
550static inline u16 seq_sub(u16 sq1, u16 sq2)
551{
552 return (sq1 - sq2) & SEQ_MASK;
553}
554
555
556static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
557 struct tid_ampdu_rx *tid_agg_rx,
558 int index)
559{
560 struct ieee80211_supported_band *sband;
561 struct ieee80211_rate *rate = NULL;
562 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
563 struct ieee80211_rx_status *status;
564
565 if (!skb)
566 goto no_frame;
567
568 status = IEEE80211_SKB_RXCB(skb);
569
570 /* release the reordered frames to stack */
571 sband = hw->wiphy->bands[status->band];
572 if (!(status->flag & RX_FLAG_HT))
573 rate = &sband->bitrates[status->rate_idx];
574 __ieee80211_rx_handle_packet(hw, skb, rate);
575 tid_agg_rx->stored_mpdu_num--;
576 tid_agg_rx->reorder_buf[index] = NULL;
577
578no_frame:
579 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
580}
581
582static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
583 struct tid_ampdu_rx *tid_agg_rx,
584 u16 head_seq_num)
585{
586 int index;
587
588 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
589 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
590 tid_agg_rx->buf_size;
591 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
592 }
593}
594
595/*
596 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
597 * the skb was added to the buffer longer than this time ago, the earlier
598 * frames that have not yet been received are assumed to be lost and the skb
599 * can be released for processing. This may also release other skb's from the
600 * reorder buffer if there are no additional gaps between the frames.
601 */
602#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
603
604/*
605 * As this function belongs to the RX path it must be under
606 * rcu_read_lock protection. It returns false if the frame
607 * can be processed immediately, true if it was consumed.
608 */
609static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
610 struct tid_ampdu_rx *tid_agg_rx,
611 struct sk_buff *skb)
612{
613 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
614 u16 sc = le16_to_cpu(hdr->seq_ctrl);
615 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
616 u16 head_seq_num, buf_size;
617 int index;
618
619 buf_size = tid_agg_rx->buf_size;
620 head_seq_num = tid_agg_rx->head_seq_num;
621
622 /* frame with out of date sequence number */
623 if (seq_less(mpdu_seq_num, head_seq_num)) {
624 dev_kfree_skb(skb);
625 return true;
626 }
627
628 /*
629 * If frame the sequence number exceeds our buffering window
630 * size release some previous frames to make room for this one.
631 */
632 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
633 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
634 /* release stored frames up to new head to stack */
635 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
636 }
637
638 /* Now the new frame is always in the range of the reordering buffer */
639
640 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
641
642 /* check if we already stored this frame */
643 if (tid_agg_rx->reorder_buf[index]) {
644 dev_kfree_skb(skb);
645 return true;
646 }
647
648 /*
649 * If the current MPDU is in the right order and nothing else
650 * is stored we can process it directly, no need to buffer it.
651 */
652 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
653 tid_agg_rx->stored_mpdu_num == 0) {
654 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
655 return false;
656 }
657
658 /* put the frame in the reordering buffer */
659 tid_agg_rx->reorder_buf[index] = skb;
660 tid_agg_rx->reorder_time[index] = jiffies;
661 tid_agg_rx->stored_mpdu_num++;
662 /* release the buffer until next missing frame */
663 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
664 tid_agg_rx->buf_size;
665 if (!tid_agg_rx->reorder_buf[index] &&
666 tid_agg_rx->stored_mpdu_num > 1) {
667 /*
668 * No buffers ready to be released, but check whether any
669 * frames in the reorder buffer have timed out.
670 */
671 int j;
672 int skipped = 1;
673 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
674 j = (j + 1) % tid_agg_rx->buf_size) {
675 if (!tid_agg_rx->reorder_buf[j]) {
676 skipped++;
677 continue;
678 }
679 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
680 HT_RX_REORDER_BUF_TIMEOUT))
681 break;
682
683#ifdef CONFIG_MAC80211_HT_DEBUG
684 if (net_ratelimit())
685 printk(KERN_DEBUG "%s: release an RX reorder "
686 "frame due to timeout on earlier "
687 "frames\n",
688 wiphy_name(hw->wiphy));
689#endif
690 ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
691
692 /*
693 * Increment the head seq# also for the skipped slots.
694 */
695 tid_agg_rx->head_seq_num =
696 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
697 skipped = 0;
698 }
699 } else while (tid_agg_rx->reorder_buf[index]) {
700 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
701 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
702 tid_agg_rx->buf_size;
703 }
704
705 return true;
706}
707
708/*
709 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
710 * true if the MPDU was buffered, false if it should be processed.
711 */
712static bool ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
713 struct sk_buff *skb)
714{
715 struct ieee80211_hw *hw = &local->hw;
716 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
717 struct sta_info *sta;
718 struct tid_ampdu_rx *tid_agg_rx;
719 u16 sc;
720 int tid;
721
722 if (!ieee80211_is_data_qos(hdr->frame_control))
723 return false;
724
725 /*
726 * filter the QoS data rx stream according to
727 * STA/TID and check if this STA/TID is on aggregation
728 */
729
730 sta = sta_info_get(local, hdr->addr2);
731 if (!sta)
732 return false;
733
734 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
735
736 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
737 return false;
738
739 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
740
741 /* qos null data frames are excluded */
742 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
743 return false;
744
745 /* new, potentially un-ordered, ampdu frame - process it */
746
747 /* reset session timer */
748 if (tid_agg_rx->timeout)
749 mod_timer(&tid_agg_rx->session_timer,
750 TU_TO_EXP_TIME(tid_agg_rx->timeout));
751
752 /* if this mpdu is fragmented - terminate rx aggregation session */
753 sc = le16_to_cpu(hdr->seq_ctrl);
754 if (sc & IEEE80211_SCTL_FRAG) {
755 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
756 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
757 dev_kfree_skb(skb);
758 return true;
759 }
760
761 return ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb);
762}
33b64eb2 763
49461622 764static ieee80211_rx_result debug_noinline
5cf121c3 765ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
571ecf67 766{
a7767f95 767 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
571ecf67
JB
768
769 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
770 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
a7767f95 771 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
5cf121c3 772 rx->sta->last_seq_ctrl[rx->queue] ==
571ecf67 773 hdr->seq_ctrl)) {
5cf121c3 774 if (rx->flags & IEEE80211_RX_RA_MATCH) {
571ecf67
JB
775 rx->local->dot11FrameDuplicateCount++;
776 rx->sta->num_duplicates++;
777 }
e4c26add 778 return RX_DROP_MONITOR;
571ecf67 779 } else
5cf121c3 780 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
571ecf67
JB
781 }
782
571ecf67
JB
783 if (unlikely(rx->skb->len < 16)) {
784 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
e4c26add 785 return RX_DROP_MONITOR;
571ecf67
JB
786 }
787
571ecf67
JB
788 /* Drop disallowed frame classes based on STA auth/assoc state;
789 * IEEE 802.11, Chap 5.5.
790 *
ccd7b362
JB
791 * mac80211 filters only based on association state, i.e. it drops
792 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
793 * deauth/disassoc frames when needed. In addition, hostapd is
794 * responsible for filtering on both auth and assoc states.
795 */
33b64eb2 796
902acc78 797 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 798 return ieee80211_rx_mesh_check(rx);
33b64eb2 799
a7767f95
HH
800 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
801 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 802 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
07346f81 803 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
a7767f95
HH
804 if ((!ieee80211_has_fromds(hdr->frame_control) &&
805 !ieee80211_has_tods(hdr->frame_control) &&
806 ieee80211_is_data(hdr->frame_control)) ||
807 !(rx->flags & IEEE80211_RX_RA_MATCH)) {
571ecf67
JB
808 /* Drop IBSS frames and frames for other hosts
809 * silently. */
e4c26add 810 return RX_DROP_MONITOR;
571ecf67
JB
811 }
812
e4c26add 813 return RX_DROP_MONITOR;
571ecf67
JB
814 }
815
9ae54c84 816 return RX_CONTINUE;
570bd537
JB
817}
818
819
49461622 820static ieee80211_rx_result debug_noinline
5cf121c3 821ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
570bd537 822{
eb9fb5b8
JB
823 struct sk_buff *skb = rx->skb;
824 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
825 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3017b80b
JB
826 int keyidx;
827 int hdrlen;
e4c26add 828 ieee80211_rx_result result = RX_DROP_UNUSABLE;
d4e46a3d 829 struct ieee80211_key *stakey = NULL;
3cfcf6ac 830 int mmie_keyidx = -1;
570bd537 831
3017b80b
JB
832 /*
833 * Key selection 101
834 *
3cfcf6ac 835 * There are four types of keys:
3017b80b 836 * - GTK (group keys)
3cfcf6ac 837 * - IGTK (group keys for management frames)
3017b80b
JB
838 * - PTK (pairwise keys)
839 * - STK (station-to-station pairwise keys)
840 *
841 * When selecting a key, we have to distinguish between multicast
842 * (including broadcast) and unicast frames, the latter can only
3cfcf6ac
JM
843 * use PTKs and STKs while the former always use GTKs and IGTKs.
844 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
845 * unicast frames can also use key indices like GTKs. Hence, if we
846 * don't have a PTK/STK we check the key index for a WEP key.
3017b80b 847 *
8dc06a1c
JB
848 * Note that in a regular BSS, multicast frames are sent by the
849 * AP only, associated stations unicast the frame to the AP first
850 * which then multicasts it on their behalf.
851 *
3017b80b
JB
852 * There is also a slight problem in IBSS mode: GTKs are negotiated
853 * with each station, that is something we don't currently handle.
8dc06a1c
JB
854 * The spec seems to expect that one negotiates the same key with
855 * every station but there's no such requirement; VLANs could be
856 * possible.
3017b80b
JB
857 */
858
3017b80b 859 /*
1990af8d 860 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
861 * addressed to us nor a multicast frame.
862 */
5cf121c3 863 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
9ae54c84 864 return RX_CONTINUE;
3017b80b 865
d4e46a3d
JB
866 if (rx->sta)
867 stakey = rcu_dereference(rx->sta->key);
868
0c7c10c7
JM
869 if (!ieee80211_has_protected(hdr->frame_control))
870 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
871
d4e46a3d
JB
872 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
873 rx->key = stakey;
0c7c10c7
JM
874 /* Skip decryption if the frame is not protected. */
875 if (!ieee80211_has_protected(hdr->frame_control))
876 return RX_CONTINUE;
3cfcf6ac
JM
877 } else if (mmie_keyidx >= 0) {
878 /* Broadcast/multicast robust management frame / BIP */
eb9fb5b8
JB
879 if ((status->flag & RX_FLAG_DECRYPTED) &&
880 (status->flag & RX_FLAG_IV_STRIPPED))
3cfcf6ac
JM
881 return RX_CONTINUE;
882
883 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
884 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
885 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
886 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
0c7c10c7
JM
887 } else if (!ieee80211_has_protected(hdr->frame_control)) {
888 /*
889 * The frame was not protected, so skip decryption. However, we
890 * need to set rx->key if there is a key that could have been
891 * used so that the frame may be dropped if encryption would
892 * have been expected.
893 */
894 struct ieee80211_key *key = NULL;
895 if (ieee80211_is_mgmt(hdr->frame_control) &&
896 is_multicast_ether_addr(hdr->addr1) &&
897 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
898 rx->key = key;
899 else if ((key = rcu_dereference(rx->sdata->default_key)))
900 rx->key = key;
901 return RX_CONTINUE;
571ecf67 902 } else {
3017b80b
JB
903 /*
904 * The device doesn't give us the IV so we won't be
905 * able to look up the key. That's ok though, we
906 * don't need to decrypt the frame, we just won't
907 * be able to keep statistics accurate.
908 * Except for key threshold notifications, should
909 * we somehow allow the driver to tell us which key
910 * the hardware used if this flag is set?
911 */
eb9fb5b8
JB
912 if ((status->flag & RX_FLAG_DECRYPTED) &&
913 (status->flag & RX_FLAG_IV_STRIPPED))
9ae54c84 914 return RX_CONTINUE;
3017b80b 915
a7767f95 916 hdrlen = ieee80211_hdrlen(hdr->frame_control);
3017b80b
JB
917
918 if (rx->skb->len < 8 + hdrlen)
e4c26add 919 return RX_DROP_UNUSABLE; /* TODO: count this? */
3017b80b
JB
920
921 /*
922 * no need to call ieee80211_wep_get_keyidx,
923 * it verifies a bunch of things we've done already
924 */
925 keyidx = rx->skb->data[hdrlen + 3] >> 6;
926
d4e46a3d 927 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
3017b80b
JB
928
929 /*
930 * RSNA-protected unicast frames should always be sent with
931 * pairwise or station-to-station keys, but for WEP we allow
932 * using a key index as well.
933 */
8f20fc24 934 if (rx->key && rx->key->conf.alg != ALG_WEP &&
3017b80b
JB
935 !is_multicast_ether_addr(hdr->addr1))
936 rx->key = NULL;
571ecf67
JB
937 }
938
3017b80b 939 if (rx->key) {
571ecf67 940 rx->key->tx_rx_count++;
011bfcc4 941 /* TODO: add threshold stuff again */
1990af8d 942 } else {
e4c26add 943 return RX_DROP_MONITOR;
70f08765
JB
944 }
945
1990af8d
JB
946 /* Check for weak IVs if possible */
947 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
a7767f95 948 ieee80211_is_data(hdr->frame_control) &&
eb9fb5b8
JB
949 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
950 !(status->flag & RX_FLAG_DECRYPTED)) &&
1990af8d
JB
951 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
952 rx->sta->wep_weak_iv_count++;
953
70f08765
JB
954 switch (rx->key->conf.alg) {
955 case ALG_WEP:
e2f036da
MN
956 result = ieee80211_crypto_wep_decrypt(rx);
957 break;
70f08765 958 case ALG_TKIP:
e2f036da
MN
959 result = ieee80211_crypto_tkip_decrypt(rx);
960 break;
70f08765 961 case ALG_CCMP:
e2f036da
MN
962 result = ieee80211_crypto_ccmp_decrypt(rx);
963 break;
3cfcf6ac
JM
964 case ALG_AES_CMAC:
965 result = ieee80211_crypto_aes_cmac_decrypt(rx);
966 break;
70f08765
JB
967 }
968
e2f036da 969 /* either the frame has been decrypted or will be dropped */
eb9fb5b8 970 status->flag |= RX_FLAG_DECRYPTED;
e2f036da
MN
971
972 return result;
70f08765
JB
973}
974
572e0012
KV
975static ieee80211_rx_result debug_noinline
976ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
977{
978 struct ieee80211_local *local;
979 struct ieee80211_hdr *hdr;
980 struct sk_buff *skb;
981
982 local = rx->local;
983 skb = rx->skb;
984 hdr = (struct ieee80211_hdr *) skb->data;
985
986 if (!local->pspolling)
987 return RX_CONTINUE;
988
989 if (!ieee80211_has_fromds(hdr->frame_control))
990 /* this is not from AP */
991 return RX_CONTINUE;
992
993 if (!ieee80211_is_data(hdr->frame_control))
994 return RX_CONTINUE;
995
996 if (!ieee80211_has_moredata(hdr->frame_control)) {
997 /* AP has no more frames buffered for us */
998 local->pspolling = false;
999 return RX_CONTINUE;
1000 }
1001
1002 /* more data bit is set, let's request a new frame from the AP */
1003 ieee80211_send_pspoll(local, rx->sdata);
1004
1005 return RX_CONTINUE;
1006}
1007
133b8226 1008static void ap_sta_ps_start(struct sta_info *sta)
571ecf67 1009{
133b8226 1010 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1011 struct ieee80211_local *local = sdata->local;
0795af57 1012
3e122be0 1013 atomic_inc(&sdata->bss->num_sta_ps);
af818581 1014 set_sta_flags(sta, WLAN_STA_PS_STA);
24487981 1015 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
571ecf67 1016#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26
JB
1017 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1018 sdata->dev->name, sta->sta.addr, sta->sta.aid);
571ecf67
JB
1019#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1020}
1021
ff9458d3 1022static void ap_sta_ps_end(struct sta_info *sta)
571ecf67 1023{
133b8226 1024 struct ieee80211_sub_if_data *sdata = sta->sdata;
571ecf67 1025
3e122be0 1026 atomic_dec(&sdata->bss->num_sta_ps);
004c872e 1027
af818581 1028 clear_sta_flags(sta, WLAN_STA_PS_STA);
004c872e 1029
571ecf67 1030#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26
JB
1031 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1032 sdata->dev->name, sta->sta.addr, sta->sta.aid);
571ecf67 1033#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
004c872e 1034
af818581 1035 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
571ecf67 1036#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
af818581
JB
1037 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1038 sdata->dev->name, sta->sta.addr, sta->sta.aid);
571ecf67 1039#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
af818581
JB
1040 return;
1041 }
1042
1043 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1044}
1045
49461622 1046static ieee80211_rx_result debug_noinline
5cf121c3 1047ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1048{
1049 struct sta_info *sta = rx->sta;
eb9fb5b8
JB
1050 struct sk_buff *skb = rx->skb;
1051 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1052 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
571ecf67
JB
1053
1054 if (!sta)
9ae54c84 1055 return RX_CONTINUE;
571ecf67 1056
b291ba11
JB
1057 /*
1058 * Update last_rx only for IBSS packets which are for the current
1059 * BSSID to avoid keeping the current IBSS network alive in cases
1060 * where other STAs start using different BSSID.
1061 */
05c914fe 1062 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1063 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1064 NL80211_IFTYPE_ADHOC);
46900298 1065 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
571ecf67 1066 sta->last_rx = jiffies;
b291ba11
JB
1067 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1068 /*
33b64eb2
LCC
1069 * Mesh beacons will update last_rx when if they are found to
1070 * match the current local configuration when processed.
571ecf67 1071 */
b291ba11 1072 sta->last_rx = jiffies;
571ecf67
JB
1073 }
1074
5cf121c3 1075 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
9ae54c84 1076 return RX_CONTINUE;
571ecf67 1077
3cf335d5
KV
1078 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1079 ieee80211_sta_rx_notify(rx->sdata, hdr);
1080
571ecf67
JB
1081 sta->rx_fragments++;
1082 sta->rx_bytes += rx->skb->len;
eb9fb5b8
JB
1083 sta->last_signal = status->signal;
1084 sta->last_noise = status->noise;
571ecf67 1085
72eaa43a
JB
1086 /*
1087 * Change STA power saving mode only at the end of a frame
1088 * exchange sequence.
1089 */
3e122be0 1090 if (!ieee80211_has_morefrags(hdr->frame_control) &&
05c914fe
JB
1091 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1092 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
af818581 1093 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
72eaa43a
JB
1094 /*
1095 * Ignore doze->wake transitions that are
1096 * indicated by non-data frames, the standard
1097 * is unclear here, but for example going to
1098 * PS mode and then scanning would cause a
1099 * doze->wake transition for the probe request,
1100 * and that is clearly undesirable.
1101 */
1102 if (ieee80211_is_data(hdr->frame_control) &&
1103 !ieee80211_has_pm(hdr->frame_control))
ff9458d3 1104 ap_sta_ps_end(sta);
72eaa43a
JB
1105 } else {
1106 if (ieee80211_has_pm(hdr->frame_control))
1107 ap_sta_ps_start(sta);
1108 }
571ecf67
JB
1109 }
1110
22403def
JB
1111 /*
1112 * Drop (qos-)data::nullfunc frames silently, since they
1113 * are used only to control station power saving mode.
1114 */
1115 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1116 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
571ecf67 1117 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
22403def
JB
1118 /*
1119 * Update counter and free packet here to avoid
1120 * counting this as a dropped packed.
1121 */
571ecf67
JB
1122 sta->rx_packets++;
1123 dev_kfree_skb(rx->skb);
9ae54c84 1124 return RX_QUEUED;
571ecf67
JB
1125 }
1126
9ae54c84 1127 return RX_CONTINUE;
571ecf67
JB
1128} /* ieee80211_rx_h_sta_process */
1129
571ecf67
JB
1130static inline struct ieee80211_fragment_entry *
1131ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1132 unsigned int frag, unsigned int seq, int rx_queue,
1133 struct sk_buff **skb)
1134{
1135 struct ieee80211_fragment_entry *entry;
1136 int idx;
1137
1138 idx = sdata->fragment_next;
1139 entry = &sdata->fragments[sdata->fragment_next++];
1140 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1141 sdata->fragment_next = 0;
1142
1143 if (!skb_queue_empty(&entry->skb_list)) {
f4ea83dd 1144#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
571ecf67
JB
1145 struct ieee80211_hdr *hdr =
1146 (struct ieee80211_hdr *) entry->skb_list.next->data;
1147 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1148 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
0c68ae26 1149 "addr1=%pM addr2=%pM\n",
571ecf67
JB
1150 sdata->dev->name, idx,
1151 jiffies - entry->first_frag_time, entry->seq,
0c68ae26 1152 entry->last_frag, hdr->addr1, hdr->addr2);
f4ea83dd 1153#endif
571ecf67
JB
1154 __skb_queue_purge(&entry->skb_list);
1155 }
1156
1157 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1158 *skb = NULL;
1159 entry->first_frag_time = jiffies;
1160 entry->seq = seq;
1161 entry->rx_queue = rx_queue;
1162 entry->last_frag = frag;
1163 entry->ccmp = 0;
1164 entry->extra_len = 0;
1165
1166 return entry;
1167}
1168
1169static inline struct ieee80211_fragment_entry *
1170ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
b73d70ad 1171 unsigned int frag, unsigned int seq,
571ecf67
JB
1172 int rx_queue, struct ieee80211_hdr *hdr)
1173{
1174 struct ieee80211_fragment_entry *entry;
1175 int i, idx;
1176
1177 idx = sdata->fragment_next;
1178 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1179 struct ieee80211_hdr *f_hdr;
571ecf67
JB
1180
1181 idx--;
1182 if (idx < 0)
1183 idx = IEEE80211_FRAGMENT_MAX - 1;
1184
1185 entry = &sdata->fragments[idx];
1186 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1187 entry->rx_queue != rx_queue ||
1188 entry->last_frag + 1 != frag)
1189 continue;
1190
b73d70ad 1191 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
571ecf67 1192
b73d70ad
HH
1193 /*
1194 * Check ftype and addresses are equal, else check next fragment
1195 */
1196 if (((hdr->frame_control ^ f_hdr->frame_control) &
1197 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
571ecf67
JB
1198 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1199 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1200 continue;
1201
ab46623e 1202 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
1203 __skb_queue_purge(&entry->skb_list);
1204 continue;
1205 }
1206 return entry;
1207 }
1208
1209 return NULL;
1210}
1211
49461622 1212static ieee80211_rx_result debug_noinline
5cf121c3 1213ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67
JB
1214{
1215 struct ieee80211_hdr *hdr;
1216 u16 sc;
358c8d9d 1217 __le16 fc;
571ecf67
JB
1218 unsigned int frag, seq;
1219 struct ieee80211_fragment_entry *entry;
1220 struct sk_buff *skb;
1221
b73d70ad 1222 hdr = (struct ieee80211_hdr *)rx->skb->data;
358c8d9d 1223 fc = hdr->frame_control;
571ecf67
JB
1224 sc = le16_to_cpu(hdr->seq_ctrl);
1225 frag = sc & IEEE80211_SCTL_FRAG;
1226
358c8d9d 1227 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
571ecf67
JB
1228 (rx->skb)->len < 24 ||
1229 is_multicast_ether_addr(hdr->addr1))) {
1230 /* not fragmented */
1231 goto out;
1232 }
1233 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1234
1235 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1236
1237 if (frag == 0) {
1238 /* This is the first fragment of a new frame. */
1239 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
5cf121c3 1240 rx->queue, &(rx->skb));
8f20fc24 1241 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
358c8d9d 1242 ieee80211_has_protected(fc)) {
571ecf67
JB
1243 /* Store CCMP PN so that we can verify that the next
1244 * fragment has a sequential PN value. */
1245 entry->ccmp = 1;
1246 memcpy(entry->last_pn,
5cf121c3 1247 rx->key->u.ccmp.rx_pn[rx->queue],
571ecf67
JB
1248 CCMP_PN_LEN);
1249 }
9ae54c84 1250 return RX_QUEUED;
571ecf67
JB
1251 }
1252
1253 /* This is a fragment for a frame that should already be pending in
1254 * fragment cache. Add this fragment to the end of the pending entry.
1255 */
b73d70ad 1256 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
571ecf67
JB
1257 if (!entry) {
1258 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
e4c26add 1259 return RX_DROP_MONITOR;
571ecf67
JB
1260 }
1261
1262 /* Verify that MPDUs within one MSDU have sequential PN values.
1263 * (IEEE 802.11i, 8.3.3.4.5) */
1264 if (entry->ccmp) {
1265 int i;
1266 u8 pn[CCMP_PN_LEN], *rpn;
8f20fc24 1267 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
e4c26add 1268 return RX_DROP_UNUSABLE;
571ecf67
JB
1269 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1270 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1271 pn[i]++;
1272 if (pn[i])
1273 break;
1274 }
5cf121c3 1275 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
f4ea83dd 1276 if (memcmp(pn, rpn, CCMP_PN_LEN))
e4c26add 1277 return RX_DROP_UNUSABLE;
571ecf67
JB
1278 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1279 }
1280
358c8d9d 1281 skb_pull(rx->skb, ieee80211_hdrlen(fc));
571ecf67
JB
1282 __skb_queue_tail(&entry->skb_list, rx->skb);
1283 entry->last_frag = frag;
1284 entry->extra_len += rx->skb->len;
358c8d9d 1285 if (ieee80211_has_morefrags(fc)) {
571ecf67 1286 rx->skb = NULL;
9ae54c84 1287 return RX_QUEUED;
571ecf67
JB
1288 }
1289
1290 rx->skb = __skb_dequeue(&entry->skb_list);
1291 if (skb_tailroom(rx->skb) < entry->extra_len) {
1292 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1293 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1294 GFP_ATOMIC))) {
1295 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1296 __skb_queue_purge(&entry->skb_list);
e4c26add 1297 return RX_DROP_UNUSABLE;
571ecf67
JB
1298 }
1299 }
1300 while ((skb = __skb_dequeue(&entry->skb_list))) {
1301 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1302 dev_kfree_skb(skb);
1303 }
1304
1305 /* Complete frame has been reassembled - process it now */
5cf121c3 1306 rx->flags |= IEEE80211_RX_FRAGMENTED;
571ecf67
JB
1307
1308 out:
1309 if (rx->sta)
1310 rx->sta->rx_packets++;
1311 if (is_multicast_ether_addr(hdr->addr1))
1312 rx->local->dot11MulticastReceivedFrameCount++;
1313 else
1314 ieee80211_led_rx(rx->local);
9ae54c84 1315 return RX_CONTINUE;
571ecf67
JB
1316}
1317
49461622 1318static ieee80211_rx_result debug_noinline
5cf121c3 1319ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
571ecf67 1320{
af818581 1321 struct ieee80211_sub_if_data *sdata = rx->sdata;
358c8d9d 1322 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
571ecf67 1323
358c8d9d 1324 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
5cf121c3 1325 !(rx->flags & IEEE80211_RX_RA_MATCH)))
9ae54c84 1326 return RX_CONTINUE;
571ecf67 1327
05c914fe
JB
1328 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1329 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
e4c26add 1330 return RX_DROP_UNUSABLE;
98f0b0a3 1331
af818581
JB
1332 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1333 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1334 else
1335 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
571ecf67 1336
9ae54c84 1337 /* Free PS Poll skb here instead of returning RX_DROP that would
571ecf67
JB
1338 * count as an dropped frame. */
1339 dev_kfree_skb(rx->skb);
1340
9ae54c84 1341 return RX_QUEUED;
571ecf67
JB
1342}
1343
49461622 1344static ieee80211_rx_result debug_noinline
5cf121c3 1345ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
6e0d114d 1346{
6e0d114d 1347 u8 *data = rx->skb->data;
238f74a2 1348 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
6e0d114d 1349
238f74a2 1350 if (!ieee80211_is_data_qos(hdr->frame_control))
9ae54c84 1351 return RX_CONTINUE;
6e0d114d
JB
1352
1353 /* remove the qos control field, update frame type and meta-data */
238f74a2
HH
1354 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1355 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1356 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
6e0d114d 1357 /* change frame type to non QOS */
238f74a2 1358 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
6e0d114d 1359
9ae54c84 1360 return RX_CONTINUE;
6e0d114d
JB
1361}
1362
76ee65bf 1363static int
5cf121c3 1364ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 1365{
07346f81 1366 if (unlikely(!rx->sta ||
f4ea83dd 1367 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
76ee65bf 1368 return -EACCES;
571ecf67 1369
76ee65bf 1370 return 0;
571ecf67
JB
1371}
1372
76ee65bf 1373static int
358c8d9d 1374ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
571ecf67 1375{
eb9fb5b8
JB
1376 struct sk_buff *skb = rx->skb;
1377 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1378
3017b80b 1379 /*
7848ba7d
JB
1380 * Pass through unencrypted frames if the hardware has
1381 * decrypted them already.
3017b80b 1382 */
eb9fb5b8 1383 if (status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1384 return 0;
571ecf67
JB
1385
1386 /* Drop unencrypted frames if key is set. */
358c8d9d
HH
1387 if (unlikely(!ieee80211_has_protected(fc) &&
1388 !ieee80211_is_nullfunc(fc) &&
f2ca3ea4 1389 ieee80211_is_data(fc) &&
b3fc9c6c 1390 (rx->key || rx->sdata->drop_unencrypted)))
76ee65bf 1391 return -EACCES;
f2ca3ea4
JM
1392 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1393 if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1394 rx->key))
1395 return -EACCES;
1396 /* BIP does not use Protected field, so need to check MMIE */
1397 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
1398 && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
1399 rx->key))
1400 return -EACCES;
1401 /*
1402 * When using MFP, Action frames are not allowed prior to
1403 * having configured keys.
1404 */
1405 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1406 ieee80211_is_robust_mgmt_frame(
1407 (struct ieee80211_hdr *) rx->skb->data)))
1408 return -EACCES;
1409 }
b3fc9c6c 1410
76ee65bf 1411 return 0;
571ecf67
JB
1412}
1413
76ee65bf 1414static int
e31a16d6 1415__ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
571ecf67 1416{
eb9fb5b8
JB
1417 struct ieee80211_sub_if_data *sdata = rx->sdata;
1418 struct net_device *dev = sdata->dev;
f14543ee
FF
1419 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1420
9bc383de
JB
1421 if (ieee80211_has_a4(hdr->frame_control) &&
1422 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
f14543ee 1423 return -1;
9bc383de
JB
1424
1425 if (is_multicast_ether_addr(hdr->addr1) &&
1426 ((sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta) ||
1427 (sdata->vif.type == NL80211_IFTYPE_STATION && sdata->u.mgd.use_4addr)))
f14543ee 1428 return -1;
571ecf67 1429
e31a16d6 1430 return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
76ee65bf 1431}
571ecf67 1432
ce3edf6d
JB
1433/*
1434 * requires that rx->skb is a frame with ethernet header
1435 */
358c8d9d 1436static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
ce3edf6d 1437{
c97c23e3 1438 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
ce3edf6d
JB
1439 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1440 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1441
1442 /*
1443 * Allow EAPOL frames to us/the PAE group address regardless
1444 * of whether the frame was encrypted or not.
1445 */
1446 if (ehdr->h_proto == htons(ETH_P_PAE) &&
eb9fb5b8 1447 (compare_ether_addr(ehdr->h_dest, rx->sdata->dev->dev_addr) == 0 ||
ce3edf6d
JB
1448 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1449 return true;
1450
1451 if (ieee80211_802_1x_port_control(rx) ||
358c8d9d 1452 ieee80211_drop_unencrypted(rx, fc))
ce3edf6d
JB
1453 return false;
1454
1455 return true;
1456}
1457
1458/*
1459 * requires that rx->skb is a frame with ethernet header
1460 */
76ee65bf 1461static void
5cf121c3 1462ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf 1463{
eb9fb5b8
JB
1464 struct ieee80211_sub_if_data *sdata = rx->sdata;
1465 struct net_device *dev = sdata->dev;
76ee65bf
RR
1466 struct ieee80211_local *local = rx->local;
1467 struct sk_buff *skb, *xmit_skb;
ce3edf6d
JB
1468 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1469 struct sta_info *dsta;
571ecf67 1470
76ee65bf
RR
1471 skb = rx->skb;
1472 xmit_skb = NULL;
571ecf67 1473
05c914fe
JB
1474 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1475 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
213cd118 1476 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
9bc383de
JB
1477 (rx->flags & IEEE80211_RX_RA_MATCH) &&
1478 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
ce3edf6d
JB
1479 if (is_multicast_ether_addr(ehdr->h_dest)) {
1480 /*
1481 * send multicast frames both to higher layers in
1482 * local net stack and back to the wireless medium
1483 */
76ee65bf
RR
1484 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1485 if (!xmit_skb && net_ratelimit())
571ecf67
JB
1486 printk(KERN_DEBUG "%s: failed to clone "
1487 "multicast frame\n", dev->name);
1488 } else {
571ecf67 1489 dsta = sta_info_get(local, skb->data);
d0709a65 1490 if (dsta && dsta->sdata->dev == dev) {
ce3edf6d
JB
1491 /*
1492 * The destination station is associated to
1493 * this AP (in this VLAN), so send the frame
1494 * directly to it and do not pass it to local
1495 * net stack.
571ecf67 1496 */
76ee65bf 1497 xmit_skb = skb;
571ecf67
JB
1498 skb = NULL;
1499 }
571ecf67
JB
1500 }
1501 }
1502
1503 if (skb) {
d1c3a37c
JB
1504 int align __maybe_unused;
1505
1506#if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1507 /*
1508 * 'align' will only take the values 0 or 2 here
1509 * since all frames are required to be aligned
1510 * to 2-byte boundaries when being passed to
1511 * mac80211. That also explains the __skb_push()
1512 * below.
1513 */
dacb6f1d 1514 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
d1c3a37c
JB
1515 if (align) {
1516 if (WARN_ON(skb_headroom(skb) < 3)) {
1517 dev_kfree_skb(skb);
1518 skb = NULL;
1519 } else {
1520 u8 *data = skb->data;
8ce0b589
ZY
1521 size_t len = skb_headlen(skb);
1522 skb->data -= align;
1523 memmove(skb->data, data, len);
1524 skb_set_tail_pointer(skb, len);
d1c3a37c
JB
1525 }
1526 }
1527#endif
1528
1529 if (skb) {
1530 /* deliver to local stack */
1531 skb->protocol = eth_type_trans(skb, dev);
1532 memset(skb->cb, 0, sizeof(skb->cb));
1533 netif_rx(skb);
1534 }
571ecf67
JB
1535 }
1536
76ee65bf 1537 if (xmit_skb) {
571ecf67 1538 /* send to wireless media */
f831e909 1539 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
1540 skb_reset_network_header(xmit_skb);
1541 skb_reset_mac_header(xmit_skb);
76ee65bf 1542 dev_queue_xmit(xmit_skb);
571ecf67 1543 }
76ee65bf
RR
1544}
1545
49461622 1546static ieee80211_rx_result debug_noinline
5cf121c3 1547ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
fd4c7f2f 1548{
eb9fb5b8 1549 struct net_device *dev = rx->sdata->dev;
fd4c7f2f 1550 struct ieee80211_local *local = rx->local;
358c8d9d 1551 u16 ethertype;
fd4c7f2f
RR
1552 u8 *payload;
1553 struct sk_buff *skb = rx->skb, *frame = NULL;
358c8d9d
HH
1554 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1555 __le16 fc = hdr->frame_control;
fd4c7f2f
RR
1556 const struct ethhdr *eth;
1557 int remaining, err;
1558 u8 dst[ETH_ALEN];
1559 u8 src[ETH_ALEN];
fd4c7f2f 1560
358c8d9d 1561 if (unlikely(!ieee80211_is_data(fc)))
9ae54c84 1562 return RX_CONTINUE;
fd4c7f2f 1563
358c8d9d 1564 if (unlikely(!ieee80211_is_data_present(fc)))
e4c26add 1565 return RX_DROP_MONITOR;
fd4c7f2f 1566
5cf121c3 1567 if (!(rx->flags & IEEE80211_RX_AMSDU))
9ae54c84 1568 return RX_CONTINUE;
fd4c7f2f 1569
e31a16d6 1570 err = __ieee80211_data_to_8023(rx);
fd4c7f2f 1571 if (unlikely(err))
e4c26add 1572 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1573
1574 skb->dev = dev;
1575
1576 dev->stats.rx_packets++;
1577 dev->stats.rx_bytes += skb->len;
1578
1579 /* skip the wrapping header */
1580 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1581 if (!eth)
e4c26add 1582 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1583
1584 while (skb != frame) {
1585 u8 padding;
1586 __be16 len = eth->h_proto;
1587 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1588
1589 remaining = skb->len;
1590 memcpy(dst, eth->h_dest, ETH_ALEN);
1591 memcpy(src, eth->h_source, ETH_ALEN);
1592
1593 padding = ((4 - subframe_len) & 0x3);
1594 /* the last MSDU has no padding */
f4ea83dd 1595 if (subframe_len > remaining)
e4c26add 1596 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1597
1598 skb_pull(skb, sizeof(struct ethhdr));
1599 /* if last subframe reuse skb */
1600 if (remaining <= subframe_len + padding)
1601 frame = skb;
1602 else {
d1c3a37c
JB
1603 /*
1604 * Allocate and reserve two bytes more for payload
1605 * alignment since sizeof(struct ethhdr) is 14.
1606 */
1607 frame = dev_alloc_skb(
1608 ALIGN(local->hw.extra_tx_headroom, 4) +
1609 subframe_len + 2);
fd4c7f2f
RR
1610
1611 if (frame == NULL)
e4c26add 1612 return RX_DROP_UNUSABLE;
fd4c7f2f 1613
d1c3a37c
JB
1614 skb_reserve(frame,
1615 ALIGN(local->hw.extra_tx_headroom, 4) +
1616 sizeof(struct ethhdr) + 2);
fd4c7f2f
RR
1617 memcpy(skb_put(frame, ntohs(len)), skb->data,
1618 ntohs(len));
1619
1620 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1621 padding);
1622 if (!eth) {
fd4c7f2f 1623 dev_kfree_skb(frame);
e4c26add 1624 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1625 }
1626 }
1627
ce3edf6d 1628 skb_reset_network_header(frame);
fd4c7f2f
RR
1629 frame->dev = dev;
1630 frame->priority = skb->priority;
1631 rx->skb = frame;
1632
fd4c7f2f
RR
1633 payload = frame->data;
1634 ethertype = (payload[6] << 8) | payload[7];
1635
1636 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
ce3edf6d
JB
1637 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1638 compare_ether_addr(payload,
1639 bridge_tunnel_header) == 0)) {
fd4c7f2f
RR
1640 /* remove RFC1042 or Bridge-Tunnel
1641 * encapsulation and replace EtherType */
1642 skb_pull(frame, 6);
1643 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1644 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1645 } else {
ce3edf6d
JB
1646 memcpy(skb_push(frame, sizeof(__be16)),
1647 &len, sizeof(__be16));
fd4c7f2f
RR
1648 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1649 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1650 }
1651
358c8d9d 1652 if (!ieee80211_frame_allowed(rx, fc)) {
ce3edf6d 1653 if (skb == frame) /* last frame */
e4c26add 1654 return RX_DROP_UNUSABLE;
ce3edf6d
JB
1655 dev_kfree_skb(frame);
1656 continue;
1657 }
fd4c7f2f
RR
1658
1659 ieee80211_deliver_skb(rx);
1660 }
1661
9ae54c84 1662 return RX_QUEUED;
fd4c7f2f
RR
1663}
1664
bf94e17b 1665#ifdef CONFIG_MAC80211_MESH
b0dee578 1666static ieee80211_rx_result
e32f85f7
LCC
1667ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1668{
1669 struct ieee80211_hdr *hdr;
1670 struct ieee80211s_hdr *mesh_hdr;
1671 unsigned int hdrlen;
1672 struct sk_buff *skb = rx->skb, *fwd_skb;
3b8d81e0 1673 struct ieee80211_local *local = rx->local;
eb9fb5b8 1674 struct ieee80211_sub_if_data *sdata = rx->sdata;
e32f85f7
LCC
1675
1676 hdr = (struct ieee80211_hdr *) skb->data;
1677 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1678 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1679
1680 if (!ieee80211_is_data(hdr->frame_control))
1681 return RX_CONTINUE;
1682
1683 if (!mesh_hdr->ttl)
1684 /* illegal frame */
1685 return RX_DROP_MONITOR;
1686
43b7b314 1687 if (mesh_hdr->flags & MESH_FLAGS_AE) {
79617dee 1688 struct mesh_path *mppath;
43b7b314
JC
1689 char *proxied_addr;
1690 char *mpp_addr;
1691
1692 if (is_multicast_ether_addr(hdr->addr1)) {
1693 mpp_addr = hdr->addr3;
1694 proxied_addr = mesh_hdr->eaddr1;
1695 } else {
1696 mpp_addr = hdr->addr4;
1697 proxied_addr = mesh_hdr->eaddr2;
1698 }
79617dee 1699
79617dee 1700 rcu_read_lock();
43b7b314 1701 mppath = mpp_path_lookup(proxied_addr, sdata);
79617dee 1702 if (!mppath) {
43b7b314 1703 mpp_path_add(proxied_addr, mpp_addr, sdata);
79617dee
Y
1704 } else {
1705 spin_lock_bh(&mppath->state_lock);
1706 mppath->exp_time = jiffies;
43b7b314
JC
1707 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1708 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
79617dee
Y
1709 spin_unlock_bh(&mppath->state_lock);
1710 }
1711 rcu_read_unlock();
1712 }
1713
3c5772a5
JC
1714 /* Frame has reached destination. Don't forward */
1715 if (!is_multicast_ether_addr(hdr->addr1) &&
eb9fb5b8 1716 compare_ether_addr(sdata->dev->dev_addr, hdr->addr3) == 0)
e32f85f7
LCC
1717 return RX_CONTINUE;
1718
1719 mesh_hdr->ttl--;
1720
1721 if (rx->flags & IEEE80211_RX_RA_MATCH) {
1722 if (!mesh_hdr->ttl)
472dbc45 1723 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
e32f85f7
LCC
1724 dropped_frames_ttl);
1725 else {
1726 struct ieee80211_hdr *fwd_hdr;
3b8d81e0
JB
1727 struct ieee80211_tx_info *info;
1728
e32f85f7
LCC
1729 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1730
1731 if (!fwd_skb && net_ratelimit())
1732 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
eb9fb5b8 1733 sdata->dev->name);
e32f85f7
LCC
1734
1735 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
eb9fb5b8 1736 memcpy(fwd_hdr->addr2, sdata->dev->dev_addr, ETH_ALEN);
3b8d81e0
JB
1737 info = IEEE80211_SKB_CB(fwd_skb);
1738 memset(info, 0, sizeof(*info));
1739 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
5061b0c2 1740 info->control.vif = &rx->sdata->vif;
3b8d81e0 1741 ieee80211_select_queue(local, fwd_skb);
c8a61a7d
DW
1742 if (is_multicast_ether_addr(fwd_hdr->addr1))
1743 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1744 fwded_mcast);
1745 else {
3c5772a5
JC
1746 int err;
1747 /*
1748 * Save TA to addr1 to send TA a path error if a
1749 * suitable next hop is not found
1750 */
1751 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
249b405c 1752 ETH_ALEN);
3c5772a5 1753 err = mesh_nexthop_lookup(fwd_skb, sdata);
249b405c
JC
1754 /* Failed to immediately resolve next hop:
1755 * fwded frame was dropped or will be added
1756 * later to the pending skb queue. */
1757 if (err)
1758 return RX_DROP_MONITOR;
c8a61a7d
DW
1759
1760 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1761 fwded_unicast);
249b405c
JC
1762 }
1763 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1764 fwded_frames);
3b8d81e0 1765 ieee80211_add_pending_skb(local, fwd_skb);
e32f85f7
LCC
1766 }
1767 }
1768
3c5772a5 1769 if (is_multicast_ether_addr(hdr->addr1) ||
eb9fb5b8 1770 sdata->dev->flags & IFF_PROMISC)
e32f85f7
LCC
1771 return RX_CONTINUE;
1772 else
1773 return RX_DROP_MONITOR;
1774}
bf94e17b 1775#endif
e32f85f7 1776
49461622 1777static ieee80211_rx_result debug_noinline
5cf121c3 1778ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf 1779{
eb9fb5b8
JB
1780 struct ieee80211_sub_if_data *sdata = rx->sdata;
1781 struct net_device *dev = sdata->dev;
358c8d9d
HH
1782 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1783 __le16 fc = hdr->frame_control;
ce3edf6d 1784 int err;
76ee65bf 1785
358c8d9d 1786 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
9ae54c84 1787 return RX_CONTINUE;
76ee65bf 1788
358c8d9d 1789 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
e4c26add 1790 return RX_DROP_MONITOR;
76ee65bf 1791
f14543ee
FF
1792 /*
1793 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1794 * that a 4-addr station can be detected and moved into a separate VLAN
1795 */
1796 if (ieee80211_has_a4(hdr->frame_control) &&
1797 sdata->vif.type == NL80211_IFTYPE_AP)
1798 return RX_DROP_MONITOR;
1799
e31a16d6 1800 err = __ieee80211_data_to_8023(rx);
76ee65bf 1801 if (unlikely(err))
e4c26add 1802 return RX_DROP_UNUSABLE;
76ee65bf 1803
358c8d9d 1804 if (!ieee80211_frame_allowed(rx, fc))
e4c26add 1805 return RX_DROP_MONITOR;
ce3edf6d 1806
76ee65bf
RR
1807 rx->skb->dev = dev;
1808
1809 dev->stats.rx_packets++;
1810 dev->stats.rx_bytes += rx->skb->len;
1811
1812 ieee80211_deliver_skb(rx);
571ecf67 1813
9ae54c84 1814 return RX_QUEUED;
571ecf67
JB
1815}
1816
49461622 1817static ieee80211_rx_result debug_noinline
5cf121c3 1818ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
71364716
RR
1819{
1820 struct ieee80211_local *local = rx->local;
1821 struct ieee80211_hw *hw = &local->hw;
1822 struct sk_buff *skb = rx->skb;
a7767f95 1823 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
71364716
RR
1824 struct tid_ampdu_rx *tid_agg_rx;
1825 u16 start_seq_num;
1826 u16 tid;
1827
a7767f95 1828 if (likely(!ieee80211_is_ctl(bar->frame_control)))
9ae54c84 1829 return RX_CONTINUE;
71364716 1830
a7767f95 1831 if (ieee80211_is_back_req(bar->frame_control)) {
71364716 1832 if (!rx->sta)
a02ae758 1833 return RX_DROP_MONITOR;
71364716 1834 tid = le16_to_cpu(bar->control) >> 12;
cee24a3e
RR
1835 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1836 != HT_AGG_STATE_OPERATIONAL)
a02ae758 1837 return RX_DROP_MONITOR;
cee24a3e 1838 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
71364716
RR
1839
1840 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1841
1842 /* reset session timer */
20ad19d0
JB
1843 if (tid_agg_rx->timeout)
1844 mod_timer(&tid_agg_rx->session_timer,
1845 TU_TO_EXP_TIME(tid_agg_rx->timeout));
71364716 1846
a02ae758
JB
1847 /* release stored frames up to start of BAR */
1848 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
1849 kfree_skb(skb);
1850 return RX_QUEUED;
71364716
RR
1851 }
1852
9ae54c84 1853 return RX_CONTINUE;
71364716
RR
1854}
1855
f4f727a6
JM
1856static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1857 struct ieee80211_mgmt *mgmt,
1858 size_t len)
fea14732
JM
1859{
1860 struct ieee80211_local *local = sdata->local;
1861 struct sk_buff *skb;
1862 struct ieee80211_mgmt *resp;
1863
1864 if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
1865 /* Not to own unicast address */
1866 return;
1867 }
1868
46900298
JB
1869 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1870 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
77fdaa12 1871 /* Not from the current AP or not associated yet. */
fea14732
JM
1872 return;
1873 }
1874
1875 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1876 /* Too short SA Query request frame */
1877 return;
1878 }
1879
1880 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1881 if (skb == NULL)
1882 return;
1883
1884 skb_reserve(skb, local->hw.extra_tx_headroom);
1885 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1886 memset(resp, 0, 24);
1887 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1888 memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
46900298 1889 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
fea14732
JM
1890 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1891 IEEE80211_STYPE_ACTION);
1892 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1893 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1894 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1895 memcpy(resp->u.action.u.sa_query.trans_id,
1896 mgmt->u.action.u.sa_query.trans_id,
1897 WLAN_SA_QUERY_TR_ID_LEN);
1898
62ae67be 1899 ieee80211_tx_skb(sdata, skb);
fea14732
JM
1900}
1901
de1ede7a
JB
1902static ieee80211_rx_result debug_noinline
1903ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1904{
1905 struct ieee80211_local *local = rx->local;
eb9fb5b8 1906 struct ieee80211_sub_if_data *sdata = rx->sdata;
de1ede7a
JB
1907 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1908 int len = rx->skb->len;
1909
1910 if (!ieee80211_is_action(mgmt->frame_control))
1911 return RX_CONTINUE;
1912
1913 if (!rx->sta)
1914 return RX_DROP_MONITOR;
1915
1916 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1917 return RX_DROP_MONITOR;
1918
97ebe12a
JM
1919 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1920 return RX_DROP_MONITOR;
1921
de1ede7a
JB
1922 /* all categories we currently handle have action_code */
1923 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1924 return RX_DROP_MONITOR;
1925
de1ede7a
JB
1926 switch (mgmt->u.action.category) {
1927 case WLAN_CATEGORY_BACK:
8abd3f9b
JB
1928 /*
1929 * The aggregation code is not prepared to handle
1930 * anything but STA/AP due to the BSSID handling;
1931 * IBSS could work in the code but isn't supported
1932 * by drivers or the standard.
1933 */
1934 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1935 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1936 sdata->vif.type != NL80211_IFTYPE_AP)
1937 return RX_DROP_MONITOR;
1938
de1ede7a
JB
1939 switch (mgmt->u.action.u.addba_req.action_code) {
1940 case WLAN_ACTION_ADDBA_REQ:
1941 if (len < (IEEE80211_MIN_ACTION_SIZE +
1942 sizeof(mgmt->u.action.u.addba_req)))
1943 return RX_DROP_MONITOR;
1944 ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1945 break;
1946 case WLAN_ACTION_ADDBA_RESP:
1947 if (len < (IEEE80211_MIN_ACTION_SIZE +
1948 sizeof(mgmt->u.action.u.addba_resp)))
1949 return RX_DROP_MONITOR;
1950 ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1951 break;
1952 case WLAN_ACTION_DELBA:
1953 if (len < (IEEE80211_MIN_ACTION_SIZE +
1954 sizeof(mgmt->u.action.u.delba)))
1955 return RX_DROP_MONITOR;
1956 ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1957 break;
1958 }
39192c0b
JB
1959 break;
1960 case WLAN_CATEGORY_SPECTRUM_MGMT:
1961 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1962 return RX_DROP_MONITOR;
46900298
JB
1963
1964 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1965 return RX_DROP_MONITOR;
1966
39192c0b
JB
1967 switch (mgmt->u.action.u.measurement.action_code) {
1968 case WLAN_ACTION_SPCT_MSR_REQ:
1969 if (len < (IEEE80211_MIN_ACTION_SIZE +
1970 sizeof(mgmt->u.action.u.measurement)))
1971 return RX_DROP_MONITOR;
1972 ieee80211_process_measurement_req(sdata, mgmt, len);
1973 break;
c481ec97
S
1974 case WLAN_ACTION_SPCT_CHL_SWITCH:
1975 if (len < (IEEE80211_MIN_ACTION_SIZE +
1976 sizeof(mgmt->u.action.u.chan_switch)))
1977 return RX_DROP_MONITOR;
1978
cc32abd4
JB
1979 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1980 return RX_DROP_MONITOR;
1981
46900298 1982 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
c481ec97
S
1983 return RX_DROP_MONITOR;
1984
77fdaa12 1985 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
39192c0b
JB
1986 }
1987 break;
fea14732
JM
1988 case WLAN_CATEGORY_SA_QUERY:
1989 if (len < (IEEE80211_MIN_ACTION_SIZE +
1990 sizeof(mgmt->u.action.u.sa_query)))
1991 return RX_DROP_MONITOR;
1992 switch (mgmt->u.action.u.sa_query.action) {
1993 case WLAN_ACTION_SA_QUERY_REQUEST:
1994 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1995 return RX_DROP_MONITOR;
1996 ieee80211_process_sa_query_req(sdata, mgmt, len);
1997 break;
1998 case WLAN_ACTION_SA_QUERY_RESPONSE:
1999 /*
2000 * SA Query response is currently only used in AP mode
2001 * and it is processed in user space.
2002 */
2003 return RX_CONTINUE;
2004 }
2005 break;
39192c0b
JB
2006 default:
2007 return RX_CONTINUE;
de1ede7a
JB
2008 }
2009
39192c0b
JB
2010 rx->sta->rx_packets++;
2011 dev_kfree_skb(rx->skb);
2012 return RX_QUEUED;
de1ede7a
JB
2013}
2014
49461622 2015static ieee80211_rx_result debug_noinline
5cf121c3 2016ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67 2017{
eb9fb5b8 2018 struct ieee80211_sub_if_data *sdata = rx->sdata;
97ebe12a 2019 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
571ecf67 2020
5cf121c3 2021 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
e4c26add 2022 return RX_DROP_MONITOR;
571ecf67 2023
97ebe12a
JM
2024 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
2025 return RX_DROP_MONITOR;
2026
472dbc45 2027 if (ieee80211_vif_is_mesh(&sdata->vif))
f1d58c25 2028 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
472dbc45 2029
3832c287 2030 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
f1d58c25 2031 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
f9d540ee 2032
7986cf95 2033 if (sdata->vif.type == NL80211_IFTYPE_STATION)
f1d58c25 2034 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
46900298 2035
7986cf95 2036 return RX_DROP_MONITOR;
571ecf67
JB
2037}
2038
3b8d81e0 2039static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
5cf121c3 2040 struct ieee80211_rx_data *rx)
571ecf67 2041{
a7767f95
HH
2042 int keyidx;
2043 unsigned int hdrlen;
571ecf67 2044
a7767f95 2045 hdrlen = ieee80211_hdrlen(hdr->frame_control);
571ecf67
JB
2046 if (rx->skb->len >= hdrlen + 4)
2047 keyidx = rx->skb->data[hdrlen + 3] >> 6;
2048 else
2049 keyidx = -1;
2050
8944b79f 2051 if (!rx->sta) {
aa0daf0e
JB
2052 /*
2053 * Some hardware seem to generate incorrect Michael MIC
2054 * reports; ignore them to avoid triggering countermeasures.
2055 */
af2ced6a 2056 return;
571ecf67
JB
2057 }
2058
a7767f95 2059 if (!ieee80211_has_protected(hdr->frame_control))
af2ced6a 2060 return;
571ecf67 2061
05c914fe 2062 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
aa0daf0e
JB
2063 /*
2064 * APs with pairwise keys should never receive Michael MIC
2065 * errors for non-zero keyidx because these are reserved for
2066 * group keys and only the AP is sending real multicast
2067 * frames in the BSS.
2068 */
af2ced6a 2069 return;
571ecf67
JB
2070 }
2071
a7767f95
HH
2072 if (!ieee80211_is_data(hdr->frame_control) &&
2073 !ieee80211_is_auth(hdr->frame_control))
af2ced6a 2074 return;
571ecf67 2075
e6d6e342
JB
2076 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
2077 GFP_ATOMIC);
571ecf67
JB
2078}
2079
5cf121c3 2080/* TODO: use IEEE80211_RX_FRAGMENTED */
5f0b7de5
JB
2081static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2082 struct ieee80211_rate *rate)
3d30d949
MW
2083{
2084 struct ieee80211_sub_if_data *sdata;
2085 struct ieee80211_local *local = rx->local;
2086 struct ieee80211_rtap_hdr {
2087 struct ieee80211_radiotap_header hdr;
2088 u8 flags;
5f0b7de5 2089 u8 rate_or_pad;
3d30d949
MW
2090 __le16 chan_freq;
2091 __le16 chan_flags;
2092 } __attribute__ ((packed)) *rthdr;
2093 struct sk_buff *skb = rx->skb, *skb2;
2094 struct net_device *prev_dev = NULL;
eb9fb5b8 2095 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3d30d949 2096
8c0c709e 2097 if (status->flag & RX_FLAG_INTERNAL_CMTR)
3d30d949
MW
2098 goto out_free_skb;
2099
2100 if (skb_headroom(skb) < sizeof(*rthdr) &&
2101 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2102 goto out_free_skb;
2103
2104 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2105 memset(rthdr, 0, sizeof(*rthdr));
2106 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2107 rthdr->hdr.it_present =
2108 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
3d30d949
MW
2109 (1 << IEEE80211_RADIOTAP_CHANNEL));
2110
5f0b7de5
JB
2111 if (rate) {
2112 rthdr->rate_or_pad = rate->bitrate / 5;
2113 rthdr->hdr.it_present |=
2114 cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2115 }
3d30d949
MW
2116 rthdr->chan_freq = cpu_to_le16(status->freq);
2117
2118 if (status->band == IEEE80211_BAND_5GHZ)
2119 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2120 IEEE80211_CHAN_5GHZ);
2121 else
2122 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2123 IEEE80211_CHAN_2GHZ);
2124
2125 skb_set_mac_header(skb, 0);
2126 skb->ip_summed = CHECKSUM_UNNECESSARY;
2127 skb->pkt_type = PACKET_OTHERHOST;
2128 skb->protocol = htons(ETH_P_802_2);
2129
2130 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2131 if (!netif_running(sdata->dev))
2132 continue;
2133
05c914fe 2134 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3d30d949
MW
2135 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2136 continue;
2137
2138 if (prev_dev) {
2139 skb2 = skb_clone(skb, GFP_ATOMIC);
2140 if (skb2) {
2141 skb2->dev = prev_dev;
2142 netif_rx(skb2);
2143 }
2144 }
2145
2146 prev_dev = sdata->dev;
2147 sdata->dev->stats.rx_packets++;
2148 sdata->dev->stats.rx_bytes += skb->len;
2149 }
2150
2151 if (prev_dev) {
2152 skb->dev = prev_dev;
2153 netif_rx(skb);
2154 skb = NULL;
2155 } else
2156 goto out_free_skb;
2157
8c0c709e 2158 status->flag |= RX_FLAG_INTERNAL_CMTR;
3d30d949
MW
2159 return;
2160
2161 out_free_skb:
2162 dev_kfree_skb(skb);
2163}
2164
571ecf67 2165
8944b79f 2166static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
5cf121c3 2167 struct ieee80211_rx_data *rx,
5f0b7de5
JB
2168 struct sk_buff *skb,
2169 struct ieee80211_rate *rate)
58905290 2170{
58905290
JB
2171 ieee80211_rx_result res = RX_DROP_MONITOR;
2172
8944b79f
JB
2173 rx->skb = skb;
2174 rx->sdata = sdata;
8944b79f 2175
e32f85f7
LCC
2176#define CALL_RXH(rxh) \
2177 do { \
2178 res = rxh(rx); \
2179 if (res != RX_CONTINUE) \
2180 goto rxh_done; \
2181 } while (0);
49461622
JB
2182
2183 CALL_RXH(ieee80211_rx_h_passive_scan)
2184 CALL_RXH(ieee80211_rx_h_check)
2185 CALL_RXH(ieee80211_rx_h_decrypt)
572e0012 2186 CALL_RXH(ieee80211_rx_h_check_more_data)
49461622
JB
2187 CALL_RXH(ieee80211_rx_h_sta_process)
2188 CALL_RXH(ieee80211_rx_h_defragment)
2189 CALL_RXH(ieee80211_rx_h_ps_poll)
2190 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2191 /* must be after MMIC verify so header is counted in MPDU mic */
2192 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2193 CALL_RXH(ieee80211_rx_h_amsdu)
bf94e17b 2194#ifdef CONFIG_MAC80211_MESH
e32f85f7
LCC
2195 if (ieee80211_vif_is_mesh(&sdata->vif))
2196 CALL_RXH(ieee80211_rx_h_mesh_fwding);
bf94e17b 2197#endif
49461622
JB
2198 CALL_RXH(ieee80211_rx_h_data)
2199 CALL_RXH(ieee80211_rx_h_ctrl)
de1ede7a 2200 CALL_RXH(ieee80211_rx_h_action)
49461622
JB
2201 CALL_RXH(ieee80211_rx_h_mgmt)
2202
2203#undef CALL_RXH
2204
2205 rxh_done:
58905290
JB
2206 switch (res) {
2207 case RX_DROP_MONITOR:
49461622
JB
2208 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2209 if (rx->sta)
2210 rx->sta->rx_dropped++;
2211 /* fall through */
2212 case RX_CONTINUE:
5f0b7de5 2213 ieee80211_rx_cooked_monitor(rx, rate);
3d30d949 2214 break;
58905290 2215 case RX_DROP_UNUSABLE:
49461622
JB
2216 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2217 if (rx->sta)
2218 rx->sta->rx_dropped++;
58905290
JB
2219 dev_kfree_skb(rx->skb);
2220 break;
49461622
JB
2221 case RX_QUEUED:
2222 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
2223 break;
58905290
JB
2224 }
2225}
2226
571ecf67
JB
2227/* main receive path */
2228
23a24def 2229static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
7ab17c45 2230 struct ieee80211_rx_data *rx,
23a24def
JB
2231 struct ieee80211_hdr *hdr)
2232{
eb9fb5b8
JB
2233 struct sk_buff *skb = rx->skb;
2234 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2235 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
23a24def
JB
2236 int multicast = is_multicast_ether_addr(hdr->addr1);
2237
51fb61e7 2238 switch (sdata->vif.type) {
05c914fe 2239 case NL80211_IFTYPE_STATION:
9bc383de 2240 if (!bssid && !sdata->u.mgd.use_4addr)
23a24def 2241 return 0;
77fdaa12
JB
2242 if (!multicast &&
2243 compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
4150c572 2244 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 2245 return 0;
5cf121c3 2246 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
2247 }
2248 break;
05c914fe 2249 case NL80211_IFTYPE_ADHOC:
23a24def
JB
2250 if (!bssid)
2251 return 0;
a7767f95 2252 if (ieee80211_is_beacon(hdr->frame_control)) {
9d9bf77d 2253 return 1;
87291c02 2254 }
46900298 2255 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
5cf121c3 2256 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def 2257 return 0;
5cf121c3 2258 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
2259 } else if (!multicast &&
2260 compare_ether_addr(sdata->dev->dev_addr,
2261 hdr->addr1) != 0) {
4150c572 2262 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 2263 return 0;
5cf121c3 2264 rx->flags &= ~IEEE80211_RX_RA_MATCH;
0fb8ca45
JM
2265 } else if (!rx->sta) {
2266 int rate_idx;
eb9fb5b8 2267 if (status->flag & RX_FLAG_HT)
0fb8ca45
JM
2268 rate_idx = 0; /* TODO: HT rates */
2269 else
eb9fb5b8 2270 rate_idx = status->rate_idx;
ab1f5c0b 2271 rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
0fb8ca45
JM
2272 BIT(rate_idx));
2273 }
23a24def 2274 break;
05c914fe 2275 case NL80211_IFTYPE_MESH_POINT:
6032f934
JB
2276 if (!multicast &&
2277 compare_ether_addr(sdata->dev->dev_addr,
2278 hdr->addr1) != 0) {
2279 if (!(sdata->dev->flags & IFF_PROMISC))
2280 return 0;
2281
5cf121c3 2282 rx->flags &= ~IEEE80211_RX_RA_MATCH;
6032f934
JB
2283 }
2284 break;
05c914fe
JB
2285 case NL80211_IFTYPE_AP_VLAN:
2286 case NL80211_IFTYPE_AP:
23a24def
JB
2287 if (!bssid) {
2288 if (compare_ether_addr(sdata->dev->dev_addr,
2289 hdr->addr1))
2290 return 0;
2291 } else if (!ieee80211_bssid_match(bssid,
2292 sdata->dev->dev_addr)) {
5cf121c3 2293 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def 2294 return 0;
5cf121c3 2295 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def 2296 }
23a24def 2297 break;
05c914fe 2298 case NL80211_IFTYPE_WDS:
a7767f95 2299 if (bssid || !ieee80211_is_data(hdr->frame_control))
23a24def
JB
2300 return 0;
2301 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2302 return 0;
2303 break;
05c914fe 2304 case NL80211_IFTYPE_MONITOR:
05c914fe
JB
2305 case NL80211_IFTYPE_UNSPECIFIED:
2306 case __NL80211_IFTYPE_AFTER_LAST:
fb1c1cd6
JB
2307 /* should never get here */
2308 WARN_ON(1);
2309 break;
23a24def
JB
2310 }
2311
2312 return 1;
2313}
2314
571ecf67 2315/*
6368e4b1
RR
2316 * This is the actual Rx frames handler. as it blongs to Rx path it must
2317 * be called with rcu_read_lock protection.
571ecf67 2318 */
71ebb4aa
RR
2319static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2320 struct sk_buff *skb,
8318d78a 2321 struct ieee80211_rate *rate)
571ecf67 2322{
f1d58c25 2323 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
571ecf67
JB
2324 struct ieee80211_local *local = hw_to_local(hw);
2325 struct ieee80211_sub_if_data *sdata;
571ecf67 2326 struct ieee80211_hdr *hdr;
5cf121c3 2327 struct ieee80211_rx_data rx;
6368e4b1 2328 int prepares;
8e6f0032
JB
2329 struct ieee80211_sub_if_data *prev = NULL;
2330 struct sk_buff *skb_new;
571ecf67 2331
358c8d9d 2332 hdr = (struct ieee80211_hdr *)skb->data;
571ecf67
JB
2333 memset(&rx, 0, sizeof(rx));
2334 rx.skb = skb;
2335 rx.local = local;
72abd81b 2336
358c8d9d 2337 if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
571ecf67 2338 local->dot11ReceivedFragmentCount++;
571ecf67 2339
142b9f50
HS
2340 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2341 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
5cf121c3 2342 rx.flags |= IEEE80211_RX_IN_SCAN;
571ecf67 2343
38f3714d 2344 ieee80211_parse_qos(&rx);
d1c3a37c 2345 ieee80211_verify_alignment(&rx);
38f3714d 2346
af2ced6a 2347 rx.sta = sta_info_get(local, hdr->addr2);
eb9fb5b8 2348 if (rx.sta)
af2ced6a 2349 rx.sdata = rx.sta->sdata;
571ecf67 2350
fbc44bf7
JB
2351 if (rx.sdata && ieee80211_is_data(hdr->frame_control)) {
2352 rx.flags |= IEEE80211_RX_RA_MATCH;
2353 prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
af2ced6a
JB
2354 if (prepares) {
2355 if (status->flag & RX_FLAG_MMIC_ERROR) {
2356 if (rx.flags & IEEE80211_RX_RA_MATCH)
2357 ieee80211_rx_michael_mic_report(hdr, &rx);
2358 } else
2359 prev = rx.sdata;
2360 }
fbc44bf7 2361 } else list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2a8a9a88
JB
2362 if (!netif_running(sdata->dev))
2363 continue;
2364
fbc44bf7
JB
2365 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2366 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
b2e7771e
JB
2367 continue;
2368
5cf121c3 2369 rx.flags |= IEEE80211_RX_RA_MATCH;
7ab17c45 2370 prepares = prepare_for_handlers(sdata, &rx, hdr);
23a24def 2371
6368e4b1 2372 if (!prepares)
23a24def 2373 continue;
8e6f0032 2374
af2ced6a
JB
2375 if (status->flag & RX_FLAG_MMIC_ERROR) {
2376 rx.sdata = sdata;
2377 if (rx.flags & IEEE80211_RX_RA_MATCH)
2378 ieee80211_rx_michael_mic_report(hdr, &rx);
2379 continue;
2380 }
2381
340e11f3
JB
2382 /*
2383 * frame is destined for this interface, but if it's not
2384 * also for the previous one we handle that after the
2385 * loop to avoid copying the SKB once too much
2386 */
2387
2388 if (!prev) {
2389 prev = sdata;
2390 continue;
8e6f0032 2391 }
340e11f3
JB
2392
2393 /*
2394 * frame was destined for the previous interface
2395 * so invoke RX handlers for it
2396 */
2397
2398 skb_new = skb_copy(skb, GFP_ATOMIC);
2399 if (!skb_new) {
2400 if (net_ratelimit())
2401 printk(KERN_DEBUG "%s: failed to copy "
a4278e18 2402 "multicast frame for %s\n",
dd1cd4c6
JB
2403 wiphy_name(local->hw.wiphy),
2404 prev->dev->name);
340e11f3
JB
2405 continue;
2406 }
5f0b7de5 2407 ieee80211_invoke_rx_handlers(prev, &rx, skb_new, rate);
8e6f0032 2408 prev = sdata;
571ecf67 2409 }
a4b7d7bd 2410 if (prev)
5f0b7de5 2411 ieee80211_invoke_rx_handlers(prev, &rx, skb, rate);
a4b7d7bd 2412 else
8e6f0032 2413 dev_kfree_skb(skb);
571ecf67 2414}
6368e4b1
RR
2415
2416/*
2417 * This is the receive path handler. It is called by a low level driver when an
2418 * 802.11 MPDU is received from the hardware.
2419 */
103bf9f7 2420void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
6368e4b1
RR
2421{
2422 struct ieee80211_local *local = hw_to_local(hw);
8318d78a
JB
2423 struct ieee80211_rate *rate = NULL;
2424 struct ieee80211_supported_band *sband;
f1d58c25 2425 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
8318d78a 2426
d20ef63d
JB
2427 WARN_ON_ONCE(softirq_count() == 0);
2428
77a980dc
JB
2429 if (WARN_ON(status->band < 0 ||
2430 status->band >= IEEE80211_NUM_BANDS))
2431 goto drop;
8318d78a
JB
2432
2433 sband = local->hw.wiphy->bands[status->band];
77a980dc
JB
2434 if (WARN_ON(!sband))
2435 goto drop;
8318d78a 2436
89c3a8ac
JB
2437 /*
2438 * If we're suspending, it is possible although not too likely
2439 * that we'd be receiving frames after having already partially
2440 * quiesced the stack. We can't process such frames then since
2441 * that might, for example, cause stations to be added or other
2442 * driver callbacks be invoked.
2443 */
77a980dc
JB
2444 if (unlikely(local->quiescing || local->suspended))
2445 goto drop;
89c3a8ac 2446
ea77f12f
JB
2447 /*
2448 * The same happens when we're not even started,
2449 * but that's worth a warning.
2450 */
77a980dc
JB
2451 if (WARN_ON(!local->started))
2452 goto drop;
ea77f12f 2453
0fb8ca45 2454 if (status->flag & RX_FLAG_HT) {
e5d6eb83
LR
2455 /*
2456 * rate_idx is MCS index, which can be [0-76] as documented on:
2457 *
2458 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2459 *
2460 * Anything else would be some sort of driver or hardware error.
2461 * The driver should catch hardware errors.
2462 */
2463 if (WARN((status->rate_idx < 0 ||
2464 status->rate_idx > 76),
2465 "Rate marked as an HT rate but passed "
2466 "status->rate_idx is not "
2467 "an MCS index [0-76]: %d (0x%02x)\n",
2468 status->rate_idx,
2469 status->rate_idx))
77a980dc 2470 goto drop;
0fb8ca45
JM
2471 } else {
2472 if (WARN_ON(status->rate_idx < 0 ||
2473 status->rate_idx >= sband->n_bitrates))
77a980dc 2474 goto drop;
0fb8ca45
JM
2475 rate = &sband->bitrates[status->rate_idx];
2476 }
6368e4b1
RR
2477
2478 /*
2479 * key references and virtual interfaces are protected using RCU
2480 * and this requires that we are in a read-side RCU section during
2481 * receive processing
2482 */
2483 rcu_read_lock();
2484
2485 /*
2486 * Frames with failed FCS/PLCP checksum are not returned,
2487 * all other frames are returned without radiotap header
2488 * if it was previously present.
2489 * Also, frames with less than 16 bytes are dropped.
2490 */
f1d58c25 2491 skb = ieee80211_rx_monitor(local, skb, rate);
6368e4b1
RR
2492 if (!skb) {
2493 rcu_read_unlock();
2494 return;
2495 }
2496
aec67952
JM
2497 /*
2498 * In theory, the block ack reordering should happen after duplicate
2499 * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
2500 * the call to ieee80211_rx_reorder_ampdu() should really be moved to
2501 * happen as a new RX handler between ieee80211_rx_h_check and
2502 * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
2503 * the time being, the call can be here since RX reorder buf processing
2504 * will implicitly skip duplicates. We could, in theory at least,
2505 * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
2506 * frames from other than operational channel), but that should not
2507 * happen in normal networks.
2508 */
f1d58c25
JB
2509 if (!ieee80211_rx_reorder_ampdu(local, skb))
2510 __ieee80211_rx_handle_packet(hw, skb, rate);
6368e4b1
RR
2511
2512 rcu_read_unlock();
77a980dc
JB
2513
2514 return;
2515 drop:
2516 kfree_skb(skb);
6368e4b1 2517}
103bf9f7 2518EXPORT_SYMBOL(ieee80211_rx);
571ecf67
JB
2519
2520/* This is a version of the rx handler that can be called from hard irq
2521 * context. Post the skb on the queue and schedule the tasklet */
f1d58c25 2522void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
571ecf67
JB
2523{
2524 struct ieee80211_local *local = hw_to_local(hw);
2525
2526 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2527
571ecf67
JB
2528 skb->pkt_type = IEEE80211_RX_MSG;
2529 skb_queue_tail(&local->skb_queue, skb);
2530 tasklet_schedule(&local->tasklet);
2531}
2532EXPORT_SYMBOL(ieee80211_rx_irqsafe);