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