blob: 3aae8e9e4e0aab5bc8348b043e8107810d029cdd [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
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040016#include <linux/rcupdate.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020017#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020022#include "wep.h"
23#include "wpa.h"
24#include "tkip.h"
25#include "wme.h"
26
Ron Rindjunsky71364712007-12-25 17:00:36 +020027u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
28 struct tid_ampdu_rx *tid_agg_rx,
29 struct sk_buff *skb, u16 mpdu_seq_num,
30 int bar_req);
Johannes Bergb2e77712007-09-26 15:19:39 +020031/*
32 * monitor mode reception
33 *
34 * This function cleans up the SKB, i.e. it removes all the stuff
35 * only useful for monitoring.
36 */
37static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
38 struct sk_buff *skb,
39 int rtap_len)
40{
41 skb_pull(skb, rtap_len);
42
43 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
44 if (likely(skb->len > FCS_LEN))
45 skb_trim(skb, skb->len - FCS_LEN);
46 else {
47 /* driver bug */
48 WARN_ON(1);
49 dev_kfree_skb(skb);
50 skb = NULL;
51 }
52 }
53
54 return skb;
55}
56
57static inline int should_drop_frame(struct ieee80211_rx_status *status,
58 struct sk_buff *skb,
59 int present_fcs_len,
60 int radiotap_len)
61{
62 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
63
64 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
65 return 1;
66 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
67 return 1;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020068 if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
69 cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
70 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
Ron Rindjunsky71364712007-12-25 17:00:36 +020071 cpu_to_le16(IEEE80211_STYPE_PSPOLL)) &&
72 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
73 cpu_to_le16(IEEE80211_STYPE_BACK_REQ)))
Johannes Bergb2e77712007-09-26 15:19:39 +020074 return 1;
75 return 0;
76}
77
78/*
79 * This function copies a received frame to all monitor interfaces and
80 * returns a cleaned-up SKB that no longer includes the FCS nor the
81 * radiotap header the driver might have added.
82 */
83static struct sk_buff *
84ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
85 struct ieee80211_rx_status *status)
86{
87 struct ieee80211_sub_if_data *sdata;
88 struct ieee80211_rate *rate;
89 int needed_headroom = 0;
Johannes Bergc49e5ea2007-12-11 21:33:42 +010090 struct ieee80211_radiotap_header *rthdr;
91 __le64 *rttsft = NULL;
92 struct ieee80211_rtap_fixed_data {
Johannes Bergb2e77712007-09-26 15:19:39 +020093 u8 flags;
94 u8 rate;
95 __le16 chan_freq;
96 __le16 chan_flags;
97 u8 antsignal;
98 u8 padding_for_rxflags;
99 __le16 rx_flags;
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100100 } __attribute__ ((packed)) *rtfixed;
Johannes Bergb2e77712007-09-26 15:19:39 +0200101 struct sk_buff *skb, *skb2;
102 struct net_device *prev_dev = NULL;
103 int present_fcs_len = 0;
104 int rtap_len = 0;
105
106 /*
107 * First, we may need to make a copy of the skb because
108 * (1) we need to modify it for radiotap (if not present), and
109 * (2) the other RX handlers will modify the skb we got.
110 *
111 * We don't need to, of course, if we aren't going to return
112 * the SKB because it has a bad FCS/PLCP checksum.
113 */
114 if (status->flag & RX_FLAG_RADIOTAP)
115 rtap_len = ieee80211_get_radiotap_len(origskb->data);
116 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100117 /* room for radiotap header, always present fields and TSFT */
118 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
Johannes Bergb2e77712007-09-26 15:19:39 +0200119
120 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
121 present_fcs_len = FCS_LEN;
122
123 if (!local->monitors) {
124 if (should_drop_frame(status, origskb, present_fcs_len,
125 rtap_len)) {
126 dev_kfree_skb(origskb);
127 return NULL;
128 }
129
130 return remove_monitor_info(local, origskb, rtap_len);
131 }
132
133 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
134 /* only need to expand headroom if necessary */
135 skb = origskb;
136 origskb = NULL;
137
138 /*
139 * This shouldn't trigger often because most devices have an
140 * RX header they pull before we get here, and that should
141 * be big enough for our radiotap information. We should
142 * probably export the length to drivers so that we can have
143 * them allocate enough headroom to start with.
144 */
145 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100146 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200147 dev_kfree_skb(skb);
148 return NULL;
149 }
150 } else {
151 /*
152 * Need to make a copy and possibly remove radiotap header
153 * and FCS from the original.
154 */
155 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
156
157 origskb = remove_monitor_info(local, origskb, rtap_len);
158
159 if (!skb)
160 return origskb;
161 }
162
163 /* if necessary, prepend radiotap information */
164 if (!(status->flag & RX_FLAG_RADIOTAP)) {
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100165 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
166 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
167 if (status->flag & RX_FLAG_TSFT) {
168 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
169 rtap_len += 8;
170 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200171 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
172 memset(rthdr, 0, sizeof(*rthdr));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100173 memset(rtfixed, 0, sizeof(*rtfixed));
174 rthdr->it_present =
Johannes Bergb2e77712007-09-26 15:19:39 +0200175 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
176 (1 << IEEE80211_RADIOTAP_RATE) |
177 (1 << IEEE80211_RADIOTAP_CHANNEL) |
178 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
179 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100180 rtfixed->flags = 0;
181 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
182 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
183
184 if (rttsft) {
185 *rttsft = cpu_to_le64(status->mactime);
186 rthdr->it_present |=
187 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
188 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200189
190 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100191 rtfixed->rx_flags = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200192 if (status->flag &
193 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100194 rtfixed->rx_flags |=
Johannes Bergb2e77712007-09-26 15:19:39 +0200195 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
196
197 rate = ieee80211_get_rate(local, status->phymode,
198 status->rate);
199 if (rate)
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100200 rtfixed->rate = rate->rate / 5;
Johannes Bergb2e77712007-09-26 15:19:39 +0200201
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100202 rtfixed->chan_freq = cpu_to_le16(status->freq);
Johannes Bergb2e77712007-09-26 15:19:39 +0200203
204 if (status->phymode == MODE_IEEE80211A)
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100205 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200206 cpu_to_le16(IEEE80211_CHAN_OFDM |
207 IEEE80211_CHAN_5GHZ);
208 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100209 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200210 cpu_to_le16(IEEE80211_CHAN_DYN |
211 IEEE80211_CHAN_2GHZ);
212
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100213 rtfixed->antsignal = status->ssi;
214 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Bergb2e77712007-09-26 15:19:39 +0200215 }
216
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100217 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200218 skb->ip_summed = CHECKSUM_UNNECESSARY;
219 skb->pkt_type = PACKET_OTHERHOST;
220 skb->protocol = htons(ETH_P_802_2);
221
222 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
223 if (!netif_running(sdata->dev))
224 continue;
225
Johannes Berg51fb61e2007-12-19 01:31:27 +0100226 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +0200227 continue;
228
229 if (prev_dev) {
230 skb2 = skb_clone(skb, GFP_ATOMIC);
231 if (skb2) {
232 skb2->dev = prev_dev;
233 netif_rx(skb2);
234 }
235 }
236
237 prev_dev = sdata->dev;
238 sdata->dev->stats.rx_packets++;
239 sdata->dev->stats.rx_bytes += skb->len;
240 }
241
242 if (prev_dev) {
243 skb->dev = prev_dev;
244 netif_rx(skb);
245 } else
246 dev_kfree_skb(skb);
247
248 return origskb;
249}
250
251
Johannes Berg38f37142008-01-29 17:07:43 +0100252static void ieee80211_parse_qos(struct ieee80211_txrx_data *rx)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200253{
254 u8 *data = rx->skb->data;
255 int tid;
256
257 /* does the frame have a qos control field? */
258 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
259 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
260 /* frame has qos control */
261 tid = qc[0] & QOS_CONTROL_TID_MASK;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200262 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200263 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200264 else
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200265 rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200266 } else {
267 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
268 /* Separate TID for management frames */
269 tid = NUM_RX_DATA_QUEUES - 1;
270 } else {
271 /* no qos control present */
272 tid = 0; /* 802.1d - Best Effort */
273 }
274 }
Johannes Berg52865df2007-07-27 15:43:22 +0200275
Johannes Berg6e0d1142007-07-27 15:43:22 +0200276 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865df2007-07-27 15:43:22 +0200277 /* only a debug counter, sta might not be assigned properly yet */
278 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200279 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200280
281 rx->u.rx.queue = tid;
282 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
283 * For now, set skb->priority to 0 for other cases. */
284 rx->skb->priority = (tid > 7) ? 0 : tid;
Johannes Berg38f37142008-01-29 17:07:43 +0100285}
Johannes Berg6e0d1142007-07-27 15:43:22 +0200286
Johannes Berg38f37142008-01-29 17:07:43 +0100287static void ieee80211_verify_ip_alignment(struct ieee80211_txrx_data *rx)
288{
289#ifdef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
290 int hdrlen;
291
292 if (!WLAN_FC_DATA_PRESENT(rx->fc))
293 return;
294
295 /*
296 * Drivers are required to align the payload data in a way that
297 * guarantees that the contained IP header is aligned to a four-
298 * byte boundary. In the case of regular frames, this simply means
299 * aligning the payload to a four-byte boundary (because either
300 * the IP header is directly contained, or IV/RFC1042 headers that
301 * have a length divisible by four are in front of it.
302 *
303 * With A-MSDU frames, however, the payload data address must
304 * yield two modulo four because there are 14-byte 802.3 headers
305 * within the A-MSDU frames that push the IP header further back
306 * to a multiple of four again. Thankfully, the specs were sane
307 * enough this time around to require padding each A-MSDU subframe
308 * to a length that is a multiple of four.
309 *
310 * Padding like atheros hardware adds which is inbetween the 802.11
311 * header and the payload is not supported, the driver is required
312 * to move the 802.11 header further back in that case.
313 */
314 hdrlen = ieee80211_get_hdrlen(rx->fc);
315 if (rx->flags & IEEE80211_TXRXD_RX_AMSDU)
316 hdrlen += ETH_HLEN;
317 WARN_ON_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3);
318#endif
Johannes Berg6e0d1142007-07-27 15:43:22 +0200319}
320
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200321
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +0200322static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200323 struct sk_buff *skb,
324 struct ieee80211_rx_status *status)
Johannes Berg571ecf62007-07-27 15:43:22 +0200325{
Johannes Berg571ecf62007-07-27 15:43:22 +0200326 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
327 u32 load = 0, hdrtime;
328 struct ieee80211_rate *rate;
329 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
330 int i;
331
332 /* Estimate total channel use caused by this frame */
333
334 if (unlikely(mode->num_rates < 0))
335 return TXRX_CONTINUE;
336
337 rate = &mode->rates[0];
338 for (i = 0; i < mode->num_rates; i++) {
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200339 if (mode->rates[i].val == status->rate) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200340 rate = &mode->rates[i];
341 break;
342 }
343 }
344
345 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
346 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
347
348 if (mode->mode == MODE_IEEE80211A ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200349 (mode->mode == MODE_IEEE80211G &&
350 rate->flags & IEEE80211_RATE_ERP))
351 hdrtime = CHAN_UTIL_HDR_SHORT;
352 else
353 hdrtime = CHAN_UTIL_HDR_LONG;
354
355 load = hdrtime;
356 if (!is_multicast_ether_addr(hdr->addr1))
357 load += hdrtime;
358
359 load += skb->len * rate->rate_inv;
360
361 /* Divide channel_use by 8 to avoid wrapping around the counter */
362 load >>= CHAN_UTIL_SHIFT;
Johannes Berg571ecf62007-07-27 15:43:22 +0200363
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200364 return load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200365}
366
Johannes Berg571ecf62007-07-27 15:43:22 +0200367/* rx handlers */
368
369static ieee80211_txrx_result
370ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
371{
Johannes Berg52865df2007-07-27 15:43:22 +0200372 if (rx->sta)
373 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200374 rx->sdata->channel_use_raw += rx->u.rx.load;
375 return TXRX_CONTINUE;
376}
377
Johannes Berg571ecf62007-07-27 15:43:22 +0200378static ieee80211_txrx_result
379ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
380{
381 struct ieee80211_local *local = rx->local;
382 struct sk_buff *skb = rx->skb;
383
Zhu Yiece8edd2007-11-22 10:53:21 +0800384 if (unlikely(local->sta_hw_scanning))
385 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
386
387 if (unlikely(local->sta_sw_scanning)) {
388 /* drop all the other packets during a software scan anyway */
389 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
390 != TXRX_QUEUED)
391 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +0200392 return TXRX_QUEUED;
393 }
394
Jiri Slabybadffb72007-08-28 17:01:54 -0400395 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200396 /* scanning finished during invoking of handlers */
397 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
398 return TXRX_DROP;
399 }
400
401 return TXRX_CONTINUE;
402}
403
404static ieee80211_txrx_result
405ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
406{
407 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200408 hdr = (struct ieee80211_hdr *) rx->skb->data;
409
410 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
411 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
412 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
413 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
414 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400415 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200416 rx->local->dot11FrameDuplicateCount++;
417 rx->sta->num_duplicates++;
418 }
419 return TXRX_DROP;
420 } else
421 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
422 }
423
Johannes Berg571ecf62007-07-27 15:43:22 +0200424 if (unlikely(rx->skb->len < 16)) {
425 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
426 return TXRX_DROP;
427 }
428
Johannes Berg571ecf62007-07-27 15:43:22 +0200429 /* Drop disallowed frame classes based on STA auth/assoc state;
430 * IEEE 802.11, Chap 5.5.
431 *
432 * 80211.o does filtering only based on association state, i.e., it
433 * drops Class 3 frames from not associated stations. hostapd sends
434 * deauth/disassoc frames when needed. In addition, hostapd is
435 * responsible for filtering on both auth and assoc states.
436 */
437 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
438 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
439 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100440 rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200441 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
442 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
443 !(rx->fc & IEEE80211_FCTL_TODS) &&
444 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400445 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200446 /* Drop IBSS frames and frames for other hosts
447 * silently. */
448 return TXRX_DROP;
449 }
450
Johannes Bergf9d540e2007-09-28 14:02:09 +0200451 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +0200452 }
453
Johannes Berg570bd532007-07-27 15:43:22 +0200454 return TXRX_CONTINUE;
455}
456
457
458static ieee80211_txrx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200459ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200460{
461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400462 int keyidx;
463 int hdrlen;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200464 ieee80211_txrx_result result = TXRX_DROP;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400465 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200466
Johannes Berg3017b802007-08-28 17:01:53 -0400467 /*
468 * Key selection 101
469 *
470 * There are three types of keys:
471 * - GTK (group keys)
472 * - PTK (pairwise keys)
473 * - STK (station-to-station pairwise keys)
474 *
475 * When selecting a key, we have to distinguish between multicast
476 * (including broadcast) and unicast frames, the latter can only
477 * use PTKs and STKs while the former always use GTKs. Unless, of
478 * course, actual WEP keys ("pre-RSNA") are used, then unicast
479 * frames can also use key indizes like GTKs. Hence, if we don't
480 * have a PTK/STK we check the key index for a WEP key.
481 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400482 * Note that in a regular BSS, multicast frames are sent by the
483 * AP only, associated stations unicast the frame to the AP first
484 * which then multicasts it on their behalf.
485 *
Johannes Berg3017b802007-08-28 17:01:53 -0400486 * There is also a slight problem in IBSS mode: GTKs are negotiated
487 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400488 * The spec seems to expect that one negotiates the same key with
489 * every station but there's no such requirement; VLANs could be
490 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400491 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200492
Johannes Berg3017b802007-08-28 17:01:53 -0400493 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
494 return TXRX_CONTINUE;
495
496 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200497 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400498 * addressed to us nor a multicast frame.
499 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400500 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400501 return TXRX_CONTINUE;
502
Johannes Bergd4e46a32007-09-14 11:10:24 -0400503 if (rx->sta)
504 stakey = rcu_dereference(rx->sta->key);
505
506 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
507 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200508 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400509 /*
510 * The device doesn't give us the IV so we won't be
511 * able to look up the key. That's ok though, we
512 * don't need to decrypt the frame, we just won't
513 * be able to keep statistics accurate.
514 * Except for key threshold notifications, should
515 * we somehow allow the driver to tell us which key
516 * the hardware used if this flag is set?
517 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400518 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
519 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400520 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200521
Johannes Berg3017b802007-08-28 17:01:53 -0400522 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200523
Johannes Berg3017b802007-08-28 17:01:53 -0400524 if (rx->skb->len < 8 + hdrlen)
525 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200526
Johannes Berg3017b802007-08-28 17:01:53 -0400527 /*
528 * no need to call ieee80211_wep_get_keyidx,
529 * it verifies a bunch of things we've done already
530 */
531 keyidx = rx->skb->data[hdrlen + 3] >> 6;
532
Johannes Bergd4e46a32007-09-14 11:10:24 -0400533 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400534
535 /*
536 * RSNA-protected unicast frames should always be sent with
537 * pairwise or station-to-station keys, but for WEP we allow
538 * using a key index as well.
539 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400540 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400541 !is_multicast_ether_addr(hdr->addr1))
542 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200543 }
544
Johannes Berg3017b802007-08-28 17:01:53 -0400545 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200546 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400547 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200548 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500549#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200550 if (net_ratelimit())
551 printk(KERN_DEBUG "%s: RX protected frame,"
552 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500553#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berg70f08762007-09-26 17:53:14 +0200554 return TXRX_DROP;
555 }
556
Johannes Berg1990af82007-09-26 17:53:15 +0200557 /* Check for weak IVs if possible */
558 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
559 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
560 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
561 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
562 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
563 rx->sta->wep_weak_iv_count++;
564
Johannes Berg70f08762007-09-26 17:53:14 +0200565 switch (rx->key->conf.alg) {
566 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200567 result = ieee80211_crypto_wep_decrypt(rx);
568 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200569 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200570 result = ieee80211_crypto_tkip_decrypt(rx);
571 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200572 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200573 result = ieee80211_crypto_ccmp_decrypt(rx);
574 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200575 }
576
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200577 /* either the frame has been decrypted or will be dropped */
578 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
579
580 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200581}
582
Johannes Berg571ecf62007-07-27 15:43:22 +0200583static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
584{
585 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700586 DECLARE_MAC_BUF(mac);
587
Johannes Berg571ecf62007-07-27 15:43:22 +0200588 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
589
590 if (sdata->bss)
591 atomic_inc(&sdata->bss->num_sta_ps);
592 sta->flags |= WLAN_STA_PS;
593 sta->pspoll = 0;
594#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700595 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
596 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200597#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
598}
599
600static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
601{
602 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
603 struct sk_buff *skb;
604 int sent = 0;
605 struct ieee80211_sub_if_data *sdata;
606 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700607 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200608
609 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
610 if (sdata->bss)
611 atomic_dec(&sdata->bss->num_sta_ps);
612 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
613 sta->pspoll = 0;
614 if (!skb_queue_empty(&sta->ps_tx_buf)) {
615 if (local->ops->set_tim)
616 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
617 if (sdata->bss)
618 bss_tim_clear(local, sdata->bss, sta->aid);
619 }
620#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700621 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
622 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200623#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
624 /* Send all buffered frames to the station */
625 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
626 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
627 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400628 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200629 dev_queue_xmit(skb);
630 }
631 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
632 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
633 local->total_ps_buffered--;
634 sent++;
635#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700636 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200637 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700638 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200639#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400640 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200641 dev_queue_xmit(skb);
642 }
643
644 return sent;
645}
646
647static ieee80211_txrx_result
648ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
649{
650 struct sta_info *sta = rx->sta;
651 struct net_device *dev = rx->dev;
652 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
653
654 if (!sta)
655 return TXRX_CONTINUE;
656
657 /* Update last_rx only for IBSS packets which are for the current
658 * BSSID to avoid keeping the current IBSS network alive in cases where
659 * other STAs are using different BSSID. */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100660 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200661 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
662 IEEE80211_IF_TYPE_IBSS);
Johannes Berg571ecf62007-07-27 15:43:22 +0200663 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
664 sta->last_rx = jiffies;
665 } else
666 if (!is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100667 rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200668 /* Update last_rx only for unicast frames in order to prevent
669 * the Probe Request frames (the only broadcast frames from a
670 * STA in infrastructure mode) from keeping a connection alive.
671 */
672 sta->last_rx = jiffies;
673 }
674
Jiri Slabybadffb72007-08-28 17:01:54 -0400675 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200676 return TXRX_CONTINUE;
677
678 sta->rx_fragments++;
679 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400680 sta->last_rssi = rx->u.rx.status->ssi;
681 sta->last_signal = rx->u.rx.status->signal;
682 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200683
684 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
685 /* Change STA power saving mode only in the end of a frame
686 * exchange sequence */
687 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
688 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
689 else if (!(sta->flags & WLAN_STA_PS) &&
690 (rx->fc & IEEE80211_FCTL_PM))
691 ap_sta_ps_start(dev, sta);
692 }
693
694 /* Drop data::nullfunc frames silently, since they are used only to
695 * control station power saving mode. */
696 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
697 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
698 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
699 /* Update counter and free packet here to avoid counting this
700 * as a dropped packed. */
701 sta->rx_packets++;
702 dev_kfree_skb(rx->skb);
703 return TXRX_QUEUED;
704 }
705
706 return TXRX_CONTINUE;
707} /* ieee80211_rx_h_sta_process */
708
Johannes Berg571ecf62007-07-27 15:43:22 +0200709static inline struct ieee80211_fragment_entry *
710ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
711 unsigned int frag, unsigned int seq, int rx_queue,
712 struct sk_buff **skb)
713{
714 struct ieee80211_fragment_entry *entry;
715 int idx;
716
717 idx = sdata->fragment_next;
718 entry = &sdata->fragments[sdata->fragment_next++];
719 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
720 sdata->fragment_next = 0;
721
722 if (!skb_queue_empty(&entry->skb_list)) {
723#ifdef CONFIG_MAC80211_DEBUG
724 struct ieee80211_hdr *hdr =
725 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700726 DECLARE_MAC_BUF(mac);
727 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200728 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
729 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700730 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200731 sdata->dev->name, idx,
732 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700733 entry->last_frag, print_mac(mac, hdr->addr1),
734 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200735#endif /* CONFIG_MAC80211_DEBUG */
736 __skb_queue_purge(&entry->skb_list);
737 }
738
739 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
740 *skb = NULL;
741 entry->first_frag_time = jiffies;
742 entry->seq = seq;
743 entry->rx_queue = rx_queue;
744 entry->last_frag = frag;
745 entry->ccmp = 0;
746 entry->extra_len = 0;
747
748 return entry;
749}
750
751static inline struct ieee80211_fragment_entry *
752ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
753 u16 fc, unsigned int frag, unsigned int seq,
754 int rx_queue, struct ieee80211_hdr *hdr)
755{
756 struct ieee80211_fragment_entry *entry;
757 int i, idx;
758
759 idx = sdata->fragment_next;
760 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
761 struct ieee80211_hdr *f_hdr;
762 u16 f_fc;
763
764 idx--;
765 if (idx < 0)
766 idx = IEEE80211_FRAGMENT_MAX - 1;
767
768 entry = &sdata->fragments[idx];
769 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
770 entry->rx_queue != rx_queue ||
771 entry->last_frag + 1 != frag)
772 continue;
773
774 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
775 f_fc = le16_to_cpu(f_hdr->frame_control);
776
777 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
778 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
779 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
780 continue;
781
782 if (entry->first_frag_time + 2 * HZ < jiffies) {
783 __skb_queue_purge(&entry->skb_list);
784 continue;
785 }
786 return entry;
787 }
788
789 return NULL;
790}
791
792static ieee80211_txrx_result
793ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
794{
795 struct ieee80211_hdr *hdr;
796 u16 sc;
797 unsigned int frag, seq;
798 struct ieee80211_fragment_entry *entry;
799 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700800 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200801
802 hdr = (struct ieee80211_hdr *) rx->skb->data;
803 sc = le16_to_cpu(hdr->seq_ctrl);
804 frag = sc & IEEE80211_SCTL_FRAG;
805
806 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
807 (rx->skb)->len < 24 ||
808 is_multicast_ether_addr(hdr->addr1))) {
809 /* not fragmented */
810 goto out;
811 }
812 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
813
814 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
815
816 if (frag == 0) {
817 /* This is the first fragment of a new frame. */
818 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
819 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400820 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200821 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
822 /* Store CCMP PN so that we can verify that the next
823 * fragment has a sequential PN value. */
824 entry->ccmp = 1;
825 memcpy(entry->last_pn,
826 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
827 CCMP_PN_LEN);
828 }
829 return TXRX_QUEUED;
830 }
831
832 /* This is a fragment for a frame that should already be pending in
833 * fragment cache. Add this fragment to the end of the pending entry.
834 */
835 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
836 rx->u.rx.queue, hdr);
837 if (!entry) {
838 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
839 return TXRX_DROP;
840 }
841
842 /* Verify that MPDUs within one MSDU have sequential PN values.
843 * (IEEE 802.11i, 8.3.3.4.5) */
844 if (entry->ccmp) {
845 int i;
846 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400847 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200848 return TXRX_DROP;
849 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
850 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
851 pn[i]++;
852 if (pn[i])
853 break;
854 }
855 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
856 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400857 if (net_ratelimit())
858 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700859 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400860 " PN=%02x%02x%02x%02x%02x%02x "
861 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700862 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400863 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
864 rpn[5], pn[0], pn[1], pn[2], pn[3],
865 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200866 return TXRX_DROP;
867 }
868 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
869 }
870
871 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
872 __skb_queue_tail(&entry->skb_list, rx->skb);
873 entry->last_frag = frag;
874 entry->extra_len += rx->skb->len;
875 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
876 rx->skb = NULL;
877 return TXRX_QUEUED;
878 }
879
880 rx->skb = __skb_dequeue(&entry->skb_list);
881 if (skb_tailroom(rx->skb) < entry->extra_len) {
882 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
883 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
884 GFP_ATOMIC))) {
885 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
886 __skb_queue_purge(&entry->skb_list);
887 return TXRX_DROP;
888 }
889 }
890 while ((skb = __skb_dequeue(&entry->skb_list))) {
891 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
892 dev_kfree_skb(skb);
893 }
894
895 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400896 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200897
898 out:
899 if (rx->sta)
900 rx->sta->rx_packets++;
901 if (is_multicast_ether_addr(hdr->addr1))
902 rx->local->dot11MulticastReceivedFrameCount++;
903 else
904 ieee80211_led_rx(rx->local);
905 return TXRX_CONTINUE;
906}
907
908static ieee80211_txrx_result
909ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
910{
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200911 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg571ecf62007-07-27 15:43:22 +0200912 struct sk_buff *skb;
913 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700914 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200915
916 if (likely(!rx->sta ||
917 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
918 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400919 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200920 return TXRX_CONTINUE;
921
Johannes Berg51fb61e2007-12-19 01:31:27 +0100922 if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
923 (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200924 return TXRX_DROP;
925
Johannes Berg571ecf62007-07-27 15:43:22 +0200926 skb = skb_dequeue(&rx->sta->tx_filtered);
927 if (!skb) {
928 skb = skb_dequeue(&rx->sta->ps_tx_buf);
929 if (skb)
930 rx->local->total_ps_buffered--;
931 }
932 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
933 skb_queue_empty(&rx->sta->ps_tx_buf);
934
935 if (skb) {
936 struct ieee80211_hdr *hdr =
937 (struct ieee80211_hdr *) skb->data;
938
939 /* tell TX path to send one frame even though the STA may
940 * still remain is PS mode after this frame exchange */
941 rx->sta->pspoll = 1;
942
943#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700944 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
945 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200946 skb_queue_len(&rx->sta->ps_tx_buf));
947#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
948
949 /* Use MoreData flag to indicate whether there are more
950 * buffered frames for this STA */
951 if (no_pending_pkts) {
952 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
953 rx->sta->flags &= ~WLAN_STA_TIM;
954 } else
955 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
956
957 dev_queue_xmit(skb);
958
959 if (no_pending_pkts) {
960 if (rx->local->ops->set_tim)
961 rx->local->ops->set_tim(local_to_hw(rx->local),
962 rx->sta->aid, 0);
963 if (rx->sdata->bss)
964 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
965 }
966#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
967 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700968 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200969 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700970 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200971#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
972
973 }
974
975 /* Free PS Poll skb here instead of returning TXRX_DROP that would
976 * count as an dropped frame. */
977 dev_kfree_skb(rx->skb);
978
979 return TXRX_QUEUED;
980}
981
982static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200983ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
984{
985 u16 fc = rx->fc;
986 u8 *data = rx->skb->data;
987 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
988
989 if (!WLAN_FC_IS_QOS_DATA(fc))
990 return TXRX_CONTINUE;
991
992 /* remove the qos control field, update frame type and meta-data */
993 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
994 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
995 /* change frame type to non QOS */
996 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
997 hdr->frame_control = cpu_to_le16(fc);
998
999 return TXRX_CONTINUE;
1000}
1001
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001002static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001003ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001004{
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001005 if (unlikely(rx->sdata->ieee802_1x_pac &&
1006 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001007#ifdef CONFIG_MAC80211_DEBUG
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001008 printk(KERN_DEBUG "%s: dropped frame "
1009 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001010#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001011 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001012 }
1013
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001014 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001015}
1016
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001017static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001018ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001019{
Johannes Berg3017b802007-08-28 17:01:53 -04001020 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001021 * Pass through unencrypted frames if the hardware has
1022 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001023 */
Johannes Berg7848ba72007-09-14 11:10:25 -04001024 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001025 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001026
1027 /* Drop unencrypted frames if key is set. */
1028 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1029 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1030 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001031 (rx->key || rx->sdata->drop_unencrypted))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001032 if (net_ratelimit())
1033 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1034 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001035 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001036 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001037 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001038}
1039
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001040static int
1041ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001042{
1043 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001044 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1045 u16 fc, hdrlen, ethertype;
1046 u8 *payload;
1047 u8 dst[ETH_ALEN];
1048 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001049 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001050 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001051 DECLARE_MAC_BUF(mac);
1052 DECLARE_MAC_BUF(mac2);
1053 DECLARE_MAC_BUF(mac3);
1054 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001055
1056 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001057
1058 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001059 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001060
1061 hdrlen = ieee80211_get_hdrlen(fc);
1062
1063 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1064 * header
1065 * IEEE 802.11 address fields:
1066 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1067 * 0 0 DA SA BSSID n/a
1068 * 0 1 DA BSSID SA n/a
1069 * 1 0 BSSID SA DA n/a
1070 * 1 1 RA TA DA SA
1071 */
1072
1073 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1074 case IEEE80211_FCTL_TODS:
1075 /* BSSID SA DA */
1076 memcpy(dst, hdr->addr3, ETH_ALEN);
1077 memcpy(src, hdr->addr2, ETH_ALEN);
1078
Johannes Berg51fb61e2007-12-19 01:31:27 +01001079 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
1080 sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001081 if (net_ratelimit())
1082 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001083 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001084 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001085 print_mac(mac, hdr->addr1),
1086 print_mac(mac2, hdr->addr2),
1087 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001088 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001089 }
1090 break;
1091 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1092 /* RA TA DA SA */
1093 memcpy(dst, hdr->addr3, ETH_ALEN);
1094 memcpy(src, hdr->addr4, ETH_ALEN);
1095
Johannes Berg51fb61e2007-12-19 01:31:27 +01001096 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001097 if (net_ratelimit())
1098 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001099 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001100 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001101 print_mac(mac, hdr->addr1),
1102 print_mac(mac2, hdr->addr2),
1103 print_mac(mac3, hdr->addr3),
1104 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001105 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001106 }
1107 break;
1108 case IEEE80211_FCTL_FROMDS:
1109 /* DA BSSID SA */
1110 memcpy(dst, hdr->addr1, ETH_ALEN);
1111 memcpy(src, hdr->addr3, ETH_ALEN);
1112
Johannes Berg51fb61e2007-12-19 01:31:27 +01001113 if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
John W. Linvilleb3316152007-08-28 17:01:55 -04001114 (is_multicast_ether_addr(dst) &&
1115 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001116 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001117 break;
1118 case 0:
1119 /* DA SA BSSID */
1120 memcpy(dst, hdr->addr1, ETH_ALEN);
1121 memcpy(src, hdr->addr2, ETH_ALEN);
1122
Johannes Berg51fb61e2007-12-19 01:31:27 +01001123 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001124 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001125 printk(KERN_DEBUG "%s: dropped IBSS frame "
1126 "(DA=%s SA=%s BSSID=%s)\n",
1127 dev->name,
1128 print_mac(mac, hdr->addr1),
1129 print_mac(mac2, hdr->addr2),
1130 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001131 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001132 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001133 }
1134 break;
1135 }
1136
Johannes Berg571ecf62007-07-27 15:43:22 +02001137 if (unlikely(skb->len - hdrlen < 8)) {
1138 if (net_ratelimit()) {
1139 printk(KERN_DEBUG "%s: RX too short data frame "
1140 "payload\n", dev->name);
1141 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001142 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001143 }
1144
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001145 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001146 ethertype = (payload[6] << 8) | payload[7];
1147
1148 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1149 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1150 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1151 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1152 * replace EtherType */
1153 skb_pull(skb, hdrlen + 6);
1154 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1155 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1156 } else {
1157 struct ethhdr *ehdr;
1158 __be16 len;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001159
Johannes Berg571ecf62007-07-27 15:43:22 +02001160 skb_pull(skb, hdrlen);
1161 len = htons(skb->len);
1162 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1163 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1164 memcpy(ehdr->h_source, src, ETH_ALEN);
1165 ehdr->h_proto = len;
1166 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001167 return 0;
1168}
Johannes Berg571ecf62007-07-27 15:43:22 +02001169
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001170/*
1171 * requires that rx->skb is a frame with ethernet header
1172 */
1173static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1174{
1175 static const u8 pae_group_addr[ETH_ALEN]
1176 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1177 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1178
1179 /*
1180 * Allow EAPOL frames to us/the PAE group address regardless
1181 * of whether the frame was encrypted or not.
1182 */
1183 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1184 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1185 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1186 return true;
1187
1188 if (ieee80211_802_1x_port_control(rx) ||
1189 ieee80211_drop_unencrypted(rx))
1190 return false;
1191
1192 return true;
1193}
1194
1195/*
1196 * requires that rx->skb is a frame with ethernet header
1197 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001198static void
1199ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1200{
1201 struct net_device *dev = rx->dev;
1202 struct ieee80211_local *local = rx->local;
1203 struct sk_buff *skb, *xmit_skb;
1204 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001205 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1206 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001207
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001208 skb = rx->skb;
1209 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001210
Johannes Berg51fb61e2007-12-19 01:31:27 +01001211 if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
1212 sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
Jiri Slabybadffb72007-08-28 17:01:54 -04001213 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001214 if (is_multicast_ether_addr(ehdr->h_dest)) {
1215 /*
1216 * send multicast frames both to higher layers in
1217 * local net stack and back to the wireless medium
1218 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001219 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1220 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001221 printk(KERN_DEBUG "%s: failed to clone "
1222 "multicast frame\n", dev->name);
1223 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001224 dsta = sta_info_get(local, skb->data);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001225 if (dsta && dsta->dev == dev) {
1226 /*
1227 * The destination station is associated to
1228 * this AP (in this VLAN), so send the frame
1229 * directly to it and do not pass it to local
1230 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001231 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001232 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001233 skb = NULL;
1234 }
1235 if (dsta)
1236 sta_info_put(dsta);
1237 }
1238 }
1239
1240 if (skb) {
1241 /* deliver to local stack */
1242 skb->protocol = eth_type_trans(skb, dev);
1243 memset(skb->cb, 0, sizeof(skb->cb));
1244 netif_rx(skb);
1245 }
1246
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001247 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001248 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001249 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001250 skb_reset_network_header(xmit_skb);
1251 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001252 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001253 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001254}
1255
1256static ieee80211_txrx_result
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001257ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1258{
1259 struct net_device *dev = rx->dev;
1260 struct ieee80211_local *local = rx->local;
1261 u16 fc, ethertype;
1262 u8 *payload;
1263 struct sk_buff *skb = rx->skb, *frame = NULL;
1264 const struct ethhdr *eth;
1265 int remaining, err;
1266 u8 dst[ETH_ALEN];
1267 u8 src[ETH_ALEN];
1268 DECLARE_MAC_BUF(mac);
1269
1270 fc = rx->fc;
1271 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1272 return TXRX_CONTINUE;
1273
1274 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1275 return TXRX_DROP;
1276
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +02001277 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001278 return TXRX_CONTINUE;
1279
1280 err = ieee80211_data_to_8023(rx);
1281 if (unlikely(err))
1282 return TXRX_DROP;
1283
1284 skb->dev = dev;
1285
1286 dev->stats.rx_packets++;
1287 dev->stats.rx_bytes += skb->len;
1288
1289 /* skip the wrapping header */
1290 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1291 if (!eth)
1292 return TXRX_DROP;
1293
1294 while (skb != frame) {
1295 u8 padding;
1296 __be16 len = eth->h_proto;
1297 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1298
1299 remaining = skb->len;
1300 memcpy(dst, eth->h_dest, ETH_ALEN);
1301 memcpy(src, eth->h_source, ETH_ALEN);
1302
1303 padding = ((4 - subframe_len) & 0x3);
1304 /* the last MSDU has no padding */
1305 if (subframe_len > remaining) {
1306 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1307 return TXRX_DROP;
1308 }
1309
1310 skb_pull(skb, sizeof(struct ethhdr));
1311 /* if last subframe reuse skb */
1312 if (remaining <= subframe_len + padding)
1313 frame = skb;
1314 else {
1315 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1316 subframe_len);
1317
1318 if (frame == NULL)
1319 return TXRX_DROP;
1320
1321 skb_reserve(frame, local->hw.extra_tx_headroom +
1322 sizeof(struct ethhdr));
1323 memcpy(skb_put(frame, ntohs(len)), skb->data,
1324 ntohs(len));
1325
1326 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1327 padding);
1328 if (!eth) {
1329 printk(KERN_DEBUG "%s: wrong buffer size ",
1330 dev->name);
1331 dev_kfree_skb(frame);
1332 return TXRX_DROP;
1333 }
1334 }
1335
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001336 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001337 frame->dev = dev;
1338 frame->priority = skb->priority;
1339 rx->skb = frame;
1340
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001341 payload = frame->data;
1342 ethertype = (payload[6] << 8) | payload[7];
1343
1344 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001345 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1346 compare_ether_addr(payload,
1347 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001348 /* remove RFC1042 or Bridge-Tunnel
1349 * encapsulation and replace EtherType */
1350 skb_pull(frame, 6);
1351 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1352 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1353 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001354 memcpy(skb_push(frame, sizeof(__be16)),
1355 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001356 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1357 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1358 }
1359
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001360 if (!ieee80211_frame_allowed(rx)) {
1361 if (skb == frame) /* last frame */
1362 return TXRX_DROP;
1363 dev_kfree_skb(frame);
1364 continue;
1365 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001366
1367 ieee80211_deliver_skb(rx);
1368 }
1369
1370 return TXRX_QUEUED;
1371}
1372
1373static ieee80211_txrx_result
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001374ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1375{
1376 struct net_device *dev = rx->dev;
1377 u16 fc;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001378 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001379
1380 fc = rx->fc;
1381 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1382 return TXRX_CONTINUE;
1383
1384 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1385 return TXRX_DROP;
1386
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001387 err = ieee80211_data_to_8023(rx);
1388 if (unlikely(err))
1389 return TXRX_DROP;
1390
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001391 if (!ieee80211_frame_allowed(rx))
1392 return TXRX_DROP;
1393
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001394 rx->skb->dev = dev;
1395
1396 dev->stats.rx_packets++;
1397 dev->stats.rx_bytes += rx->skb->len;
1398
1399 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001400
1401 return TXRX_QUEUED;
1402}
1403
1404static ieee80211_txrx_result
Ron Rindjunsky71364712007-12-25 17:00:36 +02001405ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
1406{
1407 struct ieee80211_local *local = rx->local;
1408 struct ieee80211_hw *hw = &local->hw;
1409 struct sk_buff *skb = rx->skb;
1410 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
1411 struct tid_ampdu_rx *tid_agg_rx;
1412 u16 start_seq_num;
1413 u16 tid;
1414
1415 if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
1416 return TXRX_CONTINUE;
1417
1418 if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
1419 if (!rx->sta)
1420 return TXRX_CONTINUE;
1421 tid = le16_to_cpu(bar->control) >> 12;
1422 tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
1423 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1424 return TXRX_CONTINUE;
1425
1426 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1427
1428 /* reset session timer */
1429 if (tid_agg_rx->timeout) {
1430 unsigned long expires =
1431 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1432 mod_timer(&tid_agg_rx->session_timer, expires);
1433 }
1434
1435 /* manage reordering buffer according to requested */
1436 /* sequence number */
1437 rcu_read_lock();
1438 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1439 start_seq_num, 1);
1440 rcu_read_unlock();
1441 return TXRX_DROP;
1442 }
1443
1444 return TXRX_CONTINUE;
1445}
1446
1447static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +02001448ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1449{
1450 struct ieee80211_sub_if_data *sdata;
1451
Jiri Slabybadffb72007-08-28 17:01:54 -04001452 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001453 return TXRX_DROP;
1454
1455 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001456 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
1457 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001458 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg571ecf62007-07-27 15:43:22 +02001459 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001460 else
1461 return TXRX_DROP;
1462
Johannes Berg571ecf62007-07-27 15:43:22 +02001463 return TXRX_QUEUED;
1464}
1465
1466static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1467 struct ieee80211_local *local,
1468 ieee80211_rx_handler *handlers,
1469 struct ieee80211_txrx_data *rx,
1470 struct sta_info *sta)
1471{
1472 ieee80211_rx_handler *handler;
1473 ieee80211_txrx_result res = TXRX_DROP;
1474
1475 for (handler = handlers; *handler != NULL; handler++) {
1476 res = (*handler)(rx);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001477
1478 switch (res) {
1479 case TXRX_CONTINUE:
1480 continue;
1481 case TXRX_DROP:
1482 I802_DEBUG_INC(local->rx_handlers_drop);
1483 if (sta)
1484 sta->rx_dropped++;
1485 break;
1486 case TXRX_QUEUED:
1487 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001488 break;
1489 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001490 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001491 }
1492
Johannes Berg8e6f0032007-07-27 15:43:22 +02001493 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001494 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001495 return res;
1496}
1497
1498static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1499 ieee80211_rx_handler *handlers,
1500 struct ieee80211_txrx_data *rx,
1501 struct sta_info *sta)
1502{
1503 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1504 TXRX_CONTINUE)
1505 dev_kfree_skb(rx->skb);
1506}
1507
1508static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1509 struct ieee80211_hdr *hdr,
1510 struct sta_info *sta,
1511 struct ieee80211_txrx_data *rx)
1512{
1513 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001514 DECLARE_MAC_BUF(mac);
1515 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001516
1517 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1518 if (rx->skb->len >= hdrlen + 4)
1519 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1520 else
1521 keyidx = -1;
1522
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001523 if (net_ratelimit())
1524 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001525 "failure from %s to %s keyidx=%d\n",
1526 dev->name, print_mac(mac, hdr->addr2),
1527 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001528
1529 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001530 /*
1531 * Some hardware seem to generate incorrect Michael MIC
1532 * reports; ignore them to avoid triggering countermeasures.
1533 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001534 if (net_ratelimit())
1535 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001536 "error for unknown address %s\n",
1537 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001538 goto ignore;
1539 }
1540
1541 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001542 if (net_ratelimit())
1543 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001544 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001545 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001546 goto ignore;
1547 }
1548
Johannes Berg51fb61e2007-12-19 01:31:27 +01001549 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001550 /*
1551 * APs with pairwise keys should never receive Michael MIC
1552 * errors for non-zero keyidx because these are reserved for
1553 * group keys and only the AP is sending real multicast
1554 * frames in the BSS.
1555 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001556 if (net_ratelimit())
1557 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1558 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001559 " (src %s)\n", dev->name, keyidx,
1560 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001561 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001562 }
1563
1564 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1565 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1566 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001567 if (net_ratelimit())
1568 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1569 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001570 "(fc=0x%04x) (src %s)\n",
1571 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001572 goto ignore;
1573 }
1574
Johannes Bergeb063c12007-08-28 17:01:53 -04001575 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001576 ignore:
1577 dev_kfree_skb(rx->skb);
1578 rx->skb = NULL;
1579}
1580
1581ieee80211_rx_handler ieee80211_rx_handlers[] =
1582{
1583 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001584 ieee80211_rx_h_passive_scan,
1585 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001586 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001587 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001588 ieee80211_rx_h_defragment,
1589 ieee80211_rx_h_ps_poll,
1590 ieee80211_rx_h_michael_mic_verify,
1591 /* this must be after decryption - so header is counted in MPDU mic
1592 * must be before pae and data, so QOS_DATA format frames
1593 * are not passed to user space by these functions
1594 */
1595 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001596 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001597 ieee80211_rx_h_data,
Ron Rindjunsky71364712007-12-25 17:00:36 +02001598 ieee80211_rx_h_ctrl,
Johannes Berg571ecf62007-07-27 15:43:22 +02001599 ieee80211_rx_h_mgmt,
1600 NULL
1601};
1602
1603/* main receive path */
1604
Johannes Berg23a24de2007-07-27 15:43:22 +02001605static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1606 u8 *bssid, struct ieee80211_txrx_data *rx,
1607 struct ieee80211_hdr *hdr)
1608{
1609 int multicast = is_multicast_ether_addr(hdr->addr1);
1610
Johannes Berg51fb61e2007-12-19 01:31:27 +01001611 switch (sdata->vif.type) {
Johannes Berg23a24de2007-07-27 15:43:22 +02001612 case IEEE80211_IF_TYPE_STA:
1613 if (!bssid)
1614 return 0;
1615 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001616 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001617 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001618 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001619 } else if (!multicast &&
1620 compare_ether_addr(sdata->dev->dev_addr,
1621 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001622 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001623 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001624 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001625 }
1626 break;
1627 case IEEE80211_IF_TYPE_IBSS:
1628 if (!bssid)
1629 return 0;
1630 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001631 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001632 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001633 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001634 } else if (!multicast &&
1635 compare_ether_addr(sdata->dev->dev_addr,
1636 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001637 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001638 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001639 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001640 } else if (!rx->sta)
1641 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1642 bssid, hdr->addr2);
1643 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001644 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001645 case IEEE80211_IF_TYPE_AP:
1646 if (!bssid) {
1647 if (compare_ether_addr(sdata->dev->dev_addr,
1648 hdr->addr1))
1649 return 0;
1650 } else if (!ieee80211_bssid_match(bssid,
1651 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001652 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001653 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001654 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001655 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001656 if (sdata->dev == sdata->local->mdev &&
1657 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001658 /* do not receive anything via
1659 * master device when not scanning */
1660 return 0;
1661 break;
1662 case IEEE80211_IF_TYPE_WDS:
1663 if (bssid ||
1664 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1665 return 0;
1666 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1667 return 0;
1668 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001669 case IEEE80211_IF_TYPE_MNTR:
1670 /* take everything */
1671 break;
Johannes Berga2897552007-09-28 14:01:25 +02001672 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001673 /* should never get here */
1674 WARN_ON(1);
1675 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001676 }
1677
1678 return 1;
1679}
1680
Johannes Berg571ecf62007-07-27 15:43:22 +02001681/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001682 * This is the actual Rx frames handler. as it blongs to Rx path it must
1683 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02001684 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001685static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
1686 struct sk_buff *skb,
1687 struct ieee80211_rx_status *status,
1688 u32 load)
Johannes Berg571ecf62007-07-27 15:43:22 +02001689{
1690 struct ieee80211_local *local = hw_to_local(hw);
1691 struct ieee80211_sub_if_data *sdata;
1692 struct sta_info *sta;
1693 struct ieee80211_hdr *hdr;
1694 struct ieee80211_txrx_data rx;
1695 u16 type;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001696 int prepares;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001697 struct ieee80211_sub_if_data *prev = NULL;
1698 struct sk_buff *skb_new;
1699 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001700
1701 hdr = (struct ieee80211_hdr *) skb->data;
1702 memset(&rx, 0, sizeof(rx));
1703 rx.skb = skb;
1704 rx.local = local;
1705
1706 rx.u.rx.status = status;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001707 rx.u.rx.load = load;
Johannes Bergb2e77712007-09-26 15:19:39 +02001708 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001709 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001710
Johannes Bergb2e77712007-09-26 15:19:39 +02001711 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001712 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001713
Johannes Bergb2e77712007-09-26 15:19:39 +02001714 sta = rx.sta = sta_info_get(local, hdr->addr2);
1715 if (sta) {
1716 rx.dev = rx.sta->dev;
1717 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1718 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001719
Johannes Berg571ecf62007-07-27 15:43:22 +02001720 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1721 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1722 goto end;
1723 }
1724
Zhu Yiece8edd2007-11-22 10:53:21 +08001725 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001726 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001727
Johannes Berg38f37142008-01-29 17:07:43 +01001728 ieee80211_parse_qos(&rx);
1729 ieee80211_verify_ip_alignment(&rx);
1730
Johannes Berg571ecf62007-07-27 15:43:22 +02001731 skb = rx.skb;
1732
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001733 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001734 !atomic_read(&local->iff_promiscs) &&
1735 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001736 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001737 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001738 rx.sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001739 sta_info_put(sta);
1740 return;
1741 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001742
Johannes Berg79010422007-09-18 17:29:21 -04001743 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001744 if (!netif_running(sdata->dev))
1745 continue;
1746
Johannes Berg51fb61e2007-12-19 01:31:27 +01001747 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +02001748 continue;
1749
Johannes Berg51fb61e2007-12-19 01:31:27 +01001750 bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Bergb2e77712007-09-26 15:19:39 +02001751 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001752 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02001753 /* prepare_for_handlers can change sta */
1754 sta = rx.sta;
1755
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001756 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02001757 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001758
Johannes Berg340e11f2007-07-27 15:43:22 +02001759 /*
1760 * frame is destined for this interface, but if it's not
1761 * also for the previous one we handle that after the
1762 * loop to avoid copying the SKB once too much
1763 */
1764
1765 if (!prev) {
1766 prev = sdata;
1767 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001768 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001769
1770 /*
1771 * frame was destined for the previous interface
1772 * so invoke RX handlers for it
1773 */
1774
1775 skb_new = skb_copy(skb, GFP_ATOMIC);
1776 if (!skb_new) {
1777 if (net_ratelimit())
1778 printk(KERN_DEBUG "%s: failed to copy "
1779 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001780 wiphy_name(local->hw.wiphy),
1781 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001782 continue;
1783 }
Helmut Schaa69f817b2007-12-21 15:16:35 +01001784 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg340e11f2007-07-27 15:43:22 +02001785 rx.skb = skb_new;
1786 rx.dev = prev->dev;
1787 rx.sdata = prev;
1788 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1789 &rx, sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001790 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001791 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001792 if (prev) {
Helmut Schaa69f817b2007-12-21 15:16:35 +01001793 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001794 rx.skb = skb;
1795 rx.dev = prev->dev;
1796 rx.sdata = prev;
1797 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1798 &rx, sta);
1799 } else
1800 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001801
Johannes Berg8e6f0032007-07-27 15:43:22 +02001802 end:
Johannes Berg571ecf62007-07-27 15:43:22 +02001803 if (sta)
1804 sta_info_put(sta);
1805}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001806
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001807#define SEQ_MODULO 0x1000
1808#define SEQ_MASK 0xfff
1809
1810static inline int seq_less(u16 sq1, u16 sq2)
1811{
1812 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1813}
1814
1815static inline u16 seq_inc(u16 sq)
1816{
1817 return ((sq + 1) & SEQ_MASK);
1818}
1819
1820static inline u16 seq_sub(u16 sq1, u16 sq2)
1821{
1822 return ((sq1 - sq2) & SEQ_MASK);
1823}
1824
1825
Ron Rindjunsky71364712007-12-25 17:00:36 +02001826/*
1827 * As it function blongs to Rx path it must be called with
1828 * the proper rcu_read_lock protection for its flow.
1829 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001830u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
1831 struct tid_ampdu_rx *tid_agg_rx,
1832 struct sk_buff *skb, u16 mpdu_seq_num,
1833 int bar_req)
1834{
1835 struct ieee80211_local *local = hw_to_local(hw);
1836 struct ieee80211_rx_status status;
1837 u16 head_seq_num, buf_size;
1838 int index;
1839 u32 pkt_load;
1840
1841 buf_size = tid_agg_rx->buf_size;
1842 head_seq_num = tid_agg_rx->head_seq_num;
1843
1844 /* frame with out of date sequence number */
1845 if (seq_less(mpdu_seq_num, head_seq_num)) {
1846 dev_kfree_skb(skb);
1847 return 1;
1848 }
1849
1850 /* if frame sequence number exceeds our buffering window size or
1851 * block Ack Request arrived - release stored frames */
1852 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
1853 /* new head to the ordering buffer */
1854 if (bar_req)
1855 head_seq_num = mpdu_seq_num;
1856 else
1857 head_seq_num =
1858 seq_inc(seq_sub(mpdu_seq_num, buf_size));
1859 /* release stored frames up to new head to stack */
1860 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1861 index = seq_sub(tid_agg_rx->head_seq_num,
1862 tid_agg_rx->ssn)
1863 % tid_agg_rx->buf_size;
1864
1865 if (tid_agg_rx->reorder_buf[index]) {
1866 /* release the reordered frames to stack */
1867 memcpy(&status,
1868 tid_agg_rx->reorder_buf[index]->cb,
1869 sizeof(status));
1870 pkt_load = ieee80211_rx_load_stats(local,
1871 tid_agg_rx->reorder_buf[index],
1872 &status);
1873 __ieee80211_rx_handle_packet(hw,
1874 tid_agg_rx->reorder_buf[index],
1875 &status, pkt_load);
1876 tid_agg_rx->stored_mpdu_num--;
1877 tid_agg_rx->reorder_buf[index] = NULL;
1878 }
1879 tid_agg_rx->head_seq_num =
1880 seq_inc(tid_agg_rx->head_seq_num);
1881 }
1882 if (bar_req)
1883 return 1;
1884 }
1885
1886 /* now the new frame is always in the range of the reordering */
1887 /* buffer window */
1888 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
1889 % tid_agg_rx->buf_size;
1890 /* check if we already stored this frame */
1891 if (tid_agg_rx->reorder_buf[index]) {
1892 dev_kfree_skb(skb);
1893 return 1;
1894 }
1895
1896 /* if arrived mpdu is in the right order and nothing else stored */
1897 /* release it immediately */
1898 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1899 tid_agg_rx->stored_mpdu_num == 0) {
1900 tid_agg_rx->head_seq_num =
1901 seq_inc(tid_agg_rx->head_seq_num);
1902 return 0;
1903 }
1904
1905 /* put the frame in the reordering buffer */
1906 tid_agg_rx->reorder_buf[index] = skb;
1907 tid_agg_rx->stored_mpdu_num++;
1908 /* release the buffer until next missing frame */
1909 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
1910 % tid_agg_rx->buf_size;
1911 while (tid_agg_rx->reorder_buf[index]) {
1912 /* release the reordered frame back to stack */
1913 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
1914 sizeof(status));
1915 pkt_load = ieee80211_rx_load_stats(local,
1916 tid_agg_rx->reorder_buf[index],
1917 &status);
1918 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
1919 &status, pkt_load);
1920 tid_agg_rx->stored_mpdu_num--;
1921 tid_agg_rx->reorder_buf[index] = NULL;
1922 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
1923 index = seq_sub(tid_agg_rx->head_seq_num,
1924 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
1925 }
1926 return 1;
1927}
1928
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001929static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
1930 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001931{
1932 struct ieee80211_hw *hw = &local->hw;
1933 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1934 struct sta_info *sta;
1935 struct tid_ampdu_rx *tid_agg_rx;
1936 u16 fc, sc;
1937 u16 mpdu_seq_num;
1938 u8 ret = 0, *qc;
1939 int tid;
1940
1941 sta = sta_info_get(local, hdr->addr2);
1942 if (!sta)
1943 return ret;
1944
1945 fc = le16_to_cpu(hdr->frame_control);
1946
1947 /* filter the QoS data rx stream according to
1948 * STA/TID and check if this STA/TID is on aggregation */
1949 if (!WLAN_FC_IS_QOS_DATA(fc))
1950 goto end_reorder;
1951
1952 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
1953 tid = qc[0] & QOS_CONTROL_TID_MASK;
1954 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
1955
1956 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1957 goto end_reorder;
1958
1959 /* null data frames are excluded */
Ron Rindjunsky8b6bbe72008-01-23 18:17:13 +02001960 if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001961 goto end_reorder;
1962
1963 /* new un-ordered ampdu frame - process it */
1964
1965 /* reset session timer */
1966 if (tid_agg_rx->timeout) {
1967 unsigned long expires =
1968 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1969 mod_timer(&tid_agg_rx->session_timer, expires);
1970 }
1971
1972 /* if this mpdu is fragmented - terminate rx aggregation session */
1973 sc = le16_to_cpu(hdr->seq_ctrl);
1974 if (sc & IEEE80211_SCTL_FRAG) {
1975 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
1976 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
1977 ret = 1;
1978 goto end_reorder;
1979 }
1980
1981 /* according to mpdu sequence number deal with reordering buffer */
1982 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1983 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
1984 mpdu_seq_num, 0);
1985end_reorder:
1986 if (sta)
1987 sta_info_put(sta);
1988 return ret;
1989}
1990
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001991/*
1992 * This is the receive path handler. It is called by a low level driver when an
1993 * 802.11 MPDU is received from the hardware.
1994 */
1995void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1996 struct ieee80211_rx_status *status)
1997{
1998 struct ieee80211_local *local = hw_to_local(hw);
1999 u32 pkt_load;
2000
2001 /*
2002 * key references and virtual interfaces are protected using RCU
2003 * and this requires that we are in a read-side RCU section during
2004 * receive processing
2005 */
2006 rcu_read_lock();
2007
2008 /*
2009 * Frames with failed FCS/PLCP checksum are not returned,
2010 * all other frames are returned without radiotap header
2011 * if it was previously present.
2012 * Also, frames with less than 16 bytes are dropped.
2013 */
2014 skb = ieee80211_rx_monitor(local, skb, status);
2015 if (!skb) {
2016 rcu_read_unlock();
2017 return;
2018 }
2019
2020 pkt_load = ieee80211_rx_load_stats(local, skb, status);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002021 local->channel_use_raw += pkt_load;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002022
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002023 if (!ieee80211_rx_reorder_ampdu(local, skb))
2024 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002025
2026 rcu_read_unlock();
2027}
Johannes Berg571ecf62007-07-27 15:43:22 +02002028EXPORT_SYMBOL(__ieee80211_rx);
2029
2030/* This is a version of the rx handler that can be called from hard irq
2031 * context. Post the skb on the queue and schedule the tasklet */
2032void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
2033 struct ieee80211_rx_status *status)
2034{
2035 struct ieee80211_local *local = hw_to_local(hw);
2036
2037 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2038
2039 skb->dev = local->mdev;
2040 /* copy status into skb->cb for use by tasklet */
2041 memcpy(skb->cb, status, sizeof(*status));
2042 skb->pkt_type = IEEE80211_RX_MSG;
2043 skb_queue_tail(&local->skb_queue, skb);
2044 tasklet_schedule(&local->tasklet);
2045}
2046EXPORT_SYMBOL(ieee80211_rx_irqsafe);