blob: 775365f856c92b6b5b395879d4504a2aefb0b3ad [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 Berga02ae752009-11-16 12:00:40 +010030static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
31 struct tid_ampdu_rx *tid_agg_rx,
32 u16 head_seq_num);
33
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 Berg0869aea02009-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 Berg0869aea02009-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 Berg0869aea02009-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 Berg0869aea02009-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 Berg0869aea02009-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 Berg0869aea02009-10-28 10:03:35 +0100248 return remove_monitor_info(local, origskb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200249 }
250
Johannes Berg0869aea02009-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 Berg0869aea02009-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 Berg0869aea02009-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 Berg52865dfd2007-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 Schaafbe9c4292009-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 Malinen3cfcf6ac2009-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 Bergeb9fb5b82009-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
537
Johannes Berg49461622008-06-30 15:10:45 +0200538static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100539ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200540{
Harvey Harrisona7767f92008-07-02 16:30:51 -0700541 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200542
543 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
544 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700545 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100546 rx->sta->last_seq_ctrl[rx->queue] ==
Johannes Berg571ecf62007-07-27 15:43:22 +0200547 hdr->seq_ctrl)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100548 if (rx->flags & IEEE80211_RX_RA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200549 rx->local->dot11FrameDuplicateCount++;
550 rx->sta->num_duplicates++;
551 }
Johannes Berge4c26ad2008-01-31 19:48:21 +0100552 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200553 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100554 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +0200555 }
556
Johannes Berg571ecf62007-07-27 15:43:22 +0200557 if (unlikely(rx->skb->len < 16)) {
558 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100559 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200560 }
561
Johannes Berg571ecf62007-07-27 15:43:22 +0200562 /* Drop disallowed frame classes based on STA auth/assoc state;
563 * IEEE 802.11, Chap 5.5.
564 *
Johannes Bergccd7b362008-09-11 00:01:56 +0200565 * mac80211 filters only based on association state, i.e. it drops
566 * Class 3 frames from not associated stations. hostapd sends
Johannes Berg571ecf62007-07-27 15:43:22 +0200567 * deauth/disassoc frames when needed. In addition, hostapd is
568 * responsible for filtering on both auth and assoc states.
569 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100570
Johannes Berg902acc72008-02-23 15:17:19 +0100571 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100572 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100573
Harvey Harrisona7767f92008-07-02 16:30:51 -0700574 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
575 ieee80211_is_pspoll(hdr->frame_control)) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200576 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Johannes Berg07346f812008-05-03 01:02:02 +0200577 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
Harvey Harrisona7767f92008-07-02 16:30:51 -0700578 if ((!ieee80211_has_fromds(hdr->frame_control) &&
579 !ieee80211_has_tods(hdr->frame_control) &&
580 ieee80211_is_data(hdr->frame_control)) ||
581 !(rx->flags & IEEE80211_RX_RA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200582 /* Drop IBSS frames and frames for other hosts
583 * silently. */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100584 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200585 }
586
Johannes Berge4c26ad2008-01-31 19:48:21 +0100587 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200588 }
589
Johannes Berg9ae54c82008-01-31 19:48:20 +0100590 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200591}
592
593
Johannes Berg49461622008-06-30 15:10:45 +0200594static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100595ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200596{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100597 struct sk_buff *skb = rx->skb;
598 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
599 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400600 int keyidx;
601 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100602 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400603 struct ieee80211_key *stakey = NULL;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200604 int mmie_keyidx = -1;
Johannes Berg570bd532007-07-27 15:43:22 +0200605
Johannes Berg3017b802007-08-28 17:01:53 -0400606 /*
607 * Key selection 101
608 *
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200609 * There are four types of keys:
Johannes Berg3017b802007-08-28 17:01:53 -0400610 * - GTK (group keys)
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200611 * - IGTK (group keys for management frames)
Johannes Berg3017b802007-08-28 17:01:53 -0400612 * - PTK (pairwise keys)
613 * - STK (station-to-station pairwise keys)
614 *
615 * When selecting a key, we have to distinguish between multicast
616 * (including broadcast) and unicast frames, the latter can only
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200617 * use PTKs and STKs while the former always use GTKs and IGTKs.
618 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
619 * unicast frames can also use key indices like GTKs. Hence, if we
620 * don't have a PTK/STK we check the key index for a WEP key.
Johannes Berg3017b802007-08-28 17:01:53 -0400621 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400622 * Note that in a regular BSS, multicast frames are sent by the
623 * AP only, associated stations unicast the frame to the AP first
624 * which then multicasts it on their behalf.
625 *
Johannes Berg3017b802007-08-28 17:01:53 -0400626 * There is also a slight problem in IBSS mode: GTKs are negotiated
627 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400628 * The spec seems to expect that one negotiates the same key with
629 * every station but there's no such requirement; VLANs could be
630 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400631 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200632
Johannes Berg3017b802007-08-28 17:01:53 -0400633 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200634 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400635 * addressed to us nor a multicast frame.
636 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100637 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100638 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400639
Johannes Bergd4e46a32007-09-14 11:10:24 -0400640 if (rx->sta)
641 stakey = rcu_dereference(rx->sta->key);
642
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300643 if (!ieee80211_has_protected(hdr->frame_control))
644 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
645
Johannes Bergd4e46a32007-09-14 11:10:24 -0400646 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
647 rx->key = stakey;
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300648 /* Skip decryption if the frame is not protected. */
649 if (!ieee80211_has_protected(hdr->frame_control))
650 return RX_CONTINUE;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200651 } else if (mmie_keyidx >= 0) {
652 /* Broadcast/multicast robust management frame / BIP */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100653 if ((status->flag & RX_FLAG_DECRYPTED) &&
654 (status->flag & RX_FLAG_IV_STRIPPED))
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200655 return RX_CONTINUE;
656
657 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
658 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
659 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
660 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
Jouni Malinen0c7c10c2009-05-08 12:34:10 +0300661 } else if (!ieee80211_has_protected(hdr->frame_control)) {
662 /*
663 * The frame was not protected, so skip decryption. However, we
664 * need to set rx->key if there is a key that could have been
665 * used so that the frame may be dropped if encryption would
666 * have been expected.
667 */
668 struct ieee80211_key *key = NULL;
669 if (ieee80211_is_mgmt(hdr->frame_control) &&
670 is_multicast_ether_addr(hdr->addr1) &&
671 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
672 rx->key = key;
673 else if ((key = rcu_dereference(rx->sdata->default_key)))
674 rx->key = key;
675 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200676 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400677 /*
678 * The device doesn't give us the IV so we won't be
679 * able to look up the key. That's ok though, we
680 * don't need to decrypt the frame, we just won't
681 * be able to keep statistics accurate.
682 * Except for key threshold notifications, should
683 * we somehow allow the driver to tell us which key
684 * the hardware used if this flag is set?
685 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100686 if ((status->flag & RX_FLAG_DECRYPTED) &&
687 (status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100688 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200689
Harvey Harrisona7767f92008-07-02 16:30:51 -0700690 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +0200691
Johannes Berg3017b802007-08-28 17:01:53 -0400692 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100693 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200694
Johannes Berg3017b802007-08-28 17:01:53 -0400695 /*
696 * no need to call ieee80211_wep_get_keyidx,
697 * it verifies a bunch of things we've done already
698 */
699 keyidx = rx->skb->data[hdrlen + 3] >> 6;
700
Johannes Bergd4e46a32007-09-14 11:10:24 -0400701 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400702
703 /*
704 * RSNA-protected unicast frames should always be sent with
705 * pairwise or station-to-station keys, but for WEP we allow
706 * using a key index as well.
707 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400708 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400709 !is_multicast_ether_addr(hdr->addr1))
710 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200711 }
712
Johannes Berg3017b802007-08-28 17:01:53 -0400713 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200714 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400715 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200716 } else {
Johannes Berge4c26ad2008-01-31 19:48:21 +0100717 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +0200718 }
719
Johannes Berg1990af82007-09-26 17:53:15 +0200720 /* Check for weak IVs if possible */
721 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
Harvey Harrisona7767f92008-07-02 16:30:51 -0700722 ieee80211_is_data(hdr->frame_control) &&
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100723 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
724 !(status->flag & RX_FLAG_DECRYPTED)) &&
Johannes Berg1990af82007-09-26 17:53:15 +0200725 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
726 rx->sta->wep_weak_iv_count++;
727
Johannes Berg70f08762007-09-26 17:53:14 +0200728 switch (rx->key->conf.alg) {
729 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200730 result = ieee80211_crypto_wep_decrypt(rx);
731 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200732 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200733 result = ieee80211_crypto_tkip_decrypt(rx);
734 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200735 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200736 result = ieee80211_crypto_ccmp_decrypt(rx);
737 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200738 case ALG_AES_CMAC:
739 result = ieee80211_crypto_aes_cmac_decrypt(rx);
740 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200741 }
742
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200743 /* either the frame has been decrypted or will be dropped */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100744 status->flag |= RX_FLAG_DECRYPTED;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200745
746 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200747}
748
Kalle Valo572e0012009-02-10 17:09:31 +0200749static ieee80211_rx_result debug_noinline
750ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
751{
752 struct ieee80211_local *local;
753 struct ieee80211_hdr *hdr;
754 struct sk_buff *skb;
755
756 local = rx->local;
757 skb = rx->skb;
758 hdr = (struct ieee80211_hdr *) skb->data;
759
760 if (!local->pspolling)
761 return RX_CONTINUE;
762
763 if (!ieee80211_has_fromds(hdr->frame_control))
764 /* this is not from AP */
765 return RX_CONTINUE;
766
767 if (!ieee80211_is_data(hdr->frame_control))
768 return RX_CONTINUE;
769
770 if (!ieee80211_has_moredata(hdr->frame_control)) {
771 /* AP has no more frames buffered for us */
772 local->pspolling = false;
773 return RX_CONTINUE;
774 }
775
776 /* more data bit is set, let's request a new frame from the AP */
777 ieee80211_send_pspoll(local, rx->sdata);
778
779 return RX_CONTINUE;
780}
781
Johannes Berg133b8222008-09-16 14:18:59 +0200782static void ap_sta_ps_start(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +0200783{
Johannes Berg133b8222008-09-16 14:18:59 +0200784 struct ieee80211_sub_if_data *sdata = sta->sdata;
Christian Lamparter4571d3b2008-11-30 00:48:41 +0100785 struct ieee80211_local *local = sdata->local;
Joe Perches0795af52007-10-03 17:59:30 -0700786
Johannes Berg3e122be2008-07-09 14:40:34 +0200787 atomic_inc(&sdata->bss->num_sta_ps);
Johannes Bergaf818582009-11-06 11:35:50 +0100788 set_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg24487982009-04-23 18:52:52 +0200789 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
Johannes Berg571ecf62007-07-27 15:43:22 +0200790#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700791 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
792 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200793#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
794}
795
Johannes Bergff9458d2009-10-30 12:56:02 +0100796static void ap_sta_ps_end(struct sta_info *sta)
Johannes Berg571ecf62007-07-27 15:43:22 +0200797{
Johannes Berg133b8222008-09-16 14:18:59 +0200798 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +0200799
Johannes Berg3e122be2008-07-09 14:40:34 +0200800 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg004c8722008-02-20 11:21:35 +0100801
Johannes Bergaf818582009-11-06 11:35:50 +0100802 clear_sta_flags(sta, WLAN_STA_PS_STA);
Johannes Berg004c8722008-02-20 11:21:35 +0100803
Johannes Berg571ecf62007-07-27 15:43:22 +0200804#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700805 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
806 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200807#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg004c8722008-02-20 11:21:35 +0100808
Johannes Bergaf818582009-11-06 11:35:50 +0100809 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200810#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Bergaf818582009-11-06 11:35:50 +0100811 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
812 sdata->dev->name, sta->sta.addr, sta->sta.aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200813#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Bergaf818582009-11-06 11:35:50 +0100814 return;
815 }
816
817 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg571ecf62007-07-27 15:43:22 +0200818}
819
Johannes Berg49461622008-06-30 15:10:45 +0200820static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100821ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200822{
823 struct sta_info *sta = rx->sta;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100824 struct sk_buff *skb = rx->skb;
825 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
826 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200827
828 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100829 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200830
Johannes Bergb291ba12009-07-10 15:29:03 +0200831 /*
832 * Update last_rx only for IBSS packets which are for the current
833 * BSSID to avoid keeping the current IBSS network alive in cases
834 * where other STAs start using different BSSID.
835 */
Johannes Berg05c914f2008-09-11 00:01:58 +0200836 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200837 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
Johannes Berg05c914f2008-09-11 00:01:58 +0200838 NL80211_IFTYPE_ADHOC);
Johannes Berg46900292009-02-15 12:44:28 +0100839 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
Johannes Berg571ecf62007-07-27 15:43:22 +0200840 sta->last_rx = jiffies;
Johannes Bergb291ba12009-07-10 15:29:03 +0200841 } else if (!is_multicast_ether_addr(hdr->addr1)) {
842 /*
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100843 * Mesh beacons will update last_rx when if they are found to
844 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +0200845 */
Johannes Bergb291ba12009-07-10 15:29:03 +0200846 sta->last_rx = jiffies;
Johannes Berg571ecf62007-07-27 15:43:22 +0200847 }
848
Johannes Berg5cf121c2008-02-25 16:27:43 +0100849 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100850 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200851
Kalle Valo3cf335d52009-03-22 21:57:06 +0200852 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
853 ieee80211_sta_rx_notify(rx->sdata, hdr);
854
Johannes Berg571ecf62007-07-27 15:43:22 +0200855 sta->rx_fragments++;
856 sta->rx_bytes += rx->skb->len;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +0100857 sta->last_signal = status->signal;
858 sta->last_noise = status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200859
Johannes Berg72eaa432008-11-26 15:02:58 +0100860 /*
861 * Change STA power saving mode only at the end of a frame
862 * exchange sequence.
863 */
Johannes Berg3e122be2008-07-09 14:40:34 +0200864 if (!ieee80211_has_morefrags(hdr->frame_control) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200865 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
866 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
Johannes Bergaf818582009-11-06 11:35:50 +0100867 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
Johannes Berg72eaa432008-11-26 15:02:58 +0100868 /*
869 * Ignore doze->wake transitions that are
870 * indicated by non-data frames, the standard
871 * is unclear here, but for example going to
872 * PS mode and then scanning would cause a
873 * doze->wake transition for the probe request,
874 * and that is clearly undesirable.
875 */
876 if (ieee80211_is_data(hdr->frame_control) &&
877 !ieee80211_has_pm(hdr->frame_control))
Johannes Bergff9458d2009-10-30 12:56:02 +0100878 ap_sta_ps_end(sta);
Johannes Berg72eaa432008-11-26 15:02:58 +0100879 } else {
880 if (ieee80211_has_pm(hdr->frame_control))
881 ap_sta_ps_start(sta);
882 }
Johannes Berg571ecf62007-07-27 15:43:22 +0200883 }
884
Johannes Berg22403de2009-10-30 12:55:03 +0100885 /*
886 * Drop (qos-)data::nullfunc frames silently, since they
887 * are used only to control station power saving mode.
888 */
889 if (ieee80211_is_nullfunc(hdr->frame_control) ||
890 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200891 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
Johannes Berg22403de2009-10-30 12:55:03 +0100892 /*
893 * Update counter and free packet here to avoid
894 * counting this as a dropped packed.
895 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200896 sta->rx_packets++;
897 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100898 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200899 }
900
Johannes Berg9ae54c82008-01-31 19:48:20 +0100901 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200902} /* ieee80211_rx_h_sta_process */
903
Johannes Berg571ecf62007-07-27 15:43:22 +0200904static inline struct ieee80211_fragment_entry *
905ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
906 unsigned int frag, unsigned int seq, int rx_queue,
907 struct sk_buff **skb)
908{
909 struct ieee80211_fragment_entry *entry;
910 int idx;
911
912 idx = sdata->fragment_next;
913 entry = &sdata->fragments[sdata->fragment_next++];
914 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
915 sdata->fragment_next = 0;
916
917 if (!skb_queue_empty(&entry->skb_list)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200918#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg571ecf62007-07-27 15:43:22 +0200919 struct ieee80211_hdr *hdr =
920 (struct ieee80211_hdr *) entry->skb_list.next->data;
921 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
922 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700923 "addr1=%pM addr2=%pM\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200924 sdata->dev->name, idx,
925 jiffies - entry->first_frag_time, entry->seq,
Johannes Berg0c68ae262008-10-27 15:56:10 -0700926 entry->last_frag, hdr->addr1, hdr->addr2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200927#endif
Johannes Berg571ecf62007-07-27 15:43:22 +0200928 __skb_queue_purge(&entry->skb_list);
929 }
930
931 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
932 *skb = NULL;
933 entry->first_frag_time = jiffies;
934 entry->seq = seq;
935 entry->rx_queue = rx_queue;
936 entry->last_frag = frag;
937 entry->ccmp = 0;
938 entry->extra_len = 0;
939
940 return entry;
941}
942
943static inline struct ieee80211_fragment_entry *
944ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700945 unsigned int frag, unsigned int seq,
Johannes Berg571ecf62007-07-27 15:43:22 +0200946 int rx_queue, struct ieee80211_hdr *hdr)
947{
948 struct ieee80211_fragment_entry *entry;
949 int i, idx;
950
951 idx = sdata->fragment_next;
952 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
953 struct ieee80211_hdr *f_hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200954
955 idx--;
956 if (idx < 0)
957 idx = IEEE80211_FRAGMENT_MAX - 1;
958
959 entry = &sdata->fragments[idx];
960 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
961 entry->rx_queue != rx_queue ||
962 entry->last_frag + 1 != frag)
963 continue;
964
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700965 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
Johannes Berg571ecf62007-07-27 15:43:22 +0200966
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700967 /*
968 * Check ftype and addresses are equal, else check next fragment
969 */
970 if (((hdr->frame_control ^ f_hdr->frame_control) &
971 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200972 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
973 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
974 continue;
975
S.Çağlar Onurab466232008-02-14 17:36:47 +0200976 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200977 __skb_queue_purge(&entry->skb_list);
978 continue;
979 }
980 return entry;
981 }
982
983 return NULL;
984}
985
Johannes Berg49461622008-06-30 15:10:45 +0200986static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100987ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200988{
989 struct ieee80211_hdr *hdr;
990 u16 sc;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700991 __le16 fc;
Johannes Berg571ecf62007-07-27 15:43:22 +0200992 unsigned int frag, seq;
993 struct ieee80211_fragment_entry *entry;
994 struct sk_buff *skb;
995
Harvey Harrisonb73d70a2008-07-15 18:44:12 -0700996 hdr = (struct ieee80211_hdr *)rx->skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700997 fc = hdr->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +0200998 sc = le16_to_cpu(hdr->seq_ctrl);
999 frag = sc & IEEE80211_SCTL_FRAG;
1000
Harvey Harrison358c8d92008-07-15 18:44:13 -07001001 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
Johannes Berg571ecf62007-07-27 15:43:22 +02001002 (rx->skb)->len < 24 ||
1003 is_multicast_ether_addr(hdr->addr1))) {
1004 /* not fragmented */
1005 goto out;
1006 }
1007 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1008
1009 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1010
1011 if (frag == 0) {
1012 /* This is the first fragment of a new frame. */
1013 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001014 rx->queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -04001015 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Harvey Harrison358c8d92008-07-15 18:44:13 -07001016 ieee80211_has_protected(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001017 /* Store CCMP PN so that we can verify that the next
1018 * fragment has a sequential PN value. */
1019 entry->ccmp = 1;
1020 memcpy(entry->last_pn,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001021 rx->key->u.ccmp.rx_pn[rx->queue],
Johannes Berg571ecf62007-07-27 15:43:22 +02001022 CCMP_PN_LEN);
1023 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01001024 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001025 }
1026
1027 /* This is a fragment for a frame that should already be pending in
1028 * fragment cache. Add this fragment to the end of the pending entry.
1029 */
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001030 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001031 if (!entry) {
1032 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001033 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001034 }
1035
1036 /* Verify that MPDUs within one MSDU have sequential PN values.
1037 * (IEEE 802.11i, 8.3.3.4.5) */
1038 if (entry->ccmp) {
1039 int i;
1040 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -04001041 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001042 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001043 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1044 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1045 pn[i]++;
1046 if (pn[i])
1047 break;
1048 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001049 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001050 if (memcmp(pn, rpn, CCMP_PN_LEN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001051 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001052 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1053 }
1054
Harvey Harrison358c8d92008-07-15 18:44:13 -07001055 skb_pull(rx->skb, ieee80211_hdrlen(fc));
Johannes Berg571ecf62007-07-27 15:43:22 +02001056 __skb_queue_tail(&entry->skb_list, rx->skb);
1057 entry->last_frag = frag;
1058 entry->extra_len += rx->skb->len;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001059 if (ieee80211_has_morefrags(fc)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001060 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001061 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001062 }
1063
1064 rx->skb = __skb_dequeue(&entry->skb_list);
1065 if (skb_tailroom(rx->skb) < entry->extra_len) {
1066 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1067 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1068 GFP_ATOMIC))) {
1069 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1070 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001071 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001072 }
1073 }
1074 while ((skb = __skb_dequeue(&entry->skb_list))) {
1075 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1076 dev_kfree_skb(skb);
1077 }
1078
1079 /* Complete frame has been reassembled - process it now */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001080 rx->flags |= IEEE80211_RX_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001081
1082 out:
1083 if (rx->sta)
1084 rx->sta->rx_packets++;
1085 if (is_multicast_ether_addr(hdr->addr1))
1086 rx->local->dot11MulticastReceivedFrameCount++;
1087 else
1088 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001089 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001090}
1091
Johannes Berg49461622008-06-30 15:10:45 +02001092static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001093ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001094{
Johannes Bergaf818582009-11-06 11:35:50 +01001095 struct ieee80211_sub_if_data *sdata = rx->sdata;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001096 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
Johannes Berg571ecf62007-07-27 15:43:22 +02001097
Harvey Harrison358c8d92008-07-15 18:44:13 -07001098 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
Johannes Berg5cf121c2008-02-25 16:27:43 +01001099 !(rx->flags & IEEE80211_RX_RA_MATCH)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001100 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +02001101
Johannes Berg05c914f2008-09-11 00:01:58 +02001102 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1103 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001104 return RX_DROP_UNUSABLE;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +02001105
Johannes Bergaf818582009-11-06 11:35:50 +01001106 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1107 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1108 else
1109 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
Johannes Berg571ecf62007-07-27 15:43:22 +02001110
Johannes Berg9ae54c82008-01-31 19:48:20 +01001111 /* Free PS Poll skb here instead of returning RX_DROP that would
Johannes Berg571ecf62007-07-27 15:43:22 +02001112 * count as an dropped frame. */
1113 dev_kfree_skb(rx->skb);
1114
Johannes Berg9ae54c82008-01-31 19:48:20 +01001115 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001116}
1117
Johannes Berg49461622008-06-30 15:10:45 +02001118static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001119ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +02001120{
Johannes Berg6e0d1142007-07-27 15:43:22 +02001121 u8 *data = rx->skb->data;
Harvey Harrison238f74a2008-07-02 11:05:34 -07001122 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001123
Harvey Harrison238f74a2008-07-02 11:05:34 -07001124 if (!ieee80211_is_data_qos(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001125 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001126
1127 /* remove the qos control field, update frame type and meta-data */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001128 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1129 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1130 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001131 /* change frame type to non QOS */
Harvey Harrison238f74a2008-07-02 11:05:34 -07001132 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Berg6e0d1142007-07-27 15:43:22 +02001133
Johannes Berg9ae54c82008-01-31 19:48:20 +01001134 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001135}
1136
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001137static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001138ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001139{
Johannes Berg07346f812008-05-03 01:02:02 +02001140 if (unlikely(!rx->sta ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001141 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001142 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001143
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001144 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001145}
1146
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001147static int
Harvey Harrison358c8d92008-07-15 18:44:13 -07001148ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Berg571ecf62007-07-27 15:43:22 +02001149{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001150 struct sk_buff *skb = rx->skb;
1151 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1152
Johannes Berg3017b802007-08-28 17:01:53 -04001153 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001154 * Pass through unencrypted frames if the hardware has
1155 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001156 */
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001157 if (status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001158 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001159
1160 /* Drop unencrypted frames if key is set. */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001161 if (unlikely(!ieee80211_has_protected(fc) &&
1162 !ieee80211_is_nullfunc(fc) &&
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001163 ieee80211_is_data(fc) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001164 (rx->key || rx->sdata->drop_unencrypted)))
1165 return -EACCES;
Jouni Malinenf2ca3ea2009-05-08 12:36:03 +03001166 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1167 if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1168 rx->key))
1169 return -EACCES;
1170 /* BIP does not use Protected field, so need to check MMIE */
1171 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
1172 && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
1173 rx->key))
1174 return -EACCES;
1175 /*
1176 * When using MFP, Action frames are not allowed prior to
1177 * having configured keys.
1178 */
1179 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1180 ieee80211_is_robust_mgmt_frame(
1181 (struct ieee80211_hdr *) rx->skb->data)))
1182 return -EACCES;
1183 }
Johannes Bergb3fc9c62008-04-13 10:12:47 +02001184
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001185 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001186}
1187
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001188static int
Zhu Yie31a16d2009-05-21 21:47:03 +08001189__ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001190{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001191 struct ieee80211_sub_if_data *sdata = rx->sdata;
1192 struct net_device *dev = sdata->dev;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001193 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1194
1195 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->use_4addr &&
1196 ieee80211_has_a4(hdr->frame_control))
1197 return -1;
1198 if (sdata->use_4addr && is_multicast_ether_addr(hdr->addr1))
1199 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001200
Zhu Yie31a16d2009-05-21 21:47:03 +08001201 return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001202}
Johannes Berg571ecf62007-07-27 15:43:22 +02001203
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001204/*
1205 * requires that rx->skb is a frame with ethernet header
1206 */
Harvey Harrison358c8d92008-07-15 18:44:13 -07001207static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001208{
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301209 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001210 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1211 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1212
1213 /*
1214 * Allow EAPOL frames to us/the PAE group address regardless
1215 * of whether the frame was encrypted or not.
1216 */
1217 if (ehdr->h_proto == htons(ETH_P_PAE) &&
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001218 (compare_ether_addr(ehdr->h_dest, rx->sdata->dev->dev_addr) == 0 ||
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001219 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1220 return true;
1221
1222 if (ieee80211_802_1x_port_control(rx) ||
Harvey Harrison358c8d92008-07-15 18:44:13 -07001223 ieee80211_drop_unencrypted(rx, fc))
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001224 return false;
1225
1226 return true;
1227}
1228
1229/*
1230 * requires that rx->skb is a frame with ethernet header
1231 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001232static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01001233ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001234{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001235 struct ieee80211_sub_if_data *sdata = rx->sdata;
1236 struct net_device *dev = sdata->dev;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001237 struct ieee80211_local *local = rx->local;
1238 struct sk_buff *skb, *xmit_skb;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001239 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1240 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001241
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001242 skb = rx->skb;
1243 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001244
Johannes Berg05c914f2008-09-11 00:01:58 +02001245 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1246 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
Johannes Berg213cd112008-09-11 00:01:54 +02001247 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
Felix Fietkauf501dba2009-11-11 13:17:36 +01001248 (rx->flags & IEEE80211_RX_RA_MATCH) && !rx->sdata->use_4addr) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001249 if (is_multicast_ether_addr(ehdr->h_dest)) {
1250 /*
1251 * send multicast frames both to higher layers in
1252 * local net stack and back to the wireless medium
1253 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001254 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1255 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001256 printk(KERN_DEBUG "%s: failed to clone "
1257 "multicast frame\n", dev->name);
1258 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001259 dsta = sta_info_get(local, skb->data);
Johannes Bergd0709a62008-02-25 16:27:46 +01001260 if (dsta && dsta->sdata->dev == dev) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001261 /*
1262 * The destination station is associated to
1263 * this AP (in this VLAN), so send the frame
1264 * directly to it and do not pass it to local
1265 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001266 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001267 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001268 skb = NULL;
1269 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001270 }
1271 }
1272
1273 if (skb) {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001274 int align __maybe_unused;
1275
1276#if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1277 /*
1278 * 'align' will only take the values 0 or 2 here
1279 * since all frames are required to be aligned
1280 * to 2-byte boundaries when being passed to
1281 * mac80211. That also explains the __skb_push()
1282 * below.
1283 */
matthieu castetdacb6f12009-06-04 22:16:18 +02001284 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
Johannes Bergd1c3a372009-01-07 00:26:10 +01001285 if (align) {
1286 if (WARN_ON(skb_headroom(skb) < 3)) {
1287 dev_kfree_skb(skb);
1288 skb = NULL;
1289 } else {
1290 u8 *data = skb->data;
Zhu Yi8ce0b582009-10-28 13:13:52 -07001291 size_t len = skb_headlen(skb);
1292 skb->data -= align;
1293 memmove(skb->data, data, len);
1294 skb_set_tail_pointer(skb, len);
Johannes Bergd1c3a372009-01-07 00:26:10 +01001295 }
1296 }
1297#endif
1298
1299 if (skb) {
1300 /* deliver to local stack */
1301 skb->protocol = eth_type_trans(skb, dev);
1302 memset(skb->cb, 0, sizeof(skb->cb));
1303 netif_rx(skb);
1304 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001305 }
1306
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001307 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001308 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001309 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001310 skb_reset_network_header(xmit_skb);
1311 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001312 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001313 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001314}
1315
Johannes Berg49461622008-06-30 15:10:45 +02001316static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001317ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001318{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001319 struct net_device *dev = rx->sdata->dev;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001320 struct ieee80211_local *local = rx->local;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001321 u16 ethertype;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001322 u8 *payload;
1323 struct sk_buff *skb = rx->skb, *frame = NULL;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1325 __le16 fc = hdr->frame_control;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001326 const struct ethhdr *eth;
1327 int remaining, err;
1328 u8 dst[ETH_ALEN];
1329 u8 src[ETH_ALEN];
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001330
Harvey Harrison358c8d92008-07-15 18:44:13 -07001331 if (unlikely(!ieee80211_is_data(fc)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001332 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001333
Harvey Harrison358c8d92008-07-15 18:44:13 -07001334 if (unlikely(!ieee80211_is_data_present(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001335 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001336
Johannes Berg5cf121c2008-02-25 16:27:43 +01001337 if (!(rx->flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001338 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001339
Zhu Yie31a16d2009-05-21 21:47:03 +08001340 err = __ieee80211_data_to_8023(rx);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001341 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001342 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001343
1344 skb->dev = dev;
1345
1346 dev->stats.rx_packets++;
1347 dev->stats.rx_bytes += skb->len;
1348
1349 /* skip the wrapping header */
1350 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1351 if (!eth)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001352 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001353
1354 while (skb != frame) {
1355 u8 padding;
1356 __be16 len = eth->h_proto;
1357 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1358
1359 remaining = skb->len;
1360 memcpy(dst, eth->h_dest, ETH_ALEN);
1361 memcpy(src, eth->h_source, ETH_ALEN);
1362
1363 padding = ((4 - subframe_len) & 0x3);
1364 /* the last MSDU has no padding */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001365 if (subframe_len > remaining)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001366 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001367
1368 skb_pull(skb, sizeof(struct ethhdr));
1369 /* if last subframe reuse skb */
1370 if (remaining <= subframe_len + padding)
1371 frame = skb;
1372 else {
Johannes Bergd1c3a372009-01-07 00:26:10 +01001373 /*
1374 * Allocate and reserve two bytes more for payload
1375 * alignment since sizeof(struct ethhdr) is 14.
1376 */
1377 frame = dev_alloc_skb(
1378 ALIGN(local->hw.extra_tx_headroom, 4) +
1379 subframe_len + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001380
1381 if (frame == NULL)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001382 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001383
Johannes Bergd1c3a372009-01-07 00:26:10 +01001384 skb_reserve(frame,
1385 ALIGN(local->hw.extra_tx_headroom, 4) +
1386 sizeof(struct ethhdr) + 2);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001387 memcpy(skb_put(frame, ntohs(len)), skb->data,
1388 ntohs(len));
1389
1390 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1391 padding);
1392 if (!eth) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001393 dev_kfree_skb(frame);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001394 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001395 }
1396 }
1397
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001398 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001399 frame->dev = dev;
1400 frame->priority = skb->priority;
1401 rx->skb = frame;
1402
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001403 payload = frame->data;
1404 ethertype = (payload[6] << 8) | payload[7];
1405
1406 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001407 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1408 compare_ether_addr(payload,
1409 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001410 /* remove RFC1042 or Bridge-Tunnel
1411 * encapsulation and replace EtherType */
1412 skb_pull(frame, 6);
1413 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1414 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1415 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001416 memcpy(skb_push(frame, sizeof(__be16)),
1417 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001418 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1419 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1420 }
1421
Harvey Harrison358c8d92008-07-15 18:44:13 -07001422 if (!ieee80211_frame_allowed(rx, fc)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001423 if (skb == frame) /* last frame */
Johannes Berge4c26ad2008-01-31 19:48:21 +01001424 return RX_DROP_UNUSABLE;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001425 dev_kfree_skb(frame);
1426 continue;
1427 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001428
1429 ieee80211_deliver_skb(rx);
1430 }
1431
Johannes Berg9ae54c82008-01-31 19:48:20 +01001432 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001433}
1434
Ingo Molnarbf94e172008-10-12 23:51:38 -07001435#ifdef CONFIG_MAC80211_MESH
Davide Pesaventob0dee572008-09-27 17:29:12 +02001436static ieee80211_rx_result
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001437ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1438{
1439 struct ieee80211_hdr *hdr;
1440 struct ieee80211s_hdr *mesh_hdr;
1441 unsigned int hdrlen;
1442 struct sk_buff *skb = rx->skb, *fwd_skb;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001443 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001444 struct ieee80211_sub_if_data *sdata = rx->sdata;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001445
1446 hdr = (struct ieee80211_hdr *) skb->data;
1447 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1448 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1449
1450 if (!ieee80211_is_data(hdr->frame_control))
1451 return RX_CONTINUE;
1452
1453 if (!mesh_hdr->ttl)
1454 /* illegal frame */
1455 return RX_DROP_MONITOR;
1456
Javier Cardona43b7b312009-10-15 18:10:51 -07001457 if (mesh_hdr->flags & MESH_FLAGS_AE) {
YanBo79617de2008-09-22 13:30:32 +08001458 struct mesh_path *mppath;
Javier Cardona43b7b312009-10-15 18:10:51 -07001459 char *proxied_addr;
1460 char *mpp_addr;
1461
1462 if (is_multicast_ether_addr(hdr->addr1)) {
1463 mpp_addr = hdr->addr3;
1464 proxied_addr = mesh_hdr->eaddr1;
1465 } else {
1466 mpp_addr = hdr->addr4;
1467 proxied_addr = mesh_hdr->eaddr2;
1468 }
YanBo79617de2008-09-22 13:30:32 +08001469
YanBo79617de2008-09-22 13:30:32 +08001470 rcu_read_lock();
Javier Cardona43b7b312009-10-15 18:10:51 -07001471 mppath = mpp_path_lookup(proxied_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001472 if (!mppath) {
Javier Cardona43b7b312009-10-15 18:10:51 -07001473 mpp_path_add(proxied_addr, mpp_addr, sdata);
YanBo79617de2008-09-22 13:30:32 +08001474 } else {
1475 spin_lock_bh(&mppath->state_lock);
1476 mppath->exp_time = jiffies;
Javier Cardona43b7b312009-10-15 18:10:51 -07001477 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1478 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08001479 spin_unlock_bh(&mppath->state_lock);
1480 }
1481 rcu_read_unlock();
1482 }
1483
Javier Cardona3c5772a2009-08-10 12:15:48 -07001484 /* Frame has reached destination. Don't forward */
1485 if (!is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001486 compare_ether_addr(sdata->dev->dev_addr, hdr->addr3) == 0)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001487 return RX_CONTINUE;
1488
1489 mesh_hdr->ttl--;
1490
1491 if (rx->flags & IEEE80211_RX_RA_MATCH) {
1492 if (!mesh_hdr->ttl)
Johannes Berg472dbc42008-09-11 00:01:49 +02001493 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001494 dropped_frames_ttl);
1495 else {
1496 struct ieee80211_hdr *fwd_hdr;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001497 struct ieee80211_tx_info *info;
1498
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001499 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1500
1501 if (!fwd_skb && net_ratelimit())
1502 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001503 sdata->dev->name);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001504
1505 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001506 memcpy(fwd_hdr->addr2, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001507 info = IEEE80211_SKB_CB(fwd_skb);
1508 memset(info, 0, sizeof(*info));
1509 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001510 info->control.vif = &rx->sdata->vif;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001511 ieee80211_select_queue(local, fwd_skb);
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001512 if (is_multicast_ether_addr(fwd_hdr->addr1))
1513 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1514 fwded_mcast);
1515 else {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001516 int err;
1517 /*
1518 * Save TA to addr1 to send TA a path error if a
1519 * suitable next hop is not found
1520 */
1521 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
Javier Cardona249b4052009-07-07 10:55:03 -07001522 ETH_ALEN);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001523 err = mesh_nexthop_lookup(fwd_skb, sdata);
Javier Cardona249b4052009-07-07 10:55:03 -07001524 /* Failed to immediately resolve next hop:
1525 * fwded frame was dropped or will be added
1526 * later to the pending skb queue. */
1527 if (err)
1528 return RX_DROP_MONITOR;
Daniel Walkerc8a61a72009-08-18 10:59:00 -07001529
1530 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1531 fwded_unicast);
Javier Cardona249b4052009-07-07 10:55:03 -07001532 }
1533 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1534 fwded_frames);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001535 ieee80211_add_pending_skb(local, fwd_skb);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001536 }
1537 }
1538
Javier Cardona3c5772a2009-08-10 12:15:48 -07001539 if (is_multicast_ether_addr(hdr->addr1) ||
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001540 sdata->dev->flags & IFF_PROMISC)
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001541 return RX_CONTINUE;
1542 else
1543 return RX_DROP_MONITOR;
1544}
Ingo Molnarbf94e172008-10-12 23:51:38 -07001545#endif
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001546
1547static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001548ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001549{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001550 struct ieee80211_sub_if_data *sdata = rx->sdata;
1551 struct net_device *dev = sdata->dev;
Harvey Harrison358c8d92008-07-15 18:44:13 -07001552 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1553 __le16 fc = hdr->frame_control;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001554 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001555
Harvey Harrison358c8d92008-07-15 18:44:13 -07001556 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001557 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001558
Harvey Harrison358c8d92008-07-15 18:44:13 -07001559 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001560 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001561
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001562 /*
1563 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1564 * that a 4-addr station can be detected and moved into a separate VLAN
1565 */
1566 if (ieee80211_has_a4(hdr->frame_control) &&
1567 sdata->vif.type == NL80211_IFTYPE_AP)
1568 return RX_DROP_MONITOR;
1569
Zhu Yie31a16d2009-05-21 21:47:03 +08001570 err = __ieee80211_data_to_8023(rx);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001571 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001572 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001573
Harvey Harrison358c8d92008-07-15 18:44:13 -07001574 if (!ieee80211_frame_allowed(rx, fc))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001575 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001576
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001577 rx->skb->dev = dev;
1578
1579 dev->stats.rx_packets++;
1580 dev->stats.rx_bytes += rx->skb->len;
1581
1582 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001583
Johannes Berg9ae54c82008-01-31 19:48:20 +01001584 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001585}
1586
Johannes Berg49461622008-06-30 15:10:45 +02001587static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001588ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
Ron Rindjunsky71364712007-12-25 17:00:36 +02001589{
1590 struct ieee80211_local *local = rx->local;
1591 struct ieee80211_hw *hw = &local->hw;
1592 struct sk_buff *skb = rx->skb;
Harvey Harrisona7767f92008-07-02 16:30:51 -07001593 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001594 struct tid_ampdu_rx *tid_agg_rx;
1595 u16 start_seq_num;
1596 u16 tid;
1597
Harvey Harrisona7767f92008-07-02 16:30:51 -07001598 if (likely(!ieee80211_is_ctl(bar->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001599 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001600
Harvey Harrisona7767f92008-07-02 16:30:51 -07001601 if (ieee80211_is_back_req(bar->frame_control)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +02001602 if (!rx->sta)
Johannes Berga02ae752009-11-16 12:00:40 +01001603 return RX_DROP_MONITOR;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001604 tid = le16_to_cpu(bar->control) >> 12;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001605 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1606 != HT_AGG_STATE_OPERATIONAL)
Johannes Berga02ae752009-11-16 12:00:40 +01001607 return RX_DROP_MONITOR;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001608 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
Ron Rindjunsky71364712007-12-25 17:00:36 +02001609
1610 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1611
1612 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01001613 if (tid_agg_rx->timeout)
1614 mod_timer(&tid_agg_rx->session_timer,
1615 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunsky71364712007-12-25 17:00:36 +02001616
Johannes Berga02ae752009-11-16 12:00:40 +01001617 /* release stored frames up to start of BAR */
1618 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
1619 kfree_skb(skb);
1620 return RX_QUEUED;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001621 }
1622
Johannes Berg9ae54c82008-01-31 19:48:20 +01001623 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001624}
1625
Jouni Malinenf4f727a2009-01-10 11:46:53 +02001626static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1627 struct ieee80211_mgmt *mgmt,
1628 size_t len)
Jouni Malinenfea14732009-01-08 13:32:06 +02001629{
1630 struct ieee80211_local *local = sdata->local;
1631 struct sk_buff *skb;
1632 struct ieee80211_mgmt *resp;
1633
1634 if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
1635 /* Not to own unicast address */
1636 return;
1637 }
1638
Johannes Berg46900292009-02-15 12:44:28 +01001639 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1640 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02001641 /* Not from the current AP or not associated yet. */
Jouni Malinenfea14732009-01-08 13:32:06 +02001642 return;
1643 }
1644
1645 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1646 /* Too short SA Query request frame */
1647 return;
1648 }
1649
1650 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1651 if (skb == NULL)
1652 return;
1653
1654 skb_reserve(skb, local->hw.extra_tx_headroom);
1655 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1656 memset(resp, 0, 24);
1657 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1658 memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001659 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
Jouni Malinenfea14732009-01-08 13:32:06 +02001660 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1661 IEEE80211_STYPE_ACTION);
1662 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1663 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1664 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1665 memcpy(resp->u.action.u.sa_query.trans_id,
1666 mgmt->u.action.u.sa_query.trans_id,
1667 WLAN_SA_QUERY_TR_ID_LEN);
1668
Johannes Berg62ae67b2009-11-18 18:42:05 +01001669 ieee80211_tx_skb(sdata, skb);
Jouni Malinenfea14732009-01-08 13:32:06 +02001670}
1671
Johannes Berg49461622008-06-30 15:10:45 +02001672static ieee80211_rx_result debug_noinline
Johannes Bergde1ede72008-09-09 14:42:50 +02001673ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1674{
1675 struct ieee80211_local *local = rx->local;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001676 struct ieee80211_sub_if_data *sdata = rx->sdata;
Johannes Bergde1ede72008-09-09 14:42:50 +02001677 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1678 int len = rx->skb->len;
1679
1680 if (!ieee80211_is_action(mgmt->frame_control))
1681 return RX_CONTINUE;
1682
1683 if (!rx->sta)
1684 return RX_DROP_MONITOR;
1685
1686 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1687 return RX_DROP_MONITOR;
1688
Jouni Malinen97ebe122009-01-08 13:32:08 +02001689 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1690 return RX_DROP_MONITOR;
1691
Johannes Bergde1ede72008-09-09 14:42:50 +02001692 /* all categories we currently handle have action_code */
1693 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1694 return RX_DROP_MONITOR;
1695
Johannes Bergde1ede72008-09-09 14:42:50 +02001696 switch (mgmt->u.action.category) {
1697 case WLAN_CATEGORY_BACK:
Johannes Berg8abd3f92009-02-10 21:25:47 +01001698 /*
1699 * The aggregation code is not prepared to handle
1700 * anything but STA/AP due to the BSSID handling;
1701 * IBSS could work in the code but isn't supported
1702 * by drivers or the standard.
1703 */
1704 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1705 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1706 sdata->vif.type != NL80211_IFTYPE_AP)
1707 return RX_DROP_MONITOR;
1708
Johannes Bergde1ede72008-09-09 14:42:50 +02001709 switch (mgmt->u.action.u.addba_req.action_code) {
1710 case WLAN_ACTION_ADDBA_REQ:
1711 if (len < (IEEE80211_MIN_ACTION_SIZE +
1712 sizeof(mgmt->u.action.u.addba_req)))
1713 return RX_DROP_MONITOR;
1714 ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1715 break;
1716 case WLAN_ACTION_ADDBA_RESP:
1717 if (len < (IEEE80211_MIN_ACTION_SIZE +
1718 sizeof(mgmt->u.action.u.addba_resp)))
1719 return RX_DROP_MONITOR;
1720 ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1721 break;
1722 case WLAN_ACTION_DELBA:
1723 if (len < (IEEE80211_MIN_ACTION_SIZE +
1724 sizeof(mgmt->u.action.u.delba)))
1725 return RX_DROP_MONITOR;
1726 ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1727 break;
1728 }
Johannes Berg39192c02008-09-09 14:49:03 +02001729 break;
1730 case WLAN_CATEGORY_SPECTRUM_MGMT:
1731 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1732 return RX_DROP_MONITOR;
Johannes Berg46900292009-02-15 12:44:28 +01001733
1734 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1735 return RX_DROP_MONITOR;
1736
Johannes Berg39192c02008-09-09 14:49:03 +02001737 switch (mgmt->u.action.u.measurement.action_code) {
1738 case WLAN_ACTION_SPCT_MSR_REQ:
1739 if (len < (IEEE80211_MIN_ACTION_SIZE +
1740 sizeof(mgmt->u.action.u.measurement)))
1741 return RX_DROP_MONITOR;
1742 ieee80211_process_measurement_req(sdata, mgmt, len);
1743 break;
Sujithc481ec92009-01-06 09:28:37 +05301744 case WLAN_ACTION_SPCT_CHL_SWITCH:
1745 if (len < (IEEE80211_MIN_ACTION_SIZE +
1746 sizeof(mgmt->u.action.u.chan_switch)))
1747 return RX_DROP_MONITOR;
1748
Johannes Bergcc32abd2009-05-15 11:52:31 +02001749 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1750 return RX_DROP_MONITOR;
1751
Johannes Berg46900292009-02-15 12:44:28 +01001752 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
Sujithc481ec92009-01-06 09:28:37 +05301753 return RX_DROP_MONITOR;
1754
Johannes Berg77fdaa12009-07-07 03:45:17 +02001755 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg39192c02008-09-09 14:49:03 +02001756 }
1757 break;
Jouni Malinenfea14732009-01-08 13:32:06 +02001758 case WLAN_CATEGORY_SA_QUERY:
1759 if (len < (IEEE80211_MIN_ACTION_SIZE +
1760 sizeof(mgmt->u.action.u.sa_query)))
1761 return RX_DROP_MONITOR;
1762 switch (mgmt->u.action.u.sa_query.action) {
1763 case WLAN_ACTION_SA_QUERY_REQUEST:
1764 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1765 return RX_DROP_MONITOR;
1766 ieee80211_process_sa_query_req(sdata, mgmt, len);
1767 break;
1768 case WLAN_ACTION_SA_QUERY_RESPONSE:
1769 /*
1770 * SA Query response is currently only used in AP mode
1771 * and it is processed in user space.
1772 */
1773 return RX_CONTINUE;
1774 }
1775 break;
Johannes Berg39192c02008-09-09 14:49:03 +02001776 default:
1777 return RX_CONTINUE;
Johannes Bergde1ede72008-09-09 14:42:50 +02001778 }
1779
Johannes Berg39192c02008-09-09 14:49:03 +02001780 rx->sta->rx_packets++;
1781 dev_kfree_skb(rx->skb);
1782 return RX_QUEUED;
Johannes Bergde1ede72008-09-09 14:42:50 +02001783}
1784
1785static ieee80211_rx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +01001786ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001787{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001788 struct ieee80211_sub_if_data *sdata = rx->sdata;
Jouni Malinen97ebe122009-01-08 13:32:08 +02001789 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02001790
Johannes Berg5cf121c2008-02-25 16:27:43 +01001791 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001792 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001793
Jouni Malinen97ebe122009-01-08 13:32:08 +02001794 if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
1795 return RX_DROP_MONITOR;
1796
Johannes Berg472dbc42008-09-11 00:01:49 +02001797 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Bergf1d58c22009-06-17 13:13:00 +02001798 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
Johannes Berg472dbc42008-09-11 00:01:49 +02001799
Johannes Berg3832c282009-03-24 08:46:57 +01001800 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
Johannes Bergf1d58c22009-06-17 13:13:00 +02001801 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001802
Johannes Berg7986cf92009-03-21 17:08:43 +01001803 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Johannes Bergf1d58c22009-06-17 13:13:00 +02001804 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
Johannes Berg46900292009-02-15 12:44:28 +01001805
Johannes Berg7986cf92009-03-21 17:08:43 +01001806 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001807}
1808
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001809static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001810 struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001811{
Harvey Harrisona7767f92008-07-02 16:30:51 -07001812 int keyidx;
1813 unsigned int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001814
Harvey Harrisona7767f92008-07-02 16:30:51 -07001815 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001816 if (rx->skb->len >= hdrlen + 4)
1817 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1818 else
1819 keyidx = -1;
1820
Johannes Berg8944b792008-01-31 19:48:26 +01001821 if (!rx->sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001822 /*
1823 * Some hardware seem to generate incorrect Michael MIC
1824 * reports; ignore them to avoid triggering countermeasures.
1825 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01001826 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001827 }
1828
Harvey Harrisona7767f92008-07-02 16:30:51 -07001829 if (!ieee80211_has_protected(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01001830 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001831
Johannes Berg05c914f2008-09-11 00:01:58 +02001832 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001833 /*
1834 * APs with pairwise keys should never receive Michael MIC
1835 * errors for non-zero keyidx because these are reserved for
1836 * group keys and only the AP is sending real multicast
1837 * frames in the BSS.
1838 */
Johannes Bergaf2ced62009-11-16 12:00:39 +01001839 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001840 }
1841
Harvey Harrisona7767f92008-07-02 16:30:51 -07001842 if (!ieee80211_is_data(hdr->frame_control) &&
1843 !ieee80211_is_auth(hdr->frame_control))
Johannes Bergaf2ced62009-11-16 12:00:39 +01001844 return;
Johannes Berg571ecf62007-07-27 15:43:22 +02001845
Johannes Berge6d6e342009-07-01 21:26:47 +02001846 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
1847 GFP_ATOMIC);
Johannes Berg571ecf62007-07-27 15:43:22 +02001848}
1849
Johannes Berg5cf121c2008-02-25 16:27:43 +01001850/* TODO: use IEEE80211_RX_FRAGMENTED */
Johannes Berg5f0b7de2009-11-16 13:58:21 +01001851static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
1852 struct ieee80211_rate *rate)
Michael Wu3d30d942008-01-31 19:48:27 +01001853{
1854 struct ieee80211_sub_if_data *sdata;
1855 struct ieee80211_local *local = rx->local;
1856 struct ieee80211_rtap_hdr {
1857 struct ieee80211_radiotap_header hdr;
1858 u8 flags;
Johannes Berg5f0b7de2009-11-16 13:58:21 +01001859 u8 rate_or_pad;
Michael Wu3d30d942008-01-31 19:48:27 +01001860 __le16 chan_freq;
1861 __le16 chan_flags;
1862 } __attribute__ ((packed)) *rthdr;
1863 struct sk_buff *skb = rx->skb, *skb2;
1864 struct net_device *prev_dev = NULL;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01001865 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Michael Wu3d30d942008-01-31 19:48:27 +01001866
Johannes Berg5cf121c2008-02-25 16:27:43 +01001867 if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
Michael Wu3d30d942008-01-31 19:48:27 +01001868 goto out_free_skb;
1869
1870 if (skb_headroom(skb) < sizeof(*rthdr) &&
1871 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1872 goto out_free_skb;
1873
1874 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1875 memset(rthdr, 0, sizeof(*rthdr));
1876 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1877 rthdr->hdr.it_present =
1878 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
Michael Wu3d30d942008-01-31 19:48:27 +01001879 (1 << IEEE80211_RADIOTAP_CHANNEL));
1880
Johannes Berg5f0b7de2009-11-16 13:58:21 +01001881 if (rate) {
1882 rthdr->rate_or_pad = rate->bitrate / 5;
1883 rthdr->hdr.it_present |=
1884 cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
1885 }
Michael Wu3d30d942008-01-31 19:48:27 +01001886 rthdr->chan_freq = cpu_to_le16(status->freq);
1887
1888 if (status->band == IEEE80211_BAND_5GHZ)
1889 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1890 IEEE80211_CHAN_5GHZ);
1891 else
1892 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1893 IEEE80211_CHAN_2GHZ);
1894
1895 skb_set_mac_header(skb, 0);
1896 skb->ip_summed = CHECKSUM_UNNECESSARY;
1897 skb->pkt_type = PACKET_OTHERHOST;
1898 skb->protocol = htons(ETH_P_802_2);
1899
1900 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1901 if (!netif_running(sdata->dev))
1902 continue;
1903
Johannes Berg05c914f2008-09-11 00:01:58 +02001904 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
Michael Wu3d30d942008-01-31 19:48:27 +01001905 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1906 continue;
1907
1908 if (prev_dev) {
1909 skb2 = skb_clone(skb, GFP_ATOMIC);
1910 if (skb2) {
1911 skb2->dev = prev_dev;
1912 netif_rx(skb2);
1913 }
1914 }
1915
1916 prev_dev = sdata->dev;
1917 sdata->dev->stats.rx_packets++;
1918 sdata->dev->stats.rx_bytes += skb->len;
1919 }
1920
1921 if (prev_dev) {
1922 skb->dev = prev_dev;
1923 netif_rx(skb);
1924 skb = NULL;
1925 } else
1926 goto out_free_skb;
1927
Johannes Berg5cf121c2008-02-25 16:27:43 +01001928 rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
Michael Wu3d30d942008-01-31 19:48:27 +01001929 return;
1930
1931 out_free_skb:
1932 dev_kfree_skb(skb);
1933}
1934
Johannes Berg571ecf62007-07-27 15:43:22 +02001935
Johannes Berg8944b792008-01-31 19:48:26 +01001936static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001937 struct ieee80211_rx_data *rx,
Johannes Berg5f0b7de2009-11-16 13:58:21 +01001938 struct sk_buff *skb,
1939 struct ieee80211_rate *rate)
Johannes Berg58905292008-01-31 19:48:25 +01001940{
Johannes Berg58905292008-01-31 19:48:25 +01001941 ieee80211_rx_result res = RX_DROP_MONITOR;
1942
Johannes Berg8944b792008-01-31 19:48:26 +01001943 rx->skb = skb;
1944 rx->sdata = sdata;
Johannes Berg8944b792008-01-31 19:48:26 +01001945
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001946#define CALL_RXH(rxh) \
1947 do { \
1948 res = rxh(rx); \
1949 if (res != RX_CONTINUE) \
1950 goto rxh_done; \
1951 } while (0);
Johannes Berg58905292008-01-31 19:48:25 +01001952
Johannes Berg49461622008-06-30 15:10:45 +02001953 CALL_RXH(ieee80211_rx_h_passive_scan)
1954 CALL_RXH(ieee80211_rx_h_check)
1955 CALL_RXH(ieee80211_rx_h_decrypt)
Kalle Valo572e0012009-02-10 17:09:31 +02001956 CALL_RXH(ieee80211_rx_h_check_more_data)
Johannes Berg49461622008-06-30 15:10:45 +02001957 CALL_RXH(ieee80211_rx_h_sta_process)
1958 CALL_RXH(ieee80211_rx_h_defragment)
1959 CALL_RXH(ieee80211_rx_h_ps_poll)
1960 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
1961 /* must be after MMIC verify so header is counted in MPDU mic */
1962 CALL_RXH(ieee80211_rx_h_remove_qos_control)
1963 CALL_RXH(ieee80211_rx_h_amsdu)
Ingo Molnarbf94e172008-10-12 23:51:38 -07001964#ifdef CONFIG_MAC80211_MESH
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001965 if (ieee80211_vif_is_mesh(&sdata->vif))
1966 CALL_RXH(ieee80211_rx_h_mesh_fwding);
Ingo Molnarbf94e172008-10-12 23:51:38 -07001967#endif
Johannes Berg49461622008-06-30 15:10:45 +02001968 CALL_RXH(ieee80211_rx_h_data)
1969 CALL_RXH(ieee80211_rx_h_ctrl)
Johannes Bergde1ede72008-09-09 14:42:50 +02001970 CALL_RXH(ieee80211_rx_h_action)
Johannes Berg49461622008-06-30 15:10:45 +02001971 CALL_RXH(ieee80211_rx_h_mgmt)
Johannes Berg58905292008-01-31 19:48:25 +01001972
Johannes Berg49461622008-06-30 15:10:45 +02001973#undef CALL_RXH
1974
1975 rxh_done:
Johannes Berg58905292008-01-31 19:48:25 +01001976 switch (res) {
Michael Wu3d30d942008-01-31 19:48:27 +01001977 case RX_DROP_MONITOR:
Johannes Berg49461622008-06-30 15:10:45 +02001978 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1979 if (rx->sta)
1980 rx->sta->rx_dropped++;
1981 /* fall through */
1982 case RX_CONTINUE:
Johannes Berg5f0b7de2009-11-16 13:58:21 +01001983 ieee80211_rx_cooked_monitor(rx, rate);
Michael Wu3d30d942008-01-31 19:48:27 +01001984 break;
1985 case RX_DROP_UNUSABLE:
Johannes Berg49461622008-06-30 15:10:45 +02001986 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1987 if (rx->sta)
1988 rx->sta->rx_dropped++;
Johannes Berg58905292008-01-31 19:48:25 +01001989 dev_kfree_skb(rx->skb);
1990 break;
Johannes Berg49461622008-06-30 15:10:45 +02001991 case RX_QUEUED:
1992 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
1993 break;
Johannes Berg58905292008-01-31 19:48:25 +01001994 }
1995}
1996
Johannes Berg571ecf62007-07-27 15:43:22 +02001997/* main receive path */
1998
Johannes Berg23a24de2007-07-27 15:43:22 +02001999static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg7ab17c42009-02-10 21:25:43 +01002000 struct ieee80211_rx_data *rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02002001 struct ieee80211_hdr *hdr)
2002{
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002003 struct sk_buff *skb = rx->skb;
2004 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2005 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg23a24de2007-07-27 15:43:22 +02002006 int multicast = is_multicast_ether_addr(hdr->addr1);
2007
Johannes Berg51fb61e2007-12-19 01:31:27 +01002008 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002009 case NL80211_IFTYPE_STATION:
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002010 if (!bssid && !sdata->use_4addr)
Johannes Berg23a24de2007-07-27 15:43:22 +02002011 return 0;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002012 if (!multicast &&
2013 compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04002014 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002015 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002016 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002017 }
2018 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002019 case NL80211_IFTYPE_ADHOC:
Johannes Berg23a24de2007-07-27 15:43:22 +02002020 if (!bssid)
2021 return 0;
Harvey Harrisona7767f92008-07-02 16:30:51 -07002022 if (ieee80211_is_beacon(hdr->frame_control)) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002023 return 1;
Vladimir Koutny87291c02008-06-13 16:50:44 +02002024 }
Johannes Berg46900292009-02-15 12:44:28 +01002025 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002026 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002027 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002028 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002029 } else if (!multicast &&
2030 compare_ether_addr(sdata->dev->dev_addr,
2031 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04002032 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02002033 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002034 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002035 } else if (!rx->sta) {
2036 int rate_idx;
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002037 if (status->flag & RX_FLAG_HT)
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002038 rate_idx = 0; /* TODO: HT rates */
2039 else
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002040 rate_idx = status->rate_idx;
Rami Rosenab1f5c02008-12-11 14:00:25 +02002041 rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002042 BIT(rate_idx));
2043 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002044 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002045 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg6032f932008-02-23 15:17:07 +01002046 if (!multicast &&
2047 compare_ether_addr(sdata->dev->dev_addr,
2048 hdr->addr1) != 0) {
2049 if (!(sdata->dev->flags & IFF_PROMISC))
2050 return 0;
2051
Johannes Berg5cf121c2008-02-25 16:27:43 +01002052 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg6032f932008-02-23 15:17:07 +01002053 }
2054 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002055 case NL80211_IFTYPE_AP_VLAN:
2056 case NL80211_IFTYPE_AP:
Johannes Berg23a24de2007-07-27 15:43:22 +02002057 if (!bssid) {
2058 if (compare_ether_addr(sdata->dev->dev_addr,
2059 hdr->addr1))
2060 return 0;
2061 } else if (!ieee80211_bssid_match(bssid,
2062 sdata->dev->dev_addr)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01002063 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02002064 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002065 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02002066 }
Johannes Berg23a24de2007-07-27 15:43:22 +02002067 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002068 case NL80211_IFTYPE_WDS:
Harvey Harrisona7767f92008-07-02 16:30:51 -07002069 if (bssid || !ieee80211_is_data(hdr->frame_control))
Johannes Berg23a24de2007-07-27 15:43:22 +02002070 return 0;
2071 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2072 return 0;
2073 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002074 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +02002075 case NL80211_IFTYPE_UNSPECIFIED:
2076 case __NL80211_IFTYPE_AFTER_LAST:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02002077 /* should never get here */
2078 WARN_ON(1);
2079 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02002080 }
2081
2082 return 1;
2083}
2084
Johannes Berg571ecf62007-07-27 15:43:22 +02002085/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002086 * This is the actual Rx frames handler. as it blongs to Rx path it must
2087 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02002088 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002089static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2090 struct sk_buff *skb,
Johannes Berg8318d782008-01-24 19:38:38 +01002091 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +02002092{
Johannes Bergf1d58c22009-06-17 13:13:00 +02002093 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002094 struct ieee80211_local *local = hw_to_local(hw);
2095 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002096 struct ieee80211_hdr *hdr;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002097 struct ieee80211_rx_data rx;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002098 int prepares;
Johannes Berg8e6f00322007-07-27 15:43:22 +02002099 struct ieee80211_sub_if_data *prev = NULL;
2100 struct sk_buff *skb_new;
Johannes Berg571ecf62007-07-27 15:43:22 +02002101
Harvey Harrison358c8d92008-07-15 18:44:13 -07002102 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg571ecf62007-07-27 15:43:22 +02002103 memset(&rx, 0, sizeof(rx));
2104 rx.skb = skb;
2105 rx.local = local;
Johannes Berg72abd812007-09-17 01:29:22 -04002106
Harvey Harrison358c8d92008-07-15 18:44:13 -07002107 if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
Johannes Berg571ecf62007-07-27 15:43:22 +02002108 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02002109
Helmut Schaa142b9f52009-07-23 13:18:01 +02002110 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2111 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
Johannes Berg5cf121c2008-02-25 16:27:43 +01002112 rx.flags |= IEEE80211_RX_IN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02002113
Johannes Berg38f37142008-01-29 17:07:43 +01002114 ieee80211_parse_qos(&rx);
Johannes Bergd1c3a372009-01-07 00:26:10 +01002115 ieee80211_verify_alignment(&rx);
Johannes Berg38f37142008-01-29 17:07:43 +01002116
Johannes Bergaf2ced62009-11-16 12:00:39 +01002117 rx.sta = sta_info_get(local, hdr->addr2);
Johannes Bergeb9fb5b82009-11-16 13:58:20 +01002118 if (rx.sta)
Johannes Bergaf2ced62009-11-16 12:00:39 +01002119 rx.sdata = rx.sta->sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002120
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002121 if (rx.sdata && ieee80211_is_data(hdr->frame_control)) {
2122 rx.flags |= IEEE80211_RX_RA_MATCH;
2123 prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
Johannes Bergaf2ced62009-11-16 12:00:39 +01002124 if (prepares) {
2125 if (status->flag & RX_FLAG_MMIC_ERROR) {
2126 if (rx.flags & IEEE80211_RX_RA_MATCH)
2127 ieee80211_rx_michael_mic_report(hdr, &rx);
2128 } else
2129 prev = rx.sdata;
2130 }
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002131 } else list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04002132 if (!netif_running(sdata->dev))
2133 continue;
2134
Johannes Bergfbc44bf2009-10-01 22:06:29 +02002135 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2136 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Bergb2e77712007-09-26 15:19:39 +02002137 continue;
2138
Johannes Berg5cf121c2008-02-25 16:27:43 +01002139 rx.flags |= IEEE80211_RX_RA_MATCH;
Johannes Berg7ab17c42009-02-10 21:25:43 +01002140 prepares = prepare_for_handlers(sdata, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02002141
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002142 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02002143 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02002144
Johannes Bergaf2ced62009-11-16 12:00:39 +01002145 if (status->flag & RX_FLAG_MMIC_ERROR) {
2146 rx.sdata = sdata;
2147 if (rx.flags & IEEE80211_RX_RA_MATCH)
2148 ieee80211_rx_michael_mic_report(hdr, &rx);
2149 continue;
2150 }
2151
Johannes Berg340e11f2007-07-27 15:43:22 +02002152 /*
2153 * frame is destined for this interface, but if it's not
2154 * also for the previous one we handle that after the
2155 * loop to avoid copying the SKB once too much
2156 */
2157
2158 if (!prev) {
2159 prev = sdata;
2160 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02002161 }
Johannes Berg340e11f2007-07-27 15:43:22 +02002162
2163 /*
2164 * frame was destined for the previous interface
2165 * so invoke RX handlers for it
2166 */
2167
2168 skb_new = skb_copy(skb, GFP_ATOMIC);
2169 if (!skb_new) {
2170 if (net_ratelimit())
2171 printk(KERN_DEBUG "%s: failed to copy "
Pavel Roskina4278e12008-05-12 09:02:24 -04002172 "multicast frame for %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04002173 wiphy_name(local->hw.wiphy),
2174 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02002175 continue;
2176 }
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002177 ieee80211_invoke_rx_handlers(prev, &rx, skb_new, rate);
Johannes Berg8e6f00322007-07-27 15:43:22 +02002178 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02002179 }
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002180 if (prev)
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002181 ieee80211_invoke_rx_handlers(prev, &rx, skb, rate);
Harvey Harrisona4b7d7b2008-07-15 18:44:14 -07002182 else
Johannes Berg8e6f00322007-07-27 15:43:22 +02002183 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02002184}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002185
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002186#define SEQ_MODULO 0x1000
2187#define SEQ_MASK 0xfff
2188
2189static inline int seq_less(u16 sq1, u16 sq2)
2190{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002191 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002192}
2193
2194static inline u16 seq_inc(u16 sq)
2195{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002196 return (sq + 1) & SEQ_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002197}
2198
2199static inline u16 seq_sub(u16 sq1, u16 sq2)
2200{
Johannes Bergc6a1fa12008-10-07 12:04:32 +02002201 return (sq1 - sq2) & SEQ_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002202}
2203
2204
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002205static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
2206 struct tid_ampdu_rx *tid_agg_rx,
2207 int index)
2208{
2209 struct ieee80211_supported_band *sband;
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002210 struct ieee80211_rate *rate = NULL;
Johannes Bergf1d58c22009-06-17 13:13:00 +02002211 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
2212 struct ieee80211_rx_status *status;
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002213
Johannes Bergf1d58c22009-06-17 13:13:00 +02002214 if (!skb)
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002215 goto no_frame;
2216
Johannes Bergf1d58c22009-06-17 13:13:00 +02002217 status = IEEE80211_SKB_RXCB(skb);
2218
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002219 /* release the reordered frames to stack */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002220 sband = hw->wiphy->bands[status->band];
Johannes Berg5f0b7de2009-11-16 13:58:21 +01002221 if (!(status->flag & RX_FLAG_HT))
Johannes Bergf1d58c22009-06-17 13:13:00 +02002222 rate = &sband->bitrates[status->rate_idx];
2223 __ieee80211_rx_handle_packet(hw, skb, rate);
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002224 tid_agg_rx->stored_mpdu_num--;
2225 tid_agg_rx->reorder_buf[index] = NULL;
2226
2227no_frame:
2228 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2229}
2230
Johannes Berga02ae752009-11-16 12:00:40 +01002231static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
2232 struct tid_ampdu_rx *tid_agg_rx,
2233 u16 head_seq_num)
2234{
2235 int index;
2236
2237 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2238 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2239 tid_agg_rx->buf_size;
2240 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
2241 }
2242}
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002243
Ron Rindjunsky71364712007-12-25 17:00:36 +02002244/*
Jouni Malinen4d050f12009-05-05 20:35:14 +03002245 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
2246 * the skb was added to the buffer longer than this time ago, the earlier
2247 * frames that have not yet been received are assumed to be lost and the skb
2248 * can be released for processing. This may also release other skb's from the
2249 * reorder buffer if there are no additional gaps between the frames.
2250 */
2251#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
2252
2253/*
Johannes Berga02ae752009-11-16 12:00:40 +01002254 * As this function belongs to the RX path it must be under
2255 * rcu_read_lock protection. It returns false if the frame
2256 * can be processed immediately, true if it was consumed.
Ron Rindjunsky71364712007-12-25 17:00:36 +02002257 */
Johannes Berga02ae752009-11-16 12:00:40 +01002258static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
2259 struct tid_ampdu_rx *tid_agg_rx,
2260 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002261{
Johannes Berga02ae752009-11-16 12:00:40 +01002262 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2263 u16 sc = le16_to_cpu(hdr->seq_ctrl);
2264 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002265 u16 head_seq_num, buf_size;
2266 int index;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002267
2268 buf_size = tid_agg_rx->buf_size;
2269 head_seq_num = tid_agg_rx->head_seq_num;
2270
2271 /* frame with out of date sequence number */
2272 if (seq_less(mpdu_seq_num, head_seq_num)) {
2273 dev_kfree_skb(skb);
Johannes Berga02ae752009-11-16 12:00:40 +01002274 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002275 }
2276
Johannes Berga02ae752009-11-16 12:00:40 +01002277 /*
2278 * If frame the sequence number exceeds our buffering window
2279 * size release some previous frames to make room for this one.
2280 */
2281 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
2282 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002283 /* release stored frames up to new head to stack */
Johannes Berga02ae752009-11-16 12:00:40 +01002284 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002285 }
2286
Johannes Berga02ae752009-11-16 12:00:40 +01002287 /* Now the new frame is always in the range of the reordering buffer */
2288
2289 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2290
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002291 /* check if we already stored this frame */
2292 if (tid_agg_rx->reorder_buf[index]) {
2293 dev_kfree_skb(skb);
Johannes Berga02ae752009-11-16 12:00:40 +01002294 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002295 }
2296
Johannes Berga02ae752009-11-16 12:00:40 +01002297 /*
2298 * If the current MPDU is in the right order and nothing else
2299 * is stored we can process it directly, no need to buffer it.
2300 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002301 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
Johannes Berga02ae752009-11-16 12:00:40 +01002302 tid_agg_rx->stored_mpdu_num == 0) {
2303 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2304 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002305 }
2306
2307 /* put the frame in the reordering buffer */
2308 tid_agg_rx->reorder_buf[index] = skb;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002309 tid_agg_rx->reorder_time[index] = jiffies;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002310 tid_agg_rx->stored_mpdu_num++;
2311 /* release the buffer until next missing frame */
Johannes Berga02ae752009-11-16 12:00:40 +01002312 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2313 tid_agg_rx->buf_size;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002314 if (!tid_agg_rx->reorder_buf[index] &&
2315 tid_agg_rx->stored_mpdu_num > 1) {
2316 /*
2317 * No buffers ready to be released, but check whether any
2318 * frames in the reorder buffer have timed out.
2319 */
2320 int j;
2321 int skipped = 1;
2322 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
2323 j = (j + 1) % tid_agg_rx->buf_size) {
Johannes Berga02ae752009-11-16 12:00:40 +01002324 if (!tid_agg_rx->reorder_buf[j]) {
Jouni Malinen4d050f12009-05-05 20:35:14 +03002325 skipped++;
2326 continue;
2327 }
2328 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
Johannes Berga02ae752009-11-16 12:00:40 +01002329 HT_RX_REORDER_BUF_TIMEOUT))
Jouni Malinen4d050f12009-05-05 20:35:14 +03002330 break;
2331
2332#ifdef CONFIG_MAC80211_HT_DEBUG
2333 if (net_ratelimit())
2334 printk(KERN_DEBUG "%s: release an RX reorder "
2335 "frame due to timeout on earlier "
2336 "frames\n",
2337 wiphy_name(hw->wiphy));
2338#endif
2339 ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
2340
2341 /*
2342 * Increment the head seq# also for the skipped slots.
2343 */
2344 tid_agg_rx->head_seq_num =
Johannes Berga02ae752009-11-16 12:00:40 +01002345 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
Jouni Malinen4d050f12009-05-05 20:35:14 +03002346 skipped = 0;
2347 }
2348 } else while (tid_agg_rx->reorder_buf[index]) {
Jouni Malinen2d3babd2009-05-05 20:35:13 +03002349 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
Johannes Berga02ae752009-11-16 12:00:40 +01002350 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
2351 tid_agg_rx->buf_size;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002352 }
Johannes Berga02ae752009-11-16 12:00:40 +01002353
2354 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002355}
2356
Johannes Berga02ae752009-11-16 12:00:40 +01002357/*
2358 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
2359 * true if the MPDU was buffered, false if it should be processed.
2360 */
2361static bool ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2362 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002363{
2364 struct ieee80211_hw *hw = &local->hw;
2365 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2366 struct sta_info *sta;
2367 struct tid_ampdu_rx *tid_agg_rx;
Harvey Harrison87228f52008-06-11 14:21:59 -07002368 u16 sc;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002369 int tid;
2370
Johannes Berga02ae752009-11-16 12:00:40 +01002371 if (!ieee80211_is_data_qos(hdr->frame_control))
2372 return false;
2373
2374 /*
2375 * filter the QoS data rx stream according to
2376 * STA/TID and check if this STA/TID is on aggregation
2377 */
2378
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002379 sta = sta_info_get(local, hdr->addr2);
2380 if (!sta)
Johannes Berga02ae752009-11-16 12:00:40 +01002381 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002382
Harvey Harrison238f74a2008-07-02 11:05:34 -07002383 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002384
Ron Rindjunskycee24a32008-03-26 20:36:03 +02002385 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
Johannes Berga02ae752009-11-16 12:00:40 +01002386 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002387
Ron Rindjunskycee24a32008-03-26 20:36:03 +02002388 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
2389
Emmanuel Grumbach2560b6e2008-07-10 00:47:19 +03002390 /* qos null data frames are excluded */
2391 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
Johannes Berga02ae752009-11-16 12:00:40 +01002392 return false;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002393
Johannes Berga02ae752009-11-16 12:00:40 +01002394 /* new, potentially un-ordered, ampdu frame - process it */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002395
2396 /* reset session timer */
Johannes Berg20ad19d2009-02-10 21:25:45 +01002397 if (tid_agg_rx->timeout)
2398 mod_timer(&tid_agg_rx->session_timer,
2399 TU_TO_EXP_TIME(tid_agg_rx->timeout));
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002400
2401 /* if this mpdu is fragmented - terminate rx aggregation session */
2402 sc = le16_to_cpu(hdr->seq_ctrl);
2403 if (sc & IEEE80211_SCTL_FRAG) {
Johannes Berg17741cd2008-09-11 00:02:02 +02002404 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002405 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
Johannes Berga02ae752009-11-16 12:00:40 +01002406 dev_kfree_skb(skb);
2407 return true;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002408 }
2409
Johannes Berga02ae752009-11-16 12:00:40 +01002410 return ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002411}
2412
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002413/*
2414 * This is the receive path handler. It is called by a low level driver when an
2415 * 802.11 MPDU is received from the hardware.
2416 */
John W. Linville103bf9f2009-08-20 16:34:15 -04002417void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002418{
2419 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg8318d782008-01-24 19:38:38 +01002420 struct ieee80211_rate *rate = NULL;
2421 struct ieee80211_supported_band *sband;
Johannes Bergf1d58c22009-06-17 13:13:00 +02002422 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +01002423
Johannes Bergd20ef632009-10-11 15:10:40 +02002424 WARN_ON_ONCE(softirq_count() == 0);
2425
Johannes Berg77a980d2009-08-24 11:46:30 +02002426 if (WARN_ON(status->band < 0 ||
2427 status->band >= IEEE80211_NUM_BANDS))
2428 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002429
2430 sband = local->hw.wiphy->bands[status->band];
Johannes Berg77a980d2009-08-24 11:46:30 +02002431 if (WARN_ON(!sband))
2432 goto drop;
Johannes Berg8318d782008-01-24 19:38:38 +01002433
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002434 /*
2435 * If we're suspending, it is possible although not too likely
2436 * that we'd be receiving frames after having already partially
2437 * quiesced the stack. We can't process such frames then since
2438 * that might, for example, cause stations to be added or other
2439 * driver callbacks be invoked.
2440 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002441 if (unlikely(local->quiescing || local->suspended))
2442 goto drop;
Johannes Berg89c3a8a2009-07-28 18:10:17 +02002443
Johannes Bergea77f122009-08-21 14:44:45 +02002444 /*
2445 * The same happens when we're not even started,
2446 * but that's worth a warning.
2447 */
Johannes Berg77a980d2009-08-24 11:46:30 +02002448 if (WARN_ON(!local->started))
2449 goto drop;
Johannes Bergea77f122009-08-21 14:44:45 +02002450
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002451 if (status->flag & RX_FLAG_HT) {
Luis R. Rodrigueze5d6eb82009-11-09 16:03:22 -05002452 /*
2453 * rate_idx is MCS index, which can be [0-76] as documented on:
2454 *
2455 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2456 *
2457 * Anything else would be some sort of driver or hardware error.
2458 * The driver should catch hardware errors.
2459 */
2460 if (WARN((status->rate_idx < 0 ||
2461 status->rate_idx > 76),
2462 "Rate marked as an HT rate but passed "
2463 "status->rate_idx is not "
2464 "an MCS index [0-76]: %d (0x%02x)\n",
2465 status->rate_idx,
2466 status->rate_idx))
Johannes Berg77a980d2009-08-24 11:46:30 +02002467 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002468 } else {
2469 if (WARN_ON(status->rate_idx < 0 ||
2470 status->rate_idx >= sband->n_bitrates))
Johannes Berg77a980d2009-08-24 11:46:30 +02002471 goto drop;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02002472 rate = &sband->bitrates[status->rate_idx];
2473 }
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002474
2475 /*
2476 * key references and virtual interfaces are protected using RCU
2477 * and this requires that we are in a read-side RCU section during
2478 * receive processing
2479 */
2480 rcu_read_lock();
2481
2482 /*
2483 * Frames with failed FCS/PLCP checksum are not returned,
2484 * all other frames are returned without radiotap header
2485 * if it was previously present.
2486 * Also, frames with less than 16 bytes are dropped.
2487 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002488 skb = ieee80211_rx_monitor(local, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002489 if (!skb) {
2490 rcu_read_unlock();
2491 return;
2492 }
2493
Jouni Malinenaec67952009-05-05 20:35:15 +03002494 /*
2495 * In theory, the block ack reordering should happen after duplicate
2496 * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
2497 * the call to ieee80211_rx_reorder_ampdu() should really be moved to
2498 * happen as a new RX handler between ieee80211_rx_h_check and
2499 * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
2500 * the time being, the call can be here since RX reorder buf processing
2501 * will implicitly skip duplicates. We could, in theory at least,
2502 * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
2503 * frames from other than operational channel), but that should not
2504 * happen in normal networks.
2505 */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002506 if (!ieee80211_rx_reorder_ampdu(local, skb))
2507 __ieee80211_rx_handle_packet(hw, skb, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002508
2509 rcu_read_unlock();
Johannes Berg77a980d2009-08-24 11:46:30 +02002510
2511 return;
2512 drop:
2513 kfree_skb(skb);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002514}
John W. Linville103bf9f2009-08-20 16:34:15 -04002515EXPORT_SYMBOL(ieee80211_rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02002516
2517/* This is a version of the rx handler that can be called from hard irq
2518 * context. Post the skb on the queue and schedule the tasklet */
Johannes Bergf1d58c22009-06-17 13:13:00 +02002519void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
Johannes Berg571ecf62007-07-27 15:43:22 +02002520{
2521 struct ieee80211_local *local = hw_to_local(hw);
2522
2523 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2524
Johannes Berg571ecf62007-07-27 15:43:22 +02002525 skb->pkt_type = IEEE80211_RX_MSG;
2526 skb_queue_tail(&local->skb_queue, skb);
2527 tasklet_schedule(&local->tasklet);
2528}
2529EXPORT_SYMBOL(ieee80211_rx_irqsafe);