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