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