]> err.no Git - linux-2.6/blob - net/mac80211/rx.c
mac80211: Restore rx.fc before every invocation of ieee80211_invoke_rx_handlers
[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 static ieee80211_txrx_result
292 ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
293 {
294         struct ieee80211_local *local = rx->local;
295         struct sk_buff *skb = rx->skb;
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 == rx->u.rx.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         local->channel_use_raw += load;
334         rx->u.rx.load = load;
335
336         return TXRX_CONTINUE;
337 }
338
339 ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
340 {
341         ieee80211_rx_h_parse_qos,
342         ieee80211_rx_h_load_stats,
343         NULL
344 };
345
346 /* rx handlers */
347
348 static ieee80211_txrx_result
349 ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
350 {
351         if (rx->sta)
352                 rx->sta->channel_use_raw += rx->u.rx.load;
353         rx->sdata->channel_use_raw += rx->u.rx.load;
354         return TXRX_CONTINUE;
355 }
356
357 static ieee80211_txrx_result
358 ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
359 {
360         struct ieee80211_local *local = rx->local;
361         struct sk_buff *skb = rx->skb;
362
363         if (unlikely(local->sta_hw_scanning))
364                 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
365
366         if (unlikely(local->sta_sw_scanning)) {
367                 /* drop all the other packets during a software scan anyway */
368                 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
369                     != TXRX_QUEUED)
370                         dev_kfree_skb(skb);
371                 return TXRX_QUEUED;
372         }
373
374         if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
375                 /* scanning finished during invoking of handlers */
376                 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
377                 return TXRX_DROP;
378         }
379
380         return TXRX_CONTINUE;
381 }
382
383 static ieee80211_txrx_result
384 ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
385 {
386         struct ieee80211_hdr *hdr;
387         hdr = (struct ieee80211_hdr *) rx->skb->data;
388
389         /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
390         if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
391                 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
392                              rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
393                              hdr->seq_ctrl)) {
394                         if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
395                                 rx->local->dot11FrameDuplicateCount++;
396                                 rx->sta->num_duplicates++;
397                         }
398                         return TXRX_DROP;
399                 } else
400                         rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
401         }
402
403         if (unlikely(rx->skb->len < 16)) {
404                 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
405                 return TXRX_DROP;
406         }
407
408         /* Drop disallowed frame classes based on STA auth/assoc state;
409          * IEEE 802.11, Chap 5.5.
410          *
411          * 80211.o does filtering only based on association state, i.e., it
412          * drops Class 3 frames from not associated stations. hostapd sends
413          * deauth/disassoc frames when needed. In addition, hostapd is
414          * responsible for filtering on both auth and assoc states.
415          */
416         if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
417                       ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
418                        (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
419                      rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
420                      (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
421                 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
422                      !(rx->fc & IEEE80211_FCTL_TODS) &&
423                      (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
424                     || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
425                         /* Drop IBSS frames and frames for other hosts
426                          * silently. */
427                         return TXRX_DROP;
428                 }
429
430                 return TXRX_DROP;
431         }
432
433         return TXRX_CONTINUE;
434 }
435
436
437 static ieee80211_txrx_result
438 ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
439 {
440         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
441         int keyidx;
442         int hdrlen;
443         ieee80211_txrx_result result = TXRX_DROP;
444         struct ieee80211_key *stakey = NULL;
445
446         /*
447          * Key selection 101
448          *
449          * There are three types of keys:
450          *  - GTK (group keys)
451          *  - PTK (pairwise keys)
452          *  - STK (station-to-station pairwise keys)
453          *
454          * When selecting a key, we have to distinguish between multicast
455          * (including broadcast) and unicast frames, the latter can only
456          * use PTKs and STKs while the former always use GTKs. Unless, of
457          * course, actual WEP keys ("pre-RSNA") are used, then unicast
458          * frames can also use key indizes like GTKs. Hence, if we don't
459          * have a PTK/STK we check the key index for a WEP key.
460          *
461          * Note that in a regular BSS, multicast frames are sent by the
462          * AP only, associated stations unicast the frame to the AP first
463          * which then multicasts it on their behalf.
464          *
465          * There is also a slight problem in IBSS mode: GTKs are negotiated
466          * with each station, that is something we don't currently handle.
467          * The spec seems to expect that one negotiates the same key with
468          * every station but there's no such requirement; VLANs could be
469          * possible.
470          */
471
472         if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
473                 return TXRX_CONTINUE;
474
475         /*
476          * No point in finding a key and decrypting if the frame is neither
477          * addressed to us nor a multicast frame.
478          */
479         if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
480                 return TXRX_CONTINUE;
481
482         if (rx->sta)
483                 stakey = rcu_dereference(rx->sta->key);
484
485         if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
486                 rx->key = stakey;
487         } else {
488                 /*
489                  * The device doesn't give us the IV so we won't be
490                  * able to look up the key. That's ok though, we
491                  * don't need to decrypt the frame, we just won't
492                  * be able to keep statistics accurate.
493                  * Except for key threshold notifications, should
494                  * we somehow allow the driver to tell us which key
495                  * the hardware used if this flag is set?
496                  */
497                 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
498                     (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
499                         return TXRX_CONTINUE;
500
501                 hdrlen = ieee80211_get_hdrlen(rx->fc);
502
503                 if (rx->skb->len < 8 + hdrlen)
504                         return TXRX_DROP; /* TODO: count this? */
505
506                 /*
507                  * no need to call ieee80211_wep_get_keyidx,
508                  * it verifies a bunch of things we've done already
509                  */
510                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
511
512                 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
513
514                 /*
515                  * RSNA-protected unicast frames should always be sent with
516                  * pairwise or station-to-station keys, but for WEP we allow
517                  * using a key index as well.
518                  */
519                 if (rx->key && rx->key->conf.alg != ALG_WEP &&
520                     !is_multicast_ether_addr(hdr->addr1))
521                         rx->key = NULL;
522         }
523
524         if (rx->key) {
525                 rx->key->tx_rx_count++;
526                 /* TODO: add threshold stuff again */
527         } else {
528 #ifdef CONFIG_MAC80211_DEBUG
529                 if (net_ratelimit())
530                         printk(KERN_DEBUG "%s: RX protected frame,"
531                                " but have no key\n", rx->dev->name);
532 #endif /* CONFIG_MAC80211_DEBUG */
533                 return TXRX_DROP;
534         }
535
536         /* Check for weak IVs if possible */
537         if (rx->sta && rx->key->conf.alg == ALG_WEP &&
538             ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
539             (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
540              !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
541             ieee80211_wep_is_weak_iv(rx->skb, rx->key))
542                 rx->sta->wep_weak_iv_count++;
543
544         switch (rx->key->conf.alg) {
545         case ALG_WEP:
546                 result = ieee80211_crypto_wep_decrypt(rx);
547                 break;
548         case ALG_TKIP:
549                 result = ieee80211_crypto_tkip_decrypt(rx);
550                 break;
551         case ALG_CCMP:
552                 result = ieee80211_crypto_ccmp_decrypt(rx);
553                 break;
554         }
555
556         /* either the frame has been decrypted or will be dropped */
557         rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
558
559         return result;
560 }
561
562 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
563 {
564         struct ieee80211_sub_if_data *sdata;
565         DECLARE_MAC_BUF(mac);
566
567         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
568
569         if (sdata->bss)
570                 atomic_inc(&sdata->bss->num_sta_ps);
571         sta->flags |= WLAN_STA_PS;
572         sta->pspoll = 0;
573 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
574         printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
575                dev->name, print_mac(mac, sta->addr), sta->aid);
576 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
577 }
578
579 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
580 {
581         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
582         struct sk_buff *skb;
583         int sent = 0;
584         struct ieee80211_sub_if_data *sdata;
585         struct ieee80211_tx_packet_data *pkt_data;
586         DECLARE_MAC_BUF(mac);
587
588         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
589         if (sdata->bss)
590                 atomic_dec(&sdata->bss->num_sta_ps);
591         sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
592         sta->pspoll = 0;
593         if (!skb_queue_empty(&sta->ps_tx_buf)) {
594                 if (local->ops->set_tim)
595                         local->ops->set_tim(local_to_hw(local), sta->aid, 0);
596                 if (sdata->bss)
597                         bss_tim_clear(local, sdata->bss, sta->aid);
598         }
599 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
600         printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
601                dev->name, print_mac(mac, sta->addr), sta->aid);
602 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
603         /* Send all buffered frames to the station */
604         while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
605                 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
606                 sent++;
607                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
608                 dev_queue_xmit(skb);
609         }
610         while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
611                 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
612                 local->total_ps_buffered--;
613                 sent++;
614 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
615                 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
616                        "since STA not sleeping anymore\n", dev->name,
617                        print_mac(mac, sta->addr), sta->aid);
618 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
619                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
620                 dev_queue_xmit(skb);
621         }
622
623         return sent;
624 }
625
626 static ieee80211_txrx_result
627 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
628 {
629         struct sta_info *sta = rx->sta;
630         struct net_device *dev = rx->dev;
631         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
632
633         if (!sta)
634                 return TXRX_CONTINUE;
635
636         /* Update last_rx only for IBSS packets which are for the current
637          * BSSID to avoid keeping the current IBSS network alive in cases where
638          * other STAs are using different BSSID. */
639         if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
640                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
641                 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
642                         sta->last_rx = jiffies;
643         } else
644         if (!is_multicast_ether_addr(hdr->addr1) ||
645             rx->sdata->type == IEEE80211_IF_TYPE_STA) {
646                 /* Update last_rx only for unicast frames in order to prevent
647                  * the Probe Request frames (the only broadcast frames from a
648                  * STA in infrastructure mode) from keeping a connection alive.
649                  */
650                 sta->last_rx = jiffies;
651         }
652
653         if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
654                 return TXRX_CONTINUE;
655
656         sta->rx_fragments++;
657         sta->rx_bytes += rx->skb->len;
658         sta->last_rssi = rx->u.rx.status->ssi;
659         sta->last_signal = rx->u.rx.status->signal;
660         sta->last_noise = rx->u.rx.status->noise;
661
662         if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
663                 /* Change STA power saving mode only in the end of a frame
664                  * exchange sequence */
665                 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
666                         rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
667                 else if (!(sta->flags & WLAN_STA_PS) &&
668                          (rx->fc & IEEE80211_FCTL_PM))
669                         ap_sta_ps_start(dev, sta);
670         }
671
672         /* Drop data::nullfunc frames silently, since they are used only to
673          * control station power saving mode. */
674         if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
675             (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
676                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
677                 /* Update counter and free packet here to avoid counting this
678                  * as a dropped packed. */
679                 sta->rx_packets++;
680                 dev_kfree_skb(rx->skb);
681                 return TXRX_QUEUED;
682         }
683
684         return TXRX_CONTINUE;
685 } /* ieee80211_rx_h_sta_process */
686
687 static inline struct ieee80211_fragment_entry *
688 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
689                          unsigned int frag, unsigned int seq, int rx_queue,
690                          struct sk_buff **skb)
691 {
692         struct ieee80211_fragment_entry *entry;
693         int idx;
694
695         idx = sdata->fragment_next;
696         entry = &sdata->fragments[sdata->fragment_next++];
697         if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
698                 sdata->fragment_next = 0;
699
700         if (!skb_queue_empty(&entry->skb_list)) {
701 #ifdef CONFIG_MAC80211_DEBUG
702                 struct ieee80211_hdr *hdr =
703                         (struct ieee80211_hdr *) entry->skb_list.next->data;
704                 DECLARE_MAC_BUF(mac);
705                 DECLARE_MAC_BUF(mac2);
706                 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
707                        "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
708                        "addr1=%s addr2=%s\n",
709                        sdata->dev->name, idx,
710                        jiffies - entry->first_frag_time, entry->seq,
711                        entry->last_frag, print_mac(mac, hdr->addr1),
712                        print_mac(mac2, hdr->addr2));
713 #endif /* CONFIG_MAC80211_DEBUG */
714                 __skb_queue_purge(&entry->skb_list);
715         }
716
717         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
718         *skb = NULL;
719         entry->first_frag_time = jiffies;
720         entry->seq = seq;
721         entry->rx_queue = rx_queue;
722         entry->last_frag = frag;
723         entry->ccmp = 0;
724         entry->extra_len = 0;
725
726         return entry;
727 }
728
729 static inline struct ieee80211_fragment_entry *
730 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
731                           u16 fc, unsigned int frag, unsigned int seq,
732                           int rx_queue, struct ieee80211_hdr *hdr)
733 {
734         struct ieee80211_fragment_entry *entry;
735         int i, idx;
736
737         idx = sdata->fragment_next;
738         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
739                 struct ieee80211_hdr *f_hdr;
740                 u16 f_fc;
741
742                 idx--;
743                 if (idx < 0)
744                         idx = IEEE80211_FRAGMENT_MAX - 1;
745
746                 entry = &sdata->fragments[idx];
747                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
748                     entry->rx_queue != rx_queue ||
749                     entry->last_frag + 1 != frag)
750                         continue;
751
752                 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
753                 f_fc = le16_to_cpu(f_hdr->frame_control);
754
755                 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
756                     compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
757                     compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
758                         continue;
759
760                 if (entry->first_frag_time + 2 * HZ < jiffies) {
761                         __skb_queue_purge(&entry->skb_list);
762                         continue;
763                 }
764                 return entry;
765         }
766
767         return NULL;
768 }
769
770 static ieee80211_txrx_result
771 ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
772 {
773         struct ieee80211_hdr *hdr;
774         u16 sc;
775         unsigned int frag, seq;
776         struct ieee80211_fragment_entry *entry;
777         struct sk_buff *skb;
778         DECLARE_MAC_BUF(mac);
779
780         hdr = (struct ieee80211_hdr *) rx->skb->data;
781         sc = le16_to_cpu(hdr->seq_ctrl);
782         frag = sc & IEEE80211_SCTL_FRAG;
783
784         if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
785                    (rx->skb)->len < 24 ||
786                    is_multicast_ether_addr(hdr->addr1))) {
787                 /* not fragmented */
788                 goto out;
789         }
790         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
791
792         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
793
794         if (frag == 0) {
795                 /* This is the first fragment of a new frame. */
796                 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
797                                                  rx->u.rx.queue, &(rx->skb));
798                 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
799                     (rx->fc & IEEE80211_FCTL_PROTECTED)) {
800                         /* Store CCMP PN so that we can verify that the next
801                          * fragment has a sequential PN value. */
802                         entry->ccmp = 1;
803                         memcpy(entry->last_pn,
804                                rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
805                                CCMP_PN_LEN);
806                 }
807                 return TXRX_QUEUED;
808         }
809
810         /* This is a fragment for a frame that should already be pending in
811          * fragment cache. Add this fragment to the end of the pending entry.
812          */
813         entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
814                                           rx->u.rx.queue, hdr);
815         if (!entry) {
816                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
817                 return TXRX_DROP;
818         }
819
820         /* Verify that MPDUs within one MSDU have sequential PN values.
821          * (IEEE 802.11i, 8.3.3.4.5) */
822         if (entry->ccmp) {
823                 int i;
824                 u8 pn[CCMP_PN_LEN], *rpn;
825                 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
826                         return TXRX_DROP;
827                 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
828                 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
829                         pn[i]++;
830                         if (pn[i])
831                                 break;
832                 }
833                 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
834                 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
835                         if (net_ratelimit())
836                                 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
837                                        "sequential A2=%s"
838                                        " PN=%02x%02x%02x%02x%02x%02x "
839                                        "(expected %02x%02x%02x%02x%02x%02x)\n",
840                                        rx->dev->name, print_mac(mac, hdr->addr2),
841                                        rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
842                                        rpn[5], pn[0], pn[1], pn[2], pn[3],
843                                        pn[4], pn[5]);
844                         return TXRX_DROP;
845                 }
846                 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
847         }
848
849         skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
850         __skb_queue_tail(&entry->skb_list, rx->skb);
851         entry->last_frag = frag;
852         entry->extra_len += rx->skb->len;
853         if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
854                 rx->skb = NULL;
855                 return TXRX_QUEUED;
856         }
857
858         rx->skb = __skb_dequeue(&entry->skb_list);
859         if (skb_tailroom(rx->skb) < entry->extra_len) {
860                 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
861                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
862                                               GFP_ATOMIC))) {
863                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
864                         __skb_queue_purge(&entry->skb_list);
865                         return TXRX_DROP;
866                 }
867         }
868         while ((skb = __skb_dequeue(&entry->skb_list))) {
869                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
870                 dev_kfree_skb(skb);
871         }
872
873         /* Complete frame has been reassembled - process it now */
874         rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
875
876  out:
877         if (rx->sta)
878                 rx->sta->rx_packets++;
879         if (is_multicast_ether_addr(hdr->addr1))
880                 rx->local->dot11MulticastReceivedFrameCount++;
881         else
882                 ieee80211_led_rx(rx->local);
883         return TXRX_CONTINUE;
884 }
885
886 static ieee80211_txrx_result
887 ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
888 {
889         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
890         struct sk_buff *skb;
891         int no_pending_pkts;
892         DECLARE_MAC_BUF(mac);
893
894         if (likely(!rx->sta ||
895                    (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
896                    (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
897                    !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
898                 return TXRX_CONTINUE;
899
900         if ((sdata->type != IEEE80211_IF_TYPE_AP) &&
901             (sdata->type != IEEE80211_IF_TYPE_VLAN))
902                 return TXRX_DROP;
903
904         skb = skb_dequeue(&rx->sta->tx_filtered);
905         if (!skb) {
906                 skb = skb_dequeue(&rx->sta->ps_tx_buf);
907                 if (skb)
908                         rx->local->total_ps_buffered--;
909         }
910         no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
911                 skb_queue_empty(&rx->sta->ps_tx_buf);
912
913         if (skb) {
914                 struct ieee80211_hdr *hdr =
915                         (struct ieee80211_hdr *) skb->data;
916
917                 /* tell TX path to send one frame even though the STA may
918                  * still remain is PS mode after this frame exchange */
919                 rx->sta->pspoll = 1;
920
921 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
922                 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
923                        print_mac(mac, rx->sta->addr), rx->sta->aid,
924                        skb_queue_len(&rx->sta->ps_tx_buf));
925 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
926
927                 /* Use MoreData flag to indicate whether there are more
928                  * buffered frames for this STA */
929                 if (no_pending_pkts) {
930                         hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
931                         rx->sta->flags &= ~WLAN_STA_TIM;
932                 } else
933                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
934
935                 dev_queue_xmit(skb);
936
937                 if (no_pending_pkts) {
938                         if (rx->local->ops->set_tim)
939                                 rx->local->ops->set_tim(local_to_hw(rx->local),
940                                                        rx->sta->aid, 0);
941                         if (rx->sdata->bss)
942                                 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
943                 }
944 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
945         } else if (!rx->u.rx.sent_ps_buffered) {
946                 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
947                        "though there is no buffered frames for it\n",
948                        rx->dev->name, print_mac(mac, rx->sta->addr));
949 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
950
951         }
952
953         /* Free PS Poll skb here instead of returning TXRX_DROP that would
954          * count as an dropped frame. */
955         dev_kfree_skb(rx->skb);
956
957         return TXRX_QUEUED;
958 }
959
960 static ieee80211_txrx_result
961 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
962 {
963         u16 fc = rx->fc;
964         u8 *data = rx->skb->data;
965         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
966
967         if (!WLAN_FC_IS_QOS_DATA(fc))
968                 return TXRX_CONTINUE;
969
970         /* remove the qos control field, update frame type and meta-data */
971         memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
972         hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
973         /* change frame type to non QOS */
974         rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
975         hdr->frame_control = cpu_to_le16(fc);
976
977         return TXRX_CONTINUE;
978 }
979
980 static int
981 ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
982 {
983         if (unlikely(rx->sdata->ieee802_1x_pac &&
984                      (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
985 #ifdef CONFIG_MAC80211_DEBUG
986                 printk(KERN_DEBUG "%s: dropped frame "
987                        "(unauthorized port)\n", rx->dev->name);
988 #endif /* CONFIG_MAC80211_DEBUG */
989                 return -EACCES;
990         }
991
992         return 0;
993 }
994
995 static int
996 ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
997 {
998         /*
999          * Pass through unencrypted frames if the hardware has
1000          * decrypted them already.
1001          */
1002         if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
1003                 return 0;
1004
1005         /* Drop unencrypted frames if key is set. */
1006         if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1007                      (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1008                      (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
1009                      (rx->key || rx->sdata->drop_unencrypted))) {
1010                 if (net_ratelimit())
1011                         printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1012                                "encryption\n", rx->dev->name);
1013                 return -EACCES;
1014         }
1015         return 0;
1016 }
1017
1018 static int
1019 ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
1020 {
1021         struct net_device *dev = rx->dev;
1022         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1023         u16 fc, hdrlen, ethertype;
1024         u8 *payload;
1025         u8 dst[ETH_ALEN];
1026         u8 src[ETH_ALEN];
1027         struct sk_buff *skb = rx->skb;
1028         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1029         DECLARE_MAC_BUF(mac);
1030         DECLARE_MAC_BUF(mac2);
1031         DECLARE_MAC_BUF(mac3);
1032         DECLARE_MAC_BUF(mac4);
1033
1034         fc = rx->fc;
1035
1036         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1037                 return -1;
1038
1039         hdrlen = ieee80211_get_hdrlen(fc);
1040
1041         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1042          * header
1043          * IEEE 802.11 address fields:
1044          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1045          *   0     0   DA    SA    BSSID n/a
1046          *   0     1   DA    BSSID SA    n/a
1047          *   1     0   BSSID SA    DA    n/a
1048          *   1     1   RA    TA    DA    SA
1049          */
1050
1051         switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1052         case IEEE80211_FCTL_TODS:
1053                 /* BSSID SA DA */
1054                 memcpy(dst, hdr->addr3, ETH_ALEN);
1055                 memcpy(src, hdr->addr2, ETH_ALEN);
1056
1057                 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1058                              sdata->type != IEEE80211_IF_TYPE_VLAN)) {
1059                         if (net_ratelimit())
1060                                 printk(KERN_DEBUG "%s: dropped ToDS frame "
1061                                        "(BSSID=%s SA=%s DA=%s)\n",
1062                                        dev->name,
1063                                        print_mac(mac, hdr->addr1),
1064                                        print_mac(mac2, hdr->addr2),
1065                                        print_mac(mac3, hdr->addr3));
1066                         return -1;
1067                 }
1068                 break;
1069         case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1070                 /* RA TA DA SA */
1071                 memcpy(dst, hdr->addr3, ETH_ALEN);
1072                 memcpy(src, hdr->addr4, ETH_ALEN);
1073
1074                 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
1075                         if (net_ratelimit())
1076                                 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
1077                                        "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1078                                        rx->dev->name,
1079                                        print_mac(mac, hdr->addr1),
1080                                        print_mac(mac2, hdr->addr2),
1081                                        print_mac(mac3, hdr->addr3),
1082                                        print_mac(mac4, hdr->addr4));
1083                         return -1;
1084                 }
1085                 break;
1086         case IEEE80211_FCTL_FROMDS:
1087                 /* DA BSSID SA */
1088                 memcpy(dst, hdr->addr1, ETH_ALEN);
1089                 memcpy(src, hdr->addr3, ETH_ALEN);
1090
1091                 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1092                     (is_multicast_ether_addr(dst) &&
1093                      !compare_ether_addr(src, dev->dev_addr)))
1094                         return -1;
1095                 break;
1096         case 0:
1097                 /* DA SA BSSID */
1098                 memcpy(dst, hdr->addr1, ETH_ALEN);
1099                 memcpy(src, hdr->addr2, ETH_ALEN);
1100
1101                 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1102                         if (net_ratelimit()) {
1103                                 printk(KERN_DEBUG "%s: dropped IBSS frame "
1104                                        "(DA=%s SA=%s BSSID=%s)\n",
1105                                        dev->name,
1106                                        print_mac(mac, hdr->addr1),
1107                                        print_mac(mac2, hdr->addr2),
1108                                        print_mac(mac3, hdr->addr3));
1109                         }
1110                         return -1;
1111                 }
1112                 break;
1113         }
1114
1115         if (unlikely(skb->len - hdrlen < 8)) {
1116                 if (net_ratelimit()) {
1117                         printk(KERN_DEBUG "%s: RX too short data frame "
1118                                "payload\n", dev->name);
1119                 }
1120                 return -1;
1121         }
1122
1123         payload = skb->data + hdrlen;
1124         ethertype = (payload[6] << 8) | payload[7];
1125
1126         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1127                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1128                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1129                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1130                  * replace EtherType */
1131                 skb_pull(skb, hdrlen + 6);
1132                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1133                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1134         } else {
1135                 struct ethhdr *ehdr;
1136                 __be16 len;
1137
1138                 skb_pull(skb, hdrlen);
1139                 len = htons(skb->len);
1140                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1141                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1142                 memcpy(ehdr->h_source, src, ETH_ALEN);
1143                 ehdr->h_proto = len;
1144         }
1145         return 0;
1146 }
1147
1148 /*
1149  * requires that rx->skb is a frame with ethernet header
1150  */
1151 static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1152 {
1153         static const u8 pae_group_addr[ETH_ALEN]
1154                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1155         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1156
1157         /*
1158          * Allow EAPOL frames to us/the PAE group address regardless
1159          * of whether the frame was encrypted or not.
1160          */
1161         if (ehdr->h_proto == htons(ETH_P_PAE) &&
1162             (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1163              compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1164                 return true;
1165
1166         if (ieee80211_802_1x_port_control(rx) ||
1167             ieee80211_drop_unencrypted(rx))
1168                 return false;
1169
1170         return true;
1171 }
1172
1173 /*
1174  * requires that rx->skb is a frame with ethernet header
1175  */
1176 static void
1177 ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1178 {
1179         struct net_device *dev = rx->dev;
1180         struct ieee80211_local *local = rx->local;
1181         struct sk_buff *skb, *xmit_skb;
1182         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1183         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1184         struct sta_info *dsta;
1185
1186         skb = rx->skb;
1187         xmit_skb = NULL;
1188
1189         if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP ||
1190                                       sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1191             (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
1192                 if (is_multicast_ether_addr(ehdr->h_dest)) {
1193                         /*
1194                          * send multicast frames both to higher layers in
1195                          * local net stack and back to the wireless medium
1196                          */
1197                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
1198                         if (!xmit_skb && net_ratelimit())
1199                                 printk(KERN_DEBUG "%s: failed to clone "
1200                                        "multicast frame\n", dev->name);
1201                 } else {
1202                         dsta = sta_info_get(local, skb->data);
1203                         if (dsta && dsta->dev == dev) {
1204                                 /*
1205                                  * The destination station is associated to
1206                                  * this AP (in this VLAN), so send the frame
1207                                  * directly to it and do not pass it to local
1208                                  * net stack.
1209                                  */
1210                                 xmit_skb = skb;
1211                                 skb = NULL;
1212                         }
1213                         if (dsta)
1214                                 sta_info_put(dsta);
1215                 }
1216         }
1217
1218         if (skb) {
1219                 /* deliver to local stack */
1220                 skb->protocol = eth_type_trans(skb, dev);
1221                 memset(skb->cb, 0, sizeof(skb->cb));
1222                 netif_rx(skb);
1223         }
1224
1225         if (xmit_skb) {
1226                 /* send to wireless media */
1227                 xmit_skb->protocol = htons(ETH_P_802_3);
1228                 skb_reset_network_header(xmit_skb);
1229                 skb_reset_mac_header(xmit_skb);
1230                 dev_queue_xmit(xmit_skb);
1231         }
1232 }
1233
1234 static ieee80211_txrx_result
1235 ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1236 {
1237         struct net_device *dev = rx->dev;
1238         struct ieee80211_local *local = rx->local;
1239         u16 fc, ethertype;
1240         u8 *payload;
1241         struct sk_buff *skb = rx->skb, *frame = NULL;
1242         const struct ethhdr *eth;
1243         int remaining, err;
1244         u8 dst[ETH_ALEN];
1245         u8 src[ETH_ALEN];
1246         DECLARE_MAC_BUF(mac);
1247
1248         fc = rx->fc;
1249         if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1250                 return TXRX_CONTINUE;
1251
1252         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1253                 return TXRX_DROP;
1254
1255         if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
1256                 return TXRX_CONTINUE;
1257
1258         err = ieee80211_data_to_8023(rx);
1259         if (unlikely(err))
1260                 return TXRX_DROP;
1261
1262         skb->dev = dev;
1263
1264         dev->stats.rx_packets++;
1265         dev->stats.rx_bytes += skb->len;
1266
1267         /* skip the wrapping header */
1268         eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1269         if (!eth)
1270                 return TXRX_DROP;
1271
1272         while (skb != frame) {
1273                 u8 padding;
1274                 __be16 len = eth->h_proto;
1275                 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1276
1277                 remaining = skb->len;
1278                 memcpy(dst, eth->h_dest, ETH_ALEN);
1279                 memcpy(src, eth->h_source, ETH_ALEN);
1280
1281                 padding = ((4 - subframe_len) & 0x3);
1282                 /* the last MSDU has no padding */
1283                 if (subframe_len > remaining) {
1284                         printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1285                         return TXRX_DROP;
1286                 }
1287
1288                 skb_pull(skb, sizeof(struct ethhdr));
1289                 /* if last subframe reuse skb */
1290                 if (remaining <= subframe_len + padding)
1291                         frame = skb;
1292                 else {
1293                         frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1294                                               subframe_len);
1295
1296                         if (frame == NULL)
1297                                 return TXRX_DROP;
1298
1299                         skb_reserve(frame, local->hw.extra_tx_headroom +
1300                                     sizeof(struct ethhdr));
1301                         memcpy(skb_put(frame, ntohs(len)), skb->data,
1302                                 ntohs(len));
1303
1304                         eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1305                                                         padding);
1306                         if (!eth) {
1307                                 printk(KERN_DEBUG "%s: wrong buffer size ",
1308                                        dev->name);
1309                                 dev_kfree_skb(frame);
1310                                 return TXRX_DROP;
1311                         }
1312                 }
1313
1314                 skb_reset_network_header(frame);
1315                 frame->dev = dev;
1316                 frame->priority = skb->priority;
1317                 rx->skb = frame;
1318
1319                 payload = frame->data;
1320                 ethertype = (payload[6] << 8) | payload[7];
1321
1322                 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1323                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1324                            compare_ether_addr(payload,
1325                                               bridge_tunnel_header) == 0)) {
1326                         /* remove RFC1042 or Bridge-Tunnel
1327                          * encapsulation and replace EtherType */
1328                         skb_pull(frame, 6);
1329                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1330                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1331                 } else {
1332                         memcpy(skb_push(frame, sizeof(__be16)),
1333                                &len, sizeof(__be16));
1334                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1335                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1336                 }
1337
1338                 if (!ieee80211_frame_allowed(rx)) {
1339                         if (skb == frame) /* last frame */
1340                                 return TXRX_DROP;
1341                         dev_kfree_skb(frame);
1342                         continue;
1343                 }
1344
1345                 ieee80211_deliver_skb(rx);
1346         }
1347
1348         return TXRX_QUEUED;
1349 }
1350
1351 static ieee80211_txrx_result
1352 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1353 {
1354         struct net_device *dev = rx->dev;
1355         u16 fc;
1356         int err;
1357
1358         fc = rx->fc;
1359         if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1360                 return TXRX_CONTINUE;
1361
1362         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1363                 return TXRX_DROP;
1364
1365         err = ieee80211_data_to_8023(rx);
1366         if (unlikely(err))
1367                 return TXRX_DROP;
1368
1369         if (!ieee80211_frame_allowed(rx))
1370                 return TXRX_DROP;
1371
1372         rx->skb->dev = dev;
1373
1374         dev->stats.rx_packets++;
1375         dev->stats.rx_bytes += rx->skb->len;
1376
1377         ieee80211_deliver_skb(rx);
1378
1379         return TXRX_QUEUED;
1380 }
1381
1382 static ieee80211_txrx_result
1383 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1384 {
1385         struct ieee80211_sub_if_data *sdata;
1386
1387         if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
1388                 return TXRX_DROP;
1389
1390         sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1391         if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1392              sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1393             !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
1394                 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1395         else
1396                 return TXRX_DROP;
1397
1398         return TXRX_QUEUED;
1399 }
1400
1401 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1402                                 struct ieee80211_local *local,
1403                                 ieee80211_rx_handler *handlers,
1404                                 struct ieee80211_txrx_data *rx,
1405                                 struct sta_info *sta)
1406 {
1407         ieee80211_rx_handler *handler;
1408         ieee80211_txrx_result res = TXRX_DROP;
1409
1410         for (handler = handlers; *handler != NULL; handler++) {
1411                 res = (*handler)(rx);
1412
1413                 switch (res) {
1414                 case TXRX_CONTINUE:
1415                         continue;
1416                 case TXRX_DROP:
1417                         I802_DEBUG_INC(local->rx_handlers_drop);
1418                         if (sta)
1419                                 sta->rx_dropped++;
1420                         break;
1421                 case TXRX_QUEUED:
1422                         I802_DEBUG_INC(local->rx_handlers_queued);
1423                         break;
1424                 }
1425                 break;
1426         }
1427
1428         if (res == TXRX_DROP)
1429                 dev_kfree_skb(rx->skb);
1430         return res;
1431 }
1432
1433 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1434                                                 ieee80211_rx_handler *handlers,
1435                                                 struct ieee80211_txrx_data *rx,
1436                                                 struct sta_info *sta)
1437 {
1438         if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1439             TXRX_CONTINUE)
1440                 dev_kfree_skb(rx->skb);
1441 }
1442
1443 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1444                                             struct ieee80211_hdr *hdr,
1445                                             struct sta_info *sta,
1446                                             struct ieee80211_txrx_data *rx)
1447 {
1448         int keyidx, hdrlen;
1449         DECLARE_MAC_BUF(mac);
1450         DECLARE_MAC_BUF(mac2);
1451
1452         hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1453         if (rx->skb->len >= hdrlen + 4)
1454                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1455         else
1456                 keyidx = -1;
1457
1458         if (net_ratelimit())
1459                 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1460                        "failure from %s to %s keyidx=%d\n",
1461                        dev->name, print_mac(mac, hdr->addr2),
1462                        print_mac(mac2, hdr->addr1), keyidx);
1463
1464         if (!sta) {
1465                 /*
1466                  * Some hardware seem to generate incorrect Michael MIC
1467                  * reports; ignore them to avoid triggering countermeasures.
1468                  */
1469                 if (net_ratelimit())
1470                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1471                                "error for unknown address %s\n",
1472                                dev->name, print_mac(mac, hdr->addr2));
1473                 goto ignore;
1474         }
1475
1476         if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1477                 if (net_ratelimit())
1478                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1479                                "error for a frame with no PROTECTED flag (src "
1480                                "%s)\n", dev->name, print_mac(mac, hdr->addr2));
1481                 goto ignore;
1482         }
1483
1484         if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
1485                 /*
1486                  * APs with pairwise keys should never receive Michael MIC
1487                  * errors for non-zero keyidx because these are reserved for
1488                  * group keys and only the AP is sending real multicast
1489                  * frames in the BSS.
1490                  */
1491                 if (net_ratelimit())
1492                         printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1493                                "a frame with non-zero keyidx (%d)"
1494                                " (src %s)\n", dev->name, keyidx,
1495                                print_mac(mac, hdr->addr2));
1496                 goto ignore;
1497         }
1498
1499         if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1500             ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1501              (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1502                 if (net_ratelimit())
1503                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1504                                "error for a frame that cannot be encrypted "
1505                                "(fc=0x%04x) (src %s)\n",
1506                                dev->name, rx->fc, print_mac(mac, hdr->addr2));
1507                 goto ignore;
1508         }
1509
1510         mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
1511  ignore:
1512         dev_kfree_skb(rx->skb);
1513         rx->skb = NULL;
1514 }
1515
1516 ieee80211_rx_handler ieee80211_rx_handlers[] =
1517 {
1518         ieee80211_rx_h_if_stats,
1519         ieee80211_rx_h_passive_scan,
1520         ieee80211_rx_h_check,
1521         ieee80211_rx_h_decrypt,
1522         ieee80211_rx_h_sta_process,
1523         ieee80211_rx_h_defragment,
1524         ieee80211_rx_h_ps_poll,
1525         ieee80211_rx_h_michael_mic_verify,
1526         /* this must be after decryption - so header is counted in MPDU mic
1527          * must be before pae and data, so QOS_DATA format frames
1528          * are not passed to user space by these functions
1529          */
1530         ieee80211_rx_h_remove_qos_control,
1531         ieee80211_rx_h_amsdu,
1532         ieee80211_rx_h_data,
1533         ieee80211_rx_h_mgmt,
1534         NULL
1535 };
1536
1537 /* main receive path */
1538
1539 static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1540                                 u8 *bssid, struct ieee80211_txrx_data *rx,
1541                                 struct ieee80211_hdr *hdr)
1542 {
1543         int multicast = is_multicast_ether_addr(hdr->addr1);
1544
1545         switch (sdata->type) {
1546         case IEEE80211_IF_TYPE_STA:
1547                 if (!bssid)
1548                         return 0;
1549                 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1550                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1551                                 return 0;
1552                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1553                 } else if (!multicast &&
1554                            compare_ether_addr(sdata->dev->dev_addr,
1555                                               hdr->addr1) != 0) {
1556                         if (!(sdata->dev->flags & IFF_PROMISC))
1557                                 return 0;
1558                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1559                 }
1560                 break;
1561         case IEEE80211_IF_TYPE_IBSS:
1562                 if (!bssid)
1563                         return 0;
1564                 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1565                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1566                                 return 0;
1567                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1568                 } else if (!multicast &&
1569                            compare_ether_addr(sdata->dev->dev_addr,
1570                                               hdr->addr1) != 0) {
1571                         if (!(sdata->dev->flags & IFF_PROMISC))
1572                                 return 0;
1573                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1574                 } else if (!rx->sta)
1575                         rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1576                                                          bssid, hdr->addr2);
1577                 break;
1578         case IEEE80211_IF_TYPE_VLAN:
1579         case IEEE80211_IF_TYPE_AP:
1580                 if (!bssid) {
1581                         if (compare_ether_addr(sdata->dev->dev_addr,
1582                                                hdr->addr1))
1583                                 return 0;
1584                 } else if (!ieee80211_bssid_match(bssid,
1585                                         sdata->dev->dev_addr)) {
1586                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1587                                 return 0;
1588                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1589                 }
1590                 if (sdata->dev == sdata->local->mdev &&
1591                     !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1592                         /* do not receive anything via
1593                          * master device when not scanning */
1594                         return 0;
1595                 break;
1596         case IEEE80211_IF_TYPE_WDS:
1597                 if (bssid ||
1598                     (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1599                         return 0;
1600                 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1601                         return 0;
1602                 break;
1603         case IEEE80211_IF_TYPE_MNTR:
1604                 /* take everything */
1605                 break;
1606         case IEEE80211_IF_TYPE_INVALID:
1607                 /* should never get here */
1608                 WARN_ON(1);
1609                 break;
1610         }
1611
1612         return 1;
1613 }
1614
1615 /*
1616  * This is the receive path handler. It is called by a low level driver when an
1617  * 802.11 MPDU is received from the hardware.
1618  */
1619 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1620                     struct ieee80211_rx_status *status)
1621 {
1622         struct ieee80211_local *local = hw_to_local(hw);
1623         struct ieee80211_sub_if_data *sdata;
1624         struct sta_info *sta;
1625         struct ieee80211_hdr *hdr;
1626         struct ieee80211_txrx_data rx;
1627         u16 type;
1628         int prepres;
1629         struct ieee80211_sub_if_data *prev = NULL;
1630         struct sk_buff *skb_new;
1631         u8 *bssid;
1632         int hdrlen;
1633
1634         /*
1635          * key references and virtual interfaces are protected using RCU
1636          * and this requires that we are in a read-side RCU section during
1637          * receive processing
1638          */
1639         rcu_read_lock();
1640
1641         /*
1642          * Frames with failed FCS/PLCP checksum are not returned,
1643          * all other frames are returned without radiotap header
1644          * if it was previously present.
1645          * Also, frames with less than 16 bytes are dropped.
1646          */
1647         skb = ieee80211_rx_monitor(local, skb, status);
1648         if (!skb) {
1649                 rcu_read_unlock();
1650                 return;
1651         }
1652
1653         hdr = (struct ieee80211_hdr *) skb->data;
1654         memset(&rx, 0, sizeof(rx));
1655         rx.skb = skb;
1656         rx.local = local;
1657
1658         rx.u.rx.status = status;
1659         rx.fc = le16_to_cpu(hdr->frame_control);
1660         type = rx.fc & IEEE80211_FCTL_FTYPE;
1661
1662         /*
1663          * Drivers are required to align the payload data to a four-byte
1664          * boundary, so the last two bits of the address where it starts
1665          * may not be set. The header is required to be directly before
1666          * the payload data, padding like atheros hardware adds which is
1667          * inbetween the 802.11 header and the payload is not supported,
1668          * the driver is required to move the 802.11 header further back
1669          * in that case.
1670          */
1671         hdrlen = ieee80211_get_hdrlen(rx.fc);
1672         WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1673
1674         if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1675                 local->dot11ReceivedFragmentCount++;
1676
1677         sta = rx.sta = sta_info_get(local, hdr->addr2);
1678         if (sta) {
1679                 rx.dev = rx.sta->dev;
1680                 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1681         }
1682
1683         if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1684                 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1685                 goto end;
1686         }
1687
1688         if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
1689                 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
1690
1691         if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1692                                            sta) != TXRX_CONTINUE)
1693                 goto end;
1694         skb = rx.skb;
1695
1696         if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
1697             !atomic_read(&local->iff_promiscs) &&
1698             !is_multicast_ether_addr(hdr->addr1)) {
1699                 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1700                 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
1701                                              rx.sta);
1702                 sta_info_put(sta);
1703                 rcu_read_unlock();
1704                 return;
1705         }
1706
1707         bssid = ieee80211_get_bssid(hdr, skb->len);
1708
1709         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1710                 if (!netif_running(sdata->dev))
1711                         continue;
1712
1713                 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1714                         continue;
1715
1716                 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1717                 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1718                 /* prepare_for_handlers can change sta */
1719                 sta = rx.sta;
1720
1721                 if (!prepres)
1722                         continue;
1723
1724                 /*
1725                  * frame is destined for this interface, but if it's not
1726                  * also for the previous one we handle that after the
1727                  * loop to avoid copying the SKB once too much
1728                  */
1729
1730                 if (!prev) {
1731                         prev = sdata;
1732                         continue;
1733                 }
1734
1735                 /*
1736                  * frame was destined for the previous interface
1737                  * so invoke RX handlers for it
1738                  */
1739
1740                 skb_new = skb_copy(skb, GFP_ATOMIC);
1741                 if (!skb_new) {
1742                         if (net_ratelimit())
1743                                 printk(KERN_DEBUG "%s: failed to copy "
1744                                        "multicast frame for %s",
1745                                        wiphy_name(local->hw.wiphy),
1746                                        prev->dev->name);
1747                         continue;
1748                 }
1749                 rx.fc = le16_to_cpu(hdr->frame_control);
1750                 rx.skb = skb_new;
1751                 rx.dev = prev->dev;
1752                 rx.sdata = prev;
1753                 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1754                                              &rx, sta);
1755                 prev = sdata;
1756         }
1757         if (prev) {
1758                 rx.fc = le16_to_cpu(hdr->frame_control);
1759                 rx.skb = skb;
1760                 rx.dev = prev->dev;
1761                 rx.sdata = prev;
1762                 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1763                                              &rx, sta);
1764         } else
1765                 dev_kfree_skb(skb);
1766
1767  end:
1768         rcu_read_unlock();
1769
1770         if (sta)
1771                 sta_info_put(sta);
1772 }
1773 EXPORT_SYMBOL(__ieee80211_rx);
1774
1775 /* This is a version of the rx handler that can be called from hard irq
1776  * context. Post the skb on the queue and schedule the tasklet */
1777 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1778                           struct ieee80211_rx_status *status)
1779 {
1780         struct ieee80211_local *local = hw_to_local(hw);
1781
1782         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1783
1784         skb->dev = local->mdev;
1785         /* copy status into skb->cb for use by tasklet */
1786         memcpy(skb->cb, status, sizeof(*status));
1787         skb->pkt_type = IEEE80211_RX_MSG;
1788         skb_queue_tail(&local->skb_queue, skb);
1789         tasklet_schedule(&local->tasklet);
1790 }
1791 EXPORT_SYMBOL(ieee80211_rx_irqsafe);