blob: ec15a4929f7a755607874ab4c323679c0f41c4fa [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 Berg571ecf62007-07-27 15:43:22 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
S.Çağlar Onurab466232008-02-14 17:36:47 +020012#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020014#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040018#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040019#include <linux/export.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020020#include <net/mac80211.h>
21#include <net/ieee80211_radiotap.h>
Johannes Bergd26ad372012-02-20 11:38:41 +010022#include <asm/unaligned.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020023
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040026#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010027#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020028#include "wep.h"
29#include "wpa.h"
30#include "tkip.h"
31#include "wme.h"
Johannes Berg1d8d3de2011-12-16 15:28:57 +010032#include "rate.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020033
Johannes Bergb2e77712007-09-26 15:19:39 +020034/*
35 * monitor mode reception
36 *
37 * This function cleans up the SKB, i.e. it removes all the stuff
38 * only useful for monitoring.
39 */
40static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
Johannes Berg0869aea2009-10-28 10:03:35 +010041 struct sk_buff *skb)
Johannes Bergb2e77712007-09-26 15:19:39 +020042{
Johannes Berg90b9e4462012-11-16 10:09:08 +010043 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
44
Johannes Bergb2e77712007-09-26 15:19:39 +020045 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
46 if (likely(skb->len > FCS_LEN))
Zhu Yie3cf8b32010-03-29 17:35:07 +080047 __pskb_trim(skb, skb->len - FCS_LEN);
Johannes Bergb2e77712007-09-26 15:19:39 +020048 else {
49 /* driver bug */
50 WARN_ON(1);
51 dev_kfree_skb(skb);
52 skb = NULL;
53 }
54 }
55
Johannes Berg90b9e4462012-11-16 10:09:08 +010056 if (status->vendor_radiotap_len)
57 __pskb_pull(skb, status->vendor_radiotap_len);
58
Johannes Bergb2e77712007-09-26 15:19:39 +020059 return skb;
60}
61
Johannes Berg1df332e2012-10-26 00:09:11 +020062static inline int should_drop_frame(struct sk_buff *skb, int present_fcs_len)
Johannes Bergb2e77712007-09-26 15:19:39 +020063{
Johannes Bergf1d58c22009-06-17 13:13:00 +020064 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Harvey Harrison182503a2008-06-22 16:45:29 -070065 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergb2e77712007-09-26 15:19:39 +020066
Johannes Berg4c298672012-07-05 11:34:31 +020067 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
68 RX_FLAG_FAILED_PLCP_CRC |
69 RX_FLAG_AMPDU_IS_ZEROLEN))
Johannes Bergb2e77712007-09-26 15:19:39 +020070 return 1;
Johannes Berg0869aea2009-10-28 10:03:35 +010071 if (unlikely(skb->len < 16 + present_fcs_len))
Johannes Bergb2e77712007-09-26 15:19:39 +020072 return 1;
Harvey Harrison87228f52008-06-11 14:21:59 -070073 if (ieee80211_is_ctl(hdr->frame_control) &&
74 !ieee80211_is_pspoll(hdr->frame_control) &&
75 !ieee80211_is_back_req(hdr->frame_control))
Johannes Bergb2e77712007-09-26 15:19:39 +020076 return 1;
77 return 0;
78}
79
Bruno Randolf601ae7f2008-05-08 19:22:43 +020080static int
Johannes Berg90b9e4462012-11-16 10:09:08 +010081ieee80211_rx_radiotap_space(struct ieee80211_local *local,
82 struct ieee80211_rx_status *status)
Bruno Randolf601ae7f2008-05-08 19:22:43 +020083{
84 int len;
85
86 /* always present fields */
87 len = sizeof(struct ieee80211_radiotap_header) + 9;
88
Johannes Berg90b9e4462012-11-16 10:09:08 +010089 /* allocate extra bitmap */
90 if (status->vendor_radiotap_len)
91 len += 4;
92
93 if (ieee80211_have_rx_timestamp(status)) {
94 len = ALIGN(len, 8);
Bruno Randolf601ae7f2008-05-08 19:22:43 +020095 len += 8;
Johannes Berg90b9e4462012-11-16 10:09:08 +010096 }
Johannes Berg7fee5372009-01-30 11:13:06 +010097 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
Bruno Randolf601ae7f2008-05-08 19:22:43 +020098 len += 1;
Bruno Randolf601ae7f2008-05-08 19:22:43 +020099
Johannes Berg90b9e4462012-11-16 10:09:08 +0100100 /* padding for RX_FLAGS if necessary */
101 len = ALIGN(len, 2);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200102
Johannes Berg6d744ba2011-01-27 14:13:17 +0100103 if (status->flag & RX_FLAG_HT) /* HT info */
104 len += 3;
105
Johannes Berg4c298672012-07-05 11:34:31 +0200106 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100107 len = ALIGN(len, 4);
Johannes Berg4c298672012-07-05 11:34:31 +0200108 len += 8;
109 }
110
Johannes Berg90b9e4462012-11-16 10:09:08 +0100111 if (status->vendor_radiotap_len) {
112 if (WARN_ON_ONCE(status->vendor_radiotap_align == 0))
113 status->vendor_radiotap_align = 1;
114 /* align standard part of vendor namespace */
115 len = ALIGN(len, 2);
116 /* allocate standard part of vendor namespace */
117 len += 6;
118 /* align vendor-defined part */
119 len = ALIGN(len, status->vendor_radiotap_align);
120 /* vendor-defined part is already in skb */
121 }
122
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200123 return len;
124}
125
Johannes Berg00ea6de2012-09-05 15:54:51 +0200126/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200127 * ieee80211_add_rx_radiotap_header - add radiotap header
128 *
129 * add a radiotap header containing all the fields which the hardware provided.
130 */
131static void
132ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
133 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200134 struct ieee80211_rate *rate,
Felix Fietkau973ef212012-04-16 14:56:48 +0200135 int rtap_len, bool has_fcs)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200136{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200137 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200138 struct ieee80211_radiotap_header *rthdr;
139 unsigned char *pos;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100140 u16 rx_flags = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800141 int mpdulen;
142
143 mpdulen = skb->len;
144 if (!(has_fcs && (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)))
145 mpdulen += FCS_LEN;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200146
147 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
148 memset(rthdr, 0, rtap_len);
149
150 /* radiotap header, set always present flags */
151 rthdr->it_present =
152 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200153 (1 << IEEE80211_RADIOTAP_CHANNEL) |
154 (1 << IEEE80211_RADIOTAP_ANTENNA) |
155 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
Johannes Berg90b9e4462012-11-16 10:09:08 +0100156 rthdr->it_len = cpu_to_le16(rtap_len + status->vendor_radiotap_len);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200157
Johannes Berg1df332e2012-10-26 00:09:11 +0200158 pos = (unsigned char *)(rthdr + 1);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200159
Johannes Berg90b9e4462012-11-16 10:09:08 +0100160 if (status->vendor_radiotap_len) {
161 rthdr->it_present |=
162 cpu_to_le32(BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE)) |
163 cpu_to_le32(BIT(IEEE80211_RADIOTAP_EXT));
164 put_unaligned_le32(status->vendor_radiotap_bitmap, pos);
165 pos += 4;
166 }
167
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200168 /* the order of the following fields is important */
169
170 /* IEEE80211_RADIOTAP_TSFT */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800171 if (ieee80211_have_rx_timestamp(status)) {
Johannes Berg90b9e4462012-11-16 10:09:08 +0100172 /* padding */
173 while ((pos - (u8 *)rthdr) & 7)
174 *pos++ = 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800175 put_unaligned_le64(
176 ieee80211_calculate_rx_timestamp(local, status,
177 mpdulen, 0),
178 pos);
Johannes Berg1df332e2012-10-26 00:09:11 +0200179 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200180 pos += 8;
181 }
182
183 /* IEEE80211_RADIOTAP_FLAGS */
Felix Fietkau973ef212012-04-16 14:56:48 +0200184 if (has_fcs && (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS))
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200185 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100186 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
187 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200188 if (status->flag & RX_FLAG_SHORTPRE)
189 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200190 pos++;
191
192 /* IEEE80211_RADIOTAP_RATE */
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100193 if (!rate || status->flag & RX_FLAG_HT) {
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200194 /*
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100195 * Without rate information don't add it. If we have,
Mohammed Shafi Shajakhan38f37be2011-02-07 10:10:04 +0530196 * MCS information is a separate field in radiotap,
Johannes Berg73b48092011-04-18 17:05:21 +0200197 * added below. The byte here is needed as padding
198 * for the channel though, so initialise it to 0.
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200199 */
200 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200201 } else {
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200202 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200203 *pos = rate->bitrate / 5;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200204 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200205 pos++;
206
207 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100208 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200209 pos += 2;
210 if (status->band == IEEE80211_BAND_5GHZ)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100211 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
212 pos);
Johannes Berg5f0b7de2009-11-16 13:58:21 +0100213 else if (status->flag & RX_FLAG_HT)
214 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
215 pos);
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100216 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100217 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
218 pos);
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100219 else if (rate)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100220 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
221 pos);
Johannes Bergf8d1ccf2011-11-08 12:28:33 +0100222 else
223 put_unaligned_le16(IEEE80211_CHAN_2GHZ, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200224 pos += 2;
225
226 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
Felix Fietkaufe8431f2012-03-01 18:00:07 +0100227 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM &&
228 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200229 *pos = status->signal;
230 rthdr->it_present |=
231 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
232 pos++;
233 }
234
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200235 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
236
237 /* IEEE80211_RADIOTAP_ANTENNA */
238 *pos = status->antenna;
239 pos++;
240
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200241 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
242
243 /* IEEE80211_RADIOTAP_RX_FLAGS */
244 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100245 if ((pos - (u8 *)rthdr) & 1)
Johannes Berg90b9e4462012-11-16 10:09:08 +0100246 *pos++ = 0;
Johannes Bergaae89832009-03-13 12:52:10 +0100247 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100248 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
249 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200250 pos += 2;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100251
252 if (status->flag & RX_FLAG_HT) {
253 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
Johannes Bergac55d2f2012-05-10 09:09:10 +0200254 *pos++ = local->hw.radiotap_mcs_details;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100255 *pos = 0;
256 if (status->flag & RX_FLAG_SHORT_GI)
257 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
258 if (status->flag & RX_FLAG_40MHZ)
259 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
Johannes Bergac55d2f2012-05-10 09:09:10 +0200260 if (status->flag & RX_FLAG_HT_GF)
261 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
Johannes Berg6d744ba2011-01-27 14:13:17 +0100262 pos++;
263 *pos++ = status->rate_idx;
264 }
Johannes Berg4c298672012-07-05 11:34:31 +0200265
266 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
267 u16 flags = 0;
268
269 /* ensure 4 byte alignment */
270 while ((pos - (u8 *)rthdr) & 3)
271 pos++;
272 rthdr->it_present |=
273 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
274 put_unaligned_le32(status->ampdu_reference, pos);
275 pos += 4;
276 if (status->flag & RX_FLAG_AMPDU_REPORT_ZEROLEN)
277 flags |= IEEE80211_RADIOTAP_AMPDU_REPORT_ZEROLEN;
278 if (status->flag & RX_FLAG_AMPDU_IS_ZEROLEN)
279 flags |= IEEE80211_RADIOTAP_AMPDU_IS_ZEROLEN;
280 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
281 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
282 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
283 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
284 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
285 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
286 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
287 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
288 put_unaligned_le16(flags, pos);
289 pos += 2;
290 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
291 *pos++ = status->ampdu_delimiter_crc;
292 else
293 *pos++ = 0;
294 *pos++ = 0;
295 }
Johannes Berg90b9e4462012-11-16 10:09:08 +0100296
297 if (status->vendor_radiotap_len) {
298 /* ensure 2 byte alignment for the vendor field as required */
299 if ((pos - (u8 *)rthdr) & 1)
300 *pos++ = 0;
301 *pos++ = status->vendor_radiotap_oui[0];
302 *pos++ = status->vendor_radiotap_oui[1];
303 *pos++ = status->vendor_radiotap_oui[2];
304 *pos++ = status->vendor_radiotap_subns;
305 put_unaligned_le16(status->vendor_radiotap_len, pos);
306 pos += 2;
307 /* align the actual payload as requested */
308 while ((pos - (u8 *)rthdr) & (status->vendor_radiotap_align - 1))
309 *pos++ = 0;
310 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200311}
312
Johannes Bergb2e77712007-09-26 15:19:39 +0200313/*
314 * This function copies a received frame to all monitor interfaces and
315 * returns a cleaned-up SKB that no longer includes the FCS nor the
316 * radiotap header the driver might have added.
317 */
318static struct sk_buff *
319ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100320 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200321{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200322 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200323 struct ieee80211_sub_if_data *sdata;
Johannes Berg293702a2012-03-02 13:18:19 +0100324 int needed_headroom;
Johannes Bergb2e77712007-09-26 15:19:39 +0200325 struct sk_buff *skb, *skb2;
326 struct net_device *prev_dev = NULL;
327 int present_fcs_len = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200328
329 /*
330 * First, we may need to make a copy of the skb because
331 * (1) we need to modify it for radiotap (if not present), and
332 * (2) the other RX handlers will modify the skb we got.
333 *
334 * We don't need to, of course, if we aren't going to return
335 * the SKB because it has a bad FCS/PLCP checksum.
336 */
Johannes Berg0869aea2009-10-28 10:03:35 +0100337
338 /* room for the radiotap header based on driver features */
Johannes Berg90b9e4462012-11-16 10:09:08 +0100339 needed_headroom = ieee80211_rx_radiotap_space(local, status);
Johannes Bergb2e77712007-09-26 15:19:39 +0200340
341 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
342 present_fcs_len = FCS_LEN;
343
Zhu Yie3cf8b32010-03-29 17:35:07 +0800344 /* make sure hdr->frame_control is on the linear part */
345 if (!pskb_may_pull(origskb, 2)) {
346 dev_kfree_skb(origskb);
347 return NULL;
348 }
349
Johannes Bergb2e77712007-09-26 15:19:39 +0200350 if (!local->monitors) {
Johannes Berg0869aea2009-10-28 10:03:35 +0100351 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200352 dev_kfree_skb(origskb);
353 return NULL;
354 }
355
Johannes Berg0869aea2009-10-28 10:03:35 +0100356 return remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200357 }
358
Johannes Berg0869aea2009-10-28 10:03:35 +0100359 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200360 /* only need to expand headroom if necessary */
361 skb = origskb;
362 origskb = NULL;
363
364 /*
365 * This shouldn't trigger often because most devices have an
366 * RX header they pull before we get here, and that should
367 * be big enough for our radiotap information. We should
368 * probably export the length to drivers so that we can have
369 * them allocate enough headroom to start with.
370 */
371 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100372 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200373 dev_kfree_skb(skb);
374 return NULL;
375 }
376 } else {
377 /*
378 * Need to make a copy and possibly remove radiotap header
379 * and FCS from the original.
380 */
381 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
382
Johannes Berg0869aea2009-10-28 10:03:35 +0100383 origskb = remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200384
385 if (!skb)
386 return origskb;
387 }
388
Johannes Berg0869aea2009-10-28 10:03:35 +0100389 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +0200390 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
391 true);
Johannes Bergb2e77712007-09-26 15:19:39 +0200392
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100393 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200394 skb->ip_summed = CHECKSUM_UNNECESSARY;
395 skb->pkt_type = PACKET_OTHERHOST;
396 skb->protocol = htons(ETH_P_802_2);
397
398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200399 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200400 continue;
401
Michael Wu3d30d942008-01-31 19:48:27 +0100402 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
403 continue;
404
Johannes Berg9607e6b2009-12-23 13:15:31 +0100405 if (!ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100406 continue;
407
Johannes Bergb2e77712007-09-26 15:19:39 +0200408 if (prev_dev) {
409 skb2 = skb_clone(skb, GFP_ATOMIC);
410 if (skb2) {
411 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400412 netif_receive_skb(skb2);
Johannes Bergb2e77712007-09-26 15:19:39 +0200413 }
414 }
415
416 prev_dev = sdata->dev;
417 sdata->dev->stats.rx_packets++;
418 sdata->dev->stats.rx_bytes += skb->len;
419 }
420
421 if (prev_dev) {
422 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -0400423 netif_receive_skb(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200424 } else
425 dev_kfree_skb(skb);
426
427 return origskb;
428}
429
Johannes Berg5cf121c2008-02-25 16:27:43 +0100430static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200431{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700432 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200433 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg9e262972011-07-07 18:45:03 +0200434 int tid, seqno_idx, security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200435
436 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700437 if (ieee80211_is_data_qos(hdr->frame_control)) {
438 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200439 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700440 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
Johannes Berg04b7dcf2011-06-22 10:06:59 +0200441 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
Johannes Berg554891e2010-09-24 12:38:25 +0200442 status->rx_flags |= IEEE80211_RX_AMSDU;
Johannes Berg9e262972011-07-07 18:45:03 +0200443
444 seqno_idx = tid;
445 security_idx = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200446 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200447 /*
448 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
449 *
450 * Sequence numbers for management frames, QoS data
451 * frames with a broadcast/multicast address in the
452 * Address 1 field, and all non-QoS data frames sent
453 * by QoS STAs are assigned using an additional single
454 * modulo-4096 counter, [...]
455 *
456 * We also use that counter for non-QoS STAs.
457 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100458 seqno_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200459 security_idx = 0;
460 if (ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg5a306f52012-11-14 23:22:21 +0100461 security_idx = IEEE80211_NUM_TIDS;
Johannes Berg9e262972011-07-07 18:45:03 +0200462 tid = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200463 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200464
Johannes Berg9e262972011-07-07 18:45:03 +0200465 rx->seqno_idx = seqno_idx;
466 rx->security_idx = security_idx;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200467 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
468 * For now, set skb->priority to 0 for other cases. */
469 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100470}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200471
Johannes Bergd1c3a372009-01-07 00:26:10 +0100472/**
473 * DOC: Packet alignment
474 *
475 * Drivers always need to pass packets that are aligned to two-byte boundaries
476 * to the stack.
477 *
478 * Additionally, should, if possible, align the payload data in a way that
479 * guarantees that the contained IP header is aligned to a four-byte
480 * boundary. In the case of regular frames, this simply means aligning the
481 * payload to a four-byte boundary (because either the IP header is directly
482 * contained, or IV/RFC1042 headers that have a length divisible by four are
Kalle Valo59d9cb02009-12-17 13:54:57 +0100483 * in front of it). If the payload data is not properly aligned and the
484 * architecture doesn't support efficient unaligned operations, mac80211
485 * will align the data.
Johannes Bergd1c3a372009-01-07 00:26:10 +0100486 *
487 * With A-MSDU frames, however, the payload data address must yield two modulo
488 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
489 * push the IP header further back to a multiple of four again. Thankfully, the
490 * specs were sane enough this time around to require padding each A-MSDU
491 * subframe to a length that is a multiple of four.
492 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300493 * Padding like Atheros hardware adds which is between the 802.11 header and
Johannes Bergd1c3a372009-01-07 00:26:10 +0100494 * the payload is not supported, the driver is required to move the 802.11
495 * header to be directly in front of the payload in that case.
496 */
497static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100498{
Kalle Valo59d9cb02009-12-17 13:54:57 +0100499#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
500 WARN_ONCE((unsigned long)rx->skb->data & 1,
501 "unaligned packet at 0x%p\n", rx->skb->data);
Johannes Bergd1c3a372009-01-07 00:26:10 +0100502#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200503}
504
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200505
Johannes Berg571ecf62007-07-27 15:43:22 +0200506/* rx handlers */
507
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200508static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
509{
510 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
511
512 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
513 return 0;
514
515 return ieee80211_is_robust_mgmt_frame(hdr);
516}
517
518
519static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
520{
521 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
522
523 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
524 return 0;
525
526 return ieee80211_is_robust_mgmt_frame(hdr);
527}
528
529
530/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
531static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
532{
533 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
534 struct ieee80211_mmie *mmie;
535
Johannes Berg1df332e2012-10-26 00:09:11 +0200536 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200537 return -1;
538
539 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
540 return -1; /* not a robust management frame */
541
542 mmie = (struct ieee80211_mmie *)
543 (skb->data + skb->len - sizeof(*mmie));
544 if (mmie->element_id != WLAN_EID_MMIE ||
545 mmie->length != sizeof(*mmie) - 2)
546 return -1;
547
548 return le16_to_cpu(mmie->key_id);
549}
550
Johannes Berg1df332e2012-10-26 00:09:11 +0200551static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100552{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700553 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg47846c92009-11-25 17:46:19 +0100554 char *dev_addr = rx->sdata->vif.addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100555
Harvey Harrisona7767f92008-07-02 16:30:51 -0700556 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700557 if (is_multicast_ether_addr(hdr->addr1)) {
558 if (ieee80211_has_tods(hdr->frame_control) ||
Johannes Berg1df332e2012-10-26 00:09:11 +0200559 !ieee80211_has_fromds(hdr->frame_control))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700560 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000561 if (ether_addr_equal(hdr->addr3, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700562 return RX_DROP_MONITOR;
563 } else {
564 if (!ieee80211_has_a4(hdr->frame_control))
565 return RX_DROP_MONITOR;
Joe Perchesb203ca32012-05-08 18:56:52 +0000566 if (ether_addr_equal(hdr->addr4, dev_addr))
Javier Cardona3c5772a2009-08-10 12:15:48 -0700567 return RX_DROP_MONITOR;
568 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100569 }
570
571 /* If there is not an established peer link and this is not a peer link
572 * establisment frame, beacon or probe, drop the frame.
573 */
574
Javier Cardona57cf8042011-05-13 10:45:43 -0700575 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100576 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100577
Harvey Harrisona7767f92008-07-02 16:30:51 -0700578 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100579 return RX_DROP_MONITOR;
580
Harvey Harrisona7767f92008-07-02 16:30:51 -0700581 if (ieee80211_is_action(hdr->frame_control)) {
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700582 u8 category;
Johannes Berg9b395bc2012-10-26 00:36:40 +0200583
584 /* make sure category field is present */
585 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
586 return RX_DROP_MONITOR;
587
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100588 mgmt = (struct ieee80211_mgmt *)hdr;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -0700589 category = mgmt->u.action.category;
590 if (category != WLAN_CATEGORY_MESH_ACTION &&
Johannes Berg1df332e2012-10-26 00:09:11 +0200591 category != WLAN_CATEGORY_SELF_PROTECTED)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100592 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100593 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100594 }
595
Harvey Harrisona7767f92008-07-02 16:30:51 -0700596 if (ieee80211_is_probe_req(hdr->frame_control) ||
597 ieee80211_is_probe_resp(hdr->frame_control) ||
Javier Cardona71839122011-04-07 15:08:31 -0700598 ieee80211_is_beacon(hdr->frame_control) ||
599 ieee80211_is_auth(hdr->frame_control))
Harvey Harrisona7767f92008-07-02 16:30:51 -0700600 return RX_CONTINUE;
601
602 return RX_DROP_MONITOR;
Harvey Harrisona7767f92008-07-02 16:30:51 -0700603 }
604
Johannes Berg902acc72008-02-23 15:17:19 +0100605 return RX_CONTINUE;
606}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100607
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100608#define SEQ_MODULO 0x1000
609#define SEQ_MASK 0xfff
610
611static inline int seq_less(u16 sq1, u16 sq2)
612{
613 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
614}
615
616static inline u16 seq_inc(u16 sq)
617{
618 return (sq + 1) & SEQ_MASK;
619}
620
621static inline u16 seq_sub(u16 sq1, u16 sq2)
622{
623 return (sq1 - sq2) & SEQ_MASK;
624}
625
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200626static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100627 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100628 int index)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100629{
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200630 struct ieee80211_local *local = sdata->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100631 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
Christian Lamparter4cfda472010-12-27 23:21:26 +0100632 struct ieee80211_rx_status *status;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100633
Johannes Bergdd318572010-11-29 11:09:16 +0100634 lockdep_assert_held(&tid_agg_rx->reorder_lock);
635
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100636 if (!skb)
637 goto no_frame;
638
Christian Lamparter071d9ac2010-08-05 01:36:36 +0200639 /* release the frame from the reorder ring buffer */
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100640 tid_agg_rx->stored_mpdu_num--;
641 tid_agg_rx->reorder_buf[index] = NULL;
Christian Lamparter4cfda472010-12-27 23:21:26 +0100642 status = IEEE80211_SKB_RXCB(skb);
643 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100644 skb_queue_tail(&local->rx_skb_queue, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100645
646no_frame:
647 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
648}
649
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200650static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100651 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100652 u16 head_seq_num)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100653{
654 int index;
655
Johannes Bergdd318572010-11-29 11:09:16 +0100656 lockdep_assert_held(&tid_agg_rx->reorder_lock);
657
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100658 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
659 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
660 tid_agg_rx->buf_size;
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200661 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100662 }
663}
664
665/*
666 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
667 * the skb was added to the buffer longer than this time ago, the earlier
668 * frames that have not yet been received are assumed to be lost and the skb
669 * can be released for processing. This may also release other skb's from the
670 * reorder buffer if there are no additional gaps between the frames.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200671 *
672 * Callers must hold tid_agg_rx->reorder_lock.
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100673 */
674#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
675
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200676static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100677 struct tid_ampdu_rx *tid_agg_rx)
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200678{
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200679 int index, j;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200680
Johannes Bergdd318572010-11-29 11:09:16 +0100681 lockdep_assert_held(&tid_agg_rx->reorder_lock);
682
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200683 /* release the buffer until next missing frame */
684 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
685 tid_agg_rx->buf_size;
686 if (!tid_agg_rx->reorder_buf[index] &&
Eliad Peller07ae2df2012-02-01 18:48:09 +0200687 tid_agg_rx->stored_mpdu_num) {
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200688 /*
689 * No buffers ready to be released, but check whether any
690 * frames in the reorder buffer have timed out.
691 */
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200692 int skipped = 1;
693 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
694 j = (j + 1) % tid_agg_rx->buf_size) {
695 if (!tid_agg_rx->reorder_buf[j]) {
696 skipped++;
697 continue;
698 }
Daniel Halperin499fe9a2011-03-24 16:01:48 -0700699 if (skipped &&
700 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200701 HT_RX_REORDER_BUF_TIMEOUT))
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200702 goto set_release_timer;
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200703
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200704 ht_dbg_ratelimited(sdata,
705 "release an RX reorder frame due to timeout on earlier frames\n");
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200706 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j);
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200707
708 /*
709 * Increment the head seq# also for the skipped slots.
710 */
711 tid_agg_rx->head_seq_num =
712 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
713 skipped = 0;
714 }
715 } else while (tid_agg_rx->reorder_buf[index]) {
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200716 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index);
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200717 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
718 tid_agg_rx->buf_size;
719 }
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200720
721 if (tid_agg_rx->stored_mpdu_num) {
722 j = index = seq_sub(tid_agg_rx->head_seq_num,
723 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
724
725 for (; j != (index - 1) % tid_agg_rx->buf_size;
726 j = (j + 1) % tid_agg_rx->buf_size) {
727 if (tid_agg_rx->reorder_buf[j])
728 break;
729 }
730
731 set_release_timer:
732
733 mod_timer(&tid_agg_rx->reorder_timer,
Christian Lamparter334df732011-04-24 20:41:16 +0200734 tid_agg_rx->reorder_time[j] + 1 +
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200735 HT_RX_REORDER_BUF_TIMEOUT);
736 } else {
737 del_timer(&tid_agg_rx->reorder_timer);
738 }
Christian Lamparteraa0c8632010-08-05 01:36:04 +0200739}
740
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100741/*
742 * As this function belongs to the RX path it must be under
743 * rcu_read_lock protection. It returns false if the frame
744 * can be processed immediately, true if it was consumed.
745 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200746static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100747 struct tid_ampdu_rx *tid_agg_rx,
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100748 struct sk_buff *skb)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100749{
750 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
751 u16 sc = le16_to_cpu(hdr->seq_ctrl);
752 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
753 u16 head_seq_num, buf_size;
754 int index;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200755 bool ret = true;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100756
Johannes Bergdd318572010-11-29 11:09:16 +0100757 spin_lock(&tid_agg_rx->reorder_lock);
758
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100759 buf_size = tid_agg_rx->buf_size;
760 head_seq_num = tid_agg_rx->head_seq_num;
761
762 /* frame with out of date sequence number */
763 if (seq_less(mpdu_seq_num, head_seq_num)) {
764 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200765 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100766 }
767
768 /*
769 * If frame the sequence number exceeds our buffering window
770 * size release some previous frames to make room for this one.
771 */
772 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
773 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
774 /* release stored frames up to new head to stack */
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200775 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
776 head_seq_num);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100777 }
778
779 /* Now the new frame is always in the range of the reordering buffer */
780
781 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
782
783 /* check if we already stored this frame */
784 if (tid_agg_rx->reorder_buf[index]) {
785 dev_kfree_skb(skb);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200786 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100787 }
788
789 /*
790 * If the current MPDU is in the right order and nothing else
791 * is stored we can process it directly, no need to buffer it.
Johannes Bergc835b212011-03-15 23:17:01 +0100792 * If it is first but there's something stored, we may be able
793 * to release frames after this one.
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100794 */
795 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
796 tid_agg_rx->stored_mpdu_num == 0) {
797 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200798 ret = false;
799 goto out;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100800 }
801
802 /* put the frame in the reordering buffer */
803 tid_agg_rx->reorder_buf[index] = skb;
804 tid_agg_rx->reorder_time[index] = jiffies;
805 tid_agg_rx->stored_mpdu_num++;
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200806 ieee80211_sta_reorder_release(sdata, tid_agg_rx);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100807
Christian Lamparter2bff8eb2010-08-05 01:36:41 +0200808 out:
809 spin_unlock(&tid_agg_rx->reorder_lock);
810 return ret;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100811}
812
813/*
814 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
815 * true if the MPDU was buffered, false if it should be processed.
816 */
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100817static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx)
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100818{
Johannes Berg2569a822009-11-25 17:46:17 +0100819 struct sk_buff *skb = rx->skb;
820 struct ieee80211_local *local = rx->local;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100821 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Thomas Pedersen660c6a42011-11-03 21:11:12 -0700822 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg2569a822009-11-25 17:46:17 +0100823 struct sta_info *sta = rx->sta;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100824 struct tid_ampdu_rx *tid_agg_rx;
825 u16 sc;
Thomas Pedersen6cc00d52011-11-03 21:11:11 -0700826 u8 tid, ack_policy;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100827
828 if (!ieee80211_is_data_qos(hdr->frame_control))
Johannes Berg2569a822009-11-25 17:46:17 +0100829 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100830
831 /*
832 * filter the QoS data rx stream according to
833 * STA/TID and check if this STA/TID is on aggregation
834 */
835
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100836 if (!sta)
Johannes Berg2569a822009-11-25 17:46:17 +0100837 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100838
Thomas Pedersen6cc00d52011-11-03 21:11:11 -0700839 ack_policy = *ieee80211_get_qos_ctl(hdr) &
840 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100841 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
842
Johannes Berga87f7362010-06-10 10:21:38 +0200843 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
844 if (!tid_agg_rx)
845 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100846
847 /* qos null data frames are excluded */
848 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga87f7362010-06-10 10:21:38 +0200849 goto dont_reorder;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100850
Thomas Pedersen6cc00d52011-11-03 21:11:11 -0700851 /* not part of a BA session */
852 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
853 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
854 goto dont_reorder;
855
Thomas Pedersen660c6a42011-11-03 21:11:12 -0700856 /* not actually part of this BA session */
857 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
858 goto dont_reorder;
859
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100860 /* new, potentially un-ordered, ampdu frame - process it */
861
862 /* reset session timer */
863 if (tid_agg_rx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +0100864 tid_agg_rx->last_rx = jiffies;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100865
866 /* if this mpdu is fragmented - terminate rx aggregation session */
867 sc = le16_to_cpu(hdr->seq_ctrl);
868 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Bergc1475ca2010-06-10 10:21:37 +0200869 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg344eec62010-06-10 10:21:36 +0200870 skb_queue_tail(&rx->sdata->skb_queue, skb);
871 ieee80211_queue_work(&local->hw, &rx->sdata->work);
Johannes Berg2569a822009-11-25 17:46:17 +0100872 return;
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100873 }
874
Johannes Berga87f7362010-06-10 10:21:38 +0200875 /*
876 * No locking needed -- we will only ever process one
877 * RX packet at a time, and thus own tid_agg_rx. All
878 * other code manipulating it needs to (and does) make
879 * sure that we cannot get to it any more before doing
880 * anything with it.
881 */
Johannes Bergd3b2fb52012-06-22 12:48:38 +0200882 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb))
Johannes Berg2569a822009-11-25 17:46:17 +0100883 return;
884
885 dont_reorder:
Christian Lamparter24a8fda2010-12-30 17:25:29 +0100886 skb_queue_tail(&local->rx_skb_queue, skb);
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100887}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100888
Johannes Berg49461622008-06-30 15:10:45 +0200889static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100890ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200891{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700892 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +0200893 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +0200894
895 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
896 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700897 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
Johannes Berg9e262972011-07-07 18:45:03 +0200898 rx->sta->last_seq_ctrl[rx->seqno_idx] ==
Johannes Berg571ecf62007-07-27 15:43:22 +0200899 hdr->seq_ctrl)) {
Johannes Berg554891e2010-09-24 12:38:25 +0200900 if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200901 rx->local->dot11FrameDuplicateCount++;
902 rx->sta->num_duplicates++;
903 }
Felix Fietkaub1f93312011-02-04 19:20:08 +0100904 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200905 } else
Johannes Berg9e262972011-07-07 18:45:03 +0200906 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +0200907 }
908
Johannes Berg571ecf62007-07-27 15:43:22 +0200909 if (unlikely(rx->skb->len < 16)) {
910 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100911 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200912 }
913
Johannes Berg571ecf62007-07-27 15:43:22 +0200914 /* Drop disallowed frame classes based on STA auth/assoc state;
915 * IEEE 802.11, Chap 5.5.
916 *
Johannes Bergccd7b362008-09-11 00:01:56 +0200917 * mac80211 filters only based on association state, i.e. it drops
918 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +0200919 * deauth/disassoc frames when needed. In addition, hostapd is
920 * responsible for filtering on both auth and assoc states.
921 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100922
Johannes Berg902acc72008-02-23 15:17:19 +0100923 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100924 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100925
Harvey Harrisona7767f92008-07-02 16:30:51 -0700926 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
927 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200928 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Bill Jordan1be7fe82010-10-01 11:20:41 -0400929 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200930 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
Johannes Berg7852e362012-01-20 13:55:24 +0100931 /*
932 * accept port control frames from the AP even when it's not
933 * yet marked ASSOC to prevent a race where we don't set the
934 * assoc bit quickly enough before it sends the first frame
935 */
936 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
Guy Eilam2a33bee2011-08-17 15:18:15 +0300937 ieee80211_is_data_present(hdr->frame_control)) {
Johannes Berg6dbda2d2012-10-26 00:41:23 +0200938 unsigned int hdrlen;
939 __be16 ethertype;
Guy Eilam2a33bee2011-08-17 15:18:15 +0300940
Johannes Berg6dbda2d2012-10-26 00:41:23 +0200941 hdrlen = ieee80211_hdrlen(hdr->frame_control);
942
943 if (rx->skb->len < hdrlen + 8)
944 return RX_DROP_MONITOR;
945
946 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
947 if (ethertype == rx->sdata->control_port_protocol)
Guy Eilam2a33bee2011-08-17 15:18:15 +0300948 return RX_CONTINUE;
949 }
Johannes Berg21fc7562011-11-04 11:18:13 +0100950
951 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
952 cfg80211_rx_spurious_frame(rx->sdata->dev,
953 hdr->addr2,
954 GFP_ATOMIC))
955 return RX_DROP_UNUSABLE;
956
Johannes Berge4c26ad2008-01-31 19:48:21 +0100957 return RX_DROP_MONITOR;
Guy Eilam2a33bee2011-08-17 15:18:15 +0300958 }
Johannes Berg571ecf62007-07-27 15:43:22 +0200959
Johannes Berg9ae54c82008-01-31 19:48:20 +0100960 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200961}
962
963
Johannes Berg49461622008-06-30 15:10:45 +0200964static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100965ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200966{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100967 struct sk_buff *skb = rx->skb;
968 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
969 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400970 int keyidx;
971 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100972 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Berge31b8212010-10-05 19:39:30 +0200973 struct ieee80211_key *sta_ptk = NULL;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200974 int mmie_keyidx = -1;
Johannes Berg761ab472010-05-28 14:24:19 +0200975 __le16 fc;
Johannes Berg570bd532007-07-27 15:43:22 +0200976
Johannes Berg3017b802007-08-28 17:01:53 -0400977 /*
978 * Key selection 101
979 *
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200980 * There are four types of keys:
Johannes Berg3017b802007-08-28 17:01:53 -0400981 * - GTK (group keys)
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200982 * - IGTK (group keys for management frames)
Johannes Berg3017b802007-08-28 17:01:53 -0400983 * - PTK (pairwise keys)
984 * - STK (station-to-station pairwise keys)
985 *
986 * When selecting a key, we have to distinguish between multicast
987 * (including broadcast) and unicast frames, the latter can only
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200988 * use PTKs and STKs while the former always use GTKs and IGTKs.
989 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
990 * unicast frames can also use key indices like GTKs. Hence, if we
991 * don't have a PTK/STK we check the key index for a WEP key.
Johannes Berg3017b802007-08-28 17:01:53 -0400992 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400993 * Note that in a regular BSS, multicast frames are sent by the
994 * AP only, associated stations unicast the frame to the AP first
995 * which then multicasts it on their behalf.
996 *
Johannes Berg3017b802007-08-28 17:01:53 -0400997 * There is also a slight problem in IBSS mode: GTKs are negotiated
998 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400999 * The spec seems to expect that one negotiates the same key with
1000 * every station but there's no such requirement; VLANs could be
1001 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -04001002 */
Johannes Berg571ecf62007-07-27 15:43:22 +02001003
Johannes Berg3017b802007-08-28 17:01:53 -04001004 /*
Johannes Berg1990af82007-09-26 17:53:15 +02001005 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -04001006 * addressed to us nor a multicast frame.
1007 */
Johannes Berg554891e2010-09-24 12:38:25 +02001008 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001009 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -04001010
Johannes Berg2569a822009-11-25 17:46:17 +01001011 /* start without a key */
1012 rx->key = NULL;
1013
Johannes Bergd4e46a32007-09-14 11:10:24 -04001014 if (rx->sta)
Johannes Berge31b8212010-10-05 19:39:30 +02001015 sta_ptk = rcu_dereference(rx->sta->ptk);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001016
Johannes Berg761ab472010-05-28 14:24:19 +02001017 fc = hdr->frame_control;
1018
1019 if (!ieee80211_has_protected(fc))
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001020 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1021
Johannes Berge31b8212010-10-05 19:39:30 +02001022 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1023 rx->key = sta_ptk;
Johannes Bergdc1580d2010-08-10 09:46:40 +02001024 if ((status->flag & RX_FLAG_DECRYPTED) &&
1025 (status->flag & RX_FLAG_IV_STRIPPED))
1026 return RX_CONTINUE;
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001027 /* Skip decryption if the frame is not protected. */
Johannes Berg761ab472010-05-28 14:24:19 +02001028 if (!ieee80211_has_protected(fc))
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001029 return RX_CONTINUE;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001030 } else if (mmie_keyidx >= 0) {
1031 /* Broadcast/multicast robust management frame / BIP */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001032 if ((status->flag & RX_FLAG_DECRYPTED) &&
1033 (status->flag & RX_FLAG_IV_STRIPPED))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001034 return RX_CONTINUE;
1035
1036 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1037 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1038 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
Johannes Berge31b8212010-10-05 19:39:30 +02001039 if (rx->sta)
1040 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1041 if (!rx->key)
1042 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
Johannes Berg761ab472010-05-28 14:24:19 +02001043 } else if (!ieee80211_has_protected(fc)) {
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001044 /*
1045 * The frame was not protected, so skip decryption. However, we
1046 * need to set rx->key if there is a key that could have been
1047 * used so that the frame may be dropped if encryption would
1048 * have been expected.
1049 */
1050 struct ieee80211_key *key = NULL;
Johannes Berg897bed82010-12-09 19:49:00 +01001051 struct ieee80211_sub_if_data *sdata = rx->sdata;
1052 int i;
1053
Johannes Berg761ab472010-05-28 14:24:19 +02001054 if (ieee80211_is_mgmt(fc) &&
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001055 is_multicast_ether_addr(hdr->addr1) &&
1056 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1057 rx->key = key;
Johannes Berg897bed82010-12-09 19:49:00 +01001058 else {
1059 if (rx->sta) {
1060 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1061 key = rcu_dereference(rx->sta->gtk[i]);
1062 if (key)
1063 break;
1064 }
1065 }
1066 if (!key) {
1067 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1068 key = rcu_dereference(sdata->keys[i]);
1069 if (key)
1070 break;
1071 }
1072 }
1073 if (key)
1074 rx->key = key;
1075 }
Jouni Malinen0c7c10c2009-05-08 12:34:10 +03001076 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001077 } else {
Zhu Yi39184b12010-04-08 15:35:10 +08001078 u8 keyid;
Johannes Berg3017b802007-08-28 17:01:53 -04001079 /*
1080 * The device doesn't give us the IV so we won't be
1081 * able to look up the key. That's ok though, we
1082 * don't need to decrypt the frame, we just won't
1083 * be able to keep statistics accurate.
1084 * Except for key threshold notifications, should
1085 * we somehow allow the driver to tell us which key
1086 * the hardware used if this flag is set?
1087 */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001088 if ((status->flag & RX_FLAG_DECRYPTED) &&
1089 (status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001090 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001091
Johannes Berg761ab472010-05-28 14:24:19 +02001092 hdrlen = ieee80211_hdrlen(fc);
Johannes Berg571ecf62007-07-27 15:43:22 +02001093
Johannes Berg3017b802007-08-28 17:01:53 -04001094 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001095 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +02001096
Johannes Berg3017b802007-08-28 17:01:53 -04001097 /*
1098 * no need to call ieee80211_wep_get_keyidx,
1099 * it verifies a bunch of things we've done already
1100 */
Zhu Yi39184b12010-04-08 15:35:10 +08001101 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1102 keyidx = keyid >> 6;
Johannes Berg3017b802007-08-28 17:01:53 -04001103
Johannes Berge31b8212010-10-05 19:39:30 +02001104 /* check per-station GTK first, if multicast packet */
1105 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1106 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -04001107
Johannes Berge31b8212010-10-05 19:39:30 +02001108 /* if not found, try default key */
1109 if (!rx->key) {
1110 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1111
1112 /*
1113 * RSNA-protected unicast frames should always be
1114 * sent with pairwise or station-to-station keys,
1115 * but for WEP we allow using a key index as well.
1116 */
1117 if (rx->key &&
1118 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1119 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1120 !is_multicast_ether_addr(hdr->addr1))
1121 rx->key = NULL;
1122 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001123 }
1124
Johannes Berg3017b802007-08-28 17:01:53 -04001125 if (rx->key) {
Johannes Berg95acac62011-07-12 12:30:59 +02001126 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1127 return RX_DROP_MONITOR;
1128
Johannes Berg571ecf62007-07-27 15:43:22 +02001129 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -04001130 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +02001131 } else {
Johannes Berge4c26ad2008-01-31 19:48:21 +01001132 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +02001133 }
1134
Johannes Berg97359d12010-08-10 09:46:38 +02001135 switch (rx->key->conf.cipher) {
1136 case WLAN_CIPHER_SUITE_WEP40:
1137 case WLAN_CIPHER_SUITE_WEP104:
Mattias Nisslere2f036d2007-10-07 16:35:31 +02001138 result = ieee80211_crypto_wep_decrypt(rx);
1139 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001140 case WLAN_CIPHER_SUITE_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +02001141 result = ieee80211_crypto_tkip_decrypt(rx);
1142 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001143 case WLAN_CIPHER_SUITE_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +02001144 result = ieee80211_crypto_ccmp_decrypt(rx);
1145 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001146 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001147 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1148 break;
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001149 default:
1150 /*
1151 * We can reach here only with HW-only algorithms
1152 * but why didn't it decrypt the frame?!
1153 */
1154 return RX_DROP_UNUSABLE;
Johannes Berg70f08762007-09-26 17:53:14 +02001155 }
1156
Johannes Berga8286912012-03-12 13:49:14 +01001157 /* the hdr variable is invalid after the decrypt handlers */
1158
Mattias Nisslere2f036d2007-10-07 16:35:31 +02001159 /* either the frame has been decrypted or will be dropped */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001160 status->flag |= RX_FLAG_DECRYPTED;
Mattias Nisslere2f036d2007-10-07 16:35:31 +02001161
1162 return result;
Johannes Berg70f08762007-09-26 17:53:14 +02001163}
1164
Kalle Valo572e0012009-02-10 17:09:31 +02001165static ieee80211_rx_result debug_noinline
1166ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1167{
1168 struct ieee80211_local *local;
1169 struct ieee80211_hdr *hdr;
1170 struct sk_buff *skb;
1171
1172 local = rx->local;
1173 skb = rx->skb;
1174 hdr = (struct ieee80211_hdr *) skb->data;
1175
1176 if (!local->pspolling)
1177 return RX_CONTINUE;
1178
1179 if (!ieee80211_has_fromds(hdr->frame_control))
1180 /* this is not from AP */
1181 return RX_CONTINUE;
1182
1183 if (!ieee80211_is_data(hdr->frame_control))
1184 return RX_CONTINUE;
1185
1186 if (!ieee80211_has_moredata(hdr->frame_control)) {
1187 /* AP has no more frames buffered for us */
1188 local->pspolling = false;
1189 return RX_CONTINUE;
1190 }
1191
1192 /* more data bit is set, let's request a new frame from the AP */
1193 ieee80211_send_pspoll(local, rx->sdata);
1194
1195 return RX_CONTINUE;
1196}
1197
Marco Porschd012a602012-10-10 12:39:50 -07001198static void sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001199{
Johannes Berg133b8222008-09-16 14:18:59 +02001200 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001201 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -07001202 struct ps_data *ps;
Joe Perches0795af52007-10-03 17:59:30 -07001203
Marco Porschd012a602012-10-10 12:39:50 -07001204 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1205 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1206 ps = &sdata->bss->ps;
1207 else
1208 return;
1209
1210 atomic_inc(&ps->num_sta_ps);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001211 set_sta_flag(sta, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001212 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1213 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001214 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1215 sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +02001216}
1217
Marco Porschd012a602012-10-10 12:39:50 -07001218static void sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001219{
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001220 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1221 sta->sta.addr, sta->sta.aid);
Johannes Berg004c8722008-02-20 11:21:35 +01001222
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001223 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001224 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1225 sta->sta.addr, sta->sta.aid);
Johannes Bergaf818582009-11-06 11:35:50 +01001226 return;
1227 }
1228
1229 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001230}
1231
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001232int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
1233{
1234 struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
1235 bool in_ps;
1236
1237 WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
1238
1239 /* Don't let the same PS state be set twice */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001240 in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001241 if ((start && in_ps) || (!start && !in_ps))
1242 return -EINVAL;
1243
1244 if (start)
Marco Porschd012a602012-10-10 12:39:50 -07001245 sta_ps_start(sta_inf);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001246 else
Marco Porschd012a602012-10-10 12:39:50 -07001247 sta_ps_end(sta_inf);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001248
1249 return 0;
1250}
1251EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1252
Johannes Berg49461622008-06-30 15:10:45 +02001253static ieee80211_rx_result debug_noinline
Johannes Berg47086fc2011-09-29 16:04:33 +02001254ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1255{
1256 struct ieee80211_sub_if_data *sdata = rx->sdata;
1257 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1258 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1259 int tid, ac;
1260
1261 if (!rx->sta || !(status->rx_flags & IEEE80211_RX_RA_MATCH))
1262 return RX_CONTINUE;
1263
1264 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1265 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1266 return RX_CONTINUE;
1267
1268 /*
1269 * The device handles station powersave, so don't do anything about
1270 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1271 * it to mac80211 since they're handled.)
1272 */
1273 if (sdata->local->hw.flags & IEEE80211_HW_AP_LINK_PS)
1274 return RX_CONTINUE;
1275
1276 /*
1277 * Don't do anything if the station isn't already asleep. In
1278 * the uAPSD case, the station will probably be marked asleep,
1279 * in the PS-Poll case the station must be confused ...
1280 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001281 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
Johannes Berg47086fc2011-09-29 16:04:33 +02001282 return RX_CONTINUE;
1283
1284 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001285 if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
1286 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
Johannes Bergdeeaee12011-09-29 16:04:35 +02001287 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1288 else
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001289 set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001290 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001291
1292 /* Free PS Poll skb here instead of returning RX_DROP that would
1293 * count as an dropped frame. */
1294 dev_kfree_skb(rx->skb);
1295
1296 return RX_QUEUED;
1297 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1298 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1299 ieee80211_has_pm(hdr->frame_control) &&
1300 (ieee80211_is_data_qos(hdr->frame_control) ||
1301 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1302 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1303 ac = ieee802_1d_to_ac[tid & 7];
1304
1305 /*
1306 * If this AC is not trigger-enabled do nothing.
1307 *
1308 * NB: This could/should check a separate bitmap of trigger-
1309 * enabled queues, but for now we only implement uAPSD w/o
1310 * TSPEC changes to the ACs, so they're always the same.
1311 */
1312 if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
1313 return RX_CONTINUE;
1314
1315 /* if we are in a service period, do nothing */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001316 if (test_sta_flag(rx->sta, WLAN_STA_SP))
Johannes Berg47086fc2011-09-29 16:04:33 +02001317 return RX_CONTINUE;
1318
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001319 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
Johannes Berg47086fc2011-09-29 16:04:33 +02001320 ieee80211_sta_ps_deliver_uapsd(rx->sta);
1321 else
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001322 set_sta_flag(rx->sta, WLAN_STA_UAPSD);
Johannes Berg47086fc2011-09-29 16:04:33 +02001323 }
1324
1325 return RX_CONTINUE;
1326}
1327
1328static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001329ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001330{
1331 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001332 struct sk_buff *skb = rx->skb;
1333 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1334 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001335
1336 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001337 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001338
Johannes Bergb291ba12009-07-10 15:29:03 +02001339 /*
1340 * Update last_rx only for IBSS packets which are for the current
1341 * BSSID to avoid keeping the current IBSS network alive in cases
1342 * where other STAs start using different BSSID.
1343 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001344 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001345 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001346 NL80211_IFTYPE_ADHOC);
Joe Perchesb203ca32012-05-08 18:56:52 +00001347 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001348 sta->last_rx = jiffies;
Felix Fietkau3af63342011-02-27 22:08:01 +01001349 if (ieee80211_is_data(hdr->frame_control)) {
1350 sta->last_rx_rate_idx = status->rate_idx;
1351 sta->last_rx_rate_flag = status->flag;
1352 }
1353 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001354 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1355 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001356 * Mesh beacons will update last_rx when if they are found to
1357 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001358 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001359 sta->last_rx = jiffies;
Felix Fietkau3af63342011-02-27 22:08:01 +01001360 if (ieee80211_is_data(hdr->frame_control)) {
1361 sta->last_rx_rate_idx = status->rate_idx;
1362 sta->last_rx_rate_flag = status->flag;
1363 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001364 }
1365
Johannes Berg554891e2010-09-24 12:38:25 +02001366 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001367 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001368
Kalle Valo3cf335d2009-03-22 21:57:06 +02001369 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1370 ieee80211_sta_rx_notify(rx->sdata, hdr);
1371
Johannes Berg571ecf62007-07-27 15:43:22 +02001372 sta->rx_fragments++;
1373 sta->rx_bytes += rx->skb->len;
Felix Fietkaufe8431f2012-03-01 18:00:07 +01001374 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1375 sta->last_signal = status->signal;
1376 ewma_add(&sta->avg_signal, -status->signal);
1377 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001378
Johannes Berg72eaa432008-11-26 15:02:58 +01001379 /*
1380 * Change STA power saving mode only at the end of a frame
1381 * exchange sequence.
1382 */
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001383 if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
1384 !ieee80211_has_morefrags(hdr->frame_control) &&
Christian Lamparter4cfda472010-12-27 23:21:26 +01001385 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001386 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1387 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001388 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
Johannes Berg72eaa432008-11-26 15:02:58 +01001389 /*
1390 * Ignore doze->wake transitions that are
1391 * indicated by non-data frames, the standard
1392 * is unclear here, but for example going to
1393 * PS mode and then scanning would cause a
1394 * doze->wake transition for the probe request,
1395 * and that is clearly undesirable.
1396 */
1397 if (ieee80211_is_data(hdr->frame_control) &&
1398 !ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001399 sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001400 } else {
1401 if (ieee80211_has_pm(hdr->frame_control))
Marco Porschd012a602012-10-10 12:39:50 -07001402 sta_ps_start(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001403 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001404 }
1405
Johannes Berg22403de2009-10-30 12:55:03 +01001406 /*
1407 * Drop (qos-)data::nullfunc frames silently, since they
1408 * are used only to control station power saving mode.
1409 */
1410 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1411 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001412 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Felix Fietkaud5242152010-01-08 18:06:26 +01001413
1414 /*
1415 * If we receive a 4-addr nullfunc frame from a STA
Johannes Berge7f4a942011-11-04 11:18:20 +01001416 * that was not moved to a 4-addr STA vlan yet send
1417 * the event to userspace and for older hostapd drop
1418 * the frame to the monitor interface.
Felix Fietkaud5242152010-01-08 18:06:26 +01001419 */
1420 if (ieee80211_has_a4(hdr->frame_control) &&
1421 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1422 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
Johannes Berge7f4a942011-11-04 11:18:20 +01001423 !rx->sdata->u.vlan.sta))) {
1424 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1425 cfg80211_rx_unexpected_4addr_frame(
1426 rx->sdata->dev, sta->sta.addr,
1427 GFP_ATOMIC);
Felix Fietkaud5242152010-01-08 18:06:26 +01001428 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01001429 }
Johannes Berg22403de2009-10-30 12:55:03 +01001430 /*
1431 * Update counter and free packet here to avoid
1432 * counting this as a dropped packed.
1433 */
Johannes Berg571ecf62007-07-27 15:43:22 +02001434 sta->rx_packets++;
1435 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001436 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001437 }
1438
Johannes Berg9ae54c82008-01-31 19:48:20 +01001439 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001440} /* ieee80211_rx_h_sta_process */
1441
Johannes Berg571ecf62007-07-27 15:43:22 +02001442static inline struct ieee80211_fragment_entry *
1443ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1444 unsigned int frag, unsigned int seq, int rx_queue,
1445 struct sk_buff **skb)
1446{
1447 struct ieee80211_fragment_entry *entry;
Johannes Berg571ecf62007-07-27 15:43:22 +02001448
Johannes Berg571ecf62007-07-27 15:43:22 +02001449 entry = &sdata->fragments[sdata->fragment_next++];
1450 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1451 sdata->fragment_next = 0;
1452
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001453 if (!skb_queue_empty(&entry->skb_list))
Johannes Berg571ecf62007-07-27 15:43:22 +02001454 __skb_queue_purge(&entry->skb_list);
Johannes Berg571ecf62007-07-27 15:43:22 +02001455
1456 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1457 *skb = NULL;
1458 entry->first_frag_time = jiffies;
1459 entry->seq = seq;
1460 entry->rx_queue = rx_queue;
1461 entry->last_frag = frag;
1462 entry->ccmp = 0;
1463 entry->extra_len = 0;
1464
1465 return entry;
1466}
1467
1468static inline struct ieee80211_fragment_entry *
1469ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001470 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02001471 int rx_queue, struct ieee80211_hdr *hdr)
1472{
1473 struct ieee80211_fragment_entry *entry;
1474 int i, idx;
1475
1476 idx = sdata->fragment_next;
1477 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1478 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +02001479
1480 idx--;
1481 if (idx < 0)
1482 idx = IEEE80211_FRAGMENT_MAX - 1;
1483
1484 entry = &sdata->fragments[idx];
1485 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1486 entry->rx_queue != rx_queue ||
1487 entry->last_frag + 1 != frag)
1488 continue;
1489
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001490 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001491
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001492 /*
1493 * Check ftype and addresses are equal, else check next fragment
1494 */
1495 if (((hdr->frame_control ^ f_hdr->frame_control) &
1496 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001497 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1498 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
Johannes Berg571ecf62007-07-27 15:43:22 +02001499 continue;
1500
S.Çağlar Onurab466232008-02-14 17:36:47 +02001501 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001502 __skb_queue_purge(&entry->skb_list);
1503 continue;
1504 }
1505 return entry;
1506 }
1507
1508 return NULL;
1509}
1510
Johannes Berg49461622008-06-30 15:10:45 +02001511static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001512ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001513{
1514 struct ieee80211_hdr *hdr;
1515 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001516 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001517 unsigned int frag, seq;
1518 struct ieee80211_fragment_entry *entry;
1519 struct sk_buff *skb;
Johannes Berg554891e2010-09-24 12:38:25 +02001520 struct ieee80211_rx_status *status;
Johannes Berg571ecf62007-07-27 15:43:22 +02001521
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001522 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001523 fc = hdr->frame_control;
Javier Cardonaf7fbf702012-10-25 11:10:18 -07001524
1525 if (ieee80211_is_ctl(fc))
1526 return RX_CONTINUE;
1527
Johannes Berg571ecf62007-07-27 15:43:22 +02001528 sc = le16_to_cpu(hdr->seq_ctrl);
1529 frag = sc & IEEE80211_SCTL_FRAG;
1530
Harvey Harrison358c8d92008-07-15 18:44:13 -07001531 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
Johannes Berg571ecf62007-07-27 15:43:22 +02001532 is_multicast_ether_addr(hdr->addr1))) {
1533 /* not fragmented */
1534 goto out;
1535 }
1536 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1537
Zhu Yie3cf8b32010-03-29 17:35:07 +08001538 if (skb_linearize(rx->skb))
1539 return RX_DROP_UNUSABLE;
1540
Abhijeet Kolekar058897a2010-05-11 11:22:11 -07001541 /*
1542 * skb_linearize() might change the skb->data and
1543 * previously cached variables (in this case, hdr) need to
1544 * be refreshed with the new data.
1545 */
1546 hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001547 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1548
1549 if (frag == 0) {
1550 /* This is the first fragment of a new frame. */
1551 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg9e262972011-07-07 18:45:03 +02001552 rx->seqno_idx, &(rx->skb));
Johannes Berg97359d12010-08-10 09:46:38 +02001553 if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001554 ieee80211_has_protected(fc)) {
Johannes Berg9e262972011-07-07 18:45:03 +02001555 int queue = rx->security_idx;
Johannes Berg571ecf62007-07-27 15:43:22 +02001556 /* Store CCMP PN so that we can verify that the next
1557 * fragment has a sequential PN value. */
1558 entry->ccmp = 1;
1559 memcpy(entry->last_pn,
Jouni Malinen91902522010-06-11 10:27:33 -07001560 rx->key->u.ccmp.rx_pn[queue],
Johannes Berg571ecf62007-07-27 15:43:22 +02001561 CCMP_PN_LEN);
1562 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001563 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001564 }
1565
1566 /* This is a fragment for a frame that should already be pending in
1567 * fragment cache. Add this fragment to the end of the pending entry.
1568 */
Johannes Berg9e262972011-07-07 18:45:03 +02001569 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1570 rx->seqno_idx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001571 if (!entry) {
1572 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001573 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001574 }
1575
1576 /* Verify that MPDUs within one MSDU have sequential PN values.
1577 * (IEEE 802.11i, 8.3.3.4.5) */
1578 if (entry->ccmp) {
1579 int i;
1580 u8 pn[CCMP_PN_LEN], *rpn;
Jouni Malinen91902522010-06-11 10:27:33 -07001581 int queue;
Johannes Berg97359d12010-08-10 09:46:38 +02001582 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001583 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001584 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1585 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1586 pn[i]++;
1587 if (pn[i])
1588 break;
1589 }
Johannes Berg9e262972011-07-07 18:45:03 +02001590 queue = rx->security_idx;
Jouni Malinen91902522010-06-11 10:27:33 -07001591 rpn = rx->key->u.ccmp.rx_pn[queue];
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001592 if (memcmp(pn, rpn, CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001593 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001594 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1595 }
1596
Harvey Harrison358c8d92008-07-15 18:44:13 -07001597 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02001598 __skb_queue_tail(&entry->skb_list, rx->skb);
1599 entry->last_frag = frag;
1600 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001601 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001602 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001603 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001604 }
1605
1606 rx->skb = __skb_dequeue(&entry->skb_list);
1607 if (skb_tailroom(rx->skb) < entry->extra_len) {
1608 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1609 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1610 GFP_ATOMIC))) {
1611 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1612 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001613 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001614 }
1615 }
1616 while ((skb = __skb_dequeue(&entry->skb_list))) {
1617 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1618 dev_kfree_skb(skb);
1619 }
1620
1621 /* Complete frame has been reassembled - process it now */
Johannes Berg554891e2010-09-24 12:38:25 +02001622 status = IEEE80211_SKB_RXCB(rx->skb);
1623 status->rx_flags |= IEEE80211_RX_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001624
1625 out:
1626 if (rx->sta)
1627 rx->sta->rx_packets++;
1628 if (is_multicast_ether_addr(hdr->addr1))
1629 rx->local->dot11MulticastReceivedFrameCount++;
1630 else
1631 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001632 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001633}
1634
Johannes Berg1df332e2012-10-26 00:09:11 +02001635static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001636{
Johannes Berg1df332e2012-10-26 00:09:11 +02001637 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001638 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001639
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001640 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001641}
1642
Johannes Berg1df332e2012-10-26 00:09:11 +02001643static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02001644{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001645 struct sk_buff *skb = rx->skb;
1646 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1647
Johannes Berg3017b802007-08-28 17:01:53 -04001648 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001649 * Pass through unencrypted frames if the hardware has
1650 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001651 */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001652 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001653 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001654
1655 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001656 if (unlikely(!ieee80211_has_protected(fc) &&
1657 !ieee80211_is_nullfunc(fc) &&
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001658 ieee80211_is_data(fc) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001659 (rx->key || rx->sdata->drop_unencrypted)))
1660 return -EACCES;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01001661
1662 return 0;
1663}
1664
Johannes Berg1df332e2012-10-26 00:09:11 +02001665static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
Johannes Bergbef5d1c2010-02-16 11:05:00 +01001666{
1667 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Jouni Malinene3efca02010-03-28 22:31:15 -07001668 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergbef5d1c2010-02-16 11:05:00 +01001669 __le16 fc = hdr->frame_control;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01001670
Jouni Malinene3efca02010-03-28 22:31:15 -07001671 /*
1672 * Pass through unencrypted frames if the hardware has
1673 * decrypted them already.
1674 */
1675 if (status->flag & RX_FLAG_DECRYPTED)
1676 return 0;
Johannes Bergbef5d1c2010-02-16 11:05:00 +01001677
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001678 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
Jouni Malinend211e902010-03-28 22:29:52 -07001679 if (unlikely(!ieee80211_has_protected(fc) &&
1680 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02001681 rx->key)) {
1682 if (ieee80211_is_deauth(fc))
1683 cfg80211_send_unprot_deauth(rx->sdata->dev,
1684 rx->skb->data,
1685 rx->skb->len);
1686 else if (ieee80211_is_disassoc(fc))
1687 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1688 rx->skb->data,
1689 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001690 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02001691 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001692 /* BIP does not use Protected field, so need to check MMIE */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001693 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
Jouni Malinencf4e5942010-12-16 00:52:40 +02001694 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
1695 if (ieee80211_is_deauth(fc))
1696 cfg80211_send_unprot_deauth(rx->sdata->dev,
1697 rx->skb->data,
1698 rx->skb->len);
1699 else if (ieee80211_is_disassoc(fc))
1700 cfg80211_send_unprot_disassoc(rx->sdata->dev,
1701 rx->skb->data,
1702 rx->skb->len);
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001703 return -EACCES;
Jouni Malinencf4e5942010-12-16 00:52:40 +02001704 }
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001705 /*
1706 * When using MFP, Action frames are not allowed prior to
1707 * having configured keys.
1708 */
1709 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1710 ieee80211_is_robust_mgmt_frame(
1711 (struct ieee80211_hdr *) rx->skb->data)))
1712 return -EACCES;
1713 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02001714
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001715 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001716}
1717
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001718static int
Felix Fietkau4114fa22011-04-12 19:15:22 +02001719__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
Johannes Berg571ecf62007-07-27 15:43:22 +02001720{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001721 struct ieee80211_sub_if_data *sdata = rx->sdata;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001722 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001723 bool check_port_control = false;
1724 struct ethhdr *ehdr;
1725 int ret;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001726
Felix Fietkau4114fa22011-04-12 19:15:22 +02001727 *port_control = false;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001728 if (ieee80211_has_a4(hdr->frame_control) &&
1729 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001730 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001731
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001732 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1733 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
1734
1735 if (!sdata->u.mgd.use_4addr)
1736 return -1;
1737 else
1738 check_port_control = true;
1739 }
1740
Johannes Berg9bc383d2009-11-19 11:55:19 +01001741 if (is_multicast_ether_addr(hdr->addr1) &&
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001742 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001743 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001744
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001745 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
Felix Fietkau4114fa22011-04-12 19:15:22 +02001746 if (ret < 0)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001747 return ret;
1748
1749 ehdr = (struct ethhdr *) rx->skb->data;
Felix Fietkau4114fa22011-04-12 19:15:22 +02001750 if (ehdr->h_proto == rx->sdata->control_port_protocol)
1751 *port_control = true;
1752 else if (check_port_control)
Felix Fietkaufbb327c2011-01-18 15:48:48 +01001753 return -1;
1754
1755 return 0;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001756}
Johannes Berg571ecf62007-07-27 15:43:22 +02001757
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001758/*
1759 * requires that rx->skb is a frame with ethernet header
1760 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001761static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001762{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301763 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001764 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1765 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1766
1767 /*
1768 * Allow EAPOL frames to us/the PAE group address regardless
1769 * of whether the frame was encrypted or not.
1770 */
Johannes Berga621fa42010-08-27 14:26:54 +03001771 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
Joe Perches3bc79452012-05-08 18:56:53 +00001772 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
1773 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001774 return true;
1775
1776 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07001777 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001778 return false;
1779
1780 return true;
1781}
1782
1783/*
1784 * requires that rx->skb is a frame with ethernet header
1785 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001786static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01001787ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001788{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001789 struct ieee80211_sub_if_data *sdata = rx->sdata;
1790 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001791 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001792 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1793 struct sta_info *dsta;
Johannes Berg554891e2010-09-24 12:38:25 +02001794 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001795
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001796 skb = rx->skb;
1797 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001798
Johannes Berg05c914f2008-09-11 00:01:58 +02001799 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1800 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02001801 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg554891e2010-09-24 12:38:25 +02001802 (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01001803 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001804 if (is_multicast_ether_addr(ehdr->h_dest)) {
1805 /*
1806 * send multicast frames both to higher layers in
1807 * local net stack and back to the wireless medium
1808 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001809 xmit_skb = skb_copy(skb, GFP_ATOMIC);
Joe Perchese87cc472012-05-13 21:56:26 +00001810 if (!xmit_skb)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001811 net_info_ratelimited("%s: failed to clone multicast frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00001812 dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001813 } else {
Johannes Bergabe60632009-11-25 17:46:18 +01001814 dsta = sta_info_get(sdata, skb->data);
1815 if (dsta) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001816 /*
1817 * The destination station is associated to
1818 * this AP (in this VLAN), so send the frame
1819 * directly to it and do not pass it to local
1820 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001821 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001822 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001823 skb = NULL;
1824 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001825 }
1826 }
1827
1828 if (skb) {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001829 int align __maybe_unused;
1830
Kalle Valo59d9cb02009-12-17 13:54:57 +01001831#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Johannes Bergd1c3a372009-01-07 00:26:10 +01001832 /*
1833 * 'align' will only take the values 0 or 2 here
1834 * since all frames are required to be aligned
1835 * to 2-byte boundaries when being passed to
1836 * mac80211. That also explains the __skb_push()
1837 * below.
1838 */
matthieu castetdacb6f12009-06-04 22:16:18 +02001839 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01001840 if (align) {
1841 if (WARN_ON(skb_headroom(skb) < 3)) {
1842 dev_kfree_skb(skb);
1843 skb = NULL;
1844 } else {
1845 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07001846 size_t len = skb_headlen(skb);
1847 skb->data -= align;
1848 memmove(skb->data, data, len);
1849 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01001850 }
1851 }
1852#endif
1853
1854 if (skb) {
1855 /* deliver to local stack */
1856 skb->protocol = eth_type_trans(skb, dev);
1857 memset(skb->cb, 0, sizeof(skb->cb));
John W. Linville5548a8a2010-06-24 14:25:56 -04001858 netif_receive_skb(skb);
Johannes Bergd1c3a372009-01-07 00:26:10 +01001859 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001860 }
1861
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001862 if (xmit_skb) {
Helmut Schaaaef6c922011-12-21 09:11:35 +01001863 /*
1864 * Send to wireless media and increase priority by 256 to
1865 * keep the received priority instead of reclassifying
1866 * the frame (see cfg80211_classify8021d).
1867 */
1868 xmit_skb->priority += 256;
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001869 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001870 skb_reset_network_header(xmit_skb);
1871 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001872 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001873 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001874}
1875
Johannes Berg49461622008-06-30 15:10:45 +02001876static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001877ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001878{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001879 struct net_device *dev = rx->sdata->dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08001880 struct sk_buff *skb = rx->skb;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001881 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1882 __le16 fc = hdr->frame_control;
Zhu Yieaf85ca2009-12-01 10:18:37 +08001883 struct sk_buff_head frame_list;
Johannes Berg554891e2010-09-24 12:38:25 +02001884 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001885
Harvey Harrison358c8d92008-07-15 18:44:13 -07001886 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001887 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001888
Harvey Harrison358c8d92008-07-15 18:44:13 -07001889 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001890 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001891
Johannes Berg554891e2010-09-24 12:38:25 +02001892 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001893 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001894
Zhu Yieaf85ca2009-12-01 10:18:37 +08001895 if (ieee80211_has_a4(hdr->frame_control) &&
1896 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1897 !rx->sdata->u.vlan.sta)
1898 return RX_DROP_UNUSABLE;
1899
1900 if (is_multicast_ether_addr(hdr->addr1) &&
1901 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1902 rx->sdata->u.vlan.sta) ||
1903 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1904 rx->sdata->u.mgd.use_4addr)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001905 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001906
1907 skb->dev = dev;
Zhu Yieaf85ca2009-12-01 10:18:37 +08001908 __skb_queue_head_init(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001909
Zhu Yie3cf8b32010-03-29 17:35:07 +08001910 if (skb_linearize(skb))
1911 return RX_DROP_UNUSABLE;
1912
Zhu Yieaf85ca2009-12-01 10:18:37 +08001913 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
1914 rx->sdata->vif.type,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -07001915 rx->local->hw.extra_tx_headroom, true);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001916
Zhu Yieaf85ca2009-12-01 10:18:37 +08001917 while (!skb_queue_empty(&frame_list)) {
1918 rx->skb = __skb_dequeue(&frame_list);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001919
Harvey Harrison358c8d92008-07-15 18:44:13 -07001920 if (!ieee80211_frame_allowed(rx, fc)) {
Zhu Yieaf85ca2009-12-01 10:18:37 +08001921 dev_kfree_skb(rx->skb);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001922 continue;
1923 }
Zhu Yieaf85ca2009-12-01 10:18:37 +08001924 dev->stats.rx_packets++;
1925 dev->stats.rx_bytes += rx->skb->len;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001926
1927 ieee80211_deliver_skb(rx);
1928 }
1929
Johannes Berg9ae54c82008-01-31 19:48:20 +01001930 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001931}
1932
Ingo Molnarbf94e172008-10-12 23:51:38 -07001933#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02001934static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001935ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1936{
Thomas Pedersen30789eb2011-11-24 17:15:26 -08001937 struct ieee80211_hdr *fwd_hdr, *hdr;
1938 struct ieee80211_tx_info *info;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001939 struct ieee80211s_hdr *mesh_hdr;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001940 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001941 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001942 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02001943 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08001944 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Thomas Pedersen0cfda852011-11-24 17:15:25 -08001945 __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08001946 u16 q, hdrlen;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001947
1948 hdr = (struct ieee80211_hdr *) skb->data;
1949 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg9b395bc2012-10-26 00:36:40 +02001950
1951 /* make sure fixed part of mesh header is there, also checks skb len */
1952 if (!pskb_may_pull(rx->skb, hdrlen + 6))
1953 return RX_DROP_MONITOR;
1954
1955 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1956
1957 /* make sure full mesh header is there, also checks skb len */
1958 if (!pskb_may_pull(rx->skb,
1959 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
1960 return RX_DROP_MONITOR;
1961
1962 /* reload pointers */
1963 hdr = (struct ieee80211_hdr *) skb->data;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001964 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1965
Thomas Pedersen2157fdd2011-09-01 12:32:14 -07001966 /* frame is in RMC, don't forward */
1967 if (ieee80211_is_data(hdr->frame_control) &&
1968 is_multicast_ether_addr(hdr->addr1) &&
1969 mesh_rmc_check(hdr->addr3, mesh_hdr, rx->sdata))
1970 return RX_DROP_MONITOR;
1971
Javier Cardona555cb712012-10-24 12:43:30 -07001972 if (!ieee80211_is_data(hdr->frame_control) ||
1973 !(status->rx_flags & IEEE80211_RX_RA_MATCH))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001974 return RX_CONTINUE;
1975
1976 if (!mesh_hdr->ttl)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001977 return RX_DROP_MONITOR;
1978
Javier Cardona43b7b312009-10-15 18:10:51 -07001979 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08001980 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07001981 char *proxied_addr;
1982 char *mpp_addr;
1983
1984 if (is_multicast_ether_addr(hdr->addr1)) {
1985 mpp_addr = hdr->addr3;
1986 proxied_addr = mesh_hdr->eaddr1;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001987 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
1988 /* has_a4 already checked in ieee80211_rx_mesh_check */
Javier Cardona43b7b312009-10-15 18:10:51 -07001989 mpp_addr = hdr->addr4;
1990 proxied_addr = mesh_hdr->eaddr2;
Johannes Berg9b395bc2012-10-26 00:36:40 +02001991 } else {
1992 return RX_DROP_MONITOR;
Javier Cardona43b7b312009-10-15 18:10:51 -07001993 }
YanBo79617de2008-09-22 13:30:32 +08001994
YanBo79617de2008-09-22 13:30:32 +08001995 rcu_read_lock();
Javier Cardona43b7b312009-10-15 18:10:51 -07001996 mppath = mpp_path_lookup(proxied_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001997 if (!mppath) {
Javier Cardona43b7b312009-10-15 18:10:51 -07001998 mpp_path_add(proxied_addr, mpp_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001999 } else {
2000 spin_lock_bh(&mppath->state_lock);
Joe Perchesb203ca32012-05-08 18:56:52 +00002001 if (!ether_addr_equal(mppath->mpp, mpp_addr))
Javier Cardona43b7b312009-10-15 18:10:51 -07002002 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002003 spin_unlock_bh(&mppath->state_lock);
2004 }
2005 rcu_read_unlock();
2006 }
2007
Javier Cardona3c5772a2009-08-10 12:15:48 -07002008 /* Frame has reached destination. Don't forward */
2009 if (!is_multicast_ether_addr(hdr->addr1) &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002010 ether_addr_equal(sdata->vif.addr, hdr->addr3))
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002011 return RX_CONTINUE;
2012
Yoni Divinsky00e96de2012-06-20 15:39:13 +03002013 q = ieee80211_select_queue_80211(sdata, skb, hdr);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002014 if (ieee80211_queue_stopped(&local->hw, q)) {
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002015 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
Thomas Pedersend3c15972011-11-24 17:15:23 -08002016 return RX_DROP_MONITOR;
2017 }
2018 skb_set_queue_mapping(skb, q);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002019
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002020 if (!--mesh_hdr->ttl) {
2021 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
Javier Cardona2ac64cd2012-10-24 12:43:31 -07002022 goto out;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002023 }
2024
Chun-Yeow Yeohd6655082012-03-02 02:03:19 +08002025 if (!ifmsh->mshcfg.dot11MeshForwarding)
2026 goto out;
2027
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002028 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2029 if (!fwd_skb) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002030 net_info_ratelimited("%s: failed to clone mesh frame\n",
Joe Perchese87cc472012-05-13 21:56:26 +00002031 sdata->name);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002032 goto out;
2033 }
2034
2035 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2036 info = IEEE80211_SKB_CB(fwd_skb);
2037 memset(info, 0, sizeof(*info));
2038 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2039 info->control.vif = &rx->sdata->vif;
2040 info->control.jiffies = jiffies;
2041 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2042 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2043 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2044 } else if (!mesh_nexthop_lookup(fwd_skb, sdata)) {
2045 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2046 } else {
2047 /* unable to resolve next hop */
2048 mesh_path_error_tx(ifmsh->mshcfg.element_ttl, fwd_hdr->addr3,
Johannes Berg1df332e2012-10-26 00:09:11 +02002049 0, reason, fwd_hdr->addr2, sdata);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002050 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
Jesper Juhl74b8cc32012-01-14 21:52:17 +01002051 kfree_skb(fwd_skb);
Thomas Pedersen30789eb2011-11-24 17:15:26 -08002052 return RX_DROP_MONITOR;
2053 }
2054
2055 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2056 ieee80211_add_pending_skb(local, fwd_skb);
Johannes Bergb51aff02010-12-22 10:15:07 +01002057 out:
Javier Cardona3c5772a2009-08-10 12:15:48 -07002058 if (is_multicast_ether_addr(hdr->addr1) ||
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002059 sdata->dev->flags & IFF_PROMISC)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002060 return RX_CONTINUE;
2061 else
2062 return RX_DROP_MONITOR;
2063}
Ingo Molnarbf94e172008-10-12 23:51:38 -07002064#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002065
2066static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002067ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002068{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002069 struct ieee80211_sub_if_data *sdata = rx->sdata;
Vivek Natarajane15276a2010-02-08 17:47:01 +05302070 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002071 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07002072 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2073 __le16 fc = hdr->frame_control;
Felix Fietkau4114fa22011-04-12 19:15:22 +02002074 bool port_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002075 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002076
Harvey Harrison358c8d92008-07-15 18:44:13 -07002077 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002078 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002079
Harvey Harrison358c8d92008-07-15 18:44:13 -07002080 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002081 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002082
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002083 /*
Johannes Berge7f4a942011-11-04 11:18:20 +01002084 * Send unexpected-4addr-frame event to hostapd. For older versions,
2085 * also drop the frame to cooked monitor interfaces.
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002086 */
2087 if (ieee80211_has_a4(hdr->frame_control) &&
Johannes Berge7f4a942011-11-04 11:18:20 +01002088 sdata->vif.type == NL80211_IFTYPE_AP) {
2089 if (rx->sta &&
2090 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2091 cfg80211_rx_unexpected_4addr_frame(
2092 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002093 return RX_DROP_MONITOR;
Johannes Berge7f4a942011-11-04 11:18:20 +01002094 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002095
Felix Fietkau4114fa22011-04-12 19:15:22 +02002096 err = __ieee80211_data_to_8023(rx, &port_control);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002097 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002098 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002099
Harvey Harrison358c8d92008-07-15 18:44:13 -07002100 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002101 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002102
Felix Fietkau4114fa22011-04-12 19:15:22 +02002103 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2104 unlikely(port_control) && sdata->bss) {
2105 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2106 u.ap);
2107 dev = sdata->dev;
2108 rx->sdata = sdata;
2109 }
2110
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002111 rx->skb->dev = dev;
2112
2113 dev->stats.rx_packets++;
2114 dev->stats.rx_bytes += rx->skb->len;
2115
Helmut Schaa08ca9442010-11-30 12:19:34 +01002116 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
Rajkumar Manoharan8c99f692011-02-02 22:57:53 +05302117 !is_multicast_ether_addr(
2118 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2119 (!local->scanning &&
2120 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
Vivek Natarajane15276a2010-02-08 17:47:01 +05302121 mod_timer(&local->dynamic_ps_timer, jiffies +
2122 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2123 }
2124
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02002125 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002126
Johannes Berg9ae54c82008-01-31 19:48:20 +01002127 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002128}
2129
Johannes Berg49461622008-06-30 15:10:45 +02002130static ieee80211_rx_result debug_noinline
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002131ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
Ron Rindjunsky71364712007-12-25 17:00:36 +02002132{
Ron Rindjunsky71364712007-12-25 17:00:36 +02002133 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002134 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002135 struct tid_ampdu_rx *tid_agg_rx;
2136 u16 start_seq_num;
2137 u16 tid;
2138
Harvey Harrisona7767f92008-07-02 16:30:51 -07002139 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002140 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002141
Harvey Harrisona7767f92008-07-02 16:30:51 -07002142 if (ieee80211_is_back_req(bar->frame_control)) {
Johannes Berg8ae59772010-05-30 14:52:58 +02002143 struct {
2144 __le16 control, start_seq_num;
2145 } __packed bar_data;
2146
Ron Rindjunsky71364712007-12-25 17:00:36 +02002147 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002148 return RX_DROP_MONITOR;
Johannes Berg8ae59772010-05-30 14:52:58 +02002149
2150 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2151 &bar_data, sizeof(bar_data)))
2152 return RX_DROP_MONITOR;
2153
Johannes Berg8ae59772010-05-30 14:52:58 +02002154 tid = le16_to_cpu(bar_data.control) >> 12;
Johannes Berga87f7362010-06-10 10:21:38 +02002155
2156 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2157 if (!tid_agg_rx)
Johannes Berga02ae752009-11-16 12:00:40 +01002158 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002159
Johannes Berg8ae59772010-05-30 14:52:58 +02002160 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002161
2162 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002163 if (tid_agg_rx->timeout)
2164 mod_timer(&tid_agg_rx->session_timer,
2165 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02002166
Johannes Bergdd318572010-11-29 11:09:16 +01002167 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Berga02ae752009-11-16 12:00:40 +01002168 /* release stored frames up to start of BAR */
Johannes Bergd3b2fb52012-06-22 12:48:38 +02002169 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2170 start_seq_num);
Johannes Bergdd318572010-11-29 11:09:16 +01002171 spin_unlock(&tid_agg_rx->reorder_lock);
2172
Johannes Berga02ae752009-11-16 12:00:40 +01002173 kfree_skb(skb);
2174 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002175 }
2176
Johannes Berg08daeca2010-05-30 14:53:43 +02002177 /*
2178 * After this point, we only want management frames,
2179 * so we can drop all remaining control frames to
2180 * cooked monitor interfaces.
2181 */
2182 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02002183}
2184
Jouni Malinenf4f727a2009-01-10 11:46:53 +02002185static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2186 struct ieee80211_mgmt *mgmt,
2187 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02002188{
2189 struct ieee80211_local *local = sdata->local;
2190 struct sk_buff *skb;
2191 struct ieee80211_mgmt *resp;
2192
Joe Perchesb203ca32012-05-08 18:56:52 +00002193 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
Jouni Malinenfea14732009-01-08 13:32:06 +02002194 /* Not to own unicast address */
2195 return;
2196 }
2197
Joe Perchesb203ca32012-05-08 18:56:52 +00002198 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2199 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002200 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02002201 return;
2202 }
2203
2204 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2205 /* Too short SA Query request frame */
2206 return;
2207 }
2208
2209 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2210 if (skb == NULL)
2211 return;
2212
2213 skb_reserve(skb, local->hw.extra_tx_headroom);
2214 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2215 memset(resp, 0, 24);
2216 memcpy(resp->da, mgmt->sa, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002217 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002218 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02002219 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2220 IEEE80211_STYPE_ACTION);
2221 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2222 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2223 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2224 memcpy(resp->u.action.u.sa_query.trans_id,
2225 mgmt->u.action.u.sa_query.trans_id,
2226 WLAN_SA_QUERY_TR_ID_LEN);
2227
Johannes Berg62ae67b2009-11-18 18:42:05 +01002228 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02002229}
2230
Johannes Berg49461622008-06-30 15:10:45 +02002231static ieee80211_rx_result debug_noinline
Johannes Berg2e161f72010-08-12 15:38:38 +02002232ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2233{
2234 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002235 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02002236
2237 /*
2238 * From here on, look only at management frames.
2239 * Data and control frames are already handled,
2240 * and unknown (reserved) frames are useless.
2241 */
2242 if (rx->skb->len < 24)
2243 return RX_DROP_MONITOR;
2244
2245 if (!ieee80211_is_mgmt(mgmt->frame_control))
2246 return RX_DROP_MONITOR;
2247
Johannes Bergee971922011-11-04 11:18:18 +01002248 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2249 ieee80211_is_beacon(mgmt->frame_control) &&
2250 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
Johannes Berg804483e2012-03-05 22:18:41 +01002251 int sig = 0;
2252
2253 if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
2254 sig = status->signal;
2255
Johannes Bergee971922011-11-04 11:18:18 +01002256 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2257 rx->skb->data, rx->skb->len,
Ben Greear37c73b52012-10-26 14:49:25 -07002258 status->freq, sig);
Johannes Bergee971922011-11-04 11:18:18 +01002259 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2260 }
2261
Johannes Berg554891e2010-09-24 12:38:25 +02002262 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
Johannes Berg2e161f72010-08-12 15:38:38 +02002263 return RX_DROP_MONITOR;
2264
2265 if (ieee80211_drop_unencrypted_mgmt(rx))
2266 return RX_DROP_UNUSABLE;
2267
2268 return RX_CONTINUE;
2269}
2270
2271static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02002272ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2273{
2274 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002275 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02002276 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg554891e2010-09-24 12:38:25 +02002277 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Bergde1ede72008-09-09 14:42:50 +02002278 int len = rx->skb->len;
2279
2280 if (!ieee80211_is_action(mgmt->frame_control))
2281 return RX_CONTINUE;
2282
Jouni Malinen026331c2010-02-15 12:53:10 +02002283 /* drop too small frames */
2284 if (len < IEEE80211_MIN_ACTION_SIZE)
2285 return RX_DROP_UNUSABLE;
2286
2287 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
Johannes Berg84040802010-02-15 12:46:39 +02002288 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02002289
Johannes Berg554891e2010-09-24 12:38:25 +02002290 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
Johannes Berg84040802010-02-15 12:46:39 +02002291 return RX_DROP_UNUSABLE;
Johannes Bergde1ede72008-09-09 14:42:50 +02002292
Johannes Bergde1ede72008-09-09 14:42:50 +02002293 switch (mgmt->u.action.category) {
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002294 case WLAN_CATEGORY_HT:
2295 /* reject HT action frames from stations not supporting HT */
2296 if (!rx->sta->sta.ht_cap.ht_supported)
2297 goto invalid;
2298
2299 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2300 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2301 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2302 sdata->vif.type != NL80211_IFTYPE_AP &&
2303 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2304 break;
2305
2306 /* verify action & smps_control are present */
2307 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2308 goto invalid;
2309
2310 switch (mgmt->u.action.u.ht_smps.action) {
2311 case WLAN_HT_ACTION_SMPS: {
2312 struct ieee80211_supported_band *sband;
2313 u8 smps;
2314
2315 /* convert to HT capability */
2316 switch (mgmt->u.action.u.ht_smps.smps_control) {
2317 case WLAN_HT_SMPS_CONTROL_DISABLED:
2318 smps = WLAN_HT_CAP_SM_PS_DISABLED;
2319 break;
2320 case WLAN_HT_SMPS_CONTROL_STATIC:
2321 smps = WLAN_HT_CAP_SM_PS_STATIC;
2322 break;
2323 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
2324 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
2325 break;
2326 default:
2327 goto invalid;
2328 }
2329 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
2330
2331 /* if no change do nothing */
2332 if ((rx->sta->sta.ht_cap.cap &
2333 IEEE80211_HT_CAP_SM_PS) == smps)
2334 goto handled;
2335
2336 rx->sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SM_PS;
2337 rx->sta->sta.ht_cap.cap |= smps;
2338
2339 sband = rx->local->hw.wiphy->bands[status->band];
2340
Johannes Berg64f68e52012-03-28 10:58:37 +02002341 rate_control_rate_update(local, sband, rx->sta,
2342 IEEE80211_RC_SMPS_CHANGED);
Johannes Berg1d8d3de2011-12-16 15:28:57 +01002343 goto handled;
2344 }
2345 default:
2346 goto invalid;
2347 }
2348
2349 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002350 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01002351 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
Thomas Pedersenae2772b2011-10-26 14:47:29 -07002352 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg8abd3f92009-02-10 21:25:47 +01002353 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
Alexander Simon13c40c52011-11-30 16:56:34 +01002354 sdata->vif.type != NL80211_IFTYPE_AP &&
2355 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg84040802010-02-15 12:46:39 +02002356 break;
Johannes Berg8abd3f92009-02-10 21:25:47 +01002357
Jouni Malinen026331c2010-02-15 12:53:10 +02002358 /* verify action_code is present */
2359 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2360 break;
2361
Johannes Bergde1ede72008-09-09 14:42:50 +02002362 switch (mgmt->u.action.u.addba_req.action_code) {
2363 case WLAN_ACTION_ADDBA_REQ:
2364 if (len < (IEEE80211_MIN_ACTION_SIZE +
2365 sizeof(mgmt->u.action.u.addba_req)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002366 goto invalid;
2367 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002368 case WLAN_ACTION_ADDBA_RESP:
2369 if (len < (IEEE80211_MIN_ACTION_SIZE +
2370 sizeof(mgmt->u.action.u.addba_resp)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002371 goto invalid;
2372 break;
Johannes Bergde1ede72008-09-09 14:42:50 +02002373 case WLAN_ACTION_DELBA:
2374 if (len < (IEEE80211_MIN_ACTION_SIZE +
2375 sizeof(mgmt->u.action.u.delba)))
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002376 goto invalid;
2377 break;
2378 default:
2379 goto invalid;
Johannes Bergde1ede72008-09-09 14:42:50 +02002380 }
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002381
Johannes Berg8b58ff82010-06-10 10:21:51 +02002382 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02002383 case WLAN_CATEGORY_SPECTRUM_MGMT:
Johannes Berg4797c7b2012-07-26 14:31:19 +02002384 if (status->band != IEEE80211_BAND_5GHZ)
Johannes Berg84040802010-02-15 12:46:39 +02002385 break;
Johannes Berg46900292009-02-15 12:44:28 +01002386
2387 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002388 break;
Johannes Berg46900292009-02-15 12:44:28 +01002389
Jouni Malinen026331c2010-02-15 12:53:10 +02002390 /* verify action_code is present */
2391 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2392 break;
2393
Johannes Berg39192c02008-09-09 14:49:03 +02002394 switch (mgmt->u.action.u.measurement.action_code) {
2395 case WLAN_ACTION_SPCT_MSR_REQ:
2396 if (len < (IEEE80211_MIN_ACTION_SIZE +
2397 sizeof(mgmt->u.action.u.measurement)))
Johannes Berg84040802010-02-15 12:46:39 +02002398 break;
Johannes Berg39192c02008-09-09 14:49:03 +02002399 ieee80211_process_measurement_req(sdata, mgmt, len);
Johannes Berg84040802010-02-15 12:46:39 +02002400 goto handled;
Sujithc481ec92009-01-06 09:28:37 +05302401 case WLAN_ACTION_SPCT_CHL_SWITCH:
2402 if (len < (IEEE80211_MIN_ACTION_SIZE +
2403 sizeof(mgmt->u.action.u.chan_switch)))
Johannes Berg84040802010-02-15 12:46:39 +02002404 break;
Sujithc481ec92009-01-06 09:28:37 +05302405
Johannes Bergcc32abd2009-05-15 11:52:31 +02002406 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002407 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02002408
Joe Perchesb203ca32012-05-08 18:56:52 +00002409 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
Johannes Berg84040802010-02-15 12:46:39 +02002410 break;
Sujithc481ec92009-01-06 09:28:37 +05302411
Johannes Berg8b58ff82010-06-10 10:21:51 +02002412 goto queue;
Johannes Berg39192c02008-09-09 14:49:03 +02002413 }
2414 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002415 case WLAN_CATEGORY_SA_QUERY:
2416 if (len < (IEEE80211_MIN_ACTION_SIZE +
2417 sizeof(mgmt->u.action.u.sa_query)))
Johannes Berg84040802010-02-15 12:46:39 +02002418 break;
2419
Jouni Malinenfea14732009-01-08 13:32:06 +02002420 switch (mgmt->u.action.u.sa_query.action) {
2421 case WLAN_ACTION_SA_QUERY_REQUEST:
2422 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg84040802010-02-15 12:46:39 +02002423 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02002424 ieee80211_process_sa_query_req(sdata, mgmt, len);
Johannes Berg84040802010-02-15 12:46:39 +02002425 goto handled;
Jouni Malinenfea14732009-01-08 13:32:06 +02002426 }
2427 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07002428 case WLAN_CATEGORY_SELF_PROTECTED:
Johannes Berg9b395bc2012-10-26 00:36:40 +02002429 if (len < (IEEE80211_MIN_ACTION_SIZE +
2430 sizeof(mgmt->u.action.u.self_prot.action_code)))
2431 break;
2432
Thomas Pedersen8db09852011-08-12 20:01:00 -07002433 switch (mgmt->u.action.u.self_prot.action_code) {
2434 case WLAN_SP_MESH_PEERING_OPEN:
2435 case WLAN_SP_MESH_PEERING_CLOSE:
2436 case WLAN_SP_MESH_PEERING_CONFIRM:
2437 if (!ieee80211_vif_is_mesh(&sdata->vif))
2438 goto invalid;
2439 if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
2440 /* userspace handles this frame */
2441 break;
2442 goto queue;
2443 case WLAN_SP_MGK_INFORM:
2444 case WLAN_SP_MGK_ACK:
2445 if (!ieee80211_vif_is_mesh(&sdata->vif))
2446 goto invalid;
2447 break;
2448 }
2449 break;
Javier Cardonad3aaec8a2011-05-03 16:57:09 -07002450 case WLAN_CATEGORY_MESH_ACTION:
Johannes Berg9b395bc2012-10-26 00:36:40 +02002451 if (len < (IEEE80211_MIN_ACTION_SIZE +
2452 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2453 break;
2454
Johannes Berg77a121c2010-06-10 10:21:34 +02002455 if (!ieee80211_vif_is_mesh(&sdata->vif))
2456 break;
Thomas Pedersen25d49e42011-08-11 19:35:15 -07002457 if (mesh_action_is_path_sel(mgmt) &&
Johannes Berg1df332e2012-10-26 00:09:11 +02002458 !mesh_path_sel_is_hwmp(sdata))
Javier Cardonac7108a72010-12-16 17:37:50 -08002459 break;
2460 goto queue;
Johannes Berg84040802010-02-15 12:46:39 +02002461 }
Jouni Malinen026331c2010-02-15 12:53:10 +02002462
Johannes Berg2e161f72010-08-12 15:38:38 +02002463 return RX_CONTINUE;
2464
Johannes Bergbed7ee6e2010-06-10 10:21:35 +02002465 invalid:
Johannes Berg554891e2010-09-24 12:38:25 +02002466 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
Johannes Berg2e161f72010-08-12 15:38:38 +02002467 /* will return in the next handlers */
2468 return RX_CONTINUE;
2469
2470 handled:
2471 if (rx->sta)
2472 rx->sta->rx_packets++;
2473 dev_kfree_skb(rx->skb);
2474 return RX_QUEUED;
2475
2476 queue:
2477 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2478 skb_queue_tail(&sdata->skb_queue, rx->skb);
2479 ieee80211_queue_work(&local->hw, &sdata->work);
2480 if (rx->sta)
2481 rx->sta->rx_packets++;
2482 return RX_QUEUED;
2483}
2484
2485static ieee80211_rx_result debug_noinline
2486ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2487{
Johannes Berg554891e2010-09-24 12:38:25 +02002488 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg804483e2012-03-05 22:18:41 +01002489 int sig = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +02002490
2491 /* skip known-bad action frames and return them in the next handler */
Johannes Berg554891e2010-09-24 12:38:25 +02002492 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
Johannes Berg2e161f72010-08-12 15:38:38 +02002493 return RX_CONTINUE;
Felix Fietkaud7907442010-01-07 20:23:53 +01002494
Jouni Malinen026331c2010-02-15 12:53:10 +02002495 /*
2496 * Getting here means the kernel doesn't know how to handle
2497 * it, but maybe userspace does ... include returned frames
2498 * so userspace can register for those to know whether ones
2499 * it transmitted were processed or returned.
2500 */
Jouni Malinen026331c2010-02-15 12:53:10 +02002501
Johannes Berg804483e2012-03-05 22:18:41 +01002502 if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
2503 sig = status->signal;
2504
Johannes Berg71bbc992012-06-15 15:30:18 +02002505 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
Johannes Berg2e161f72010-08-12 15:38:38 +02002506 rx->skb->data, rx->skb->len,
2507 GFP_ATOMIC)) {
2508 if (rx->sta)
2509 rx->sta->rx_packets++;
2510 dev_kfree_skb(rx->skb);
2511 return RX_QUEUED;
2512 }
2513
Johannes Berg2e161f72010-08-12 15:38:38 +02002514 return RX_CONTINUE;
2515}
2516
2517static ieee80211_rx_result debug_noinline
2518ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2519{
2520 struct ieee80211_local *local = rx->local;
2521 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2522 struct sk_buff *nskb;
2523 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg554891e2010-09-24 12:38:25 +02002524 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
Johannes Berg2e161f72010-08-12 15:38:38 +02002525
2526 if (!ieee80211_is_action(mgmt->frame_control))
2527 return RX_CONTINUE;
2528
2529 /*
2530 * For AP mode, hostapd is responsible for handling any action
2531 * frames that we didn't handle, including returning unknown
2532 * ones. For all other modes we will return them to the sender,
2533 * setting the 0x80 bit in the action category, as required by
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02002534 * 802.11-2012 9.24.4.
Johannes Berg2e161f72010-08-12 15:38:38 +02002535 * Newer versions of hostapd shall also use the management frame
2536 * registration mechanisms, but older ones still use cooked
2537 * monitor interfaces so push all frames there.
2538 */
Johannes Berg554891e2010-09-24 12:38:25 +02002539 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
Johannes Berg2e161f72010-08-12 15:38:38 +02002540 (sdata->vif.type == NL80211_IFTYPE_AP ||
2541 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2542 return RX_DROP_MONITOR;
Jouni Malinen026331c2010-02-15 12:53:10 +02002543
Johannes Berg4b5ebcc2012-06-27 15:38:56 +02002544 if (is_multicast_ether_addr(mgmt->da))
2545 return RX_DROP_MONITOR;
2546
Johannes Berg84040802010-02-15 12:46:39 +02002547 /* do not return rejected action frames */
2548 if (mgmt->u.action.category & 0x80)
2549 return RX_DROP_UNUSABLE;
2550
2551 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2552 GFP_ATOMIC);
2553 if (nskb) {
John W. Linville292b4df2010-06-24 11:13:56 -04002554 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
Johannes Berg84040802010-02-15 12:46:39 +02002555
John W. Linville292b4df2010-06-24 11:13:56 -04002556 nmgmt->u.action.category |= 0x80;
2557 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2558 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
Johannes Berg84040802010-02-15 12:46:39 +02002559
2560 memset(nskb->cb, 0, sizeof(nskb->cb));
2561
2562 ieee80211_tx_skb(rx->sdata, nskb);
Johannes Bergde1ede72008-09-09 14:42:50 +02002563 }
Johannes Berg39192c02008-09-09 14:49:03 +02002564 dev_kfree_skb(rx->skb);
2565 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02002566}
2567
2568static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002569ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002570{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002571 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Berg77a121c2010-06-10 10:21:34 +02002572 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2573 __le16 stype;
Johannes Berg571ecf62007-07-27 15:43:22 +02002574
Johannes Berg77a121c2010-06-10 10:21:34 +02002575 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
Johannes Berg472dbc42008-09-11 00:01:49 +02002576
Johannes Berg77a121c2010-06-10 10:21:34 +02002577 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2578 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2579 sdata->vif.type != NL80211_IFTYPE_STATION)
2580 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02002581
Johannes Berg77a121c2010-06-10 10:21:34 +02002582 switch (stype) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002583 case cpu_to_le16(IEEE80211_STYPE_AUTH):
Johannes Berg77a121c2010-06-10 10:21:34 +02002584 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2585 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2586 /* process for all: mesh, mlme, ibss */
2587 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01002588 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2589 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
Johannes Berg77a121c2010-06-10 10:21:34 +02002590 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2591 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
Christian Lamparter2c313332010-11-29 20:53:23 +01002592 if (is_multicast_ether_addr(mgmt->da) &&
2593 !is_broadcast_ether_addr(mgmt->da))
2594 return RX_DROP_MONITOR;
2595
Johannes Berg77a121c2010-06-10 10:21:34 +02002596 /* process only for station */
2597 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2598 return RX_DROP_MONITOR;
2599 break;
2600 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
Johannes Berg77a121c2010-06-10 10:21:34 +02002601 /* process only for ibss */
2602 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
2603 return RX_DROP_MONITOR;
2604 break;
2605 default:
2606 return RX_DROP_MONITOR;
2607 }
Johannes Berg46900292009-02-15 12:44:28 +01002608
Johannes Berg77a121c2010-06-10 10:21:34 +02002609 /* queue up frame and kick off work to process it */
Johannes Bergc1475ca2010-06-10 10:21:37 +02002610 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
Johannes Berg77a121c2010-06-10 10:21:34 +02002611 skb_queue_tail(&sdata->skb_queue, rx->skb);
2612 ieee80211_queue_work(&rx->local->hw, &sdata->work);
Johannes Berg8b58ff82010-06-10 10:21:51 +02002613 if (rx->sta)
2614 rx->sta->rx_packets++;
Johannes Berg77a121c2010-06-10 10:21:34 +02002615
2616 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02002617}
2618
Johannes Berg5cf121c2008-02-25 16:27:43 +01002619/* TODO: use IEEE80211_RX_FRAGMENTED */
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002620static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2621 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01002622{
2623 struct ieee80211_sub_if_data *sdata;
2624 struct ieee80211_local *local = rx->local;
Michael Wu3d30d942008-01-31 19:48:27 +01002625 struct sk_buff *skb = rx->skb, *skb2;
2626 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002627 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg293702a2012-03-02 13:18:19 +01002628 int needed_headroom;
Michael Wu3d30d942008-01-31 19:48:27 +01002629
Johannes Berg554891e2010-09-24 12:38:25 +02002630 /*
2631 * If cooked monitor has been processed already, then
2632 * don't do it again. If not, set the flag.
2633 */
2634 if (rx->flags & IEEE80211_RX_CMNTR)
John W. Linville7c1e1832010-09-24 15:52:49 -04002635 goto out_free_skb;
Johannes Berg554891e2010-09-24 12:38:25 +02002636 rx->flags |= IEEE80211_RX_CMNTR;
John W. Linville7c1e1832010-09-24 15:52:49 -04002637
Johannes Berg152c4772011-10-21 10:22:22 +02002638 /* If there are no cooked monitor interfaces, just free the SKB */
2639 if (!local->cooked_mntrs)
2640 goto out_free_skb;
2641
Johannes Berg293702a2012-03-02 13:18:19 +01002642 /* room for the radiotap header based on driver features */
Johannes Berg90b9e4462012-11-16 10:09:08 +01002643 needed_headroom = ieee80211_rx_radiotap_space(local, status);
Johannes Berg293702a2012-03-02 13:18:19 +01002644
2645 if (skb_headroom(skb) < needed_headroom &&
2646 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
Michael Wu3d30d942008-01-31 19:48:27 +01002647 goto out_free_skb;
2648
Johannes Berg293702a2012-03-02 13:18:19 +01002649 /* prepend radiotap information */
Felix Fietkau973ef212012-04-16 14:56:48 +02002650 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
2651 false);
Michael Wu3d30d942008-01-31 19:48:27 +01002652
2653 skb_set_mac_header(skb, 0);
2654 skb->ip_summed = CHECKSUM_UNNECESSARY;
2655 skb->pkt_type = PACKET_OTHERHOST;
2656 skb->protocol = htons(ETH_P_802_2);
2657
2658 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01002659 if (!ieee80211_sdata_running(sdata))
Michael Wu3d30d942008-01-31 19:48:27 +01002660 continue;
2661
Johannes Berg05c914f2008-09-11 00:01:58 +02002662 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Michael Wu3d30d942008-01-31 19:48:27 +01002663 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2664 continue;
2665
2666 if (prev_dev) {
2667 skb2 = skb_clone(skb, GFP_ATOMIC);
2668 if (skb2) {
2669 skb2->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04002670 netif_receive_skb(skb2);
Michael Wu3d30d942008-01-31 19:48:27 +01002671 }
2672 }
2673
2674 prev_dev = sdata->dev;
2675 sdata->dev->stats.rx_packets++;
2676 sdata->dev->stats.rx_bytes += skb->len;
2677 }
2678
2679 if (prev_dev) {
2680 skb->dev = prev_dev;
John W. Linville5548a8a2010-06-24 14:25:56 -04002681 netif_receive_skb(skb);
Johannes Berg554891e2010-09-24 12:38:25 +02002682 return;
2683 }
Michael Wu3d30d942008-01-31 19:48:27 +01002684
2685 out_free_skb:
2686 dev_kfree_skb(skb);
2687}
2688
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002689static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
2690 ieee80211_rx_result res)
2691{
2692 switch (res) {
2693 case RX_DROP_MONITOR:
2694 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2695 if (rx->sta)
2696 rx->sta->rx_dropped++;
2697 /* fall through */
2698 case RX_CONTINUE: {
2699 struct ieee80211_rate *rate = NULL;
2700 struct ieee80211_supported_band *sband;
2701 struct ieee80211_rx_status *status;
2702
2703 status = IEEE80211_SKB_RXCB((rx->skb));
2704
2705 sband = rx->local->hw.wiphy->bands[status->band];
2706 if (!(status->flag & RX_FLAG_HT))
2707 rate = &sband->bitrates[status->rate_idx];
2708
2709 ieee80211_rx_cooked_monitor(rx, rate);
2710 break;
2711 }
2712 case RX_DROP_UNUSABLE:
2713 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2714 if (rx->sta)
2715 rx->sta->rx_dropped++;
2716 dev_kfree_skb(rx->skb);
2717 break;
2718 case RX_QUEUED:
2719 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
2720 break;
2721 }
2722}
2723
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002724static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002725{
2726 ieee80211_rx_result res = RX_DROP_MONITOR;
2727 struct sk_buff *skb;
2728
2729#define CALL_RXH(rxh) \
2730 do { \
2731 res = rxh(rx); \
2732 if (res != RX_CONTINUE) \
2733 goto rxh_next; \
2734 } while (0);
2735
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002736 spin_lock(&rx->local->rx_skb_queue.lock);
2737 if (rx->local->running_rx_handler)
2738 goto unlock;
2739
2740 rx->local->running_rx_handler = true;
2741
2742 while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
2743 spin_unlock(&rx->local->rx_skb_queue.lock);
2744
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002745 /*
2746 * all the other fields are valid across frames
2747 * that belong to an aMPDU since they are on the
2748 * same TID from the same station
2749 */
2750 rx->skb = skb;
2751
2752 CALL_RXH(ieee80211_rx_h_decrypt)
2753 CALL_RXH(ieee80211_rx_h_check_more_data)
Johannes Berg47086fc2011-09-29 16:04:33 +02002754 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002755 CALL_RXH(ieee80211_rx_h_sta_process)
2756 CALL_RXH(ieee80211_rx_h_defragment)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002757 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2758 /* must be after MMIC verify so header is counted in MPDU mic */
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002759#ifdef CONFIG_MAC80211_MESH
2760 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2761 CALL_RXH(ieee80211_rx_h_mesh_fwding);
2762#endif
Javier Cardona2154c812011-09-07 17:49:53 -07002763 CALL_RXH(ieee80211_rx_h_amsdu)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002764 CALL_RXH(ieee80211_rx_h_data)
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002765 CALL_RXH(ieee80211_rx_h_ctrl);
Johannes Berg2e161f72010-08-12 15:38:38 +02002766 CALL_RXH(ieee80211_rx_h_mgmt_check)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002767 CALL_RXH(ieee80211_rx_h_action)
Johannes Berg2e161f72010-08-12 15:38:38 +02002768 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2769 CALL_RXH(ieee80211_rx_h_action_return)
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002770 CALL_RXH(ieee80211_rx_h_mgmt)
2771
2772 rxh_next:
2773 ieee80211_rx_handlers_result(rx, res);
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002774 spin_lock(&rx->local->rx_skb_queue.lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002775#undef CALL_RXH
2776 }
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002777
2778 rx->local->running_rx_handler = false;
2779
2780 unlock:
2781 spin_unlock(&rx->local->rx_skb_queue.lock);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002782}
Johannes Berg571ecf62007-07-27 15:43:22 +02002783
Johannes Berg4406c372010-09-24 11:21:06 +02002784static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
Johannes Berg58905292008-01-31 19:48:25 +01002785{
Johannes Berg58905292008-01-31 19:48:25 +01002786 ieee80211_rx_result res = RX_DROP_MONITOR;
2787
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002788#define CALL_RXH(rxh) \
2789 do { \
2790 res = rxh(rx); \
2791 if (res != RX_CONTINUE) \
Johannes Berg2569a822009-11-25 17:46:17 +01002792 goto rxh_next; \
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002793 } while (0);
Johannes Berg58905292008-01-31 19:48:25 +01002794
Johannes Berg49461622008-06-30 15:10:45 +02002795 CALL_RXH(ieee80211_rx_h_check)
Johannes Berg2569a822009-11-25 17:46:17 +01002796
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002797 ieee80211_rx_reorder_ampdu(rx);
Johannes Berg2569a822009-11-25 17:46:17 +01002798
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002799 ieee80211_rx_handlers(rx);
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002800 return;
Johannes Berg49461622008-06-30 15:10:45 +02002801
Johannes Berg2569a822009-11-25 17:46:17 +01002802 rxh_next:
Christian Lamparteraa0c8632010-08-05 01:36:04 +02002803 ieee80211_rx_handlers_result(rx, res);
2804
2805#undef CALL_RXH
Johannes Berg58905292008-01-31 19:48:25 +01002806}
2807
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02002808/*
Johannes Bergdd318572010-11-29 11:09:16 +01002809 * This function makes calls into the RX path, therefore
2810 * it has to be invoked under RCU read lock.
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02002811 */
2812void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
2813{
Johannes Berg554891e2010-09-24 12:38:25 +02002814 struct ieee80211_rx_data rx = {
2815 .sta = sta,
2816 .sdata = sta->sdata,
2817 .local = sta->local,
Johannes Berg9e262972011-07-07 18:45:03 +02002818 /* This is OK -- must be QoS data frame */
2819 .security_idx = tid,
2820 .seqno_idx = tid,
Helmut Schaafcf8bd32011-04-01 15:46:05 +02002821 .flags = 0,
Johannes Berg554891e2010-09-24 12:38:25 +02002822 };
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02002823 struct tid_ampdu_rx *tid_agg_rx;
2824
2825 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
2826 if (!tid_agg_rx)
2827 return;
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02002828
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02002829 spin_lock(&tid_agg_rx->reorder_lock);
Johannes Bergd3b2fb52012-06-22 12:48:38 +02002830 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx);
Christian Lamparter2c15a0c2010-08-24 19:22:42 +02002831 spin_unlock(&tid_agg_rx->reorder_lock);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02002832
Christian Lamparter24a8fda2010-12-30 17:25:29 +01002833 ieee80211_rx_handlers(&rx);
Christian Lamparter2bff8eb2010-08-05 01:36:41 +02002834}
2835
Johannes Berg571ecf62007-07-27 15:43:22 +02002836/* main receive path */
2837
Johannes Berg20b01f82010-09-24 11:21:05 +02002838static int prepare_for_handlers(struct ieee80211_rx_data *rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02002839 struct ieee80211_hdr *hdr)
2840{
Johannes Berg20b01f82010-09-24 11:21:05 +02002841 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002842 struct sk_buff *skb = rx->skb;
2843 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2844 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02002845 int multicast = is_multicast_ether_addr(hdr->addr1);
2846
Johannes Berg51fb61e2007-12-19 01:31:27 +01002847 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002848 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01002849 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg23a24de2007-07-27 15:43:22 +02002850 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002851 if (!multicast &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002852 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
Felix Fietkau4f312332011-02-05 23:48:37 +01002853 if (!(sdata->dev->flags & IFF_PROMISC) ||
2854 sdata->u.mgd.use_4addr)
Johannes Berg23a24de2007-07-27 15:43:22 +02002855 return 0;
Johannes Berg554891e2010-09-24 12:38:25 +02002856 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002857 }
2858 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002859 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02002860 if (!bssid)
2861 return 0;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002862 if (ieee80211_is_beacon(hdr->frame_control)) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002863 return 1;
Johannes Bergd48b2962012-07-06 22:19:27 +02002864 } else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2865 return 0;
Johannes Berg23a24de2007-07-27 15:43:22 +02002866 } else if (!multicast &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002867 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
Johannes Berg4150c572007-09-17 01:29:23 -04002868 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002869 return 0;
Johannes Berg554891e2010-09-24 12:38:25 +02002870 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002871 } else if (!rx->sta) {
2872 int rate_idx;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002873 if (status->flag & RX_FLAG_HT)
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002874 rate_idx = 0; /* TODO: HT rates */
2875 else
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002876 rate_idx = status->rate_idx;
Johannes Berg8bf11d82011-12-15 11:17:37 +01002877 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
2878 BIT(rate_idx));
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002879 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002880 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002881 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg6032f932008-02-23 15:17:07 +01002882 if (!multicast &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002883 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
Johannes Berg6032f932008-02-23 15:17:07 +01002884 if (!(sdata->dev->flags & IFF_PROMISC))
2885 return 0;
2886
Johannes Berg554891e2010-09-24 12:38:25 +02002887 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg6032f932008-02-23 15:17:07 +01002888 }
2889 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002890 case NL80211_IFTYPE_AP_VLAN:
2891 case NL80211_IFTYPE_AP:
Johannes Berg23a24de2007-07-27 15:43:22 +02002892 if (!bssid) {
Joe Perchesb203ca32012-05-08 18:56:52 +00002893 if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
Johannes Berg23a24de2007-07-27 15:43:22 +02002894 return 0;
Johannes Bergf142c6b2012-06-18 20:07:15 +02002895 } else if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
Johannes Berg3df6eae2011-12-06 10:39:40 +01002896 /*
2897 * Accept public action frames even when the
2898 * BSSID doesn't match, this is used for P2P
2899 * and location updates. Note that mac80211
2900 * itself never looks at these frames.
2901 */
Johannes Bergd48b2962012-07-06 22:19:27 +02002902 if (ieee80211_is_public_action(hdr, skb->len))
Johannes Berg3df6eae2011-12-06 10:39:40 +01002903 return 1;
Johannes Bergd48b2962012-07-06 22:19:27 +02002904 if (!ieee80211_is_beacon(hdr->frame_control))
Johannes Berg23a24de2007-07-27 15:43:22 +02002905 return 0;
Johannes Berg554891e2010-09-24 12:38:25 +02002906 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002907 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002908 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002909 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07002910 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg23a24de2007-07-27 15:43:22 +02002911 return 0;
Joe Perchesb203ca32012-05-08 18:56:52 +00002912 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
Johannes Berg23a24de2007-07-27 15:43:22 +02002913 return 0;
2914 break;
Johannes Bergf142c6b2012-06-18 20:07:15 +02002915 case NL80211_IFTYPE_P2P_DEVICE:
2916 if (!ieee80211_is_public_action(hdr, skb->len) &&
2917 !ieee80211_is_probe_req(hdr->frame_control) &&
2918 !ieee80211_is_probe_resp(hdr->frame_control) &&
2919 !ieee80211_is_beacon(hdr->frame_control))
2920 return 0;
2921 if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
2922 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2923 break;
Johannes Berg2ca27bc2010-09-16 14:58:23 +02002924 default:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02002925 /* should never get here */
Johannes Bergf142c6b2012-06-18 20:07:15 +02002926 WARN_ON_ONCE(1);
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02002927 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02002928 }
2929
2930 return 1;
2931}
2932
Johannes Berg571ecf62007-07-27 15:43:22 +02002933/*
Johannes Berg4406c372010-09-24 11:21:06 +02002934 * This function returns whether or not the SKB
2935 * was destined for RX processing or not, which,
2936 * if consume is true, is equivalent to whether
2937 * or not the skb was consumed.
2938 */
2939static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
2940 struct sk_buff *skb, bool consume)
2941{
2942 struct ieee80211_local *local = rx->local;
2943 struct ieee80211_sub_if_data *sdata = rx->sdata;
2944 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2945 struct ieee80211_hdr *hdr = (void *)skb->data;
2946 int prepares;
2947
2948 rx->skb = skb;
Johannes Berg554891e2010-09-24 12:38:25 +02002949 status->rx_flags |= IEEE80211_RX_RA_MATCH;
Johannes Berg4406c372010-09-24 11:21:06 +02002950 prepares = prepare_for_handlers(rx, hdr);
2951
2952 if (!prepares)
2953 return false;
2954
Johannes Berg4406c372010-09-24 11:21:06 +02002955 if (!consume) {
2956 skb = skb_copy(skb, GFP_ATOMIC);
2957 if (!skb) {
2958 if (net_ratelimit())
2959 wiphy_debug(local->hw.wiphy,
Ben Greearb305dae2011-01-08 10:30:54 -08002960 "failed to copy skb for %s\n",
Johannes Berg4406c372010-09-24 11:21:06 +02002961 sdata->name);
2962 return true;
2963 }
2964
2965 rx->skb = skb;
2966 }
2967
2968 ieee80211_invoke_rx_handlers(rx);
2969 return true;
2970}
2971
2972/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002973 * This is the actual Rx frames handler. as it blongs to Rx path it must
2974 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02002975 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002976static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
Christian Lamparter071d9ac2010-08-05 01:36:36 +02002977 struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02002978{
2979 struct ieee80211_local *local = hw_to_local(hw);
2980 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002981 struct ieee80211_hdr *hdr;
Zhu Yie3cf8b32010-03-29 17:35:07 +08002982 __le16 fc;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002983 struct ieee80211_rx_data rx;
Johannes Berg4406c372010-09-24 11:21:06 +02002984 struct ieee80211_sub_if_data *prev;
Ben Greear56af3262010-09-23 10:22:24 -07002985 struct sta_info *sta, *tmp, *prev_sta;
Zhu Yie3cf8b32010-03-29 17:35:07 +08002986 int err = 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02002987
Zhu Yie3cf8b32010-03-29 17:35:07 +08002988 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02002989 memset(&rx, 0, sizeof(rx));
2990 rx.skb = skb;
2991 rx.local = local;
Johannes Berg72abd812007-09-17 01:29:22 -04002992
Zhu Yie3cf8b32010-03-29 17:35:07 +08002993 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
Johannes Berg571ecf62007-07-27 15:43:22 +02002994 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02002995
Johannes Berg4a4f1a52012-10-26 00:33:36 +02002996 if (ieee80211_is_mgmt(fc)) {
2997 /* drop frame if too short for header */
2998 if (skb->len < ieee80211_hdrlen(fc))
2999 err = -ENOBUFS;
3000 else
3001 err = skb_linearize(skb);
3002 } else {
Zhu Yie3cf8b32010-03-29 17:35:07 +08003003 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
Johannes Berg4a4f1a52012-10-26 00:33:36 +02003004 }
Zhu Yie3cf8b32010-03-29 17:35:07 +08003005
3006 if (err) {
3007 dev_kfree_skb(skb);
3008 return;
3009 }
3010
3011 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +01003012 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01003013 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01003014
Johannes Bergd48b2962012-07-06 22:19:27 +02003015 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3016 ieee80211_is_beacon(hdr->frame_control)))
3017 ieee80211_scan_rx(local, skb);
3018
Zhu Yie3cf8b32010-03-29 17:35:07 +08003019 if (ieee80211_is_data(fc)) {
Ben Greear56af3262010-09-23 10:22:24 -07003020 prev_sta = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02003021
Johannes Berg7852e362012-01-20 13:55:24 +01003022 for_each_sta_info(local, hdr->addr2, sta, tmp) {
Ben Greear56af3262010-09-23 10:22:24 -07003023 if (!prev_sta) {
3024 prev_sta = sta;
3025 continue;
3026 }
3027
3028 rx.sta = prev_sta;
3029 rx.sdata = prev_sta->sdata;
Johannes Berg4406c372010-09-24 11:21:06 +02003030 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Johannes Berg571ecf62007-07-27 15:43:22 +02003031
Ben Greear56af3262010-09-23 10:22:24 -07003032 prev_sta = sta;
Johannes Berg4406c372010-09-24 11:21:06 +02003033 }
Ben Greear56af3262010-09-23 10:22:24 -07003034
3035 if (prev_sta) {
3036 rx.sta = prev_sta;
3037 rx.sdata = prev_sta->sdata;
3038
Johannes Berg4406c372010-09-24 11:21:06 +02003039 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
Ben Greear56af3262010-09-23 10:22:24 -07003040 return;
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05303041 goto out;
Ben Greear56af3262010-09-23 10:22:24 -07003042 }
Johannes Berg4406c372010-09-24 11:21:06 +02003043 }
3044
Johannes Berg4b0dd982010-09-24 11:21:07 +02003045 prev = NULL;
Johannes Berg4406c372010-09-24 11:21:06 +02003046
Johannes Berg4b0dd982010-09-24 11:21:07 +02003047 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3048 if (!ieee80211_sdata_running(sdata))
3049 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01003050
Johannes Berg4b0dd982010-09-24 11:21:07 +02003051 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
3052 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3053 continue;
Johannes Bergabe60632009-11-25 17:46:18 +01003054
Johannes Berg4b0dd982010-09-24 11:21:07 +02003055 /*
3056 * frame is destined for this interface, but if it's
3057 * not also for the previous one we handle that after
3058 * the loop to avoid copying the SKB once too much
3059 */
Johannes Bergb2e77712007-09-26 15:19:39 +02003060
Johannes Berg4b0dd982010-09-24 11:21:07 +02003061 if (!prev) {
Johannes Berg340e11f2007-07-27 15:43:22 +02003062 prev = sdata;
Johannes Berg4b0dd982010-09-24 11:21:07 +02003063 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02003064 }
Felix Fietkau4bb29f82010-01-22 00:36:39 +01003065
Johannes Berg7852e362012-01-20 13:55:24 +01003066 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02003067 rx.sdata = prev;
3068 ieee80211_prepare_and_rx_handle(&rx, skb, false);
Felix Fietkau4bb29f82010-01-22 00:36:39 +01003069
Johannes Berg4b0dd982010-09-24 11:21:07 +02003070 prev = sdata;
3071 }
Johannes Berg4406c372010-09-24 11:21:06 +02003072
Johannes Berg4b0dd982010-09-24 11:21:07 +02003073 if (prev) {
Johannes Berg7852e362012-01-20 13:55:24 +01003074 rx.sta = sta_info_get_bss(prev, hdr->addr2);
Johannes Berg4b0dd982010-09-24 11:21:07 +02003075 rx.sdata = prev;
3076
3077 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
3078 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02003079 }
Johannes Berg4406c372010-09-24 11:21:06 +02003080
Senthil Balasubramanian8e26d5a2010-11-30 20:15:38 +05303081 out:
Johannes Berg4406c372010-09-24 11:21:06 +02003082 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02003083}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003084
3085/*
3086 * This is the receive path handler. It is called by a low level driver when an
3087 * 802.11 MPDU is received from the hardware.
3088 */
John W. Linville103bf9f2009-08-20 16:34:15 -04003089void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003090{
3091 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01003092 struct ieee80211_rate *rate = NULL;
3093 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02003094 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01003095
Johannes Bergd20ef632009-10-11 15:10:40 +02003096 WARN_ON_ONCE(softirq_count() == 0);
3097
Johannes Berg444e38032012-09-30 17:08:35 +02003098 if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
Johannes Berg77a980d2009-08-24 11:46:30 +02003099 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01003100
3101 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02003102 if (WARN_ON(!sband))
3103 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01003104
Johannes Berg89c3a8a2009-07-28 18:10:17 +02003105 /*
3106 * If we're suspending, it is possible although not too likely
3107 * that we'd be receiving frames after having already partially
3108 * quiesced the stack. We can't process such frames then since
3109 * that might, for example, cause stations to be added or other
3110 * driver callbacks be invoked.
3111 */
Johannes Berg77a980d2009-08-24 11:46:30 +02003112 if (unlikely(local->quiescing || local->suspended))
3113 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02003114
Arik Nemtsov04800ad2012-06-06 11:25:02 +03003115 /* We might be during a HW reconfig, prevent Rx for the same reason */
3116 if (unlikely(local->in_reconfig))
3117 goto drop;
3118
Johannes Bergea77f122009-08-21 14:44:45 +02003119 /*
3120 * The same happens when we're not even started,
3121 * but that's worth a warning.
3122 */
Johannes Berg77a980d2009-08-24 11:46:30 +02003123 if (WARN_ON(!local->started))
3124 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02003125
Johannes Bergfc885182010-07-30 13:23:12 +02003126 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05003127 /*
Johannes Bergfc885182010-07-30 13:23:12 +02003128 * Validate the rate, unless a PLCP error means that
3129 * we probably can't have a valid rate here anyway.
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05003130 */
Johannes Bergfc885182010-07-30 13:23:12 +02003131
3132 if (status->flag & RX_FLAG_HT) {
3133 /*
3134 * rate_idx is MCS index, which can be [0-76]
3135 * as documented on:
3136 *
3137 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
3138 *
3139 * Anything else would be some sort of driver or
3140 * hardware error. The driver should catch hardware
3141 * errors.
3142 */
Johannes Berg444e38032012-09-30 17:08:35 +02003143 if (WARN(status->rate_idx > 76,
Johannes Bergfc885182010-07-30 13:23:12 +02003144 "Rate marked as an HT rate but passed "
3145 "status->rate_idx is not "
3146 "an MCS index [0-76]: %d (0x%02x)\n",
3147 status->rate_idx,
3148 status->rate_idx))
3149 goto drop;
3150 } else {
Johannes Berg444e38032012-09-30 17:08:35 +02003151 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
Johannes Bergfc885182010-07-30 13:23:12 +02003152 goto drop;
3153 rate = &sband->bitrates[status->rate_idx];
3154 }
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02003155 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003156
Johannes Berg554891e2010-09-24 12:38:25 +02003157 status->rx_flags = 0;
3158
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003159 /*
3160 * key references and virtual interfaces are protected using RCU
3161 * and this requires that we are in a read-side RCU section during
3162 * receive processing
3163 */
3164 rcu_read_lock();
3165
3166 /*
3167 * Frames with failed FCS/PLCP checksum are not returned,
3168 * all other frames are returned without radiotap header
3169 * if it was previously present.
3170 * Also, frames with less than 16 bytes are dropped.
3171 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02003172 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003173 if (!skb) {
3174 rcu_read_unlock();
3175 return;
3176 }
3177
Johannes Berge1e54062010-11-30 08:58:45 +01003178 ieee80211_tpt_led_trig_rx(local,
3179 ((struct ieee80211_hdr *)skb->data)->frame_control,
3180 skb->len);
Christian Lamparter071d9ac2010-08-05 01:36:36 +02003181 __ieee80211_rx_handle_packet(hw, skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003182
3183 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02003184
3185 return;
3186 drop:
3187 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02003188}
John W. Linville103bf9f2009-08-20 16:34:15 -04003189EXPORT_SYMBOL(ieee80211_rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02003190
3191/* This is a version of the rx handler that can be called from hard irq
3192 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02003193void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02003194{
3195 struct ieee80211_local *local = hw_to_local(hw);
3196
3197 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
3198
Johannes Berg571ecf62007-07-27 15:43:22 +02003199 skb->pkt_type = IEEE80211_RX_MSG;
3200 skb_queue_tail(&local->skb_queue, skb);
3201 tasklet_schedule(&local->tasklet);
3202}
3203EXPORT_SYMBOL(ieee80211_rx_irqsafe);