blob: d0018fc40b095c29ae590e5eadc3b631f98dee95 [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 Berg38f37142008-01-29 17:07:43 +0100254static void ieee80211_parse_qos(struct ieee80211_txrx_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)
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200265 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200266 else
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200267 rx->flags &= ~IEEE80211_TXRXD_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 Berg52865df2007-07-27 15:43:22 +0200277
Johannes Berg6e0d1142007-07-27 15:43:22 +0200278 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865df2007-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
283 rx->u.rx.queue = tid;
284 /* 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 Berg38f37142008-01-29 17:07:43 +0100289static void ieee80211_verify_ip_alignment(struct ieee80211_txrx_data *rx)
290{
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);
317 if (rx->flags & IEEE80211_TXRXD_RX_AMSDU)
318 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 Berg571ecf62007-07-27 15:43:22 +0200360ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
361{
Johannes Berg52865df2007-07-27 15:43:22 +0200362 if (rx->sta)
363 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200364 rx->sdata->channel_use_raw += rx->u.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 Berg571ecf62007-07-27 15:43:22 +0200369ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
370{
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))
375 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
376
377 if (unlikely(local->sta_sw_scanning)) {
378 /* drop all the other packets during a software scan anyway */
379 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.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
Jiri Slabybadffb72007-08-28 17:01:54 -0400385 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_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 +0100394#ifdef CONFIG_MAC80211_MESH
395#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
396static ieee80211_rx_result
397ieee80211_rx_mesh_check(struct ieee80211_txrx_data *rx)
398{
399 int hdrlen = ieee80211_get_hdrlen(rx->fc);
400 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
401 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
402 if (!((rx->fc & IEEE80211_FCTL_FROMDS) &&
403 (rx->fc & IEEE80211_FCTL_TODS)))
404 return RX_DROP_MONITOR;
405 if (memcmp(hdr->addr4, rx->dev->dev_addr, ETH_ALEN) == 0)
406 return RX_DROP_MONITOR;
407 }
408
409 /* If there is not an established peer link and this is not a peer link
410 * establisment frame, beacon or probe, drop the frame.
411 */
412
413 if (!rx->sta || rx->sta->plink_state != ESTAB) {
414 struct ieee80211_mgmt *mgmt;
415 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
416 return RX_DROP_MONITOR;
417
418 switch (rx->fc & IEEE80211_FCTL_STYPE) {
419 case IEEE80211_STYPE_ACTION:
420 mgmt = (struct ieee80211_mgmt *)hdr;
421 if (mgmt->u.action.category != PLINK_CATEGORY)
422 return RX_DROP_MONITOR;
423 /* fall through on else */
424 case IEEE80211_STYPE_PROBE_REQ:
425 case IEEE80211_STYPE_PROBE_RESP:
426 case IEEE80211_STYPE_BEACON:
427 return RX_CONTINUE;
428 break;
429 default:
430 return RX_DROP_MONITOR;
431 }
432
433 } else if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
434 is_broadcast_ether_addr(hdr->addr1) &&
435 mesh_rmc_check(hdr->addr4, msh_h_get(hdr, hdrlen), rx->dev))
436 return RX_DROP_MONITOR;
437 else
438 return RX_CONTINUE;
439}
Johannes Berg902acc72008-02-23 15:17:19 +0100440#undef msh_h_get
441#else
442static inline ieee80211_rx_result
443ieee80211_rx_mesh_check(struct ieee80211_txrx_data *rx)
444{
445 return RX_CONTINUE;
446}
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100447#endif
448
449
Johannes Berg9ae54c82008-01-31 19:48:20 +0100450static ieee80211_rx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200451ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
452{
453 struct ieee80211_hdr *hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100454
Johannes Berg571ecf62007-07-27 15:43:22 +0200455 hdr = (struct ieee80211_hdr *) rx->skb->data;
456
457 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
458 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
459 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
460 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
461 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400462 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200463 rx->local->dot11FrameDuplicateCount++;
464 rx->sta->num_duplicates++;
465 }
Johannes Berge4c26ad2008-01-31 19:48:21 +0100466 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200467 } else
468 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
469 }
470
Johannes Berg571ecf62007-07-27 15:43:22 +0200471 if (unlikely(rx->skb->len < 16)) {
472 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100473 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200474 }
475
Johannes Berg571ecf62007-07-27 15:43:22 +0200476 /* Drop disallowed frame classes based on STA auth/assoc state;
477 * IEEE 802.11, Chap 5.5.
478 *
479 * 80211.o does filtering only based on association state, i.e., it
480 * drops Class 3 frames from not associated stations. hostapd sends
481 * deauth/disassoc frames when needed. In addition, hostapd is
482 * responsible for filtering on both auth and assoc states.
483 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100484
Johannes Berg902acc72008-02-23 15:17:19 +0100485 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100486 return ieee80211_rx_mesh_check(rx);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100487
Johannes Berg571ecf62007-07-27 15:43:22 +0200488 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
489 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
490 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100491 rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200492 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
493 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
494 !(rx->fc & IEEE80211_FCTL_TODS) &&
495 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400496 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200497 /* Drop IBSS frames and frames for other hosts
498 * silently. */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100499 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200500 }
501
Johannes Berge4c26ad2008-01-31 19:48:21 +0100502 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200503 }
504
Johannes Berg9ae54c82008-01-31 19:48:20 +0100505 return RX_CONTINUE;
Johannes Berg570bd532007-07-27 15:43:22 +0200506}
507
508
Johannes Berg9ae54c82008-01-31 19:48:20 +0100509static ieee80211_rx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200510ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200511{
512 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400513 int keyidx;
514 int hdrlen;
Johannes Berge4c26ad2008-01-31 19:48:21 +0100515 ieee80211_rx_result result = RX_DROP_UNUSABLE;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400516 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200517
Johannes Berg3017b802007-08-28 17:01:53 -0400518 /*
519 * Key selection 101
520 *
521 * There are three types of keys:
522 * - GTK (group keys)
523 * - PTK (pairwise keys)
524 * - STK (station-to-station pairwise keys)
525 *
526 * When selecting a key, we have to distinguish between multicast
527 * (including broadcast) and unicast frames, the latter can only
528 * use PTKs and STKs while the former always use GTKs. Unless, of
529 * course, actual WEP keys ("pre-RSNA") are used, then unicast
530 * frames can also use key indizes like GTKs. Hence, if we don't
531 * have a PTK/STK we check the key index for a WEP key.
532 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400533 * Note that in a regular BSS, multicast frames are sent by the
534 * AP only, associated stations unicast the frame to the AP first
535 * which then multicasts it on their behalf.
536 *
Johannes Berg3017b802007-08-28 17:01:53 -0400537 * There is also a slight problem in IBSS mode: GTKs are negotiated
538 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400539 * The spec seems to expect that one negotiates the same key with
540 * every station but there's no such requirement; VLANs could be
541 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400542 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200543
Johannes Berg3017b802007-08-28 17:01:53 -0400544 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100545 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400546
547 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200548 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400549 * addressed to us nor a multicast frame.
550 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400551 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100552 return RX_CONTINUE;
Johannes Berg3017b802007-08-28 17:01:53 -0400553
Johannes Bergd4e46a32007-09-14 11:10:24 -0400554 if (rx->sta)
555 stakey = rcu_dereference(rx->sta->key);
556
557 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
558 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200559 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400560 /*
561 * The device doesn't give us the IV so we won't be
562 * able to look up the key. That's ok though, we
563 * don't need to decrypt the frame, we just won't
564 * be able to keep statistics accurate.
565 * Except for key threshold notifications, should
566 * we somehow allow the driver to tell us which key
567 * the hardware used if this flag is set?
568 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400569 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
570 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100571 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200572
Johannes Berg3017b802007-08-28 17:01:53 -0400573 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200574
Johannes Berg3017b802007-08-28 17:01:53 -0400575 if (rx->skb->len < 8 + hdrlen)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100576 return RX_DROP_UNUSABLE; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200577
Johannes Berg3017b802007-08-28 17:01:53 -0400578 /*
579 * no need to call ieee80211_wep_get_keyidx,
580 * it verifies a bunch of things we've done already
581 */
582 keyidx = rx->skb->data[hdrlen + 3] >> 6;
583
Johannes Bergd4e46a32007-09-14 11:10:24 -0400584 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400585
586 /*
587 * RSNA-protected unicast frames should always be sent with
588 * pairwise or station-to-station keys, but for WEP we allow
589 * using a key index as well.
590 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400591 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400592 !is_multicast_ether_addr(hdr->addr1))
593 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200594 }
595
Johannes Berg3017b802007-08-28 17:01:53 -0400596 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200597 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400598 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200599 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500600#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200601 if (net_ratelimit())
602 printk(KERN_DEBUG "%s: RX protected frame,"
603 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500604#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berge4c26ad2008-01-31 19:48:21 +0100605 return RX_DROP_MONITOR;
Johannes Berg70f08762007-09-26 17:53:14 +0200606 }
607
Johannes Berg1990af82007-09-26 17:53:15 +0200608 /* Check for weak IVs if possible */
609 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
610 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
611 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
612 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
613 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
614 rx->sta->wep_weak_iv_count++;
615
Johannes Berg70f08762007-09-26 17:53:14 +0200616 switch (rx->key->conf.alg) {
617 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200618 result = ieee80211_crypto_wep_decrypt(rx);
619 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200620 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200621 result = ieee80211_crypto_tkip_decrypt(rx);
622 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200623 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200624 result = ieee80211_crypto_ccmp_decrypt(rx);
625 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200626 }
627
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200628 /* either the frame has been decrypted or will be dropped */
629 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
630
631 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200632}
633
Johannes Berg571ecf62007-07-27 15:43:22 +0200634static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
635{
636 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700637 DECLARE_MAC_BUF(mac);
638
Johannes Berg571ecf62007-07-27 15:43:22 +0200639 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
640
641 if (sdata->bss)
642 atomic_inc(&sdata->bss->num_sta_ps);
643 sta->flags |= WLAN_STA_PS;
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100644 sta->flags &= ~WLAN_STA_PSPOLL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200645#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700646 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
647 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200648#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
649}
650
651static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
652{
653 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
654 struct sk_buff *skb;
655 int sent = 0;
656 struct ieee80211_sub_if_data *sdata;
657 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700658 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200659
660 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
Johannes Berg004c8722008-02-20 11:21:35 +0100661
Johannes Berg571ecf62007-07-27 15:43:22 +0200662 if (sdata->bss)
663 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg004c8722008-02-20 11:21:35 +0100664
Johannes Berg836341a2008-02-20 02:07:21 +0100665 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_PSPOLL);
Johannes Berg004c8722008-02-20 11:21:35 +0100666
667 if (!skb_queue_empty(&sta->ps_tx_buf))
668 sta_info_clear_tim_bit(sta);
669
Johannes Berg571ecf62007-07-27 15:43:22 +0200670#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700671 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
672 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200673#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg004c8722008-02-20 11:21:35 +0100674
Johannes Berg571ecf62007-07-27 15:43:22 +0200675 /* Send all buffered frames to the station */
676 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
677 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
678 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400679 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200680 dev_queue_xmit(skb);
681 }
682 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
683 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
684 local->total_ps_buffered--;
685 sent++;
686#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700687 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200688 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700689 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200690#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400691 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200692 dev_queue_xmit(skb);
693 }
694
695 return sent;
696}
697
Johannes Berg9ae54c82008-01-31 19:48:20 +0100698static ieee80211_rx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200699ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
700{
701 struct sta_info *sta = rx->sta;
702 struct net_device *dev = rx->dev;
703 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
704
705 if (!sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100706 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200707
708 /* Update last_rx only for IBSS packets which are for the current
709 * BSSID to avoid keeping the current IBSS network alive in cases where
710 * other STAs are using different BSSID. */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100711 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200712 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
713 IEEE80211_IF_TYPE_IBSS);
Johannes Berg571ecf62007-07-27 15:43:22 +0200714 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
715 sta->last_rx = jiffies;
716 } else
717 if (!is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100718 rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200719 /* Update last_rx only for unicast frames in order to prevent
720 * the Probe Request frames (the only broadcast frames from a
721 * STA in infrastructure mode) from keeping a connection alive.
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100722 * Mesh beacons will update last_rx when if they are found to
723 * match the current local configuration when processed.
Johannes Berg571ecf62007-07-27 15:43:22 +0200724 */
725 sta->last_rx = jiffies;
726 }
727
Jiri Slabybadffb72007-08-28 17:01:54 -0400728 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100729 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200730
731 sta->rx_fragments++;
732 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400733 sta->last_rssi = rx->u.rx.status->ssi;
734 sta->last_signal = rx->u.rx.status->signal;
735 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200736
737 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
738 /* Change STA power saving mode only in the end of a frame
739 * exchange sequence */
740 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
741 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
742 else if (!(sta->flags & WLAN_STA_PS) &&
743 (rx->fc & IEEE80211_FCTL_PM))
744 ap_sta_ps_start(dev, sta);
745 }
746
747 /* Drop data::nullfunc frames silently, since they are used only to
748 * control station power saving mode. */
749 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
750 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
751 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
752 /* Update counter and free packet here to avoid counting this
753 * as a dropped packed. */
754 sta->rx_packets++;
755 dev_kfree_skb(rx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100756 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200757 }
758
Johannes Berg9ae54c82008-01-31 19:48:20 +0100759 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200760} /* ieee80211_rx_h_sta_process */
761
Johannes Berg571ecf62007-07-27 15:43:22 +0200762static inline struct ieee80211_fragment_entry *
763ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
764 unsigned int frag, unsigned int seq, int rx_queue,
765 struct sk_buff **skb)
766{
767 struct ieee80211_fragment_entry *entry;
768 int idx;
769
770 idx = sdata->fragment_next;
771 entry = &sdata->fragments[sdata->fragment_next++];
772 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
773 sdata->fragment_next = 0;
774
775 if (!skb_queue_empty(&entry->skb_list)) {
776#ifdef CONFIG_MAC80211_DEBUG
777 struct ieee80211_hdr *hdr =
778 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700779 DECLARE_MAC_BUF(mac);
780 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200781 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
782 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700783 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200784 sdata->dev->name, idx,
785 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700786 entry->last_frag, print_mac(mac, hdr->addr1),
787 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200788#endif /* CONFIG_MAC80211_DEBUG */
789 __skb_queue_purge(&entry->skb_list);
790 }
791
792 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
793 *skb = NULL;
794 entry->first_frag_time = jiffies;
795 entry->seq = seq;
796 entry->rx_queue = rx_queue;
797 entry->last_frag = frag;
798 entry->ccmp = 0;
799 entry->extra_len = 0;
800
801 return entry;
802}
803
804static inline struct ieee80211_fragment_entry *
805ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
806 u16 fc, unsigned int frag, unsigned int seq,
807 int rx_queue, struct ieee80211_hdr *hdr)
808{
809 struct ieee80211_fragment_entry *entry;
810 int i, idx;
811
812 idx = sdata->fragment_next;
813 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
814 struct ieee80211_hdr *f_hdr;
815 u16 f_fc;
816
817 idx--;
818 if (idx < 0)
819 idx = IEEE80211_FRAGMENT_MAX - 1;
820
821 entry = &sdata->fragments[idx];
822 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
823 entry->rx_queue != rx_queue ||
824 entry->last_frag + 1 != frag)
825 continue;
826
827 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
828 f_fc = le16_to_cpu(f_hdr->frame_control);
829
830 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
831 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
832 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
833 continue;
834
S.Çağlar Onurab466232008-02-14 17:36:47 +0200835 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200836 __skb_queue_purge(&entry->skb_list);
837 continue;
838 }
839 return entry;
840 }
841
842 return NULL;
843}
844
Johannes Berg9ae54c82008-01-31 19:48:20 +0100845static ieee80211_rx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200846ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
847{
848 struct ieee80211_hdr *hdr;
849 u16 sc;
850 unsigned int frag, seq;
851 struct ieee80211_fragment_entry *entry;
852 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700853 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200854
855 hdr = (struct ieee80211_hdr *) rx->skb->data;
856 sc = le16_to_cpu(hdr->seq_ctrl);
857 frag = sc & IEEE80211_SCTL_FRAG;
858
859 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
860 (rx->skb)->len < 24 ||
861 is_multicast_ether_addr(hdr->addr1))) {
862 /* not fragmented */
863 goto out;
864 }
865 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
866
867 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
868
869 if (frag == 0) {
870 /* This is the first fragment of a new frame. */
871 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
872 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400873 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200874 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
875 /* Store CCMP PN so that we can verify that the next
876 * fragment has a sequential PN value. */
877 entry->ccmp = 1;
878 memcpy(entry->last_pn,
879 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
880 CCMP_PN_LEN);
881 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100882 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200883 }
884
885 /* This is a fragment for a frame that should already be pending in
886 * fragment cache. Add this fragment to the end of the pending entry.
887 */
888 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
889 rx->u.rx.queue, hdr);
890 if (!entry) {
891 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100892 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +0200893 }
894
895 /* Verify that MPDUs within one MSDU have sequential PN values.
896 * (IEEE 802.11i, 8.3.3.4.5) */
897 if (entry->ccmp) {
898 int i;
899 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400900 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berge4c26ad2008-01-31 19:48:21 +0100901 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200902 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
903 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
904 pn[i]++;
905 if (pn[i])
906 break;
907 }
908 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
909 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400910 if (net_ratelimit())
911 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700912 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400913 " PN=%02x%02x%02x%02x%02x%02x "
914 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700915 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400916 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
917 rpn[5], pn[0], pn[1], pn[2], pn[3],
918 pn[4], pn[5]);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100919 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200920 }
921 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
922 }
923
924 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
925 __skb_queue_tail(&entry->skb_list, rx->skb);
926 entry->last_frag = frag;
927 entry->extra_len += rx->skb->len;
928 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
929 rx->skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +0100930 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200931 }
932
933 rx->skb = __skb_dequeue(&entry->skb_list);
934 if (skb_tailroom(rx->skb) < entry->extra_len) {
935 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
936 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
937 GFP_ATOMIC))) {
938 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
939 __skb_queue_purge(&entry->skb_list);
Johannes Berge4c26ad2008-01-31 19:48:21 +0100940 return RX_DROP_UNUSABLE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200941 }
942 }
943 while ((skb = __skb_dequeue(&entry->skb_list))) {
944 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
945 dev_kfree_skb(skb);
946 }
947
948 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400949 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200950
951 out:
952 if (rx->sta)
953 rx->sta->rx_packets++;
954 if (is_multicast_ether_addr(hdr->addr1))
955 rx->local->dot11MulticastReceivedFrameCount++;
956 else
957 ieee80211_led_rx(rx->local);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100958 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200959}
960
Johannes Berg9ae54c82008-01-31 19:48:20 +0100961static ieee80211_rx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200962ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
963{
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200964 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg571ecf62007-07-27 15:43:22 +0200965 struct sk_buff *skb;
966 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700967 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200968
969 if (likely(!rx->sta ||
970 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
971 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400972 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100973 return RX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200974
Johannes Berg51fb61e2007-12-19 01:31:27 +0100975 if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
976 (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
Johannes Berge4c26ad2008-01-31 19:48:21 +0100977 return RX_DROP_UNUSABLE;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200978
Johannes Berg571ecf62007-07-27 15:43:22 +0200979 skb = skb_dequeue(&rx->sta->tx_filtered);
980 if (!skb) {
981 skb = skb_dequeue(&rx->sta->ps_tx_buf);
982 if (skb)
983 rx->local->total_ps_buffered--;
984 }
985 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
986 skb_queue_empty(&rx->sta->ps_tx_buf);
987
988 if (skb) {
989 struct ieee80211_hdr *hdr =
990 (struct ieee80211_hdr *) skb->data;
991
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100992 /*
993 * Tell TX path to send one frame even though the STA may
994 * still remain is PS mode after this frame exchange.
995 */
996 rx->sta->flags |= WLAN_STA_PSPOLL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200997
998#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700999 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
1000 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +02001001 skb_queue_len(&rx->sta->ps_tx_buf));
1002#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1003
1004 /* Use MoreData flag to indicate whether there are more
1005 * buffered frames for this STA */
Johannes Berg836341a2008-02-20 02:07:21 +01001006 if (no_pending_pkts)
Johannes Berg571ecf62007-07-27 15:43:22 +02001007 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg836341a2008-02-20 02:07:21 +01001008 else
Johannes Berg571ecf62007-07-27 15:43:22 +02001009 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1010
1011 dev_queue_xmit(skb);
1012
Johannes Berg004c8722008-02-20 11:21:35 +01001013 if (no_pending_pkts)
1014 sta_info_clear_tim_bit(rx->sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001015#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1016 } else if (!rx->u.rx.sent_ps_buffered) {
Johannes Berg004c8722008-02-20 11:21:35 +01001017 /*
1018 * FIXME: This can be the result of a race condition between
1019 * us expiring a frame and the station polling for it.
1020 * Should we send it a null-func frame indicating we
1021 * have nothing buffered for it?
1022 */
Joe Perches0795af52007-10-03 17:59:30 -07001023 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +02001024 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -07001025 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +02001026#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg571ecf62007-07-27 15:43:22 +02001027 }
1028
Johannes Berg9ae54c82008-01-31 19:48:20 +01001029 /* Free PS Poll skb here instead of returning RX_DROP that would
Johannes Berg571ecf62007-07-27 15:43:22 +02001030 * count as an dropped frame. */
1031 dev_kfree_skb(rx->skb);
1032
Johannes Berg9ae54c82008-01-31 19:48:20 +01001033 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001034}
1035
Johannes Berg9ae54c82008-01-31 19:48:20 +01001036static ieee80211_rx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +02001037ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
1038{
1039 u16 fc = rx->fc;
1040 u8 *data = rx->skb->data;
1041 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
1042
1043 if (!WLAN_FC_IS_QOS_DATA(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001044 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001045
1046 /* remove the qos control field, update frame type and meta-data */
1047 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
1048 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
1049 /* change frame type to non QOS */
1050 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
1051 hdr->frame_control = cpu_to_le16(fc);
1052
Johannes Berg9ae54c82008-01-31 19:48:20 +01001053 return RX_CONTINUE;
Johannes Berg6e0d1142007-07-27 15:43:22 +02001054}
1055
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001056static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001057ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001058{
Johannes Berg238814f2008-01-28 17:19:37 +01001059 if (unlikely(!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED))) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001060#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg238814f2008-01-28 17:19:37 +01001061 if (net_ratelimit())
1062 printk(KERN_DEBUG "%s: dropped frame "
1063 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001064#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001065 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001066 }
1067
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001068 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001069}
1070
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001071static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001072ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001073{
Johannes Berg3017b802007-08-28 17:01:53 -04001074 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001075 * Pass through unencrypted frames if the hardware has
1076 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001077 */
Johannes Berg7848ba72007-09-14 11:10:25 -04001078 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001079 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001080
1081 /* Drop unencrypted frames if key is set. */
1082 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1083 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1084 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001085 (rx->key || rx->sdata->drop_unencrypted))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001086 if (net_ratelimit())
1087 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1088 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001089 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001090 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001091 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001092}
1093
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001094static int
1095ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001096{
1097 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001098 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1099 u16 fc, hdrlen, ethertype;
1100 u8 *payload;
1101 u8 dst[ETH_ALEN];
1102 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001103 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001104 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001105 DECLARE_MAC_BUF(mac);
1106 DECLARE_MAC_BUF(mac2);
1107 DECLARE_MAC_BUF(mac3);
1108 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001109
1110 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001111
1112 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001113 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001114
1115 hdrlen = ieee80211_get_hdrlen(fc);
1116
Johannes Berg902acc72008-02-23 15:17:19 +01001117 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001118 int meshhdrlen = ieee80211_get_mesh_hdrlen(
1119 (struct ieee80211s_hdr *) (skb->data + hdrlen));
1120 /* Copy on cb:
1121 * - mesh header: to be used for mesh forwarding
1122 * decision. It will also be used as mesh header template at
1123 * tx.c:ieee80211_subif_start_xmit() if interface
1124 * type is mesh and skb->pkt_type == PACKET_OTHERHOST
1125 * - ta: to be used if a RERR needs to be sent.
1126 */
1127 memcpy(skb->cb, skb->data + hdrlen, meshhdrlen);
1128 memcpy(MESH_PREQ(skb), hdr->addr2, ETH_ALEN);
1129 hdrlen += meshhdrlen;
1130 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001131
Johannes Berg571ecf62007-07-27 15:43:22 +02001132 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1133 * header
1134 * IEEE 802.11 address fields:
1135 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1136 * 0 0 DA SA BSSID n/a
1137 * 0 1 DA BSSID SA n/a
1138 * 1 0 BSSID SA DA n/a
1139 * 1 1 RA TA DA SA
1140 */
1141
1142 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1143 case IEEE80211_FCTL_TODS:
1144 /* BSSID SA DA */
1145 memcpy(dst, hdr->addr3, ETH_ALEN);
1146 memcpy(src, hdr->addr2, ETH_ALEN);
1147
Johannes Berg51fb61e2007-12-19 01:31:27 +01001148 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
1149 sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001150 if (net_ratelimit())
1151 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001152 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001153 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001154 print_mac(mac, hdr->addr1),
1155 print_mac(mac2, hdr->addr2),
1156 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001157 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001158 }
1159 break;
1160 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1161 /* RA TA DA SA */
1162 memcpy(dst, hdr->addr3, ETH_ALEN);
1163 memcpy(src, hdr->addr4, ETH_ALEN);
1164
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001165 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS &&
1166 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)) {
1167 if (net_ratelimit())
1168 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001169 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001170 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001171 print_mac(mac, hdr->addr1),
1172 print_mac(mac2, hdr->addr2),
1173 print_mac(mac3, hdr->addr3),
1174 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001175 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001176 }
1177 break;
1178 case IEEE80211_FCTL_FROMDS:
1179 /* DA BSSID SA */
1180 memcpy(dst, hdr->addr1, ETH_ALEN);
1181 memcpy(src, hdr->addr3, ETH_ALEN);
1182
Johannes Berg51fb61e2007-12-19 01:31:27 +01001183 if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
John W. Linvilleb3316152007-08-28 17:01:55 -04001184 (is_multicast_ether_addr(dst) &&
1185 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001186 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001187 break;
1188 case 0:
1189 /* DA SA BSSID */
1190 memcpy(dst, hdr->addr1, ETH_ALEN);
1191 memcpy(src, hdr->addr2, ETH_ALEN);
1192
Johannes Berg51fb61e2007-12-19 01:31:27 +01001193 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001194 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001195 printk(KERN_DEBUG "%s: dropped IBSS frame "
1196 "(DA=%s SA=%s BSSID=%s)\n",
1197 dev->name,
1198 print_mac(mac, hdr->addr1),
1199 print_mac(mac2, hdr->addr2),
1200 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001201 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001202 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001203 }
1204 break;
1205 }
1206
Johannes Berg571ecf62007-07-27 15:43:22 +02001207 if (unlikely(skb->len - hdrlen < 8)) {
1208 if (net_ratelimit()) {
1209 printk(KERN_DEBUG "%s: RX too short data frame "
1210 "payload\n", dev->name);
1211 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001212 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001213 }
1214
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001215 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001216 ethertype = (payload[6] << 8) | payload[7];
1217
1218 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1219 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1220 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1221 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1222 * replace EtherType */
1223 skb_pull(skb, hdrlen + 6);
1224 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1225 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1226 } else {
1227 struct ethhdr *ehdr;
1228 __be16 len;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001229
Johannes Berg571ecf62007-07-27 15:43:22 +02001230 skb_pull(skb, hdrlen);
1231 len = htons(skb->len);
1232 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1233 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1234 memcpy(ehdr->h_source, src, ETH_ALEN);
1235 ehdr->h_proto = len;
1236 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001237 return 0;
1238}
Johannes Berg571ecf62007-07-27 15:43:22 +02001239
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001240/*
1241 * requires that rx->skb is a frame with ethernet header
1242 */
1243static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1244{
1245 static const u8 pae_group_addr[ETH_ALEN]
1246 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1247 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1248
1249 /*
1250 * Allow EAPOL frames to us/the PAE group address regardless
1251 * of whether the frame was encrypted or not.
1252 */
1253 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1254 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1255 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1256 return true;
1257
1258 if (ieee80211_802_1x_port_control(rx) ||
1259 ieee80211_drop_unencrypted(rx))
1260 return false;
1261
1262 return true;
1263}
1264
1265/*
1266 * requires that rx->skb is a frame with ethernet header
1267 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001268static void
1269ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1270{
1271 struct net_device *dev = rx->dev;
1272 struct ieee80211_local *local = rx->local;
1273 struct sk_buff *skb, *xmit_skb;
1274 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001275 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1276 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001277
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001278 skb = rx->skb;
1279 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001280
Johannes Berg51fb61e2007-12-19 01:31:27 +01001281 if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
1282 sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
Jiri Slabybadffb72007-08-28 17:01:54 -04001283 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001284 if (is_multicast_ether_addr(ehdr->h_dest)) {
1285 /*
1286 * send multicast frames both to higher layers in
1287 * local net stack and back to the wireless medium
1288 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001289 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1290 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001291 printk(KERN_DEBUG "%s: failed to clone "
1292 "multicast frame\n", dev->name);
1293 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001294 dsta = sta_info_get(local, skb->data);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001295 if (dsta && dsta->dev == dev) {
1296 /*
1297 * The destination station is associated to
1298 * this AP (in this VLAN), so send the frame
1299 * directly to it and do not pass it to local
1300 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001301 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001302 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001303 skb = NULL;
1304 }
1305 if (dsta)
1306 sta_info_put(dsta);
1307 }
1308 }
1309
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001310 /* Mesh forwarding */
Johannes Berg902acc72008-02-23 15:17:19 +01001311 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001312 u8 *mesh_ttl = &((struct ieee80211s_hdr *)skb->cb)->ttl;
1313 (*mesh_ttl)--;
1314
1315 if (is_multicast_ether_addr(skb->data)) {
1316 if (*mesh_ttl > 0) {
1317 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1318 if (!xmit_skb && net_ratelimit())
1319 printk(KERN_DEBUG "%s: failed to clone "
1320 "multicast frame\n", dev->name);
1321 else
1322 xmit_skb->pkt_type = PACKET_OTHERHOST;
1323 } else
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 } else if (skb->pkt_type != PACKET_OTHERHOST &&
1327 compare_ether_addr(dev->dev_addr, skb->data) != 0) {
1328 if (*mesh_ttl == 0) {
Johannes Berg902acc72008-02-23 15:17:19 +01001329 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.sta,
1330 dropped_frames_ttl);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001331 dev_kfree_skb(skb);
1332 skb = NULL;
1333 } else {
1334 xmit_skb = skb;
1335 xmit_skb->pkt_type = PACKET_OTHERHOST;
1336 if (!(dev->flags & IFF_PROMISC))
1337 skb = NULL;
1338 }
1339 }
1340 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001341
Johannes Berg571ecf62007-07-27 15:43:22 +02001342 if (skb) {
1343 /* deliver to local stack */
1344 skb->protocol = eth_type_trans(skb, dev);
1345 memset(skb->cb, 0, sizeof(skb->cb));
1346 netif_rx(skb);
1347 }
1348
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001349 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001350 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001351 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001352 skb_reset_network_header(xmit_skb);
1353 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001354 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001355 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001356}
1357
Johannes Berg9ae54c82008-01-31 19:48:20 +01001358static ieee80211_rx_result
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001359ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1360{
1361 struct net_device *dev = rx->dev;
1362 struct ieee80211_local *local = rx->local;
1363 u16 fc, ethertype;
1364 u8 *payload;
1365 struct sk_buff *skb = rx->skb, *frame = NULL;
1366 const struct ethhdr *eth;
1367 int remaining, err;
1368 u8 dst[ETH_ALEN];
1369 u8 src[ETH_ALEN];
1370 DECLARE_MAC_BUF(mac);
1371
1372 fc = rx->fc;
1373 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001374 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001375
1376 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001377 return RX_DROP_MONITOR;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001378
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +02001379 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001380 return RX_CONTINUE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001381
1382 err = ieee80211_data_to_8023(rx);
1383 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001384 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001385
1386 skb->dev = dev;
1387
1388 dev->stats.rx_packets++;
1389 dev->stats.rx_bytes += skb->len;
1390
1391 /* skip the wrapping header */
1392 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1393 if (!eth)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001394 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001395
1396 while (skb != frame) {
1397 u8 padding;
1398 __be16 len = eth->h_proto;
1399 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1400
1401 remaining = skb->len;
1402 memcpy(dst, eth->h_dest, ETH_ALEN);
1403 memcpy(src, eth->h_source, ETH_ALEN);
1404
1405 padding = ((4 - subframe_len) & 0x3);
1406 /* the last MSDU has no padding */
1407 if (subframe_len > remaining) {
1408 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001409 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001410 }
1411
1412 skb_pull(skb, sizeof(struct ethhdr));
1413 /* if last subframe reuse skb */
1414 if (remaining <= subframe_len + padding)
1415 frame = skb;
1416 else {
1417 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1418 subframe_len);
1419
1420 if (frame == NULL)
Johannes Berge4c26ad2008-01-31 19:48:21 +01001421 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001422
1423 skb_reserve(frame, local->hw.extra_tx_headroom +
1424 sizeof(struct ethhdr));
1425 memcpy(skb_put(frame, ntohs(len)), skb->data,
1426 ntohs(len));
1427
1428 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1429 padding);
1430 if (!eth) {
1431 printk(KERN_DEBUG "%s: wrong buffer size ",
1432 dev->name);
1433 dev_kfree_skb(frame);
Johannes Berge4c26ad2008-01-31 19:48:21 +01001434 return RX_DROP_UNUSABLE;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001435 }
1436 }
1437
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001438 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001439 frame->dev = dev;
1440 frame->priority = skb->priority;
1441 rx->skb = frame;
1442
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001443 payload = frame->data;
1444 ethertype = (payload[6] << 8) | payload[7];
1445
1446 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001447 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1448 compare_ether_addr(payload,
1449 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001450 /* remove RFC1042 or Bridge-Tunnel
1451 * encapsulation and replace EtherType */
1452 skb_pull(frame, 6);
1453 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1454 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1455 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001456 memcpy(skb_push(frame, sizeof(__be16)),
1457 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001458 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1459 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1460 }
1461
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001462 if (!ieee80211_frame_allowed(rx)) {
1463 if (skb == frame) /* last frame */
Johannes Berge4c26ad2008-01-31 19:48:21 +01001464 return RX_DROP_UNUSABLE;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001465 dev_kfree_skb(frame);
1466 continue;
1467 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001468
1469 ieee80211_deliver_skb(rx);
1470 }
1471
Johannes Berg9ae54c82008-01-31 19:48:20 +01001472 return RX_QUEUED;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001473}
1474
Johannes Berg9ae54c82008-01-31 19:48:20 +01001475static ieee80211_rx_result
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001476ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1477{
1478 struct net_device *dev = rx->dev;
1479 u16 fc;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001480 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001481
1482 fc = rx->fc;
1483 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001484 return RX_CONTINUE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001485
1486 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001487 return RX_DROP_MONITOR;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001488
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001489 err = ieee80211_data_to_8023(rx);
1490 if (unlikely(err))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001491 return RX_DROP_UNUSABLE;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001492
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001493 if (!ieee80211_frame_allowed(rx))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001494 return RX_DROP_MONITOR;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001495
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001496 rx->skb->dev = dev;
1497
1498 dev->stats.rx_packets++;
1499 dev->stats.rx_bytes += rx->skb->len;
1500
1501 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001502
Johannes Berg9ae54c82008-01-31 19:48:20 +01001503 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001504}
1505
Johannes Berg9ae54c82008-01-31 19:48:20 +01001506static ieee80211_rx_result
Ron Rindjunsky71364712007-12-25 17:00:36 +02001507ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
1508{
1509 struct ieee80211_local *local = rx->local;
1510 struct ieee80211_hw *hw = &local->hw;
1511 struct sk_buff *skb = rx->skb;
1512 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
1513 struct tid_ampdu_rx *tid_agg_rx;
1514 u16 start_seq_num;
1515 u16 tid;
1516
1517 if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001518 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001519
1520 if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
1521 if (!rx->sta)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001522 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001523 tid = le16_to_cpu(bar->control) >> 12;
1524 tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
1525 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01001526 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001527
1528 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1529
1530 /* reset session timer */
1531 if (tid_agg_rx->timeout) {
1532 unsigned long expires =
1533 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1534 mod_timer(&tid_agg_rx->session_timer, expires);
1535 }
1536
1537 /* manage reordering buffer according to requested */
1538 /* sequence number */
1539 rcu_read_lock();
1540 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1541 start_seq_num, 1);
1542 rcu_read_unlock();
Johannes Berge4c26ad2008-01-31 19:48:21 +01001543 return RX_DROP_UNUSABLE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001544 }
1545
Johannes Berg9ae54c82008-01-31 19:48:20 +01001546 return RX_CONTINUE;
Ron Rindjunsky71364712007-12-25 17:00:36 +02001547}
1548
Johannes Berg9ae54c82008-01-31 19:48:20 +01001549static ieee80211_rx_result
Johannes Berg571ecf62007-07-27 15:43:22 +02001550ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1551{
1552 struct ieee80211_sub_if_data *sdata;
1553
Jiri Slabybadffb72007-08-28 17:01:54 -04001554 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berge4c26ad2008-01-31 19:48:21 +01001555 return RX_DROP_MONITOR;
Johannes Berg571ecf62007-07-27 15:43:22 +02001556
1557 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001558 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001559 sdata->vif.type == IEEE80211_IF_TYPE_IBSS ||
1560 sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001561 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg571ecf62007-07-27 15:43:22 +02001562 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001563 else
Johannes Berge4c26ad2008-01-31 19:48:21 +01001564 return RX_DROP_MONITOR;
Johannes Bergf9d540e2007-09-28 14:02:09 +02001565
Johannes Berg9ae54c82008-01-31 19:48:20 +01001566 return RX_QUEUED;
Johannes Berg571ecf62007-07-27 15:43:22 +02001567}
1568
Johannes Berg571ecf62007-07-27 15:43:22 +02001569static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1570 struct ieee80211_hdr *hdr,
Johannes Berg571ecf62007-07-27 15:43:22 +02001571 struct ieee80211_txrx_data *rx)
1572{
1573 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001574 DECLARE_MAC_BUF(mac);
1575 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001576
1577 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1578 if (rx->skb->len >= hdrlen + 4)
1579 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1580 else
1581 keyidx = -1;
1582
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001583 if (net_ratelimit())
1584 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001585 "failure from %s to %s keyidx=%d\n",
1586 dev->name, print_mac(mac, hdr->addr2),
1587 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001588
Johannes Berg8944b792008-01-31 19:48:26 +01001589 if (!rx->sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001590 /*
1591 * Some hardware seem to generate incorrect Michael MIC
1592 * reports; ignore them to avoid triggering countermeasures.
1593 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001594 if (net_ratelimit())
1595 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001596 "error for unknown address %s\n",
1597 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001598 goto ignore;
1599 }
1600
1601 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001602 if (net_ratelimit())
1603 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001604 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001605 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001606 goto ignore;
1607 }
1608
Johannes Berg51fb61e2007-12-19 01:31:27 +01001609 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001610 /*
1611 * APs with pairwise keys should never receive Michael MIC
1612 * errors for non-zero keyidx because these are reserved for
1613 * group keys and only the AP is sending real multicast
1614 * frames in the BSS.
1615 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001616 if (net_ratelimit())
1617 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1618 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001619 " (src %s)\n", dev->name, keyidx,
1620 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001621 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001622 }
1623
1624 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1625 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1626 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001627 if (net_ratelimit())
1628 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1629 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001630 "(fc=0x%04x) (src %s)\n",
1631 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001632 goto ignore;
1633 }
1634
Johannes Bergeb063c12007-08-28 17:01:53 -04001635 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001636 ignore:
1637 dev_kfree_skb(rx->skb);
1638 rx->skb = NULL;
1639}
1640
Michael Wu3d30d942008-01-31 19:48:27 +01001641static void ieee80211_rx_cooked_monitor(struct ieee80211_txrx_data *rx)
1642{
1643 struct ieee80211_sub_if_data *sdata;
1644 struct ieee80211_local *local = rx->local;
1645 struct ieee80211_rtap_hdr {
1646 struct ieee80211_radiotap_header hdr;
1647 u8 flags;
1648 u8 rate;
1649 __le16 chan_freq;
1650 __le16 chan_flags;
1651 } __attribute__ ((packed)) *rthdr;
1652 struct sk_buff *skb = rx->skb, *skb2;
1653 struct net_device *prev_dev = NULL;
1654 struct ieee80211_rx_status *status = rx->u.rx.status;
1655
1656 if (rx->flags & IEEE80211_TXRXD_RX_CMNTR_REPORTED)
1657 goto out_free_skb;
1658
1659 if (skb_headroom(skb) < sizeof(*rthdr) &&
1660 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1661 goto out_free_skb;
1662
1663 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1664 memset(rthdr, 0, sizeof(*rthdr));
1665 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1666 rthdr->hdr.it_present =
1667 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1668 (1 << IEEE80211_RADIOTAP_RATE) |
1669 (1 << IEEE80211_RADIOTAP_CHANNEL));
1670
1671 rthdr->rate = rx->u.rx.rate->bitrate / 5;
1672 rthdr->chan_freq = cpu_to_le16(status->freq);
1673
1674 if (status->band == IEEE80211_BAND_5GHZ)
1675 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1676 IEEE80211_CHAN_5GHZ);
1677 else
1678 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1679 IEEE80211_CHAN_2GHZ);
1680
1681 skb_set_mac_header(skb, 0);
1682 skb->ip_summed = CHECKSUM_UNNECESSARY;
1683 skb->pkt_type = PACKET_OTHERHOST;
1684 skb->protocol = htons(ETH_P_802_2);
1685
1686 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1687 if (!netif_running(sdata->dev))
1688 continue;
1689
1690 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR ||
1691 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1692 continue;
1693
1694 if (prev_dev) {
1695 skb2 = skb_clone(skb, GFP_ATOMIC);
1696 if (skb2) {
1697 skb2->dev = prev_dev;
1698 netif_rx(skb2);
1699 }
1700 }
1701
1702 prev_dev = sdata->dev;
1703 sdata->dev->stats.rx_packets++;
1704 sdata->dev->stats.rx_bytes += skb->len;
1705 }
1706
1707 if (prev_dev) {
1708 skb->dev = prev_dev;
1709 netif_rx(skb);
1710 skb = NULL;
1711 } else
1712 goto out_free_skb;
1713
1714 rx->flags |= IEEE80211_TXRXD_RX_CMNTR_REPORTED;
1715 return;
1716
1717 out_free_skb:
1718 dev_kfree_skb(skb);
1719}
1720
Johannes Berg58905292008-01-31 19:48:25 +01001721typedef ieee80211_rx_result (*ieee80211_rx_handler)(struct ieee80211_txrx_data *);
1722static ieee80211_rx_handler ieee80211_rx_handlers[] =
Johannes Berg571ecf62007-07-27 15:43:22 +02001723{
1724 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001725 ieee80211_rx_h_passive_scan,
1726 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001727 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001728 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001729 ieee80211_rx_h_defragment,
1730 ieee80211_rx_h_ps_poll,
1731 ieee80211_rx_h_michael_mic_verify,
1732 /* this must be after decryption - so header is counted in MPDU mic
1733 * must be before pae and data, so QOS_DATA format frames
1734 * are not passed to user space by these functions
1735 */
1736 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001737 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001738 ieee80211_rx_h_data,
Ron Rindjunsky71364712007-12-25 17:00:36 +02001739 ieee80211_rx_h_ctrl,
Johannes Berg571ecf62007-07-27 15:43:22 +02001740 ieee80211_rx_h_mgmt,
1741 NULL
1742};
1743
Johannes Berg8944b792008-01-31 19:48:26 +01001744static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
Johannes Berg58905292008-01-31 19:48:25 +01001745 struct ieee80211_txrx_data *rx,
Johannes Berg8944b792008-01-31 19:48:26 +01001746 struct sk_buff *skb)
Johannes Berg58905292008-01-31 19:48:25 +01001747{
1748 ieee80211_rx_handler *handler;
1749 ieee80211_rx_result res = RX_DROP_MONITOR;
1750
Johannes Berg8944b792008-01-31 19:48:26 +01001751 rx->skb = skb;
1752 rx->sdata = sdata;
1753 rx->dev = sdata->dev;
1754
Johannes Berg58905292008-01-31 19:48:25 +01001755 for (handler = ieee80211_rx_handlers; *handler != NULL; handler++) {
1756 res = (*handler)(rx);
1757
1758 switch (res) {
1759 case RX_CONTINUE:
1760 continue;
1761 case RX_DROP_UNUSABLE:
1762 case RX_DROP_MONITOR:
Johannes Berg8944b792008-01-31 19:48:26 +01001763 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1764 if (rx->sta)
1765 rx->sta->rx_dropped++;
Johannes Berg58905292008-01-31 19:48:25 +01001766 break;
1767 case RX_QUEUED:
Johannes Berg8944b792008-01-31 19:48:26 +01001768 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
Johannes Berg58905292008-01-31 19:48:25 +01001769 break;
1770 }
1771 break;
1772 }
1773
1774 switch (res) {
Johannes Berg58905292008-01-31 19:48:25 +01001775 case RX_CONTINUE:
Michael Wu3d30d942008-01-31 19:48:27 +01001776 case RX_DROP_MONITOR:
1777 ieee80211_rx_cooked_monitor(rx);
1778 break;
1779 case RX_DROP_UNUSABLE:
Johannes Berg58905292008-01-31 19:48:25 +01001780 dev_kfree_skb(rx->skb);
1781 break;
1782 }
1783}
1784
Johannes Berg571ecf62007-07-27 15:43:22 +02001785/* main receive path */
1786
Johannes Berg23a24de2007-07-27 15:43:22 +02001787static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1788 u8 *bssid, struct ieee80211_txrx_data *rx,
1789 struct ieee80211_hdr *hdr)
1790{
1791 int multicast = is_multicast_ether_addr(hdr->addr1);
1792
Johannes Berg51fb61e2007-12-19 01:31:27 +01001793 switch (sdata->vif.type) {
Johannes Berg23a24de2007-07-27 15:43:22 +02001794 case IEEE80211_IF_TYPE_STA:
1795 if (!bssid)
1796 return 0;
1797 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001798 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001799 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001800 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001801 } else if (!multicast &&
1802 compare_ether_addr(sdata->dev->dev_addr,
1803 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001804 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001805 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001806 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001807 }
1808 break;
1809 case IEEE80211_IF_TYPE_IBSS:
1810 if (!bssid)
1811 return 0;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001812 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
1813 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
1814 return 1;
1815 else if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001816 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001817 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001818 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001819 } else if (!multicast &&
1820 compare_ether_addr(sdata->dev->dev_addr,
1821 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001822 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001823 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001824 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001825 } else if (!rx->sta)
1826 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1827 bssid, hdr->addr2);
1828 break;
Johannes Berg6032f932008-02-23 15:17:07 +01001829 case IEEE80211_IF_TYPE_MESH_POINT:
1830 if (!multicast &&
1831 compare_ether_addr(sdata->dev->dev_addr,
1832 hdr->addr1) != 0) {
1833 if (!(sdata->dev->flags & IFF_PROMISC))
1834 return 0;
1835
1836 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1837 }
1838 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001839 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001840 case IEEE80211_IF_TYPE_AP:
1841 if (!bssid) {
1842 if (compare_ether_addr(sdata->dev->dev_addr,
1843 hdr->addr1))
1844 return 0;
1845 } else if (!ieee80211_bssid_match(bssid,
1846 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001847 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001848 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001849 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001850 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001851 if (sdata->dev == sdata->local->mdev &&
1852 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001853 /* do not receive anything via
1854 * master device when not scanning */
1855 return 0;
1856 break;
1857 case IEEE80211_IF_TYPE_WDS:
1858 if (bssid ||
1859 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1860 return 0;
1861 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1862 return 0;
1863 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001864 case IEEE80211_IF_TYPE_MNTR:
1865 /* take everything */
1866 break;
Johannes Berga2897552007-09-28 14:01:25 +02001867 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001868 /* should never get here */
1869 WARN_ON(1);
1870 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001871 }
1872
1873 return 1;
1874}
1875
Johannes Berg571ecf62007-07-27 15:43:22 +02001876/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001877 * This is the actual Rx frames handler. as it blongs to Rx path it must
1878 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02001879 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001880static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
1881 struct sk_buff *skb,
1882 struct ieee80211_rx_status *status,
Johannes Berg8318d782008-01-24 19:38:38 +01001883 u32 load,
1884 struct ieee80211_rate *rate)
Johannes Berg571ecf62007-07-27 15:43:22 +02001885{
1886 struct ieee80211_local *local = hw_to_local(hw);
1887 struct ieee80211_sub_if_data *sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001888 struct ieee80211_hdr *hdr;
1889 struct ieee80211_txrx_data rx;
1890 u16 type;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001891 int prepares;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001892 struct ieee80211_sub_if_data *prev = NULL;
1893 struct sk_buff *skb_new;
1894 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001895
1896 hdr = (struct ieee80211_hdr *) skb->data;
1897 memset(&rx, 0, sizeof(rx));
1898 rx.skb = skb;
1899 rx.local = local;
1900
1901 rx.u.rx.status = status;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001902 rx.u.rx.load = load;
Johannes Berg8318d782008-01-24 19:38:38 +01001903 rx.u.rx.rate = rate;
Johannes Bergb2e77712007-09-26 15:19:39 +02001904 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001905 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001906
Johannes Bergb2e77712007-09-26 15:19:39 +02001907 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001908 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001909
Johannes Berg8944b792008-01-31 19:48:26 +01001910 rx.sta = sta_info_get(local, hdr->addr2);
1911 if (rx.sta) {
Johannes Bergb2e77712007-09-26 15:19:39 +02001912 rx.dev = rx.sta->dev;
1913 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1914 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001915
Johannes Berg571ecf62007-07-27 15:43:22 +02001916 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
Johannes Berg8944b792008-01-31 19:48:26 +01001917 ieee80211_rx_michael_mic_report(local->mdev, hdr, &rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001918 goto end;
1919 }
1920
Zhu Yiece8edd2007-11-22 10:53:21 +08001921 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001922 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001923
Johannes Berg38f37142008-01-29 17:07:43 +01001924 ieee80211_parse_qos(&rx);
1925 ieee80211_verify_ip_alignment(&rx);
1926
Johannes Berg571ecf62007-07-27 15:43:22 +02001927 skb = rx.skb;
1928
Johannes Berg79010422007-09-18 17:29:21 -04001929 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001930 if (!netif_running(sdata->dev))
1931 continue;
1932
Johannes Berg51fb61e2007-12-19 01:31:27 +01001933 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +02001934 continue;
1935
Johannes Berg51fb61e2007-12-19 01:31:27 +01001936 bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Bergb2e77712007-09-26 15:19:39 +02001937 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001938 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02001939
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001940 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02001941 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001942
Johannes Berg340e11f2007-07-27 15:43:22 +02001943 /*
1944 * frame is destined for this interface, but if it's not
1945 * also for the previous one we handle that after the
1946 * loop to avoid copying the SKB once too much
1947 */
1948
1949 if (!prev) {
1950 prev = sdata;
1951 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001952 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001953
1954 /*
1955 * frame was destined for the previous interface
1956 * so invoke RX handlers for it
1957 */
1958
1959 skb_new = skb_copy(skb, GFP_ATOMIC);
1960 if (!skb_new) {
1961 if (net_ratelimit())
1962 printk(KERN_DEBUG "%s: failed to copy "
1963 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001964 wiphy_name(local->hw.wiphy),
1965 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001966 continue;
1967 }
Helmut Schaa69f817b2007-12-21 15:16:35 +01001968 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8944b792008-01-31 19:48:26 +01001969 ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001970 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001971 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001972 if (prev) {
Helmut Schaa69f817b2007-12-21 15:16:35 +01001973 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8944b792008-01-31 19:48:26 +01001974 ieee80211_invoke_rx_handlers(prev, &rx, skb);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001975 } else
1976 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001977
Johannes Berg8e6f0032007-07-27 15:43:22 +02001978 end:
Johannes Berg8944b792008-01-31 19:48:26 +01001979 if (rx.sta)
1980 sta_info_put(rx.sta);
Johannes Berg571ecf62007-07-27 15:43:22 +02001981}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001982
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001983#define SEQ_MODULO 0x1000
1984#define SEQ_MASK 0xfff
1985
1986static inline int seq_less(u16 sq1, u16 sq2)
1987{
1988 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1989}
1990
1991static inline u16 seq_inc(u16 sq)
1992{
1993 return ((sq + 1) & SEQ_MASK);
1994}
1995
1996static inline u16 seq_sub(u16 sq1, u16 sq2)
1997{
1998 return ((sq1 - sq2) & SEQ_MASK);
1999}
2000
2001
Ron Rindjunsky71364712007-12-25 17:00:36 +02002002/*
2003 * As it function blongs to Rx path it must be called with
2004 * the proper rcu_read_lock protection for its flow.
2005 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002006u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
2007 struct tid_ampdu_rx *tid_agg_rx,
2008 struct sk_buff *skb, u16 mpdu_seq_num,
2009 int bar_req)
2010{
2011 struct ieee80211_local *local = hw_to_local(hw);
2012 struct ieee80211_rx_status status;
2013 u16 head_seq_num, buf_size;
2014 int index;
2015 u32 pkt_load;
Johannes Berg8318d782008-01-24 19:38:38 +01002016 struct ieee80211_supported_band *sband;
2017 struct ieee80211_rate *rate;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002018
2019 buf_size = tid_agg_rx->buf_size;
2020 head_seq_num = tid_agg_rx->head_seq_num;
2021
2022 /* frame with out of date sequence number */
2023 if (seq_less(mpdu_seq_num, head_seq_num)) {
2024 dev_kfree_skb(skb);
2025 return 1;
2026 }
2027
2028 /* if frame sequence number exceeds our buffering window size or
2029 * block Ack Request arrived - release stored frames */
2030 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
2031 /* new head to the ordering buffer */
2032 if (bar_req)
2033 head_seq_num = mpdu_seq_num;
2034 else
2035 head_seq_num =
2036 seq_inc(seq_sub(mpdu_seq_num, buf_size));
2037 /* release stored frames up to new head to stack */
2038 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2039 index = seq_sub(tid_agg_rx->head_seq_num,
2040 tid_agg_rx->ssn)
2041 % tid_agg_rx->buf_size;
2042
2043 if (tid_agg_rx->reorder_buf[index]) {
2044 /* release the reordered frames to stack */
2045 memcpy(&status,
2046 tid_agg_rx->reorder_buf[index]->cb,
2047 sizeof(status));
Johannes Berg8318d782008-01-24 19:38:38 +01002048 sband = local->hw.wiphy->bands[status.band];
2049 rate = &sband->bitrates[status.rate_idx];
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002050 pkt_load = ieee80211_rx_load_stats(local,
2051 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002052 &status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002053 __ieee80211_rx_handle_packet(hw,
2054 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002055 &status, pkt_load, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002056 tid_agg_rx->stored_mpdu_num--;
2057 tid_agg_rx->reorder_buf[index] = NULL;
2058 }
2059 tid_agg_rx->head_seq_num =
2060 seq_inc(tid_agg_rx->head_seq_num);
2061 }
2062 if (bar_req)
2063 return 1;
2064 }
2065
2066 /* now the new frame is always in the range of the reordering */
2067 /* buffer window */
2068 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
2069 % tid_agg_rx->buf_size;
2070 /* check if we already stored this frame */
2071 if (tid_agg_rx->reorder_buf[index]) {
2072 dev_kfree_skb(skb);
2073 return 1;
2074 }
2075
2076 /* if arrived mpdu is in the right order and nothing else stored */
2077 /* release it immediately */
2078 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
2079 tid_agg_rx->stored_mpdu_num == 0) {
2080 tid_agg_rx->head_seq_num =
2081 seq_inc(tid_agg_rx->head_seq_num);
2082 return 0;
2083 }
2084
2085 /* put the frame in the reordering buffer */
2086 tid_agg_rx->reorder_buf[index] = skb;
2087 tid_agg_rx->stored_mpdu_num++;
2088 /* release the buffer until next missing frame */
2089 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
2090 % tid_agg_rx->buf_size;
2091 while (tid_agg_rx->reorder_buf[index]) {
2092 /* release the reordered frame back to stack */
2093 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
2094 sizeof(status));
Johannes Berg8318d782008-01-24 19:38:38 +01002095 sband = local->hw.wiphy->bands[status.band];
2096 rate = &sband->bitrates[status.rate_idx];
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002097 pkt_load = ieee80211_rx_load_stats(local,
2098 tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002099 &status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002100 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
Johannes Berg8318d782008-01-24 19:38:38 +01002101 &status, pkt_load, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002102 tid_agg_rx->stored_mpdu_num--;
2103 tid_agg_rx->reorder_buf[index] = NULL;
2104 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2105 index = seq_sub(tid_agg_rx->head_seq_num,
2106 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2107 }
2108 return 1;
2109}
2110
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02002111static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2112 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002113{
2114 struct ieee80211_hw *hw = &local->hw;
2115 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2116 struct sta_info *sta;
2117 struct tid_ampdu_rx *tid_agg_rx;
2118 u16 fc, sc;
2119 u16 mpdu_seq_num;
2120 u8 ret = 0, *qc;
2121 int tid;
2122
2123 sta = sta_info_get(local, hdr->addr2);
2124 if (!sta)
2125 return ret;
2126
2127 fc = le16_to_cpu(hdr->frame_control);
2128
2129 /* filter the QoS data rx stream according to
2130 * STA/TID and check if this STA/TID is on aggregation */
2131 if (!WLAN_FC_IS_QOS_DATA(fc))
2132 goto end_reorder;
2133
2134 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
2135 tid = qc[0] & QOS_CONTROL_TID_MASK;
2136 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
2137
2138 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
2139 goto end_reorder;
2140
2141 /* null data frames are excluded */
Ron Rindjunsky8b6bbe72008-01-23 18:17:13 +02002142 if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002143 goto end_reorder;
2144
2145 /* new un-ordered ampdu frame - process it */
2146
2147 /* reset session timer */
2148 if (tid_agg_rx->timeout) {
2149 unsigned long expires =
2150 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
2151 mod_timer(&tid_agg_rx->session_timer, expires);
2152 }
2153
2154 /* if this mpdu is fragmented - terminate rx aggregation session */
2155 sc = le16_to_cpu(hdr->seq_ctrl);
2156 if (sc & IEEE80211_SCTL_FRAG) {
2157 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
2158 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
2159 ret = 1;
2160 goto end_reorder;
2161 }
2162
2163 /* according to mpdu sequence number deal with reordering buffer */
2164 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
2165 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
2166 mpdu_seq_num, 0);
2167end_reorder:
2168 if (sta)
2169 sta_info_put(sta);
2170 return ret;
2171}
2172
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002173/*
2174 * This is the receive path handler. It is called by a low level driver when an
2175 * 802.11 MPDU is received from the hardware.
2176 */
2177void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
2178 struct ieee80211_rx_status *status)
2179{
2180 struct ieee80211_local *local = hw_to_local(hw);
2181 u32 pkt_load;
Johannes Berg8318d782008-01-24 19:38:38 +01002182 struct ieee80211_rate *rate = NULL;
2183 struct ieee80211_supported_band *sband;
2184
2185 if (status->band < 0 ||
2186 status->band > IEEE80211_NUM_BANDS) {
2187 WARN_ON(1);
2188 return;
2189 }
2190
2191 sband = local->hw.wiphy->bands[status->band];
2192
2193 if (!sband ||
2194 status->rate_idx < 0 ||
2195 status->rate_idx >= sband->n_bitrates) {
2196 WARN_ON(1);
2197 return;
2198 }
2199
2200 rate = &sband->bitrates[status->rate_idx];
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002201
2202 /*
2203 * key references and virtual interfaces are protected using RCU
2204 * and this requires that we are in a read-side RCU section during
2205 * receive processing
2206 */
2207 rcu_read_lock();
2208
2209 /*
2210 * Frames with failed FCS/PLCP checksum are not returned,
2211 * all other frames are returned without radiotap header
2212 * if it was previously present.
2213 * Also, frames with less than 16 bytes are dropped.
2214 */
Johannes Berg8318d782008-01-24 19:38:38 +01002215 skb = ieee80211_rx_monitor(local, skb, status, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002216 if (!skb) {
2217 rcu_read_unlock();
2218 return;
2219 }
2220
Johannes Berg8318d782008-01-24 19:38:38 +01002221 pkt_load = ieee80211_rx_load_stats(local, skb, status, rate);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002222 local->channel_use_raw += pkt_load;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002223
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002224 if (!ieee80211_rx_reorder_ampdu(local, skb))
Johannes Berg8318d782008-01-24 19:38:38 +01002225 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load, rate);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002226
2227 rcu_read_unlock();
2228}
Johannes Berg571ecf62007-07-27 15:43:22 +02002229EXPORT_SYMBOL(__ieee80211_rx);
2230
2231/* This is a version of the rx handler that can be called from hard irq
2232 * context. Post the skb on the queue and schedule the tasklet */
2233void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
2234 struct ieee80211_rx_status *status)
2235{
2236 struct ieee80211_local *local = hw_to_local(hw);
2237
2238 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2239
2240 skb->dev = local->mdev;
2241 /* copy status into skb->cb for use by tasklet */
2242 memcpy(skb->cb, status, sizeof(*status));
2243 skb->pkt_type = IEEE80211_RX_MSG;
2244 skb_queue_tail(&local->skb_queue, skb);
2245 tasklet_schedule(&local->tasklet);
2246}
2247EXPORT_SYMBOL(ieee80211_rx_irqsafe);