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