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