[MAC80211]: remove management interface
[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_load_key(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 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         }
511
512         return TXRX_CONTINUE;
513 }
514
515 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
516 {
517         struct ieee80211_sub_if_data *sdata;
518         DECLARE_MAC_BUF(mac);
519
520         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
521
522         if (sdata->bss)
523                 atomic_inc(&sdata->bss->num_sta_ps);
524         sta->flags |= WLAN_STA_PS;
525         sta->pspoll = 0;
526 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
527         printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
528                dev->name, print_mac(mac, sta->addr), sta->aid);
529 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
530 }
531
532 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
533 {
534         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
535         struct sk_buff *skb;
536         int sent = 0;
537         struct ieee80211_sub_if_data *sdata;
538         struct ieee80211_tx_packet_data *pkt_data;
539         DECLARE_MAC_BUF(mac);
540
541         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
542         if (sdata->bss)
543                 atomic_dec(&sdata->bss->num_sta_ps);
544         sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
545         sta->pspoll = 0;
546         if (!skb_queue_empty(&sta->ps_tx_buf)) {
547                 if (local->ops->set_tim)
548                         local->ops->set_tim(local_to_hw(local), sta->aid, 0);
549                 if (sdata->bss)
550                         bss_tim_clear(local, sdata->bss, sta->aid);
551         }
552 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
553         printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
554                dev->name, print_mac(mac, sta->addr), sta->aid);
555 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
556         /* Send all buffered frames to the station */
557         while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
558                 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
559                 sent++;
560                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
561                 dev_queue_xmit(skb);
562         }
563         while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
564                 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
565                 local->total_ps_buffered--;
566                 sent++;
567 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
568                 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
569                        "since STA not sleeping anymore\n", dev->name,
570                        print_mac(mac, sta->addr), sta->aid);
571 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
572                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
573                 dev_queue_xmit(skb);
574         }
575
576         return sent;
577 }
578
579 static ieee80211_txrx_result
580 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
581 {
582         struct sta_info *sta = rx->sta;
583         struct net_device *dev = rx->dev;
584         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
585
586         if (!sta)
587                 return TXRX_CONTINUE;
588
589         /* Update last_rx only for IBSS packets which are for the current
590          * BSSID to avoid keeping the current IBSS network alive in cases where
591          * other STAs are using different BSSID. */
592         if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
593                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
594                 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
595                         sta->last_rx = jiffies;
596         } else
597         if (!is_multicast_ether_addr(hdr->addr1) ||
598             rx->sdata->type == IEEE80211_IF_TYPE_STA) {
599                 /* Update last_rx only for unicast frames in order to prevent
600                  * the Probe Request frames (the only broadcast frames from a
601                  * STA in infrastructure mode) from keeping a connection alive.
602                  */
603                 sta->last_rx = jiffies;
604         }
605
606         if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
607                 return TXRX_CONTINUE;
608
609         sta->rx_fragments++;
610         sta->rx_bytes += rx->skb->len;
611         sta->last_rssi = rx->u.rx.status->ssi;
612         sta->last_signal = rx->u.rx.status->signal;
613         sta->last_noise = rx->u.rx.status->noise;
614
615         if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
616                 /* Change STA power saving mode only in the end of a frame
617                  * exchange sequence */
618                 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
619                         rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
620                 else if (!(sta->flags & WLAN_STA_PS) &&
621                          (rx->fc & IEEE80211_FCTL_PM))
622                         ap_sta_ps_start(dev, sta);
623         }
624
625         /* Drop data::nullfunc frames silently, since they are used only to
626          * control station power saving mode. */
627         if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
628             (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
629                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
630                 /* Update counter and free packet here to avoid counting this
631                  * as a dropped packed. */
632                 sta->rx_packets++;
633                 dev_kfree_skb(rx->skb);
634                 return TXRX_QUEUED;
635         }
636
637         return TXRX_CONTINUE;
638 } /* ieee80211_rx_h_sta_process */
639
640 static ieee80211_txrx_result
641 ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
642 {
643         if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
644             (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
645             !rx->key || rx->key->conf.alg != ALG_WEP ||
646             !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
647                 return TXRX_CONTINUE;
648
649         /* Check for weak IVs, if hwaccel did not remove IV from the frame */
650         if (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
651             !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED))
652                 if (ieee80211_wep_is_weak_iv(rx->skb, rx->key))
653                         rx->sta->wep_weak_iv_count++;
654
655         return TXRX_CONTINUE;
656 }
657
658 static ieee80211_txrx_result
659 ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
660 {
661         if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
662                 return TXRX_CONTINUE;
663
664         if (!rx->key) {
665                 if (net_ratelimit())
666                         printk(KERN_DEBUG "%s: RX protected frame,"
667                                " but have no key\n", rx->dev->name);
668                 return TXRX_DROP;
669         }
670
671         switch (rx->key->conf.alg) {
672         case ALG_WEP:
673                 return ieee80211_crypto_wep_decrypt(rx);
674         case ALG_TKIP:
675                 return ieee80211_crypto_tkip_decrypt(rx);
676         case ALG_CCMP:
677                 return ieee80211_crypto_ccmp_decrypt(rx);
678         case ALG_NONE:
679                 return TXRX_CONTINUE;
680         }
681
682         /* not reached */
683         WARN_ON(1);
684         return TXRX_DROP;
685 }
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 sk_buff *skb;
890         int no_pending_pkts;
891         DECLARE_MAC_BUF(mac);
892
893         if (likely(!rx->sta ||
894                    (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
895                    (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
896                    !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
897                 return TXRX_CONTINUE;
898
899         skb = skb_dequeue(&rx->sta->tx_filtered);
900         if (!skb) {
901                 skb = skb_dequeue(&rx->sta->ps_tx_buf);
902                 if (skb)
903                         rx->local->total_ps_buffered--;
904         }
905         no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
906                 skb_queue_empty(&rx->sta->ps_tx_buf);
907
908         if (skb) {
909                 struct ieee80211_hdr *hdr =
910                         (struct ieee80211_hdr *) skb->data;
911
912                 /* tell TX path to send one frame even though the STA may
913                  * still remain is PS mode after this frame exchange */
914                 rx->sta->pspoll = 1;
915
916 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
917                 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
918                        print_mac(mac, rx->sta->addr), rx->sta->aid,
919                        skb_queue_len(&rx->sta->ps_tx_buf));
920 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
921
922                 /* Use MoreData flag to indicate whether there are more
923                  * buffered frames for this STA */
924                 if (no_pending_pkts) {
925                         hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
926                         rx->sta->flags &= ~WLAN_STA_TIM;
927                 } else
928                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
929
930                 dev_queue_xmit(skb);
931
932                 if (no_pending_pkts) {
933                         if (rx->local->ops->set_tim)
934                                 rx->local->ops->set_tim(local_to_hw(rx->local),
935                                                        rx->sta->aid, 0);
936                         if (rx->sdata->bss)
937                                 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
938                 }
939 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
940         } else if (!rx->u.rx.sent_ps_buffered) {
941                 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
942                        "though there is no buffered frames for it\n",
943                        rx->dev->name, print_mac(mac, rx->sta->addr));
944 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
945
946         }
947
948         /* Free PS Poll skb here instead of returning TXRX_DROP that would
949          * count as an dropped frame. */
950         dev_kfree_skb(rx->skb);
951
952         return TXRX_QUEUED;
953 }
954
955 static ieee80211_txrx_result
956 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
957 {
958         u16 fc = rx->fc;
959         u8 *data = rx->skb->data;
960         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
961
962         if (!WLAN_FC_IS_QOS_DATA(fc))
963                 return TXRX_CONTINUE;
964
965         /* remove the qos control field, update frame type and meta-data */
966         memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
967         hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
968         /* change frame type to non QOS */
969         rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
970         hdr->frame_control = cpu_to_le16(fc);
971
972         return TXRX_CONTINUE;
973 }
974
975 static ieee80211_txrx_result
976 ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
977 {
978         if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
979             rx->sdata->type != IEEE80211_IF_TYPE_STA &&
980             (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
981                 return TXRX_CONTINUE;
982
983         if (unlikely(rx->sdata->ieee802_1x &&
984                      (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
985                      (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
986                      (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
987                      !ieee80211_is_eapol(rx->skb))) {
988 #ifdef CONFIG_MAC80211_DEBUG
989                 struct ieee80211_hdr *hdr =
990                         (struct ieee80211_hdr *) rx->skb->data;
991                 DECLARE_MAC_BUF(mac);
992                 printk(KERN_DEBUG "%s: dropped frame from %s"
993                        " (unauthorized port)\n", rx->dev->name,
994                        print_mac(mac, hdr->addr2));
995 #endif /* CONFIG_MAC80211_DEBUG */
996                 return TXRX_DROP;
997         }
998
999         return TXRX_CONTINUE;
1000 }
1001
1002 static ieee80211_txrx_result
1003 ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
1004 {
1005         /*
1006          * Pass through unencrypted frames if the hardware has
1007          * decrypted them already.
1008          */
1009         if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
1010                 return TXRX_CONTINUE;
1011
1012         /* Drop unencrypted frames if key is set. */
1013         if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1014                      (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1015                      (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
1016                      (rx->key || rx->sdata->drop_unencrypted) &&
1017                      (rx->sdata->eapol == 0 ||
1018                       !ieee80211_is_eapol(rx->skb)))) {
1019                 if (net_ratelimit())
1020                         printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1021                                "encryption\n", rx->dev->name);
1022                 return TXRX_DROP;
1023         }
1024         return TXRX_CONTINUE;
1025 }
1026
1027 static ieee80211_txrx_result
1028 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1029 {
1030         struct net_device *dev = rx->dev;
1031         struct ieee80211_local *local = rx->local;
1032         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1033         u16 fc, hdrlen, ethertype;
1034         u8 *payload;
1035         u8 dst[ETH_ALEN];
1036         u8 src[ETH_ALEN];
1037         struct sk_buff *skb = rx->skb, *skb2;
1038         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1039         DECLARE_MAC_BUF(mac);
1040         DECLARE_MAC_BUF(mac2);
1041         DECLARE_MAC_BUF(mac3);
1042         DECLARE_MAC_BUF(mac4);
1043
1044         fc = rx->fc;
1045         if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1046                 return TXRX_CONTINUE;
1047
1048         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1049                 return TXRX_DROP;
1050
1051         hdrlen = ieee80211_get_hdrlen(fc);
1052
1053         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1054          * header
1055          * IEEE 802.11 address fields:
1056          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1057          *   0     0   DA    SA    BSSID n/a
1058          *   0     1   DA    BSSID SA    n/a
1059          *   1     0   BSSID SA    DA    n/a
1060          *   1     1   RA    TA    DA    SA
1061          */
1062
1063         switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1064         case IEEE80211_FCTL_TODS:
1065                 /* BSSID SA DA */
1066                 memcpy(dst, hdr->addr3, ETH_ALEN);
1067                 memcpy(src, hdr->addr2, ETH_ALEN);
1068
1069                 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1070                              sdata->type != IEEE80211_IF_TYPE_VLAN)) {
1071                         if (net_ratelimit())
1072                                 printk(KERN_DEBUG "%s: dropped ToDS frame "
1073                                        "(BSSID=%s SA=%s DA=%s)\n",
1074                                        dev->name,
1075                                        print_mac(mac, hdr->addr1),
1076                                        print_mac(mac2, hdr->addr2),
1077                                        print_mac(mac3, hdr->addr3));
1078                         return TXRX_DROP;
1079                 }
1080                 break;
1081         case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1082                 /* RA TA DA SA */
1083                 memcpy(dst, hdr->addr3, ETH_ALEN);
1084                 memcpy(src, hdr->addr4, ETH_ALEN);
1085
1086                 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
1087                         if (net_ratelimit())
1088                                 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
1089                                        "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1090                                        rx->dev->name,
1091                                        print_mac(mac, hdr->addr1),
1092                                        print_mac(mac2, hdr->addr2),
1093                                        print_mac(mac3, hdr->addr3),
1094                                        print_mac(mac4, hdr->addr4));
1095                         return TXRX_DROP;
1096                 }
1097                 break;
1098         case IEEE80211_FCTL_FROMDS:
1099                 /* DA BSSID SA */
1100                 memcpy(dst, hdr->addr1, ETH_ALEN);
1101                 memcpy(src, hdr->addr3, ETH_ALEN);
1102
1103                 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1104                     (is_multicast_ether_addr(dst) &&
1105                      !compare_ether_addr(src, dev->dev_addr)))
1106                         return TXRX_DROP;
1107                 break;
1108         case 0:
1109                 /* DA SA BSSID */
1110                 memcpy(dst, hdr->addr1, ETH_ALEN);
1111                 memcpy(src, hdr->addr2, ETH_ALEN);
1112
1113                 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1114                         if (net_ratelimit()) {
1115                                 printk(KERN_DEBUG "%s: dropped IBSS frame "
1116                                        "(DA=%s SA=%s BSSID=%s)\n",
1117                                        dev->name,
1118                                        print_mac(mac, hdr->addr1),
1119                                        print_mac(mac2, hdr->addr2),
1120                                        print_mac(mac3, hdr->addr3));
1121                         }
1122                         return TXRX_DROP;
1123                 }
1124                 break;
1125         }
1126
1127         payload = skb->data + hdrlen;
1128
1129         if (unlikely(skb->len - hdrlen < 8)) {
1130                 if (net_ratelimit()) {
1131                         printk(KERN_DEBUG "%s: RX too short data frame "
1132                                "payload\n", dev->name);
1133                 }
1134                 return TXRX_DROP;
1135         }
1136
1137         ethertype = (payload[6] << 8) | payload[7];
1138
1139         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1140                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1141                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1142                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1143                  * replace EtherType */
1144                 skb_pull(skb, hdrlen + 6);
1145                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1146                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1147         } else {
1148                 struct ethhdr *ehdr;
1149                 __be16 len;
1150                 skb_pull(skb, hdrlen);
1151                 len = htons(skb->len);
1152                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1153                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1154                 memcpy(ehdr->h_source, src, ETH_ALEN);
1155                 ehdr->h_proto = len;
1156         }
1157         skb->dev = dev;
1158
1159         skb2 = NULL;
1160
1161         dev->stats.rx_packets++;
1162         dev->stats.rx_bytes += skb->len;
1163
1164         if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
1165             || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1166             (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
1167                 if (is_multicast_ether_addr(skb->data)) {
1168                         /* send multicast frames both to higher layers in
1169                          * local net stack and back to the wireless media */
1170                         skb2 = skb_copy(skb, GFP_ATOMIC);
1171                         if (!skb2 && net_ratelimit())
1172                                 printk(KERN_DEBUG "%s: failed to clone "
1173                                        "multicast frame\n", dev->name);
1174                 } else {
1175                         struct sta_info *dsta;
1176                         dsta = sta_info_get(local, skb->data);
1177                         if (dsta && !dsta->dev) {
1178                                 if (net_ratelimit())
1179                                         printk(KERN_DEBUG "Station with null "
1180                                                "dev structure!\n");
1181                         } else if (dsta && dsta->dev == dev) {
1182                                 /* Destination station is associated to this
1183                                  * AP, so send the frame directly to it and
1184                                  * do not pass the frame to local net stack.
1185                                  */
1186                                 skb2 = skb;
1187                                 skb = NULL;
1188                         }
1189                         if (dsta)
1190                                 sta_info_put(dsta);
1191                 }
1192         }
1193
1194         if (skb) {
1195                 /* deliver to local stack */
1196                 skb->protocol = eth_type_trans(skb, dev);
1197                 memset(skb->cb, 0, sizeof(skb->cb));
1198                 netif_rx(skb);
1199         }
1200
1201         if (skb2) {
1202                 /* send to wireless media */
1203                 skb2->protocol = __constant_htons(ETH_P_802_3);
1204                 skb_set_network_header(skb2, 0);
1205                 skb_set_mac_header(skb2, 0);
1206                 dev_queue_xmit(skb2);
1207         }
1208
1209         return TXRX_QUEUED;
1210 }
1211
1212 static ieee80211_txrx_result
1213 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1214 {
1215         struct ieee80211_sub_if_data *sdata;
1216
1217         if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
1218                 return TXRX_DROP;
1219
1220         sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1221         if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1222              sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1223             !rx->local->user_space_mlme)
1224                 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1225         else
1226                 return TXRX_DROP;
1227
1228         return TXRX_QUEUED;
1229 }
1230
1231 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1232                                 struct ieee80211_local *local,
1233                                 ieee80211_rx_handler *handlers,
1234                                 struct ieee80211_txrx_data *rx,
1235                                 struct sta_info *sta)
1236 {
1237         ieee80211_rx_handler *handler;
1238         ieee80211_txrx_result res = TXRX_DROP;
1239
1240         for (handler = handlers; *handler != NULL; handler++) {
1241                 res = (*handler)(rx);
1242
1243                 switch (res) {
1244                 case TXRX_CONTINUE:
1245                         continue;
1246                 case TXRX_DROP:
1247                         I802_DEBUG_INC(local->rx_handlers_drop);
1248                         if (sta)
1249                                 sta->rx_dropped++;
1250                         break;
1251                 case TXRX_QUEUED:
1252                         I802_DEBUG_INC(local->rx_handlers_queued);
1253                         break;
1254                 }
1255                 break;
1256         }
1257
1258         if (res == TXRX_DROP)
1259                 dev_kfree_skb(rx->skb);
1260         return res;
1261 }
1262
1263 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1264                                                 ieee80211_rx_handler *handlers,
1265                                                 struct ieee80211_txrx_data *rx,
1266                                                 struct sta_info *sta)
1267 {
1268         if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1269             TXRX_CONTINUE)
1270                 dev_kfree_skb(rx->skb);
1271 }
1272
1273 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1274                                             struct ieee80211_hdr *hdr,
1275                                             struct sta_info *sta,
1276                                             struct ieee80211_txrx_data *rx)
1277 {
1278         int keyidx, hdrlen;
1279         DECLARE_MAC_BUF(mac);
1280         DECLARE_MAC_BUF(mac2);
1281
1282         hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1283         if (rx->skb->len >= hdrlen + 4)
1284                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1285         else
1286                 keyidx = -1;
1287
1288         if (net_ratelimit())
1289                 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1290                        "failure from %s to %s keyidx=%d\n",
1291                        dev->name, print_mac(mac, hdr->addr2),
1292                        print_mac(mac2, hdr->addr1), keyidx);
1293
1294         if (!sta) {
1295                 /*
1296                  * Some hardware seem to generate incorrect Michael MIC
1297                  * reports; ignore them to avoid triggering countermeasures.
1298                  */
1299                 if (net_ratelimit())
1300                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1301                                "error for unknown address %s\n",
1302                                dev->name, print_mac(mac, hdr->addr2));
1303                 goto ignore;
1304         }
1305
1306         if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1307                 if (net_ratelimit())
1308                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1309                                "error for a frame with no PROTECTED flag (src "
1310                                "%s)\n", dev->name, print_mac(mac, hdr->addr2));
1311                 goto ignore;
1312         }
1313
1314         if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
1315                 /*
1316                  * APs with pairwise keys should never receive Michael MIC
1317                  * errors for non-zero keyidx because these are reserved for
1318                  * group keys and only the AP is sending real multicast
1319                  * frames in the BSS.
1320                  */
1321                 if (net_ratelimit())
1322                         printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1323                                "a frame with non-zero keyidx (%d)"
1324                                " (src %s)\n", dev->name, keyidx,
1325                                print_mac(mac, hdr->addr2));
1326                 goto ignore;
1327         }
1328
1329         if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1330             ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1331              (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1332                 if (net_ratelimit())
1333                         printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1334                                "error for a frame that cannot be encrypted "
1335                                "(fc=0x%04x) (src %s)\n",
1336                                dev->name, rx->fc, print_mac(mac, hdr->addr2));
1337                 goto ignore;
1338         }
1339
1340         mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
1341  ignore:
1342         dev_kfree_skb(rx->skb);
1343         rx->skb = NULL;
1344 }
1345
1346 ieee80211_rx_handler ieee80211_rx_handlers[] =
1347 {
1348         ieee80211_rx_h_if_stats,
1349         ieee80211_rx_h_passive_scan,
1350         ieee80211_rx_h_check,
1351         ieee80211_rx_h_load_key,
1352         ieee80211_rx_h_sta_process,
1353         ieee80211_rx_h_wep_weak_iv_detection,
1354         ieee80211_rx_h_decrypt,
1355         ieee80211_rx_h_defragment,
1356         ieee80211_rx_h_ps_poll,
1357         ieee80211_rx_h_michael_mic_verify,
1358         /* this must be after decryption - so header is counted in MPDU mic
1359          * must be before pae and data, so QOS_DATA format frames
1360          * are not passed to user space by these functions
1361          */
1362         ieee80211_rx_h_remove_qos_control,
1363         ieee80211_rx_h_802_1x_pae,
1364         ieee80211_rx_h_drop_unencrypted,
1365         ieee80211_rx_h_data,
1366         ieee80211_rx_h_mgmt,
1367         NULL
1368 };
1369
1370 /* main receive path */
1371
1372 static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1373                                 u8 *bssid, struct ieee80211_txrx_data *rx,
1374                                 struct ieee80211_hdr *hdr)
1375 {
1376         int multicast = is_multicast_ether_addr(hdr->addr1);
1377
1378         switch (sdata->type) {
1379         case IEEE80211_IF_TYPE_STA:
1380                 if (!bssid)
1381                         return 0;
1382                 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1383                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1384                                 return 0;
1385                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1386                 } else if (!multicast &&
1387                            compare_ether_addr(sdata->dev->dev_addr,
1388                                               hdr->addr1) != 0) {
1389                         if (!(sdata->dev->flags & IFF_PROMISC))
1390                                 return 0;
1391                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1392                 }
1393                 break;
1394         case IEEE80211_IF_TYPE_IBSS:
1395                 if (!bssid)
1396                         return 0;
1397                 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1398                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1399                                 return 0;
1400                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1401                 } else if (!multicast &&
1402                            compare_ether_addr(sdata->dev->dev_addr,
1403                                               hdr->addr1) != 0) {
1404                         if (!(sdata->dev->flags & IFF_PROMISC))
1405                                 return 0;
1406                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1407                 } else if (!rx->sta)
1408                         rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1409                                                          bssid, hdr->addr2);
1410                 break;
1411         case IEEE80211_IF_TYPE_VLAN:
1412         case IEEE80211_IF_TYPE_AP:
1413                 if (!bssid) {
1414                         if (compare_ether_addr(sdata->dev->dev_addr,
1415                                                hdr->addr1))
1416                                 return 0;
1417                 } else if (!ieee80211_bssid_match(bssid,
1418                                         sdata->dev->dev_addr)) {
1419                         if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1420                                 return 0;
1421                         rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1422                 }
1423                 if (sdata->dev == sdata->local->mdev &&
1424                     !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1425                         /* do not receive anything via
1426                          * master device when not scanning */
1427                         return 0;
1428                 break;
1429         case IEEE80211_IF_TYPE_WDS:
1430                 if (bssid ||
1431                     (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1432                         return 0;
1433                 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1434                         return 0;
1435                 break;
1436         case IEEE80211_IF_TYPE_MNTR:
1437                 /* take everything */
1438                 break;
1439         case IEEE80211_IF_TYPE_INVALID:
1440                 /* should never get here */
1441                 WARN_ON(1);
1442                 break;
1443         }
1444
1445         return 1;
1446 }
1447
1448 /*
1449  * This is the receive path handler. It is called by a low level driver when an
1450  * 802.11 MPDU is received from the hardware.
1451  */
1452 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1453                     struct ieee80211_rx_status *status)
1454 {
1455         struct ieee80211_local *local = hw_to_local(hw);
1456         struct ieee80211_sub_if_data *sdata;
1457         struct sta_info *sta;
1458         struct ieee80211_hdr *hdr;
1459         struct ieee80211_txrx_data rx;
1460         u16 type;
1461         int prepres;
1462         struct ieee80211_sub_if_data *prev = NULL;
1463         struct sk_buff *skb_new;
1464         u8 *bssid;
1465
1466         /*
1467          * key references and virtual interfaces are protected using RCU
1468          * and this requires that we are in a read-side RCU section during
1469          * receive processing
1470          */
1471         rcu_read_lock();
1472
1473         /*
1474          * Frames with failed FCS/PLCP checksum are not returned,
1475          * all other frames are returned without radiotap header
1476          * if it was previously present.
1477          * Also, frames with less than 16 bytes are dropped.
1478          */
1479         skb = ieee80211_rx_monitor(local, skb, status);
1480         if (!skb) {
1481                 rcu_read_unlock();
1482                 return;
1483         }
1484
1485         hdr = (struct ieee80211_hdr *) skb->data;
1486         memset(&rx, 0, sizeof(rx));
1487         rx.skb = skb;
1488         rx.local = local;
1489
1490         rx.u.rx.status = status;
1491         rx.fc = le16_to_cpu(hdr->frame_control);
1492         type = rx.fc & IEEE80211_FCTL_FTYPE;
1493
1494         if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1495                 local->dot11ReceivedFragmentCount++;
1496
1497         sta = rx.sta = sta_info_get(local, hdr->addr2);
1498         if (sta) {
1499                 rx.dev = rx.sta->dev;
1500                 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1501         }
1502
1503         if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1504                 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1505                 goto end;
1506         }
1507
1508         if (unlikely(local->sta_scanning))
1509                 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
1510
1511         if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1512                                            sta) != TXRX_CONTINUE)
1513                 goto end;
1514         skb = rx.skb;
1515
1516         if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
1517             !atomic_read(&local->iff_promiscs) &&
1518             !is_multicast_ether_addr(hdr->addr1)) {
1519                 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1520                 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
1521                                              rx.sta);
1522                 sta_info_put(sta);
1523                 rcu_read_unlock();
1524                 return;
1525         }
1526
1527         bssid = ieee80211_get_bssid(hdr, skb->len);
1528
1529         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1530                 if (!netif_running(sdata->dev))
1531                         continue;
1532
1533                 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1534                         continue;
1535
1536                 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1537                 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1538                 /* prepare_for_handlers can change sta */
1539                 sta = rx.sta;
1540
1541                 if (!prepres)
1542                         continue;
1543
1544                 /*
1545                  * frame is destined for this interface, but if it's not
1546                  * also for the previous one we handle that after the
1547                  * loop to avoid copying the SKB once too much
1548                  */
1549
1550                 if (!prev) {
1551                         prev = sdata;
1552                         continue;
1553                 }
1554
1555                 /*
1556                  * frame was destined for the previous interface
1557                  * so invoke RX handlers for it
1558                  */
1559
1560                 skb_new = skb_copy(skb, GFP_ATOMIC);
1561                 if (!skb_new) {
1562                         if (net_ratelimit())
1563                                 printk(KERN_DEBUG "%s: failed to copy "
1564                                        "multicast frame for %s",
1565                                        wiphy_name(local->hw.wiphy),
1566                                        prev->dev->name);
1567                         continue;
1568                 }
1569                 rx.skb = skb_new;
1570                 rx.dev = prev->dev;
1571                 rx.sdata = prev;
1572                 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1573                                              &rx, sta);
1574                 prev = sdata;
1575         }
1576         if (prev) {
1577                 rx.skb = skb;
1578                 rx.dev = prev->dev;
1579                 rx.sdata = prev;
1580                 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1581                                              &rx, sta);
1582         } else
1583                 dev_kfree_skb(skb);
1584
1585  end:
1586         rcu_read_unlock();
1587
1588         if (sta)
1589                 sta_info_put(sta);
1590 }
1591 EXPORT_SYMBOL(__ieee80211_rx);
1592
1593 /* This is a version of the rx handler that can be called from hard irq
1594  * context. Post the skb on the queue and schedule the tasklet */
1595 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1596                           struct ieee80211_rx_status *status)
1597 {
1598         struct ieee80211_local *local = hw_to_local(hw);
1599
1600         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1601
1602         skb->dev = local->mdev;
1603         /* copy status into skb->cb for use by tasklet */
1604         memcpy(skb->cb, status, sizeof(*status));
1605         skb->pkt_type = IEEE80211_RX_MSG;
1606         skb_queue_tail(&local->skb_queue, skb);
1607         tasklet_schedule(&local->tasklet);
1608 }
1609 EXPORT_SYMBOL(ieee80211_rx_irqsafe);