blob: d71a63e1fd6ab1aa78d6f77da4200f765611044e [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>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
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>
Johannes Berg571ecf62007-07-27 15:43:22 +020013#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040017#include <linux/rcupdate.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020018#include <net/mac80211.h>
19#include <net/ieee80211_radiotap.h>
20
21#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020022#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040023#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010024#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020025#include "wep.h"
26#include "wpa.h"
27#include "tkip.h"
28#include "wme.h"
29
Johannes Berg1edfb1a2009-11-25 17:46:16 +010030static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
31 struct sk_buff *skb,
32 struct ieee80211_rate *rate);
Johannes Berga02ae752009-11-16 12:00:40 +010033
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 Bergb2e77712007-09-26 15:19:39 +020043 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
44 if (likely(skb->len > FCS_LEN))
45 skb_trim(skb, skb->len - FCS_LEN);
46 else {
47 /* driver bug */
48 WARN_ON(1);
49 dev_kfree_skb(skb);
50 skb = NULL;
51 }
52 }
53
54 return skb;
55}
56
Johannes Bergf1d58c22009-06-17 13:13:00 +020057static inline int should_drop_frame(struct sk_buff *skb,
Johannes Berg0869aea2009-10-28 10:03:35 +010058 int present_fcs_len)
Johannes Bergb2e77712007-09-26 15:19:39 +020059{
Johannes Bergf1d58c22009-06-17 13:13:00 +020060 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Harvey Harrison182503a2008-06-22 16:45:29 -070061 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergb2e77712007-09-26 15:19:39 +020062
63 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
64 return 1;
Johannes Berg0869aea2009-10-28 10:03:35 +010065 if (unlikely(skb->len < 16 + present_fcs_len))
Johannes Bergb2e77712007-09-26 15:19:39 +020066 return 1;
Harvey Harrison87228f52008-06-11 14:21:59 -070067 if (ieee80211_is_ctl(hdr->frame_control) &&
68 !ieee80211_is_pspoll(hdr->frame_control) &&
69 !ieee80211_is_back_req(hdr->frame_control))
Johannes Bergb2e77712007-09-26 15:19:39 +020070 return 1;
71 return 0;
72}
73
Bruno Randolf601ae7f2008-05-08 19:22:43 +020074static int
75ieee80211_rx_radiotap_len(struct ieee80211_local *local,
76 struct ieee80211_rx_status *status)
77{
78 int len;
79
80 /* always present fields */
81 len = sizeof(struct ieee80211_radiotap_header) + 9;
82
83 if (status->flag & RX_FLAG_TSFT)
84 len += 8;
Johannes Berg7fee5372009-01-30 11:13:06 +010085 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
Bruno Randolf601ae7f2008-05-08 19:22:43 +020086 len += 1;
87 if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
88 len += 1;
89
90 if (len & 1) /* padding for RX_FLAGS if necessary */
91 len++;
92
Bruno Randolf601ae7f2008-05-08 19:22:43 +020093 return len;
94}
95
Johannes Bergd1c3a372009-01-07 00:26:10 +010096/*
Bruno Randolf601ae7f2008-05-08 19:22:43 +020097 * ieee80211_add_rx_radiotap_header - add radiotap header
98 *
99 * add a radiotap header containing all the fields which the hardware provided.
100 */
101static void
102ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
103 struct sk_buff *skb,
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200104 struct ieee80211_rate *rate,
105 int rtap_len)
106{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200107 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200108 struct ieee80211_radiotap_header *rthdr;
109 unsigned char *pos;
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100110 u16 rx_flags = 0;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200111
112 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
113 memset(rthdr, 0, rtap_len);
114
115 /* radiotap header, set always present flags */
116 rthdr->it_present =
117 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200118 (1 << IEEE80211_RADIOTAP_CHANNEL) |
119 (1 << IEEE80211_RADIOTAP_ANTENNA) |
120 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
121 rthdr->it_len = cpu_to_le16(rtap_len);
122
123 pos = (unsigned char *)(rthdr+1);
124
125 /* the order of the following fields is important */
126
127 /* IEEE80211_RADIOTAP_TSFT */
128 if (status->flag & RX_FLAG_TSFT) {
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100129 put_unaligned_le64(status->mactime, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200130 rthdr->it_present |=
131 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
132 pos += 8;
133 }
134
135 /* IEEE80211_RADIOTAP_FLAGS */
136 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
137 *pos |= IEEE80211_RADIOTAP_F_FCS;
Johannes Bergaae89832009-03-13 12:52:10 +0100138 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
139 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
Bruno Randolfb4f28bb2008-07-30 17:19:55 +0200140 if (status->flag & RX_FLAG_SHORTPRE)
141 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200142 pos++;
143
144 /* IEEE80211_RADIOTAP_RATE */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200145 if (status->flag & RX_FLAG_HT) {
146 /*
147 * TODO: add following information into radiotap header once
148 * suitable fields are defined for it:
149 * - MCS index (status->rate_idx)
150 * - HT40 (status->flag & RX_FLAG_40MHZ)
151 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
152 */
153 *pos = 0;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200154 } else {
Jouni Malinenebe6c7b2009-01-10 11:47:33 +0200155 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
Jouni Malinen0fb8ca42008-12-12 14:38:33 +0200156 *pos = rate->bitrate / 5;
Jouni Malinen8d6f6582008-12-15 10:37:50 +0200157 }
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200158 pos++;
159
160 /* IEEE80211_RADIOTAP_CHANNEL */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100161 put_unaligned_le16(status->freq, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200162 pos += 2;
163 if (status->band == IEEE80211_BAND_5GHZ)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100164 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
165 pos);
Johannes Berg5f0b7de2009-11-16 13:58:21 +0100166 else if (status->flag & RX_FLAG_HT)
167 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
168 pos);
Bruno Randolf9deb1ae2008-07-30 17:20:06 +0200169 else if (rate->flags & IEEE80211_RATE_ERP_G)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100170 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
171 pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200172 else
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100173 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
174 pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200175 pos += 2;
176
177 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
178 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
179 *pos = status->signal;
180 rthdr->it_present |=
181 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
182 pos++;
183 }
184
185 /* IEEE80211_RADIOTAP_DBM_ANTNOISE */
186 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
187 *pos = status->noise;
188 rthdr->it_present |=
189 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTNOISE);
190 pos++;
191 }
192
193 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
194
195 /* IEEE80211_RADIOTAP_ANTENNA */
196 *pos = status->antenna;
197 pos++;
198
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200199 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
200
201 /* IEEE80211_RADIOTAP_RX_FLAGS */
202 /* ensure 2 byte alignment for the 2 byte field as required */
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100203 if ((pos - (u8 *)rthdr) & 1)
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200204 pos++;
Johannes Bergaae89832009-03-13 12:52:10 +0100205 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
Johannes Berg6a86b9c2009-10-28 09:58:52 +0100206 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
207 put_unaligned_le16(rx_flags, pos);
Bruno Randolf601ae7f2008-05-08 19:22:43 +0200208 pos += 2;
209}
210
Johannes Bergb2e77712007-09-26 15:19:39 +0200211/*
212 * This function copies a received frame to all monitor interfaces and
213 * returns a cleaned-up SKB that no longer includes the FCS nor the
214 * radiotap header the driver might have added.
215 */
216static struct sk_buff *
217ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +0100218 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +0200219{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200220 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200221 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2e77712007-09-26 15:19:39 +0200222 int needed_headroom = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200223 struct sk_buff *skb, *skb2;
224 struct net_device *prev_dev = NULL;
225 int present_fcs_len = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200226
227 /*
228 * First, we may need to make a copy of the skb because
229 * (1) we need to modify it for radiotap (if not present), and
230 * (2) the other RX handlers will modify the skb we got.
231 *
232 * We don't need to, of course, if we aren't going to return
233 * the SKB because it has a bad FCS/PLCP checksum.
234 */
Johannes Berg0869aea2009-10-28 10:03:35 +0100235
236 /* room for the radiotap header based on driver features */
237 needed_headroom = ieee80211_rx_radiotap_len(local, status);
Johannes Bergb2e77712007-09-26 15:19:39 +0200238
239 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
240 present_fcs_len = FCS_LEN;
241
242 if (!local->monitors) {
Johannes Berg0869aea2009-10-28 10:03:35 +0100243 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200244 dev_kfree_skb(origskb);
245 return NULL;
246 }
247
Johannes Berg0869aea2009-10-28 10:03:35 +0100248 return remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200249 }
250
Johannes Berg0869aea2009-10-28 10:03:35 +0100251 if (should_drop_frame(origskb, present_fcs_len)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200252 /* only need to expand headroom if necessary */
253 skb = origskb;
254 origskb = NULL;
255
256 /*
257 * This shouldn't trigger often because most devices have an
258 * RX header they pull before we get here, and that should
259 * be big enough for our radiotap information. We should
260 * probably export the length to drivers so that we can have
261 * them allocate enough headroom to start with.
262 */
263 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100264 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200265 dev_kfree_skb(skb);
266 return NULL;
267 }
268 } else {
269 /*
270 * Need to make a copy and possibly remove radiotap header
271 * and FCS from the original.
272 */
273 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
274
Johannes Berg0869aea2009-10-28 10:03:35 +0100275 origskb = remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200276
277 if (!skb)
278 return origskb;
279 }
280
Johannes Berg0869aea2009-10-28 10:03:35 +0100281 /* prepend radiotap information */
282 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
Johannes Bergb2e77712007-09-26 15:19:39 +0200283
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100284 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200285 skb->ip_summed = CHECKSUM_UNNECESSARY;
286 skb->pkt_type = PACKET_OTHERHOST;
287 skb->protocol = htons(ETH_P_802_2);
288
289 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
290 if (!netif_running(sdata->dev))
291 continue;
292
Johannes Berg05c914f2008-09-11 00:01:58 +0200293 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200294 continue;
295
Michael Wu3d30d942008-01-31 19:48:27 +0100296 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
297 continue;
298
Johannes Bergb2e77712007-09-26 15:19:39 +0200299 if (prev_dev) {
300 skb2 = skb_clone(skb, GFP_ATOMIC);
301 if (skb2) {
302 skb2->dev = prev_dev;
303 netif_rx(skb2);
304 }
305 }
306
307 prev_dev = sdata->dev;
308 sdata->dev->stats.rx_packets++;
309 sdata->dev->stats.rx_bytes += skb->len;
310 }
311
312 if (prev_dev) {
313 skb->dev = prev_dev;
314 netif_rx(skb);
315 } else
316 dev_kfree_skb(skb);
317
318 return origskb;
319}
320
321
Johannes Berg5cf121c2008-02-25 16:27:43 +0100322static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200323{
Harvey Harrison238f74a2008-07-02 11:05:34 -0700324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200325 int tid;
326
327 /* does the frame have a qos control field? */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700328 if (ieee80211_is_data_qos(hdr->frame_control)) {
329 u8 *qc = ieee80211_get_qos_ctl(hdr);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200330 /* frame has qos control */
Harvey Harrison238f74a2008-07-02 11:05:34 -0700331 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
332 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100333 rx->flags |= IEEE80211_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200334 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100335 rx->flags &= ~IEEE80211_RX_AMSDU;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200336 } else {
Johannes Berg1411f9b2008-07-10 10:11:02 +0200337 /*
338 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
339 *
340 * Sequence numbers for management frames, QoS data
341 * frames with a broadcast/multicast address in the
342 * Address 1 field, and all non-QoS data frames sent
343 * by QoS STAs are assigned using an additional single
344 * modulo-4096 counter, [...]
345 *
346 * We also use that counter for non-QoS STAs.
347 */
348 tid = NUM_RX_DATA_QUEUES - 1;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200349 }
Johannes Berg52865df2007-07-27 15:43:22 +0200350
Johannes Berg5cf121c2008-02-25 16:27:43 +0100351 rx->queue = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200352 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
353 * For now, set skb->priority to 0 for other cases. */
354 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100355}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200356
Johannes Bergd1c3a372009-01-07 00:26:10 +0100357/**
358 * DOC: Packet alignment
359 *
360 * Drivers always need to pass packets that are aligned to two-byte boundaries
361 * to the stack.
362 *
363 * Additionally, should, if possible, align the payload data in a way that
364 * guarantees that the contained IP header is aligned to a four-byte
365 * boundary. In the case of regular frames, this simply means aligning the
366 * payload to a four-byte boundary (because either the IP header is directly
367 * contained, or IV/RFC1042 headers that have a length divisible by four are
368 * in front of it).
369 *
370 * With A-MSDU frames, however, the payload data address must yield two modulo
371 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
372 * push the IP header further back to a multiple of four again. Thankfully, the
373 * specs were sane enough this time around to require padding each A-MSDU
374 * subframe to a length that is a multiple of four.
375 *
376 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
377 * the payload is not supported, the driver is required to move the 802.11
378 * header to be directly in front of the payload in that case.
379 */
380static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100381{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700382 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg38f37142008-01-29 17:07:43 +0100383 int hdrlen;
384
Johannes Bergd1c3a372009-01-07 00:26:10 +0100385#ifndef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
386 return;
387#endif
388
389 if (WARN_ONCE((unsigned long)rx->skb->data & 1,
390 "unaligned packet at 0x%p\n", rx->skb->data))
391 return;
392
Harvey Harrisona7767f92008-07-02 16:30:51 -0700393 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg38f37142008-01-29 17:07:43 +0100394 return;
395
Harvey Harrisona7767f92008-07-02 16:30:51 -0700396 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100397 if (rx->flags & IEEE80211_RX_AMSDU)
Johannes Berg38f37142008-01-29 17:07:43 +0100398 hdrlen += ETH_HLEN;
Johannes Bergd1c3a372009-01-07 00:26:10 +0100399 WARN_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3,
400 "unaligned IP payload at 0x%p\n", rx->skb->data + hdrlen);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200401}
402
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200403
Johannes Berg571ecf62007-07-27 15:43:22 +0200404/* rx handlers */
405
Johannes Berg49461622008-06-30 15:10:45 +0200406static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100407ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200408{
409 struct ieee80211_local *local = rx->local;
410 struct sk_buff *skb = rx->skb;
411
Helmut Schaafbe9c422009-07-23 12:14:04 +0200412 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
Johannes Bergf1d58c22009-06-17 13:13:00 +0200413 return ieee80211_scan_rx(rx->sdata, skb);
Zhu Yiece8edd2007-11-22 10:53:21 +0800414
Helmut Schaa142b9f52009-07-23 13:18:01 +0200415 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
416 (rx->flags & IEEE80211_RX_IN_SCAN))) {
Zhu Yiece8edd2007-11-22 10:53:21 +0800417 /* drop all the other packets during a software scan anyway */
Johannes Bergf1d58c22009-06-17 13:13:00 +0200418 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
Zhu Yiece8edd2007-11-22 10:53:21 +0800419 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100420 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200421 }
422
Johannes Berg5cf121c2008-02-25 16:27:43 +0100423 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200424 /* scanning finished during invoking of handlers */
425 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100426 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200427 }
428
Johannes Berg9ae54c82008-01-31 19:48:20 +0100429 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200430}
431
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200432
433static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
434{
435 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
436
437 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
438 return 0;
439
440 return ieee80211_is_robust_mgmt_frame(hdr);
441}
442
443
444static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
445{
446 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
447
448 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
449 return 0;
450
451 return ieee80211_is_robust_mgmt_frame(hdr);
452}
453
454
455/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
456static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
457{
458 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
459 struct ieee80211_mmie *mmie;
460
461 if (skb->len < 24 + sizeof(*mmie) ||
462 !is_multicast_ether_addr(hdr->da))
463 return -1;
464
465 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
466 return -1; /* not a robust management frame */
467
468 mmie = (struct ieee80211_mmie *)
469 (skb->data + skb->len - sizeof(*mmie));
470 if (mmie->element_id != WLAN_EID_MMIE ||
471 mmie->length != sizeof(*mmie) - 2)
472 return -1;
473
474 return le16_to_cpu(mmie->key_id);
475}
476
477
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100478static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100479ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100480{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700481 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
482 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100483 char *dev_addr = rx->sdata->dev->dev_addr;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100484
Harvey Harrisona7767f92008-07-02 16:30:51 -0700485 if (ieee80211_is_data(hdr->frame_control)) {
Javier Cardona3c5772a2009-08-10 12:15:48 -0700486 if (is_multicast_ether_addr(hdr->addr1)) {
487 if (ieee80211_has_tods(hdr->frame_control) ||
488 !ieee80211_has_fromds(hdr->frame_control))
489 return RX_DROP_MONITOR;
490 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
491 return RX_DROP_MONITOR;
492 } else {
493 if (!ieee80211_has_a4(hdr->frame_control))
494 return RX_DROP_MONITOR;
495 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
496 return RX_DROP_MONITOR;
497 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100498 }
499
500 /* If there is not an established peer link and this is not a peer link
501 * establisment frame, beacon or probe, drop the frame.
502 */
503
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800504 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100505 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100506
Harvey Harrisona7767f92008-07-02 16:30:51 -0700507 if (!ieee80211_is_mgmt(hdr->frame_control))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100508 return RX_DROP_MONITOR;
509
Harvey Harrisona7767f92008-07-02 16:30:51 -0700510 if (ieee80211_is_action(hdr->frame_control)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100511 mgmt = (struct ieee80211_mgmt *)hdr;
Rui Paulo09383932009-11-09 23:46:43 +0000512 if (mgmt->u.action.category != MESH_PLINK_CATEGORY)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100513 return RX_DROP_MONITOR;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100514 return RX_CONTINUE;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100515 }
516
Harvey Harrisona7767f92008-07-02 16:30:51 -0700517 if (ieee80211_is_probe_req(hdr->frame_control) ||
518 ieee80211_is_probe_resp(hdr->frame_control) ||
519 ieee80211_is_beacon(hdr->frame_control))
520 return RX_CONTINUE;
521
522 return RX_DROP_MONITOR;
523
524 }
525
526#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
527
528 if (ieee80211_is_data(hdr->frame_control) &&
529 is_multicast_ether_addr(hdr->addr1) &&
Javier Cardona3c5772a2009-08-10 12:15:48 -0700530 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100531 return RX_DROP_MONITOR;
Johannes Berg902acc72008-02-23 15:17:19 +0100532#undef msh_h_get
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100533
Johannes Berg902acc72008-02-23 15:17:19 +0100534 return RX_CONTINUE;
535}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100536
Johannes Berg1edfb1a2009-11-25 17:46:16 +0100537#define SEQ_MODULO 0x1000
538#define SEQ_MASK 0xfff
539
540static inline int seq_less(u16 sq1, u16 sq2)
541{
542 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
543}
544
545static inline u16 seq_inc(u16 sq)
546{
547 return (sq + 1) & SEQ_MASK;
548}
549
550static inline u16 seq_sub(u16 sq1, u16 sq2)
551{
552 return (sq1 - sq2) & SEQ_MASK;
553}
554
555
556static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
557 struct tid_ampdu_rx *tid_agg_rx,
558 int index)
559{
560 struct ieee80211_supported_band *sband;
561 struct ieee80211_rate *rate = NULL;
562 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
563 struct ieee80211_rx_status *status;
564
565 if (!skb)
566 goto no_frame;
567
568 status = IEEE80211_SKB_RXCB(skb);
569
570 /* release the reordered frames to stack */
571 sband = hw->wiphy->bands[status->band];
572 if (!(status->flag & RX_FLAG_HT))
573 rate = &sband->bitrates[status->rate_idx];
574 __ieee80211_rx_handle_packet(hw, skb, rate);
575 tid_agg_rx->stored_mpdu_num--;
576 tid_agg_rx->reorder_buf[index] = NULL;
577
578no_frame:
579 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
580}
581
582static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
583 struct tid_ampdu_rx *tid_agg_rx,
584 u16 head_seq_num)
585{
586 int index;
587
588 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
589 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
590 tid_agg_rx->buf_size;
591 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
592 }
593}
594
595/*
596 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
597 * the skb was added to the buffer longer than this time ago, the earlier
598 * frames that have not yet been received are assumed to be lost and the skb
599 * can be released for processing. This may also release other skb's from the
600 * reorder buffer if there are no additional gaps between the frames.
601 */
602#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
603
604/*
605 * As this function belongs to the RX path it must be under
606 * rcu_read_lock protection. It returns false if the frame
607 * can be processed immediately, true if it was consumed.
608 */
609static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
610 struct tid_ampdu_rx *tid_agg_rx,
611 struct sk_buff *skb)
612{
613 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
614 u16 sc = le16_to_cpu(hdr->seq_ctrl);
615 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
616 u16 head_seq_num, buf_size;
617 int index;
618
619 buf_size = tid_agg_rx->buf_size;
620 head_seq_num = tid_agg_rx->head_seq_num;
621
622 /* frame with out of date sequence number */
623 if (seq_less(mpdu_seq_num, head_seq_num)) {
624 dev_kfree_skb(skb);
625 return true;
626 }
627
628 /*
629 * If frame the sequence number exceeds our buffering window
630 * size release some previous frames to make room for this one.
631 */
632 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
633 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
634 /* release stored frames up to new head to stack */
635 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
636 }
637
638 /* Now the new frame is always in the range of the reordering buffer */
639
640 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
641
642 /* check if we already stored this frame */
643 if (tid_agg_rx->reorder_buf[index]) {
644 dev_kfree_skb(skb);
645 return true;
646 }
647
648 /*
649 * If the current MPDU is in the right order and nothing else
650 * is stored we can process it directly, no need to buffer it.
651 */
652 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
653 tid_agg_rx->stored_mpdu_num == 0) {
654 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
655 return false;
656 }
657
658 /* put the frame in the reordering buffer */
659 tid_agg_rx->reorder_buf[index] = skb;
660 tid_agg_rx->reorder_time[index] = jiffies;
661 tid_agg_rx->stored_mpdu_num++;
662 /* release the buffer until next missing frame */
663 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
664 tid_agg_rx->buf_size;
665 if (!tid_agg_rx->reorder_buf[index] &&
666 tid_agg_rx->stored_mpdu_num > 1) {
667 /*
668 * No buffers ready to be released, but check whether any
669 * frames in the reorder buffer have timed out.
670 */
671 int j;
672 int skipped = 1;
673 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
674 j = (j + 1) % tid_agg_rx->buf_size) {
675 if (!tid_agg_rx->reorder_buf[j]) {
676 skipped++;
677 continue;
678 }
679 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
680 HT_RX_REORDER_BUF_TIMEOUT))
681 break;
682
683#ifdef CONFIG_MAC80211_HT_DEBUG
684 if (net_ratelimit())
685 printk(KERN_DEBUG "%s: release an RX reorder "
686 "frame due to timeout on earlier "
687 "frames\n",
688 wiphy_name(hw->wiphy));
689#endif
690 ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
691
692 /*
693 * Increment the head seq# also for the skipped slots.
694 */
695 tid_agg_rx->head_seq_num =
696 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
697 skipped = 0;
698 }
699 } else while (tid_agg_rx->reorder_buf[index]) {
700 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
701 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
702 tid_agg_rx->buf_size;
703 }
704
705 return true;
706}
707
708/*
709 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
710 * true if the MPDU was buffered, false if it should be processed.
711 */
712static bool ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
713 struct sk_buff *skb)
714{
715 struct ieee80211_hw *hw = &local->hw;
716 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
717 struct sta_info *sta;
718 struct tid_ampdu_rx *tid_agg_rx;
719 u16 sc;
720 int tid;
721
722 if (!ieee80211_is_data_qos(hdr->frame_control))
723 return false;
724
725 /*
726 * filter the QoS data rx stream according to
727 * STA/TID and check if this STA/TID is on aggregation
728 */
729
730 sta = sta_info_get(local, hdr->addr2);
731 if (!sta)
732 return false;
733
734 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
735
736 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
737 return false;
738
739 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
740
741 /* qos null data frames are excluded */
742 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
743 return false;
744
745 /* new, potentially un-ordered, ampdu frame - process it */
746
747 /* reset session timer */
748 if (tid_agg_rx->timeout)
749 mod_timer(&tid_agg_rx->session_timer,
750 TU_TO_EXP_TIME(tid_agg_rx->timeout));
751
752 /* if this mpdu is fragmented - terminate rx aggregation session */
753 sc = le16_to_cpu(hdr->seq_ctrl);
754 if (sc & IEEE80211_SCTL_FRAG) {
755 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
756 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
757 dev_kfree_skb(skb);
758 return true;
759 }
760
761 return ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb);
762}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100763
Johannes Berg49461622008-06-30 15:10:45 +0200764static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100765ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200766{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700767 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200768
769 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
770 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700771 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100772 rx->sta->last_seq_ctrl[rx->queue] ==
Johannes Berg571ecf62007-07-27 15:43:22 +0200773 hdr->seq_ctrl)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100774 if (rx->flags & IEEE80211_RX_RA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200775 rx->local->dot11FrameDuplicateCount++;
776 rx->sta->num_duplicates++;
777 }
Johannes Berge4c26ad2008-01-31 19:48:21 +0100778 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200779 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100780 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +0200781 }
782
Johannes Berg571ecf62007-07-27 15:43:22 +0200783 if (unlikely(rx->skb->len < 16)) {
784 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100785 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200786 }
787
Johannes Berg571ecf62007-07-27 15:43:22 +0200788 /* Drop disallowed frame classes based on STA auth/assoc state;
789 * IEEE 802.11, Chap 5.5.
790 *
Johannes Bergccd7b362008-09-11 00:01:56 +0200791 * mac80211 filters only based on association state, i.e. it drops
792 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +0200793 * deauth/disassoc frames when needed. In addition, hostapd is
794 * responsible for filtering on both auth and assoc states.
795 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100796
Johannes Berg902acc72008-02-23 15:17:19 +0100797 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100798 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100799
Harvey Harrisona7767f92008-07-02 16:30:51 -0700800 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
801 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200802 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Johannes Berg07346f812008-05-03 01:02:02 +0200803 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700804 if ((!ieee80211_has_fromds(hdr->frame_control) &&
805 !ieee80211_has_tods(hdr->frame_control) &&
806 ieee80211_is_data(hdr->frame_control)) ||
807 !(rx->flags & IEEE80211_RX_RA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200808 /* Drop IBSS frames and frames for other hosts
809 * silently. */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100810 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200811 }
812
Johannes Berge4c26ad2008-01-31 19:48:21 +0100813 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200814 }
815
Johannes Berg9ae54c82008-01-31 19:48:20 +0100816 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200817}
818
819
Johannes Berg49461622008-06-30 15:10:45 +0200820static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100821ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200822{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100823 struct sk_buff *skb = rx->skb;
824 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
825 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400826 int keyidx;
827 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100828 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400829 struct ieee80211_key *stakey = NULL;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200830 int mmie_keyidx = -1;
Johannes Berg570bd532007-07-27 15:43:22 +0200831
Johannes Berg3017b802007-08-28 17:01:53 -0400832 /*
833 * Key selection 101
834 *
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200835 * There are four types of keys:
Johannes Berg3017b802007-08-28 17:01:53 -0400836 * - GTK (group keys)
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200837 * - IGTK (group keys for management frames)
Johannes Berg3017b802007-08-28 17:01:53 -0400838 * - PTK (pairwise keys)
839 * - STK (station-to-station pairwise keys)
840 *
841 * When selecting a key, we have to distinguish between multicast
842 * (including broadcast) and unicast frames, the latter can only
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200843 * use PTKs and STKs while the former always use GTKs and IGTKs.
844 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
845 * unicast frames can also use key indices like GTKs. Hence, if we
846 * don't have a PTK/STK we check the key index for a WEP key.
Johannes Berg3017b802007-08-28 17:01:53 -0400847 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400848 * Note that in a regular BSS, multicast frames are sent by the
849 * AP only, associated stations unicast the frame to the AP first
850 * which then multicasts it on their behalf.
851 *
Johannes Berg3017b802007-08-28 17:01:53 -0400852 * There is also a slight problem in IBSS mode: GTKs are negotiated
853 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400854 * The spec seems to expect that one negotiates the same key with
855 * every station but there's no such requirement; VLANs could be
856 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400857 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200858
Johannes Berg3017b802007-08-28 17:01:53 -0400859 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200860 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400861 * addressed to us nor a multicast frame.
862 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100863 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100864 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400865
Johannes Bergd4e46a32007-09-14 11:10:24 -0400866 if (rx->sta)
867 stakey = rcu_dereference(rx->sta->key);
868
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300869 if (!ieee80211_has_protected(hdr->frame_control))
870 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
871
Johannes Bergd4e46a32007-09-14 11:10:24 -0400872 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
873 rx->key = stakey;
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300874 /* Skip decryption if the frame is not protected. */
875 if (!ieee80211_has_protected(hdr->frame_control))
876 return RX_CONTINUE;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200877 } else if (mmie_keyidx >= 0) {
878 /* Broadcast/multicast robust management frame / BIP */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100879 if ((status->flag & RX_FLAG_DECRYPTED) &&
880 (status->flag & RX_FLAG_IV_STRIPPED))
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200881 return RX_CONTINUE;
882
883 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
884 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
885 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
886 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300887 } else if (!ieee80211_has_protected(hdr->frame_control)) {
888 /*
889 * The frame was not protected, so skip decryption. However, we
890 * need to set rx->key if there is a key that could have been
891 * used so that the frame may be dropped if encryption would
892 * have been expected.
893 */
894 struct ieee80211_key *key = NULL;
895 if (ieee80211_is_mgmt(hdr->frame_control) &&
896 is_multicast_ether_addr(hdr->addr1) &&
897 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
898 rx->key = key;
899 else if ((key = rcu_dereference(rx->sdata->default_key)))
900 rx->key = key;
901 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200902 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400903 /*
904 * The device doesn't give us the IV so we won't be
905 * able to look up the key. That's ok though, we
906 * don't need to decrypt the frame, we just won't
907 * be able to keep statistics accurate.
908 * Except for key threshold notifications, should
909 * we somehow allow the driver to tell us which key
910 * the hardware used if this flag is set?
911 */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100912 if ((status->flag & RX_FLAG_DECRYPTED) &&
913 (status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100914 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200915
Harvey Harrisona7767f92008-07-02 16:30:51 -0700916 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +0200917
Johannes Berg3017b802007-08-28 17:01:53 -0400918 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100919 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200920
Johannes Berg3017b802007-08-28 17:01:53 -0400921 /*
922 * no need to call ieee80211_wep_get_keyidx,
923 * it verifies a bunch of things we've done already
924 */
925 keyidx = rx->skb->data[hdrlen + 3] >> 6;
926
Johannes Bergd4e46a32007-09-14 11:10:24 -0400927 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400928
929 /*
930 * RSNA-protected unicast frames should always be sent with
931 * pairwise or station-to-station keys, but for WEP we allow
932 * using a key index as well.
933 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400934 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400935 !is_multicast_ether_addr(hdr->addr1))
936 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200937 }
938
Johannes Berg3017b802007-08-28 17:01:53 -0400939 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200940 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400941 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200942 } else {
Johannes Berge4c26ad2008-01-31 19:48:21 +0100943 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +0200944 }
945
Johannes Berg1990af82007-09-26 17:53:15 +0200946 /* Check for weak IVs if possible */
947 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
Harvey Harrisona7767f92008-07-02 16:30:51 -0700948 ieee80211_is_data(hdr->frame_control) &&
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100949 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
950 !(status->flag & RX_FLAG_DECRYPTED)) &&
Johannes Berg1990af82007-09-26 17:53:15 +0200951 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
952 rx->sta->wep_weak_iv_count++;
953
Johannes Berg70f08762007-09-26 17:53:14 +0200954 switch (rx->key->conf.alg) {
955 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200956 result = ieee80211_crypto_wep_decrypt(rx);
957 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200958 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200959 result = ieee80211_crypto_tkip_decrypt(rx);
960 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200961 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200962 result = ieee80211_crypto_ccmp_decrypt(rx);
963 break;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +0200964 case ALG_AES_CMAC:
965 result = ieee80211_crypto_aes_cmac_decrypt(rx);
966 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200967 }
968
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200969 /* either the frame has been decrypted or will be dropped */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +0100970 status->flag |= RX_FLAG_DECRYPTED;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200971
972 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200973}
974
Kalle Valo572e0012009-02-10 17:09:31 +0200975static ieee80211_rx_result debug_noinline
976ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
977{
978 struct ieee80211_local *local;
979 struct ieee80211_hdr *hdr;
980 struct sk_buff *skb;
981
982 local = rx->local;
983 skb = rx->skb;
984 hdr = (struct ieee80211_hdr *) skb->data;
985
986 if (!local->pspolling)
987 return RX_CONTINUE;
988
989 if (!ieee80211_has_fromds(hdr->frame_control))
990 /* this is not from AP */
991 return RX_CONTINUE;
992
993 if (!ieee80211_is_data(hdr->frame_control))
994 return RX_CONTINUE;
995
996 if (!ieee80211_has_moredata(hdr->frame_control)) {
997 /* AP has no more frames buffered for us */
998 local->pspolling = false;
999 return RX_CONTINUE;
1000 }
1001
1002 /* more data bit is set, let's request a new frame from the AP */
1003 ieee80211_send_pspoll(local, rx->sdata);
1004
1005 return RX_CONTINUE;
1006}
1007
Johannes Berg133b8222008-09-16 14:18:59 +02001008static void ap_sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001009{
Johannes Berg133b8222008-09-16 14:18:59 +02001010 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +01001011 struct ieee80211_local *local = sdata->local;
Joe Perches0795af52007-10-03 17:59:30 -07001012
Johannes Berg3e122be2008-07-09 14:40:34 +02001013 atomic_inc(&sdata->bss->num_sta_ps);
Johannes Bergaf818582009-11-06 11:35:50 +01001014 set_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg24487982009-04-23 18:52:52 +02001015 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001016#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07001017 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1018 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +02001019#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1020}
1021
Johannes Bergff9458d2009-10-30 12:56:02 +01001022static void ap_sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +02001023{
Johannes Berg133b8222008-09-16 14:18:59 +02001024 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001025
Johannes Berg3e122be2008-07-09 14:40:34 +02001026 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg004c8722008-02-20 11:21:35 +01001027
Johannes Bergaf818582009-11-06 11:35:50 +01001028 clear_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg004c8722008-02-20 11:21:35 +01001029
Johannes Berg571ecf62007-07-27 15:43:22 +02001030#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07001031 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1032 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +02001033#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg004c8722008-02-20 11:21:35 +01001034
Johannes Bergaf818582009-11-06 11:35:50 +01001035 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001036#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Bergaf818582009-11-06 11:35:50 +01001037 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1038 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +02001039#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Bergaf818582009-11-06 11:35:50 +01001040 return;
1041 }
1042
1043 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001044}
1045
Johannes Berg49461622008-06-30 15:10:45 +02001046static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001047ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001048{
1049 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001050 struct sk_buff *skb = rx->skb;
1051 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1052 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001053
1054 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001055 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001056
Johannes Bergb291ba12009-07-10 15:29:03 +02001057 /*
1058 * Update last_rx only for IBSS packets which are for the current
1059 * BSSID to avoid keeping the current IBSS network alive in cases
1060 * where other STAs start using different BSSID.
1061 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001062 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001063 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +02001064 NL80211_IFTYPE_ADHOC);
Johannes Berg46900292009-02-15 12:44:28 +01001065 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
Johannes Berg571ecf62007-07-27 15:43:22 +02001066 sta->last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +02001067 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1068 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001069 * Mesh beacons will update last_rx when if they are found to
1070 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +02001071 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001072 sta->last_rx = jiffies;
Johannes Berg571ecf62007-07-27 15:43:22 +02001073 }
1074
Johannes Berg5cf121c2008-02-25 16:27:43 +01001075 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001076 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001077
Kalle Valo3cf335d2009-03-22 21:57:06 +02001078 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1079 ieee80211_sta_rx_notify(rx->sdata, hdr);
1080
Johannes Berg571ecf62007-07-27 15:43:22 +02001081 sta->rx_fragments++;
1082 sta->rx_bytes += rx->skb->len;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001083 sta->last_signal = status->signal;
1084 sta->last_noise = status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +02001085
Johannes Berg72eaa432008-11-26 15:02:58 +01001086 /*
1087 * Change STA power saving mode only at the end of a frame
1088 * exchange sequence.
1089 */
Johannes Berg3e122be2008-07-09 14:40:34 +02001090 if (!ieee80211_has_morefrags(hdr->frame_control) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001091 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1092 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergaf818582009-11-06 11:35:50 +01001093 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
Johannes Berg72eaa432008-11-26 15:02:58 +01001094 /*
1095 * Ignore doze->wake transitions that are
1096 * indicated by non-data frames, the standard
1097 * is unclear here, but for example going to
1098 * PS mode and then scanning would cause a
1099 * doze->wake transition for the probe request,
1100 * and that is clearly undesirable.
1101 */
1102 if (ieee80211_is_data(hdr->frame_control) &&
1103 !ieee80211_has_pm(hdr->frame_control))
Johannes Bergff9458d2009-10-30 12:56:02 +01001104 ap_sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +01001105 } else {
1106 if (ieee80211_has_pm(hdr->frame_control))
1107 ap_sta_ps_start(sta);
1108 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001109 }
1110
Johannes Berg22403de2009-10-30 12:55:03 +01001111 /*
1112 * Drop (qos-)data::nullfunc frames silently, since they
1113 * are used only to control station power saving mode.
1114 */
1115 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1116 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001117 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Johannes Berg22403de2009-10-30 12:55:03 +01001118 /*
1119 * Update counter and free packet here to avoid
1120 * counting this as a dropped packed.
1121 */
Johannes Berg571ecf62007-07-27 15:43:22 +02001122 sta->rx_packets++;
1123 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001124 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001125 }
1126
Johannes Berg9ae54c82008-01-31 19:48:20 +01001127 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001128} /* ieee80211_rx_h_sta_process */
1129
Johannes Berg571ecf62007-07-27 15:43:22 +02001130static inline struct ieee80211_fragment_entry *
1131ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1132 unsigned int frag, unsigned int seq, int rx_queue,
1133 struct sk_buff **skb)
1134{
1135 struct ieee80211_fragment_entry *entry;
1136 int idx;
1137
1138 idx = sdata->fragment_next;
1139 entry = &sdata->fragments[sdata->fragment_next++];
1140 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1141 sdata->fragment_next = 0;
1142
1143 if (!skb_queue_empty(&entry->skb_list)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001144#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg571ecf62007-07-27 15:43:22 +02001145 struct ieee80211_hdr *hdr =
1146 (struct ieee80211_hdr *) entry->skb_list.next->data;
1147 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1148 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Johannes Berg0c68ae262008-10-27 15:56:10 -07001149 "addr1=%pM addr2=%pM\n",
Johannes Berg571ecf62007-07-27 15:43:22 +02001150 sdata->dev->name, idx,
1151 jiffies - entry->first_frag_time, entry->seq,
Johannes Berg0c68ae262008-10-27 15:56:10 -07001152 entry->last_frag, hdr->addr1, hdr->addr2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001153#endif
Johannes Berg571ecf62007-07-27 15:43:22 +02001154 __skb_queue_purge(&entry->skb_list);
1155 }
1156
1157 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1158 *skb = NULL;
1159 entry->first_frag_time = jiffies;
1160 entry->seq = seq;
1161 entry->rx_queue = rx_queue;
1162 entry->last_frag = frag;
1163 entry->ccmp = 0;
1164 entry->extra_len = 0;
1165
1166 return entry;
1167}
1168
1169static inline struct ieee80211_fragment_entry *
1170ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001171 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +02001172 int rx_queue, struct ieee80211_hdr *hdr)
1173{
1174 struct ieee80211_fragment_entry *entry;
1175 int i, idx;
1176
1177 idx = sdata->fragment_next;
1178 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1179 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +02001180
1181 idx--;
1182 if (idx < 0)
1183 idx = IEEE80211_FRAGMENT_MAX - 1;
1184
1185 entry = &sdata->fragments[idx];
1186 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1187 entry->rx_queue != rx_queue ||
1188 entry->last_frag + 1 != frag)
1189 continue;
1190
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001191 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001192
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001193 /*
1194 * Check ftype and addresses are equal, else check next fragment
1195 */
1196 if (((hdr->frame_control ^ f_hdr->frame_control) &
1197 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Johannes Berg571ecf62007-07-27 15:43:22 +02001198 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1199 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1200 continue;
1201
S.Çağlar Onurab466232008-02-14 17:36:47 +02001202 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001203 __skb_queue_purge(&entry->skb_list);
1204 continue;
1205 }
1206 return entry;
1207 }
1208
1209 return NULL;
1210}
1211
Johannes Berg49461622008-06-30 15:10:45 +02001212static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001213ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001214{
1215 struct ieee80211_hdr *hdr;
1216 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001217 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001218 unsigned int frag, seq;
1219 struct ieee80211_fragment_entry *entry;
1220 struct sk_buff *skb;
1221
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001222 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001223 fc = hdr->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02001224 sc = le16_to_cpu(hdr->seq_ctrl);
1225 frag = sc & IEEE80211_SCTL_FRAG;
1226
Harvey Harrison358c8d92008-07-15 18:44:13 -07001227 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
Johannes Berg571ecf62007-07-27 15:43:22 +02001228 (rx->skb)->len < 24 ||
1229 is_multicast_ether_addr(hdr->addr1))) {
1230 /* not fragmented */
1231 goto out;
1232 }
1233 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1234
1235 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1236
1237 if (frag == 0) {
1238 /* This is the first fragment of a new frame. */
1239 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001240 rx->queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -04001241 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001242 ieee80211_has_protected(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001243 /* Store CCMP PN so that we can verify that the next
1244 * fragment has a sequential PN value. */
1245 entry->ccmp = 1;
1246 memcpy(entry->last_pn,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001247 rx->key->u.ccmp.rx_pn[rx->queue],
Johannes Berg571ecf62007-07-27 15:43:22 +02001248 CCMP_PN_LEN);
1249 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001250 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001251 }
1252
1253 /* This is a fragment for a frame that should already be pending in
1254 * fragment cache. Add this fragment to the end of the pending entry.
1255 */
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001256 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001257 if (!entry) {
1258 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001259 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001260 }
1261
1262 /* Verify that MPDUs within one MSDU have sequential PN values.
1263 * (IEEE 802.11i, 8.3.3.4.5) */
1264 if (entry->ccmp) {
1265 int i;
1266 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -04001267 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001268 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001269 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1270 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1271 pn[i]++;
1272 if (pn[i])
1273 break;
1274 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001275 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001276 if (memcmp(pn, rpn, CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001277 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001278 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1279 }
1280
Harvey Harrison358c8d92008-07-15 18:44:13 -07001281 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02001282 __skb_queue_tail(&entry->skb_list, rx->skb);
1283 entry->last_frag = frag;
1284 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001285 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001286 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001287 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001288 }
1289
1290 rx->skb = __skb_dequeue(&entry->skb_list);
1291 if (skb_tailroom(rx->skb) < entry->extra_len) {
1292 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1293 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1294 GFP_ATOMIC))) {
1295 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1296 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001297 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001298 }
1299 }
1300 while ((skb = __skb_dequeue(&entry->skb_list))) {
1301 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1302 dev_kfree_skb(skb);
1303 }
1304
1305 /* Complete frame has been reassembled - process it now */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001306 rx->flags |= IEEE80211_RX_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001307
1308 out:
1309 if (rx->sta)
1310 rx->sta->rx_packets++;
1311 if (is_multicast_ether_addr(hdr->addr1))
1312 rx->local->dot11MulticastReceivedFrameCount++;
1313 else
1314 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001315 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001316}
1317
Johannes Berg49461622008-06-30 15:10:45 +02001318static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001319ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001320{
Johannes Bergaf818582009-11-06 11:35:50 +01001321 struct ieee80211_sub_if_data *sdata = rx->sdata;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001322 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02001323
Harvey Harrison358c8d92008-07-15 18:44:13 -07001324 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
Johannes Berg5cf121c2008-02-25 16:27:43 +01001325 !(rx->flags & IEEE80211_RX_RA_MATCH)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001326 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001327
Johannes Berg05c914f2008-09-11 00:01:58 +02001328 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1329 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001330 return RX_DROP_UNUSABLE;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +02001331
Johannes Bergaf818582009-11-06 11:35:50 +01001332 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1333 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1334 else
1335 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
Johannes Berg571ecf62007-07-27 15:43:22 +02001336
Johannes Berg9ae54c82008-01-31 19:48:20 +01001337 /* Free PS Poll skb here instead of returning RX_DROP that would
Johannes Berg571ecf62007-07-27 15:43:22 +02001338 * count as an dropped frame. */
1339 dev_kfree_skb(rx->skb);
1340
Johannes Berg9ae54c82008-01-31 19:48:20 +01001341 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001342}
1343
Johannes Berg49461622008-06-30 15:10:45 +02001344static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001345ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +02001346{
Johannes Berg6e0d1142007-07-27 15:43:22 +02001347 u8 *data = rx->skb->data;
Harvey Harrison238f74a2008-07-02 11:05:34 -07001348 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001349
Harvey Harrison238f74a2008-07-02 11:05:34 -07001350 if (!ieee80211_is_data_qos(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001351 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001352
1353 /* remove the qos control field, update frame type and meta-data */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001354 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1355 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1356 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001357 /* change frame type to non QOS */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001358 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001359
Johannes Berg9ae54c82008-01-31 19:48:20 +01001360 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001361}
1362
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001363static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001364ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001365{
Johannes Berg07346f812008-05-03 01:02:02 +02001366 if (unlikely(!rx->sta ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001367 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001368 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001369
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001370 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001371}
1372
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001373static int
Harvey Harrison358c8d92008-07-15 18:44:13 -07001374ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02001375{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001376 struct sk_buff *skb = rx->skb;
1377 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1378
Johannes Berg3017b802007-08-28 17:01:53 -04001379 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001380 * Pass through unencrypted frames if the hardware has
1381 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001382 */
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001383 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001384 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001385
1386 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001387 if (unlikely(!ieee80211_has_protected(fc) &&
1388 !ieee80211_is_nullfunc(fc) &&
Jouni Malinenf2ca3ea42009-05-08 12:36:03 +03001389 ieee80211_is_data(fc) &&
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001390 (rx->key || rx->sdata->drop_unencrypted)))
1391 return -EACCES;
Jouni Malinenf2ca3ea42009-05-08 12:36:03 +03001392 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1393 if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1394 rx->key))
1395 return -EACCES;
1396 /* BIP does not use Protected field, so need to check MMIE */
1397 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
1398 && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
1399 rx->key))
1400 return -EACCES;
1401 /*
1402 * When using MFP, Action frames are not allowed prior to
1403 * having configured keys.
1404 */
1405 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1406 ieee80211_is_robust_mgmt_frame(
1407 (struct ieee80211_hdr *) rx->skb->data)))
1408 return -EACCES;
1409 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02001410
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001411 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001412}
1413
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001414static int
Zhu Yie31a16d2009-05-21 21:47:03 +08001415__ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001416{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001417 struct ieee80211_sub_if_data *sdata = rx->sdata;
1418 struct net_device *dev = sdata->dev;
Felix Fietkauf14543e2009-11-10 20:10:05 +01001419 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1420
Johannes Berg9bc383d2009-11-19 11:55:19 +01001421 if (ieee80211_has_a4(hdr->frame_control) &&
1422 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
Felix Fietkauf14543e2009-11-10 20:10:05 +01001423 return -1;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001424
1425 if (is_multicast_ether_addr(hdr->addr1) &&
1426 ((sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta) ||
1427 (sdata->vif.type == NL80211_IFTYPE_STATION && sdata->u.mgd.use_4addr)))
Felix Fietkauf14543e2009-11-10 20:10:05 +01001428 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001429
Zhu Yie31a16d2009-05-21 21:47:03 +08001430 return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001431}
Johannes Berg571ecf62007-07-27 15:43:22 +02001432
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001433/*
1434 * requires that rx->skb is a frame with ethernet header
1435 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001436static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001437{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301438 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001439 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1440 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1441
1442 /*
1443 * Allow EAPOL frames to us/the PAE group address regardless
1444 * of whether the frame was encrypted or not.
1445 */
1446 if (ehdr->h_proto == htons(ETH_P_PAE) &&
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001447 (compare_ether_addr(ehdr->h_dest, rx->sdata->dev->dev_addr) == 0 ||
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001448 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1449 return true;
1450
1451 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07001452 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001453 return false;
1454
1455 return true;
1456}
1457
1458/*
1459 * requires that rx->skb is a frame with ethernet header
1460 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001461static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01001462ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001463{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001464 struct ieee80211_sub_if_data *sdata = rx->sdata;
1465 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001466 struct ieee80211_local *local = rx->local;
1467 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001468 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1469 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001470
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001471 skb = rx->skb;
1472 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001473
Johannes Berg05c914f2008-09-11 00:01:58 +02001474 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1475 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02001476 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Johannes Berg9bc383d2009-11-19 11:55:19 +01001477 (rx->flags & IEEE80211_RX_RA_MATCH) &&
1478 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001479 if (is_multicast_ether_addr(ehdr->h_dest)) {
1480 /*
1481 * send multicast frames both to higher layers in
1482 * local net stack and back to the wireless medium
1483 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001484 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1485 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001486 printk(KERN_DEBUG "%s: failed to clone "
1487 "multicast frame\n", dev->name);
1488 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001489 dsta = sta_info_get(local, skb->data);
Johannes Bergd0709a62008-02-25 16:27:46 +01001490 if (dsta && dsta->sdata->dev == dev) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001491 /*
1492 * The destination station is associated to
1493 * this AP (in this VLAN), so send the frame
1494 * directly to it and do not pass it to local
1495 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001496 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001497 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001498 skb = NULL;
1499 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001500 }
1501 }
1502
1503 if (skb) {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001504 int align __maybe_unused;
1505
1506#if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1507 /*
1508 * 'align' will only take the values 0 or 2 here
1509 * since all frames are required to be aligned
1510 * to 2-byte boundaries when being passed to
1511 * mac80211. That also explains the __skb_push()
1512 * below.
1513 */
matthieu castetdacb6f12009-06-04 22:16:18 +02001514 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01001515 if (align) {
1516 if (WARN_ON(skb_headroom(skb) < 3)) {
1517 dev_kfree_skb(skb);
1518 skb = NULL;
1519 } else {
1520 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07001521 size_t len = skb_headlen(skb);
1522 skb->data -= align;
1523 memmove(skb->data, data, len);
1524 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01001525 }
1526 }
1527#endif
1528
1529 if (skb) {
1530 /* deliver to local stack */
1531 skb->protocol = eth_type_trans(skb, dev);
1532 memset(skb->cb, 0, sizeof(skb->cb));
1533 netif_rx(skb);
1534 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001535 }
1536
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001537 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001538 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001539 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001540 skb_reset_network_header(xmit_skb);
1541 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001542 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001543 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001544}
1545
Johannes Berg49461622008-06-30 15:10:45 +02001546static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001547ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001548{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001549 struct net_device *dev = rx->sdata->dev;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001550 struct ieee80211_local *local = rx->local;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001551 u16 ethertype;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001552 u8 *payload;
1553 struct sk_buff *skb = rx->skb, *frame = NULL;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001554 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1555 __le16 fc = hdr->frame_control;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001556 const struct ethhdr *eth;
1557 int remaining, err;
1558 u8 dst[ETH_ALEN];
1559 u8 src[ETH_ALEN];
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001560
Harvey Harrison358c8d92008-07-15 18:44:13 -07001561 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001562 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001563
Harvey Harrison358c8d92008-07-15 18:44:13 -07001564 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001565 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001566
Johannes Berg5cf121c2008-02-25 16:27:43 +01001567 if (!(rx->flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001568 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001569
Zhu Yie31a16d2009-05-21 21:47:03 +08001570 err = __ieee80211_data_to_8023(rx);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001571 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001572 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001573
1574 skb->dev = dev;
1575
1576 dev->stats.rx_packets++;
1577 dev->stats.rx_bytes += skb->len;
1578
1579 /* skip the wrapping header */
1580 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1581 if (!eth)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001582 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001583
1584 while (skb != frame) {
1585 u8 padding;
1586 __be16 len = eth->h_proto;
1587 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1588
1589 remaining = skb->len;
1590 memcpy(dst, eth->h_dest, ETH_ALEN);
1591 memcpy(src, eth->h_source, ETH_ALEN);
1592
1593 padding = ((4 - subframe_len) & 0x3);
1594 /* the last MSDU has no padding */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001595 if (subframe_len > remaining)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001596 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001597
1598 skb_pull(skb, sizeof(struct ethhdr));
1599 /* if last subframe reuse skb */
1600 if (remaining <= subframe_len + padding)
1601 frame = skb;
1602 else {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001603 /*
1604 * Allocate and reserve two bytes more for payload
1605 * alignment since sizeof(struct ethhdr) is 14.
1606 */
1607 frame = dev_alloc_skb(
1608 ALIGN(local->hw.extra_tx_headroom, 4) +
1609 subframe_len + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001610
1611 if (frame == NULL)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001612 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001613
Johannes Bergd1c3a372009-01-07 00:26:10 +01001614 skb_reserve(frame,
1615 ALIGN(local->hw.extra_tx_headroom, 4) +
1616 sizeof(struct ethhdr) + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001617 memcpy(skb_put(frame, ntohs(len)), skb->data,
1618 ntohs(len));
1619
1620 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1621 padding);
1622 if (!eth) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001623 dev_kfree_skb(frame);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001624 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001625 }
1626 }
1627
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001628 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001629 frame->dev = dev;
1630 frame->priority = skb->priority;
1631 rx->skb = frame;
1632
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001633 payload = frame->data;
1634 ethertype = (payload[6] << 8) | payload[7];
1635
1636 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001637 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1638 compare_ether_addr(payload,
1639 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001640 /* remove RFC1042 or Bridge-Tunnel
1641 * encapsulation and replace EtherType */
1642 skb_pull(frame, 6);
1643 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1644 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1645 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001646 memcpy(skb_push(frame, sizeof(__be16)),
1647 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001648 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1649 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1650 }
1651
Harvey Harrison358c8d92008-07-15 18:44:13 -07001652 if (!ieee80211_frame_allowed(rx, fc)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001653 if (skb == frame) /* last frame */
Johannes Berge4c26ad2008-01-31 19:48:21 +01001654 return RX_DROP_UNUSABLE;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001655 dev_kfree_skb(frame);
1656 continue;
1657 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001658
1659 ieee80211_deliver_skb(rx);
1660 }
1661
Johannes Berg9ae54c82008-01-31 19:48:20 +01001662 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001663}
1664
Ingo Molnarbf94e172008-10-12 23:51:38 -07001665#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02001666static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001667ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1668{
1669 struct ieee80211_hdr *hdr;
1670 struct ieee80211s_hdr *mesh_hdr;
1671 unsigned int hdrlen;
1672 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001673 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001674 struct ieee80211_sub_if_data *sdata = rx->sdata;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001675
1676 hdr = (struct ieee80211_hdr *) skb->data;
1677 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1678 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1679
1680 if (!ieee80211_is_data(hdr->frame_control))
1681 return RX_CONTINUE;
1682
1683 if (!mesh_hdr->ttl)
1684 /* illegal frame */
1685 return RX_DROP_MONITOR;
1686
Javier Cardona43b7b312009-10-15 18:10:51 -07001687 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08001688 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07001689 char *proxied_addr;
1690 char *mpp_addr;
1691
1692 if (is_multicast_ether_addr(hdr->addr1)) {
1693 mpp_addr = hdr->addr3;
1694 proxied_addr = mesh_hdr->eaddr1;
1695 } else {
1696 mpp_addr = hdr->addr4;
1697 proxied_addr = mesh_hdr->eaddr2;
1698 }
YanBo79617de2008-09-22 13:30:32 +08001699
YanBo79617de2008-09-22 13:30:32 +08001700 rcu_read_lock();
Javier Cardona43b7b312009-10-15 18:10:51 -07001701 mppath = mpp_path_lookup(proxied_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001702 if (!mppath) {
Javier Cardona43b7b312009-10-15 18:10:51 -07001703 mpp_path_add(proxied_addr, mpp_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001704 } else {
1705 spin_lock_bh(&mppath->state_lock);
1706 mppath->exp_time = jiffies;
Javier Cardona43b7b312009-10-15 18:10:51 -07001707 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1708 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08001709 spin_unlock_bh(&mppath->state_lock);
1710 }
1711 rcu_read_unlock();
1712 }
1713
Javier Cardona3c5772a2009-08-10 12:15:48 -07001714 /* Frame has reached destination. Don't forward */
1715 if (!is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001716 compare_ether_addr(sdata->dev->dev_addr, hdr->addr3) == 0)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001717 return RX_CONTINUE;
1718
1719 mesh_hdr->ttl--;
1720
1721 if (rx->flags & IEEE80211_RX_RA_MATCH) {
1722 if (!mesh_hdr->ttl)
Johannes Berg472dbc42008-09-11 00:01:49 +02001723 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001724 dropped_frames_ttl);
1725 else {
1726 struct ieee80211_hdr *fwd_hdr;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001727 struct ieee80211_tx_info *info;
1728
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001729 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1730
1731 if (!fwd_skb && net_ratelimit())
1732 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001733 sdata->dev->name);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001734
1735 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001736 memcpy(fwd_hdr->addr2, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001737 info = IEEE80211_SKB_CB(fwd_skb);
1738 memset(info, 0, sizeof(*info));
1739 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001740 info->control.vif = &rx->sdata->vif;
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001741 ieee80211_select_queue(local, fwd_skb);
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001742 if (is_multicast_ether_addr(fwd_hdr->addr1))
1743 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1744 fwded_mcast);
1745 else {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001746 int err;
1747 /*
1748 * Save TA to addr1 to send TA a path error if a
1749 * suitable next hop is not found
1750 */
1751 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
Javier Cardona249b4052009-07-07 10:55:03 -07001752 ETH_ALEN);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001753 err = mesh_nexthop_lookup(fwd_skb, sdata);
Javier Cardona249b4052009-07-07 10:55:03 -07001754 /* Failed to immediately resolve next hop:
1755 * fwded frame was dropped or will be added
1756 * later to the pending skb queue. */
1757 if (err)
1758 return RX_DROP_MONITOR;
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001759
1760 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1761 fwded_unicast);
Javier Cardona249b4052009-07-07 10:55:03 -07001762 }
1763 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1764 fwded_frames);
Johannes Berg3b8d81e2009-06-17 17:43:56 +02001765 ieee80211_add_pending_skb(local, fwd_skb);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001766 }
1767 }
1768
Javier Cardona3c5772a2009-08-10 12:15:48 -07001769 if (is_multicast_ether_addr(hdr->addr1) ||
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001770 sdata->dev->flags & IFF_PROMISC)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001771 return RX_CONTINUE;
1772 else
1773 return RX_DROP_MONITOR;
1774}
Ingo Molnarbf94e172008-10-12 23:51:38 -07001775#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001776
1777static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001778ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001779{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001780 struct ieee80211_sub_if_data *sdata = rx->sdata;
1781 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001782 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1783 __le16 fc = hdr->frame_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001784 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001785
Harvey Harrison358c8d92008-07-15 18:44:13 -07001786 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001787 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001788
Harvey Harrison358c8d92008-07-15 18:44:13 -07001789 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001790 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001791
Felix Fietkauf14543e2009-11-10 20:10:05 +01001792 /*
1793 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1794 * that a 4-addr station can be detected and moved into a separate VLAN
1795 */
1796 if (ieee80211_has_a4(hdr->frame_control) &&
1797 sdata->vif.type == NL80211_IFTYPE_AP)
1798 return RX_DROP_MONITOR;
1799
Zhu Yie31a16d2009-05-21 21:47:03 +08001800 err = __ieee80211_data_to_8023(rx);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001801 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001802 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001803
Harvey Harrison358c8d92008-07-15 18:44:13 -07001804 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001805 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001806
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001807 rx->skb->dev = dev;
1808
1809 dev->stats.rx_packets++;
1810 dev->stats.rx_bytes += rx->skb->len;
1811
1812 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001813
Johannes Berg9ae54c82008-01-31 19:48:20 +01001814 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001815}
1816
Johannes Berg49461622008-06-30 15:10:45 +02001817static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001818ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
Ron Rindjunsky71364712007-12-25 17:00:36 +02001819{
1820 struct ieee80211_local *local = rx->local;
1821 struct ieee80211_hw *hw = &local->hw;
1822 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001823 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001824 struct tid_ampdu_rx *tid_agg_rx;
1825 u16 start_seq_num;
1826 u16 tid;
1827
Harvey Harrisona7767f92008-07-02 16:30:51 -07001828 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001829 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001830
Harvey Harrisona7767f92008-07-02 16:30:51 -07001831 if (ieee80211_is_back_req(bar->frame_control)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001832 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01001833 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001834 tid = le16_to_cpu(bar->control) >> 12;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001835 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1836 != HT_AGG_STATE_OPERATIONAL)
Johannes Berga02ae752009-11-16 12:00:40 +01001837 return RX_DROP_MONITOR;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001838 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
Ron Rindjunsky71364712007-12-25 17:00:36 +02001839
1840 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1841
1842 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01001843 if (tid_agg_rx->timeout)
1844 mod_timer(&tid_agg_rx->session_timer,
1845 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02001846
Johannes Berga02ae752009-11-16 12:00:40 +01001847 /* release stored frames up to start of BAR */
1848 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
1849 kfree_skb(skb);
1850 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001851 }
1852
Johannes Berg9ae54c82008-01-31 19:48:20 +01001853 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001854}
1855
Jouni Malinenf4f727a2009-01-10 11:46:53 +02001856static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1857 struct ieee80211_mgmt *mgmt,
1858 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02001859{
1860 struct ieee80211_local *local = sdata->local;
1861 struct sk_buff *skb;
1862 struct ieee80211_mgmt *resp;
1863
1864 if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
1865 /* Not to own unicast address */
1866 return;
1867 }
1868
Johannes Berg46900292009-02-15 12:44:28 +01001869 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1870 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001871 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02001872 return;
1873 }
1874
1875 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1876 /* Too short SA Query request frame */
1877 return;
1878 }
1879
1880 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1881 if (skb == NULL)
1882 return;
1883
1884 skb_reserve(skb, local->hw.extra_tx_headroom);
1885 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1886 memset(resp, 0, 24);
1887 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1888 memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001889 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02001890 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1891 IEEE80211_STYPE_ACTION);
1892 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1893 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1894 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1895 memcpy(resp->u.action.u.sa_query.trans_id,
1896 mgmt->u.action.u.sa_query.trans_id,
1897 WLAN_SA_QUERY_TR_ID_LEN);
1898
Johannes Berg62ae67b2009-11-18 18:42:05 +01001899 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02001900}
1901
Johannes Berg49461622008-06-30 15:10:45 +02001902static ieee80211_rx_result debug_noinline
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001903ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1904{
1905 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01001906 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001907 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1908 int len = rx->skb->len;
1909
1910 if (!ieee80211_is_action(mgmt->frame_control))
1911 return RX_CONTINUE;
1912
1913 if (!rx->sta)
1914 return RX_DROP_MONITOR;
1915
1916 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1917 return RX_DROP_MONITOR;
1918
Jouni Malinen97ebe122009-01-08 13:32:08 +02001919 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1920 return RX_DROP_MONITOR;
1921
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001922 /* all categories we currently handle have action_code */
1923 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1924 return RX_DROP_MONITOR;
1925
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001926 switch (mgmt->u.action.category) {
1927 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01001928 /*
1929 * The aggregation code is not prepared to handle
1930 * anything but STA/AP due to the BSSID handling;
1931 * IBSS could work in the code but isn't supported
1932 * by drivers or the standard.
1933 */
1934 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1935 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1936 sdata->vif.type != NL80211_IFTYPE_AP)
1937 return RX_DROP_MONITOR;
1938
Johannes Bergde1ede7a2008-09-09 14:42:50 +02001939 switch (mgmt->u.action.u.addba_req.action_code) {
1940 case WLAN_ACTION_ADDBA_REQ:
1941 if (len < (IEEE80211_MIN_ACTION_SIZE +
1942 sizeof(mgmt->u.action.u.addba_req)))
1943 return RX_DROP_MONITOR;
1944 ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1945 break;
1946 case WLAN_ACTION_ADDBA_RESP:
1947 if (len < (IEEE80211_MIN_ACTION_SIZE +
1948 sizeof(mgmt->u.action.u.addba_resp)))
1949 return RX_DROP_MONITOR;
1950 ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1951 break;
1952 case WLAN_ACTION_DELBA:
1953 if (len < (IEEE80211_MIN_ACTION_SIZE +
1954 sizeof(mgmt->u.action.u.delba)))
1955 return RX_DROP_MONITOR;
1956 ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1957 break;
1958 }
Johannes Berg39192c02008-09-09 14:49:03 +02001959 break;
1960 case WLAN_CATEGORY_SPECTRUM_MGMT:
1961 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1962 return RX_DROP_MONITOR;
Johannes Berg46900292009-02-15 12:44:28 +01001963
1964 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1965 return RX_DROP_MONITOR;
1966
Johannes Berg39192c02008-09-09 14:49:03 +02001967 switch (mgmt->u.action.u.measurement.action_code) {
1968 case WLAN_ACTION_SPCT_MSR_REQ:
1969 if (len < (IEEE80211_MIN_ACTION_SIZE +
1970 sizeof(mgmt->u.action.u.measurement)))
1971 return RX_DROP_MONITOR;
1972 ieee80211_process_measurement_req(sdata, mgmt, len);
1973 break;
Sujithc481ec92009-01-06 09:28:37 +05301974 case WLAN_ACTION_SPCT_CHL_SWITCH:
1975 if (len < (IEEE80211_MIN_ACTION_SIZE +
1976 sizeof(mgmt->u.action.u.chan_switch)))
1977 return RX_DROP_MONITOR;
1978
Johannes Bergcc32abd2009-05-15 11:52:31 +02001979 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1980 return RX_DROP_MONITOR;
1981
Johannes Berg46900292009-02-15 12:44:28 +01001982 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
Sujithc481ec92009-01-06 09:28:37 +05301983 return RX_DROP_MONITOR;
1984
Johannes Berg77fdaa12009-07-07 03:45:17 +02001985 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg39192c02008-09-09 14:49:03 +02001986 }
1987 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02001988 case WLAN_CATEGORY_SA_QUERY:
1989 if (len < (IEEE80211_MIN_ACTION_SIZE +
1990 sizeof(mgmt->u.action.u.sa_query)))
1991 return RX_DROP_MONITOR;
1992 switch (mgmt->u.action.u.sa_query.action) {
1993 case WLAN_ACTION_SA_QUERY_REQUEST:
1994 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1995 return RX_DROP_MONITOR;
1996 ieee80211_process_sa_query_req(sdata, mgmt, len);
1997 break;
1998 case WLAN_ACTION_SA_QUERY_RESPONSE:
1999 /*
2000 * SA Query response is currently only used in AP mode
2001 * and it is processed in user space.
2002 */
2003 return RX_CONTINUE;
2004 }
2005 break;
Johannes Berg39192c02008-09-09 14:49:03 +02002006 default:
2007 return RX_CONTINUE;
Johannes Bergde1ede7a2008-09-09 14:42:50 +02002008 }
2009
Johannes Berg39192c02008-09-09 14:49:03 +02002010 rx->sta->rx_packets++;
2011 dev_kfree_skb(rx->skb);
2012 return RX_QUEUED;
Johannes Bergde1ede7a2008-09-09 14:42:50 +02002013}
2014
2015static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01002016ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002017{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002018 struct ieee80211_sub_if_data *sdata = rx->sdata;
Jouni Malinen97ebe122009-01-08 13:32:08 +02002019 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002020
Johannes Berg5cf121c2008-02-25 16:27:43 +01002021 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +01002022 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002023
Jouni Malinen97ebe122009-01-08 13:32:08 +02002024 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
2025 return RX_DROP_MONITOR;
2026
Johannes Berg472dbc42008-09-11 00:01:49 +02002027 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Bergf1d58c22009-06-17 13:13:00 +02002028 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02002029
Johannes Berg3832c282009-03-24 08:46:57 +01002030 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
Johannes Bergf1d58c22009-06-17 13:13:00 +02002031 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
Johannes Bergf9d540e2007-09-28 14:02:09 +02002032
Johannes Berg7986cf92009-03-21 17:08:43 +01002033 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Johannes Bergf1d58c22009-06-17 13:13:00 +02002034 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg46900292009-02-15 12:44:28 +01002035
Johannes Berg7986cf92009-03-21 17:08:43 +01002036 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02002037}
2038
Johannes Berg3b8d81e2009-06-17 17:43:56 +02002039static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
Johannes Berg5cf121c2008-02-25 16:27:43 +01002040 struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02002041{
Harvey Harrisona7767f92008-07-02 16:30:51 -07002042 int keyidx;
2043 unsigned int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02002044
Harvey Harrisona7767f92008-07-02 16:30:51 -07002045 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02002046 if (rx->skb->len >= hdrlen + 4)
2047 keyidx = rx->skb->data[hdrlen + 3] >> 6;
2048 else
2049 keyidx = -1;
2050
Johannes Berg8944b792008-01-31 19:48:26 +01002051 if (!rx->sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02002052 /*
2053 * Some hardware seem to generate incorrect Michael MIC
2054 * reports; ignore them to avoid triggering countermeasures.
2055 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01002056 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02002057 }
2058
Harvey Harrisona7767f92008-07-02 16:30:51 -07002059 if (!ieee80211_has_protected(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01002060 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02002061
Johannes Berg05c914f2008-09-11 00:01:58 +02002062 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02002063 /*
2064 * APs with pairwise keys should never receive Michael MIC
2065 * errors for non-zero keyidx because these are reserved for
2066 * group keys and only the AP is sending real multicast
2067 * frames in the BSS.
2068 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01002069 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02002070 }
2071
Harvey Harrisona7767f92008-07-02 16:30:51 -07002072 if (!ieee80211_is_data(hdr->frame_control) &&
2073 !ieee80211_is_auth(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01002074 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02002075
Johannes Berge6d6e342009-07-01 21:26:47 +02002076 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
2077 GFP_ATOMIC);
Johannes Berg571ecf62007-07-27 15:43:22 +02002078}
2079
Johannes Berg5cf121c2008-02-25 16:27:43 +01002080/* TODO: use IEEE80211_RX_FRAGMENTED */
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002081static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2082 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01002083{
2084 struct ieee80211_sub_if_data *sdata;
2085 struct ieee80211_local *local = rx->local;
2086 struct ieee80211_rtap_hdr {
2087 struct ieee80211_radiotap_header hdr;
2088 u8 flags;
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002089 u8 rate_or_pad;
Michael Wu3d30d942008-01-31 19:48:27 +01002090 __le16 chan_freq;
2091 __le16 chan_flags;
2092 } __attribute__ ((packed)) *rthdr;
2093 struct sk_buff *skb = rx->skb, *skb2;
2094 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002095 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01002096
Johannes Berg8c0c7092009-11-25 17:46:15 +01002097 if (status->flag & RX_FLAG_INTERNAL_CMTR)
Michael Wu3d30d942008-01-31 19:48:27 +01002098 goto out_free_skb;
2099
2100 if (skb_headroom(skb) < sizeof(*rthdr) &&
2101 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2102 goto out_free_skb;
2103
2104 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2105 memset(rthdr, 0, sizeof(*rthdr));
2106 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2107 rthdr->hdr.it_present =
2108 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
Michael Wu3d30d942008-01-31 19:48:27 +01002109 (1 << IEEE80211_RADIOTAP_CHANNEL));
2110
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002111 if (rate) {
2112 rthdr->rate_or_pad = rate->bitrate / 5;
2113 rthdr->hdr.it_present |=
2114 cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2115 }
Michael Wu3d30d942008-01-31 19:48:27 +01002116 rthdr->chan_freq = cpu_to_le16(status->freq);
2117
2118 if (status->band == IEEE80211_BAND_5GHZ)
2119 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2120 IEEE80211_CHAN_5GHZ);
2121 else
2122 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2123 IEEE80211_CHAN_2GHZ);
2124
2125 skb_set_mac_header(skb, 0);
2126 skb->ip_summed = CHECKSUM_UNNECESSARY;
2127 skb->pkt_type = PACKET_OTHERHOST;
2128 skb->protocol = htons(ETH_P_802_2);
2129
2130 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2131 if (!netif_running(sdata->dev))
2132 continue;
2133
Johannes Berg05c914f2008-09-11 00:01:58 +02002134 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Michael Wu3d30d942008-01-31 19:48:27 +01002135 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2136 continue;
2137
2138 if (prev_dev) {
2139 skb2 = skb_clone(skb, GFP_ATOMIC);
2140 if (skb2) {
2141 skb2->dev = prev_dev;
2142 netif_rx(skb2);
2143 }
2144 }
2145
2146 prev_dev = sdata->dev;
2147 sdata->dev->stats.rx_packets++;
2148 sdata->dev->stats.rx_bytes += skb->len;
2149 }
2150
2151 if (prev_dev) {
2152 skb->dev = prev_dev;
2153 netif_rx(skb);
2154 skb = NULL;
2155 } else
2156 goto out_free_skb;
2157
Johannes Berg8c0c7092009-11-25 17:46:15 +01002158 status->flag |= RX_FLAG_INTERNAL_CMTR;
Michael Wu3d30d942008-01-31 19:48:27 +01002159 return;
2160
2161 out_free_skb:
2162 dev_kfree_skb(skb);
2163}
2164
Johannes Berg571ecf62007-07-27 15:43:22 +02002165
Johannes Berg8944b792008-01-31 19:48:26 +01002166static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg5cf121c2008-02-25 16:27:43 +01002167 struct ieee80211_rx_data *rx,
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002168 struct sk_buff *skb,
2169 struct ieee80211_rate *rate)
Johannes Berg58905292008-01-31 19:48:25 +01002170{
Johannes Berg58905292008-01-31 19:48:25 +01002171 ieee80211_rx_result res = RX_DROP_MONITOR;
2172
Johannes Berg8944b792008-01-31 19:48:26 +01002173 rx->skb = skb;
2174 rx->sdata = sdata;
Johannes Berg8944b792008-01-31 19:48:26 +01002175
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002176#define CALL_RXH(rxh) \
2177 do { \
2178 res = rxh(rx); \
2179 if (res != RX_CONTINUE) \
2180 goto rxh_done; \
2181 } while (0);
Johannes Berg58905292008-01-31 19:48:25 +01002182
Johannes Berg49461622008-06-30 15:10:45 +02002183 CALL_RXH(ieee80211_rx_h_passive_scan)
2184 CALL_RXH(ieee80211_rx_h_check)
2185 CALL_RXH(ieee80211_rx_h_decrypt)
Kalle Valo572e0012009-02-10 17:09:31 +02002186 CALL_RXH(ieee80211_rx_h_check_more_data)
Johannes Berg49461622008-06-30 15:10:45 +02002187 CALL_RXH(ieee80211_rx_h_sta_process)
2188 CALL_RXH(ieee80211_rx_h_defragment)
2189 CALL_RXH(ieee80211_rx_h_ps_poll)
2190 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2191 /* must be after MMIC verify so header is counted in MPDU mic */
2192 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2193 CALL_RXH(ieee80211_rx_h_amsdu)
Ingo Molnarbf94e172008-10-12 23:51:38 -07002194#ifdef CONFIG_MAC80211_MESH
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02002195 if (ieee80211_vif_is_mesh(&sdata->vif))
2196 CALL_RXH(ieee80211_rx_h_mesh_fwding);
Ingo Molnarbf94e172008-10-12 23:51:38 -07002197#endif
Johannes Berg49461622008-06-30 15:10:45 +02002198 CALL_RXH(ieee80211_rx_h_data)
2199 CALL_RXH(ieee80211_rx_h_ctrl)
Johannes Bergde1ede7a2008-09-09 14:42:50 +02002200 CALL_RXH(ieee80211_rx_h_action)
Johannes Berg49461622008-06-30 15:10:45 +02002201 CALL_RXH(ieee80211_rx_h_mgmt)
Johannes Berg58905292008-01-31 19:48:25 +01002202
Johannes Berg49461622008-06-30 15:10:45 +02002203#undef CALL_RXH
2204
2205 rxh_done:
Johannes Berg58905292008-01-31 19:48:25 +01002206 switch (res) {
Michael Wu3d30d942008-01-31 19:48:27 +01002207 case RX_DROP_MONITOR:
Johannes Berg49461622008-06-30 15:10:45 +02002208 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2209 if (rx->sta)
2210 rx->sta->rx_dropped++;
2211 /* fall through */
2212 case RX_CONTINUE:
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002213 ieee80211_rx_cooked_monitor(rx, rate);
Michael Wu3d30d942008-01-31 19:48:27 +01002214 break;
2215 case RX_DROP_UNUSABLE:
Johannes Berg49461622008-06-30 15:10:45 +02002216 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2217 if (rx->sta)
2218 rx->sta->rx_dropped++;
Johannes Berg58905292008-01-31 19:48:25 +01002219 dev_kfree_skb(rx->skb);
2220 break;
Johannes Berg49461622008-06-30 15:10:45 +02002221 case RX_QUEUED:
2222 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
2223 break;
Johannes Berg58905292008-01-31 19:48:25 +01002224 }
2225}
2226
Johannes Berg571ecf62007-07-27 15:43:22 +02002227/* main receive path */
2228
Johannes Berg23a24de2007-07-27 15:43:22 +02002229static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg7ab17c42009-02-10 21:25:43 +01002230 struct ieee80211_rx_data *rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02002231 struct ieee80211_hdr *hdr)
2232{
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002233 struct sk_buff *skb = rx->skb;
2234 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2235 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02002236 int multicast = is_multicast_ether_addr(hdr->addr1);
2237
Johannes Berg51fb61e2007-12-19 01:31:27 +01002238 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002239 case NL80211_IFTYPE_STATION:
Johannes Berg9bc383d2009-11-19 11:55:19 +01002240 if (!bssid && !sdata->u.mgd.use_4addr)
Johannes Berg23a24de2007-07-27 15:43:22 +02002241 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002242 if (!multicast &&
2243 compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04002244 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002245 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002246 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002247 }
2248 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002249 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02002250 if (!bssid)
2251 return 0;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002252 if (ieee80211_is_beacon(hdr->frame_control)) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002253 return 1;
Vladimir Koutny87291c02008-06-13 16:50:44 +02002254 }
Johannes Berg46900292009-02-15 12:44:28 +01002255 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002256 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002257 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002258 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002259 } else if (!multicast &&
2260 compare_ether_addr(sdata->dev->dev_addr,
2261 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04002262 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002263 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002264 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002265 } else if (!rx->sta) {
2266 int rate_idx;
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002267 if (status->flag & RX_FLAG_HT)
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002268 rate_idx = 0; /* TODO: HT rates */
2269 else
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002270 rate_idx = status->rate_idx;
Rami Rosenab1f5c02008-12-11 14:00:25 +02002271 rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002272 BIT(rate_idx));
2273 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002274 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002275 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg6032f932008-02-23 15:17:07 +01002276 if (!multicast &&
2277 compare_ether_addr(sdata->dev->dev_addr,
2278 hdr->addr1) != 0) {
2279 if (!(sdata->dev->flags & IFF_PROMISC))
2280 return 0;
2281
Johannes Berg5cf121c2008-02-25 16:27:43 +01002282 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg6032f932008-02-23 15:17:07 +01002283 }
2284 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002285 case NL80211_IFTYPE_AP_VLAN:
2286 case NL80211_IFTYPE_AP:
Johannes Berg23a24de2007-07-27 15:43:22 +02002287 if (!bssid) {
2288 if (compare_ether_addr(sdata->dev->dev_addr,
2289 hdr->addr1))
2290 return 0;
2291 } else if (!ieee80211_bssid_match(bssid,
2292 sdata->dev->dev_addr)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002293 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002294 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002295 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002296 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002297 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002298 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07002299 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg23a24de2007-07-27 15:43:22 +02002300 return 0;
2301 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2302 return 0;
2303 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002304 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +02002305 case NL80211_IFTYPE_UNSPECIFIED:
2306 case __NL80211_IFTYPE_AFTER_LAST:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02002307 /* should never get here */
2308 WARN_ON(1);
2309 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02002310 }
2311
2312 return 1;
2313}
2314
Johannes Berg571ecf62007-07-27 15:43:22 +02002315/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002316 * This is the actual Rx frames handler. as it blongs to Rx path it must
2317 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02002318 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002319static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2320 struct sk_buff *skb,
Johannes Berg8318d782008-01-24 19:38:38 +01002321 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +02002322{
Johannes Bergf1d58c22009-06-17 13:13:00 +02002323 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002324 struct ieee80211_local *local = hw_to_local(hw);
2325 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002326 struct ieee80211_hdr *hdr;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002327 struct ieee80211_rx_data rx;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002328 int prepares;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002329 struct ieee80211_sub_if_data *prev = NULL;
2330 struct sk_buff *skb_new;
Johannes Berg571ecf62007-07-27 15:43:22 +02002331
Harvey Harrison358c8d92008-07-15 18:44:13 -07002332 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002333 memset(&rx, 0, sizeof(rx));
2334 rx.skb = skb;
2335 rx.local = local;
Johannes Berg72abd812007-09-17 01:29:22 -04002336
Harvey Harrison358c8d92008-07-15 18:44:13 -07002337 if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg571ecf62007-07-27 15:43:22 +02002338 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02002339
Helmut Schaa142b9f52009-07-23 13:18:01 +02002340 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2341 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
Johannes Berg5cf121c2008-02-25 16:27:43 +01002342 rx.flags |= IEEE80211_RX_IN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02002343
Johannes Berg38f37142008-01-29 17:07:43 +01002344 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002345 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01002346
Johannes Bergaf2ced62009-11-16 12:00:39 +01002347 rx.sta = sta_info_get(local, hdr->addr2);
Johannes Bergeb9fb5b2009-11-16 13:58:20 +01002348 if (rx.sta)
Johannes Bergaf2ced62009-11-16 12:00:39 +01002349 rx.sdata = rx.sta->sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002350
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002351 if (rx.sdata && ieee80211_is_data(hdr->frame_control)) {
2352 rx.flags |= IEEE80211_RX_RA_MATCH;
2353 prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
Johannes Bergaf2ced62009-11-16 12:00:39 +01002354 if (prepares) {
2355 if (status->flag & RX_FLAG_MMIC_ERROR) {
2356 if (rx.flags & IEEE80211_RX_RA_MATCH)
2357 ieee80211_rx_michael_mic_report(hdr, &rx);
2358 } else
2359 prev = rx.sdata;
2360 }
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002361 } else list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04002362 if (!netif_running(sdata->dev))
2363 continue;
2364
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002365 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2366 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Bergb2e77712007-09-26 15:19:39 +02002367 continue;
2368
Johannes Berg5cf121c2008-02-25 16:27:43 +01002369 rx.flags |= IEEE80211_RX_RA_MATCH;
Johannes Berg7ab17c42009-02-10 21:25:43 +01002370 prepares = prepare_for_handlers(sdata, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02002371
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002372 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02002373 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002374
Johannes Bergaf2ced62009-11-16 12:00:39 +01002375 if (status->flag & RX_FLAG_MMIC_ERROR) {
2376 rx.sdata = sdata;
2377 if (rx.flags & IEEE80211_RX_RA_MATCH)
2378 ieee80211_rx_michael_mic_report(hdr, &rx);
2379 continue;
2380 }
2381
Johannes Berg340e11f2007-07-27 15:43:22 +02002382 /*
2383 * frame is destined for this interface, but if it's not
2384 * also for the previous one we handle that after the
2385 * loop to avoid copying the SKB once too much
2386 */
2387
2388 if (!prev) {
2389 prev = sdata;
2390 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02002391 }
Johannes Berg340e11f2007-07-27 15:43:22 +02002392
2393 /*
2394 * frame was destined for the previous interface
2395 * so invoke RX handlers for it
2396 */
2397
2398 skb_new = skb_copy(skb, GFP_ATOMIC);
2399 if (!skb_new) {
2400 if (net_ratelimit())
2401 printk(KERN_DEBUG "%s: failed to copy "
Pavel Roskina4278e12008-05-12 09:02:24 -04002402 "multicast frame for %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04002403 wiphy_name(local->hw.wiphy),
2404 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02002405 continue;
2406 }
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002407 ieee80211_invoke_rx_handlers(prev, &rx, skb_new, rate);
Johannes Berg8e6f0032007-07-27 15:43:22 +02002408 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002409 }
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002410 if (prev)
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002411 ieee80211_invoke_rx_handlers(prev, &rx, skb, rate);
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002412 else
Johannes Berg8e6f0032007-07-27 15:43:22 +02002413 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002414}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002415
2416/*
2417 * This is the receive path handler. It is called by a low level driver when an
2418 * 802.11 MPDU is received from the hardware.
2419 */
John W. Linville103bf9f2009-08-20 16:34:15 -04002420void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002421{
2422 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01002423 struct ieee80211_rate *rate = NULL;
2424 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02002425 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01002426
Johannes Bergd20ef632009-10-11 15:10:40 +02002427 WARN_ON_ONCE(softirq_count() == 0);
2428
Johannes Berg77a980d2009-08-24 11:46:30 +02002429 if (WARN_ON(status->band < 0 ||
2430 status->band >= IEEE80211_NUM_BANDS))
2431 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002432
2433 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02002434 if (WARN_ON(!sband))
2435 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002436
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002437 /*
2438 * If we're suspending, it is possible although not too likely
2439 * that we'd be receiving frames after having already partially
2440 * quiesced the stack. We can't process such frames then since
2441 * that might, for example, cause stations to be added or other
2442 * driver callbacks be invoked.
2443 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002444 if (unlikely(local->quiescing || local->suspended))
2445 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002446
Johannes Bergea77f122009-08-21 14:44:45 +02002447 /*
2448 * The same happens when we're not even started,
2449 * but that's worth a warning.
2450 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002451 if (WARN_ON(!local->started))
2452 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02002453
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002454 if (status->flag & RX_FLAG_HT) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05002455 /*
2456 * rate_idx is MCS index, which can be [0-76] as documented on:
2457 *
2458 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2459 *
2460 * Anything else would be some sort of driver or hardware error.
2461 * The driver should catch hardware errors.
2462 */
2463 if (WARN((status->rate_idx < 0 ||
2464 status->rate_idx > 76),
2465 "Rate marked as an HT rate but passed "
2466 "status->rate_idx is not "
2467 "an MCS index [0-76]: %d (0x%02x)\n",
2468 status->rate_idx,
2469 status->rate_idx))
Johannes Berg77a980d2009-08-24 11:46:30 +02002470 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002471 } else {
2472 if (WARN_ON(status->rate_idx < 0 ||
2473 status->rate_idx >= sband->n_bitrates))
Johannes Berg77a980d2009-08-24 11:46:30 +02002474 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002475 rate = &sband->bitrates[status->rate_idx];
2476 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002477
2478 /*
2479 * key references and virtual interfaces are protected using RCU
2480 * and this requires that we are in a read-side RCU section during
2481 * receive processing
2482 */
2483 rcu_read_lock();
2484
2485 /*
2486 * Frames with failed FCS/PLCP checksum are not returned,
2487 * all other frames are returned without radiotap header
2488 * if it was previously present.
2489 * Also, frames with less than 16 bytes are dropped.
2490 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002491 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002492 if (!skb) {
2493 rcu_read_unlock();
2494 return;
2495 }
2496
Jouni Malinenaec67952009-05-05 20:35:15 +03002497 /*
2498 * In theory, the block ack reordering should happen after duplicate
2499 * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
2500 * the call to ieee80211_rx_reorder_ampdu() should really be moved to
2501 * happen as a new RX handler between ieee80211_rx_h_check and
2502 * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
2503 * the time being, the call can be here since RX reorder buf processing
2504 * will implicitly skip duplicates. We could, in theory at least,
2505 * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
2506 * frames from other than operational channel), but that should not
2507 * happen in normal networks.
2508 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002509 if (!ieee80211_rx_reorder_ampdu(local, skb))
2510 __ieee80211_rx_handle_packet(hw, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002511
2512 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02002513
2514 return;
2515 drop:
2516 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002517}
John W. Linville103bf9f2009-08-20 16:34:15 -04002518EXPORT_SYMBOL(ieee80211_rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002519
2520/* This is a version of the rx handler that can be called from hard irq
2521 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002522void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02002523{
2524 struct ieee80211_local *local = hw_to_local(hw);
2525
2526 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2527
Johannes Berg571ecf62007-07-27 15:43:22 +02002528 skb->pkt_type = IEEE80211_RX_MSG;
2529 skb_queue_tail(&local->skb_queue, skb);
2530 tasklet_schedule(&local->tasklet);
2531}
2532EXPORT_SYMBOL(ieee80211_rx_irqsafe);