6a265aa73a46769476fc96cf3e522fd3ae1071be
[cascardo/linux.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  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/jiffies.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/skbuff.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rcupdate.h>
21 #include <linux/export.h>
22 #include <linux/bitops.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <asm/unaligned.h>
26
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
29 #include "led.h"
30 #include "mesh.h"
31 #include "wep.h"
32 #include "wpa.h"
33 #include "tkip.h"
34 #include "wme.h"
35 #include "rate.h"
36
37 static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
38 {
39         struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
40
41         u64_stats_update_begin(&tstats->syncp);
42         tstats->rx_packets++;
43         tstats->rx_bytes += len;
44         u64_stats_update_end(&tstats->syncp);
45 }
46
47 static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
48                                enum nl80211_iftype type)
49 {
50         __le16 fc = hdr->frame_control;
51
52         if (ieee80211_is_data(fc)) {
53                 if (len < 24) /* drop incorrect hdr len (data) */
54                         return NULL;
55
56                 if (ieee80211_has_a4(fc))
57                         return NULL;
58                 if (ieee80211_has_tods(fc))
59                         return hdr->addr1;
60                 if (ieee80211_has_fromds(fc))
61                         return hdr->addr2;
62
63                 return hdr->addr3;
64         }
65
66         if (ieee80211_is_mgmt(fc)) {
67                 if (len < 24) /* drop incorrect hdr len (mgmt) */
68                         return NULL;
69                 return hdr->addr3;
70         }
71
72         if (ieee80211_is_ctl(fc)) {
73                 if (ieee80211_is_pspoll(fc))
74                         return hdr->addr1;
75
76                 if (ieee80211_is_back_req(fc)) {
77                         switch (type) {
78                         case NL80211_IFTYPE_STATION:
79                                 return hdr->addr2;
80                         case NL80211_IFTYPE_AP:
81                         case NL80211_IFTYPE_AP_VLAN:
82                                 return hdr->addr1;
83                         default:
84                                 break; /* fall through to the return */
85                         }
86                 }
87         }
88
89         return NULL;
90 }
91
92 /*
93  * monitor mode reception
94  *
95  * This function cleans up the SKB, i.e. it removes all the stuff
96  * only useful for monitoring.
97  */
98 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
99                                            struct sk_buff *skb,
100                                            unsigned int rtap_vendor_space)
101 {
102         if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
103                 if (likely(skb->len > FCS_LEN))
104                         __pskb_trim(skb, skb->len - FCS_LEN);
105                 else {
106                         /* driver bug */
107                         WARN_ON(1);
108                         dev_kfree_skb(skb);
109                         return NULL;
110                 }
111         }
112
113         __pskb_pull(skb, rtap_vendor_space);
114
115         return skb;
116 }
117
118 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
119                                      unsigned int rtap_vendor_space)
120 {
121         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
122         struct ieee80211_hdr *hdr;
123
124         hdr = (void *)(skb->data + rtap_vendor_space);
125
126         if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
127                             RX_FLAG_FAILED_PLCP_CRC |
128                             RX_FLAG_ONLY_MONITOR))
129                 return true;
130
131         if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
132                 return true;
133
134         if (ieee80211_is_ctl(hdr->frame_control) &&
135             !ieee80211_is_pspoll(hdr->frame_control) &&
136             !ieee80211_is_back_req(hdr->frame_control))
137                 return true;
138
139         return false;
140 }
141
142 static int
143 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
144                              struct ieee80211_rx_status *status,
145                              struct sk_buff *skb)
146 {
147         int len;
148
149         /* always present fields */
150         len = sizeof(struct ieee80211_radiotap_header) + 8;
151
152         /* allocate extra bitmaps */
153         if (status->chains)
154                 len += 4 * hweight8(status->chains);
155
156         if (ieee80211_have_rx_timestamp(status)) {
157                 len = ALIGN(len, 8);
158                 len += 8;
159         }
160         if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
161                 len += 1;
162
163         /* antenna field, if we don't have per-chain info */
164         if (!status->chains)
165                 len += 1;
166
167         /* padding for RX_FLAGS if necessary */
168         len = ALIGN(len, 2);
169
170         if (status->flag & RX_FLAG_HT) /* HT info */
171                 len += 3;
172
173         if (status->flag & RX_FLAG_AMPDU_DETAILS) {
174                 len = ALIGN(len, 4);
175                 len += 8;
176         }
177
178         if (status->flag & RX_FLAG_VHT) {
179                 len = ALIGN(len, 2);
180                 len += 12;
181         }
182
183         if (status->chains) {
184                 /* antenna and antenna signal fields */
185                 len += 2 * hweight8(status->chains);
186         }
187
188         if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
189                 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
190
191                 /* vendor presence bitmap */
192                 len += 4;
193                 /* alignment for fixed 6-byte vendor data header */
194                 len = ALIGN(len, 2);
195                 /* vendor data header */
196                 len += 6;
197                 if (WARN_ON(rtap->align == 0))
198                         rtap->align = 1;
199                 len = ALIGN(len, rtap->align);
200                 len += rtap->len + rtap->pad;
201         }
202
203         return len;
204 }
205
206 /*
207  * ieee80211_add_rx_radiotap_header - add radiotap header
208  *
209  * add a radiotap header containing all the fields which the hardware provided.
210  */
211 static void
212 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
213                                  struct sk_buff *skb,
214                                  struct ieee80211_rate *rate,
215                                  int rtap_len, bool has_fcs)
216 {
217         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
218         struct ieee80211_radiotap_header *rthdr;
219         unsigned char *pos;
220         __le32 *it_present;
221         u32 it_present_val;
222         u16 rx_flags = 0;
223         u16 channel_flags = 0;
224         int mpdulen, chain;
225         unsigned long chains = status->chains;
226         struct ieee80211_vendor_radiotap rtap = {};
227
228         if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
229                 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
230                 /* rtap.len and rtap.pad are undone immediately */
231                 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
232         }
233
234         mpdulen = skb->len;
235         if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
236                 mpdulen += FCS_LEN;
237
238         rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
239         memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
240         it_present = &rthdr->it_present;
241
242         /* radiotap header, set always present flags */
243         rthdr->it_len = cpu_to_le16(rtap_len);
244         it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
245                          BIT(IEEE80211_RADIOTAP_CHANNEL) |
246                          BIT(IEEE80211_RADIOTAP_RX_FLAGS);
247
248         if (!status->chains)
249                 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
250
251         for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
252                 it_present_val |=
253                         BIT(IEEE80211_RADIOTAP_EXT) |
254                         BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
255                 put_unaligned_le32(it_present_val, it_present);
256                 it_present++;
257                 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
258                                  BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
259         }
260
261         if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
262                 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
263                                   BIT(IEEE80211_RADIOTAP_EXT);
264                 put_unaligned_le32(it_present_val, it_present);
265                 it_present++;
266                 it_present_val = rtap.present;
267         }
268
269         put_unaligned_le32(it_present_val, it_present);
270
271         pos = (void *)(it_present + 1);
272
273         /* the order of the following fields is important */
274
275         /* IEEE80211_RADIOTAP_TSFT */
276         if (ieee80211_have_rx_timestamp(status)) {
277                 /* padding */
278                 while ((pos - (u8 *)rthdr) & 7)
279                         *pos++ = 0;
280                 put_unaligned_le64(
281                         ieee80211_calculate_rx_timestamp(local, status,
282                                                          mpdulen, 0),
283                         pos);
284                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
285                 pos += 8;
286         }
287
288         /* IEEE80211_RADIOTAP_FLAGS */
289         if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
290                 *pos |= IEEE80211_RADIOTAP_F_FCS;
291         if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
292                 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
293         if (status->flag & RX_FLAG_SHORTPRE)
294                 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
295         pos++;
296
297         /* IEEE80211_RADIOTAP_RATE */
298         if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
299                 /*
300                  * Without rate information don't add it. If we have,
301                  * MCS information is a separate field in radiotap,
302                  * added below. The byte here is needed as padding
303                  * for the channel though, so initialise it to 0.
304                  */
305                 *pos = 0;
306         } else {
307                 int shift = 0;
308                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
309                 if (status->flag & RX_FLAG_10MHZ)
310                         shift = 1;
311                 else if (status->flag & RX_FLAG_5MHZ)
312                         shift = 2;
313                 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
314         }
315         pos++;
316
317         /* IEEE80211_RADIOTAP_CHANNEL */
318         put_unaligned_le16(status->freq, pos);
319         pos += 2;
320         if (status->flag & RX_FLAG_10MHZ)
321                 channel_flags |= IEEE80211_CHAN_HALF;
322         else if (status->flag & RX_FLAG_5MHZ)
323                 channel_flags |= IEEE80211_CHAN_QUARTER;
324
325         if (status->band == NL80211_BAND_5GHZ)
326                 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
327         else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
328                 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
329         else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
330                 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
331         else if (rate)
332                 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
333         else
334                 channel_flags |= IEEE80211_CHAN_2GHZ;
335         put_unaligned_le16(channel_flags, pos);
336         pos += 2;
337
338         /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
339         if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
340             !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
341                 *pos = status->signal;
342                 rthdr->it_present |=
343                         cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
344                 pos++;
345         }
346
347         /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
348
349         if (!status->chains) {
350                 /* IEEE80211_RADIOTAP_ANTENNA */
351                 *pos = status->antenna;
352                 pos++;
353         }
354
355         /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
356
357         /* IEEE80211_RADIOTAP_RX_FLAGS */
358         /* ensure 2 byte alignment for the 2 byte field as required */
359         if ((pos - (u8 *)rthdr) & 1)
360                 *pos++ = 0;
361         if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
362                 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
363         put_unaligned_le16(rx_flags, pos);
364         pos += 2;
365
366         if (status->flag & RX_FLAG_HT) {
367                 unsigned int stbc;
368
369                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
370                 *pos++ = local->hw.radiotap_mcs_details;
371                 *pos = 0;
372                 if (status->flag & RX_FLAG_SHORT_GI)
373                         *pos |= IEEE80211_RADIOTAP_MCS_SGI;
374                 if (status->flag & RX_FLAG_40MHZ)
375                         *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
376                 if (status->flag & RX_FLAG_HT_GF)
377                         *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
378                 if (status->flag & RX_FLAG_LDPC)
379                         *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
380                 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
381                 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
382                 pos++;
383                 *pos++ = status->rate_idx;
384         }
385
386         if (status->flag & RX_FLAG_AMPDU_DETAILS) {
387                 u16 flags = 0;
388
389                 /* ensure 4 byte alignment */
390                 while ((pos - (u8 *)rthdr) & 3)
391                         pos++;
392                 rthdr->it_present |=
393                         cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
394                 put_unaligned_le32(status->ampdu_reference, pos);
395                 pos += 4;
396                 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
397                         flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
398                 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
399                         flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
400                 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
401                         flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
402                 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
403                         flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
404                 put_unaligned_le16(flags, pos);
405                 pos += 2;
406                 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
407                         *pos++ = status->ampdu_delimiter_crc;
408                 else
409                         *pos++ = 0;
410                 *pos++ = 0;
411         }
412
413         if (status->flag & RX_FLAG_VHT) {
414                 u16 known = local->hw.radiotap_vht_details;
415
416                 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
417                 put_unaligned_le16(known, pos);
418                 pos += 2;
419                 /* flags */
420                 if (status->flag & RX_FLAG_SHORT_GI)
421                         *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
422                 /* in VHT, STBC is binary */
423                 if (status->flag & RX_FLAG_STBC_MASK)
424                         *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
425                 if (status->vht_flag & RX_VHT_FLAG_BF)
426                         *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
427                 pos++;
428                 /* bandwidth */
429                 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
430                         *pos++ = 4;
431                 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
432                         *pos++ = 11;
433                 else if (status->flag & RX_FLAG_40MHZ)
434                         *pos++ = 1;
435                 else /* 20 MHz */
436                         *pos++ = 0;
437                 /* MCS/NSS */
438                 *pos = (status->rate_idx << 4) | status->vht_nss;
439                 pos += 4;
440                 /* coding field */
441                 if (status->flag & RX_FLAG_LDPC)
442                         *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
443                 pos++;
444                 /* group ID */
445                 pos++;
446                 /* partial_aid */
447                 pos += 2;
448         }
449
450         for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
451                 *pos++ = status->chain_signal[chain];
452                 *pos++ = chain;
453         }
454
455         if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
456                 /* ensure 2 byte alignment for the vendor field as required */
457                 if ((pos - (u8 *)rthdr) & 1)
458                         *pos++ = 0;
459                 *pos++ = rtap.oui[0];
460                 *pos++ = rtap.oui[1];
461                 *pos++ = rtap.oui[2];
462                 *pos++ = rtap.subns;
463                 put_unaligned_le16(rtap.len, pos);
464                 pos += 2;
465                 /* align the actual payload as requested */
466                 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
467                         *pos++ = 0;
468                 /* data (and possible padding) already follows */
469         }
470 }
471
472 /*
473  * This function copies a received frame to all monitor interfaces and
474  * returns a cleaned-up SKB that no longer includes the FCS nor the
475  * radiotap header the driver might have added.
476  */
477 static struct sk_buff *
478 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
479                      struct ieee80211_rate *rate)
480 {
481         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
482         struct ieee80211_sub_if_data *sdata;
483         int rt_hdrlen, needed_headroom;
484         struct sk_buff *skb, *skb2;
485         struct net_device *prev_dev = NULL;
486         int present_fcs_len = 0;
487         unsigned int rtap_vendor_space = 0;
488         struct ieee80211_mgmt *mgmt;
489         struct ieee80211_sub_if_data *monitor_sdata =
490                 rcu_dereference(local->monitor_sdata);
491
492         if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
493                 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
494
495                 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
496         }
497
498         /*
499          * First, we may need to make a copy of the skb because
500          *  (1) we need to modify it for radiotap (if not present), and
501          *  (2) the other RX handlers will modify the skb we got.
502          *
503          * We don't need to, of course, if we aren't going to return
504          * the SKB because it has a bad FCS/PLCP checksum.
505          */
506
507         if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
508                 present_fcs_len = FCS_LEN;
509
510         /* ensure hdr->frame_control and vendor radiotap data are in skb head */
511         if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
512                 dev_kfree_skb(origskb);
513                 return NULL;
514         }
515
516         if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
517                 if (should_drop_frame(origskb, present_fcs_len,
518                                       rtap_vendor_space)) {
519                         dev_kfree_skb(origskb);
520                         return NULL;
521                 }
522
523                 return remove_monitor_info(local, origskb, rtap_vendor_space);
524         }
525
526         /* room for the radiotap header based on driver features */
527         rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
528         needed_headroom = rt_hdrlen - rtap_vendor_space;
529
530         if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
531                 /* only need to expand headroom if necessary */
532                 skb = origskb;
533                 origskb = NULL;
534
535                 /*
536                  * This shouldn't trigger often because most devices have an
537                  * RX header they pull before we get here, and that should
538                  * be big enough for our radiotap information. We should
539                  * probably export the length to drivers so that we can have
540                  * them allocate enough headroom to start with.
541                  */
542                 if (skb_headroom(skb) < needed_headroom &&
543                     pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
544                         dev_kfree_skb(skb);
545                         return NULL;
546                 }
547         } else {
548                 /*
549                  * Need to make a copy and possibly remove radiotap header
550                  * and FCS from the original.
551                  */
552                 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
553
554                 origskb = remove_monitor_info(local, origskb,
555                                               rtap_vendor_space);
556
557                 if (!skb)
558                         return origskb;
559         }
560
561         /* prepend radiotap information */
562         ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
563
564         skb_reset_mac_header(skb);
565         skb->ip_summed = CHECKSUM_UNNECESSARY;
566         skb->pkt_type = PACKET_OTHERHOST;
567         skb->protocol = htons(ETH_P_802_2);
568
569         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
570                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
571                         continue;
572
573                 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
574                         continue;
575
576                 if (!ieee80211_sdata_running(sdata))
577                         continue;
578
579                 if (prev_dev) {
580                         skb2 = skb_clone(skb, GFP_ATOMIC);
581                         if (skb2) {
582                                 skb2->dev = prev_dev;
583                                 netif_receive_skb(skb2);
584                         }
585                 }
586
587                 prev_dev = sdata->dev;
588                 ieee80211_rx_stats(sdata->dev, skb->len);
589         }
590
591         mgmt = (void *)skb->data;
592         if (monitor_sdata &&
593             skb->len >= IEEE80211_MIN_ACTION_SIZE + 1 + VHT_MUMIMO_GROUPS_DATA_LEN &&
594             ieee80211_is_action(mgmt->frame_control) &&
595             mgmt->u.action.category == WLAN_CATEGORY_VHT &&
596             mgmt->u.action.u.vht_group_notif.action_code == WLAN_VHT_ACTION_GROUPID_MGMT &&
597             is_valid_ether_addr(monitor_sdata->u.mntr.mu_follow_addr) &&
598             ether_addr_equal(mgmt->da, monitor_sdata->u.mntr.mu_follow_addr)) {
599                 struct sk_buff *mu_skb = skb_copy(skb, GFP_ATOMIC);
600
601                 if (mu_skb) {
602                         mu_skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
603                         skb_queue_tail(&monitor_sdata->skb_queue, mu_skb);
604                         ieee80211_queue_work(&local->hw, &monitor_sdata->work);
605                 }
606         }
607
608         if (prev_dev) {
609                 skb->dev = prev_dev;
610                 netif_receive_skb(skb);
611         } else
612                 dev_kfree_skb(skb);
613
614         return origskb;
615 }
616
617 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
618 {
619         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
620         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
621         int tid, seqno_idx, security_idx;
622
623         /* does the frame have a qos control field? */
624         if (ieee80211_is_data_qos(hdr->frame_control)) {
625                 u8 *qc = ieee80211_get_qos_ctl(hdr);
626                 /* frame has qos control */
627                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
628                 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
629                         status->rx_flags |= IEEE80211_RX_AMSDU;
630
631                 seqno_idx = tid;
632                 security_idx = tid;
633         } else {
634                 /*
635                  * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
636                  *
637                  *      Sequence numbers for management frames, QoS data
638                  *      frames with a broadcast/multicast address in the
639                  *      Address 1 field, and all non-QoS data frames sent
640                  *      by QoS STAs are assigned using an additional single
641                  *      modulo-4096 counter, [...]
642                  *
643                  * We also use that counter for non-QoS STAs.
644                  */
645                 seqno_idx = IEEE80211_NUM_TIDS;
646                 security_idx = 0;
647                 if (ieee80211_is_mgmt(hdr->frame_control))
648                         security_idx = IEEE80211_NUM_TIDS;
649                 tid = 0;
650         }
651
652         rx->seqno_idx = seqno_idx;
653         rx->security_idx = security_idx;
654         /* Set skb->priority to 1d tag if highest order bit of TID is not set.
655          * For now, set skb->priority to 0 for other cases. */
656         rx->skb->priority = (tid > 7) ? 0 : tid;
657 }
658
659 /**
660  * DOC: Packet alignment
661  *
662  * Drivers always need to pass packets that are aligned to two-byte boundaries
663  * to the stack.
664  *
665  * Additionally, should, if possible, align the payload data in a way that
666  * guarantees that the contained IP header is aligned to a four-byte
667  * boundary. In the case of regular frames, this simply means aligning the
668  * payload to a four-byte boundary (because either the IP header is directly
669  * contained, or IV/RFC1042 headers that have a length divisible by four are
670  * in front of it).  If the payload data is not properly aligned and the
671  * architecture doesn't support efficient unaligned operations, mac80211
672  * will align the data.
673  *
674  * With A-MSDU frames, however, the payload data address must yield two modulo
675  * four because there are 14-byte 802.3 headers within the A-MSDU frames that
676  * push the IP header further back to a multiple of four again. Thankfully, the
677  * specs were sane enough this time around to require padding each A-MSDU
678  * subframe to a length that is a multiple of four.
679  *
680  * Padding like Atheros hardware adds which is between the 802.11 header and
681  * the payload is not supported, the driver is required to move the 802.11
682  * header to be directly in front of the payload in that case.
683  */
684 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
685 {
686 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
687         WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
688 #endif
689 }
690
691
692 /* rx handlers */
693
694 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
695 {
696         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
697
698         if (is_multicast_ether_addr(hdr->addr1))
699                 return 0;
700
701         return ieee80211_is_robust_mgmt_frame(skb);
702 }
703
704
705 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
706 {
707         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
708
709         if (!is_multicast_ether_addr(hdr->addr1))
710                 return 0;
711
712         return ieee80211_is_robust_mgmt_frame(skb);
713 }
714
715
716 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
717 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
718 {
719         struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
720         struct ieee80211_mmie *mmie;
721         struct ieee80211_mmie_16 *mmie16;
722
723         if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
724                 return -1;
725
726         if (!ieee80211_is_robust_mgmt_frame(skb))
727                 return -1; /* not a robust management frame */
728
729         mmie = (struct ieee80211_mmie *)
730                 (skb->data + skb->len - sizeof(*mmie));
731         if (mmie->element_id == WLAN_EID_MMIE &&
732             mmie->length == sizeof(*mmie) - 2)
733                 return le16_to_cpu(mmie->key_id);
734
735         mmie16 = (struct ieee80211_mmie_16 *)
736                 (skb->data + skb->len - sizeof(*mmie16));
737         if (skb->len >= 24 + sizeof(*mmie16) &&
738             mmie16->element_id == WLAN_EID_MMIE &&
739             mmie16->length == sizeof(*mmie16) - 2)
740                 return le16_to_cpu(mmie16->key_id);
741
742         return -1;
743 }
744
745 static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
746                                   struct sk_buff *skb)
747 {
748         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
749         __le16 fc;
750         int hdrlen;
751         u8 keyid;
752
753         fc = hdr->frame_control;
754         hdrlen = ieee80211_hdrlen(fc);
755
756         if (skb->len < hdrlen + cs->hdr_len)
757                 return -EINVAL;
758
759         skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
760         keyid &= cs->key_idx_mask;
761         keyid >>= cs->key_idx_shift;
762
763         return keyid;
764 }
765
766 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
767 {
768         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
769         char *dev_addr = rx->sdata->vif.addr;
770
771         if (ieee80211_is_data(hdr->frame_control)) {
772                 if (is_multicast_ether_addr(hdr->addr1)) {
773                         if (ieee80211_has_tods(hdr->frame_control) ||
774                             !ieee80211_has_fromds(hdr->frame_control))
775                                 return RX_DROP_MONITOR;
776                         if (ether_addr_equal(hdr->addr3, dev_addr))
777                                 return RX_DROP_MONITOR;
778                 } else {
779                         if (!ieee80211_has_a4(hdr->frame_control))
780                                 return RX_DROP_MONITOR;
781                         if (ether_addr_equal(hdr->addr4, dev_addr))
782                                 return RX_DROP_MONITOR;
783                 }
784         }
785
786         /* If there is not an established peer link and this is not a peer link
787          * establisment frame, beacon or probe, drop the frame.
788          */
789
790         if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
791                 struct ieee80211_mgmt *mgmt;
792
793                 if (!ieee80211_is_mgmt(hdr->frame_control))
794                         return RX_DROP_MONITOR;
795
796                 if (ieee80211_is_action(hdr->frame_control)) {
797                         u8 category;
798
799                         /* make sure category field is present */
800                         if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
801                                 return RX_DROP_MONITOR;
802
803                         mgmt = (struct ieee80211_mgmt *)hdr;
804                         category = mgmt->u.action.category;
805                         if (category != WLAN_CATEGORY_MESH_ACTION &&
806                             category != WLAN_CATEGORY_SELF_PROTECTED)
807                                 return RX_DROP_MONITOR;
808                         return RX_CONTINUE;
809                 }
810
811                 if (ieee80211_is_probe_req(hdr->frame_control) ||
812                     ieee80211_is_probe_resp(hdr->frame_control) ||
813                     ieee80211_is_beacon(hdr->frame_control) ||
814                     ieee80211_is_auth(hdr->frame_control))
815                         return RX_CONTINUE;
816
817                 return RX_DROP_MONITOR;
818         }
819
820         return RX_CONTINUE;
821 }
822
823 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
824                                               int index)
825 {
826         struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
827         struct sk_buff *tail = skb_peek_tail(frames);
828         struct ieee80211_rx_status *status;
829
830         if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
831                 return true;
832
833         if (!tail)
834                 return false;
835
836         status = IEEE80211_SKB_RXCB(tail);
837         if (status->flag & RX_FLAG_AMSDU_MORE)
838                 return false;
839
840         return true;
841 }
842
843 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
844                                             struct tid_ampdu_rx *tid_agg_rx,
845                                             int index,
846                                             struct sk_buff_head *frames)
847 {
848         struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
849         struct sk_buff *skb;
850         struct ieee80211_rx_status *status;
851
852         lockdep_assert_held(&tid_agg_rx->reorder_lock);
853
854         if (skb_queue_empty(skb_list))
855                 goto no_frame;
856
857         if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
858                 __skb_queue_purge(skb_list);
859                 goto no_frame;
860         }
861
862         /* release frames from the reorder ring buffer */
863         tid_agg_rx->stored_mpdu_num--;
864         while ((skb = __skb_dequeue(skb_list))) {
865                 status = IEEE80211_SKB_RXCB(skb);
866                 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
867                 __skb_queue_tail(frames, skb);
868         }
869
870 no_frame:
871         tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
872         tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
873 }
874
875 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
876                                              struct tid_ampdu_rx *tid_agg_rx,
877                                              u16 head_seq_num,
878                                              struct sk_buff_head *frames)
879 {
880         int index;
881
882         lockdep_assert_held(&tid_agg_rx->reorder_lock);
883
884         while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
885                 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
886                 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
887                                                 frames);
888         }
889 }
890
891 /*
892  * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
893  * the skb was added to the buffer longer than this time ago, the earlier
894  * frames that have not yet been received are assumed to be lost and the skb
895  * can be released for processing. This may also release other skb's from the
896  * reorder buffer if there are no additional gaps between the frames.
897  *
898  * Callers must hold tid_agg_rx->reorder_lock.
899  */
900 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
901
902 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
903                                           struct tid_ampdu_rx *tid_agg_rx,
904                                           struct sk_buff_head *frames)
905 {
906         int index, i, j;
907
908         lockdep_assert_held(&tid_agg_rx->reorder_lock);
909
910         /* release the buffer until next missing frame */
911         index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
912         if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
913             tid_agg_rx->stored_mpdu_num) {
914                 /*
915                  * No buffers ready to be released, but check whether any
916                  * frames in the reorder buffer have timed out.
917                  */
918                 int skipped = 1;
919                 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
920                      j = (j + 1) % tid_agg_rx->buf_size) {
921                         if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
922                                 skipped++;
923                                 continue;
924                         }
925                         if (skipped &&
926                             !time_after(jiffies, tid_agg_rx->reorder_time[j] +
927                                         HT_RX_REORDER_BUF_TIMEOUT))
928                                 goto set_release_timer;
929
930                         /* don't leave incomplete A-MSDUs around */
931                         for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
932                              i = (i + 1) % tid_agg_rx->buf_size)
933                                 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
934
935                         ht_dbg_ratelimited(sdata,
936                                            "release an RX reorder frame due to timeout on earlier frames\n");
937                         ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
938                                                         frames);
939
940                         /*
941                          * Increment the head seq# also for the skipped slots.
942                          */
943                         tid_agg_rx->head_seq_num =
944                                 (tid_agg_rx->head_seq_num +
945                                  skipped) & IEEE80211_SN_MASK;
946                         skipped = 0;
947                 }
948         } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
949                 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
950                                                 frames);
951                 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
952         }
953
954         if (tid_agg_rx->stored_mpdu_num) {
955                 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
956
957                 for (; j != (index - 1) % tid_agg_rx->buf_size;
958                      j = (j + 1) % tid_agg_rx->buf_size) {
959                         if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
960                                 break;
961                 }
962
963  set_release_timer:
964
965                 if (!tid_agg_rx->removed)
966                         mod_timer(&tid_agg_rx->reorder_timer,
967                                   tid_agg_rx->reorder_time[j] + 1 +
968                                   HT_RX_REORDER_BUF_TIMEOUT);
969         } else {
970                 del_timer(&tid_agg_rx->reorder_timer);
971         }
972 }
973
974 /*
975  * As this function belongs to the RX path it must be under
976  * rcu_read_lock protection. It returns false if the frame
977  * can be processed immediately, true if it was consumed.
978  */
979 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
980                                              struct tid_ampdu_rx *tid_agg_rx,
981                                              struct sk_buff *skb,
982                                              struct sk_buff_head *frames)
983 {
984         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
985         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
986         u16 sc = le16_to_cpu(hdr->seq_ctrl);
987         u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
988         u16 head_seq_num, buf_size;
989         int index;
990         bool ret = true;
991
992         spin_lock(&tid_agg_rx->reorder_lock);
993
994         /*
995          * Offloaded BA sessions have no known starting sequence number so pick
996          * one from first Rxed frame for this tid after BA was started.
997          */
998         if (unlikely(tid_agg_rx->auto_seq)) {
999                 tid_agg_rx->auto_seq = false;
1000                 tid_agg_rx->ssn = mpdu_seq_num;
1001                 tid_agg_rx->head_seq_num = mpdu_seq_num;
1002         }
1003
1004         buf_size = tid_agg_rx->buf_size;
1005         head_seq_num = tid_agg_rx->head_seq_num;
1006
1007         /* frame with out of date sequence number */
1008         if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1009                 dev_kfree_skb(skb);
1010                 goto out;
1011         }
1012
1013         /*
1014          * If frame the sequence number exceeds our buffering window
1015          * size release some previous frames to make room for this one.
1016          */
1017         if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1018                 head_seq_num = ieee80211_sn_inc(
1019                                 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1020                 /* release stored frames up to new head to stack */
1021                 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1022                                                  head_seq_num, frames);
1023         }
1024
1025         /* Now the new frame is always in the range of the reordering buffer */
1026
1027         index = mpdu_seq_num % tid_agg_rx->buf_size;
1028
1029         /* check if we already stored this frame */
1030         if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1031                 dev_kfree_skb(skb);
1032                 goto out;
1033         }
1034
1035         /*
1036          * If the current MPDU is in the right order and nothing else
1037          * is stored we can process it directly, no need to buffer it.
1038          * If it is first but there's something stored, we may be able
1039          * to release frames after this one.
1040          */
1041         if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1042             tid_agg_rx->stored_mpdu_num == 0) {
1043                 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1044                         tid_agg_rx->head_seq_num =
1045                                 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1046                 ret = false;
1047                 goto out;
1048         }
1049
1050         /* put the frame in the reordering buffer */
1051         __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1052         if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1053                 tid_agg_rx->reorder_time[index] = jiffies;
1054                 tid_agg_rx->stored_mpdu_num++;
1055                 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1056         }
1057
1058  out:
1059         spin_unlock(&tid_agg_rx->reorder_lock);
1060         return ret;
1061 }
1062
1063 /*
1064  * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1065  * true if the MPDU was buffered, false if it should be processed.
1066  */
1067 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1068                                        struct sk_buff_head *frames)
1069 {
1070         struct sk_buff *skb = rx->skb;
1071         struct ieee80211_local *local = rx->local;
1072         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1073         struct sta_info *sta = rx->sta;
1074         struct tid_ampdu_rx *tid_agg_rx;
1075         u16 sc;
1076         u8 tid, ack_policy;
1077
1078         if (!ieee80211_is_data_qos(hdr->frame_control) ||
1079             is_multicast_ether_addr(hdr->addr1))
1080                 goto dont_reorder;
1081
1082         /*
1083          * filter the QoS data rx stream according to
1084          * STA/TID and check if this STA/TID is on aggregation
1085          */
1086
1087         if (!sta)
1088                 goto dont_reorder;
1089
1090         ack_policy = *ieee80211_get_qos_ctl(hdr) &
1091                      IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1092         tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1093
1094         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1095         if (!tid_agg_rx)
1096                 goto dont_reorder;
1097
1098         /* qos null data frames are excluded */
1099         if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1100                 goto dont_reorder;
1101
1102         /* not part of a BA session */
1103         if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1104             ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1105                 goto dont_reorder;
1106
1107         /* new, potentially un-ordered, ampdu frame - process it */
1108
1109         /* reset session timer */
1110         if (tid_agg_rx->timeout)
1111                 tid_agg_rx->last_rx = jiffies;
1112
1113         /* if this mpdu is fragmented - terminate rx aggregation session */
1114         sc = le16_to_cpu(hdr->seq_ctrl);
1115         if (sc & IEEE80211_SCTL_FRAG) {
1116                 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
1117                 skb_queue_tail(&rx->sdata->skb_queue, skb);
1118                 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1119                 return;
1120         }
1121
1122         /*
1123          * No locking needed -- we will only ever process one
1124          * RX packet at a time, and thus own tid_agg_rx. All
1125          * other code manipulating it needs to (and does) make
1126          * sure that we cannot get to it any more before doing
1127          * anything with it.
1128          */
1129         if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1130                                              frames))
1131                 return;
1132
1133  dont_reorder:
1134         __skb_queue_tail(frames, skb);
1135 }
1136
1137 static ieee80211_rx_result debug_noinline
1138 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1139 {
1140         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1141         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1142
1143         if (status->flag & RX_FLAG_DUP_VALIDATED)
1144                 return RX_CONTINUE;
1145
1146         /*
1147          * Drop duplicate 802.11 retransmissions
1148          * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1149          */
1150
1151         if (rx->skb->len < 24)
1152                 return RX_CONTINUE;
1153
1154         if (ieee80211_is_ctl(hdr->frame_control) ||
1155             ieee80211_is_qos_nullfunc(hdr->frame_control) ||
1156             is_multicast_ether_addr(hdr->addr1))
1157                 return RX_CONTINUE;
1158
1159         if (!rx->sta)
1160                 return RX_CONTINUE;
1161
1162         if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1163                      rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1164                 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1165                 rx->sta->rx_stats.num_duplicates++;
1166                 return RX_DROP_UNUSABLE;
1167         } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1168                 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1169         }
1170
1171         return RX_CONTINUE;
1172 }
1173
1174 static ieee80211_rx_result debug_noinline
1175 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1176 {
1177         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1178
1179         /* Drop disallowed frame classes based on STA auth/assoc state;
1180          * IEEE 802.11, Chap 5.5.
1181          *
1182          * mac80211 filters only based on association state, i.e. it drops
1183          * Class 3 frames from not associated stations. hostapd sends
1184          * deauth/disassoc frames when needed. In addition, hostapd is
1185          * responsible for filtering on both auth and assoc states.
1186          */
1187
1188         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1189                 return ieee80211_rx_mesh_check(rx);
1190
1191         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1192                       ieee80211_is_pspoll(hdr->frame_control)) &&
1193                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1194                      rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1195                      rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1196                      (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1197                 /*
1198                  * accept port control frames from the AP even when it's not
1199                  * yet marked ASSOC to prevent a race where we don't set the
1200                  * assoc bit quickly enough before it sends the first frame
1201                  */
1202                 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1203                     ieee80211_is_data_present(hdr->frame_control)) {
1204                         unsigned int hdrlen;
1205                         __be16 ethertype;
1206
1207                         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1208
1209                         if (rx->skb->len < hdrlen + 8)
1210                                 return RX_DROP_MONITOR;
1211
1212                         skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1213                         if (ethertype == rx->sdata->control_port_protocol)
1214                                 return RX_CONTINUE;
1215                 }
1216
1217                 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1218                     cfg80211_rx_spurious_frame(rx->sdata->dev,
1219                                                hdr->addr2,
1220                                                GFP_ATOMIC))
1221                         return RX_DROP_UNUSABLE;
1222
1223                 return RX_DROP_MONITOR;
1224         }
1225
1226         return RX_CONTINUE;
1227 }
1228
1229
1230 static ieee80211_rx_result debug_noinline
1231 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1232 {
1233         struct ieee80211_local *local;
1234         struct ieee80211_hdr *hdr;
1235         struct sk_buff *skb;
1236
1237         local = rx->local;
1238         skb = rx->skb;
1239         hdr = (struct ieee80211_hdr *) skb->data;
1240
1241         if (!local->pspolling)
1242                 return RX_CONTINUE;
1243
1244         if (!ieee80211_has_fromds(hdr->frame_control))
1245                 /* this is not from AP */
1246                 return RX_CONTINUE;
1247
1248         if (!ieee80211_is_data(hdr->frame_control))
1249                 return RX_CONTINUE;
1250
1251         if (!ieee80211_has_moredata(hdr->frame_control)) {
1252                 /* AP has no more frames buffered for us */
1253                 local->pspolling = false;
1254                 return RX_CONTINUE;
1255         }
1256
1257         /* more data bit is set, let's request a new frame from the AP */
1258         ieee80211_send_pspoll(local, rx->sdata);
1259
1260         return RX_CONTINUE;
1261 }
1262
1263 static void sta_ps_start(struct sta_info *sta)
1264 {
1265         struct ieee80211_sub_if_data *sdata = sta->sdata;
1266         struct ieee80211_local *local = sdata->local;
1267         struct ps_data *ps;
1268         int tid;
1269
1270         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1271             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1272                 ps = &sdata->bss->ps;
1273         else
1274                 return;
1275
1276         atomic_inc(&ps->num_sta_ps);
1277         set_sta_flag(sta, WLAN_STA_PS_STA);
1278         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1279                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1280         ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1281                sta->sta.addr, sta->sta.aid);
1282
1283         ieee80211_clear_fast_xmit(sta);
1284
1285         if (!sta->sta.txq[0])
1286                 return;
1287
1288         for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1289                 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1290
1291                 if (txqi->tin.backlog_packets)
1292                         set_bit(tid, &sta->txq_buffered_tids);
1293                 else
1294                         clear_bit(tid, &sta->txq_buffered_tids);
1295         }
1296 }
1297
1298 static void sta_ps_end(struct sta_info *sta)
1299 {
1300         ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1301                sta->sta.addr, sta->sta.aid);
1302
1303         if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1304                 /*
1305                  * Clear the flag only if the other one is still set
1306                  * so that the TX path won't start TX'ing new frames
1307                  * directly ... In the case that the driver flag isn't
1308                  * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1309                  */
1310                 clear_sta_flag(sta, WLAN_STA_PS_STA);
1311                 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1312                        sta->sta.addr, sta->sta.aid);
1313                 return;
1314         }
1315
1316         set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1317         clear_sta_flag(sta, WLAN_STA_PS_STA);
1318         ieee80211_sta_ps_deliver_wakeup(sta);
1319 }
1320
1321 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1322 {
1323         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1324         bool in_ps;
1325
1326         WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1327
1328         /* Don't let the same PS state be set twice */
1329         in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1330         if ((start && in_ps) || (!start && !in_ps))
1331                 return -EINVAL;
1332
1333         if (start)
1334                 sta_ps_start(sta);
1335         else
1336                 sta_ps_end(sta);
1337
1338         return 0;
1339 }
1340 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1341
1342 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1343 {
1344         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1345
1346         if (test_sta_flag(sta, WLAN_STA_SP))
1347                 return;
1348
1349         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1350                 ieee80211_sta_ps_deliver_poll_response(sta);
1351         else
1352                 set_sta_flag(sta, WLAN_STA_PSPOLL);
1353 }
1354 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1355
1356 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1357 {
1358         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1359         u8 ac = ieee802_1d_to_ac[tid & 7];
1360
1361         /*
1362          * If this AC is not trigger-enabled do nothing.
1363          *
1364          * NB: This could/should check a separate bitmap of trigger-
1365          * enabled queues, but for now we only implement uAPSD w/o
1366          * TSPEC changes to the ACs, so they're always the same.
1367          */
1368         if (!(sta->sta.uapsd_queues & BIT(ac)))
1369                 return;
1370
1371         /* if we are in a service period, do nothing */
1372         if (test_sta_flag(sta, WLAN_STA_SP))
1373                 return;
1374
1375         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1376                 ieee80211_sta_ps_deliver_uapsd(sta);
1377         else
1378                 set_sta_flag(sta, WLAN_STA_UAPSD);
1379 }
1380 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1381
1382 static ieee80211_rx_result debug_noinline
1383 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1384 {
1385         struct ieee80211_sub_if_data *sdata = rx->sdata;
1386         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1387         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1388
1389         if (!rx->sta)
1390                 return RX_CONTINUE;
1391
1392         if (sdata->vif.type != NL80211_IFTYPE_AP &&
1393             sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1394                 return RX_CONTINUE;
1395
1396         /*
1397          * The device handles station powersave, so don't do anything about
1398          * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1399          * it to mac80211 since they're handled.)
1400          */
1401         if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1402                 return RX_CONTINUE;
1403
1404         /*
1405          * Don't do anything if the station isn't already asleep. In
1406          * the uAPSD case, the station will probably be marked asleep,
1407          * in the PS-Poll case the station must be confused ...
1408          */
1409         if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1410                 return RX_CONTINUE;
1411
1412         if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1413                 ieee80211_sta_pspoll(&rx->sta->sta);
1414
1415                 /* Free PS Poll skb here instead of returning RX_DROP that would
1416                  * count as an dropped frame. */
1417                 dev_kfree_skb(rx->skb);
1418
1419                 return RX_QUEUED;
1420         } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1421                    !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1422                    ieee80211_has_pm(hdr->frame_control) &&
1423                    (ieee80211_is_data_qos(hdr->frame_control) ||
1424                     ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1425                 u8 tid;
1426
1427                 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1428
1429                 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1430         }
1431
1432         return RX_CONTINUE;
1433 }
1434
1435 static ieee80211_rx_result debug_noinline
1436 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1437 {
1438         struct sta_info *sta = rx->sta;
1439         struct sk_buff *skb = rx->skb;
1440         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1441         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1442         int i;
1443
1444         if (!sta)
1445                 return RX_CONTINUE;
1446
1447         /*
1448          * Update last_rx only for IBSS packets which are for the current
1449          * BSSID and for station already AUTHORIZED to avoid keeping the
1450          * current IBSS network alive in cases where other STAs start
1451          * using different BSSID. This will also give the station another
1452          * chance to restart the authentication/authorization in case
1453          * something went wrong the first time.
1454          */
1455         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1456                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1457                                                 NL80211_IFTYPE_ADHOC);
1458                 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1459                     test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1460                         sta->rx_stats.last_rx = jiffies;
1461                         if (ieee80211_is_data(hdr->frame_control) &&
1462                             !is_multicast_ether_addr(hdr->addr1))
1463                                 sta->rx_stats.last_rate =
1464                                         sta_stats_encode_rate(status);
1465                 }
1466         } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1467                 sta->rx_stats.last_rx = jiffies;
1468         } else if (!is_multicast_ether_addr(hdr->addr1)) {
1469                 /*
1470                  * Mesh beacons will update last_rx when if they are found to
1471                  * match the current local configuration when processed.
1472                  */
1473                 sta->rx_stats.last_rx = jiffies;
1474                 if (ieee80211_is_data(hdr->frame_control))
1475                         sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1476         }
1477
1478         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1479                 ieee80211_sta_rx_notify(rx->sdata, hdr);
1480
1481         sta->rx_stats.fragments++;
1482
1483         u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1484         sta->rx_stats.bytes += rx->skb->len;
1485         u64_stats_update_end(&rx->sta->rx_stats.syncp);
1486
1487         if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1488                 sta->rx_stats.last_signal = status->signal;
1489                 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1490         }
1491
1492         if (status->chains) {
1493                 sta->rx_stats.chains = status->chains;
1494                 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1495                         int signal = status->chain_signal[i];
1496
1497                         if (!(status->chains & BIT(i)))
1498                                 continue;
1499
1500                         sta->rx_stats.chain_signal_last[i] = signal;
1501                         ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1502                                         -signal);
1503                 }
1504         }
1505
1506         /*
1507          * Change STA power saving mode only at the end of a frame
1508          * exchange sequence.
1509          */
1510         if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1511             !ieee80211_has_morefrags(hdr->frame_control) &&
1512             !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1513             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1514              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1515             /* PM bit is only checked in frames where it isn't reserved,
1516              * in AP mode it's reserved in non-bufferable management frames
1517              * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1518              */
1519             (!ieee80211_is_mgmt(hdr->frame_control) ||
1520              ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
1521                 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1522                         if (!ieee80211_has_pm(hdr->frame_control))
1523                                 sta_ps_end(sta);
1524                 } else {
1525                         if (ieee80211_has_pm(hdr->frame_control))
1526                                 sta_ps_start(sta);
1527                 }
1528         }
1529
1530         /* mesh power save support */
1531         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1532                 ieee80211_mps_rx_h_sta_process(sta, hdr);
1533
1534         /*
1535          * Drop (qos-)data::nullfunc frames silently, since they
1536          * are used only to control station power saving mode.
1537          */
1538         if (ieee80211_is_nullfunc(hdr->frame_control) ||
1539             ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1540                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1541
1542                 /*
1543                  * If we receive a 4-addr nullfunc frame from a STA
1544                  * that was not moved to a 4-addr STA vlan yet send
1545                  * the event to userspace and for older hostapd drop
1546                  * the frame to the monitor interface.
1547                  */
1548                 if (ieee80211_has_a4(hdr->frame_control) &&
1549                     (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1550                      (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1551                       !rx->sdata->u.vlan.sta))) {
1552                         if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1553                                 cfg80211_rx_unexpected_4addr_frame(
1554                                         rx->sdata->dev, sta->sta.addr,
1555                                         GFP_ATOMIC);
1556                         return RX_DROP_MONITOR;
1557                 }
1558                 /*
1559                  * Update counter and free packet here to avoid
1560                  * counting this as a dropped packed.
1561                  */
1562                 sta->rx_stats.packets++;
1563                 dev_kfree_skb(rx->skb);
1564                 return RX_QUEUED;
1565         }
1566
1567         return RX_CONTINUE;
1568 } /* ieee80211_rx_h_sta_process */
1569
1570 static ieee80211_rx_result debug_noinline
1571 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1572 {
1573         struct sk_buff *skb = rx->skb;
1574         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1575         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1576         int keyidx;
1577         int hdrlen;
1578         ieee80211_rx_result result = RX_DROP_UNUSABLE;
1579         struct ieee80211_key *sta_ptk = NULL;
1580         int mmie_keyidx = -1;
1581         __le16 fc;
1582         const struct ieee80211_cipher_scheme *cs = NULL;
1583
1584         /*
1585          * Key selection 101
1586          *
1587          * There are four types of keys:
1588          *  - GTK (group keys)
1589          *  - IGTK (group keys for management frames)
1590          *  - PTK (pairwise keys)
1591          *  - STK (station-to-station pairwise keys)
1592          *
1593          * When selecting a key, we have to distinguish between multicast
1594          * (including broadcast) and unicast frames, the latter can only
1595          * use PTKs and STKs while the former always use GTKs and IGTKs.
1596          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1597          * unicast frames can also use key indices like GTKs. Hence, if we
1598          * don't have a PTK/STK we check the key index for a WEP key.
1599          *
1600          * Note that in a regular BSS, multicast frames are sent by the
1601          * AP only, associated stations unicast the frame to the AP first
1602          * which then multicasts it on their behalf.
1603          *
1604          * There is also a slight problem in IBSS mode: GTKs are negotiated
1605          * with each station, that is something we don't currently handle.
1606          * The spec seems to expect that one negotiates the same key with
1607          * every station but there's no such requirement; VLANs could be
1608          * possible.
1609          */
1610
1611         /* start without a key */
1612         rx->key = NULL;
1613         fc = hdr->frame_control;
1614
1615         if (rx->sta) {
1616                 int keyid = rx->sta->ptk_idx;
1617
1618                 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1619                         cs = rx->sta->cipher_scheme;
1620                         keyid = ieee80211_get_cs_keyid(cs, rx->skb);
1621                         if (unlikely(keyid < 0))
1622                                 return RX_DROP_UNUSABLE;
1623                 }
1624                 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1625         }
1626
1627         if (!ieee80211_has_protected(fc))
1628                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1629
1630         if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1631                 rx->key = sta_ptk;
1632                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1633                     (status->flag & RX_FLAG_IV_STRIPPED))
1634                         return RX_CONTINUE;
1635                 /* Skip decryption if the frame is not protected. */
1636                 if (!ieee80211_has_protected(fc))
1637                         return RX_CONTINUE;
1638         } else if (mmie_keyidx >= 0) {
1639                 /* Broadcast/multicast robust management frame / BIP */
1640                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1641                     (status->flag & RX_FLAG_IV_STRIPPED))
1642                         return RX_CONTINUE;
1643
1644                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1645                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1646                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1647                 if (rx->sta) {
1648                         if (ieee80211_is_group_privacy_action(skb) &&
1649                             test_sta_flag(rx->sta, WLAN_STA_MFP))
1650                                 return RX_DROP_MONITOR;
1651
1652                         rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1653                 }
1654                 if (!rx->key)
1655                         rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1656         } else if (!ieee80211_has_protected(fc)) {
1657                 /*
1658                  * The frame was not protected, so skip decryption. However, we
1659                  * need to set rx->key if there is a key that could have been
1660                  * used so that the frame may be dropped if encryption would
1661                  * have been expected.
1662                  */
1663                 struct ieee80211_key *key = NULL;
1664                 struct ieee80211_sub_if_data *sdata = rx->sdata;
1665                 int i;
1666
1667                 if (ieee80211_is_mgmt(fc) &&
1668                     is_multicast_ether_addr(hdr->addr1) &&
1669                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1670                         rx->key = key;
1671                 else {
1672                         if (rx->sta) {
1673                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1674                                         key = rcu_dereference(rx->sta->gtk[i]);
1675                                         if (key)
1676                                                 break;
1677                                 }
1678                         }
1679                         if (!key) {
1680                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1681                                         key = rcu_dereference(sdata->keys[i]);
1682                                         if (key)
1683                                                 break;
1684                                 }
1685                         }
1686                         if (key)
1687                                 rx->key = key;
1688                 }
1689                 return RX_CONTINUE;
1690         } else {
1691                 u8 keyid;
1692
1693                 /*
1694                  * The device doesn't give us the IV so we won't be
1695                  * able to look up the key. That's ok though, we
1696                  * don't need to decrypt the frame, we just won't
1697                  * be able to keep statistics accurate.
1698                  * Except for key threshold notifications, should
1699                  * we somehow allow the driver to tell us which key
1700                  * the hardware used if this flag is set?
1701                  */
1702                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1703                     (status->flag & RX_FLAG_IV_STRIPPED))
1704                         return RX_CONTINUE;
1705
1706                 hdrlen = ieee80211_hdrlen(fc);
1707
1708                 if (cs) {
1709                         keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
1710
1711                         if (unlikely(keyidx < 0))
1712                                 return RX_DROP_UNUSABLE;
1713                 } else {
1714                         if (rx->skb->len < 8 + hdrlen)
1715                                 return RX_DROP_UNUSABLE; /* TODO: count this? */
1716                         /*
1717                          * no need to call ieee80211_wep_get_keyidx,
1718                          * it verifies a bunch of things we've done already
1719                          */
1720                         skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1721                         keyidx = keyid >> 6;
1722                 }
1723
1724                 /* check per-station GTK first, if multicast packet */
1725                 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1726                         rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1727
1728                 /* if not found, try default key */
1729                 if (!rx->key) {
1730                         rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1731
1732                         /*
1733                          * RSNA-protected unicast frames should always be
1734                          * sent with pairwise or station-to-station keys,
1735                          * but for WEP we allow using a key index as well.
1736                          */
1737                         if (rx->key &&
1738                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1739                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1740                             !is_multicast_ether_addr(hdr->addr1))
1741                                 rx->key = NULL;
1742                 }
1743         }
1744
1745         if (rx->key) {
1746                 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1747                         return RX_DROP_MONITOR;
1748
1749                 /* TODO: add threshold stuff again */
1750         } else {
1751                 return RX_DROP_MONITOR;
1752         }
1753
1754         switch (rx->key->conf.cipher) {
1755         case WLAN_CIPHER_SUITE_WEP40:
1756         case WLAN_CIPHER_SUITE_WEP104:
1757                 result = ieee80211_crypto_wep_decrypt(rx);
1758                 break;
1759         case WLAN_CIPHER_SUITE_TKIP:
1760                 result = ieee80211_crypto_tkip_decrypt(rx);
1761                 break;
1762         case WLAN_CIPHER_SUITE_CCMP:
1763                 result = ieee80211_crypto_ccmp_decrypt(
1764                         rx, IEEE80211_CCMP_MIC_LEN);
1765                 break;
1766         case WLAN_CIPHER_SUITE_CCMP_256:
1767                 result = ieee80211_crypto_ccmp_decrypt(
1768                         rx, IEEE80211_CCMP_256_MIC_LEN);
1769                 break;
1770         case WLAN_CIPHER_SUITE_AES_CMAC:
1771                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1772                 break;
1773         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1774                 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1775                 break;
1776         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1777         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1778                 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1779                 break;
1780         case WLAN_CIPHER_SUITE_GCMP:
1781         case WLAN_CIPHER_SUITE_GCMP_256:
1782                 result = ieee80211_crypto_gcmp_decrypt(rx);
1783                 break;
1784         default:
1785                 result = ieee80211_crypto_hw_decrypt(rx);
1786         }
1787
1788         /* the hdr variable is invalid after the decrypt handlers */
1789
1790         /* either the frame has been decrypted or will be dropped */
1791         status->flag |= RX_FLAG_DECRYPTED;
1792
1793         return result;
1794 }
1795
1796 static inline struct ieee80211_fragment_entry *
1797 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1798                          unsigned int frag, unsigned int seq, int rx_queue,
1799                          struct sk_buff **skb)
1800 {
1801         struct ieee80211_fragment_entry *entry;
1802
1803         entry = &sdata->fragments[sdata->fragment_next++];
1804         if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1805                 sdata->fragment_next = 0;
1806
1807         if (!skb_queue_empty(&entry->skb_list))
1808                 __skb_queue_purge(&entry->skb_list);
1809
1810         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1811         *skb = NULL;
1812         entry->first_frag_time = jiffies;
1813         entry->seq = seq;
1814         entry->rx_queue = rx_queue;
1815         entry->last_frag = frag;
1816         entry->check_sequential_pn = false;
1817         entry->extra_len = 0;
1818
1819         return entry;
1820 }
1821
1822 static inline struct ieee80211_fragment_entry *
1823 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1824                           unsigned int frag, unsigned int seq,
1825                           int rx_queue, struct ieee80211_hdr *hdr)
1826 {
1827         struct ieee80211_fragment_entry *entry;
1828         int i, idx;
1829
1830         idx = sdata->fragment_next;
1831         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1832                 struct ieee80211_hdr *f_hdr;
1833
1834                 idx--;
1835                 if (idx < 0)
1836                         idx = IEEE80211_FRAGMENT_MAX - 1;
1837
1838                 entry = &sdata->fragments[idx];
1839                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1840                     entry->rx_queue != rx_queue ||
1841                     entry->last_frag + 1 != frag)
1842                         continue;
1843
1844                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1845
1846                 /*
1847                  * Check ftype and addresses are equal, else check next fragment
1848                  */
1849                 if (((hdr->frame_control ^ f_hdr->frame_control) &
1850                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1851                     !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1852                     !ether_addr_equal(hdr->addr2, f_hdr->addr2))
1853                         continue;
1854
1855                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1856                         __skb_queue_purge(&entry->skb_list);
1857                         continue;
1858                 }
1859                 return entry;
1860         }
1861
1862         return NULL;
1863 }
1864
1865 static ieee80211_rx_result debug_noinline
1866 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1867 {
1868         struct ieee80211_hdr *hdr;
1869         u16 sc;
1870         __le16 fc;
1871         unsigned int frag, seq;
1872         struct ieee80211_fragment_entry *entry;
1873         struct sk_buff *skb;
1874         struct ieee80211_rx_status *status;
1875
1876         hdr = (struct ieee80211_hdr *)rx->skb->data;
1877         fc = hdr->frame_control;
1878
1879         if (ieee80211_is_ctl(fc))
1880                 return RX_CONTINUE;
1881
1882         sc = le16_to_cpu(hdr->seq_ctrl);
1883         frag = sc & IEEE80211_SCTL_FRAG;
1884
1885         if (is_multicast_ether_addr(hdr->addr1)) {
1886                 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
1887                 goto out_no_led;
1888         }
1889
1890         if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1891                 goto out;
1892
1893         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1894
1895         if (skb_linearize(rx->skb))
1896                 return RX_DROP_UNUSABLE;
1897
1898         /*
1899          *  skb_linearize() might change the skb->data and
1900          *  previously cached variables (in this case, hdr) need to
1901          *  be refreshed with the new data.
1902          */
1903         hdr = (struct ieee80211_hdr *)rx->skb->data;
1904         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1905
1906         if (frag == 0) {
1907                 /* This is the first fragment of a new frame. */
1908                 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1909                                                  rx->seqno_idx, &(rx->skb));
1910                 if (rx->key &&
1911                     (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
1912                      rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
1913                      rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
1914                      rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
1915                     ieee80211_has_protected(fc)) {
1916                         int queue = rx->security_idx;
1917
1918                         /* Store CCMP/GCMP PN so that we can verify that the
1919                          * next fragment has a sequential PN value.
1920                          */
1921                         entry->check_sequential_pn = true;
1922                         memcpy(entry->last_pn,
1923                                rx->key->u.ccmp.rx_pn[queue],
1924                                IEEE80211_CCMP_PN_LEN);
1925                         BUILD_BUG_ON(offsetof(struct ieee80211_key,
1926                                               u.ccmp.rx_pn) !=
1927                                      offsetof(struct ieee80211_key,
1928                                               u.gcmp.rx_pn));
1929                         BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
1930                                      sizeof(rx->key->u.gcmp.rx_pn[queue]));
1931                         BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
1932                                      IEEE80211_GCMP_PN_LEN);
1933                 }
1934                 return RX_QUEUED;
1935         }
1936
1937         /* This is a fragment for a frame that should already be pending in
1938          * fragment cache. Add this fragment to the end of the pending entry.
1939          */
1940         entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1941                                           rx->seqno_idx, hdr);
1942         if (!entry) {
1943                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1944                 return RX_DROP_MONITOR;
1945         }
1946
1947         /* "The receiver shall discard MSDUs and MMPDUs whose constituent
1948          *  MPDU PN values are not incrementing in steps of 1."
1949          * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
1950          * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
1951          */
1952         if (entry->check_sequential_pn) {
1953                 int i;
1954                 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
1955                 int queue;
1956
1957                 if (!rx->key ||
1958                     (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
1959                      rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
1960                      rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
1961                      rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
1962                         return RX_DROP_UNUSABLE;
1963                 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1964                 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
1965                         pn[i]++;
1966                         if (pn[i])
1967                                 break;
1968                 }
1969                 queue = rx->security_idx;
1970                 rpn = rx->key->u.ccmp.rx_pn[queue];
1971                 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
1972                         return RX_DROP_UNUSABLE;
1973                 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
1974         }
1975
1976         skb_pull(rx->skb, ieee80211_hdrlen(fc));
1977         __skb_queue_tail(&entry->skb_list, rx->skb);
1978         entry->last_frag = frag;
1979         entry->extra_len += rx->skb->len;
1980         if (ieee80211_has_morefrags(fc)) {
1981                 rx->skb = NULL;
1982                 return RX_QUEUED;
1983         }
1984
1985         rx->skb = __skb_dequeue(&entry->skb_list);
1986         if (skb_tailroom(rx->skb) < entry->extra_len) {
1987                 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
1988                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1989                                               GFP_ATOMIC))) {
1990                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1991                         __skb_queue_purge(&entry->skb_list);
1992                         return RX_DROP_UNUSABLE;
1993                 }
1994         }
1995         while ((skb = __skb_dequeue(&entry->skb_list))) {
1996                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1997                 dev_kfree_skb(skb);
1998         }
1999
2000         /* Complete frame has been reassembled - process it now */
2001         status = IEEE80211_SKB_RXCB(rx->skb);
2002
2003  out:
2004         ieee80211_led_rx(rx->local);
2005  out_no_led:
2006         if (rx->sta)
2007                 rx->sta->rx_stats.packets++;
2008         return RX_CONTINUE;
2009 }
2010
2011 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2012 {
2013         if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2014                 return -EACCES;
2015
2016         return 0;
2017 }
2018
2019 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2020 {
2021         struct sk_buff *skb = rx->skb;
2022         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2023
2024         /*
2025          * Pass through unencrypted frames if the hardware has
2026          * decrypted them already.
2027          */
2028         if (status->flag & RX_FLAG_DECRYPTED)
2029                 return 0;
2030
2031         /* Drop unencrypted frames if key is set. */
2032         if (unlikely(!ieee80211_has_protected(fc) &&
2033                      !ieee80211_is_nullfunc(fc) &&
2034                      ieee80211_is_data(fc) && rx->key))
2035                 return -EACCES;
2036
2037         return 0;
2038 }
2039
2040 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2041 {
2042         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2043         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2044         __le16 fc = hdr->frame_control;
2045
2046         /*
2047          * Pass through unencrypted frames if the hardware has
2048          * decrypted them already.
2049          */
2050         if (status->flag & RX_FLAG_DECRYPTED)
2051                 return 0;
2052
2053         if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2054                 if (unlikely(!ieee80211_has_protected(fc) &&
2055                              ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2056                              rx->key)) {
2057                         if (ieee80211_is_deauth(fc) ||
2058                             ieee80211_is_disassoc(fc))
2059                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2060                                                              rx->skb->data,
2061                                                              rx->skb->len);
2062                         return -EACCES;
2063                 }
2064                 /* BIP does not use Protected field, so need to check MMIE */
2065                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2066                              ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2067                         if (ieee80211_is_deauth(fc) ||
2068                             ieee80211_is_disassoc(fc))
2069                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2070                                                              rx->skb->data,
2071                                                              rx->skb->len);
2072                         return -EACCES;
2073                 }
2074                 /*
2075                  * When using MFP, Action frames are not allowed prior to
2076                  * having configured keys.
2077                  */
2078                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2079                              ieee80211_is_robust_mgmt_frame(rx->skb)))
2080                         return -EACCES;
2081         }
2082
2083         return 0;
2084 }
2085
2086 static int
2087 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2088 {
2089         struct ieee80211_sub_if_data *sdata = rx->sdata;
2090         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2091         bool check_port_control = false;
2092         struct ethhdr *ehdr;
2093         int ret;
2094
2095         *port_control = false;
2096         if (ieee80211_has_a4(hdr->frame_control) &&
2097             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2098                 return -1;
2099
2100         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2101             !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2102
2103                 if (!sdata->u.mgd.use_4addr)
2104                         return -1;
2105                 else
2106                         check_port_control = true;
2107         }
2108
2109         if (is_multicast_ether_addr(hdr->addr1) &&
2110             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2111                 return -1;
2112
2113         ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2114         if (ret < 0)
2115                 return ret;
2116
2117         ehdr = (struct ethhdr *) rx->skb->data;
2118         if (ehdr->h_proto == rx->sdata->control_port_protocol)
2119                 *port_control = true;
2120         else if (check_port_control)
2121                 return -1;
2122
2123         return 0;
2124 }
2125
2126 /*
2127  * requires that rx->skb is a frame with ethernet header
2128  */
2129 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2130 {
2131         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2132                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2133         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2134
2135         /*
2136          * Allow EAPOL frames to us/the PAE group address regardless
2137          * of whether the frame was encrypted or not.
2138          */
2139         if (ehdr->h_proto == rx->sdata->control_port_protocol &&
2140             (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2141              ether_addr_equal(ehdr->h_dest, pae_group_addr)))
2142                 return true;
2143
2144         if (ieee80211_802_1x_port_control(rx) ||
2145             ieee80211_drop_unencrypted(rx, fc))
2146                 return false;
2147
2148         return true;
2149 }
2150
2151 /*
2152  * requires that rx->skb is a frame with ethernet header
2153  */
2154 static void
2155 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2156 {
2157         struct ieee80211_sub_if_data *sdata = rx->sdata;
2158         struct net_device *dev = sdata->dev;
2159         struct sk_buff *skb, *xmit_skb;
2160         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2161         struct sta_info *dsta;
2162
2163         skb = rx->skb;
2164         xmit_skb = NULL;
2165
2166         ieee80211_rx_stats(dev, skb->len);
2167
2168         if (rx->sta) {
2169                 /* The seqno index has the same property as needed
2170                  * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2171                  * for non-QoS-data frames. Here we know it's a data
2172                  * frame, so count MSDUs.
2173                  */
2174                 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2175                 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2176                 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2177         }
2178
2179         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2180              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2181             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2182             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2183                 if (is_multicast_ether_addr(ehdr->h_dest)) {
2184                         /*
2185                          * send multicast frames both to higher layers in
2186                          * local net stack and back to the wireless medium
2187                          */
2188                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
2189                         if (!xmit_skb)
2190                                 net_info_ratelimited("%s: failed to clone multicast frame\n",
2191                                                     dev->name);
2192                 } else {
2193                         dsta = sta_info_get(sdata, skb->data);
2194                         if (dsta) {
2195                                 /*
2196                                  * The destination station is associated to
2197                                  * this AP (in this VLAN), so send the frame
2198                                  * directly to it and do not pass it to local
2199                                  * net stack.
2200                                  */
2201                                 xmit_skb = skb;
2202                                 skb = NULL;
2203                         }
2204                 }
2205         }
2206
2207 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2208         if (skb) {
2209                 /* 'align' will only take the values 0 or 2 here since all
2210                  * frames are required to be aligned to 2-byte boundaries
2211                  * when being passed to mac80211; the code here works just
2212                  * as well if that isn't true, but mac80211 assumes it can
2213                  * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2214                  */
2215                 int align;
2216
2217                 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2218                 if (align) {
2219                         if (WARN_ON(skb_headroom(skb) < 3)) {
2220                                 dev_kfree_skb(skb);
2221                                 skb = NULL;
2222                         } else {
2223                                 u8 *data = skb->data;
2224                                 size_t len = skb_headlen(skb);
2225                                 skb->data -= align;
2226                                 memmove(skb->data, data, len);
2227                                 skb_set_tail_pointer(skb, len);
2228                         }
2229                 }
2230         }
2231 #endif
2232
2233         if (skb) {
2234                 /* deliver to local stack */
2235                 skb->protocol = eth_type_trans(skb, dev);
2236                 memset(skb->cb, 0, sizeof(skb->cb));
2237                 if (rx->napi)
2238                         napi_gro_receive(rx->napi, skb);
2239                 else
2240                         netif_receive_skb(skb);
2241         }
2242
2243         if (xmit_skb) {
2244                 /*
2245                  * Send to wireless media and increase priority by 256 to
2246                  * keep the received priority instead of reclassifying
2247                  * the frame (see cfg80211_classify8021d).
2248                  */
2249                 xmit_skb->priority += 256;
2250                 xmit_skb->protocol = htons(ETH_P_802_3);
2251                 skb_reset_network_header(xmit_skb);
2252                 skb_reset_mac_header(xmit_skb);
2253                 dev_queue_xmit(xmit_skb);
2254         }
2255 }
2256
2257 static ieee80211_rx_result debug_noinline
2258 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2259 {
2260         struct net_device *dev = rx->sdata->dev;
2261         struct sk_buff *skb = rx->skb;
2262         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2263         __le16 fc = hdr->frame_control;
2264         struct sk_buff_head frame_list;
2265         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2266
2267         if (unlikely(!ieee80211_is_data(fc)))
2268                 return RX_CONTINUE;
2269
2270         if (unlikely(!ieee80211_is_data_present(fc)))
2271                 return RX_DROP_MONITOR;
2272
2273         if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2274                 return RX_CONTINUE;
2275
2276         if (ieee80211_has_a4(hdr->frame_control) &&
2277             rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2278             !rx->sdata->u.vlan.sta)
2279                 return RX_DROP_UNUSABLE;
2280
2281         if (is_multicast_ether_addr(hdr->addr1) &&
2282             ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2283               rx->sdata->u.vlan.sta) ||
2284              (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2285               rx->sdata->u.mgd.use_4addr)))
2286                 return RX_DROP_UNUSABLE;
2287
2288         skb->dev = dev;
2289         __skb_queue_head_init(&frame_list);
2290
2291         ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2292                                  rx->sdata->vif.type,
2293                                  rx->local->hw.extra_tx_headroom, true);
2294
2295         while (!skb_queue_empty(&frame_list)) {
2296                 rx->skb = __skb_dequeue(&frame_list);
2297
2298                 if (!ieee80211_frame_allowed(rx, fc)) {
2299                         dev_kfree_skb(rx->skb);
2300                         continue;
2301                 }
2302
2303                 ieee80211_deliver_skb(rx);
2304         }
2305
2306         return RX_QUEUED;
2307 }
2308
2309 #ifdef CONFIG_MAC80211_MESH
2310 static ieee80211_rx_result
2311 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2312 {
2313         struct ieee80211_hdr *fwd_hdr, *hdr;
2314         struct ieee80211_tx_info *info;
2315         struct ieee80211s_hdr *mesh_hdr;
2316         struct sk_buff *skb = rx->skb, *fwd_skb;
2317         struct ieee80211_local *local = rx->local;
2318         struct ieee80211_sub_if_data *sdata = rx->sdata;
2319         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2320         u16 ac, q, hdrlen;
2321
2322         hdr = (struct ieee80211_hdr *) skb->data;
2323         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2324
2325         /* make sure fixed part of mesh header is there, also checks skb len */
2326         if (!pskb_may_pull(rx->skb, hdrlen + 6))
2327                 return RX_DROP_MONITOR;
2328
2329         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2330
2331         /* make sure full mesh header is there, also checks skb len */
2332         if (!pskb_may_pull(rx->skb,
2333                            hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2334                 return RX_DROP_MONITOR;
2335
2336         /* reload pointers */
2337         hdr = (struct ieee80211_hdr *) skb->data;
2338         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2339
2340         if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2341                 return RX_DROP_MONITOR;
2342
2343         /* frame is in RMC, don't forward */
2344         if (ieee80211_is_data(hdr->frame_control) &&
2345             is_multicast_ether_addr(hdr->addr1) &&
2346             mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2347                 return RX_DROP_MONITOR;
2348
2349         if (!ieee80211_is_data(hdr->frame_control))
2350                 return RX_CONTINUE;
2351
2352         if (!mesh_hdr->ttl)
2353                 return RX_DROP_MONITOR;
2354
2355         if (mesh_hdr->flags & MESH_FLAGS_AE) {
2356                 struct mesh_path *mppath;
2357                 char *proxied_addr;
2358                 char *mpp_addr;
2359
2360                 if (is_multicast_ether_addr(hdr->addr1)) {
2361                         mpp_addr = hdr->addr3;
2362                         proxied_addr = mesh_hdr->eaddr1;
2363                 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2364                         /* has_a4 already checked in ieee80211_rx_mesh_check */
2365                         mpp_addr = hdr->addr4;
2366                         proxied_addr = mesh_hdr->eaddr2;
2367                 } else {
2368                         return RX_DROP_MONITOR;
2369                 }
2370
2371                 rcu_read_lock();
2372                 mppath = mpp_path_lookup(sdata, proxied_addr);
2373                 if (!mppath) {
2374                         mpp_path_add(sdata, proxied_addr, mpp_addr);
2375                 } else {
2376                         spin_lock_bh(&mppath->state_lock);
2377                         if (!ether_addr_equal(mppath->mpp, mpp_addr))
2378                                 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2379                         mppath->exp_time = jiffies;
2380                         spin_unlock_bh(&mppath->state_lock);
2381                 }
2382                 rcu_read_unlock();
2383         }
2384
2385         /* Frame has reached destination.  Don't forward */
2386         if (!is_multicast_ether_addr(hdr->addr1) &&
2387             ether_addr_equal(sdata->vif.addr, hdr->addr3))
2388                 return RX_CONTINUE;
2389
2390         ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2391         q = sdata->vif.hw_queue[ac];
2392         if (ieee80211_queue_stopped(&local->hw, q)) {
2393                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2394                 return RX_DROP_MONITOR;
2395         }
2396         skb_set_queue_mapping(skb, q);
2397
2398         if (!--mesh_hdr->ttl) {
2399                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2400                 goto out;
2401         }
2402
2403         if (!ifmsh->mshcfg.dot11MeshForwarding)
2404                 goto out;
2405
2406         fwd_skb = skb_copy(skb, GFP_ATOMIC);
2407         if (!fwd_skb) {
2408                 net_info_ratelimited("%s: failed to clone mesh frame\n",
2409                                     sdata->name);
2410                 goto out;
2411         }
2412
2413         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
2414         fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2415         info = IEEE80211_SKB_CB(fwd_skb);
2416         memset(info, 0, sizeof(*info));
2417         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2418         info->control.vif = &rx->sdata->vif;
2419         info->control.jiffies = jiffies;
2420         if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2421                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2422                 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2423                 /* update power mode indication when forwarding */
2424                 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2425         } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2426                 /* mesh power mode flags updated in mesh_nexthop_lookup */
2427                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2428         } else {
2429                 /* unable to resolve next hop */
2430                 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2431                                    fwd_hdr->addr3, 0,
2432                                    WLAN_REASON_MESH_PATH_NOFORWARD,
2433                                    fwd_hdr->addr2);
2434                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2435                 kfree_skb(fwd_skb);
2436                 return RX_DROP_MONITOR;
2437         }
2438
2439         IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2440         ieee80211_add_pending_skb(local, fwd_skb);
2441  out:
2442         if (is_multicast_ether_addr(hdr->addr1))
2443                 return RX_CONTINUE;
2444         return RX_DROP_MONITOR;
2445 }
2446 #endif
2447
2448 static ieee80211_rx_result debug_noinline
2449 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2450 {
2451         struct ieee80211_sub_if_data *sdata = rx->sdata;
2452         struct ieee80211_local *local = rx->local;
2453         struct net_device *dev = sdata->dev;
2454         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2455         __le16 fc = hdr->frame_control;
2456         bool port_control;
2457         int err;
2458
2459         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2460                 return RX_CONTINUE;
2461
2462         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2463                 return RX_DROP_MONITOR;
2464
2465         /*
2466          * Send unexpected-4addr-frame event to hostapd. For older versions,
2467          * also drop the frame to cooked monitor interfaces.
2468          */
2469         if (ieee80211_has_a4(hdr->frame_control) &&
2470             sdata->vif.type == NL80211_IFTYPE_AP) {
2471                 if (rx->sta &&
2472                     !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2473                         cfg80211_rx_unexpected_4addr_frame(
2474                                 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2475                 return RX_DROP_MONITOR;
2476         }
2477
2478         err = __ieee80211_data_to_8023(rx, &port_control);
2479         if (unlikely(err))
2480                 return RX_DROP_UNUSABLE;
2481
2482         if (!ieee80211_frame_allowed(rx, fc))
2483                 return RX_DROP_MONITOR;
2484
2485         /* directly handle TDLS channel switch requests/responses */
2486         if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2487                                                 cpu_to_be16(ETH_P_TDLS))) {
2488                 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2489
2490                 if (pskb_may_pull(rx->skb,
2491                                   offsetof(struct ieee80211_tdls_data, u)) &&
2492                     tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2493                     tf->category == WLAN_CATEGORY_TDLS &&
2494                     (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2495                      tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2496                         skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2497                         schedule_work(&local->tdls_chsw_work);
2498                         if (rx->sta)
2499                                 rx->sta->rx_stats.packets++;
2500
2501                         return RX_QUEUED;
2502                 }
2503         }
2504
2505         if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2506             unlikely(port_control) && sdata->bss) {
2507                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2508                                      u.ap);
2509                 dev = sdata->dev;
2510                 rx->sdata = sdata;
2511         }
2512
2513         rx->skb->dev = dev;
2514
2515         if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2516             local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2517             !is_multicast_ether_addr(
2518                     ((struct ethhdr *)rx->skb->data)->h_dest) &&
2519             (!local->scanning &&
2520              !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2521                 mod_timer(&local->dynamic_ps_timer, jiffies +
2522                           msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2523
2524         ieee80211_deliver_skb(rx);
2525
2526         return RX_QUEUED;
2527 }
2528
2529 static ieee80211_rx_result debug_noinline
2530 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2531 {
2532         struct sk_buff *skb = rx->skb;
2533         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2534         struct tid_ampdu_rx *tid_agg_rx;
2535         u16 start_seq_num;
2536         u16 tid;
2537
2538         if (likely(!ieee80211_is_ctl(bar->frame_control)))
2539                 return RX_CONTINUE;
2540
2541         if (ieee80211_is_back_req(bar->frame_control)) {
2542                 struct {
2543                         __le16 control, start_seq_num;
2544                 } __packed bar_data;
2545                 struct ieee80211_event event = {
2546                         .type = BAR_RX_EVENT,
2547                 };
2548
2549                 if (!rx->sta)
2550                         return RX_DROP_MONITOR;
2551
2552                 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2553                                   &bar_data, sizeof(bar_data)))
2554                         return RX_DROP_MONITOR;
2555
2556                 tid = le16_to_cpu(bar_data.control) >> 12;
2557
2558                 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2559                 if (!tid_agg_rx)
2560                         return RX_DROP_MONITOR;
2561
2562                 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2563                 event.u.ba.tid = tid;
2564                 event.u.ba.ssn = start_seq_num;
2565                 event.u.ba.sta = &rx->sta->sta;
2566
2567                 /* reset session timer */
2568                 if (tid_agg_rx->timeout)
2569                         mod_timer(&tid_agg_rx->session_timer,
2570                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
2571
2572                 spin_lock(&tid_agg_rx->reorder_lock);
2573                 /* release stored frames up to start of BAR */
2574                 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2575                                                  start_seq_num, frames);
2576                 spin_unlock(&tid_agg_rx->reorder_lock);
2577
2578                 drv_event_callback(rx->local, rx->sdata, &event);
2579
2580                 kfree_skb(skb);
2581                 return RX_QUEUED;
2582         }
2583
2584         /*
2585          * After this point, we only want management frames,
2586          * so we can drop all remaining control frames to
2587          * cooked monitor interfaces.
2588          */
2589         return RX_DROP_MONITOR;
2590 }
2591
2592 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2593                                            struct ieee80211_mgmt *mgmt,
2594                                            size_t len)
2595 {
2596         struct ieee80211_local *local = sdata->local;
2597         struct sk_buff *skb;
2598         struct ieee80211_mgmt *resp;
2599
2600         if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2601                 /* Not to own unicast address */
2602                 return;
2603         }
2604
2605         if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2606             !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
2607                 /* Not from the current AP or not associated yet. */
2608                 return;
2609         }
2610
2611         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2612                 /* Too short SA Query request frame */
2613                 return;
2614         }
2615
2616         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2617         if (skb == NULL)
2618                 return;
2619
2620         skb_reserve(skb, local->hw.extra_tx_headroom);
2621         resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2622         memset(resp, 0, 24);
2623         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2624         memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2625         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2626         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2627                                           IEEE80211_STYPE_ACTION);
2628         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2629         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2630         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2631         memcpy(resp->u.action.u.sa_query.trans_id,
2632                mgmt->u.action.u.sa_query.trans_id,
2633                WLAN_SA_QUERY_TR_ID_LEN);
2634
2635         ieee80211_tx_skb(sdata, skb);
2636 }
2637
2638 static ieee80211_rx_result debug_noinline
2639 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2640 {
2641         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2642         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2643
2644         /*
2645          * From here on, look only at management frames.
2646          * Data and control frames are already handled,
2647          * and unknown (reserved) frames are useless.
2648          */
2649         if (rx->skb->len < 24)
2650                 return RX_DROP_MONITOR;
2651
2652         if (!ieee80211_is_mgmt(mgmt->frame_control))
2653                 return RX_DROP_MONITOR;
2654
2655         if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2656             ieee80211_is_beacon(mgmt->frame_control) &&
2657             !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
2658                 int sig = 0;
2659
2660                 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2661                         sig = status->signal;
2662
2663                 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2664                                             rx->skb->data, rx->skb->len,
2665                                             status->freq, sig);
2666                 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2667         }
2668
2669         if (ieee80211_drop_unencrypted_mgmt(rx))
2670                 return RX_DROP_UNUSABLE;
2671
2672         return RX_CONTINUE;
2673 }
2674
2675 static ieee80211_rx_result debug_noinline
2676 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2677 {
2678         struct ieee80211_local *local = rx->local;
2679         struct ieee80211_sub_if_data *sdata = rx->sdata;
2680         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2681         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2682         int len = rx->skb->len;
2683
2684         if (!ieee80211_is_action(mgmt->frame_control))
2685                 return RX_CONTINUE;
2686
2687         /* drop too small frames */
2688         if (len < IEEE80211_MIN_ACTION_SIZE)
2689                 return RX_DROP_UNUSABLE;
2690
2691         if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2692             mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2693             mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2694                 return RX_DROP_UNUSABLE;
2695
2696         switch (mgmt->u.action.category) {
2697         case WLAN_CATEGORY_HT:
2698                 /* reject HT action frames from stations not supporting HT */
2699                 if (!rx->sta->sta.ht_cap.ht_supported)
2700                         goto invalid;
2701
2702                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2703                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2704                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2705                     sdata->vif.type != NL80211_IFTYPE_AP &&
2706                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
2707                         break;
2708
2709                 /* verify action & smps_control/chanwidth are present */
2710                 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2711                         goto invalid;
2712
2713                 switch (mgmt->u.action.u.ht_smps.action) {
2714                 case WLAN_HT_ACTION_SMPS: {
2715                         struct ieee80211_supported_band *sband;
2716                         enum ieee80211_smps_mode smps_mode;
2717
2718                         /* convert to HT capability */
2719                         switch (mgmt->u.action.u.ht_smps.smps_control) {
2720                         case WLAN_HT_SMPS_CONTROL_DISABLED:
2721                                 smps_mode = IEEE80211_SMPS_OFF;
2722                                 break;
2723                         case WLAN_HT_SMPS_CONTROL_STATIC:
2724                                 smps_mode = IEEE80211_SMPS_STATIC;
2725                                 break;
2726                         case WLAN_HT_SMPS_CONTROL_DYNAMIC:
2727                                 smps_mode = IEEE80211_SMPS_DYNAMIC;
2728                                 break;
2729                         default:
2730                                 goto invalid;
2731                         }
2732
2733                         /* if no change do nothing */
2734                         if (rx->sta->sta.smps_mode == smps_mode)
2735                                 goto handled;
2736                         rx->sta->sta.smps_mode = smps_mode;
2737
2738                         sband = rx->local->hw.wiphy->bands[status->band];
2739
2740                         rate_control_rate_update(local, sband, rx->sta,
2741                                                  IEEE80211_RC_SMPS_CHANGED);
2742                         goto handled;
2743                 }
2744                 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2745                         struct ieee80211_supported_band *sband;
2746                         u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
2747                         enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
2748
2749                         /* If it doesn't support 40 MHz it can't change ... */
2750                         if (!(rx->sta->sta.ht_cap.cap &
2751                                         IEEE80211_HT_CAP_SUP_WIDTH_20_40))
2752                                 goto handled;
2753
2754                         if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
2755                                 max_bw = IEEE80211_STA_RX_BW_20;
2756                         else
2757                                 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2758
2759                         /* set cur_max_bandwidth and recalc sta bw */
2760                         rx->sta->cur_max_bandwidth = max_bw;
2761                         new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
2762
2763                         if (rx->sta->sta.bandwidth == new_bw)
2764                                 goto handled;
2765
2766                         rx->sta->sta.bandwidth = new_bw;
2767                         sband = rx->local->hw.wiphy->bands[status->band];
2768
2769                         rate_control_rate_update(local, sband, rx->sta,
2770                                                  IEEE80211_RC_BW_CHANGED);
2771                         goto handled;
2772                 }
2773                 default:
2774                         goto invalid;
2775                 }
2776
2777                 break;
2778         case WLAN_CATEGORY_PUBLIC:
2779                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2780                         goto invalid;
2781                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2782                         break;
2783                 if (!rx->sta)
2784                         break;
2785                 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2786                         break;
2787                 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2788                                 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2789                         break;
2790                 if (len < offsetof(struct ieee80211_mgmt,
2791                                    u.action.u.ext_chan_switch.variable))
2792                         goto invalid;
2793                 goto queue;
2794         case WLAN_CATEGORY_VHT:
2795                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2796                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2797                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2798                     sdata->vif.type != NL80211_IFTYPE_AP &&
2799                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
2800                         break;
2801
2802                 /* verify action code is present */
2803                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2804                         goto invalid;
2805
2806                 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2807                 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2808                         u8 opmode;
2809
2810                         /* verify opmode is present */
2811                         if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2812                                 goto invalid;
2813
2814                         opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2815
2816                         ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
2817                                                     opmode, status->band);
2818                         goto handled;
2819                 }
2820                 case WLAN_VHT_ACTION_GROUPID_MGMT: {
2821                         if (len < IEEE80211_MIN_ACTION_SIZE + 25)
2822                                 goto invalid;
2823                         goto queue;
2824                 }
2825                 default:
2826                         break;
2827                 }
2828                 break;
2829         case WLAN_CATEGORY_BACK:
2830                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2831                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2832                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2833                     sdata->vif.type != NL80211_IFTYPE_AP &&
2834                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
2835                         break;
2836
2837                 /* verify action_code is present */
2838                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2839                         break;
2840
2841                 switch (mgmt->u.action.u.addba_req.action_code) {
2842                 case WLAN_ACTION_ADDBA_REQ:
2843                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2844                                    sizeof(mgmt->u.action.u.addba_req)))
2845                                 goto invalid;
2846                         break;
2847                 case WLAN_ACTION_ADDBA_RESP:
2848                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2849                                    sizeof(mgmt->u.action.u.addba_resp)))
2850                                 goto invalid;
2851                         break;
2852                 case WLAN_ACTION_DELBA:
2853                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2854                                    sizeof(mgmt->u.action.u.delba)))
2855                                 goto invalid;
2856                         break;
2857                 default:
2858                         goto invalid;
2859                 }
2860
2861                 goto queue;
2862         case WLAN_CATEGORY_SPECTRUM_MGMT:
2863                 /* verify action_code is present */
2864                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2865                         break;
2866
2867                 switch (mgmt->u.action.u.measurement.action_code) {
2868                 case WLAN_ACTION_SPCT_MSR_REQ:
2869                         if (status->band != NL80211_BAND_5GHZ)
2870                                 break;
2871
2872                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2873                                    sizeof(mgmt->u.action.u.measurement)))
2874                                 break;
2875
2876                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2877                                 break;
2878
2879                         ieee80211_process_measurement_req(sdata, mgmt, len);
2880                         goto handled;
2881                 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2882                         u8 *bssid;
2883                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2884                                    sizeof(mgmt->u.action.u.chan_switch)))
2885                                 break;
2886
2887                         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2888                             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2889                             sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2890                                 break;
2891
2892                         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2893                                 bssid = sdata->u.mgd.bssid;
2894                         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2895                                 bssid = sdata->u.ibss.bssid;
2896                         else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2897                                 bssid = mgmt->sa;
2898                         else
2899                                 break;
2900
2901                         if (!ether_addr_equal(mgmt->bssid, bssid))
2902                                 break;
2903
2904                         goto queue;
2905                         }
2906                 }
2907                 break;
2908         case WLAN_CATEGORY_SA_QUERY:
2909                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2910                            sizeof(mgmt->u.action.u.sa_query)))
2911                         break;
2912
2913                 switch (mgmt->u.action.u.sa_query.action) {
2914                 case WLAN_ACTION_SA_QUERY_REQUEST:
2915                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2916                                 break;
2917                         ieee80211_process_sa_query_req(sdata, mgmt, len);
2918                         goto handled;
2919                 }
2920                 break;
2921         case WLAN_CATEGORY_SELF_PROTECTED:
2922                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2923                            sizeof(mgmt->u.action.u.self_prot.action_code)))
2924                         break;
2925
2926                 switch (mgmt->u.action.u.self_prot.action_code) {
2927                 case WLAN_SP_MESH_PEERING_OPEN:
2928                 case WLAN_SP_MESH_PEERING_CLOSE:
2929                 case WLAN_SP_MESH_PEERING_CONFIRM:
2930                         if (!ieee80211_vif_is_mesh(&sdata->vif))
2931                                 goto invalid;
2932                         if (sdata->u.mesh.user_mpm)
2933                                 /* userspace handles this frame */
2934                                 break;
2935                         goto queue;
2936                 case WLAN_SP_MGK_INFORM:
2937                 case WLAN_SP_MGK_ACK:
2938                         if (!ieee80211_vif_is_mesh(&sdata->vif))
2939                                 goto invalid;
2940                         break;
2941                 }
2942                 break;
2943         case WLAN_CATEGORY_MESH_ACTION:
2944                 if (len < (IEEE80211_MIN_ACTION_SIZE +
2945                            sizeof(mgmt->u.action.u.mesh_action.action_code)))
2946                         break;
2947
2948                 if (!ieee80211_vif_is_mesh(&sdata->vif))
2949                         break;
2950                 if (mesh_action_is_path_sel(mgmt) &&
2951                     !mesh_path_sel_is_hwmp(sdata))
2952                         break;
2953                 goto queue;
2954         }
2955
2956         return RX_CONTINUE;
2957
2958  invalid:
2959         status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2960         /* will return in the next handlers */
2961         return RX_CONTINUE;
2962
2963  handled:
2964         if (rx->sta)
2965                 rx->sta->rx_stats.packets++;
2966         dev_kfree_skb(rx->skb);
2967         return RX_QUEUED;
2968
2969  queue:
2970         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2971         skb_queue_tail(&sdata->skb_queue, rx->skb);
2972         ieee80211_queue_work(&local->hw, &sdata->work);
2973         if (rx->sta)
2974                 rx->sta->rx_stats.packets++;
2975         return RX_QUEUED;
2976 }
2977
2978 static ieee80211_rx_result debug_noinline
2979 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2980 {
2981         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2982         int sig = 0;
2983
2984         /* skip known-bad action frames and return them in the next handler */
2985         if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2986                 return RX_CONTINUE;
2987
2988         /*
2989          * Getting here means the kernel doesn't know how to handle
2990          * it, but maybe userspace does ... include returned frames
2991          * so userspace can register for those to know whether ones
2992          * it transmitted were processed or returned.
2993          */
2994
2995         if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2996                 sig = status->signal;
2997
2998         if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
2999                              rx->skb->data, rx->skb->len, 0)) {
3000                 if (rx->sta)
3001                         rx->sta->rx_stats.packets++;
3002                 dev_kfree_skb(rx->skb);
3003                 return RX_QUEUED;
3004         }
3005
3006         return RX_CONTINUE;
3007 }
3008
3009 static ieee80211_rx_result debug_noinline
3010 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3011 {
3012         struct ieee80211_local *local = rx->local;
3013         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3014         struct sk_buff *nskb;
3015         struct ieee80211_sub_if_data *sdata = rx->sdata;
3016         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3017
3018         if (!ieee80211_is_action(mgmt->frame_control))
3019                 return RX_CONTINUE;
3020
3021         /*
3022          * For AP mode, hostapd is responsible for handling any action
3023          * frames that we didn't handle, including returning unknown
3024          * ones. For all other modes we will return them to the sender,
3025          * setting the 0x80 bit in the action category, as required by
3026          * 802.11-2012 9.24.4.
3027          * Newer versions of hostapd shall also use the management frame
3028          * registration mechanisms, but older ones still use cooked
3029          * monitor interfaces so push all frames there.
3030          */
3031         if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3032             (sdata->vif.type == NL80211_IFTYPE_AP ||
3033              sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3034                 return RX_DROP_MONITOR;
3035
3036         if (is_multicast_ether_addr(mgmt->da))
3037                 return RX_DROP_MONITOR;
3038
3039         /* do not return rejected action frames */
3040         if (mgmt->u.action.category & 0x80)
3041                 return RX_DROP_UNUSABLE;
3042
3043         nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3044                                GFP_ATOMIC);
3045         if (nskb) {
3046                 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3047
3048                 nmgmt->u.action.category |= 0x80;
3049                 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3050                 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3051
3052                 memset(nskb->cb, 0, sizeof(nskb->cb));
3053
3054                 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3055                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3056
3057                         info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3058                                       IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3059                                       IEEE80211_TX_CTL_NO_CCK_RATE;
3060                         if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3061                                 info->hw_queue =
3062                                         local->hw.offchannel_tx_hw_queue;
3063                 }
3064
3065                 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3066                                             status->band);
3067         }
3068         dev_kfree_skb(rx->skb);
3069         return RX_QUEUED;
3070 }
3071
3072 static ieee80211_rx_result debug_noinline
3073 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3074 {
3075         struct ieee80211_sub_if_data *sdata = rx->sdata;
3076         struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3077         __le16 stype;
3078
3079         stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3080
3081         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3082             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3083             sdata->vif.type != NL80211_IFTYPE_OCB &&
3084             sdata->vif.type != NL80211_IFTYPE_STATION)
3085                 return RX_DROP_MONITOR;
3086
3087         switch (stype) {
3088         case cpu_to_le16(IEEE80211_STYPE_AUTH):
3089         case cpu_to_le16(IEEE80211_STYPE_BEACON):
3090         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3091                 /* process for all: mesh, mlme, ibss */
3092                 break;
3093         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3094         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3095         case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3096         case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3097                 if (is_multicast_ether_addr(mgmt->da) &&
3098                     !is_broadcast_ether_addr(mgmt->da))
3099                         return RX_DROP_MONITOR;
3100
3101                 /* process only for station */
3102                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3103                         return RX_DROP_MONITOR;
3104                 break;
3105         case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3106                 /* process only for ibss and mesh */
3107                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3108                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3109                         return RX_DROP_MONITOR;
3110                 break;
3111         default:
3112                 return RX_DROP_MONITOR;
3113         }
3114
3115         /* queue up frame and kick off work to process it */
3116         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
3117         skb_queue_tail(&sdata->skb_queue, rx->skb);
3118         ieee80211_queue_work(&rx->local->hw, &sdata->work);
3119         if (rx->sta)
3120                 rx->sta->rx_stats.packets++;
3121
3122         return RX_QUEUED;
3123 }
3124
3125 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3126                                         struct ieee80211_rate *rate)
3127 {
3128         struct ieee80211_sub_if_data *sdata;
3129         struct ieee80211_local *local = rx->local;
3130         struct sk_buff *skb = rx->skb, *skb2;
3131         struct net_device *prev_dev = NULL;
3132         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3133         int needed_headroom;
3134
3135         /*
3136          * If cooked monitor has been processed already, then
3137          * don't do it again. If not, set the flag.
3138          */
3139         if (rx->flags & IEEE80211_RX_CMNTR)
3140                 goto out_free_skb;
3141         rx->flags |= IEEE80211_RX_CMNTR;
3142
3143         /* If there are no cooked monitor interfaces, just free the SKB */
3144         if (!local->cooked_mntrs)
3145                 goto out_free_skb;
3146
3147         /* vendor data is long removed here */
3148         status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3149         /* room for the radiotap header based on driver features */
3150         needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3151
3152         if (skb_headroom(skb) < needed_headroom &&
3153             pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3154                 goto out_free_skb;
3155
3156         /* prepend radiotap information */
3157         ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3158                                          false);
3159
3160         skb_reset_mac_header(skb);
3161         skb->ip_summed = CHECKSUM_UNNECESSARY;
3162         skb->pkt_type = PACKET_OTHERHOST;
3163         skb->protocol = htons(ETH_P_802_2);
3164
3165         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3166                 if (!ieee80211_sdata_running(sdata))
3167                         continue;
3168
3169                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3170                     !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3171                         continue;
3172
3173                 if (prev_dev) {
3174                         skb2 = skb_clone(skb, GFP_ATOMIC);
3175                         if (skb2) {
3176                                 skb2->dev = prev_dev;
3177                                 netif_receive_skb(skb2);
3178                         }
3179                 }
3180
3181                 prev_dev = sdata->dev;
3182                 ieee80211_rx_stats(sdata->dev, skb->len);
3183         }
3184
3185         if (prev_dev) {
3186                 skb->dev = prev_dev;
3187                 netif_receive_skb(skb);
3188                 return;
3189         }
3190
3191  out_free_skb:
3192         dev_kfree_skb(skb);
3193 }
3194
3195 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3196                                          ieee80211_rx_result res)
3197 {
3198         switch (res) {
3199         case RX_DROP_MONITOR:
3200                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3201                 if (rx->sta)
3202                         rx->sta->rx_stats.dropped++;
3203                 /* fall through */
3204         case RX_CONTINUE: {
3205                 struct ieee80211_rate *rate = NULL;
3206                 struct ieee80211_supported_band *sband;
3207                 struct ieee80211_rx_status *status;
3208
3209                 status = IEEE80211_SKB_RXCB((rx->skb));
3210
3211                 sband = rx->local->hw.wiphy->bands[status->band];
3212                 if (!(status->flag & RX_FLAG_HT) &&
3213                     !(status->flag & RX_FLAG_VHT))
3214                         rate = &sband->bitrates[status->rate_idx];
3215
3216                 ieee80211_rx_cooked_monitor(rx, rate);
3217                 break;
3218                 }
3219         case RX_DROP_UNUSABLE:
3220                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3221                 if (rx->sta)
3222                         rx->sta->rx_stats.dropped++;
3223                 dev_kfree_skb(rx->skb);
3224                 break;
3225         case RX_QUEUED:
3226                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3227                 break;
3228         }
3229 }
3230
3231 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3232                                   struct sk_buff_head *frames)
3233 {
3234         ieee80211_rx_result res = RX_DROP_MONITOR;
3235         struct sk_buff *skb;
3236
3237 #define CALL_RXH(rxh)                   \
3238         do {                            \
3239                 res = rxh(rx);          \
3240                 if (res != RX_CONTINUE) \
3241                         goto rxh_next;  \
3242         } while (0)
3243
3244         /* Lock here to avoid hitting all of the data used in the RX
3245          * path (e.g. key data, station data, ...) concurrently when
3246          * a frame is released from the reorder buffer due to timeout
3247          * from the timer, potentially concurrently with RX from the
3248          * driver.
3249          */
3250         spin_lock_bh(&rx->local->rx_path_lock);
3251
3252         while ((skb = __skb_dequeue(frames))) {
3253                 /*
3254                  * all the other fields are valid across frames
3255                  * that belong to an aMPDU since they are on the
3256                  * same TID from the same station
3257                  */
3258                 rx->skb = skb;
3259
3260                 CALL_RXH(ieee80211_rx_h_check_more_data);
3261                 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3262                 CALL_RXH(ieee80211_rx_h_sta_process);
3263                 CALL_RXH(ieee80211_rx_h_decrypt);
3264                 CALL_RXH(ieee80211_rx_h_defragment);
3265                 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3266                 /* must be after MMIC verify so header is counted in MPDU mic */
3267 #ifdef CONFIG_MAC80211_MESH
3268                 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3269                         CALL_RXH(ieee80211_rx_h_mesh_fwding);
3270 #endif
3271                 CALL_RXH(ieee80211_rx_h_amsdu);
3272                 CALL_RXH(ieee80211_rx_h_data);
3273
3274                 /* special treatment -- needs the queue */
3275                 res = ieee80211_rx_h_ctrl(rx, frames);
3276                 if (res != RX_CONTINUE)
3277                         goto rxh_next;
3278
3279                 CALL_RXH(ieee80211_rx_h_mgmt_check);
3280                 CALL_RXH(ieee80211_rx_h_action);
3281                 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3282                 CALL_RXH(ieee80211_rx_h_action_return);
3283                 CALL_RXH(ieee80211_rx_h_mgmt);
3284
3285  rxh_next:
3286                 ieee80211_rx_handlers_result(rx, res);
3287
3288 #undef CALL_RXH
3289         }
3290
3291         spin_unlock_bh(&rx->local->rx_path_lock);
3292 }
3293
3294 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3295 {
3296         struct sk_buff_head reorder_release;
3297         ieee80211_rx_result res = RX_DROP_MONITOR;
3298
3299         __skb_queue_head_init(&reorder_release);
3300
3301 #define CALL_RXH(rxh)                   \
3302         do {                            \
3303                 res = rxh(rx);          \
3304                 if (res != RX_CONTINUE) \
3305                         goto rxh_next;  \
3306         } while (0)
3307
3308         CALL_RXH(ieee80211_rx_h_check_dup);
3309         CALL_RXH(ieee80211_rx_h_check);
3310
3311         ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3312
3313         ieee80211_rx_handlers(rx, &reorder_release);
3314         return;
3315
3316  rxh_next:
3317         ieee80211_rx_handlers_result(rx, res);
3318
3319 #undef CALL_RXH
3320 }
3321
3322 /*
3323  * This function makes calls into the RX path, therefore
3324  * it has to be invoked under RCU read lock.
3325  */
3326 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3327 {
3328         struct sk_buff_head frames;
3329         struct ieee80211_rx_data rx = {
3330                 .sta = sta,
3331                 .sdata = sta->sdata,
3332                 .local = sta->local,
3333                 /* This is OK -- must be QoS data frame */
3334                 .security_idx = tid,
3335                 .seqno_idx = tid,
3336                 .napi = NULL, /* must be NULL to not have races */
3337         };
3338         struct tid_ampdu_rx *tid_agg_rx;
3339
3340         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3341         if (!tid_agg_rx)
3342                 return;
3343
3344         __skb_queue_head_init(&frames);
3345
3346         spin_lock(&tid_agg_rx->reorder_lock);
3347         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3348         spin_unlock(&tid_agg_rx->reorder_lock);
3349
3350         if (!skb_queue_empty(&frames)) {
3351                 struct ieee80211_event event = {
3352                         .type = BA_FRAME_TIMEOUT,
3353                         .u.ba.tid = tid,
3354                         .u.ba.sta = &sta->sta,
3355                 };
3356                 drv_event_callback(rx.local, rx.sdata, &event);
3357         }
3358
3359         ieee80211_rx_handlers(&rx, &frames);
3360 }
3361
3362 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3363                                           u16 ssn, u64 filtered,
3364                                           u16 received_mpdus)
3365 {
3366         struct sta_info *sta;
3367         struct tid_ampdu_rx *tid_agg_rx;
3368         struct sk_buff_head frames;
3369         struct ieee80211_rx_data rx = {
3370                 /* This is OK -- must be QoS data frame */
3371                 .security_idx = tid,
3372                 .seqno_idx = tid,
3373         };
3374         int i, diff;
3375
3376         if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3377                 return;
3378
3379         __skb_queue_head_init(&frames);
3380
3381         sta = container_of(pubsta, struct sta_info, sta);
3382
3383         rx.sta = sta;
3384         rx.sdata = sta->sdata;
3385         rx.local = sta->local;
3386
3387         rcu_read_lock();
3388         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3389         if (!tid_agg_rx)
3390                 goto out;
3391
3392         spin_lock_bh(&tid_agg_rx->reorder_lock);
3393
3394         if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3395                 int release;
3396
3397                 /* release all frames in the reorder buffer */
3398                 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3399                            IEEE80211_SN_MODULO;
3400                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3401                                                  release, &frames);
3402                 /* update ssn to match received ssn */
3403                 tid_agg_rx->head_seq_num = ssn;
3404         } else {
3405                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3406                                                  &frames);
3407         }
3408
3409         /* handle the case that received ssn is behind the mac ssn.
3410          * it can be tid_agg_rx->buf_size behind and still be valid */
3411         diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3412         if (diff >= tid_agg_rx->buf_size) {
3413                 tid_agg_rx->reorder_buf_filtered = 0;
3414                 goto release;
3415         }
3416         filtered = filtered >> diff;
3417         ssn += diff;
3418
3419         /* update bitmap */
3420         for (i = 0; i < tid_agg_rx->buf_size; i++) {
3421                 int index = (ssn + i) % tid_agg_rx->buf_size;
3422
3423                 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3424                 if (filtered & BIT_ULL(i))
3425                         tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3426         }
3427
3428         /* now process also frames that the filter marking released */
3429         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3430
3431 release:
3432         spin_unlock_bh(&tid_agg_rx->reorder_lock);
3433
3434         ieee80211_rx_handlers(&rx, &frames);
3435
3436  out:
3437         rcu_read_unlock();
3438 }
3439 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3440
3441 /* main receive path */
3442
3443 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3444 {
3445         struct ieee80211_sub_if_data *sdata = rx->sdata;
3446         struct sk_buff *skb = rx->skb;
3447         struct ieee80211_hdr *hdr = (void *)skb->data;
3448         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3449         u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3450         int multicast = is_multicast_ether_addr(hdr->addr1);
3451
3452         switch (sdata->vif.type) {
3453         case NL80211_IFTYPE_STATION:
3454                 if (!bssid && !sdata->u.mgd.use_4addr)
3455                         return false;
3456                 if (multicast)
3457                         return true;
3458                 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3459         case NL80211_IFTYPE_ADHOC:
3460                 if (!bssid)
3461                         return false;
3462                 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3463                     ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3464                         return false;
3465                 if (ieee80211_is_beacon(hdr->frame_control))
3466                         return true;
3467                 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3468                         return false;
3469                 if (!multicast &&
3470                     !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3471                         return false;
3472                 if (!rx->sta) {
3473                         int rate_idx;
3474                         if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3475                                 rate_idx = 0; /* TODO: HT/VHT rates */
3476                         else
3477                                 rate_idx = status->rate_idx;
3478                         ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3479                                                  BIT(rate_idx));
3480                 }
3481                 return true;
3482         case NL80211_IFTYPE_OCB:
3483                 if (!bssid)
3484                         return false;
3485                 if (!ieee80211_is_data_present(hdr->frame_control))
3486                         return false;
3487                 if (!is_broadcast_ether_addr(bssid))
3488                         return false;
3489                 if (!multicast &&
3490                     !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
3491                         return false;
3492                 if (!rx->sta) {
3493                         int rate_idx;
3494                         if (status->flag & RX_FLAG_HT)
3495                                 rate_idx = 0; /* TODO: HT rates */
3496                         else
3497                                 rate_idx = status->rate_idx;
3498                         ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3499                                                 BIT(rate_idx));
3500                 }
3501                 return true;
3502         case NL80211_IFTYPE_MESH_POINT:
3503                 if (multicast)
3504                         return true;
3505                 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3506         case NL80211_IFTYPE_AP_VLAN:
3507         case NL80211_IFTYPE_AP:
3508                 if (!bssid)
3509                         return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3510
3511                 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3512                         /*
3513                          * Accept public action frames even when the
3514                          * BSSID doesn't match, this is used for P2P
3515                          * and location updates. Note that mac80211
3516                          * itself never looks at these frames.
3517                          */
3518                         if (!multicast &&
3519                             !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3520                                 return false;
3521                         if (ieee80211_is_public_action(hdr, skb->len))
3522                                 return true;
3523                         return ieee80211_is_beacon(hdr->frame_control);
3524                 }
3525
3526                 if (!ieee80211_has_tods(hdr->frame_control)) {
3527                         /* ignore data frames to TDLS-peers */
3528                         if (ieee80211_is_data(hdr->frame_control))
3529                                 return false;
3530                         /* ignore action frames to TDLS-peers */
3531                         if (ieee80211_is_action(hdr->frame_control) &&
3532                             !is_broadcast_ether_addr(bssid) &&
3533                             !ether_addr_equal(bssid, hdr->addr1))
3534                                 return false;
3535                 }
3536                 return true;
3537         case NL80211_IFTYPE_WDS:
3538                 if (bssid || !ieee80211_is_data(hdr->frame_control))
3539                         return false;
3540                 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
3541         case NL80211_IFTYPE_P2P_DEVICE:
3542                 return ieee80211_is_public_action(hdr, skb->len) ||
3543                        ieee80211_is_probe_req(hdr->frame_control) ||
3544                        ieee80211_is_probe_resp(hdr->frame_control) ||
3545                        ieee80211_is_beacon(hdr->frame_control);
3546         default:
3547                 break;
3548         }
3549
3550         WARN_ON_ONCE(1);
3551         return false;
3552 }
3553
3554 void ieee80211_check_fast_rx(struct sta_info *sta)
3555 {
3556         struct ieee80211_sub_if_data *sdata = sta->sdata;
3557         struct ieee80211_local *local = sdata->local;
3558         struct ieee80211_key *key;
3559         struct ieee80211_fast_rx fastrx = {
3560                 .dev = sdata->dev,
3561                 .vif_type = sdata->vif.type,
3562                 .control_port_protocol = sdata->control_port_protocol,
3563         }, *old, *new = NULL;
3564         bool assign = false;
3565
3566         /* use sparse to check that we don't return without updating */
3567         __acquire(check_fast_rx);
3568
3569         BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3570         BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3571         ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3572         ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3573
3574         fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3575
3576         /* fast-rx doesn't do reordering */
3577         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3578             !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3579                 goto clear;
3580
3581         switch (sdata->vif.type) {
3582         case NL80211_IFTYPE_STATION:
3583                 /* 4-addr is harder to deal with, later maybe */
3584                 if (sdata->u.mgd.use_4addr)
3585                         goto clear;
3586                 /* software powersave is a huge mess, avoid all of it */
3587                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3588                         goto clear;
3589                 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3590                     !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3591                         goto clear;
3592                 if (sta->sta.tdls) {
3593                         fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3594                         fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3595                         fastrx.expected_ds_bits = 0;
3596                 } else {
3597                         fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3598                         fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3599                         fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3600                         fastrx.expected_ds_bits =
3601                                 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3602                 }
3603                 break;
3604         case NL80211_IFTYPE_AP_VLAN:
3605         case NL80211_IFTYPE_AP:
3606                 /* parallel-rx requires this, at least with calls to
3607                  * ieee80211_sta_ps_transition()
3608                  */
3609                 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3610                         goto clear;
3611                 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3612                 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3613                 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3614
3615                 fastrx.internal_forward =
3616                         !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3617                         (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3618                          !sdata->u.vlan.sta);
3619                 break;
3620         default:
3621                 goto clear;
3622         }
3623
3624         if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3625                 goto clear;
3626
3627         rcu_read_lock();
3628         key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3629         if (key) {
3630                 switch (key->conf.cipher) {
3631                 case WLAN_CIPHER_SUITE_TKIP:
3632                         /* we don't want to deal with MMIC in fast-rx */
3633                         goto clear_rcu;
3634                 case WLAN_CIPHER_SUITE_CCMP:
3635                 case WLAN_CIPHER_SUITE_CCMP_256:
3636                 case WLAN_CIPHER_SUITE_GCMP:
3637                 case WLAN_CIPHER_SUITE_GCMP_256:
3638                         break;
3639                 default:
3640                         /* we also don't want to deal with WEP or cipher scheme
3641                          * since those require looking up the key idx in the
3642                          * frame, rather than assuming the PTK is used
3643                          * (we need to revisit this once we implement the real
3644                          * PTK index, which is now valid in the spec, but we
3645                          * haven't implemented that part yet)
3646                          */
3647                         goto clear_rcu;
3648                 }
3649
3650                 fastrx.key = true;
3651                 fastrx.icv_len = key->conf.icv_len;
3652         }
3653
3654         assign = true;
3655  clear_rcu:
3656         rcu_read_unlock();
3657  clear:
3658         __release(check_fast_rx);
3659
3660         if (assign)
3661                 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3662
3663         spin_lock_bh(&sta->lock);
3664         old = rcu_dereference_protected(sta->fast_rx, true);
3665         rcu_assign_pointer(sta->fast_rx, new);
3666         spin_unlock_bh(&sta->lock);
3667
3668         if (old)
3669                 kfree_rcu(old, rcu_head);
3670 }
3671
3672 void ieee80211_clear_fast_rx(struct sta_info *sta)
3673 {
3674         struct ieee80211_fast_rx *old;
3675
3676         spin_lock_bh(&sta->lock);
3677         old = rcu_dereference_protected(sta->fast_rx, true);
3678         RCU_INIT_POINTER(sta->fast_rx, NULL);
3679         spin_unlock_bh(&sta->lock);
3680
3681         if (old)
3682                 kfree_rcu(old, rcu_head);
3683 }
3684
3685 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3686 {
3687         struct ieee80211_local *local = sdata->local;
3688         struct sta_info *sta;
3689
3690         lockdep_assert_held(&local->sta_mtx);
3691
3692         list_for_each_entry_rcu(sta, &local->sta_list, list) {
3693                 if (sdata != sta->sdata &&
3694                     (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3695                         continue;
3696                 ieee80211_check_fast_rx(sta);
3697         }
3698 }
3699
3700 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3701 {
3702         struct ieee80211_local *local = sdata->local;
3703
3704         mutex_lock(&local->sta_mtx);
3705         __ieee80211_check_fast_rx_iface(sdata);
3706         mutex_unlock(&local->sta_mtx);
3707 }
3708
3709 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3710                                      struct ieee80211_fast_rx *fast_rx)
3711 {
3712         struct sk_buff *skb = rx->skb;
3713         struct ieee80211_hdr *hdr = (void *)skb->data;
3714         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3715         struct sta_info *sta = rx->sta;
3716         int orig_len = skb->len;
3717         int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3718         struct {
3719                 u8 snap[sizeof(rfc1042_header)];
3720                 __be16 proto;
3721         } *payload __aligned(2);
3722         struct {
3723                 u8 da[ETH_ALEN];
3724                 u8 sa[ETH_ALEN];
3725         } addrs __aligned(2);
3726         struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3727
3728         if (fast_rx->uses_rss)
3729                 stats = this_cpu_ptr(sta->pcpu_rx_stats);
3730
3731         /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3732          * to a common data structure; drivers can implement that per queue
3733          * but we don't have that information in mac80211
3734          */
3735         if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3736                 return false;
3737
3738 #define FAST_RX_CRYPT_FLAGS     (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3739
3740         /* If using encryption, we also need to have:
3741          *  - PN_VALIDATED: similar, but the implementation is tricky
3742          *  - DECRYPTED: necessary for PN_VALIDATED
3743          */
3744         if (fast_rx->key &&
3745             (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
3746                 return false;
3747
3748         /* we don't deal with A-MSDU deaggregation here */
3749         if (status->rx_flags & IEEE80211_RX_AMSDU)
3750                 return false;
3751
3752         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3753                 return false;
3754
3755         if (unlikely(ieee80211_is_frag(hdr)))
3756                 return false;
3757
3758         /* Since our interface address cannot be multicast, this
3759          * implicitly also rejects multicast frames without the
3760          * explicit check.
3761          *
3762          * We shouldn't get any *data* frames not addressed to us
3763          * (AP mode will accept multicast *management* frames), but
3764          * punting here will make it go through the full checks in
3765          * ieee80211_accept_frame().
3766          */
3767         if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
3768                 return false;
3769
3770         if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
3771                                               IEEE80211_FCTL_TODS)) !=
3772             fast_rx->expected_ds_bits)
3773                 goto drop;
3774
3775         /* assign the key to drop unencrypted frames (later)
3776          * and strip the IV/MIC if necessary
3777          */
3778         if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
3779                 /* GCMP header length is the same */
3780                 snap_offs += IEEE80211_CCMP_HDR_LEN;
3781         }
3782
3783         if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
3784                 goto drop;
3785         payload = (void *)(skb->data + snap_offs);
3786
3787         if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
3788                 return false;
3789
3790         /* Don't handle these here since they require special code.
3791          * Accept AARP and IPX even though they should come with a
3792          * bridge-tunnel header - but if we get them this way then
3793          * there's little point in discarding them.
3794          */
3795         if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
3796                      payload->proto == fast_rx->control_port_protocol))
3797                 return false;
3798
3799         /* after this point, don't punt to the slowpath! */
3800
3801         if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
3802             pskb_trim(skb, skb->len - fast_rx->icv_len))
3803                 goto drop;
3804
3805         if (unlikely(fast_rx->sta_notify)) {
3806                 ieee80211_sta_rx_notify(rx->sdata, hdr);
3807                 fast_rx->sta_notify = false;
3808         }
3809
3810         /* statistics part of ieee80211_rx_h_sta_process() */
3811         stats->last_rx = jiffies;
3812         stats->last_rate = sta_stats_encode_rate(status);
3813
3814         stats->fragments++;
3815
3816         if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
3817                 stats->last_signal = status->signal;
3818                 if (!fast_rx->uses_rss)
3819                         ewma_signal_add(&sta->rx_stats_avg.signal,
3820                                         -status->signal);
3821         }
3822
3823         if (status->chains) {
3824                 int i;
3825
3826                 stats->chains = status->chains;
3827                 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
3828                         int signal = status->chain_signal[i];
3829
3830                         if (!(status->chains & BIT(i)))
3831                                 continue;
3832
3833                         stats->chain_signal_last[i] = signal;
3834                         if (!fast_rx->uses_rss)
3835                                 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
3836                                                 -signal);
3837                 }
3838         }
3839         /* end of statistics */
3840
3841         if (rx->key && !ieee80211_has_protected(hdr->frame_control))
3842                 goto drop;
3843
3844         /* do the header conversion - first grab the addresses */
3845         ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
3846         ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
3847         /* remove the SNAP but leave the ethertype */
3848         skb_pull(skb, snap_offs + sizeof(rfc1042_header));
3849         /* push the addresses in front */
3850         memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
3851
3852         skb->dev = fast_rx->dev;
3853
3854         ieee80211_rx_stats(fast_rx->dev, skb->len);
3855
3856         /* The seqno index has the same property as needed
3857          * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
3858          * for non-QoS-data frames. Here we know it's a data
3859          * frame, so count MSDUs.
3860          */
3861         u64_stats_update_begin(&stats->syncp);
3862         stats->msdu[rx->seqno_idx]++;
3863         stats->bytes += orig_len;
3864         u64_stats_update_end(&stats->syncp);
3865
3866         if (fast_rx->internal_forward) {
3867                 struct sta_info *dsta = sta_info_get(rx->sdata, skb->data);
3868
3869                 if (dsta) {
3870                         /*
3871                          * Send to wireless media and increase priority by 256
3872                          * to keep the received priority instead of
3873                          * reclassifying the frame (see cfg80211_classify8021d).
3874                          */
3875                         skb->priority += 256;
3876                         skb->protocol = htons(ETH_P_802_3);
3877                         skb_reset_network_header(skb);
3878                         skb_reset_mac_header(skb);
3879                         dev_queue_xmit(skb);
3880                         return true;
3881                 }
3882         }
3883
3884         /* deliver to local stack */
3885         skb->protocol = eth_type_trans(skb, fast_rx->dev);
3886         memset(skb->cb, 0, sizeof(skb->cb));
3887         if (rx->napi)
3888                 napi_gro_receive(rx->napi, skb);
3889         else
3890                 netif_receive_skb(skb);
3891
3892         return true;
3893  drop:
3894         dev_kfree_skb(skb);
3895         stats->dropped++;
3896         return true;
3897 }
3898
3899 /*
3900  * This function returns whether or not the SKB
3901  * was destined for RX processing or not, which,
3902  * if consume is true, is equivalent to whether
3903  * or not the skb was consumed.
3904  */
3905 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3906                                             struct sk_buff *skb, bool consume)
3907 {
3908         struct ieee80211_local *local = rx->local;
3909         struct ieee80211_sub_if_data *sdata = rx->sdata;
3910
3911         rx->skb = skb;
3912
3913         /* See if we can do fast-rx; if we have to copy we already lost,
3914          * so punt in that case. We should never have to deliver a data
3915          * frame to multiple interfaces anyway.
3916          *
3917          * We skip the ieee80211_accept_frame() call and do the necessary
3918          * checking inside ieee80211_invoke_fast_rx().
3919          */
3920         if (consume && rx->sta) {
3921                 struct ieee80211_fast_rx *fast_rx;
3922
3923                 fast_rx = rcu_dereference(rx->sta->fast_rx);
3924                 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
3925                         return true;
3926         }
3927
3928         if (!ieee80211_accept_frame(rx))
3929                 return false;
3930
3931         if (!consume) {
3932                 skb = skb_copy(skb, GFP_ATOMIC);
3933                 if (!skb) {
3934                         if (net_ratelimit())
3935                                 wiphy_debug(local->hw.wiphy,
3936                                         "failed to copy skb for %s\n",
3937                                         sdata->name);
3938                         return true;
3939                 }
3940
3941                 rx->skb = skb;
3942         }
3943
3944         ieee80211_invoke_rx_handlers(rx);
3945         return true;
3946 }
3947
3948 /*
3949  * This is the actual Rx frames handler. as it belongs to Rx path it must
3950  * be called with rcu_read_lock protection.
3951  */
3952 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
3953                                          struct ieee80211_sta *pubsta,
3954                                          struct sk_buff *skb,
3955                                          struct napi_struct *napi)
3956 {
3957         struct ieee80211_local *local = hw_to_local(hw);
3958         struct ieee80211_sub_if_data *sdata;
3959         struct ieee80211_hdr *hdr;
3960         __le16 fc;
3961         struct ieee80211_rx_data rx;
3962         struct ieee80211_sub_if_data *prev;
3963         struct rhash_head *tmp;
3964         int err = 0;
3965
3966         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
3967         memset(&rx, 0, sizeof(rx));
3968         rx.skb = skb;
3969         rx.local = local;
3970         rx.napi = napi;
3971
3972         if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
3973                 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
3974
3975         if (ieee80211_is_mgmt(fc)) {
3976                 /* drop frame if too short for header */
3977                 if (skb->len < ieee80211_hdrlen(fc))
3978                         err = -ENOBUFS;
3979                 else
3980                         err = skb_linearize(skb);
3981         } else {
3982                 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
3983         }
3984
3985         if (err) {
3986                 dev_kfree_skb(skb);
3987                 return;
3988         }
3989
3990         hdr = (struct ieee80211_hdr *)skb->data;
3991         ieee80211_parse_qos(&rx);
3992         ieee80211_verify_alignment(&rx);
3993
3994         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3995                      ieee80211_is_beacon(hdr->frame_control)))
3996                 ieee80211_scan_rx(local, skb);
3997
3998         if (pubsta) {
3999                 rx.sta = container_of(pubsta, struct sta_info, sta);
4000                 rx.sdata = rx.sta->sdata;
4001                 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4002                         return;
4003                 goto out;
4004         } else if (ieee80211_is_data(fc)) {
4005                 struct sta_info *sta, *prev_sta;
4006                 const struct bucket_table *tbl;
4007
4008                 prev_sta = NULL;
4009
4010                 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
4011
4012                 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
4013                         if (!prev_sta) {
4014                                 prev_sta = sta;
4015                                 continue;
4016                         }
4017
4018                         rx.sta = prev_sta;
4019                         rx.sdata = prev_sta->sdata;
4020                         ieee80211_prepare_and_rx_handle(&rx, skb, false);
4021
4022                         prev_sta = sta;
4023                 }
4024
4025                 if (prev_sta) {
4026                         rx.sta = prev_sta;
4027                         rx.sdata = prev_sta->sdata;
4028
4029                         if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4030                                 return;
4031                         goto out;
4032                 }
4033         }
4034
4035         prev = NULL;
4036
4037         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4038                 if (!ieee80211_sdata_running(sdata))
4039                         continue;
4040
4041                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4042                     sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4043                         continue;
4044
4045                 /*
4046                  * frame is destined for this interface, but if it's
4047                  * not also for the previous one we handle that after
4048                  * the loop to avoid copying the SKB once too much
4049                  */
4050
4051                 if (!prev) {
4052                         prev = sdata;
4053                         continue;
4054                 }
4055
4056                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4057                 rx.sdata = prev;
4058                 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4059
4060                 prev = sdata;
4061         }
4062
4063         if (prev) {
4064                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4065                 rx.sdata = prev;
4066
4067                 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4068                         return;
4069         }
4070
4071  out:
4072         dev_kfree_skb(skb);
4073 }
4074
4075 /*
4076  * This is the receive path handler. It is called by a low level driver when an
4077  * 802.11 MPDU is received from the hardware.
4078  */
4079 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4080                        struct sk_buff *skb, struct napi_struct *napi)
4081 {
4082         struct ieee80211_local *local = hw_to_local(hw);
4083         struct ieee80211_rate *rate = NULL;
4084         struct ieee80211_supported_band *sband;
4085         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4086
4087         WARN_ON_ONCE(softirq_count() == 0);
4088
4089         if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4090                 goto drop;
4091
4092         sband = local->hw.wiphy->bands[status->band];
4093         if (WARN_ON(!sband))
4094                 goto drop;
4095
4096         /*
4097          * If we're suspending, it is possible although not too likely
4098          * that we'd be receiving frames after having already partially
4099          * quiesced the stack. We can't process such frames then since
4100          * that might, for example, cause stations to be added or other
4101          * driver callbacks be invoked.
4102          */
4103         if (unlikely(local->quiescing || local->suspended))
4104                 goto drop;
4105
4106         /* We might be during a HW reconfig, prevent Rx for the same reason */
4107         if (unlikely(local->in_reconfig))
4108                 goto drop;
4109
4110         /*
4111          * The same happens when we're not even started,
4112          * but that's worth a warning.
4113          */
4114         if (WARN_ON(!local->started))
4115                 goto drop;
4116
4117         if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4118                 /*
4119                  * Validate the rate, unless a PLCP error means that
4120                  * we probably can't have a valid rate here anyway.
4121                  */
4122
4123                 if (status->flag & RX_FLAG_HT) {
4124                         /*
4125                          * rate_idx is MCS index, which can be [0-76]
4126                          * as documented on:
4127                          *
4128                          * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4129                          *
4130                          * Anything else would be some sort of driver or
4131                          * hardware error. The driver should catch hardware
4132                          * errors.
4133                          */
4134                         if (WARN(status->rate_idx > 76,
4135                                  "Rate marked as an HT rate but passed "
4136                                  "status->rate_idx is not "
4137                                  "an MCS index [0-76]: %d (0x%02x)\n",
4138                                  status->rate_idx,
4139                                  status->rate_idx))
4140                                 goto drop;
4141                 } else if (status->flag & RX_FLAG_VHT) {
4142                         if (WARN_ONCE(status->rate_idx > 9 ||
4143                                       !status->vht_nss ||
4144                                       status->vht_nss > 8,
4145                                       "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4146                                       status->rate_idx, status->vht_nss))
4147                                 goto drop;
4148                 } else {
4149                         if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4150                                 goto drop;
4151                         rate = &sband->bitrates[status->rate_idx];
4152                 }
4153         }
4154
4155         status->rx_flags = 0;
4156
4157         /*
4158          * key references and virtual interfaces are protected using RCU
4159          * and this requires that we are in a read-side RCU section during
4160          * receive processing
4161          */
4162         rcu_read_lock();
4163
4164         /*
4165          * Frames with failed FCS/PLCP checksum are not returned,
4166          * all other frames are returned without radiotap header
4167          * if it was previously present.
4168          * Also, frames with less than 16 bytes are dropped.
4169          */
4170         skb = ieee80211_rx_monitor(local, skb, rate);
4171         if (!skb) {
4172                 rcu_read_unlock();
4173                 return;
4174         }
4175
4176         ieee80211_tpt_led_trig_rx(local,
4177                         ((struct ieee80211_hdr *)skb->data)->frame_control,
4178                         skb->len);
4179
4180         __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
4181
4182         rcu_read_unlock();
4183
4184         return;
4185  drop:
4186         kfree_skb(skb);
4187 }
4188 EXPORT_SYMBOL(ieee80211_rx_napi);
4189
4190 /* This is a version of the rx handler that can be called from hard irq
4191  * context. Post the skb on the queue and schedule the tasklet */
4192 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4193 {
4194         struct ieee80211_local *local = hw_to_local(hw);
4195
4196         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4197
4198         skb->pkt_type = IEEE80211_RX_MSG;
4199         skb_queue_tail(&local->skb_queue, skb);
4200         tasklet_schedule(&local->tasklet);
4201 }
4202 EXPORT_SYMBOL(ieee80211_rx_irqsafe);