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