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