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