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