mac80211: Optimize scans on current operating channel.
[profile/ivi/kernel-adaptation-intel-automotive.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-2010  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/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rcupdate.h>
19 #include <net/mac80211.h>
20 #include <net/ieee80211_radiotap.h>
21
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "led.h"
25 #include "mesh.h"
26 #include "wep.h"
27 #include "wpa.h"
28 #include "tkip.h"
29 #include "wme.h"
30
31 /*
32  * monitor mode reception
33  *
34  * This function cleans up the SKB, i.e. it removes all the stuff
35  * only useful for monitoring.
36  */
37 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
38                                            struct sk_buff *skb)
39 {
40         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
41                 if (likely(skb->len > FCS_LEN))
42                         __pskb_trim(skb, skb->len - FCS_LEN);
43                 else {
44                         /* driver bug */
45                         WARN_ON(1);
46                         dev_kfree_skb(skb);
47                         skb = NULL;
48                 }
49         }
50
51         return skb;
52 }
53
54 static inline int should_drop_frame(struct sk_buff *skb,
55                                     int present_fcs_len)
56 {
57         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
58         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
59
60         if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
61                 return 1;
62         if (unlikely(skb->len < 16 + present_fcs_len))
63                 return 1;
64         if (ieee80211_is_ctl(hdr->frame_control) &&
65             !ieee80211_is_pspoll(hdr->frame_control) &&
66             !ieee80211_is_back_req(hdr->frame_control))
67                 return 1;
68         return 0;
69 }
70
71 static int
72 ieee80211_rx_radiotap_len(struct ieee80211_local *local,
73                           struct ieee80211_rx_status *status)
74 {
75         int len;
76
77         /* always present fields */
78         len = sizeof(struct ieee80211_radiotap_header) + 9;
79
80         if (status->flag & RX_FLAG_TSFT)
81                 len += 8;
82         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
83                 len += 1;
84
85         if (len & 1) /* padding for RX_FLAGS if necessary */
86                 len++;
87
88         if (status->flag & RX_FLAG_HT) /* HT info */
89                 len += 3;
90
91         return len;
92 }
93
94 /*
95  * ieee80211_add_rx_radiotap_header - add radiotap header
96  *
97  * add a radiotap header containing all the fields which the hardware provided.
98  */
99 static void
100 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
101                                  struct sk_buff *skb,
102                                  struct ieee80211_rate *rate,
103                                  int rtap_len)
104 {
105         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
106         struct ieee80211_radiotap_header *rthdr;
107         unsigned char *pos;
108         u16 rx_flags = 0;
109
110         rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
111         memset(rthdr, 0, rtap_len);
112
113         /* radiotap header, set always present flags */
114         rthdr->it_present =
115                 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
116                             (1 << IEEE80211_RADIOTAP_CHANNEL) |
117                             (1 << IEEE80211_RADIOTAP_ANTENNA) |
118                             (1 << IEEE80211_RADIOTAP_RX_FLAGS));
119         rthdr->it_len = cpu_to_le16(rtap_len);
120
121         pos = (unsigned char *)(rthdr+1);
122
123         /* the order of the following fields is important */
124
125         /* IEEE80211_RADIOTAP_TSFT */
126         if (status->flag & RX_FLAG_TSFT) {
127                 put_unaligned_le64(status->mactime, pos);
128                 rthdr->it_present |=
129                         cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
130                 pos += 8;
131         }
132
133         /* IEEE80211_RADIOTAP_FLAGS */
134         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
135                 *pos |= IEEE80211_RADIOTAP_F_FCS;
136         if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
137                 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
138         if (status->flag & RX_FLAG_SHORTPRE)
139                 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
140         pos++;
141
142         /* IEEE80211_RADIOTAP_RATE */
143         if (status->flag & RX_FLAG_HT) {
144                 /*
145                  * TODO: add following information into radiotap header once
146                  * suitable fields are defined for it:
147                  * - MCS index (status->rate_idx)
148                  * - HT40 (status->flag & RX_FLAG_40MHZ)
149                  * - short-GI (status->flag & RX_FLAG_SHORT_GI)
150                  */
151                 *pos = 0;
152         } else {
153                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
154                 *pos = rate->bitrate / 5;
155         }
156         pos++;
157
158         /* IEEE80211_RADIOTAP_CHANNEL */
159         put_unaligned_le16(status->freq, pos);
160         pos += 2;
161         if (status->band == IEEE80211_BAND_5GHZ)
162                 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
163                                    pos);
164         else if (status->flag & RX_FLAG_HT)
165                 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
166                                    pos);
167         else if (rate->flags & IEEE80211_RATE_ERP_G)
168                 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
169                                    pos);
170         else
171                 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
172                                    pos);
173         pos += 2;
174
175         /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
176         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
177                 *pos = status->signal;
178                 rthdr->it_present |=
179                         cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
180                 pos++;
181         }
182
183         /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
184
185         /* IEEE80211_RADIOTAP_ANTENNA */
186         *pos = status->antenna;
187         pos++;
188
189         /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
190
191         /* IEEE80211_RADIOTAP_RX_FLAGS */
192         /* ensure 2 byte alignment for the 2 byte field as required */
193         if ((pos - (u8 *)rthdr) & 1)
194                 pos++;
195         if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
196                 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
197         put_unaligned_le16(rx_flags, pos);
198         pos += 2;
199
200         if (status->flag & RX_FLAG_HT) {
201                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
202                 *pos++ = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
203                          IEEE80211_RADIOTAP_MCS_HAVE_GI |
204                          IEEE80211_RADIOTAP_MCS_HAVE_BW;
205                 *pos = 0;
206                 if (status->flag & RX_FLAG_SHORT_GI)
207                         *pos |= IEEE80211_RADIOTAP_MCS_SGI;
208                 if (status->flag & RX_FLAG_40MHZ)
209                         *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
210                 pos++;
211                 *pos++ = status->rate_idx;
212         }
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
231         /*
232          * First, we may need to make a copy of the skb because
233          *  (1) we need to modify it for radiotap (if not present), and
234          *  (2) the other RX handlers will modify the skb we got.
235          *
236          * We don't need to, of course, if we aren't going to return
237          * the SKB because it has a bad FCS/PLCP checksum.
238          */
239
240         /* room for the radiotap header based on driver features */
241         needed_headroom = ieee80211_rx_radiotap_len(local, status);
242
243         if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
244                 present_fcs_len = FCS_LEN;
245
246         /* make sure hdr->frame_control is on the linear part */
247         if (!pskb_may_pull(origskb, 2)) {
248                 dev_kfree_skb(origskb);
249                 return NULL;
250         }
251
252         if (!local->monitors) {
253                 if (should_drop_frame(origskb, present_fcs_len)) {
254                         dev_kfree_skb(origskb);
255                         return NULL;
256                 }
257
258                 return remove_monitor_info(local, origskb);
259         }
260
261         if (should_drop_frame(origskb, present_fcs_len)) {
262                 /* only need to expand headroom if necessary */
263                 skb = origskb;
264                 origskb = NULL;
265
266                 /*
267                  * This shouldn't trigger often because most devices have an
268                  * RX header they pull before we get here, and that should
269                  * be big enough for our radiotap information. We should
270                  * probably export the length to drivers so that we can have
271                  * them allocate enough headroom to start with.
272                  */
273                 if (skb_headroom(skb) < needed_headroom &&
274                     pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
275                         dev_kfree_skb(skb);
276                         return NULL;
277                 }
278         } else {
279                 /*
280                  * Need to make a copy and possibly remove radiotap header
281                  * and FCS from the original.
282                  */
283                 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
284
285                 origskb = remove_monitor_info(local, origskb);
286
287                 if (!skb)
288                         return origskb;
289         }
290
291         /* prepend radiotap information */
292         ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
293
294         skb_reset_mac_header(skb);
295         skb->ip_summed = CHECKSUM_UNNECESSARY;
296         skb->pkt_type = PACKET_OTHERHOST;
297         skb->protocol = htons(ETH_P_802_2);
298
299         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
300                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
301                         continue;
302
303                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
304                         continue;
305
306                 if (!ieee80211_sdata_running(sdata))
307                         continue;
308
309                 if (prev_dev) {
310                         skb2 = skb_clone(skb, GFP_ATOMIC);
311                         if (skb2) {
312                                 skb2->dev = prev_dev;
313                                 netif_receive_skb(skb2);
314                         }
315                 }
316
317                 prev_dev = sdata->dev;
318                 sdata->dev->stats.rx_packets++;
319                 sdata->dev->stats.rx_bytes += skb->len;
320         }
321
322         if (prev_dev) {
323                 skb->dev = prev_dev;
324                 netif_receive_skb(skb);
325         } else
326                 dev_kfree_skb(skb);
327
328         return origskb;
329 }
330
331
332 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
333 {
334         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
335         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
336         int tid;
337
338         /* does the frame have a qos control field? */
339         if (ieee80211_is_data_qos(hdr->frame_control)) {
340                 u8 *qc = ieee80211_get_qos_ctl(hdr);
341                 /* frame has qos control */
342                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
343                 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
344                         status->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).  If the payload data is not properly aligned and the
378  * architecture doesn't support efficient unaligned operations, mac80211
379  * will align the data.
380  *
381  * With A-MSDU frames, however, the payload data address must yield two modulo
382  * four because there are 14-byte 802.3 headers within the A-MSDU frames that
383  * push the IP header further back to a multiple of four again. Thankfully, the
384  * specs were sane enough this time around to require padding each A-MSDU
385  * subframe to a length that is a multiple of four.
386  *
387  * Padding like Atheros hardware adds which is inbetween the 802.11 header and
388  * the payload is not supported, the driver is required to move the 802.11
389  * header to be directly in front of the payload in that case.
390  */
391 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
392 {
393 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
394         WARN_ONCE((unsigned long)rx->skb->data & 1,
395                   "unaligned packet at 0x%p\n", rx->skb->data);
396 #endif
397 }
398
399
400 /* rx handlers */
401
402 static ieee80211_rx_result debug_noinline
403 ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
404 {
405         struct ieee80211_local *local = rx->local;
406         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
407         struct sk_buff *skb = rx->skb;
408
409         if (likely(!(status->rx_flags & IEEE80211_RX_IN_SCAN)))
410                 return RX_CONTINUE;
411
412         if (test_bit(SCAN_HW_SCANNING, &local->scanning) ||
413             test_bit(SCAN_SW_SCANNING, &local->scanning))
414                 return ieee80211_scan_rx(rx->sdata, skb);
415
416         /* scanning finished during invoking of handlers */
417         I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
418         return RX_DROP_UNUSABLE;
419 }
420
421
422 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
423 {
424         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
425
426         if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
427                 return 0;
428
429         return ieee80211_is_robust_mgmt_frame(hdr);
430 }
431
432
433 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
434 {
435         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
436
437         if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
438                 return 0;
439
440         return ieee80211_is_robust_mgmt_frame(hdr);
441 }
442
443
444 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
445 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
446 {
447         struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
448         struct ieee80211_mmie *mmie;
449
450         if (skb->len < 24 + sizeof(*mmie) ||
451             !is_multicast_ether_addr(hdr->da))
452                 return -1;
453
454         if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
455                 return -1; /* not a robust management frame */
456
457         mmie = (struct ieee80211_mmie *)
458                 (skb->data + skb->len - sizeof(*mmie));
459         if (mmie->element_id != WLAN_EID_MMIE ||
460             mmie->length != sizeof(*mmie) - 2)
461                 return -1;
462
463         return le16_to_cpu(mmie->key_id);
464 }
465
466
467 static ieee80211_rx_result
468 ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
469 {
470         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
471         unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
472         char *dev_addr = rx->sdata->vif.addr;
473
474         if (ieee80211_is_data(hdr->frame_control)) {
475                 if (is_multicast_ether_addr(hdr->addr1)) {
476                         if (ieee80211_has_tods(hdr->frame_control) ||
477                                 !ieee80211_has_fromds(hdr->frame_control))
478                                 return RX_DROP_MONITOR;
479                         if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
480                                 return RX_DROP_MONITOR;
481                 } else {
482                         if (!ieee80211_has_a4(hdr->frame_control))
483                                 return RX_DROP_MONITOR;
484                         if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
485                                 return RX_DROP_MONITOR;
486                 }
487         }
488
489         /* If there is not an established peer link and this is not a peer link
490          * establisment frame, beacon or probe, drop the frame.
491          */
492
493         if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
494                 struct ieee80211_mgmt *mgmt;
495
496                 if (!ieee80211_is_mgmt(hdr->frame_control))
497                         return RX_DROP_MONITOR;
498
499                 if (ieee80211_is_action(hdr->frame_control)) {
500                         mgmt = (struct ieee80211_mgmt *)hdr;
501                         if (mgmt->u.action.category != WLAN_CATEGORY_MESH_PLINK)
502                                 return RX_DROP_MONITOR;
503                         return RX_CONTINUE;
504                 }
505
506                 if (ieee80211_is_probe_req(hdr->frame_control) ||
507                     ieee80211_is_probe_resp(hdr->frame_control) ||
508                     ieee80211_is_beacon(hdr->frame_control))
509                         return RX_CONTINUE;
510
511                 return RX_DROP_MONITOR;
512
513         }
514
515 #define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
516
517         if (ieee80211_is_data(hdr->frame_control) &&
518             is_multicast_ether_addr(hdr->addr1) &&
519             mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
520                 return RX_DROP_MONITOR;
521 #undef msh_h_get
522
523         return RX_CONTINUE;
524 }
525
526 #define SEQ_MODULO 0x1000
527 #define SEQ_MASK   0xfff
528
529 static inline int seq_less(u16 sq1, u16 sq2)
530 {
531         return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
532 }
533
534 static inline u16 seq_inc(u16 sq)
535 {
536         return (sq + 1) & SEQ_MASK;
537 }
538
539 static inline u16 seq_sub(u16 sq1, u16 sq2)
540 {
541         return (sq1 - sq2) & SEQ_MASK;
542 }
543
544
545 static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
546                                             struct tid_ampdu_rx *tid_agg_rx,
547                                             int index)
548 {
549         struct ieee80211_local *local = hw_to_local(hw);
550         struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
551         struct ieee80211_rx_status *status;
552
553         lockdep_assert_held(&tid_agg_rx->reorder_lock);
554
555         if (!skb)
556                 goto no_frame;
557
558         /* release the frame from the reorder ring buffer */
559         tid_agg_rx->stored_mpdu_num--;
560         tid_agg_rx->reorder_buf[index] = NULL;
561         status = IEEE80211_SKB_RXCB(skb);
562         status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
563         skb_queue_tail(&local->rx_skb_queue, skb);
564
565 no_frame:
566         tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
567 }
568
569 static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
570                                              struct tid_ampdu_rx *tid_agg_rx,
571                                              u16 head_seq_num)
572 {
573         int index;
574
575         lockdep_assert_held(&tid_agg_rx->reorder_lock);
576
577         while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
578                 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
579                                                         tid_agg_rx->buf_size;
580                 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
581         }
582 }
583
584 /*
585  * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
586  * the skb was added to the buffer longer than this time ago, the earlier
587  * frames that have not yet been received are assumed to be lost and the skb
588  * can be released for processing. This may also release other skb's from the
589  * reorder buffer if there are no additional gaps between the frames.
590  *
591  * Callers must hold tid_agg_rx->reorder_lock.
592  */
593 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
594
595 static void ieee80211_sta_reorder_release(struct ieee80211_hw *hw,
596                                           struct tid_ampdu_rx *tid_agg_rx)
597 {
598         int index, j;
599
600         lockdep_assert_held(&tid_agg_rx->reorder_lock);
601
602         /* release the buffer until next missing frame */
603         index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
604                                                 tid_agg_rx->buf_size;
605         if (!tid_agg_rx->reorder_buf[index] &&
606             tid_agg_rx->stored_mpdu_num > 1) {
607                 /*
608                  * No buffers ready to be released, but check whether any
609                  * frames in the reorder buffer have timed out.
610                  */
611                 int skipped = 1;
612                 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
613                      j = (j + 1) % tid_agg_rx->buf_size) {
614                         if (!tid_agg_rx->reorder_buf[j]) {
615                                 skipped++;
616                                 continue;
617                         }
618                         if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
619                                         HT_RX_REORDER_BUF_TIMEOUT))
620                                 goto set_release_timer;
621
622 #ifdef CONFIG_MAC80211_HT_DEBUG
623                         if (net_ratelimit())
624                                 wiphy_debug(hw->wiphy,
625                                             "release an RX reorder frame due to timeout on earlier frames\n");
626 #endif
627                         ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
628
629                         /*
630                          * Increment the head seq# also for the skipped slots.
631                          */
632                         tid_agg_rx->head_seq_num =
633                                 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
634                         skipped = 0;
635                 }
636         } else while (tid_agg_rx->reorder_buf[index]) {
637                 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
638                 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
639                                                         tid_agg_rx->buf_size;
640         }
641
642         if (tid_agg_rx->stored_mpdu_num) {
643                 j = index = seq_sub(tid_agg_rx->head_seq_num,
644                                     tid_agg_rx->ssn) % tid_agg_rx->buf_size;
645
646                 for (; j != (index - 1) % tid_agg_rx->buf_size;
647                      j = (j + 1) % tid_agg_rx->buf_size) {
648                         if (tid_agg_rx->reorder_buf[j])
649                                 break;
650                 }
651
652  set_release_timer:
653
654                 mod_timer(&tid_agg_rx->reorder_timer,
655                           tid_agg_rx->reorder_time[j] +
656                           HT_RX_REORDER_BUF_TIMEOUT);
657         } else {
658                 del_timer(&tid_agg_rx->reorder_timer);
659         }
660 }
661
662 /*
663  * As this function belongs to the RX path it must be under
664  * rcu_read_lock protection. It returns false if the frame
665  * can be processed immediately, true if it was consumed.
666  */
667 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
668                                              struct tid_ampdu_rx *tid_agg_rx,
669                                              struct sk_buff *skb)
670 {
671         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
672         u16 sc = le16_to_cpu(hdr->seq_ctrl);
673         u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
674         u16 head_seq_num, buf_size;
675         int index;
676         bool ret = true;
677
678         spin_lock(&tid_agg_rx->reorder_lock);
679
680         buf_size = tid_agg_rx->buf_size;
681         head_seq_num = tid_agg_rx->head_seq_num;
682
683         /* frame with out of date sequence number */
684         if (seq_less(mpdu_seq_num, head_seq_num)) {
685                 dev_kfree_skb(skb);
686                 goto out;
687         }
688
689         /*
690          * If frame the sequence number exceeds our buffering window
691          * size release some previous frames to make room for this one.
692          */
693         if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
694                 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
695                 /* release stored frames up to new head to stack */
696                 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
697         }
698
699         /* Now the new frame is always in the range of the reordering buffer */
700
701         index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
702
703         /* check if we already stored this frame */
704         if (tid_agg_rx->reorder_buf[index]) {
705                 dev_kfree_skb(skb);
706                 goto out;
707         }
708
709         /*
710          * If the current MPDU is in the right order and nothing else
711          * is stored we can process it directly, no need to buffer it.
712          */
713         if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
714             tid_agg_rx->stored_mpdu_num == 0) {
715                 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
716                 ret = false;
717                 goto out;
718         }
719
720         /* put the frame in the reordering buffer */
721         tid_agg_rx->reorder_buf[index] = skb;
722         tid_agg_rx->reorder_time[index] = jiffies;
723         tid_agg_rx->stored_mpdu_num++;
724         ieee80211_sta_reorder_release(hw, tid_agg_rx);
725
726  out:
727         spin_unlock(&tid_agg_rx->reorder_lock);
728         return ret;
729 }
730
731 /*
732  * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
733  * true if the MPDU was buffered, false if it should be processed.
734  */
735 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx)
736 {
737         struct sk_buff *skb = rx->skb;
738         struct ieee80211_local *local = rx->local;
739         struct ieee80211_hw *hw = &local->hw;
740         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
741         struct sta_info *sta = rx->sta;
742         struct tid_ampdu_rx *tid_agg_rx;
743         u16 sc;
744         int tid;
745
746         if (!ieee80211_is_data_qos(hdr->frame_control))
747                 goto dont_reorder;
748
749         /*
750          * filter the QoS data rx stream according to
751          * STA/TID and check if this STA/TID is on aggregation
752          */
753
754         if (!sta)
755                 goto dont_reorder;
756
757         tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
758
759         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
760         if (!tid_agg_rx)
761                 goto dont_reorder;
762
763         /* qos null data frames are excluded */
764         if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
765                 goto dont_reorder;
766
767         /* new, potentially un-ordered, ampdu frame - process it */
768
769         /* reset session timer */
770         if (tid_agg_rx->timeout)
771                 mod_timer(&tid_agg_rx->session_timer,
772                           TU_TO_EXP_TIME(tid_agg_rx->timeout));
773
774         /* if this mpdu is fragmented - terminate rx aggregation session */
775         sc = le16_to_cpu(hdr->seq_ctrl);
776         if (sc & IEEE80211_SCTL_FRAG) {
777                 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
778                 skb_queue_tail(&rx->sdata->skb_queue, skb);
779                 ieee80211_queue_work(&local->hw, &rx->sdata->work);
780                 return;
781         }
782
783         /*
784          * No locking needed -- we will only ever process one
785          * RX packet at a time, and thus own tid_agg_rx. All
786          * other code manipulating it needs to (and does) make
787          * sure that we cannot get to it any more before doing
788          * anything with it.
789          */
790         if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb))
791                 return;
792
793  dont_reorder:
794         skb_queue_tail(&local->rx_skb_queue, skb);
795 }
796
797 static ieee80211_rx_result debug_noinline
798 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
799 {
800         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
801         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
802
803         /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
804         if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
805                 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
806                              rx->sta->last_seq_ctrl[rx->queue] ==
807                              hdr->seq_ctrl)) {
808                         if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
809                                 rx->local->dot11FrameDuplicateCount++;
810                                 rx->sta->num_duplicates++;
811                         }
812                         return RX_DROP_UNUSABLE;
813                 } else
814                         rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
815         }
816
817         if (unlikely(rx->skb->len < 16)) {
818                 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
819                 return RX_DROP_MONITOR;
820         }
821
822         /* Drop disallowed frame classes based on STA auth/assoc state;
823          * IEEE 802.11, Chap 5.5.
824          *
825          * mac80211 filters only based on association state, i.e. it drops
826          * Class 3 frames from not associated stations. hostapd sends
827          * deauth/disassoc frames when needed. In addition, hostapd is
828          * responsible for filtering on both auth and assoc states.
829          */
830
831         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
832                 return ieee80211_rx_mesh_check(rx);
833
834         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
835                       ieee80211_is_pspoll(hdr->frame_control)) &&
836                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
837                      rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
838                      (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
839                 if ((!ieee80211_has_fromds(hdr->frame_control) &&
840                      !ieee80211_has_tods(hdr->frame_control) &&
841                      ieee80211_is_data(hdr->frame_control)) ||
842                     !(status->rx_flags & IEEE80211_RX_RA_MATCH)) {
843                         /* Drop IBSS frames and frames for other hosts
844                          * silently. */
845                         return RX_DROP_MONITOR;
846                 }
847
848                 return RX_DROP_MONITOR;
849         }
850
851         return RX_CONTINUE;
852 }
853
854
855 static ieee80211_rx_result debug_noinline
856 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
857 {
858         struct sk_buff *skb = rx->skb;
859         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
860         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
861         int keyidx;
862         int hdrlen;
863         ieee80211_rx_result result = RX_DROP_UNUSABLE;
864         struct ieee80211_key *sta_ptk = NULL;
865         int mmie_keyidx = -1;
866         __le16 fc;
867
868         /*
869          * Key selection 101
870          *
871          * There are four types of keys:
872          *  - GTK (group keys)
873          *  - IGTK (group keys for management frames)
874          *  - PTK (pairwise keys)
875          *  - STK (station-to-station pairwise keys)
876          *
877          * When selecting a key, we have to distinguish between multicast
878          * (including broadcast) and unicast frames, the latter can only
879          * use PTKs and STKs while the former always use GTKs and IGTKs.
880          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
881          * unicast frames can also use key indices like GTKs. Hence, if we
882          * don't have a PTK/STK we check the key index for a WEP key.
883          *
884          * Note that in a regular BSS, multicast frames are sent by the
885          * AP only, associated stations unicast the frame to the AP first
886          * which then multicasts it on their behalf.
887          *
888          * There is also a slight problem in IBSS mode: GTKs are negotiated
889          * with each station, that is something we don't currently handle.
890          * The spec seems to expect that one negotiates the same key with
891          * every station but there's no such requirement; VLANs could be
892          * possible.
893          */
894
895         /*
896          * No point in finding a key and decrypting if the frame is neither
897          * addressed to us nor a multicast frame.
898          */
899         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
900                 return RX_CONTINUE;
901
902         /* start without a key */
903         rx->key = NULL;
904
905         if (rx->sta)
906                 sta_ptk = rcu_dereference(rx->sta->ptk);
907
908         fc = hdr->frame_control;
909
910         if (!ieee80211_has_protected(fc))
911                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
912
913         if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
914                 rx->key = sta_ptk;
915                 if ((status->flag & RX_FLAG_DECRYPTED) &&
916                     (status->flag & RX_FLAG_IV_STRIPPED))
917                         return RX_CONTINUE;
918                 /* Skip decryption if the frame is not protected. */
919                 if (!ieee80211_has_protected(fc))
920                         return RX_CONTINUE;
921         } else if (mmie_keyidx >= 0) {
922                 /* Broadcast/multicast robust management frame / BIP */
923                 if ((status->flag & RX_FLAG_DECRYPTED) &&
924                     (status->flag & RX_FLAG_IV_STRIPPED))
925                         return RX_CONTINUE;
926
927                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
928                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
929                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
930                 if (rx->sta)
931                         rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
932                 if (!rx->key)
933                         rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
934         } else if (!ieee80211_has_protected(fc)) {
935                 /*
936                  * The frame was not protected, so skip decryption. However, we
937                  * need to set rx->key if there is a key that could have been
938                  * used so that the frame may be dropped if encryption would
939                  * have been expected.
940                  */
941                 struct ieee80211_key *key = NULL;
942                 struct ieee80211_sub_if_data *sdata = rx->sdata;
943                 int i;
944
945                 if (ieee80211_is_mgmt(fc) &&
946                     is_multicast_ether_addr(hdr->addr1) &&
947                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
948                         rx->key = key;
949                 else {
950                         if (rx->sta) {
951                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
952                                         key = rcu_dereference(rx->sta->gtk[i]);
953                                         if (key)
954                                                 break;
955                                 }
956                         }
957                         if (!key) {
958                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
959                                         key = rcu_dereference(sdata->keys[i]);
960                                         if (key)
961                                                 break;
962                                 }
963                         }
964                         if (key)
965                                 rx->key = key;
966                 }
967                 return RX_CONTINUE;
968         } else {
969                 u8 keyid;
970                 /*
971                  * The device doesn't give us the IV so we won't be
972                  * able to look up the key. That's ok though, we
973                  * don't need to decrypt the frame, we just won't
974                  * be able to keep statistics accurate.
975                  * Except for key threshold notifications, should
976                  * we somehow allow the driver to tell us which key
977                  * the hardware used if this flag is set?
978                  */
979                 if ((status->flag & RX_FLAG_DECRYPTED) &&
980                     (status->flag & RX_FLAG_IV_STRIPPED))
981                         return RX_CONTINUE;
982
983                 hdrlen = ieee80211_hdrlen(fc);
984
985                 if (rx->skb->len < 8 + hdrlen)
986                         return RX_DROP_UNUSABLE; /* TODO: count this? */
987
988                 /*
989                  * no need to call ieee80211_wep_get_keyidx,
990                  * it verifies a bunch of things we've done already
991                  */
992                 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
993                 keyidx = keyid >> 6;
994
995                 /* check per-station GTK first, if multicast packet */
996                 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
997                         rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
998
999                 /* if not found, try default key */
1000                 if (!rx->key) {
1001                         rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1002
1003                         /*
1004                          * RSNA-protected unicast frames should always be
1005                          * sent with pairwise or station-to-station keys,
1006                          * but for WEP we allow using a key index as well.
1007                          */
1008                         if (rx->key &&
1009                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1010                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1011                             !is_multicast_ether_addr(hdr->addr1))
1012                                 rx->key = NULL;
1013                 }
1014         }
1015
1016         if (rx->key) {
1017                 rx->key->tx_rx_count++;
1018                 /* TODO: add threshold stuff again */
1019         } else {
1020                 return RX_DROP_MONITOR;
1021         }
1022
1023         if (skb_linearize(rx->skb))
1024                 return RX_DROP_UNUSABLE;
1025         /* the hdr variable is invalid now! */
1026
1027         switch (rx->key->conf.cipher) {
1028         case WLAN_CIPHER_SUITE_WEP40:
1029         case WLAN_CIPHER_SUITE_WEP104:
1030                 /* Check for weak IVs if possible */
1031                 if (rx->sta && ieee80211_is_data(fc) &&
1032                     (!(status->flag & RX_FLAG_IV_STRIPPED) ||
1033                      !(status->flag & RX_FLAG_DECRYPTED)) &&
1034                     ieee80211_wep_is_weak_iv(rx->skb, rx->key))
1035                         rx->sta->wep_weak_iv_count++;
1036
1037                 result = ieee80211_crypto_wep_decrypt(rx);
1038                 break;
1039         case WLAN_CIPHER_SUITE_TKIP:
1040                 result = ieee80211_crypto_tkip_decrypt(rx);
1041                 break;
1042         case WLAN_CIPHER_SUITE_CCMP:
1043                 result = ieee80211_crypto_ccmp_decrypt(rx);
1044                 break;
1045         case WLAN_CIPHER_SUITE_AES_CMAC:
1046                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1047                 break;
1048         default:
1049                 /*
1050                  * We can reach here only with HW-only algorithms
1051                  * but why didn't it decrypt the frame?!
1052                  */
1053                 return RX_DROP_UNUSABLE;
1054         }
1055
1056         /* either the frame has been decrypted or will be dropped */
1057         status->flag |= RX_FLAG_DECRYPTED;
1058
1059         return result;
1060 }
1061
1062 static ieee80211_rx_result debug_noinline
1063 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1064 {
1065         struct ieee80211_local *local;
1066         struct ieee80211_hdr *hdr;
1067         struct sk_buff *skb;
1068
1069         local = rx->local;
1070         skb = rx->skb;
1071         hdr = (struct ieee80211_hdr *) skb->data;
1072
1073         if (!local->pspolling)
1074                 return RX_CONTINUE;
1075
1076         if (!ieee80211_has_fromds(hdr->frame_control))
1077                 /* this is not from AP */
1078                 return RX_CONTINUE;
1079
1080         if (!ieee80211_is_data(hdr->frame_control))
1081                 return RX_CONTINUE;
1082
1083         if (!ieee80211_has_moredata(hdr->frame_control)) {
1084                 /* AP has no more frames buffered for us */
1085                 local->pspolling = false;
1086                 return RX_CONTINUE;
1087         }
1088
1089         /* more data bit is set, let's request a new frame from the AP */
1090         ieee80211_send_pspoll(local, rx->sdata);
1091
1092         return RX_CONTINUE;
1093 }
1094
1095 static void ap_sta_ps_start(struct sta_info *sta)
1096 {
1097         struct ieee80211_sub_if_data *sdata = sta->sdata;
1098         struct ieee80211_local *local = sdata->local;
1099
1100         atomic_inc(&sdata->bss->num_sta_ps);
1101         set_sta_flags(sta, WLAN_STA_PS_STA);
1102         if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1103                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1104 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1105         printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1106                sdata->name, sta->sta.addr, sta->sta.aid);
1107 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1108 }
1109
1110 static void ap_sta_ps_end(struct sta_info *sta)
1111 {
1112         struct ieee80211_sub_if_data *sdata = sta->sdata;
1113
1114         atomic_dec(&sdata->bss->num_sta_ps);
1115
1116 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1117         printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1118                sdata->name, sta->sta.addr, sta->sta.aid);
1119 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1120
1121         if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
1122 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1123                 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1124                        sdata->name, sta->sta.addr, sta->sta.aid);
1125 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1126                 return;
1127         }
1128
1129         ieee80211_sta_ps_deliver_wakeup(sta);
1130 }
1131
1132 int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
1133 {
1134         struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
1135         bool in_ps;
1136
1137         WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
1138
1139         /* Don't let the same PS state be set twice */
1140         in_ps = test_sta_flags(sta_inf, WLAN_STA_PS_STA);
1141         if ((start && in_ps) || (!start && !in_ps))
1142                 return -EINVAL;
1143
1144         if (start)
1145                 ap_sta_ps_start(sta_inf);
1146         else
1147                 ap_sta_ps_end(sta_inf);
1148
1149         return 0;
1150 }
1151 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1152
1153 static ieee80211_rx_result debug_noinline
1154 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1155 {
1156         struct sta_info *sta = rx->sta;
1157         struct sk_buff *skb = rx->skb;
1158         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1159         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1160
1161         if (!sta)
1162                 return RX_CONTINUE;
1163
1164         /*
1165          * Update last_rx only for IBSS packets which are for the current
1166          * BSSID to avoid keeping the current IBSS network alive in cases
1167          * where other STAs start using different BSSID.
1168          */
1169         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1170                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1171                                                 NL80211_IFTYPE_ADHOC);
1172                 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
1173                         sta->last_rx = jiffies;
1174         } else if (!is_multicast_ether_addr(hdr->addr1)) {
1175                 /*
1176                  * Mesh beacons will update last_rx when if they are found to
1177                  * match the current local configuration when processed.
1178                  */
1179                 sta->last_rx = jiffies;
1180         }
1181
1182         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
1183                 return RX_CONTINUE;
1184
1185         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1186                 ieee80211_sta_rx_notify(rx->sdata, hdr);
1187
1188         sta->rx_fragments++;
1189         sta->rx_bytes += rx->skb->len;
1190         sta->last_signal = status->signal;
1191         ewma_add(&sta->avg_signal, -status->signal);
1192
1193         /*
1194          * Change STA power saving mode only at the end of a frame
1195          * exchange sequence.
1196          */
1197         if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
1198             !ieee80211_has_morefrags(hdr->frame_control) &&
1199             !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1200             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1201              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1202                 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
1203                         /*
1204                          * Ignore doze->wake transitions that are
1205                          * indicated by non-data frames, the standard
1206                          * is unclear here, but for example going to
1207                          * PS mode and then scanning would cause a
1208                          * doze->wake transition for the probe request,
1209                          * and that is clearly undesirable.
1210                          */
1211                         if (ieee80211_is_data(hdr->frame_control) &&
1212                             !ieee80211_has_pm(hdr->frame_control))
1213                                 ap_sta_ps_end(sta);
1214                 } else {
1215                         if (ieee80211_has_pm(hdr->frame_control))
1216                                 ap_sta_ps_start(sta);
1217                 }
1218         }
1219
1220         /*
1221          * Drop (qos-)data::nullfunc frames silently, since they
1222          * are used only to control station power saving mode.
1223          */
1224         if (ieee80211_is_nullfunc(hdr->frame_control) ||
1225             ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1226                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1227
1228                 /*
1229                  * If we receive a 4-addr nullfunc frame from a STA
1230                  * that was not moved to a 4-addr STA vlan yet, drop
1231                  * the frame to the monitor interface, to make sure
1232                  * that hostapd sees it
1233                  */
1234                 if (ieee80211_has_a4(hdr->frame_control) &&
1235                     (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1236                      (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1237                       !rx->sdata->u.vlan.sta)))
1238                         return RX_DROP_MONITOR;
1239                 /*
1240                  * Update counter and free packet here to avoid
1241                  * counting this as a dropped packed.
1242                  */
1243                 sta->rx_packets++;
1244                 dev_kfree_skb(rx->skb);
1245                 return RX_QUEUED;
1246         }
1247
1248         return RX_CONTINUE;
1249 } /* ieee80211_rx_h_sta_process */
1250
1251 static inline struct ieee80211_fragment_entry *
1252 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1253                          unsigned int frag, unsigned int seq, int rx_queue,
1254                          struct sk_buff **skb)
1255 {
1256         struct ieee80211_fragment_entry *entry;
1257         int idx;
1258
1259         idx = sdata->fragment_next;
1260         entry = &sdata->fragments[sdata->fragment_next++];
1261         if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1262                 sdata->fragment_next = 0;
1263
1264         if (!skb_queue_empty(&entry->skb_list)) {
1265 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1266                 struct ieee80211_hdr *hdr =
1267                         (struct ieee80211_hdr *) entry->skb_list.next->data;
1268                 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1269                        "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
1270                        "addr1=%pM addr2=%pM\n",
1271                        sdata->name, idx,
1272                        jiffies - entry->first_frag_time, entry->seq,
1273                        entry->last_frag, hdr->addr1, hdr->addr2);
1274 #endif
1275                 __skb_queue_purge(&entry->skb_list);
1276         }
1277
1278         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1279         *skb = NULL;
1280         entry->first_frag_time = jiffies;
1281         entry->seq = seq;
1282         entry->rx_queue = rx_queue;
1283         entry->last_frag = frag;
1284         entry->ccmp = 0;
1285         entry->extra_len = 0;
1286
1287         return entry;
1288 }
1289
1290 static inline struct ieee80211_fragment_entry *
1291 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1292                           unsigned int frag, unsigned int seq,
1293                           int rx_queue, struct ieee80211_hdr *hdr)
1294 {
1295         struct ieee80211_fragment_entry *entry;
1296         int i, idx;
1297
1298         idx = sdata->fragment_next;
1299         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1300                 struct ieee80211_hdr *f_hdr;
1301
1302                 idx--;
1303                 if (idx < 0)
1304                         idx = IEEE80211_FRAGMENT_MAX - 1;
1305
1306                 entry = &sdata->fragments[idx];
1307                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1308                     entry->rx_queue != rx_queue ||
1309                     entry->last_frag + 1 != frag)
1310                         continue;
1311
1312                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1313
1314                 /*
1315                  * Check ftype and addresses are equal, else check next fragment
1316                  */
1317                 if (((hdr->frame_control ^ f_hdr->frame_control) &
1318                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1319                     compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1320                     compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1321                         continue;
1322
1323                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1324                         __skb_queue_purge(&entry->skb_list);
1325                         continue;
1326                 }
1327                 return entry;
1328         }
1329
1330         return NULL;
1331 }
1332
1333 static ieee80211_rx_result debug_noinline
1334 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1335 {
1336         struct ieee80211_hdr *hdr;
1337         u16 sc;
1338         __le16 fc;
1339         unsigned int frag, seq;
1340         struct ieee80211_fragment_entry *entry;
1341         struct sk_buff *skb;
1342         struct ieee80211_rx_status *status;
1343
1344         hdr = (struct ieee80211_hdr *)rx->skb->data;
1345         fc = hdr->frame_control;
1346         sc = le16_to_cpu(hdr->seq_ctrl);
1347         frag = sc & IEEE80211_SCTL_FRAG;
1348
1349         if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
1350                    (rx->skb)->len < 24 ||
1351                    is_multicast_ether_addr(hdr->addr1))) {
1352                 /* not fragmented */
1353                 goto out;
1354         }
1355         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1356
1357         if (skb_linearize(rx->skb))
1358                 return RX_DROP_UNUSABLE;
1359
1360         /*
1361          *  skb_linearize() might change the skb->data and
1362          *  previously cached variables (in this case, hdr) need to
1363          *  be refreshed with the new data.
1364          */
1365         hdr = (struct ieee80211_hdr *)rx->skb->data;
1366         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1367
1368         if (frag == 0) {
1369                 /* This is the first fragment of a new frame. */
1370                 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1371                                                  rx->queue, &(rx->skb));
1372                 if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
1373                     ieee80211_has_protected(fc)) {
1374                         int queue = ieee80211_is_mgmt(fc) ?
1375                                 NUM_RX_DATA_QUEUES : rx->queue;
1376                         /* Store CCMP PN so that we can verify that the next
1377                          * fragment has a sequential PN value. */
1378                         entry->ccmp = 1;
1379                         memcpy(entry->last_pn,
1380                                rx->key->u.ccmp.rx_pn[queue],
1381                                CCMP_PN_LEN);
1382                 }
1383                 return RX_QUEUED;
1384         }
1385
1386         /* This is a fragment for a frame that should already be pending in
1387          * fragment cache. Add this fragment to the end of the pending entry.
1388          */
1389         entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
1390         if (!entry) {
1391                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1392                 return RX_DROP_MONITOR;
1393         }
1394
1395         /* Verify that MPDUs within one MSDU have sequential PN values.
1396          * (IEEE 802.11i, 8.3.3.4.5) */
1397         if (entry->ccmp) {
1398                 int i;
1399                 u8 pn[CCMP_PN_LEN], *rpn;
1400                 int queue;
1401                 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
1402                         return RX_DROP_UNUSABLE;
1403                 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1404                 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1405                         pn[i]++;
1406                         if (pn[i])
1407                                 break;
1408                 }
1409                 queue = ieee80211_is_mgmt(fc) ?
1410                         NUM_RX_DATA_QUEUES : rx->queue;
1411                 rpn = rx->key->u.ccmp.rx_pn[queue];
1412                 if (memcmp(pn, rpn, CCMP_PN_LEN))
1413                         return RX_DROP_UNUSABLE;
1414                 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1415         }
1416
1417         skb_pull(rx->skb, ieee80211_hdrlen(fc));
1418         __skb_queue_tail(&entry->skb_list, rx->skb);
1419         entry->last_frag = frag;
1420         entry->extra_len += rx->skb->len;
1421         if (ieee80211_has_morefrags(fc)) {
1422                 rx->skb = NULL;
1423                 return RX_QUEUED;
1424         }
1425
1426         rx->skb = __skb_dequeue(&entry->skb_list);
1427         if (skb_tailroom(rx->skb) < entry->extra_len) {
1428                 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1429                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1430                                               GFP_ATOMIC))) {
1431                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1432                         __skb_queue_purge(&entry->skb_list);
1433                         return RX_DROP_UNUSABLE;
1434                 }
1435         }
1436         while ((skb = __skb_dequeue(&entry->skb_list))) {
1437                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1438                 dev_kfree_skb(skb);
1439         }
1440
1441         /* Complete frame has been reassembled - process it now */
1442         status = IEEE80211_SKB_RXCB(rx->skb);
1443         status->rx_flags |= IEEE80211_RX_FRAGMENTED;
1444
1445  out:
1446         if (rx->sta)
1447                 rx->sta->rx_packets++;
1448         if (is_multicast_ether_addr(hdr->addr1))
1449                 rx->local->dot11MulticastReceivedFrameCount++;
1450         else
1451                 ieee80211_led_rx(rx->local);
1452         return RX_CONTINUE;
1453 }
1454
1455 static ieee80211_rx_result debug_noinline
1456 ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
1457 {
1458         struct ieee80211_sub_if_data *sdata = rx->sdata;
1459         __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
1460         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1461
1462         if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
1463                    !(status->rx_flags & IEEE80211_RX_RA_MATCH)))
1464                 return RX_CONTINUE;
1465
1466         if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1467             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1468                 return RX_DROP_UNUSABLE;
1469
1470         if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1471                 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1472         else
1473                 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
1474
1475         /* Free PS Poll skb here instead of returning RX_DROP that would
1476          * count as an dropped frame. */
1477         dev_kfree_skb(rx->skb);
1478
1479         return RX_QUEUED;
1480 }
1481
1482 static ieee80211_rx_result debug_noinline
1483 ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
1484 {
1485         u8 *data = rx->skb->data;
1486         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
1487
1488         if (!ieee80211_is_data_qos(hdr->frame_control))
1489                 return RX_CONTINUE;
1490
1491         /* remove the qos control field, update frame type and meta-data */
1492         memmove(data + IEEE80211_QOS_CTL_LEN, data,
1493                 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1494         hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
1495         /* change frame type to non QOS */
1496         hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1497
1498         return RX_CONTINUE;
1499 }
1500
1501 static int
1502 ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
1503 {
1504         if (unlikely(!rx->sta ||
1505             !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
1506                 return -EACCES;
1507
1508         return 0;
1509 }
1510
1511 static int
1512 ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1513 {
1514         struct sk_buff *skb = rx->skb;
1515         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1516
1517         /*
1518          * Pass through unencrypted frames if the hardware has
1519          * decrypted them already.
1520          */
1521         if (status->flag & RX_FLAG_DECRYPTED)
1522                 return 0;
1523
1524         /* Drop unencrypted frames if key is set. */
1525         if (unlikely(!ieee80211_has_protected(fc) &&
1526                      !ieee80211_is_nullfunc(fc) &&
1527                      ieee80211_is_data(fc) &&
1528                      (rx->key || rx->sdata->drop_unencrypted)))
1529                 return -EACCES;
1530
1531         return 0;
1532 }
1533
1534 static int
1535 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
1536 {
1537         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1538         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1539         __le16 fc = hdr->frame_control;
1540
1541         /*
1542          * Pass through unencrypted frames if the hardware has
1543          * decrypted them already.
1544          */
1545         if (status->flag & RX_FLAG_DECRYPTED)
1546                 return 0;
1547
1548         if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1549                 if (unlikely(!ieee80211_has_protected(fc) &&
1550                              ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1551                              rx->key)) {
1552                         if (ieee80211_is_deauth(fc))
1553                                 cfg80211_send_unprot_deauth(rx->sdata->dev,
1554                                                             rx->skb->data,
1555                                                             rx->skb->len);
1556                         else if (ieee80211_is_disassoc(fc))
1557                                 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1558                                                               rx->skb->data,
1559                                                               rx->skb->len);
1560                         return -EACCES;
1561                 }
1562                 /* BIP does not use Protected field, so need to check MMIE */
1563                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
1564                              ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
1565                         if (ieee80211_is_deauth(fc))
1566                                 cfg80211_send_unprot_deauth(rx->sdata->dev,
1567                                                             rx->skb->data,
1568                                                             rx->skb->len);
1569                         else if (ieee80211_is_disassoc(fc))
1570                                 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1571                                                               rx->skb->data,
1572                                                               rx->skb->len);
1573                         return -EACCES;
1574                 }
1575                 /*
1576                  * When using MFP, Action frames are not allowed prior to
1577                  * having configured keys.
1578                  */
1579                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1580                              ieee80211_is_robust_mgmt_frame(
1581                                      (struct ieee80211_hdr *) rx->skb->data)))
1582                         return -EACCES;
1583         }
1584
1585         return 0;
1586 }
1587
1588 static int
1589 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
1590 {
1591         struct ieee80211_sub_if_data *sdata = rx->sdata;
1592         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1593         bool check_port_control = false;
1594         struct ethhdr *ehdr;
1595         int ret;
1596
1597         if (ieee80211_has_a4(hdr->frame_control) &&
1598             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
1599                 return -1;
1600
1601         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1602             !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
1603
1604                 if (!sdata->u.mgd.use_4addr)
1605                         return -1;
1606                 else
1607                         check_port_control = true;
1608         }
1609
1610         if (is_multicast_ether_addr(hdr->addr1) &&
1611             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
1612                 return -1;
1613
1614         ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
1615         if (ret < 0 || !check_port_control)
1616                 return ret;
1617
1618         ehdr = (struct ethhdr *) rx->skb->data;
1619         if (ehdr->h_proto != rx->sdata->control_port_protocol)
1620                 return -1;
1621
1622         return 0;
1623 }
1624
1625 /*
1626  * requires that rx->skb is a frame with ethernet header
1627  */
1628 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
1629 {
1630         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
1631                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1632         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1633
1634         /*
1635          * Allow EAPOL frames to us/the PAE group address regardless
1636          * of whether the frame was encrypted or not.
1637          */
1638         if (ehdr->h_proto == rx->sdata->control_port_protocol &&
1639             (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
1640              compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1641                 return true;
1642
1643         if (ieee80211_802_1x_port_control(rx) ||
1644             ieee80211_drop_unencrypted(rx, fc))
1645                 return false;
1646
1647         return true;
1648 }
1649
1650 /*
1651  * requires that rx->skb is a frame with ethernet header
1652  */
1653 static void
1654 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
1655 {
1656         struct ieee80211_sub_if_data *sdata = rx->sdata;
1657         struct net_device *dev = sdata->dev;
1658         struct sk_buff *skb, *xmit_skb;
1659         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1660         struct sta_info *dsta;
1661         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1662
1663         skb = rx->skb;
1664         xmit_skb = NULL;
1665
1666         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1667              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1668             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
1669             (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
1670             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
1671                 if (is_multicast_ether_addr(ehdr->h_dest)) {
1672                         /*
1673                          * send multicast frames both to higher layers in
1674                          * local net stack and back to the wireless medium
1675                          */
1676                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
1677                         if (!xmit_skb && net_ratelimit())
1678                                 printk(KERN_DEBUG "%s: failed to clone "
1679                                        "multicast frame\n", dev->name);
1680                 } else {
1681                         dsta = sta_info_get(sdata, skb->data);
1682                         if (dsta) {
1683                                 /*
1684                                  * The destination station is associated to
1685                                  * this AP (in this VLAN), so send the frame
1686                                  * directly to it and do not pass it to local
1687                                  * net stack.
1688                                  */
1689                                 xmit_skb = skb;
1690                                 skb = NULL;
1691                         }
1692                 }
1693         }
1694
1695         if (skb) {
1696                 int align __maybe_unused;
1697
1698 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1699                 /*
1700                  * 'align' will only take the values 0 or 2 here
1701                  * since all frames are required to be aligned
1702                  * to 2-byte boundaries when being passed to
1703                  * mac80211. That also explains the __skb_push()
1704                  * below.
1705                  */
1706                 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
1707                 if (align) {
1708                         if (WARN_ON(skb_headroom(skb) < 3)) {
1709                                 dev_kfree_skb(skb);
1710                                 skb = NULL;
1711                         } else {
1712                                 u8 *data = skb->data;
1713                                 size_t len = skb_headlen(skb);
1714                                 skb->data -= align;
1715                                 memmove(skb->data, data, len);
1716                                 skb_set_tail_pointer(skb, len);
1717                         }
1718                 }
1719 #endif
1720
1721                 if (skb) {
1722                         /* deliver to local stack */
1723                         skb->protocol = eth_type_trans(skb, dev);
1724                         memset(skb->cb, 0, sizeof(skb->cb));
1725                         netif_receive_skb(skb);
1726                 }
1727         }
1728
1729         if (xmit_skb) {
1730                 /* send to wireless media */
1731                 xmit_skb->protocol = htons(ETH_P_802_3);
1732                 skb_reset_network_header(xmit_skb);
1733                 skb_reset_mac_header(xmit_skb);
1734                 dev_queue_xmit(xmit_skb);
1735         }
1736 }
1737
1738 static ieee80211_rx_result debug_noinline
1739 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
1740 {
1741         struct net_device *dev = rx->sdata->dev;
1742         struct sk_buff *skb = rx->skb;
1743         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1744         __le16 fc = hdr->frame_control;
1745         struct sk_buff_head frame_list;
1746         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1747
1748         if (unlikely(!ieee80211_is_data(fc)))
1749                 return RX_CONTINUE;
1750
1751         if (unlikely(!ieee80211_is_data_present(fc)))
1752                 return RX_DROP_MONITOR;
1753
1754         if (!(status->rx_flags & IEEE80211_RX_AMSDU))
1755                 return RX_CONTINUE;
1756
1757         if (ieee80211_has_a4(hdr->frame_control) &&
1758             rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1759             !rx->sdata->u.vlan.sta)
1760                 return RX_DROP_UNUSABLE;
1761
1762         if (is_multicast_ether_addr(hdr->addr1) &&
1763             ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1764               rx->sdata->u.vlan.sta) ||
1765              (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1766               rx->sdata->u.mgd.use_4addr)))
1767                 return RX_DROP_UNUSABLE;
1768
1769         skb->dev = dev;
1770         __skb_queue_head_init(&frame_list);
1771
1772         if (skb_linearize(skb))
1773                 return RX_DROP_UNUSABLE;
1774
1775         ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
1776                                  rx->sdata->vif.type,
1777                                  rx->local->hw.extra_tx_headroom);
1778
1779         while (!skb_queue_empty(&frame_list)) {
1780                 rx->skb = __skb_dequeue(&frame_list);
1781
1782                 if (!ieee80211_frame_allowed(rx, fc)) {
1783                         dev_kfree_skb(rx->skb);
1784                         continue;
1785                 }
1786                 dev->stats.rx_packets++;
1787                 dev->stats.rx_bytes += rx->skb->len;
1788
1789                 ieee80211_deliver_skb(rx);
1790         }
1791
1792         return RX_QUEUED;
1793 }
1794
1795 #ifdef CONFIG_MAC80211_MESH
1796 static ieee80211_rx_result
1797 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1798 {
1799         struct ieee80211_hdr *hdr;
1800         struct ieee80211s_hdr *mesh_hdr;
1801         unsigned int hdrlen;
1802         struct sk_buff *skb = rx->skb, *fwd_skb;
1803         struct ieee80211_local *local = rx->local;
1804         struct ieee80211_sub_if_data *sdata = rx->sdata;
1805         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1806
1807         hdr = (struct ieee80211_hdr *) skb->data;
1808         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1809         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1810
1811         if (!ieee80211_is_data(hdr->frame_control))
1812                 return RX_CONTINUE;
1813
1814         if (!mesh_hdr->ttl)
1815                 /* illegal frame */
1816                 return RX_DROP_MONITOR;
1817
1818         if (mesh_hdr->flags & MESH_FLAGS_AE) {
1819                 struct mesh_path *mppath;
1820                 char *proxied_addr;
1821                 char *mpp_addr;
1822
1823                 if (is_multicast_ether_addr(hdr->addr1)) {
1824                         mpp_addr = hdr->addr3;
1825                         proxied_addr = mesh_hdr->eaddr1;
1826                 } else {
1827                         mpp_addr = hdr->addr4;
1828                         proxied_addr = mesh_hdr->eaddr2;
1829                 }
1830
1831                 rcu_read_lock();
1832                 mppath = mpp_path_lookup(proxied_addr, sdata);
1833                 if (!mppath) {
1834                         mpp_path_add(proxied_addr, mpp_addr, sdata);
1835                 } else {
1836                         spin_lock_bh(&mppath->state_lock);
1837                         if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1838                                 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
1839                         spin_unlock_bh(&mppath->state_lock);
1840                 }
1841                 rcu_read_unlock();
1842         }
1843
1844         /* Frame has reached destination.  Don't forward */
1845         if (!is_multicast_ether_addr(hdr->addr1) &&
1846             compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
1847                 return RX_CONTINUE;
1848
1849         mesh_hdr->ttl--;
1850
1851         if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
1852                 if (!mesh_hdr->ttl)
1853                         IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
1854                                                      dropped_frames_ttl);
1855                 else {
1856                         struct ieee80211_hdr *fwd_hdr;
1857                         struct ieee80211_tx_info *info;
1858
1859                         fwd_skb = skb_copy(skb, GFP_ATOMIC);
1860
1861                         if (!fwd_skb && net_ratelimit())
1862                                 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1863                                                    sdata->name);
1864                         if (!fwd_skb)
1865                                 goto out;
1866
1867                         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
1868                         memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
1869                         info = IEEE80211_SKB_CB(fwd_skb);
1870                         memset(info, 0, sizeof(*info));
1871                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1872                         info->control.vif = &rx->sdata->vif;
1873                         skb_set_queue_mapping(skb,
1874                                 ieee80211_select_queue(rx->sdata, fwd_skb));
1875                         ieee80211_set_qos_hdr(local, skb);
1876                         if (is_multicast_ether_addr(fwd_hdr->addr1))
1877                                 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1878                                                                 fwded_mcast);
1879                         else {
1880                                 int err;
1881                                 /*
1882                                  * Save TA to addr1 to send TA a path error if a
1883                                  * suitable next hop is not found
1884                                  */
1885                                 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
1886                                                 ETH_ALEN);
1887                                 err = mesh_nexthop_lookup(fwd_skb, sdata);
1888                                 /* Failed to immediately resolve next hop:
1889                                  * fwded frame was dropped or will be added
1890                                  * later to the pending skb queue.  */
1891                                 if (err)
1892                                         return RX_DROP_MONITOR;
1893
1894                                 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1895                                                                 fwded_unicast);
1896                         }
1897                         IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1898                                                      fwded_frames);
1899                         ieee80211_add_pending_skb(local, fwd_skb);
1900                 }
1901         }
1902
1903  out:
1904         if (is_multicast_ether_addr(hdr->addr1) ||
1905             sdata->dev->flags & IFF_PROMISC)
1906                 return RX_CONTINUE;
1907         else
1908                 return RX_DROP_MONITOR;
1909 }
1910 #endif
1911
1912 static ieee80211_rx_result debug_noinline
1913 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
1914 {
1915         struct ieee80211_sub_if_data *sdata = rx->sdata;
1916         struct ieee80211_local *local = rx->local;
1917         struct net_device *dev = sdata->dev;
1918         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1919         __le16 fc = hdr->frame_control;
1920         int err;
1921
1922         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
1923                 return RX_CONTINUE;
1924
1925         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1926                 return RX_DROP_MONITOR;
1927
1928         /*
1929          * Allow the cooked monitor interface of an AP to see 4-addr frames so
1930          * that a 4-addr station can be detected and moved into a separate VLAN
1931          */
1932         if (ieee80211_has_a4(hdr->frame_control) &&
1933             sdata->vif.type == NL80211_IFTYPE_AP)
1934                 return RX_DROP_MONITOR;
1935
1936         err = __ieee80211_data_to_8023(rx);
1937         if (unlikely(err))
1938                 return RX_DROP_UNUSABLE;
1939
1940         if (!ieee80211_frame_allowed(rx, fc))
1941                 return RX_DROP_MONITOR;
1942
1943         rx->skb->dev = dev;
1944
1945         dev->stats.rx_packets++;
1946         dev->stats.rx_bytes += rx->skb->len;
1947
1948         if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
1949             !is_multicast_ether_addr(
1950                     ((struct ethhdr *)rx->skb->data)->h_dest) &&
1951             (!local->scanning &&
1952              !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
1953                         mod_timer(&local->dynamic_ps_timer, jiffies +
1954                          msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
1955         }
1956
1957         ieee80211_deliver_skb(rx);
1958
1959         return RX_QUEUED;
1960 }
1961
1962 static ieee80211_rx_result debug_noinline
1963 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
1964 {
1965         struct ieee80211_local *local = rx->local;
1966         struct ieee80211_hw *hw = &local->hw;
1967         struct sk_buff *skb = rx->skb;
1968         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
1969         struct tid_ampdu_rx *tid_agg_rx;
1970         u16 start_seq_num;
1971         u16 tid;
1972
1973         if (likely(!ieee80211_is_ctl(bar->frame_control)))
1974                 return RX_CONTINUE;
1975
1976         if (ieee80211_is_back_req(bar->frame_control)) {
1977                 struct {
1978                         __le16 control, start_seq_num;
1979                 } __packed bar_data;
1980
1981                 if (!rx->sta)
1982                         return RX_DROP_MONITOR;
1983
1984                 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
1985                                   &bar_data, sizeof(bar_data)))
1986                         return RX_DROP_MONITOR;
1987
1988                 tid = le16_to_cpu(bar_data.control) >> 12;
1989
1990                 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
1991                 if (!tid_agg_rx)
1992                         return RX_DROP_MONITOR;
1993
1994                 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
1995
1996                 /* reset session timer */
1997                 if (tid_agg_rx->timeout)
1998                         mod_timer(&tid_agg_rx->session_timer,
1999                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
2000
2001                 spin_lock(&tid_agg_rx->reorder_lock);
2002                 /* release stored frames up to start of BAR */
2003                 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
2004                 spin_unlock(&tid_agg_rx->reorder_lock);
2005
2006                 kfree_skb(skb);
2007                 return RX_QUEUED;
2008         }
2009
2010         /*
2011          * After this point, we only want management frames,
2012          * so we can drop all remaining control frames to
2013          * cooked monitor interfaces.
2014          */
2015         return RX_DROP_MONITOR;
2016 }
2017
2018 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2019                                            struct ieee80211_mgmt *mgmt,
2020                                            size_t len)
2021 {
2022         struct ieee80211_local *local = sdata->local;
2023         struct sk_buff *skb;
2024         struct ieee80211_mgmt *resp;
2025
2026         if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
2027                 /* Not to own unicast address */
2028                 return;
2029         }
2030
2031         if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
2032             compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
2033                 /* Not from the current AP or not associated yet. */
2034                 return;
2035         }
2036
2037         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2038                 /* Too short SA Query request frame */
2039                 return;
2040         }
2041
2042         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2043         if (skb == NULL)
2044                 return;
2045
2046         skb_reserve(skb, local->hw.extra_tx_headroom);
2047         resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2048         memset(resp, 0, 24);
2049         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2050         memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2051         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2052         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2053                                           IEEE80211_STYPE_ACTION);
2054         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2055         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2056         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2057         memcpy(resp->u.action.u.sa_query.trans_id,
2058                mgmt->u.action.u.sa_query.trans_id,
2059                WLAN_SA_QUERY_TR_ID_LEN);
2060
2061         ieee80211_tx_skb(sdata, skb);
2062 }
2063
2064 static ieee80211_rx_result debug_noinline
2065 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2066 {
2067         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2068         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2069
2070         /*
2071          * From here on, look only at management frames.
2072          * Data and control frames are already handled,
2073          * and unknown (reserved) frames are useless.
2074          */
2075         if (rx->skb->len < 24)
2076                 return RX_DROP_MONITOR;
2077
2078         if (!ieee80211_is_mgmt(mgmt->frame_control))
2079                 return RX_DROP_MONITOR;
2080
2081         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2082                 return RX_DROP_MONITOR;
2083
2084         if (ieee80211_drop_unencrypted_mgmt(rx))
2085                 return RX_DROP_UNUSABLE;
2086
2087         return RX_CONTINUE;
2088 }
2089
2090 static ieee80211_rx_result debug_noinline
2091 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2092 {
2093         struct ieee80211_local *local = rx->local;
2094         struct ieee80211_sub_if_data *sdata = rx->sdata;
2095         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2096         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2097         int len = rx->skb->len;
2098
2099         if (!ieee80211_is_action(mgmt->frame_control))
2100                 return RX_CONTINUE;
2101
2102         /* drop too small frames */
2103         if (len < IEEE80211_MIN_ACTION_SIZE)
2104                 return RX_DROP_UNUSABLE;
2105
2106         if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
2107                 return RX_DROP_UNUSABLE;
2108
2109         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2110                 return RX_DROP_UNUSABLE;
2111
2112         switch (mgmt->u.action.category) {
2113         case WLAN_CATEGORY_BACK:
2114                 /*
2115                  * The aggregation code is not prepared to handle
2116                  * anything but STA/AP due to the BSSID handling;
2117                  * IBSS could work in the code but isn't supported
2118                  * by drivers or the standard.
2119                  */
2120                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2121                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2122                     sdata->vif.type != NL80211_IFTYPE_AP)
2123                         break;
2124
2125                 /* verify action_code is present */
2126                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2127                         break;
2128
2129                 switch (mgmt->u.action.u.addba_req.action_code) {
2130                 case WLAN_ACTION_ADDBA_REQ:
2131                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2132                                    sizeof(mgmt->u.action.u.addba_req)))
2133                                 goto invalid;
2134                         break;
2135                 case WLAN_ACTION_ADDBA_RESP:
2136                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2137                                    sizeof(mgmt->u.action.u.addba_resp)))
2138                                 goto invalid;
2139                         break;
2140                 case WLAN_ACTION_DELBA:
2141                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2142                                    sizeof(mgmt->u.action.u.delba)))
2143                                 goto invalid;
2144                         break;
2145                 default:
2146                         goto invalid;
2147                 }
2148
2149                 goto queue;
2150         case WLAN_CATEGORY_SPECTRUM_MGMT:
2151                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2152                         break;
2153
2154                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2155                         break;
2156
2157                 /* verify action_code is present */
2158                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2159                         break;
2160
2161                 switch (mgmt->u.action.u.measurement.action_code) {
2162                 case WLAN_ACTION_SPCT_MSR_REQ:
2163                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2164                                    sizeof(mgmt->u.action.u.measurement)))
2165                                 break;
2166                         ieee80211_process_measurement_req(sdata, mgmt, len);
2167                         goto handled;
2168                 case WLAN_ACTION_SPCT_CHL_SWITCH:
2169                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2170                                    sizeof(mgmt->u.action.u.chan_switch)))
2171                                 break;
2172
2173                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2174                                 break;
2175
2176                         if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
2177                                 break;
2178
2179                         goto queue;
2180                 }
2181                 break;
2182         case WLAN_CATEGORY_SA_QUERY:
2183                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2184                            sizeof(mgmt->u.action.u.sa_query)))
2185                         break;
2186
2187                 switch (mgmt->u.action.u.sa_query.action) {
2188                 case WLAN_ACTION_SA_QUERY_REQUEST:
2189                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2190                                 break;
2191                         ieee80211_process_sa_query_req(sdata, mgmt, len);
2192                         goto handled;
2193                 }
2194                 break;
2195         case WLAN_CATEGORY_MESH_PLINK:
2196                 if (!ieee80211_vif_is_mesh(&sdata->vif))
2197                         break;
2198                 goto queue;
2199         case WLAN_CATEGORY_MESH_PATH_SEL:
2200                 if (!mesh_path_sel_is_hwmp(sdata))
2201                         break;
2202                 goto queue;
2203         }
2204
2205         return RX_CONTINUE;
2206
2207  invalid:
2208         status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2209         /* will return in the next handlers */
2210         return RX_CONTINUE;
2211
2212  handled:
2213         if (rx->sta)
2214                 rx->sta->rx_packets++;
2215         dev_kfree_skb(rx->skb);
2216         return RX_QUEUED;
2217
2218  queue:
2219         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2220         skb_queue_tail(&sdata->skb_queue, rx->skb);
2221         ieee80211_queue_work(&local->hw, &sdata->work);
2222         if (rx->sta)
2223                 rx->sta->rx_packets++;
2224         return RX_QUEUED;
2225 }
2226
2227 static ieee80211_rx_result debug_noinline
2228 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2229 {
2230         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2231
2232         /* skip known-bad action frames and return them in the next handler */
2233         if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2234                 return RX_CONTINUE;
2235
2236         /*
2237          * Getting here means the kernel doesn't know how to handle
2238          * it, but maybe userspace does ... include returned frames
2239          * so userspace can register for those to know whether ones
2240          * it transmitted were processed or returned.
2241          */
2242
2243         if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
2244                              rx->skb->data, rx->skb->len,
2245                              GFP_ATOMIC)) {
2246                 if (rx->sta)
2247                         rx->sta->rx_packets++;
2248                 dev_kfree_skb(rx->skb);
2249                 return RX_QUEUED;
2250         }
2251
2252
2253         return RX_CONTINUE;
2254 }
2255
2256 static ieee80211_rx_result debug_noinline
2257 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2258 {
2259         struct ieee80211_local *local = rx->local;
2260         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2261         struct sk_buff *nskb;
2262         struct ieee80211_sub_if_data *sdata = rx->sdata;
2263         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2264
2265         if (!ieee80211_is_action(mgmt->frame_control))
2266                 return RX_CONTINUE;
2267
2268         /*
2269          * For AP mode, hostapd is responsible for handling any action
2270          * frames that we didn't handle, including returning unknown
2271          * ones. For all other modes we will return them to the sender,
2272          * setting the 0x80 bit in the action category, as required by
2273          * 802.11-2007 7.3.1.11.
2274          * Newer versions of hostapd shall also use the management frame
2275          * registration mechanisms, but older ones still use cooked
2276          * monitor interfaces so push all frames there.
2277          */
2278         if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2279             (sdata->vif.type == NL80211_IFTYPE_AP ||
2280              sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2281                 return RX_DROP_MONITOR;
2282
2283         /* do not return rejected action frames */
2284         if (mgmt->u.action.category & 0x80)
2285                 return RX_DROP_UNUSABLE;
2286
2287         nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2288                                GFP_ATOMIC);
2289         if (nskb) {
2290                 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
2291
2292                 nmgmt->u.action.category |= 0x80;
2293                 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2294                 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
2295
2296                 memset(nskb->cb, 0, sizeof(nskb->cb));
2297
2298                 ieee80211_tx_skb(rx->sdata, nskb);
2299         }
2300         dev_kfree_skb(rx->skb);
2301         return RX_QUEUED;
2302 }
2303
2304 static ieee80211_rx_result debug_noinline
2305 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
2306 {
2307         struct ieee80211_sub_if_data *sdata = rx->sdata;
2308         ieee80211_rx_result rxs;
2309         struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2310         __le16 stype;
2311
2312         rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
2313         if (rxs != RX_CONTINUE)
2314                 return rxs;
2315
2316         stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
2317
2318         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2319             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2320             sdata->vif.type != NL80211_IFTYPE_STATION)
2321                 return RX_DROP_MONITOR;
2322
2323         switch (stype) {
2324         case cpu_to_le16(IEEE80211_STYPE_BEACON):
2325         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2326                 /* process for all: mesh, mlme, ibss */
2327                 break;
2328         case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2329         case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2330                 if (is_multicast_ether_addr(mgmt->da) &&
2331                     !is_broadcast_ether_addr(mgmt->da))
2332                         return RX_DROP_MONITOR;
2333
2334                 /* process only for station */
2335                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2336                         return RX_DROP_MONITOR;
2337                 break;
2338         case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2339         case cpu_to_le16(IEEE80211_STYPE_AUTH):
2340                 /* process only for ibss */
2341                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
2342                         return RX_DROP_MONITOR;
2343                 break;
2344         default:
2345                 return RX_DROP_MONITOR;
2346         }
2347
2348         /* queue up frame and kick off work to process it */
2349         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2350         skb_queue_tail(&sdata->skb_queue, rx->skb);
2351         ieee80211_queue_work(&rx->local->hw, &sdata->work);
2352         if (rx->sta)
2353                 rx->sta->rx_packets++;
2354
2355         return RX_QUEUED;
2356 }
2357
2358 static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
2359                                             struct ieee80211_rx_data *rx)
2360 {
2361         int keyidx;
2362         unsigned int hdrlen;
2363
2364         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2365         if (rx->skb->len >= hdrlen + 4)
2366                 keyidx = rx->skb->data[hdrlen + 3] >> 6;
2367         else
2368                 keyidx = -1;
2369
2370         if (!rx->sta) {
2371                 /*
2372                  * Some hardware seem to generate incorrect Michael MIC
2373                  * reports; ignore them to avoid triggering countermeasures.
2374                  */
2375                 return;
2376         }
2377
2378         if (!ieee80211_has_protected(hdr->frame_control))
2379                 return;
2380
2381         if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
2382                 /*
2383                  * APs with pairwise keys should never receive Michael MIC
2384                  * errors for non-zero keyidx because these are reserved for
2385                  * group keys and only the AP is sending real multicast
2386                  * frames in the BSS.
2387                  */
2388                 return;
2389         }
2390
2391         if (!ieee80211_is_data(hdr->frame_control) &&
2392             !ieee80211_is_auth(hdr->frame_control))
2393                 return;
2394
2395         mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
2396                                         GFP_ATOMIC);
2397 }
2398
2399 /* TODO: use IEEE80211_RX_FRAGMENTED */
2400 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2401                                         struct ieee80211_rate *rate)
2402 {
2403         struct ieee80211_sub_if_data *sdata;
2404         struct ieee80211_local *local = rx->local;
2405         struct ieee80211_rtap_hdr {
2406                 struct ieee80211_radiotap_header hdr;
2407                 u8 flags;
2408                 u8 rate_or_pad;
2409                 __le16 chan_freq;
2410                 __le16 chan_flags;
2411         } __packed *rthdr;
2412         struct sk_buff *skb = rx->skb, *skb2;
2413         struct net_device *prev_dev = NULL;
2414         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2415
2416         /*
2417          * If cooked monitor has been processed already, then
2418          * don't do it again. If not, set the flag.
2419          */
2420         if (rx->flags & IEEE80211_RX_CMNTR)
2421                 goto out_free_skb;
2422         rx->flags |= IEEE80211_RX_CMNTR;
2423
2424         if (skb_headroom(skb) < sizeof(*rthdr) &&
2425             pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2426                 goto out_free_skb;
2427
2428         rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2429         memset(rthdr, 0, sizeof(*rthdr));
2430         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2431         rthdr->hdr.it_present =
2432                 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
2433                             (1 << IEEE80211_RADIOTAP_CHANNEL));
2434
2435         if (rate) {
2436                 rthdr->rate_or_pad = rate->bitrate / 5;
2437                 rthdr->hdr.it_present |=
2438                         cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2439         }
2440         rthdr->chan_freq = cpu_to_le16(status->freq);
2441
2442         if (status->band == IEEE80211_BAND_5GHZ)
2443                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2444                                                 IEEE80211_CHAN_5GHZ);
2445         else
2446                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2447                                                 IEEE80211_CHAN_2GHZ);
2448
2449         skb_set_mac_header(skb, 0);
2450         skb->ip_summed = CHECKSUM_UNNECESSARY;
2451         skb->pkt_type = PACKET_OTHERHOST;
2452         skb->protocol = htons(ETH_P_802_2);
2453
2454         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2455                 if (!ieee80211_sdata_running(sdata))
2456                         continue;
2457
2458                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
2459                     !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2460                         continue;
2461
2462                 if (prev_dev) {
2463                         skb2 = skb_clone(skb, GFP_ATOMIC);
2464                         if (skb2) {
2465                                 skb2->dev = prev_dev;
2466                                 netif_receive_skb(skb2);
2467                         }
2468                 }
2469
2470                 prev_dev = sdata->dev;
2471                 sdata->dev->stats.rx_packets++;
2472                 sdata->dev->stats.rx_bytes += skb->len;
2473         }
2474
2475         if (prev_dev) {
2476                 skb->dev = prev_dev;
2477                 netif_receive_skb(skb);
2478                 return;
2479         }
2480
2481  out_free_skb:
2482         dev_kfree_skb(skb);
2483 }
2484
2485 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
2486                                          ieee80211_rx_result res)
2487 {
2488         switch (res) {
2489         case RX_DROP_MONITOR:
2490                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2491                 if (rx->sta)
2492                         rx->sta->rx_dropped++;
2493                 /* fall through */
2494         case RX_CONTINUE: {
2495                 struct ieee80211_rate *rate = NULL;
2496                 struct ieee80211_supported_band *sband;
2497                 struct ieee80211_rx_status *status;
2498
2499                 status = IEEE80211_SKB_RXCB((rx->skb));
2500
2501                 sband = rx->local->hw.wiphy->bands[status->band];
2502                 if (!(status->flag & RX_FLAG_HT))
2503                         rate = &sband->bitrates[status->rate_idx];
2504
2505                 ieee80211_rx_cooked_monitor(rx, rate);
2506                 break;
2507                 }
2508         case RX_DROP_UNUSABLE:
2509                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2510                 if (rx->sta)
2511                         rx->sta->rx_dropped++;
2512                 dev_kfree_skb(rx->skb);
2513                 break;
2514         case RX_QUEUED:
2515                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
2516                 break;
2517         }
2518 }
2519
2520 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
2521 {
2522         ieee80211_rx_result res = RX_DROP_MONITOR;
2523         struct sk_buff *skb;
2524
2525 #define CALL_RXH(rxh)                   \
2526         do {                            \
2527                 res = rxh(rx);          \
2528                 if (res != RX_CONTINUE) \
2529                         goto rxh_next;  \
2530         } while (0);
2531
2532         spin_lock(&rx->local->rx_skb_queue.lock);
2533         if (rx->local->running_rx_handler)
2534                 goto unlock;
2535
2536         rx->local->running_rx_handler = true;
2537
2538         while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
2539                 spin_unlock(&rx->local->rx_skb_queue.lock);
2540
2541                 /*
2542                  * all the other fields are valid across frames
2543                  * that belong to an aMPDU since they are on the
2544                  * same TID from the same station
2545                  */
2546                 rx->skb = skb;
2547                 rx->flags = 0;
2548
2549                 CALL_RXH(ieee80211_rx_h_decrypt)
2550                 CALL_RXH(ieee80211_rx_h_check_more_data)
2551                 CALL_RXH(ieee80211_rx_h_sta_process)
2552                 CALL_RXH(ieee80211_rx_h_defragment)
2553                 CALL_RXH(ieee80211_rx_h_ps_poll)
2554                 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2555                 /* must be after MMIC verify so header is counted in MPDU mic */
2556                 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2557                 CALL_RXH(ieee80211_rx_h_amsdu)
2558 #ifdef CONFIG_MAC80211_MESH
2559                 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2560                         CALL_RXH(ieee80211_rx_h_mesh_fwding);
2561 #endif
2562                 CALL_RXH(ieee80211_rx_h_data)
2563                 CALL_RXH(ieee80211_rx_h_ctrl);
2564                 CALL_RXH(ieee80211_rx_h_mgmt_check)
2565                 CALL_RXH(ieee80211_rx_h_action)
2566                 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2567                 CALL_RXH(ieee80211_rx_h_action_return)
2568                 CALL_RXH(ieee80211_rx_h_mgmt)
2569
2570  rxh_next:
2571                 ieee80211_rx_handlers_result(rx, res);
2572                 spin_lock(&rx->local->rx_skb_queue.lock);
2573 #undef CALL_RXH
2574         }
2575
2576         rx->local->running_rx_handler = false;
2577
2578  unlock:
2579         spin_unlock(&rx->local->rx_skb_queue.lock);
2580 }
2581
2582 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
2583 {
2584         ieee80211_rx_result res = RX_DROP_MONITOR;
2585
2586 #define CALL_RXH(rxh)                   \
2587         do {                            \
2588                 res = rxh(rx);          \
2589                 if (res != RX_CONTINUE) \
2590                         goto rxh_next;  \
2591         } while (0);
2592
2593         CALL_RXH(ieee80211_rx_h_passive_scan)
2594         CALL_RXH(ieee80211_rx_h_check)
2595
2596         ieee80211_rx_reorder_ampdu(rx);
2597
2598         ieee80211_rx_handlers(rx);
2599         return;
2600
2601  rxh_next:
2602         ieee80211_rx_handlers_result(rx, res);
2603
2604 #undef CALL_RXH
2605 }
2606
2607 /*
2608  * This function makes calls into the RX path, therefore
2609  * it has to be invoked under RCU read lock.
2610  */
2611 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
2612 {
2613         struct ieee80211_rx_data rx = {
2614                 .sta = sta,
2615                 .sdata = sta->sdata,
2616                 .local = sta->local,
2617                 .queue = tid,
2618         };
2619         struct tid_ampdu_rx *tid_agg_rx;
2620
2621         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
2622         if (!tid_agg_rx)
2623                 return;
2624
2625         spin_lock(&tid_agg_rx->reorder_lock);
2626         ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
2627         spin_unlock(&tid_agg_rx->reorder_lock);
2628
2629         ieee80211_rx_handlers(&rx);
2630 }
2631
2632 /* main receive path */
2633
2634 static int prepare_for_handlers(struct ieee80211_rx_data *rx,
2635                                 struct ieee80211_hdr *hdr)
2636 {
2637         struct ieee80211_sub_if_data *sdata = rx->sdata;
2638         struct sk_buff *skb = rx->skb;
2639         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2640         u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
2641         int multicast = is_multicast_ether_addr(hdr->addr1);
2642
2643         switch (sdata->vif.type) {
2644         case NL80211_IFTYPE_STATION:
2645                 if (!bssid && !sdata->u.mgd.use_4addr)
2646                         return 0;
2647                 if (!multicast &&
2648                     compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
2649                         if (!(sdata->dev->flags & IFF_PROMISC))
2650                                 return 0;
2651                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2652                 }
2653                 break;
2654         case NL80211_IFTYPE_ADHOC:
2655                 if (!bssid)
2656                         return 0;
2657                 if (ieee80211_is_beacon(hdr->frame_control)) {
2658                         return 1;
2659                 }
2660                 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2661                         if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
2662                                 return 0;
2663                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2664                 } else if (!multicast &&
2665                            compare_ether_addr(sdata->vif.addr,
2666                                               hdr->addr1) != 0) {
2667                         if (!(sdata->dev->flags & IFF_PROMISC))
2668                                 return 0;
2669                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2670                 } else if (!rx->sta) {
2671                         int rate_idx;
2672                         if (status->flag & RX_FLAG_HT)
2673                                 rate_idx = 0; /* TODO: HT rates */
2674                         else
2675                                 rate_idx = status->rate_idx;
2676                         rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
2677                                         hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
2678                 }
2679                 break;
2680         case NL80211_IFTYPE_MESH_POINT:
2681                 if (!multicast &&
2682                     compare_ether_addr(sdata->vif.addr,
2683                                        hdr->addr1) != 0) {
2684                         if (!(sdata->dev->flags & IFF_PROMISC))
2685                                 return 0;
2686
2687                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2688                 }
2689                 break;
2690         case NL80211_IFTYPE_AP_VLAN:
2691         case NL80211_IFTYPE_AP:
2692                 if (!bssid) {
2693                         if (compare_ether_addr(sdata->vif.addr,
2694                                                hdr->addr1))
2695                                 return 0;
2696                 } else if (!ieee80211_bssid_match(bssid,
2697                                         sdata->vif.addr)) {
2698                         if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
2699                             !ieee80211_is_beacon(hdr->frame_control))
2700                                 return 0;
2701                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2702                 }
2703                 break;
2704         case NL80211_IFTYPE_WDS:
2705                 if (bssid || !ieee80211_is_data(hdr->frame_control))
2706                         return 0;
2707                 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2708                         return 0;
2709                 break;
2710         default:
2711                 /* should never get here */
2712                 WARN_ON(1);
2713                 break;
2714         }
2715
2716         return 1;
2717 }
2718
2719 /*
2720  * This function returns whether or not the SKB
2721  * was destined for RX processing or not, which,
2722  * if consume is true, is equivalent to whether
2723  * or not the skb was consumed.
2724  */
2725 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
2726                                             struct sk_buff *skb, bool consume)
2727 {
2728         struct ieee80211_local *local = rx->local;
2729         struct ieee80211_sub_if_data *sdata = rx->sdata;
2730         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2731         struct ieee80211_hdr *hdr = (void *)skb->data;
2732         int prepares;
2733
2734         rx->skb = skb;
2735         status->rx_flags |= IEEE80211_RX_RA_MATCH;
2736         prepares = prepare_for_handlers(rx, hdr);
2737
2738         if (!prepares)
2739                 return false;
2740
2741         if (status->flag & RX_FLAG_MMIC_ERROR) {
2742                 if (status->rx_flags & IEEE80211_RX_RA_MATCH)
2743                         ieee80211_rx_michael_mic_report(hdr, rx);
2744                 return false;
2745         }
2746
2747         if (!consume) {
2748                 skb = skb_copy(skb, GFP_ATOMIC);
2749                 if (!skb) {
2750                         if (net_ratelimit())
2751                                 wiphy_debug(local->hw.wiphy,
2752                                         "failed to copy skb for %s\n",
2753                                         sdata->name);
2754                         return true;
2755                 }
2756
2757                 rx->skb = skb;
2758         }
2759
2760         ieee80211_invoke_rx_handlers(rx);
2761         return true;
2762 }
2763
2764 /*
2765  * This is the actual Rx frames handler. as it blongs to Rx path it must
2766  * be called with rcu_read_lock protection.
2767  */
2768 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2769                                          struct sk_buff *skb)
2770 {
2771         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2772         struct ieee80211_local *local = hw_to_local(hw);
2773         struct ieee80211_sub_if_data *sdata;
2774         struct ieee80211_hdr *hdr;
2775         __le16 fc;
2776         struct ieee80211_rx_data rx;
2777         struct ieee80211_sub_if_data *prev;
2778         struct sta_info *sta, *tmp, *prev_sta;
2779         int err = 0;
2780
2781         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
2782         memset(&rx, 0, sizeof(rx));
2783         rx.skb = skb;
2784         rx.local = local;
2785
2786         if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
2787                 local->dot11ReceivedFragmentCount++;
2788
2789         if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2790                      test_bit(SCAN_SW_SCANNING, &local->scanning)))
2791                 status->rx_flags |= IEEE80211_RX_IN_SCAN;
2792
2793         if (ieee80211_is_mgmt(fc))
2794                 err = skb_linearize(skb);
2795         else
2796                 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
2797
2798         if (err) {
2799                 dev_kfree_skb(skb);
2800                 return;
2801         }
2802
2803         hdr = (struct ieee80211_hdr *)skb->data;
2804         ieee80211_parse_qos(&rx);
2805         ieee80211_verify_alignment(&rx);
2806
2807         if (ieee80211_is_data(fc)) {
2808                 prev_sta = NULL;
2809
2810                 for_each_sta_info(local, hdr->addr2, sta, tmp) {
2811                         if (!prev_sta) {
2812                                 prev_sta = sta;
2813                                 continue;
2814                         }
2815
2816                         rx.sta = prev_sta;
2817                         rx.sdata = prev_sta->sdata;
2818                         ieee80211_prepare_and_rx_handle(&rx, skb, false);
2819
2820                         prev_sta = sta;
2821                 }
2822
2823                 if (prev_sta) {
2824                         rx.sta = prev_sta;
2825                         rx.sdata = prev_sta->sdata;
2826
2827                         if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
2828                                 return;
2829                         goto out;
2830                 }
2831         }
2832
2833         prev = NULL;
2834
2835         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2836                 if (!ieee80211_sdata_running(sdata))
2837                         continue;
2838
2839                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2840                     sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2841                         continue;
2842
2843                 /*
2844                  * frame is destined for this interface, but if it's
2845                  * not also for the previous one we handle that after
2846                  * the loop to avoid copying the SKB once too much
2847                  */
2848
2849                 if (!prev) {
2850                         prev = sdata;
2851                         continue;
2852                 }
2853
2854                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2855                 rx.sdata = prev;
2856                 ieee80211_prepare_and_rx_handle(&rx, skb, false);
2857
2858                 prev = sdata;
2859         }
2860
2861         if (prev) {
2862                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2863                 rx.sdata = prev;
2864
2865                 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
2866                         return;
2867         }
2868
2869  out:
2870         dev_kfree_skb(skb);
2871 }
2872
2873 /*
2874  * This is the receive path handler. It is called by a low level driver when an
2875  * 802.11 MPDU is received from the hardware.
2876  */
2877 void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
2878 {
2879         struct ieee80211_local *local = hw_to_local(hw);
2880         struct ieee80211_rate *rate = NULL;
2881         struct ieee80211_supported_band *sband;
2882         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2883
2884         WARN_ON_ONCE(softirq_count() == 0);
2885
2886         if (WARN_ON(status->band < 0 ||
2887                     status->band >= IEEE80211_NUM_BANDS))
2888                 goto drop;
2889
2890         sband = local->hw.wiphy->bands[status->band];
2891         if (WARN_ON(!sband))
2892                 goto drop;
2893
2894         /*
2895          * If we're suspending, it is possible although not too likely
2896          * that we'd be receiving frames after having already partially
2897          * quiesced the stack. We can't process such frames then since
2898          * that might, for example, cause stations to be added or other
2899          * driver callbacks be invoked.
2900          */
2901         if (unlikely(local->quiescing || local->suspended))
2902                 goto drop;
2903
2904         /*
2905          * The same happens when we're not even started,
2906          * but that's worth a warning.
2907          */
2908         if (WARN_ON(!local->started))
2909                 goto drop;
2910
2911         if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
2912                 /*
2913                  * Validate the rate, unless a PLCP error means that
2914                  * we probably can't have a valid rate here anyway.
2915                  */
2916
2917                 if (status->flag & RX_FLAG_HT) {
2918                         /*
2919                          * rate_idx is MCS index, which can be [0-76]
2920                          * as documented on:
2921                          *
2922                          * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2923                          *
2924                          * Anything else would be some sort of driver or
2925                          * hardware error. The driver should catch hardware
2926                          * errors.
2927                          */
2928                         if (WARN((status->rate_idx < 0 ||
2929                                  status->rate_idx > 76),
2930                                  "Rate marked as an HT rate but passed "
2931                                  "status->rate_idx is not "
2932                                  "an MCS index [0-76]: %d (0x%02x)\n",
2933                                  status->rate_idx,
2934                                  status->rate_idx))
2935                                 goto drop;
2936                 } else {
2937                         if (WARN_ON(status->rate_idx < 0 ||
2938                                     status->rate_idx >= sband->n_bitrates))
2939                                 goto drop;
2940                         rate = &sband->bitrates[status->rate_idx];
2941                 }
2942         }
2943
2944         status->rx_flags = 0;
2945
2946         /*
2947          * key references and virtual interfaces are protected using RCU
2948          * and this requires that we are in a read-side RCU section during
2949          * receive processing
2950          */
2951         rcu_read_lock();
2952
2953         /*
2954          * Frames with failed FCS/PLCP checksum are not returned,
2955          * all other frames are returned without radiotap header
2956          * if it was previously present.
2957          * Also, frames with less than 16 bytes are dropped.
2958          */
2959         skb = ieee80211_rx_monitor(local, skb, rate);
2960         if (!skb) {
2961                 rcu_read_unlock();
2962                 return;
2963         }
2964
2965         ieee80211_tpt_led_trig_rx(local,
2966                         ((struct ieee80211_hdr *)skb->data)->frame_control,
2967                         skb->len);
2968         __ieee80211_rx_handle_packet(hw, skb);
2969
2970         rcu_read_unlock();
2971
2972         return;
2973  drop:
2974         kfree_skb(skb);
2975 }
2976 EXPORT_SYMBOL(ieee80211_rx);
2977
2978 /* This is a version of the rx handler that can be called from hard irq
2979  * context. Post the skb on the queue and schedule the tasklet */
2980 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
2981 {
2982         struct ieee80211_local *local = hw_to_local(hw);
2983
2984         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2985
2986         skb->pkt_type = IEEE80211_RX_MSG;
2987         skb_queue_tail(&local->skb_queue, skb);
2988         tasklet_schedule(&local->tasklet);
2989 }
2990 EXPORT_SYMBOL(ieee80211_rx_irqsafe);