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