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