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