~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/rx.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp