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