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