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