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