GNU Linux-libre 4.19.209-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_BLOCKACK &&
1326             ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1327                 goto dont_reorder;
1328
1329         /* new, potentially un-ordered, ampdu frame - process it */
1330
1331         /* reset session timer */
1332         if (tid_agg_rx->timeout)
1333                 tid_agg_rx->last_rx = jiffies;
1334
1335         /* if this mpdu is fragmented - terminate rx aggregation session */
1336         sc = le16_to_cpu(hdr->seq_ctrl);
1337         if (sc & IEEE80211_SCTL_FRAG) {
1338                 skb_queue_tail(&rx->sdata->skb_queue, skb);
1339                 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1340                 return;
1341         }
1342
1343         /*
1344          * No locking needed -- we will only ever process one
1345          * RX packet at a time, and thus own tid_agg_rx. All
1346          * other code manipulating it needs to (and does) make
1347          * sure that we cannot get to it any more before doing
1348          * anything with it.
1349          */
1350         if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1351                                              frames))
1352                 return;
1353
1354  dont_reorder:
1355         __skb_queue_tail(frames, skb);
1356 }
1357
1358 static ieee80211_rx_result debug_noinline
1359 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1360 {
1361         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1362         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1363
1364         if (status->flag & RX_FLAG_DUP_VALIDATED)
1365                 return RX_CONTINUE;
1366
1367         /*
1368          * Drop duplicate 802.11 retransmissions
1369          * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1370          */
1371
1372         if (rx->skb->len < 24)
1373                 return RX_CONTINUE;
1374
1375         if (ieee80211_is_ctl(hdr->frame_control) ||
1376             ieee80211_is_any_nullfunc(hdr->frame_control) ||
1377             is_multicast_ether_addr(hdr->addr1))
1378                 return RX_CONTINUE;
1379
1380         if (!rx->sta)
1381                 return RX_CONTINUE;
1382
1383         if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1384                      rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1385                 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1386                 rx->sta->rx_stats.num_duplicates++;
1387                 return RX_DROP_UNUSABLE;
1388         } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1389                 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1390         }
1391
1392         return RX_CONTINUE;
1393 }
1394
1395 static ieee80211_rx_result debug_noinline
1396 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1397 {
1398         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1399
1400         /* Drop disallowed frame classes based on STA auth/assoc state;
1401          * IEEE 802.11, Chap 5.5.
1402          *
1403          * mac80211 filters only based on association state, i.e. it drops
1404          * Class 3 frames from not associated stations. hostapd sends
1405          * deauth/disassoc frames when needed. In addition, hostapd is
1406          * responsible for filtering on both auth and assoc states.
1407          */
1408
1409         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1410                 return ieee80211_rx_mesh_check(rx);
1411
1412         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1413                       ieee80211_is_pspoll(hdr->frame_control)) &&
1414                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1415                      rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1416                      rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1417                      (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1418                 /*
1419                  * accept port control frames from the AP even when it's not
1420                  * yet marked ASSOC to prevent a race where we don't set the
1421                  * assoc bit quickly enough before it sends the first frame
1422                  */
1423                 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1424                     ieee80211_is_data_present(hdr->frame_control)) {
1425                         unsigned int hdrlen;
1426                         __be16 ethertype;
1427
1428                         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1429
1430                         if (rx->skb->len < hdrlen + 8)
1431                                 return RX_DROP_MONITOR;
1432
1433                         skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1434                         if (ethertype == rx->sdata->control_port_protocol)
1435                                 return RX_CONTINUE;
1436                 }
1437
1438                 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1439                     cfg80211_rx_spurious_frame(rx->sdata->dev,
1440                                                hdr->addr2,
1441                                                GFP_ATOMIC))
1442                         return RX_DROP_UNUSABLE;
1443
1444                 return RX_DROP_MONITOR;
1445         }
1446
1447         return RX_CONTINUE;
1448 }
1449
1450
1451 static ieee80211_rx_result debug_noinline
1452 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1453 {
1454         struct ieee80211_local *local;
1455         struct ieee80211_hdr *hdr;
1456         struct sk_buff *skb;
1457
1458         local = rx->local;
1459         skb = rx->skb;
1460         hdr = (struct ieee80211_hdr *) skb->data;
1461
1462         if (!local->pspolling)
1463                 return RX_CONTINUE;
1464
1465         if (!ieee80211_has_fromds(hdr->frame_control))
1466                 /* this is not from AP */
1467                 return RX_CONTINUE;
1468
1469         if (!ieee80211_is_data(hdr->frame_control))
1470                 return RX_CONTINUE;
1471
1472         if (!ieee80211_has_moredata(hdr->frame_control)) {
1473                 /* AP has no more frames buffered for us */
1474                 local->pspolling = false;
1475                 return RX_CONTINUE;
1476         }
1477
1478         /* more data bit is set, let's request a new frame from the AP */
1479         ieee80211_send_pspoll(local, rx->sdata);
1480
1481         return RX_CONTINUE;
1482 }
1483
1484 static void sta_ps_start(struct sta_info *sta)
1485 {
1486         struct ieee80211_sub_if_data *sdata = sta->sdata;
1487         struct ieee80211_local *local = sdata->local;
1488         struct ps_data *ps;
1489         int tid;
1490
1491         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1492             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1493                 ps = &sdata->bss->ps;
1494         else
1495                 return;
1496
1497         atomic_inc(&ps->num_sta_ps);
1498         set_sta_flag(sta, WLAN_STA_PS_STA);
1499         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1500                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1501         ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1502                sta->sta.addr, sta->sta.aid);
1503
1504         ieee80211_clear_fast_xmit(sta);
1505
1506         if (!sta->sta.txq[0])
1507                 return;
1508
1509         for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1510                 if (txq_has_queue(sta->sta.txq[tid]))
1511                         set_bit(tid, &sta->txq_buffered_tids);
1512                 else
1513                         clear_bit(tid, &sta->txq_buffered_tids);
1514         }
1515 }
1516
1517 static void sta_ps_end(struct sta_info *sta)
1518 {
1519         ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1520                sta->sta.addr, sta->sta.aid);
1521
1522         if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1523                 /*
1524                  * Clear the flag only if the other one is still set
1525                  * so that the TX path won't start TX'ing new frames
1526                  * directly ... In the case that the driver flag isn't
1527                  * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1528                  */
1529                 clear_sta_flag(sta, WLAN_STA_PS_STA);
1530                 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1531                        sta->sta.addr, sta->sta.aid);
1532                 return;
1533         }
1534
1535         set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1536         clear_sta_flag(sta, WLAN_STA_PS_STA);
1537         ieee80211_sta_ps_deliver_wakeup(sta);
1538 }
1539
1540 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1541 {
1542         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1543         bool in_ps;
1544
1545         WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1546
1547         /* Don't let the same PS state be set twice */
1548         in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1549         if ((start && in_ps) || (!start && !in_ps))
1550                 return -EINVAL;
1551
1552         if (start)
1553                 sta_ps_start(sta);
1554         else
1555                 sta_ps_end(sta);
1556
1557         return 0;
1558 }
1559 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1560
1561 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1562 {
1563         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1564
1565         if (test_sta_flag(sta, WLAN_STA_SP))
1566                 return;
1567
1568         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1569                 ieee80211_sta_ps_deliver_poll_response(sta);
1570         else
1571                 set_sta_flag(sta, WLAN_STA_PSPOLL);
1572 }
1573 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1574
1575 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1576 {
1577         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1578         int ac = ieee80211_ac_from_tid(tid);
1579
1580         /*
1581          * If this AC is not trigger-enabled do nothing unless the
1582          * driver is calling us after it already checked.
1583          *
1584          * NB: This could/should check a separate bitmap of trigger-
1585          * enabled queues, but for now we only implement uAPSD w/o
1586          * TSPEC changes to the ACs, so they're always the same.
1587          */
1588         if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1589             tid != IEEE80211_NUM_TIDS)
1590                 return;
1591
1592         /* if we are in a service period, do nothing */
1593         if (test_sta_flag(sta, WLAN_STA_SP))
1594                 return;
1595
1596         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1597                 ieee80211_sta_ps_deliver_uapsd(sta);
1598         else
1599                 set_sta_flag(sta, WLAN_STA_UAPSD);
1600 }
1601 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1602
1603 static ieee80211_rx_result debug_noinline
1604 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1605 {
1606         struct ieee80211_sub_if_data *sdata = rx->sdata;
1607         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1608         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1609
1610         if (!rx->sta)
1611                 return RX_CONTINUE;
1612
1613         if (sdata->vif.type != NL80211_IFTYPE_AP &&
1614             sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1615                 return RX_CONTINUE;
1616
1617         /*
1618          * The device handles station powersave, so don't do anything about
1619          * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1620          * it to mac80211 since they're handled.)
1621          */
1622         if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1623                 return RX_CONTINUE;
1624
1625         /*
1626          * Don't do anything if the station isn't already asleep. In
1627          * the uAPSD case, the station will probably be marked asleep,
1628          * in the PS-Poll case the station must be confused ...
1629          */
1630         if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1631                 return RX_CONTINUE;
1632
1633         if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1634                 ieee80211_sta_pspoll(&rx->sta->sta);
1635
1636                 /* Free PS Poll skb here instead of returning RX_DROP that would
1637                  * count as an dropped frame. */
1638                 dev_kfree_skb(rx->skb);
1639
1640                 return RX_QUEUED;
1641         } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1642                    !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1643                    ieee80211_has_pm(hdr->frame_control) &&
1644                    (ieee80211_is_data_qos(hdr->frame_control) ||
1645                     ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1646                 u8 tid = ieee80211_get_tid(hdr);
1647
1648                 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1649         }
1650
1651         return RX_CONTINUE;
1652 }
1653
1654 static ieee80211_rx_result debug_noinline
1655 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1656 {
1657         struct sta_info *sta = rx->sta;
1658         struct sk_buff *skb = rx->skb;
1659         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1660         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1661         int i;
1662
1663         if (!sta)
1664                 return RX_CONTINUE;
1665
1666         /*
1667          * Update last_rx only for IBSS packets which are for the current
1668          * BSSID and for station already AUTHORIZED to avoid keeping the
1669          * current IBSS network alive in cases where other STAs start
1670          * using different BSSID. This will also give the station another
1671          * chance to restart the authentication/authorization in case
1672          * something went wrong the first time.
1673          */
1674         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1675                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1676                                                 NL80211_IFTYPE_ADHOC);
1677                 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1678                     test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1679                         sta->rx_stats.last_rx = jiffies;
1680                         if (ieee80211_is_data(hdr->frame_control) &&
1681                             !is_multicast_ether_addr(hdr->addr1))
1682                                 sta->rx_stats.last_rate =
1683                                         sta_stats_encode_rate(status);
1684                 }
1685         } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1686                 sta->rx_stats.last_rx = jiffies;
1687         } else if (!is_multicast_ether_addr(hdr->addr1)) {
1688                 /*
1689                  * Mesh beacons will update last_rx when if they are found to
1690                  * match the current local configuration when processed.
1691                  */
1692                 sta->rx_stats.last_rx = jiffies;
1693                 if (ieee80211_is_data(hdr->frame_control))
1694                         sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1695         }
1696
1697         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1698                 ieee80211_sta_rx_notify(rx->sdata, hdr);
1699
1700         sta->rx_stats.fragments++;
1701
1702         u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1703         sta->rx_stats.bytes += rx->skb->len;
1704         u64_stats_update_end(&rx->sta->rx_stats.syncp);
1705
1706         if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1707                 sta->rx_stats.last_signal = status->signal;
1708                 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1709         }
1710
1711         if (status->chains) {
1712                 sta->rx_stats.chains = status->chains;
1713                 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1714                         int signal = status->chain_signal[i];
1715
1716                         if (!(status->chains & BIT(i)))
1717                                 continue;
1718
1719                         sta->rx_stats.chain_signal_last[i] = signal;
1720                         ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1721                                         -signal);
1722                 }
1723         }
1724
1725         /*
1726          * Change STA power saving mode only at the end of a frame
1727          * exchange sequence, and only for a data or management
1728          * frame as specified in IEEE 802.11-2016 11.2.3.2
1729          */
1730         if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1731             !ieee80211_has_morefrags(hdr->frame_control) &&
1732             !is_multicast_ether_addr(hdr->addr1) &&
1733             (ieee80211_is_mgmt(hdr->frame_control) ||
1734              ieee80211_is_data(hdr->frame_control)) &&
1735             !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1736             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1737              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1738                 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1739                         if (!ieee80211_has_pm(hdr->frame_control))
1740                                 sta_ps_end(sta);
1741                 } else {
1742                         if (ieee80211_has_pm(hdr->frame_control))
1743                                 sta_ps_start(sta);
1744                 }
1745         }
1746
1747         /* mesh power save support */
1748         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1749                 ieee80211_mps_rx_h_sta_process(sta, hdr);
1750
1751         /*
1752          * Drop (qos-)data::nullfunc frames silently, since they
1753          * are used only to control station power saving mode.
1754          */
1755         if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1756                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1757
1758                 /*
1759                  * If we receive a 4-addr nullfunc frame from a STA
1760                  * that was not moved to a 4-addr STA vlan yet send
1761                  * the event to userspace and for older hostapd drop
1762                  * the frame to the monitor interface.
1763                  */
1764                 if (ieee80211_has_a4(hdr->frame_control) &&
1765                     (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1766                      (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1767                       !rx->sdata->u.vlan.sta))) {
1768                         if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1769                                 cfg80211_rx_unexpected_4addr_frame(
1770                                         rx->sdata->dev, sta->sta.addr,
1771                                         GFP_ATOMIC);
1772                         return RX_DROP_MONITOR;
1773                 }
1774                 /*
1775                  * Update counter and free packet here to avoid
1776                  * counting this as a dropped packed.
1777                  */
1778                 sta->rx_stats.packets++;
1779                 dev_kfree_skb(rx->skb);
1780                 return RX_QUEUED;
1781         }
1782
1783         return RX_CONTINUE;
1784 } /* ieee80211_rx_h_sta_process */
1785
1786 static ieee80211_rx_result debug_noinline
1787 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1788 {
1789         struct sk_buff *skb = rx->skb;
1790         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1791         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1792         int keyidx;
1793         int hdrlen;
1794         ieee80211_rx_result result = RX_DROP_UNUSABLE;
1795         struct ieee80211_key *sta_ptk = NULL;
1796         int mmie_keyidx = -1;
1797         __le16 fc;
1798         const struct ieee80211_cipher_scheme *cs = NULL;
1799
1800         /*
1801          * Key selection 101
1802          *
1803          * There are four types of keys:
1804          *  - GTK (group keys)
1805          *  - IGTK (group keys for management frames)
1806          *  - PTK (pairwise keys)
1807          *  - STK (station-to-station pairwise keys)
1808          *
1809          * When selecting a key, we have to distinguish between multicast
1810          * (including broadcast) and unicast frames, the latter can only
1811          * use PTKs and STKs while the former always use GTKs and IGTKs.
1812          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1813          * unicast frames can also use key indices like GTKs. Hence, if we
1814          * don't have a PTK/STK we check the key index for a WEP key.
1815          *
1816          * Note that in a regular BSS, multicast frames are sent by the
1817          * AP only, associated stations unicast the frame to the AP first
1818          * which then multicasts it on their behalf.
1819          *
1820          * There is also a slight problem in IBSS mode: GTKs are negotiated
1821          * with each station, that is something we don't currently handle.
1822          * The spec seems to expect that one negotiates the same key with
1823          * every station but there's no such requirement; VLANs could be
1824          * possible.
1825          */
1826
1827         /* start without a key */
1828         rx->key = NULL;
1829         fc = hdr->frame_control;
1830
1831         if (rx->sta) {
1832                 int keyid = rx->sta->ptk_idx;
1833
1834                 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1835                         cs = rx->sta->cipher_scheme;
1836                         keyid = ieee80211_get_cs_keyid(cs, rx->skb);
1837                         if (unlikely(keyid < 0))
1838                                 return RX_DROP_UNUSABLE;
1839                 }
1840                 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1841         }
1842
1843         if (!ieee80211_has_protected(fc))
1844                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1845
1846         if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1847                 rx->key = sta_ptk;
1848                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1849                     (status->flag & RX_FLAG_IV_STRIPPED))
1850                         return RX_CONTINUE;
1851                 /* Skip decryption if the frame is not protected. */
1852                 if (!ieee80211_has_protected(fc))
1853                         return RX_CONTINUE;
1854         } else if (mmie_keyidx >= 0) {
1855                 /* Broadcast/multicast robust management frame / BIP */
1856                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1857                     (status->flag & RX_FLAG_IV_STRIPPED))
1858                         return RX_CONTINUE;
1859
1860                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1861                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1862                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1863                 if (rx->sta) {
1864                         if (ieee80211_is_group_privacy_action(skb) &&
1865                             test_sta_flag(rx->sta, WLAN_STA_MFP))
1866                                 return RX_DROP_MONITOR;
1867
1868                         rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1869                 }
1870                 if (!rx->key)
1871                         rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1872         } else if (!ieee80211_has_protected(fc)) {
1873                 /*
1874                  * The frame was not protected, so skip decryption. However, we
1875                  * need to set rx->key if there is a key that could have been
1876                  * used so that the frame may be dropped if encryption would
1877                  * have been expected.
1878                  */
1879                 struct ieee80211_key *key = NULL;
1880                 struct ieee80211_sub_if_data *sdata = rx->sdata;
1881                 int i;
1882
1883                 if (ieee80211_is_mgmt(fc) &&
1884                     is_multicast_ether_addr(hdr->addr1) &&
1885                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1886                         rx->key = key;
1887                 else {
1888                         if (rx->sta) {
1889                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1890                                         key = rcu_dereference(rx->sta->gtk[i]);
1891                                         if (key)
1892                                                 break;
1893                                 }
1894                         }
1895                         if (!key) {
1896                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1897                                         key = rcu_dereference(sdata->keys[i]);
1898                                         if (key)
1899                                                 break;
1900                                 }
1901                         }
1902                         if (key)
1903                                 rx->key = key;
1904                 }
1905                 return RX_CONTINUE;
1906         } else {
1907                 u8 keyid;
1908
1909                 /*
1910                  * The device doesn't give us the IV so we won't be
1911                  * able to look up the key. That's ok though, we
1912                  * don't need to decrypt the frame, we just won't
1913                  * be able to keep statistics accurate.
1914                  * Except for key threshold notifications, should
1915                  * we somehow allow the driver to tell us which key
1916                  * the hardware used if this flag is set?
1917                  */
1918                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1919                     (status->flag & RX_FLAG_IV_STRIPPED))
1920                         return RX_CONTINUE;
1921
1922                 hdrlen = ieee80211_hdrlen(fc);
1923
1924                 if (cs) {
1925                         keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
1926
1927                         if (unlikely(keyidx < 0))
1928                                 return RX_DROP_UNUSABLE;
1929                 } else {
1930                         if (rx->skb->len < 8 + hdrlen)
1931                                 return RX_DROP_UNUSABLE; /* TODO: count this? */
1932                         /*
1933                          * no need to call ieee80211_wep_get_keyidx,
1934                          * it verifies a bunch of things we've done already
1935                          */
1936                         skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1937                         keyidx = keyid >> 6;
1938                 }
1939
1940                 /* check per-station GTK first, if multicast packet */
1941                 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1942                         rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1943
1944                 /* if not found, try default key */
1945                 if (!rx->key) {
1946                         rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1947
1948                         /*
1949                          * RSNA-protected unicast frames should always be
1950                          * sent with pairwise or station-to-station keys,
1951                          * but for WEP we allow using a key index as well.
1952                          */
1953                         if (rx->key &&
1954                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1955                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1956                             !is_multicast_ether_addr(hdr->addr1))
1957                                 rx->key = NULL;
1958                 }
1959         }
1960
1961         if (rx->key) {
1962                 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1963                         return RX_DROP_MONITOR;
1964
1965                 /* TODO: add threshold stuff again */
1966         } else {
1967                 return RX_DROP_MONITOR;
1968         }
1969
1970         switch (rx->key->conf.cipher) {
1971         case WLAN_CIPHER_SUITE_WEP40:
1972         case WLAN_CIPHER_SUITE_WEP104:
1973                 result = ieee80211_crypto_wep_decrypt(rx);
1974                 break;
1975         case WLAN_CIPHER_SUITE_TKIP:
1976                 result = ieee80211_crypto_tkip_decrypt(rx);
1977                 break;
1978         case WLAN_CIPHER_SUITE_CCMP:
1979                 result = ieee80211_crypto_ccmp_decrypt(
1980                         rx, IEEE80211_CCMP_MIC_LEN);
1981                 break;
1982         case WLAN_CIPHER_SUITE_CCMP_256:
1983                 result = ieee80211_crypto_ccmp_decrypt(
1984                         rx, IEEE80211_CCMP_256_MIC_LEN);
1985                 break;
1986         case WLAN_CIPHER_SUITE_AES_CMAC:
1987                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1988                 break;
1989         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1990                 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1991                 break;
1992         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1993         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1994                 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1995                 break;
1996         case WLAN_CIPHER_SUITE_GCMP:
1997         case WLAN_CIPHER_SUITE_GCMP_256:
1998                 result = ieee80211_crypto_gcmp_decrypt(rx);
1999                 break;
2000         default:
2001                 result = ieee80211_crypto_hw_decrypt(rx);
2002         }
2003
2004         /* the hdr variable is invalid after the decrypt handlers */
2005
2006         /* either the frame has been decrypted or will be dropped */
2007         status->flag |= RX_FLAG_DECRYPTED;
2008
2009         return result;
2010 }
2011
2012 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2013 {
2014         int i;
2015
2016         for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2017                 skb_queue_head_init(&cache->entries[i].skb_list);
2018 }
2019
2020 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2021 {
2022         int i;
2023
2024         for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2025                 __skb_queue_purge(&cache->entries[i].skb_list);
2026 }
2027
2028 static inline struct ieee80211_fragment_entry *
2029 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
2030                          unsigned int frag, unsigned int seq, int rx_queue,
2031                          struct sk_buff **skb)
2032 {
2033         struct ieee80211_fragment_entry *entry;
2034
2035         entry = &cache->entries[cache->next++];
2036         if (cache->next >= IEEE80211_FRAGMENT_MAX)
2037                 cache->next = 0;
2038
2039         __skb_queue_purge(&entry->skb_list);
2040
2041         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2042         *skb = NULL;
2043         entry->first_frag_time = jiffies;
2044         entry->seq = seq;
2045         entry->rx_queue = rx_queue;
2046         entry->last_frag = frag;
2047         entry->check_sequential_pn = false;
2048         entry->extra_len = 0;
2049
2050         return entry;
2051 }
2052
2053 static inline struct ieee80211_fragment_entry *
2054 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2055                           unsigned int frag, unsigned int seq,
2056                           int rx_queue, struct ieee80211_hdr *hdr)
2057 {
2058         struct ieee80211_fragment_entry *entry;
2059         int i, idx;
2060
2061         idx = cache->next;
2062         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2063                 struct ieee80211_hdr *f_hdr;
2064
2065                 idx--;
2066                 if (idx < 0)
2067                         idx = IEEE80211_FRAGMENT_MAX - 1;
2068
2069                 entry = &cache->entries[idx];
2070                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2071                     entry->rx_queue != rx_queue ||
2072                     entry->last_frag + 1 != frag)
2073                         continue;
2074
2075                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
2076
2077                 /*
2078                  * Check ftype and addresses are equal, else check next fragment
2079                  */
2080                 if (((hdr->frame_control ^ f_hdr->frame_control) &
2081                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2082                     !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2083                     !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2084                         continue;
2085
2086                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2087                         __skb_queue_purge(&entry->skb_list);
2088                         continue;
2089                 }
2090                 return entry;
2091         }
2092
2093         return NULL;
2094 }
2095
2096 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2097 {
2098         return rx->key &&
2099                 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2100                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2101                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2102                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2103                 ieee80211_has_protected(fc);
2104 }
2105
2106 static ieee80211_rx_result debug_noinline
2107 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2108 {
2109         struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2110         struct ieee80211_hdr *hdr;
2111         u16 sc;
2112         __le16 fc;
2113         unsigned int frag, seq;
2114         struct ieee80211_fragment_entry *entry;
2115         struct sk_buff *skb;
2116         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2117
2118         hdr = (struct ieee80211_hdr *)rx->skb->data;
2119         fc = hdr->frame_control;
2120
2121         if (ieee80211_is_ctl(fc))
2122                 return RX_CONTINUE;
2123
2124         sc = le16_to_cpu(hdr->seq_ctrl);
2125         frag = sc & IEEE80211_SCTL_FRAG;
2126
2127         if (rx->sta)
2128                 cache = &rx->sta->frags;
2129
2130         if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2131                 goto out;
2132
2133         if (is_multicast_ether_addr(hdr->addr1))
2134                 return RX_DROP_MONITOR;
2135
2136         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2137
2138         if (skb_linearize(rx->skb))
2139                 return RX_DROP_UNUSABLE;
2140
2141         /*
2142          *  skb_linearize() might change the skb->data and
2143          *  previously cached variables (in this case, hdr) need to
2144          *  be refreshed with the new data.
2145          */
2146         hdr = (struct ieee80211_hdr *)rx->skb->data;
2147         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2148
2149         if (frag == 0) {
2150                 /* This is the first fragment of a new frame. */
2151                 entry = ieee80211_reassemble_add(cache, frag, seq,
2152                                                  rx->seqno_idx, &(rx->skb));
2153                 if (requires_sequential_pn(rx, fc)) {
2154                         int queue = rx->security_idx;
2155
2156                         /* Store CCMP/GCMP PN so that we can verify that the
2157                          * next fragment has a sequential PN value.
2158                          */
2159                         entry->check_sequential_pn = true;
2160                         entry->is_protected = true;
2161                         entry->key_color = rx->key->color;
2162                         memcpy(entry->last_pn,
2163                                rx->key->u.ccmp.rx_pn[queue],
2164                                IEEE80211_CCMP_PN_LEN);
2165                         BUILD_BUG_ON(offsetof(struct ieee80211_key,
2166                                               u.ccmp.rx_pn) !=
2167                                      offsetof(struct ieee80211_key,
2168                                               u.gcmp.rx_pn));
2169                         BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2170                                      sizeof(rx->key->u.gcmp.rx_pn[queue]));
2171                         BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2172                                      IEEE80211_GCMP_PN_LEN);
2173                 } else if (rx->key &&
2174                            (ieee80211_has_protected(fc) ||
2175                             (status->flag & RX_FLAG_DECRYPTED))) {
2176                         entry->is_protected = true;
2177                         entry->key_color = rx->key->color;
2178                 }
2179                 return RX_QUEUED;
2180         }
2181
2182         /* This is a fragment for a frame that should already be pending in
2183          * fragment cache. Add this fragment to the end of the pending entry.
2184          */
2185         entry = ieee80211_reassemble_find(cache, frag, seq,
2186                                           rx->seqno_idx, hdr);
2187         if (!entry) {
2188                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2189                 return RX_DROP_MONITOR;
2190         }
2191
2192         /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2193          *  MPDU PN values are not incrementing in steps of 1."
2194          * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2195          * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2196          */
2197         if (entry->check_sequential_pn) {
2198                 int i;
2199                 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2200
2201                 if (!requires_sequential_pn(rx, fc))
2202                         return RX_DROP_UNUSABLE;
2203
2204                 /* Prevent mixed key and fragment cache attacks */
2205                 if (entry->key_color != rx->key->color)
2206                         return RX_DROP_UNUSABLE;
2207
2208                 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2209                 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2210                         pn[i]++;
2211                         if (pn[i])
2212                                 break;
2213                 }
2214
2215                 rpn = rx->ccm_gcm.pn;
2216                 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2217                         return RX_DROP_UNUSABLE;
2218                 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2219         } else if (entry->is_protected &&
2220                    (!rx->key ||
2221                     (!ieee80211_has_protected(fc) &&
2222                      !(status->flag & RX_FLAG_DECRYPTED)) ||
2223                     rx->key->color != entry->key_color)) {
2224                 /* Drop this as a mixed key or fragment cache attack, even
2225                  * if for TKIP Michael MIC should protect us, and WEP is a
2226                  * lost cause anyway.
2227                  */
2228                 return RX_DROP_UNUSABLE;
2229         } else if (entry->is_protected && rx->key &&
2230                    entry->key_color != rx->key->color &&
2231                    (status->flag & RX_FLAG_DECRYPTED)) {
2232                 return RX_DROP_UNUSABLE;
2233         }
2234
2235         skb_pull(rx->skb, ieee80211_hdrlen(fc));
2236         __skb_queue_tail(&entry->skb_list, rx->skb);
2237         entry->last_frag = frag;
2238         entry->extra_len += rx->skb->len;
2239         if (ieee80211_has_morefrags(fc)) {
2240                 rx->skb = NULL;
2241                 return RX_QUEUED;
2242         }
2243
2244         rx->skb = __skb_dequeue(&entry->skb_list);
2245         if (skb_tailroom(rx->skb) < entry->extra_len) {
2246                 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2247                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2248                                               GFP_ATOMIC))) {
2249                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2250                         __skb_queue_purge(&entry->skb_list);
2251                         return RX_DROP_UNUSABLE;
2252                 }
2253         }
2254         while ((skb = __skb_dequeue(&entry->skb_list))) {
2255                 skb_put_data(rx->skb, skb->data, skb->len);
2256                 dev_kfree_skb(skb);
2257         }
2258
2259  out:
2260         ieee80211_led_rx(rx->local);
2261         if (rx->sta)
2262                 rx->sta->rx_stats.packets++;
2263         return RX_CONTINUE;
2264 }
2265
2266 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2267 {
2268         if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2269                 return -EACCES;
2270
2271         return 0;
2272 }
2273
2274 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2275 {
2276         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2277         struct sk_buff *skb = rx->skb;
2278         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2279
2280         /*
2281          * Pass through unencrypted frames if the hardware has
2282          * decrypted them already.
2283          */
2284         if (status->flag & RX_FLAG_DECRYPTED)
2285                 return 0;
2286
2287         /* check mesh EAPOL frames first */
2288         if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2289                      ieee80211_is_data(fc))) {
2290                 struct ieee80211s_hdr *mesh_hdr;
2291                 u16 hdr_len = ieee80211_hdrlen(fc);
2292                 u16 ethertype_offset;
2293                 __be16 ethertype;
2294
2295                 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2296                         goto drop_check;
2297
2298                 /* make sure fixed part of mesh header is there, also checks skb len */
2299                 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2300                         goto drop_check;
2301
2302                 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2303                 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2304                                    sizeof(rfc1042_header);
2305
2306                 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2307                     ethertype == rx->sdata->control_port_protocol)
2308                         return 0;
2309         }
2310
2311 drop_check:
2312         /* Drop unencrypted frames if key is set. */
2313         if (unlikely(!ieee80211_has_protected(fc) &&
2314                      !ieee80211_is_any_nullfunc(fc) &&
2315                      ieee80211_is_data(fc) && rx->key))
2316                 return -EACCES;
2317
2318         return 0;
2319 }
2320
2321 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2322 {
2323         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2324         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2325         __le16 fc = hdr->frame_control;
2326
2327         /*
2328          * Pass through unencrypted frames if the hardware has
2329          * decrypted them already.
2330          */
2331         if (status->flag & RX_FLAG_DECRYPTED)
2332                 return 0;
2333
2334         if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2335                 if (unlikely(!ieee80211_has_protected(fc) &&
2336                              ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2337                              rx->key)) {
2338                         if (ieee80211_is_deauth(fc) ||
2339                             ieee80211_is_disassoc(fc))
2340                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2341                                                              rx->skb->data,
2342                                                              rx->skb->len);
2343                         return -EACCES;
2344                 }
2345                 /* BIP does not use Protected field, so need to check MMIE */
2346                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2347                              ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2348                         if (ieee80211_is_deauth(fc) ||
2349                             ieee80211_is_disassoc(fc))
2350                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2351                                                              rx->skb->data,
2352                                                              rx->skb->len);
2353                         return -EACCES;
2354                 }
2355                 /*
2356                  * When using MFP, Action frames are not allowed prior to
2357                  * having configured keys.
2358                  */
2359                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2360                              ieee80211_is_robust_mgmt_frame(rx->skb)))
2361                         return -EACCES;
2362         }
2363
2364         return 0;
2365 }
2366
2367 static int
2368 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2369 {
2370         struct ieee80211_sub_if_data *sdata = rx->sdata;
2371         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2372         bool check_port_control = false;
2373         struct ethhdr *ehdr;
2374         int ret;
2375
2376         *port_control = false;
2377         if (ieee80211_has_a4(hdr->frame_control) &&
2378             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2379                 return -1;
2380
2381         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2382             !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2383
2384                 if (!sdata->u.mgd.use_4addr)
2385                         return -1;
2386                 else
2387                         check_port_control = true;
2388         }
2389
2390         if (is_multicast_ether_addr(hdr->addr1) &&
2391             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2392                 return -1;
2393
2394         ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2395         if (ret < 0)
2396                 return ret;
2397
2398         ehdr = (struct ethhdr *) rx->skb->data;
2399         if (ehdr->h_proto == rx->sdata->control_port_protocol)
2400                 *port_control = true;
2401         else if (check_port_control)
2402                 return -1;
2403
2404         return 0;
2405 }
2406
2407 /*
2408  * requires that rx->skb is a frame with ethernet header
2409  */
2410 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2411 {
2412         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2413                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2414         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2415
2416         /*
2417          * Allow EAPOL frames to us/the PAE group address regardless of
2418          * whether the frame was encrypted or not, and always disallow
2419          * all other destination addresses for them.
2420          */
2421         if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2422                 return ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2423                        ether_addr_equal(ehdr->h_dest, pae_group_addr);
2424
2425         if (ieee80211_802_1x_port_control(rx) ||
2426             ieee80211_drop_unencrypted(rx, fc))
2427                 return false;
2428
2429         return true;
2430 }
2431
2432 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2433                                                  struct ieee80211_rx_data *rx)
2434 {
2435         struct ieee80211_sub_if_data *sdata = rx->sdata;
2436         struct net_device *dev = sdata->dev;
2437
2438         if (unlikely((skb->protocol == sdata->control_port_protocol ||
2439                       skb->protocol == cpu_to_be16(ETH_P_PREAUTH)) &&
2440                      sdata->control_port_over_nl80211)) {
2441                 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2442                 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2443
2444                 cfg80211_rx_control_port(dev, skb, noencrypt);
2445                 dev_kfree_skb(skb);
2446         } else {
2447                 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2448
2449                 memset(skb->cb, 0, sizeof(skb->cb));
2450
2451                 /*
2452                  * 802.1X over 802.11 requires that the authenticator address
2453                  * be used for EAPOL frames. However, 802.1X allows the use of
2454                  * the PAE group address instead. If the interface is part of
2455                  * a bridge and we pass the frame with the PAE group address,
2456                  * then the bridge will forward it to the network (even if the
2457                  * client was not associated yet), which isn't supposed to
2458                  * happen.
2459                  * To avoid that, rewrite the destination address to our own
2460                  * address, so that the authenticator (e.g. hostapd) will see
2461                  * the frame, but bridge won't forward it anywhere else. Note
2462                  * that due to earlier filtering, the only other address can
2463                  * be the PAE group address.
2464                  */
2465                 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2466                              !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2467                         ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2468
2469                 /* deliver to local stack */
2470                 if (rx->napi)
2471                         napi_gro_receive(rx->napi, skb);
2472                 else
2473                         netif_receive_skb(skb);
2474         }
2475 }
2476
2477 /*
2478  * requires that rx->skb is a frame with ethernet header
2479  */
2480 static void
2481 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2482 {
2483         struct ieee80211_sub_if_data *sdata = rx->sdata;
2484         struct net_device *dev = sdata->dev;
2485         struct sk_buff *skb, *xmit_skb;
2486         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2487         struct sta_info *dsta;
2488
2489         skb = rx->skb;
2490         xmit_skb = NULL;
2491
2492         ieee80211_rx_stats(dev, skb->len);
2493
2494         if (rx->sta) {
2495                 /* The seqno index has the same property as needed
2496                  * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2497                  * for non-QoS-data frames. Here we know it's a data
2498                  * frame, so count MSDUs.
2499                  */
2500                 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2501                 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2502                 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2503         }
2504
2505         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2506              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2507             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2508             ehdr->h_proto != rx->sdata->control_port_protocol &&
2509             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2510                 if (is_multicast_ether_addr(ehdr->h_dest) &&
2511                     ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2512                         /*
2513                          * send multicast frames both to higher layers in
2514                          * local net stack and back to the wireless medium
2515                          */
2516                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
2517                         if (!xmit_skb)
2518                                 net_info_ratelimited("%s: failed to clone multicast frame\n",
2519                                                     dev->name);
2520                 } else if (!is_multicast_ether_addr(ehdr->h_dest)) {
2521                         dsta = sta_info_get(sdata, skb->data);
2522                         if (dsta) {
2523                                 /*
2524                                  * The destination station is associated to
2525                                  * this AP (in this VLAN), so send the frame
2526                                  * directly to it and do not pass it to local
2527                                  * net stack.
2528                                  */
2529                                 xmit_skb = skb;
2530                                 skb = NULL;
2531                         }
2532                 }
2533         }
2534
2535 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2536         if (skb) {
2537                 /* 'align' will only take the values 0 or 2 here since all
2538                  * frames are required to be aligned to 2-byte boundaries
2539                  * when being passed to mac80211; the code here works just
2540                  * as well if that isn't true, but mac80211 assumes it can
2541                  * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2542                  */
2543                 int align;
2544
2545                 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2546                 if (align) {
2547                         if (WARN_ON(skb_headroom(skb) < 3)) {
2548                                 dev_kfree_skb(skb);
2549                                 skb = NULL;
2550                         } else {
2551                                 u8 *data = skb->data;
2552                                 size_t len = skb_headlen(skb);
2553                                 skb->data -= align;
2554                                 memmove(skb->data, data, len);
2555                                 skb_set_tail_pointer(skb, len);
2556                         }
2557                 }
2558         }
2559 #endif
2560
2561         if (skb) {
2562                 skb->protocol = eth_type_trans(skb, dev);
2563                 ieee80211_deliver_skb_to_local_stack(skb, rx);
2564         }
2565
2566         if (xmit_skb) {
2567                 /*
2568                  * Send to wireless media and increase priority by 256 to
2569                  * keep the received priority instead of reclassifying
2570                  * the frame (see cfg80211_classify8021d).
2571                  */
2572                 xmit_skb->priority += 256;
2573                 xmit_skb->protocol = htons(ETH_P_802_3);
2574                 skb_reset_network_header(xmit_skb);
2575                 skb_reset_mac_header(xmit_skb);
2576                 dev_queue_xmit(xmit_skb);
2577         }
2578 }
2579
2580 static ieee80211_rx_result debug_noinline
2581 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
2582 {
2583         struct net_device *dev = rx->sdata->dev;
2584         struct sk_buff *skb = rx->skb;
2585         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2586         __le16 fc = hdr->frame_control;
2587         struct sk_buff_head frame_list;
2588         struct ethhdr ethhdr;
2589         const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2590
2591         if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2592                 check_da = NULL;
2593                 check_sa = NULL;
2594         } else switch (rx->sdata->vif.type) {
2595                 case NL80211_IFTYPE_AP:
2596                 case NL80211_IFTYPE_AP_VLAN:
2597                         check_da = NULL;
2598                         break;
2599                 case NL80211_IFTYPE_STATION:
2600                         if (!rx->sta ||
2601                             !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2602                                 check_sa = NULL;
2603                         break;
2604                 case NL80211_IFTYPE_MESH_POINT:
2605                         check_sa = NULL;
2606                         break;
2607                 default:
2608                         break;
2609         }
2610
2611         skb->dev = dev;
2612         __skb_queue_head_init(&frame_list);
2613
2614         if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2615                                           rx->sdata->vif.addr,
2616                                           rx->sdata->vif.type,
2617                                           data_offset, true))
2618                 return RX_DROP_UNUSABLE;
2619
2620         ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2621                                  rx->sdata->vif.type,
2622                                  rx->local->hw.extra_tx_headroom,
2623                                  check_da, check_sa);
2624
2625         while (!skb_queue_empty(&frame_list)) {
2626                 rx->skb = __skb_dequeue(&frame_list);
2627
2628                 if (!ieee80211_frame_allowed(rx, fc)) {
2629                         dev_kfree_skb(rx->skb);
2630                         continue;
2631                 }
2632
2633                 ieee80211_deliver_skb(rx);
2634         }
2635
2636         return RX_QUEUED;
2637 }
2638
2639 static ieee80211_rx_result debug_noinline
2640 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2641 {
2642         struct sk_buff *skb = rx->skb;
2643         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2644         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2645         __le16 fc = hdr->frame_control;
2646
2647         if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2648                 return RX_CONTINUE;
2649
2650         if (unlikely(!ieee80211_is_data(fc)))
2651                 return RX_CONTINUE;
2652
2653         if (unlikely(!ieee80211_is_data_present(fc)))
2654                 return RX_DROP_MONITOR;
2655
2656         if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2657                 switch (rx->sdata->vif.type) {
2658                 case NL80211_IFTYPE_AP_VLAN:
2659                         if (!rx->sdata->u.vlan.sta)
2660                                 return RX_DROP_UNUSABLE;
2661                         break;
2662                 case NL80211_IFTYPE_STATION:
2663                         if (!rx->sdata->u.mgd.use_4addr)
2664                                 return RX_DROP_UNUSABLE;
2665                         break;
2666                 default:
2667                         return RX_DROP_UNUSABLE;
2668                 }
2669         }
2670
2671         if (is_multicast_ether_addr(hdr->addr1))
2672                 return RX_DROP_UNUSABLE;
2673
2674         if (rx->key) {
2675                 /*
2676                  * We should not receive A-MSDUs on pre-HT connections,
2677                  * and HT connections cannot use old ciphers. Thus drop
2678                  * them, as in those cases we couldn't even have SPP
2679                  * A-MSDUs or such.
2680                  */
2681                 switch (rx->key->conf.cipher) {
2682                 case WLAN_CIPHER_SUITE_WEP40:
2683                 case WLAN_CIPHER_SUITE_WEP104:
2684                 case WLAN_CIPHER_SUITE_TKIP:
2685                         return RX_DROP_UNUSABLE;
2686                 default:
2687                         break;
2688                 }
2689         }
2690
2691         return __ieee80211_rx_h_amsdu(rx, 0);
2692 }
2693
2694 #ifdef CONFIG_MAC80211_MESH
2695 static ieee80211_rx_result
2696 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2697 {
2698         struct ieee80211_hdr *fwd_hdr, *hdr;
2699         struct ieee80211_tx_info *info;
2700         struct ieee80211s_hdr *mesh_hdr;
2701         struct sk_buff *skb = rx->skb, *fwd_skb;
2702         struct ieee80211_local *local = rx->local;
2703         struct ieee80211_sub_if_data *sdata = rx->sdata;
2704         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2705         u16 ac, q, hdrlen;
2706         int tailroom = 0;
2707
2708         hdr = (struct ieee80211_hdr *) skb->data;
2709         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2710
2711         /* make sure fixed part of mesh header is there, also checks skb len */
2712         if (!pskb_may_pull(rx->skb, hdrlen + 6))
2713                 return RX_DROP_MONITOR;
2714
2715         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2716
2717         /* make sure full mesh header is there, also checks skb len */
2718         if (!pskb_may_pull(rx->skb,
2719                            hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2720                 return RX_DROP_MONITOR;
2721
2722         /* reload pointers */
2723         hdr = (struct ieee80211_hdr *) skb->data;
2724         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2725
2726         if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2727                 return RX_DROP_MONITOR;
2728
2729         /* frame is in RMC, don't forward */
2730         if (ieee80211_is_data(hdr->frame_control) &&
2731             is_multicast_ether_addr(hdr->addr1) &&
2732             mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2733                 return RX_DROP_MONITOR;
2734
2735         if (!ieee80211_is_data(hdr->frame_control))
2736                 return RX_CONTINUE;
2737
2738         if (!mesh_hdr->ttl)
2739                 return RX_DROP_MONITOR;
2740
2741         if (mesh_hdr->flags & MESH_FLAGS_AE) {
2742                 struct mesh_path *mppath;
2743                 char *proxied_addr;
2744                 char *mpp_addr;
2745
2746                 if (is_multicast_ether_addr(hdr->addr1)) {
2747                         mpp_addr = hdr->addr3;
2748                         proxied_addr = mesh_hdr->eaddr1;
2749                 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2750                             MESH_FLAGS_AE_A5_A6) {
2751                         /* has_a4 already checked in ieee80211_rx_mesh_check */
2752                         mpp_addr = hdr->addr4;
2753                         proxied_addr = mesh_hdr->eaddr2;
2754                 } else {
2755                         return RX_DROP_MONITOR;
2756                 }
2757
2758                 rcu_read_lock();
2759                 mppath = mpp_path_lookup(sdata, proxied_addr);
2760                 if (!mppath) {
2761                         mpp_path_add(sdata, proxied_addr, mpp_addr);
2762                 } else {
2763                         spin_lock_bh(&mppath->state_lock);
2764                         if (!ether_addr_equal(mppath->mpp, mpp_addr))
2765                                 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2766                         mppath->exp_time = jiffies;
2767                         spin_unlock_bh(&mppath->state_lock);
2768                 }
2769                 rcu_read_unlock();
2770         }
2771
2772         /* Frame has reached destination.  Don't forward */
2773         if (!is_multicast_ether_addr(hdr->addr1) &&
2774             ether_addr_equal(sdata->vif.addr, hdr->addr3))
2775                 return RX_CONTINUE;
2776
2777         ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2778         q = sdata->vif.hw_queue[ac];
2779         if (ieee80211_queue_stopped(&local->hw, q)) {
2780                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2781                 return RX_DROP_MONITOR;
2782         }
2783         skb_set_queue_mapping(skb, q);
2784
2785         if (!--mesh_hdr->ttl) {
2786                 if (!is_multicast_ether_addr(hdr->addr1))
2787                         IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2788                                                      dropped_frames_ttl);
2789                 goto out;
2790         }
2791
2792         if (!ifmsh->mshcfg.dot11MeshForwarding)
2793                 goto out;
2794
2795         if (sdata->crypto_tx_tailroom_needed_cnt)
2796                 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2797
2798         fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2799                                        sdata->encrypt_headroom,
2800                                   tailroom, GFP_ATOMIC);
2801         if (!fwd_skb)
2802                 goto out;
2803
2804         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
2805         fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2806         info = IEEE80211_SKB_CB(fwd_skb);
2807         memset(info, 0, sizeof(*info));
2808         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2809         info->control.vif = &rx->sdata->vif;
2810         info->control.jiffies = jiffies;
2811         if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2812                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2813                 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2814                 /* update power mode indication when forwarding */
2815                 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2816         } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2817                 /* mesh power mode flags updated in mesh_nexthop_lookup */
2818                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2819         } else {
2820                 /* unable to resolve next hop */
2821                 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2822                                    fwd_hdr->addr3, 0,
2823                                    WLAN_REASON_MESH_PATH_NOFORWARD,
2824                                    fwd_hdr->addr2);
2825                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2826                 kfree_skb(fwd_skb);
2827                 return RX_DROP_MONITOR;
2828         }
2829
2830         IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2831         ieee80211_add_pending_skb(local, fwd_skb);
2832  out:
2833         if (is_multicast_ether_addr(hdr->addr1))
2834                 return RX_CONTINUE;
2835         return RX_DROP_MONITOR;
2836 }
2837 #endif
2838
2839 static ieee80211_rx_result debug_noinline
2840 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2841 {
2842         struct ieee80211_sub_if_data *sdata = rx->sdata;
2843         struct ieee80211_local *local = rx->local;
2844         struct net_device *dev = sdata->dev;
2845         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2846         __le16 fc = hdr->frame_control;
2847         bool port_control;
2848         int err;
2849
2850         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2851                 return RX_CONTINUE;
2852
2853         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2854                 return RX_DROP_MONITOR;
2855
2856         /*
2857          * Send unexpected-4addr-frame event to hostapd. For older versions,
2858          * also drop the frame to cooked monitor interfaces.
2859          */
2860         if (ieee80211_has_a4(hdr->frame_control) &&
2861             sdata->vif.type == NL80211_IFTYPE_AP) {
2862                 if (rx->sta &&
2863                     !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2864                         cfg80211_rx_unexpected_4addr_frame(
2865                                 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2866                 return RX_DROP_MONITOR;
2867         }
2868
2869         err = __ieee80211_data_to_8023(rx, &port_control);
2870         if (unlikely(err))
2871                 return RX_DROP_UNUSABLE;
2872
2873         if (!ieee80211_frame_allowed(rx, fc))
2874                 return RX_DROP_MONITOR;
2875
2876         /* directly handle TDLS channel switch requests/responses */
2877         if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2878                                                 cpu_to_be16(ETH_P_TDLS))) {
2879                 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2880
2881                 if (pskb_may_pull(rx->skb,
2882                                   offsetof(struct ieee80211_tdls_data, u)) &&
2883                     tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2884                     tf->category == WLAN_CATEGORY_TDLS &&
2885                     (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2886                      tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2887                         skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2888                         schedule_work(&local->tdls_chsw_work);
2889                         if (rx->sta)
2890                                 rx->sta->rx_stats.packets++;
2891
2892                         return RX_QUEUED;
2893                 }
2894         }
2895
2896         if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2897             unlikely(port_control) && sdata->bss) {
2898                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2899                                      u.ap);
2900                 dev = sdata->dev;
2901                 rx->sdata = sdata;
2902         }
2903
2904         rx->skb->dev = dev;
2905
2906         if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2907             local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2908             !is_multicast_ether_addr(
2909                     ((struct ethhdr *)rx->skb->data)->h_dest) &&
2910             (!local->scanning &&
2911              !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2912                 mod_timer(&local->dynamic_ps_timer, jiffies +
2913                           msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2914
2915         ieee80211_deliver_skb(rx);
2916
2917         return RX_QUEUED;
2918 }
2919
2920 static ieee80211_rx_result debug_noinline
2921 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2922 {
2923         struct sk_buff *skb = rx->skb;
2924         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2925         struct tid_ampdu_rx *tid_agg_rx;
2926         u16 start_seq_num;
2927         u16 tid;
2928
2929         if (likely(!ieee80211_is_ctl(bar->frame_control)))
2930                 return RX_CONTINUE;
2931
2932         if (ieee80211_is_back_req(bar->frame_control)) {
2933                 struct {
2934                         __le16 control, start_seq_num;
2935                 } __packed bar_data;
2936                 struct ieee80211_event event = {
2937                         .type = BAR_RX_EVENT,
2938                 };
2939
2940                 if (!rx->sta)
2941                         return RX_DROP_MONITOR;
2942
2943                 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2944                                   &bar_data, sizeof(bar_data)))
2945                         return RX_DROP_MONITOR;
2946
2947                 tid = le16_to_cpu(bar_data.control) >> 12;
2948
2949                 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2950                     !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2951                         ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2952                                              WLAN_BACK_RECIPIENT,
2953                                              WLAN_REASON_QSTA_REQUIRE_SETUP);
2954
2955                 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2956                 if (!tid_agg_rx)
2957                         return RX_DROP_MONITOR;
2958
2959                 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2960                 event.u.ba.tid = tid;
2961                 event.u.ba.ssn = start_seq_num;
2962                 event.u.ba.sta = &rx->sta->sta;
2963
2964                 /* reset session timer */
2965                 if (tid_agg_rx->timeout)
2966                         mod_timer(&tid_agg_rx->session_timer,
2967                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
2968
2969                 spin_lock(&tid_agg_rx->reorder_lock);
2970                 /* release stored frames up to start of BAR */
2971                 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2972                                                  start_seq_num, frames);
2973                 spin_unlock(&tid_agg_rx->reorder_lock);
2974
2975                 drv_event_callback(rx->local, rx->sdata, &event);
2976
2977                 kfree_skb(skb);
2978                 return RX_QUEUED;
2979         }
2980
2981         /*
2982          * After this point, we only want management frames,
2983          * so we can drop all remaining control frames to
2984          * cooked monitor interfaces.
2985          */
2986         return RX_DROP_MONITOR;
2987 }
2988
2989 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2990                                            struct ieee80211_mgmt *mgmt,
2991                                            size_t len)
2992 {
2993         struct ieee80211_local *local = sdata->local;
2994         struct sk_buff *skb;
2995         struct ieee80211_mgmt *resp;
2996
2997         if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2998                 /* Not to own unicast address */
2999                 return;
3000         }
3001
3002         if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3003             !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
3004                 /* Not from the current AP or not associated yet. */
3005                 return;
3006         }
3007
3008         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3009                 /* Too short SA Query request frame */
3010                 return;
3011         }
3012
3013         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3014         if (skb == NULL)
3015                 return;
3016
3017         skb_reserve(skb, local->hw.extra_tx_headroom);
3018         resp = skb_put_zero(skb, 24);
3019         memcpy(resp->da, mgmt->sa, ETH_ALEN);
3020         memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3021         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3022         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3023                                           IEEE80211_STYPE_ACTION);
3024         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3025         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3026         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3027         memcpy(resp->u.action.u.sa_query.trans_id,
3028                mgmt->u.action.u.sa_query.trans_id,
3029                WLAN_SA_QUERY_TR_ID_LEN);
3030
3031         ieee80211_tx_skb(sdata, skb);
3032 }
3033
3034 static ieee80211_rx_result debug_noinline
3035 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3036 {
3037         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3038         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3039
3040         /*
3041          * From here on, look only at management frames.
3042          * Data and control frames are already handled,
3043          * and unknown (reserved) frames are useless.
3044          */
3045         if (rx->skb->len < 24)
3046                 return RX_DROP_MONITOR;
3047
3048         if (!ieee80211_is_mgmt(mgmt->frame_control))
3049                 return RX_DROP_MONITOR;
3050
3051         if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3052             ieee80211_is_beacon(mgmt->frame_control) &&
3053             !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3054                 int sig = 0;
3055
3056                 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3057                     !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3058                         sig = status->signal;
3059
3060                 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
3061                                             rx->skb->data, rx->skb->len,
3062                                             status->freq, sig);
3063                 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3064         }
3065
3066         if (ieee80211_drop_unencrypted_mgmt(rx))
3067                 return RX_DROP_UNUSABLE;
3068
3069         return RX_CONTINUE;
3070 }
3071
3072 static ieee80211_rx_result debug_noinline
3073 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3074 {
3075         struct ieee80211_local *local = rx->local;
3076         struct ieee80211_sub_if_data *sdata = rx->sdata;
3077         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3078         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3079         int len = rx->skb->len;
3080
3081         if (!ieee80211_is_action(mgmt->frame_control))
3082                 return RX_CONTINUE;
3083
3084         /* drop too small frames */
3085         if (len < IEEE80211_MIN_ACTION_SIZE)
3086                 return RX_DROP_UNUSABLE;
3087
3088         if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3089             mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3090             mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3091                 return RX_DROP_UNUSABLE;
3092
3093         switch (mgmt->u.action.category) {
3094         case WLAN_CATEGORY_HT:
3095                 /* reject HT action frames from stations not supporting HT */
3096                 if (!rx->sta->sta.ht_cap.ht_supported)
3097                         goto invalid;
3098
3099                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3100                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3101                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3102                     sdata->vif.type != NL80211_IFTYPE_AP &&
3103                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3104                         break;
3105
3106                 /* verify action & smps_control/chanwidth are present */
3107                 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3108                         goto invalid;
3109
3110                 switch (mgmt->u.action.u.ht_smps.action) {
3111                 case WLAN_HT_ACTION_SMPS: {
3112                         struct ieee80211_supported_band *sband;
3113                         enum ieee80211_smps_mode smps_mode;
3114                         struct sta_opmode_info sta_opmode = {};
3115
3116                         /* convert to HT capability */
3117                         switch (mgmt->u.action.u.ht_smps.smps_control) {
3118                         case WLAN_HT_SMPS_CONTROL_DISABLED:
3119                                 smps_mode = IEEE80211_SMPS_OFF;
3120                                 break;
3121                         case WLAN_HT_SMPS_CONTROL_STATIC:
3122                                 smps_mode = IEEE80211_SMPS_STATIC;
3123                                 break;
3124                         case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3125                                 smps_mode = IEEE80211_SMPS_DYNAMIC;
3126                                 break;
3127                         default:
3128                                 goto invalid;
3129                         }
3130
3131                         /* if no change do nothing */
3132                         if (rx->sta->sta.smps_mode == smps_mode)
3133                                 goto handled;
3134                         rx->sta->sta.smps_mode = smps_mode;
3135                         sta_opmode.smps_mode =
3136                                 ieee80211_smps_mode_to_smps_mode(smps_mode);
3137                         sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3138
3139                         sband = rx->local->hw.wiphy->bands[status->band];
3140
3141                         rate_control_rate_update(local, sband, rx->sta,
3142                                                  IEEE80211_RC_SMPS_CHANGED);
3143                         cfg80211_sta_opmode_change_notify(sdata->dev,
3144                                                           rx->sta->addr,
3145                                                           &sta_opmode,
3146                                                           GFP_ATOMIC);
3147                         goto handled;
3148                 }
3149                 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3150                         struct ieee80211_supported_band *sband;
3151                         u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3152                         enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3153                         struct sta_opmode_info sta_opmode = {};
3154
3155                         /* If it doesn't support 40 MHz it can't change ... */
3156                         if (!(rx->sta->sta.ht_cap.cap &
3157                                         IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3158                                 goto handled;
3159
3160                         if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3161                                 max_bw = IEEE80211_STA_RX_BW_20;
3162                         else
3163                                 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3164
3165                         /* set cur_max_bandwidth and recalc sta bw */
3166                         rx->sta->cur_max_bandwidth = max_bw;
3167                         new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
3168
3169                         if (rx->sta->sta.bandwidth == new_bw)
3170                                 goto handled;
3171
3172                         rx->sta->sta.bandwidth = new_bw;
3173                         sband = rx->local->hw.wiphy->bands[status->band];
3174                         sta_opmode.bw =
3175                                 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
3176                         sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3177
3178                         rate_control_rate_update(local, sband, rx->sta,
3179                                                  IEEE80211_RC_BW_CHANGED);
3180                         cfg80211_sta_opmode_change_notify(sdata->dev,
3181                                                           rx->sta->addr,
3182                                                           &sta_opmode,
3183                                                           GFP_ATOMIC);
3184                         goto handled;
3185                 }
3186                 default:
3187                         goto invalid;
3188                 }
3189
3190                 break;
3191         case WLAN_CATEGORY_PUBLIC:
3192                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3193                         goto invalid;
3194                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3195                         break;
3196                 if (!rx->sta)
3197                         break;
3198                 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3199                         break;
3200                 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3201                                 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3202                         break;
3203                 if (len < offsetof(struct ieee80211_mgmt,
3204                                    u.action.u.ext_chan_switch.variable))
3205                         goto invalid;
3206                 goto queue;
3207         case WLAN_CATEGORY_VHT:
3208                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3209                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3210                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3211                     sdata->vif.type != NL80211_IFTYPE_AP &&
3212                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3213                         break;
3214
3215                 /* verify action code is present */
3216                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3217                         goto invalid;
3218
3219                 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3220                 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3221                         /* verify opmode is present */
3222                         if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3223                                 goto invalid;
3224                         goto queue;
3225                 }
3226                 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3227                         if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3228                                 goto invalid;
3229                         goto queue;
3230                 }
3231                 default:
3232                         break;
3233                 }
3234                 break;
3235         case WLAN_CATEGORY_BACK:
3236                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3237                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3238                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3239                     sdata->vif.type != NL80211_IFTYPE_AP &&
3240                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3241                         break;
3242
3243                 /* verify action_code is present */
3244                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3245                         break;
3246
3247                 switch (mgmt->u.action.u.addba_req.action_code) {
3248                 case WLAN_ACTION_ADDBA_REQ:
3249                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3250                                    sizeof(mgmt->u.action.u.addba_req)))
3251                                 goto invalid;
3252                         break;
3253                 case WLAN_ACTION_ADDBA_RESP:
3254                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3255                                    sizeof(mgmt->u.action.u.addba_resp)))
3256                                 goto invalid;
3257                         break;
3258                 case WLAN_ACTION_DELBA:
3259                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3260                                    sizeof(mgmt->u.action.u.delba)))
3261                                 goto invalid;
3262                         break;
3263                 default:
3264                         goto invalid;
3265                 }
3266
3267                 goto queue;
3268         case WLAN_CATEGORY_SPECTRUM_MGMT:
3269                 /* verify action_code is present */
3270                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3271                         break;
3272
3273                 switch (mgmt->u.action.u.measurement.action_code) {
3274                 case WLAN_ACTION_SPCT_MSR_REQ:
3275                         if (status->band != NL80211_BAND_5GHZ)
3276                                 break;
3277
3278                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3279                                    sizeof(mgmt->u.action.u.measurement)))
3280                                 break;
3281
3282                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3283                                 break;
3284
3285                         ieee80211_process_measurement_req(sdata, mgmt, len);
3286                         goto handled;
3287                 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3288                         u8 *bssid;
3289                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3290                                    sizeof(mgmt->u.action.u.chan_switch)))
3291                                 break;
3292
3293                         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3294                             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3295                             sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3296                                 break;
3297
3298                         if (sdata->vif.type == NL80211_IFTYPE_STATION)
3299                                 bssid = sdata->u.mgd.bssid;
3300                         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3301                                 bssid = sdata->u.ibss.bssid;
3302                         else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3303                                 bssid = mgmt->sa;
3304                         else
3305                                 break;
3306
3307                         if (!ether_addr_equal(mgmt->bssid, bssid))
3308                                 break;
3309
3310                         goto queue;
3311                         }
3312                 }
3313                 break;
3314         case WLAN_CATEGORY_SA_QUERY:
3315                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3316                            sizeof(mgmt->u.action.u.sa_query)))
3317                         break;
3318
3319                 switch (mgmt->u.action.u.sa_query.action) {
3320                 case WLAN_ACTION_SA_QUERY_REQUEST:
3321                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3322                                 break;
3323                         ieee80211_process_sa_query_req(sdata, mgmt, len);
3324                         goto handled;
3325                 }
3326                 break;
3327         case WLAN_CATEGORY_SELF_PROTECTED:
3328                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3329                            sizeof(mgmt->u.action.u.self_prot.action_code)))
3330                         break;
3331
3332                 switch (mgmt->u.action.u.self_prot.action_code) {
3333                 case WLAN_SP_MESH_PEERING_OPEN:
3334                 case WLAN_SP_MESH_PEERING_CLOSE:
3335                 case WLAN_SP_MESH_PEERING_CONFIRM:
3336                         if (!ieee80211_vif_is_mesh(&sdata->vif))
3337                                 goto invalid;
3338                         if (sdata->u.mesh.user_mpm)
3339                                 /* userspace handles this frame */
3340                                 break;
3341                         goto queue;
3342                 case WLAN_SP_MGK_INFORM:
3343                 case WLAN_SP_MGK_ACK:
3344                         if (!ieee80211_vif_is_mesh(&sdata->vif))
3345                                 goto invalid;
3346                         break;
3347                 }
3348                 break;
3349         case WLAN_CATEGORY_MESH_ACTION:
3350                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3351                            sizeof(mgmt->u.action.u.mesh_action.action_code)))
3352                         break;
3353
3354                 if (!ieee80211_vif_is_mesh(&sdata->vif))
3355                         break;
3356                 if (mesh_action_is_path_sel(mgmt) &&
3357                     !mesh_path_sel_is_hwmp(sdata))
3358                         break;
3359                 goto queue;
3360         }
3361
3362         return RX_CONTINUE;
3363
3364  invalid:
3365         status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3366         /* will return in the next handlers */
3367         return RX_CONTINUE;
3368
3369  handled:
3370         if (rx->sta)
3371                 rx->sta->rx_stats.packets++;
3372         dev_kfree_skb(rx->skb);
3373         return RX_QUEUED;
3374
3375  queue:
3376         skb_queue_tail(&sdata->skb_queue, rx->skb);
3377         ieee80211_queue_work(&local->hw, &sdata->work);
3378         if (rx->sta)
3379                 rx->sta->rx_stats.packets++;
3380         return RX_QUEUED;
3381 }
3382
3383 static ieee80211_rx_result debug_noinline
3384 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3385 {
3386         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3387         int sig = 0;
3388
3389         /* skip known-bad action frames and return them in the next handler */
3390         if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3391                 return RX_CONTINUE;
3392
3393         /*
3394          * Getting here means the kernel doesn't know how to handle
3395          * it, but maybe userspace does ... include returned frames
3396          * so userspace can register for those to know whether ones
3397          * it transmitted were processed or returned.
3398          */
3399
3400         if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3401             !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3402                 sig = status->signal;
3403
3404         if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
3405                              rx->skb->data, rx->skb->len, 0)) {
3406                 if (rx->sta)
3407                         rx->sta->rx_stats.packets++;
3408                 dev_kfree_skb(rx->skb);
3409                 return RX_QUEUED;
3410         }
3411
3412         return RX_CONTINUE;
3413 }
3414
3415 static ieee80211_rx_result debug_noinline
3416 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3417 {
3418         struct ieee80211_local *local = rx->local;
3419         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3420         struct sk_buff *nskb;
3421         struct ieee80211_sub_if_data *sdata = rx->sdata;
3422         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3423
3424         if (!ieee80211_is_action(mgmt->frame_control))
3425                 return RX_CONTINUE;
3426
3427         /*
3428          * For AP mode, hostapd is responsible for handling any action
3429          * frames that we didn't handle, including returning unknown
3430          * ones. For all other modes we will return them to the sender,
3431          * setting the 0x80 bit in the action category, as required by
3432          * 802.11-2012 9.24.4.
3433          * Newer versions of hostapd shall also use the management frame
3434          * registration mechanisms, but older ones still use cooked
3435          * monitor interfaces so push all frames there.
3436          */
3437         if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3438             (sdata->vif.type == NL80211_IFTYPE_AP ||
3439              sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3440                 return RX_DROP_MONITOR;
3441
3442         if (is_multicast_ether_addr(mgmt->da))
3443                 return RX_DROP_MONITOR;
3444
3445         /* do not return rejected action frames */
3446         if (mgmt->u.action.category & 0x80)
3447                 return RX_DROP_UNUSABLE;
3448
3449         nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3450                                GFP_ATOMIC);
3451         if (nskb) {
3452                 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3453
3454                 nmgmt->u.action.category |= 0x80;
3455                 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3456                 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3457
3458                 memset(nskb->cb, 0, sizeof(nskb->cb));
3459
3460                 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3461                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3462
3463                         info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3464                                       IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3465                                       IEEE80211_TX_CTL_NO_CCK_RATE;
3466                         if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3467                                 info->hw_queue =
3468                                         local->hw.offchannel_tx_hw_queue;
3469                 }
3470
3471                 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3472                                             status->band, 0);
3473         }
3474         dev_kfree_skb(rx->skb);
3475         return RX_QUEUED;
3476 }
3477
3478 static ieee80211_rx_result debug_noinline
3479 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3480 {
3481         struct ieee80211_sub_if_data *sdata = rx->sdata;
3482         struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3483         __le16 stype;
3484
3485         stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3486
3487         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3488             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3489             sdata->vif.type != NL80211_IFTYPE_OCB &&
3490             sdata->vif.type != NL80211_IFTYPE_STATION)
3491                 return RX_DROP_MONITOR;
3492
3493         switch (stype) {
3494         case cpu_to_le16(IEEE80211_STYPE_AUTH):
3495         case cpu_to_le16(IEEE80211_STYPE_BEACON):
3496         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3497                 /* process for all: mesh, mlme, ibss */
3498                 break;
3499         case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3500                 if (is_multicast_ether_addr(mgmt->da) &&
3501                     !is_broadcast_ether_addr(mgmt->da))
3502                         return RX_DROP_MONITOR;
3503
3504                 /* process only for station/IBSS */
3505                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3506                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3507                         return RX_DROP_MONITOR;
3508                 break;
3509         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3510         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3511         case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3512                 if (is_multicast_ether_addr(mgmt->da) &&
3513                     !is_broadcast_ether_addr(mgmt->da))
3514                         return RX_DROP_MONITOR;
3515
3516                 /* process only for station */
3517                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3518                         return RX_DROP_MONITOR;
3519                 break;
3520         case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3521                 /* process only for ibss and mesh */
3522                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3523                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3524                         return RX_DROP_MONITOR;
3525                 break;
3526         default:
3527                 return RX_DROP_MONITOR;
3528         }
3529
3530         /* queue up frame and kick off work to process it */
3531         skb_queue_tail(&sdata->skb_queue, rx->skb);
3532         ieee80211_queue_work(&rx->local->hw, &sdata->work);
3533         if (rx->sta)
3534                 rx->sta->rx_stats.packets++;
3535
3536         return RX_QUEUED;
3537 }
3538
3539 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3540                                         struct ieee80211_rate *rate)
3541 {
3542         struct ieee80211_sub_if_data *sdata;
3543         struct ieee80211_local *local = rx->local;
3544         struct sk_buff *skb = rx->skb, *skb2;
3545         struct net_device *prev_dev = NULL;
3546         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3547         int needed_headroom;
3548
3549         /*
3550          * If cooked monitor has been processed already, then
3551          * don't do it again. If not, set the flag.
3552          */
3553         if (rx->flags & IEEE80211_RX_CMNTR)
3554                 goto out_free_skb;
3555         rx->flags |= IEEE80211_RX_CMNTR;
3556
3557         /* If there are no cooked monitor interfaces, just free the SKB */
3558         if (!local->cooked_mntrs)
3559                 goto out_free_skb;
3560
3561         /* vendor data is long removed here */
3562         status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3563         /* room for the radiotap header based on driver features */
3564         needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3565
3566         if (skb_headroom(skb) < needed_headroom &&
3567             pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3568                 goto out_free_skb;
3569
3570         /* prepend radiotap information */
3571         ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3572                                          false);
3573
3574         skb_reset_mac_header(skb);
3575         skb->ip_summed = CHECKSUM_UNNECESSARY;
3576         skb->pkt_type = PACKET_OTHERHOST;
3577         skb->protocol = htons(ETH_P_802_2);
3578
3579         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3580                 if (!ieee80211_sdata_running(sdata))
3581                         continue;
3582
3583                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3584                     !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3585                         continue;
3586
3587                 if (prev_dev) {
3588                         skb2 = skb_clone(skb, GFP_ATOMIC);
3589                         if (skb2) {
3590                                 skb2->dev = prev_dev;
3591                                 netif_receive_skb(skb2);
3592                         }
3593                 }
3594
3595                 prev_dev = sdata->dev;
3596                 ieee80211_rx_stats(sdata->dev, skb->len);
3597         }
3598
3599         if (prev_dev) {
3600                 skb->dev = prev_dev;
3601                 netif_receive_skb(skb);
3602                 return;
3603         }
3604
3605  out_free_skb:
3606         dev_kfree_skb(skb);
3607 }
3608
3609 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3610                                          ieee80211_rx_result res)
3611 {
3612         switch (res) {
3613         case RX_DROP_MONITOR:
3614                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3615                 if (rx->sta)
3616                         rx->sta->rx_stats.dropped++;
3617                 /* fall through */
3618         case RX_CONTINUE: {
3619                 struct ieee80211_rate *rate = NULL;
3620                 struct ieee80211_supported_band *sband;
3621                 struct ieee80211_rx_status *status;
3622
3623                 status = IEEE80211_SKB_RXCB((rx->skb));
3624
3625                 sband = rx->local->hw.wiphy->bands[status->band];
3626                 if (status->encoding == RX_ENC_LEGACY)
3627                         rate = &sband->bitrates[status->rate_idx];
3628
3629                 ieee80211_rx_cooked_monitor(rx, rate);
3630                 break;
3631                 }
3632         case RX_DROP_UNUSABLE:
3633                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3634                 if (rx->sta)
3635                         rx->sta->rx_stats.dropped++;
3636                 dev_kfree_skb(rx->skb);
3637                 break;
3638         case RX_QUEUED:
3639                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3640                 break;
3641         }
3642 }
3643
3644 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3645                                   struct sk_buff_head *frames)
3646 {
3647         ieee80211_rx_result res = RX_DROP_MONITOR;
3648         struct sk_buff *skb;
3649
3650 #define CALL_RXH(rxh)                   \
3651         do {                            \
3652                 res = rxh(rx);          \
3653                 if (res != RX_CONTINUE) \
3654                         goto rxh_next;  \
3655         } while (0)
3656
3657         /* Lock here to avoid hitting all of the data used in the RX
3658          * path (e.g. key data, station data, ...) concurrently when
3659          * a frame is released from the reorder buffer due to timeout
3660          * from the timer, potentially concurrently with RX from the
3661          * driver.
3662          */
3663         spin_lock_bh(&rx->local->rx_path_lock);
3664
3665         while ((skb = __skb_dequeue(frames))) {
3666                 /*
3667                  * all the other fields are valid across frames
3668                  * that belong to an aMPDU since they are on the
3669                  * same TID from the same station
3670                  */
3671                 rx->skb = skb;
3672
3673                 CALL_RXH(ieee80211_rx_h_check_more_data);
3674                 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3675                 CALL_RXH(ieee80211_rx_h_sta_process);
3676                 CALL_RXH(ieee80211_rx_h_decrypt);
3677                 CALL_RXH(ieee80211_rx_h_defragment);
3678                 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3679                 /* must be after MMIC verify so header is counted in MPDU mic */
3680 #ifdef CONFIG_MAC80211_MESH
3681                 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3682                         CALL_RXH(ieee80211_rx_h_mesh_fwding);
3683 #endif
3684                 CALL_RXH(ieee80211_rx_h_amsdu);
3685                 CALL_RXH(ieee80211_rx_h_data);
3686
3687                 /* special treatment -- needs the queue */
3688                 res = ieee80211_rx_h_ctrl(rx, frames);
3689                 if (res != RX_CONTINUE)
3690                         goto rxh_next;
3691
3692                 CALL_RXH(ieee80211_rx_h_mgmt_check);
3693                 CALL_RXH(ieee80211_rx_h_action);
3694                 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3695                 CALL_RXH(ieee80211_rx_h_action_return);
3696                 CALL_RXH(ieee80211_rx_h_mgmt);
3697
3698  rxh_next:
3699                 ieee80211_rx_handlers_result(rx, res);
3700
3701 #undef CALL_RXH
3702         }
3703
3704         spin_unlock_bh(&rx->local->rx_path_lock);
3705 }
3706
3707 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3708 {
3709         struct sk_buff_head reorder_release;
3710         ieee80211_rx_result res = RX_DROP_MONITOR;
3711
3712         __skb_queue_head_init(&reorder_release);
3713
3714 #define CALL_RXH(rxh)                   \
3715         do {                            \
3716                 res = rxh(rx);          \
3717                 if (res != RX_CONTINUE) \
3718                         goto rxh_next;  \
3719         } while (0)
3720
3721         CALL_RXH(ieee80211_rx_h_check_dup);
3722         CALL_RXH(ieee80211_rx_h_check);
3723
3724         ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3725
3726         ieee80211_rx_handlers(rx, &reorder_release);
3727         return;
3728
3729  rxh_next:
3730         ieee80211_rx_handlers_result(rx, res);
3731
3732 #undef CALL_RXH
3733 }
3734
3735 /*
3736  * This function makes calls into the RX path, therefore
3737  * it has to be invoked under RCU read lock.
3738  */
3739 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3740 {
3741         struct sk_buff_head frames;
3742         struct ieee80211_rx_data rx = {
3743                 .sta = sta,
3744                 .sdata = sta->sdata,
3745                 .local = sta->local,
3746                 /* This is OK -- must be QoS data frame */
3747                 .security_idx = tid,
3748                 .seqno_idx = tid,
3749                 .napi = NULL, /* must be NULL to not have races */
3750         };
3751         struct tid_ampdu_rx *tid_agg_rx;
3752
3753         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3754         if (!tid_agg_rx)
3755                 return;
3756
3757         __skb_queue_head_init(&frames);
3758
3759         spin_lock(&tid_agg_rx->reorder_lock);
3760         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3761         spin_unlock(&tid_agg_rx->reorder_lock);
3762
3763         if (!skb_queue_empty(&frames)) {
3764                 struct ieee80211_event event = {
3765                         .type = BA_FRAME_TIMEOUT,
3766                         .u.ba.tid = tid,
3767                         .u.ba.sta = &sta->sta,
3768                 };
3769                 drv_event_callback(rx.local, rx.sdata, &event);
3770         }
3771
3772         ieee80211_rx_handlers(&rx, &frames);
3773 }
3774
3775 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3776                                           u16 ssn, u64 filtered,
3777                                           u16 received_mpdus)
3778 {
3779         struct sta_info *sta;
3780         struct tid_ampdu_rx *tid_agg_rx;
3781         struct sk_buff_head frames;
3782         struct ieee80211_rx_data rx = {
3783                 /* This is OK -- must be QoS data frame */
3784                 .security_idx = tid,
3785                 .seqno_idx = tid,
3786         };
3787         int i, diff;
3788
3789         if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3790                 return;
3791
3792         __skb_queue_head_init(&frames);
3793
3794         sta = container_of(pubsta, struct sta_info, sta);
3795
3796         rx.sta = sta;
3797         rx.sdata = sta->sdata;
3798         rx.local = sta->local;
3799
3800         rcu_read_lock();
3801         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3802         if (!tid_agg_rx)
3803                 goto out;
3804
3805         spin_lock_bh(&tid_agg_rx->reorder_lock);
3806
3807         if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3808                 int release;
3809
3810                 /* release all frames in the reorder buffer */
3811                 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3812                            IEEE80211_SN_MODULO;
3813                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3814                                                  release, &frames);
3815                 /* update ssn to match received ssn */
3816                 tid_agg_rx->head_seq_num = ssn;
3817         } else {
3818                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3819                                                  &frames);
3820         }
3821
3822         /* handle the case that received ssn is behind the mac ssn.
3823          * it can be tid_agg_rx->buf_size behind and still be valid */
3824         diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3825         if (diff >= tid_agg_rx->buf_size) {
3826                 tid_agg_rx->reorder_buf_filtered = 0;
3827                 goto release;
3828         }
3829         filtered = filtered >> diff;
3830         ssn += diff;
3831
3832         /* update bitmap */
3833         for (i = 0; i < tid_agg_rx->buf_size; i++) {
3834                 int index = (ssn + i) % tid_agg_rx->buf_size;
3835
3836                 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3837                 if (filtered & BIT_ULL(i))
3838                         tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3839         }
3840
3841         /* now process also frames that the filter marking released */
3842         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3843
3844 release:
3845         spin_unlock_bh(&tid_agg_rx->reorder_lock);
3846
3847         ieee80211_rx_handlers(&rx, &frames);
3848
3849  out:
3850         rcu_read_unlock();
3851 }
3852 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3853
3854 /* main receive path */
3855
3856 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3857 {
3858         struct ieee80211_sub_if_data *sdata = rx->sdata;
3859         struct sk_buff *skb = rx->skb;
3860         struct ieee80211_hdr *hdr = (void *)skb->data;
3861         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3862         u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3863         bool multicast = is_multicast_ether_addr(hdr->addr1);
3864
3865         switch (sdata->vif.type) {
3866         case NL80211_IFTYPE_STATION:
3867                 if (!bssid && !sdata->u.mgd.use_4addr)
3868                         return false;
3869                 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3870                         return false;
3871                 if (multicast)
3872                         return true;
3873                 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3874         case NL80211_IFTYPE_ADHOC:
3875                 if (!bssid)
3876                         return false;
3877                 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3878                     ether_addr_equal(sdata->u.ibss.bssid, 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 > 9 ||
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);