blob: e35c42ebb7a57720dd23cb186d1569f8bdc06e99 [file] [log] [blame]
Johannes Berg571ecf62007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Berg84040802010-02-15 12:46:39 +02005 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Sara Sharonb7540d82017-02-06 15:28:42 +02007 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
Johannes Berg571ecf62007-07-27 15:43:22 +02008 *
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
S.Çağlar Onurab466232008-02-14 17:36:47 +020014#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020016#include <linux/kernel.h>
17#include <linux/skbuff.h>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040020#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sara Sharon06470f72016-01-28 16:19:25 +020022#include <linux/bitops.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020023#include <net/mac80211.h>
24#include <net/ieee80211_radiotap.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010025#include <asm/unaligned.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020026
27#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020028#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040029#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010030#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020031#include "wep.h"
32#include "wpa.h"
33#include "tkip.h"
34#include "wme.h"
Johannes Berg1d8d3de2011-12-16 15:28:57 +010035#include "rate.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020036
Johannes Berg5a490512015-04-22 17:10:38 +020037static 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
Johannes Berga6828492015-06-16 15:17:15 +020047static 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
Johannes Bergb2e77712007-09-26 15:19:39 +020092/*
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 */
Johannes Berg30841f52017-04-11 15:38:56 +020098static void remove_monitor_info(struct sk_buff *skb,
99 unsigned int present_fcs_len,
100 unsigned int rtap_vendor_space)
Johannes Bergb2e77712007-09-26 15:19:39 +0200101{
Johannes Berg30841f52017-04-11 15:38:56 +0200102 if (present_fcs_len)
103 __pskb_trim(skb, skb->len - present_fcs_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100104 __pskb_pull(skb, rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200105}
106
Johannes Berg1f7bba72014-11-06 22:56:36 +0100107static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
108 unsigned int rtap_vendor_space)
Johannes Bergb2e77712007-09-26 15:19:39 +0200109{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200110 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100111 struct ieee80211_hdr *hdr;
112
113 hdr = (void *)(skb->data + rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200114
Johannes Berg4c298672012-07-05 11:34:31 +0200115 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100116 RX_FLAG_FAILED_PLCP_CRC |
117 RX_FLAG_ONLY_MONITOR))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800118 return true;
119
Johannes Berg1f7bba72014-11-06 22:56:36 +0100120 if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800121 return true;
122
Harvey Harrison87228f52008-06-11 14:21:59 -0700123 if (ieee80211_is_ctl(hdr->frame_control) &&
124 !ieee80211_is_pspoll(hdr->frame_control) &&
125 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800126 return true;
127
128 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200129}
130
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200131static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100132ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
133 struct ieee80211_rx_status *status,
134 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200135{
136 int len;
137
138 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200139 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200140
Johannes Berga144f372013-07-03 13:34:02 +0200141 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200142 if (status->chains)
143 len += 4 * hweight8(status->chains);
Johannes Berg90b9e4462012-11-16 10:09:08 +0100144
145 if (ieee80211_have_rx_timestamp(status)) {
146 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200147 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100148 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200149 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200150 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200151
Johannes Berga144f372013-07-03 13:34:02 +0200152 /* antenna field, if we don't have per-chain info */
153 if (!status->chains)
154 len += 1;
155
Johannes Berg90b9e4462012-11-16 10:09:08 +0100156 /* padding for RX_FLAGS if necessary */
157 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200158
Johannes Berg6d744ba2011-01-27 14:13:17 +0100159 if (status->flag & RX_FLAG_HT) /* HT info */
160 len += 3;
161
Johannes Berg4c298672012-07-05 11:34:31 +0200162 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100163 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200164 len += 8;
165 }
166
Johannes Berg51648922012-11-22 23:00:18 +0100167 if (status->flag & RX_FLAG_VHT) {
168 len = ALIGN(len, 2);
169 len += 12;
170 }
171
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300172 if (local->hw.radiotap_timestamp.units_pos >= 0) {
173 len = ALIGN(len, 8);
174 len += 12;
175 }
176
Johannes Berga144f372013-07-03 13:34:02 +0200177 if (status->chains) {
178 /* antenna and antenna signal fields */
179 len += 2 * hweight8(status->chains);
180 }
181
Johannes Berg1f7bba72014-11-06 22:56:36 +0100182 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
183 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
184
185 /* vendor presence bitmap */
186 len += 4;
187 /* alignment for fixed 6-byte vendor data header */
188 len = ALIGN(len, 2);
189 /* vendor data header */
190 len += 6;
191 if (WARN_ON(rtap->align == 0))
192 rtap->align = 1;
193 len = ALIGN(len, rtap->align);
194 len += rtap->len + rtap->pad;
195 }
196
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200197 return len;
198}
199
Johannes Berg00ea6de2012-09-05 15:54:51 +0200200/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200201 * ieee80211_add_rx_radiotap_header - add radiotap header
202 *
203 * add a radiotap header containing all the fields which the hardware provided.
204 */
205static void
206ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
207 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200208 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200209 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200210{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200211 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200212 struct ieee80211_radiotap_header *rthdr;
213 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200214 __le32 *it_present;
215 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100216 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200217 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200218 int mpdulen, chain;
219 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100220 struct ieee80211_vendor_radiotap rtap = {};
221
222 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
223 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
224 /* rtap.len and rtap.pad are undone immediately */
225 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
226 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800227
228 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200229 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800230 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200231
232 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100233 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200234 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200235
236 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200237 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200238 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
239 BIT(IEEE80211_RADIOTAP_CHANNEL) |
240 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200241
Johannes Berga144f372013-07-03 13:34:02 +0200242 if (!status->chains)
243 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
244
245 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
246 it_present_val |=
247 BIT(IEEE80211_RADIOTAP_EXT) |
248 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
249 put_unaligned_le32(it_present_val, it_present);
250 it_present++;
251 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
252 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
253 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200254
Johannes Berg1f7bba72014-11-06 22:56:36 +0100255 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
256 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
257 BIT(IEEE80211_RADIOTAP_EXT);
258 put_unaligned_le32(it_present_val, it_present);
259 it_present++;
260 it_present_val = rtap.present;
261 }
262
Johannes Berga144f372013-07-03 13:34:02 +0200263 put_unaligned_le32(it_present_val, it_present);
264
265 pos = (void *)(it_present + 1);
266
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200267 /* the order of the following fields is important */
268
269 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800270 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100271 /* padding */
272 while ((pos - (u8 *)rthdr) & 7)
273 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800274 put_unaligned_le64(
275 ieee80211_calculate_rx_timestamp(local, status,
276 mpdulen, 0),
277 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200278 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200279 pos += 8;
280 }
281
282 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200283 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200284 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100285 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
286 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200287 if (status->flag & RX_FLAG_SHORTPRE)
288 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200289 pos++;
290
291 /* IEEE80211_RADIOTAP_RATE */
Johannes Berg56146182012-11-09 15:07:02 +0100292 if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200293 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100294 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530295 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200296 * added below. The byte here is needed as padding
297 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200298 */
299 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200300 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200301 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200302 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200303 if (status->flag & RX_FLAG_10MHZ)
304 shift = 1;
305 else if (status->flag & RX_FLAG_5MHZ)
306 shift = 2;
307 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200308 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200309 pos++;
310
311 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100312 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200313 pos += 2;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200314 if (status->flag & RX_FLAG_10MHZ)
315 channel_flags |= IEEE80211_CHAN_HALF;
316 else if (status->flag & RX_FLAG_5MHZ)
317 channel_flags |= IEEE80211_CHAN_QUARTER;
318
Johannes Berg57fbcce2016-04-12 15:56:15 +0200319 if (status->band == NL80211_BAND_5GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200320 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Berg56146182012-11-09 15:07:02 +0100321 else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200322 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100323 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200324 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100325 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100326 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100327 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200328 channel_flags |= IEEE80211_CHAN_2GHZ;
329 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200330 pos += 2;
331
332 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200333 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100334 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200335 *pos = status->signal;
336 rthdr->it_present |=
337 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
338 pos++;
339 }
340
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200341 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
342
Johannes Berga144f372013-07-03 13:34:02 +0200343 if (!status->chains) {
344 /* IEEE80211_RADIOTAP_ANTENNA */
345 *pos = status->antenna;
346 pos++;
347 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200348
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200349 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
350
351 /* IEEE80211_RADIOTAP_RX_FLAGS */
352 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100353 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100354 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100355 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100356 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
357 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200358 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100359
360 if (status->flag & RX_FLAG_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200361 unsigned int stbc;
362
Johannes Berg6d744ba2011-01-27 14:13:17 +0100363 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200364 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100365 *pos = 0;
366 if (status->flag & RX_FLAG_SHORT_GI)
367 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
368 if (status->flag & RX_FLAG_40MHZ)
369 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Bergac55d2f2012-05-10 09:09:10 +0200370 if (status->flag & RX_FLAG_HT_GF)
371 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200372 if (status->flag & RX_FLAG_LDPC)
373 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200374 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
375 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100376 pos++;
377 *pos++ = status->rate_idx;
378 }
Johannes Berg4c298672012-07-05 11:34:31 +0200379
380 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
381 u16 flags = 0;
382
383 /* ensure 4 byte alignment */
384 while ((pos - (u8 *)rthdr) & 3)
385 pos++;
386 rthdr->it_present |=
387 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
388 put_unaligned_le32(status->ampdu_reference, pos);
389 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200390 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
391 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
392 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
393 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
394 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
395 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
396 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
397 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
398 put_unaligned_le16(flags, pos);
399 pos += 2;
400 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
401 *pos++ = status->ampdu_delimiter_crc;
402 else
403 *pos++ = 0;
404 *pos++ = 0;
405 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100406
Johannes Berg51648922012-11-22 23:00:18 +0100407 if (status->flag & RX_FLAG_VHT) {
408 u16 known = local->hw.radiotap_vht_details;
409
410 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100411 put_unaligned_le16(known, pos);
412 pos += 2;
413 /* flags */
414 if (status->flag & RX_FLAG_SHORT_GI)
415 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200416 /* in VHT, STBC is binary */
417 if (status->flag & RX_FLAG_STBC_MASK)
418 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200419 if (status->vht_flag & RX_VHT_FLAG_BF)
420 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100421 pos++;
422 /* bandwidth */
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +0200423 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Berg51648922012-11-22 23:00:18 +0100424 *pos++ = 4;
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +0200425 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Berg51648922012-11-22 23:00:18 +0100426 *pos++ = 11;
427 else if (status->flag & RX_FLAG_40MHZ)
428 *pos++ = 1;
429 else /* 20 MHz */
430 *pos++ = 0;
431 /* MCS/NSS */
432 *pos = (status->rate_idx << 4) | status->vht_nss;
433 pos += 4;
434 /* coding field */
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200435 if (status->flag & RX_FLAG_LDPC)
436 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100437 pos++;
438 /* group ID */
439 pos++;
440 /* partial_aid */
441 pos += 2;
442 }
443
Johannes Berg99ee7ca2016-08-29 23:25:17 +0300444 if (local->hw.radiotap_timestamp.units_pos >= 0) {
445 u16 accuracy = 0;
446 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
447
448 rthdr->it_present |=
449 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
450
451 /* ensure 8 byte alignment */
452 while ((pos - (u8 *)rthdr) & 7)
453 pos++;
454
455 put_unaligned_le64(status->device_timestamp, pos);
456 pos += sizeof(u64);
457
458 if (local->hw.radiotap_timestamp.accuracy >= 0) {
459 accuracy = local->hw.radiotap_timestamp.accuracy;
460 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
461 }
462 put_unaligned_le16(accuracy, pos);
463 pos += sizeof(u16);
464
465 *pos++ = local->hw.radiotap_timestamp.units_pos;
466 *pos++ = flags;
467 }
468
Johannes Berga144f372013-07-03 13:34:02 +0200469 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
470 *pos++ = status->chain_signal[chain];
471 *pos++ = chain;
472 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100473
474 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
475 /* ensure 2 byte alignment for the vendor field as required */
476 if ((pos - (u8 *)rthdr) & 1)
477 *pos++ = 0;
478 *pos++ = rtap.oui[0];
479 *pos++ = rtap.oui[1];
480 *pos++ = rtap.oui[2];
481 *pos++ = rtap.subns;
482 put_unaligned_le16(rtap.len, pos);
483 pos += 2;
484 /* align the actual payload as requested */
485 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
486 *pos++ = 0;
487 /* data (and possible padding) already follows */
488 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200489}
490
Johannes Bergb2e77712007-09-26 15:19:39 +0200491/*
492 * This function copies a received frame to all monitor interfaces and
493 * returns a cleaned-up SKB that no longer includes the FCS nor the
494 * radiotap header the driver might have added.
495 */
496static struct sk_buff *
497ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100498 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200499{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200500 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200501 struct ieee80211_sub_if_data *sdata;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100502 int rt_hdrlen, needed_headroom;
Johannes Bergb2e77712007-09-26 15:19:39 +0200503 struct sk_buff *skb, *skb2;
504 struct net_device *prev_dev = NULL;
505 int present_fcs_len = 0;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100506 unsigned int rtap_vendor_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300507 struct ieee80211_mgmt *mgmt;
508 struct ieee80211_sub_if_data *monitor_sdata =
509 rcu_dereference(local->monitor_sdata);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100510
511 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
512 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
513
514 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
515 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200516
517 /*
518 * First, we may need to make a copy of the skb because
519 * (1) we need to modify it for radiotap (if not present), and
520 * (2) the other RX handlers will modify the skb we got.
521 *
522 * We don't need to, of course, if we aren't going to return
523 * the SKB because it has a bad FCS/PLCP checksum.
524 */
Johannes Berg0869aea2009-10-28 10:03:35 +0100525
Johannes Berg30841f52017-04-11 15:38:56 +0200526 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
527 if (unlikely(origskb->len <= FCS_LEN)) {
528 /* driver bug */
529 WARN_ON(1);
530 dev_kfree_skb(origskb);
531 return NULL;
532 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200533 present_fcs_len = FCS_LEN;
Johannes Berg30841f52017-04-11 15:38:56 +0200534 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200535
Johannes Berg1f7bba72014-11-06 22:56:36 +0100536 /* ensure hdr->frame_control and vendor radiotap data are in skb head */
537 if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800538 dev_kfree_skb(origskb);
539 return NULL;
540 }
541
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100542 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg1f7bba72014-11-06 22:56:36 +0100543 if (should_drop_frame(origskb, present_fcs_len,
544 rtap_vendor_space)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200545 dev_kfree_skb(origskb);
546 return NULL;
547 }
548
Johannes Berg30841f52017-04-11 15:38:56 +0200549 remove_monitor_info(origskb, present_fcs_len,
550 rtap_vendor_space);
551 return origskb;
Johannes Bergb2e77712007-09-26 15:19:39 +0200552 }
553
Helmut Schaa751413e2012-12-05 14:36:12 +0100554 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +0100555 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
556 needed_headroom = rt_hdrlen - rtap_vendor_space;
Helmut Schaa751413e2012-12-05 14:36:12 +0100557
Johannes Berg1f7bba72014-11-06 22:56:36 +0100558 if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200559 /* only need to expand headroom if necessary */
560 skb = origskb;
561 origskb = NULL;
562
563 /*
564 * This shouldn't trigger often because most devices have an
565 * RX header they pull before we get here, and that should
566 * be big enough for our radiotap information. We should
567 * probably export the length to drivers so that we can have
568 * them allocate enough headroom to start with.
569 */
570 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100571 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200572 dev_kfree_skb(skb);
573 return NULL;
574 }
575 } else {
576 /*
577 * Need to make a copy and possibly remove radiotap header
578 * and FCS from the original.
579 */
580 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
Johannes Berg30841f52017-04-11 15:38:56 +0200581 remove_monitor_info(origskb, present_fcs_len,
582 rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200583
584 if (!skb)
585 return origskb;
586 }
587
Johannes Berg0869aea2009-10-28 10:03:35 +0100588 /* prepend radiotap information */
Johannes Berg1f7bba72014-11-06 22:56:36 +0100589 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
Johannes Bergb2e77712007-09-26 15:19:39 +0200590
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100591 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200592 skb->ip_summed = CHECKSUM_UNNECESSARY;
593 skb->pkt_type = PACKET_OTHERHOST;
594 skb->protocol = htons(ETH_P_802_2);
595
596 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200597 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200598 continue;
599
Aviya Erenfeldd8212182016-08-29 23:25:15 +0300600 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
Michael Wu3d30d942008-01-31 19:48:27 +0100601 continue;
602
Johannes Berg9607e6b2009-12-23 13:15:31 +0100603 if (!ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100604 continue;
605
Johannes Bergb2e77712007-09-26 15:19:39 +0200606 if (prev_dev) {
607 skb2 = skb_clone(skb, GFP_ATOMIC);
608 if (skb2) {
609 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400610 netif_receive_skb(skb2);
Johannes Bergb2e77712007-09-26 15:19:39 +0200611 }
612 }
613
614 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +0200615 ieee80211_rx_stats(sdata->dev, skb->len);
Johannes Bergb2e77712007-09-26 15:19:39 +0200616 }
617
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300618 mgmt = (void *)skb->data;
619 if (monitor_sdata &&
620 skb->len >= IEEE80211_MIN_ACTION_SIZE + 1 + VHT_MUMIMO_GROUPS_DATA_LEN &&
621 ieee80211_is_action(mgmt->frame_control) &&
622 mgmt->u.action.category == WLAN_CATEGORY_VHT &&
623 mgmt->u.action.u.vht_group_notif.action_code == WLAN_VHT_ACTION_GROUPID_MGMT &&
624 is_valid_ether_addr(monitor_sdata->u.mntr.mu_follow_addr) &&
625 ether_addr_equal(mgmt->da, monitor_sdata->u.mntr.mu_follow_addr)) {
626 struct sk_buff *mu_skb = skb_copy(skb, GFP_ATOMIC);
627
628 if (mu_skb) {
629 mu_skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
630 skb_queue_tail(&monitor_sdata->skb_queue, mu_skb);
631 ieee80211_queue_work(&local->hw, &monitor_sdata->work);
632 }
633 }
634
Johannes Bergb2e77712007-09-26 15:19:39 +0200635 if (prev_dev) {
636 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400637 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200638 } else
639 dev_kfree_skb(skb);
640
641 return origskb;
642}
643
Johannes Berg5cf121c2008-02-25 16:27:43 +0100644static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200645{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700646 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200647 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200648 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200649
650 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700651 if (ieee80211_is_data_qos(hdr->frame_control)) {
652 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200653 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700654 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200655 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200656 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200657
658 seqno_idx = tid;
659 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200660 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200661 /*
662 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
663 *
664 * Sequence numbers for management frames, QoS data
665 * frames with a broadcast/multicast address in the
666 * Address 1 field, and all non-QoS data frames sent
667 * by QoS STAs are assigned using an additional single
668 * modulo-4096 counter, [...]
669 *
670 * We also use that counter for non-QoS STAs.
671 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100672 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200673 security_idx = 0;
674 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100675 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200676 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200677 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200678
Johannes Berg9e262972011-07-07 18:45:03 +0200679 rx->seqno_idx = seqno_idx;
680 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200681 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
682 * For now, set skb->priority to 0 for other cases. */
683 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100684}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200685
Johannes Bergd1c3a372009-01-07 00:26:10 +0100686/**
687 * DOC: Packet alignment
688 *
689 * Drivers always need to pass packets that are aligned to two-byte boundaries
690 * to the stack.
691 *
692 * Additionally, should, if possible, align the payload data in a way that
693 * guarantees that the contained IP header is aligned to a four-byte
694 * boundary. In the case of regular frames, this simply means aligning the
695 * payload to a four-byte boundary (because either the IP header is directly
696 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100697 * in front of it). If the payload data is not properly aligned and the
698 * architecture doesn't support efficient unaligned operations, mac80211
699 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100700 *
701 * With A-MSDU frames, however, the payload data address must yield two modulo
702 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
703 * push the IP header further back to a multiple of four again. Thankfully, the
704 * specs were sane enough this time around to require padding each A-MSDU
705 * subframe to a length that is a multiple of four.
706 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300707 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100708 * the payload is not supported, the driver is required to move the 802.11
709 * header to be directly in front of the payload in that case.
710 */
711static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100712{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100713#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100714 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100715#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200716}
717
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200718
Johannes Berg571ecf62007-07-27 15:43:22 +0200719/* rx handlers */
720
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200721static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
722{
723 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
724
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100725 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200726 return 0;
727
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100728 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200729}
730
731
732static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
733{
734 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
735
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100736 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200737 return 0;
738
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100739 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200740}
741
742
743/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
744static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
745{
746 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
747 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200748 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200749
Johannes Berg1df332e2012-10-26 00:09:11 +0200750 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200751 return -1;
752
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100753 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200754 return -1; /* not a robust management frame */
755
756 mmie = (struct ieee80211_mmie *)
757 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200758 if (mmie->element_id == WLAN_EID_MMIE &&
759 mmie->length == sizeof(*mmie) - 2)
760 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200761
Jouni Malinen56c52da2015-01-24 19:52:08 +0200762 mmie16 = (struct ieee80211_mmie_16 *)
763 (skb->data + skb->len - sizeof(*mmie16));
764 if (skb->len >= 24 + sizeof(*mmie16) &&
765 mmie16->element_id == WLAN_EID_MMIE &&
766 mmie16->length == sizeof(*mmie16) - 2)
767 return le16_to_cpu(mmie16->key_id);
768
769 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200770}
771
Johannes Berg2c61cf92016-03-17 15:02:55 +0200772static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
773 struct sk_buff *skb)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200774{
775 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
776 __le16 fc;
777 int hdrlen;
778 u8 keyid;
779
780 fc = hdr->frame_control;
781 hdrlen = ieee80211_hdrlen(fc);
782
783 if (skb->len < hdrlen + cs->hdr_len)
784 return -EINVAL;
785
786 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
787 keyid &= cs->key_idx_mask;
788 keyid >>= cs->key_idx_shift;
789
790 return keyid;
791}
792
Johannes Berg1df332e2012-10-26 00:09:11 +0200793static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100794{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700795 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +0100796 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100797
Harvey Harrisona7767f92008-07-02 16:30:51 -0700798 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700799 if (is_multicast_ether_addr(hdr->addr1)) {
800 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +0200801 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700802 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000803 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700804 return RX_DROP_MONITOR;
805 } else {
806 if (!ieee80211_has_a4(hdr->frame_control))
807 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000808 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700809 return RX_DROP_MONITOR;
810 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100811 }
812
813 /* If there is not an established peer link and this is not a peer link
814 * establisment frame, beacon or probe, drop the frame.
815 */
816
Javier Cardona57cf8042011-05-13 10:45:43 -0700817 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100818 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100819
Harvey Harrisona7767f92008-07-02 16:30:51 -0700820 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100821 return RX_DROP_MONITOR;
822
Harvey Harrisona7767f92008-07-02 16:30:51 -0700823 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700824 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +0200825
826 /* make sure category field is present */
827 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
828 return RX_DROP_MONITOR;
829
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100830 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700831 category = mgmt->u.action.category;
832 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +0200833 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100834 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100835 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100836 }
837
Harvey Harrisona7767f92008-07-02 16:30:51 -0700838 if (ieee80211_is_probe_req(hdr->frame_control) ||
839 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -0700840 ieee80211_is_beacon(hdr->frame_control) ||
841 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -0700842 return RX_CONTINUE;
843
844 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -0700845 }
846
Johannes Berg902acc72008-02-23 15:17:19 +0100847 return RX_CONTINUE;
848}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100849
Johannes Bergfb4ea052016-01-28 16:19:24 +0200850static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
851 int index)
852{
853 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
854 struct sk_buff *tail = skb_peek_tail(frames);
855 struct ieee80211_rx_status *status;
856
Sara Sharon06470f72016-01-28 16:19:25 +0200857 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
858 return true;
859
Johannes Bergfb4ea052016-01-28 16:19:24 +0200860 if (!tail)
861 return false;
862
863 status = IEEE80211_SKB_RXCB(tail);
864 if (status->flag & RX_FLAG_AMSDU_MORE)
865 return false;
866
867 return true;
868}
869
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200870static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100871 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000872 int index,
873 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100874{
Michal Kazior83eb9352014-07-16 12:09:31 +0200875 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
876 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +0100877 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100878
Johannes Bergdd318572010-11-29 11:09:16 +0100879 lockdep_assert_held(&tid_agg_rx->reorder_lock);
880
Michal Kazior83eb9352014-07-16 12:09:31 +0200881 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100882 goto no_frame;
883
Johannes Bergfb4ea052016-01-28 16:19:24 +0200884 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +0200885 __skb_queue_purge(skb_list);
886 goto no_frame;
887 }
888
889 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100890 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +0200891 while ((skb = __skb_dequeue(skb_list))) {
892 status = IEEE80211_SKB_RXCB(skb);
893 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
894 __skb_queue_tail(frames, skb);
895 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100896
897no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +0200898 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +0100899 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100900}
901
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200902static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100903 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000904 u16 head_seq_num,
905 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100906{
907 int index;
908
Johannes Bergdd318572010-11-29 11:09:16 +0100909 lockdep_assert_held(&tid_agg_rx->reorder_lock);
910
Johannes Berg9a886582013-02-15 19:25:00 +0100911 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +0200912 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000913 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
914 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100915 }
916}
917
918/*
919 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
920 * the skb was added to the buffer longer than this time ago, the earlier
921 * frames that have not yet been received are assumed to be lost and the skb
922 * can be released for processing. This may also release other skb's from the
923 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200924 *
925 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100926 */
927#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
928
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200929static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000930 struct tid_ampdu_rx *tid_agg_rx,
931 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200932{
Michal Kazior83eb9352014-07-16 12:09:31 +0200933 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200934
Johannes Bergdd318572010-11-29 11:09:16 +0100935 lockdep_assert_held(&tid_agg_rx->reorder_lock);
936
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200937 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +0200938 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +0200939 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +0200940 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200941 /*
942 * No buffers ready to be released, but check whether any
943 * frames in the reorder buffer have timed out.
944 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200945 int skipped = 1;
946 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
947 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +0200948 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200949 skipped++;
950 continue;
951 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -0700952 if (skipped &&
953 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200954 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200955 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200956
Michal Kazior83eb9352014-07-16 12:09:31 +0200957 /* don't leave incomplete A-MSDUs around */
958 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
959 i = (i + 1) % tid_agg_rx->buf_size)
960 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
961
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200962 ht_dbg_ratelimited(sdata,
963 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000964 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
965 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200966
967 /*
968 * Increment the head seq# also for the skipped slots.
969 */
970 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +0100971 (tid_agg_rx->head_seq_num +
972 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200973 skipped = 0;
974 }
Johannes Bergfb4ea052016-01-28 16:19:24 +0200975 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000976 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
977 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +0200978 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200979 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200980
981 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +0200982 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200983
984 for (; j != (index - 1) % tid_agg_rx->buf_size;
985 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +0200986 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200987 break;
988 }
989
990 set_release_timer:
991
Johannes Berg788211d2015-04-01 14:20:42 +0200992 if (!tid_agg_rx->removed)
993 mod_timer(&tid_agg_rx->reorder_timer,
994 tid_agg_rx->reorder_time[j] + 1 +
995 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200996 } else {
997 del_timer(&tid_agg_rx->reorder_timer);
998 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200999}
1000
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001001/*
1002 * As this function belongs to the RX path it must be under
1003 * rcu_read_lock protection. It returns false if the frame
1004 * can be processed immediately, true if it was consumed.
1005 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001006static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001007 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001008 struct sk_buff *skb,
1009 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001010{
1011 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +02001012 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001013 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1014 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1015 u16 head_seq_num, buf_size;
1016 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001017 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001018
Johannes Bergdd318572010-11-29 11:09:16 +01001019 spin_lock(&tid_agg_rx->reorder_lock);
1020
Michal Kazior4549cf22014-09-02 14:05:10 +02001021 /*
1022 * Offloaded BA sessions have no known starting sequence number so pick
1023 * one from first Rxed frame for this tid after BA was started.
1024 */
1025 if (unlikely(tid_agg_rx->auto_seq)) {
1026 tid_agg_rx->auto_seq = false;
1027 tid_agg_rx->ssn = mpdu_seq_num;
1028 tid_agg_rx->head_seq_num = mpdu_seq_num;
1029 }
1030
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001031 buf_size = tid_agg_rx->buf_size;
1032 head_seq_num = tid_agg_rx->head_seq_num;
1033
Sara Sharonb7540d82017-02-06 15:28:42 +02001034 /*
1035 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1036 * be reordered.
1037 */
1038 if (unlikely(!tid_agg_rx->started)) {
1039 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1040 ret = false;
1041 goto out;
1042 }
1043 tid_agg_rx->started = true;
1044 }
1045
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001046 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001047 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001048 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001049 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001050 }
1051
1052 /*
1053 * If frame the sequence number exceeds our buffering window
1054 * size release some previous frames to make room for this one.
1055 */
Johannes Berg9a886582013-02-15 19:25:00 +01001056 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1057 head_seq_num = ieee80211_sn_inc(
1058 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001059 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001060 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001061 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001062 }
1063
1064 /* Now the new frame is always in the range of the reordering buffer */
1065
Karl Beldan2e3049b2013-10-24 15:53:32 +02001066 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001067
1068 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001069 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001070 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001071 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001072 }
1073
1074 /*
1075 * If the current MPDU is in the right order and nothing else
1076 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001077 * If it is first but there's something stored, we may be able
1078 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001079 */
1080 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1081 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001082 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1083 tid_agg_rx->head_seq_num =
1084 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001085 ret = false;
1086 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001087 }
1088
1089 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001090 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1091 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1092 tid_agg_rx->reorder_time[index] = jiffies;
1093 tid_agg_rx->stored_mpdu_num++;
1094 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1095 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001096
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001097 out:
1098 spin_unlock(&tid_agg_rx->reorder_lock);
1099 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001100}
1101
1102/*
1103 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1104 * true if the MPDU was buffered, false if it should be processed.
1105 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001106static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1107 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001108{
Johannes Berg2569a822009-11-25 17:46:17 +01001109 struct sk_buff *skb = rx->skb;
1110 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001111 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001112 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001113 struct tid_ampdu_rx *tid_agg_rx;
1114 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001115 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001116
Johannes Berg051a41f2013-11-20 11:28:27 +01001117 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1118 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001119 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001120
1121 /*
1122 * filter the QoS data rx stream according to
1123 * STA/TID and check if this STA/TID is on aggregation
1124 */
1125
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001126 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001127 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001128
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001129 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1130 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001131 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1132
Johannes Berga87f7362010-06-10 10:21:38 +02001133 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001134 if (!tid_agg_rx) {
1135 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1136 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1137 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1138 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1139 WLAN_BACK_RECIPIENT,
1140 WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga87f7362010-06-10 10:21:38 +02001141 goto dont_reorder;
Johannes Bergbfe40fa2016-08-29 23:25:18 +03001142 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001143
1144 /* qos null data frames are excluded */
1145 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001146 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001147
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001148 /* not part of a BA session */
1149 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1150 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1151 goto dont_reorder;
1152
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001153 /* new, potentially un-ordered, ampdu frame - process it */
1154
1155 /* reset session timer */
1156 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001157 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001158
1159 /* if this mpdu is fragmented - terminate rx aggregation session */
1160 sc = le16_to_cpu(hdr->seq_ctrl);
1161 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Bergc1475ca2010-06-10 10:21:37 +02001162 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg344eec62010-06-10 10:21:36 +02001163 skb_queue_tail(&rx->sdata->skb_queue, skb);
1164 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001165 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001166 }
1167
Johannes Berga87f7362010-06-10 10:21:38 +02001168 /*
1169 * No locking needed -- we will only ever process one
1170 * RX packet at a time, and thus own tid_agg_rx. All
1171 * other code manipulating it needs to (and does) make
1172 * sure that we cannot get to it any more before doing
1173 * anything with it.
1174 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001175 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1176 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001177 return;
1178
1179 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001180 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001181}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001182
Johannes Berg49461622008-06-30 15:10:45 +02001183static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001184ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001185{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001186 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001187 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001188
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001189 if (status->flag & RX_FLAG_DUP_VALIDATED)
1190 return RX_CONTINUE;
1191
Johannes Berg6b0f3272013-07-11 22:33:26 +02001192 /*
1193 * Drop duplicate 802.11 retransmissions
1194 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1195 */
Johannes Berg03954422014-11-11 16:49:25 +01001196
1197 if (rx->skb->len < 24)
1198 return RX_CONTINUE;
1199
1200 if (ieee80211_is_ctl(hdr->frame_control) ||
1201 ieee80211_is_qos_nullfunc(hdr->frame_control) ||
1202 is_multicast_ether_addr(hdr->addr1))
1203 return RX_CONTINUE;
1204
Johannes Berga732fa72015-10-14 18:27:07 +02001205 if (!rx->sta)
1206 return RX_CONTINUE;
1207
1208 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1209 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1210 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001211 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001212 return RX_DROP_UNUSABLE;
1213 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1214 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001215 }
1216
Johannes Berg03954422014-11-11 16:49:25 +01001217 return RX_CONTINUE;
1218}
1219
1220static ieee80211_rx_result debug_noinline
1221ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1222{
1223 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1224
Johannes Berg571ecf62007-07-27 15:43:22 +02001225 /* Drop disallowed frame classes based on STA auth/assoc state;
1226 * IEEE 802.11, Chap 5.5.
1227 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001228 * mac80211 filters only based on association state, i.e. it drops
1229 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001230 * deauth/disassoc frames when needed. In addition, hostapd is
1231 * responsible for filtering on both auth and assoc states.
1232 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001233
Johannes Berg902acc72008-02-23 15:17:19 +01001234 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001235 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001236
Harvey Harrisona7767f92008-07-02 16:30:51 -07001237 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1238 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001239 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Bill Jordan1be7fe82010-10-01 11:20:41 -04001240 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001241 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001242 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001243 /*
1244 * accept port control frames from the AP even when it's not
1245 * yet marked ASSOC to prevent a race where we don't set the
1246 * assoc bit quickly enough before it sends the first frame
1247 */
1248 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001249 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001250 unsigned int hdrlen;
1251 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001252
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001253 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1254
1255 if (rx->skb->len < hdrlen + 8)
1256 return RX_DROP_MONITOR;
1257
1258 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1259 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001260 return RX_CONTINUE;
1261 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001262
1263 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1264 cfg80211_rx_spurious_frame(rx->sdata->dev,
1265 hdr->addr2,
1266 GFP_ATOMIC))
1267 return RX_DROP_UNUSABLE;
1268
Johannes Berge4c26ad2008-01-31 19:48:21 +01001269 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001270 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001271
Johannes Berg9ae54c82008-01-31 19:48:20 +01001272 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001273}
1274
1275
Johannes Berg49461622008-06-30 15:10:45 +02001276static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001277ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1278{
1279 struct ieee80211_local *local;
1280 struct ieee80211_hdr *hdr;
1281 struct sk_buff *skb;
1282
1283 local = rx->local;
1284 skb = rx->skb;
1285 hdr = (struct ieee80211_hdr *) skb->data;
1286
1287 if (!local->pspolling)
1288 return RX_CONTINUE;
1289
1290 if (!ieee80211_has_fromds(hdr->frame_control))
1291 /* this is not from AP */
1292 return RX_CONTINUE;
1293
1294 if (!ieee80211_is_data(hdr->frame_control))
1295 return RX_CONTINUE;
1296
1297 if (!ieee80211_has_moredata(hdr->frame_control)) {
1298 /* AP has no more frames buffered for us */
1299 local->pspolling = false;
1300 return RX_CONTINUE;
1301 }
1302
1303 /* more data bit is set, let's request a new frame from the AP */
1304 ieee80211_send_pspoll(local, rx->sdata);
1305
1306 return RX_CONTINUE;
1307}
1308
Marco Porschd012a602012-10-10 12:39:50 -07001309static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001310{
Johannes Berg133b8222008-09-16 14:18:59 +02001311 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001312 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001313 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001314 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001315
Marco Porschd012a602012-10-10 12:39:50 -07001316 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1317 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1318 ps = &sdata->bss->ps;
1319 else
1320 return;
1321
1322 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001323 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001324 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001325 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001326 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1327 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001328
Johannes Berg17c18bf2015-03-21 15:25:43 +01001329 ieee80211_clear_fast_xmit(sta);
1330
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001331 if (!sta->sta.txq[0])
1332 return;
1333
1334 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001335 if (txq_has_queue(sta->sta.txq[tid]))
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001336 set_bit(tid, &sta->txq_buffered_tids);
1337 else
1338 clear_bit(tid, &sta->txq_buffered_tids);
1339 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001340}
1341
Marco Porschd012a602012-10-10 12:39:50 -07001342static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001343{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001344 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1345 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001346
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001347 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001348 /*
1349 * Clear the flag only if the other one is still set
1350 * so that the TX path won't start TX'ing new frames
1351 * directly ... In the case that the driver flag isn't
1352 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1353 */
1354 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001355 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1356 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001357 return;
1358 }
1359
Johannes Berg5ac2e352014-05-27 16:32:27 +02001360 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1361 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001362 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001363}
1364
Johannes Bergcf471612015-06-16 16:16:38 +02001365int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001366{
Johannes Bergcf471612015-06-16 16:16:38 +02001367 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001368 bool in_ps;
1369
Johannes Bergcf471612015-06-16 16:16:38 +02001370 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001371
1372 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001373 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001374 if ((start && in_ps) || (!start && !in_ps))
1375 return -EINVAL;
1376
1377 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001378 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001379 else
Johannes Bergcf471612015-06-16 16:16:38 +02001380 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001381
1382 return 0;
1383}
1384EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1385
Johannes Berg46fa38e2016-05-03 16:58:00 +03001386void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1387{
1388 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1389
1390 if (test_sta_flag(sta, WLAN_STA_SP))
1391 return;
1392
1393 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1394 ieee80211_sta_ps_deliver_poll_response(sta);
1395 else
1396 set_sta_flag(sta, WLAN_STA_PSPOLL);
1397}
1398EXPORT_SYMBOL(ieee80211_sta_pspoll);
1399
1400void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1401{
1402 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001403 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg46fa38e2016-05-03 16:58:00 +03001404
1405 /*
Emmanuel Grumbach0aa419e2016-10-18 23:12:10 +03001406 * If this AC is not trigger-enabled do nothing unless the
1407 * driver is calling us after it already checked.
Johannes Berg46fa38e2016-05-03 16:58:00 +03001408 *
1409 * NB: This could/should check a separate bitmap of trigger-
1410 * enabled queues, but for now we only implement uAPSD w/o
1411 * TSPEC changes to the ACs, so they're always the same.
1412 */
Emmanuel Grumbachf438ceb2016-10-18 23:12:12 +03001413 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1414 tid != IEEE80211_NUM_TIDS)
Johannes Berg46fa38e2016-05-03 16:58:00 +03001415 return;
1416
1417 /* if we are in a service period, do nothing */
1418 if (test_sta_flag(sta, WLAN_STA_SP))
1419 return;
1420
1421 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1422 ieee80211_sta_ps_deliver_uapsd(sta);
1423 else
1424 set_sta_flag(sta, WLAN_STA_UAPSD);
1425}
1426EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1427
Johannes Berg49461622008-06-30 15:10:45 +02001428static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001429ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1430{
1431 struct ieee80211_sub_if_data *sdata = rx->sdata;
1432 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1433 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001434
Johannes Berg5c900672015-04-22 14:48:34 +02001435 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001436 return RX_CONTINUE;
1437
1438 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1439 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1440 return RX_CONTINUE;
1441
1442 /*
1443 * The device handles station powersave, so don't do anything about
1444 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1445 * it to mac80211 since they're handled.)
1446 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001447 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001448 return RX_CONTINUE;
1449
1450 /*
1451 * Don't do anything if the station isn't already asleep. In
1452 * the uAPSD case, the station will probably be marked asleep,
1453 * in the PS-Poll case the station must be confused ...
1454 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001455 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001456 return RX_CONTINUE;
1457
1458 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001459 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001460
1461 /* Free PS Poll skb here instead of returning RX_DROP that would
1462 * count as an dropped frame. */
1463 dev_kfree_skb(rx->skb);
1464
1465 return RX_QUEUED;
1466 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1467 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1468 ieee80211_has_pm(hdr->frame_control) &&
1469 (ieee80211_is_data_qos(hdr->frame_control) ||
1470 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001471 u8 tid;
1472
Johannes Berg47086fc2011-09-29 16:04:33 +02001473 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg47086fc2011-09-29 16:04:33 +02001474
Johannes Berg46fa38e2016-05-03 16:58:00 +03001475 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001476 }
1477
1478 return RX_CONTINUE;
1479}
1480
1481static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001482ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001483{
1484 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001485 struct sk_buff *skb = rx->skb;
1486 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1487 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001488 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001489
1490 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001491 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001492
Johannes Bergb291ba12009-07-10 15:29:03 +02001493 /*
1494 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001495 * BSSID and for station already AUTHORIZED to avoid keeping the
1496 * current IBSS network alive in cases where other STAs start
1497 * using different BSSID. This will also give the station another
1498 * chance to restart the authentication/authorization in case
1499 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001500 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001501 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001502 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001503 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001504 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1505 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001506 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001507 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001508 !is_multicast_ether_addr(hdr->addr1))
1509 sta->rx_stats.last_rate =
1510 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001511 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001512 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001513 sta->rx_stats.last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +02001514 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1515 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001516 * Mesh beacons will update last_rx when if they are found to
1517 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001518 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001519 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001520 if (ieee80211_is_data(hdr->frame_control))
1521 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001522 }
1523
Kalle Valo3cf335d2009-03-22 21:57:06 +02001524 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1525 ieee80211_sta_rx_notify(rx->sdata, hdr);
1526
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001527 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001528
1529 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001530 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001531 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1532
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001533 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001534 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001535 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001536 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001537
Felix Fietkauef0621e2013-04-22 16:29:31 +02001538 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001539 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001540 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1541 int signal = status->chain_signal[i];
1542
1543 if (!(status->chains & BIT(i)))
1544 continue;
1545
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001546 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001547 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001548 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001549 }
1550 }
1551
Johannes Berg72eaa432008-11-26 15:02:58 +01001552 /*
1553 * Change STA power saving mode only at the end of a frame
1554 * exchange sequence.
1555 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001556 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001557 !ieee80211_has_morefrags(hdr->frame_control) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001558 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001559 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Johannes Bergb4ba5442014-01-24 14:41:44 +01001560 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1561 /* PM bit is only checked in frames where it isn't reserved,
1562 * in AP mode it's reserved in non-bufferable management frames
1563 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1564 */
1565 (!ieee80211_is_mgmt(hdr->frame_control) ||
1566 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001567 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001568 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001569 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001570 } else {
1571 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001572 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001573 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001574 }
1575
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001576 /* mesh power save support */
1577 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1578 ieee80211_mps_rx_h_sta_process(sta, hdr);
1579
Johannes Berg22403de2009-10-30 12:55:03 +01001580 /*
1581 * Drop (qos-)data::nullfunc frames silently, since they
1582 * are used only to control station power saving mode.
1583 */
1584 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1585 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001586 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001587
1588 /*
1589 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001590 * that was not moved to a 4-addr STA vlan yet send
1591 * the event to userspace and for older hostapd drop
1592 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001593 */
1594 if (ieee80211_has_a4(hdr->frame_control) &&
1595 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1596 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001597 !rx->sdata->u.vlan.sta))) {
1598 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1599 cfg80211_rx_unexpected_4addr_frame(
1600 rx->sdata->dev, sta->sta.addr,
1601 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001602 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001603 }
Johannes Berg22403de2009-10-30 12:55:03 +01001604 /*
1605 * Update counter and free packet here to avoid
1606 * counting this as a dropped packed.
1607 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001608 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001609 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001610 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001611 }
1612
Johannes Berg9ae54c82008-01-31 19:48:20 +01001613 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001614} /* ieee80211_rx_h_sta_process */
1615
Johan Almbladh86c228a2013-08-14 15:29:46 +02001616static ieee80211_rx_result debug_noinline
1617ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1618{
1619 struct sk_buff *skb = rx->skb;
1620 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1621 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1622 int keyidx;
1623 int hdrlen;
1624 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1625 struct ieee80211_key *sta_ptk = NULL;
1626 int mmie_keyidx = -1;
1627 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001628 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001629
1630 /*
1631 * Key selection 101
1632 *
1633 * There are four types of keys:
1634 * - GTK (group keys)
1635 * - IGTK (group keys for management frames)
1636 * - PTK (pairwise keys)
1637 * - STK (station-to-station pairwise keys)
1638 *
1639 * When selecting a key, we have to distinguish between multicast
1640 * (including broadcast) and unicast frames, the latter can only
1641 * use PTKs and STKs while the former always use GTKs and IGTKs.
1642 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1643 * unicast frames can also use key indices like GTKs. Hence, if we
1644 * don't have a PTK/STK we check the key index for a WEP key.
1645 *
1646 * Note that in a regular BSS, multicast frames are sent by the
1647 * AP only, associated stations unicast the frame to the AP first
1648 * which then multicasts it on their behalf.
1649 *
1650 * There is also a slight problem in IBSS mode: GTKs are negotiated
1651 * with each station, that is something we don't currently handle.
1652 * The spec seems to expect that one negotiates the same key with
1653 * every station but there's no such requirement; VLANs could be
1654 * possible.
1655 */
1656
Johan Almbladh86c228a2013-08-14 15:29:46 +02001657 /* start without a key */
1658 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001659 fc = hdr->frame_control;
1660
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001661 if (rx->sta) {
1662 int keyid = rx->sta->ptk_idx;
1663
1664 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1665 cs = rx->sta->cipher_scheme;
Johannes Berg2c61cf92016-03-17 15:02:55 +02001666 keyid = ieee80211_get_cs_keyid(cs, rx->skb);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001667 if (unlikely(keyid < 0))
1668 return RX_DROP_UNUSABLE;
1669 }
1670 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1671 }
1672
Johan Almbladh86c228a2013-08-14 15:29:46 +02001673 if (!ieee80211_has_protected(fc))
1674 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1675
1676 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1677 rx->key = sta_ptk;
1678 if ((status->flag & RX_FLAG_DECRYPTED) &&
1679 (status->flag & RX_FLAG_IV_STRIPPED))
1680 return RX_CONTINUE;
1681 /* Skip decryption if the frame is not protected. */
1682 if (!ieee80211_has_protected(fc))
1683 return RX_CONTINUE;
1684 } else if (mmie_keyidx >= 0) {
1685 /* Broadcast/multicast robust management frame / BIP */
1686 if ((status->flag & RX_FLAG_DECRYPTED) &&
1687 (status->flag & RX_FLAG_IV_STRIPPED))
1688 return RX_CONTINUE;
1689
1690 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1691 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1692 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09001693 if (rx->sta) {
1694 if (ieee80211_is_group_privacy_action(skb) &&
1695 test_sta_flag(rx->sta, WLAN_STA_MFP))
1696 return RX_DROP_MONITOR;
1697
Johan Almbladh86c228a2013-08-14 15:29:46 +02001698 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09001699 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001700 if (!rx->key)
1701 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1702 } else if (!ieee80211_has_protected(fc)) {
1703 /*
1704 * The frame was not protected, so skip decryption. However, we
1705 * need to set rx->key if there is a key that could have been
1706 * used so that the frame may be dropped if encryption would
1707 * have been expected.
1708 */
1709 struct ieee80211_key *key = NULL;
1710 struct ieee80211_sub_if_data *sdata = rx->sdata;
1711 int i;
1712
1713 if (ieee80211_is_mgmt(fc) &&
1714 is_multicast_ether_addr(hdr->addr1) &&
1715 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1716 rx->key = key;
1717 else {
1718 if (rx->sta) {
1719 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1720 key = rcu_dereference(rx->sta->gtk[i]);
1721 if (key)
1722 break;
1723 }
1724 }
1725 if (!key) {
1726 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1727 key = rcu_dereference(sdata->keys[i]);
1728 if (key)
1729 break;
1730 }
1731 }
1732 if (key)
1733 rx->key = key;
1734 }
1735 return RX_CONTINUE;
1736 } else {
1737 u8 keyid;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001738
Johan Almbladh86c228a2013-08-14 15:29:46 +02001739 /*
1740 * The device doesn't give us the IV so we won't be
1741 * able to look up the key. That's ok though, we
1742 * don't need to decrypt the frame, we just won't
1743 * be able to keep statistics accurate.
1744 * Except for key threshold notifications, should
1745 * we somehow allow the driver to tell us which key
1746 * the hardware used if this flag is set?
1747 */
1748 if ((status->flag & RX_FLAG_DECRYPTED) &&
1749 (status->flag & RX_FLAG_IV_STRIPPED))
1750 return RX_CONTINUE;
1751
1752 hdrlen = ieee80211_hdrlen(fc);
1753
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001754 if (cs) {
Johannes Berg2c61cf92016-03-17 15:02:55 +02001755 keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001756
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001757 if (unlikely(keyidx < 0))
1758 return RX_DROP_UNUSABLE;
1759 } else {
1760 if (rx->skb->len < 8 + hdrlen)
1761 return RX_DROP_UNUSABLE; /* TODO: count this? */
1762 /*
1763 * no need to call ieee80211_wep_get_keyidx,
1764 * it verifies a bunch of things we've done already
1765 */
1766 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1767 keyidx = keyid >> 6;
1768 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001769
1770 /* check per-station GTK first, if multicast packet */
1771 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1772 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1773
1774 /* if not found, try default key */
1775 if (!rx->key) {
1776 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1777
1778 /*
1779 * RSNA-protected unicast frames should always be
1780 * sent with pairwise or station-to-station keys,
1781 * but for WEP we allow using a key index as well.
1782 */
1783 if (rx->key &&
1784 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1785 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1786 !is_multicast_ether_addr(hdr->addr1))
1787 rx->key = NULL;
1788 }
1789 }
1790
1791 if (rx->key) {
1792 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1793 return RX_DROP_MONITOR;
1794
Johan Almbladh86c228a2013-08-14 15:29:46 +02001795 /* TODO: add threshold stuff again */
1796 } else {
1797 return RX_DROP_MONITOR;
1798 }
1799
1800 switch (rx->key->conf.cipher) {
1801 case WLAN_CIPHER_SUITE_WEP40:
1802 case WLAN_CIPHER_SUITE_WEP104:
1803 result = ieee80211_crypto_wep_decrypt(rx);
1804 break;
1805 case WLAN_CIPHER_SUITE_TKIP:
1806 result = ieee80211_crypto_tkip_decrypt(rx);
1807 break;
1808 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001809 result = ieee80211_crypto_ccmp_decrypt(
1810 rx, IEEE80211_CCMP_MIC_LEN);
1811 break;
1812 case WLAN_CIPHER_SUITE_CCMP_256:
1813 result = ieee80211_crypto_ccmp_decrypt(
1814 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001815 break;
1816 case WLAN_CIPHER_SUITE_AES_CMAC:
1817 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1818 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02001819 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1820 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1821 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001822 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1823 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1824 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1825 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001826 case WLAN_CIPHER_SUITE_GCMP:
1827 case WLAN_CIPHER_SUITE_GCMP_256:
1828 result = ieee80211_crypto_gcmp_decrypt(rx);
1829 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001830 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001831 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001832 }
1833
1834 /* the hdr variable is invalid after the decrypt handlers */
1835
1836 /* either the frame has been decrypted or will be dropped */
1837 status->flag |= RX_FLAG_DECRYPTED;
1838
1839 return result;
1840}
1841
Johannes Berg571ecf62007-07-27 15:43:22 +02001842static inline struct ieee80211_fragment_entry *
1843ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1844 unsigned int frag, unsigned int seq, int rx_queue,
1845 struct sk_buff **skb)
1846{
1847 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02001848
Johannes Berg571ecf62007-07-27 15:43:22 +02001849 entry = &sdata->fragments[sdata->fragment_next++];
1850 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1851 sdata->fragment_next = 0;
1852
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001853 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02001854 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02001855
1856 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1857 *skb = NULL;
1858 entry->first_frag_time = jiffies;
1859 entry->seq = seq;
1860 entry->rx_queue = rx_queue;
1861 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01001862 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02001863 entry->extra_len = 0;
1864
1865 return entry;
1866}
1867
1868static inline struct ieee80211_fragment_entry *
1869ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001870 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02001871 int rx_queue, struct ieee80211_hdr *hdr)
1872{
1873 struct ieee80211_fragment_entry *entry;
1874 int i, idx;
1875
1876 idx = sdata->fragment_next;
1877 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1878 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +02001879
1880 idx--;
1881 if (idx < 0)
1882 idx = IEEE80211_FRAGMENT_MAX - 1;
1883
1884 entry = &sdata->fragments[idx];
1885 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1886 entry->rx_queue != rx_queue ||
1887 entry->last_frag + 1 != frag)
1888 continue;
1889
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001890 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001891
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001892 /*
1893 * Check ftype and addresses are equal, else check next fragment
1894 */
1895 if (((hdr->frame_control ^ f_hdr->frame_control) &
1896 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001897 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1898 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02001899 continue;
1900
S.Çağlar Onurab466232008-02-14 17:36:47 +02001901 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001902 __skb_queue_purge(&entry->skb_list);
1903 continue;
1904 }
1905 return entry;
1906 }
1907
1908 return NULL;
1909}
1910
Johannes Berg49461622008-06-30 15:10:45 +02001911static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001912ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001913{
1914 struct ieee80211_hdr *hdr;
1915 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001916 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001917 unsigned int frag, seq;
1918 struct ieee80211_fragment_entry *entry;
1919 struct sk_buff *skb;
1920
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001921 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001922 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07001923
1924 if (ieee80211_is_ctl(fc))
1925 return RX_CONTINUE;
1926
Johannes Berg571ecf62007-07-27 15:43:22 +02001927 sc = le16_to_cpu(hdr->seq_ctrl);
1928 frag = sc & IEEE80211_SCTL_FRAG;
1929
Johannes Bergb8fff402014-11-03 13:57:46 +01001930 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02001931 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01001932 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02001933 }
Johannes Bergb8fff402014-11-03 13:57:46 +01001934
Andreas Müllerd0259332014-12-12 12:11:11 +01001935 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1936 goto out;
1937
Johannes Berg571ecf62007-07-27 15:43:22 +02001938 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1939
Zhu Yie3cf8b32010-03-29 17:35:07 +08001940 if (skb_linearize(rx->skb))
1941 return RX_DROP_UNUSABLE;
1942
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07001943 /*
1944 * skb_linearize() might change the skb->data and
1945 * previously cached variables (in this case, hdr) need to
1946 * be refreshed with the new data.
1947 */
1948 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001949 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1950
1951 if (frag == 0) {
1952 /* This is the first fragment of a new frame. */
1953 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02001954 rx->seqno_idx, &(rx->skb));
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001955 if (rx->key &&
1956 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
Johannes Berg9acc54b2016-02-26 22:13:40 +01001957 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
1958 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
1959 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001960 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02001961 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01001962
1963 /* Store CCMP/GCMP PN so that we can verify that the
1964 * next fragment has a sequential PN value.
1965 */
1966 entry->check_sequential_pn = true;
Johannes Berg571ecf62007-07-27 15:43:22 +02001967 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07001968 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02001969 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01001970 BUILD_BUG_ON(offsetof(struct ieee80211_key,
1971 u.ccmp.rx_pn) !=
1972 offsetof(struct ieee80211_key,
1973 u.gcmp.rx_pn));
1974 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
1975 sizeof(rx->key->u.gcmp.rx_pn[queue]));
1976 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
1977 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02001978 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001979 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001980 }
1981
1982 /* This is a fragment for a frame that should already be pending in
1983 * fragment cache. Add this fragment to the end of the pending entry.
1984 */
Johannes Berg9e262972011-07-07 18:45:03 +02001985 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1986 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001987 if (!entry) {
1988 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001989 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001990 }
1991
Johannes Berg9acc54b2016-02-26 22:13:40 +01001992 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
1993 * MPDU PN values are not incrementing in steps of 1."
1994 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
1995 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
1996 */
1997 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001998 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001999 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07002000 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01002001
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02002002 if (!rx->key ||
2003 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
Johannes Berg9acc54b2016-02-26 22:13:40 +01002004 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2005 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2006 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002007 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002008 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2009 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002010 pn[i]++;
2011 if (pn[i])
2012 break;
2013 }
Johannes Berg9e262972011-07-07 18:45:03 +02002014 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07002015 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02002016 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002017 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02002018 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02002019 }
2020
Harvey Harrison358c8d92008-07-15 18:44:13 -07002021 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02002022 __skb_queue_tail(&entry->skb_list, rx->skb);
2023 entry->last_frag = frag;
2024 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002025 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02002026 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002027 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002028 }
2029
2030 rx->skb = __skb_dequeue(&entry->skb_list);
2031 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02002032 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02002033 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2034 GFP_ATOMIC))) {
2035 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2036 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01002037 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002038 }
2039 }
2040 while ((skb = __skb_dequeue(&entry->skb_list))) {
2041 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
2042 dev_kfree_skb(skb);
2043 }
2044
Johannes Berg571ecf62007-07-27 15:43:22 +02002045 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002046 ieee80211_led_rx(rx->local);
2047 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002048 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002049 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002050 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002051}
2052
Johannes Berg1df332e2012-10-26 00:09:11 +02002053static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002054{
Johannes Berg1df332e2012-10-26 00:09:11 +02002055 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002056 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002057
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002058 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002059}
2060
Johannes Berg1df332e2012-10-26 00:09:11 +02002061static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002062{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002063 struct sk_buff *skb = rx->skb;
2064 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2065
Johannes Berg3017b802007-08-28 17:01:53 -04002066 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002067 * Pass through unencrypted frames if the hardware has
2068 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002069 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002070 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002071 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002072
2073 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002074 if (unlikely(!ieee80211_has_protected(fc) &&
2075 !ieee80211_is_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002076 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002077 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002078
2079 return 0;
2080}
2081
Johannes Berg1df332e2012-10-26 00:09:11 +02002082static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002083{
2084 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002085 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002086 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002087
Jouni Malinene3efca02010-03-28 22:31:15 -07002088 /*
2089 * Pass through unencrypted frames if the hardware has
2090 * decrypted them already.
2091 */
2092 if (status->flag & RX_FLAG_DECRYPTED)
2093 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002094
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002095 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002096 if (unlikely(!ieee80211_has_protected(fc) &&
2097 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002098 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002099 if (ieee80211_is_deauth(fc) ||
2100 ieee80211_is_disassoc(fc))
2101 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2102 rx->skb->data,
2103 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002104 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002105 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002106 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002107 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002108 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002109 if (ieee80211_is_deauth(fc) ||
2110 ieee80211_is_disassoc(fc))
2111 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2112 rx->skb->data,
2113 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002114 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002115 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002116 /*
2117 * When using MFP, Action frames are not allowed prior to
2118 * having configured keys.
2119 */
2120 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002121 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002122 return -EACCES;
2123 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002124
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002125 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002126}
2127
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002128static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002129__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002130{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002131 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002132 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002133 bool check_port_control = false;
2134 struct ethhdr *ehdr;
2135 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002136
Felix Fietkau4114fa22011-04-12 19:15:22 +02002137 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002138 if (ieee80211_has_a4(hdr->frame_control) &&
2139 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002140 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002141
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002142 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2143 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2144
2145 if (!sdata->u.mgd.use_4addr)
2146 return -1;
2147 else
2148 check_port_control = true;
2149 }
2150
Johannes Berg9bc383d2009-11-19 11:55:19 +01002151 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002152 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002153 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002154
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002155 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002156 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002157 return ret;
2158
2159 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002160 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2161 *port_control = true;
2162 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002163 return -1;
2164
2165 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002166}
Johannes Berg571ecf62007-07-27 15:43:22 +02002167
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002168/*
2169 * requires that rx->skb is a frame with ethernet header
2170 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002171static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002172{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302173 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002174 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2175 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2176
2177 /*
2178 * Allow EAPOL frames to us/the PAE group address regardless
2179 * of whether the frame was encrypted or not.
2180 */
Johannes Berga621fa42010-08-27 14:26:54 +03002181 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002182 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2183 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002184 return true;
2185
2186 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002187 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002188 return false;
2189
2190 return true;
2191}
2192
2193/*
2194 * requires that rx->skb is a frame with ethernet header
2195 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002196static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002197ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002198{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002199 struct ieee80211_sub_if_data *sdata = rx->sdata;
2200 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002201 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002202 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2203 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002204
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002205 skb = rx->skb;
2206 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002207
Johannes Berg5a490512015-04-22 17:10:38 +02002208 ieee80211_rx_stats(dev, skb->len);
2209
Johannes Bergde8f18d2016-03-31 20:02:03 +03002210 if (rx->sta) {
2211 /* The seqno index has the same property as needed
2212 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2213 * for non-QoS-data frames. Here we know it's a data
2214 * frame, so count MSDUs.
2215 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002216 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002217 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002218 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002219 }
2220
Johannes Berg05c914f2008-09-11 00:01:58 +02002221 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2222 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002223 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002224 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Michael Braun72f15d52016-10-10 19:12:21 +02002225 if (is_multicast_ether_addr(ehdr->h_dest) &&
2226 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002227 /*
2228 * send multicast frames both to higher layers in
2229 * local net stack and back to the wireless medium
2230 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002231 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002232 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002233 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002234 dev->name);
Michael Braun72f15d52016-10-10 19:12:21 +02002235 } else if (!is_multicast_ether_addr(ehdr->h_dest)) {
Johannes Bergabe60632009-11-25 17:46:18 +01002236 dsta = sta_info_get(sdata, skb->data);
2237 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002238 /*
2239 * The destination station is associated to
2240 * this AP (in this VLAN), so send the frame
2241 * directly to it and do not pass it to local
2242 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002243 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002244 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002245 skb = NULL;
2246 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002247 }
2248 }
2249
Kalle Valo59d9cb02009-12-17 13:54:57 +01002250#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002251 if (skb) {
2252 /* 'align' will only take the values 0 or 2 here since all
2253 * frames are required to be aligned to 2-byte boundaries
2254 * when being passed to mac80211; the code here works just
2255 * as well if that isn't true, but mac80211 assumes it can
2256 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002257 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002258 int align;
2259
2260 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002261 if (align) {
2262 if (WARN_ON(skb_headroom(skb) < 3)) {
2263 dev_kfree_skb(skb);
2264 skb = NULL;
2265 } else {
2266 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002267 size_t len = skb_headlen(skb);
2268 skb->data -= align;
2269 memmove(skb->data, data, len);
2270 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002271 }
2272 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002273 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002274#endif
2275
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002276 if (skb) {
2277 /* deliver to local stack */
2278 skb->protocol = eth_type_trans(skb, dev);
2279 memset(skb->cb, 0, sizeof(skb->cb));
Johannes Bergaf9f9b22015-06-11 16:02:32 +02002280 if (rx->napi)
2281 napi_gro_receive(rx->napi, skb);
Johannes Berg06d181a2014-02-04 20:51:09 +01002282 else
2283 netif_receive_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002284 }
2285
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002286 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002287 /*
2288 * Send to wireless media and increase priority by 256 to
2289 * keep the received priority instead of reclassifying
2290 * the frame (see cfg80211_classify8021d).
2291 */
2292 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002293 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002294 skb_reset_network_header(xmit_skb);
2295 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002296 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002297 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002298}
2299
Johannes Berg49461622008-06-30 15:10:45 +02002300static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002301ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002302{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002303 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002304 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002305 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2306 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002307 struct sk_buff_head frame_list;
Johannes Berg554891e2010-09-24 12:38:25 +02002308 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg7f6990c2016-10-05 15:29:49 +02002309 struct ethhdr ethhdr;
Johannes Berge2b52272016-10-05 16:42:06 +02002310 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002311
Harvey Harrison358c8d92008-07-15 18:44:13 -07002312 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002313 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002314
Harvey Harrison358c8d92008-07-15 18:44:13 -07002315 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002316 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002317
Johannes Berg554891e2010-09-24 12:38:25 +02002318 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002319 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002320
Johannes Bergea720932016-10-05 10:14:42 +02002321 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2322 switch (rx->sdata->vif.type) {
2323 case NL80211_IFTYPE_AP_VLAN:
2324 if (!rx->sdata->u.vlan.sta)
2325 return RX_DROP_UNUSABLE;
2326 break;
2327 case NL80211_IFTYPE_STATION:
2328 if (!rx->sdata->u.mgd.use_4addr)
2329 return RX_DROP_UNUSABLE;
2330 break;
2331 default:
2332 return RX_DROP_UNUSABLE;
2333 }
Johannes Berge2b52272016-10-05 16:42:06 +02002334 check_da = NULL;
2335 check_sa = NULL;
2336 } else switch (rx->sdata->vif.type) {
2337 case NL80211_IFTYPE_AP:
2338 case NL80211_IFTYPE_AP_VLAN:
2339 check_da = NULL;
2340 break;
2341 case NL80211_IFTYPE_STATION:
2342 if (!rx->sta ||
2343 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2344 check_sa = NULL;
2345 break;
2346 case NL80211_IFTYPE_MESH_POINT:
2347 check_sa = NULL;
2348 break;
2349 default:
2350 break;
Johannes Bergea720932016-10-05 10:14:42 +02002351 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08002352
Johannes Bergea720932016-10-05 10:14:42 +02002353 if (is_multicast_ether_addr(hdr->addr1))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002354 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002355
2356 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002357 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002358
Johannes Berg7f6990c2016-10-05 15:29:49 +02002359 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2360 rx->sdata->vif.addr,
2361 rx->sdata->vif.type))
2362 return RX_DROP_UNUSABLE;
2363
Zhu Yieaf85ca2009-12-01 10:18:37 +08002364 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2365 rx->sdata->vif.type,
Johannes Berg8b935ee2016-10-05 16:17:01 +02002366 rx->local->hw.extra_tx_headroom,
Johannes Berge2b52272016-10-05 16:42:06 +02002367 check_da, check_sa);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002368
Zhu Yieaf85ca2009-12-01 10:18:37 +08002369 while (!skb_queue_empty(&frame_list)) {
2370 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002371
Harvey Harrison358c8d92008-07-15 18:44:13 -07002372 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002373 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002374 continue;
2375 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002376
2377 ieee80211_deliver_skb(rx);
2378 }
2379
Johannes Berg9ae54c82008-01-31 19:48:20 +01002380 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002381}
2382
Ingo Molnarbf94e172008-10-12 23:51:38 -07002383#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002384static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002385ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2386{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002387 struct ieee80211_hdr *fwd_hdr, *hdr;
2388 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002389 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002390 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002391 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002392 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002393 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002394 u16 ac, q, hdrlen;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002395
2396 hdr = (struct ieee80211_hdr *) skb->data;
2397 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002398
2399 /* make sure fixed part of mesh header is there, also checks skb len */
2400 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2401 return RX_DROP_MONITOR;
2402
2403 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2404
2405 /* make sure full mesh header is there, also checks skb len */
2406 if (!pskb_may_pull(rx->skb,
2407 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2408 return RX_DROP_MONITOR;
2409
2410 /* reload pointers */
2411 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002412 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2413
Bob Copelandd0c22112015-03-02 14:28:52 -05002414 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2415 return RX_DROP_MONITOR;
2416
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002417 /* frame is in RMC, don't forward */
2418 if (ieee80211_is_data(hdr->frame_control) &&
2419 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002420 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002421 return RX_DROP_MONITOR;
2422
Johannes Berg5c900672015-04-22 14:48:34 +02002423 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002424 return RX_CONTINUE;
2425
2426 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002427 return RX_DROP_MONITOR;
2428
Javier Cardona43b7b312009-10-15 18:10:51 -07002429 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002430 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002431 char *proxied_addr;
2432 char *mpp_addr;
2433
2434 if (is_multicast_ether_addr(hdr->addr1)) {
2435 mpp_addr = hdr->addr3;
2436 proxied_addr = mesh_hdr->eaddr1;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002437 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2438 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002439 mpp_addr = hdr->addr4;
2440 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002441 } else {
2442 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002443 }
YanBo79617de2008-09-22 13:30:32 +08002444
YanBo79617de2008-09-22 13:30:32 +08002445 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002446 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002447 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002448 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002449 } else {
2450 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002451 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002452 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002453 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002454 spin_unlock_bh(&mppath->state_lock);
2455 }
2456 rcu_read_unlock();
2457 }
2458
Javier Cardona3c5772a2009-08-10 12:15:48 -07002459 /* Frame has reached destination. Don't forward */
2460 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002461 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002462 return RX_CONTINUE;
2463
Michal Kaziorcf440122016-01-25 14:43:24 +01002464 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2465 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002466 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002467 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002468 return RX_DROP_MONITOR;
2469 }
2470 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002471
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002472 if (!--mesh_hdr->ttl) {
2473 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002474 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002475 }
2476
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002477 if (!ifmsh->mshcfg.dot11MeshForwarding)
2478 goto out;
2479
Cedric Izoardc38c39b2017-01-11 14:39:07 +00002480 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2481 sdata->encrypt_headroom, 0, GFP_ATOMIC);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002482 if (!fwd_skb) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002483 net_info_ratelimited("%s: failed to clone mesh frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002484 sdata->name);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002485 goto out;
2486 }
2487
2488 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002489 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002490 info = IEEE80211_SKB_CB(fwd_skb);
2491 memset(info, 0, sizeof(*info));
2492 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2493 info->control.vif = &rx->sdata->vif;
2494 info->control.jiffies = jiffies;
2495 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2496 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2497 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002498 /* update power mode indication when forwarding */
2499 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002500 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002501 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002502 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2503 } else {
2504 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002505 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002506 fwd_hdr->addr3, 0,
2507 WLAN_REASON_MESH_PATH_NOFORWARD,
2508 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002509 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002510 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002511 return RX_DROP_MONITOR;
2512 }
2513
2514 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2515 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002516 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002517 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002518 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002519 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002520}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002521#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002522
2523static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002524ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002525{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002526 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302527 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002528 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002529 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2530 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002531 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002532 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002533
Harvey Harrison358c8d92008-07-15 18:44:13 -07002534 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002535 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002536
Harvey Harrison358c8d92008-07-15 18:44:13 -07002537 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002538 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002539
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002540 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002541 * Send unexpected-4addr-frame event to hostapd. For older versions,
2542 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002543 */
2544 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002545 sdata->vif.type == NL80211_IFTYPE_AP) {
2546 if (rx->sta &&
2547 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2548 cfg80211_rx_unexpected_4addr_frame(
2549 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002550 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002551 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002552
Felix Fietkau4114fa22011-04-12 19:15:22 +02002553 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002554 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002555 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002556
Harvey Harrison358c8d92008-07-15 18:44:13 -07002557 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002558 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002559
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002560 /* directly handle TDLS channel switch requests/responses */
2561 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2562 cpu_to_be16(ETH_P_TDLS))) {
2563 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2564
2565 if (pskb_may_pull(rx->skb,
2566 offsetof(struct ieee80211_tdls_data, u)) &&
2567 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2568 tf->category == WLAN_CATEGORY_TDLS &&
2569 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2570 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002571 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2572 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002573 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002574 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002575
2576 return RX_QUEUED;
2577 }
2578 }
2579
Felix Fietkau4114fa22011-04-12 19:15:22 +02002580 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2581 unlikely(port_control) && sdata->bss) {
2582 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2583 u.ap);
2584 dev = sdata->dev;
2585 rx->sdata = sdata;
2586 }
2587
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002588 rx->skb->dev = dev;
2589
Johannes Berg602fae42016-03-17 15:02:52 +02002590 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2591 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302592 !is_multicast_ether_addr(
2593 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2594 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002595 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2596 mod_timer(&local->dynamic_ps_timer, jiffies +
2597 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05302598
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002599 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002600
Johannes Berg9ae54c82008-01-31 19:48:20 +01002601 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002602}
2603
Johannes Berg49461622008-06-30 15:10:45 +02002604static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002605ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002606{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002607 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002608 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002609 struct tid_ampdu_rx *tid_agg_rx;
2610 u16 start_seq_num;
2611 u16 tid;
2612
Harvey Harrisona7767f92008-07-02 16:30:51 -07002613 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002614 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002615
Harvey Harrisona7767f92008-07-02 16:30:51 -07002616 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002617 struct {
2618 __le16 control, start_seq_num;
2619 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002620 struct ieee80211_event event = {
2621 .type = BAR_RX_EVENT,
2622 };
Johannes Berg8ae59772010-05-30 14:52:58 +02002623
Ron Rindjunsky71364712007-12-25 17:00:36 +02002624 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002625 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002626
2627 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2628 &bar_data, sizeof(bar_data)))
2629 return RX_DROP_MONITOR;
2630
Johannes Berg8ae59772010-05-30 14:52:58 +02002631 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02002632
Johannes Berg53f24972016-08-29 23:25:19 +03002633 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2634 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2635 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2636 WLAN_BACK_RECIPIENT,
2637 WLAN_REASON_QSTA_REQUIRE_SETUP);
2638
Johannes Berga87f7362010-06-10 10:21:38 +02002639 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2640 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01002641 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002642
Johannes Berg8ae59772010-05-30 14:52:58 +02002643 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002644 event.u.ba.tid = tid;
2645 event.u.ba.ssn = start_seq_num;
2646 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002647
2648 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002649 if (tid_agg_rx->timeout)
2650 mod_timer(&tid_agg_rx->session_timer,
2651 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02002652
Johannes Bergdd318572010-11-29 11:09:16 +01002653 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01002654 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02002655 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002656 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01002657 spin_unlock(&tid_agg_rx->reorder_lock);
2658
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002659 drv_event_callback(rx->local, rx->sdata, &event);
2660
Johannes Berga02ae752009-11-16 12:00:40 +01002661 kfree_skb(skb);
2662 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002663 }
2664
Johannes Berg08daeca2010-05-30 14:53:43 +02002665 /*
2666 * After this point, we only want management frames,
2667 * so we can drop all remaining control frames to
2668 * cooked monitor interfaces.
2669 */
2670 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002671}
2672
Jouni Malinenf4f727a2009-01-10 11:46:53 +02002673static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2674 struct ieee80211_mgmt *mgmt,
2675 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02002676{
2677 struct ieee80211_local *local = sdata->local;
2678 struct sk_buff *skb;
2679 struct ieee80211_mgmt *resp;
2680
Joe Perchesb203ca32012-05-08 18:56:52 +00002681 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02002682 /* Not to own unicast address */
2683 return;
2684 }
2685
Joe Perchesb203ca32012-05-08 18:56:52 +00002686 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2687 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002688 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02002689 return;
2690 }
2691
2692 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2693 /* Too short SA Query request frame */
2694 return;
2695 }
2696
2697 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2698 if (skb == NULL)
2699 return;
2700
2701 skb_reserve(skb, local->hw.extra_tx_headroom);
2702 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2703 memset(resp, 0, 24);
2704 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002705 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002706 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02002707 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2708 IEEE80211_STYPE_ACTION);
2709 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2710 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2711 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2712 memcpy(resp->u.action.u.sa_query.trans_id,
2713 mgmt->u.action.u.sa_query.trans_id,
2714 WLAN_SA_QUERY_TR_ID_LEN);
2715
Johannes Berg62ae67b2009-11-18 18:42:05 +01002716 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02002717}
2718
Johannes Berg49461622008-06-30 15:10:45 +02002719static ieee80211_rx_result debug_noinline
Johannes Berg2e161f72010-08-12 15:38:38 +02002720ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2721{
2722 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002723 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02002724
2725 /*
2726 * From here on, look only at management frames.
2727 * Data and control frames are already handled,
2728 * and unknown (reserved) frames are useless.
2729 */
2730 if (rx->skb->len < 24)
2731 return RX_DROP_MONITOR;
2732
2733 if (!ieee80211_is_mgmt(mgmt->frame_control))
2734 return RX_DROP_MONITOR;
2735
Johannes Bergee971922011-11-04 11:18:18 +01002736 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2737 ieee80211_is_beacon(mgmt->frame_control) &&
2738 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01002739 int sig = 0;
2740
Johannes Berg30686bf2015-06-02 21:39:54 +02002741 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
Johannes Berg804483e2012-03-05 22:18:41 +01002742 sig = status->signal;
2743
Johannes Bergee971922011-11-04 11:18:18 +01002744 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2745 rx->skb->data, rx->skb->len,
Ben Greear37c73b52012-10-26 14:49:25 -07002746 status->freq, sig);
Johannes Bergee971922011-11-04 11:18:18 +01002747 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2748 }
2749
Johannes Berg2e161f72010-08-12 15:38:38 +02002750 if (ieee80211_drop_unencrypted_mgmt(rx))
2751 return RX_DROP_UNUSABLE;
2752
2753 return RX_CONTINUE;
2754}
2755
2756static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02002757ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2758{
2759 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002760 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02002761 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002762 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02002763 int len = rx->skb->len;
2764
2765 if (!ieee80211_is_action(mgmt->frame_control))
2766 return RX_CONTINUE;
2767
Jouni Malinen026331c2010-02-15 12:53:10 +02002768 /* drop too small frames */
2769 if (len < IEEE80211_MIN_ACTION_SIZE)
2770 return RX_DROP_UNUSABLE;
2771
Marco Porsch815b8092012-12-05 15:04:26 -08002772 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002773 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2774 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02002775 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02002776
Johannes Bergde1ede72008-09-09 14:42:50 +02002777 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002778 case WLAN_CATEGORY_HT:
2779 /* reject HT action frames from stations not supporting HT */
2780 if (!rx->sta->sta.ht_cap.ht_supported)
2781 goto invalid;
2782
2783 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2784 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2785 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2786 sdata->vif.type != NL80211_IFTYPE_AP &&
2787 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2788 break;
2789
Johannes Bergec61cd62012-12-28 12:12:10 +01002790 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002791 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2792 goto invalid;
2793
2794 switch (mgmt->u.action.u.ht_smps.action) {
2795 case WLAN_HT_ACTION_SMPS: {
2796 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01002797 enum ieee80211_smps_mode smps_mode;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002798
2799 /* convert to HT capability */
2800 switch (mgmt->u.action.u.ht_smps.smps_control) {
2801 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002802 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002803 break;
2804 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002805 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002806 break;
2807 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002808 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002809 break;
2810 default:
2811 goto invalid;
2812 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002813
2814 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01002815 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002816 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01002817 rx->sta->sta.smps_mode = smps_mode;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002818
2819 sband = rx->local->hw.wiphy->bands[status->band];
2820
Johannes Berg64f68e52012-03-28 10:58:37 +02002821 rate_control_rate_update(local, sband, rx->sta,
2822 IEEE80211_RC_SMPS_CHANGED);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002823 goto handled;
2824 }
Johannes Bergec61cd62012-12-28 12:12:10 +01002825 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2826 struct ieee80211_supported_band *sband;
2827 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002828 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01002829
2830 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002831 if (!(rx->sta->sta.ht_cap.cap &
2832 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01002833 goto handled;
2834
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002835 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002836 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01002837 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002838 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2839
2840 /* set cur_max_bandwidth and recalc sta bw */
2841 rx->sta->cur_max_bandwidth = max_bw;
2842 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002843
2844 if (rx->sta->sta.bandwidth == new_bw)
2845 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01002846
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002847 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01002848 sband = rx->local->hw.wiphy->bands[status->band];
2849
2850 rate_control_rate_update(local, sband, rx->sta,
2851 IEEE80211_RC_BW_CHANGED);
2852 goto handled;
2853 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002854 default:
2855 goto invalid;
2856 }
2857
2858 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01002859 case WLAN_CATEGORY_PUBLIC:
2860 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2861 goto invalid;
2862 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2863 break;
2864 if (!rx->sta)
2865 break;
2866 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2867 break;
2868 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2869 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2870 break;
2871 if (len < offsetof(struct ieee80211_mgmt,
2872 u.action.u.ext_chan_switch.variable))
2873 goto invalid;
2874 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01002875 case WLAN_CATEGORY_VHT:
2876 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2877 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2878 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2879 sdata->vif.type != NL80211_IFTYPE_AP &&
2880 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2881 break;
2882
2883 /* verify action code is present */
2884 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2885 goto invalid;
2886
2887 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2888 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
Johannes Berg0af83d32012-12-27 18:55:36 +01002889 /* verify opmode is present */
2890 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2891 goto invalid;
Johannes Bergd2941df2016-10-20 08:52:50 +02002892 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01002893 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002894 case WLAN_VHT_ACTION_GROUPID_MGMT: {
2895 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
2896 goto invalid;
2897 goto queue;
2898 }
Johannes Berg0af83d32012-12-27 18:55:36 +01002899 default:
2900 break;
2901 }
2902 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002903 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01002904 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07002905 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01002906 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01002907 sdata->vif.type != NL80211_IFTYPE_AP &&
2908 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02002909 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01002910
Jouni Malinen026331c2010-02-15 12:53:10 +02002911 /* verify action_code is present */
2912 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2913 break;
2914
Johannes Bergde1ede72008-09-09 14:42:50 +02002915 switch (mgmt->u.action.u.addba_req.action_code) {
2916 case WLAN_ACTION_ADDBA_REQ:
2917 if (len < (IEEE80211_MIN_ACTION_SIZE +
2918 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002919 goto invalid;
2920 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002921 case WLAN_ACTION_ADDBA_RESP:
2922 if (len < (IEEE80211_MIN_ACTION_SIZE +
2923 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002924 goto invalid;
2925 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002926 case WLAN_ACTION_DELBA:
2927 if (len < (IEEE80211_MIN_ACTION_SIZE +
2928 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002929 goto invalid;
2930 break;
2931 default:
2932 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02002933 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002934
Johannes Berg8b58ff82010-06-10 10:21:51 +02002935 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02002936 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02002937 /* verify action_code is present */
2938 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2939 break;
2940
Johannes Berg39192c02008-09-09 14:49:03 +02002941 switch (mgmt->u.action.u.measurement.action_code) {
2942 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02002943 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002944 break;
2945
Johannes Berg39192c02008-09-09 14:49:03 +02002946 if (len < (IEEE80211_MIN_ACTION_SIZE +
2947 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02002948 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002949
Johannes Bergcc32abd2009-05-15 11:52:31 +02002950 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002951 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02002952
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002953 ieee80211_process_measurement_req(sdata, mgmt, len);
2954 goto handled;
2955 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2956 u8 *bssid;
2957 if (len < (IEEE80211_MIN_ACTION_SIZE +
2958 sizeof(mgmt->u.action.u.chan_switch)))
2959 break;
2960
2961 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002962 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2963 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002964 break;
2965
2966 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2967 bssid = sdata->u.mgd.bssid;
2968 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2969 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002970 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2971 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002972 else
2973 break;
2974
2975 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02002976 break;
Sujithc481ec92009-01-06 09:28:37 +05302977
Johannes Berg8b58ff82010-06-10 10:21:51 +02002978 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002979 }
Johannes Berg39192c02008-09-09 14:49:03 +02002980 }
2981 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002982 case WLAN_CATEGORY_SA_QUERY:
2983 if (len < (IEEE80211_MIN_ACTION_SIZE +
2984 sizeof(mgmt->u.action.u.sa_query)))
Johannes Berg84040802010-02-15 12:46:39 +02002985 break;
2986
Jouni Malinenfea14732009-01-08 13:32:06 +02002987 switch (mgmt->u.action.u.sa_query.action) {
2988 case WLAN_ACTION_SA_QUERY_REQUEST:
2989 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002990 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002991 ieee80211_process_sa_query_req(sdata, mgmt, len);
Johannes Berg84040802010-02-15 12:46:39 +02002992 goto handled;
Jouni Malinenfea14732009-01-08 13:32:06 +02002993 }
2994 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07002995 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02002996 if (len < (IEEE80211_MIN_ACTION_SIZE +
2997 sizeof(mgmt->u.action.u.self_prot.action_code)))
2998 break;
2999
Thomas Pedersen8db09852011-08-12 20:01:00 -07003000 switch (mgmt->u.action.u.self_prot.action_code) {
3001 case WLAN_SP_MESH_PEERING_OPEN:
3002 case WLAN_SP_MESH_PEERING_CLOSE:
3003 case WLAN_SP_MESH_PEERING_CONFIRM:
3004 if (!ieee80211_vif_is_mesh(&sdata->vif))
3005 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08003006 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07003007 /* userspace handles this frame */
3008 break;
3009 goto queue;
3010 case WLAN_SP_MGK_INFORM:
3011 case WLAN_SP_MGK_ACK:
3012 if (!ieee80211_vif_is_mesh(&sdata->vif))
3013 goto invalid;
3014 break;
3015 }
3016 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07003017 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02003018 if (len < (IEEE80211_MIN_ACTION_SIZE +
3019 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3020 break;
3021
Johannes Berg77a121c2010-06-10 10:21:34 +02003022 if (!ieee80211_vif_is_mesh(&sdata->vif))
3023 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07003024 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02003025 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08003026 break;
3027 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02003028 }
Jouni Malinen026331c2010-02-15 12:53:10 +02003029
Johannes Berg2e161f72010-08-12 15:38:38 +02003030 return RX_CONTINUE;
3031
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02003032 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02003033 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f72010-08-12 15:38:38 +02003034 /* will return in the next handlers */
3035 return RX_CONTINUE;
3036
3037 handled:
3038 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003039 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003040 dev_kfree_skb(rx->skb);
3041 return RX_QUEUED;
3042
3043 queue:
3044 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
3045 skb_queue_tail(&sdata->skb_queue, rx->skb);
3046 ieee80211_queue_work(&local->hw, &sdata->work);
3047 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003048 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003049 return RX_QUEUED;
3050}
3051
3052static ieee80211_rx_result debug_noinline
3053ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3054{
Johannes Berg554891e2010-09-24 12:38:25 +02003055 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01003056 int sig = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +02003057
3058 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02003059 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f72010-08-12 15:38:38 +02003060 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01003061
Jouni Malinen026331c2010-02-15 12:53:10 +02003062 /*
3063 * Getting here means the kernel doesn't know how to handle
3064 * it, but maybe userspace does ... include returned frames
3065 * so userspace can register for those to know whether ones
3066 * it transmitted were processed or returned.
3067 */
Jouni Malinen026331c2010-02-15 12:53:10 +02003068
Johannes Berg30686bf2015-06-02 21:39:54 +02003069 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
Johannes Berg804483e2012-03-05 22:18:41 +01003070 sig = status->signal;
3071
Johannes Berg71bbc992012-06-15 15:30:18 +02003072 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07003073 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f72010-08-12 15:38:38 +02003074 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003075 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003076 dev_kfree_skb(rx->skb);
3077 return RX_QUEUED;
3078 }
3079
Johannes Berg2e161f72010-08-12 15:38:38 +02003080 return RX_CONTINUE;
3081}
3082
3083static ieee80211_rx_result debug_noinline
3084ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3085{
3086 struct ieee80211_local *local = rx->local;
3087 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3088 struct sk_buff *nskb;
3089 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003090 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02003091
3092 if (!ieee80211_is_action(mgmt->frame_control))
3093 return RX_CONTINUE;
3094
3095 /*
3096 * For AP mode, hostapd is responsible for handling any action
3097 * frames that we didn't handle, including returning unknown
3098 * ones. For all other modes we will return them to the sender,
3099 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003100 * 802.11-2012 9.24.4.
Johannes Berg2e161f72010-08-12 15:38:38 +02003101 * Newer versions of hostapd shall also use the management frame
3102 * registration mechanisms, but older ones still use cooked
3103 * monitor interfaces so push all frames there.
3104 */
Johannes Berg554891e2010-09-24 12:38:25 +02003105 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f72010-08-12 15:38:38 +02003106 (sdata->vif.type == NL80211_IFTYPE_AP ||
3107 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3108 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003109
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003110 if (is_multicast_ether_addr(mgmt->da))
3111 return RX_DROP_MONITOR;
3112
Johannes Berg84040802010-02-15 12:46:39 +02003113 /* do not return rejected action frames */
3114 if (mgmt->u.action.category & 0x80)
3115 return RX_DROP_UNUSABLE;
3116
3117 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3118 GFP_ATOMIC);
3119 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003120 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003121
John W. Linville292b4df2010-06-24 11:13:56 -04003122 nmgmt->u.action.category |= 0x80;
3123 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3124 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003125
3126 memset(nskb->cb, 0, sizeof(nskb->cb));
3127
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003128 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3129 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3130
3131 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3132 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3133 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003134 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003135 info->hw_queue =
3136 local->hw.offchannel_tx_hw_queue;
3137 }
3138
3139 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3140 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003141 }
Johannes Berg39192c02008-09-09 14:49:03 +02003142 dev_kfree_skb(rx->skb);
3143 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003144}
3145
3146static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003147ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003148{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003149 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003150 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3151 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003152
Johannes Berg77a121c2010-06-10 10:21:34 +02003153 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003154
Johannes Berg77a121c2010-06-10 10:21:34 +02003155 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3156 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003157 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003158 sdata->vif.type != NL80211_IFTYPE_STATION)
3159 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003160
Johannes Berg77a121c2010-06-10 10:21:34 +02003161 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003162 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003163 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3164 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3165 /* process for all: mesh, mlme, ibss */
3166 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003167 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3168 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003169 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3170 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c313332010-11-29 20:53:23 +01003171 if (is_multicast_ether_addr(mgmt->da) &&
3172 !is_broadcast_ether_addr(mgmt->da))
3173 return RX_DROP_MONITOR;
3174
Johannes Berg77a121c2010-06-10 10:21:34 +02003175 /* process only for station */
3176 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3177 return RX_DROP_MONITOR;
3178 break;
3179 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003180 /* process only for ibss and mesh */
3181 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3182 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003183 return RX_DROP_MONITOR;
3184 break;
3185 default:
3186 return RX_DROP_MONITOR;
3187 }
Johannes Berg46900292009-02-15 12:44:28 +01003188
Johannes Berg77a121c2010-06-10 10:21:34 +02003189 /* queue up frame and kick off work to process it */
Johannes Bergc1475ca2010-06-10 10:21:37 +02003190 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg77a121c2010-06-10 10:21:34 +02003191 skb_queue_tail(&sdata->skb_queue, rx->skb);
3192 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003193 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003194 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003195
3196 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003197}
3198
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003199static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3200 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003201{
3202 struct ieee80211_sub_if_data *sdata;
3203 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003204 struct sk_buff *skb = rx->skb, *skb2;
3205 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003206 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003207 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003208
Johannes Berg554891e2010-09-24 12:38:25 +02003209 /*
3210 * If cooked monitor has been processed already, then
3211 * don't do it again. If not, set the flag.
3212 */
3213 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003214 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003215 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003216
Johannes Berg152c4772011-10-21 10:22:22 +02003217 /* If there are no cooked monitor interfaces, just free the SKB */
3218 if (!local->cooked_mntrs)
3219 goto out_free_skb;
3220
Johannes Berg1f7bba72014-11-06 22:56:36 +01003221 /* vendor data is long removed here */
3222 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003223 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003224 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003225
3226 if (skb_headroom(skb) < needed_headroom &&
3227 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003228 goto out_free_skb;
3229
Johannes Berg293702a2012-03-02 13:18:19 +01003230 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003231 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3232 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003233
Zhang Shengjud57a5442016-03-03 01:16:56 +00003234 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003235 skb->ip_summed = CHECKSUM_UNNECESSARY;
3236 skb->pkt_type = PACKET_OTHERHOST;
3237 skb->protocol = htons(ETH_P_802_2);
3238
3239 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01003240 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003241 continue;
3242
Johannes Berg05c914f2008-09-11 00:01:58 +02003243 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003244 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003245 continue;
3246
3247 if (prev_dev) {
3248 skb2 = skb_clone(skb, GFP_ATOMIC);
3249 if (skb2) {
3250 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003251 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003252 }
3253 }
3254
3255 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +02003256 ieee80211_rx_stats(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003257 }
3258
3259 if (prev_dev) {
3260 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003261 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003262 return;
3263 }
Michael Wu3d30d942008-01-31 19:48:27 +01003264
3265 out_free_skb:
3266 dev_kfree_skb(skb);
3267}
3268
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003269static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3270 ieee80211_rx_result res)
3271{
3272 switch (res) {
3273 case RX_DROP_MONITOR:
3274 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3275 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003276 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003277 /* fall through */
3278 case RX_CONTINUE: {
3279 struct ieee80211_rate *rate = NULL;
3280 struct ieee80211_supported_band *sband;
3281 struct ieee80211_rx_status *status;
3282
3283 status = IEEE80211_SKB_RXCB((rx->skb));
3284
3285 sband = rx->local->hw.wiphy->bands[status->band];
Johannes Berg56146182012-11-09 15:07:02 +01003286 if (!(status->flag & RX_FLAG_HT) &&
3287 !(status->flag & RX_FLAG_VHT))
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003288 rate = &sband->bitrates[status->rate_idx];
3289
3290 ieee80211_rx_cooked_monitor(rx, rate);
3291 break;
3292 }
3293 case RX_DROP_UNUSABLE:
3294 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3295 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003296 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003297 dev_kfree_skb(rx->skb);
3298 break;
3299 case RX_QUEUED:
3300 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3301 break;
3302 }
3303}
3304
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003305static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3306 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003307{
3308 ieee80211_rx_result res = RX_DROP_MONITOR;
3309 struct sk_buff *skb;
3310
3311#define CALL_RXH(rxh) \
3312 do { \
3313 res = rxh(rx); \
3314 if (res != RX_CONTINUE) \
3315 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003316 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003317
Johannes Berg45ceeee2015-03-16 09:08:20 +01003318 /* Lock here to avoid hitting all of the data used in the RX
3319 * path (e.g. key data, station data, ...) concurrently when
3320 * a frame is released from the reorder buffer due to timeout
3321 * from the timer, potentially concurrently with RX from the
3322 * driver.
3323 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003324 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003325
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003326 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003327 /*
3328 * all the other fields are valid across frames
3329 * that belong to an aMPDU since they are on the
3330 * same TID from the same station
3331 */
3332 rx->skb = skb;
3333
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003334 CALL_RXH(ieee80211_rx_h_check_more_data);
3335 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3336 CALL_RXH(ieee80211_rx_h_sta_process);
3337 CALL_RXH(ieee80211_rx_h_decrypt);
3338 CALL_RXH(ieee80211_rx_h_defragment);
3339 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003340 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003341#ifdef CONFIG_MAC80211_MESH
3342 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3343 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3344#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003345 CALL_RXH(ieee80211_rx_h_amsdu);
3346 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003347
3348 /* special treatment -- needs the queue */
3349 res = ieee80211_rx_h_ctrl(rx, frames);
3350 if (res != RX_CONTINUE)
3351 goto rxh_next;
3352
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003353 CALL_RXH(ieee80211_rx_h_mgmt_check);
3354 CALL_RXH(ieee80211_rx_h_action);
3355 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3356 CALL_RXH(ieee80211_rx_h_action_return);
3357 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003358
3359 rxh_next:
3360 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003361
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003362#undef CALL_RXH
3363 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003364
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003365 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003366}
Johannes Berg571ecf62007-07-27 15:43:22 +02003367
Johannes Berg4406c372010-09-24 11:21:06 +02003368static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003369{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003370 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003371 ieee80211_rx_result res = RX_DROP_MONITOR;
3372
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003373 __skb_queue_head_init(&reorder_release);
3374
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003375#define CALL_RXH(rxh) \
3376 do { \
3377 res = rxh(rx); \
3378 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003379 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003380 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003381
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003382 CALL_RXH(ieee80211_rx_h_check_dup);
3383 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003384
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003385 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003386
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003387 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003388 return;
Johannes Berg49461622008-06-30 15:10:45 +02003389
Johannes Berg2569a822009-11-25 17:46:17 +01003390 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003391 ieee80211_rx_handlers_result(rx, res);
3392
3393#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003394}
3395
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003396/*
Johannes Bergdd318572010-11-29 11:09:16 +01003397 * This function makes calls into the RX path, therefore
3398 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003399 */
3400void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3401{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003402 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003403 struct ieee80211_rx_data rx = {
3404 .sta = sta,
3405 .sdata = sta->sdata,
3406 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003407 /* This is OK -- must be QoS data frame */
3408 .security_idx = tid,
3409 .seqno_idx = tid,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02003410 .napi = NULL, /* must be NULL to not have races */
Johannes Berg554891e2010-09-24 12:38:25 +02003411 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003412 struct tid_ampdu_rx *tid_agg_rx;
3413
3414 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3415 if (!tid_agg_rx)
3416 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003417
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003418 __skb_queue_head_init(&frames);
3419
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003420 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003421 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003422 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003423
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003424 if (!skb_queue_empty(&frames)) {
3425 struct ieee80211_event event = {
3426 .type = BA_FRAME_TIMEOUT,
3427 .u.ba.tid = tid,
3428 .u.ba.sta = &sta->sta,
3429 };
3430 drv_event_callback(rx.local, rx.sdata, &event);
3431 }
3432
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003433 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003434}
3435
Sara Sharon06470f72016-01-28 16:19:25 +02003436void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3437 u16 ssn, u64 filtered,
3438 u16 received_mpdus)
3439{
3440 struct sta_info *sta;
3441 struct tid_ampdu_rx *tid_agg_rx;
3442 struct sk_buff_head frames;
3443 struct ieee80211_rx_data rx = {
3444 /* This is OK -- must be QoS data frame */
3445 .security_idx = tid,
3446 .seqno_idx = tid,
3447 };
3448 int i, diff;
3449
3450 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3451 return;
3452
3453 __skb_queue_head_init(&frames);
3454
3455 sta = container_of(pubsta, struct sta_info, sta);
3456
3457 rx.sta = sta;
3458 rx.sdata = sta->sdata;
3459 rx.local = sta->local;
3460
3461 rcu_read_lock();
3462 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3463 if (!tid_agg_rx)
3464 goto out;
3465
3466 spin_lock_bh(&tid_agg_rx->reorder_lock);
3467
3468 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3469 int release;
3470
3471 /* release all frames in the reorder buffer */
3472 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3473 IEEE80211_SN_MODULO;
3474 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3475 release, &frames);
3476 /* update ssn to match received ssn */
3477 tid_agg_rx->head_seq_num = ssn;
3478 } else {
3479 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3480 &frames);
3481 }
3482
3483 /* handle the case that received ssn is behind the mac ssn.
3484 * it can be tid_agg_rx->buf_size behind and still be valid */
3485 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3486 if (diff >= tid_agg_rx->buf_size) {
3487 tid_agg_rx->reorder_buf_filtered = 0;
3488 goto release;
3489 }
3490 filtered = filtered >> diff;
3491 ssn += diff;
3492
3493 /* update bitmap */
3494 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3495 int index = (ssn + i) % tid_agg_rx->buf_size;
3496
3497 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3498 if (filtered & BIT_ULL(i))
3499 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3500 }
3501
3502 /* now process also frames that the filter marking released */
3503 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3504
3505release:
3506 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3507
3508 ieee80211_rx_handlers(&rx, &frames);
3509
3510 out:
3511 rcu_read_unlock();
3512}
3513EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3514
Johannes Berg571ecf62007-07-27 15:43:22 +02003515/* main receive path */
3516
Johannes Berga58fbe12015-04-22 15:08:39 +02003517static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003518{
Johannes Berg20b01f82010-09-24 11:21:05 +02003519 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003520 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02003521 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003522 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3523 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02003524 int multicast = is_multicast_ether_addr(hdr->addr1);
3525
Johannes Berg51fb61e2007-12-19 01:31:27 +01003526 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003527 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01003528 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003529 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003530 if (multicast)
3531 return true;
3532 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003533 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02003534 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003535 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02003536 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3537 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003538 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003539 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003540 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02003541 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003542 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003543 if (!multicast &&
3544 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003545 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003546 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003547 int rate_idx;
Johannes Berg56146182012-11-09 15:07:02 +01003548 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3549 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003550 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003551 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01003552 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3553 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003554 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003555 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003556 case NL80211_IFTYPE_OCB:
3557 if (!bssid)
3558 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02003559 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003560 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003561 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003562 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003563 if (!multicast &&
3564 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003565 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003566 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003567 int rate_idx;
3568 if (status->flag & RX_FLAG_HT)
3569 rate_idx = 0; /* TODO: HT rates */
3570 else
3571 rate_idx = status->rate_idx;
3572 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3573 BIT(rate_idx));
3574 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003575 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02003576 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga58fbe12015-04-22 15:08:39 +02003577 if (multicast)
3578 return true;
3579 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003580 case NL80211_IFTYPE_AP_VLAN:
3581 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02003582 if (!bssid)
3583 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3584
3585 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01003586 /*
3587 * Accept public action frames even when the
3588 * BSSID doesn't match, this is used for P2P
3589 * and location updates. Note that mac80211
3590 * itself never looks at these frames.
3591 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02003592 if (!multicast &&
3593 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003594 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02003595 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003596 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02003597 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02003598 }
3599
3600 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003601 /* ignore data frames to TDLS-peers */
3602 if (ieee80211_is_data(hdr->frame_control))
3603 return false;
3604 /* ignore action frames to TDLS-peers */
3605 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02003606 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003607 !ether_addr_equal(bssid, hdr->addr1))
3608 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02003609 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003610 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02003611 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07003612 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003613 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003614 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
Johannes Bergf142c6b2012-06-18 20:07:15 +02003615 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02003616 return ieee80211_is_public_action(hdr, skb->len) ||
3617 ieee80211_is_probe_req(hdr->frame_control) ||
3618 ieee80211_is_probe_resp(hdr->frame_control) ||
3619 ieee80211_is_beacon(hdr->frame_control);
Ayala Bekercb3b7d82016-09-20 17:31:13 +03003620 case NL80211_IFTYPE_NAN:
3621 /* Currently no frames on NAN interface are allowed */
3622 return false;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02003623 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02003624 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02003625 }
3626
Johannes Berga58fbe12015-04-22 15:08:39 +02003627 WARN_ON_ONCE(1);
3628 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02003629}
3630
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003631void ieee80211_check_fast_rx(struct sta_info *sta)
3632{
3633 struct ieee80211_sub_if_data *sdata = sta->sdata;
3634 struct ieee80211_local *local = sdata->local;
3635 struct ieee80211_key *key;
3636 struct ieee80211_fast_rx fastrx = {
3637 .dev = sdata->dev,
3638 .vif_type = sdata->vif.type,
3639 .control_port_protocol = sdata->control_port_protocol,
3640 }, *old, *new = NULL;
3641 bool assign = false;
3642
3643 /* use sparse to check that we don't return without updating */
3644 __acquire(check_fast_rx);
3645
3646 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3647 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3648 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3649 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3650
Johannes Bergc9c59622016-03-31 20:02:11 +03003651 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3652
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003653 /* fast-rx doesn't do reordering */
3654 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3655 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3656 goto clear;
3657
3658 switch (sdata->vif.type) {
3659 case NL80211_IFTYPE_STATION:
3660 /* 4-addr is harder to deal with, later maybe */
3661 if (sdata->u.mgd.use_4addr)
3662 goto clear;
3663 /* software powersave is a huge mess, avoid all of it */
3664 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3665 goto clear;
3666 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3667 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3668 goto clear;
3669 if (sta->sta.tdls) {
3670 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3671 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3672 fastrx.expected_ds_bits = 0;
3673 } else {
3674 fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3675 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3676 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3677 fastrx.expected_ds_bits =
3678 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3679 }
3680 break;
3681 case NL80211_IFTYPE_AP_VLAN:
3682 case NL80211_IFTYPE_AP:
3683 /* parallel-rx requires this, at least with calls to
3684 * ieee80211_sta_ps_transition()
3685 */
3686 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3687 goto clear;
3688 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3689 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3690 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3691
3692 fastrx.internal_forward =
3693 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3694 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3695 !sdata->u.vlan.sta);
3696 break;
3697 default:
3698 goto clear;
3699 }
3700
3701 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3702 goto clear;
3703
3704 rcu_read_lock();
3705 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3706 if (key) {
3707 switch (key->conf.cipher) {
3708 case WLAN_CIPHER_SUITE_TKIP:
3709 /* we don't want to deal with MMIC in fast-rx */
3710 goto clear_rcu;
3711 case WLAN_CIPHER_SUITE_CCMP:
3712 case WLAN_CIPHER_SUITE_CCMP_256:
3713 case WLAN_CIPHER_SUITE_GCMP:
3714 case WLAN_CIPHER_SUITE_GCMP_256:
3715 break;
3716 default:
3717 /* we also don't want to deal with WEP or cipher scheme
3718 * since those require looking up the key idx in the
3719 * frame, rather than assuming the PTK is used
3720 * (we need to revisit this once we implement the real
3721 * PTK index, which is now valid in the spec, but we
3722 * haven't implemented that part yet)
3723 */
3724 goto clear_rcu;
3725 }
3726
3727 fastrx.key = true;
3728 fastrx.icv_len = key->conf.icv_len;
3729 }
3730
3731 assign = true;
3732 clear_rcu:
3733 rcu_read_unlock();
3734 clear:
3735 __release(check_fast_rx);
3736
3737 if (assign)
3738 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3739
3740 spin_lock_bh(&sta->lock);
3741 old = rcu_dereference_protected(sta->fast_rx, true);
3742 rcu_assign_pointer(sta->fast_rx, new);
3743 spin_unlock_bh(&sta->lock);
3744
3745 if (old)
3746 kfree_rcu(old, rcu_head);
3747}
3748
3749void ieee80211_clear_fast_rx(struct sta_info *sta)
3750{
3751 struct ieee80211_fast_rx *old;
3752
3753 spin_lock_bh(&sta->lock);
3754 old = rcu_dereference_protected(sta->fast_rx, true);
3755 RCU_INIT_POINTER(sta->fast_rx, NULL);
3756 spin_unlock_bh(&sta->lock);
3757
3758 if (old)
3759 kfree_rcu(old, rcu_head);
3760}
3761
3762void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3763{
3764 struct ieee80211_local *local = sdata->local;
3765 struct sta_info *sta;
3766
3767 lockdep_assert_held(&local->sta_mtx);
3768
3769 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3770 if (sdata != sta->sdata &&
3771 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3772 continue;
3773 ieee80211_check_fast_rx(sta);
3774 }
3775}
3776
3777void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3778{
3779 struct ieee80211_local *local = sdata->local;
3780
3781 mutex_lock(&local->sta_mtx);
3782 __ieee80211_check_fast_rx_iface(sdata);
3783 mutex_unlock(&local->sta_mtx);
3784}
3785
3786static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3787 struct ieee80211_fast_rx *fast_rx)
3788{
3789 struct sk_buff *skb = rx->skb;
3790 struct ieee80211_hdr *hdr = (void *)skb->data;
3791 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3792 struct sta_info *sta = rx->sta;
3793 int orig_len = skb->len;
3794 int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3795 struct {
3796 u8 snap[sizeof(rfc1042_header)];
3797 __be16 proto;
3798 } *payload __aligned(2);
3799 struct {
3800 u8 da[ETH_ALEN];
3801 u8 sa[ETH_ALEN];
3802 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03003803 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3804
3805 if (fast_rx->uses_rss)
3806 stats = this_cpu_ptr(sta->pcpu_rx_stats);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003807
3808 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3809 * to a common data structure; drivers can implement that per queue
3810 * but we don't have that information in mac80211
3811 */
3812 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3813 return false;
3814
3815#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3816
3817 /* If using encryption, we also need to have:
3818 * - PN_VALIDATED: similar, but the implementation is tricky
3819 * - DECRYPTED: necessary for PN_VALIDATED
3820 */
3821 if (fast_rx->key &&
3822 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
3823 return false;
3824
3825 /* we don't deal with A-MSDU deaggregation here */
3826 if (status->rx_flags & IEEE80211_RX_AMSDU)
3827 return false;
3828
3829 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3830 return false;
3831
3832 if (unlikely(ieee80211_is_frag(hdr)))
3833 return false;
3834
3835 /* Since our interface address cannot be multicast, this
3836 * implicitly also rejects multicast frames without the
3837 * explicit check.
3838 *
3839 * We shouldn't get any *data* frames not addressed to us
3840 * (AP mode will accept multicast *management* frames), but
3841 * punting here will make it go through the full checks in
3842 * ieee80211_accept_frame().
3843 */
3844 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
3845 return false;
3846
3847 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
3848 IEEE80211_FCTL_TODS)) !=
3849 fast_rx->expected_ds_bits)
3850 goto drop;
3851
3852 /* assign the key to drop unencrypted frames (later)
3853 * and strip the IV/MIC if necessary
3854 */
3855 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
3856 /* GCMP header length is the same */
3857 snap_offs += IEEE80211_CCMP_HDR_LEN;
3858 }
3859
3860 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
3861 goto drop;
3862 payload = (void *)(skb->data + snap_offs);
3863
3864 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
3865 return false;
3866
3867 /* Don't handle these here since they require special code.
3868 * Accept AARP and IPX even though they should come with a
3869 * bridge-tunnel header - but if we get them this way then
3870 * there's little point in discarding them.
3871 */
3872 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
3873 payload->proto == fast_rx->control_port_protocol))
3874 return false;
3875
3876 /* after this point, don't punt to the slowpath! */
3877
3878 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
3879 pskb_trim(skb, skb->len - fast_rx->icv_len))
3880 goto drop;
3881
3882 if (unlikely(fast_rx->sta_notify)) {
3883 ieee80211_sta_rx_notify(rx->sdata, hdr);
3884 fast_rx->sta_notify = false;
3885 }
3886
3887 /* statistics part of ieee80211_rx_h_sta_process() */
Johannes Bergc9c59622016-03-31 20:02:11 +03003888 stats->last_rx = jiffies;
3889 stats->last_rate = sta_stats_encode_rate(status);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003890
Johannes Bergc9c59622016-03-31 20:02:11 +03003891 stats->fragments++;
Johannes Berg0328edc2017-02-20 08:59:16 +01003892 stats->packets++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003893
3894 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Bergc9c59622016-03-31 20:02:11 +03003895 stats->last_signal = status->signal;
3896 if (!fast_rx->uses_rss)
3897 ewma_signal_add(&sta->rx_stats_avg.signal,
3898 -status->signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003899 }
3900
3901 if (status->chains) {
3902 int i;
3903
Johannes Bergc9c59622016-03-31 20:02:11 +03003904 stats->chains = status->chains;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003905 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
3906 int signal = status->chain_signal[i];
3907
3908 if (!(status->chains & BIT(i)))
3909 continue;
3910
Johannes Bergc9c59622016-03-31 20:02:11 +03003911 stats->chain_signal_last[i] = signal;
3912 if (!fast_rx->uses_rss)
3913 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
3914 -signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003915 }
3916 }
3917 /* end of statistics */
3918
3919 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
3920 goto drop;
3921
3922 /* do the header conversion - first grab the addresses */
3923 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
3924 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
3925 /* remove the SNAP but leave the ethertype */
3926 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
3927 /* push the addresses in front */
3928 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
3929
3930 skb->dev = fast_rx->dev;
3931
3932 ieee80211_rx_stats(fast_rx->dev, skb->len);
3933
3934 /* The seqno index has the same property as needed
3935 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
3936 * for non-QoS-data frames. Here we know it's a data
3937 * frame, so count MSDUs.
3938 */
Johannes Bergc9c59622016-03-31 20:02:11 +03003939 u64_stats_update_begin(&stats->syncp);
3940 stats->msdu[rx->seqno_idx]++;
3941 stats->bytes += orig_len;
3942 u64_stats_update_end(&stats->syncp);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003943
3944 if (fast_rx->internal_forward) {
Johannes Bergeeb0d562016-12-14 16:47:43 +01003945 struct sk_buff *xmit_skb = NULL;
3946 bool multicast = is_multicast_ether_addr(skb->data);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003947
Johannes Bergeeb0d562016-12-14 16:47:43 +01003948 if (multicast) {
3949 xmit_skb = skb_copy(skb, GFP_ATOMIC);
3950 } else if (sta_info_get(rx->sdata, skb->data)) {
3951 xmit_skb = skb;
3952 skb = NULL;
3953 }
3954
3955 if (xmit_skb) {
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003956 /*
3957 * Send to wireless media and increase priority by 256
3958 * to keep the received priority instead of
3959 * reclassifying the frame (see cfg80211_classify8021d).
3960 */
Johannes Bergeeb0d562016-12-14 16:47:43 +01003961 xmit_skb->priority += 256;
3962 xmit_skb->protocol = htons(ETH_P_802_3);
3963 skb_reset_network_header(xmit_skb);
3964 skb_reset_mac_header(xmit_skb);
3965 dev_queue_xmit(xmit_skb);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003966 }
Johannes Bergeeb0d562016-12-14 16:47:43 +01003967
3968 if (!skb)
3969 return true;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003970 }
3971
3972 /* deliver to local stack */
3973 skb->protocol = eth_type_trans(skb, fast_rx->dev);
3974 memset(skb->cb, 0, sizeof(skb->cb));
3975 if (rx->napi)
3976 napi_gro_receive(rx->napi, skb);
3977 else
3978 netif_receive_skb(skb);
3979
3980 return true;
3981 drop:
3982 dev_kfree_skb(skb);
Johannes Bergc9c59622016-03-31 20:02:11 +03003983 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003984 return true;
3985}
3986
Johannes Berg571ecf62007-07-27 15:43:22 +02003987/*
Johannes Berg4406c372010-09-24 11:21:06 +02003988 * This function returns whether or not the SKB
3989 * was destined for RX processing or not, which,
3990 * if consume is true, is equivalent to whether
3991 * or not the skb was consumed.
3992 */
3993static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3994 struct sk_buff *skb, bool consume)
3995{
3996 struct ieee80211_local *local = rx->local;
3997 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02003998
3999 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02004000
Johannes Berg49ddf8e2016-03-31 20:02:10 +03004001 /* See if we can do fast-rx; if we have to copy we already lost,
4002 * so punt in that case. We should never have to deliver a data
4003 * frame to multiple interfaces anyway.
4004 *
4005 * We skip the ieee80211_accept_frame() call and do the necessary
4006 * checking inside ieee80211_invoke_fast_rx().
4007 */
4008 if (consume && rx->sta) {
4009 struct ieee80211_fast_rx *fast_rx;
4010
4011 fast_rx = rcu_dereference(rx->sta->fast_rx);
4012 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4013 return true;
4014 }
4015
Johannes Berga58fbe12015-04-22 15:08:39 +02004016 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02004017 return false;
4018
Johannes Berg4406c372010-09-24 11:21:06 +02004019 if (!consume) {
4020 skb = skb_copy(skb, GFP_ATOMIC);
4021 if (!skb) {
4022 if (net_ratelimit())
4023 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08004024 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02004025 sdata->name);
4026 return true;
4027 }
4028
4029 rx->skb = skb;
4030 }
4031
4032 ieee80211_invoke_rx_handlers(rx);
4033 return true;
4034}
4035
4036/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08004037 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004038 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02004039 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02004040static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03004041 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004042 struct sk_buff *skb,
4043 struct napi_struct *napi)
Johannes Berg571ecf62007-07-27 15:43:22 +02004044{
4045 struct ieee80211_local *local = hw_to_local(hw);
4046 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02004047 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b32010-03-29 17:35:07 +08004048 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004049 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02004050 struct ieee80211_sub_if_data *prev;
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004051 struct rhlist_head *tmp;
Zhu Yie3cf8b32010-03-29 17:35:07 +08004052 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02004053
Zhu Yie3cf8b32010-03-29 17:35:07 +08004054 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02004055 memset(&rx, 0, sizeof(rx));
4056 rx.skb = skb;
4057 rx.local = local;
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004058 rx.napi = napi;
Johannes Berg72abd812007-09-17 01:29:22 -04004059
Zhu Yie3cf8b32010-03-29 17:35:07 +08004060 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02004061 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02004062
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004063 if (ieee80211_is_mgmt(fc)) {
4064 /* drop frame if too short for header */
4065 if (skb->len < ieee80211_hdrlen(fc))
4066 err = -ENOBUFS;
4067 else
4068 err = skb_linearize(skb);
4069 } else {
Zhu Yie3cf8b32010-03-29 17:35:07 +08004070 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02004071 }
Zhu Yie3cf8b32010-03-29 17:35:07 +08004072
4073 if (err) {
4074 dev_kfree_skb(skb);
4075 return;
4076 }
4077
4078 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01004079 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01004080 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01004081
Johannes Bergd48b2962012-07-06 22:19:27 +02004082 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4083 ieee80211_is_beacon(hdr->frame_control)))
4084 ieee80211_scan_rx(local, skb);
4085
Johannes Berg19d19e92017-02-27 09:38:11 +01004086 if (ieee80211_is_data(fc)) {
Johannes Bergd63b5482016-03-31 20:02:02 +03004087 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004088
Johannes Berg19d19e92017-02-27 09:38:11 +01004089 if (pubsta) {
4090 rx.sta = container_of(pubsta, struct sta_info, sta);
4091 rx.sdata = rx.sta->sdata;
4092 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4093 return;
4094 goto out;
4095 }
4096
Ben Greear56af3262010-09-23 10:22:24 -07004097 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004098
Herbert Xu83e7e4c2016-09-19 19:00:10 +08004099 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004100 if (!prev_sta) {
4101 prev_sta = sta;
4102 continue;
4103 }
4104
4105 rx.sta = prev_sta;
4106 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004107 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004108
Ben Greear56af3262010-09-23 10:22:24 -07004109 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004110 }
Ben Greear56af3262010-09-23 10:22:24 -07004111
4112 if (prev_sta) {
4113 rx.sta = prev_sta;
4114 rx.sdata = prev_sta->sdata;
4115
Johannes Berg4406c372010-09-24 11:21:06 +02004116 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004117 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304118 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004119 }
Johannes Berg4406c372010-09-24 11:21:06 +02004120 }
4121
Johannes Berg4b0dd982010-09-24 11:21:07 +02004122 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004123
Johannes Berg4b0dd982010-09-24 11:21:07 +02004124 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4125 if (!ieee80211_sdata_running(sdata))
4126 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004127
Johannes Berg4b0dd982010-09-24 11:21:07 +02004128 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4129 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4130 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004131
Johannes Berg4b0dd982010-09-24 11:21:07 +02004132 /*
4133 * frame is destined for this interface, but if it's
4134 * not also for the previous one we handle that after
4135 * the loop to avoid copying the SKB once too much
4136 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004137
Johannes Berg4b0dd982010-09-24 11:21:07 +02004138 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004139 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004140 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004141 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004142
Johannes Berg7852e362012-01-20 13:55:24 +01004143 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004144 rx.sdata = prev;
4145 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004146
Johannes Berg4b0dd982010-09-24 11:21:07 +02004147 prev = sdata;
4148 }
Johannes Berg4406c372010-09-24 11:21:06 +02004149
Johannes Berg4b0dd982010-09-24 11:21:07 +02004150 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004151 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004152 rx.sdata = prev;
4153
4154 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4155 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004156 }
Johannes Berg4406c372010-09-24 11:21:06 +02004157
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304158 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004159 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004160}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004161
4162/*
4163 * This is the receive path handler. It is called by a low level driver when an
4164 * 802.11 MPDU is received from the hardware.
4165 */
Johannes Bergd63b5482016-03-31 20:02:02 +03004166void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4167 struct sk_buff *skb, struct napi_struct *napi)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004168{
4169 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004170 struct ieee80211_rate *rate = NULL;
4171 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004172 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004173
Johannes Bergd20ef632009-10-11 15:10:40 +02004174 WARN_ON_ONCE(softirq_count() == 0);
4175
Johannes Berg57fbcce2016-04-12 15:56:15 +02004176 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004177 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004178
4179 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004180 if (WARN_ON(!sband))
4181 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004182
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004183 /*
4184 * If we're suspending, it is possible although not too likely
4185 * that we'd be receiving frames after having already partially
4186 * quiesced the stack. We can't process such frames then since
4187 * that might, for example, cause stations to be added or other
4188 * driver callbacks be invoked.
4189 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004190 if (unlikely(local->quiescing || local->suspended))
4191 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004192
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004193 /* We might be during a HW reconfig, prevent Rx for the same reason */
4194 if (unlikely(local->in_reconfig))
4195 goto drop;
4196
Johannes Bergea77f122009-08-21 14:44:45 +02004197 /*
4198 * The same happens when we're not even started,
4199 * but that's worth a warning.
4200 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004201 if (WARN_ON(!local->started))
4202 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004203
Johannes Bergfc885182010-07-30 13:23:12 +02004204 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004205 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004206 * Validate the rate, unless a PLCP error means that
4207 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004208 */
Johannes Bergfc885182010-07-30 13:23:12 +02004209
4210 if (status->flag & RX_FLAG_HT) {
4211 /*
4212 * rate_idx is MCS index, which can be [0-76]
4213 * as documented on:
4214 *
4215 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4216 *
4217 * Anything else would be some sort of driver or
4218 * hardware error. The driver should catch hardware
4219 * errors.
4220 */
Johannes Berg444e3802012-09-30 17:08:35 +02004221 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004222 "Rate marked as an HT rate but passed "
4223 "status->rate_idx is not "
4224 "an MCS index [0-76]: %d (0x%02x)\n",
4225 status->rate_idx,
4226 status->rate_idx))
4227 goto drop;
Johannes Berg56146182012-11-09 15:07:02 +01004228 } else if (status->flag & RX_FLAG_VHT) {
4229 if (WARN_ONCE(status->rate_idx > 9 ||
4230 !status->vht_nss ||
4231 status->vht_nss > 8,
4232 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4233 status->rate_idx, status->vht_nss))
4234 goto drop;
Johannes Bergfc885182010-07-30 13:23:12 +02004235 } else {
Johannes Berg444e3802012-09-30 17:08:35 +02004236 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004237 goto drop;
4238 rate = &sband->bitrates[status->rate_idx];
4239 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004240 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004241
Johannes Berg554891e2010-09-24 12:38:25 +02004242 status->rx_flags = 0;
4243
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004244 /*
4245 * key references and virtual interfaces are protected using RCU
4246 * and this requires that we are in a read-side RCU section during
4247 * receive processing
4248 */
4249 rcu_read_lock();
4250
4251 /*
4252 * Frames with failed FCS/PLCP checksum are not returned,
4253 * all other frames are returned without radiotap header
4254 * if it was previously present.
4255 * Also, frames with less than 16 bytes are dropped.
4256 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004257 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004258 if (!skb) {
4259 rcu_read_unlock();
4260 return;
4261 }
4262
Johannes Berge1e54062010-11-30 08:58:45 +01004263 ieee80211_tpt_led_trig_rx(local,
4264 ((struct ieee80211_hdr *)skb->data)->frame_control,
4265 skb->len);
Johannes Bergd63b5482016-03-31 20:02:02 +03004266
4267 __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004268
4269 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02004270
4271 return;
4272 drop:
4273 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004274}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004275EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004276
4277/* This is a version of the rx handler that can be called from hard irq
4278 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004279void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004280{
4281 struct ieee80211_local *local = hw_to_local(hw);
4282
4283 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4284
Johannes Berg571ecf62007-07-27 15:43:22 +02004285 skb->pkt_type = IEEE80211_RX_MSG;
4286 skb_queue_tail(&local->skb_queue, skb);
4287 tasklet_schedule(&local->tasklet);
4288}
4289EXPORT_SYMBOL(ieee80211_rx_irqsafe);