blob: 2e65ca1cd1aa8be324af49536acd5fc0e16e1c29 [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"
22#include "ieee80211_led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010023#include "mesh.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020024#include "wep.h"
25#include "wpa.h"
26#include "tkip.h"
27#include "wme.h"
28
Ron Rindjunsky71364712007-12-25 17:00:36 +020029u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
30 struct tid_ampdu_rx *tid_agg_rx,
31 struct sk_buff *skb, u16 mpdu_seq_num,
32 int bar_req);
Johannes Bergb2e77712007-09-26 15:19:39 +020033/*
34 * monitor mode reception
35 *
36 * This function cleans up the SKB, i.e. it removes all the stuff
37 * only useful for monitoring.
38 */
39static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
40 struct sk_buff *skb,
41 int rtap_len)
42{
43 skb_pull(skb, rtap_len);
44
45 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
46 if (likely(skb->len > FCS_LEN))
47 skb_trim(skb, skb->len - FCS_LEN);
48 else {
49 /* driver bug */
50 WARN_ON(1);
51 dev_kfree_skb(skb);
52 skb = NULL;
53 }
54 }
55
56 return skb;
57}
58
59static inline int should_drop_frame(struct ieee80211_rx_status *status,
60 struct sk_buff *skb,
61 int present_fcs_len,
62 int radiotap_len)
63{
64 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
65
66 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
67 return 1;
68 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
69 return 1;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020070 if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
71 cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
72 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
Ron Rindjunsky71364712007-12-25 17:00:36 +020073 cpu_to_le16(IEEE80211_STYPE_PSPOLL)) &&
74 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
75 cpu_to_le16(IEEE80211_STYPE_BACK_REQ)))
Johannes Bergb2e77712007-09-26 15:19:39 +020076 return 1;
77 return 0;
78}
79
80/*
81 * This function copies a received frame to all monitor interfaces and
82 * returns a cleaned-up SKB that no longer includes the FCS nor the
83 * radiotap header the driver might have added.
84 */
85static struct sk_buff *
86ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
Johannes Berg8318d782008-01-24 19:38:38 +010087 struct ieee80211_rx_status *status,
88 struct ieee80211_rate *rate)
Johannes Bergb2e77712007-09-26 15:19:39 +020089{
90 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2e77712007-09-26 15:19:39 +020091 int needed_headroom = 0;
Johannes Bergc49e5ea2007-12-11 21:33:42 +010092 struct ieee80211_radiotap_header *rthdr;
93 __le64 *rttsft = NULL;
94 struct ieee80211_rtap_fixed_data {
Johannes Bergb2e77712007-09-26 15:19:39 +020095 u8 flags;
96 u8 rate;
97 __le16 chan_freq;
98 __le16 chan_flags;
99 u8 antsignal;
100 u8 padding_for_rxflags;
101 __le16 rx_flags;
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100102 } __attribute__ ((packed)) *rtfixed;
Johannes Bergb2e77712007-09-26 15:19:39 +0200103 struct sk_buff *skb, *skb2;
104 struct net_device *prev_dev = NULL;
105 int present_fcs_len = 0;
106 int rtap_len = 0;
107
108 /*
109 * First, we may need to make a copy of the skb because
110 * (1) we need to modify it for radiotap (if not present), and
111 * (2) the other RX handlers will modify the skb we got.
112 *
113 * We don't need to, of course, if we aren't going to return
114 * the SKB because it has a bad FCS/PLCP checksum.
115 */
116 if (status->flag & RX_FLAG_RADIOTAP)
117 rtap_len = ieee80211_get_radiotap_len(origskb->data);
118 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100119 /* room for radiotap header, always present fields and TSFT */
120 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
Johannes Bergb2e77712007-09-26 15:19:39 +0200121
122 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
123 present_fcs_len = FCS_LEN;
124
125 if (!local->monitors) {
126 if (should_drop_frame(status, origskb, present_fcs_len,
127 rtap_len)) {
128 dev_kfree_skb(origskb);
129 return NULL;
130 }
131
132 return remove_monitor_info(local, origskb, rtap_len);
133 }
134
135 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
136 /* only need to expand headroom if necessary */
137 skb = origskb;
138 origskb = NULL;
139
140 /*
141 * This shouldn't trigger often because most devices have an
142 * RX header they pull before we get here, and that should
143 * be big enough for our radiotap information. We should
144 * probably export the length to drivers so that we can have
145 * them allocate enough headroom to start with.
146 */
147 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100148 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200149 dev_kfree_skb(skb);
150 return NULL;
151 }
152 } else {
153 /*
154 * Need to make a copy and possibly remove radiotap header
155 * and FCS from the original.
156 */
157 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
158
159 origskb = remove_monitor_info(local, origskb, rtap_len);
160
161 if (!skb)
162 return origskb;
163 }
164
165 /* if necessary, prepend radiotap information */
166 if (!(status->flag & RX_FLAG_RADIOTAP)) {
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100167 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
168 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
169 if (status->flag & RX_FLAG_TSFT) {
170 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
171 rtap_len += 8;
172 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200173 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
174 memset(rthdr, 0, sizeof(*rthdr));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100175 memset(rtfixed, 0, sizeof(*rtfixed));
176 rthdr->it_present =
Johannes Bergb2e77712007-09-26 15:19:39 +0200177 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
178 (1 << IEEE80211_RADIOTAP_RATE) |
179 (1 << IEEE80211_RADIOTAP_CHANNEL) |
180 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
181 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100182 rtfixed->flags = 0;
183 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
184 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
185
186 if (rttsft) {
187 *rttsft = cpu_to_le64(status->mactime);
188 rthdr->it_present |=
189 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
190 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200191
192 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100193 rtfixed->rx_flags = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200194 if (status->flag &
195 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100196 rtfixed->rx_flags |=
Johannes Bergb2e77712007-09-26 15:19:39 +0200197 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
198
Johannes Berg8318d782008-01-24 19:38:38 +0100199 rtfixed->rate = rate->bitrate / 5;
Johannes Bergb2e77712007-09-26 15:19:39 +0200200
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100201 rtfixed->chan_freq = cpu_to_le16(status->freq);
Johannes Bergb2e77712007-09-26 15:19:39 +0200202
Johannes Berg8318d782008-01-24 19:38:38 +0100203 if (status->band == IEEE80211_BAND_5GHZ)
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100204 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200205 cpu_to_le16(IEEE80211_CHAN_OFDM |
206 IEEE80211_CHAN_5GHZ);
207 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100208 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200209 cpu_to_le16(IEEE80211_CHAN_DYN |
210 IEEE80211_CHAN_2GHZ);
211
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100212 rtfixed->antsignal = status->ssi;
213 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Bergb2e77712007-09-26 15:19:39 +0200214 }
215
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100216 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200217 skb->ip_summed = CHECKSUM_UNNECESSARY;
218 skb->pkt_type = PACKET_OTHERHOST;
219 skb->protocol = htons(ETH_P_802_2);
220
221 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
222 if (!netif_running(sdata->dev))
223 continue;
224
Johannes Berg51fb61e2007-12-19 01:31:27 +0100225 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200226 continue;
227
Michael Wu3d30d942008-01-31 19:48:27 +0100228 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
229 continue;
230
Johannes Bergb2e77712007-09-26 15:19:39 +0200231 if (prev_dev) {
232 skb2 = skb_clone(skb, GFP_ATOMIC);
233 if (skb2) {
234 skb2->dev = prev_dev;
235 netif_rx(skb2);
236 }
237 }
238
239 prev_dev = sdata->dev;
240 sdata->dev->stats.rx_packets++;
241 sdata->dev->stats.rx_bytes += skb->len;
242 }
243
244 if (prev_dev) {
245 skb->dev = prev_dev;
246 netif_rx(skb);
247 } else
248 dev_kfree_skb(skb);
249
250 return origskb;
251}
252
253
Johannes Berg5cf121c2008-02-25 16:27:43 +0100254static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200255{
256 u8 *data = rx->skb->data;
257 int tid;
258
259 /* does the frame have a qos control field? */
260 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
261 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
262 /* frame has qos control */
263 tid = qc[0] & QOS_CONTROL_TID_MASK;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200264 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100265 rx->flags |= IEEE80211_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200266 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100267 rx->flags &= ~IEEE80211_RX_AMSDU;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200268 } else {
269 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
270 /* Separate TID for management frames */
271 tid = NUM_RX_DATA_QUEUES - 1;
272 } else {
273 /* no qos control present */
274 tid = 0; /* 802.1d - Best Effort */
275 }
276 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200277
Johannes Berg6e0d1142007-07-27 15:43:22 +0200278 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865dfd2007-07-27 15:43:22 +0200279 /* only a debug counter, sta might not be assigned properly yet */
280 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200281 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200282
Johannes Berg5cf121c2008-02-25 16:27:43 +0100283 rx->queue = tid;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200284 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
285 * For now, set skb->priority to 0 for other cases. */
286 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100287}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200288
Johannes Berg5cf121c2008-02-25 16:27:43 +0100289static void ieee80211_verify_ip_alignment(struct ieee80211_rx_data *rx)
Johannes Berg38f37142008-01-29 17:07:43 +0100290{
291#ifdef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
292 int hdrlen;
293
294 if (!WLAN_FC_DATA_PRESENT(rx->fc))
295 return;
296
297 /*
298 * Drivers are required to align the payload data in a way that
299 * guarantees that the contained IP header is aligned to a four-
300 * byte boundary. In the case of regular frames, this simply means
301 * aligning the payload to a four-byte boundary (because either
302 * the IP header is directly contained, or IV/RFC1042 headers that
303 * have a length divisible by four are in front of it.
304 *
305 * With A-MSDU frames, however, the payload data address must
306 * yield two modulo four because there are 14-byte 802.3 headers
307 * within the A-MSDU frames that push the IP header further back
308 * to a multiple of four again. Thankfully, the specs were sane
309 * enough this time around to require padding each A-MSDU subframe
310 * to a length that is a multiple of four.
311 *
312 * Padding like atheros hardware adds which is inbetween the 802.11
313 * header and the payload is not supported, the driver is required
314 * to move the 802.11 header further back in that case.
315 */
316 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100317 if (rx->flags & IEEE80211_RX_AMSDU)
Johannes Berg38f37142008-01-29 17:07:43 +0100318 hdrlen += ETH_HLEN;
319 WARN_ON_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3);
320#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200321}
322
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200323
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +0200324static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
Johannes Berg8318d782008-01-24 19:38:38 +0100325 struct sk_buff *skb,
326 struct ieee80211_rx_status *status,
327 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +0200328{
Johannes Berg571ecf62007-07-27 15:43:22 +0200329 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
330 u32 load = 0, hdrtime;
Johannes Berg571ecf62007-07-27 15:43:22 +0200331
332 /* Estimate total channel use caused by this frame */
333
Johannes Berg571ecf62007-07-27 15:43:22 +0200334 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
335 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
336
Johannes Berg8318d782008-01-24 19:38:38 +0100337 if (status->band == IEEE80211_BAND_5GHZ ||
338 (status->band == IEEE80211_BAND_5GHZ &&
339 rate->flags & IEEE80211_RATE_ERP_G))
Johannes Berg571ecf62007-07-27 15:43:22 +0200340 hdrtime = CHAN_UTIL_HDR_SHORT;
341 else
342 hdrtime = CHAN_UTIL_HDR_LONG;
343
344 load = hdrtime;
345 if (!is_multicast_ether_addr(hdr->addr1))
346 load += hdrtime;
347
Johannes Berg8318d782008-01-24 19:38:38 +0100348 /* TODO: optimise again */
349 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
Johannes Berg571ecf62007-07-27 15:43:22 +0200350
351 /* Divide channel_use by 8 to avoid wrapping around the counter */
352 load >>= CHAN_UTIL_SHIFT;
Johannes Berg571ecf62007-07-27 15:43:22 +0200353
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200354 return load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200355}
356
Johannes Berg571ecf62007-07-27 15:43:22 +0200357/* rx handlers */
358
Johannes Berg9ae54c82008-01-31 19:48:20 +0100359static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100360ieee80211_rx_h_if_stats(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200361{
Johannes Berg52865dfd2007-07-27 15:43:22 +0200362 if (rx->sta)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100363 rx->sta->channel_use_raw += rx->load;
364 rx->sdata->channel_use_raw += rx->load;
Johannes Berg9ae54c82008-01-31 19:48:20 +0100365 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200366}
367
Johannes Berg9ae54c82008-01-31 19:48:20 +0100368static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100369ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200370{
371 struct ieee80211_local *local = rx->local;
372 struct sk_buff *skb = rx->skb;
373
Zhu Yiece8edd2007-11-22 10:53:21 +0800374 if (unlikely(local->sta_hw_scanning))
Johannes Berg5cf121c2008-02-25 16:27:43 +0100375 return ieee80211_sta_rx_scan(rx->dev, skb, rx->status);
Zhu Yiece8edd2007-11-22 10:53:21 +0800376
377 if (unlikely(local->sta_sw_scanning)) {
378 /* drop all the other packets during a software scan anyway */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100379 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->status)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100380 != RX_QUEUED)
Zhu Yiece8edd2007-11-22 10:53:21 +0800381 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100382 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200383 }
384
Johannes Berg5cf121c2008-02-25 16:27:43 +0100385 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200386 /* scanning finished during invoking of handlers */
387 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100388 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200389 }
390
Johannes Berg9ae54c82008-01-31 19:48:20 +0100391 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200392}
393
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100394static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100395ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100396{
397 int hdrlen = ieee80211_get_hdrlen(rx->fc);
398 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100399
400#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
401
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100402 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
403 if (!((rx->fc & IEEE80211_FCTL_FROMDS) &&
404 (rx->fc & IEEE80211_FCTL_TODS)))
405 return RX_DROP_MONITOR;
406 if (memcmp(hdr->addr4, rx->dev->dev_addr, ETH_ALEN) == 0)
407 return RX_DROP_MONITOR;
408 }
409
410 /* If there is not an established peer link and this is not a peer link
411 * establisment frame, beacon or probe, drop the frame.
412 */
413
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100414 if (!rx->sta || sta_plink_state(rx->sta) != ESTAB) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100415 struct ieee80211_mgmt *mgmt;
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100416
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100417 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
418 return RX_DROP_MONITOR;
419
420 switch (rx->fc & IEEE80211_FCTL_STYPE) {
421 case IEEE80211_STYPE_ACTION:
422 mgmt = (struct ieee80211_mgmt *)hdr;
423 if (mgmt->u.action.category != PLINK_CATEGORY)
424 return RX_DROP_MONITOR;
425 /* fall through on else */
426 case IEEE80211_STYPE_PROBE_REQ:
427 case IEEE80211_STYPE_PROBE_RESP:
428 case IEEE80211_STYPE_BEACON:
429 return RX_CONTINUE;
430 break;
431 default:
432 return RX_DROP_MONITOR;
433 }
434
435 } else if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
436 is_broadcast_ether_addr(hdr->addr1) &&
437 mesh_rmc_check(hdr->addr4, msh_h_get(hdr, hdrlen), rx->dev))
438 return RX_DROP_MONITOR;
Johannes Berg902acc72008-02-23 15:17:19 +0100439#undef msh_h_get
Johannes Bergd6d1a5a2008-02-25 16:24:38 +0100440
Johannes Berg902acc72008-02-23 15:17:19 +0100441 return RX_CONTINUE;
442}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100443
444
Johannes Berg9ae54c82008-01-31 19:48:20 +0100445static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100446ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200447{
448 struct ieee80211_hdr *hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100449
Johannes Berg571ecf62007-07-27 15:43:22 +0200450 hdr = (struct ieee80211_hdr *) rx->skb->data;
451
452 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
453 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
454 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100455 rx->sta->last_seq_ctrl[rx->queue] ==
Johannes Berg571ecf62007-07-27 15:43:22 +0200456 hdr->seq_ctrl)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100457 if (rx->flags & IEEE80211_RX_RA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200458 rx->local->dot11FrameDuplicateCount++;
459 rx->sta->num_duplicates++;
460 }
Johannes Berge4c26ad2008-01-31 19:48:21 +0100461 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200462 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100463 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
Johannes Berg571ecf62007-07-27 15:43:22 +0200464 }
465
Johannes Berg571ecf62007-07-27 15:43:22 +0200466 if (unlikely(rx->skb->len < 16)) {
467 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100468 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200469 }
470
Johannes Berg571ecf62007-07-27 15:43:22 +0200471 /* Drop disallowed frame classes based on STA auth/assoc state;
472 * IEEE 802.11, Chap 5.5.
473 *
474 * 80211.o does filtering only based on association state, i.e., it
475 * drops Class 3 frames from not associated stations. hostapd sends
476 * deauth/disassoc frames when needed. In addition, hostapd is
477 * responsible for filtering on both auth and assoc states.
478 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100479
Johannes Berg902acc72008-02-23 15:17:19 +0100480 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100481 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100482
Johannes Berg571ecf62007-07-27 15:43:22 +0200483 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
484 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
485 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100486 rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200487 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
488 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
489 !(rx->fc & IEEE80211_FCTL_TODS) &&
490 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100491 || !(rx->flags & IEEE80211_RX_RA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200492 /* Drop IBSS frames and frames for other hosts
493 * silently. */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100494 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200495 }
496
Johannes Berge4c26ad2008-01-31 19:48:21 +0100497 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200498 }
499
Johannes Berg9ae54c82008-01-31 19:48:20 +0100500 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200501}
502
503
Johannes Berg9ae54c82008-01-31 19:48:20 +0100504static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100505ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200506{
507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400508 int keyidx;
509 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100510 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400511 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200512
Johannes Berg3017b802007-08-28 17:01:53 -0400513 /*
514 * Key selection 101
515 *
516 * There are three types of keys:
517 * - GTK (group keys)
518 * - PTK (pairwise keys)
519 * - STK (station-to-station pairwise keys)
520 *
521 * When selecting a key, we have to distinguish between multicast
522 * (including broadcast) and unicast frames, the latter can only
523 * use PTKs and STKs while the former always use GTKs. Unless, of
524 * course, actual WEP keys ("pre-RSNA") are used, then unicast
525 * frames can also use key indizes like GTKs. Hence, if we don't
526 * have a PTK/STK we check the key index for a WEP key.
527 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400528 * Note that in a regular BSS, multicast frames are sent by the
529 * AP only, associated stations unicast the frame to the AP first
530 * which then multicasts it on their behalf.
531 *
Johannes Berg3017b802007-08-28 17:01:53 -0400532 * There is also a slight problem in IBSS mode: GTKs are negotiated
533 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400534 * The spec seems to expect that one negotiates the same key with
535 * every station but there's no such requirement; VLANs could be
536 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400537 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200538
Johannes Berg3017b802007-08-28 17:01:53 -0400539 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100540 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400541
542 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200543 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400544 * addressed to us nor a multicast frame.
545 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100546 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100547 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400548
Johannes Bergd4e46a32007-09-14 11:10:24 -0400549 if (rx->sta)
550 stakey = rcu_dereference(rx->sta->key);
551
552 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
553 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200554 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400555 /*
556 * The device doesn't give us the IV so we won't be
557 * able to look up the key. That's ok though, we
558 * don't need to decrypt the frame, we just won't
559 * be able to keep statistics accurate.
560 * Except for key threshold notifications, should
561 * we somehow allow the driver to tell us which key
562 * the hardware used if this flag is set?
563 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100564 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
565 (rx->status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100566 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200567
Johannes Berg3017b802007-08-28 17:01:53 -0400568 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200569
Johannes Berg3017b802007-08-28 17:01:53 -0400570 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100571 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200572
Johannes Berg3017b802007-08-28 17:01:53 -0400573 /*
574 * no need to call ieee80211_wep_get_keyidx,
575 * it verifies a bunch of things we've done already
576 */
577 keyidx = rx->skb->data[hdrlen + 3] >> 6;
578
Johannes Bergd4e46a32007-09-14 11:10:24 -0400579 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400580
581 /*
582 * RSNA-protected unicast frames should always be sent with
583 * pairwise or station-to-station keys, but for WEP we allow
584 * using a key index as well.
585 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400586 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400587 !is_multicast_ether_addr(hdr->addr1))
588 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200589 }
590
Johannes Berg3017b802007-08-28 17:01:53 -0400591 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200592 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400593 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200594 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500595#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200596 if (net_ratelimit())
597 printk(KERN_DEBUG "%s: RX protected frame,"
598 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500599#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100600 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +0200601 }
602
Johannes Berg1990af82007-09-26 17:53:15 +0200603 /* Check for weak IVs if possible */
604 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
605 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100606 (!(rx->status->flag & RX_FLAG_IV_STRIPPED) ||
607 !(rx->status->flag & RX_FLAG_DECRYPTED)) &&
Johannes Berg1990af82007-09-26 17:53:15 +0200608 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
609 rx->sta->wep_weak_iv_count++;
610
Johannes Berg70f08762007-09-26 17:53:14 +0200611 switch (rx->key->conf.alg) {
612 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200613 result = ieee80211_crypto_wep_decrypt(rx);
614 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200615 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200616 result = ieee80211_crypto_tkip_decrypt(rx);
617 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200618 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200619 result = ieee80211_crypto_ccmp_decrypt(rx);
620 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200621 }
622
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200623 /* either the frame has been decrypted or will be dropped */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100624 rx->status->flag |= RX_FLAG_DECRYPTED;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200625
626 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200627}
628
Johannes Berg571ecf62007-07-27 15:43:22 +0200629static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
630{
631 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700632 DECLARE_MAC_BUF(mac);
633
Johannes Berg571ecf62007-07-27 15:43:22 +0200634 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
635
636 if (sdata->bss)
637 atomic_inc(&sdata->bss->num_sta_ps);
638 sta->flags |= WLAN_STA_PS;
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100639 sta->flags &= ~WLAN_STA_PSPOLL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200640#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700641 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
642 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200643#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
644}
645
646static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
647{
648 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
649 struct sk_buff *skb;
650 int sent = 0;
651 struct ieee80211_sub_if_data *sdata;
652 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700653 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200654
655 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
Johannes Berg004c8722008-02-20 11:21:35 +0100656
Johannes Berg571ecf62007-07-27 15:43:22 +0200657 if (sdata->bss)
658 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg004c8722008-02-20 11:21:35 +0100659
Johannes Berg836341a2008-02-20 02:07:21 +0100660 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_PSPOLL);
Johannes Berg004c8722008-02-20 11:21:35 +0100661
662 if (!skb_queue_empty(&sta->ps_tx_buf))
663 sta_info_clear_tim_bit(sta);
664
Johannes Berg571ecf62007-07-27 15:43:22 +0200665#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700666 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
667 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200668#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg004c8722008-02-20 11:21:35 +0100669
Johannes Berg571ecf62007-07-27 15:43:22 +0200670 /* Send all buffered frames to the station */
671 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
672 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
673 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400674 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200675 dev_queue_xmit(skb);
676 }
677 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
678 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
679 local->total_ps_buffered--;
680 sent++;
681#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700682 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200683 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700684 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200685#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400686 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200687 dev_queue_xmit(skb);
688 }
689
690 return sent;
691}
692
Johannes Berg9ae54c82008-01-31 19:48:20 +0100693static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100694ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200695{
696 struct sta_info *sta = rx->sta;
697 struct net_device *dev = rx->dev;
698 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
699
700 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100701 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200702
703 /* Update last_rx only for IBSS packets which are for the current
704 * BSSID to avoid keeping the current IBSS network alive in cases where
705 * other STAs are using different BSSID. */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100706 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200707 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
708 IEEE80211_IF_TYPE_IBSS);
Johannes Berg571ecf62007-07-27 15:43:22 +0200709 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
710 sta->last_rx = jiffies;
711 } else
712 if (!is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100713 rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200714 /* Update last_rx only for unicast frames in order to prevent
715 * the Probe Request frames (the only broadcast frames from a
716 * STA in infrastructure mode) from keeping a connection alive.
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100717 * Mesh beacons will update last_rx when if they are found to
718 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +0200719 */
720 sta->last_rx = jiffies;
721 }
722
Johannes Berg5cf121c2008-02-25 16:27:43 +0100723 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100724 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200725
726 sta->rx_fragments++;
727 sta->rx_bytes += rx->skb->len;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100728 sta->last_rssi = rx->status->ssi;
729 sta->last_signal = rx->status->signal;
730 sta->last_noise = rx->status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200731
732 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
733 /* Change STA power saving mode only in the end of a frame
734 * exchange sequence */
735 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
Johannes Berg5cf121c2008-02-25 16:27:43 +0100736 rx->sent_ps_buffered += ap_sta_ps_end(dev, sta);
Johannes Berg571ecf62007-07-27 15:43:22 +0200737 else if (!(sta->flags & WLAN_STA_PS) &&
738 (rx->fc & IEEE80211_FCTL_PM))
739 ap_sta_ps_start(dev, sta);
740 }
741
742 /* Drop data::nullfunc frames silently, since they are used only to
743 * control station power saving mode. */
744 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
745 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
746 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
747 /* Update counter and free packet here to avoid counting this
748 * as a dropped packed. */
749 sta->rx_packets++;
750 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100751 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200752 }
753
Johannes Berg9ae54c82008-01-31 19:48:20 +0100754 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200755} /* ieee80211_rx_h_sta_process */
756
Johannes Berg571ecf62007-07-27 15:43:22 +0200757static inline struct ieee80211_fragment_entry *
758ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
759 unsigned int frag, unsigned int seq, int rx_queue,
760 struct sk_buff **skb)
761{
762 struct ieee80211_fragment_entry *entry;
763 int idx;
764
765 idx = sdata->fragment_next;
766 entry = &sdata->fragments[sdata->fragment_next++];
767 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
768 sdata->fragment_next = 0;
769
770 if (!skb_queue_empty(&entry->skb_list)) {
771#ifdef CONFIG_MAC80211_DEBUG
772 struct ieee80211_hdr *hdr =
773 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700774 DECLARE_MAC_BUF(mac);
775 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200776 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
777 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700778 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200779 sdata->dev->name, idx,
780 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700781 entry->last_frag, print_mac(mac, hdr->addr1),
782 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200783#endif /* CONFIG_MAC80211_DEBUG */
784 __skb_queue_purge(&entry->skb_list);
785 }
786
787 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
788 *skb = NULL;
789 entry->first_frag_time = jiffies;
790 entry->seq = seq;
791 entry->rx_queue = rx_queue;
792 entry->last_frag = frag;
793 entry->ccmp = 0;
794 entry->extra_len = 0;
795
796 return entry;
797}
798
799static inline struct ieee80211_fragment_entry *
800ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
801 u16 fc, unsigned int frag, unsigned int seq,
802 int rx_queue, struct ieee80211_hdr *hdr)
803{
804 struct ieee80211_fragment_entry *entry;
805 int i, idx;
806
807 idx = sdata->fragment_next;
808 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
809 struct ieee80211_hdr *f_hdr;
810 u16 f_fc;
811
812 idx--;
813 if (idx < 0)
814 idx = IEEE80211_FRAGMENT_MAX - 1;
815
816 entry = &sdata->fragments[idx];
817 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
818 entry->rx_queue != rx_queue ||
819 entry->last_frag + 1 != frag)
820 continue;
821
822 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
823 f_fc = le16_to_cpu(f_hdr->frame_control);
824
825 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
826 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
827 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
828 continue;
829
S.Çağlar Onurab466232008-02-14 17:36:47 +0200830 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200831 __skb_queue_purge(&entry->skb_list);
832 continue;
833 }
834 return entry;
835 }
836
837 return NULL;
838}
839
Johannes Berg9ae54c82008-01-31 19:48:20 +0100840static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100841ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200842{
843 struct ieee80211_hdr *hdr;
844 u16 sc;
845 unsigned int frag, seq;
846 struct ieee80211_fragment_entry *entry;
847 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700848 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200849
850 hdr = (struct ieee80211_hdr *) rx->skb->data;
851 sc = le16_to_cpu(hdr->seq_ctrl);
852 frag = sc & IEEE80211_SCTL_FRAG;
853
854 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
855 (rx->skb)->len < 24 ||
856 is_multicast_ether_addr(hdr->addr1))) {
857 /* not fragmented */
858 goto out;
859 }
860 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
861
862 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
863
864 if (frag == 0) {
865 /* This is the first fragment of a new frame. */
866 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100867 rx->queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400868 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200869 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
870 /* Store CCMP PN so that we can verify that the next
871 * fragment has a sequential PN value. */
872 entry->ccmp = 1;
873 memcpy(entry->last_pn,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100874 rx->key->u.ccmp.rx_pn[rx->queue],
Johannes Berg571ecf62007-07-27 15:43:22 +0200875 CCMP_PN_LEN);
876 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100877 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200878 }
879
880 /* This is a fragment for a frame that should already be pending in
881 * fragment cache. Add this fragment to the end of the pending entry.
882 */
883 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
Johannes Berg5cf121c2008-02-25 16:27:43 +0100884 rx->queue, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +0200885 if (!entry) {
886 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100887 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200888 }
889
890 /* Verify that MPDUs within one MSDU have sequential PN values.
891 * (IEEE 802.11i, 8.3.3.4.5) */
892 if (entry->ccmp) {
893 int i;
894 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400895 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100896 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200897 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
898 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
899 pn[i]++;
900 if (pn[i])
901 break;
902 }
Johannes Berg5cf121c2008-02-25 16:27:43 +0100903 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
Johannes Berg571ecf62007-07-27 15:43:22 +0200904 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400905 if (net_ratelimit())
906 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700907 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400908 " PN=%02x%02x%02x%02x%02x%02x "
909 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700910 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400911 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
912 rpn[5], pn[0], pn[1], pn[2], pn[3],
913 pn[4], pn[5]);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100914 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200915 }
916 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
917 }
918
919 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
920 __skb_queue_tail(&entry->skb_list, rx->skb);
921 entry->last_frag = frag;
922 entry->extra_len += rx->skb->len;
923 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
924 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +0100925 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200926 }
927
928 rx->skb = __skb_dequeue(&entry->skb_list);
929 if (skb_tailroom(rx->skb) < entry->extra_len) {
930 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
931 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
932 GFP_ATOMIC))) {
933 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
934 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100935 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200936 }
937 }
938 while ((skb = __skb_dequeue(&entry->skb_list))) {
939 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
940 dev_kfree_skb(skb);
941 }
942
943 /* Complete frame has been reassembled - process it now */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100944 rx->flags |= IEEE80211_RX_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200945
946 out:
947 if (rx->sta)
948 rx->sta->rx_packets++;
949 if (is_multicast_ether_addr(hdr->addr1))
950 rx->local->dot11MulticastReceivedFrameCount++;
951 else
952 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100953 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200954}
955
Johannes Berg9ae54c82008-01-31 19:48:20 +0100956static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100957ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200958{
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200959 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg571ecf62007-07-27 15:43:22 +0200960 struct sk_buff *skb;
961 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700962 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200963
964 if (likely(!rx->sta ||
965 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
966 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Johannes Berg5cf121c2008-02-25 16:27:43 +0100967 !(rx->flags & IEEE80211_RX_RA_MATCH)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100968 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200969
Johannes Berg51fb61e2007-12-19 01:31:27 +0100970 if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
971 (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100972 return RX_DROP_UNUSABLE;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200973
Johannes Berg571ecf62007-07-27 15:43:22 +0200974 skb = skb_dequeue(&rx->sta->tx_filtered);
975 if (!skb) {
976 skb = skb_dequeue(&rx->sta->ps_tx_buf);
977 if (skb)
978 rx->local->total_ps_buffered--;
979 }
980 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
981 skb_queue_empty(&rx->sta->ps_tx_buf);
982
983 if (skb) {
984 struct ieee80211_hdr *hdr =
985 (struct ieee80211_hdr *) skb->data;
986
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100987 /*
988 * Tell TX path to send one frame even though the STA may
989 * still remain is PS mode after this frame exchange.
990 */
991 rx->sta->flags |= WLAN_STA_PSPOLL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200992
993#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700994 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
995 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200996 skb_queue_len(&rx->sta->ps_tx_buf));
997#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
998
999 /* Use MoreData flag to indicate whether there are more
1000 * buffered frames for this STA */
Johannes Berg836341a2008-02-20 02:07:21 +01001001 if (no_pending_pkts)
Johannes Berg571ecf62007-07-27 15:43:22 +02001002 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg836341a2008-02-20 02:07:21 +01001003 else
Johannes Berg571ecf62007-07-27 15:43:22 +02001004 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1005
1006 dev_queue_xmit(skb);
1007
Johannes Berg004c8722008-02-20 11:21:35 +01001008 if (no_pending_pkts)
1009 sta_info_clear_tim_bit(rx->sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001010#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg5cf121c2008-02-25 16:27:43 +01001011 } else if (!rx->sent_ps_buffered) {
Johannes Berg004c8722008-02-20 11:21:35 +01001012 /*
1013 * FIXME: This can be the result of a race condition between
1014 * us expiring a frame and the station polling for it.
1015 * Should we send it a null-func frame indicating we
1016 * have nothing buffered for it?
1017 */
Joe Perches0795af52007-10-03 17:59:30 -07001018 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +02001019 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -07001020 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +02001021#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg571ecf62007-07-27 15:43:22 +02001022 }
1023
Johannes Berg9ae54c82008-01-31 19:48:20 +01001024 /* Free PS Poll skb here instead of returning RX_DROP that would
Johannes Berg571ecf62007-07-27 15:43:22 +02001025 * count as an dropped frame. */
1026 dev_kfree_skb(rx->skb);
1027
Johannes Berg9ae54c82008-01-31 19:48:20 +01001028 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001029}
1030
Johannes Berg9ae54c82008-01-31 19:48:20 +01001031static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +01001032ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +02001033{
1034 u16 fc = rx->fc;
1035 u8 *data = rx->skb->data;
1036 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
1037
1038 if (!WLAN_FC_IS_QOS_DATA(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001039 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001040
1041 /* remove the qos control field, update frame type and meta-data */
1042 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
1043 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
1044 /* change frame type to non QOS */
1045 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
1046 hdr->frame_control = cpu_to_le16(fc);
1047
Johannes Berg9ae54c82008-01-31 19:48:20 +01001048 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001049}
1050
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001051static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001052ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001053{
Johannes Berg238814f2008-01-28 17:19:37 +01001054 if (unlikely(!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED))) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001055#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg238814f2008-01-28 17:19:37 +01001056 if (net_ratelimit())
1057 printk(KERN_DEBUG "%s: dropped frame "
1058 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001059#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001060 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001061 }
1062
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001063 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001064}
1065
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001066static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001067ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001068{
Johannes Berg3017b802007-08-28 17:01:53 -04001069 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001070 * Pass through unencrypted frames if the hardware has
1071 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001072 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001073 if (rx->status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001074 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001075
1076 /* Drop unencrypted frames if key is set. */
1077 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1078 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1079 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001080 (rx->key || rx->sdata->drop_unencrypted))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001081 if (net_ratelimit())
1082 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1083 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001084 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001085 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001086 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001087}
1088
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001089static int
Johannes Berg5cf121c2008-02-25 16:27:43 +01001090ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001091{
1092 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001093 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1094 u16 fc, hdrlen, ethertype;
1095 u8 *payload;
1096 u8 dst[ETH_ALEN];
1097 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001098 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001099 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001100 DECLARE_MAC_BUF(mac);
1101 DECLARE_MAC_BUF(mac2);
1102 DECLARE_MAC_BUF(mac3);
1103 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001104
1105 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001106
1107 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001108 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001109
1110 hdrlen = ieee80211_get_hdrlen(fc);
1111
Johannes Berg902acc72008-02-23 15:17:19 +01001112 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001113 int meshhdrlen = ieee80211_get_mesh_hdrlen(
1114 (struct ieee80211s_hdr *) (skb->data + hdrlen));
1115 /* Copy on cb:
1116 * - mesh header: to be used for mesh forwarding
1117 * decision. It will also be used as mesh header template at
1118 * tx.c:ieee80211_subif_start_xmit() if interface
1119 * type is mesh and skb->pkt_type == PACKET_OTHERHOST
1120 * - ta: to be used if a RERR needs to be sent.
1121 */
1122 memcpy(skb->cb, skb->data + hdrlen, meshhdrlen);
1123 memcpy(MESH_PREQ(skb), hdr->addr2, ETH_ALEN);
1124 hdrlen += meshhdrlen;
1125 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001126
Johannes Berg571ecf62007-07-27 15:43:22 +02001127 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1128 * header
1129 * IEEE 802.11 address fields:
1130 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1131 * 0 0 DA SA BSSID n/a
1132 * 0 1 DA BSSID SA n/a
1133 * 1 0 BSSID SA DA n/a
1134 * 1 1 RA TA DA SA
1135 */
1136
1137 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1138 case IEEE80211_FCTL_TODS:
1139 /* BSSID SA DA */
1140 memcpy(dst, hdr->addr3, ETH_ALEN);
1141 memcpy(src, hdr->addr2, ETH_ALEN);
1142
Johannes Berg51fb61e2007-12-19 01:31:27 +01001143 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
1144 sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001145 if (net_ratelimit())
1146 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001147 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001148 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001149 print_mac(mac, hdr->addr1),
1150 print_mac(mac2, hdr->addr2),
1151 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001152 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001153 }
1154 break;
1155 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1156 /* RA TA DA SA */
1157 memcpy(dst, hdr->addr3, ETH_ALEN);
1158 memcpy(src, hdr->addr4, ETH_ALEN);
1159
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001160 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS &&
1161 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)) {
1162 if (net_ratelimit())
1163 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001164 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001165 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001166 print_mac(mac, hdr->addr1),
1167 print_mac(mac2, hdr->addr2),
1168 print_mac(mac3, hdr->addr3),
1169 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001170 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001171 }
1172 break;
1173 case IEEE80211_FCTL_FROMDS:
1174 /* DA BSSID SA */
1175 memcpy(dst, hdr->addr1, ETH_ALEN);
1176 memcpy(src, hdr->addr3, ETH_ALEN);
1177
Johannes Berg51fb61e2007-12-19 01:31:27 +01001178 if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
John W. Linvilleb3316152007-08-28 17:01:55 -04001179 (is_multicast_ether_addr(dst) &&
1180 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001181 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001182 break;
1183 case 0:
1184 /* DA SA BSSID */
1185 memcpy(dst, hdr->addr1, ETH_ALEN);
1186 memcpy(src, hdr->addr2, ETH_ALEN);
1187
Johannes Berg51fb61e2007-12-19 01:31:27 +01001188 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001189 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001190 printk(KERN_DEBUG "%s: dropped IBSS frame "
1191 "(DA=%s SA=%s BSSID=%s)\n",
1192 dev->name,
1193 print_mac(mac, hdr->addr1),
1194 print_mac(mac2, hdr->addr2),
1195 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001196 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001197 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001198 }
1199 break;
1200 }
1201
Johannes Berg571ecf62007-07-27 15:43:22 +02001202 if (unlikely(skb->len - hdrlen < 8)) {
1203 if (net_ratelimit()) {
1204 printk(KERN_DEBUG "%s: RX too short data frame "
1205 "payload\n", dev->name);
1206 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001207 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001208 }
1209
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001210 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001211 ethertype = (payload[6] << 8) | payload[7];
1212
1213 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1214 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1215 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1216 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1217 * replace EtherType */
1218 skb_pull(skb, hdrlen + 6);
1219 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1220 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1221 } else {
1222 struct ethhdr *ehdr;
1223 __be16 len;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001224
Johannes Berg571ecf62007-07-27 15:43:22 +02001225 skb_pull(skb, hdrlen);
1226 len = htons(skb->len);
1227 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1228 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1229 memcpy(ehdr->h_source, src, ETH_ALEN);
1230 ehdr->h_proto = len;
1231 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001232 return 0;
1233}
Johannes Berg571ecf62007-07-27 15:43:22 +02001234
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001235/*
1236 * requires that rx->skb is a frame with ethernet header
1237 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001238static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx)
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001239{
1240 static const u8 pae_group_addr[ETH_ALEN]
1241 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1242 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1243
1244 /*
1245 * Allow EAPOL frames to us/the PAE group address regardless
1246 * of whether the frame was encrypted or not.
1247 */
1248 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1249 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1250 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1251 return true;
1252
1253 if (ieee80211_802_1x_port_control(rx) ||
1254 ieee80211_drop_unencrypted(rx))
1255 return false;
1256
1257 return true;
1258}
1259
1260/*
1261 * requires that rx->skb is a frame with ethernet header
1262 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001263static void
Johannes Berg5cf121c2008-02-25 16:27:43 +01001264ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001265{
1266 struct net_device *dev = rx->dev;
1267 struct ieee80211_local *local = rx->local;
1268 struct sk_buff *skb, *xmit_skb;
1269 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001270 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1271 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001272
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001273 skb = rx->skb;
1274 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001275
Johannes Berg51fb61e2007-12-19 01:31:27 +01001276 if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
1277 sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +01001278 (rx->flags & IEEE80211_RX_RA_MATCH)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001279 if (is_multicast_ether_addr(ehdr->h_dest)) {
1280 /*
1281 * send multicast frames both to higher layers in
1282 * local net stack and back to the wireless medium
1283 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001284 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1285 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001286 printk(KERN_DEBUG "%s: failed to clone "
1287 "multicast frame\n", dev->name);
1288 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001289 dsta = sta_info_get(local, skb->data);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001290 if (dsta && dsta->dev == dev) {
1291 /*
1292 * The destination station is associated to
1293 * this AP (in this VLAN), so send the frame
1294 * directly to it and do not pass it to local
1295 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001296 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001297 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001298 skb = NULL;
1299 }
1300 if (dsta)
1301 sta_info_put(dsta);
1302 }
1303 }
1304
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001305 /* Mesh forwarding */
Johannes Berg902acc72008-02-23 15:17:19 +01001306 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001307 u8 *mesh_ttl = &((struct ieee80211s_hdr *)skb->cb)->ttl;
1308 (*mesh_ttl)--;
1309
1310 if (is_multicast_ether_addr(skb->data)) {
1311 if (*mesh_ttl > 0) {
1312 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1313 if (!xmit_skb && net_ratelimit())
1314 printk(KERN_DEBUG "%s: failed to clone "
1315 "multicast frame\n", dev->name);
1316 else
1317 xmit_skb->pkt_type = PACKET_OTHERHOST;
1318 } else
Johannes Berg902acc72008-02-23 15:17:19 +01001319 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.sta,
1320 dropped_frames_ttl);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001321 } else if (skb->pkt_type != PACKET_OTHERHOST &&
1322 compare_ether_addr(dev->dev_addr, skb->data) != 0) {
1323 if (*mesh_ttl == 0) {
Johannes Berg902acc72008-02-23 15:17:19 +01001324 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.sta,
1325 dropped_frames_ttl);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001326 dev_kfree_skb(skb);
1327 skb = NULL;
1328 } else {
1329 xmit_skb = skb;
1330 xmit_skb->pkt_type = PACKET_OTHERHOST;
1331 if (!(dev->flags & IFF_PROMISC))
1332 skb = NULL;
1333 }
1334 }
1335 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001336
Johannes Berg571ecf62007-07-27 15:43:22 +02001337 if (skb) {
1338 /* deliver to local stack */
1339 skb->protocol = eth_type_trans(skb, dev);
1340 memset(skb->cb, 0, sizeof(skb->cb));
1341 netif_rx(skb);
1342 }
1343
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001344 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001345 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001346 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001347 skb_reset_network_header(xmit_skb);
1348 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001349 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001350 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001351}
1352
Johannes Berg9ae54c82008-01-31 19:48:20 +01001353static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +01001354ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001355{
1356 struct net_device *dev = rx->dev;
1357 struct ieee80211_local *local = rx->local;
1358 u16 fc, ethertype;
1359 u8 *payload;
1360 struct sk_buff *skb = rx->skb, *frame = NULL;
1361 const struct ethhdr *eth;
1362 int remaining, err;
1363 u8 dst[ETH_ALEN];
1364 u8 src[ETH_ALEN];
1365 DECLARE_MAC_BUF(mac);
1366
1367 fc = rx->fc;
1368 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001369 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001370
1371 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001372 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001373
Johannes Berg5cf121c2008-02-25 16:27:43 +01001374 if (!(rx->flags & IEEE80211_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001375 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001376
1377 err = ieee80211_data_to_8023(rx);
1378 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001379 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001380
1381 skb->dev = dev;
1382
1383 dev->stats.rx_packets++;
1384 dev->stats.rx_bytes += skb->len;
1385
1386 /* skip the wrapping header */
1387 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1388 if (!eth)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001389 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001390
1391 while (skb != frame) {
1392 u8 padding;
1393 __be16 len = eth->h_proto;
1394 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1395
1396 remaining = skb->len;
1397 memcpy(dst, eth->h_dest, ETH_ALEN);
1398 memcpy(src, eth->h_source, ETH_ALEN);
1399
1400 padding = ((4 - subframe_len) & 0x3);
1401 /* the last MSDU has no padding */
1402 if (subframe_len > remaining) {
1403 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001404 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001405 }
1406
1407 skb_pull(skb, sizeof(struct ethhdr));
1408 /* if last subframe reuse skb */
1409 if (remaining <= subframe_len + padding)
1410 frame = skb;
1411 else {
1412 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1413 subframe_len);
1414
1415 if (frame == NULL)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001416 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001417
1418 skb_reserve(frame, local->hw.extra_tx_headroom +
1419 sizeof(struct ethhdr));
1420 memcpy(skb_put(frame, ntohs(len)), skb->data,
1421 ntohs(len));
1422
1423 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1424 padding);
1425 if (!eth) {
1426 printk(KERN_DEBUG "%s: wrong buffer size ",
1427 dev->name);
1428 dev_kfree_skb(frame);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001429 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001430 }
1431 }
1432
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001433 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001434 frame->dev = dev;
1435 frame->priority = skb->priority;
1436 rx->skb = frame;
1437
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001438 payload = frame->data;
1439 ethertype = (payload[6] << 8) | payload[7];
1440
1441 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001442 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1443 compare_ether_addr(payload,
1444 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001445 /* remove RFC1042 or Bridge-Tunnel
1446 * encapsulation and replace EtherType */
1447 skb_pull(frame, 6);
1448 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1449 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1450 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001451 memcpy(skb_push(frame, sizeof(__be16)),
1452 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001453 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1454 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1455 }
1456
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001457 if (!ieee80211_frame_allowed(rx)) {
1458 if (skb == frame) /* last frame */
Johannes Berge4c26ad2008-01-31 19:48:21 +01001459 return RX_DROP_UNUSABLE;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001460 dev_kfree_skb(frame);
1461 continue;
1462 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001463
1464 ieee80211_deliver_skb(rx);
1465 }
1466
Johannes Berg9ae54c82008-01-31 19:48:20 +01001467 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001468}
1469
Johannes Berg9ae54c82008-01-31 19:48:20 +01001470static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +01001471ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001472{
1473 struct net_device *dev = rx->dev;
1474 u16 fc;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001475 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001476
1477 fc = rx->fc;
1478 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001479 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001480
1481 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001482 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001483
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001484 err = ieee80211_data_to_8023(rx);
1485 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001486 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001487
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001488 if (!ieee80211_frame_allowed(rx))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001489 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001490
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001491 rx->skb->dev = dev;
1492
1493 dev->stats.rx_packets++;
1494 dev->stats.rx_bytes += rx->skb->len;
1495
1496 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001497
Johannes Berg9ae54c82008-01-31 19:48:20 +01001498 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001499}
1500
Johannes Berg9ae54c82008-01-31 19:48:20 +01001501static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +01001502ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
Ron Rindjunsky71364712007-12-25 17:00:36 +02001503{
1504 struct ieee80211_local *local = rx->local;
1505 struct ieee80211_hw *hw = &local->hw;
1506 struct sk_buff *skb = rx->skb;
1507 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
1508 struct tid_ampdu_rx *tid_agg_rx;
1509 u16 start_seq_num;
1510 u16 tid;
1511
1512 if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001513 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001514
1515 if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
1516 if (!rx->sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001517 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001518 tid = le16_to_cpu(bar->control) >> 12;
1519 tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
1520 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001521 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001522
1523 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1524
1525 /* reset session timer */
1526 if (tid_agg_rx->timeout) {
1527 unsigned long expires =
1528 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1529 mod_timer(&tid_agg_rx->session_timer, expires);
1530 }
1531
1532 /* manage reordering buffer according to requested */
1533 /* sequence number */
1534 rcu_read_lock();
1535 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1536 start_seq_num, 1);
1537 rcu_read_unlock();
Johannes Berge4c26ad2008-01-31 19:48:21 +01001538 return RX_DROP_UNUSABLE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001539 }
1540
Johannes Berg9ae54c82008-01-31 19:48:20 +01001541 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001542}
1543
Johannes Berg9ae54c82008-01-31 19:48:20 +01001544static ieee80211_rx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +01001545ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001546{
1547 struct ieee80211_sub_if_data *sdata;
1548
Johannes Berg5cf121c2008-02-25 16:27:43 +01001549 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001550 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001551
1552 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001553 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001554 sdata->vif.type == IEEE80211_IF_TYPE_IBSS ||
1555 sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001556 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg5cf121c2008-02-25 16:27:43 +01001557 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001558 else
Johannes Berge4c26ad2008-01-31 19:48:21 +01001559 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02001560
Johannes Berg9ae54c82008-01-31 19:48:20 +01001561 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001562}
1563
Johannes Berg571ecf62007-07-27 15:43:22 +02001564static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1565 struct ieee80211_hdr *hdr,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001566 struct ieee80211_rx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001567{
1568 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001569 DECLARE_MAC_BUF(mac);
1570 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001571
1572 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1573 if (rx->skb->len >= hdrlen + 4)
1574 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1575 else
1576 keyidx = -1;
1577
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001578 if (net_ratelimit())
1579 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001580 "failure from %s to %s keyidx=%d\n",
1581 dev->name, print_mac(mac, hdr->addr2),
1582 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001583
Johannes Berg8944b792008-01-31 19:48:26 +01001584 if (!rx->sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001585 /*
1586 * Some hardware seem to generate incorrect Michael MIC
1587 * reports; ignore them to avoid triggering countermeasures.
1588 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001589 if (net_ratelimit())
1590 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001591 "error for unknown address %s\n",
1592 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001593 goto ignore;
1594 }
1595
1596 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001597 if (net_ratelimit())
1598 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001599 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001600 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001601 goto ignore;
1602 }
1603
Johannes Berg51fb61e2007-12-19 01:31:27 +01001604 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001605 /*
1606 * APs with pairwise keys should never receive Michael MIC
1607 * errors for non-zero keyidx because these are reserved for
1608 * group keys and only the AP is sending real multicast
1609 * frames in the BSS.
1610 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001611 if (net_ratelimit())
1612 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1613 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001614 " (src %s)\n", dev->name, keyidx,
1615 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001616 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001617 }
1618
1619 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1620 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1621 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001622 if (net_ratelimit())
1623 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1624 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001625 "(fc=0x%04x) (src %s)\n",
1626 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001627 goto ignore;
1628 }
1629
Johannes Bergeb063c12007-08-28 17:01:53 -04001630 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001631 ignore:
1632 dev_kfree_skb(rx->skb);
1633 rx->skb = NULL;
1634}
1635
Johannes Berg5cf121c2008-02-25 16:27:43 +01001636/* TODO: use IEEE80211_RX_FRAGMENTED */
1637static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx)
Michael Wu3d30d942008-01-31 19:48:27 +01001638{
1639 struct ieee80211_sub_if_data *sdata;
1640 struct ieee80211_local *local = rx->local;
1641 struct ieee80211_rtap_hdr {
1642 struct ieee80211_radiotap_header hdr;
1643 u8 flags;
1644 u8 rate;
1645 __le16 chan_freq;
1646 __le16 chan_flags;
1647 } __attribute__ ((packed)) *rthdr;
1648 struct sk_buff *skb = rx->skb, *skb2;
1649 struct net_device *prev_dev = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001650 struct ieee80211_rx_status *status = rx->status;
Michael Wu3d30d942008-01-31 19:48:27 +01001651
Johannes Berg5cf121c2008-02-25 16:27:43 +01001652 if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
Michael Wu3d30d942008-01-31 19:48:27 +01001653 goto out_free_skb;
1654
1655 if (skb_headroom(skb) < sizeof(*rthdr) &&
1656 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1657 goto out_free_skb;
1658
1659 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1660 memset(rthdr, 0, sizeof(*rthdr));
1661 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1662 rthdr->hdr.it_present =
1663 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1664 (1 << IEEE80211_RADIOTAP_RATE) |
1665 (1 << IEEE80211_RADIOTAP_CHANNEL));
1666
Johannes Berg5cf121c2008-02-25 16:27:43 +01001667 rthdr->rate = rx->rate->bitrate / 5;
Michael Wu3d30d942008-01-31 19:48:27 +01001668 rthdr->chan_freq = cpu_to_le16(status->freq);
1669
1670 if (status->band == IEEE80211_BAND_5GHZ)
1671 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1672 IEEE80211_CHAN_5GHZ);
1673 else
1674 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1675 IEEE80211_CHAN_2GHZ);
1676
1677 skb_set_mac_header(skb, 0);
1678 skb->ip_summed = CHECKSUM_UNNECESSARY;
1679 skb->pkt_type = PACKET_OTHERHOST;
1680 skb->protocol = htons(ETH_P_802_2);
1681
1682 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1683 if (!netif_running(sdata->dev))
1684 continue;
1685
1686 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR ||
1687 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1688 continue;
1689
1690 if (prev_dev) {
1691 skb2 = skb_clone(skb, GFP_ATOMIC);
1692 if (skb2) {
1693 skb2->dev = prev_dev;
1694 netif_rx(skb2);
1695 }
1696 }
1697
1698 prev_dev = sdata->dev;
1699 sdata->dev->stats.rx_packets++;
1700 sdata->dev->stats.rx_bytes += skb->len;
1701 }
1702
1703 if (prev_dev) {
1704 skb->dev = prev_dev;
1705 netif_rx(skb);
1706 skb = NULL;
1707 } else
1708 goto out_free_skb;
1709
Johannes Berg5cf121c2008-02-25 16:27:43 +01001710 rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
Michael Wu3d30d942008-01-31 19:48:27 +01001711 return;
1712
1713 out_free_skb:
1714 dev_kfree_skb(skb);
1715}
1716
Johannes Berg5cf121c2008-02-25 16:27:43 +01001717typedef ieee80211_rx_result (*ieee80211_rx_handler)(struct ieee80211_rx_data *);
Johannes Berg58905292008-01-31 19:48:25 +01001718static ieee80211_rx_handler ieee80211_rx_handlers[] =
Johannes Berg571ecf62007-07-27 15:43:22 +02001719{
1720 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001721 ieee80211_rx_h_passive_scan,
1722 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001723 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001724 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001725 ieee80211_rx_h_defragment,
1726 ieee80211_rx_h_ps_poll,
1727 ieee80211_rx_h_michael_mic_verify,
1728 /* this must be after decryption - so header is counted in MPDU mic
1729 * must be before pae and data, so QOS_DATA format frames
1730 * are not passed to user space by these functions
1731 */
1732 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001733 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001734 ieee80211_rx_h_data,
Ron Rindjunsky71364712007-12-25 17:00:36 +02001735 ieee80211_rx_h_ctrl,
Johannes Berg571ecf62007-07-27 15:43:22 +02001736 ieee80211_rx_h_mgmt,
1737 NULL
1738};
1739
Johannes Berg8944b792008-01-31 19:48:26 +01001740static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001741 struct ieee80211_rx_data *rx,
Johannes Berg8944b792008-01-31 19:48:26 +01001742 struct sk_buff *skb)
Johannes Berg58905292008-01-31 19:48:25 +01001743{
1744 ieee80211_rx_handler *handler;
1745 ieee80211_rx_result res = RX_DROP_MONITOR;
1746
Johannes Berg8944b792008-01-31 19:48:26 +01001747 rx->skb = skb;
1748 rx->sdata = sdata;
1749 rx->dev = sdata->dev;
1750
Johannes Berg58905292008-01-31 19:48:25 +01001751 for (handler = ieee80211_rx_handlers; *handler != NULL; handler++) {
1752 res = (*handler)(rx);
1753
1754 switch (res) {
1755 case RX_CONTINUE:
1756 continue;
1757 case RX_DROP_UNUSABLE:
1758 case RX_DROP_MONITOR:
Johannes Berg8944b792008-01-31 19:48:26 +01001759 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1760 if (rx->sta)
1761 rx->sta->rx_dropped++;
Johannes Berg58905292008-01-31 19:48:25 +01001762 break;
1763 case RX_QUEUED:
Johannes Berg8944b792008-01-31 19:48:26 +01001764 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
Johannes Berg58905292008-01-31 19:48:25 +01001765 break;
1766 }
1767 break;
1768 }
1769
1770 switch (res) {
Johannes Berg58905292008-01-31 19:48:25 +01001771 case RX_CONTINUE:
Michael Wu3d30d942008-01-31 19:48:27 +01001772 case RX_DROP_MONITOR:
1773 ieee80211_rx_cooked_monitor(rx);
1774 break;
1775 case RX_DROP_UNUSABLE:
Johannes Berg58905292008-01-31 19:48:25 +01001776 dev_kfree_skb(rx->skb);
1777 break;
1778 }
1779}
1780
Johannes Berg571ecf62007-07-27 15:43:22 +02001781/* main receive path */
1782
Johannes Berg23a24de2007-07-27 15:43:22 +02001783static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001784 u8 *bssid, struct ieee80211_rx_data *rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001785 struct ieee80211_hdr *hdr)
1786{
1787 int multicast = is_multicast_ether_addr(hdr->addr1);
1788
Johannes Berg51fb61e2007-12-19 01:31:27 +01001789 switch (sdata->vif.type) {
Johannes Berg23a24de2007-07-27 15:43:22 +02001790 case IEEE80211_IF_TYPE_STA:
1791 if (!bssid)
1792 return 0;
1793 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001794 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001795 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001796 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001797 } else if (!multicast &&
1798 compare_ether_addr(sdata->dev->dev_addr,
1799 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001800 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001801 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001802 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001803 }
1804 break;
1805 case IEEE80211_IF_TYPE_IBSS:
1806 if (!bssid)
1807 return 0;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001808 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
1809 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
1810 return 1;
1811 else if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001812 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001813 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001814 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001815 } else if (!multicast &&
1816 compare_ether_addr(sdata->dev->dev_addr,
1817 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001818 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001819 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001820 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001821 } else if (!rx->sta)
1822 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1823 bssid, hdr->addr2);
1824 break;
Johannes Berg6032f932008-02-23 15:17:07 +01001825 case IEEE80211_IF_TYPE_MESH_POINT:
1826 if (!multicast &&
1827 compare_ether_addr(sdata->dev->dev_addr,
1828 hdr->addr1) != 0) {
1829 if (!(sdata->dev->flags & IFF_PROMISC))
1830 return 0;
1831
Johannes Berg5cf121c2008-02-25 16:27:43 +01001832 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg6032f932008-02-23 15:17:07 +01001833 }
1834 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001835 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001836 case IEEE80211_IF_TYPE_AP:
1837 if (!bssid) {
1838 if (compare_ether_addr(sdata->dev->dev_addr,
1839 hdr->addr1))
1840 return 0;
1841 } else if (!ieee80211_bssid_match(bssid,
1842 sdata->dev->dev_addr)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001843 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001844 return 0;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001845 rx->flags &= ~IEEE80211_RX_RA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001846 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001847 if (sdata->dev == sdata->local->mdev &&
Johannes Berg5cf121c2008-02-25 16:27:43 +01001848 !(rx->flags & IEEE80211_RX_IN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001849 /* do not receive anything via
1850 * master device when not scanning */
1851 return 0;
1852 break;
1853 case IEEE80211_IF_TYPE_WDS:
1854 if (bssid ||
1855 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1856 return 0;
1857 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1858 return 0;
1859 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001860 case IEEE80211_IF_TYPE_MNTR:
1861 /* take everything */
1862 break;
Johannes Berga2897552007-09-28 14:01:25 +02001863 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001864 /* should never get here */
1865 WARN_ON(1);
1866 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001867 }
1868
1869 return 1;
1870}
1871
Johannes Berg571ecf62007-07-27 15:43:22 +02001872/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001873 * This is the actual Rx frames handler. as it blongs to Rx path it must
1874 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02001875 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001876static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
1877 struct sk_buff *skb,
1878 struct ieee80211_rx_status *status,
Johannes Berg8318d782008-01-24 19:38:38 +01001879 u32 load,
1880 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +02001881{
1882 struct ieee80211_local *local = hw_to_local(hw);
1883 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001884 struct ieee80211_hdr *hdr;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001885 struct ieee80211_rx_data rx;
Johannes Berg571ecf62007-07-27 15:43:22 +02001886 u16 type;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001887 int prepares;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001888 struct ieee80211_sub_if_data *prev = NULL;
1889 struct sk_buff *skb_new;
1890 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001891
1892 hdr = (struct ieee80211_hdr *) skb->data;
1893 memset(&rx, 0, sizeof(rx));
1894 rx.skb = skb;
1895 rx.local = local;
1896
Johannes Berg5cf121c2008-02-25 16:27:43 +01001897 rx.status = status;
1898 rx.load = load;
1899 rx.rate = rate;
Johannes Bergb2e77712007-09-26 15:19:39 +02001900 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001901 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001902
Johannes Bergb2e77712007-09-26 15:19:39 +02001903 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001904 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001905
Johannes Berg8944b792008-01-31 19:48:26 +01001906 rx.sta = sta_info_get(local, hdr->addr2);
1907 if (rx.sta) {
Johannes Bergb2e77712007-09-26 15:19:39 +02001908 rx.dev = rx.sta->dev;
1909 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1910 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001911
Johannes Berg571ecf62007-07-27 15:43:22 +02001912 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
Johannes Berg8944b792008-01-31 19:48:26 +01001913 ieee80211_rx_michael_mic_report(local->mdev, hdr, &rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001914 goto end;
1915 }
1916
Zhu Yiece8edd2007-11-22 10:53:21 +08001917 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Johannes Berg5cf121c2008-02-25 16:27:43 +01001918 rx.flags |= IEEE80211_RX_IN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001919
Johannes Berg38f37142008-01-29 17:07:43 +01001920 ieee80211_parse_qos(&rx);
1921 ieee80211_verify_ip_alignment(&rx);
1922
Johannes Berg571ecf62007-07-27 15:43:22 +02001923 skb = rx.skb;
1924
Johannes Berg79010422007-09-18 17:29:21 -04001925 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001926 if (!netif_running(sdata->dev))
1927 continue;
1928
Johannes Berg51fb61e2007-12-19 01:31:27 +01001929 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +02001930 continue;
1931
Johannes Berg51fb61e2007-12-19 01:31:27 +01001932 bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001933 rx.flags |= IEEE80211_RX_RA_MATCH;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001934 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02001935
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001936 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02001937 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001938
Johannes Berg340e11f2007-07-27 15:43:22 +02001939 /*
1940 * frame is destined for this interface, but if it's not
1941 * also for the previous one we handle that after the
1942 * loop to avoid copying the SKB once too much
1943 */
1944
1945 if (!prev) {
1946 prev = sdata;
1947 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001948 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001949
1950 /*
1951 * frame was destined for the previous interface
1952 * so invoke RX handlers for it
1953 */
1954
1955 skb_new = skb_copy(skb, GFP_ATOMIC);
1956 if (!skb_new) {
1957 if (net_ratelimit())
1958 printk(KERN_DEBUG "%s: failed to copy "
1959 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001960 wiphy_name(local->hw.wiphy),
1961 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001962 continue;
1963 }
Helmut Schaa69f817b2007-12-21 15:16:35 +01001964 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8944b792008-01-31 19:48:26 +01001965 ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001966 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001967 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001968 if (prev) {
Helmut Schaa69f817b2007-12-21 15:16:35 +01001969 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8944b792008-01-31 19:48:26 +01001970 ieee80211_invoke_rx_handlers(prev, &rx, skb);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001971 } else
1972 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001973
Johannes Berg8e6f00322007-07-27 15:43:22 +02001974 end:
Johannes Berg8944b792008-01-31 19:48:26 +01001975 if (rx.sta)
1976 sta_info_put(rx.sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001977}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001978
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001979#define SEQ_MODULO 0x1000
1980#define SEQ_MASK 0xfff
1981
1982static inline int seq_less(u16 sq1, u16 sq2)
1983{
1984 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1985}
1986
1987static inline u16 seq_inc(u16 sq)
1988{
1989 return ((sq + 1) & SEQ_MASK);
1990}
1991
1992static inline u16 seq_sub(u16 sq1, u16 sq2)
1993{
1994 return ((sq1 - sq2) & SEQ_MASK);
1995}
1996
1997
Ron Rindjunsky71364712007-12-25 17:00:36 +02001998/*
1999 * As it function blongs to Rx path it must be called with
2000 * the proper rcu_read_lock protection for its flow.
2001 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002002u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
2003 struct tid_ampdu_rx *tid_agg_rx,
2004 struct sk_buff *skb, u16 mpdu_seq_num,
2005 int bar_req)
2006{
2007 struct ieee80211_local *local = hw_to_local(hw);
2008 struct ieee80211_rx_status status;
2009 u16 head_seq_num, buf_size;
2010 int index;
2011 u32 pkt_load;
Johannes Berg8318d782008-01-24 19:38:38 +01002012 struct ieee80211_supported_band *sband;
2013 struct ieee80211_rate *rate;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002014
2015 buf_size = tid_agg_rx->buf_size;
2016 head_seq_num = tid_agg_rx->head_seq_num;
2017
2018 /* frame with out of date sequence number */
2019 if (seq_less(mpdu_seq_num, head_seq_num)) {
2020 dev_kfree_skb(skb);
2021 return 1;
2022 }
2023
2024 /* if frame sequence number exceeds our buffering window size or
2025 * block Ack Request arrived - release stored frames */
2026 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
2027 /* new head to the ordering buffer */
2028 if (bar_req)
2029 head_seq_num = mpdu_seq_num;
2030 else
2031 head_seq_num =
2032 seq_inc(seq_sub(mpdu_seq_num, buf_size));
2033 /* release stored frames up to new head to stack */
2034 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2035 index = seq_sub(tid_agg_rx->head_seq_num,
2036 tid_agg_rx->ssn)
2037 % tid_agg_rx->buf_size;
2038
2039 if (tid_agg_rx->reorder_buf[index]) {
2040 /* release the reordered frames to stack */
2041 memcpy(&status,
2042 tid_agg_rx->reorder_buf[index]->cb,
2043 sizeof(status));
Johannes Berg8318d782008-01-24 19:38:38 +01002044 sband = local->hw.wiphy->bands[status.band];
2045 rate = &sband->bitrates[status.rate_idx];
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002046 pkt_load = ieee80211_rx_load_stats(local,
2047 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002048 &status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002049 __ieee80211_rx_handle_packet(hw,
2050 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002051 &status, pkt_load, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002052 tid_agg_rx->stored_mpdu_num--;
2053 tid_agg_rx->reorder_buf[index] = NULL;
2054 }
2055 tid_agg_rx->head_seq_num =
2056 seq_inc(tid_agg_rx->head_seq_num);
2057 }
2058 if (bar_req)
2059 return 1;
2060 }
2061
2062 /* now the new frame is always in the range of the reordering */
2063 /* buffer window */
2064 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
2065 % tid_agg_rx->buf_size;
2066 /* check if we already stored this frame */
2067 if (tid_agg_rx->reorder_buf[index]) {
2068 dev_kfree_skb(skb);
2069 return 1;
2070 }
2071
2072 /* if arrived mpdu is in the right order and nothing else stored */
2073 /* release it immediately */
2074 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
2075 tid_agg_rx->stored_mpdu_num == 0) {
2076 tid_agg_rx->head_seq_num =
2077 seq_inc(tid_agg_rx->head_seq_num);
2078 return 0;
2079 }
2080
2081 /* put the frame in the reordering buffer */
2082 tid_agg_rx->reorder_buf[index] = skb;
2083 tid_agg_rx->stored_mpdu_num++;
2084 /* release the buffer until next missing frame */
2085 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
2086 % tid_agg_rx->buf_size;
2087 while (tid_agg_rx->reorder_buf[index]) {
2088 /* release the reordered frame back to stack */
2089 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
2090 sizeof(status));
Johannes Berg8318d782008-01-24 19:38:38 +01002091 sband = local->hw.wiphy->bands[status.band];
2092 rate = &sband->bitrates[status.rate_idx];
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002093 pkt_load = ieee80211_rx_load_stats(local,
2094 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002095 &status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002096 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002097 &status, pkt_load, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002098 tid_agg_rx->stored_mpdu_num--;
2099 tid_agg_rx->reorder_buf[index] = NULL;
2100 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2101 index = seq_sub(tid_agg_rx->head_seq_num,
2102 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2103 }
2104 return 1;
2105}
2106
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002107static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2108 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002109{
2110 struct ieee80211_hw *hw = &local->hw;
2111 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2112 struct sta_info *sta;
2113 struct tid_ampdu_rx *tid_agg_rx;
2114 u16 fc, sc;
2115 u16 mpdu_seq_num;
2116 u8 ret = 0, *qc;
2117 int tid;
2118
2119 sta = sta_info_get(local, hdr->addr2);
2120 if (!sta)
2121 return ret;
2122
2123 fc = le16_to_cpu(hdr->frame_control);
2124
2125 /* filter the QoS data rx stream according to
2126 * STA/TID and check if this STA/TID is on aggregation */
2127 if (!WLAN_FC_IS_QOS_DATA(fc))
2128 goto end_reorder;
2129
2130 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
2131 tid = qc[0] & QOS_CONTROL_TID_MASK;
2132 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
2133
2134 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
2135 goto end_reorder;
2136
2137 /* null data frames are excluded */
Ron Rindjunsky8b6bbe72008-01-23 18:17:13 +02002138 if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002139 goto end_reorder;
2140
2141 /* new un-ordered ampdu frame - process it */
2142
2143 /* reset session timer */
2144 if (tid_agg_rx->timeout) {
2145 unsigned long expires =
2146 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
2147 mod_timer(&tid_agg_rx->session_timer, expires);
2148 }
2149
2150 /* if this mpdu is fragmented - terminate rx aggregation session */
2151 sc = le16_to_cpu(hdr->seq_ctrl);
2152 if (sc & IEEE80211_SCTL_FRAG) {
2153 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
2154 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
2155 ret = 1;
2156 goto end_reorder;
2157 }
2158
2159 /* according to mpdu sequence number deal with reordering buffer */
2160 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
2161 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
2162 mpdu_seq_num, 0);
2163end_reorder:
2164 if (sta)
2165 sta_info_put(sta);
2166 return ret;
2167}
2168
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002169/*
2170 * This is the receive path handler. It is called by a low level driver when an
2171 * 802.11 MPDU is received from the hardware.
2172 */
2173void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
2174 struct ieee80211_rx_status *status)
2175{
2176 struct ieee80211_local *local = hw_to_local(hw);
2177 u32 pkt_load;
Johannes Berg8318d782008-01-24 19:38:38 +01002178 struct ieee80211_rate *rate = NULL;
2179 struct ieee80211_supported_band *sband;
2180
2181 if (status->band < 0 ||
2182 status->band > IEEE80211_NUM_BANDS) {
2183 WARN_ON(1);
2184 return;
2185 }
2186
2187 sband = local->hw.wiphy->bands[status->band];
2188
2189 if (!sband ||
2190 status->rate_idx < 0 ||
2191 status->rate_idx >= sband->n_bitrates) {
2192 WARN_ON(1);
2193 return;
2194 }
2195
2196 rate = &sband->bitrates[status->rate_idx];
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002197
2198 /*
2199 * key references and virtual interfaces are protected using RCU
2200 * and this requires that we are in a read-side RCU section during
2201 * receive processing
2202 */
2203 rcu_read_lock();
2204
2205 /*
2206 * Frames with failed FCS/PLCP checksum are not returned,
2207 * all other frames are returned without radiotap header
2208 * if it was previously present.
2209 * Also, frames with less than 16 bytes are dropped.
2210 */
Johannes Berg8318d782008-01-24 19:38:38 +01002211 skb = ieee80211_rx_monitor(local, skb, status, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002212 if (!skb) {
2213 rcu_read_unlock();
2214 return;
2215 }
2216
Johannes Berg8318d782008-01-24 19:38:38 +01002217 pkt_load = ieee80211_rx_load_stats(local, skb, status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002218 local->channel_use_raw += pkt_load;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002219
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002220 if (!ieee80211_rx_reorder_ampdu(local, skb))
Johannes Berg8318d782008-01-24 19:38:38 +01002221 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002222
2223 rcu_read_unlock();
2224}
Johannes Berg571ecf62007-07-27 15:43:22 +02002225EXPORT_SYMBOL(__ieee80211_rx);
2226
2227/* This is a version of the rx handler that can be called from hard irq
2228 * context. Post the skb on the queue and schedule the tasklet */
2229void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
2230 struct ieee80211_rx_status *status)
2231{
2232 struct ieee80211_local *local = hw_to_local(hw);
2233
2234 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2235
2236 skb->dev = local->mdev;
2237 /* copy status into skb->cb for use by tasklet */
2238 memcpy(skb->cb, status, sizeof(*status));
2239 skb->pkt_type = IEEE80211_RX_MSG;
2240 skb_queue_tail(&local->skb_queue, skb);
2241 tasklet_schedule(&local->tasklet);
2242}
2243EXPORT_SYMBOL(ieee80211_rx_irqsafe);