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