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