blob: 6a265aa73a46769476fc96cf3e522fd3ae1071be [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
Johannes Bergfb4ea052016-01-28 16:19:24 +02007 * Copyright(c) 2015 - 2016 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 */
98static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
Johannes Berg1f7bba72014-11-06 22:56:36 +010099 struct sk_buff *skb,
100 unsigned int rtap_vendor_space)
Johannes Bergb2e77712007-09-26 15:19:39 +0200101{
Johannes Berg30686bf2015-06-02 21:39:54 +0200102 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200103 if (likely(skb->len > FCS_LEN))
Zhu Yie3cf8b32010-03-29 17:35:07 +0800104 __pskb_trim(skb, skb->len - FCS_LEN);
Johannes Bergb2e77712007-09-26 15:19:39 +0200105 else {
106 /* driver bug */
107 WARN_ON(1);
108 dev_kfree_skb(skb);
Dan Carpenter06247602012-11-27 20:31:19 +0300109 return NULL;
Johannes Bergb2e77712007-09-26 15:19:39 +0200110 }
111 }
112
Johannes Berg1f7bba72014-11-06 22:56:36 +0100113 __pskb_pull(skb, rtap_vendor_space);
114
Johannes Bergb2e77712007-09-26 15:19:39 +0200115 return skb;
116}
117
Johannes Berg1f7bba72014-11-06 22:56:36 +0100118static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
119 unsigned int rtap_vendor_space)
Johannes Bergb2e77712007-09-26 15:19:39 +0200120{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200121 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100122 struct ieee80211_hdr *hdr;
123
124 hdr = (void *)(skb->data + rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200125
Johannes Berg4c298672012-07-05 11:34:31 +0200126 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100127 RX_FLAG_FAILED_PLCP_CRC |
128 RX_FLAG_ONLY_MONITOR))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800129 return true;
130
Johannes Berg1f7bba72014-11-06 22:56:36 +0100131 if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800132 return true;
133
Harvey Harrison87228f52008-06-11 14:21:59 -0700134 if (ieee80211_is_ctl(hdr->frame_control) &&
135 !ieee80211_is_pspoll(hdr->frame_control) &&
136 !ieee80211_is_back_req(hdr->frame_control))
Zhao, Gang6b59db72014-04-21 12:52:59 +0800137 return true;
138
139 return false;
Johannes Bergb2e77712007-09-26 15:19:39 +0200140}
141
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200142static int
Johannes Berg1f7bba72014-11-06 22:56:36 +0100143ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
144 struct ieee80211_rx_status *status,
145 struct sk_buff *skb)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200146{
147 int len;
148
149 /* always present fields */
Johannes Berga144f372013-07-03 13:34:02 +0200150 len = sizeof(struct ieee80211_radiotap_header) + 8;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200151
Johannes Berga144f372013-07-03 13:34:02 +0200152 /* allocate extra bitmaps */
Johannes Berga144f372013-07-03 13:34:02 +0200153 if (status->chains)
154 len += 4 * hweight8(status->chains);
Johannes Berg90b9e4462012-11-16 10:09:08 +0100155
156 if (ieee80211_have_rx_timestamp(status)) {
157 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200158 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +0100159 }
Johannes Berg30686bf2015-06-02 21:39:54 +0200160 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200161 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200162
Johannes Berga144f372013-07-03 13:34:02 +0200163 /* antenna field, if we don't have per-chain info */
164 if (!status->chains)
165 len += 1;
166
Johannes Berg90b9e4462012-11-16 10:09:08 +0100167 /* padding for RX_FLAGS if necessary */
168 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200169
Johannes Berg6d744ba2011-01-27 14:13:17 +0100170 if (status->flag & RX_FLAG_HT) /* HT info */
171 len += 3;
172
Johannes Berg4c298672012-07-05 11:34:31 +0200173 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100174 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200175 len += 8;
176 }
177
Johannes Berg51648922012-11-22 23:00:18 +0100178 if (status->flag & RX_FLAG_VHT) {
179 len = ALIGN(len, 2);
180 len += 12;
181 }
182
Johannes Berga144f372013-07-03 13:34:02 +0200183 if (status->chains) {
184 /* antenna and antenna signal fields */
185 len += 2 * hweight8(status->chains);
186 }
187
Johannes Berg1f7bba72014-11-06 22:56:36 +0100188 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
189 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
190
191 /* vendor presence bitmap */
192 len += 4;
193 /* alignment for fixed 6-byte vendor data header */
194 len = ALIGN(len, 2);
195 /* vendor data header */
196 len += 6;
197 if (WARN_ON(rtap->align == 0))
198 rtap->align = 1;
199 len = ALIGN(len, rtap->align);
200 len += rtap->len + rtap->pad;
201 }
202
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200203 return len;
204}
205
Johannes Berg00ea6de2012-09-05 15:54:51 +0200206/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200207 * ieee80211_add_rx_radiotap_header - add radiotap header
208 *
209 * add a radiotap header containing all the fields which the hardware provided.
210 */
211static void
212ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
213 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200214 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200215 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200216{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200217 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200218 struct ieee80211_radiotap_header *rthdr;
219 unsigned char *pos;
Johannes Berga144f372013-07-03 13:34:02 +0200220 __le32 *it_present;
221 u32 it_present_val;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100222 u16 rx_flags = 0;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200223 u16 channel_flags = 0;
Johannes Berga144f372013-07-03 13:34:02 +0200224 int mpdulen, chain;
225 unsigned long chains = status->chains;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100226 struct ieee80211_vendor_radiotap rtap = {};
227
228 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
229 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
230 /* rtap.len and rtap.pad are undone immediately */
231 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
232 }
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800233
234 mpdulen = skb->len;
Johannes Berg30686bf2015-06-02 21:39:54 +0200235 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800236 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200237
238 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100239 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
Johannes Berga144f372013-07-03 13:34:02 +0200240 it_present = &rthdr->it_present;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200241
242 /* radiotap header, set always present flags */
Emmanuel Grumbach0059b2b2014-02-05 16:36:01 +0200243 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Berga144f372013-07-03 13:34:02 +0200244 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
245 BIT(IEEE80211_RADIOTAP_CHANNEL) |
246 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200247
Johannes Berga144f372013-07-03 13:34:02 +0200248 if (!status->chains)
249 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
250
251 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
252 it_present_val |=
253 BIT(IEEE80211_RADIOTAP_EXT) |
254 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
255 put_unaligned_le32(it_present_val, it_present);
256 it_present++;
257 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
258 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
259 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200260
Johannes Berg1f7bba72014-11-06 22:56:36 +0100261 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
262 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
263 BIT(IEEE80211_RADIOTAP_EXT);
264 put_unaligned_le32(it_present_val, it_present);
265 it_present++;
266 it_present_val = rtap.present;
267 }
268
Johannes Berga144f372013-07-03 13:34:02 +0200269 put_unaligned_le32(it_present_val, it_present);
270
271 pos = (void *)(it_present + 1);
272
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200273 /* the order of the following fields is important */
274
275 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800276 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100277 /* padding */
278 while ((pos - (u8 *)rthdr) & 7)
279 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800280 put_unaligned_le64(
281 ieee80211_calculate_rx_timestamp(local, status,
282 mpdulen, 0),
283 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200284 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200285 pos += 8;
286 }
287
288 /* IEEE80211_RADIOTAP_FLAGS */
Johannes Berg30686bf2015-06-02 21:39:54 +0200289 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200290 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100291 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
292 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200293 if (status->flag & RX_FLAG_SHORTPRE)
294 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200295 pos++;
296
297 /* IEEE80211_RADIOTAP_RATE */
Johannes Berg56146182012-11-09 15:07:02 +0100298 if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200299 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100300 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530301 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200302 * added below. The byte here is needed as padding
303 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200304 */
305 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200306 } else {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200307 int shift = 0;
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200308 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200309 if (status->flag & RX_FLAG_10MHZ)
310 shift = 1;
311 else if (status->flag & RX_FLAG_5MHZ)
312 shift = 2;
313 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200314 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200315 pos++;
316
317 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100318 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200319 pos += 2;
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200320 if (status->flag & RX_FLAG_10MHZ)
321 channel_flags |= IEEE80211_CHAN_HALF;
322 else if (status->flag & RX_FLAG_5MHZ)
323 channel_flags |= IEEE80211_CHAN_QUARTER;
324
Johannes Berg57fbcce2016-04-12 15:56:15 +0200325 if (status->band == NL80211_BAND_5GHZ)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200326 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
Johannes Berg56146182012-11-09 15:07:02 +0100327 else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200328 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100329 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200330 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100331 else if (rate)
Mathy Vanhoef3a5c5e82015-01-20 15:05:08 +0100332 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100333 else
Simon Wunderlicha5e70692013-07-08 16:55:52 +0200334 channel_flags |= IEEE80211_CHAN_2GHZ;
335 put_unaligned_le16(channel_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200336 pos += 2;
337
338 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Johannes Berg30686bf2015-06-02 21:39:54 +0200339 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100340 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200341 *pos = status->signal;
342 rthdr->it_present |=
343 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
344 pos++;
345 }
346
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200347 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
348
Johannes Berga144f372013-07-03 13:34:02 +0200349 if (!status->chains) {
350 /* IEEE80211_RADIOTAP_ANTENNA */
351 *pos = status->antenna;
352 pos++;
353 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200354
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200355 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
356
357 /* IEEE80211_RADIOTAP_RX_FLAGS */
358 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100359 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100360 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100361 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100362 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
363 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200364 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100365
366 if (status->flag & RX_FLAG_HT) {
Oleksij Rempel786677d2013-05-24 12:05:45 +0200367 unsigned int stbc;
368
Johannes Berg6d744ba2011-01-27 14:13:17 +0100369 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200370 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100371 *pos = 0;
372 if (status->flag & RX_FLAG_SHORT_GI)
373 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
374 if (status->flag & RX_FLAG_40MHZ)
375 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Bergac55d2f2012-05-10 09:09:10 +0200376 if (status->flag & RX_FLAG_HT_GF)
377 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200378 if (status->flag & RX_FLAG_LDPC)
379 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
Oleksij Rempel786677d2013-05-24 12:05:45 +0200380 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
381 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100382 pos++;
383 *pos++ = status->rate_idx;
384 }
Johannes Berg4c298672012-07-05 11:34:31 +0200385
386 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
387 u16 flags = 0;
388
389 /* ensure 4 byte alignment */
390 while ((pos - (u8 *)rthdr) & 3)
391 pos++;
392 rthdr->it_present |=
393 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
394 put_unaligned_le32(status->ampdu_reference, pos);
395 pos += 4;
Johannes Berg4c298672012-07-05 11:34:31 +0200396 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
397 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
398 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
399 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
400 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
401 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
402 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
403 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
404 put_unaligned_le16(flags, pos);
405 pos += 2;
406 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
407 *pos++ = status->ampdu_delimiter_crc;
408 else
409 *pos++ = 0;
410 *pos++ = 0;
411 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100412
Johannes Berg51648922012-11-22 23:00:18 +0100413 if (status->flag & RX_FLAG_VHT) {
414 u16 known = local->hw.radiotap_vht_details;
415
416 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
Johannes Berg51648922012-11-22 23:00:18 +0100417 put_unaligned_le16(known, pos);
418 pos += 2;
419 /* flags */
420 if (status->flag & RX_FLAG_SHORT_GI)
421 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200422 /* in VHT, STBC is binary */
423 if (status->flag & RX_FLAG_STBC_MASK)
424 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
Emmanuel Grumbachfb378c22014-03-04 10:35:25 +0200425 if (status->vht_flag & RX_VHT_FLAG_BF)
426 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
Johannes Berg51648922012-11-22 23:00:18 +0100427 pos++;
428 /* bandwidth */
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +0200429 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Berg51648922012-11-22 23:00:18 +0100430 *pos++ = 4;
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +0200431 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Berg51648922012-11-22 23:00:18 +0100432 *pos++ = 11;
433 else if (status->flag & RX_FLAG_40MHZ)
434 *pos++ = 1;
435 else /* 20 MHz */
436 *pos++ = 0;
437 /* MCS/NSS */
438 *pos = (status->rate_idx << 4) | status->vht_nss;
439 pos += 4;
440 /* coding field */
Emmanuel Grumbach63c361f2014-02-05 12:48:53 +0200441 if (status->flag & RX_FLAG_LDPC)
442 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
Johannes Berg51648922012-11-22 23:00:18 +0100443 pos++;
444 /* group ID */
445 pos++;
446 /* partial_aid */
447 pos += 2;
448 }
449
Johannes Berga144f372013-07-03 13:34:02 +0200450 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
451 *pos++ = status->chain_signal[chain];
452 *pos++ = chain;
453 }
Johannes Berg1f7bba72014-11-06 22:56:36 +0100454
455 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
456 /* ensure 2 byte alignment for the vendor field as required */
457 if ((pos - (u8 *)rthdr) & 1)
458 *pos++ = 0;
459 *pos++ = rtap.oui[0];
460 *pos++ = rtap.oui[1];
461 *pos++ = rtap.oui[2];
462 *pos++ = rtap.subns;
463 put_unaligned_le16(rtap.len, pos);
464 pos += 2;
465 /* align the actual payload as requested */
466 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
467 *pos++ = 0;
468 /* data (and possible padding) already follows */
469 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200470}
471
Johannes Bergb2e77712007-09-26 15:19:39 +0200472/*
473 * This function copies a received frame to all monitor interfaces and
474 * returns a cleaned-up SKB that no longer includes the FCS nor the
475 * radiotap header the driver might have added.
476 */
477static struct sk_buff *
478ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100479 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200480{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200481 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200482 struct ieee80211_sub_if_data *sdata;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100483 int rt_hdrlen, needed_headroom;
Johannes Bergb2e77712007-09-26 15:19:39 +0200484 struct sk_buff *skb, *skb2;
485 struct net_device *prev_dev = NULL;
486 int present_fcs_len = 0;
Johannes Berg1f7bba72014-11-06 22:56:36 +0100487 unsigned int rtap_vendor_space = 0;
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300488 struct ieee80211_mgmt *mgmt;
489 struct ieee80211_sub_if_data *monitor_sdata =
490 rcu_dereference(local->monitor_sdata);
Johannes Berg1f7bba72014-11-06 22:56:36 +0100491
492 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
493 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
494
495 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
496 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200497
498 /*
499 * First, we may need to make a copy of the skb because
500 * (1) we need to modify it for radiotap (if not present), and
501 * (2) the other RX handlers will modify the skb we got.
502 *
503 * We don't need to, of course, if we aren't going to return
504 * the SKB because it has a bad FCS/PLCP checksum.
505 */
Johannes Berg0869aea02009-10-28 10:03:35 +0100506
Johannes Berg30686bf2015-06-02 21:39:54 +0200507 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
Johannes Bergb2e77712007-09-26 15:19:39 +0200508 present_fcs_len = FCS_LEN;
509
Johannes Berg1f7bba72014-11-06 22:56:36 +0100510 /* ensure hdr->frame_control and vendor radiotap data are in skb head */
511 if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800512 dev_kfree_skb(origskb);
513 return NULL;
514 }
515
Grzegorz Bajorski17883042015-12-11 14:39:46 +0100516 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
Johannes Berg1f7bba72014-11-06 22:56:36 +0100517 if (should_drop_frame(origskb, present_fcs_len,
518 rtap_vendor_space)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200519 dev_kfree_skb(origskb);
520 return NULL;
521 }
522
Johannes Berg1f7bba72014-11-06 22:56:36 +0100523 return remove_monitor_info(local, origskb, rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200524 }
525
Helmut Schaa751413e2012-12-05 14:36:12 +0100526 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +0100527 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
528 needed_headroom = rt_hdrlen - rtap_vendor_space;
Helmut Schaa751413e2012-12-05 14:36:12 +0100529
Johannes Berg1f7bba72014-11-06 22:56:36 +0100530 if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200531 /* only need to expand headroom if necessary */
532 skb = origskb;
533 origskb = NULL;
534
535 /*
536 * This shouldn't trigger often because most devices have an
537 * RX header they pull before we get here, and that should
538 * be big enough for our radiotap information. We should
539 * probably export the length to drivers so that we can have
540 * them allocate enough headroom to start with.
541 */
542 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100543 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200544 dev_kfree_skb(skb);
545 return NULL;
546 }
547 } else {
548 /*
549 * Need to make a copy and possibly remove radiotap header
550 * and FCS from the original.
551 */
552 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
553
Johannes Berg1f7bba72014-11-06 22:56:36 +0100554 origskb = remove_monitor_info(local, origskb,
555 rtap_vendor_space);
Johannes Bergb2e77712007-09-26 15:19:39 +0200556
557 if (!skb)
558 return origskb;
559 }
560
Johannes Berg0869aea02009-10-28 10:03:35 +0100561 /* prepend radiotap information */
Johannes Berg1f7bba72014-11-06 22:56:36 +0100562 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
Johannes Bergb2e77712007-09-26 15:19:39 +0200563
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100564 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200565 skb->ip_summed = CHECKSUM_UNNECESSARY;
566 skb->pkt_type = PACKET_OTHERHOST;
567 skb->protocol = htons(ETH_P_802_2);
568
569 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200570 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200571 continue;
572
Aviya Erenfeldd8212182016-08-29 23:25:15 +0300573 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
Michael Wu3d30d942008-01-31 19:48:27 +0100574 continue;
575
Johannes Berg9607e6b2009-12-23 13:15:31 +0100576 if (!ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100577 continue;
578
Johannes Bergb2e77712007-09-26 15:19:39 +0200579 if (prev_dev) {
580 skb2 = skb_clone(skb, GFP_ATOMIC);
581 if (skb2) {
582 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400583 netif_receive_skb(skb2);
Johannes Bergb2e77712007-09-26 15:19:39 +0200584 }
585 }
586
587 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +0200588 ieee80211_rx_stats(sdata->dev, skb->len);
Johannes Bergb2e77712007-09-26 15:19:39 +0200589 }
590
Aviya Erenfeld42bd20d2016-08-29 23:25:16 +0300591 mgmt = (void *)skb->data;
592 if (monitor_sdata &&
593 skb->len >= IEEE80211_MIN_ACTION_SIZE + 1 + VHT_MUMIMO_GROUPS_DATA_LEN &&
594 ieee80211_is_action(mgmt->frame_control) &&
595 mgmt->u.action.category == WLAN_CATEGORY_VHT &&
596 mgmt->u.action.u.vht_group_notif.action_code == WLAN_VHT_ACTION_GROUPID_MGMT &&
597 is_valid_ether_addr(monitor_sdata->u.mntr.mu_follow_addr) &&
598 ether_addr_equal(mgmt->da, monitor_sdata->u.mntr.mu_follow_addr)) {
599 struct sk_buff *mu_skb = skb_copy(skb, GFP_ATOMIC);
600
601 if (mu_skb) {
602 mu_skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
603 skb_queue_tail(&monitor_sdata->skb_queue, mu_skb);
604 ieee80211_queue_work(&local->hw, &monitor_sdata->work);
605 }
606 }
607
Johannes Bergb2e77712007-09-26 15:19:39 +0200608 if (prev_dev) {
609 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400610 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200611 } else
612 dev_kfree_skb(skb);
613
614 return origskb;
615}
616
Johannes Berg5cf121c2008-02-25 16:27:43 +0100617static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200618{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700619 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200620 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200621 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200622
623 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700624 if (ieee80211_is_data_qos(hdr->frame_control)) {
625 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200626 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700627 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200628 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200629 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200630
631 seqno_idx = tid;
632 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200633 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200634 /*
635 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
636 *
637 * Sequence numbers for management frames, QoS data
638 * frames with a broadcast/multicast address in the
639 * Address 1 field, and all non-QoS data frames sent
640 * by QoS STAs are assigned using an additional single
641 * modulo-4096 counter, [...]
642 *
643 * We also use that counter for non-QoS STAs.
644 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100645 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200646 security_idx = 0;
647 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100648 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200649 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200650 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200651
Johannes Berg9e262972011-07-07 18:45:03 +0200652 rx->seqno_idx = seqno_idx;
653 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200654 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
655 * For now, set skb->priority to 0 for other cases. */
656 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100657}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200658
Johannes Bergd1c3a372009-01-07 00:26:10 +0100659/**
660 * DOC: Packet alignment
661 *
662 * Drivers always need to pass packets that are aligned to two-byte boundaries
663 * to the stack.
664 *
665 * Additionally, should, if possible, align the payload data in a way that
666 * guarantees that the contained IP header is aligned to a four-byte
667 * boundary. In the case of regular frames, this simply means aligning the
668 * payload to a four-byte boundary (because either the IP header is directly
669 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100670 * in front of it). If the payload data is not properly aligned and the
671 * architecture doesn't support efficient unaligned operations, mac80211
672 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100673 *
674 * With A-MSDU frames, however, the payload data address must yield two modulo
675 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
676 * push the IP header further back to a multiple of four again. Thankfully, the
677 * specs were sane enough this time around to require padding each A-MSDU
678 * subframe to a length that is a multiple of four.
679 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300680 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100681 * the payload is not supported, the driver is required to move the 802.11
682 * header to be directly in front of the payload in that case.
683 */
684static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100685{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100686#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg441275e2015-11-06 12:34:24 +0100687 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100688#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200689}
690
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200691
Johannes Berg571ecf62007-07-27 15:43:22 +0200692/* rx handlers */
693
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200694static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
695{
696 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
697
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100698 if (is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200699 return 0;
700
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100701 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200702}
703
704
705static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
706{
707 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
708
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100709 if (!is_multicast_ether_addr(hdr->addr1))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200710 return 0;
711
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100712 return ieee80211_is_robust_mgmt_frame(skb);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200713}
714
715
716/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
717static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
718{
719 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
720 struct ieee80211_mmie *mmie;
Jouni Malinen56c52da2015-01-24 19:52:08 +0200721 struct ieee80211_mmie_16 *mmie16;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200722
Johannes Berg1df332e2012-10-26 00:09:11 +0200723 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200724 return -1;
725
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100726 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200727 return -1; /* not a robust management frame */
728
729 mmie = (struct ieee80211_mmie *)
730 (skb->data + skb->len - sizeof(*mmie));
Jouni Malinen56c52da2015-01-24 19:52:08 +0200731 if (mmie->element_id == WLAN_EID_MMIE &&
732 mmie->length == sizeof(*mmie) - 2)
733 return le16_to_cpu(mmie->key_id);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200734
Jouni Malinen56c52da2015-01-24 19:52:08 +0200735 mmie16 = (struct ieee80211_mmie_16 *)
736 (skb->data + skb->len - sizeof(*mmie16));
737 if (skb->len >= 24 + sizeof(*mmie16) &&
738 mmie16->element_id == WLAN_EID_MMIE &&
739 mmie16->length == sizeof(*mmie16) - 2)
740 return le16_to_cpu(mmie16->key_id);
741
742 return -1;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200743}
744
Johannes Berg2c61cf92016-03-17 15:02:55 +0200745static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
746 struct sk_buff *skb)
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200747{
748 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
749 __le16 fc;
750 int hdrlen;
751 u8 keyid;
752
753 fc = hdr->frame_control;
754 hdrlen = ieee80211_hdrlen(fc);
755
756 if (skb->len < hdrlen + cs->hdr_len)
757 return -EINVAL;
758
759 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
760 keyid &= cs->key_idx_mask;
761 keyid >>= cs->key_idx_shift;
762
763 return keyid;
764}
765
Johannes Berg1df332e2012-10-26 00:09:11 +0200766static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100767{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700768 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +0100769 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100770
Harvey Harrisona7767f92008-07-02 16:30:51 -0700771 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700772 if (is_multicast_ether_addr(hdr->addr1)) {
773 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +0200774 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700775 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000776 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700777 return RX_DROP_MONITOR;
778 } else {
779 if (!ieee80211_has_a4(hdr->frame_control))
780 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000781 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700782 return RX_DROP_MONITOR;
783 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100784 }
785
786 /* If there is not an established peer link and this is not a peer link
787 * establisment frame, beacon or probe, drop the frame.
788 */
789
Javier Cardona57cf8042011-05-13 10:45:43 -0700790 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100791 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100792
Harvey Harrisona7767f92008-07-02 16:30:51 -0700793 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100794 return RX_DROP_MONITOR;
795
Harvey Harrisona7767f92008-07-02 16:30:51 -0700796 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700797 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +0200798
799 /* make sure category field is present */
800 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
801 return RX_DROP_MONITOR;
802
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100803 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700804 category = mgmt->u.action.category;
805 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +0200806 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100807 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100808 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100809 }
810
Harvey Harrisona7767f92008-07-02 16:30:51 -0700811 if (ieee80211_is_probe_req(hdr->frame_control) ||
812 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -0700813 ieee80211_is_beacon(hdr->frame_control) ||
814 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -0700815 return RX_CONTINUE;
816
817 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -0700818 }
819
Johannes Berg902acc72008-02-23 15:17:19 +0100820 return RX_CONTINUE;
821}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100822
Johannes Bergfb4ea052016-01-28 16:19:24 +0200823static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
824 int index)
825{
826 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
827 struct sk_buff *tail = skb_peek_tail(frames);
828 struct ieee80211_rx_status *status;
829
Sara Sharon06470f72016-01-28 16:19:25 +0200830 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
831 return true;
832
Johannes Bergfb4ea052016-01-28 16:19:24 +0200833 if (!tail)
834 return false;
835
836 status = IEEE80211_SKB_RXCB(tail);
837 if (status->flag & RX_FLAG_AMSDU_MORE)
838 return false;
839
840 return true;
841}
842
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200843static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100844 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000845 int index,
846 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100847{
Michal Kazior83eb9352014-07-16 12:09:31 +0200848 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
849 struct sk_buff *skb;
Christian Lamparter4cfda472010-12-27 23:21:26 +0100850 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100851
Johannes Bergdd318572010-11-29 11:09:16 +0100852 lockdep_assert_held(&tid_agg_rx->reorder_lock);
853
Michal Kazior83eb9352014-07-16 12:09:31 +0200854 if (skb_queue_empty(skb_list))
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100855 goto no_frame;
856
Johannes Bergfb4ea052016-01-28 16:19:24 +0200857 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Michal Kazior83eb9352014-07-16 12:09:31 +0200858 __skb_queue_purge(skb_list);
859 goto no_frame;
860 }
861
862 /* release frames from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100863 tid_agg_rx->stored_mpdu_num--;
Michal Kazior83eb9352014-07-16 12:09:31 +0200864 while ((skb = __skb_dequeue(skb_list))) {
865 status = IEEE80211_SKB_RXCB(skb);
866 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
867 __skb_queue_tail(frames, skb);
868 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100869
870no_frame:
Sara Sharon06470f72016-01-28 16:19:25 +0200871 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
Johannes Berg9a886582013-02-15 19:25:00 +0100872 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100873}
874
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200875static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100876 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000877 u16 head_seq_num,
878 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100879{
880 int index;
881
Johannes Bergdd318572010-11-29 11:09:16 +0100882 lockdep_assert_held(&tid_agg_rx->reorder_lock);
883
Johannes Berg9a886582013-02-15 19:25:00 +0100884 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
Karl Beldan2e3049b2013-10-24 15:53:32 +0200885 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000886 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
887 frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100888 }
889}
890
891/*
892 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
893 * the skb was added to the buffer longer than this time ago, the earlier
894 * frames that have not yet been received are assumed to be lost and the skb
895 * can be released for processing. This may also release other skb's from the
896 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200897 *
898 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100899 */
900#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
901
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200902static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000903 struct tid_ampdu_rx *tid_agg_rx,
904 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200905{
Michal Kazior83eb9352014-07-16 12:09:31 +0200906 int index, i, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200907
Johannes Bergdd318572010-11-29 11:09:16 +0100908 lockdep_assert_held(&tid_agg_rx->reorder_lock);
909
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200910 /* release the buffer until next missing frame */
Karl Beldan2e3049b2013-10-24 15:53:32 +0200911 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Johannes Bergfb4ea052016-01-28 16:19:24 +0200912 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
Eliad Peller07ae2df2012-02-01 18:48:09 +0200913 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200914 /*
915 * No buffers ready to be released, but check whether any
916 * frames in the reorder buffer have timed out.
917 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200918 int skipped = 1;
919 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
920 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +0200921 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200922 skipped++;
923 continue;
924 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -0700925 if (skipped &&
926 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200927 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200928 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200929
Michal Kazior83eb9352014-07-16 12:09:31 +0200930 /* don't leave incomplete A-MSDUs around */
931 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
932 i = (i + 1) % tid_agg_rx->buf_size)
933 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
934
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200935 ht_dbg_ratelimited(sdata,
936 "release an RX reorder frame due to timeout on earlier frames\n");
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000937 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
938 frames);
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200939
940 /*
941 * Increment the head seq# also for the skipped slots.
942 */
943 tid_agg_rx->head_seq_num =
Johannes Berg9a886582013-02-15 19:25:00 +0100944 (tid_agg_rx->head_seq_num +
945 skipped) & IEEE80211_SN_MASK;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200946 skipped = 0;
947 }
Johannes Bergfb4ea052016-01-28 16:19:24 +0200948 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000949 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
950 frames);
Karl Beldan2e3049b2013-10-24 15:53:32 +0200951 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200952 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200953
954 if (tid_agg_rx->stored_mpdu_num) {
Karl Beldan2e3049b2013-10-24 15:53:32 +0200955 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200956
957 for (; j != (index - 1) % tid_agg_rx->buf_size;
958 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Bergfb4ea052016-01-28 16:19:24 +0200959 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200960 break;
961 }
962
963 set_release_timer:
964
Johannes Berg788211d2015-04-01 14:20:42 +0200965 if (!tid_agg_rx->removed)
966 mod_timer(&tid_agg_rx->reorder_timer,
967 tid_agg_rx->reorder_time[j] + 1 +
968 HT_RX_REORDER_BUF_TIMEOUT);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200969 } else {
970 del_timer(&tid_agg_rx->reorder_timer);
971 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200972}
973
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100974/*
975 * As this function belongs to the RX path it must be under
976 * rcu_read_lock protection. It returns false if the frame
977 * can be processed immediately, true if it was consumed.
978 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200979static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100980 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +0000981 struct sk_buff *skb,
982 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100983{
984 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michal Kazior83eb9352014-07-16 12:09:31 +0200985 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100986 u16 sc = le16_to_cpu(hdr->seq_ctrl);
987 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
988 u16 head_seq_num, buf_size;
989 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200990 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100991
Johannes Bergdd318572010-11-29 11:09:16 +0100992 spin_lock(&tid_agg_rx->reorder_lock);
993
Michal Kazior4549cf22014-09-02 14:05:10 +0200994 /*
995 * Offloaded BA sessions have no known starting sequence number so pick
996 * one from first Rxed frame for this tid after BA was started.
997 */
998 if (unlikely(tid_agg_rx->auto_seq)) {
999 tid_agg_rx->auto_seq = false;
1000 tid_agg_rx->ssn = mpdu_seq_num;
1001 tid_agg_rx->head_seq_num = mpdu_seq_num;
1002 }
1003
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001004 buf_size = tid_agg_rx->buf_size;
1005 head_seq_num = tid_agg_rx->head_seq_num;
1006
1007 /* frame with out of date sequence number */
Johannes Berg9a886582013-02-15 19:25:00 +01001008 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001009 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001010 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001011 }
1012
1013 /*
1014 * If frame the sequence number exceeds our buffering window
1015 * size release some previous frames to make room for this one.
1016 */
Johannes Berg9a886582013-02-15 19:25:00 +01001017 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1018 head_seq_num = ieee80211_sn_inc(
1019 ieee80211_sn_sub(mpdu_seq_num, buf_size));
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001020 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02001021 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001022 head_seq_num, frames);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001023 }
1024
1025 /* Now the new frame is always in the range of the reordering buffer */
1026
Karl Beldan2e3049b2013-10-24 15:53:32 +02001027 index = mpdu_seq_num % tid_agg_rx->buf_size;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001028
1029 /* check if we already stored this frame */
Johannes Bergfb4ea052016-01-28 16:19:24 +02001030 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001031 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001032 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001033 }
1034
1035 /*
1036 * If the current MPDU is in the right order and nothing else
1037 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +01001038 * If it is first but there's something stored, we may be able
1039 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001040 */
1041 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1042 tid_agg_rx->stored_mpdu_num == 0) {
Michal Kazior83eb9352014-07-16 12:09:31 +02001043 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1044 tid_agg_rx->head_seq_num =
1045 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001046 ret = false;
1047 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001048 }
1049
1050 /* put the frame in the reordering buffer */
Michal Kazior83eb9352014-07-16 12:09:31 +02001051 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1052 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1053 tid_agg_rx->reorder_time[index] = jiffies;
1054 tid_agg_rx->stored_mpdu_num++;
1055 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1056 }
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001057
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02001058 out:
1059 spin_unlock(&tid_agg_rx->reorder_lock);
1060 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001061}
1062
1063/*
1064 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1065 * true if the MPDU was buffered, false if it should be processed.
1066 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001067static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1068 struct sk_buff_head *frames)
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001069{
Johannes Berg2569a822009-11-25 17:46:17 +01001070 struct sk_buff *skb = rx->skb;
1071 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001072 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg2569a822009-11-25 17:46:17 +01001073 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001074 struct tid_ampdu_rx *tid_agg_rx;
1075 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001076 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001077
Johannes Berg051a41fa2013-11-20 11:28:27 +01001078 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1079 is_multicast_ether_addr(hdr->addr1))
Johannes Berg2569a822009-11-25 17:46:17 +01001080 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001081
1082 /*
1083 * filter the QoS data rx stream according to
1084 * STA/TID and check if this STA/TID is on aggregation
1085 */
1086
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001087 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +01001088 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001089
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001090 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1091 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001092 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1093
Johannes Berga87f7362010-06-10 10:21:38 +02001094 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1095 if (!tid_agg_rx)
1096 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001097
1098 /* qos null data frames are excluded */
1099 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +02001100 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001101
Thomas Pedersen6cc00d52011-11-03 21:11:11 -07001102 /* not part of a BA session */
1103 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1104 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1105 goto dont_reorder;
1106
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001107 /* new, potentially un-ordered, ampdu frame - process it */
1108
1109 /* reset session timer */
1110 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001111 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001112
1113 /* if this mpdu is fragmented - terminate rx aggregation session */
1114 sc = le16_to_cpu(hdr->seq_ctrl);
1115 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Bergc1475ca2010-06-10 10:21:37 +02001116 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg344eec62010-06-10 10:21:36 +02001117 skb_queue_tail(&rx->sdata->skb_queue, skb);
1118 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +01001119 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001120 }
1121
Johannes Berga87f7362010-06-10 10:21:38 +02001122 /*
1123 * No locking needed -- we will only ever process one
1124 * RX packet at a time, and thus own tid_agg_rx. All
1125 * other code manipulating it needs to (and does) make
1126 * sure that we cannot get to it any more before doing
1127 * anything with it.
1128 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001129 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1130 frames))
Johannes Berg2569a822009-11-25 17:46:17 +01001131 return;
1132
1133 dont_reorder:
Christian Lamparterf9e124f2013-02-04 17:44:44 +00001134 __skb_queue_tail(frames, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +01001135}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001136
Johannes Berg49461622008-06-30 15:10:45 +02001137static ieee80211_rx_result debug_noinline
Johannes Berg03954422014-11-11 16:49:25 +01001138ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001139{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02001141 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001142
Sara Sharonf9cfa5f2015-12-08 16:04:33 +02001143 if (status->flag & RX_FLAG_DUP_VALIDATED)
1144 return RX_CONTINUE;
1145
Johannes Berg6b0f3272013-07-11 22:33:26 +02001146 /*
1147 * Drop duplicate 802.11 retransmissions
1148 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1149 */
Johannes Berg03954422014-11-11 16:49:25 +01001150
1151 if (rx->skb->len < 24)
1152 return RX_CONTINUE;
1153
1154 if (ieee80211_is_ctl(hdr->frame_control) ||
1155 ieee80211_is_qos_nullfunc(hdr->frame_control) ||
1156 is_multicast_ether_addr(hdr->addr1))
1157 return RX_CONTINUE;
1158
Johannes Berga732fa72015-10-14 18:27:07 +02001159 if (!rx->sta)
1160 return RX_CONTINUE;
1161
1162 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1163 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1164 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001165 rx->sta->rx_stats.num_duplicates++;
Johannes Berga732fa72015-10-14 18:27:07 +02001166 return RX_DROP_UNUSABLE;
1167 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1168 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +02001169 }
1170
Johannes Berg03954422014-11-11 16:49:25 +01001171 return RX_CONTINUE;
1172}
1173
1174static ieee80211_rx_result debug_noinline
1175ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1176{
1177 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1178
Johannes Berg571ecf62007-07-27 15:43:22 +02001179 /* Drop disallowed frame classes based on STA auth/assoc state;
1180 * IEEE 802.11, Chap 5.5.
1181 *
Johannes Bergccd7b362008-09-11 00:01:56 +02001182 * mac80211 filters only based on association state, i.e. it drops
1183 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +02001184 * deauth/disassoc frames when needed. In addition, hostapd is
1185 * responsible for filtering on both auth and assoc states.
1186 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001187
Johannes Berg902acc72008-02-23 15:17:19 +01001188 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001189 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001190
Harvey Harrisona7767f92008-07-02 16:30:51 -07001191 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1192 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001193 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Bill Jordan1be7fe82010-10-01 11:20:41 -04001194 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001195 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001196 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +01001197 /*
1198 * accept port control frames from the AP even when it's not
1199 * yet marked ASSOC to prevent a race where we don't set the
1200 * assoc bit quickly enough before it sends the first frame
1201 */
1202 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +03001203 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001204 unsigned int hdrlen;
1205 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001206
Johannes Berg6dbda2d2012-10-26 00:41:23 +02001207 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1208
1209 if (rx->skb->len < hdrlen + 8)
1210 return RX_DROP_MONITOR;
1211
1212 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1213 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +03001214 return RX_CONTINUE;
1215 }
Johannes Berg21fc7562011-11-04 11:18:13 +01001216
1217 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1218 cfg80211_rx_spurious_frame(rx->sdata->dev,
1219 hdr->addr2,
1220 GFP_ATOMIC))
1221 return RX_DROP_UNUSABLE;
1222
Johannes Berge4c26ad2008-01-31 19:48:21 +01001223 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +03001224 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001225
Johannes Berg9ae54c82008-01-31 19:48:20 +01001226 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +02001227}
1228
1229
Johannes Berg49461622008-06-30 15:10:45 +02001230static ieee80211_rx_result debug_noinline
Kalle Valo572e0012009-02-10 17:09:31 +02001231ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1232{
1233 struct ieee80211_local *local;
1234 struct ieee80211_hdr *hdr;
1235 struct sk_buff *skb;
1236
1237 local = rx->local;
1238 skb = rx->skb;
1239 hdr = (struct ieee80211_hdr *) skb->data;
1240
1241 if (!local->pspolling)
1242 return RX_CONTINUE;
1243
1244 if (!ieee80211_has_fromds(hdr->frame_control))
1245 /* this is not from AP */
1246 return RX_CONTINUE;
1247
1248 if (!ieee80211_is_data(hdr->frame_control))
1249 return RX_CONTINUE;
1250
1251 if (!ieee80211_has_moredata(hdr->frame_control)) {
1252 /* AP has no more frames buffered for us */
1253 local->pspolling = false;
1254 return RX_CONTINUE;
1255 }
1256
1257 /* more data bit is set, let's request a new frame from the AP */
1258 ieee80211_send_pspoll(local, rx->sdata);
1259
1260 return RX_CONTINUE;
1261}
1262
Marco Porschd012a602012-10-10 12:39:50 -07001263static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001264{
Johannes Berg133b8222008-09-16 14:18:59 +02001265 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001266 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001267 struct ps_data *ps;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001268 int tid;
Joe Perches0795af52007-10-03 17:59:30 -07001269
Marco Porschd012a602012-10-10 12:39:50 -07001270 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1271 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1272 ps = &sdata->bss->ps;
1273 else
1274 return;
1275
1276 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001277 set_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berg30686bf2015-06-02 21:39:54 +02001278 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001279 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001280 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1281 sta->sta.addr, sta->sta.aid);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001282
Johannes Berg17c18bf2015-03-21 15:25:43 +01001283 ieee80211_clear_fast_xmit(sta);
1284
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001285 if (!sta->sta.txq[0])
1286 return;
1287
1288 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1289 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1290
Felix Fietkau4e3f21b2016-07-11 15:09:15 +02001291 if (txqi->tin.backlog_packets)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001292 set_bit(tid, &sta->txq_buffered_tids);
1293 else
1294 clear_bit(tid, &sta->txq_buffered_tids);
1295 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001296}
1297
Marco Porschd012a602012-10-10 12:39:50 -07001298static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001299{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001300 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1301 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001302
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001303 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berge3685e02014-02-20 11:19:58 +01001304 /*
1305 * Clear the flag only if the other one is still set
1306 * so that the TX path won't start TX'ing new frames
1307 * directly ... In the case that the driver flag isn't
1308 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1309 */
1310 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001311 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1312 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001313 return;
1314 }
1315
Johannes Berg5ac2e352014-05-27 16:32:27 +02001316 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1317 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Bergaf818582009-11-06 11:35:50 +01001318 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001319}
1320
Johannes Bergcf471612015-06-16 16:16:38 +02001321int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001322{
Johannes Bergcf471612015-06-16 16:16:38 +02001323 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001324 bool in_ps;
1325
Johannes Bergcf471612015-06-16 16:16:38 +02001326 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001327
1328 /* Don't let the same PS state be set twice */
Johannes Bergcf471612015-06-16 16:16:38 +02001329 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001330 if ((start && in_ps) || (!start && !in_ps))
1331 return -EINVAL;
1332
1333 if (start)
Johannes Bergcf471612015-06-16 16:16:38 +02001334 sta_ps_start(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001335 else
Johannes Bergcf471612015-06-16 16:16:38 +02001336 sta_ps_end(sta);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001337
1338 return 0;
1339}
1340EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1341
Johannes Berg46fa38e2016-05-03 16:58:00 +03001342void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1343{
1344 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1345
1346 if (test_sta_flag(sta, WLAN_STA_SP))
1347 return;
1348
1349 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1350 ieee80211_sta_ps_deliver_poll_response(sta);
1351 else
1352 set_sta_flag(sta, WLAN_STA_PSPOLL);
1353}
1354EXPORT_SYMBOL(ieee80211_sta_pspoll);
1355
1356void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1357{
1358 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1359 u8 ac = ieee802_1d_to_ac[tid & 7];
1360
1361 /*
1362 * If this AC is not trigger-enabled do nothing.
1363 *
1364 * NB: This could/should check a separate bitmap of trigger-
1365 * enabled queues, but for now we only implement uAPSD w/o
1366 * TSPEC changes to the ACs, so they're always the same.
1367 */
1368 if (!(sta->sta.uapsd_queues & BIT(ac)))
1369 return;
1370
1371 /* if we are in a service period, do nothing */
1372 if (test_sta_flag(sta, WLAN_STA_SP))
1373 return;
1374
1375 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1376 ieee80211_sta_ps_deliver_uapsd(sta);
1377 else
1378 set_sta_flag(sta, WLAN_STA_UAPSD);
1379}
1380EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1381
Johannes Berg49461622008-06-30 15:10:45 +02001382static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001383ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1384{
1385 struct ieee80211_sub_if_data *sdata = rx->sdata;
1386 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1387 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg47086fc2011-09-29 16:04:33 +02001388
Johannes Berg5c900672015-04-22 14:48:34 +02001389 if (!rx->sta)
Johannes Berg47086fc2011-09-29 16:04:33 +02001390 return RX_CONTINUE;
1391
1392 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1393 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1394 return RX_CONTINUE;
1395
1396 /*
1397 * The device handles station powersave, so don't do anything about
1398 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1399 * it to mac80211 since they're handled.)
1400 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001401 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
Johannes Berg47086fc2011-09-29 16:04:33 +02001402 return RX_CONTINUE;
1403
1404 /*
1405 * Don't do anything if the station isn't already asleep. In
1406 * the uAPSD case, the station will probably be marked asleep,
1407 * in the PS-Poll case the station must be confused ...
1408 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001409 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001410 return RX_CONTINUE;
1411
1412 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001413 ieee80211_sta_pspoll(&rx->sta->sta);
Johannes Berg47086fc2011-09-29 16:04:33 +02001414
1415 /* Free PS Poll skb here instead of returning RX_DROP that would
1416 * count as an dropped frame. */
1417 dev_kfree_skb(rx->skb);
1418
1419 return RX_QUEUED;
1420 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1421 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1422 ieee80211_has_pm(hdr->frame_control) &&
1423 (ieee80211_is_data_qos(hdr->frame_control) ||
1424 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
Johannes Berg46fa38e2016-05-03 16:58:00 +03001425 u8 tid;
1426
Johannes Berg47086fc2011-09-29 16:04:33 +02001427 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg47086fc2011-09-29 16:04:33 +02001428
Johannes Berg46fa38e2016-05-03 16:58:00 +03001429 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
Johannes Berg47086fc2011-09-29 16:04:33 +02001430 }
1431
1432 return RX_CONTINUE;
1433}
1434
1435static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001436ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001437{
1438 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001439 struct sk_buff *skb = rx->skb;
1440 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1441 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001442 int i;
Johannes Berg571ecf62007-07-27 15:43:22 +02001443
1444 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001445 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001446
Johannes Bergb291ba12009-07-10 15:29:03 +02001447 /*
1448 * Update last_rx only for IBSS packets which are for the current
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001449 * BSSID and for station already AUTHORIZED to avoid keeping the
1450 * current IBSS network alive in cases where other STAs start
1451 * using different BSSID. This will also give the station another
1452 * chance to restart the authentication/authorization in case
1453 * something went wrong the first time.
Johannes Bergb291ba12009-07-10 15:29:03 +02001454 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001455 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001456 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001457 NL80211_IFTYPE_ADHOC);
Antonio Quartullie584da5e2012-11-25 23:13:42 +01001458 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1459 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001460 sta->rx_stats.last_rx = jiffies;
Henning Roggef4ebddf2014-05-01 10:03:46 +02001461 if (ieee80211_is_data(hdr->frame_control) &&
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001462 !is_multicast_ether_addr(hdr->addr1))
1463 sta->rx_stats.last_rate =
1464 sta_stats_encode_rate(status);
Felix Fietkau3af63342011-02-27 22:08:01 +01001465 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01001466 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001467 sta->rx_stats.last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +02001468 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1469 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001470 * Mesh beacons will update last_rx when if they are found to
1471 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001472 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001473 sta->rx_stats.last_rx = jiffies;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001474 if (ieee80211_is_data(hdr->frame_control))
1475 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
Johannes Berg571ecf62007-07-27 15:43:22 +02001476 }
1477
Kalle Valo3cf335d2009-03-22 21:57:06 +02001478 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1479 ieee80211_sta_rx_notify(rx->sdata, hdr);
1480
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001481 sta->rx_stats.fragments++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001482
1483 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001484 sta->rx_stats.bytes += rx->skb->len;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001485 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1486
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001487 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001488 sta->rx_stats.last_signal = status->signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001489 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001490 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001491
Felix Fietkauef0621e2013-04-22 16:29:31 +02001492 if (status->chains) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001493 sta->rx_stats.chains = status->chains;
Felix Fietkauef0621e2013-04-22 16:29:31 +02001494 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1495 int signal = status->chain_signal[i];
1496
1497 if (!(status->chains & BIT(i)))
1498 continue;
1499
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001500 sta->rx_stats.chain_signal_last[i] = signal;
Johannes Berg0be6ed12016-03-31 20:02:05 +03001501 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001502 -signal);
Felix Fietkauef0621e2013-04-22 16:29:31 +02001503 }
1504 }
1505
Johannes Berg72eaa432008-11-26 15:02:58 +01001506 /*
1507 * Change STA power saving mode only at the end of a frame
1508 * exchange sequence.
1509 */
Johannes Berg30686bf2015-06-02 21:39:54 +02001510 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001511 !ieee80211_has_morefrags(hdr->frame_control) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001512 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001513 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
Johannes Bergb4ba5442014-01-24 14:41:44 +01001514 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1515 /* PM bit is only checked in frames where it isn't reserved,
1516 * in AP mode it's reserved in non-bufferable management frames
1517 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1518 */
1519 (!ieee80211_is_mgmt(hdr->frame_control) ||
1520 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001521 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Bergb4ba5442014-01-24 14:41:44 +01001522 if (!ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001523 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001524 } else {
1525 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001526 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001527 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001528 }
1529
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001530 /* mesh power save support */
1531 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1532 ieee80211_mps_rx_h_sta_process(sta, hdr);
1533
Johannes Berg22403de2009-10-30 12:55:03 +01001534 /*
1535 * Drop (qos-)data::nullfunc frames silently, since they
1536 * are used only to control station power saving mode.
1537 */
1538 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1539 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001540 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001541
1542 /*
1543 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001544 * that was not moved to a 4-addr STA vlan yet send
1545 * the event to userspace and for older hostapd drop
1546 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001547 */
1548 if (ieee80211_has_a4(hdr->frame_control) &&
1549 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1550 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001551 !rx->sdata->u.vlan.sta))) {
1552 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1553 cfg80211_rx_unexpected_4addr_frame(
1554 rx->sdata->dev, sta->sta.addr,
1555 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001556 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001557 }
Johannes Berg22403de2009-10-30 12:55:03 +01001558 /*
1559 * Update counter and free packet here to avoid
1560 * counting this as a dropped packed.
1561 */
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001562 sta->rx_stats.packets++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001563 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001564 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001565 }
1566
Johannes Berg9ae54c82008-01-31 19:48:20 +01001567 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001568} /* ieee80211_rx_h_sta_process */
1569
Johan Almbladh86c228a2013-08-14 15:29:46 +02001570static ieee80211_rx_result debug_noinline
1571ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1572{
1573 struct sk_buff *skb = rx->skb;
1574 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1575 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1576 int keyidx;
1577 int hdrlen;
1578 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1579 struct ieee80211_key *sta_ptk = NULL;
1580 int mmie_keyidx = -1;
1581 __le16 fc;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001582 const struct ieee80211_cipher_scheme *cs = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001583
1584 /*
1585 * Key selection 101
1586 *
1587 * There are four types of keys:
1588 * - GTK (group keys)
1589 * - IGTK (group keys for management frames)
1590 * - PTK (pairwise keys)
1591 * - STK (station-to-station pairwise keys)
1592 *
1593 * When selecting a key, we have to distinguish between multicast
1594 * (including broadcast) and unicast frames, the latter can only
1595 * use PTKs and STKs while the former always use GTKs and IGTKs.
1596 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1597 * unicast frames can also use key indices like GTKs. Hence, if we
1598 * don't have a PTK/STK we check the key index for a WEP key.
1599 *
1600 * Note that in a regular BSS, multicast frames are sent by the
1601 * AP only, associated stations unicast the frame to the AP first
1602 * which then multicasts it on their behalf.
1603 *
1604 * There is also a slight problem in IBSS mode: GTKs are negotiated
1605 * with each station, that is something we don't currently handle.
1606 * The spec seems to expect that one negotiates the same key with
1607 * every station but there's no such requirement; VLANs could be
1608 * possible.
1609 */
1610
Johan Almbladh86c228a2013-08-14 15:29:46 +02001611 /* start without a key */
1612 rx->key = NULL;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001613 fc = hdr->frame_control;
1614
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001615 if (rx->sta) {
1616 int keyid = rx->sta->ptk_idx;
1617
1618 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1619 cs = rx->sta->cipher_scheme;
Johannes Berg2c61cf92016-03-17 15:02:55 +02001620 keyid = ieee80211_get_cs_keyid(cs, rx->skb);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001621 if (unlikely(keyid < 0))
1622 return RX_DROP_UNUSABLE;
1623 }
1624 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1625 }
1626
Johan Almbladh86c228a2013-08-14 15:29:46 +02001627 if (!ieee80211_has_protected(fc))
1628 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1629
1630 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1631 rx->key = sta_ptk;
1632 if ((status->flag & RX_FLAG_DECRYPTED) &&
1633 (status->flag & RX_FLAG_IV_STRIPPED))
1634 return RX_CONTINUE;
1635 /* Skip decryption if the frame is not protected. */
1636 if (!ieee80211_has_protected(fc))
1637 return RX_CONTINUE;
1638 } else if (mmie_keyidx >= 0) {
1639 /* Broadcast/multicast robust management frame / BIP */
1640 if ((status->flag & RX_FLAG_DECRYPTED) &&
1641 (status->flag & RX_FLAG_IV_STRIPPED))
1642 return RX_CONTINUE;
1643
1644 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1645 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1646 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Masashi Honma46f6b062016-06-22 19:55:20 +09001647 if (rx->sta) {
1648 if (ieee80211_is_group_privacy_action(skb) &&
1649 test_sta_flag(rx->sta, WLAN_STA_MFP))
1650 return RX_DROP_MONITOR;
1651
Johan Almbladh86c228a2013-08-14 15:29:46 +02001652 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
Masashi Honma46f6b062016-06-22 19:55:20 +09001653 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001654 if (!rx->key)
1655 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1656 } else if (!ieee80211_has_protected(fc)) {
1657 /*
1658 * The frame was not protected, so skip decryption. However, we
1659 * need to set rx->key if there is a key that could have been
1660 * used so that the frame may be dropped if encryption would
1661 * have been expected.
1662 */
1663 struct ieee80211_key *key = NULL;
1664 struct ieee80211_sub_if_data *sdata = rx->sdata;
1665 int i;
1666
1667 if (ieee80211_is_mgmt(fc) &&
1668 is_multicast_ether_addr(hdr->addr1) &&
1669 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1670 rx->key = key;
1671 else {
1672 if (rx->sta) {
1673 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1674 key = rcu_dereference(rx->sta->gtk[i]);
1675 if (key)
1676 break;
1677 }
1678 }
1679 if (!key) {
1680 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1681 key = rcu_dereference(sdata->keys[i]);
1682 if (key)
1683 break;
1684 }
1685 }
1686 if (key)
1687 rx->key = key;
1688 }
1689 return RX_CONTINUE;
1690 } else {
1691 u8 keyid;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001692
Johan Almbladh86c228a2013-08-14 15:29:46 +02001693 /*
1694 * The device doesn't give us the IV so we won't be
1695 * able to look up the key. That's ok though, we
1696 * don't need to decrypt the frame, we just won't
1697 * be able to keep statistics accurate.
1698 * Except for key threshold notifications, should
1699 * we somehow allow the driver to tell us which key
1700 * the hardware used if this flag is set?
1701 */
1702 if ((status->flag & RX_FLAG_DECRYPTED) &&
1703 (status->flag & RX_FLAG_IV_STRIPPED))
1704 return RX_CONTINUE;
1705
1706 hdrlen = ieee80211_hdrlen(fc);
1707
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001708 if (cs) {
Johannes Berg2c61cf92016-03-17 15:02:55 +02001709 keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001710
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001711 if (unlikely(keyidx < 0))
1712 return RX_DROP_UNUSABLE;
1713 } else {
1714 if (rx->skb->len < 8 + hdrlen)
1715 return RX_DROP_UNUSABLE; /* TODO: count this? */
1716 /*
1717 * no need to call ieee80211_wep_get_keyidx,
1718 * it verifies a bunch of things we've done already
1719 */
1720 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1721 keyidx = keyid >> 6;
1722 }
Johan Almbladh86c228a2013-08-14 15:29:46 +02001723
1724 /* check per-station GTK first, if multicast packet */
1725 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1726 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1727
1728 /* if not found, try default key */
1729 if (!rx->key) {
1730 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1731
1732 /*
1733 * RSNA-protected unicast frames should always be
1734 * sent with pairwise or station-to-station keys,
1735 * but for WEP we allow using a key index as well.
1736 */
1737 if (rx->key &&
1738 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1739 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1740 !is_multicast_ether_addr(hdr->addr1))
1741 rx->key = NULL;
1742 }
1743 }
1744
1745 if (rx->key) {
1746 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1747 return RX_DROP_MONITOR;
1748
Johan Almbladh86c228a2013-08-14 15:29:46 +02001749 /* TODO: add threshold stuff again */
1750 } else {
1751 return RX_DROP_MONITOR;
1752 }
1753
1754 switch (rx->key->conf.cipher) {
1755 case WLAN_CIPHER_SUITE_WEP40:
1756 case WLAN_CIPHER_SUITE_WEP104:
1757 result = ieee80211_crypto_wep_decrypt(rx);
1758 break;
1759 case WLAN_CIPHER_SUITE_TKIP:
1760 result = ieee80211_crypto_tkip_decrypt(rx);
1761 break;
1762 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001763 result = ieee80211_crypto_ccmp_decrypt(
1764 rx, IEEE80211_CCMP_MIC_LEN);
1765 break;
1766 case WLAN_CIPHER_SUITE_CCMP_256:
1767 result = ieee80211_crypto_ccmp_decrypt(
1768 rx, IEEE80211_CCMP_256_MIC_LEN);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001769 break;
1770 case WLAN_CIPHER_SUITE_AES_CMAC:
1771 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1772 break;
Jouni Malinen56c52da2015-01-24 19:52:08 +02001773 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1774 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1775 break;
Jouni Malinen8ade5382015-01-24 19:52:09 +02001776 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1777 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1778 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1779 break;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001780 case WLAN_CIPHER_SUITE_GCMP:
1781 case WLAN_CIPHER_SUITE_GCMP_256:
1782 result = ieee80211_crypto_gcmp_decrypt(rx);
1783 break;
Johan Almbladh86c228a2013-08-14 15:29:46 +02001784 default:
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001785 result = ieee80211_crypto_hw_decrypt(rx);
Johan Almbladh86c228a2013-08-14 15:29:46 +02001786 }
1787
1788 /* the hdr variable is invalid after the decrypt handlers */
1789
1790 /* either the frame has been decrypted or will be dropped */
1791 status->flag |= RX_FLAG_DECRYPTED;
1792
1793 return result;
1794}
1795
Johannes Berg571ecf62007-07-27 15:43:22 +02001796static inline struct ieee80211_fragment_entry *
1797ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1798 unsigned int frag, unsigned int seq, int rx_queue,
1799 struct sk_buff **skb)
1800{
1801 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02001802
Johannes Berg571ecf62007-07-27 15:43:22 +02001803 entry = &sdata->fragments[sdata->fragment_next++];
1804 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1805 sdata->fragment_next = 0;
1806
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001807 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02001808 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02001809
1810 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1811 *skb = NULL;
1812 entry->first_frag_time = jiffies;
1813 entry->seq = seq;
1814 entry->rx_queue = rx_queue;
1815 entry->last_frag = frag;
Johannes Berg9acc54b2016-02-26 22:13:40 +01001816 entry->check_sequential_pn = false;
Johannes Berg571ecf62007-07-27 15:43:22 +02001817 entry->extra_len = 0;
1818
1819 return entry;
1820}
1821
1822static inline struct ieee80211_fragment_entry *
1823ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001824 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02001825 int rx_queue, struct ieee80211_hdr *hdr)
1826{
1827 struct ieee80211_fragment_entry *entry;
1828 int i, idx;
1829
1830 idx = sdata->fragment_next;
1831 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1832 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +02001833
1834 idx--;
1835 if (idx < 0)
1836 idx = IEEE80211_FRAGMENT_MAX - 1;
1837
1838 entry = &sdata->fragments[idx];
1839 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1840 entry->rx_queue != rx_queue ||
1841 entry->last_frag + 1 != frag)
1842 continue;
1843
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001844 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001845
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001846 /*
1847 * Check ftype and addresses are equal, else check next fragment
1848 */
1849 if (((hdr->frame_control ^ f_hdr->frame_control) &
1850 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001851 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1852 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02001853 continue;
1854
S.Çağlar Onurab466232008-02-14 17:36:47 +02001855 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001856 __skb_queue_purge(&entry->skb_list);
1857 continue;
1858 }
1859 return entry;
1860 }
1861
1862 return NULL;
1863}
1864
Johannes Berg49461622008-06-30 15:10:45 +02001865static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001866ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001867{
1868 struct ieee80211_hdr *hdr;
1869 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001870 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001871 unsigned int frag, seq;
1872 struct ieee80211_fragment_entry *entry;
1873 struct sk_buff *skb;
Johannes Berg554891e2010-09-24 12:38:25 +02001874 struct ieee80211_rx_status *status;
Johannes Berg571ecf62007-07-27 15:43:22 +02001875
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001876 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001877 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07001878
1879 if (ieee80211_is_ctl(fc))
1880 return RX_CONTINUE;
1881
Johannes Berg571ecf62007-07-27 15:43:22 +02001882 sc = le16_to_cpu(hdr->seq_ctrl);
1883 frag = sc & IEEE80211_SCTL_FRAG;
1884
Johannes Bergb8fff402014-11-03 13:57:46 +01001885 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergc206ca62015-04-22 20:47:28 +02001886 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
Andreas Müllerd0259332014-12-12 12:11:11 +01001887 goto out_no_led;
Johannes Berg571ecf62007-07-27 15:43:22 +02001888 }
Johannes Bergb8fff402014-11-03 13:57:46 +01001889
Andreas Müllerd0259332014-12-12 12:11:11 +01001890 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1891 goto out;
1892
Johannes Berg571ecf62007-07-27 15:43:22 +02001893 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1894
Zhu Yie3cf8b32010-03-29 17:35:07 +08001895 if (skb_linearize(rx->skb))
1896 return RX_DROP_UNUSABLE;
1897
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07001898 /*
1899 * skb_linearize() might change the skb->data and
1900 * previously cached variables (in this case, hdr) need to
1901 * be refreshed with the new data.
1902 */
1903 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001904 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1905
1906 if (frag == 0) {
1907 /* This is the first fragment of a new frame. */
1908 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02001909 rx->seqno_idx, &(rx->skb));
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001910 if (rx->key &&
1911 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
Johannes Berg9acc54b2016-02-26 22:13:40 +01001912 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
1913 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
1914 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001915 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02001916 int queue = rx->security_idx;
Johannes Berg9acc54b2016-02-26 22:13:40 +01001917
1918 /* Store CCMP/GCMP PN so that we can verify that the
1919 * next fragment has a sequential PN value.
1920 */
1921 entry->check_sequential_pn = true;
Johannes Berg571ecf62007-07-27 15:43:22 +02001922 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07001923 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg4325f6c2013-05-08 13:09:08 +02001924 IEEE80211_CCMP_PN_LEN);
Johannes Berg9acc54b2016-02-26 22:13:40 +01001925 BUILD_BUG_ON(offsetof(struct ieee80211_key,
1926 u.ccmp.rx_pn) !=
1927 offsetof(struct ieee80211_key,
1928 u.gcmp.rx_pn));
1929 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
1930 sizeof(rx->key->u.gcmp.rx_pn[queue]));
1931 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
1932 IEEE80211_GCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02001933 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001934 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001935 }
1936
1937 /* This is a fragment for a frame that should already be pending in
1938 * fragment cache. Add this fragment to the end of the pending entry.
1939 */
Johannes Berg9e262972011-07-07 18:45:03 +02001940 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1941 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001942 if (!entry) {
1943 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001944 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001945 }
1946
Johannes Berg9acc54b2016-02-26 22:13:40 +01001947 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
1948 * MPDU PN values are not incrementing in steps of 1."
1949 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
1950 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
1951 */
1952 if (entry->check_sequential_pn) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001953 int i;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001954 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07001955 int queue;
Johannes Berg9acc54b2016-02-26 22:13:40 +01001956
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001957 if (!rx->key ||
1958 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
Johannes Berg9acc54b2016-02-26 22:13:40 +01001959 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
1960 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
1961 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001962 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001963 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1964 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001965 pn[i]++;
1966 if (pn[i])
1967 break;
1968 }
Johannes Berg9e262972011-07-07 18:45:03 +02001969 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07001970 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Berg4325f6c2013-05-08 13:09:08 +02001971 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001972 return RX_DROP_UNUSABLE;
Johannes Berg4325f6c2013-05-08 13:09:08 +02001973 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
Johannes Berg571ecf62007-07-27 15:43:22 +02001974 }
1975
Harvey Harrison358c8d92008-07-15 18:44:13 -07001976 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02001977 __skb_queue_tail(&entry->skb_list, rx->skb);
1978 entry->last_frag = frag;
1979 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001980 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001981 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001982 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001983 }
1984
1985 rx->skb = __skb_dequeue(&entry->skb_list);
1986 if (skb_tailroom(rx->skb) < entry->extra_len) {
Johannes Bergf1160432015-04-22 20:25:20 +02001987 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
Johannes Berg571ecf62007-07-27 15:43:22 +02001988 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1989 GFP_ATOMIC))) {
1990 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1991 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001992 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001993 }
1994 }
1995 while ((skb = __skb_dequeue(&entry->skb_list))) {
1996 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1997 dev_kfree_skb(skb);
1998 }
1999
2000 /* Complete frame has been reassembled - process it now */
Johannes Berg554891e2010-09-24 12:38:25 +02002001 status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002002
2003 out:
Andreas Müllerd0259332014-12-12 12:11:11 +01002004 ieee80211_led_rx(rx->local);
2005 out_no_led:
Johannes Berg571ecf62007-07-27 15:43:22 +02002006 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002007 rx->sta->rx_stats.packets++;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002008 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02002009}
2010
Johannes Berg1df332e2012-10-26 00:09:11 +02002011static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002012{
Johannes Berg1df332e2012-10-26 00:09:11 +02002013 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002014 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02002015
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002016 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002017}
2018
Johannes Berg1df332e2012-10-26 00:09:11 +02002019static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02002020{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002021 struct sk_buff *skb = rx->skb;
2022 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2023
Johannes Berg3017b802007-08-28 17:01:53 -04002024 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04002025 * Pass through unencrypted frames if the hardware has
2026 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04002027 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002028 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002029 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002030
2031 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002032 if (unlikely(!ieee80211_has_protected(fc) &&
2033 !ieee80211_is_nullfunc(fc) &&
Johannes Berge8f4fb72015-03-20 11:37:36 +01002034 ieee80211_is_data(fc) && rx->key))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002035 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002036
2037 return 0;
2038}
2039
Johannes Berg1df332e2012-10-26 00:09:11 +02002040static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002041{
2042 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07002043 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002044 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002045
Jouni Malinene3efca02010-03-28 22:31:15 -07002046 /*
2047 * Pass through unencrypted frames if the hardware has
2048 * decrypted them already.
2049 */
2050 if (status->flag & RX_FLAG_DECRYPTED)
2051 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01002052
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002053 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07002054 if (unlikely(!ieee80211_has_protected(fc) &&
2055 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002056 rx->key)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002057 if (ieee80211_is_deauth(fc) ||
2058 ieee80211_is_disassoc(fc))
2059 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2060 rx->skb->data,
2061 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002062 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002063 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002064 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08002065 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02002066 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002067 if (ieee80211_is_deauth(fc) ||
2068 ieee80211_is_disassoc(fc))
2069 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2070 rx->skb->data,
2071 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002072 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02002073 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002074 /*
2075 * When using MFP, Action frames are not allowed prior to
2076 * having configured keys.
2077 */
2078 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +01002079 ieee80211_is_robust_mgmt_frame(rx->skb)))
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03002080 return -EACCES;
2081 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02002082
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002083 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002084}
2085
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002086static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02002087__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02002088{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002089 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002090 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002091 bool check_port_control = false;
2092 struct ethhdr *ehdr;
2093 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002094
Felix Fietkau4114fa22011-04-12 19:15:22 +02002095 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002096 if (ieee80211_has_a4(hdr->frame_control) &&
2097 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002098 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002099
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002100 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2101 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2102
2103 if (!sdata->u.mgd.use_4addr)
2104 return -1;
2105 else
2106 check_port_control = true;
2107 }
2108
Johannes Berg9bc383d2009-11-19 11:55:19 +01002109 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002110 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002111 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02002112
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002113 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02002114 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002115 return ret;
2116
2117 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002118 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2119 *port_control = true;
2120 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01002121 return -1;
2122
2123 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002124}
Johannes Berg571ecf62007-07-27 15:43:22 +02002125
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002126/*
2127 * requires that rx->skb is a frame with ethernet header
2128 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07002129static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002130{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05302131 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002132 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2133 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2134
2135 /*
2136 * Allow EAPOL frames to us/the PAE group address regardless
2137 * of whether the frame was encrypted or not.
2138 */
Johannes Berga621fa42010-08-27 14:26:54 +03002139 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00002140 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2141 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002142 return true;
2143
2144 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07002145 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002146 return false;
2147
2148 return true;
2149}
2150
2151/*
2152 * requires that rx->skb is a frame with ethernet header
2153 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002154static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01002155ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002156{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002157 struct ieee80211_sub_if_data *sdata = rx->sdata;
2158 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002159 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002160 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2161 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02002162
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002163 skb = rx->skb;
2164 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02002165
Johannes Berg5a490512015-04-22 17:10:38 +02002166 ieee80211_rx_stats(dev, skb->len);
2167
Johannes Bergde8f18d2016-03-31 20:02:03 +03002168 if (rx->sta) {
2169 /* The seqno index has the same property as needed
2170 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2171 * for non-QoS-data frames. Here we know it's a data
2172 * frame, so count MSDUs.
2173 */
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002174 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002175 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002176 u64_stats_update_end(&rx->sta->rx_stats.syncp);
Johannes Bergde8f18d2016-03-31 20:02:03 +03002177 }
2178
Johannes Berg05c914f2008-09-11 00:01:58 +02002179 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2180 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02002181 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01002182 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002183 if (is_multicast_ether_addr(ehdr->h_dest)) {
2184 /*
2185 * send multicast frames both to higher layers in
2186 * local net stack and back to the wireless medium
2187 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002188 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00002189 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002190 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002191 dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02002192 } else {
Johannes Bergabe60632009-11-25 17:46:18 +01002193 dsta = sta_info_get(sdata, skb->data);
2194 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002195 /*
2196 * The destination station is associated to
2197 * this AP (in this VLAN), so send the frame
2198 * directly to it and do not pass it to local
2199 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02002200 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002201 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02002202 skb = NULL;
2203 }
Johannes Berg571ecf62007-07-27 15:43:22 +02002204 }
2205 }
2206
Kalle Valo59d9cb02009-12-17 13:54:57 +01002207#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002208 if (skb) {
2209 /* 'align' will only take the values 0 or 2 here since all
2210 * frames are required to be aligned to 2-byte boundaries
2211 * when being passed to mac80211; the code here works just
2212 * as well if that isn't true, but mac80211 assumes it can
2213 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
Johannes Bergd1c3a372009-01-07 00:26:10 +01002214 */
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002215 int align;
2216
2217 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01002218 if (align) {
2219 if (WARN_ON(skb_headroom(skb) < 3)) {
2220 dev_kfree_skb(skb);
2221 skb = NULL;
2222 } else {
2223 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07002224 size_t len = skb_headlen(skb);
2225 skb->data -= align;
2226 memmove(skb->data, data, len);
2227 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002228 }
2229 }
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002230 }
Johannes Bergd1c3a372009-01-07 00:26:10 +01002231#endif
2232
Johannes Berg9e890a1f2013-12-04 09:16:31 +01002233 if (skb) {
2234 /* deliver to local stack */
2235 skb->protocol = eth_type_trans(skb, dev);
2236 memset(skb->cb, 0, sizeof(skb->cb));
Johannes Bergaf9f9b22015-06-11 16:02:32 +02002237 if (rx->napi)
2238 napi_gro_receive(rx->napi, skb);
Johannes Berg06d181a2014-02-04 20:51:09 +01002239 else
2240 netif_receive_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002241 }
2242
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002243 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01002244 /*
2245 * Send to wireless media and increase priority by 256 to
2246 * keep the received priority instead of reclassifying
2247 * the frame (see cfg80211_classify8021d).
2248 */
2249 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09002250 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002251 skb_reset_network_header(xmit_skb);
2252 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002253 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002254 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002255}
2256
Johannes Berg49461622008-06-30 15:10:45 +02002257static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002258ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002259{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002260 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002261 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002262 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2263 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002264 struct sk_buff_head frame_list;
Johannes Berg554891e2010-09-24 12:38:25 +02002265 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002266
Harvey Harrison358c8d92008-07-15 18:44:13 -07002267 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002268 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002269
Harvey Harrison358c8d92008-07-15 18:44:13 -07002270 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002271 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002272
Johannes Berg554891e2010-09-24 12:38:25 +02002273 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002274 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002275
Zhu Yieaf85ca2009-12-01 10:18:37 +08002276 if (ieee80211_has_a4(hdr->frame_control) &&
2277 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2278 !rx->sdata->u.vlan.sta)
2279 return RX_DROP_UNUSABLE;
2280
2281 if (is_multicast_ether_addr(hdr->addr1) &&
2282 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2283 rx->sdata->u.vlan.sta) ||
2284 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2285 rx->sdata->u.mgd.use_4addr)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002286 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002287
2288 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08002289 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002290
Zhu Yieaf85ca2009-12-01 10:18:37 +08002291 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2292 rx->sdata->vif.type,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -07002293 rx->local->hw.extra_tx_headroom, true);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002294
Zhu Yieaf85ca2009-12-01 10:18:37 +08002295 while (!skb_queue_empty(&frame_list)) {
2296 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002297
Harvey Harrison358c8d92008-07-15 18:44:13 -07002298 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08002299 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002300 continue;
2301 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002302
2303 ieee80211_deliver_skb(rx);
2304 }
2305
Johannes Berg9ae54c82008-01-31 19:48:20 +01002306 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02002307}
2308
Ingo Molnarbf94e172008-10-12 23:51:38 -07002309#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02002310static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002311ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2312{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002313 struct ieee80211_hdr *fwd_hdr, *hdr;
2314 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002315 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002316 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002317 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002318 struct ieee80211_sub_if_data *sdata = rx->sdata;
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002319 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Michal Kaziorcf440122016-01-25 14:43:24 +01002320 u16 ac, q, hdrlen;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002321
2322 hdr = (struct ieee80211_hdr *) skb->data;
2323 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02002324
2325 /* make sure fixed part of mesh header is there, also checks skb len */
2326 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2327 return RX_DROP_MONITOR;
2328
2329 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2330
2331 /* make sure full mesh header is there, also checks skb len */
2332 if (!pskb_may_pull(rx->skb,
2333 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2334 return RX_DROP_MONITOR;
2335
2336 /* reload pointers */
2337 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002338 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2339
Bob Copelandd0c22112015-03-02 14:28:52 -05002340 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2341 return RX_DROP_MONITOR;
2342
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002343 /* frame is in RMC, don't forward */
2344 if (ieee80211_is_data(hdr->frame_control) &&
2345 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergbf7cd942013-02-15 14:40:31 +01002346 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07002347 return RX_DROP_MONITOR;
2348
Johannes Berg5c900672015-04-22 14:48:34 +02002349 if (!ieee80211_is_data(hdr->frame_control))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002350 return RX_CONTINUE;
2351
2352 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002353 return RX_DROP_MONITOR;
2354
Javier Cardona43b7b312009-10-15 18:10:51 -07002355 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08002356 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07002357 char *proxied_addr;
2358 char *mpp_addr;
2359
2360 if (is_multicast_ether_addr(hdr->addr1)) {
2361 mpp_addr = hdr->addr3;
2362 proxied_addr = mesh_hdr->eaddr1;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002363 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2364 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07002365 mpp_addr = hdr->addr4;
2366 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02002367 } else {
2368 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07002369 }
YanBo79617de2008-09-22 13:30:32 +08002370
YanBo79617de2008-09-22 13:30:32 +08002371 rcu_read_lock();
Johannes Bergbf7cd942013-02-15 14:40:31 +01002372 mppath = mpp_path_lookup(sdata, proxied_addr);
YanBo79617de2008-09-22 13:30:32 +08002373 if (!mppath) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002374 mpp_path_add(sdata, proxied_addr, mpp_addr);
YanBo79617de2008-09-22 13:30:32 +08002375 } else {
2376 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002377 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002378 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
Henning Roggeab1c7902016-02-03 13:58:37 +01002379 mppath->exp_time = jiffies;
YanBo79617de2008-09-22 13:30:32 +08002380 spin_unlock_bh(&mppath->state_lock);
2381 }
2382 rcu_read_unlock();
2383 }
2384
Javier Cardona3c5772a2009-08-10 12:15:48 -07002385 /* Frame has reached destination. Don't forward */
2386 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002387 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002388 return RX_CONTINUE;
2389
Michal Kaziorcf440122016-01-25 14:43:24 +01002390 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2391 q = sdata->vif.hw_queue[ac];
Thomas Pedersend3c15972011-11-24 17:15:23 -08002392 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002393 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002394 return RX_DROP_MONITOR;
2395 }
2396 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002397
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002398 if (!--mesh_hdr->ttl) {
2399 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002400 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002401 }
2402
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002403 if (!ifmsh->mshcfg.dot11MeshForwarding)
2404 goto out;
2405
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002406 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2407 if (!fwd_skb) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002408 net_info_ratelimited("%s: failed to clone mesh frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002409 sdata->name);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002410 goto out;
2411 }
2412
2413 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Thomas Pedersen9d6d6f42013-04-08 11:06:12 -07002414 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002415 info = IEEE80211_SKB_CB(fwd_skb);
2416 memset(info, 0, sizeof(*info));
2417 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2418 info->control.vif = &rx->sdata->vif;
2419 info->control.jiffies = jiffies;
2420 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2421 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2422 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002423 /* update power mode indication when forwarding */
2424 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002425 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002426 /* mesh power mode flags updated in mesh_nexthop_lookup */
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002427 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2428 } else {
2429 /* unable to resolve next hop */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002430 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
Chun-Yeow Yeohf63f8422013-11-13 15:39:12 +08002431 fwd_hdr->addr3, 0,
2432 WLAN_REASON_MESH_PATH_NOFORWARD,
2433 fwd_hdr->addr2);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002434 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002435 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002436 return RX_DROP_MONITOR;
2437 }
2438
2439 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2440 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002441 out:
Johannes Bergdf140462015-04-22 14:40:58 +02002442 if (is_multicast_ether_addr(hdr->addr1))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002443 return RX_CONTINUE;
Johannes Bergdf140462015-04-22 14:40:58 +02002444 return RX_DROP_MONITOR;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002445}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002446#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002447
2448static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002449ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002450{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002451 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302452 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002453 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2455 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002456 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002457 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002458
Harvey Harrison358c8d92008-07-15 18:44:13 -07002459 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002460 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002461
Harvey Harrison358c8d92008-07-15 18:44:13 -07002462 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002463 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002464
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002465 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002466 * Send unexpected-4addr-frame event to hostapd. For older versions,
2467 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002468 */
2469 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002470 sdata->vif.type == NL80211_IFTYPE_AP) {
2471 if (rx->sta &&
2472 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2473 cfg80211_rx_unexpected_4addr_frame(
2474 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002475 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002476 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002477
Felix Fietkau4114fa22011-04-12 19:15:22 +02002478 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002479 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002480 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002481
Harvey Harrison358c8d92008-07-15 18:44:13 -07002482 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002483 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002484
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002485 /* directly handle TDLS channel switch requests/responses */
2486 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2487 cpu_to_be16(ETH_P_TDLS))) {
2488 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2489
2490 if (pskb_may_pull(rx->skb,
2491 offsetof(struct ieee80211_tdls_data, u)) &&
2492 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2493 tf->category == WLAN_CATEGORY_TDLS &&
2494 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2495 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03002496 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2497 schedule_work(&local->tdls_chsw_work);
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002498 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002499 rx->sta->rx_stats.packets++;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02002500
2501 return RX_QUEUED;
2502 }
2503 }
2504
Felix Fietkau4114fa22011-04-12 19:15:22 +02002505 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2506 unlikely(port_control) && sdata->bss) {
2507 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2508 u.ap);
2509 dev = sdata->dev;
2510 rx->sdata = sdata;
2511 }
2512
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002513 rx->skb->dev = dev;
2514
Johannes Berg602fae42016-03-17 15:02:52 +02002515 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2516 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302517 !is_multicast_ether_addr(
2518 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2519 (!local->scanning &&
Johannes Berg602fae42016-03-17 15:02:52 +02002520 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2521 mod_timer(&local->dynamic_ps_timer, jiffies +
2522 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
Vivek Natarajane15276a2010-02-08 17:47:01 +05302523
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002524 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002525
Johannes Berg9ae54c82008-01-31 19:48:20 +01002526 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002527}
2528
Johannes Berg49461622008-06-30 15:10:45 +02002529static ieee80211_rx_result debug_noinline
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002530ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002531{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002532 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002533 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002534 struct tid_ampdu_rx *tid_agg_rx;
2535 u16 start_seq_num;
2536 u16 tid;
2537
Harvey Harrisona7767f92008-07-02 16:30:51 -07002538 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002539 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002540
Harvey Harrisona7767f92008-07-02 16:30:51 -07002541 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002542 struct {
2543 __le16 control, start_seq_num;
2544 } __packed bar_data;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002545 struct ieee80211_event event = {
2546 .type = BAR_RX_EVENT,
2547 };
Johannes Berg8ae59772010-05-30 14:52:58 +02002548
Ron Rindjunsky71364712007-12-25 17:00:36 +02002549 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002550 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002551
2552 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2553 &bar_data, sizeof(bar_data)))
2554 return RX_DROP_MONITOR;
2555
Johannes Berg8ae59772010-05-30 14:52:58 +02002556 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02002557
2558 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2559 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01002560 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002561
Johannes Berg8ae59772010-05-30 14:52:58 +02002562 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002563 event.u.ba.tid = tid;
2564 event.u.ba.ssn = start_seq_num;
2565 event.u.ba.sta = &rx->sta->sta;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002566
2567 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002568 if (tid_agg_rx->timeout)
2569 mod_timer(&tid_agg_rx->session_timer,
2570 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02002571
Johannes Bergdd318572010-11-29 11:09:16 +01002572 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01002573 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02002574 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
Christian Lamparterf9e124f2013-02-04 17:44:44 +00002575 start_seq_num, frames);
Johannes Bergdd318572010-11-29 11:09:16 +01002576 spin_unlock(&tid_agg_rx->reorder_lock);
2577
Emmanuel Grumbach63822462015-04-20 22:53:37 +03002578 drv_event_callback(rx->local, rx->sdata, &event);
2579
Johannes Berga02ae752009-11-16 12:00:40 +01002580 kfree_skb(skb);
2581 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002582 }
2583
Johannes Berg08daeca2010-05-30 14:53:43 +02002584 /*
2585 * After this point, we only want management frames,
2586 * so we can drop all remaining control frames to
2587 * cooked monitor interfaces.
2588 */
2589 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002590}
2591
Jouni Malinenf4f727a2009-01-10 11:46:53 +02002592static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2593 struct ieee80211_mgmt *mgmt,
2594 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02002595{
2596 struct ieee80211_local *local = sdata->local;
2597 struct sk_buff *skb;
2598 struct ieee80211_mgmt *resp;
2599
Joe Perchesb203ca32012-05-08 18:56:52 +00002600 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02002601 /* Not to own unicast address */
2602 return;
2603 }
2604
Joe Perchesb203ca32012-05-08 18:56:52 +00002605 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2606 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002607 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02002608 return;
2609 }
2610
2611 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2612 /* Too short SA Query request frame */
2613 return;
2614 }
2615
2616 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2617 if (skb == NULL)
2618 return;
2619
2620 skb_reserve(skb, local->hw.extra_tx_headroom);
2621 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2622 memset(resp, 0, 24);
2623 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002624 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002625 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02002626 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2627 IEEE80211_STYPE_ACTION);
2628 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2629 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2630 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2631 memcpy(resp->u.action.u.sa_query.trans_id,
2632 mgmt->u.action.u.sa_query.trans_id,
2633 WLAN_SA_QUERY_TR_ID_LEN);
2634
Johannes Berg62ae67b2009-11-18 18:42:05 +01002635 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02002636}
2637
Johannes Berg49461622008-06-30 15:10:45 +02002638static ieee80211_rx_result debug_noinline
Johannes Berg2e161f72010-08-12 15:38:38 +02002639ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2640{
2641 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002642 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02002643
2644 /*
2645 * From here on, look only at management frames.
2646 * Data and control frames are already handled,
2647 * and unknown (reserved) frames are useless.
2648 */
2649 if (rx->skb->len < 24)
2650 return RX_DROP_MONITOR;
2651
2652 if (!ieee80211_is_mgmt(mgmt->frame_control))
2653 return RX_DROP_MONITOR;
2654
Johannes Bergee971922011-11-04 11:18:18 +01002655 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2656 ieee80211_is_beacon(mgmt->frame_control) &&
2657 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01002658 int sig = 0;
2659
Johannes Berg30686bf2015-06-02 21:39:54 +02002660 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
Johannes Berg804483e2012-03-05 22:18:41 +01002661 sig = status->signal;
2662
Johannes Bergee971922011-11-04 11:18:18 +01002663 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2664 rx->skb->data, rx->skb->len,
Ben Greear37c73b52012-10-26 14:49:25 -07002665 status->freq, sig);
Johannes Bergee971922011-11-04 11:18:18 +01002666 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2667 }
2668
Johannes Berg2e161f72010-08-12 15:38:38 +02002669 if (ieee80211_drop_unencrypted_mgmt(rx))
2670 return RX_DROP_UNUSABLE;
2671
2672 return RX_CONTINUE;
2673}
2674
2675static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02002676ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2677{
2678 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002679 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02002680 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002681 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02002682 int len = rx->skb->len;
2683
2684 if (!ieee80211_is_action(mgmt->frame_control))
2685 return RX_CONTINUE;
2686
Jouni Malinen026331c2010-02-15 12:53:10 +02002687 /* drop too small frames */
2688 if (len < IEEE80211_MIN_ACTION_SIZE)
2689 return RX_DROP_UNUSABLE;
2690
Marco Porsch815b8092012-12-05 15:04:26 -08002691 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002692 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2693 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
Johannes Berg84040802010-02-15 12:46:39 +02002694 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02002695
Johannes Bergde1ede72008-09-09 14:42:50 +02002696 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002697 case WLAN_CATEGORY_HT:
2698 /* reject HT action frames from stations not supporting HT */
2699 if (!rx->sta->sta.ht_cap.ht_supported)
2700 goto invalid;
2701
2702 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2703 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2704 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2705 sdata->vif.type != NL80211_IFTYPE_AP &&
2706 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2707 break;
2708
Johannes Bergec61cd62012-12-28 12:12:10 +01002709 /* verify action & smps_control/chanwidth are present */
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002710 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2711 goto invalid;
2712
2713 switch (mgmt->u.action.u.ht_smps.action) {
2714 case WLAN_HT_ACTION_SMPS: {
2715 struct ieee80211_supported_band *sband;
Johannes Bergaf0ed692013-02-12 14:21:00 +01002716 enum ieee80211_smps_mode smps_mode;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002717
2718 /* convert to HT capability */
2719 switch (mgmt->u.action.u.ht_smps.smps_control) {
2720 case WLAN_HT_SMPS_CONTROL_DISABLED:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002721 smps_mode = IEEE80211_SMPS_OFF;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002722 break;
2723 case WLAN_HT_SMPS_CONTROL_STATIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002724 smps_mode = IEEE80211_SMPS_STATIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002725 break;
2726 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
Johannes Bergaf0ed692013-02-12 14:21:00 +01002727 smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002728 break;
2729 default:
2730 goto invalid;
2731 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002732
2733 /* if no change do nothing */
Johannes Bergaf0ed692013-02-12 14:21:00 +01002734 if (rx->sta->sta.smps_mode == smps_mode)
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002735 goto handled;
Johannes Bergaf0ed692013-02-12 14:21:00 +01002736 rx->sta->sta.smps_mode = smps_mode;
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002737
2738 sband = rx->local->hw.wiphy->bands[status->band];
2739
Johannes Berg64f68e52012-03-28 10:58:37 +02002740 rate_control_rate_update(local, sband, rx->sta,
2741 IEEE80211_RC_SMPS_CHANGED);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002742 goto handled;
2743 }
Johannes Bergec61cd62012-12-28 12:12:10 +01002744 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2745 struct ieee80211_supported_band *sband;
2746 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002747 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01002748
2749 /* If it doesn't support 40 MHz it can't change ... */
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002750 if (!(rx->sta->sta.ht_cap.cap &
2751 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Bergec61cd62012-12-28 12:12:10 +01002752 goto handled;
2753
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002754 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002755 max_bw = IEEE80211_STA_RX_BW_20;
Johannes Bergec61cd62012-12-28 12:12:10 +01002756 else
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002757 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2758
2759 /* set cur_max_bandwidth and recalc sta bw */
2760 rx->sta->cur_max_bandwidth = max_bw;
2761 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002762
2763 if (rx->sta->sta.bandwidth == new_bw)
2764 goto handled;
Johannes Bergec61cd62012-12-28 12:12:10 +01002765
Eliad Peller1c45c5c2014-12-14 11:05:51 +02002766 rx->sta->sta.bandwidth = new_bw;
Johannes Bergec61cd62012-12-28 12:12:10 +01002767 sband = rx->local->hw.wiphy->bands[status->band];
2768
2769 rate_control_rate_update(local, sband, rx->sta,
2770 IEEE80211_RC_BW_CHANGED);
2771 goto handled;
2772 }
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002773 default:
2774 goto invalid;
2775 }
2776
2777 break;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01002778 case WLAN_CATEGORY_PUBLIC:
2779 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2780 goto invalid;
2781 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2782 break;
2783 if (!rx->sta)
2784 break;
2785 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2786 break;
2787 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2788 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2789 break;
2790 if (len < offsetof(struct ieee80211_mgmt,
2791 u.action.u.ext_chan_switch.variable))
2792 goto invalid;
2793 goto queue;
Johannes Berg0af83d32012-12-27 18:55:36 +01002794 case WLAN_CATEGORY_VHT:
2795 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2796 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2797 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2798 sdata->vif.type != NL80211_IFTYPE_AP &&
2799 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2800 break;
2801
2802 /* verify action code is present */
2803 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2804 goto invalid;
2805
2806 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2807 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2808 u8 opmode;
2809
2810 /* verify opmode is present */
2811 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2812 goto invalid;
2813
2814 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2815
2816 ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
Eyal Shapiracf1e05c2015-12-08 16:04:36 +02002817 opmode, status->band);
Johannes Berg0af83d32012-12-27 18:55:36 +01002818 goto handled;
2819 }
Sara Sharon23a1f8d2015-12-08 16:04:31 +02002820 case WLAN_VHT_ACTION_GROUPID_MGMT: {
2821 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
2822 goto invalid;
2823 goto queue;
2824 }
Johannes Berg0af83d32012-12-27 18:55:36 +01002825 default:
2826 break;
2827 }
2828 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002829 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01002830 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07002831 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01002832 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01002833 sdata->vif.type != NL80211_IFTYPE_AP &&
2834 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02002835 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01002836
Jouni Malinen026331c2010-02-15 12:53:10 +02002837 /* verify action_code is present */
2838 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2839 break;
2840
Johannes Bergde1ede72008-09-09 14:42:50 +02002841 switch (mgmt->u.action.u.addba_req.action_code) {
2842 case WLAN_ACTION_ADDBA_REQ:
2843 if (len < (IEEE80211_MIN_ACTION_SIZE +
2844 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002845 goto invalid;
2846 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002847 case WLAN_ACTION_ADDBA_RESP:
2848 if (len < (IEEE80211_MIN_ACTION_SIZE +
2849 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002850 goto invalid;
2851 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002852 case WLAN_ACTION_DELBA:
2853 if (len < (IEEE80211_MIN_ACTION_SIZE +
2854 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002855 goto invalid;
2856 break;
2857 default:
2858 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02002859 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002860
Johannes Berg8b58ff82010-06-10 10:21:51 +02002861 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02002862 case WLAN_CATEGORY_SPECTRUM_MGMT:
Jouni Malinen026331c2010-02-15 12:53:10 +02002863 /* verify action_code is present */
2864 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2865 break;
2866
Johannes Berg39192c02008-09-09 14:49:03 +02002867 switch (mgmt->u.action.u.measurement.action_code) {
2868 case WLAN_ACTION_SPCT_MSR_REQ:
Johannes Berg57fbcce2016-04-12 15:56:15 +02002869 if (status->band != NL80211_BAND_5GHZ)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002870 break;
2871
Johannes Berg39192c02008-09-09 14:49:03 +02002872 if (len < (IEEE80211_MIN_ACTION_SIZE +
2873 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02002874 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002875
Johannes Bergcc32abd2009-05-15 11:52:31 +02002876 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002877 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02002878
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002879 ieee80211_process_measurement_req(sdata, mgmt, len);
2880 goto handled;
2881 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2882 u8 *bssid;
2883 if (len < (IEEE80211_MIN_ACTION_SIZE +
2884 sizeof(mgmt->u.action.u.chan_switch)))
2885 break;
2886
2887 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002888 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2889 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002890 break;
2891
2892 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2893 bssid = sdata->u.mgd.bssid;
2894 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2895 bssid = sdata->u.ibss.bssid;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002896 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2897 bssid = mgmt->sa;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002898 else
2899 break;
2900
2901 if (!ether_addr_equal(mgmt->bssid, bssid))
Johannes Berg84040802010-02-15 12:46:39 +02002902 break;
Sujithc481ec92009-01-06 09:28:37 +05302903
Johannes Berg8b58ff82010-06-10 10:21:51 +02002904 goto queue;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002905 }
Johannes Berg39192c02008-09-09 14:49:03 +02002906 }
2907 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002908 case WLAN_CATEGORY_SA_QUERY:
2909 if (len < (IEEE80211_MIN_ACTION_SIZE +
2910 sizeof(mgmt->u.action.u.sa_query)))
Johannes Berg84040802010-02-15 12:46:39 +02002911 break;
2912
Jouni Malinenfea14732009-01-08 13:32:06 +02002913 switch (mgmt->u.action.u.sa_query.action) {
2914 case WLAN_ACTION_SA_QUERY_REQUEST:
2915 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002916 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002917 ieee80211_process_sa_query_req(sdata, mgmt, len);
Johannes Berg84040802010-02-15 12:46:39 +02002918 goto handled;
Jouni Malinenfea14732009-01-08 13:32:06 +02002919 }
2920 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07002921 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02002922 if (len < (IEEE80211_MIN_ACTION_SIZE +
2923 sizeof(mgmt->u.action.u.self_prot.action_code)))
2924 break;
2925
Thomas Pedersen8db09852011-08-12 20:01:00 -07002926 switch (mgmt->u.action.u.self_prot.action_code) {
2927 case WLAN_SP_MESH_PEERING_OPEN:
2928 case WLAN_SP_MESH_PEERING_CLOSE:
2929 case WLAN_SP_MESH_PEERING_CONFIRM:
2930 if (!ieee80211_vif_is_mesh(&sdata->vif))
2931 goto invalid;
Thomas Pedersena6dad6a2013-03-04 13:06:12 -08002932 if (sdata->u.mesh.user_mpm)
Thomas Pedersen8db09852011-08-12 20:01:00 -07002933 /* userspace handles this frame */
2934 break;
2935 goto queue;
2936 case WLAN_SP_MGK_INFORM:
2937 case WLAN_SP_MGK_ACK:
2938 if (!ieee80211_vif_is_mesh(&sdata->vif))
2939 goto invalid;
2940 break;
2941 }
2942 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07002943 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02002944 if (len < (IEEE80211_MIN_ACTION_SIZE +
2945 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2946 break;
2947
Johannes Berg77a121c2010-06-10 10:21:34 +02002948 if (!ieee80211_vif_is_mesh(&sdata->vif))
2949 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07002950 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02002951 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08002952 break;
2953 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02002954 }
Jouni Malinen026331c2010-02-15 12:53:10 +02002955
Johannes Berg2e161f72010-08-12 15:38:38 +02002956 return RX_CONTINUE;
2957
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002958 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02002959 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f72010-08-12 15:38:38 +02002960 /* will return in the next handlers */
2961 return RX_CONTINUE;
2962
2963 handled:
2964 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002965 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02002966 dev_kfree_skb(rx->skb);
2967 return RX_QUEUED;
2968
2969 queue:
2970 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2971 skb_queue_tail(&sdata->skb_queue, rx->skb);
2972 ieee80211_queue_work(&local->hw, &sdata->work);
2973 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002974 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02002975 return RX_QUEUED;
2976}
2977
2978static ieee80211_rx_result debug_noinline
2979ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2980{
Johannes Berg554891e2010-09-24 12:38:25 +02002981 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01002982 int sig = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +02002983
2984 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02002985 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f72010-08-12 15:38:38 +02002986 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01002987
Jouni Malinen026331c2010-02-15 12:53:10 +02002988 /*
2989 * Getting here means the kernel doesn't know how to handle
2990 * it, but maybe userspace does ... include returned frames
2991 * so userspace can register for those to know whether ones
2992 * it transmitted were processed or returned.
2993 */
Jouni Malinen026331c2010-02-15 12:53:10 +02002994
Johannes Berg30686bf2015-06-02 21:39:54 +02002995 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
Johannes Berg804483e2012-03-05 22:18:41 +01002996 sig = status->signal;
2997
Johannes Berg71bbc992012-06-15 15:30:18 +02002998 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -07002999 rx->skb->data, rx->skb->len, 0)) {
Johannes Berg2e161f72010-08-12 15:38:38 +02003000 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003001 rx->sta->rx_stats.packets++;
Johannes Berg2e161f72010-08-12 15:38:38 +02003002 dev_kfree_skb(rx->skb);
3003 return RX_QUEUED;
3004 }
3005
Johannes Berg2e161f72010-08-12 15:38:38 +02003006 return RX_CONTINUE;
3007}
3008
3009static ieee80211_rx_result debug_noinline
3010ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3011{
3012 struct ieee80211_local *local = rx->local;
3013 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3014 struct sk_buff *nskb;
3015 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02003016 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02003017
3018 if (!ieee80211_is_action(mgmt->frame_control))
3019 return RX_CONTINUE;
3020
3021 /*
3022 * For AP mode, hostapd is responsible for handling any action
3023 * frames that we didn't handle, including returning unknown
3024 * ones. For all other modes we will return them to the sender,
3025 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003026 * 802.11-2012 9.24.4.
Johannes Berg2e161f72010-08-12 15:38:38 +02003027 * Newer versions of hostapd shall also use the management frame
3028 * registration mechanisms, but older ones still use cooked
3029 * monitor interfaces so push all frames there.
3030 */
Johannes Berg554891e2010-09-24 12:38:25 +02003031 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f72010-08-12 15:38:38 +02003032 (sdata->vif.type == NL80211_IFTYPE_AP ||
3033 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3034 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02003035
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02003036 if (is_multicast_ether_addr(mgmt->da))
3037 return RX_DROP_MONITOR;
3038
Johannes Berg84040802010-02-15 12:46:39 +02003039 /* do not return rejected action frames */
3040 if (mgmt->u.action.category & 0x80)
3041 return RX_DROP_UNUSABLE;
3042
3043 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3044 GFP_ATOMIC);
3045 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04003046 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02003047
John W. Linville292b4df2010-06-24 11:13:56 -04003048 nmgmt->u.action.category |= 0x80;
3049 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3050 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02003051
3052 memset(nskb->cb, 0, sizeof(nskb->cb));
3053
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003054 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3055 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3056
3057 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3058 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3059 IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Berg30686bf2015-06-02 21:39:54 +02003060 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg07e5a5f2013-03-07 13:22:05 +01003061 info->hw_queue =
3062 local->hw.offchannel_tx_hw_queue;
3063 }
3064
3065 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3066 status->band);
Johannes Bergde1ede72008-09-09 14:42:50 +02003067 }
Johannes Berg39192c02008-09-09 14:49:03 +02003068 dev_kfree_skb(rx->skb);
3069 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02003070}
3071
3072static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01003073ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02003074{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003075 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02003076 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3077 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02003078
Johannes Berg77a121c2010-06-10 10:21:34 +02003079 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02003080
Johannes Berg77a121c2010-06-10 10:21:34 +02003081 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3082 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003083 sdata->vif.type != NL80211_IFTYPE_OCB &&
Johannes Berg77a121c2010-06-10 10:21:34 +02003084 sdata->vif.type != NL80211_IFTYPE_STATION)
3085 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02003086
Johannes Berg77a121c2010-06-10 10:21:34 +02003087 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003088 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02003089 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3090 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3091 /* process for all: mesh, mlme, ibss */
3092 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003093 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3094 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02003095 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3096 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c31333a2010-11-29 20:53:23 +01003097 if (is_multicast_ether_addr(mgmt->da) &&
3098 !is_broadcast_ether_addr(mgmt->da))
3099 return RX_DROP_MONITOR;
3100
Johannes Berg77a121c2010-06-10 10:21:34 +02003101 /* process only for station */
3102 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3103 return RX_DROP_MONITOR;
3104 break;
3105 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Thomas Pedersen9fb04b52013-02-14 11:20:14 -08003106 /* process only for ibss and mesh */
3107 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3108 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
Johannes Berg77a121c2010-06-10 10:21:34 +02003109 return RX_DROP_MONITOR;
3110 break;
3111 default:
3112 return RX_DROP_MONITOR;
3113 }
Johannes Berg46900292009-02-15 12:44:28 +01003114
Johannes Berg77a121c2010-06-10 10:21:34 +02003115 /* queue up frame and kick off work to process it */
Johannes Bergc1475ca2010-06-10 10:21:37 +02003116 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg77a121c2010-06-10 10:21:34 +02003117 skb_queue_tail(&sdata->skb_queue, rx->skb);
3118 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02003119 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003120 rx->sta->rx_stats.packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02003121
3122 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02003123}
3124
Johannes Berg5f0b7de2009-11-16 13:58:21 +01003125static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3126 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01003127{
3128 struct ieee80211_sub_if_data *sdata;
3129 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01003130 struct sk_buff *skb = rx->skb, *skb2;
3131 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003132 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003133 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01003134
Johannes Berg554891e2010-09-24 12:38:25 +02003135 /*
3136 * If cooked monitor has been processed already, then
3137 * don't do it again. If not, set the flag.
3138 */
3139 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04003140 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02003141 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04003142
Johannes Berg152c4772011-10-21 10:22:22 +02003143 /* If there are no cooked monitor interfaces, just free the SKB */
3144 if (!local->cooked_mntrs)
3145 goto out_free_skb;
3146
Johannes Berg1f7bba72014-11-06 22:56:36 +01003147 /* vendor data is long removed here */
3148 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
Johannes Berg293702a2012-03-02 13:18:19 +01003149 /* room for the radiotap header based on driver features */
Johannes Berg1f7bba72014-11-06 22:56:36 +01003150 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
Johannes Berg293702a2012-03-02 13:18:19 +01003151
3152 if (skb_headroom(skb) < needed_headroom &&
3153 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01003154 goto out_free_skb;
3155
Johannes Berg293702a2012-03-02 13:18:19 +01003156 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02003157 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3158 false);
Michael Wu3d30d942008-01-31 19:48:27 +01003159
Zhang Shengjud57a5442016-03-03 01:16:56 +00003160 skb_reset_mac_header(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01003161 skb->ip_summed = CHECKSUM_UNNECESSARY;
3162 skb->pkt_type = PACKET_OTHERHOST;
3163 skb->protocol = htons(ETH_P_802_2);
3164
3165 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01003166 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01003167 continue;
3168
Johannes Berg05c914f2008-09-11 00:01:58 +02003169 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Aviya Erenfeldd8212182016-08-29 23:25:15 +03003170 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
Michael Wu3d30d942008-01-31 19:48:27 +01003171 continue;
3172
3173 if (prev_dev) {
3174 skb2 = skb_clone(skb, GFP_ATOMIC);
3175 if (skb2) {
3176 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003177 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01003178 }
3179 }
3180
3181 prev_dev = sdata->dev;
Johannes Berg5a490512015-04-22 17:10:38 +02003182 ieee80211_rx_stats(sdata->dev, skb->len);
Michael Wu3d30d942008-01-31 19:48:27 +01003183 }
3184
3185 if (prev_dev) {
3186 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04003187 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02003188 return;
3189 }
Michael Wu3d30d942008-01-31 19:48:27 +01003190
3191 out_free_skb:
3192 dev_kfree_skb(skb);
3193}
3194
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003195static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3196 ieee80211_rx_result res)
3197{
3198 switch (res) {
3199 case RX_DROP_MONITOR:
3200 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3201 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003202 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003203 /* fall through */
3204 case RX_CONTINUE: {
3205 struct ieee80211_rate *rate = NULL;
3206 struct ieee80211_supported_band *sband;
3207 struct ieee80211_rx_status *status;
3208
3209 status = IEEE80211_SKB_RXCB((rx->skb));
3210
3211 sband = rx->local->hw.wiphy->bands[status->band];
Johannes Berg56146182012-11-09 15:07:02 +01003212 if (!(status->flag & RX_FLAG_HT) &&
3213 !(status->flag & RX_FLAG_VHT))
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003214 rate = &sband->bitrates[status->rate_idx];
3215
3216 ieee80211_rx_cooked_monitor(rx, rate);
3217 break;
3218 }
3219 case RX_DROP_UNUSABLE:
3220 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3221 if (rx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02003222 rx->sta->rx_stats.dropped++;
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003223 dev_kfree_skb(rx->skb);
3224 break;
3225 case RX_QUEUED:
3226 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3227 break;
3228 }
3229}
3230
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003231static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3232 struct sk_buff_head *frames)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003233{
3234 ieee80211_rx_result res = RX_DROP_MONITOR;
3235 struct sk_buff *skb;
3236
3237#define CALL_RXH(rxh) \
3238 do { \
3239 res = rxh(rx); \
3240 if (res != RX_CONTINUE) \
3241 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003242 } while (0)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003243
Johannes Berg45ceeee2015-03-16 09:08:20 +01003244 /* Lock here to avoid hitting all of the data used in the RX
3245 * path (e.g. key data, station data, ...) concurrently when
3246 * a frame is released from the reorder buffer due to timeout
3247 * from the timer, potentially concurrently with RX from the
3248 * driver.
3249 */
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003250 spin_lock_bh(&rx->local->rx_path_lock);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003251
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003252 while ((skb = __skb_dequeue(frames))) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003253 /*
3254 * all the other fields are valid across frames
3255 * that belong to an aMPDU since they are on the
3256 * same TID from the same station
3257 */
3258 rx->skb = skb;
3259
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003260 CALL_RXH(ieee80211_rx_h_check_more_data);
3261 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3262 CALL_RXH(ieee80211_rx_h_sta_process);
3263 CALL_RXH(ieee80211_rx_h_decrypt);
3264 CALL_RXH(ieee80211_rx_h_defragment);
3265 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003266 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003267#ifdef CONFIG_MAC80211_MESH
3268 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3269 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3270#endif
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003271 CALL_RXH(ieee80211_rx_h_amsdu);
3272 CALL_RXH(ieee80211_rx_h_data);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003273
3274 /* special treatment -- needs the queue */
3275 res = ieee80211_rx_h_ctrl(rx, frames);
3276 if (res != RX_CONTINUE)
3277 goto rxh_next;
3278
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003279 CALL_RXH(ieee80211_rx_h_mgmt_check);
3280 CALL_RXH(ieee80211_rx_h_action);
3281 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3282 CALL_RXH(ieee80211_rx_h_action_return);
3283 CALL_RXH(ieee80211_rx_h_mgmt);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003284
3285 rxh_next:
3286 ieee80211_rx_handlers_result(rx, res);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003287
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003288#undef CALL_RXH
3289 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01003290
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003291 spin_unlock_bh(&rx->local->rx_path_lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003292}
Johannes Berg571ecf62007-07-27 15:43:22 +02003293
Johannes Berg4406c372010-09-24 11:21:06 +02003294static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01003295{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003296 struct sk_buff_head reorder_release;
Johannes Berg58905292008-01-31 19:48:25 +01003297 ieee80211_rx_result res = RX_DROP_MONITOR;
3298
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003299 __skb_queue_head_init(&reorder_release);
3300
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02003301#define CALL_RXH(rxh) \
3302 do { \
3303 res = rxh(rx); \
3304 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01003305 goto rxh_next; \
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003306 } while (0)
Johannes Berg58905292008-01-31 19:48:25 +01003307
Johannes Berg8ebaa5b2016-03-31 20:02:04 +03003308 CALL_RXH(ieee80211_rx_h_check_dup);
3309 CALL_RXH(ieee80211_rx_h_check);
Johannes Berg2569a822009-11-25 17:46:17 +01003310
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003311 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
Johannes Berg2569a822009-11-25 17:46:17 +01003312
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003313 ieee80211_rx_handlers(rx, &reorder_release);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003314 return;
Johannes Berg49461622008-06-30 15:10:45 +02003315
Johannes Berg2569a822009-11-25 17:46:17 +01003316 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02003317 ieee80211_rx_handlers_result(rx, res);
3318
3319#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01003320}
3321
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003322/*
Johannes Bergdd318572010-11-29 11:09:16 +01003323 * This function makes calls into the RX path, therefore
3324 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003325 */
3326void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3327{
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003328 struct sk_buff_head frames;
Johannes Berg554891e2010-09-24 12:38:25 +02003329 struct ieee80211_rx_data rx = {
3330 .sta = sta,
3331 .sdata = sta->sdata,
3332 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02003333 /* This is OK -- must be QoS data frame */
3334 .security_idx = tid,
3335 .seqno_idx = tid,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02003336 .napi = NULL, /* must be NULL to not have races */
Johannes Berg554891e2010-09-24 12:38:25 +02003337 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003338 struct tid_ampdu_rx *tid_agg_rx;
3339
3340 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3341 if (!tid_agg_rx)
3342 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003343
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003344 __skb_queue_head_init(&frames);
3345
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003346 spin_lock(&tid_agg_rx->reorder_lock);
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003347 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02003348 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003349
Emmanuel Grumbachb497de62015-04-20 22:53:38 +03003350 if (!skb_queue_empty(&frames)) {
3351 struct ieee80211_event event = {
3352 .type = BA_FRAME_TIMEOUT,
3353 .u.ba.tid = tid,
3354 .u.ba.sta = &sta->sta,
3355 };
3356 drv_event_callback(rx.local, rx.sdata, &event);
3357 }
3358
Christian Lamparterf9e124f2013-02-04 17:44:44 +00003359 ieee80211_rx_handlers(&rx, &frames);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02003360}
3361
Sara Sharon06470f72016-01-28 16:19:25 +02003362void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3363 u16 ssn, u64 filtered,
3364 u16 received_mpdus)
3365{
3366 struct sta_info *sta;
3367 struct tid_ampdu_rx *tid_agg_rx;
3368 struct sk_buff_head frames;
3369 struct ieee80211_rx_data rx = {
3370 /* This is OK -- must be QoS data frame */
3371 .security_idx = tid,
3372 .seqno_idx = tid,
3373 };
3374 int i, diff;
3375
3376 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3377 return;
3378
3379 __skb_queue_head_init(&frames);
3380
3381 sta = container_of(pubsta, struct sta_info, sta);
3382
3383 rx.sta = sta;
3384 rx.sdata = sta->sdata;
3385 rx.local = sta->local;
3386
3387 rcu_read_lock();
3388 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3389 if (!tid_agg_rx)
3390 goto out;
3391
3392 spin_lock_bh(&tid_agg_rx->reorder_lock);
3393
3394 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3395 int release;
3396
3397 /* release all frames in the reorder buffer */
3398 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3399 IEEE80211_SN_MODULO;
3400 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3401 release, &frames);
3402 /* update ssn to match received ssn */
3403 tid_agg_rx->head_seq_num = ssn;
3404 } else {
3405 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3406 &frames);
3407 }
3408
3409 /* handle the case that received ssn is behind the mac ssn.
3410 * it can be tid_agg_rx->buf_size behind and still be valid */
3411 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3412 if (diff >= tid_agg_rx->buf_size) {
3413 tid_agg_rx->reorder_buf_filtered = 0;
3414 goto release;
3415 }
3416 filtered = filtered >> diff;
3417 ssn += diff;
3418
3419 /* update bitmap */
3420 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3421 int index = (ssn + i) % tid_agg_rx->buf_size;
3422
3423 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3424 if (filtered & BIT_ULL(i))
3425 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3426 }
3427
3428 /* now process also frames that the filter marking released */
3429 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3430
3431release:
3432 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3433
3434 ieee80211_rx_handlers(&rx, &frames);
3435
3436 out:
3437 rcu_read_unlock();
3438}
3439EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3440
Johannes Berg571ecf62007-07-27 15:43:22 +02003441/* main receive path */
3442
Johannes Berga58fbe12015-04-22 15:08:39 +02003443static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
Johannes Berg23a24de2007-07-27 15:43:22 +02003444{
Johannes Berg20b01f82010-09-24 11:21:05 +02003445 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003446 struct sk_buff *skb = rx->skb;
Johannes Berga58fbe12015-04-22 15:08:39 +02003447 struct ieee80211_hdr *hdr = (void *)skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003448 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3449 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02003450 int multicast = is_multicast_ether_addr(hdr->addr1);
3451
Johannes Berg51fb61e2007-12-19 01:31:27 +01003452 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003453 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01003454 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003455 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003456 if (multicast)
3457 return true;
3458 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003459 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02003460 if (!bssid)
Johannes Berg3c2723f52014-01-07 16:23:24 +01003461 return false;
Felix Fietkau6329b8d2013-09-17 11:15:43 +02003462 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3463 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003464 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003465 if (ieee80211_is_beacon(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003466 return true;
Johannes Berga58fbe12015-04-22 15:08:39 +02003467 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003468 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003469 if (!multicast &&
3470 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003471 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003472 if (!rx->sta) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003473 int rate_idx;
Johannes Berg56146182012-11-09 15:07:02 +01003474 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3475 rate_idx = 0; /* TODO: HT/VHT rates */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003476 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01003477 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01003478 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3479 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003480 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003481 return true;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003482 case NL80211_IFTYPE_OCB:
3483 if (!bssid)
3484 return false;
Bertold Van den Berghcc117292015-08-05 16:02:42 +02003485 if (!ieee80211_is_data_present(hdr->frame_control))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003486 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003487 if (!is_broadcast_ether_addr(bssid))
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003488 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003489 if (!multicast &&
3490 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
Johannes Bergdf140462015-04-22 14:40:58 +02003491 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003492 if (!rx->sta) {
Rostislav Lisovy239281f2014-11-03 10:33:19 +01003493 int rate_idx;
3494 if (status->flag & RX_FLAG_HT)
3495 rate_idx = 0; /* TODO: HT rates */
3496 else
3497 rate_idx = status->rate_idx;
3498 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3499 BIT(rate_idx));
3500 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003501 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02003502 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga58fbe12015-04-22 15:08:39 +02003503 if (multicast)
3504 return true;
3505 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
Johannes Berg05c914f2008-09-11 00:01:58 +02003506 case NL80211_IFTYPE_AP_VLAN:
3507 case NL80211_IFTYPE_AP:
Johannes Berga58fbe12015-04-22 15:08:39 +02003508 if (!bssid)
3509 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3510
3511 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01003512 /*
3513 * Accept public action frames even when the
3514 * BSSID doesn't match, this is used for P2P
3515 * and location updates. Note that mac80211
3516 * itself never looks at these frames.
3517 */
Johannes Berg2b9ccd42013-05-13 16:42:40 +02003518 if (!multicast &&
3519 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003520 return false;
Johannes Bergd48b2962012-07-06 22:19:27 +02003521 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003522 return true;
Johannes Berg5c900672015-04-22 14:48:34 +02003523 return ieee80211_is_beacon(hdr->frame_control);
Johannes Berga58fbe12015-04-22 15:08:39 +02003524 }
3525
3526 if (!ieee80211_has_tods(hdr->frame_control)) {
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003527 /* ignore data frames to TDLS-peers */
3528 if (ieee80211_is_data(hdr->frame_control))
3529 return false;
3530 /* ignore action frames to TDLS-peers */
3531 if (ieee80211_is_action(hdr->frame_control) &&
Jouni Malinen1ec7bae2016-03-01 00:29:00 +02003532 !is_broadcast_ether_addr(bssid) &&
Arik Nemtsovdb8e1732014-07-17 17:14:30 +03003533 !ether_addr_equal(bssid, hdr->addr1))
3534 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02003535 }
Johannes Berga58fbe12015-04-22 15:08:39 +02003536 return true;
Johannes Berg05c914f2008-09-11 00:01:58 +02003537 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07003538 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg3c2723f52014-01-07 16:23:24 +01003539 return false;
Johannes Berga58fbe12015-04-22 15:08:39 +02003540 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
Johannes Bergf142c6b2012-06-18 20:07:15 +02003541 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berga58fbe12015-04-22 15:08:39 +02003542 return ieee80211_is_public_action(hdr, skb->len) ||
3543 ieee80211_is_probe_req(hdr->frame_control) ||
3544 ieee80211_is_probe_resp(hdr->frame_control) ||
3545 ieee80211_is_beacon(hdr->frame_control);
Johannes Berg2ca27bc2010-09-16 14:58:23 +02003546 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02003547 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02003548 }
3549
Johannes Berga58fbe12015-04-22 15:08:39 +02003550 WARN_ON_ONCE(1);
3551 return false;
Johannes Berg23a24de2007-07-27 15:43:22 +02003552}
3553
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003554void ieee80211_check_fast_rx(struct sta_info *sta)
3555{
3556 struct ieee80211_sub_if_data *sdata = sta->sdata;
3557 struct ieee80211_local *local = sdata->local;
3558 struct ieee80211_key *key;
3559 struct ieee80211_fast_rx fastrx = {
3560 .dev = sdata->dev,
3561 .vif_type = sdata->vif.type,
3562 .control_port_protocol = sdata->control_port_protocol,
3563 }, *old, *new = NULL;
3564 bool assign = false;
3565
3566 /* use sparse to check that we don't return without updating */
3567 __acquire(check_fast_rx);
3568
3569 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3570 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3571 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3572 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3573
Johannes Bergc9c59622016-03-31 20:02:11 +03003574 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3575
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003576 /* fast-rx doesn't do reordering */
3577 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3578 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3579 goto clear;
3580
3581 switch (sdata->vif.type) {
3582 case NL80211_IFTYPE_STATION:
3583 /* 4-addr is harder to deal with, later maybe */
3584 if (sdata->u.mgd.use_4addr)
3585 goto clear;
3586 /* software powersave is a huge mess, avoid all of it */
3587 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3588 goto clear;
3589 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3590 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3591 goto clear;
3592 if (sta->sta.tdls) {
3593 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3594 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3595 fastrx.expected_ds_bits = 0;
3596 } else {
3597 fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3598 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3599 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3600 fastrx.expected_ds_bits =
3601 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3602 }
3603 break;
3604 case NL80211_IFTYPE_AP_VLAN:
3605 case NL80211_IFTYPE_AP:
3606 /* parallel-rx requires this, at least with calls to
3607 * ieee80211_sta_ps_transition()
3608 */
3609 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3610 goto clear;
3611 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3612 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3613 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3614
3615 fastrx.internal_forward =
3616 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3617 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3618 !sdata->u.vlan.sta);
3619 break;
3620 default:
3621 goto clear;
3622 }
3623
3624 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3625 goto clear;
3626
3627 rcu_read_lock();
3628 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3629 if (key) {
3630 switch (key->conf.cipher) {
3631 case WLAN_CIPHER_SUITE_TKIP:
3632 /* we don't want to deal with MMIC in fast-rx */
3633 goto clear_rcu;
3634 case WLAN_CIPHER_SUITE_CCMP:
3635 case WLAN_CIPHER_SUITE_CCMP_256:
3636 case WLAN_CIPHER_SUITE_GCMP:
3637 case WLAN_CIPHER_SUITE_GCMP_256:
3638 break;
3639 default:
3640 /* we also don't want to deal with WEP or cipher scheme
3641 * since those require looking up the key idx in the
3642 * frame, rather than assuming the PTK is used
3643 * (we need to revisit this once we implement the real
3644 * PTK index, which is now valid in the spec, but we
3645 * haven't implemented that part yet)
3646 */
3647 goto clear_rcu;
3648 }
3649
3650 fastrx.key = true;
3651 fastrx.icv_len = key->conf.icv_len;
3652 }
3653
3654 assign = true;
3655 clear_rcu:
3656 rcu_read_unlock();
3657 clear:
3658 __release(check_fast_rx);
3659
3660 if (assign)
3661 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3662
3663 spin_lock_bh(&sta->lock);
3664 old = rcu_dereference_protected(sta->fast_rx, true);
3665 rcu_assign_pointer(sta->fast_rx, new);
3666 spin_unlock_bh(&sta->lock);
3667
3668 if (old)
3669 kfree_rcu(old, rcu_head);
3670}
3671
3672void ieee80211_clear_fast_rx(struct sta_info *sta)
3673{
3674 struct ieee80211_fast_rx *old;
3675
3676 spin_lock_bh(&sta->lock);
3677 old = rcu_dereference_protected(sta->fast_rx, true);
3678 RCU_INIT_POINTER(sta->fast_rx, NULL);
3679 spin_unlock_bh(&sta->lock);
3680
3681 if (old)
3682 kfree_rcu(old, rcu_head);
3683}
3684
3685void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3686{
3687 struct ieee80211_local *local = sdata->local;
3688 struct sta_info *sta;
3689
3690 lockdep_assert_held(&local->sta_mtx);
3691
3692 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3693 if (sdata != sta->sdata &&
3694 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3695 continue;
3696 ieee80211_check_fast_rx(sta);
3697 }
3698}
3699
3700void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3701{
3702 struct ieee80211_local *local = sdata->local;
3703
3704 mutex_lock(&local->sta_mtx);
3705 __ieee80211_check_fast_rx_iface(sdata);
3706 mutex_unlock(&local->sta_mtx);
3707}
3708
3709static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3710 struct ieee80211_fast_rx *fast_rx)
3711{
3712 struct sk_buff *skb = rx->skb;
3713 struct ieee80211_hdr *hdr = (void *)skb->data;
3714 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3715 struct sta_info *sta = rx->sta;
3716 int orig_len = skb->len;
3717 int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3718 struct {
3719 u8 snap[sizeof(rfc1042_header)];
3720 __be16 proto;
3721 } *payload __aligned(2);
3722 struct {
3723 u8 da[ETH_ALEN];
3724 u8 sa[ETH_ALEN];
3725 } addrs __aligned(2);
Johannes Bergc9c59622016-03-31 20:02:11 +03003726 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3727
3728 if (fast_rx->uses_rss)
3729 stats = this_cpu_ptr(sta->pcpu_rx_stats);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003730
3731 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3732 * to a common data structure; drivers can implement that per queue
3733 * but we don't have that information in mac80211
3734 */
3735 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3736 return false;
3737
3738#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3739
3740 /* If using encryption, we also need to have:
3741 * - PN_VALIDATED: similar, but the implementation is tricky
3742 * - DECRYPTED: necessary for PN_VALIDATED
3743 */
3744 if (fast_rx->key &&
3745 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
3746 return false;
3747
3748 /* we don't deal with A-MSDU deaggregation here */
3749 if (status->rx_flags & IEEE80211_RX_AMSDU)
3750 return false;
3751
3752 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3753 return false;
3754
3755 if (unlikely(ieee80211_is_frag(hdr)))
3756 return false;
3757
3758 /* Since our interface address cannot be multicast, this
3759 * implicitly also rejects multicast frames without the
3760 * explicit check.
3761 *
3762 * We shouldn't get any *data* frames not addressed to us
3763 * (AP mode will accept multicast *management* frames), but
3764 * punting here will make it go through the full checks in
3765 * ieee80211_accept_frame().
3766 */
3767 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
3768 return false;
3769
3770 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
3771 IEEE80211_FCTL_TODS)) !=
3772 fast_rx->expected_ds_bits)
3773 goto drop;
3774
3775 /* assign the key to drop unencrypted frames (later)
3776 * and strip the IV/MIC if necessary
3777 */
3778 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
3779 /* GCMP header length is the same */
3780 snap_offs += IEEE80211_CCMP_HDR_LEN;
3781 }
3782
3783 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
3784 goto drop;
3785 payload = (void *)(skb->data + snap_offs);
3786
3787 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
3788 return false;
3789
3790 /* Don't handle these here since they require special code.
3791 * Accept AARP and IPX even though they should come with a
3792 * bridge-tunnel header - but if we get them this way then
3793 * there's little point in discarding them.
3794 */
3795 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
3796 payload->proto == fast_rx->control_port_protocol))
3797 return false;
3798
3799 /* after this point, don't punt to the slowpath! */
3800
3801 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
3802 pskb_trim(skb, skb->len - fast_rx->icv_len))
3803 goto drop;
3804
3805 if (unlikely(fast_rx->sta_notify)) {
3806 ieee80211_sta_rx_notify(rx->sdata, hdr);
3807 fast_rx->sta_notify = false;
3808 }
3809
3810 /* statistics part of ieee80211_rx_h_sta_process() */
Johannes Bergc9c59622016-03-31 20:02:11 +03003811 stats->last_rx = jiffies;
3812 stats->last_rate = sta_stats_encode_rate(status);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003813
Johannes Bergc9c59622016-03-31 20:02:11 +03003814 stats->fragments++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003815
3816 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Johannes Bergc9c59622016-03-31 20:02:11 +03003817 stats->last_signal = status->signal;
3818 if (!fast_rx->uses_rss)
3819 ewma_signal_add(&sta->rx_stats_avg.signal,
3820 -status->signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003821 }
3822
3823 if (status->chains) {
3824 int i;
3825
Johannes Bergc9c59622016-03-31 20:02:11 +03003826 stats->chains = status->chains;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003827 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
3828 int signal = status->chain_signal[i];
3829
3830 if (!(status->chains & BIT(i)))
3831 continue;
3832
Johannes Bergc9c59622016-03-31 20:02:11 +03003833 stats->chain_signal_last[i] = signal;
3834 if (!fast_rx->uses_rss)
3835 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
3836 -signal);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003837 }
3838 }
3839 /* end of statistics */
3840
3841 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
3842 goto drop;
3843
3844 /* do the header conversion - first grab the addresses */
3845 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
3846 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
3847 /* remove the SNAP but leave the ethertype */
3848 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
3849 /* push the addresses in front */
3850 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
3851
3852 skb->dev = fast_rx->dev;
3853
3854 ieee80211_rx_stats(fast_rx->dev, skb->len);
3855
3856 /* The seqno index has the same property as needed
3857 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
3858 * for non-QoS-data frames. Here we know it's a data
3859 * frame, so count MSDUs.
3860 */
Johannes Bergc9c59622016-03-31 20:02:11 +03003861 u64_stats_update_begin(&stats->syncp);
3862 stats->msdu[rx->seqno_idx]++;
3863 stats->bytes += orig_len;
3864 u64_stats_update_end(&stats->syncp);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003865
3866 if (fast_rx->internal_forward) {
3867 struct sta_info *dsta = sta_info_get(rx->sdata, skb->data);
3868
3869 if (dsta) {
3870 /*
3871 * Send to wireless media and increase priority by 256
3872 * to keep the received priority instead of
3873 * reclassifying the frame (see cfg80211_classify8021d).
3874 */
3875 skb->priority += 256;
3876 skb->protocol = htons(ETH_P_802_3);
3877 skb_reset_network_header(skb);
3878 skb_reset_mac_header(skb);
3879 dev_queue_xmit(skb);
3880 return true;
3881 }
3882 }
3883
3884 /* deliver to local stack */
3885 skb->protocol = eth_type_trans(skb, fast_rx->dev);
3886 memset(skb->cb, 0, sizeof(skb->cb));
3887 if (rx->napi)
3888 napi_gro_receive(rx->napi, skb);
3889 else
3890 netif_receive_skb(skb);
3891
3892 return true;
3893 drop:
3894 dev_kfree_skb(skb);
Johannes Bergc9c59622016-03-31 20:02:11 +03003895 stats->dropped++;
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003896 return true;
3897}
3898
Johannes Berg571ecf62007-07-27 15:43:22 +02003899/*
Johannes Berg4406c372010-09-24 11:21:06 +02003900 * This function returns whether or not the SKB
3901 * was destined for RX processing or not, which,
3902 * if consume is true, is equivalent to whether
3903 * or not the skb was consumed.
3904 */
3905static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3906 struct sk_buff *skb, bool consume)
3907{
3908 struct ieee80211_local *local = rx->local;
3909 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02003910
3911 rx->skb = skb;
Johannes Berg4406c372010-09-24 11:21:06 +02003912
Johannes Berg49ddf8e2016-03-31 20:02:10 +03003913 /* See if we can do fast-rx; if we have to copy we already lost,
3914 * so punt in that case. We should never have to deliver a data
3915 * frame to multiple interfaces anyway.
3916 *
3917 * We skip the ieee80211_accept_frame() call and do the necessary
3918 * checking inside ieee80211_invoke_fast_rx().
3919 */
3920 if (consume && rx->sta) {
3921 struct ieee80211_fast_rx *fast_rx;
3922
3923 fast_rx = rcu_dereference(rx->sta->fast_rx);
3924 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
3925 return true;
3926 }
3927
Johannes Berga58fbe12015-04-22 15:08:39 +02003928 if (!ieee80211_accept_frame(rx))
Johannes Berg4406c372010-09-24 11:21:06 +02003929 return false;
3930
Johannes Berg4406c372010-09-24 11:21:06 +02003931 if (!consume) {
3932 skb = skb_copy(skb, GFP_ATOMIC);
3933 if (!skb) {
3934 if (net_ratelimit())
3935 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08003936 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02003937 sdata->name);
3938 return true;
3939 }
3940
3941 rx->skb = skb;
3942 }
3943
3944 ieee80211_invoke_rx_handlers(rx);
3945 return true;
3946}
3947
3948/*
Zhao, Gang6b59db72014-04-21 12:52:59 +08003949 * This is the actual Rx frames handler. as it belongs to Rx path it must
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003950 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02003951 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02003952static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Johannes Bergd63b5482016-03-31 20:02:02 +03003953 struct ieee80211_sta *pubsta,
Johannes Bergaf9f9b22015-06-11 16:02:32 +02003954 struct sk_buff *skb,
3955 struct napi_struct *napi)
Johannes Berg571ecf62007-07-27 15:43:22 +02003956{
3957 struct ieee80211_local *local = hw_to_local(hw);
3958 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02003959 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b32010-03-29 17:35:07 +08003960 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01003961 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02003962 struct ieee80211_sub_if_data *prev;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01003963 struct rhash_head *tmp;
Zhu Yie3cf8b32010-03-29 17:35:07 +08003964 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02003965
Zhu Yie3cf8b32010-03-29 17:35:07 +08003966 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02003967 memset(&rx, 0, sizeof(rx));
3968 rx.skb = skb;
3969 rx.local = local;
Johannes Bergaf9f9b22015-06-11 16:02:32 +02003970 rx.napi = napi;
Johannes Berg72abd812007-09-17 01:29:22 -04003971
Zhu Yie3cf8b32010-03-29 17:35:07 +08003972 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Bergc206ca62015-04-22 20:47:28 +02003973 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
Johannes Berg571ecf62007-07-27 15:43:22 +02003974
Johannes Berg4a4f1a52012-10-26 00:33:36 +02003975 if (ieee80211_is_mgmt(fc)) {
3976 /* drop frame if too short for header */
3977 if (skb->len < ieee80211_hdrlen(fc))
3978 err = -ENOBUFS;
3979 else
3980 err = skb_linearize(skb);
3981 } else {
Zhu Yie3cf8b32010-03-29 17:35:07 +08003982 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02003983 }
Zhu Yie3cf8b32010-03-29 17:35:07 +08003984
3985 if (err) {
3986 dev_kfree_skb(skb);
3987 return;
3988 }
3989
3990 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01003991 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01003992 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01003993
Johannes Bergd48b2962012-07-06 22:19:27 +02003994 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3995 ieee80211_is_beacon(hdr->frame_control)))
3996 ieee80211_scan_rx(local, skb);
3997
Johannes Bergd63b5482016-03-31 20:02:02 +03003998 if (pubsta) {
3999 rx.sta = container_of(pubsta, struct sta_info, sta);
4000 rx.sdata = rx.sta->sdata;
4001 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4002 return;
4003 goto out;
4004 } else if (ieee80211_is_data(fc)) {
4005 struct sta_info *sta, *prev_sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004006 const struct bucket_table *tbl;
4007
Ben Greear56af3262010-09-23 10:22:24 -07004008 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004009
Johannes Berg7bedd0c2015-02-13 21:55:15 +01004010 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
4011
4012 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07004013 if (!prev_sta) {
4014 prev_sta = sta;
4015 continue;
4016 }
4017
4018 rx.sta = prev_sta;
4019 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02004020 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02004021
Ben Greear56af3262010-09-23 10:22:24 -07004022 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02004023 }
Ben Greear56af3262010-09-23 10:22:24 -07004024
4025 if (prev_sta) {
4026 rx.sta = prev_sta;
4027 rx.sdata = prev_sta->sdata;
4028
Johannes Berg4406c372010-09-24 11:21:06 +02004029 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07004030 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304031 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07004032 }
Johannes Berg4406c372010-09-24 11:21:06 +02004033 }
4034
Johannes Berg4b0dd982010-09-24 11:21:07 +02004035 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02004036
Johannes Berg4b0dd982010-09-24 11:21:07 +02004037 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4038 if (!ieee80211_sdata_running(sdata))
4039 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004040
Johannes Berg4b0dd982010-09-24 11:21:07 +02004041 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4042 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4043 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01004044
Johannes Berg4b0dd982010-09-24 11:21:07 +02004045 /*
4046 * frame is destined for this interface, but if it's
4047 * not also for the previous one we handle that after
4048 * the loop to avoid copying the SKB once too much
4049 */
Johannes Bergb2e77712007-09-26 15:19:39 +02004050
Johannes Berg4b0dd982010-09-24 11:21:07 +02004051 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02004052 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02004053 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02004054 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004055
Johannes Berg7852e362012-01-20 13:55:24 +01004056 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004057 rx.sdata = prev;
4058 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01004059
Johannes Berg4b0dd982010-09-24 11:21:07 +02004060 prev = sdata;
4061 }
Johannes Berg4406c372010-09-24 11:21:06 +02004062
Johannes Berg4b0dd982010-09-24 11:21:07 +02004063 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01004064 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02004065 rx.sdata = prev;
4066
4067 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4068 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02004069 }
Johannes Berg4406c372010-09-24 11:21:06 +02004070
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05304071 out:
Johannes Berg4406c372010-09-24 11:21:06 +02004072 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02004073}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004074
4075/*
4076 * This is the receive path handler. It is called by a low level driver when an
4077 * 802.11 MPDU is received from the hardware.
4078 */
Johannes Bergd63b5482016-03-31 20:02:02 +03004079void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4080 struct sk_buff *skb, struct napi_struct *napi)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004081{
4082 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01004083 struct ieee80211_rate *rate = NULL;
4084 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02004085 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01004086
Johannes Bergd20ef632009-10-11 15:10:40 +02004087 WARN_ON_ONCE(softirq_count() == 0);
4088
Johannes Berg57fbcce2016-04-12 15:56:15 +02004089 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02004090 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004091
4092 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02004093 if (WARN_ON(!sband))
4094 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01004095
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004096 /*
4097 * If we're suspending, it is possible although not too likely
4098 * that we'd be receiving frames after having already partially
4099 * quiesced the stack. We can't process such frames then since
4100 * that might, for example, cause stations to be added or other
4101 * driver callbacks be invoked.
4102 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004103 if (unlikely(local->quiescing || local->suspended))
4104 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02004105
Arik Nemtsov04800ad2012-06-06 11:25:02 +03004106 /* We might be during a HW reconfig, prevent Rx for the same reason */
4107 if (unlikely(local->in_reconfig))
4108 goto drop;
4109
Johannes Bergea77f122009-08-21 14:44:45 +02004110 /*
4111 * The same happens when we're not even started,
4112 * but that's worth a warning.
4113 */
Johannes Berg77a980d2009-08-24 11:46:30 +02004114 if (WARN_ON(!local->started))
4115 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02004116
Johannes Bergfc885182010-07-30 13:23:12 +02004117 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004118 /*
Johannes Bergfc885182010-07-30 13:23:12 +02004119 * Validate the rate, unless a PLCP error means that
4120 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05004121 */
Johannes Bergfc885182010-07-30 13:23:12 +02004122
4123 if (status->flag & RX_FLAG_HT) {
4124 /*
4125 * rate_idx is MCS index, which can be [0-76]
4126 * as documented on:
4127 *
4128 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4129 *
4130 * Anything else would be some sort of driver or
4131 * hardware error. The driver should catch hardware
4132 * errors.
4133 */
Johannes Berg444e3802012-09-30 17:08:35 +02004134 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02004135 "Rate marked as an HT rate but passed "
4136 "status->rate_idx is not "
4137 "an MCS index [0-76]: %d (0x%02x)\n",
4138 status->rate_idx,
4139 status->rate_idx))
4140 goto drop;
Johannes Berg56146182012-11-09 15:07:02 +01004141 } else if (status->flag & RX_FLAG_VHT) {
4142 if (WARN_ONCE(status->rate_idx > 9 ||
4143 !status->vht_nss ||
4144 status->vht_nss > 8,
4145 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4146 status->rate_idx, status->vht_nss))
4147 goto drop;
Johannes Bergfc885182010-07-30 13:23:12 +02004148 } else {
Johannes Berg444e3802012-09-30 17:08:35 +02004149 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02004150 goto drop;
4151 rate = &sband->bitrates[status->rate_idx];
4152 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02004153 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004154
Johannes Berg554891e2010-09-24 12:38:25 +02004155 status->rx_flags = 0;
4156
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004157 /*
4158 * key references and virtual interfaces are protected using RCU
4159 * and this requires that we are in a read-side RCU section during
4160 * receive processing
4161 */
4162 rcu_read_lock();
4163
4164 /*
4165 * Frames with failed FCS/PLCP checksum are not returned,
4166 * all other frames are returned without radiotap header
4167 * if it was previously present.
4168 * Also, frames with less than 16 bytes are dropped.
4169 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004170 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004171 if (!skb) {
4172 rcu_read_unlock();
4173 return;
4174 }
4175
Johannes Berge1e54062010-11-30 08:58:45 +01004176 ieee80211_tpt_led_trig_rx(local,
4177 ((struct ieee80211_hdr *)skb->data)->frame_control,
4178 skb->len);
Johannes Bergd63b5482016-03-31 20:02:02 +03004179
4180 __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004181
4182 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02004183
4184 return;
4185 drop:
4186 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02004187}
Johannes Bergaf9f9b22015-06-11 16:02:32 +02004188EXPORT_SYMBOL(ieee80211_rx_napi);
Johannes Berg571ecf62007-07-27 15:43:22 +02004189
4190/* This is a version of the rx handler that can be called from hard irq
4191 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02004192void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02004193{
4194 struct ieee80211_local *local = hw_to_local(hw);
4195
4196 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4197
Johannes Berg571ecf62007-07-27 15:43:22 +02004198 skb->pkt_type = IEEE80211_RX_MSG;
4199 skb_queue_tail(&local->skb_queue, skb);
4200 tasklet_schedule(&local->tasklet);
4201}
4202EXPORT_SYMBOL(ieee80211_rx_irqsafe);