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