GNU Linux-libre 4.9.333-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_NOACK)
1183                 goto dont_reorder;
1184
1185         /* new, potentially un-ordered, ampdu frame - process it */
1186
1187         /* reset session timer */
1188         if (tid_agg_rx->timeout)
1189                 tid_agg_rx->last_rx = jiffies;
1190
1191         /* if this mpdu is fragmented - terminate rx aggregation session */
1192         sc = le16_to_cpu(hdr->seq_ctrl);
1193         if (sc & IEEE80211_SCTL_FRAG) {
1194                 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
1195                 skb_queue_tail(&rx->sdata->skb_queue, skb);
1196                 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1197                 return;
1198         }
1199
1200         /*
1201          * No locking needed -- we will only ever process one
1202          * RX packet at a time, and thus own tid_agg_rx. All
1203          * other code manipulating it needs to (and does) make
1204          * sure that we cannot get to it any more before doing
1205          * anything with it.
1206          */
1207         if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1208                                              frames))
1209                 return;
1210
1211  dont_reorder:
1212         __skb_queue_tail(frames, skb);
1213 }
1214
1215 static ieee80211_rx_result debug_noinline
1216 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1217 {
1218         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1219         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1220
1221         if (status->flag & RX_FLAG_DUP_VALIDATED)
1222                 return RX_CONTINUE;
1223
1224         /*
1225          * Drop duplicate 802.11 retransmissions
1226          * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1227          */
1228
1229         if (rx->skb->len < 24)
1230                 return RX_CONTINUE;
1231
1232         if (ieee80211_is_ctl(hdr->frame_control) ||
1233             ieee80211_is_any_nullfunc(hdr->frame_control) ||
1234             is_multicast_ether_addr(hdr->addr1))
1235                 return RX_CONTINUE;
1236
1237         if (!rx->sta)
1238                 return RX_CONTINUE;
1239
1240         if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1241                      rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1242                 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1243                 rx->sta->rx_stats.num_duplicates++;
1244                 return RX_DROP_UNUSABLE;
1245         } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1246                 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1247         }
1248
1249         return RX_CONTINUE;
1250 }
1251
1252 static ieee80211_rx_result debug_noinline
1253 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1254 {
1255         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1256
1257         /* Drop disallowed frame classes based on STA auth/assoc state;
1258          * IEEE 802.11, Chap 5.5.
1259          *
1260          * mac80211 filters only based on association state, i.e. it drops
1261          * Class 3 frames from not associated stations. hostapd sends
1262          * deauth/disassoc frames when needed. In addition, hostapd is
1263          * responsible for filtering on both auth and assoc states.
1264          */
1265
1266         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1267                 return ieee80211_rx_mesh_check(rx);
1268
1269         if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1270                       ieee80211_is_pspoll(hdr->frame_control)) &&
1271                      rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1272                      rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1273                      rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1274                      (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1275                 /*
1276                  * accept port control frames from the AP even when it's not
1277                  * yet marked ASSOC to prevent a race where we don't set the
1278                  * assoc bit quickly enough before it sends the first frame
1279                  */
1280                 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1281                     ieee80211_is_data_present(hdr->frame_control)) {
1282                         unsigned int hdrlen;
1283                         __be16 ethertype;
1284
1285                         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1286
1287                         if (rx->skb->len < hdrlen + 8)
1288                                 return RX_DROP_MONITOR;
1289
1290                         skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1291                         if (ethertype == rx->sdata->control_port_protocol)
1292                                 return RX_CONTINUE;
1293                 }
1294
1295                 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1296                     cfg80211_rx_spurious_frame(rx->sdata->dev,
1297                                                hdr->addr2,
1298                                                GFP_ATOMIC))
1299                         return RX_DROP_UNUSABLE;
1300
1301                 return RX_DROP_MONITOR;
1302         }
1303
1304         return RX_CONTINUE;
1305 }
1306
1307
1308 static ieee80211_rx_result debug_noinline
1309 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1310 {
1311         struct ieee80211_local *local;
1312         struct ieee80211_hdr *hdr;
1313         struct sk_buff *skb;
1314
1315         local = rx->local;
1316         skb = rx->skb;
1317         hdr = (struct ieee80211_hdr *) skb->data;
1318
1319         if (!local->pspolling)
1320                 return RX_CONTINUE;
1321
1322         if (!ieee80211_has_fromds(hdr->frame_control))
1323                 /* this is not from AP */
1324                 return RX_CONTINUE;
1325
1326         if (!ieee80211_is_data(hdr->frame_control))
1327                 return RX_CONTINUE;
1328
1329         if (!ieee80211_has_moredata(hdr->frame_control)) {
1330                 /* AP has no more frames buffered for us */
1331                 local->pspolling = false;
1332                 return RX_CONTINUE;
1333         }
1334
1335         /* more data bit is set, let's request a new frame from the AP */
1336         ieee80211_send_pspoll(local, rx->sdata);
1337
1338         return RX_CONTINUE;
1339 }
1340
1341 static void sta_ps_start(struct sta_info *sta)
1342 {
1343         struct ieee80211_sub_if_data *sdata = sta->sdata;
1344         struct ieee80211_local *local = sdata->local;
1345         struct ps_data *ps;
1346         int tid;
1347
1348         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1349             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1350                 ps = &sdata->bss->ps;
1351         else
1352                 return;
1353
1354         atomic_inc(&ps->num_sta_ps);
1355         set_sta_flag(sta, WLAN_STA_PS_STA);
1356         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1357                 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1358         ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1359                sta->sta.addr, sta->sta.aid);
1360
1361         ieee80211_clear_fast_xmit(sta);
1362
1363         if (!sta->sta.txq[0])
1364                 return;
1365
1366         for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1367                 if (txq_has_queue(sta->sta.txq[tid]))
1368                         set_bit(tid, &sta->txq_buffered_tids);
1369                 else
1370                         clear_bit(tid, &sta->txq_buffered_tids);
1371         }
1372 }
1373
1374 static void sta_ps_end(struct sta_info *sta)
1375 {
1376         ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1377                sta->sta.addr, sta->sta.aid);
1378
1379         if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1380                 /*
1381                  * Clear the flag only if the other one is still set
1382                  * so that the TX path won't start TX'ing new frames
1383                  * directly ... In the case that the driver flag isn't
1384                  * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1385                  */
1386                 clear_sta_flag(sta, WLAN_STA_PS_STA);
1387                 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1388                        sta->sta.addr, sta->sta.aid);
1389                 return;
1390         }
1391
1392         set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1393         clear_sta_flag(sta, WLAN_STA_PS_STA);
1394         ieee80211_sta_ps_deliver_wakeup(sta);
1395 }
1396
1397 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1398 {
1399         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1400         bool in_ps;
1401
1402         WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1403
1404         /* Don't let the same PS state be set twice */
1405         in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1406         if ((start && in_ps) || (!start && !in_ps))
1407                 return -EINVAL;
1408
1409         if (start)
1410                 sta_ps_start(sta);
1411         else
1412                 sta_ps_end(sta);
1413
1414         return 0;
1415 }
1416 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1417
1418 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1419 {
1420         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1421
1422         if (test_sta_flag(sta, WLAN_STA_SP))
1423                 return;
1424
1425         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1426                 ieee80211_sta_ps_deliver_poll_response(sta);
1427         else
1428                 set_sta_flag(sta, WLAN_STA_PSPOLL);
1429 }
1430 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1431
1432 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1433 {
1434         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1435         u8 ac = ieee802_1d_to_ac[tid & 7];
1436
1437         /*
1438          * If this AC is not trigger-enabled do nothing.
1439          *
1440          * NB: This could/should check a separate bitmap of trigger-
1441          * enabled queues, but for now we only implement uAPSD w/o
1442          * TSPEC changes to the ACs, so they're always the same.
1443          */
1444         if (!(sta->sta.uapsd_queues & BIT(ac)))
1445                 return;
1446
1447         /* if we are in a service period, do nothing */
1448         if (test_sta_flag(sta, WLAN_STA_SP))
1449                 return;
1450
1451         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1452                 ieee80211_sta_ps_deliver_uapsd(sta);
1453         else
1454                 set_sta_flag(sta, WLAN_STA_UAPSD);
1455 }
1456 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1457
1458 static ieee80211_rx_result debug_noinline
1459 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1460 {
1461         struct ieee80211_sub_if_data *sdata = rx->sdata;
1462         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1463         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1464
1465         if (!rx->sta)
1466                 return RX_CONTINUE;
1467
1468         if (sdata->vif.type != NL80211_IFTYPE_AP &&
1469             sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1470                 return RX_CONTINUE;
1471
1472         /*
1473          * The device handles station powersave, so don't do anything about
1474          * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1475          * it to mac80211 since they're handled.)
1476          */
1477         if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1478                 return RX_CONTINUE;
1479
1480         /*
1481          * Don't do anything if the station isn't already asleep. In
1482          * the uAPSD case, the station will probably be marked asleep,
1483          * in the PS-Poll case the station must be confused ...
1484          */
1485         if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1486                 return RX_CONTINUE;
1487
1488         if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1489                 ieee80211_sta_pspoll(&rx->sta->sta);
1490
1491                 /* Free PS Poll skb here instead of returning RX_DROP that would
1492                  * count as an dropped frame. */
1493                 dev_kfree_skb(rx->skb);
1494
1495                 return RX_QUEUED;
1496         } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1497                    !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1498                    ieee80211_has_pm(hdr->frame_control) &&
1499                    (ieee80211_is_data_qos(hdr->frame_control) ||
1500                     ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1501                 u8 tid;
1502
1503                 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1504
1505                 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1506         }
1507
1508         return RX_CONTINUE;
1509 }
1510
1511 static ieee80211_rx_result debug_noinline
1512 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1513 {
1514         struct sta_info *sta = rx->sta;
1515         struct sk_buff *skb = rx->skb;
1516         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1517         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1518         int i;
1519
1520         if (!sta)
1521                 return RX_CONTINUE;
1522
1523         /*
1524          * Update last_rx only for IBSS packets which are for the current
1525          * BSSID and for station already AUTHORIZED to avoid keeping the
1526          * current IBSS network alive in cases where other STAs start
1527          * using different BSSID. This will also give the station another
1528          * chance to restart the authentication/authorization in case
1529          * something went wrong the first time.
1530          */
1531         if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1532                 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1533                                                 NL80211_IFTYPE_ADHOC);
1534                 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1535                     test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1536                         sta->rx_stats.last_rx = jiffies;
1537                         if (ieee80211_is_data(hdr->frame_control) &&
1538                             !is_multicast_ether_addr(hdr->addr1))
1539                                 sta->rx_stats.last_rate =
1540                                         sta_stats_encode_rate(status);
1541                 }
1542         } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1543                 sta->rx_stats.last_rx = jiffies;
1544         } else if (!is_multicast_ether_addr(hdr->addr1)) {
1545                 /*
1546                  * Mesh beacons will update last_rx when if they are found to
1547                  * match the current local configuration when processed.
1548                  */
1549                 sta->rx_stats.last_rx = jiffies;
1550                 if (ieee80211_is_data(hdr->frame_control))
1551                         sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1552         }
1553
1554         if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1555                 ieee80211_sta_rx_notify(rx->sdata, hdr);
1556
1557         sta->rx_stats.fragments++;
1558
1559         u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1560         sta->rx_stats.bytes += rx->skb->len;
1561         u64_stats_update_end(&rx->sta->rx_stats.syncp);
1562
1563         if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1564                 sta->rx_stats.last_signal = status->signal;
1565                 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1566         }
1567
1568         if (status->chains) {
1569                 sta->rx_stats.chains = status->chains;
1570                 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1571                         int signal = status->chain_signal[i];
1572
1573                         if (!(status->chains & BIT(i)))
1574                                 continue;
1575
1576                         sta->rx_stats.chain_signal_last[i] = signal;
1577                         ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1578                                         -signal);
1579                 }
1580         }
1581
1582         /*
1583          * Change STA power saving mode only at the end of a frame
1584          * exchange sequence.
1585          */
1586         if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1587             !ieee80211_has_morefrags(hdr->frame_control) &&
1588             !ieee80211_is_back_req(hdr->frame_control) &&
1589             !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1590             (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1591              rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1592             /*
1593              * PM bit is only checked in frames where it isn't reserved,
1594              * in AP mode it's reserved in non-bufferable management frames
1595              * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1596              * BAR frames should be ignored as specified in
1597              * IEEE 802.11-2012 10.2.1.2.
1598              */
1599             (!ieee80211_is_mgmt(hdr->frame_control) ||
1600              ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
1601                 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1602                         if (!ieee80211_has_pm(hdr->frame_control))
1603                                 sta_ps_end(sta);
1604                 } else {
1605                         if (ieee80211_has_pm(hdr->frame_control))
1606                                 sta_ps_start(sta);
1607                 }
1608         }
1609
1610         /* mesh power save support */
1611         if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1612                 ieee80211_mps_rx_h_sta_process(sta, hdr);
1613
1614         /*
1615          * Drop (qos-)data::nullfunc frames silently, since they
1616          * are used only to control station power saving mode.
1617          */
1618         if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1619                 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1620
1621                 /*
1622                  * If we receive a 4-addr nullfunc frame from a STA
1623                  * that was not moved to a 4-addr STA vlan yet send
1624                  * the event to userspace and for older hostapd drop
1625                  * the frame to the monitor interface.
1626                  */
1627                 if (ieee80211_has_a4(hdr->frame_control) &&
1628                     (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1629                      (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1630                       !rx->sdata->u.vlan.sta))) {
1631                         if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1632                                 cfg80211_rx_unexpected_4addr_frame(
1633                                         rx->sdata->dev, sta->sta.addr,
1634                                         GFP_ATOMIC);
1635                         return RX_DROP_MONITOR;
1636                 }
1637                 /*
1638                  * Update counter and free packet here to avoid
1639                  * counting this as a dropped packed.
1640                  */
1641                 sta->rx_stats.packets++;
1642                 dev_kfree_skb(rx->skb);
1643                 return RX_QUEUED;
1644         }
1645
1646         return RX_CONTINUE;
1647 } /* ieee80211_rx_h_sta_process */
1648
1649 static ieee80211_rx_result debug_noinline
1650 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1651 {
1652         struct sk_buff *skb = rx->skb;
1653         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1654         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1655         int keyidx;
1656         int hdrlen;
1657         ieee80211_rx_result result = RX_DROP_UNUSABLE;
1658         struct ieee80211_key *sta_ptk = NULL;
1659         int mmie_keyidx = -1;
1660         __le16 fc;
1661         const struct ieee80211_cipher_scheme *cs = NULL;
1662
1663         /*
1664          * Key selection 101
1665          *
1666          * There are four types of keys:
1667          *  - GTK (group keys)
1668          *  - IGTK (group keys for management frames)
1669          *  - PTK (pairwise keys)
1670          *  - STK (station-to-station pairwise keys)
1671          *
1672          * When selecting a key, we have to distinguish between multicast
1673          * (including broadcast) and unicast frames, the latter can only
1674          * use PTKs and STKs while the former always use GTKs and IGTKs.
1675          * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1676          * unicast frames can also use key indices like GTKs. Hence, if we
1677          * don't have a PTK/STK we check the key index for a WEP key.
1678          *
1679          * Note that in a regular BSS, multicast frames are sent by the
1680          * AP only, associated stations unicast the frame to the AP first
1681          * which then multicasts it on their behalf.
1682          *
1683          * There is also a slight problem in IBSS mode: GTKs are negotiated
1684          * with each station, that is something we don't currently handle.
1685          * The spec seems to expect that one negotiates the same key with
1686          * every station but there's no such requirement; VLANs could be
1687          * possible.
1688          */
1689
1690         /* start without a key */
1691         rx->key = NULL;
1692         fc = hdr->frame_control;
1693
1694         if (rx->sta) {
1695                 int keyid = rx->sta->ptk_idx;
1696
1697                 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1698                         cs = rx->sta->cipher_scheme;
1699                         keyid = ieee80211_get_cs_keyid(cs, rx->skb);
1700                         if (unlikely(keyid < 0))
1701                                 return RX_DROP_UNUSABLE;
1702                 }
1703                 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1704         }
1705
1706         if (!ieee80211_has_protected(fc))
1707                 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1708
1709         if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1710                 rx->key = sta_ptk;
1711                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1712                     (status->flag & RX_FLAG_IV_STRIPPED))
1713                         return RX_CONTINUE;
1714                 /* Skip decryption if the frame is not protected. */
1715                 if (!ieee80211_has_protected(fc))
1716                         return RX_CONTINUE;
1717         } else if (mmie_keyidx >= 0) {
1718                 /* Broadcast/multicast robust management frame / BIP */
1719                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1720                     (status->flag & RX_FLAG_IV_STRIPPED))
1721                         return RX_CONTINUE;
1722
1723                 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1724                     mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1725                         return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1726                 if (rx->sta) {
1727                         if (ieee80211_is_group_privacy_action(skb) &&
1728                             test_sta_flag(rx->sta, WLAN_STA_MFP))
1729                                 return RX_DROP_MONITOR;
1730
1731                         rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1732                 }
1733                 if (!rx->key)
1734                         rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1735         } else if (!ieee80211_has_protected(fc)) {
1736                 /*
1737                  * The frame was not protected, so skip decryption. However, we
1738                  * need to set rx->key if there is a key that could have been
1739                  * used so that the frame may be dropped if encryption would
1740                  * have been expected.
1741                  */
1742                 struct ieee80211_key *key = NULL;
1743                 struct ieee80211_sub_if_data *sdata = rx->sdata;
1744                 int i;
1745
1746                 if (ieee80211_is_mgmt(fc) &&
1747                     is_multicast_ether_addr(hdr->addr1) &&
1748                     (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1749                         rx->key = key;
1750                 else {
1751                         if (rx->sta) {
1752                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1753                                         key = rcu_dereference(rx->sta->gtk[i]);
1754                                         if (key)
1755                                                 break;
1756                                 }
1757                         }
1758                         if (!key) {
1759                                 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1760                                         key = rcu_dereference(sdata->keys[i]);
1761                                         if (key)
1762                                                 break;
1763                                 }
1764                         }
1765                         if (key)
1766                                 rx->key = key;
1767                 }
1768                 return RX_CONTINUE;
1769         } else {
1770                 u8 keyid;
1771
1772                 /*
1773                  * The device doesn't give us the IV so we won't be
1774                  * able to look up the key. That's ok though, we
1775                  * don't need to decrypt the frame, we just won't
1776                  * be able to keep statistics accurate.
1777                  * Except for key threshold notifications, should
1778                  * we somehow allow the driver to tell us which key
1779                  * the hardware used if this flag is set?
1780                  */
1781                 if ((status->flag & RX_FLAG_DECRYPTED) &&
1782                     (status->flag & RX_FLAG_IV_STRIPPED))
1783                         return RX_CONTINUE;
1784
1785                 hdrlen = ieee80211_hdrlen(fc);
1786
1787                 if (cs) {
1788                         keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
1789
1790                         if (unlikely(keyidx < 0))
1791                                 return RX_DROP_UNUSABLE;
1792                 } else {
1793                         if (rx->skb->len < 8 + hdrlen)
1794                                 return RX_DROP_UNUSABLE; /* TODO: count this? */
1795                         /*
1796                          * no need to call ieee80211_wep_get_keyidx,
1797                          * it verifies a bunch of things we've done already
1798                          */
1799                         skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1800                         keyidx = keyid >> 6;
1801                 }
1802
1803                 /* check per-station GTK first, if multicast packet */
1804                 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1805                         rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1806
1807                 /* if not found, try default key */
1808                 if (!rx->key) {
1809                         rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1810
1811                         /*
1812                          * RSNA-protected unicast frames should always be
1813                          * sent with pairwise or station-to-station keys,
1814                          * but for WEP we allow using a key index as well.
1815                          */
1816                         if (rx->key &&
1817                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1818                             rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1819                             !is_multicast_ether_addr(hdr->addr1))
1820                                 rx->key = NULL;
1821                 }
1822         }
1823
1824         if (rx->key) {
1825                 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1826                         return RX_DROP_MONITOR;
1827
1828                 /* TODO: add threshold stuff again */
1829         } else {
1830                 return RX_DROP_MONITOR;
1831         }
1832
1833         switch (rx->key->conf.cipher) {
1834         case WLAN_CIPHER_SUITE_WEP40:
1835         case WLAN_CIPHER_SUITE_WEP104:
1836                 result = ieee80211_crypto_wep_decrypt(rx);
1837                 break;
1838         case WLAN_CIPHER_SUITE_TKIP:
1839                 result = ieee80211_crypto_tkip_decrypt(rx);
1840                 break;
1841         case WLAN_CIPHER_SUITE_CCMP:
1842                 result = ieee80211_crypto_ccmp_decrypt(
1843                         rx, IEEE80211_CCMP_MIC_LEN);
1844                 break;
1845         case WLAN_CIPHER_SUITE_CCMP_256:
1846                 result = ieee80211_crypto_ccmp_decrypt(
1847                         rx, IEEE80211_CCMP_256_MIC_LEN);
1848                 break;
1849         case WLAN_CIPHER_SUITE_AES_CMAC:
1850                 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1851                 break;
1852         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1853                 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1854                 break;
1855         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1856         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1857                 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1858                 break;
1859         case WLAN_CIPHER_SUITE_GCMP:
1860         case WLAN_CIPHER_SUITE_GCMP_256:
1861                 result = ieee80211_crypto_gcmp_decrypt(rx);
1862                 break;
1863         default:
1864                 result = ieee80211_crypto_hw_decrypt(rx);
1865         }
1866
1867         /* the hdr variable is invalid after the decrypt handlers */
1868
1869         /* either the frame has been decrypted or will be dropped */
1870         status->flag |= RX_FLAG_DECRYPTED;
1871
1872         return result;
1873 }
1874
1875 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
1876 {
1877         int i;
1878
1879         for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
1880                 skb_queue_head_init(&cache->entries[i].skb_list);
1881 }
1882
1883 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
1884 {
1885         int i;
1886
1887         for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
1888                 __skb_queue_purge(&cache->entries[i].skb_list);
1889 }
1890
1891 static inline struct ieee80211_fragment_entry *
1892 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
1893                          unsigned int frag, unsigned int seq, int rx_queue,
1894                          struct sk_buff **skb)
1895 {
1896         struct ieee80211_fragment_entry *entry;
1897
1898         entry = &cache->entries[cache->next++];
1899         if (cache->next >= IEEE80211_FRAGMENT_MAX)
1900                 cache->next = 0;
1901
1902         __skb_queue_purge(&entry->skb_list);
1903
1904         __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1905         *skb = NULL;
1906         entry->first_frag_time = jiffies;
1907         entry->seq = seq;
1908         entry->rx_queue = rx_queue;
1909         entry->last_frag = frag;
1910         entry->check_sequential_pn = false;
1911         entry->extra_len = 0;
1912
1913         return entry;
1914 }
1915
1916 static inline struct ieee80211_fragment_entry *
1917 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
1918                           unsigned int frag, unsigned int seq,
1919                           int rx_queue, struct ieee80211_hdr *hdr)
1920 {
1921         struct ieee80211_fragment_entry *entry;
1922         int i, idx;
1923
1924         idx = cache->next;
1925         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1926                 struct ieee80211_hdr *f_hdr;
1927
1928                 idx--;
1929                 if (idx < 0)
1930                         idx = IEEE80211_FRAGMENT_MAX - 1;
1931
1932                 entry = &cache->entries[idx];
1933                 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1934                     entry->rx_queue != rx_queue ||
1935                     entry->last_frag + 1 != frag)
1936                         continue;
1937
1938                 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1939
1940                 /*
1941                  * Check ftype and addresses are equal, else check next fragment
1942                  */
1943                 if (((hdr->frame_control ^ f_hdr->frame_control) &
1944                      cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1945                     !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1946                     !ether_addr_equal(hdr->addr2, f_hdr->addr2))
1947                         continue;
1948
1949                 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1950                         __skb_queue_purge(&entry->skb_list);
1951                         continue;
1952                 }
1953                 return entry;
1954         }
1955
1956         return NULL;
1957 }
1958
1959 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
1960 {
1961         return rx->key &&
1962                 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
1963                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
1964                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
1965                  rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
1966                 ieee80211_has_protected(fc);
1967 }
1968
1969 static ieee80211_rx_result debug_noinline
1970 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1971 {
1972         struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
1973         struct ieee80211_hdr *hdr;
1974         u16 sc;
1975         __le16 fc;
1976         unsigned int frag, seq;
1977         struct ieee80211_fragment_entry *entry;
1978         struct sk_buff *skb;
1979         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1980
1981         hdr = (struct ieee80211_hdr *)rx->skb->data;
1982         fc = hdr->frame_control;
1983
1984         if (ieee80211_is_ctl(fc))
1985                 return RX_CONTINUE;
1986
1987         sc = le16_to_cpu(hdr->seq_ctrl);
1988         frag = sc & IEEE80211_SCTL_FRAG;
1989
1990         if (rx->sta)
1991                 cache = &rx->sta->frags;
1992
1993         if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1994                 goto out;
1995
1996         if (is_multicast_ether_addr(hdr->addr1))
1997                 return RX_DROP_MONITOR;
1998
1999         I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2000
2001         if (skb_linearize(rx->skb))
2002                 return RX_DROP_UNUSABLE;
2003
2004         /*
2005          *  skb_linearize() might change the skb->data and
2006          *  previously cached variables (in this case, hdr) need to
2007          *  be refreshed with the new data.
2008          */
2009         hdr = (struct ieee80211_hdr *)rx->skb->data;
2010         seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2011
2012         if (frag == 0) {
2013                 /* This is the first fragment of a new frame. */
2014                 entry = ieee80211_reassemble_add(cache, frag, seq,
2015                                                  rx->seqno_idx, &(rx->skb));
2016                 if (requires_sequential_pn(rx, fc)) {
2017                         int queue = rx->security_idx;
2018
2019                         /* Store CCMP/GCMP PN so that we can verify that the
2020                          * next fragment has a sequential PN value.
2021                          */
2022                         entry->check_sequential_pn = true;
2023                         entry->is_protected = true;
2024                         entry->key_color = rx->key->color;
2025                         memcpy(entry->last_pn,
2026                                rx->key->u.ccmp.rx_pn[queue],
2027                                IEEE80211_CCMP_PN_LEN);
2028                         BUILD_BUG_ON(offsetof(struct ieee80211_key,
2029                                               u.ccmp.rx_pn) !=
2030                                      offsetof(struct ieee80211_key,
2031                                               u.gcmp.rx_pn));
2032                         BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2033                                      sizeof(rx->key->u.gcmp.rx_pn[queue]));
2034                         BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2035                                      IEEE80211_GCMP_PN_LEN);
2036                 } else if (rx->key &&
2037                            (ieee80211_has_protected(fc) ||
2038                             (status->flag & RX_FLAG_DECRYPTED))) {
2039                         entry->is_protected = true;
2040                         entry->key_color = rx->key->color;
2041                 }
2042                 return RX_QUEUED;
2043         }
2044
2045         /* This is a fragment for a frame that should already be pending in
2046          * fragment cache. Add this fragment to the end of the pending entry.
2047          */
2048         entry = ieee80211_reassemble_find(cache, frag, seq,
2049                                           rx->seqno_idx, hdr);
2050         if (!entry) {
2051                 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2052                 return RX_DROP_MONITOR;
2053         }
2054
2055         /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2056          *  MPDU PN values are not incrementing in steps of 1."
2057          * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2058          * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2059          */
2060         if (entry->check_sequential_pn) {
2061                 int i;
2062                 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2063
2064                 if (!requires_sequential_pn(rx, fc))
2065                         return RX_DROP_UNUSABLE;
2066
2067                 /* Prevent mixed key and fragment cache attacks */
2068                 if (entry->key_color != rx->key->color)
2069                         return RX_DROP_UNUSABLE;
2070
2071                 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2072                 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2073                         pn[i]++;
2074                         if (pn[i])
2075                                 break;
2076                 }
2077
2078                 rpn = rx->ccm_gcm.pn;
2079                 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2080                         return RX_DROP_UNUSABLE;
2081                 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2082         } else if (entry->is_protected &&
2083                    (!rx->key ||
2084                     (!ieee80211_has_protected(fc) &&
2085                      !(status->flag & RX_FLAG_DECRYPTED)) ||
2086                     rx->key->color != entry->key_color)) {
2087                 /* Drop this as a mixed key or fragment cache attack, even
2088                  * if for TKIP Michael MIC should protect us, and WEP is a
2089                  * lost cause anyway.
2090                  */
2091                 return RX_DROP_UNUSABLE;
2092         } else if (entry->is_protected && rx->key &&
2093                    entry->key_color != rx->key->color &&
2094                    (status->flag & RX_FLAG_DECRYPTED)) {
2095                 return RX_DROP_UNUSABLE;
2096         }
2097
2098         skb_pull(rx->skb, ieee80211_hdrlen(fc));
2099         __skb_queue_tail(&entry->skb_list, rx->skb);
2100         entry->last_frag = frag;
2101         entry->extra_len += rx->skb->len;
2102         if (ieee80211_has_morefrags(fc)) {
2103                 rx->skb = NULL;
2104                 return RX_QUEUED;
2105         }
2106
2107         rx->skb = __skb_dequeue(&entry->skb_list);
2108         if (skb_tailroom(rx->skb) < entry->extra_len) {
2109                 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2110                 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2111                                               GFP_ATOMIC))) {
2112                         I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2113                         __skb_queue_purge(&entry->skb_list);
2114                         return RX_DROP_UNUSABLE;
2115                 }
2116         }
2117         while ((skb = __skb_dequeue(&entry->skb_list))) {
2118                 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
2119                 dev_kfree_skb(skb);
2120         }
2121
2122         /* Complete frame has been reassembled - process it now */
2123         status = IEEE80211_SKB_RXCB(rx->skb);
2124
2125  out:
2126         ieee80211_led_rx(rx->local);
2127         if (rx->sta)
2128                 rx->sta->rx_stats.packets++;
2129         return RX_CONTINUE;
2130 }
2131
2132 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2133 {
2134         if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2135                 return -EACCES;
2136
2137         return 0;
2138 }
2139
2140 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2141 {
2142         struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2143         struct sk_buff *skb = rx->skb;
2144         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2145
2146         /*
2147          * Pass through unencrypted frames if the hardware has
2148          * decrypted them already.
2149          */
2150         if (status->flag & RX_FLAG_DECRYPTED)
2151                 return 0;
2152
2153         /* check mesh EAPOL frames first */
2154         if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2155                      ieee80211_is_data(fc))) {
2156                 struct ieee80211s_hdr *mesh_hdr;
2157                 u16 hdr_len = ieee80211_hdrlen(fc);
2158                 u16 ethertype_offset;
2159                 __be16 ethertype;
2160
2161                 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2162                         goto drop_check;
2163
2164                 /* make sure fixed part of mesh header is there, also checks skb len */
2165                 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2166                         goto drop_check;
2167
2168                 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2169                 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2170                                    sizeof(rfc1042_header);
2171
2172                 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2173                     ethertype == rx->sdata->control_port_protocol)
2174                         return 0;
2175         }
2176
2177 drop_check:
2178         /* Drop unencrypted frames if key is set. */
2179         if (unlikely(!ieee80211_has_protected(fc) &&
2180                      !ieee80211_is_any_nullfunc(fc) &&
2181                      ieee80211_is_data(fc) && rx->key))
2182                 return -EACCES;
2183
2184         return 0;
2185 }
2186
2187 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2188 {
2189         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2190         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2191         __le16 fc = hdr->frame_control;
2192
2193         /*
2194          * Pass through unencrypted frames if the hardware has
2195          * decrypted them already.
2196          */
2197         if (status->flag & RX_FLAG_DECRYPTED)
2198                 return 0;
2199
2200         if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2201                 if (unlikely(!ieee80211_has_protected(fc) &&
2202                              ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2203                              rx->key)) {
2204                         if (ieee80211_is_deauth(fc) ||
2205                             ieee80211_is_disassoc(fc))
2206                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2207                                                              rx->skb->data,
2208                                                              rx->skb->len);
2209                         return -EACCES;
2210                 }
2211                 /* BIP does not use Protected field, so need to check MMIE */
2212                 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2213                              ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2214                         if (ieee80211_is_deauth(fc) ||
2215                             ieee80211_is_disassoc(fc))
2216                                 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2217                                                              rx->skb->data,
2218                                                              rx->skb->len);
2219                         return -EACCES;
2220                 }
2221                 /*
2222                  * When using MFP, Action frames are not allowed prior to
2223                  * having configured keys.
2224                  */
2225                 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2226                              ieee80211_is_robust_mgmt_frame(rx->skb)))
2227                         return -EACCES;
2228         }
2229
2230         return 0;
2231 }
2232
2233 static int
2234 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2235 {
2236         struct ieee80211_sub_if_data *sdata = rx->sdata;
2237         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2238         bool check_port_control = false;
2239         struct ethhdr *ehdr;
2240         int ret;
2241
2242         *port_control = false;
2243         if (ieee80211_has_a4(hdr->frame_control) &&
2244             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2245                 return -1;
2246
2247         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2248             !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2249
2250                 if (!sdata->u.mgd.use_4addr)
2251                         return -1;
2252                 else
2253                         check_port_control = true;
2254         }
2255
2256         if (is_multicast_ether_addr(hdr->addr1) &&
2257             sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2258                 return -1;
2259
2260         ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2261         if (ret < 0)
2262                 return ret;
2263
2264         ehdr = (struct ethhdr *) rx->skb->data;
2265         if (ehdr->h_proto == rx->sdata->control_port_protocol)
2266                 *port_control = true;
2267         else if (check_port_control)
2268                 return -1;
2269
2270         return 0;
2271 }
2272
2273 /*
2274  * requires that rx->skb is a frame with ethernet header
2275  */
2276 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2277 {
2278         static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2279                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2280         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2281
2282         /*
2283          * Allow EAPOL frames to us/the PAE group address regardless of
2284          * whether the frame was encrypted or not, and always disallow
2285          * all other destination addresses for them.
2286          */
2287         if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2288                 return ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2289                        ether_addr_equal(ehdr->h_dest, pae_group_addr);
2290
2291         if (ieee80211_802_1x_port_control(rx) ||
2292             ieee80211_drop_unencrypted(rx, fc))
2293                 return false;
2294
2295         return true;
2296 }
2297
2298 /*
2299  * requires that rx->skb is a frame with ethernet header
2300  */
2301 static void
2302 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2303 {
2304         struct ieee80211_sub_if_data *sdata = rx->sdata;
2305         struct net_device *dev = sdata->dev;
2306         struct sk_buff *skb, *xmit_skb;
2307         struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2308         struct sta_info *dsta;
2309
2310         skb = rx->skb;
2311         xmit_skb = NULL;
2312
2313         ieee80211_rx_stats(dev, skb->len);
2314
2315         if (rx->sta) {
2316                 /* The seqno index has the same property as needed
2317                  * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2318                  * for non-QoS-data frames. Here we know it's a data
2319                  * frame, so count MSDUs.
2320                  */
2321                 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2322                 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2323                 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2324         }
2325
2326         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2327              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2328             !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2329             ehdr->h_proto != rx->sdata->control_port_protocol &&
2330             (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2331                 if (is_multicast_ether_addr(ehdr->h_dest)) {
2332                         /*
2333                          * send multicast frames both to higher layers in
2334                          * local net stack and back to the wireless medium
2335                          */
2336                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
2337                         if (!xmit_skb)
2338                                 net_info_ratelimited("%s: failed to clone multicast frame\n",
2339                                                     dev->name);
2340                 } else {
2341                         dsta = sta_info_get(sdata, skb->data);
2342                         if (dsta) {
2343                                 /*
2344                                  * The destination station is associated to
2345                                  * this AP (in this VLAN), so send the frame
2346                                  * directly to it and do not pass it to local
2347                                  * net stack.
2348                                  */
2349                                 xmit_skb = skb;
2350                                 skb = NULL;
2351                         }
2352                 }
2353         }
2354
2355 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2356         if (skb) {
2357                 /* 'align' will only take the values 0 or 2 here since all
2358                  * frames are required to be aligned to 2-byte boundaries
2359                  * when being passed to mac80211; the code here works just
2360                  * as well if that isn't true, but mac80211 assumes it can
2361                  * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2362                  */
2363                 int align;
2364
2365                 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2366                 if (align) {
2367                         if (WARN_ON(skb_headroom(skb) < 3)) {
2368                                 dev_kfree_skb(skb);
2369                                 skb = NULL;
2370                         } else {
2371                                 u8 *data = skb->data;
2372                                 size_t len = skb_headlen(skb);
2373                                 skb->data -= align;
2374                                 memmove(skb->data, data, len);
2375                                 skb_set_tail_pointer(skb, len);
2376                         }
2377                 }
2378         }
2379 #endif
2380
2381         if (skb) {
2382                 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
2383
2384                 /* deliver to local stack */
2385                 skb->protocol = eth_type_trans(skb, dev);
2386                 memset(skb->cb, 0, sizeof(skb->cb));
2387
2388                 /*
2389                  * 802.1X over 802.11 requires that the authenticator address
2390                  * be used for EAPOL frames. However, 802.1X allows the use of
2391                  * the PAE group address instead. If the interface is part of
2392                  * a bridge and we pass the frame with the PAE group address,
2393                  * then the bridge will forward it to the network (even if the
2394                  * client was not associated yet), which isn't supposed to
2395                  * happen.
2396                  * To avoid that, rewrite the destination address to our own
2397                  * address, so that the authenticator (e.g. hostapd) will see
2398                  * the frame, but bridge won't forward it anywhere else. Note
2399                  * that due to earlier filtering, the only other address can
2400                  * be the PAE group address.
2401                  */
2402                 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2403                              !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2404                         ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2405
2406                 if (rx->napi)
2407                         napi_gro_receive(rx->napi, skb);
2408                 else
2409                         netif_receive_skb(skb);
2410         }
2411
2412         if (xmit_skb) {
2413                 /*
2414                  * Send to wireless media and increase priority by 256 to
2415                  * keep the received priority instead of reclassifying
2416                  * the frame (see cfg80211_classify8021d).
2417                  */
2418                 xmit_skb->priority += 256;
2419                 xmit_skb->protocol = htons(ETH_P_802_3);
2420                 skb_reset_network_header(xmit_skb);
2421                 skb_reset_mac_header(xmit_skb);
2422                 dev_queue_xmit(xmit_skb);
2423         }
2424 }
2425
2426 static ieee80211_rx_result debug_noinline
2427 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2428 {
2429         struct net_device *dev = rx->sdata->dev;
2430         struct sk_buff *skb = rx->skb;
2431         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2432         __le16 fc = hdr->frame_control;
2433         struct sk_buff_head frame_list;
2434         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2435         struct ethhdr ethhdr;
2436         const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2437
2438         if (unlikely(!ieee80211_is_data(fc)))
2439                 return RX_CONTINUE;
2440
2441         if (unlikely(!ieee80211_is_data_present(fc)))
2442                 return RX_DROP_MONITOR;
2443
2444         if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2445                 return RX_CONTINUE;
2446
2447         if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2448                 switch (rx->sdata->vif.type) {
2449                 case NL80211_IFTYPE_AP_VLAN:
2450                         if (!rx->sdata->u.vlan.sta)
2451                                 return RX_DROP_UNUSABLE;
2452                         break;
2453                 case NL80211_IFTYPE_STATION:
2454                         if (!rx->sdata->u.mgd.use_4addr)
2455                                 return RX_DROP_UNUSABLE;
2456                         break;
2457                 default:
2458                         return RX_DROP_UNUSABLE;
2459                 }
2460                 check_da = NULL;
2461                 check_sa = NULL;
2462         } else switch (rx->sdata->vif.type) {
2463                 case NL80211_IFTYPE_AP:
2464                 case NL80211_IFTYPE_AP_VLAN:
2465                         check_da = NULL;
2466                         break;
2467                 case NL80211_IFTYPE_STATION:
2468                         if (!rx->sta ||
2469                             !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2470                                 check_sa = NULL;
2471                         break;
2472                 case NL80211_IFTYPE_MESH_POINT:
2473                         check_sa = NULL;
2474                         break;
2475                 default:
2476                         break;
2477         }
2478
2479         if (is_multicast_ether_addr(hdr->addr1))
2480                 return RX_DROP_UNUSABLE;
2481
2482         skb->dev = dev;
2483         __skb_queue_head_init(&frame_list);
2484
2485         if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2486                                           rx->sdata->vif.addr,
2487                                           rx->sdata->vif.type,
2488                                           true))
2489                 return RX_DROP_UNUSABLE;
2490
2491         if (rx->key) {
2492                 /*
2493                  * We should not receive A-MSDUs on pre-HT connections,
2494                  * and HT connections cannot use old ciphers. Thus drop
2495                  * them, as in those cases we couldn't even have SPP
2496                  * A-MSDUs or such.
2497                  */
2498                 switch (rx->key->conf.cipher) {
2499                 case WLAN_CIPHER_SUITE_WEP40:
2500                 case WLAN_CIPHER_SUITE_WEP104:
2501                 case WLAN_CIPHER_SUITE_TKIP:
2502                         return RX_DROP_UNUSABLE;
2503                 default:
2504                         break;
2505                 }
2506         }
2507
2508         ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2509                                  rx->sdata->vif.type,
2510                                  rx->local->hw.extra_tx_headroom,
2511                                  check_da, check_sa);
2512
2513         while (!skb_queue_empty(&frame_list)) {
2514                 rx->skb = __skb_dequeue(&frame_list);
2515
2516                 if (!ieee80211_frame_allowed(rx, fc)) {
2517                         dev_kfree_skb(rx->skb);
2518                         continue;
2519                 }
2520
2521                 ieee80211_deliver_skb(rx);
2522         }
2523
2524         return RX_QUEUED;
2525 }
2526
2527 #ifdef CONFIG_MAC80211_MESH
2528 static ieee80211_rx_result
2529 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2530 {
2531         struct ieee80211_hdr *fwd_hdr, *hdr;
2532         struct ieee80211_tx_info *info;
2533         struct ieee80211s_hdr *mesh_hdr;
2534         struct sk_buff *skb = rx->skb, *fwd_skb;
2535         struct ieee80211_local *local = rx->local;
2536         struct ieee80211_sub_if_data *sdata = rx->sdata;
2537         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2538         u16 ac, q, hdrlen;
2539
2540         hdr = (struct ieee80211_hdr *) skb->data;
2541         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2542
2543         /* make sure fixed part of mesh header is there, also checks skb len */
2544         if (!pskb_may_pull(rx->skb, hdrlen + 6))
2545                 return RX_DROP_MONITOR;
2546
2547         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2548
2549         /* make sure full mesh header is there, also checks skb len */
2550         if (!pskb_may_pull(rx->skb,
2551                            hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2552                 return RX_DROP_MONITOR;
2553
2554         /* reload pointers */
2555         hdr = (struct ieee80211_hdr *) skb->data;
2556         mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2557
2558         if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2559                 return RX_DROP_MONITOR;
2560
2561         /* frame is in RMC, don't forward */
2562         if (ieee80211_is_data(hdr->frame_control) &&
2563             is_multicast_ether_addr(hdr->addr1) &&
2564             mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2565                 return RX_DROP_MONITOR;
2566
2567         if (!ieee80211_is_data(hdr->frame_control))
2568                 return RX_CONTINUE;
2569
2570         if (!mesh_hdr->ttl)
2571                 return RX_DROP_MONITOR;
2572
2573         if (mesh_hdr->flags & MESH_FLAGS_AE) {
2574                 struct mesh_path *mppath;
2575                 char *proxied_addr;
2576                 char *mpp_addr;
2577
2578                 if (is_multicast_ether_addr(hdr->addr1)) {
2579                         mpp_addr = hdr->addr3;
2580                         proxied_addr = mesh_hdr->eaddr1;
2581                 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2582                             MESH_FLAGS_AE_A5_A6) {
2583                         /* has_a4 already checked in ieee80211_rx_mesh_check */
2584                         mpp_addr = hdr->addr4;
2585                         proxied_addr = mesh_hdr->eaddr2;
2586                 } else {
2587                         return RX_DROP_MONITOR;
2588                 }
2589
2590                 rcu_read_lock();
2591                 mppath = mpp_path_lookup(sdata, proxied_addr);
2592                 if (!mppath) {
2593                         mpp_path_add(sdata, proxied_addr, mpp_addr);
2594                 } else {
2595                         spin_lock_bh(&mppath->state_lock);
2596                         if (!ether_addr_equal(mppath->mpp, mpp_addr))
2597                                 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2598                         mppath->exp_time = jiffies;
2599                         spin_unlock_bh(&mppath->state_lock);
2600                 }
2601                 rcu_read_unlock();
2602         }
2603
2604         /* Frame has reached destination.  Don't forward */
2605         if (!is_multicast_ether_addr(hdr->addr1) &&
2606             ether_addr_equal(sdata->vif.addr, hdr->addr3))
2607                 return RX_CONTINUE;
2608
2609         ac = ieee802_1d_to_ac[skb->priority];
2610         q = sdata->vif.hw_queue[ac];
2611         if (ieee80211_queue_stopped(&local->hw, q)) {
2612                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2613                 return RX_DROP_MONITOR;
2614         }
2615         skb_set_queue_mapping(skb, ac);
2616
2617         if (!--mesh_hdr->ttl) {
2618                 if (!is_multicast_ether_addr(hdr->addr1))
2619                         IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2620                                                      dropped_frames_ttl);
2621                 goto out;
2622         }
2623
2624         if (!ifmsh->mshcfg.dot11MeshForwarding)
2625                 goto out;
2626
2627         fwd_skb = skb_copy(skb, GFP_ATOMIC);
2628         if (!fwd_skb) {
2629                 net_info_ratelimited("%s: failed to clone mesh frame\n",
2630                                     sdata->name);
2631                 goto out;
2632         }
2633
2634         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
2635         fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2636         info = IEEE80211_SKB_CB(fwd_skb);
2637         memset(info, 0, sizeof(*info));
2638         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2639         info->control.vif = &rx->sdata->vif;
2640         info->control.jiffies = jiffies;
2641         if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2642                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2643                 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2644                 /* update power mode indication when forwarding */
2645                 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2646         } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2647                 /* mesh power mode flags updated in mesh_nexthop_lookup */
2648                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2649         } else {
2650                 /* unable to resolve next hop */
2651                 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2652                                    fwd_hdr->addr3, 0,
2653                                    WLAN_REASON_MESH_PATH_NOFORWARD,
2654                                    fwd_hdr->addr2);
2655                 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2656                 kfree_skb(fwd_skb);
2657                 return RX_DROP_MONITOR;
2658         }
2659
2660         IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2661         ieee80211_add_pending_skb(local, fwd_skb);
2662  out:
2663         if (is_multicast_ether_addr(hdr->addr1))
2664                 return RX_CONTINUE;
2665         return RX_DROP_MONITOR;
2666 }
2667 #endif
2668
2669 static ieee80211_rx_result debug_noinline
2670 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2671 {
2672         struct ieee80211_sub_if_data *sdata = rx->sdata;
2673         struct ieee80211_local *local = rx->local;
2674         struct net_device *dev = sdata->dev;
2675         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2676         __le16 fc = hdr->frame_control;
2677         bool port_control;
2678         int err;
2679
2680         if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2681                 return RX_CONTINUE;
2682
2683         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2684                 return RX_DROP_MONITOR;
2685
2686         /*
2687          * Send unexpected-4addr-frame event to hostapd. For older versions,
2688          * also drop the frame to cooked monitor interfaces.
2689          */
2690         if (ieee80211_has_a4(hdr->frame_control) &&
2691             sdata->vif.type == NL80211_IFTYPE_AP) {
2692                 if (rx->sta &&
2693                     !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2694                         cfg80211_rx_unexpected_4addr_frame(
2695                                 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2696                 return RX_DROP_MONITOR;
2697         }
2698
2699         err = __ieee80211_data_to_8023(rx, &port_control);
2700         if (unlikely(err))
2701                 return RX_DROP_UNUSABLE;
2702
2703         if (!ieee80211_frame_allowed(rx, fc))
2704                 return RX_DROP_MONITOR;
2705
2706         /* directly handle TDLS channel switch requests/responses */
2707         if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2708                                                 cpu_to_be16(ETH_P_TDLS))) {
2709                 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2710
2711                 if (pskb_may_pull(rx->skb,
2712                                   offsetof(struct ieee80211_tdls_data, u)) &&
2713                     tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2714                     tf->category == WLAN_CATEGORY_TDLS &&
2715                     (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2716                      tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2717                         skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2718                         schedule_work(&local->tdls_chsw_work);
2719                         if (rx->sta)
2720                                 rx->sta->rx_stats.packets++;
2721
2722                         return RX_QUEUED;
2723                 }
2724         }
2725
2726         if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2727             unlikely(port_control) && sdata->bss) {
2728                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2729                                      u.ap);
2730                 dev = sdata->dev;
2731                 rx->sdata = sdata;
2732         }
2733
2734         rx->skb->dev = dev;
2735
2736         if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2737             local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2738             !is_multicast_ether_addr(
2739                     ((struct ethhdr *)rx->skb->data)->h_dest) &&
2740             (!local->scanning &&
2741              !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2742                 mod_timer(&local->dynamic_ps_timer, jiffies +
2743                           msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2744
2745         ieee80211_deliver_skb(rx);
2746
2747         return RX_QUEUED;
2748 }
2749
2750 static ieee80211_rx_result debug_noinline
2751 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2752 {
2753         struct sk_buff *skb = rx->skb;
2754         struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2755         struct tid_ampdu_rx *tid_agg_rx;
2756         u16 start_seq_num;
2757         u16 tid;
2758
2759         if (likely(!ieee80211_is_ctl(bar->frame_control)))
2760                 return RX_CONTINUE;
2761
2762         if (ieee80211_is_back_req(bar->frame_control)) {
2763                 struct {
2764                         __le16 control, start_seq_num;
2765                 } __packed bar_data;
2766                 struct ieee80211_event event = {
2767                         .type = BAR_RX_EVENT,
2768                 };
2769
2770                 if (!rx->sta)
2771                         return RX_DROP_MONITOR;
2772
2773                 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2774                                   &bar_data, sizeof(bar_data)))
2775                         return RX_DROP_MONITOR;
2776
2777                 tid = le16_to_cpu(bar_data.control) >> 12;
2778
2779                 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2780                     !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2781                         ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2782                                              WLAN_BACK_RECIPIENT,
2783                                              WLAN_REASON_QSTA_REQUIRE_SETUP);
2784
2785                 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2786                 if (!tid_agg_rx)
2787                         return RX_DROP_MONITOR;
2788
2789                 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2790                 event.u.ba.tid = tid;
2791                 event.u.ba.ssn = start_seq_num;
2792                 event.u.ba.sta = &rx->sta->sta;
2793
2794                 /* reset session timer */
2795                 if (tid_agg_rx->timeout)
2796                         mod_timer(&tid_agg_rx->session_timer,
2797                                   TU_TO_EXP_TIME(tid_agg_rx->timeout));
2798
2799                 spin_lock(&tid_agg_rx->reorder_lock);
2800                 /* release stored frames up to start of BAR */
2801                 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2802                                                  start_seq_num, frames);
2803                 spin_unlock(&tid_agg_rx->reorder_lock);
2804
2805                 drv_event_callback(rx->local, rx->sdata, &event);
2806
2807                 kfree_skb(skb);
2808                 return RX_QUEUED;
2809         }
2810
2811         /*
2812          * After this point, we only want management frames,
2813          * so we can drop all remaining control frames to
2814          * cooked monitor interfaces.
2815          */
2816         return RX_DROP_MONITOR;
2817 }
2818
2819 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2820                                            struct ieee80211_mgmt *mgmt,
2821                                            size_t len)
2822 {
2823         struct ieee80211_local *local = sdata->local;
2824         struct sk_buff *skb;
2825         struct ieee80211_mgmt *resp;
2826
2827         if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2828                 /* Not to own unicast address */
2829                 return;
2830         }
2831
2832         if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2833             !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
2834                 /* Not from the current AP or not associated yet. */
2835                 return;
2836         }
2837
2838         if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2839                 /* Too short SA Query request frame */
2840                 return;
2841         }
2842
2843         skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2844         if (skb == NULL)
2845                 return;
2846
2847         skb_reserve(skb, local->hw.extra_tx_headroom);
2848         resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2849         memset(resp, 0, 24);
2850         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2851         memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2852         memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2853         resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2854                                           IEEE80211_STYPE_ACTION);
2855         skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2856         resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2857         resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2858         memcpy(resp->u.action.u.sa_query.trans_id,
2859                mgmt->u.action.u.sa_query.trans_id,
2860                WLAN_SA_QUERY_TR_ID_LEN);
2861
2862         ieee80211_tx_skb(sdata, skb);
2863 }
2864
2865 static ieee80211_rx_result debug_noinline
2866 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2867 {
2868         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2869         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2870
2871         /*
2872          * From here on, look only at management frames.
2873          * Data and control frames are already handled,
2874          * and unknown (reserved) frames are useless.
2875          */
2876         if (rx->skb->len < 24)
2877                 return RX_DROP_MONITOR;
2878
2879         if (!ieee80211_is_mgmt(mgmt->frame_control))
2880                 return RX_DROP_MONITOR;
2881
2882         if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2883             ieee80211_is_beacon(mgmt->frame_control) &&
2884             !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
2885                 int sig = 0;
2886
2887                 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2888                         sig = status->signal;
2889
2890                 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2891                                             rx->skb->data, rx->skb->len,
2892                                             status->freq, sig);
2893                 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2894         }
2895
2896         if (ieee80211_drop_unencrypted_mgmt(rx))
2897                 return RX_DROP_UNUSABLE;
2898
2899         return RX_CONTINUE;
2900 }
2901
2902 static ieee80211_rx_result debug_noinline
2903 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2904 {
2905         struct ieee80211_local *local = rx->local;
2906         struct ieee80211_sub_if_data *sdata = rx->sdata;
2907         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2908         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2909         int len = rx->skb->len;
2910
2911         if (!ieee80211_is_action(mgmt->frame_control))
2912                 return RX_CONTINUE;
2913
2914         /* drop too small frames */
2915         if (len < IEEE80211_MIN_ACTION_SIZE)
2916                 return RX_DROP_UNUSABLE;
2917
2918         if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2919             mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2920             mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2921                 return RX_DROP_UNUSABLE;
2922
2923         switch (mgmt->u.action.category) {
2924         case WLAN_CATEGORY_HT:
2925                 /* reject HT action frames from stations not supporting HT */
2926                 if (!rx->sta->sta.ht_cap.ht_supported)
2927                         goto invalid;
2928
2929                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2930                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2931                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2932                     sdata->vif.type != NL80211_IFTYPE_AP &&
2933                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
2934                         break;
2935
2936                 /* verify action & smps_control/chanwidth are present */
2937                 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2938                         goto invalid;
2939
2940                 switch (mgmt->u.action.u.ht_smps.action) {
2941                 case WLAN_HT_ACTION_SMPS: {
2942                         struct ieee80211_supported_band *sband;
2943                         enum ieee80211_smps_mode smps_mode;
2944
2945                         /* convert to HT capability */
2946                         switch (mgmt->u.action.u.ht_smps.smps_control) {
2947                         case WLAN_HT_SMPS_CONTROL_DISABLED:
2948                                 smps_mode = IEEE80211_SMPS_OFF;
2949                                 break;
2950                         case WLAN_HT_SMPS_CONTROL_STATIC:
2951                                 smps_mode = IEEE80211_SMPS_STATIC;
2952                                 break;
2953                         case WLAN_HT_SMPS_CONTROL_DYNAMIC:
2954                                 smps_mode = IEEE80211_SMPS_DYNAMIC;
2955                                 break;
2956                         default:
2957                                 goto invalid;
2958                         }
2959
2960                         /* if no change do nothing */
2961                         if (rx->sta->sta.smps_mode == smps_mode)
2962                                 goto handled;
2963                         rx->sta->sta.smps_mode = smps_mode;
2964
2965                         sband = rx->local->hw.wiphy->bands[status->band];
2966
2967                         rate_control_rate_update(local, sband, rx->sta,
2968                                                  IEEE80211_RC_SMPS_CHANGED);
2969                         goto handled;
2970                 }
2971                 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2972                         struct ieee80211_supported_band *sband;
2973                         u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
2974                         enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
2975
2976                         /* If it doesn't support 40 MHz it can't change ... */
2977                         if (!(rx->sta->sta.ht_cap.cap &
2978                                         IEEE80211_HT_CAP_SUP_WIDTH_20_40))
2979                                 goto handled;
2980
2981                         if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
2982                                 max_bw = IEEE80211_STA_RX_BW_20;
2983                         else
2984                                 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2985
2986                         /* set cur_max_bandwidth and recalc sta bw */
2987                         rx->sta->cur_max_bandwidth = max_bw;
2988                         new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
2989
2990                         if (rx->sta->sta.bandwidth == new_bw)
2991                                 goto handled;
2992
2993                         rx->sta->sta.bandwidth = new_bw;
2994                         sband = rx->local->hw.wiphy->bands[status->band];
2995
2996                         rate_control_rate_update(local, sband, rx->sta,
2997                                                  IEEE80211_RC_BW_CHANGED);
2998                         goto handled;
2999                 }
3000                 default:
3001                         goto invalid;
3002                 }
3003
3004                 break;
3005         case WLAN_CATEGORY_PUBLIC:
3006                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3007                         goto invalid;
3008                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3009                         break;
3010                 if (!rx->sta)
3011                         break;
3012                 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3013                         break;
3014                 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3015                                 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3016                         break;
3017                 if (len < offsetof(struct ieee80211_mgmt,
3018                                    u.action.u.ext_chan_switch.variable))
3019                         goto invalid;
3020                 goto queue;
3021         case WLAN_CATEGORY_VHT:
3022                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3023                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3024                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3025                     sdata->vif.type != NL80211_IFTYPE_AP &&
3026                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3027                         break;
3028
3029                 /* verify action code is present */
3030                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3031                         goto invalid;
3032
3033                 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3034                 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3035                         /* verify opmode is present */
3036                         if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3037                                 goto invalid;
3038                         goto queue;
3039                 }
3040                 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3041                         if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3042                                 goto invalid;
3043                         goto queue;
3044                 }
3045                 default:
3046                         break;
3047                 }
3048                 break;
3049         case WLAN_CATEGORY_BACK:
3050                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3051                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3052                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3053                     sdata->vif.type != NL80211_IFTYPE_AP &&
3054                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3055                         break;
3056
3057                 /* verify action_code is present */
3058                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3059                         break;
3060
3061                 switch (mgmt->u.action.u.addba_req.action_code) {
3062                 case WLAN_ACTION_ADDBA_REQ:
3063                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3064                                    sizeof(mgmt->u.action.u.addba_req)))
3065                                 goto invalid;
3066                         break;
3067                 case WLAN_ACTION_ADDBA_RESP:
3068                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3069                                    sizeof(mgmt->u.action.u.addba_resp)))
3070                                 goto invalid;
3071                         break;
3072                 case WLAN_ACTION_DELBA:
3073                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3074                                    sizeof(mgmt->u.action.u.delba)))
3075                                 goto invalid;
3076                         break;
3077                 default:
3078                         goto invalid;
3079                 }
3080
3081                 goto queue;
3082         case WLAN_CATEGORY_SPECTRUM_MGMT:
3083                 /* verify action_code is present */
3084                 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3085                         break;
3086
3087                 switch (mgmt->u.action.u.measurement.action_code) {
3088                 case WLAN_ACTION_SPCT_MSR_REQ:
3089                         if (status->band != NL80211_BAND_5GHZ)
3090                                 break;
3091
3092                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3093                                    sizeof(mgmt->u.action.u.measurement)))
3094                                 break;
3095
3096                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3097                                 break;
3098
3099                         ieee80211_process_measurement_req(sdata, mgmt, len);
3100                         goto handled;
3101                 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3102                         u8 *bssid;
3103                         if (len < (IEEE80211_MIN_ACTION_SIZE +
3104                                    sizeof(mgmt->u.action.u.chan_switch)))
3105                                 break;
3106
3107                         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3108                             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3109                             sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3110                                 break;
3111
3112                         if (sdata->vif.type == NL80211_IFTYPE_STATION)
3113                                 bssid = sdata->u.mgd.bssid;
3114                         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3115                                 bssid = sdata->u.ibss.bssid;
3116                         else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3117                                 bssid = mgmt->sa;
3118                         else
3119                                 break;
3120
3121                         if (!ether_addr_equal(mgmt->bssid, bssid))
3122                                 break;
3123
3124                         goto queue;
3125                         }
3126                 }
3127                 break;
3128         case WLAN_CATEGORY_SA_QUERY:
3129                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3130                            sizeof(mgmt->u.action.u.sa_query)))
3131                         break;
3132
3133                 switch (mgmt->u.action.u.sa_query.action) {
3134                 case WLAN_ACTION_SA_QUERY_REQUEST:
3135                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3136                                 break;
3137                         ieee80211_process_sa_query_req(sdata, mgmt, len);
3138                         goto handled;
3139                 }
3140                 break;
3141         case WLAN_CATEGORY_SELF_PROTECTED:
3142                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3143                            sizeof(mgmt->u.action.u.self_prot.action_code)))
3144                         break;
3145
3146                 switch (mgmt->u.action.u.self_prot.action_code) {
3147                 case WLAN_SP_MESH_PEERING_OPEN:
3148                 case WLAN_SP_MESH_PEERING_CLOSE:
3149                 case WLAN_SP_MESH_PEERING_CONFIRM:
3150                         if (!ieee80211_vif_is_mesh(&sdata->vif))
3151                                 goto invalid;
3152                         if (sdata->u.mesh.user_mpm)
3153                                 /* userspace handles this frame */
3154                                 break;
3155                         goto queue;
3156                 case WLAN_SP_MGK_INFORM:
3157                 case WLAN_SP_MGK_ACK:
3158                         if (!ieee80211_vif_is_mesh(&sdata->vif))
3159                                 goto invalid;
3160                         break;
3161                 }
3162                 break;
3163         case WLAN_CATEGORY_MESH_ACTION:
3164                 if (len < (IEEE80211_MIN_ACTION_SIZE +
3165                            sizeof(mgmt->u.action.u.mesh_action.action_code)))
3166                         break;
3167
3168                 if (!ieee80211_vif_is_mesh(&sdata->vif))
3169                         break;
3170                 if (mesh_action_is_path_sel(mgmt) &&
3171                     !mesh_path_sel_is_hwmp(sdata))
3172                         break;
3173                 goto queue;
3174         }
3175
3176         return RX_CONTINUE;
3177
3178  invalid:
3179         status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3180         /* will return in the next handlers */
3181         return RX_CONTINUE;
3182
3183  handled:
3184         if (rx->sta)
3185                 rx->sta->rx_stats.packets++;
3186         dev_kfree_skb(rx->skb);
3187         return RX_QUEUED;
3188
3189  queue:
3190         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
3191         skb_queue_tail(&sdata->skb_queue, rx->skb);
3192         ieee80211_queue_work(&local->hw, &sdata->work);
3193         if (rx->sta)
3194                 rx->sta->rx_stats.packets++;
3195         return RX_QUEUED;
3196 }
3197
3198 static ieee80211_rx_result debug_noinline
3199 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3200 {
3201         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3202         int sig = 0;
3203
3204         /* skip known-bad action frames and return them in the next handler */
3205         if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3206                 return RX_CONTINUE;
3207
3208         /*
3209          * Getting here means the kernel doesn't know how to handle
3210          * it, but maybe userspace does ... include returned frames
3211          * so userspace can register for those to know whether ones
3212          * it transmitted were processed or returned.
3213          */
3214
3215         if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
3216                 sig = status->signal;
3217
3218         if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
3219                              rx->skb->data, rx->skb->len, 0)) {
3220                 if (rx->sta)
3221                         rx->sta->rx_stats.packets++;
3222                 dev_kfree_skb(rx->skb);
3223                 return RX_QUEUED;
3224         }
3225
3226         return RX_CONTINUE;
3227 }
3228
3229 static ieee80211_rx_result debug_noinline
3230 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3231 {
3232         struct ieee80211_local *local = rx->local;
3233         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3234         struct sk_buff *nskb;
3235         struct ieee80211_sub_if_data *sdata = rx->sdata;
3236         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3237
3238         if (!ieee80211_is_action(mgmt->frame_control))
3239                 return RX_CONTINUE;
3240
3241         /*
3242          * For AP mode, hostapd is responsible for handling any action
3243          * frames that we didn't handle, including returning unknown
3244          * ones. For all other modes we will return them to the sender,
3245          * setting the 0x80 bit in the action category, as required by
3246          * 802.11-2012 9.24.4.
3247          * Newer versions of hostapd shall also use the management frame
3248          * registration mechanisms, but older ones still use cooked
3249          * monitor interfaces so push all frames there.
3250          */
3251         if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3252             (sdata->vif.type == NL80211_IFTYPE_AP ||
3253              sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3254                 return RX_DROP_MONITOR;
3255
3256         if (is_multicast_ether_addr(mgmt->da))
3257                 return RX_DROP_MONITOR;
3258
3259         /* do not return rejected action frames */
3260         if (mgmt->u.action.category & 0x80)
3261                 return RX_DROP_UNUSABLE;
3262
3263         nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3264                                GFP_ATOMIC);
3265         if (nskb) {
3266                 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3267
3268                 nmgmt->u.action.category |= 0x80;
3269                 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3270                 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3271
3272                 memset(nskb->cb, 0, sizeof(nskb->cb));
3273
3274                 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3275                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3276
3277                         info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3278                                       IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3279                                       IEEE80211_TX_CTL_NO_CCK_RATE;
3280                         if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3281                                 info->hw_queue =
3282                                         local->hw.offchannel_tx_hw_queue;
3283                 }
3284
3285                 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3286                                             status->band);
3287         }
3288         dev_kfree_skb(rx->skb);
3289         return RX_QUEUED;
3290 }
3291
3292 static ieee80211_rx_result debug_noinline
3293 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3294 {
3295         struct ieee80211_sub_if_data *sdata = rx->sdata;
3296         struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3297         __le16 stype;
3298
3299         stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3300
3301         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3302             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3303             sdata->vif.type != NL80211_IFTYPE_OCB &&
3304             sdata->vif.type != NL80211_IFTYPE_STATION)
3305                 return RX_DROP_MONITOR;
3306
3307         switch (stype) {
3308         case cpu_to_le16(IEEE80211_STYPE_AUTH):
3309         case cpu_to_le16(IEEE80211_STYPE_BEACON):
3310         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3311                 /* process for all: mesh, mlme, ibss */
3312                 break;
3313         case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3314                 if (is_multicast_ether_addr(mgmt->da) &&
3315                     !is_broadcast_ether_addr(mgmt->da))
3316                         return RX_DROP_MONITOR;
3317
3318                 /* process only for station/IBSS */
3319                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3320                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3321                         return RX_DROP_MONITOR;
3322                 break;
3323         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3324         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3325         case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3326                 if (is_multicast_ether_addr(mgmt->da) &&
3327                     !is_broadcast_ether_addr(mgmt->da))
3328                         return RX_DROP_MONITOR;
3329
3330                 /* process only for station */
3331                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3332                         return RX_DROP_MONITOR;
3333                 break;
3334         case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3335                 /* process only for ibss and mesh */
3336                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3337                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3338                         return RX_DROP_MONITOR;
3339                 break;
3340         default:
3341                 return RX_DROP_MONITOR;
3342         }
3343
3344         /* queue up frame and kick off work to process it */
3345         rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
3346         skb_queue_tail(&sdata->skb_queue, rx->skb);
3347         ieee80211_queue_work(&rx->local->hw, &sdata->work);
3348         if (rx->sta)
3349                 rx->sta->rx_stats.packets++;
3350
3351         return RX_QUEUED;
3352 }
3353
3354 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3355                                         struct ieee80211_rate *rate)
3356 {
3357         struct ieee80211_sub_if_data *sdata;
3358         struct ieee80211_local *local = rx->local;
3359         struct sk_buff *skb = rx->skb, *skb2;
3360         struct net_device *prev_dev = NULL;
3361         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3362         int needed_headroom;
3363
3364         /*
3365          * If cooked monitor has been processed already, then
3366          * don't do it again. If not, set the flag.
3367          */
3368         if (rx->flags & IEEE80211_RX_CMNTR)
3369                 goto out_free_skb;
3370         rx->flags |= IEEE80211_RX_CMNTR;
3371
3372         /* If there are no cooked monitor interfaces, just free the SKB */
3373         if (!local->cooked_mntrs)
3374                 goto out_free_skb;
3375
3376         /* vendor data is long removed here */
3377         status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3378         /* room for the radiotap header based on driver features */
3379         needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3380
3381         if (skb_headroom(skb) < needed_headroom &&
3382             pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3383                 goto out_free_skb;
3384
3385         /* prepend radiotap information */
3386         ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3387                                          false);
3388
3389         skb_reset_mac_header(skb);
3390         skb->ip_summed = CHECKSUM_UNNECESSARY;
3391         skb->pkt_type = PACKET_OTHERHOST;
3392         skb->protocol = htons(ETH_P_802_2);
3393
3394         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3395                 if (!ieee80211_sdata_running(sdata))
3396                         continue;
3397
3398                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3399                     !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3400                         continue;
3401
3402                 if (prev_dev) {
3403                         skb2 = skb_clone(skb, GFP_ATOMIC);
3404                         if (skb2) {
3405                                 skb2->dev = prev_dev;
3406                                 netif_receive_skb(skb2);
3407                         }
3408                 }
3409
3410                 prev_dev = sdata->dev;
3411                 ieee80211_rx_stats(sdata->dev, skb->len);
3412         }
3413
3414         if (prev_dev) {
3415                 skb->dev = prev_dev;
3416                 netif_receive_skb(skb);
3417                 return;
3418         }
3419
3420  out_free_skb:
3421         dev_kfree_skb(skb);
3422 }
3423
3424 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3425                                          ieee80211_rx_result res)
3426 {
3427         switch (res) {
3428         case RX_DROP_MONITOR:
3429                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3430                 if (rx->sta)
3431                         rx->sta->rx_stats.dropped++;
3432                 /* fall through */
3433         case RX_CONTINUE: {
3434                 struct ieee80211_rate *rate = NULL;
3435                 struct ieee80211_supported_band *sband;
3436                 struct ieee80211_rx_status *status;
3437
3438                 status = IEEE80211_SKB_RXCB((rx->skb));
3439
3440                 sband = rx->local->hw.wiphy->bands[status->band];
3441                 if (!(status->flag & RX_FLAG_HT) &&
3442                     !(status->flag & RX_FLAG_VHT))
3443                         rate = &sband->bitrates[status->rate_idx];
3444
3445                 ieee80211_rx_cooked_monitor(rx, rate);
3446                 break;
3447                 }
3448         case RX_DROP_UNUSABLE:
3449                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3450                 if (rx->sta)
3451                         rx->sta->rx_stats.dropped++;
3452                 dev_kfree_skb(rx->skb);
3453                 break;
3454         case RX_QUEUED:
3455                 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3456                 break;
3457         }
3458 }
3459
3460 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3461                                   struct sk_buff_head *frames)
3462 {
3463         ieee80211_rx_result res = RX_DROP_MONITOR;
3464         struct sk_buff *skb;
3465
3466 #define CALL_RXH(rxh)                   \
3467         do {                            \
3468                 res = rxh(rx);          \
3469                 if (res != RX_CONTINUE) \
3470                         goto rxh_next;  \
3471         } while (0)
3472
3473         /* Lock here to avoid hitting all of the data used in the RX
3474          * path (e.g. key data, station data, ...) concurrently when
3475          * a frame is released from the reorder buffer due to timeout
3476          * from the timer, potentially concurrently with RX from the
3477          * driver.
3478          */
3479         spin_lock_bh(&rx->local->rx_path_lock);
3480
3481         while ((skb = __skb_dequeue(frames))) {
3482                 /*
3483                  * all the other fields are valid across frames
3484                  * that belong to an aMPDU since they are on the
3485                  * same TID from the same station
3486                  */
3487                 rx->skb = skb;
3488
3489                 CALL_RXH(ieee80211_rx_h_check_more_data);
3490                 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3491                 CALL_RXH(ieee80211_rx_h_sta_process);
3492                 CALL_RXH(ieee80211_rx_h_decrypt);
3493                 CALL_RXH(ieee80211_rx_h_defragment);
3494                 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3495                 /* must be after MMIC verify so header is counted in MPDU mic */
3496 #ifdef CONFIG_MAC80211_MESH
3497                 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3498                         CALL_RXH(ieee80211_rx_h_mesh_fwding);
3499 #endif
3500                 CALL_RXH(ieee80211_rx_h_amsdu);
3501                 CALL_RXH(ieee80211_rx_h_data);
3502
3503                 /* special treatment -- needs the queue */
3504                 res = ieee80211_rx_h_ctrl(rx, frames);
3505                 if (res != RX_CONTINUE)
3506                         goto rxh_next;
3507
3508                 CALL_RXH(ieee80211_rx_h_mgmt_check);
3509                 CALL_RXH(ieee80211_rx_h_action);
3510                 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3511                 CALL_RXH(ieee80211_rx_h_action_return);
3512                 CALL_RXH(ieee80211_rx_h_mgmt);
3513
3514  rxh_next:
3515                 ieee80211_rx_handlers_result(rx, res);
3516
3517 #undef CALL_RXH
3518         }
3519
3520         spin_unlock_bh(&rx->local->rx_path_lock);
3521 }
3522
3523 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3524 {
3525         struct sk_buff_head reorder_release;
3526         ieee80211_rx_result res = RX_DROP_MONITOR;
3527
3528         __skb_queue_head_init(&reorder_release);
3529
3530 #define CALL_RXH(rxh)                   \
3531         do {                            \
3532                 res = rxh(rx);          \
3533                 if (res != RX_CONTINUE) \
3534                         goto rxh_next;  \
3535         } while (0)
3536
3537         CALL_RXH(ieee80211_rx_h_check_dup);
3538         CALL_RXH(ieee80211_rx_h_check);
3539
3540         ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3541
3542         ieee80211_rx_handlers(rx, &reorder_release);
3543         return;
3544
3545  rxh_next:
3546         ieee80211_rx_handlers_result(rx, res);
3547
3548 #undef CALL_RXH
3549 }
3550
3551 /*
3552  * This function makes calls into the RX path, therefore
3553  * it has to be invoked under RCU read lock.
3554  */
3555 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3556 {
3557         struct sk_buff_head frames;
3558         struct ieee80211_rx_data rx = {
3559                 .sta = sta,
3560                 .sdata = sta->sdata,
3561                 .local = sta->local,
3562                 /* This is OK -- must be QoS data frame */
3563                 .security_idx = tid,
3564                 .seqno_idx = tid,
3565                 .napi = NULL, /* must be NULL to not have races */
3566         };
3567         struct tid_ampdu_rx *tid_agg_rx;
3568
3569         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3570         if (!tid_agg_rx)
3571                 return;
3572
3573         __skb_queue_head_init(&frames);
3574
3575         spin_lock(&tid_agg_rx->reorder_lock);
3576         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3577         spin_unlock(&tid_agg_rx->reorder_lock);
3578
3579         if (!skb_queue_empty(&frames)) {
3580                 struct ieee80211_event event = {
3581                         .type = BA_FRAME_TIMEOUT,
3582                         .u.ba.tid = tid,
3583                         .u.ba.sta = &sta->sta,
3584                 };
3585                 drv_event_callback(rx.local, rx.sdata, &event);
3586         }
3587
3588         ieee80211_rx_handlers(&rx, &frames);
3589 }
3590
3591 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3592                                           u16 ssn, u64 filtered,
3593                                           u16 received_mpdus)
3594 {
3595         struct sta_info *sta;
3596         struct tid_ampdu_rx *tid_agg_rx;
3597         struct sk_buff_head frames;
3598         struct ieee80211_rx_data rx = {
3599                 /* This is OK -- must be QoS data frame */
3600                 .security_idx = tid,
3601                 .seqno_idx = tid,
3602         };
3603         int i, diff;
3604
3605         if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3606                 return;
3607
3608         __skb_queue_head_init(&frames);
3609
3610         sta = container_of(pubsta, struct sta_info, sta);
3611
3612         rx.sta = sta;
3613         rx.sdata = sta->sdata;
3614         rx.local = sta->local;
3615
3616         rcu_read_lock();
3617         tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3618         if (!tid_agg_rx)
3619                 goto out;
3620
3621         spin_lock_bh(&tid_agg_rx->reorder_lock);
3622
3623         if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3624                 int release;
3625
3626                 /* release all frames in the reorder buffer */
3627                 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3628                            IEEE80211_SN_MODULO;
3629                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3630                                                  release, &frames);
3631                 /* update ssn to match received ssn */
3632                 tid_agg_rx->head_seq_num = ssn;
3633         } else {
3634                 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3635                                                  &frames);
3636         }
3637
3638         /* handle the case that received ssn is behind the mac ssn.
3639          * it can be tid_agg_rx->buf_size behind and still be valid */
3640         diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3641         if (diff >= tid_agg_rx->buf_size) {
3642                 tid_agg_rx->reorder_buf_filtered = 0;
3643                 goto release;
3644         }
3645         filtered = filtered >> diff;
3646         ssn += diff;
3647
3648         /* update bitmap */
3649         for (i = 0; i < tid_agg_rx->buf_size; i++) {
3650                 int index = (ssn + i) % tid_agg_rx->buf_size;
3651
3652                 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3653                 if (filtered & BIT_ULL(i))
3654                         tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3655         }
3656
3657         /* now process also frames that the filter marking released */
3658         ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3659
3660 release:
3661         spin_unlock_bh(&tid_agg_rx->reorder_lock);
3662
3663         ieee80211_rx_handlers(&rx, &frames);
3664
3665  out:
3666         rcu_read_unlock();
3667 }
3668 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3669
3670 /* main receive path */
3671
3672 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3673 {
3674         struct ieee80211_sub_if_data *sdata = rx->sdata;
3675         struct sk_buff *skb = rx->skb;
3676         struct ieee80211_hdr *hdr = (void *)skb->data;
3677         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3678         u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3679         int multicast = is_multicast_ether_addr(hdr->addr1);
3680
3681         switch (sdata->vif.type) {
3682         case NL80211_IFTYPE_STATION:
3683                 if (!bssid && !sdata->u.mgd.use_4addr)
3684                         return false;
3685                 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3686                         return false;
3687                 if (multicast)
3688                         return true;
3689                 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3690         case NL80211_IFTYPE_ADHOC:
3691                 if (!bssid)
3692                         return false;
3693                 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3694                     ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
3695                     !is_valid_ether_addr(hdr->addr2))
3696                         return false;
3697                 if (ieee80211_is_beacon(hdr->frame_control))
3698                         return true;
3699                 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3700                         return false;
3701                 if (!multicast &&
3702                     !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3703                         return false;
3704                 if (!rx->sta) {
3705                         int rate_idx;
3706                         if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3707                                 rate_idx = 0; /* TODO: HT/VHT rates */
3708                         else
3709                                 rate_idx = status->rate_idx;
3710                         ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3711                                                  BIT(rate_idx));
3712                 }
3713                 return true;
3714         case NL80211_IFTYPE_OCB:
3715                 if (!bssid)
3716                         return false;
3717                 if (!ieee80211_is_data_present(hdr->frame_control))
3718                         return false;
3719                 if (!is_broadcast_ether_addr(bssid))
3720                         return false;
3721                 if (!multicast &&
3722                     !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
3723                         return false;
3724                 if (!rx->sta) {
3725                         int rate_idx;
3726                         if (status->flag & RX_FLAG_HT)
3727                                 rate_idx = 0; /* TODO: HT rates */
3728                         else
3729                                 rate_idx = status->rate_idx;
3730                         ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3731                                                 BIT(rate_idx));
3732                 }
3733                 return true;
3734         case NL80211_IFTYPE_MESH_POINT:
3735                 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
3736                         return false;
3737                 if (multicast)
3738                         return true;
3739                 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3740         case NL80211_IFTYPE_AP_VLAN:
3741         case NL80211_IFTYPE_AP:
3742                 if (!bssid)
3743                         return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3744
3745                 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3746                         /*
3747                          * Accept public action frames even when the
3748                          * BSSID doesn't match, this is used for P2P
3749                          * and location updates. Note that mac80211
3750                          * itself never looks at these frames.
3751                          */
3752                         if (!multicast &&
3753                             !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3754                                 return false;
3755                         if (ieee80211_is_public_action(hdr, skb->len))
3756                                 return true;
3757                         return ieee80211_is_beacon(hdr->frame_control);
3758                 }
3759
3760                 if (!ieee80211_has_tods(hdr->frame_control)) {
3761                         /* ignore data frames to TDLS-peers */
3762                         if (ieee80211_is_data(hdr->frame_control))
3763                                 return false;
3764                         /* ignore action frames to TDLS-peers */
3765                         if (ieee80211_is_action(hdr->frame_control) &&
3766                             !is_broadcast_ether_addr(bssid) &&
3767                             !ether_addr_equal(bssid, hdr->addr1))
3768                                 return false;
3769                 }
3770
3771                 /*
3772                  * 802.11-2016 Table 9-26 says that for data frames, A1 must be
3773                  * the BSSID - we've checked that already but may have accepted
3774                  * the wildcard (ff:ff:ff:ff:ff:ff).
3775                  *
3776                  * It also says:
3777                  *      The BSSID of the Data frame is determined as follows:
3778                  *      a) If the STA is contained within an AP or is associated
3779                  *         with an AP, the BSSID is the address currently in use
3780                  *         by the STA contained in the AP.
3781                  *
3782                  * So we should not accept data frames with an address that's
3783                  * multicast.
3784                  *
3785                  * Accepting it also opens a security problem because stations
3786                  * could encrypt it with the GTK and inject traffic that way.
3787                  */
3788                 if (ieee80211_is_data(hdr->frame_control) && multicast)
3789                         return false;
3790
3791                 return true;
3792         case NL80211_IFTYPE_WDS:
3793                 if (bssid || !ieee80211_is_data(hdr->frame_control))
3794                         return false;
3795                 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
3796         case NL80211_IFTYPE_P2P_DEVICE:
3797                 return ieee80211_is_public_action(hdr, skb->len) ||
3798                        ieee80211_is_probe_req(hdr->frame_control) ||
3799                        ieee80211_is_probe_resp(hdr->frame_control) ||
3800                        ieee80211_is_beacon(hdr->frame_control);
3801         case NL80211_IFTYPE_NAN:
3802                 /* Currently no frames on NAN interface are allowed */
3803                 return false;
3804         default:
3805                 break;
3806         }
3807
3808         WARN_ON_ONCE(1);
3809         return false;
3810 }
3811
3812 void ieee80211_check_fast_rx(struct sta_info *sta)
3813 {
3814         struct ieee80211_sub_if_data *sdata = sta->sdata;
3815         struct ieee80211_local *local = sdata->local;
3816         struct ieee80211_key *key;
3817         struct ieee80211_fast_rx fastrx = {
3818                 .dev = sdata->dev,
3819                 .vif_type = sdata->vif.type,
3820                 .control_port_protocol = sdata->control_port_protocol,
3821         }, *old, *new = NULL;
3822         bool assign = false;
3823
3824         /* use sparse to check that we don't return without updating */
3825         __acquire(check_fast_rx);
3826
3827         BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3828         BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3829         ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3830         ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3831
3832         fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3833
3834         /* fast-rx doesn't do reordering */
3835         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3836             !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3837                 goto clear;
3838
3839         switch (sdata->vif.type) {
3840         case NL80211_IFTYPE_STATION:
3841                 /* 4-addr is harder to deal with, later maybe */
3842                 if (sdata->u.mgd.use_4addr)
3843                         goto clear;
3844                 /* software powersave is a huge mess, avoid all of it */
3845                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3846                         goto clear;
3847                 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3848                     !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3849                         goto clear;
3850                 if (sta->sta.tdls) {
3851                         fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3852                         fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3853                         fastrx.expected_ds_bits = 0;
3854                 } else {
3855                         fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3856                         fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3857                         fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3858                         fastrx.expected_ds_bits =
3859                                 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3860                 }
3861                 break;
3862         case NL80211_IFTYPE_AP_VLAN:
3863         case NL80211_IFTYPE_AP:
3864                 /* parallel-rx requires this, at least with calls to
3865                  * ieee80211_sta_ps_transition()
3866                  */
3867                 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3868                         goto clear;
3869                 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3870                 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3871                 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3872
3873                 fastrx.internal_forward =
3874                         !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3875                         (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3876                          !sdata->u.vlan.sta);
3877                 break;
3878         default:
3879                 goto clear;
3880         }
3881
3882         if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3883                 goto clear;
3884
3885         rcu_read_lock();
3886         key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3887         if (!key)
3888                 key = rcu_dereference(sdata->default_unicast_key);
3889         if (key) {
3890                 switch (key->conf.cipher) {
3891                 case WLAN_CIPHER_SUITE_TKIP:
3892                         /* we don't want to deal with MMIC in fast-rx */
3893                         goto clear_rcu;
3894                 case WLAN_CIPHER_SUITE_CCMP:
3895                 case WLAN_CIPHER_SUITE_CCMP_256:
3896                 case WLAN_CIPHER_SUITE_GCMP:
3897                 case WLAN_CIPHER_SUITE_GCMP_256:
3898                         break;
3899                 default:
3900                         /* we also don't want to deal with WEP or cipher scheme
3901                          * since those require looking up the key idx in the
3902                          * frame, rather than assuming the PTK is used
3903                          * (we need to revisit this once we implement the real
3904                          * PTK index, which is now valid in the spec, but we
3905                          * haven't implemented that part yet)
3906                          */
3907                         goto clear_rcu;
3908                 }
3909
3910                 fastrx.key = true;
3911                 fastrx.icv_len = key->conf.icv_len;
3912         }
3913
3914         assign = true;
3915  clear_rcu:
3916         rcu_read_unlock();
3917  clear:
3918         __release(check_fast_rx);
3919
3920         if (assign)
3921                 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3922
3923         spin_lock_bh(&sta->lock);
3924         old = rcu_dereference_protected(sta->fast_rx, true);
3925         rcu_assign_pointer(sta->fast_rx, new);
3926         spin_unlock_bh(&sta->lock);
3927
3928         if (old)
3929                 kfree_rcu(old, rcu_head);
3930 }
3931
3932 void ieee80211_clear_fast_rx(struct sta_info *sta)
3933 {
3934         struct ieee80211_fast_rx *old;
3935
3936         spin_lock_bh(&sta->lock);
3937         old = rcu_dereference_protected(sta->fast_rx, true);
3938         RCU_INIT_POINTER(sta->fast_rx, NULL);
3939         spin_unlock_bh(&sta->lock);
3940
3941         if (old)
3942                 kfree_rcu(old, rcu_head);
3943 }
3944
3945 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3946 {
3947         struct ieee80211_local *local = sdata->local;
3948         struct sta_info *sta;
3949
3950         lockdep_assert_held(&local->sta_mtx);
3951
3952         list_for_each_entry(sta, &local->sta_list, list) {
3953                 if (sdata != sta->sdata &&
3954                     (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3955                         continue;
3956                 ieee80211_check_fast_rx(sta);
3957         }
3958 }
3959
3960 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3961 {
3962         struct ieee80211_local *local = sdata->local;
3963
3964         mutex_lock(&local->sta_mtx);
3965         __ieee80211_check_fast_rx_iface(sdata);
3966         mutex_unlock(&local->sta_mtx);
3967 }
3968
3969 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3970                                      struct ieee80211_fast_rx *fast_rx)
3971 {
3972         struct sk_buff *skb = rx->skb;
3973         struct ieee80211_hdr *hdr = (void *)skb->data;
3974         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3975         struct sta_info *sta = rx->sta;
3976         int orig_len = skb->len;
3977         int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3978         struct {
3979                 u8 snap[sizeof(rfc1042_header)];
3980                 __be16 proto;
3981         } *payload __aligned(2);
3982         struct {
3983                 u8 da[ETH_ALEN];
3984                 u8 sa[ETH_ALEN];
3985         } addrs __aligned(2);
3986         struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3987
3988         if (fast_rx->uses_rss)
3989                 stats = this_cpu_ptr(sta->pcpu_rx_stats);
3990
3991         /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3992          * to a common data structure; drivers can implement that per queue
3993          * but we don't have that information in mac80211
3994          */
3995         if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3996                 return false;
3997
3998 #define FAST_RX_CRYPT_FLAGS     (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3999
4000         /* If using encryption, we also need to have:
4001          *  - PN_VALIDATED: similar, but the implementation is tricky
4002          *  - DECRYPTED: necessary for PN_VALIDATED
4003          */
4004         if (fast_rx->key &&
4005             (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4006                 return false;
4007
4008         /* we don't deal with A-MSDU deaggregation here */
4009         if (status->rx_flags & IEEE80211_RX_AMSDU)
4010                 return false;
4011
4012         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4013                 return false;
4014
4015         if (unlikely(ieee80211_is_frag(hdr)))
4016                 return false;
4017
4018         /* Since our interface address cannot be multicast, this
4019          * implicitly also rejects multicast frames without the
4020          * explicit check.
4021          *
4022          * We shouldn't get any *data* frames not addressed to us
4023          * (AP mode will accept multicast *management* frames), but
4024          * punting here will make it go through the full checks in
4025          * ieee80211_accept_frame().
4026          */
4027         if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4028                 return false;
4029
4030         if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4031                                               IEEE80211_FCTL_TODS)) !=
4032             fast_rx->expected_ds_bits)
4033                 return false;
4034
4035         /* assign the key to drop unencrypted frames (later)
4036          * and strip the IV/MIC if necessary
4037          */
4038         if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4039                 /* GCMP header length is the same */
4040                 snap_offs += IEEE80211_CCMP_HDR_LEN;
4041         }
4042
4043         if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4044                 goto drop;
4045         payload = (void *)(skb->data + snap_offs);
4046
4047         if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4048                 return false;
4049
4050         /* Don't handle these here since they require special code.
4051          * Accept AARP and IPX even though they should come with a
4052          * bridge-tunnel header - but if we get them this way then
4053          * there's little point in discarding them.
4054          */
4055         if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4056                      payload->proto == fast_rx->control_port_protocol))
4057                 return false;
4058
4059         /* after this point, don't punt to the slowpath! */
4060
4061         if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4062             pskb_trim(skb, skb->len - fast_rx->icv_len))
4063                 goto drop;
4064
4065         if (unlikely(fast_rx->sta_notify)) {
4066                 ieee80211_sta_rx_notify(rx->sdata, hdr);
4067                 fast_rx->sta_notify = false;
4068         }
4069
4070         /* statistics part of ieee80211_rx_h_sta_process() */
4071         stats->last_rx = jiffies;
4072         stats->last_rate = sta_stats_encode_rate(status);
4073
4074         stats->fragments++;
4075         stats->packets++;
4076
4077         if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4078                 stats->last_signal = status->signal;
4079                 if (!fast_rx->uses_rss)
4080                         ewma_signal_add(&sta->rx_stats_avg.signal,
4081                                         -status->signal);
4082         }
4083
4084         if (status->chains) {
4085                 int i;
4086
4087                 stats->chains = status->chains;
4088                 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4089                         int signal = status->chain_signal[i];
4090
4091                         if (!(status->chains & BIT(i)))
4092                                 continue;
4093
4094                         stats->chain_signal_last[i] = signal;
4095                         if (!fast_rx->uses_rss)
4096                                 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4097                                                 -signal);
4098                 }
4099         }
4100         /* end of statistics */
4101
4102         if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4103                 goto drop;
4104
4105         /* do the header conversion - first grab the addresses */
4106         ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4107         ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4108         /* remove the SNAP but leave the ethertype */
4109         skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4110         /* push the addresses in front */
4111         memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4112
4113         skb->dev = fast_rx->dev;
4114
4115         ieee80211_rx_stats(fast_rx->dev, skb->len);
4116
4117         /* The seqno index has the same property as needed
4118          * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4119          * for non-QoS-data frames. Here we know it's a data
4120          * frame, so count MSDUs.
4121          */
4122         u64_stats_update_begin(&stats->syncp);
4123         stats->msdu[rx->seqno_idx]++;
4124         stats->bytes += orig_len;
4125         u64_stats_update_end(&stats->syncp);
4126
4127         if (fast_rx->internal_forward) {
4128                 struct sk_buff *xmit_skb = NULL;
4129                 bool multicast = is_multicast_ether_addr(skb->data);
4130
4131                 if (multicast) {
4132                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
4133                 } else if (sta_info_get(rx->sdata, skb->data)) {
4134                         xmit_skb = skb;
4135                         skb = NULL;
4136                 }
4137
4138                 if (xmit_skb) {
4139                         /*
4140                          * Send to wireless media and increase priority by 256
4141                          * to keep the received priority instead of
4142                          * reclassifying the frame (see cfg80211_classify8021d).
4143                          */
4144                         xmit_skb->priority += 256;
4145                         xmit_skb->protocol = htons(ETH_P_802_3);
4146                         skb_reset_network_header(xmit_skb);
4147                         skb_reset_mac_header(xmit_skb);
4148                         dev_queue_xmit(xmit_skb);
4149                 }
4150
4151                 if (!skb)
4152                         return true;
4153         }
4154
4155         /* deliver to local stack */
4156         skb->protocol = eth_type_trans(skb, fast_rx->dev);
4157         memset(skb->cb, 0, sizeof(skb->cb));
4158         if (rx->napi)
4159                 napi_gro_receive(rx->napi, skb);
4160         else
4161                 netif_receive_skb(skb);
4162
4163         return true;
4164  drop:
4165         dev_kfree_skb(skb);
4166         stats->dropped++;
4167         return true;
4168 }
4169
4170 /*
4171  * This function returns whether or not the SKB
4172  * was destined for RX processing or not, which,
4173  * if consume is true, is equivalent to whether
4174  * or not the skb was consumed.
4175  */
4176 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4177                                             struct sk_buff *skb, bool consume)
4178 {
4179         struct ieee80211_local *local = rx->local;
4180         struct ieee80211_sub_if_data *sdata = rx->sdata;
4181
4182         rx->skb = skb;
4183
4184         /* See if we can do fast-rx; if we have to copy we already lost,
4185          * so punt in that case. We should never have to deliver a data
4186          * frame to multiple interfaces anyway.
4187          *
4188          * We skip the ieee80211_accept_frame() call and do the necessary
4189          * checking inside ieee80211_invoke_fast_rx().
4190          */
4191         if (consume && rx->sta) {
4192                 struct ieee80211_fast_rx *fast_rx;
4193
4194                 fast_rx = rcu_dereference(rx->sta->fast_rx);
4195                 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4196                         return true;
4197         }
4198
4199         if (!ieee80211_accept_frame(rx))
4200                 return false;
4201
4202         if (!consume) {
4203                 skb = skb_copy(skb, GFP_ATOMIC);
4204                 if (!skb) {
4205                         if (net_ratelimit())
4206                                 wiphy_debug(local->hw.wiphy,
4207                                         "failed to copy skb for %s\n",
4208                                         sdata->name);
4209                         return true;
4210                 }
4211
4212                 rx->skb = skb;
4213         }
4214
4215         ieee80211_invoke_rx_handlers(rx);
4216         return true;
4217 }
4218
4219 /*
4220  * This is the actual Rx frames handler. as it belongs to Rx path it must
4221  * be called with rcu_read_lock protection.
4222  */
4223 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4224                                          struct ieee80211_sta *pubsta,
4225                                          struct sk_buff *skb,
4226                                          struct napi_struct *napi)
4227 {
4228         struct ieee80211_local *local = hw_to_local(hw);
4229         struct ieee80211_sub_if_data *sdata;
4230         struct ieee80211_hdr *hdr;
4231         __le16 fc;
4232         struct ieee80211_rx_data rx;
4233         struct ieee80211_sub_if_data *prev;
4234         struct rhlist_head *tmp;
4235         int err = 0;
4236
4237         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4238         memset(&rx, 0, sizeof(rx));
4239         rx.skb = skb;
4240         rx.local = local;
4241         rx.napi = napi;
4242
4243         if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4244                 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4245
4246         if (ieee80211_is_mgmt(fc)) {
4247                 /* drop frame if too short for header */
4248                 if (skb->len < ieee80211_hdrlen(fc))
4249                         err = -ENOBUFS;
4250                 else
4251                         err = skb_linearize(skb);
4252         } else {
4253                 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4254         }
4255
4256         if (err) {
4257                 dev_kfree_skb(skb);
4258                 return;
4259         }
4260
4261         hdr = (struct ieee80211_hdr *)skb->data;
4262         ieee80211_parse_qos(&rx);
4263         ieee80211_verify_alignment(&rx);
4264
4265         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4266                      ieee80211_is_beacon(hdr->frame_control)))
4267                 ieee80211_scan_rx(local, skb);
4268
4269         if (ieee80211_is_data(fc)) {
4270                 struct sta_info *sta, *prev_sta;
4271
4272                 if (pubsta) {
4273                         rx.sta = container_of(pubsta, struct sta_info, sta);
4274                         rx.sdata = rx.sta->sdata;
4275                         if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4276                                 return;
4277                         goto out;
4278                 }
4279
4280                 prev_sta = NULL;
4281
4282                 for_each_sta_info(local, hdr->addr2, sta, tmp) {
4283                         if (!prev_sta) {
4284                                 prev_sta = sta;
4285                                 continue;
4286                         }
4287
4288                         rx.sta = prev_sta;
4289                         rx.sdata = prev_sta->sdata;
4290                         ieee80211_prepare_and_rx_handle(&rx, skb, false);
4291
4292                         prev_sta = sta;
4293                 }
4294
4295                 if (prev_sta) {
4296                         rx.sta = prev_sta;
4297                         rx.sdata = prev_sta->sdata;
4298
4299                         if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4300                                 return;
4301                         goto out;
4302                 }
4303         }
4304
4305         prev = NULL;
4306
4307         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4308                 if (!ieee80211_sdata_running(sdata))
4309                         continue;
4310
4311                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4312                     sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4313                         continue;
4314
4315                 /*
4316                  * frame is destined for this interface, but if it's
4317                  * not also for the previous one we handle that after
4318                  * the loop to avoid copying the SKB once too much
4319                  */
4320
4321                 if (!prev) {
4322                         prev = sdata;
4323                         continue;
4324                 }
4325
4326                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4327                 rx.sdata = prev;
4328                 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4329
4330                 prev = sdata;
4331         }
4332
4333         if (prev) {
4334                 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4335                 rx.sdata = prev;
4336
4337                 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4338                         return;
4339         }
4340
4341  out:
4342         dev_kfree_skb(skb);
4343 }
4344
4345 /*
4346  * This is the receive path handler. It is called by a low level driver when an
4347  * 802.11 MPDU is received from the hardware.
4348  */
4349 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4350                        struct sk_buff *skb, struct napi_struct *napi)
4351 {
4352         struct ieee80211_local *local = hw_to_local(hw);
4353         struct ieee80211_rate *rate = NULL;
4354         struct ieee80211_supported_band *sband;
4355         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4356
4357         WARN_ON_ONCE(softirq_count() == 0);
4358
4359         if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4360                 goto drop;
4361
4362         sband = local->hw.wiphy->bands[status->band];
4363         if (WARN_ON(!sband))
4364                 goto drop;
4365
4366         /*
4367          * If we're suspending, it is possible although not too likely
4368          * that we'd be receiving frames after having already partially
4369          * quiesced the stack. We can't process such frames then since
4370          * that might, for example, cause stations to be added or other
4371          * driver callbacks be invoked.
4372          */
4373         if (unlikely(local->quiescing || local->suspended))
4374                 goto drop;
4375
4376         /* We might be during a HW reconfig, prevent Rx for the same reason */
4377         if (unlikely(local->in_reconfig))
4378                 goto drop;
4379
4380         /*
4381          * The same happens when we're not even started,
4382          * but that's worth a warning.
4383          */
4384         if (WARN_ON(!local->started))
4385                 goto drop;
4386
4387         if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4388                 /*
4389                  * Validate the rate, unless a PLCP error means that
4390                  * we probably can't have a valid rate here anyway.
4391                  */
4392
4393                 if (status->flag & RX_FLAG_HT) {
4394                         /*
4395                          * rate_idx is MCS index, which can be [0-76]
4396                          * as documented on:
4397                          *
4398                          * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4399                          *
4400                          * Anything else would be some sort of driver or
4401                          * hardware error. The driver should catch hardware
4402                          * errors.
4403                          */
4404                         if (WARN(status->rate_idx > 76,
4405                                  "Rate marked as an HT rate but passed "
4406                                  "status->rate_idx is not "
4407                                  "an MCS index [0-76]: %d (0x%02x)\n",
4408                                  status->rate_idx,
4409                                  status->rate_idx))
4410                                 goto drop;
4411                 } else if (status->flag & RX_FLAG_VHT) {
4412                         if (WARN_ONCE(status->rate_idx > 9 ||
4413                                       !status->vht_nss ||
4414                                       status->vht_nss > 8,
4415                                       "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4416                                       status->rate_idx, status->vht_nss))
4417                                 goto drop;
4418                 } else {
4419                         if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4420                                 goto drop;
4421                         rate = &sband->bitrates[status->rate_idx];
4422                 }
4423         }
4424
4425         status->rx_flags = 0;
4426
4427         /*
4428          * key references and virtual interfaces are protected using RCU
4429          * and this requires that we are in a read-side RCU section during
4430          * receive processing
4431          */
4432         rcu_read_lock();
4433
4434         /*
4435          * Frames with failed FCS/PLCP checksum are not returned,
4436          * all other frames are returned without radiotap header
4437          * if it was previously present.
4438          * Also, frames with less than 16 bytes are dropped.
4439          */
4440         skb = ieee80211_rx_monitor(local, skb, rate);
4441         if (!skb) {
4442                 rcu_read_unlock();
4443                 return;
4444         }
4445
4446         ieee80211_tpt_led_trig_rx(local,
4447                         ((struct ieee80211_hdr *)skb->data)->frame_control,
4448                         skb->len);
4449
4450         __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
4451
4452         rcu_read_unlock();
4453
4454         return;
4455  drop:
4456         kfree_skb(skb);
4457 }
4458 EXPORT_SYMBOL(ieee80211_rx_napi);
4459
4460 /* This is a version of the rx handler that can be called from hard irq
4461  * context. Post the skb on the queue and schedule the tasklet */
4462 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4463 {
4464         struct ieee80211_local *local = hw_to_local(hw);
4465
4466         BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4467
4468         skb->pkt_type = IEEE80211_RX_MSG;
4469         skb_queue_tail(&local->skb_queue, skb);
4470         tasklet_schedule(&local->tasklet);
4471 }
4472 EXPORT_SYMBOL(ieee80211_rx_irqsafe);