]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/mac80211/rx.c
[SNMP]: Fix SNMP counters with PREEMPT
[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
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
d4e46a3d 16#include <linux/rcupdate.h>
571ecf67
JB
17#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
571ecf67
JB
22#include "wep.h"
23#include "wpa.h"
24#include "tkip.h"
25#include "wme.h"
26
b2e7771e
JB
27/*
28 * monitor mode reception
29 *
30 * This function cleans up the SKB, i.e. it removes all the stuff
31 * only useful for monitoring.
32 */
33static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
34 struct sk_buff *skb,
35 int rtap_len)
36{
37 skb_pull(skb, rtap_len);
38
39 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
40 if (likely(skb->len > FCS_LEN))
41 skb_trim(skb, skb->len - FCS_LEN);
42 else {
43 /* driver bug */
44 WARN_ON(1);
45 dev_kfree_skb(skb);
46 skb = NULL;
47 }
48 }
49
50 return skb;
51}
52
53static inline int should_drop_frame(struct ieee80211_rx_status *status,
54 struct sk_buff *skb,
55 int present_fcs_len,
56 int radiotap_len)
57{
58 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
59
60 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
61 return 1;
62 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
63 return 1;
64 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
65 cpu_to_le16(IEEE80211_FTYPE_CTL))
66 return 1;
67 return 0;
68}
69
70/*
71 * This function copies a received frame to all monitor interfaces and
72 * returns a cleaned-up SKB that no longer includes the FCS nor the
73 * radiotap header the driver might have added.
74 */
75static struct sk_buff *
76ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
77 struct ieee80211_rx_status *status)
78{
79 struct ieee80211_sub_if_data *sdata;
80 struct ieee80211_rate *rate;
81 int needed_headroom = 0;
c49e5ea3
JB
82 struct ieee80211_radiotap_header *rthdr;
83 __le64 *rttsft = NULL;
84 struct ieee80211_rtap_fixed_data {
b2e7771e
JB
85 u8 flags;
86 u8 rate;
87 __le16 chan_freq;
88 __le16 chan_flags;
89 u8 antsignal;
90 u8 padding_for_rxflags;
91 __le16 rx_flags;
c49e5ea3 92 } __attribute__ ((packed)) *rtfixed;
b2e7771e
JB
93 struct sk_buff *skb, *skb2;
94 struct net_device *prev_dev = NULL;
95 int present_fcs_len = 0;
96 int rtap_len = 0;
97
98 /*
99 * First, we may need to make a copy of the skb because
100 * (1) we need to modify it for radiotap (if not present), and
101 * (2) the other RX handlers will modify the skb we got.
102 *
103 * We don't need to, of course, if we aren't going to return
104 * the SKB because it has a bad FCS/PLCP checksum.
105 */
106 if (status->flag & RX_FLAG_RADIOTAP)
107 rtap_len = ieee80211_get_radiotap_len(origskb->data);
108 else
c49e5ea3
JB
109 /* room for radiotap header, always present fields and TSFT */
110 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
b2e7771e
JB
111
112 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
113 present_fcs_len = FCS_LEN;
114
115 if (!local->monitors) {
116 if (should_drop_frame(status, origskb, present_fcs_len,
117 rtap_len)) {
118 dev_kfree_skb(origskb);
119 return NULL;
120 }
121
122 return remove_monitor_info(local, origskb, rtap_len);
123 }
124
125 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
126 /* only need to expand headroom if necessary */
127 skb = origskb;
128 origskb = NULL;
129
130 /*
131 * This shouldn't trigger often because most devices have an
132 * RX header they pull before we get here, and that should
133 * be big enough for our radiotap information. We should
134 * probably export the length to drivers so that we can have
135 * them allocate enough headroom to start with.
136 */
137 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 138 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
139 dev_kfree_skb(skb);
140 return NULL;
141 }
142 } else {
143 /*
144 * Need to make a copy and possibly remove radiotap header
145 * and FCS from the original.
146 */
147 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
148
149 origskb = remove_monitor_info(local, origskb, rtap_len);
150
151 if (!skb)
152 return origskb;
153 }
154
155 /* if necessary, prepend radiotap information */
156 if (!(status->flag & RX_FLAG_RADIOTAP)) {
c49e5ea3
JB
157 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
158 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
159 if (status->flag & RX_FLAG_TSFT) {
160 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
161 rtap_len += 8;
162 }
b2e7771e
JB
163 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
164 memset(rthdr, 0, sizeof(*rthdr));
c49e5ea3
JB
165 memset(rtfixed, 0, sizeof(*rtfixed));
166 rthdr->it_present =
b2e7771e
JB
167 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
168 (1 << IEEE80211_RADIOTAP_RATE) |
169 (1 << IEEE80211_RADIOTAP_CHANNEL) |
170 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
171 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
c49e5ea3
JB
172 rtfixed->flags = 0;
173 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
174 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
175
176 if (rttsft) {
177 *rttsft = cpu_to_le64(status->mactime);
178 rthdr->it_present |=
179 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
180 }
b2e7771e
JB
181
182 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
c49e5ea3 183 rtfixed->rx_flags = 0;
b2e7771e
JB
184 if (status->flag &
185 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
c49e5ea3 186 rtfixed->rx_flags |=
b2e7771e
JB
187 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
188
189 rate = ieee80211_get_rate(local, status->phymode,
190 status->rate);
191 if (rate)
c49e5ea3 192 rtfixed->rate = rate->rate / 5;
b2e7771e 193
c49e5ea3 194 rtfixed->chan_freq = cpu_to_le16(status->freq);
b2e7771e
JB
195
196 if (status->phymode == MODE_IEEE80211A)
c49e5ea3 197 rtfixed->chan_flags =
b2e7771e
JB
198 cpu_to_le16(IEEE80211_CHAN_OFDM |
199 IEEE80211_CHAN_5GHZ);
200 else
c49e5ea3 201 rtfixed->chan_flags =
b2e7771e
JB
202 cpu_to_le16(IEEE80211_CHAN_DYN |
203 IEEE80211_CHAN_2GHZ);
204
c49e5ea3
JB
205 rtfixed->antsignal = status->ssi;
206 rthdr->it_len = cpu_to_le16(rtap_len);
b2e7771e
JB
207 }
208
209 skb_set_mac_header(skb, 0);
210 skb->ip_summed = CHECKSUM_UNNECESSARY;
211 skb->pkt_type = PACKET_OTHERHOST;
212 skb->protocol = htons(ETH_P_802_2);
213
214 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
215 if (!netif_running(sdata->dev))
216 continue;
217
218 if (sdata->type != IEEE80211_IF_TYPE_MNTR)
219 continue;
220
221 if (prev_dev) {
222 skb2 = skb_clone(skb, GFP_ATOMIC);
223 if (skb2) {
224 skb2->dev = prev_dev;
225 netif_rx(skb2);
226 }
227 }
228
229 prev_dev = sdata->dev;
230 sdata->dev->stats.rx_packets++;
231 sdata->dev->stats.rx_bytes += skb->len;
232 }
233
234 if (prev_dev) {
235 skb->dev = prev_dev;
236 netif_rx(skb);
237 } else
238 dev_kfree_skb(skb);
239
240 return origskb;
241}
242
243
571ecf67
JB
244/* pre-rx handlers
245 *
246 * these don't have dev/sdata fields in the rx data
52865dfd
JB
247 * The sta value should also not be used because it may
248 * be NULL even though a STA (in IBSS mode) will be added.
571ecf67
JB
249 */
250
6e0d114d
JB
251static ieee80211_txrx_result
252ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
253{
254 u8 *data = rx->skb->data;
255 int tid;
256
257 /* does the frame have a qos control field? */
258 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
259 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
260 /* frame has qos control */
261 tid = qc[0] & QOS_CONTROL_TID_MASK;
fd4c7f2f 262 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
64bd4b69 263 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
fd4c7f2f 264 else
64bd4b69 265 rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
6e0d114d
JB
266 } else {
267 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
268 /* Separate TID for management frames */
269 tid = NUM_RX_DATA_QUEUES - 1;
270 } else {
271 /* no qos control present */
272 tid = 0; /* 802.1d - Best Effort */
273 }
274 }
52865dfd 275
6e0d114d 276 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
52865dfd
JB
277 /* only a debug counter, sta might not be assigned properly yet */
278 if (rx->sta)
6e0d114d 279 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
6e0d114d
JB
280
281 rx->u.rx.queue = tid;
282 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
283 * For now, set skb->priority to 0 for other cases. */
284 rx->skb->priority = (tid > 7) ? 0 : tid;
285
286 return TXRX_CONTINUE;
287}
288
571ecf67
JB
289static ieee80211_txrx_result
290ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
291{
292 struct ieee80211_local *local = rx->local;
293 struct sk_buff *skb = rx->skb;
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
295 u32 load = 0, hdrtime;
296 struct ieee80211_rate *rate;
297 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
298 int i;
299
300 /* Estimate total channel use caused by this frame */
301
302 if (unlikely(mode->num_rates < 0))
303 return TXRX_CONTINUE;
304
305 rate = &mode->rates[0];
306 for (i = 0; i < mode->num_rates; i++) {
307 if (mode->rates[i].val == rx->u.rx.status->rate) {
308 rate = &mode->rates[i];
309 break;
310 }
311 }
312
313 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
314 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
315
316 if (mode->mode == MODE_IEEE80211A ||
571ecf67
JB
317 (mode->mode == MODE_IEEE80211G &&
318 rate->flags & IEEE80211_RATE_ERP))
319 hdrtime = CHAN_UTIL_HDR_SHORT;
320 else
321 hdrtime = CHAN_UTIL_HDR_LONG;
322
323 load = hdrtime;
324 if (!is_multicast_ether_addr(hdr->addr1))
325 load += hdrtime;
326
327 load += skb->len * rate->rate_inv;
328
329 /* Divide channel_use by 8 to avoid wrapping around the counter */
330 load >>= CHAN_UTIL_SHIFT;
331 local->channel_use_raw += load;
571ecf67
JB
332 rx->u.rx.load = load;
333
334 return TXRX_CONTINUE;
335}
336
337ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
338{
339 ieee80211_rx_h_parse_qos,
340 ieee80211_rx_h_load_stats,
341 NULL
342};
343
344/* rx handlers */
345
346static ieee80211_txrx_result
347ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
348{
52865dfd
JB
349 if (rx->sta)
350 rx->sta->channel_use_raw += rx->u.rx.load;
571ecf67
JB
351 rx->sdata->channel_use_raw += rx->u.rx.load;
352 return TXRX_CONTINUE;
353}
354
571ecf67
JB
355static ieee80211_txrx_result
356ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
357{
358 struct ieee80211_local *local = rx->local;
359 struct sk_buff *skb = rx->skb;
360
ece8eddd
ZY
361 if (unlikely(local->sta_hw_scanning))
362 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
363
364 if (unlikely(local->sta_sw_scanning)) {
365 /* drop all the other packets during a software scan anyway */
366 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
367 != TXRX_QUEUED)
368 dev_kfree_skb(skb);
571ecf67
JB
369 return TXRX_QUEUED;
370 }
371
badffb72 372 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
571ecf67
JB
373 /* scanning finished during invoking of handlers */
374 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
375 return TXRX_DROP;
376 }
377
378 return TXRX_CONTINUE;
379}
380
381static ieee80211_txrx_result
382ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
383{
384 struct ieee80211_hdr *hdr;
571ecf67
JB
385 hdr = (struct ieee80211_hdr *) rx->skb->data;
386
387 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
388 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
389 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
390 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
391 hdr->seq_ctrl)) {
badffb72 392 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
571ecf67
JB
393 rx->local->dot11FrameDuplicateCount++;
394 rx->sta->num_duplicates++;
395 }
396 return TXRX_DROP;
397 } else
398 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
399 }
400
571ecf67
JB
401 if (unlikely(rx->skb->len < 16)) {
402 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
403 return TXRX_DROP;
404 }
405
badffb72 406 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
407 rx->skb->pkt_type = PACKET_OTHERHOST;
408 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
409 rx->skb->pkt_type = PACKET_HOST;
410 else if (is_multicast_ether_addr(hdr->addr1)) {
411 if (is_broadcast_ether_addr(hdr->addr1))
412 rx->skb->pkt_type = PACKET_BROADCAST;
413 else
414 rx->skb->pkt_type = PACKET_MULTICAST;
415 } else
416 rx->skb->pkt_type = PACKET_OTHERHOST;
417
418 /* Drop disallowed frame classes based on STA auth/assoc state;
419 * IEEE 802.11, Chap 5.5.
420 *
421 * 80211.o does filtering only based on association state, i.e., it
422 * drops Class 3 frames from not associated stations. hostapd sends
423 * deauth/disassoc frames when needed. In addition, hostapd is
424 * responsible for filtering on both auth and assoc states.
425 */
426 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
427 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
428 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
429 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
430 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
431 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
432 !(rx->fc & IEEE80211_FCTL_TODS) &&
433 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
badffb72 434 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
571ecf67
JB
435 /* Drop IBSS frames and frames for other hosts
436 * silently. */
437 return TXRX_DROP;
438 }
439
f9d540ee 440 return TXRX_DROP;
571ecf67
JB
441 }
442
570bd537
JB
443 return TXRX_CONTINUE;
444}
445
446
447static ieee80211_txrx_result
1990af8d 448ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
570bd537
JB
449{
450 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3017b80b
JB
451 int keyidx;
452 int hdrlen;
e2f036da 453 ieee80211_txrx_result result = TXRX_DROP;
d4e46a3d 454 struct ieee80211_key *stakey = NULL;
570bd537 455
3017b80b
JB
456 /*
457 * Key selection 101
458 *
459 * There are three types of keys:
460 * - GTK (group keys)
461 * - PTK (pairwise keys)
462 * - STK (station-to-station pairwise keys)
463 *
464 * When selecting a key, we have to distinguish between multicast
465 * (including broadcast) and unicast frames, the latter can only
466 * use PTKs and STKs while the former always use GTKs. Unless, of
467 * course, actual WEP keys ("pre-RSNA") are used, then unicast
468 * frames can also use key indizes like GTKs. Hence, if we don't
469 * have a PTK/STK we check the key index for a WEP key.
470 *
8dc06a1c
JB
471 * Note that in a regular BSS, multicast frames are sent by the
472 * AP only, associated stations unicast the frame to the AP first
473 * which then multicasts it on their behalf.
474 *
3017b80b
JB
475 * There is also a slight problem in IBSS mode: GTKs are negotiated
476 * with each station, that is something we don't currently handle.
8dc06a1c
JB
477 * The spec seems to expect that one negotiates the same key with
478 * every station but there's no such requirement; VLANs could be
479 * possible.
3017b80b
JB
480 */
481
482 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
483 return TXRX_CONTINUE;
571ecf67 484
3017b80b 485 /*
1990af8d 486 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
487 * addressed to us nor a multicast frame.
488 */
badffb72 489 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
3017b80b
JB
490 return TXRX_CONTINUE;
491
d4e46a3d
JB
492 if (rx->sta)
493 stakey = rcu_dereference(rx->sta->key);
494
495 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
496 rx->key = stakey;
571ecf67 497 } else {
3017b80b
JB
498 /*
499 * The device doesn't give us the IV so we won't be
500 * able to look up the key. That's ok though, we
501 * don't need to decrypt the frame, we just won't
502 * be able to keep statistics accurate.
503 * Except for key threshold notifications, should
504 * we somehow allow the driver to tell us which key
505 * the hardware used if this flag is set?
506 */
7848ba7d
JB
507 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
508 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
3017b80b
JB
509 return TXRX_CONTINUE;
510
511 hdrlen = ieee80211_get_hdrlen(rx->fc);
512
513 if (rx->skb->len < 8 + hdrlen)
514 return TXRX_DROP; /* TODO: count this? */
515
516 /*
517 * no need to call ieee80211_wep_get_keyidx,
518 * it verifies a bunch of things we've done already
519 */
520 keyidx = rx->skb->data[hdrlen + 3] >> 6;
521
d4e46a3d 522 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
3017b80b
JB
523
524 /*
525 * RSNA-protected unicast frames should always be sent with
526 * pairwise or station-to-station keys, but for WEP we allow
527 * using a key index as well.
528 */
8f20fc24 529 if (rx->key && rx->key->conf.alg != ALG_WEP &&
3017b80b
JB
530 !is_multicast_ether_addr(hdr->addr1))
531 rx->key = NULL;
571ecf67
JB
532 }
533
3017b80b 534 if (rx->key) {
571ecf67 535 rx->key->tx_rx_count++;
011bfcc4 536 /* TODO: add threshold stuff again */
1990af8d 537 } else {
7f3ad894 538#ifdef CONFIG_MAC80211_DEBUG
70f08765
JB
539 if (net_ratelimit())
540 printk(KERN_DEBUG "%s: RX protected frame,"
541 " but have no key\n", rx->dev->name);
7f3ad894 542#endif /* CONFIG_MAC80211_DEBUG */
70f08765
JB
543 return TXRX_DROP;
544 }
545
1990af8d
JB
546 /* Check for weak IVs if possible */
547 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
548 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
549 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
550 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
551 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
552 rx->sta->wep_weak_iv_count++;
553
70f08765
JB
554 switch (rx->key->conf.alg) {
555 case ALG_WEP:
e2f036da
MN
556 result = ieee80211_crypto_wep_decrypt(rx);
557 break;
70f08765 558 case ALG_TKIP:
e2f036da
MN
559 result = ieee80211_crypto_tkip_decrypt(rx);
560 break;
70f08765 561 case ALG_CCMP:
e2f036da
MN
562 result = ieee80211_crypto_ccmp_decrypt(rx);
563 break;
70f08765
JB
564 }
565
e2f036da
MN
566 /* either the frame has been decrypted or will be dropped */
567 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
568
569 return result;
70f08765
JB
570}
571
571ecf67
JB
572static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
573{
574 struct ieee80211_sub_if_data *sdata;
0795af57
JP
575 DECLARE_MAC_BUF(mac);
576
571ecf67
JB
577 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
578
579 if (sdata->bss)
580 atomic_inc(&sdata->bss->num_sta_ps);
581 sta->flags |= WLAN_STA_PS;
582 sta->pspoll = 0;
583#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
584 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
585 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
586#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
587}
588
589static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
590{
591 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
592 struct sk_buff *skb;
593 int sent = 0;
594 struct ieee80211_sub_if_data *sdata;
595 struct ieee80211_tx_packet_data *pkt_data;
0795af57 596 DECLARE_MAC_BUF(mac);
571ecf67
JB
597
598 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
599 if (sdata->bss)
600 atomic_dec(&sdata->bss->num_sta_ps);
601 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
602 sta->pspoll = 0;
603 if (!skb_queue_empty(&sta->ps_tx_buf)) {
604 if (local->ops->set_tim)
605 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
606 if (sdata->bss)
607 bss_tim_clear(local, sdata->bss, sta->aid);
608 }
609#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
610 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
611 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
612#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
613 /* Send all buffered frames to the station */
614 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
615 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
616 sent++;
e8bf9649 617 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
618 dev_queue_xmit(skb);
619 }
620 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
621 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
622 local->total_ps_buffered--;
623 sent++;
624#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57 625 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
571ecf67 626 "since STA not sleeping anymore\n", dev->name,
0795af57 627 print_mac(mac, sta->addr), sta->aid);
571ecf67 628#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e8bf9649 629 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
630 dev_queue_xmit(skb);
631 }
632
633 return sent;
634}
635
636static ieee80211_txrx_result
637ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
638{
639 struct sta_info *sta = rx->sta;
640 struct net_device *dev = rx->dev;
641 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
642
643 if (!sta)
644 return TXRX_CONTINUE;
645
646 /* Update last_rx only for IBSS packets which are for the current
647 * BSSID to avoid keeping the current IBSS network alive in cases where
648 * other STAs are using different BSSID. */
649 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
650 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
651 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
652 sta->last_rx = jiffies;
653 } else
654 if (!is_multicast_ether_addr(hdr->addr1) ||
655 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
656 /* Update last_rx only for unicast frames in order to prevent
657 * the Probe Request frames (the only broadcast frames from a
658 * STA in infrastructure mode) from keeping a connection alive.
659 */
660 sta->last_rx = jiffies;
661 }
662
badffb72 663 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
664 return TXRX_CONTINUE;
665
666 sta->rx_fragments++;
667 sta->rx_bytes += rx->skb->len;
6c55aa97
LF
668 sta->last_rssi = rx->u.rx.status->ssi;
669 sta->last_signal = rx->u.rx.status->signal;
670 sta->last_noise = rx->u.rx.status->noise;
571ecf67
JB
671
672 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
673 /* Change STA power saving mode only in the end of a frame
674 * exchange sequence */
675 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
676 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
677 else if (!(sta->flags & WLAN_STA_PS) &&
678 (rx->fc & IEEE80211_FCTL_PM))
679 ap_sta_ps_start(dev, sta);
680 }
681
682 /* Drop data::nullfunc frames silently, since they are used only to
683 * control station power saving mode. */
684 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
685 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
686 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
687 /* Update counter and free packet here to avoid counting this
688 * as a dropped packed. */
689 sta->rx_packets++;
690 dev_kfree_skb(rx->skb);
691 return TXRX_QUEUED;
692 }
693
694 return TXRX_CONTINUE;
695} /* ieee80211_rx_h_sta_process */
696
571ecf67
JB
697static inline struct ieee80211_fragment_entry *
698ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
699 unsigned int frag, unsigned int seq, int rx_queue,
700 struct sk_buff **skb)
701{
702 struct ieee80211_fragment_entry *entry;
703 int idx;
704
705 idx = sdata->fragment_next;
706 entry = &sdata->fragments[sdata->fragment_next++];
707 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
708 sdata->fragment_next = 0;
709
710 if (!skb_queue_empty(&entry->skb_list)) {
711#ifdef CONFIG_MAC80211_DEBUG
712 struct ieee80211_hdr *hdr =
713 (struct ieee80211_hdr *) entry->skb_list.next->data;
0795af57
JP
714 DECLARE_MAC_BUF(mac);
715 DECLARE_MAC_BUF(mac2);
571ecf67
JB
716 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
717 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
0795af57 718 "addr1=%s addr2=%s\n",
571ecf67
JB
719 sdata->dev->name, idx,
720 jiffies - entry->first_frag_time, entry->seq,
0795af57
JP
721 entry->last_frag, print_mac(mac, hdr->addr1),
722 print_mac(mac2, hdr->addr2));
571ecf67
JB
723#endif /* CONFIG_MAC80211_DEBUG */
724 __skb_queue_purge(&entry->skb_list);
725 }
726
727 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
728 *skb = NULL;
729 entry->first_frag_time = jiffies;
730 entry->seq = seq;
731 entry->rx_queue = rx_queue;
732 entry->last_frag = frag;
733 entry->ccmp = 0;
734 entry->extra_len = 0;
735
736 return entry;
737}
738
739static inline struct ieee80211_fragment_entry *
740ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
741 u16 fc, unsigned int frag, unsigned int seq,
742 int rx_queue, struct ieee80211_hdr *hdr)
743{
744 struct ieee80211_fragment_entry *entry;
745 int i, idx;
746
747 idx = sdata->fragment_next;
748 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
749 struct ieee80211_hdr *f_hdr;
750 u16 f_fc;
751
752 idx--;
753 if (idx < 0)
754 idx = IEEE80211_FRAGMENT_MAX - 1;
755
756 entry = &sdata->fragments[idx];
757 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
758 entry->rx_queue != rx_queue ||
759 entry->last_frag + 1 != frag)
760 continue;
761
762 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
763 f_fc = le16_to_cpu(f_hdr->frame_control);
764
765 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
766 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
767 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
768 continue;
769
770 if (entry->first_frag_time + 2 * HZ < jiffies) {
771 __skb_queue_purge(&entry->skb_list);
772 continue;
773 }
774 return entry;
775 }
776
777 return NULL;
778}
779
780static ieee80211_txrx_result
781ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
782{
783 struct ieee80211_hdr *hdr;
784 u16 sc;
785 unsigned int frag, seq;
786 struct ieee80211_fragment_entry *entry;
787 struct sk_buff *skb;
0795af57 788 DECLARE_MAC_BUF(mac);
571ecf67
JB
789
790 hdr = (struct ieee80211_hdr *) rx->skb->data;
791 sc = le16_to_cpu(hdr->seq_ctrl);
792 frag = sc & IEEE80211_SCTL_FRAG;
793
794 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
795 (rx->skb)->len < 24 ||
796 is_multicast_ether_addr(hdr->addr1))) {
797 /* not fragmented */
798 goto out;
799 }
800 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
801
802 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
803
804 if (frag == 0) {
805 /* This is the first fragment of a new frame. */
806 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
807 rx->u.rx.queue, &(rx->skb));
8f20fc24 808 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
571ecf67
JB
809 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
810 /* Store CCMP PN so that we can verify that the next
811 * fragment has a sequential PN value. */
812 entry->ccmp = 1;
813 memcpy(entry->last_pn,
814 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
815 CCMP_PN_LEN);
816 }
817 return TXRX_QUEUED;
818 }
819
820 /* This is a fragment for a frame that should already be pending in
821 * fragment cache. Add this fragment to the end of the pending entry.
822 */
823 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
824 rx->u.rx.queue, hdr);
825 if (!entry) {
826 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
827 return TXRX_DROP;
828 }
829
830 /* Verify that MPDUs within one MSDU have sequential PN values.
831 * (IEEE 802.11i, 8.3.3.4.5) */
832 if (entry->ccmp) {
833 int i;
834 u8 pn[CCMP_PN_LEN], *rpn;
8f20fc24 835 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
571ecf67
JB
836 return TXRX_DROP;
837 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
838 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
839 pn[i]++;
840 if (pn[i])
841 break;
842 }
843 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
844 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
1a84f3fd
JB
845 if (net_ratelimit())
846 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
0795af57 847 "sequential A2=%s"
1a84f3fd
JB
848 " PN=%02x%02x%02x%02x%02x%02x "
849 "(expected %02x%02x%02x%02x%02x%02x)\n",
0795af57 850 rx->dev->name, print_mac(mac, hdr->addr2),
1a84f3fd
JB
851 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
852 rpn[5], pn[0], pn[1], pn[2], pn[3],
853 pn[4], pn[5]);
571ecf67
JB
854 return TXRX_DROP;
855 }
856 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
857 }
858
859 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
860 __skb_queue_tail(&entry->skb_list, rx->skb);
861 entry->last_frag = frag;
862 entry->extra_len += rx->skb->len;
863 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
864 rx->skb = NULL;
865 return TXRX_QUEUED;
866 }
867
868 rx->skb = __skb_dequeue(&entry->skb_list);
869 if (skb_tailroom(rx->skb) < entry->extra_len) {
870 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
871 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
872 GFP_ATOMIC))) {
873 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
874 __skb_queue_purge(&entry->skb_list);
875 return TXRX_DROP;
876 }
877 }
878 while ((skb = __skb_dequeue(&entry->skb_list))) {
879 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
880 dev_kfree_skb(skb);
881 }
882
883 /* Complete frame has been reassembled - process it now */
badffb72 884 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
571ecf67
JB
885
886 out:
887 if (rx->sta)
888 rx->sta->rx_packets++;
889 if (is_multicast_ether_addr(hdr->addr1))
890 rx->local->dot11MulticastReceivedFrameCount++;
891 else
892 ieee80211_led_rx(rx->local);
893 return TXRX_CONTINUE;
894}
895
896static ieee80211_txrx_result
897ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
898{
899 struct sk_buff *skb;
900 int no_pending_pkts;
0795af57 901 DECLARE_MAC_BUF(mac);
571ecf67
JB
902
903 if (likely(!rx->sta ||
904 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
905 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
badffb72 906 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
571ecf67
JB
907 return TXRX_CONTINUE;
908
909 skb = skb_dequeue(&rx->sta->tx_filtered);
910 if (!skb) {
911 skb = skb_dequeue(&rx->sta->ps_tx_buf);
912 if (skb)
913 rx->local->total_ps_buffered--;
914 }
915 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
916 skb_queue_empty(&rx->sta->ps_tx_buf);
917
918 if (skb) {
919 struct ieee80211_hdr *hdr =
920 (struct ieee80211_hdr *) skb->data;
921
922 /* tell TX path to send one frame even though the STA may
923 * still remain is PS mode after this frame exchange */
924 rx->sta->pspoll = 1;
925
926#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
927 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
928 print_mac(mac, rx->sta->addr), rx->sta->aid,
571ecf67
JB
929 skb_queue_len(&rx->sta->ps_tx_buf));
930#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
931
932 /* Use MoreData flag to indicate whether there are more
933 * buffered frames for this STA */
934 if (no_pending_pkts) {
935 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
936 rx->sta->flags &= ~WLAN_STA_TIM;
937 } else
938 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
939
940 dev_queue_xmit(skb);
941
942 if (no_pending_pkts) {
943 if (rx->local->ops->set_tim)
944 rx->local->ops->set_tim(local_to_hw(rx->local),
945 rx->sta->aid, 0);
946 if (rx->sdata->bss)
947 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
948 }
949#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
950 } else if (!rx->u.rx.sent_ps_buffered) {
0795af57 951 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
571ecf67 952 "though there is no buffered frames for it\n",
0795af57 953 rx->dev->name, print_mac(mac, rx->sta->addr));
571ecf67
JB
954#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
955
956 }
957
958 /* Free PS Poll skb here instead of returning TXRX_DROP that would
959 * count as an dropped frame. */
960 dev_kfree_skb(rx->skb);
961
962 return TXRX_QUEUED;
963}
964
6e0d114d
JB
965static ieee80211_txrx_result
966ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
967{
968 u16 fc = rx->fc;
969 u8 *data = rx->skb->data;
970 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
971
972 if (!WLAN_FC_IS_QOS_DATA(fc))
973 return TXRX_CONTINUE;
974
975 /* remove the qos control field, update frame type and meta-data */
976 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
977 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
978 /* change frame type to non QOS */
979 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
980 hdr->frame_control = cpu_to_le16(fc);
981
982 return TXRX_CONTINUE;
983}
984
76ee65bf
RR
985static int
986ieee80211_drop_802_1x_pae(struct ieee80211_txrx_data *rx, int hdrlen)
571ecf67 987{
76ee65bf 988 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb, hdrlen) &&
badffb72 989 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
f9d540ee 990 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
76ee65bf 991 return 0;
571ecf67
JB
992
993 if (unlikely(rx->sdata->ieee802_1x &&
994 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
995 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
996 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
76ee65bf 997 !ieee80211_is_eapol(rx->skb, hdrlen))) {
571ecf67 998#ifdef CONFIG_MAC80211_DEBUG
76ee65bf
RR
999 printk(KERN_DEBUG "%s: dropped frame "
1000 "(unauthorized port)\n", rx->dev->name);
571ecf67 1001#endif /* CONFIG_MAC80211_DEBUG */
76ee65bf 1002 return -EACCES;
571ecf67
JB
1003 }
1004
76ee65bf 1005 return 0;
571ecf67
JB
1006}
1007
76ee65bf
RR
1008static int
1009ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx, int hdrlen)
571ecf67 1010{
3017b80b 1011 /*
7848ba7d
JB
1012 * Pass through unencrypted frames if the hardware has
1013 * decrypted them already.
3017b80b 1014 */
7848ba7d 1015 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1016 return 0;
571ecf67
JB
1017
1018 /* Drop unencrypted frames if key is set. */
1019 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1020 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1021 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
8312512e 1022 (rx->key || rx->sdata->drop_unencrypted) &&
76ee65bf
RR
1023 (rx->sdata->eapol == 0 ||
1024 !ieee80211_is_eapol(rx->skb, hdrlen)))) {
1a84f3fd
JB
1025 if (net_ratelimit())
1026 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1027 "encryption\n", rx->dev->name);
76ee65bf 1028 return -EACCES;
571ecf67 1029 }
76ee65bf 1030 return 0;
571ecf67
JB
1031}
1032
76ee65bf
RR
1033static int
1034ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
571ecf67
JB
1035{
1036 struct net_device *dev = rx->dev;
571ecf67
JB
1037 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1038 u16 fc, hdrlen, ethertype;
1039 u8 *payload;
1040 u8 dst[ETH_ALEN];
1041 u8 src[ETH_ALEN];
76ee65bf 1042 struct sk_buff *skb = rx->skb;
571ecf67 1043 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0795af57
JP
1044 DECLARE_MAC_BUF(mac);
1045 DECLARE_MAC_BUF(mac2);
1046 DECLARE_MAC_BUF(mac3);
1047 DECLARE_MAC_BUF(mac4);
571ecf67
JB
1048
1049 fc = rx->fc;
571ecf67
JB
1050
1051 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
76ee65bf 1052 return -1;
571ecf67
JB
1053
1054 hdrlen = ieee80211_get_hdrlen(fc);
1055
1056 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1057 * header
1058 * IEEE 802.11 address fields:
1059 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1060 * 0 0 DA SA BSSID n/a
1061 * 0 1 DA BSSID SA n/a
1062 * 1 0 BSSID SA DA n/a
1063 * 1 1 RA TA DA SA
1064 */
1065
1066 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1067 case IEEE80211_FCTL_TODS:
1068 /* BSSID SA DA */
1069 memcpy(dst, hdr->addr3, ETH_ALEN);
1070 memcpy(src, hdr->addr2, ETH_ALEN);
1071
1072 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1073 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
1a84f3fd
JB
1074 if (net_ratelimit())
1075 printk(KERN_DEBUG "%s: dropped ToDS frame "
0795af57 1076 "(BSSID=%s SA=%s DA=%s)\n",
1a84f3fd 1077 dev->name,
0795af57
JP
1078 print_mac(mac, hdr->addr1),
1079 print_mac(mac2, hdr->addr2),
1080 print_mac(mac3, hdr->addr3));
76ee65bf 1081 return -1;
571ecf67
JB
1082 }
1083 break;
1084 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1085 /* RA TA DA SA */
1086 memcpy(dst, hdr->addr3, ETH_ALEN);
1087 memcpy(src, hdr->addr4, ETH_ALEN);
1088
1089 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
1a84f3fd
JB
1090 if (net_ratelimit())
1091 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
0795af57 1092 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1a84f3fd 1093 rx->dev->name,
0795af57
JP
1094 print_mac(mac, hdr->addr1),
1095 print_mac(mac2, hdr->addr2),
1096 print_mac(mac3, hdr->addr3),
1097 print_mac(mac4, hdr->addr4));
76ee65bf 1098 return -1;
571ecf67
JB
1099 }
1100 break;
1101 case IEEE80211_FCTL_FROMDS:
1102 /* DA BSSID SA */
1103 memcpy(dst, hdr->addr1, ETH_ALEN);
1104 memcpy(src, hdr->addr3, ETH_ALEN);
1105
b3316157
JL
1106 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1107 (is_multicast_ether_addr(dst) &&
1108 !compare_ether_addr(src, dev->dev_addr)))
76ee65bf 1109 return -1;
571ecf67
JB
1110 break;
1111 case 0:
1112 /* DA SA BSSID */
1113 memcpy(dst, hdr->addr1, ETH_ALEN);
1114 memcpy(src, hdr->addr2, ETH_ALEN);
1115
1116 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1117 if (net_ratelimit()) {
0795af57
JP
1118 printk(KERN_DEBUG "%s: dropped IBSS frame "
1119 "(DA=%s SA=%s BSSID=%s)\n",
1120 dev->name,
1121 print_mac(mac, hdr->addr1),
1122 print_mac(mac2, hdr->addr2),
1123 print_mac(mac3, hdr->addr3));
571ecf67 1124 }
76ee65bf 1125 return -1;
571ecf67
JB
1126 }
1127 break;
1128 }
1129
571ecf67
JB
1130 if (unlikely(skb->len - hdrlen < 8)) {
1131 if (net_ratelimit()) {
1132 printk(KERN_DEBUG "%s: RX too short data frame "
1133 "payload\n", dev->name);
1134 }
76ee65bf 1135 return -1;
571ecf67
JB
1136 }
1137
76ee65bf 1138 payload = skb->data + hdrlen;
571ecf67
JB
1139 ethertype = (payload[6] << 8) | payload[7];
1140
1141 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1142 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1143 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1144 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1145 * replace EtherType */
1146 skb_pull(skb, hdrlen + 6);
1147 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1148 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1149 } else {
1150 struct ethhdr *ehdr;
1151 __be16 len;
1152 skb_pull(skb, hdrlen);
1153 len = htons(skb->len);
1154 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1155 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1156 memcpy(ehdr->h_source, src, ETH_ALEN);
1157 ehdr->h_proto = len;
1158 }
76ee65bf
RR
1159 return 0;
1160}
571ecf67 1161
76ee65bf
RR
1162static void
1163ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1164{
1165 struct net_device *dev = rx->dev;
1166 struct ieee80211_local *local = rx->local;
1167 struct sk_buff *skb, *xmit_skb;
1168 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
571ecf67 1169
76ee65bf
RR
1170 skb = rx->skb;
1171 xmit_skb = NULL;
571ecf67
JB
1172
1173 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
badffb72
JS
1174 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1175 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
571ecf67
JB
1176 if (is_multicast_ether_addr(skb->data)) {
1177 /* send multicast frames both to higher layers in
1178 * local net stack and back to the wireless media */
76ee65bf
RR
1179 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1180 if (!xmit_skb && net_ratelimit())
571ecf67
JB
1181 printk(KERN_DEBUG "%s: failed to clone "
1182 "multicast frame\n", dev->name);
1183 } else {
1184 struct sta_info *dsta;
1185 dsta = sta_info_get(local, skb->data);
1186 if (dsta && !dsta->dev) {
1a84f3fd
JB
1187 if (net_ratelimit())
1188 printk(KERN_DEBUG "Station with null "
1189 "dev structure!\n");
571ecf67
JB
1190 } else if (dsta && dsta->dev == dev) {
1191 /* Destination station is associated to this
1192 * AP, so send the frame directly to it and
1193 * do not pass the frame to local net stack.
1194 */
76ee65bf 1195 xmit_skb = skb;
571ecf67
JB
1196 skb = NULL;
1197 }
1198 if (dsta)
1199 sta_info_put(dsta);
1200 }
1201 }
1202
1203 if (skb) {
1204 /* deliver to local stack */
1205 skb->protocol = eth_type_trans(skb, dev);
1206 memset(skb->cb, 0, sizeof(skb->cb));
1207 netif_rx(skb);
1208 }
1209
76ee65bf 1210 if (xmit_skb) {
571ecf67 1211 /* send to wireless media */
f831e909 1212 xmit_skb->protocol = htons(ETH_P_802_3);
76ee65bf
RR
1213 skb_set_network_header(xmit_skb, 0);
1214 skb_set_mac_header(xmit_skb, 0);
1215 dev_queue_xmit(xmit_skb);
571ecf67 1216 }
76ee65bf
RR
1217}
1218
fd4c7f2f
RR
1219static ieee80211_txrx_result
1220ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1221{
1222 struct net_device *dev = rx->dev;
1223 struct ieee80211_local *local = rx->local;
1224 u16 fc, ethertype;
1225 u8 *payload;
1226 struct sk_buff *skb = rx->skb, *frame = NULL;
1227 const struct ethhdr *eth;
1228 int remaining, err;
1229 u8 dst[ETH_ALEN];
1230 u8 src[ETH_ALEN];
1231 DECLARE_MAC_BUF(mac);
1232
1233 fc = rx->fc;
1234 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1235 return TXRX_CONTINUE;
1236
1237 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1238 return TXRX_DROP;
1239
64bd4b69 1240 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
fd4c7f2f
RR
1241 return TXRX_CONTINUE;
1242
1243 err = ieee80211_data_to_8023(rx);
1244 if (unlikely(err))
1245 return TXRX_DROP;
1246
1247 skb->dev = dev;
1248
1249 dev->stats.rx_packets++;
1250 dev->stats.rx_bytes += skb->len;
1251
1252 /* skip the wrapping header */
1253 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1254 if (!eth)
1255 return TXRX_DROP;
1256
1257 while (skb != frame) {
1258 u8 padding;
1259 __be16 len = eth->h_proto;
1260 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1261
1262 remaining = skb->len;
1263 memcpy(dst, eth->h_dest, ETH_ALEN);
1264 memcpy(src, eth->h_source, ETH_ALEN);
1265
1266 padding = ((4 - subframe_len) & 0x3);
1267 /* the last MSDU has no padding */
1268 if (subframe_len > remaining) {
1269 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1270 return TXRX_DROP;
1271 }
1272
1273 skb_pull(skb, sizeof(struct ethhdr));
1274 /* if last subframe reuse skb */
1275 if (remaining <= subframe_len + padding)
1276 frame = skb;
1277 else {
1278 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1279 subframe_len);
1280
1281 if (frame == NULL)
1282 return TXRX_DROP;
1283
1284 skb_reserve(frame, local->hw.extra_tx_headroom +
1285 sizeof(struct ethhdr));
1286 memcpy(skb_put(frame, ntohs(len)), skb->data,
1287 ntohs(len));
1288
1289 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1290 padding);
1291 if (!eth) {
1292 printk(KERN_DEBUG "%s: wrong buffer size ",
1293 dev->name);
1294 dev_kfree_skb(frame);
1295 return TXRX_DROP;
1296 }
1297 }
1298
1299 skb_set_network_header(frame, 0);
1300 frame->dev = dev;
1301 frame->priority = skb->priority;
1302 rx->skb = frame;
1303
1304 if ((ieee80211_drop_802_1x_pae(rx, 0)) ||
1305 (ieee80211_drop_unencrypted(rx, 0))) {
1306 if (skb == frame) /* last frame */
1307 return TXRX_DROP;
1308 dev_kfree_skb(frame);
1309 continue;
1310 }
1311
1312 payload = frame->data;
1313 ethertype = (payload[6] << 8) | payload[7];
1314
1315 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1316 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1317 compare_ether_addr(payload,
1318 bridge_tunnel_header) == 0)) {
1319 /* remove RFC1042 or Bridge-Tunnel
1320 * encapsulation and replace EtherType */
1321 skb_pull(frame, 6);
1322 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1323 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1324 } else {
1325 memcpy(skb_push(frame, sizeof(__be16)), &len,
1326 sizeof(__be16));
1327 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1328 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1329 }
1330
1331
1332 ieee80211_deliver_skb(rx);
1333 }
1334
1335 return TXRX_QUEUED;
1336}
1337
76ee65bf
RR
1338static ieee80211_txrx_result
1339ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1340{
1341 struct net_device *dev = rx->dev;
1342 u16 fc;
1343 int err, hdrlen;
1344
1345 fc = rx->fc;
1346 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1347 return TXRX_CONTINUE;
1348
1349 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1350 return TXRX_DROP;
1351
1352 hdrlen = ieee80211_get_hdrlen(fc);
1353
1354 if ((ieee80211_drop_802_1x_pae(rx, hdrlen)) ||
1355 (ieee80211_drop_unencrypted(rx, hdrlen)))
1356 return TXRX_DROP;
1357
1358 err = ieee80211_data_to_8023(rx);
1359 if (unlikely(err))
1360 return TXRX_DROP;
1361
1362 rx->skb->dev = dev;
1363
1364 dev->stats.rx_packets++;
1365 dev->stats.rx_bytes += rx->skb->len;
1366
1367 ieee80211_deliver_skb(rx);
571ecf67
JB
1368
1369 return TXRX_QUEUED;
1370}
1371
1372static ieee80211_txrx_result
1373ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1374{
1375 struct ieee80211_sub_if_data *sdata;
1376
badffb72 1377 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
1378 return TXRX_DROP;
1379
1380 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1381 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1382 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
ddd3d2be 1383 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
571ecf67 1384 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
f9d540ee
JB
1385 else
1386 return TXRX_DROP;
1387
571ecf67
JB
1388 return TXRX_QUEUED;
1389}
1390
1391static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1392 struct ieee80211_local *local,
1393 ieee80211_rx_handler *handlers,
1394 struct ieee80211_txrx_data *rx,
1395 struct sta_info *sta)
1396{
1397 ieee80211_rx_handler *handler;
1398 ieee80211_txrx_result res = TXRX_DROP;
1399
1400 for (handler = handlers; *handler != NULL; handler++) {
1401 res = (*handler)(rx);
8e6f0032
JB
1402
1403 switch (res) {
1404 case TXRX_CONTINUE:
1405 continue;
1406 case TXRX_DROP:
1407 I802_DEBUG_INC(local->rx_handlers_drop);
1408 if (sta)
1409 sta->rx_dropped++;
1410 break;
1411 case TXRX_QUEUED:
1412 I802_DEBUG_INC(local->rx_handlers_queued);
571ecf67
JB
1413 break;
1414 }
8e6f0032 1415 break;
571ecf67
JB
1416 }
1417
8e6f0032 1418 if (res == TXRX_DROP)
571ecf67 1419 dev_kfree_skb(rx->skb);
571ecf67
JB
1420 return res;
1421}
1422
1423static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1424 ieee80211_rx_handler *handlers,
1425 struct ieee80211_txrx_data *rx,
1426 struct sta_info *sta)
1427{
1428 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1429 TXRX_CONTINUE)
1430 dev_kfree_skb(rx->skb);
1431}
1432
1433static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1434 struct ieee80211_hdr *hdr,
1435 struct sta_info *sta,
1436 struct ieee80211_txrx_data *rx)
1437{
1438 int keyidx, hdrlen;
0795af57
JP
1439 DECLARE_MAC_BUF(mac);
1440 DECLARE_MAC_BUF(mac2);
571ecf67
JB
1441
1442 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1443 if (rx->skb->len >= hdrlen + 4)
1444 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1445 else
1446 keyidx = -1;
1447
1a84f3fd
JB
1448 if (net_ratelimit())
1449 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
0795af57
JP
1450 "failure from %s to %s keyidx=%d\n",
1451 dev->name, print_mac(mac, hdr->addr2),
1452 print_mac(mac2, hdr->addr1), keyidx);
571ecf67
JB
1453
1454 if (!sta) {
aa0daf0e
JB
1455 /*
1456 * Some hardware seem to generate incorrect Michael MIC
1457 * reports; ignore them to avoid triggering countermeasures.
1458 */
1a84f3fd
JB
1459 if (net_ratelimit())
1460 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
0795af57
JP
1461 "error for unknown address %s\n",
1462 dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1463 goto ignore;
1464 }
1465
1466 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1a84f3fd
JB
1467 if (net_ratelimit())
1468 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
aa0daf0e 1469 "error for a frame with no PROTECTED flag (src "
0795af57 1470 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1471 goto ignore;
1472 }
1473
7848ba7d 1474 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
aa0daf0e
JB
1475 /*
1476 * APs with pairwise keys should never receive Michael MIC
1477 * errors for non-zero keyidx because these are reserved for
1478 * group keys and only the AP is sending real multicast
1479 * frames in the BSS.
1480 */
eb063c17
JB
1481 if (net_ratelimit())
1482 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1483 "a frame with non-zero keyidx (%d)"
0795af57
JP
1484 " (src %s)\n", dev->name, keyidx,
1485 print_mac(mac, hdr->addr2));
eb063c17 1486 goto ignore;
571ecf67
JB
1487 }
1488
1489 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1490 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1491 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1a84f3fd
JB
1492 if (net_ratelimit())
1493 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1494 "error for a frame that cannot be encrypted "
0795af57
JP
1495 "(fc=0x%04x) (src %s)\n",
1496 dev->name, rx->fc, print_mac(mac, hdr->addr2));
571ecf67
JB
1497 goto ignore;
1498 }
1499
eb063c17 1500 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
571ecf67
JB
1501 ignore:
1502 dev_kfree_skb(rx->skb);
1503 rx->skb = NULL;
1504}
1505
1506ieee80211_rx_handler ieee80211_rx_handlers[] =
1507{
1508 ieee80211_rx_h_if_stats,
571ecf67
JB
1509 ieee80211_rx_h_passive_scan,
1510 ieee80211_rx_h_check,
4f0d18e2 1511 ieee80211_rx_h_decrypt,
70f08765 1512 ieee80211_rx_h_sta_process,
571ecf67
JB
1513 ieee80211_rx_h_defragment,
1514 ieee80211_rx_h_ps_poll,
1515 ieee80211_rx_h_michael_mic_verify,
1516 /* this must be after decryption - so header is counted in MPDU mic
1517 * must be before pae and data, so QOS_DATA format frames
1518 * are not passed to user space by these functions
1519 */
1520 ieee80211_rx_h_remove_qos_control,
fd4c7f2f 1521 ieee80211_rx_h_amsdu,
571ecf67
JB
1522 ieee80211_rx_h_data,
1523 ieee80211_rx_h_mgmt,
1524 NULL
1525};
1526
1527/* main receive path */
1528
23a24def
JB
1529static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1530 u8 *bssid, struct ieee80211_txrx_data *rx,
1531 struct ieee80211_hdr *hdr)
1532{
1533 int multicast = is_multicast_ether_addr(hdr->addr1);
1534
1535 switch (sdata->type) {
1536 case IEEE80211_IF_TYPE_STA:
1537 if (!bssid)
1538 return 0;
1539 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
badffb72 1540 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1541 return 0;
badffb72 1542 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1543 } else if (!multicast &&
1544 compare_ether_addr(sdata->dev->dev_addr,
1545 hdr->addr1) != 0) {
4150c572 1546 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1547 return 0;
badffb72 1548 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1549 }
1550 break;
1551 case IEEE80211_IF_TYPE_IBSS:
1552 if (!bssid)
1553 return 0;
1554 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
badffb72 1555 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1556 return 0;
badffb72 1557 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1558 } else if (!multicast &&
1559 compare_ether_addr(sdata->dev->dev_addr,
1560 hdr->addr1) != 0) {
4150c572 1561 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1562 return 0;
badffb72 1563 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1564 } else if (!rx->sta)
1565 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1566 bssid, hdr->addr2);
1567 break;
fb1c1cd6 1568 case IEEE80211_IF_TYPE_VLAN:
23a24def
JB
1569 case IEEE80211_IF_TYPE_AP:
1570 if (!bssid) {
1571 if (compare_ether_addr(sdata->dev->dev_addr,
1572 hdr->addr1))
1573 return 0;
1574 } else if (!ieee80211_bssid_match(bssid,
1575 sdata->dev->dev_addr)) {
badffb72 1576 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1577 return 0;
badffb72 1578 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def 1579 }
badffb72
JS
1580 if (sdata->dev == sdata->local->mdev &&
1581 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def
JB
1582 /* do not receive anything via
1583 * master device when not scanning */
1584 return 0;
1585 break;
1586 case IEEE80211_IF_TYPE_WDS:
1587 if (bssid ||
1588 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1589 return 0;
1590 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1591 return 0;
1592 break;
fb1c1cd6
JB
1593 case IEEE80211_IF_TYPE_MNTR:
1594 /* take everything */
1595 break;
a2897552 1596 case IEEE80211_IF_TYPE_INVALID:
fb1c1cd6
JB
1597 /* should never get here */
1598 WARN_ON(1);
1599 break;
23a24def
JB
1600 }
1601
1602 return 1;
1603}
1604
571ecf67
JB
1605/*
1606 * This is the receive path handler. It is called by a low level driver when an
1607 * 802.11 MPDU is received from the hardware.
1608 */
1609void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1610 struct ieee80211_rx_status *status)
1611{
1612 struct ieee80211_local *local = hw_to_local(hw);
1613 struct ieee80211_sub_if_data *sdata;
1614 struct sta_info *sta;
1615 struct ieee80211_hdr *hdr;
1616 struct ieee80211_txrx_data rx;
1617 u16 type;
b2e7771e 1618 int prepres;
8e6f0032
JB
1619 struct ieee80211_sub_if_data *prev = NULL;
1620 struct sk_buff *skb_new;
1621 u8 *bssid;
d10f2150 1622 int hdrlen;
571ecf67 1623
d4e46a3d 1624 /*
79010420
JB
1625 * key references and virtual interfaces are protected using RCU
1626 * and this requires that we are in a read-side RCU section during
1627 * receive processing
d4e46a3d
JB
1628 */
1629 rcu_read_lock();
1630
b2e7771e
JB
1631 /*
1632 * Frames with failed FCS/PLCP checksum are not returned,
1633 * all other frames are returned without radiotap header
1634 * if it was previously present.
1635 * Also, frames with less than 16 bytes are dropped.
1636 */
1637 skb = ieee80211_rx_monitor(local, skb, status);
1638 if (!skb) {
1639 rcu_read_unlock();
1640 return;
1641 }
1642
571ecf67
JB
1643 hdr = (struct ieee80211_hdr *) skb->data;
1644 memset(&rx, 0, sizeof(rx));
1645 rx.skb = skb;
1646 rx.local = local;
1647
1648 rx.u.rx.status = status;
b2e7771e 1649 rx.fc = le16_to_cpu(hdr->frame_control);
571ecf67 1650 type = rx.fc & IEEE80211_FCTL_FTYPE;
72abd81b 1651
d10f2150
DM
1652 /*
1653 * Drivers are required to align the payload data to a four-byte
1654 * boundary, so the last two bits of the address where it starts
1655 * may not be set. The header is required to be directly before
1656 * the payload data, padding like atheros hardware adds which is
1657 * inbetween the 802.11 header and the payload is not supported,
1658 * the driver is required to move the 802.11 header further back
1659 * in that case.
1660 */
1661 hdrlen = ieee80211_get_hdrlen(rx.fc);
1662 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1663
b2e7771e 1664 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
571ecf67 1665 local->dot11ReceivedFragmentCount++;
571ecf67 1666
b2e7771e
JB
1667 sta = rx.sta = sta_info_get(local, hdr->addr2);
1668 if (sta) {
1669 rx.dev = rx.sta->dev;
1670 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1671 }
571ecf67 1672
571ecf67
JB
1673 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1674 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1675 goto end;
1676 }
1677
ece8eddd 1678 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
badffb72 1679 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
571ecf67
JB
1680
1681 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1682 sta) != TXRX_CONTINUE)
1683 goto end;
1684 skb = rx.skb;
1685
c9ee23df 1686 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
53918994
JB
1687 !atomic_read(&local->iff_promiscs) &&
1688 !is_multicast_ether_addr(hdr->addr1)) {
badffb72 1689 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
571ecf67 1690 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
23a24def 1691 rx.sta);
8e6f0032 1692 sta_info_put(sta);
d4e46a3d 1693 rcu_read_unlock();
8e6f0032
JB
1694 return;
1695 }
1696
b2e7771e 1697 bssid = ieee80211_get_bssid(hdr, skb->len);
8e6f0032 1698
79010420 1699 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2a8a9a88
JB
1700 if (!netif_running(sdata->dev))
1701 continue;
1702
b2e7771e
JB
1703 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1704 continue;
1705
1706 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1707 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1708 /* prepare_for_handlers can change sta */
1709 sta = rx.sta;
1710
1711 if (!prepres)
1712 continue;
8e6f0032 1713
340e11f3
JB
1714 /*
1715 * frame is destined for this interface, but if it's not
1716 * also for the previous one we handle that after the
1717 * loop to avoid copying the SKB once too much
1718 */
1719
1720 if (!prev) {
1721 prev = sdata;
1722 continue;
8e6f0032 1723 }
340e11f3
JB
1724
1725 /*
1726 * frame was destined for the previous interface
1727 * so invoke RX handlers for it
1728 */
1729
1730 skb_new = skb_copy(skb, GFP_ATOMIC);
1731 if (!skb_new) {
1732 if (net_ratelimit())
1733 printk(KERN_DEBUG "%s: failed to copy "
1734 "multicast frame for %s",
dd1cd4c6
JB
1735 wiphy_name(local->hw.wiphy),
1736 prev->dev->name);
340e11f3
JB
1737 continue;
1738 }
1739 rx.skb = skb_new;
1740 rx.dev = prev->dev;
1741 rx.sdata = prev;
1742 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1743 &rx, sta);
8e6f0032 1744 prev = sdata;
571ecf67 1745 }
8e6f0032
JB
1746 if (prev) {
1747 rx.skb = skb;
1748 rx.dev = prev->dev;
1749 rx.sdata = prev;
1750 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1751 &rx, sta);
1752 } else
1753 dev_kfree_skb(skb);
571ecf67 1754
8e6f0032 1755 end:
d4e46a3d
JB
1756 rcu_read_unlock();
1757
571ecf67
JB
1758 if (sta)
1759 sta_info_put(sta);
1760}
1761EXPORT_SYMBOL(__ieee80211_rx);
1762
1763/* This is a version of the rx handler that can be called from hard irq
1764 * context. Post the skb on the queue and schedule the tasklet */
1765void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1766 struct ieee80211_rx_status *status)
1767{
1768 struct ieee80211_local *local = hw_to_local(hw);
1769
1770 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1771
1772 skb->dev = local->mdev;
1773 /* copy status into skb->cb for use by tasklet */
1774 memcpy(skb->cb, status, sizeof(*status));
1775 skb->pkt_type = IEEE80211_RX_MSG;
1776 skb_queue_tail(&local->skb_queue, skb);
1777 tasklet_schedule(&local->tasklet);
1778}
1779EXPORT_SYMBOL(ieee80211_rx_irqsafe);