mac80211: implement basic background scanning
[pandora-kernel.git] / 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/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/rcupdate.h>
18 #include <net/mac80211.h>
19 #include <net/ieee80211_radiotap.h>
20
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "led.h"
24 #include "mesh.h"
25 #include "wep.h"
26 #include "wpa.h"
27 #include "tkip.h"
28 #include "wme.h"
29
30 static u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
31                                            struct tid_ampdu_rx *tid_agg_rx,
32                                            struct sk_buff *skb,
33                                            u16 mpdu_seq_num,
34                                            int bar_req);
35 /*
36  * monitor mode reception
37  *
38  * This function cleans up the SKB, i.e. it removes all the stuff
39  * only useful for monitoring.
40  */
41 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
42                                            struct sk_buff *skb,
43                                            int rtap_len)
44 {
45         skb_pull(skb, rtap_len);
46
47         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
48                 if (likely(skb->len > FCS_LEN))
49                         skb_trim(skb, skb->len - FCS_LEN);
50                 else {
51                         /* driver bug */
52                         WARN_ON(1);
53                         dev_kfree_skb(skb);
54                         skb = NULL;
55                 }
56         }
57
58         return skb;
59 }
60
61 static inline int should_drop_frame(struct sk_buff *skb,
62                                     int present_fcs_len,
63                                     int radiotap_len)
64 {
65         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
66         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
67
68         if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
69                 return 1;
70         if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
71                 return 1;
72         if (ieee80211_is_ctl(hdr->frame_control) &&
73             !ieee80211_is_pspoll(hdr->frame_control) &&
74             !ieee80211_is_back_req(hdr->frame_control))
75                 return 1;
76         return 0;
77 }
78
79 static int
80 ieee80211_rx_radiotap_len(struct ieee80211_local *local,
81                           struct ieee80211_rx_status *status)
82 {
83         int len;
84
85         /* always present fields */
86         len = sizeof(struct ieee80211_radiotap_header) + 9;
87
88         if (status->flag & RX_FLAG_TSFT)
89                 len += 8;
90         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
91                 len += 1;
92         if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
93                 len += 1;
94
95         if (len & 1) /* padding for RX_FLAGS if necessary */
96                 len++;
97
98         /* make sure radiotap starts at a naturally aligned address */
99         if (len % 8)
100                 len = roundup(len, 8);
101
102         return len;
103 }
104
105 /*
106  * ieee80211_add_rx_radiotap_header - add radiotap header
107  *
108  * add a radiotap header containing all the fields which the hardware provided.
109  */
110 static void
111 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
112                                  struct sk_buff *skb,
113                                  struct ieee80211_rate *rate,
114                                  int rtap_len)
115 {
116         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
117         struct ieee80211_radiotap_header *rthdr;
118         unsigned char *pos;
119
120         rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
121         memset(rthdr, 0, rtap_len);
122
123         /* radiotap header, set always present flags */
124         rthdr->it_present =
125                 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
126                             (1 << IEEE80211_RADIOTAP_CHANNEL) |
127                             (1 << IEEE80211_RADIOTAP_ANTENNA) |
128                             (1 << IEEE80211_RADIOTAP_RX_FLAGS));
129         rthdr->it_len = cpu_to_le16(rtap_len);
130
131         pos = (unsigned char *)(rthdr+1);
132
133         /* the order of the following fields is important */
134
135         /* IEEE80211_RADIOTAP_TSFT */
136         if (status->flag & RX_FLAG_TSFT) {
137                 *(__le64 *)pos = cpu_to_le64(status->mactime);
138                 rthdr->it_present |=
139                         cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
140                 pos += 8;
141         }
142
143         /* IEEE80211_RADIOTAP_FLAGS */
144         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
145                 *pos |= IEEE80211_RADIOTAP_F_FCS;
146         if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
147                 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
148         if (status->flag & RX_FLAG_SHORTPRE)
149                 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
150         pos++;
151
152         /* IEEE80211_RADIOTAP_RATE */
153         if (status->flag & RX_FLAG_HT) {
154                 /*
155                  * TODO: add following information into radiotap header once
156                  * suitable fields are defined for it:
157                  * - MCS index (status->rate_idx)
158                  * - HT40 (status->flag & RX_FLAG_40MHZ)
159                  * - short-GI (status->flag & RX_FLAG_SHORT_GI)
160                  */
161                 *pos = 0;
162         } else {
163                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
164                 *pos = rate->bitrate / 5;
165         }
166         pos++;
167
168         /* IEEE80211_RADIOTAP_CHANNEL */
169         *(__le16 *)pos = cpu_to_le16(status->freq);
170         pos += 2;
171         if (status->band == IEEE80211_BAND_5GHZ)
172                 *(__le16 *)pos = cpu_to_le16(IEEE80211_CHAN_OFDM |
173                                              IEEE80211_CHAN_5GHZ);
174         else if (rate->flags & IEEE80211_RATE_ERP_G)
175                 *(__le16 *)pos = cpu_to_le16(IEEE80211_CHAN_OFDM |
176                                              IEEE80211_CHAN_2GHZ);
177         else
178                 *(__le16 *)pos = cpu_to_le16(IEEE80211_CHAN_CCK |
179                                              IEEE80211_CHAN_2GHZ);
180         pos += 2;
181
182         /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
183         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
184                 *pos = status->signal;
185                 rthdr->it_present |=
186                         cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
187                 pos++;
188         }
189
190         /* IEEE80211_RADIOTAP_DBM_ANTNOISE */
191         if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
192                 *pos = status->noise;
193                 rthdr->it_present |=
194                         cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTNOISE);
195                 pos++;
196         }
197
198         /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
199
200         /* IEEE80211_RADIOTAP_ANTENNA */
201         *pos = status->antenna;
202         pos++;
203
204         /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
205
206         /* IEEE80211_RADIOTAP_RX_FLAGS */
207         /* ensure 2 byte alignment for the 2 byte field as required */
208         if ((pos - (unsigned char *)rthdr) & 1)
209                 pos++;
210         if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
211                 *(__le16 *)pos |= cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADPLCP);
212         pos += 2;
213 }
214
215 /*
216  * This function copies a received frame to all monitor interfaces and
217  * returns a cleaned-up SKB that no longer includes the FCS nor the
218  * radiotap header the driver might have added.
219  */
220 static struct sk_buff *
221 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
222                      struct ieee80211_rate *rate)
223 {
224         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
225         struct ieee80211_sub_if_data *sdata;
226         int needed_headroom = 0;
227         struct sk_buff *skb, *skb2;
228         struct net_device *prev_dev = NULL;
229         int present_fcs_len = 0;
230         int rtap_len = 0;
231
232         /*
233          * First, we may need to make a copy of the skb because
234          *  (1) we need to modify it for radiotap (if not present), and
235          *  (2) the other RX handlers will modify the skb we got.
236          *
237          * We don't need to, of course, if we aren't going to return
238          * the SKB because it has a bad FCS/PLCP checksum.
239          */
240         if (status->flag & RX_FLAG_RADIOTAP)
241                 rtap_len = ieee80211_get_radiotap_len(origskb->data);
242         else
243                 /* room for the radiotap header based on driver features */
244                 needed_headroom = ieee80211_rx_radiotap_len(local, status);
245
246         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
247                 present_fcs_len = FCS_LEN;
248
249         if (!local->monitors) {
250                 if (should_drop_frame(origskb, present_fcs_len, rtap_len)) {
251                         dev_kfree_skb(origskb);
252                         return NULL;
253                 }
254
255                 return remove_monitor_info(local, origskb, rtap_len);
256         }
257
258         if (should_drop_frame(origskb, present_fcs_len, rtap_len)) {
259                 /* only need to expand headroom if necessary */
260                 skb = origskb;
261                 origskb = NULL;
262
263                 /*
264                  * This shouldn't trigger often because most devices have an
265                  * RX header they pull before we get here, and that should
266                  * be big enough for our radiotap information. We should
267                  * probably export the length to drivers so that we can have
268                  * them allocate enough headroom to start with.
269                  */
270                 if (skb_headroom(skb) < needed_headroom &&
271                     pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
272                         dev_kfree_skb(skb);
273                         return NULL;
274                 }
275         } else {
276                 /*
277                  * Need to make a copy and possibly remove radiotap header
278                  * and FCS from the original.
279                  */
280                 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
281
282                 origskb = remove_monitor_info(local, origskb, rtap_len);
283
284                 if (!skb)
285                         return origskb;
286         }
287
288         /* if necessary, prepend radiotap information */
289         if (!(status->flag & RX_FLAG_RADIOTAP))
290                 ieee80211_add_rx_radiotap_header(local, skb, rate,
291                                                  needed_headroom);
292
293         skb_reset_mac_header(skb);
294         skb->ip_summed = CHECKSUM_UNNECESSARY;
295         skb->pkt_type = PACKET_OTHERHOST;
296         skb->protocol = htons(ETH_P_802_2);
297
298         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
299                 if (!netif_running(sdata->dev))
300                         continue;
301
302                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
303                         continue;
304
305                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
306                         continue;
307
308                 if (prev_dev) {
309                         skb2 = skb_clone(skb, GFP_ATOMIC);
310                         if (skb2) {
311                                 skb2->dev = prev_dev;
312                                 netif_rx(skb2);
313                         }
314                 }
315
316                 prev_dev = sdata->dev;
317                 sdata->dev->stats.rx_packets++;
318                 sdata->dev->stats.rx_bytes += skb->len;
319         }
320
321         if (prev_dev) {
322                 skb->dev = prev_dev;
323                 netif_rx(skb);
324         } else
325                 dev_kfree_skb(skb);
326
327         return origskb;
328 }
329
330
331 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
332 {
333         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
334         int tid;
335
336         /* does the frame have a qos control field? */
337         if (ieee80211_is_data_qos(hdr->frame_control)) {
338                 u8 *qc = ieee80211_get_qos_ctl(hdr);
339                 /* frame has qos control */
340                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
341                 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
342                         rx->flags |= IEEE80211_RX_AMSDU;
343                 else
344                         rx->flags &= ~IEEE80211_RX_AMSDU;
345         } else {
346                 /*
347                  * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
348                  *
349                  *      Sequence numbers for management frames, QoS data
350                  *      frames with a broadcast/multicast address in the
351                  *      Address 1 field, and all non-QoS data frames sent
352                  *      by QoS STAs are assigned using an additional single
353                  *      modulo-4096 counter, [...]
354                  *
355                  * We also use that counter for non-QoS STAs.
356                  */
357                 tid = NUM_RX_DATA_QUEUES - 1;
358         }
359
360         rx->queue = tid;
361         /* Set skb->priority to 1d tag if highest order bit of TID is not set.
362          * For now, set skb->priority to 0 for other cases. */
363         rx->skb->priority = (tid > 7) ? 0 : tid;
364 }
365
366 /**
367  * DOC: Packet alignment
368  *
369  * Drivers always need to pass packets that are aligned to two-byte boundaries
370  * to the stack.
371  *
372  * Additionally, should, if possible, align the payload data in a way that
373  * guarantees that the contained IP header is aligned to a four-byte
374  * boundary. In the case of regular frames, this simply means aligning the
375  * payload to a four-byte boundary (because either the IP header is directly
376  * contained, or IV/RFC1042 headers that have a length divisible by four are
377  * in front of it).
378  *
379  * With A-MSDU frames, however, the payload data address must yield two modulo
380  * four because there are 14-byte 802.3 headers within the A-MSDU frames that
381  * push the IP header further back to a multiple of four again. Thankfully, the
382  * specs were sane enough this time around to require padding each A-MSDU
383  * subframe to a length that is a multiple of four.
384  *
385  * Padding like Atheros hardware adds which is inbetween the 802.11 header and
386  * the payload is not supported, the driver is required to move the 802.11
387  * header to be directly in front of the payload in that case.
388  */
389 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
390 {
391         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
392         int hdrlen;
393
394 #ifndef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
395         return;
396 #endif
397
398         if (WARN_ONCE((unsigned long)rx->skb->data & 1,
399                       "unaligned packet at 0x%p\n", rx->skb->data))
400                 return;
401
402         if (!ieee80211_is_data_present(hdr->frame_control))
403                 return;
404
405         hdrlen = ieee80211_hdrlen(hdr->frame_control);
406         if (rx->flags & IEEE80211_RX_AMSDU)
407                 hdrlen += ETH_HLEN;
408         WARN_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3,
409                   "unaligned IP payload at 0x%p\n", rx->skb->data + hdrlen);
410 }
411
412
413 /* rx handlers */
414
415 static ieee80211_rx_result debug_noinline
416 ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
417 {
418         struct ieee80211_local *local = rx->local;
419         struct sk_buff *skb = rx->skb;
420
421         if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
422                 return ieee80211_scan_rx(rx->sdata, skb);
423
424         if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
425                      (rx->flags & IEEE80211_RX_IN_SCAN))) {
426                 /* drop all the other packets during a software scan anyway */
427                 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
428                         dev_kfree_skb(skb);
429                 return RX_QUEUED;
430         }
431
432         if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
433                 /* scanning finished during invoking of handlers */
434                 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
435                 return RX_DROP_UNUSABLE;
436         }
437
438         return RX_CONTINUE;
439 }
440
441
442 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
443 {
444         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
445
446         if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
447                 return 0;
448
449         return ieee80211_is_robust_mgmt_frame(hdr);
450 }
451
452
453 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
454 {
455         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
456
457         if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
458                 return 0;
459
460         return ieee80211_is_robust_mgmt_frame(hdr);
461 }
462
463
464 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
465 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
466 {
467         struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
468         struct ieee80211_mmie *mmie;
469
470         if (skb->len < 24 + sizeof(*mmie) ||
471             !is_multicast_ether_addr(hdr->da))
472                 return -1;
473
474         if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
475                 return -1; /* not a robust management frame */
476
477         mmie = (struct ieee80211_mmie *)
478                 (skb->data + skb->len - sizeof(*mmie));
479         if (mmie->element_id != WLAN_EID_MMIE ||
480             mmie->length != sizeof(*mmie) - 2)
481                 return -1;
482
483         return le16_to_cpu(mmie->key_id);
484 }
485
486
487 static ieee80211_rx_result
488 ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
489 {
490         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
491         unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
492
493         if (ieee80211_is_data(hdr->frame_control)) {
494                 if (!ieee80211_has_a4(hdr->frame_control))
495                         return RX_DROP_MONITOR;
496                 if (memcmp(hdr->addr4, rx->dev->dev_addr, ETH_ALEN) == 0)
497                         return RX_DROP_MONITOR;
498         }
499
500         /* If there is not an established peer link and this is not a peer link
501          * establisment frame, beacon or probe, drop the frame.
502          */
503
504         if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
505                 struct ieee80211_mgmt *mgmt;
506
507                 if (!ieee80211_is_mgmt(hdr->frame_control))
508                         return RX_DROP_MONITOR;
509
510                 if (ieee80211_is_action(hdr->frame_control)) {
511                         mgmt = (struct ieee80211_mgmt *)hdr;
512                         if (mgmt->u.action.category != PLINK_CATEGORY)
513                                 return RX_DROP_MONITOR;
514                         return RX_CONTINUE;
515                 }
516
517                 if (ieee80211_is_probe_req(hdr->frame_control) ||
518                     ieee80211_is_probe_resp(hdr->frame_control) ||
519                     ieee80211_is_beacon(hdr->frame_control))
520                         return RX_CONTINUE;
521
522                 return RX_DROP_MONITOR;
523
524         }
525
526 #define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
527
528         if (ieee80211_is_data(hdr->frame_control) &&
529             is_multicast_ether_addr(hdr->addr1) &&
530             mesh_rmc_check(hdr->addr4, msh_h_get(hdr, hdrlen), rx->sdata))
531                 return RX_DROP_MONITOR;
532 #undef msh_h_get
533
534         return RX_CONTINUE;
535 }
536
537
538 static ieee80211_rx_result debug_noinline
539 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
540 {
541         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
542
543         /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
544         if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
545                 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
546                              rx->sta->last_seq_ctrl[rx->queue] ==
547                              hdr->seq_ctrl)) {
548                         if (rx->flags & IEEE80211_RX_RA_MATCH) {
549                                 rx->local->dot11FrameDuplicateCount++;
550                                 rx->sta->num_duplicates++;
551                         }
552                         return RX_DROP_MONITOR;
553                 } else
554                         rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
555         }
556
557         if (unlikely(rx->skb->len < 16)) {
558                 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
559                 return RX_DROP_MONITOR;
560         }
561
562         /* Drop disallowed frame classes based on STA auth/assoc state;
563          * IEEE 802.11, Chap 5.5.
564          *
565          * mac80211 filters only based on association state, i.e. it drops
566          * Class 3 frames from not associated stations. hostapd sends
567          * deauth/disassoc frames when needed. In addition, hostapd is
568          * responsible for filtering on both auth and assoc states.
569          */
570
571         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
572                 return ieee80211_rx_mesh_check(rx);
573
574         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
575                       ieee80211_is_pspoll(hdr->frame_control)) &&
576                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
577                      (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
578                 if ((!ieee80211_has_fromds(hdr->frame_control) &&
579                      !ieee80211_has_tods(hdr->frame_control) &&
580                      ieee80211_is_data(hdr->frame_control)) ||
581                     !(rx->flags & IEEE80211_RX_RA_MATCH)) {
582                         /* Drop IBSS frames and frames for other hosts
583                          * silently. */
584                         return RX_DROP_MONITOR;
585                 }
586
587                 return RX_DROP_MONITOR;
588         }
589
590         return RX_CONTINUE;
591 }
592
593
594 static ieee80211_rx_result debug_noinline
595 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
596 {
597         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
598         int keyidx;
599         int hdrlen;
600         ieee80211_rx_result result = RX_DROP_UNUSABLE;
601         struct ieee80211_key *stakey = NULL;
602         int mmie_keyidx = -1;
603
604         /*
605          * Key selection 101
606          *
607          * There are four types of keys:
608          *  - GTK (group keys)
609          *  - IGTK (group keys for management frames)
610          *  - PTK (pairwise keys)
611          *  - STK (station-to-station pairwise keys)
612          *
613          * When selecting a key, we have to distinguish between multicast
614          * (including broadcast) and unicast frames, the latter can only
615          * use PTKs and STKs while the former always use GTKs and IGTKs.
616          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
617          * unicast frames can also use key indices like GTKs. Hence, if we
618          * don't have a PTK/STK we check the key index for a WEP key.
619          *
620          * Note that in a regular BSS, multicast frames are sent by the
621          * AP only, associated stations unicast the frame to the AP first
622          * which then multicasts it on their behalf.
623          *
624          * There is also a slight problem in IBSS mode: GTKs are negotiated
625          * with each station, that is something we don't currently handle.
626          * The spec seems to expect that one negotiates the same key with
627          * every station but there's no such requirement; VLANs could be
628          * possible.
629          */
630
631         /*
632          * No point in finding a key and decrypting if the frame is neither
633          * addressed to us nor a multicast frame.
634          */
635         if (!(rx->flags & IEEE80211_RX_RA_MATCH))
636                 return RX_CONTINUE;
637
638         if (rx->sta)
639                 stakey = rcu_dereference(rx->sta->key);
640
641         if (!ieee80211_has_protected(hdr->frame_control))
642                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
643
644         if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
645                 rx->key = stakey;
646                 /* Skip decryption if the frame is not protected. */
647                 if (!ieee80211_has_protected(hdr->frame_control))
648                         return RX_CONTINUE;
649         } else if (mmie_keyidx >= 0) {
650                 /* Broadcast/multicast robust management frame / BIP */
651                 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
652                     (rx->status->flag & RX_FLAG_IV_STRIPPED))
653                         return RX_CONTINUE;
654
655                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
656                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
657                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
658                 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
659         } else if (!ieee80211_has_protected(hdr->frame_control)) {
660                 /*
661                  * The frame was not protected, so skip decryption. However, we
662                  * need to set rx->key if there is a key that could have been
663                  * used so that the frame may be dropped if encryption would
664                  * have been expected.
665                  */
666                 struct ieee80211_key *key = NULL;
667                 if (ieee80211_is_mgmt(hdr->frame_control) &&
668                     is_multicast_ether_addr(hdr->addr1) &&
669                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
670                         rx->key = key;
671                 else if ((key = rcu_dereference(rx->sdata->default_key)))
672                         rx->key = key;
673                 return RX_CONTINUE;
674         } else {
675                 /*
676                  * The device doesn't give us the IV so we won't be
677                  * able to look up the key. That's ok though, we
678                  * don't need to decrypt the frame, we just won't
679                  * be able to keep statistics accurate.
680                  * Except for key threshold notifications, should
681                  * we somehow allow the driver to tell us which key
682                  * the hardware used if this flag is set?
683                  */
684                 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
685                     (rx->status->flag & RX_FLAG_IV_STRIPPED))
686                         return RX_CONTINUE;
687
688                 hdrlen = ieee80211_hdrlen(hdr->frame_control);
689
690                 if (rx->skb->len < 8 + hdrlen)
691                         return RX_DROP_UNUSABLE; /* TODO: count this? */
692
693                 /*
694                  * no need to call ieee80211_wep_get_keyidx,
695                  * it verifies a bunch of things we've done already
696                  */
697                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
698
699                 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
700
701                 /*
702                  * RSNA-protected unicast frames should always be sent with
703                  * pairwise or station-to-station keys, but for WEP we allow
704                  * using a key index as well.
705                  */
706                 if (rx->key && rx->key->conf.alg != ALG_WEP &&
707                     !is_multicast_ether_addr(hdr->addr1))
708                         rx->key = NULL;
709         }
710
711         if (rx->key) {
712                 rx->key->tx_rx_count++;
713                 /* TODO: add threshold stuff again */
714         } else {
715                 return RX_DROP_MONITOR;
716         }
717
718         /* Check for weak IVs if possible */
719         if (rx->sta && rx->key->conf.alg == ALG_WEP &&
720             ieee80211_is_data(hdr->frame_control) &&
721             (!(rx->status->flag & RX_FLAG_IV_STRIPPED) ||
722              !(rx->status->flag & RX_FLAG_DECRYPTED)) &&
723             ieee80211_wep_is_weak_iv(rx->skb, rx->key))
724                 rx->sta->wep_weak_iv_count++;
725
726         switch (rx->key->conf.alg) {
727         case ALG_WEP:
728                 result = ieee80211_crypto_wep_decrypt(rx);
729                 break;
730         case ALG_TKIP:
731                 result = ieee80211_crypto_tkip_decrypt(rx);
732                 break;
733         case ALG_CCMP:
734                 result = ieee80211_crypto_ccmp_decrypt(rx);
735                 break;
736         case ALG_AES_CMAC:
737                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
738                 break;
739         }
740
741         /* either the frame has been decrypted or will be dropped */
742         rx->status->flag |= RX_FLAG_DECRYPTED;
743
744         return result;
745 }
746
747 static ieee80211_rx_result debug_noinline
748 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
749 {
750         struct ieee80211_local *local;
751         struct ieee80211_hdr *hdr;
752         struct sk_buff *skb;
753
754         local = rx->local;
755         skb = rx->skb;
756         hdr = (struct ieee80211_hdr *) skb->data;
757
758         if (!local->pspolling)
759                 return RX_CONTINUE;
760
761         if (!ieee80211_has_fromds(hdr->frame_control))
762                 /* this is not from AP */
763                 return RX_CONTINUE;
764
765         if (!ieee80211_is_data(hdr->frame_control))
766                 return RX_CONTINUE;
767
768         if (!ieee80211_has_moredata(hdr->frame_control)) {
769                 /* AP has no more frames buffered for us */
770                 local->pspolling = false;
771                 return RX_CONTINUE;
772         }
773
774         /* more data bit is set, let's request a new frame from the AP */
775         ieee80211_send_pspoll(local, rx->sdata);
776
777         return RX_CONTINUE;
778 }
779
780 static void ap_sta_ps_start(struct sta_info *sta)
781 {
782         struct ieee80211_sub_if_data *sdata = sta->sdata;
783         struct ieee80211_local *local = sdata->local;
784
785         atomic_inc(&sdata->bss->num_sta_ps);
786         set_and_clear_sta_flags(sta, WLAN_STA_PS, WLAN_STA_PSPOLL);
787         drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
788 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
789         printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
790                sdata->dev->name, sta->sta.addr, sta->sta.aid);
791 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
792 }
793
794 static int ap_sta_ps_end(struct sta_info *sta)
795 {
796         struct ieee80211_sub_if_data *sdata = sta->sdata;
797         struct ieee80211_local *local = sdata->local;
798         int sent, buffered;
799
800         atomic_dec(&sdata->bss->num_sta_ps);
801
802         clear_sta_flags(sta, WLAN_STA_PS | WLAN_STA_PSPOLL);
803         drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
804
805         if (!skb_queue_empty(&sta->ps_tx_buf))
806                 sta_info_clear_tim_bit(sta);
807
808 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
809         printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
810                sdata->dev->name, sta->sta.addr, sta->sta.aid);
811 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
812
813         /* Send all buffered frames to the station */
814         sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
815         buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
816         sent += buffered;
817         local->total_ps_buffered -= buffered;
818
819 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
820         printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
821                "since STA not sleeping anymore\n", sdata->dev->name,
822                sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
823 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
824
825         return sent;
826 }
827
828 static ieee80211_rx_result debug_noinline
829 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
830 {
831         struct sta_info *sta = rx->sta;
832         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
833
834         if (!sta)
835                 return RX_CONTINUE;
836
837         /*
838          * Update last_rx only for IBSS packets which are for the current
839          * BSSID to avoid keeping the current IBSS network alive in cases
840          * where other STAs start using different BSSID.
841          */
842         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
843                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
844                                                 NL80211_IFTYPE_ADHOC);
845                 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
846                         sta->last_rx = jiffies;
847         } else if (!is_multicast_ether_addr(hdr->addr1)) {
848                 /*
849                  * Mesh beacons will update last_rx when if they are found to
850                  * match the current local configuration when processed.
851                  */
852                 sta->last_rx = jiffies;
853         }
854
855         if (!(rx->flags & IEEE80211_RX_RA_MATCH))
856                 return RX_CONTINUE;
857
858         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
859                 ieee80211_sta_rx_notify(rx->sdata, hdr);
860
861         sta->rx_fragments++;
862         sta->rx_bytes += rx->skb->len;
863         sta->last_signal = rx->status->signal;
864         sta->last_qual = rx->status->qual;
865         sta->last_noise = rx->status->noise;
866
867         /*
868          * Change STA power saving mode only at the end of a frame
869          * exchange sequence.
870          */
871         if (!ieee80211_has_morefrags(hdr->frame_control) &&
872             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
873              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
874                 if (test_sta_flags(sta, WLAN_STA_PS)) {
875                         /*
876                          * Ignore doze->wake transitions that are
877                          * indicated by non-data frames, the standard
878                          * is unclear here, but for example going to
879                          * PS mode and then scanning would cause a
880                          * doze->wake transition for the probe request,
881                          * and that is clearly undesirable.
882                          */
883                         if (ieee80211_is_data(hdr->frame_control) &&
884                             !ieee80211_has_pm(hdr->frame_control))
885                                 rx->sent_ps_buffered += ap_sta_ps_end(sta);
886                 } else {
887                         if (ieee80211_has_pm(hdr->frame_control))
888                                 ap_sta_ps_start(sta);
889                 }
890         }
891
892         /* Drop data::nullfunc frames silently, since they are used only to
893          * control station power saving mode. */
894         if (ieee80211_is_nullfunc(hdr->frame_control)) {
895                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
896                 /* Update counter and free packet here to avoid counting this
897                  * as a dropped packed. */
898                 sta->rx_packets++;
899                 dev_kfree_skb(rx->skb);
900                 return RX_QUEUED;
901         }
902
903         return RX_CONTINUE;
904 } /* ieee80211_rx_h_sta_process */
905
906 static inline struct ieee80211_fragment_entry *
907 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
908                          unsigned int frag, unsigned int seq, int rx_queue,
909                          struct sk_buff **skb)
910 {
911         struct ieee80211_fragment_entry *entry;
912         int idx;
913
914         idx = sdata->fragment_next;
915         entry = &sdata->fragments[sdata->fragment_next++];
916         if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
917                 sdata->fragment_next = 0;
918
919         if (!skb_queue_empty(&entry->skb_list)) {
920 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
921                 struct ieee80211_hdr *hdr =
922                         (struct ieee80211_hdr *) entry->skb_list.next->data;
923                 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
924                        "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
925                        "addr1=%pM addr2=%pM\n",
926                        sdata->dev->name, idx,
927                        jiffies - entry->first_frag_time, entry->seq,
928                        entry->last_frag, hdr->addr1, hdr->addr2);
929 #endif
930                 __skb_queue_purge(&entry->skb_list);
931         }
932
933         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
934         *skb = NULL;
935         entry->first_frag_time = jiffies;
936         entry->seq = seq;
937         entry->rx_queue = rx_queue;
938         entry->last_frag = frag;
939         entry->ccmp = 0;
940         entry->extra_len = 0;
941
942         return entry;
943 }
944
945 static inline struct ieee80211_fragment_entry *
946 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
947                           unsigned int frag, unsigned int seq,
948                           int rx_queue, struct ieee80211_hdr *hdr)
949 {
950         struct ieee80211_fragment_entry *entry;
951         int i, idx;
952
953         idx = sdata->fragment_next;
954         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
955                 struct ieee80211_hdr *f_hdr;
956
957                 idx--;
958                 if (idx < 0)
959                         idx = IEEE80211_FRAGMENT_MAX - 1;
960
961                 entry = &sdata->fragments[idx];
962                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
963                     entry->rx_queue != rx_queue ||
964                     entry->last_frag + 1 != frag)
965                         continue;
966
967                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
968
969                 /*
970                  * Check ftype and addresses are equal, else check next fragment
971                  */
972                 if (((hdr->frame_control ^ f_hdr->frame_control) &
973                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
974                     compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
975                     compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
976                         continue;
977
978                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
979                         __skb_queue_purge(&entry->skb_list);
980                         continue;
981                 }
982                 return entry;
983         }
984
985         return NULL;
986 }
987
988 static ieee80211_rx_result debug_noinline
989 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
990 {
991         struct ieee80211_hdr *hdr;
992         u16 sc;
993         __le16 fc;
994         unsigned int frag, seq;
995         struct ieee80211_fragment_entry *entry;
996         struct sk_buff *skb;
997
998         hdr = (struct ieee80211_hdr *)rx->skb->data;
999         fc = hdr->frame_control;
1000         sc = le16_to_cpu(hdr->seq_ctrl);
1001         frag = sc & IEEE80211_SCTL_FRAG;
1002
1003         if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
1004                    (rx->skb)->len < 24 ||
1005                    is_multicast_ether_addr(hdr->addr1))) {
1006                 /* not fragmented */
1007                 goto out;
1008         }
1009         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1010
1011         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1012
1013         if (frag == 0) {
1014                 /* This is the first fragment of a new frame. */
1015                 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1016                                                  rx->queue, &(rx->skb));
1017                 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
1018                     ieee80211_has_protected(fc)) {
1019                         /* Store CCMP PN so that we can verify that the next
1020                          * fragment has a sequential PN value. */
1021                         entry->ccmp = 1;
1022                         memcpy(entry->last_pn,
1023                                rx->key->u.ccmp.rx_pn[rx->queue],
1024                                CCMP_PN_LEN);
1025                 }
1026                 return RX_QUEUED;
1027         }
1028
1029         /* This is a fragment for a frame that should already be pending in
1030          * fragment cache. Add this fragment to the end of the pending entry.
1031          */
1032         entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
1033         if (!entry) {
1034                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1035                 return RX_DROP_MONITOR;
1036         }
1037
1038         /* Verify that MPDUs within one MSDU have sequential PN values.
1039          * (IEEE 802.11i, 8.3.3.4.5) */
1040         if (entry->ccmp) {
1041                 int i;
1042                 u8 pn[CCMP_PN_LEN], *rpn;
1043                 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
1044                         return RX_DROP_UNUSABLE;
1045                 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1046                 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1047                         pn[i]++;
1048                         if (pn[i])
1049                                 break;
1050                 }
1051                 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
1052                 if (memcmp(pn, rpn, CCMP_PN_LEN))
1053                         return RX_DROP_UNUSABLE;
1054                 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1055         }
1056
1057         skb_pull(rx->skb, ieee80211_hdrlen(fc));
1058         __skb_queue_tail(&entry->skb_list, rx->skb);
1059         entry->last_frag = frag;
1060         entry->extra_len += rx->skb->len;
1061         if (ieee80211_has_morefrags(fc)) {
1062                 rx->skb = NULL;
1063                 return RX_QUEUED;
1064         }
1065
1066         rx->skb = __skb_dequeue(&entry->skb_list);
1067         if (skb_tailroom(rx->skb) < entry->extra_len) {
1068                 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1069                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1070                                               GFP_ATOMIC))) {
1071                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1072                         __skb_queue_purge(&entry->skb_list);
1073                         return RX_DROP_UNUSABLE;
1074                 }
1075         }
1076         while ((skb = __skb_dequeue(&entry->skb_list))) {
1077                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1078                 dev_kfree_skb(skb);
1079         }
1080
1081         /* Complete frame has been reassembled - process it now */
1082         rx->flags |= IEEE80211_RX_FRAGMENTED;
1083
1084  out:
1085         if (rx->sta)
1086                 rx->sta->rx_packets++;
1087         if (is_multicast_ether_addr(hdr->addr1))
1088                 rx->local->dot11MulticastReceivedFrameCount++;
1089         else
1090                 ieee80211_led_rx(rx->local);
1091         return RX_CONTINUE;
1092 }
1093
1094 static ieee80211_rx_result debug_noinline
1095 ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
1096 {
1097         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1098         struct sk_buff *skb;
1099         int no_pending_pkts;
1100         __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
1101
1102         if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
1103                    !(rx->flags & IEEE80211_RX_RA_MATCH)))
1104                 return RX_CONTINUE;
1105
1106         if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1107             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1108                 return RX_DROP_UNUSABLE;
1109
1110         skb = skb_dequeue(&rx->sta->tx_filtered);
1111         if (!skb) {
1112                 skb = skb_dequeue(&rx->sta->ps_tx_buf);
1113                 if (skb)
1114                         rx->local->total_ps_buffered--;
1115         }
1116         no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
1117                 skb_queue_empty(&rx->sta->ps_tx_buf);
1118
1119         if (skb) {
1120                 struct ieee80211_hdr *hdr =
1121                         (struct ieee80211_hdr *) skb->data;
1122
1123                 /*
1124                  * Tell TX path to send one frame even though the STA may
1125                  * still remain is PS mode after this frame exchange.
1126                  */
1127                 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
1128
1129 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1130                 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
1131                        rx->sta->sta.addr, rx->sta->sta.aid,
1132                        skb_queue_len(&rx->sta->ps_tx_buf));
1133 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1134
1135                 /* Use MoreData flag to indicate whether there are more
1136                  * buffered frames for this STA */
1137                 if (no_pending_pkts)
1138                         hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1139                 else
1140                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1141
1142                 dev_queue_xmit(skb);
1143
1144                 if (no_pending_pkts)
1145                         sta_info_clear_tim_bit(rx->sta);
1146 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1147         } else if (!rx->sent_ps_buffered) {
1148                 /*
1149                  * FIXME: This can be the result of a race condition between
1150                  *        us expiring a frame and the station polling for it.
1151                  *        Should we send it a null-func frame indicating we
1152                  *        have nothing buffered for it?
1153                  */
1154                 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
1155                        "though there are no buffered frames for it\n",
1156                        rx->dev->name, rx->sta->sta.addr);
1157 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1158         }
1159
1160         /* Free PS Poll skb here instead of returning RX_DROP that would
1161          * count as an dropped frame. */
1162         dev_kfree_skb(rx->skb);
1163
1164         return RX_QUEUED;
1165 }
1166
1167 static ieee80211_rx_result debug_noinline
1168 ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
1169 {
1170         u8 *data = rx->skb->data;
1171         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
1172
1173         if (!ieee80211_is_data_qos(hdr->frame_control))
1174                 return RX_CONTINUE;
1175
1176         /* remove the qos control field, update frame type and meta-data */
1177         memmove(data + IEEE80211_QOS_CTL_LEN, data,
1178                 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1179         hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
1180         /* change frame type to non QOS */
1181         hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1182
1183         return RX_CONTINUE;
1184 }
1185
1186 static int
1187 ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
1188 {
1189         if (unlikely(!rx->sta ||
1190             !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
1191                 return -EACCES;
1192
1193         return 0;
1194 }
1195
1196 static int
1197 ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1198 {
1199         /*
1200          * Pass through unencrypted frames if the hardware has
1201          * decrypted them already.
1202          */
1203         if (rx->status->flag & RX_FLAG_DECRYPTED)
1204                 return 0;
1205
1206         /* Drop unencrypted frames if key is set. */
1207         if (unlikely(!ieee80211_has_protected(fc) &&
1208                      !ieee80211_is_nullfunc(fc) &&
1209                      ieee80211_is_data(fc) &&
1210                      (rx->key || rx->sdata->drop_unencrypted)))
1211                 return -EACCES;
1212         if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1213                 if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1214                              rx->key))
1215                         return -EACCES;
1216                 /* BIP does not use Protected field, so need to check MMIE */
1217                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
1218                              && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
1219                              rx->key))
1220                         return -EACCES;
1221                 /*
1222                  * When using MFP, Action frames are not allowed prior to
1223                  * having configured keys.
1224                  */
1225                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1226                              ieee80211_is_robust_mgmt_frame(
1227                                      (struct ieee80211_hdr *) rx->skb->data)))
1228                         return -EACCES;
1229         }
1230
1231         return 0;
1232 }
1233
1234 static int
1235 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
1236 {
1237         struct net_device *dev = rx->dev;
1238         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1239
1240         return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
1241 }
1242
1243 /*
1244  * requires that rx->skb is a frame with ethernet header
1245  */
1246 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
1247 {
1248         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
1249                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1250         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1251
1252         /*
1253          * Allow EAPOL frames to us/the PAE group address regardless
1254          * of whether the frame was encrypted or not.
1255          */
1256         if (ehdr->h_proto == htons(ETH_P_PAE) &&
1257             (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1258              compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1259                 return true;
1260
1261         if (ieee80211_802_1x_port_control(rx) ||
1262             ieee80211_drop_unencrypted(rx, fc))
1263                 return false;
1264
1265         return true;
1266 }
1267
1268 /*
1269  * requires that rx->skb is a frame with ethernet header
1270  */
1271 static void
1272 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
1273 {
1274         struct net_device *dev = rx->dev;
1275         struct ieee80211_local *local = rx->local;
1276         struct sk_buff *skb, *xmit_skb;
1277         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1278         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1279         struct sta_info *dsta;
1280
1281         skb = rx->skb;
1282         xmit_skb = NULL;
1283
1284         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1285              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1286             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
1287             (rx->flags & IEEE80211_RX_RA_MATCH)) {
1288                 if (is_multicast_ether_addr(ehdr->h_dest)) {
1289                         /*
1290                          * send multicast frames both to higher layers in
1291                          * local net stack and back to the wireless medium
1292                          */
1293                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
1294                         if (!xmit_skb && net_ratelimit())
1295                                 printk(KERN_DEBUG "%s: failed to clone "
1296                                        "multicast frame\n", dev->name);
1297                 } else {
1298                         dsta = sta_info_get(local, skb->data);
1299                         if (dsta && dsta->sdata->dev == dev) {
1300                                 /*
1301                                  * The destination station is associated to
1302                                  * this AP (in this VLAN), so send the frame
1303                                  * directly to it and do not pass it to local
1304                                  * net stack.
1305                                  */
1306                                 xmit_skb = skb;
1307                                 skb = NULL;
1308                         }
1309                 }
1310         }
1311
1312         if (skb) {
1313                 int align __maybe_unused;
1314
1315 #if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1316                 /*
1317                  * 'align' will only take the values 0 or 2 here
1318                  * since all frames are required to be aligned
1319                  * to 2-byte boundaries when being passed to
1320                  * mac80211. That also explains the __skb_push()
1321                  * below.
1322                  */
1323                 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
1324                 if (align) {
1325                         if (WARN_ON(skb_headroom(skb) < 3)) {
1326                                 dev_kfree_skb(skb);
1327                                 skb = NULL;
1328                         } else {
1329                                 u8 *data = skb->data;
1330                                 size_t len = skb->len;
1331                                 u8 *new = __skb_push(skb, align);
1332                                 memmove(new, data, len);
1333                                 __skb_trim(skb, len);
1334                         }
1335                 }
1336 #endif
1337
1338                 if (skb) {
1339                         /* deliver to local stack */
1340                         skb->protocol = eth_type_trans(skb, dev);
1341                         memset(skb->cb, 0, sizeof(skb->cb));
1342                         netif_rx(skb);
1343                 }
1344         }
1345
1346         if (xmit_skb) {
1347                 /* send to wireless media */
1348                 xmit_skb->protocol = htons(ETH_P_802_3);
1349                 skb_reset_network_header(xmit_skb);
1350                 skb_reset_mac_header(xmit_skb);
1351                 dev_queue_xmit(xmit_skb);
1352         }
1353 }
1354
1355 static ieee80211_rx_result debug_noinline
1356 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
1357 {
1358         struct net_device *dev = rx->dev;
1359         struct ieee80211_local *local = rx->local;
1360         u16 ethertype;
1361         u8 *payload;
1362         struct sk_buff *skb = rx->skb, *frame = NULL;
1363         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1364         __le16 fc = hdr->frame_control;
1365         const struct ethhdr *eth;
1366         int remaining, err;
1367         u8 dst[ETH_ALEN];
1368         u8 src[ETH_ALEN];
1369
1370         if (unlikely(!ieee80211_is_data(fc)))
1371                 return RX_CONTINUE;
1372
1373         if (unlikely(!ieee80211_is_data_present(fc)))
1374                 return RX_DROP_MONITOR;
1375
1376         if (!(rx->flags & IEEE80211_RX_AMSDU))
1377                 return RX_CONTINUE;
1378
1379         err = __ieee80211_data_to_8023(rx);
1380         if (unlikely(err))
1381                 return RX_DROP_UNUSABLE;
1382
1383         skb->dev = dev;
1384
1385         dev->stats.rx_packets++;
1386         dev->stats.rx_bytes += skb->len;
1387
1388         /* skip the wrapping header */
1389         eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1390         if (!eth)
1391                 return RX_DROP_UNUSABLE;
1392
1393         while (skb != frame) {
1394                 u8 padding;
1395                 __be16 len = eth->h_proto;
1396                 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1397
1398                 remaining = skb->len;
1399                 memcpy(dst, eth->h_dest, ETH_ALEN);
1400                 memcpy(src, eth->h_source, ETH_ALEN);
1401
1402                 padding = ((4 - subframe_len) & 0x3);
1403                 /* the last MSDU has no padding */
1404                 if (subframe_len > remaining)
1405                         return RX_DROP_UNUSABLE;
1406
1407                 skb_pull(skb, sizeof(struct ethhdr));
1408                 /* if last subframe reuse skb */
1409                 if (remaining <= subframe_len + padding)
1410                         frame = skb;
1411                 else {
1412                         /*
1413                          * Allocate and reserve two bytes more for payload
1414                          * alignment since sizeof(struct ethhdr) is 14.
1415                          */
1416                         frame = dev_alloc_skb(
1417                                 ALIGN(local->hw.extra_tx_headroom, 4) +
1418                                 subframe_len + 2);
1419
1420                         if (frame == NULL)
1421                                 return RX_DROP_UNUSABLE;
1422
1423                         skb_reserve(frame,
1424                                     ALIGN(local->hw.extra_tx_headroom, 4) +
1425                                     sizeof(struct ethhdr) + 2);
1426                         memcpy(skb_put(frame, ntohs(len)), skb->data,
1427                                 ntohs(len));
1428
1429                         eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1430                                                         padding);
1431                         if (!eth) {
1432                                 dev_kfree_skb(frame);
1433                                 return RX_DROP_UNUSABLE;
1434                         }
1435                 }
1436
1437                 skb_reset_network_header(frame);
1438                 frame->dev = dev;
1439                 frame->priority = skb->priority;
1440                 rx->skb = frame;
1441
1442                 payload = frame->data;
1443                 ethertype = (payload[6] << 8) | payload[7];
1444
1445                 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1446                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1447                            compare_ether_addr(payload,
1448                                               bridge_tunnel_header) == 0)) {
1449                         /* remove RFC1042 or Bridge-Tunnel
1450                          * encapsulation and replace EtherType */
1451                         skb_pull(frame, 6);
1452                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1453                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1454                 } else {
1455                         memcpy(skb_push(frame, sizeof(__be16)),
1456                                &len, sizeof(__be16));
1457                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1458                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1459                 }
1460
1461                 if (!ieee80211_frame_allowed(rx, fc)) {
1462                         if (skb == frame) /* last frame */
1463                                 return RX_DROP_UNUSABLE;
1464                         dev_kfree_skb(frame);
1465                         continue;
1466                 }
1467
1468                 ieee80211_deliver_skb(rx);
1469         }
1470
1471         return RX_QUEUED;
1472 }
1473
1474 #ifdef CONFIG_MAC80211_MESH
1475 static ieee80211_rx_result
1476 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1477 {
1478         struct ieee80211_hdr *hdr;
1479         struct ieee80211s_hdr *mesh_hdr;
1480         unsigned int hdrlen;
1481         struct sk_buff *skb = rx->skb, *fwd_skb;
1482         struct ieee80211_local *local = rx->local;
1483         struct ieee80211_sub_if_data *sdata;
1484
1485         hdr = (struct ieee80211_hdr *) skb->data;
1486         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1487         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1488         sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1489
1490         if (!ieee80211_is_data(hdr->frame_control))
1491                 return RX_CONTINUE;
1492
1493         if (!mesh_hdr->ttl)
1494                 /* illegal frame */
1495                 return RX_DROP_MONITOR;
1496
1497         if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6){
1498                 struct mesh_path *mppath;
1499
1500                 rcu_read_lock();
1501                 mppath = mpp_path_lookup(mesh_hdr->eaddr2, sdata);
1502                 if (!mppath) {
1503                         mpp_path_add(mesh_hdr->eaddr2, hdr->addr4, sdata);
1504                 } else {
1505                         spin_lock_bh(&mppath->state_lock);
1506                         mppath->exp_time = jiffies;
1507                         if (compare_ether_addr(mppath->mpp, hdr->addr4) != 0)
1508                                 memcpy(mppath->mpp, hdr->addr4, ETH_ALEN);
1509                         spin_unlock_bh(&mppath->state_lock);
1510                 }
1511                 rcu_read_unlock();
1512         }
1513
1514         if (compare_ether_addr(rx->dev->dev_addr, hdr->addr3) == 0)
1515                 return RX_CONTINUE;
1516
1517         mesh_hdr->ttl--;
1518
1519         if (rx->flags & IEEE80211_RX_RA_MATCH) {
1520                 if (!mesh_hdr->ttl)
1521                         IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
1522                                                      dropped_frames_ttl);
1523                 else {
1524                         struct ieee80211_hdr *fwd_hdr;
1525                         struct ieee80211_tx_info *info;
1526
1527                         fwd_skb = skb_copy(skb, GFP_ATOMIC);
1528
1529                         if (!fwd_skb && net_ratelimit())
1530                                 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1531                                                    rx->dev->name);
1532
1533                         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
1534                         /*
1535                          * Save TA to addr1 to send TA a path error if a
1536                          * suitable next hop is not found
1537                          */
1538                         memcpy(fwd_hdr->addr1, fwd_hdr->addr2, ETH_ALEN);
1539                         memcpy(fwd_hdr->addr2, rx->dev->dev_addr, ETH_ALEN);
1540                         info = IEEE80211_SKB_CB(fwd_skb);
1541                         memset(info, 0, sizeof(*info));
1542                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1543                         info->control.vif = &rx->sdata->vif;
1544                         ieee80211_select_queue(local, fwd_skb);
1545                         if (is_multicast_ether_addr(fwd_hdr->addr3))
1546                                 memcpy(fwd_hdr->addr1, fwd_hdr->addr3,
1547                                                 ETH_ALEN);
1548                         else {
1549                                 int err = mesh_nexthop_lookup(fwd_skb, sdata);
1550                                 /* Failed to immediately resolve next hop:
1551                                  * fwded frame was dropped or will be added
1552                                  * later to the pending skb queue.  */
1553                                 if (err)
1554                                         return RX_DROP_MONITOR;
1555                         }
1556                         IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1557                                                      fwded_frames);
1558                         ieee80211_add_pending_skb(local, fwd_skb);
1559                 }
1560         }
1561
1562         if (is_multicast_ether_addr(hdr->addr3) ||
1563             rx->dev->flags & IFF_PROMISC)
1564                 return RX_CONTINUE;
1565         else
1566                 return RX_DROP_MONITOR;
1567 }
1568 #endif
1569
1570 static ieee80211_rx_result debug_noinline
1571 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
1572 {
1573         struct net_device *dev = rx->dev;
1574         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1575         __le16 fc = hdr->frame_control;
1576         int err;
1577
1578         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
1579                 return RX_CONTINUE;
1580
1581         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1582                 return RX_DROP_MONITOR;
1583
1584         err = __ieee80211_data_to_8023(rx);
1585         if (unlikely(err))
1586                 return RX_DROP_UNUSABLE;
1587
1588         if (!ieee80211_frame_allowed(rx, fc))
1589                 return RX_DROP_MONITOR;
1590
1591         rx->skb->dev = dev;
1592
1593         dev->stats.rx_packets++;
1594         dev->stats.rx_bytes += rx->skb->len;
1595
1596         ieee80211_deliver_skb(rx);
1597
1598         return RX_QUEUED;
1599 }
1600
1601 static ieee80211_rx_result debug_noinline
1602 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
1603 {
1604         struct ieee80211_local *local = rx->local;
1605         struct ieee80211_hw *hw = &local->hw;
1606         struct sk_buff *skb = rx->skb;
1607         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
1608         struct tid_ampdu_rx *tid_agg_rx;
1609         u16 start_seq_num;
1610         u16 tid;
1611
1612         if (likely(!ieee80211_is_ctl(bar->frame_control)))
1613                 return RX_CONTINUE;
1614
1615         if (ieee80211_is_back_req(bar->frame_control)) {
1616                 if (!rx->sta)
1617                         return RX_CONTINUE;
1618                 tid = le16_to_cpu(bar->control) >> 12;
1619                 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1620                                         != HT_AGG_STATE_OPERATIONAL)
1621                         return RX_CONTINUE;
1622                 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
1623
1624                 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1625
1626                 /* reset session timer */
1627                 if (tid_agg_rx->timeout)
1628                         mod_timer(&tid_agg_rx->session_timer,
1629                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
1630
1631                 /* manage reordering buffer according to requested */
1632                 /* sequence number */
1633                 rcu_read_lock();
1634                 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1635                                                  start_seq_num, 1);
1636                 rcu_read_unlock();
1637                 return RX_DROP_UNUSABLE;
1638         }
1639
1640         return RX_CONTINUE;
1641 }
1642
1643 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1644                                            struct ieee80211_mgmt *mgmt,
1645                                            size_t len)
1646 {
1647         struct ieee80211_local *local = sdata->local;
1648         struct sk_buff *skb;
1649         struct ieee80211_mgmt *resp;
1650
1651         if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
1652                 /* Not to own unicast address */
1653                 return;
1654         }
1655
1656         if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1657             compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
1658                 /* Not from the current AP or not associated yet. */
1659                 return;
1660         }
1661
1662         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1663                 /* Too short SA Query request frame */
1664                 return;
1665         }
1666
1667         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1668         if (skb == NULL)
1669                 return;
1670
1671         skb_reserve(skb, local->hw.extra_tx_headroom);
1672         resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1673         memset(resp, 0, 24);
1674         memcpy(resp->da, mgmt->sa, ETH_ALEN);
1675         memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
1676         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
1677         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1678                                           IEEE80211_STYPE_ACTION);
1679         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1680         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1681         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1682         memcpy(resp->u.action.u.sa_query.trans_id,
1683                mgmt->u.action.u.sa_query.trans_id,
1684                WLAN_SA_QUERY_TR_ID_LEN);
1685
1686         ieee80211_tx_skb(sdata, skb, 1);
1687 }
1688
1689 static ieee80211_rx_result debug_noinline
1690 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1691 {
1692         struct ieee80211_local *local = rx->local;
1693         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1694         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1695         int len = rx->skb->len;
1696
1697         if (!ieee80211_is_action(mgmt->frame_control))
1698                 return RX_CONTINUE;
1699
1700         if (!rx->sta)
1701                 return RX_DROP_MONITOR;
1702
1703         if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1704                 return RX_DROP_MONITOR;
1705
1706         if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1707                 return RX_DROP_MONITOR;
1708
1709         /* all categories we currently handle have action_code */
1710         if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1711                 return RX_DROP_MONITOR;
1712
1713         switch (mgmt->u.action.category) {
1714         case WLAN_CATEGORY_BACK:
1715                 /*
1716                  * The aggregation code is not prepared to handle
1717                  * anything but STA/AP due to the BSSID handling;
1718                  * IBSS could work in the code but isn't supported
1719                  * by drivers or the standard.
1720                  */
1721                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1722                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1723                     sdata->vif.type != NL80211_IFTYPE_AP)
1724                         return RX_DROP_MONITOR;
1725
1726                 switch (mgmt->u.action.u.addba_req.action_code) {
1727                 case WLAN_ACTION_ADDBA_REQ:
1728                         if (len < (IEEE80211_MIN_ACTION_SIZE +
1729                                    sizeof(mgmt->u.action.u.addba_req)))
1730                                 return RX_DROP_MONITOR;
1731                         ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1732                         break;
1733                 case WLAN_ACTION_ADDBA_RESP:
1734                         if (len < (IEEE80211_MIN_ACTION_SIZE +
1735                                    sizeof(mgmt->u.action.u.addba_resp)))
1736                                 return RX_DROP_MONITOR;
1737                         ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1738                         break;
1739                 case WLAN_ACTION_DELBA:
1740                         if (len < (IEEE80211_MIN_ACTION_SIZE +
1741                                    sizeof(mgmt->u.action.u.delba)))
1742                                 return RX_DROP_MONITOR;
1743                         ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1744                         break;
1745                 }
1746                 break;
1747         case WLAN_CATEGORY_SPECTRUM_MGMT:
1748                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1749                         return RX_DROP_MONITOR;
1750
1751                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1752                         return RX_DROP_MONITOR;
1753
1754                 switch (mgmt->u.action.u.measurement.action_code) {
1755                 case WLAN_ACTION_SPCT_MSR_REQ:
1756                         if (len < (IEEE80211_MIN_ACTION_SIZE +
1757                                    sizeof(mgmt->u.action.u.measurement)))
1758                                 return RX_DROP_MONITOR;
1759                         ieee80211_process_measurement_req(sdata, mgmt, len);
1760                         break;
1761                 case WLAN_ACTION_SPCT_CHL_SWITCH:
1762                         if (len < (IEEE80211_MIN_ACTION_SIZE +
1763                                    sizeof(mgmt->u.action.u.chan_switch)))
1764                                 return RX_DROP_MONITOR;
1765
1766                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1767                                 return RX_DROP_MONITOR;
1768
1769                         if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
1770                                 return RX_DROP_MONITOR;
1771
1772                         return ieee80211_sta_rx_mgmt(sdata, rx->skb);
1773                 }
1774                 break;
1775         case WLAN_CATEGORY_SA_QUERY:
1776                 if (len < (IEEE80211_MIN_ACTION_SIZE +
1777                            sizeof(mgmt->u.action.u.sa_query)))
1778                         return RX_DROP_MONITOR;
1779                 switch (mgmt->u.action.u.sa_query.action) {
1780                 case WLAN_ACTION_SA_QUERY_REQUEST:
1781                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1782                                 return RX_DROP_MONITOR;
1783                         ieee80211_process_sa_query_req(sdata, mgmt, len);
1784                         break;
1785                 case WLAN_ACTION_SA_QUERY_RESPONSE:
1786                         /*
1787                          * SA Query response is currently only used in AP mode
1788                          * and it is processed in user space.
1789                          */
1790                         return RX_CONTINUE;
1791                 }
1792                 break;
1793         default:
1794                 return RX_CONTINUE;
1795         }
1796
1797         rx->sta->rx_packets++;
1798         dev_kfree_skb(rx->skb);
1799         return RX_QUEUED;
1800 }
1801
1802 static ieee80211_rx_result debug_noinline
1803 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
1804 {
1805         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1806         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1807
1808         if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1809                 return RX_DROP_MONITOR;
1810
1811         if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1812                 return RX_DROP_MONITOR;
1813
1814         if (ieee80211_vif_is_mesh(&sdata->vif))
1815                 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
1816
1817         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
1818                 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
1819
1820         if (sdata->vif.type == NL80211_IFTYPE_STATION)
1821                 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
1822
1823         return RX_DROP_MONITOR;
1824 }
1825
1826 static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
1827                                             struct ieee80211_rx_data *rx)
1828 {
1829         int keyidx;
1830         unsigned int hdrlen;
1831
1832         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1833         if (rx->skb->len >= hdrlen + 4)
1834                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1835         else
1836                 keyidx = -1;
1837
1838         if (!rx->sta) {
1839                 /*
1840                  * Some hardware seem to generate incorrect Michael MIC
1841                  * reports; ignore them to avoid triggering countermeasures.
1842                  */
1843                 goto ignore;
1844         }
1845
1846         if (!ieee80211_has_protected(hdr->frame_control))
1847                 goto ignore;
1848
1849         if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
1850                 /*
1851                  * APs with pairwise keys should never receive Michael MIC
1852                  * errors for non-zero keyidx because these are reserved for
1853                  * group keys and only the AP is sending real multicast
1854                  * frames in the BSS.
1855                  */
1856                 goto ignore;
1857         }
1858
1859         if (!ieee80211_is_data(hdr->frame_control) &&
1860             !ieee80211_is_auth(hdr->frame_control))
1861                 goto ignore;
1862
1863         mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
1864                                         GFP_ATOMIC);
1865  ignore:
1866         dev_kfree_skb(rx->skb);
1867         rx->skb = NULL;
1868 }
1869
1870 /* TODO: use IEEE80211_RX_FRAGMENTED */
1871 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx)
1872 {
1873         struct ieee80211_sub_if_data *sdata;
1874         struct ieee80211_local *local = rx->local;
1875         struct ieee80211_rtap_hdr {
1876                 struct ieee80211_radiotap_header hdr;
1877                 u8 flags;
1878                 u8 rate;
1879                 __le16 chan_freq;
1880                 __le16 chan_flags;
1881         } __attribute__ ((packed)) *rthdr;
1882         struct sk_buff *skb = rx->skb, *skb2;
1883         struct net_device *prev_dev = NULL;
1884         struct ieee80211_rx_status *status = rx->status;
1885
1886         if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
1887                 goto out_free_skb;
1888
1889         if (skb_headroom(skb) < sizeof(*rthdr) &&
1890             pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1891                 goto out_free_skb;
1892
1893         rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1894         memset(rthdr, 0, sizeof(*rthdr));
1895         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1896         rthdr->hdr.it_present =
1897                 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1898                             (1 << IEEE80211_RADIOTAP_RATE) |
1899                             (1 << IEEE80211_RADIOTAP_CHANNEL));
1900
1901         rthdr->rate = rx->rate->bitrate / 5;
1902         rthdr->chan_freq = cpu_to_le16(status->freq);
1903
1904         if (status->band == IEEE80211_BAND_5GHZ)
1905                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1906                                                 IEEE80211_CHAN_5GHZ);
1907         else
1908                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1909                                                 IEEE80211_CHAN_2GHZ);
1910
1911         skb_set_mac_header(skb, 0);
1912         skb->ip_summed = CHECKSUM_UNNECESSARY;
1913         skb->pkt_type = PACKET_OTHERHOST;
1914         skb->protocol = htons(ETH_P_802_2);
1915
1916         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1917                 if (!netif_running(sdata->dev))
1918                         continue;
1919
1920                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
1921                     !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1922                         continue;
1923
1924                 if (prev_dev) {
1925                         skb2 = skb_clone(skb, GFP_ATOMIC);
1926                         if (skb2) {
1927                                 skb2->dev = prev_dev;
1928                                 netif_rx(skb2);
1929                         }
1930                 }
1931
1932                 prev_dev = sdata->dev;
1933                 sdata->dev->stats.rx_packets++;
1934                 sdata->dev->stats.rx_bytes += skb->len;
1935         }
1936
1937         if (prev_dev) {
1938                 skb->dev = prev_dev;
1939                 netif_rx(skb);
1940                 skb = NULL;
1941         } else
1942                 goto out_free_skb;
1943
1944         rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
1945         return;
1946
1947  out_free_skb:
1948         dev_kfree_skb(skb);
1949 }
1950
1951
1952 static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
1953                                          struct ieee80211_rx_data *rx,
1954                                          struct sk_buff *skb)
1955 {
1956         ieee80211_rx_result res = RX_DROP_MONITOR;
1957
1958         rx->skb = skb;
1959         rx->sdata = sdata;
1960         rx->dev = sdata->dev;
1961
1962 #define CALL_RXH(rxh)                   \
1963         do {                            \
1964                 res = rxh(rx);          \
1965                 if (res != RX_CONTINUE) \
1966                         goto rxh_done;  \
1967         } while (0);
1968
1969         CALL_RXH(ieee80211_rx_h_passive_scan)
1970         CALL_RXH(ieee80211_rx_h_check)
1971         CALL_RXH(ieee80211_rx_h_decrypt)
1972         CALL_RXH(ieee80211_rx_h_check_more_data)
1973         CALL_RXH(ieee80211_rx_h_sta_process)
1974         CALL_RXH(ieee80211_rx_h_defragment)
1975         CALL_RXH(ieee80211_rx_h_ps_poll)
1976         CALL_RXH(ieee80211_rx_h_michael_mic_verify)
1977         /* must be after MMIC verify so header is counted in MPDU mic */
1978         CALL_RXH(ieee80211_rx_h_remove_qos_control)
1979         CALL_RXH(ieee80211_rx_h_amsdu)
1980 #ifdef CONFIG_MAC80211_MESH
1981         if (ieee80211_vif_is_mesh(&sdata->vif))
1982                 CALL_RXH(ieee80211_rx_h_mesh_fwding);
1983 #endif
1984         CALL_RXH(ieee80211_rx_h_data)
1985         CALL_RXH(ieee80211_rx_h_ctrl)
1986         CALL_RXH(ieee80211_rx_h_action)
1987         CALL_RXH(ieee80211_rx_h_mgmt)
1988
1989 #undef CALL_RXH
1990
1991  rxh_done:
1992         switch (res) {
1993         case RX_DROP_MONITOR:
1994                 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1995                 if (rx->sta)
1996                         rx->sta->rx_dropped++;
1997                 /* fall through */
1998         case RX_CONTINUE:
1999                 ieee80211_rx_cooked_monitor(rx);
2000                 break;
2001         case RX_DROP_UNUSABLE:
2002                 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2003                 if (rx->sta)
2004                         rx->sta->rx_dropped++;
2005                 dev_kfree_skb(rx->skb);
2006                 break;
2007         case RX_QUEUED:
2008                 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
2009                 break;
2010         }
2011 }
2012
2013 /* main receive path */
2014
2015 static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
2016                                 struct ieee80211_rx_data *rx,
2017                                 struct ieee80211_hdr *hdr)
2018 {
2019         u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len, sdata->vif.type);
2020         int multicast = is_multicast_ether_addr(hdr->addr1);
2021
2022         switch (sdata->vif.type) {
2023         case NL80211_IFTYPE_STATION:
2024                 if (!bssid)
2025                         return 0;
2026                 if (!multicast &&
2027                     compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
2028                         if (!(sdata->dev->flags & IFF_PROMISC))
2029                                 return 0;
2030                         rx->flags &= ~IEEE80211_RX_RA_MATCH;
2031                 }
2032                 break;
2033         case NL80211_IFTYPE_ADHOC:
2034                 if (!bssid)
2035                         return 0;
2036                 if (ieee80211_is_beacon(hdr->frame_control)) {
2037                         return 1;
2038                 }
2039                 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2040                         if (!(rx->flags & IEEE80211_RX_IN_SCAN))
2041                                 return 0;
2042                         rx->flags &= ~IEEE80211_RX_RA_MATCH;
2043                 } else if (!multicast &&
2044                            compare_ether_addr(sdata->dev->dev_addr,
2045                                               hdr->addr1) != 0) {
2046                         if (!(sdata->dev->flags & IFF_PROMISC))
2047                                 return 0;
2048                         rx->flags &= ~IEEE80211_RX_RA_MATCH;
2049                 } else if (!rx->sta) {
2050                         int rate_idx;
2051                         if (rx->status->flag & RX_FLAG_HT)
2052                                 rate_idx = 0; /* TODO: HT rates */
2053                         else
2054                                 rate_idx = rx->status->rate_idx;
2055                         rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
2056                                 BIT(rate_idx));
2057                 }
2058                 break;
2059         case NL80211_IFTYPE_MESH_POINT:
2060                 if (!multicast &&
2061                     compare_ether_addr(sdata->dev->dev_addr,
2062                                        hdr->addr1) != 0) {
2063                         if (!(sdata->dev->flags & IFF_PROMISC))
2064                                 return 0;
2065
2066                         rx->flags &= ~IEEE80211_RX_RA_MATCH;
2067                 }
2068                 break;
2069         case NL80211_IFTYPE_AP_VLAN:
2070         case NL80211_IFTYPE_AP:
2071                 if (!bssid) {
2072                         if (compare_ether_addr(sdata->dev->dev_addr,
2073                                                hdr->addr1))
2074                                 return 0;
2075                 } else if (!ieee80211_bssid_match(bssid,
2076                                         sdata->dev->dev_addr)) {
2077                         if (!(rx->flags & IEEE80211_RX_IN_SCAN))
2078                                 return 0;
2079                         rx->flags &= ~IEEE80211_RX_RA_MATCH;
2080                 }
2081                 break;
2082         case NL80211_IFTYPE_WDS:
2083                 if (bssid || !ieee80211_is_data(hdr->frame_control))
2084                         return 0;
2085                 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2086                         return 0;
2087                 break;
2088         case NL80211_IFTYPE_MONITOR:
2089                 /* take everything */
2090                 break;
2091         case NL80211_IFTYPE_UNSPECIFIED:
2092         case __NL80211_IFTYPE_AFTER_LAST:
2093                 /* should never get here */
2094                 WARN_ON(1);
2095                 break;
2096         }
2097
2098         return 1;
2099 }
2100
2101 /*
2102  * This is the actual Rx frames handler. as it blongs to Rx path it must
2103  * be called with rcu_read_lock protection.
2104  */
2105 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2106                                          struct sk_buff *skb,
2107                                          struct ieee80211_rate *rate)
2108 {
2109         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2110         struct ieee80211_local *local = hw_to_local(hw);
2111         struct ieee80211_sub_if_data *sdata;
2112         struct ieee80211_hdr *hdr;
2113         struct ieee80211_rx_data rx;
2114         int prepares;
2115         struct ieee80211_sub_if_data *prev = NULL;
2116         struct sk_buff *skb_new;
2117
2118         hdr = (struct ieee80211_hdr *)skb->data;
2119         memset(&rx, 0, sizeof(rx));
2120         rx.skb = skb;
2121         rx.local = local;
2122
2123         rx.status = status;
2124         rx.rate = rate;
2125
2126         if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
2127                 local->dot11ReceivedFragmentCount++;
2128
2129         rx.sta = sta_info_get(local, hdr->addr2);
2130         if (rx.sta) {
2131                 rx.sdata = rx.sta->sdata;
2132                 rx.dev = rx.sta->sdata->dev;
2133         }
2134
2135         if ((status->flag & RX_FLAG_MMIC_ERROR)) {
2136                 ieee80211_rx_michael_mic_report(hdr, &rx);
2137                 return;
2138         }
2139
2140         if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2141                      test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
2142                 rx.flags |= IEEE80211_RX_IN_SCAN;
2143
2144         ieee80211_parse_qos(&rx);
2145         ieee80211_verify_alignment(&rx);
2146
2147         skb = rx.skb;
2148
2149         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2150                 if (!netif_running(sdata->dev))
2151                         continue;
2152
2153                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
2154                         continue;
2155
2156                 rx.flags |= IEEE80211_RX_RA_MATCH;
2157                 prepares = prepare_for_handlers(sdata, &rx, hdr);
2158
2159                 if (!prepares)
2160                         continue;
2161
2162                 /*
2163                  * frame is destined for this interface, but if it's not
2164                  * also for the previous one we handle that after the
2165                  * loop to avoid copying the SKB once too much
2166                  */
2167
2168                 if (!prev) {
2169                         prev = sdata;
2170                         continue;
2171                 }
2172
2173                 /*
2174                  * frame was destined for the previous interface
2175                  * so invoke RX handlers for it
2176                  */
2177
2178                 skb_new = skb_copy(skb, GFP_ATOMIC);
2179                 if (!skb_new) {
2180                         if (net_ratelimit())
2181                                 printk(KERN_DEBUG "%s: failed to copy "
2182                                        "multicast frame for %s\n",
2183                                        wiphy_name(local->hw.wiphy),
2184                                        prev->dev->name);
2185                         continue;
2186                 }
2187                 ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
2188                 prev = sdata;
2189         }
2190         if (prev)
2191                 ieee80211_invoke_rx_handlers(prev, &rx, skb);
2192         else
2193                 dev_kfree_skb(skb);
2194 }
2195
2196 #define SEQ_MODULO 0x1000
2197 #define SEQ_MASK   0xfff
2198
2199 static inline int seq_less(u16 sq1, u16 sq2)
2200 {
2201         return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
2202 }
2203
2204 static inline u16 seq_inc(u16 sq)
2205 {
2206         return (sq + 1) & SEQ_MASK;
2207 }
2208
2209 static inline u16 seq_sub(u16 sq1, u16 sq2)
2210 {
2211         return (sq1 - sq2) & SEQ_MASK;
2212 }
2213
2214
2215 static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
2216                                             struct tid_ampdu_rx *tid_agg_rx,
2217                                             int index)
2218 {
2219         struct ieee80211_supported_band *sband;
2220         struct ieee80211_rate *rate;
2221         struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
2222         struct ieee80211_rx_status *status;
2223
2224         if (!skb)
2225                 goto no_frame;
2226
2227         status = IEEE80211_SKB_RXCB(skb);
2228
2229         /* release the reordered frames to stack */
2230         sband = hw->wiphy->bands[status->band];
2231         if (status->flag & RX_FLAG_HT)
2232                 rate = sband->bitrates; /* TODO: HT rates */
2233         else
2234                 rate = &sband->bitrates[status->rate_idx];
2235         __ieee80211_rx_handle_packet(hw, skb, rate);
2236         tid_agg_rx->stored_mpdu_num--;
2237         tid_agg_rx->reorder_buf[index] = NULL;
2238
2239 no_frame:
2240         tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2241 }
2242
2243
2244 /*
2245  * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
2246  * the skb was added to the buffer longer than this time ago, the earlier
2247  * frames that have not yet been received are assumed to be lost and the skb
2248  * can be released for processing. This may also release other skb's from the
2249  * reorder buffer if there are no additional gaps between the frames.
2250  */
2251 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
2252
2253 /*
2254  * As it function blongs to Rx path it must be called with
2255  * the proper rcu_read_lock protection for its flow.
2256  */
2257 static u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
2258                                            struct tid_ampdu_rx *tid_agg_rx,
2259                                            struct sk_buff *skb,
2260                                            u16 mpdu_seq_num,
2261                                            int bar_req)
2262 {
2263         u16 head_seq_num, buf_size;
2264         int index;
2265
2266         buf_size = tid_agg_rx->buf_size;
2267         head_seq_num = tid_agg_rx->head_seq_num;
2268
2269         /* frame with out of date sequence number */
2270         if (seq_less(mpdu_seq_num, head_seq_num)) {
2271                 dev_kfree_skb(skb);
2272                 return 1;
2273         }
2274
2275         /* if frame sequence number exceeds our buffering window size or
2276          * block Ack Request arrived - release stored frames */
2277         if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
2278                 /* new head to the ordering buffer */
2279                 if (bar_req)
2280                         head_seq_num = mpdu_seq_num;
2281                 else
2282                         head_seq_num =
2283                                 seq_inc(seq_sub(mpdu_seq_num, buf_size));
2284                 /* release stored frames up to new head to stack */
2285                 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2286                         index = seq_sub(tid_agg_rx->head_seq_num,
2287                                 tid_agg_rx->ssn)
2288                                 % tid_agg_rx->buf_size;
2289                         ieee80211_release_reorder_frame(hw, tid_agg_rx,
2290                                                         index);
2291                 }
2292                 if (bar_req)
2293                         return 1;
2294         }
2295
2296         /* now the new frame is always in the range of the reordering */
2297         /* buffer window */
2298         index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
2299                                 % tid_agg_rx->buf_size;
2300         /* check if we already stored this frame */
2301         if (tid_agg_rx->reorder_buf[index]) {
2302                 dev_kfree_skb(skb);
2303                 return 1;
2304         }
2305
2306         /* if arrived mpdu is in the right order and nothing else stored */
2307         /* release it immediately */
2308         if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
2309                         tid_agg_rx->stored_mpdu_num == 0) {
2310                 tid_agg_rx->head_seq_num =
2311                         seq_inc(tid_agg_rx->head_seq_num);
2312                 return 0;
2313         }
2314
2315         /* put the frame in the reordering buffer */
2316         tid_agg_rx->reorder_buf[index] = skb;
2317         tid_agg_rx->reorder_time[index] = jiffies;
2318         tid_agg_rx->stored_mpdu_num++;
2319         /* release the buffer until next missing frame */
2320         index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
2321                                                 % tid_agg_rx->buf_size;
2322         if (!tid_agg_rx->reorder_buf[index] &&
2323             tid_agg_rx->stored_mpdu_num > 1) {
2324                 /*
2325                  * No buffers ready to be released, but check whether any
2326                  * frames in the reorder buffer have timed out.
2327                  */
2328                 int j;
2329                 int skipped = 1;
2330                 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
2331                      j = (j + 1) % tid_agg_rx->buf_size) {
2332                         if (tid_agg_rx->reorder_buf[j] == NULL) {
2333                                 skipped++;
2334                                 continue;
2335                         }
2336                         if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
2337                                         HZ / 10))
2338                                 break;
2339
2340 #ifdef CONFIG_MAC80211_HT_DEBUG
2341                         if (net_ratelimit())
2342                                 printk(KERN_DEBUG "%s: release an RX reorder "
2343                                        "frame due to timeout on earlier "
2344                                        "frames\n",
2345                                        wiphy_name(hw->wiphy));
2346 #endif
2347                         ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
2348
2349                         /*
2350                          * Increment the head seq# also for the skipped slots.
2351                          */
2352                         tid_agg_rx->head_seq_num =
2353                                 (tid_agg_rx->head_seq_num + skipped) &
2354                                 SEQ_MASK;
2355                         skipped = 0;
2356                 }
2357         } else while (tid_agg_rx->reorder_buf[index]) {
2358                 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
2359                 index = seq_sub(tid_agg_rx->head_seq_num,
2360                         tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2361         }
2362         return 1;
2363 }
2364
2365 static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2366                                      struct sk_buff *skb)
2367 {
2368         struct ieee80211_hw *hw = &local->hw;
2369         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2370         struct sta_info *sta;
2371         struct tid_ampdu_rx *tid_agg_rx;
2372         u16 sc;
2373         u16 mpdu_seq_num;
2374         u8 ret = 0;
2375         int tid;
2376
2377         sta = sta_info_get(local, hdr->addr2);
2378         if (!sta)
2379                 return ret;
2380
2381         /* filter the QoS data rx stream according to
2382          * STA/TID and check if this STA/TID is on aggregation */
2383         if (!ieee80211_is_data_qos(hdr->frame_control))
2384                 goto end_reorder;
2385
2386         tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
2387
2388         if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
2389                 goto end_reorder;
2390
2391         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
2392
2393         /* qos null data frames are excluded */
2394         if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
2395                 goto end_reorder;
2396
2397         /* new un-ordered ampdu frame - process it */
2398
2399         /* reset session timer */
2400         if (tid_agg_rx->timeout)
2401                 mod_timer(&tid_agg_rx->session_timer,
2402                           TU_TO_EXP_TIME(tid_agg_rx->timeout));
2403
2404         /* if this mpdu is fragmented - terminate rx aggregation session */
2405         sc = le16_to_cpu(hdr->seq_ctrl);
2406         if (sc & IEEE80211_SCTL_FRAG) {
2407                 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
2408                         tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
2409                 ret = 1;
2410                 goto end_reorder;
2411         }
2412
2413         /* according to mpdu sequence number deal with reordering buffer */
2414         mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
2415         ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
2416                                                 mpdu_seq_num, 0);
2417  end_reorder:
2418         return ret;
2419 }
2420
2421 /*
2422  * This is the receive path handler. It is called by a low level driver when an
2423  * 802.11 MPDU is received from the hardware.
2424  */
2425 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
2426 {
2427         struct ieee80211_local *local = hw_to_local(hw);
2428         struct ieee80211_rate *rate = NULL;
2429         struct ieee80211_supported_band *sband;
2430         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2431
2432         if (status->band < 0 ||
2433             status->band >= IEEE80211_NUM_BANDS) {
2434                 WARN_ON(1);
2435                 return;
2436         }
2437
2438         sband = local->hw.wiphy->bands[status->band];
2439         if (!sband) {
2440                 WARN_ON(1);
2441                 return;
2442         }
2443
2444         if (status->flag & RX_FLAG_HT) {
2445                 /* rate_idx is MCS index */
2446                 if (WARN_ON(status->rate_idx < 0 ||
2447                             status->rate_idx >= 76))
2448                         return;
2449                 /* HT rates are not in the table - use the highest legacy rate
2450                  * for now since other parts of mac80211 may not yet be fully
2451                  * MCS aware. */
2452                 rate = &sband->bitrates[sband->n_bitrates - 1];
2453         } else {
2454                 if (WARN_ON(status->rate_idx < 0 ||
2455                             status->rate_idx >= sband->n_bitrates))
2456                         return;
2457                 rate = &sband->bitrates[status->rate_idx];
2458         }
2459
2460         /*
2461          * key references and virtual interfaces are protected using RCU
2462          * and this requires that we are in a read-side RCU section during
2463          * receive processing
2464          */
2465         rcu_read_lock();
2466
2467         /*
2468          * Frames with failed FCS/PLCP checksum are not returned,
2469          * all other frames are returned without radiotap header
2470          * if it was previously present.
2471          * Also, frames with less than 16 bytes are dropped.
2472          */
2473         skb = ieee80211_rx_monitor(local, skb, rate);
2474         if (!skb) {
2475                 rcu_read_unlock();
2476                 return;
2477         }
2478
2479         /*
2480          * In theory, the block ack reordering should happen after duplicate
2481          * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
2482          * the call to ieee80211_rx_reorder_ampdu() should really be moved to
2483          * happen as a new RX handler between ieee80211_rx_h_check and
2484          * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
2485          * the time being, the call can be here since RX reorder buf processing
2486          * will implicitly skip duplicates. We could, in theory at least,
2487          * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
2488          * frames from other than operational channel), but that should not
2489          * happen in normal networks.
2490          */
2491         if (!ieee80211_rx_reorder_ampdu(local, skb))
2492                 __ieee80211_rx_handle_packet(hw, skb, rate);
2493
2494         rcu_read_unlock();
2495 }
2496 EXPORT_SYMBOL(__ieee80211_rx);
2497
2498 /* This is a version of the rx handler that can be called from hard irq
2499  * context. Post the skb on the queue and schedule the tasklet */
2500 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
2501 {
2502         struct ieee80211_local *local = hw_to_local(hw);
2503
2504         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2505
2506         skb->pkt_type = IEEE80211_RX_MSG;
2507         skb_queue_tail(&local->skb_queue, skb);
2508         tasklet_schedule(&local->tasklet);
2509 }
2510 EXPORT_SYMBOL(ieee80211_rx_irqsafe);