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