6d0d37d6a6d772b7d7b7e4de7ee25f07d71709ff
[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             is_multicast_ether_addr(hdr->addr1))
769                 goto dont_reorder;
770
771         /*
772          * filter the QoS data rx stream according to
773          * STA/TID and check if this STA/TID is on aggregation
774          */
775
776         if (!sta)
777                 goto dont_reorder;
778
779         tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
780
781         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
782         if (!tid_agg_rx)
783                 goto dont_reorder;
784
785         /* qos null data frames are excluded */
786         if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
787                 goto dont_reorder;
788
789         /* new, potentially un-ordered, ampdu frame - process it */
790
791         /* reset session timer */
792         if (tid_agg_rx->timeout)
793                 mod_timer(&tid_agg_rx->session_timer,
794                           TU_TO_EXP_TIME(tid_agg_rx->timeout));
795
796         /* if this mpdu is fragmented - terminate rx aggregation session */
797         sc = le16_to_cpu(hdr->seq_ctrl);
798         if (sc & IEEE80211_SCTL_FRAG) {
799                 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
800                 skb_queue_tail(&rx->sdata->skb_queue, skb);
801                 ieee80211_queue_work(&local->hw, &rx->sdata->work);
802                 return;
803         }
804
805         /*
806          * No locking needed -- we will only ever process one
807          * RX packet at a time, and thus own tid_agg_rx. All
808          * other code manipulating it needs to (and does) make
809          * sure that we cannot get to it any more before doing
810          * anything with it.
811          */
812         if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb))
813                 return;
814
815  dont_reorder:
816         skb_queue_tail(&local->rx_skb_queue, skb);
817 }
818
819 static ieee80211_rx_result debug_noinline
820 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
821 {
822         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
823         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
824
825         /*
826          * Drop duplicate 802.11 retransmissions
827          * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
828          */
829         if (rx->skb->len >= 24 && rx->sta &&
830             !ieee80211_is_ctl(hdr->frame_control) &&
831             !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
832             !is_multicast_ether_addr(hdr->addr1)) {
833                 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
834                              rx->sta->last_seq_ctrl[rx->seqno_idx] ==
835                              hdr->seq_ctrl)) {
836                         if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
837                                 rx->local->dot11FrameDuplicateCount++;
838                                 rx->sta->num_duplicates++;
839                         }
840                         return RX_DROP_UNUSABLE;
841                 } else
842                         rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
843         }
844
845         if (unlikely(rx->skb->len < 16)) {
846                 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
847                 return RX_DROP_MONITOR;
848         }
849
850         /* Drop disallowed frame classes based on STA auth/assoc state;
851          * IEEE 802.11, Chap 5.5.
852          *
853          * mac80211 filters only based on association state, i.e. it drops
854          * Class 3 frames from not associated stations. hostapd sends
855          * deauth/disassoc frames when needed. In addition, hostapd is
856          * responsible for filtering on both auth and assoc states.
857          */
858
859         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
860                 return ieee80211_rx_mesh_check(rx);
861
862         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
863                       ieee80211_is_pspoll(hdr->frame_control)) &&
864                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
865                      rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
866                      (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
867                 if (rx->sta && rx->sta->dummy &&
868                     ieee80211_is_data_present(hdr->frame_control)) {
869                         unsigned int hdrlen;
870                         __be16 ethertype;
871
872                         hdrlen = ieee80211_hdrlen(hdr->frame_control);
873
874                         if (rx->skb->len < hdrlen + 8)
875                                 return RX_DROP_MONITOR;
876
877                         skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
878                         if (ethertype == rx->sdata->control_port_protocol)
879                                 return RX_CONTINUE;
880                 }
881                 return RX_DROP_MONITOR;
882         }
883
884         return RX_CONTINUE;
885 }
886
887
888 static ieee80211_rx_result debug_noinline
889 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
890 {
891         struct sk_buff *skb = rx->skb;
892         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
893         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
894         int keyidx;
895         int hdrlen;
896         ieee80211_rx_result result = RX_DROP_UNUSABLE;
897         struct ieee80211_key *sta_ptk = NULL;
898         int mmie_keyidx = -1;
899         __le16 fc;
900
901         /*
902          * Key selection 101
903          *
904          * There are four types of keys:
905          *  - GTK (group keys)
906          *  - IGTK (group keys for management frames)
907          *  - PTK (pairwise keys)
908          *  - STK (station-to-station pairwise keys)
909          *
910          * When selecting a key, we have to distinguish between multicast
911          * (including broadcast) and unicast frames, the latter can only
912          * use PTKs and STKs while the former always use GTKs and IGTKs.
913          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
914          * unicast frames can also use key indices like GTKs. Hence, if we
915          * don't have a PTK/STK we check the key index for a WEP key.
916          *
917          * Note that in a regular BSS, multicast frames are sent by the
918          * AP only, associated stations unicast the frame to the AP first
919          * which then multicasts it on their behalf.
920          *
921          * There is also a slight problem in IBSS mode: GTKs are negotiated
922          * with each station, that is something we don't currently handle.
923          * The spec seems to expect that one negotiates the same key with
924          * every station but there's no such requirement; VLANs could be
925          * possible.
926          */
927
928         /*
929          * No point in finding a key and decrypting if the frame is neither
930          * addressed to us nor a multicast frame.
931          */
932         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
933                 return RX_CONTINUE;
934
935         /* start without a key */
936         rx->key = NULL;
937
938         if (rx->sta)
939                 sta_ptk = rcu_dereference(rx->sta->ptk);
940
941         fc = hdr->frame_control;
942
943         if (!ieee80211_has_protected(fc))
944                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
945
946         if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
947                 rx->key = sta_ptk;
948                 if ((status->flag & RX_FLAG_DECRYPTED) &&
949                     (status->flag & RX_FLAG_IV_STRIPPED))
950                         return RX_CONTINUE;
951                 /* Skip decryption if the frame is not protected. */
952                 if (!ieee80211_has_protected(fc))
953                         return RX_CONTINUE;
954         } else if (mmie_keyidx >= 0) {
955                 /* Broadcast/multicast robust management frame / BIP */
956                 if ((status->flag & RX_FLAG_DECRYPTED) &&
957                     (status->flag & RX_FLAG_IV_STRIPPED))
958                         return RX_CONTINUE;
959
960                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
961                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
962                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
963                 if (rx->sta)
964                         rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
965                 if (!rx->key)
966                         rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
967         } else if (!ieee80211_has_protected(fc)) {
968                 /*
969                  * The frame was not protected, so skip decryption. However, we
970                  * need to set rx->key if there is a key that could have been
971                  * used so that the frame may be dropped if encryption would
972                  * have been expected.
973                  */
974                 struct ieee80211_key *key = NULL;
975                 struct ieee80211_sub_if_data *sdata = rx->sdata;
976                 int i;
977
978                 if (ieee80211_is_mgmt(fc) &&
979                     is_multicast_ether_addr(hdr->addr1) &&
980                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
981                         rx->key = key;
982                 else {
983                         if (rx->sta) {
984                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
985                                         key = rcu_dereference(rx->sta->gtk[i]);
986                                         if (key)
987                                                 break;
988                                 }
989                         }
990                         if (!key) {
991                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
992                                         key = rcu_dereference(sdata->keys[i]);
993                                         if (key)
994                                                 break;
995                                 }
996                         }
997                         if (key)
998                                 rx->key = key;
999                 }
1000                 return RX_CONTINUE;
1001         } else {
1002                 u8 keyid;
1003                 /*
1004                  * The device doesn't give us the IV so we won't be
1005                  * able to look up the key. That's ok though, we
1006                  * don't need to decrypt the frame, we just won't
1007                  * be able to keep statistics accurate.
1008                  * Except for key threshold notifications, should
1009                  * we somehow allow the driver to tell us which key
1010                  * the hardware used if this flag is set?
1011                  */
1012                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1013                     (status->flag & RX_FLAG_IV_STRIPPED))
1014                         return RX_CONTINUE;
1015
1016                 hdrlen = ieee80211_hdrlen(fc);
1017
1018                 if (rx->skb->len < 8 + hdrlen)
1019                         return RX_DROP_UNUSABLE; /* TODO: count this? */
1020
1021                 /*
1022                  * no need to call ieee80211_wep_get_keyidx,
1023                  * it verifies a bunch of things we've done already
1024                  */
1025                 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1026                 keyidx = keyid >> 6;
1027
1028                 /* check per-station GTK first, if multicast packet */
1029                 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1030                         rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1031
1032                 /* if not found, try default key */
1033                 if (!rx->key) {
1034                         rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1035
1036                         /*
1037                          * RSNA-protected unicast frames should always be
1038                          * sent with pairwise or station-to-station keys,
1039                          * but for WEP we allow using a key index as well.
1040                          */
1041                         if (rx->key &&
1042                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1043                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1044                             !is_multicast_ether_addr(hdr->addr1))
1045                                 rx->key = NULL;
1046                 }
1047         }
1048
1049         if (rx->key) {
1050                 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1051                         return RX_DROP_MONITOR;
1052
1053                 rx->key->tx_rx_count++;
1054                 /* TODO: add threshold stuff again */
1055         } else {
1056                 return RX_DROP_MONITOR;
1057         }
1058
1059         if (skb_linearize(rx->skb))
1060                 return RX_DROP_UNUSABLE;
1061         /* the hdr variable is invalid now! */
1062
1063         switch (rx->key->conf.cipher) {
1064         case WLAN_CIPHER_SUITE_WEP40:
1065         case WLAN_CIPHER_SUITE_WEP104:
1066                 /* Check for weak IVs if possible */
1067                 if (rx->sta && ieee80211_is_data(fc) &&
1068                     (!(status->flag & RX_FLAG_IV_STRIPPED) ||
1069                      !(status->flag & RX_FLAG_DECRYPTED)) &&
1070                     ieee80211_wep_is_weak_iv(rx->skb, rx->key))
1071                         rx->sta->wep_weak_iv_count++;
1072
1073                 result = ieee80211_crypto_wep_decrypt(rx);
1074                 break;
1075         case WLAN_CIPHER_SUITE_TKIP:
1076                 result = ieee80211_crypto_tkip_decrypt(rx);
1077                 break;
1078         case WLAN_CIPHER_SUITE_CCMP:
1079                 result = ieee80211_crypto_ccmp_decrypt(rx);
1080                 break;
1081         case WLAN_CIPHER_SUITE_AES_CMAC:
1082                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1083                 break;
1084         default:
1085                 /*
1086                  * We can reach here only with HW-only algorithms
1087                  * but why didn't it decrypt the frame?!
1088                  */
1089                 return RX_DROP_UNUSABLE;
1090         }
1091
1092         /* either the frame has been decrypted or will be dropped */
1093         status->flag |= RX_FLAG_DECRYPTED;
1094
1095         return result;
1096 }
1097
1098 static ieee80211_rx_result debug_noinline
1099 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1100 {
1101         struct ieee80211_local *local;
1102         struct ieee80211_hdr *hdr;
1103         struct sk_buff *skb;
1104
1105         local = rx->local;
1106         skb = rx->skb;
1107         hdr = (struct ieee80211_hdr *) skb->data;
1108
1109         if (!local->pspolling)
1110                 return RX_CONTINUE;
1111
1112         if (!ieee80211_has_fromds(hdr->frame_control))
1113                 /* this is not from AP */
1114                 return RX_CONTINUE;
1115
1116         if (!ieee80211_is_data(hdr->frame_control))
1117                 return RX_CONTINUE;
1118
1119         if (!ieee80211_has_moredata(hdr->frame_control)) {
1120                 /* AP has no more frames buffered for us */
1121                 local->pspolling = false;
1122                 return RX_CONTINUE;
1123         }
1124
1125         /* more data bit is set, let's request a new frame from the AP */
1126         ieee80211_send_pspoll(local, rx->sdata);
1127
1128         return RX_CONTINUE;
1129 }
1130
1131 static void ap_sta_ps_start(struct sta_info *sta)
1132 {
1133         struct ieee80211_sub_if_data *sdata = sta->sdata;
1134         struct ieee80211_local *local = sdata->local;
1135
1136         atomic_inc(&sdata->bss->num_sta_ps);
1137         set_sta_flag(sta, WLAN_STA_PS_STA);
1138         if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1139                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1140 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1141         printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1142                sdata->name, sta->sta.addr, sta->sta.aid);
1143 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1144 }
1145
1146 static void ap_sta_ps_end(struct sta_info *sta)
1147 {
1148         struct ieee80211_sub_if_data *sdata = sta->sdata;
1149
1150         atomic_dec(&sdata->bss->num_sta_ps);
1151
1152 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1153         printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1154                sdata->name, sta->sta.addr, sta->sta.aid);
1155 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1156
1157         if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1158 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1159                 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1160                        sdata->name, sta->sta.addr, sta->sta.aid);
1161 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1162                 return;
1163         }
1164
1165         ieee80211_sta_ps_deliver_wakeup(sta);
1166 }
1167
1168 int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
1169 {
1170         struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
1171         bool in_ps;
1172
1173         WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
1174
1175         /* Don't let the same PS state be set twice */
1176         in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
1177         if ((start && in_ps) || (!start && !in_ps))
1178                 return -EINVAL;
1179
1180         if (start)
1181                 ap_sta_ps_start(sta_inf);
1182         else
1183                 ap_sta_ps_end(sta_inf);
1184
1185         return 0;
1186 }
1187 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1188
1189 static ieee80211_rx_result debug_noinline
1190 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1191 {
1192         struct ieee80211_sub_if_data *sdata = rx->sdata;
1193         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1194         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1195         int tid, ac;
1196
1197         if (!rx->sta || !(status->rx_flags & IEEE80211_RX_RA_MATCH))
1198                 return RX_CONTINUE;
1199
1200         if (sdata->vif.type != NL80211_IFTYPE_AP &&
1201             sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1202                 return RX_CONTINUE;
1203
1204         /*
1205          * The device handles station powersave, so don't do anything about
1206          * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1207          * it to mac80211 since they're handled.)
1208          */
1209         if (sdata->local->hw.flags & IEEE80211_HW_AP_LINK_PS)
1210                 return RX_CONTINUE;
1211
1212         /*
1213          * Don't do anything if the station isn't already asleep. In
1214          * the uAPSD case, the station will probably be marked asleep,
1215          * in the PS-Poll case the station must be confused ...
1216          */
1217         if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1218                 return RX_CONTINUE;
1219
1220         if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1221                 if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
1222                         if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
1223                                 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1224                         else
1225                                 set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
1226                 }
1227
1228                 /* Free PS Poll skb here instead of returning RX_DROP that would
1229                  * count as an dropped frame. */
1230                 dev_kfree_skb(rx->skb);
1231
1232                 return RX_QUEUED;
1233         } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1234                    !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1235                    ieee80211_has_pm(hdr->frame_control) &&
1236                    (ieee80211_is_data_qos(hdr->frame_control) ||
1237                     ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1238                 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1239                 ac = ieee802_1d_to_ac[tid & 7];
1240
1241                 /*
1242                  * If this AC is not trigger-enabled do nothing.
1243                  *
1244                  * NB: This could/should check a separate bitmap of trigger-
1245                  * enabled queues, but for now we only implement uAPSD w/o
1246                  * TSPEC changes to the ACs, so they're always the same.
1247                  */
1248                 if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
1249                         return RX_CONTINUE;
1250
1251                 /* if we are in a service period, do nothing */
1252                 if (test_sta_flag(rx->sta, WLAN_STA_SP))
1253                         return RX_CONTINUE;
1254
1255                 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
1256                         ieee80211_sta_ps_deliver_uapsd(rx->sta);
1257                 else
1258                         set_sta_flag(rx->sta, WLAN_STA_UAPSD);
1259         }
1260
1261         return RX_CONTINUE;
1262 }
1263
1264 static ieee80211_rx_result debug_noinline
1265 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1266 {
1267         struct sta_info *sta = rx->sta;
1268         struct sk_buff *skb = rx->skb;
1269         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1270         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1271
1272         if (!sta)
1273                 return RX_CONTINUE;
1274
1275         /*
1276          * Update last_rx only for IBSS packets which are for the current
1277          * BSSID to avoid keeping the current IBSS network alive in cases
1278          * where other STAs start using different BSSID.
1279          */
1280         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1281                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1282                                                 NL80211_IFTYPE_ADHOC);
1283                 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0) {
1284                         sta->last_rx = jiffies;
1285                         if (ieee80211_is_data(hdr->frame_control)) {
1286                                 sta->last_rx_rate_idx = status->rate_idx;
1287                                 sta->last_rx_rate_flag = status->flag;
1288                         }
1289                 }
1290         } else if (!is_multicast_ether_addr(hdr->addr1)) {
1291                 /*
1292                  * Mesh beacons will update last_rx when if they are found to
1293                  * match the current local configuration when processed.
1294                  */
1295                 sta->last_rx = jiffies;
1296                 if (ieee80211_is_data(hdr->frame_control)) {
1297                         sta->last_rx_rate_idx = status->rate_idx;
1298                         sta->last_rx_rate_flag = status->flag;
1299                 }
1300         }
1301
1302         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
1303                 return RX_CONTINUE;
1304
1305         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1306                 ieee80211_sta_rx_notify(rx->sdata, hdr);
1307
1308         sta->rx_fragments++;
1309         sta->rx_bytes += rx->skb->len;
1310         sta->last_signal = status->signal;
1311         ewma_add(&sta->avg_signal, -status->signal);
1312
1313         /*
1314          * Change STA power saving mode only at the end of a frame
1315          * exchange sequence.
1316          */
1317         if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
1318             !ieee80211_has_morefrags(hdr->frame_control) &&
1319             !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1320             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1321              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1322                 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1323                         /*
1324                          * Ignore doze->wake transitions that are
1325                          * indicated by non-data frames, the standard
1326                          * is unclear here, but for example going to
1327                          * PS mode and then scanning would cause a
1328                          * doze->wake transition for the probe request,
1329                          * and that is clearly undesirable.
1330                          */
1331                         if (ieee80211_is_data(hdr->frame_control) &&
1332                             !ieee80211_has_pm(hdr->frame_control))
1333                                 ap_sta_ps_end(sta);
1334                 } else {
1335                         if (ieee80211_has_pm(hdr->frame_control))
1336                                 ap_sta_ps_start(sta);
1337                 }
1338         }
1339
1340         /*
1341          * Drop (qos-)data::nullfunc frames silently, since they
1342          * are used only to control station power saving mode.
1343          */
1344         if (ieee80211_is_nullfunc(hdr->frame_control) ||
1345             ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1346                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1347
1348                 /*
1349                  * If we receive a 4-addr nullfunc frame from a STA
1350                  * that was not moved to a 4-addr STA vlan yet, drop
1351                  * the frame to the monitor interface, to make sure
1352                  * that hostapd sees it
1353                  */
1354                 if (ieee80211_has_a4(hdr->frame_control) &&
1355                     (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1356                      (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1357                       !rx->sdata->u.vlan.sta)))
1358                         return RX_DROP_MONITOR;
1359                 /*
1360                  * Update counter and free packet here to avoid
1361                  * counting this as a dropped packed.
1362                  */
1363                 sta->rx_packets++;
1364                 dev_kfree_skb(rx->skb);
1365                 return RX_QUEUED;
1366         }
1367
1368         return RX_CONTINUE;
1369 } /* ieee80211_rx_h_sta_process */
1370
1371 static inline struct ieee80211_fragment_entry *
1372 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1373                          unsigned int frag, unsigned int seq, int rx_queue,
1374                          struct sk_buff **skb)
1375 {
1376         struct ieee80211_fragment_entry *entry;
1377         int idx;
1378
1379         idx = sdata->fragment_next;
1380         entry = &sdata->fragments[sdata->fragment_next++];
1381         if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1382                 sdata->fragment_next = 0;
1383
1384         if (!skb_queue_empty(&entry->skb_list)) {
1385 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1386                 struct ieee80211_hdr *hdr =
1387                         (struct ieee80211_hdr *) entry->skb_list.next->data;
1388                 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1389                        "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
1390                        "addr1=%pM addr2=%pM\n",
1391                        sdata->name, idx,
1392                        jiffies - entry->first_frag_time, entry->seq,
1393                        entry->last_frag, hdr->addr1, hdr->addr2);
1394 #endif
1395                 __skb_queue_purge(&entry->skb_list);
1396         }
1397
1398         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1399         *skb = NULL;
1400         entry->first_frag_time = jiffies;
1401         entry->seq = seq;
1402         entry->rx_queue = rx_queue;
1403         entry->last_frag = frag;
1404         entry->ccmp = 0;
1405         entry->extra_len = 0;
1406
1407         return entry;
1408 }
1409
1410 static inline struct ieee80211_fragment_entry *
1411 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1412                           unsigned int frag, unsigned int seq,
1413                           int rx_queue, struct ieee80211_hdr *hdr)
1414 {
1415         struct ieee80211_fragment_entry *entry;
1416         int i, idx;
1417
1418         idx = sdata->fragment_next;
1419         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1420                 struct ieee80211_hdr *f_hdr;
1421
1422                 idx--;
1423                 if (idx < 0)
1424                         idx = IEEE80211_FRAGMENT_MAX - 1;
1425
1426                 entry = &sdata->fragments[idx];
1427                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1428                     entry->rx_queue != rx_queue ||
1429                     entry->last_frag + 1 != frag)
1430                         continue;
1431
1432                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1433
1434                 /*
1435                  * Check ftype and addresses are equal, else check next fragment
1436                  */
1437                 if (((hdr->frame_control ^ f_hdr->frame_control) &
1438                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1439                     compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1440                     compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1441                         continue;
1442
1443                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1444                         __skb_queue_purge(&entry->skb_list);
1445                         continue;
1446                 }
1447                 return entry;
1448         }
1449
1450         return NULL;
1451 }
1452
1453 static ieee80211_rx_result debug_noinline
1454 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1455 {
1456         struct ieee80211_hdr *hdr;
1457         u16 sc;
1458         __le16 fc;
1459         unsigned int frag, seq;
1460         struct ieee80211_fragment_entry *entry;
1461         struct sk_buff *skb;
1462         struct ieee80211_rx_status *status;
1463
1464         hdr = (struct ieee80211_hdr *)rx->skb->data;
1465         fc = hdr->frame_control;
1466
1467         if (ieee80211_is_ctl(fc))
1468                 return RX_CONTINUE;
1469
1470         sc = le16_to_cpu(hdr->seq_ctrl);
1471         frag = sc & IEEE80211_SCTL_FRAG;
1472
1473         if (is_multicast_ether_addr(hdr->addr1)) {
1474                 rx->local->dot11MulticastReceivedFrameCount++;
1475                 goto out_no_led;
1476         }
1477
1478         if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1479                 goto out;
1480
1481         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1482
1483         if (skb_linearize(rx->skb))
1484                 return RX_DROP_UNUSABLE;
1485
1486         /*
1487          *  skb_linearize() might change the skb->data and
1488          *  previously cached variables (in this case, hdr) need to
1489          *  be refreshed with the new data.
1490          */
1491         hdr = (struct ieee80211_hdr *)rx->skb->data;
1492         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1493
1494         if (frag == 0) {
1495                 /* This is the first fragment of a new frame. */
1496                 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1497                                                  rx->seqno_idx, &(rx->skb));
1498                 if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
1499                     ieee80211_has_protected(fc)) {
1500                         int queue = rx->security_idx;
1501                         /* Store CCMP PN so that we can verify that the next
1502                          * fragment has a sequential PN value. */
1503                         entry->ccmp = 1;
1504                         memcpy(entry->last_pn,
1505                                rx->key->u.ccmp.rx_pn[queue],
1506                                CCMP_PN_LEN);
1507                 }
1508                 return RX_QUEUED;
1509         }
1510
1511         /* This is a fragment for a frame that should already be pending in
1512          * fragment cache. Add this fragment to the end of the pending entry.
1513          */
1514         entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1515                                           rx->seqno_idx, hdr);
1516         if (!entry) {
1517                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1518                 return RX_DROP_MONITOR;
1519         }
1520
1521         /* Verify that MPDUs within one MSDU have sequential PN values.
1522          * (IEEE 802.11i, 8.3.3.4.5) */
1523         if (entry->ccmp) {
1524                 int i;
1525                 u8 pn[CCMP_PN_LEN], *rpn;
1526                 int queue;
1527                 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
1528                         return RX_DROP_UNUSABLE;
1529                 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1530                 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1531                         pn[i]++;
1532                         if (pn[i])
1533                                 break;
1534                 }
1535                 queue = rx->security_idx;
1536                 rpn = rx->key->u.ccmp.rx_pn[queue];
1537                 if (memcmp(pn, rpn, CCMP_PN_LEN))
1538                         return RX_DROP_UNUSABLE;
1539                 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1540         }
1541
1542         skb_pull(rx->skb, ieee80211_hdrlen(fc));
1543         __skb_queue_tail(&entry->skb_list, rx->skb);
1544         entry->last_frag = frag;
1545         entry->extra_len += rx->skb->len;
1546         if (ieee80211_has_morefrags(fc)) {
1547                 rx->skb = NULL;
1548                 return RX_QUEUED;
1549         }
1550
1551         rx->skb = __skb_dequeue(&entry->skb_list);
1552         if (skb_tailroom(rx->skb) < entry->extra_len) {
1553                 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1554                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1555                                               GFP_ATOMIC))) {
1556                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1557                         __skb_queue_purge(&entry->skb_list);
1558                         return RX_DROP_UNUSABLE;
1559                 }
1560         }
1561         while ((skb = __skb_dequeue(&entry->skb_list))) {
1562                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1563                 dev_kfree_skb(skb);
1564         }
1565
1566         /* Complete frame has been reassembled - process it now */
1567         status = IEEE80211_SKB_RXCB(rx->skb);
1568         status->rx_flags |= IEEE80211_RX_FRAGMENTED;
1569
1570  out:
1571         ieee80211_led_rx(rx->local);
1572  out_no_led:
1573         if (rx->sta)
1574                 rx->sta->rx_packets++;
1575         return RX_CONTINUE;
1576 }
1577
1578 static ieee80211_rx_result debug_noinline
1579 ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
1580 {
1581         u8 *data = rx->skb->data;
1582         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
1583
1584         if (!ieee80211_is_data_qos(hdr->frame_control))
1585                 return RX_CONTINUE;
1586
1587         /* remove the qos control field, update frame type and meta-data */
1588         memmove(data + IEEE80211_QOS_CTL_LEN, data,
1589                 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1590         hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
1591         /* change frame type to non QOS */
1592         hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1593
1594         return RX_CONTINUE;
1595 }
1596
1597 static int
1598 ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
1599 {
1600         if (unlikely(!rx->sta ||
1601             !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
1602                 return -EACCES;
1603
1604         return 0;
1605 }
1606
1607 static int
1608 ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1609 {
1610         struct sk_buff *skb = rx->skb;
1611         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1612
1613         /*
1614          * Pass through unencrypted frames if the hardware has
1615          * decrypted them already.
1616          */
1617         if (status->flag & RX_FLAG_DECRYPTED)
1618                 return 0;
1619
1620         /* Drop unencrypted frames if key is set. */
1621         if (unlikely(!ieee80211_has_protected(fc) &&
1622                      !ieee80211_is_nullfunc(fc) &&
1623                      ieee80211_is_data(fc) &&
1624                      (rx->key || rx->sdata->drop_unencrypted)))
1625                 return -EACCES;
1626
1627         return 0;
1628 }
1629
1630 static int
1631 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
1632 {
1633         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1634         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1635         __le16 fc = hdr->frame_control;
1636
1637         /*
1638          * Pass through unencrypted frames if the hardware has
1639          * decrypted them already.
1640          */
1641         if (status->flag & RX_FLAG_DECRYPTED)
1642                 return 0;
1643
1644         if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
1645                 if (unlikely(!ieee80211_has_protected(fc) &&
1646                              ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1647                              rx->key)) {
1648                         if (ieee80211_is_deauth(fc))
1649                                 cfg80211_send_unprot_deauth(rx->sdata->dev,
1650                                                             rx->skb->data,
1651                                                             rx->skb->len);
1652                         else if (ieee80211_is_disassoc(fc))
1653                                 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1654                                                               rx->skb->data,
1655                                                               rx->skb->len);
1656                         return -EACCES;
1657                 }
1658                 /* BIP does not use Protected field, so need to check MMIE */
1659                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
1660                              ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
1661                         if (ieee80211_is_deauth(fc))
1662                                 cfg80211_send_unprot_deauth(rx->sdata->dev,
1663                                                             rx->skb->data,
1664                                                             rx->skb->len);
1665                         else if (ieee80211_is_disassoc(fc))
1666                                 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1667                                                               rx->skb->data,
1668                                                               rx->skb->len);
1669                         return -EACCES;
1670                 }
1671                 /*
1672                  * When using MFP, Action frames are not allowed prior to
1673                  * having configured keys.
1674                  */
1675                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1676                              ieee80211_is_robust_mgmt_frame(
1677                                      (struct ieee80211_hdr *) rx->skb->data)))
1678                         return -EACCES;
1679         }
1680
1681         return 0;
1682 }
1683
1684 static int
1685 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
1686 {
1687         struct ieee80211_sub_if_data *sdata = rx->sdata;
1688         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1689         bool check_port_control = false;
1690         struct ethhdr *ehdr;
1691         int ret;
1692
1693         *port_control = false;
1694         if (ieee80211_has_a4(hdr->frame_control) &&
1695             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
1696                 return -1;
1697
1698         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1699             !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
1700
1701                 if (!sdata->u.mgd.use_4addr)
1702                         return -1;
1703                 else
1704                         check_port_control = true;
1705         }
1706
1707         if (is_multicast_ether_addr(hdr->addr1) &&
1708             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
1709                 return -1;
1710
1711         ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
1712         if (ret < 0)
1713                 return ret;
1714
1715         ehdr = (struct ethhdr *) rx->skb->data;
1716         if (ehdr->h_proto == rx->sdata->control_port_protocol)
1717                 *port_control = true;
1718         else if (check_port_control)
1719                 return -1;
1720
1721         return 0;
1722 }
1723
1724 /*
1725  * requires that rx->skb is a frame with ethernet header
1726  */
1727 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
1728 {
1729         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
1730                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1731         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1732
1733         /*
1734          * Allow EAPOL frames to us/the PAE group address regardless
1735          * of whether the frame was encrypted or not.
1736          */
1737         if (ehdr->h_proto == rx->sdata->control_port_protocol &&
1738             (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
1739              compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1740                 return true;
1741
1742         if (ieee80211_802_1x_port_control(rx) ||
1743             ieee80211_drop_unencrypted(rx, fc))
1744                 return false;
1745
1746         return true;
1747 }
1748
1749 /*
1750  * requires that rx->skb is a frame with ethernet header
1751  */
1752 static void
1753 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
1754 {
1755         struct ieee80211_sub_if_data *sdata = rx->sdata;
1756         struct net_device *dev = sdata->dev;
1757         struct sk_buff *skb, *xmit_skb;
1758         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1759         struct sta_info *dsta;
1760         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1761
1762         skb = rx->skb;
1763         xmit_skb = NULL;
1764
1765         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1766              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1767             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
1768             (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
1769             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
1770                 if (is_multicast_ether_addr(ehdr->h_dest)) {
1771                         /*
1772                          * send multicast frames both to higher layers in
1773                          * local net stack and back to the wireless medium
1774                          */
1775                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
1776                         if (!xmit_skb && net_ratelimit())
1777                                 printk(KERN_DEBUG "%s: failed to clone "
1778                                        "multicast frame\n", dev->name);
1779                 } else {
1780                         dsta = sta_info_get(sdata, skb->data);
1781                         if (dsta) {
1782                                 /*
1783                                  * The destination station is associated to
1784                                  * this AP (in this VLAN), so send the frame
1785                                  * directly to it and do not pass it to local
1786                                  * net stack.
1787                                  */
1788                                 xmit_skb = skb;
1789                                 skb = NULL;
1790                         }
1791                 }
1792         }
1793
1794         if (skb) {
1795                 int align __maybe_unused;
1796
1797 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1798                 /*
1799                  * 'align' will only take the values 0 or 2 here
1800                  * since all frames are required to be aligned
1801                  * to 2-byte boundaries when being passed to
1802                  * mac80211. That also explains the __skb_push()
1803                  * below.
1804                  */
1805                 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
1806                 if (align) {
1807                         if (WARN_ON(skb_headroom(skb) < 3)) {
1808                                 dev_kfree_skb(skb);
1809                                 skb = NULL;
1810                         } else {
1811                                 u8 *data = skb->data;
1812                                 size_t len = skb_headlen(skb);
1813                                 skb->data -= align;
1814                                 memmove(skb->data, data, len);
1815                                 skb_set_tail_pointer(skb, len);
1816                         }
1817                 }
1818 #endif
1819
1820                 if (skb) {
1821                         /* deliver to local stack */
1822                         skb->protocol = eth_type_trans(skb, dev);
1823                         memset(skb->cb, 0, sizeof(skb->cb));
1824                         netif_receive_skb(skb);
1825                 }
1826         }
1827
1828         if (xmit_skb) {
1829                 /* send to wireless media */
1830                 xmit_skb->protocol = htons(ETH_P_802_3);
1831                 skb_reset_network_header(xmit_skb);
1832                 skb_reset_mac_header(xmit_skb);
1833                 dev_queue_xmit(xmit_skb);
1834         }
1835 }
1836
1837 static ieee80211_rx_result debug_noinline
1838 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
1839 {
1840         struct net_device *dev = rx->sdata->dev;
1841         struct sk_buff *skb = rx->skb;
1842         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1843         __le16 fc = hdr->frame_control;
1844         struct sk_buff_head frame_list;
1845         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1846
1847         if (unlikely(!ieee80211_is_data(fc)))
1848                 return RX_CONTINUE;
1849
1850         if (unlikely(!ieee80211_is_data_present(fc)))
1851                 return RX_DROP_MONITOR;
1852
1853         if (!(status->rx_flags & IEEE80211_RX_AMSDU))
1854                 return RX_CONTINUE;
1855
1856         if (ieee80211_has_a4(hdr->frame_control) &&
1857             rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1858             !rx->sdata->u.vlan.sta)
1859                 return RX_DROP_UNUSABLE;
1860
1861         if (is_multicast_ether_addr(hdr->addr1) &&
1862             ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1863               rx->sdata->u.vlan.sta) ||
1864              (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1865               rx->sdata->u.mgd.use_4addr)))
1866                 return RX_DROP_UNUSABLE;
1867
1868         skb->dev = dev;
1869         __skb_queue_head_init(&frame_list);
1870
1871         if (skb_linearize(skb))
1872                 return RX_DROP_UNUSABLE;
1873
1874         ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
1875                                  rx->sdata->vif.type,
1876                                  rx->local->hw.extra_tx_headroom, true);
1877
1878         while (!skb_queue_empty(&frame_list)) {
1879                 rx->skb = __skb_dequeue(&frame_list);
1880
1881                 if (!ieee80211_frame_allowed(rx, fc)) {
1882                         dev_kfree_skb(rx->skb);
1883                         continue;
1884                 }
1885                 dev->stats.rx_packets++;
1886                 dev->stats.rx_bytes += rx->skb->len;
1887
1888                 ieee80211_deliver_skb(rx);
1889         }
1890
1891         return RX_QUEUED;
1892 }
1893
1894 #ifdef CONFIG_MAC80211_MESH
1895 static ieee80211_rx_result
1896 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1897 {
1898         struct ieee80211_hdr *hdr;
1899         struct ieee80211s_hdr *mesh_hdr;
1900         unsigned int hdrlen;
1901         struct sk_buff *skb = rx->skb, *fwd_skb;
1902         struct ieee80211_local *local = rx->local;
1903         struct ieee80211_sub_if_data *sdata = rx->sdata;
1904         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1905
1906         hdr = (struct ieee80211_hdr *) skb->data;
1907         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1908
1909         /* make sure fixed part of mesh header is there, also checks skb len */
1910         if (!pskb_may_pull(rx->skb, hdrlen + 6))
1911                 return RX_DROP_MONITOR;
1912
1913         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1914
1915         /* make sure full mesh header is there, also checks skb len */
1916         if (!pskb_may_pull(rx->skb,
1917                            hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
1918                 return RX_DROP_MONITOR;
1919
1920         /* reload pointers */
1921         hdr = (struct ieee80211_hdr *) skb->data;
1922         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1923
1924         if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
1925                 return RX_DROP_MONITOR;
1926
1927         /* frame is in RMC, don't forward */
1928         if (ieee80211_is_data(hdr->frame_control) &&
1929             is_multicast_ether_addr(hdr->addr1) &&
1930             mesh_rmc_check(hdr->addr3, mesh_hdr, rx->sdata))
1931                 return RX_DROP_MONITOR;
1932
1933         if (!ieee80211_is_data(hdr->frame_control) ||
1934             !(status->rx_flags & IEEE80211_RX_RA_MATCH))
1935                 return RX_CONTINUE;
1936
1937         if (!mesh_hdr->ttl)
1938                 /* illegal frame */
1939                 return RX_DROP_MONITOR;
1940
1941         if (ieee80211_queue_stopped(&local->hw, skb_get_queue_mapping(skb))) {
1942                 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1943                                                 dropped_frames_congestion);
1944                 return RX_DROP_MONITOR;
1945         }
1946
1947         if (mesh_hdr->flags & MESH_FLAGS_AE) {
1948                 struct mesh_path *mppath;
1949                 char *proxied_addr;
1950                 char *mpp_addr;
1951
1952                 if (is_multicast_ether_addr(hdr->addr1)) {
1953                         mpp_addr = hdr->addr3;
1954                         proxied_addr = mesh_hdr->eaddr1;
1955                 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
1956                         /* has_a4 already checked in ieee80211_rx_mesh_check */
1957                         mpp_addr = hdr->addr4;
1958                         proxied_addr = mesh_hdr->eaddr2;
1959                 } else {
1960                         return RX_DROP_MONITOR;
1961                 }
1962
1963                 rcu_read_lock();
1964                 mppath = mpp_path_lookup(proxied_addr, sdata);
1965                 if (!mppath) {
1966                         mpp_path_add(proxied_addr, mpp_addr, sdata);
1967                 } else {
1968                         spin_lock_bh(&mppath->state_lock);
1969                         if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1970                                 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
1971                         spin_unlock_bh(&mppath->state_lock);
1972                 }
1973                 rcu_read_unlock();
1974         }
1975
1976         /* Frame has reached destination.  Don't forward */
1977         if (!is_multicast_ether_addr(hdr->addr1) &&
1978             compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
1979                 return RX_CONTINUE;
1980
1981         mesh_hdr->ttl--;
1982
1983         {
1984                 if (!mesh_hdr->ttl)
1985                         IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
1986                                                      dropped_frames_ttl);
1987                 else {
1988                         struct ieee80211_hdr *fwd_hdr;
1989                         struct ieee80211_tx_info *info;
1990
1991                         fwd_skb = skb_copy(skb, GFP_ATOMIC);
1992
1993                         if (!fwd_skb && net_ratelimit())
1994                                 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1995                                                    sdata->name);
1996                         if (!fwd_skb)
1997                                 goto out;
1998
1999                         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
2000                         memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2001                         info = IEEE80211_SKB_CB(fwd_skb);
2002                         memset(info, 0, sizeof(*info));
2003                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2004                         info->control.vif = &rx->sdata->vif;
2005                         if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2006                                 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
2007                                                                 fwded_mcast);
2008                                 skb_set_queue_mapping(fwd_skb,
2009                                         ieee80211_select_queue(sdata, fwd_skb));
2010                                 ieee80211_set_qos_hdr(sdata, fwd_skb);
2011                         } else {
2012                                 int err;
2013                                 /*
2014                                  * Save TA to addr1 to send TA a path error if a
2015                                  * suitable next hop is not found
2016                                  */
2017                                 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
2018                                                 ETH_ALEN);
2019                                 err = mesh_nexthop_lookup(fwd_skb, sdata);
2020                                 /* Failed to immediately resolve next hop:
2021                                  * fwded frame was dropped or will be added
2022                                  * later to the pending skb queue.  */
2023                                 if (err)
2024                                         return RX_DROP_MONITOR;
2025
2026                                 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
2027                                                                 fwded_unicast);
2028                         }
2029                         IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
2030                                                      fwded_frames);
2031                         ieee80211_add_pending_skb(local, fwd_skb);
2032                 }
2033         }
2034
2035  out:
2036         if (is_multicast_ether_addr(hdr->addr1) ||
2037             sdata->dev->flags & IFF_PROMISC)
2038                 return RX_CONTINUE;
2039         else
2040                 return RX_DROP_MONITOR;
2041 }
2042 #endif
2043
2044 static ieee80211_rx_result debug_noinline
2045 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2046 {
2047         struct ieee80211_sub_if_data *sdata = rx->sdata;
2048         struct ieee80211_local *local = rx->local;
2049         struct net_device *dev = sdata->dev;
2050         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2051         __le16 fc = hdr->frame_control;
2052         bool port_control;
2053         int err;
2054
2055         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2056                 return RX_CONTINUE;
2057
2058         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2059                 return RX_DROP_MONITOR;
2060
2061         /*
2062          * Allow the cooked monitor interface of an AP to see 4-addr frames so
2063          * that a 4-addr station can be detected and moved into a separate VLAN
2064          */
2065         if (ieee80211_has_a4(hdr->frame_control) &&
2066             sdata->vif.type == NL80211_IFTYPE_AP)
2067                 return RX_DROP_MONITOR;
2068
2069         err = __ieee80211_data_to_8023(rx, &port_control);
2070         if (unlikely(err))
2071                 return RX_DROP_UNUSABLE;
2072
2073         if (!ieee80211_frame_allowed(rx, fc))
2074                 return RX_DROP_MONITOR;
2075
2076         if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2077             unlikely(port_control) && sdata->bss) {
2078                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2079                                      u.ap);
2080                 dev = sdata->dev;
2081                 rx->sdata = sdata;
2082         }
2083
2084         rx->skb->dev = dev;
2085
2086         dev->stats.rx_packets++;
2087         dev->stats.rx_bytes += rx->skb->len;
2088
2089         if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2090             !is_multicast_ether_addr(
2091                     ((struct ethhdr *)rx->skb->data)->h_dest) &&
2092             (!local->scanning &&
2093              !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
2094                         mod_timer(&local->dynamic_ps_timer, jiffies +
2095                          msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2096         }
2097
2098         ieee80211_deliver_skb(rx);
2099
2100         return RX_QUEUED;
2101 }
2102
2103 static ieee80211_rx_result debug_noinline
2104 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
2105 {
2106         struct ieee80211_local *local = rx->local;
2107         struct ieee80211_hw *hw = &local->hw;
2108         struct sk_buff *skb = rx->skb;
2109         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2110         struct tid_ampdu_rx *tid_agg_rx;
2111         u16 start_seq_num;
2112         u16 tid;
2113
2114         if (likely(!ieee80211_is_ctl(bar->frame_control)))
2115                 return RX_CONTINUE;
2116
2117         if (ieee80211_is_back_req(bar->frame_control)) {
2118                 struct {
2119                         __le16 control, start_seq_num;
2120                 } __packed bar_data;
2121
2122                 if (!rx->sta)
2123                         return RX_DROP_MONITOR;
2124
2125                 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2126                                   &bar_data, sizeof(bar_data)))
2127                         return RX_DROP_MONITOR;
2128
2129                 tid = le16_to_cpu(bar_data.control) >> 12;
2130
2131                 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2132                 if (!tid_agg_rx)
2133                         return RX_DROP_MONITOR;
2134
2135                 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2136
2137                 /* reset session timer */
2138                 if (tid_agg_rx->timeout)
2139                         mod_timer(&tid_agg_rx->session_timer,
2140                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
2141
2142                 spin_lock(&tid_agg_rx->reorder_lock);
2143                 /* release stored frames up to start of BAR */
2144                 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
2145                 spin_unlock(&tid_agg_rx->reorder_lock);
2146
2147                 kfree_skb(skb);
2148                 return RX_QUEUED;
2149         }
2150
2151         /*
2152          * After this point, we only want management frames,
2153          * so we can drop all remaining control frames to
2154          * cooked monitor interfaces.
2155          */
2156         return RX_DROP_MONITOR;
2157 }
2158
2159 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2160                                            struct ieee80211_mgmt *mgmt,
2161                                            size_t len)
2162 {
2163         struct ieee80211_local *local = sdata->local;
2164         struct sk_buff *skb;
2165         struct ieee80211_mgmt *resp;
2166
2167         if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
2168                 /* Not to own unicast address */
2169                 return;
2170         }
2171
2172         if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
2173             compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
2174                 /* Not from the current AP or not associated yet. */
2175                 return;
2176         }
2177
2178         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2179                 /* Too short SA Query request frame */
2180                 return;
2181         }
2182
2183         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2184         if (skb == NULL)
2185                 return;
2186
2187         skb_reserve(skb, local->hw.extra_tx_headroom);
2188         resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2189         memset(resp, 0, 24);
2190         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2191         memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2192         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2193         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2194                                           IEEE80211_STYPE_ACTION);
2195         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2196         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2197         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2198         memcpy(resp->u.action.u.sa_query.trans_id,
2199                mgmt->u.action.u.sa_query.trans_id,
2200                WLAN_SA_QUERY_TR_ID_LEN);
2201
2202         ieee80211_tx_skb(sdata, skb);
2203 }
2204
2205 static ieee80211_rx_result debug_noinline
2206 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2207 {
2208         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2209         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2210
2211         /*
2212          * From here on, look only at management frames.
2213          * Data and control frames are already handled,
2214          * and unknown (reserved) frames are useless.
2215          */
2216         if (rx->skb->len < 24)
2217                 return RX_DROP_MONITOR;
2218
2219         if (!ieee80211_is_mgmt(mgmt->frame_control))
2220                 return RX_DROP_MONITOR;
2221
2222         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2223                 return RX_DROP_MONITOR;
2224
2225         if (ieee80211_drop_unencrypted_mgmt(rx))
2226                 return RX_DROP_UNUSABLE;
2227
2228         return RX_CONTINUE;
2229 }
2230
2231 static ieee80211_rx_result debug_noinline
2232 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2233 {
2234         struct ieee80211_local *local = rx->local;
2235         struct ieee80211_sub_if_data *sdata = rx->sdata;
2236         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2237         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2238         int len = rx->skb->len;
2239
2240         if (!ieee80211_is_action(mgmt->frame_control))
2241                 return RX_CONTINUE;
2242
2243         /* drop too small frames */
2244         if (len < IEEE80211_MIN_ACTION_SIZE)
2245                 return RX_DROP_UNUSABLE;
2246
2247         if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
2248                 return RX_DROP_UNUSABLE;
2249
2250         if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2251                 return RX_DROP_UNUSABLE;
2252
2253         switch (mgmt->u.action.category) {
2254         case WLAN_CATEGORY_BACK:
2255                 /*
2256                  * The aggregation code is not prepared to handle
2257                  * anything but STA/AP due to the BSSID handling;
2258                  * IBSS could work in the code but isn't supported
2259                  * by drivers or the standard.
2260                  */
2261                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2262                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2263                     sdata->vif.type != NL80211_IFTYPE_AP)
2264                         break;
2265
2266                 /* verify action_code is present */
2267                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2268                         break;
2269
2270                 switch (mgmt->u.action.u.addba_req.action_code) {
2271                 case WLAN_ACTION_ADDBA_REQ:
2272                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2273                                    sizeof(mgmt->u.action.u.addba_req)))
2274                                 goto invalid;
2275                         break;
2276                 case WLAN_ACTION_ADDBA_RESP:
2277                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2278                                    sizeof(mgmt->u.action.u.addba_resp)))
2279                                 goto invalid;
2280                         break;
2281                 case WLAN_ACTION_DELBA:
2282                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2283                                    sizeof(mgmt->u.action.u.delba)))
2284                                 goto invalid;
2285                         break;
2286                 default:
2287                         goto invalid;
2288                 }
2289
2290                 goto queue;
2291         case WLAN_CATEGORY_SPECTRUM_MGMT:
2292                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2293                         break;
2294
2295                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2296                         break;
2297
2298                 /* verify action_code is present */
2299                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2300                         break;
2301
2302                 switch (mgmt->u.action.u.measurement.action_code) {
2303                 case WLAN_ACTION_SPCT_MSR_REQ:
2304                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2305                                    sizeof(mgmt->u.action.u.measurement)))
2306                                 break;
2307                         ieee80211_process_measurement_req(sdata, mgmt, len);
2308                         goto handled;
2309                 case WLAN_ACTION_SPCT_CHL_SWITCH:
2310                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2311                                    sizeof(mgmt->u.action.u.chan_switch)))
2312                                 break;
2313
2314                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2315                                 break;
2316
2317                         if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
2318                                 break;
2319
2320                         goto queue;
2321                 }
2322                 break;
2323         case WLAN_CATEGORY_SA_QUERY:
2324                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2325                            sizeof(mgmt->u.action.u.sa_query)))
2326                         break;
2327
2328                 switch (mgmt->u.action.u.sa_query.action) {
2329                 case WLAN_ACTION_SA_QUERY_REQUEST:
2330                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2331                                 break;
2332                         ieee80211_process_sa_query_req(sdata, mgmt, len);
2333                         goto handled;
2334                 }
2335                 break;
2336         case WLAN_CATEGORY_SELF_PROTECTED:
2337                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2338                            sizeof(mgmt->u.action.u.self_prot.action_code)))
2339                         break;
2340
2341                 switch (mgmt->u.action.u.self_prot.action_code) {
2342                 case WLAN_SP_MESH_PEERING_OPEN:
2343                 case WLAN_SP_MESH_PEERING_CLOSE:
2344                 case WLAN_SP_MESH_PEERING_CONFIRM:
2345                         if (!ieee80211_vif_is_mesh(&sdata->vif))
2346                                 goto invalid;
2347                         if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
2348                                 /* userspace handles this frame */
2349                                 break;
2350                         goto queue;
2351                 case WLAN_SP_MGK_INFORM:
2352                 case WLAN_SP_MGK_ACK:
2353                         if (!ieee80211_vif_is_mesh(&sdata->vif))
2354                                 goto invalid;
2355                         break;
2356                 }
2357                 break;
2358         case WLAN_CATEGORY_MESH_ACTION:
2359                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2360                            sizeof(mgmt->u.action.u.mesh_action.action_code)))
2361                         break;
2362
2363                 if (!ieee80211_vif_is_mesh(&sdata->vif))
2364                         break;
2365                 if (mesh_action_is_path_sel(mgmt) &&
2366                   (!mesh_path_sel_is_hwmp(sdata)))
2367                         break;
2368                 goto queue;
2369         }
2370
2371         return RX_CONTINUE;
2372
2373  invalid:
2374         status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2375         /* will return in the next handlers */
2376         return RX_CONTINUE;
2377
2378  handled:
2379         if (rx->sta)
2380                 rx->sta->rx_packets++;
2381         dev_kfree_skb(rx->skb);
2382         return RX_QUEUED;
2383
2384  queue:
2385         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2386         skb_queue_tail(&sdata->skb_queue, rx->skb);
2387         ieee80211_queue_work(&local->hw, &sdata->work);
2388         if (rx->sta)
2389                 rx->sta->rx_packets++;
2390         return RX_QUEUED;
2391 }
2392
2393 static ieee80211_rx_result debug_noinline
2394 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2395 {
2396         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2397
2398         /* skip known-bad action frames and return them in the next handler */
2399         if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2400                 return RX_CONTINUE;
2401
2402         /*
2403          * Getting here means the kernel doesn't know how to handle
2404          * it, but maybe userspace does ... include returned frames
2405          * so userspace can register for those to know whether ones
2406          * it transmitted were processed or returned.
2407          */
2408
2409         if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
2410                              rx->skb->data, rx->skb->len,
2411                              GFP_ATOMIC)) {
2412                 if (rx->sta)
2413                         rx->sta->rx_packets++;
2414                 dev_kfree_skb(rx->skb);
2415                 return RX_QUEUED;
2416         }
2417
2418
2419         return RX_CONTINUE;
2420 }
2421
2422 static ieee80211_rx_result debug_noinline
2423 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2424 {
2425         struct ieee80211_local *local = rx->local;
2426         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2427         struct sk_buff *nskb;
2428         struct ieee80211_sub_if_data *sdata = rx->sdata;
2429         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2430
2431         if (!ieee80211_is_action(mgmt->frame_control))
2432                 return RX_CONTINUE;
2433
2434         /*
2435          * For AP mode, hostapd is responsible for handling any action
2436          * frames that we didn't handle, including returning unknown
2437          * ones. For all other modes we will return them to the sender,
2438          * setting the 0x80 bit in the action category, as required by
2439          * 802.11-2012 9.24.4.
2440          * Newer versions of hostapd shall also use the management frame
2441          * registration mechanisms, but older ones still use cooked
2442          * monitor interfaces so push all frames there.
2443          */
2444         if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2445             (sdata->vif.type == NL80211_IFTYPE_AP ||
2446              sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2447                 return RX_DROP_MONITOR;
2448
2449         if (is_multicast_ether_addr(mgmt->da))
2450                 return RX_DROP_MONITOR;
2451
2452         /* do not return rejected action frames */
2453         if (mgmt->u.action.category & 0x80)
2454                 return RX_DROP_UNUSABLE;
2455
2456         nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2457                                GFP_ATOMIC);
2458         if (nskb) {
2459                 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
2460
2461                 nmgmt->u.action.category |= 0x80;
2462                 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2463                 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
2464
2465                 memset(nskb->cb, 0, sizeof(nskb->cb));
2466
2467                 ieee80211_tx_skb(rx->sdata, nskb);
2468         }
2469         dev_kfree_skb(rx->skb);
2470         return RX_QUEUED;
2471 }
2472
2473 static ieee80211_rx_result debug_noinline
2474 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
2475 {
2476         struct ieee80211_sub_if_data *sdata = rx->sdata;
2477         ieee80211_rx_result rxs;
2478         struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2479         __le16 stype;
2480
2481         rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
2482         if (rxs != RX_CONTINUE)
2483                 return rxs;
2484
2485         stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
2486
2487         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2488             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2489             sdata->vif.type != NL80211_IFTYPE_STATION)
2490                 return RX_DROP_MONITOR;
2491
2492         switch (stype) {
2493         case cpu_to_le16(IEEE80211_STYPE_BEACON):
2494         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2495                 /* process for all: mesh, mlme, ibss */
2496                 break;
2497         case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2498         case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2499                 if (is_multicast_ether_addr(mgmt->da) &&
2500                     !is_broadcast_ether_addr(mgmt->da))
2501                         return RX_DROP_MONITOR;
2502
2503                 /* process only for station */
2504                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2505                         return RX_DROP_MONITOR;
2506                 break;
2507         case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2508         case cpu_to_le16(IEEE80211_STYPE_AUTH):
2509                 /* process only for ibss */
2510                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
2511                         return RX_DROP_MONITOR;
2512                 break;
2513         default:
2514                 return RX_DROP_MONITOR;
2515         }
2516
2517         /* queue up frame and kick off work to process it */
2518         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2519         skb_queue_tail(&sdata->skb_queue, rx->skb);
2520         ieee80211_queue_work(&rx->local->hw, &sdata->work);
2521         if (rx->sta)
2522                 rx->sta->rx_packets++;
2523
2524         return RX_QUEUED;
2525 }
2526
2527 /* TODO: use IEEE80211_RX_FRAGMENTED */
2528 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2529                                         struct ieee80211_rate *rate)
2530 {
2531         struct ieee80211_sub_if_data *sdata;
2532         struct ieee80211_local *local = rx->local;
2533         struct ieee80211_rtap_hdr {
2534                 struct ieee80211_radiotap_header hdr;
2535                 u8 flags;
2536                 u8 rate_or_pad;
2537                 __le16 chan_freq;
2538                 __le16 chan_flags;
2539         } __packed *rthdr;
2540         struct sk_buff *skb = rx->skb, *skb2;
2541         struct net_device *prev_dev = NULL;
2542         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2543
2544         /*
2545          * If cooked monitor has been processed already, then
2546          * don't do it again. If not, set the flag.
2547          */
2548         if (rx->flags & IEEE80211_RX_CMNTR)
2549                 goto out_free_skb;
2550         rx->flags |= IEEE80211_RX_CMNTR;
2551
2552         if (skb_headroom(skb) < sizeof(*rthdr) &&
2553             pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2554                 goto out_free_skb;
2555
2556         rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2557         memset(rthdr, 0, sizeof(*rthdr));
2558         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2559         rthdr->hdr.it_present =
2560                 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
2561                             (1 << IEEE80211_RADIOTAP_CHANNEL));
2562
2563         if (rate) {
2564                 rthdr->rate_or_pad = rate->bitrate / 5;
2565                 rthdr->hdr.it_present |=
2566                         cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2567         }
2568         rthdr->chan_freq = cpu_to_le16(status->freq);
2569
2570         if (status->band == IEEE80211_BAND_5GHZ)
2571                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2572                                                 IEEE80211_CHAN_5GHZ);
2573         else
2574                 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2575                                                 IEEE80211_CHAN_2GHZ);
2576
2577         skb_set_mac_header(skb, 0);
2578         skb->ip_summed = CHECKSUM_UNNECESSARY;
2579         skb->pkt_type = PACKET_OTHERHOST;
2580         skb->protocol = htons(ETH_P_802_2);
2581
2582         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2583                 if (!ieee80211_sdata_running(sdata))
2584                         continue;
2585
2586                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
2587                     !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2588                         continue;
2589
2590                 if (prev_dev) {
2591                         skb2 = skb_clone(skb, GFP_ATOMIC);
2592                         if (skb2) {
2593                                 skb2->dev = prev_dev;
2594                                 netif_receive_skb(skb2);
2595                         }
2596                 }
2597
2598                 prev_dev = sdata->dev;
2599                 sdata->dev->stats.rx_packets++;
2600                 sdata->dev->stats.rx_bytes += skb->len;
2601         }
2602
2603         if (prev_dev) {
2604                 skb->dev = prev_dev;
2605                 netif_receive_skb(skb);
2606                 return;
2607         }
2608
2609  out_free_skb:
2610         dev_kfree_skb(skb);
2611 }
2612
2613 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
2614                                          ieee80211_rx_result res)
2615 {
2616         switch (res) {
2617         case RX_DROP_MONITOR:
2618                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2619                 if (rx->sta)
2620                         rx->sta->rx_dropped++;
2621                 /* fall through */
2622         case RX_CONTINUE: {
2623                 struct ieee80211_rate *rate = NULL;
2624                 struct ieee80211_supported_band *sband;
2625                 struct ieee80211_rx_status *status;
2626
2627                 status = IEEE80211_SKB_RXCB((rx->skb));
2628
2629                 sband = rx->local->hw.wiphy->bands[status->band];
2630                 if (!(status->flag & RX_FLAG_HT))
2631                         rate = &sband->bitrates[status->rate_idx];
2632
2633                 ieee80211_rx_cooked_monitor(rx, rate);
2634                 break;
2635                 }
2636         case RX_DROP_UNUSABLE:
2637                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2638                 if (rx->sta)
2639                         rx->sta->rx_dropped++;
2640                 dev_kfree_skb(rx->skb);
2641                 break;
2642         case RX_QUEUED:
2643                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
2644                 break;
2645         }
2646 }
2647
2648 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
2649 {
2650         ieee80211_rx_result res = RX_DROP_MONITOR;
2651         struct sk_buff *skb;
2652
2653 #define CALL_RXH(rxh)                   \
2654         do {                            \
2655                 res = rxh(rx);          \
2656                 if (res != RX_CONTINUE) \
2657                         goto rxh_next;  \
2658         } while (0);
2659
2660         spin_lock(&rx->local->rx_skb_queue.lock);
2661         if (rx->local->running_rx_handler)
2662                 goto unlock;
2663
2664         rx->local->running_rx_handler = true;
2665
2666         while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
2667                 spin_unlock(&rx->local->rx_skb_queue.lock);
2668
2669                 /*
2670                  * all the other fields are valid across frames
2671                  * that belong to an aMPDU since they are on the
2672                  * same TID from the same station
2673                  */
2674                 rx->skb = skb;
2675
2676                 CALL_RXH(ieee80211_rx_h_decrypt)
2677                 CALL_RXH(ieee80211_rx_h_check_more_data)
2678                 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
2679                 CALL_RXH(ieee80211_rx_h_sta_process)
2680                 CALL_RXH(ieee80211_rx_h_defragment)
2681                 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2682                 /* must be after MMIC verify so header is counted in MPDU mic */
2683 #ifdef CONFIG_MAC80211_MESH
2684                 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2685                         CALL_RXH(ieee80211_rx_h_mesh_fwding);
2686 #endif
2687                 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2688                 CALL_RXH(ieee80211_rx_h_amsdu)
2689                 CALL_RXH(ieee80211_rx_h_data)
2690                 CALL_RXH(ieee80211_rx_h_ctrl);
2691                 CALL_RXH(ieee80211_rx_h_mgmt_check)
2692                 CALL_RXH(ieee80211_rx_h_action)
2693                 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2694                 CALL_RXH(ieee80211_rx_h_action_return)
2695                 CALL_RXH(ieee80211_rx_h_mgmt)
2696
2697  rxh_next:
2698                 ieee80211_rx_handlers_result(rx, res);
2699                 spin_lock(&rx->local->rx_skb_queue.lock);
2700 #undef CALL_RXH
2701         }
2702
2703         rx->local->running_rx_handler = false;
2704
2705  unlock:
2706         spin_unlock(&rx->local->rx_skb_queue.lock);
2707 }
2708
2709 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
2710 {
2711         ieee80211_rx_result res = RX_DROP_MONITOR;
2712
2713 #define CALL_RXH(rxh)                   \
2714         do {                            \
2715                 res = rxh(rx);          \
2716                 if (res != RX_CONTINUE) \
2717                         goto rxh_next;  \
2718         } while (0);
2719
2720         CALL_RXH(ieee80211_rx_h_passive_scan)
2721         CALL_RXH(ieee80211_rx_h_check)
2722
2723         ieee80211_rx_reorder_ampdu(rx);
2724
2725         ieee80211_rx_handlers(rx);
2726         return;
2727
2728  rxh_next:
2729         ieee80211_rx_handlers_result(rx, res);
2730
2731 #undef CALL_RXH
2732 }
2733
2734 /*
2735  * This function makes calls into the RX path, therefore
2736  * it has to be invoked under RCU read lock.
2737  */
2738 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
2739 {
2740         struct ieee80211_rx_data rx = {
2741                 .sta = sta,
2742                 .sdata = sta->sdata,
2743                 .local = sta->local,
2744                 /* This is OK -- must be QoS data frame */
2745                 .security_idx = tid,
2746                 .seqno_idx = tid,
2747                 .flags = 0,
2748         };
2749         struct tid_ampdu_rx *tid_agg_rx;
2750
2751         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
2752         if (!tid_agg_rx)
2753                 return;
2754
2755         spin_lock(&tid_agg_rx->reorder_lock);
2756         ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
2757         spin_unlock(&tid_agg_rx->reorder_lock);
2758
2759         ieee80211_rx_handlers(&rx);
2760 }
2761
2762 /* main receive path */
2763
2764 static int prepare_for_handlers(struct ieee80211_rx_data *rx,
2765                                 struct ieee80211_hdr *hdr)
2766 {
2767         struct ieee80211_sub_if_data *sdata = rx->sdata;
2768         struct sk_buff *skb = rx->skb;
2769         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2770         u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
2771         int multicast = is_multicast_ether_addr(hdr->addr1);
2772
2773         switch (sdata->vif.type) {
2774         case NL80211_IFTYPE_STATION:
2775                 if (!bssid && !sdata->u.mgd.use_4addr)
2776                         return 0;
2777                 if (!multicast &&
2778                     compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
2779                         if (!(sdata->dev->flags & IFF_PROMISC) ||
2780                             sdata->u.mgd.use_4addr)
2781                                 return 0;
2782                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2783                 }
2784                 break;
2785         case NL80211_IFTYPE_ADHOC:
2786                 if (!bssid)
2787                         return 0;
2788                 if (compare_ether_addr(sdata->vif.addr, hdr->addr2) == 0 ||
2789                     compare_ether_addr(sdata->u.ibss.bssid, hdr->addr2) == 0)
2790                         return 0;
2791                 if (ieee80211_is_beacon(hdr->frame_control)) {
2792                         return 1;
2793                 }
2794                 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2795                         if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
2796                                 return 0;
2797                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2798                 } else if (!multicast &&
2799                            compare_ether_addr(sdata->vif.addr,
2800                                               hdr->addr1) != 0) {
2801                         if (!(sdata->dev->flags & IFF_PROMISC))
2802                                 return 0;
2803                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2804                 } else if (!rx->sta) {
2805                         int rate_idx;
2806                         if (status->flag & RX_FLAG_HT)
2807                                 rate_idx = 0; /* TODO: HT rates */
2808                         else
2809                                 rate_idx = status->rate_idx;
2810                         rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
2811                                         hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
2812                 }
2813                 break;
2814         case NL80211_IFTYPE_MESH_POINT:
2815                 if (!multicast &&
2816                     compare_ether_addr(sdata->vif.addr,
2817                                        hdr->addr1) != 0) {
2818                         if (!(sdata->dev->flags & IFF_PROMISC))
2819                                 return 0;
2820
2821                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2822                 }
2823                 break;
2824         case NL80211_IFTYPE_AP_VLAN:
2825         case NL80211_IFTYPE_AP:
2826                 if (!bssid) {
2827                         if (compare_ether_addr(sdata->vif.addr,
2828                                                hdr->addr1))
2829                                 return 0;
2830                 } else if (!ieee80211_bssid_match(bssid,
2831                                         sdata->vif.addr)) {
2832                         if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
2833                             !ieee80211_is_beacon(hdr->frame_control) &&
2834                             !(ieee80211_is_action(hdr->frame_control) &&
2835                               sdata->vif.p2p))
2836                                 return 0;
2837                         status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2838                 }
2839                 break;
2840         case NL80211_IFTYPE_WDS:
2841                 if (bssid || !ieee80211_is_data(hdr->frame_control))
2842                         return 0;
2843                 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2844                         return 0;
2845                 break;
2846         default:
2847                 /* should never get here */
2848                 WARN_ON(1);
2849                 break;
2850         }
2851
2852         return 1;
2853 }
2854
2855 /*
2856  * This function returns whether or not the SKB
2857  * was destined for RX processing or not, which,
2858  * if consume is true, is equivalent to whether
2859  * or not the skb was consumed.
2860  */
2861 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
2862                                             struct sk_buff *skb, bool consume)
2863 {
2864         struct ieee80211_local *local = rx->local;
2865         struct ieee80211_sub_if_data *sdata = rx->sdata;
2866         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2867         struct ieee80211_hdr *hdr = (void *)skb->data;
2868         int prepares;
2869
2870         rx->skb = skb;
2871         status->rx_flags |= IEEE80211_RX_RA_MATCH;
2872         prepares = prepare_for_handlers(rx, hdr);
2873
2874         if (!prepares)
2875                 return false;
2876
2877         if (!consume) {
2878                 skb = skb_copy(skb, GFP_ATOMIC);
2879                 if (!skb) {
2880                         if (net_ratelimit())
2881                                 wiphy_debug(local->hw.wiphy,
2882                                         "failed to copy skb for %s\n",
2883                                         sdata->name);
2884                         return true;
2885                 }
2886
2887                 rx->skb = skb;
2888         }
2889
2890         ieee80211_invoke_rx_handlers(rx);
2891         return true;
2892 }
2893
2894 /*
2895  * This is the actual Rx frames handler. as it blongs to Rx path it must
2896  * be called with rcu_read_lock protection.
2897  */
2898 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2899                                          struct sk_buff *skb)
2900 {
2901         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2902         struct ieee80211_local *local = hw_to_local(hw);
2903         struct ieee80211_sub_if_data *sdata;
2904         struct ieee80211_hdr *hdr;
2905         __le16 fc;
2906         struct ieee80211_rx_data rx;
2907         struct ieee80211_sub_if_data *prev;
2908         struct sta_info *sta, *tmp, *prev_sta;
2909         int err = 0;
2910
2911         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
2912         memset(&rx, 0, sizeof(rx));
2913         rx.skb = skb;
2914         rx.local = local;
2915
2916         if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
2917                 local->dot11ReceivedFragmentCount++;
2918
2919         if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2920                      test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
2921                 status->rx_flags |= IEEE80211_RX_IN_SCAN;
2922
2923         if (ieee80211_is_mgmt(fc)) {
2924                 /* drop frame if too short for header */
2925                 if (skb->len < ieee80211_hdrlen(fc))
2926                         err = -ENOBUFS;
2927                 else
2928                         err = skb_linearize(skb);
2929         } else {
2930                 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
2931         }
2932
2933         if (err) {
2934                 dev_kfree_skb(skb);
2935                 return;
2936         }
2937
2938         hdr = (struct ieee80211_hdr *)skb->data;
2939         ieee80211_parse_qos(&rx);
2940         ieee80211_verify_alignment(&rx);
2941
2942         if (ieee80211_is_data(fc)) {
2943                 prev_sta = NULL;
2944
2945                 for_each_sta_info_rx(local, hdr->addr2, sta, tmp) {
2946                         if (!prev_sta) {
2947                                 prev_sta = sta;
2948                                 continue;
2949                         }
2950
2951                         rx.sta = prev_sta;
2952                         rx.sdata = prev_sta->sdata;
2953                         ieee80211_prepare_and_rx_handle(&rx, skb, false);
2954
2955                         prev_sta = sta;
2956                 }
2957
2958                 if (prev_sta) {
2959                         rx.sta = prev_sta;
2960                         rx.sdata = prev_sta->sdata;
2961
2962                         if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
2963                                 return;
2964                         goto out;
2965                 }
2966         }
2967
2968         prev = NULL;
2969
2970         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2971                 if (!ieee80211_sdata_running(sdata))
2972                         continue;
2973
2974                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2975                     sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2976                         continue;
2977
2978                 /*
2979                  * frame is destined for this interface, but if it's
2980                  * not also for the previous one we handle that after
2981                  * the loop to avoid copying the SKB once too much
2982                  */
2983
2984                 if (!prev) {
2985                         prev = sdata;
2986                         continue;
2987                 }
2988
2989                 rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
2990                 rx.sdata = prev;
2991                 ieee80211_prepare_and_rx_handle(&rx, skb, false);
2992
2993                 prev = sdata;
2994         }
2995
2996         if (prev) {
2997                 rx.sta = sta_info_get_bss_rx(prev, hdr->addr2);
2998                 rx.sdata = prev;
2999
3000                 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
3001                         return;
3002         }
3003
3004  out:
3005         dev_kfree_skb(skb);
3006 }
3007
3008 /*
3009  * This is the receive path handler. It is called by a low level driver when an
3010  * 802.11 MPDU is received from the hardware.
3011  */
3012 void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
3013 {
3014         struct ieee80211_local *local = hw_to_local(hw);
3015         struct ieee80211_rate *rate = NULL;
3016         struct ieee80211_supported_band *sband;
3017         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3018
3019         WARN_ON_ONCE(softirq_count() == 0);
3020
3021         if (WARN_ON(status->band < 0 ||
3022                     status->band >= IEEE80211_NUM_BANDS))
3023                 goto drop;
3024
3025         sband = local->hw.wiphy->bands[status->band];
3026         if (WARN_ON(!sband))
3027                 goto drop;
3028
3029         /*
3030          * If we're suspending, it is possible although not too likely
3031          * that we'd be receiving frames after having already partially
3032          * quiesced the stack. We can't process such frames then since
3033          * that might, for example, cause stations to be added or other
3034          * driver callbacks be invoked.
3035          */
3036         if (unlikely(local->quiescing || local->suspended))
3037                 goto drop;
3038
3039         /*
3040          * The same happens when we're not even started,
3041          * but that's worth a warning.
3042          */
3043         if (WARN_ON(!local->started))
3044                 goto drop;
3045
3046         if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
3047                 /*
3048                  * Validate the rate, unless a PLCP error means that
3049                  * we probably can't have a valid rate here anyway.
3050                  */
3051
3052                 if (status->flag & RX_FLAG_HT) {
3053                         /*
3054                          * rate_idx is MCS index, which can be [0-76]
3055                          * as documented on:
3056                          *
3057                          * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
3058                          *
3059                          * Anything else would be some sort of driver or
3060                          * hardware error. The driver should catch hardware
3061                          * errors.
3062                          */
3063                         if (WARN((status->rate_idx < 0 ||
3064                                  status->rate_idx > 76),
3065                                  "Rate marked as an HT rate but passed "
3066                                  "status->rate_idx is not "
3067                                  "an MCS index [0-76]: %d (0x%02x)\n",
3068                                  status->rate_idx,
3069                                  status->rate_idx))
3070                                 goto drop;
3071                 } else {
3072                         if (WARN_ON(status->rate_idx < 0 ||
3073                                     status->rate_idx >= sband->n_bitrates))
3074                                 goto drop;
3075                         rate = &sband->bitrates[status->rate_idx];
3076                 }
3077         }
3078
3079         status->rx_flags = 0;
3080
3081         /*
3082          * key references and virtual interfaces are protected using RCU
3083          * and this requires that we are in a read-side RCU section during
3084          * receive processing
3085          */
3086         rcu_read_lock();
3087
3088         /*
3089          * Frames with failed FCS/PLCP checksum are not returned,
3090          * all other frames are returned without radiotap header
3091          * if it was previously present.
3092          * Also, frames with less than 16 bytes are dropped.
3093          */
3094         skb = ieee80211_rx_monitor(local, skb, rate);
3095         if (!skb) {
3096                 rcu_read_unlock();
3097                 return;
3098         }
3099
3100         ieee80211_tpt_led_trig_rx(local,
3101                         ((struct ieee80211_hdr *)skb->data)->frame_control,
3102                         skb->len);
3103         __ieee80211_rx_handle_packet(hw, skb);
3104
3105         rcu_read_unlock();
3106
3107         return;
3108  drop:
3109         kfree_skb(skb);
3110 }
3111 EXPORT_SYMBOL(ieee80211_rx);
3112
3113 /* This is a version of the rx handler that can be called from hard irq
3114  * context. Post the skb on the queue and schedule the tasklet */
3115 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
3116 {
3117         struct ieee80211_local *local = hw_to_local(hw);
3118
3119         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
3120
3121         skb->pkt_type = IEEE80211_RX_MSG;
3122         skb_queue_tail(&local->skb_queue, skb);
3123         tasklet_schedule(&local->tasklet);
3124 }
3125 EXPORT_SYMBOL(ieee80211_rx_irqsafe);