blob: e7b1eb8da25c1099c9b696abd127a285060994a0 [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
Johannes Bergb2e77712007-09-26 15:19:39 +020027/*
28 * monitor mode reception
29 *
30 * This function cleans up the SKB, i.e. it removes all the stuff
31 * only useful for monitoring.
32 */
33static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
34 struct sk_buff *skb,
35 int rtap_len)
36{
37 skb_pull(skb, rtap_len);
38
39 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
40 if (likely(skb->len > FCS_LEN))
41 skb_trim(skb, skb->len - FCS_LEN);
42 else {
43 /* driver bug */
44 WARN_ON(1);
45 dev_kfree_skb(skb);
46 skb = NULL;
47 }
48 }
49
50 return skb;
51}
52
53static inline int should_drop_frame(struct ieee80211_rx_status *status,
54 struct sk_buff *skb,
55 int present_fcs_len,
56 int radiotap_len)
57{
58 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
59
60 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
61 return 1;
62 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
63 return 1;
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020064 if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
65 cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
66 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
67 cpu_to_le16(IEEE80211_STYPE_PSPOLL)))
Johannes Bergb2e77712007-09-26 15:19:39 +020068 return 1;
69 return 0;
70}
71
72/*
73 * This function copies a received frame to all monitor interfaces and
74 * returns a cleaned-up SKB that no longer includes the FCS nor the
75 * radiotap header the driver might have added.
76 */
77static struct sk_buff *
78ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
79 struct ieee80211_rx_status *status)
80{
81 struct ieee80211_sub_if_data *sdata;
82 struct ieee80211_rate *rate;
83 int needed_headroom = 0;
Johannes Bergc49e5ea2007-12-11 21:33:42 +010084 struct ieee80211_radiotap_header *rthdr;
85 __le64 *rttsft = NULL;
86 struct ieee80211_rtap_fixed_data {
Johannes Bergb2e77712007-09-26 15:19:39 +020087 u8 flags;
88 u8 rate;
89 __le16 chan_freq;
90 __le16 chan_flags;
91 u8 antsignal;
92 u8 padding_for_rxflags;
93 __le16 rx_flags;
Johannes Bergc49e5ea2007-12-11 21:33:42 +010094 } __attribute__ ((packed)) *rtfixed;
Johannes Bergb2e77712007-09-26 15:19:39 +020095 struct sk_buff *skb, *skb2;
96 struct net_device *prev_dev = NULL;
97 int present_fcs_len = 0;
98 int rtap_len = 0;
99
100 /*
101 * First, we may need to make a copy of the skb because
102 * (1) we need to modify it for radiotap (if not present), and
103 * (2) the other RX handlers will modify the skb we got.
104 *
105 * We don't need to, of course, if we aren't going to return
106 * the SKB because it has a bad FCS/PLCP checksum.
107 */
108 if (status->flag & RX_FLAG_RADIOTAP)
109 rtap_len = ieee80211_get_radiotap_len(origskb->data);
110 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100111 /* room for radiotap header, always present fields and TSFT */
112 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
Johannes Bergb2e77712007-09-26 15:19:39 +0200113
114 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
115 present_fcs_len = FCS_LEN;
116
117 if (!local->monitors) {
118 if (should_drop_frame(status, origskb, present_fcs_len,
119 rtap_len)) {
120 dev_kfree_skb(origskb);
121 return NULL;
122 }
123
124 return remove_monitor_info(local, origskb, rtap_len);
125 }
126
127 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
128 /* only need to expand headroom if necessary */
129 skb = origskb;
130 origskb = NULL;
131
132 /*
133 * This shouldn't trigger often because most devices have an
134 * RX header they pull before we get here, and that should
135 * be big enough for our radiotap information. We should
136 * probably export the length to drivers so that we can have
137 * them allocate enough headroom to start with.
138 */
139 if (skb_headroom(skb) < needed_headroom &&
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100140 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
Johannes Bergb2e77712007-09-26 15:19:39 +0200141 dev_kfree_skb(skb);
142 return NULL;
143 }
144 } else {
145 /*
146 * Need to make a copy and possibly remove radiotap header
147 * and FCS from the original.
148 */
149 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
150
151 origskb = remove_monitor_info(local, origskb, rtap_len);
152
153 if (!skb)
154 return origskb;
155 }
156
157 /* if necessary, prepend radiotap information */
158 if (!(status->flag & RX_FLAG_RADIOTAP)) {
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100159 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
160 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
161 if (status->flag & RX_FLAG_TSFT) {
162 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
163 rtap_len += 8;
164 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200165 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
166 memset(rthdr, 0, sizeof(*rthdr));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100167 memset(rtfixed, 0, sizeof(*rtfixed));
168 rthdr->it_present =
Johannes Bergb2e77712007-09-26 15:19:39 +0200169 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
170 (1 << IEEE80211_RADIOTAP_RATE) |
171 (1 << IEEE80211_RADIOTAP_CHANNEL) |
172 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
173 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100174 rtfixed->flags = 0;
175 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
176 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
177
178 if (rttsft) {
179 *rttsft = cpu_to_le64(status->mactime);
180 rthdr->it_present |=
181 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
182 }
Johannes Bergb2e77712007-09-26 15:19:39 +0200183
184 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100185 rtfixed->rx_flags = 0;
Johannes Bergb2e77712007-09-26 15:19:39 +0200186 if (status->flag &
187 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100188 rtfixed->rx_flags |=
Johannes Bergb2e77712007-09-26 15:19:39 +0200189 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
190
191 rate = ieee80211_get_rate(local, status->phymode,
192 status->rate);
193 if (rate)
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100194 rtfixed->rate = rate->rate / 5;
Johannes Bergb2e77712007-09-26 15:19:39 +0200195
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100196 rtfixed->chan_freq = cpu_to_le16(status->freq);
Johannes Bergb2e77712007-09-26 15:19:39 +0200197
198 if (status->phymode == MODE_IEEE80211A)
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100199 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200200 cpu_to_le16(IEEE80211_CHAN_OFDM |
201 IEEE80211_CHAN_5GHZ);
202 else
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100203 rtfixed->chan_flags =
Johannes Bergb2e77712007-09-26 15:19:39 +0200204 cpu_to_le16(IEEE80211_CHAN_DYN |
205 IEEE80211_CHAN_2GHZ);
206
Johannes Bergc49e5ea2007-12-11 21:33:42 +0100207 rtfixed->antsignal = status->ssi;
208 rthdr->it_len = cpu_to_le16(rtap_len);
Johannes Bergb2e77712007-09-26 15:19:39 +0200209 }
210
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100211 skb_reset_mac_header(skb);
Johannes Bergb2e77712007-09-26 15:19:39 +0200212 skb->ip_summed = CHECKSUM_UNNECESSARY;
213 skb->pkt_type = PACKET_OTHERHOST;
214 skb->protocol = htons(ETH_P_802_2);
215
216 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
217 if (!netif_running(sdata->dev))
218 continue;
219
220 if (sdata->type != IEEE80211_IF_TYPE_MNTR)
221 continue;
222
223 if (prev_dev) {
224 skb2 = skb_clone(skb, GFP_ATOMIC);
225 if (skb2) {
226 skb2->dev = prev_dev;
227 netif_rx(skb2);
228 }
229 }
230
231 prev_dev = sdata->dev;
232 sdata->dev->stats.rx_packets++;
233 sdata->dev->stats.rx_bytes += skb->len;
234 }
235
236 if (prev_dev) {
237 skb->dev = prev_dev;
238 netif_rx(skb);
239 } else
240 dev_kfree_skb(skb);
241
242 return origskb;
243}
244
245
Johannes Berg571ecf62007-07-27 15:43:22 +0200246/* pre-rx handlers
247 *
248 * these don't have dev/sdata fields in the rx data
Johannes Berg52865df2007-07-27 15:43:22 +0200249 * The sta value should also not be used because it may
250 * be NULL even though a STA (in IBSS mode) will be added.
Johannes Berg571ecf62007-07-27 15:43:22 +0200251 */
252
253static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200254ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
255{
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;
287
288 return TXRX_CONTINUE;
289}
290
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200291
292u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
293 struct sk_buff *skb,
294 struct ieee80211_rx_status *status)
Johannes Berg571ecf62007-07-27 15:43:22 +0200295{
Johannes Berg571ecf62007-07-27 15:43:22 +0200296 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
297 u32 load = 0, hdrtime;
298 struct ieee80211_rate *rate;
299 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
300 int i;
301
302 /* Estimate total channel use caused by this frame */
303
304 if (unlikely(mode->num_rates < 0))
305 return TXRX_CONTINUE;
306
307 rate = &mode->rates[0];
308 for (i = 0; i < mode->num_rates; i++) {
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200309 if (mode->rates[i].val == status->rate) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200310 rate = &mode->rates[i];
311 break;
312 }
313 }
314
315 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
316 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
317
318 if (mode->mode == MODE_IEEE80211A ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200319 (mode->mode == MODE_IEEE80211G &&
320 rate->flags & IEEE80211_RATE_ERP))
321 hdrtime = CHAN_UTIL_HDR_SHORT;
322 else
323 hdrtime = CHAN_UTIL_HDR_LONG;
324
325 load = hdrtime;
326 if (!is_multicast_ether_addr(hdr->addr1))
327 load += hdrtime;
328
329 load += skb->len * rate->rate_inv;
330
331 /* Divide channel_use by 8 to avoid wrapping around the counter */
332 load >>= CHAN_UTIL_SHIFT;
Johannes Berg571ecf62007-07-27 15:43:22 +0200333
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200334 return load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200335}
336
337ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
338{
339 ieee80211_rx_h_parse_qos,
Johannes Berg571ecf62007-07-27 15:43:22 +0200340 NULL
341};
342
343/* rx handlers */
344
345static ieee80211_txrx_result
346ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
347{
Johannes Berg52865df2007-07-27 15:43:22 +0200348 if (rx->sta)
349 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200350 rx->sdata->channel_use_raw += rx->u.rx.load;
351 return TXRX_CONTINUE;
352}
353
Johannes Berg571ecf62007-07-27 15:43:22 +0200354static ieee80211_txrx_result
355ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
356{
357 struct ieee80211_local *local = rx->local;
358 struct sk_buff *skb = rx->skb;
359
Zhu Yiece8edd2007-11-22 10:53:21 +0800360 if (unlikely(local->sta_hw_scanning))
361 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
362
363 if (unlikely(local->sta_sw_scanning)) {
364 /* drop all the other packets during a software scan anyway */
365 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
366 != TXRX_QUEUED)
367 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +0200368 return TXRX_QUEUED;
369 }
370
Jiri Slabybadffb72007-08-28 17:01:54 -0400371 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200372 /* scanning finished during invoking of handlers */
373 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
374 return TXRX_DROP;
375 }
376
377 return TXRX_CONTINUE;
378}
379
380static ieee80211_txrx_result
381ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
382{
383 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200384 hdr = (struct ieee80211_hdr *) rx->skb->data;
385
386 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
387 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
388 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
389 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
390 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400391 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200392 rx->local->dot11FrameDuplicateCount++;
393 rx->sta->num_duplicates++;
394 }
395 return TXRX_DROP;
396 } else
397 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
398 }
399
Johannes Berg571ecf62007-07-27 15:43:22 +0200400 if (unlikely(rx->skb->len < 16)) {
401 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
402 return TXRX_DROP;
403 }
404
Johannes Berg571ecf62007-07-27 15:43:22 +0200405 /* Drop disallowed frame classes based on STA auth/assoc state;
406 * IEEE 802.11, Chap 5.5.
407 *
408 * 80211.o does filtering only based on association state, i.e., it
409 * drops Class 3 frames from not associated stations. hostapd sends
410 * deauth/disassoc frames when needed. In addition, hostapd is
411 * responsible for filtering on both auth and assoc states.
412 */
413 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
414 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
415 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
416 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
417 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
418 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
419 !(rx->fc & IEEE80211_FCTL_TODS) &&
420 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400421 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200422 /* Drop IBSS frames and frames for other hosts
423 * silently. */
424 return TXRX_DROP;
425 }
426
Johannes Bergf9d540e2007-09-28 14:02:09 +0200427 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +0200428 }
429
Johannes Berg570bd532007-07-27 15:43:22 +0200430 return TXRX_CONTINUE;
431}
432
433
434static ieee80211_txrx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200435ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200436{
437 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400438 int keyidx;
439 int hdrlen;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200440 ieee80211_txrx_result result = TXRX_DROP;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400441 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200442
Johannes Berg3017b802007-08-28 17:01:53 -0400443 /*
444 * Key selection 101
445 *
446 * There are three types of keys:
447 * - GTK (group keys)
448 * - PTK (pairwise keys)
449 * - STK (station-to-station pairwise keys)
450 *
451 * When selecting a key, we have to distinguish between multicast
452 * (including broadcast) and unicast frames, the latter can only
453 * use PTKs and STKs while the former always use GTKs. Unless, of
454 * course, actual WEP keys ("pre-RSNA") are used, then unicast
455 * frames can also use key indizes like GTKs. Hence, if we don't
456 * have a PTK/STK we check the key index for a WEP key.
457 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400458 * Note that in a regular BSS, multicast frames are sent by the
459 * AP only, associated stations unicast the frame to the AP first
460 * which then multicasts it on their behalf.
461 *
Johannes Berg3017b802007-08-28 17:01:53 -0400462 * There is also a slight problem in IBSS mode: GTKs are negotiated
463 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400464 * The spec seems to expect that one negotiates the same key with
465 * every station but there's no such requirement; VLANs could be
466 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400467 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200468
Johannes Berg3017b802007-08-28 17:01:53 -0400469 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
470 return TXRX_CONTINUE;
471
472 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200473 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400474 * addressed to us nor a multicast frame.
475 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400476 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400477 return TXRX_CONTINUE;
478
Johannes Bergd4e46a32007-09-14 11:10:24 -0400479 if (rx->sta)
480 stakey = rcu_dereference(rx->sta->key);
481
482 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
483 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200484 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400485 /*
486 * The device doesn't give us the IV so we won't be
487 * able to look up the key. That's ok though, we
488 * don't need to decrypt the frame, we just won't
489 * be able to keep statistics accurate.
490 * Except for key threshold notifications, should
491 * we somehow allow the driver to tell us which key
492 * the hardware used if this flag is set?
493 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400494 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
495 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400496 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200497
Johannes Berg3017b802007-08-28 17:01:53 -0400498 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200499
Johannes Berg3017b802007-08-28 17:01:53 -0400500 if (rx->skb->len < 8 + hdrlen)
501 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200502
Johannes Berg3017b802007-08-28 17:01:53 -0400503 /*
504 * no need to call ieee80211_wep_get_keyidx,
505 * it verifies a bunch of things we've done already
506 */
507 keyidx = rx->skb->data[hdrlen + 3] >> 6;
508
Johannes Bergd4e46a32007-09-14 11:10:24 -0400509 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400510
511 /*
512 * RSNA-protected unicast frames should always be sent with
513 * pairwise or station-to-station keys, but for WEP we allow
514 * using a key index as well.
515 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400516 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400517 !is_multicast_ether_addr(hdr->addr1))
518 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200519 }
520
Johannes Berg3017b802007-08-28 17:01:53 -0400521 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200522 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400523 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200524 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500525#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200526 if (net_ratelimit())
527 printk(KERN_DEBUG "%s: RX protected frame,"
528 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500529#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berg70f08762007-09-26 17:53:14 +0200530 return TXRX_DROP;
531 }
532
Johannes Berg1990af82007-09-26 17:53:15 +0200533 /* Check for weak IVs if possible */
534 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
535 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
536 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
537 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
538 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
539 rx->sta->wep_weak_iv_count++;
540
Johannes Berg70f08762007-09-26 17:53:14 +0200541 switch (rx->key->conf.alg) {
542 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200543 result = ieee80211_crypto_wep_decrypt(rx);
544 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200545 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200546 result = ieee80211_crypto_tkip_decrypt(rx);
547 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200548 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200549 result = ieee80211_crypto_ccmp_decrypt(rx);
550 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200551 }
552
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200553 /* either the frame has been decrypted or will be dropped */
554 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
555
556 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200557}
558
Johannes Berg571ecf62007-07-27 15:43:22 +0200559static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
560{
561 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700562 DECLARE_MAC_BUF(mac);
563
Johannes Berg571ecf62007-07-27 15:43:22 +0200564 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
565
566 if (sdata->bss)
567 atomic_inc(&sdata->bss->num_sta_ps);
568 sta->flags |= WLAN_STA_PS;
569 sta->pspoll = 0;
570#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700571 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
572 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200573#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
574}
575
576static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
577{
578 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
579 struct sk_buff *skb;
580 int sent = 0;
581 struct ieee80211_sub_if_data *sdata;
582 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700583 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200584
585 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
586 if (sdata->bss)
587 atomic_dec(&sdata->bss->num_sta_ps);
588 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
589 sta->pspoll = 0;
590 if (!skb_queue_empty(&sta->ps_tx_buf)) {
591 if (local->ops->set_tim)
592 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
593 if (sdata->bss)
594 bss_tim_clear(local, sdata->bss, sta->aid);
595 }
596#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700597 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
598 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200599#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
600 /* Send all buffered frames to the station */
601 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
602 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
603 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400604 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200605 dev_queue_xmit(skb);
606 }
607 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
608 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
609 local->total_ps_buffered--;
610 sent++;
611#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700612 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200613 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700614 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200615#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400616 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200617 dev_queue_xmit(skb);
618 }
619
620 return sent;
621}
622
623static ieee80211_txrx_result
624ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
625{
626 struct sta_info *sta = rx->sta;
627 struct net_device *dev = rx->dev;
628 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
629
630 if (!sta)
631 return TXRX_CONTINUE;
632
633 /* Update last_rx only for IBSS packets which are for the current
634 * BSSID to avoid keeping the current IBSS network alive in cases where
635 * other STAs are using different BSSID. */
636 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
637 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
638 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
639 sta->last_rx = jiffies;
640 } else
641 if (!is_multicast_ether_addr(hdr->addr1) ||
642 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
643 /* Update last_rx only for unicast frames in order to prevent
644 * the Probe Request frames (the only broadcast frames from a
645 * STA in infrastructure mode) from keeping a connection alive.
646 */
647 sta->last_rx = jiffies;
648 }
649
Jiri Slabybadffb72007-08-28 17:01:54 -0400650 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200651 return TXRX_CONTINUE;
652
653 sta->rx_fragments++;
654 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400655 sta->last_rssi = rx->u.rx.status->ssi;
656 sta->last_signal = rx->u.rx.status->signal;
657 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200658
659 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
660 /* Change STA power saving mode only in the end of a frame
661 * exchange sequence */
662 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
663 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
664 else if (!(sta->flags & WLAN_STA_PS) &&
665 (rx->fc & IEEE80211_FCTL_PM))
666 ap_sta_ps_start(dev, sta);
667 }
668
669 /* Drop data::nullfunc frames silently, since they are used only to
670 * control station power saving mode. */
671 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
672 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
673 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
674 /* Update counter and free packet here to avoid counting this
675 * as a dropped packed. */
676 sta->rx_packets++;
677 dev_kfree_skb(rx->skb);
678 return TXRX_QUEUED;
679 }
680
681 return TXRX_CONTINUE;
682} /* ieee80211_rx_h_sta_process */
683
Johannes Berg571ecf62007-07-27 15:43:22 +0200684static inline struct ieee80211_fragment_entry *
685ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
686 unsigned int frag, unsigned int seq, int rx_queue,
687 struct sk_buff **skb)
688{
689 struct ieee80211_fragment_entry *entry;
690 int idx;
691
692 idx = sdata->fragment_next;
693 entry = &sdata->fragments[sdata->fragment_next++];
694 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
695 sdata->fragment_next = 0;
696
697 if (!skb_queue_empty(&entry->skb_list)) {
698#ifdef CONFIG_MAC80211_DEBUG
699 struct ieee80211_hdr *hdr =
700 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700701 DECLARE_MAC_BUF(mac);
702 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200703 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
704 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700705 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200706 sdata->dev->name, idx,
707 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700708 entry->last_frag, print_mac(mac, hdr->addr1),
709 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200710#endif /* CONFIG_MAC80211_DEBUG */
711 __skb_queue_purge(&entry->skb_list);
712 }
713
714 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
715 *skb = NULL;
716 entry->first_frag_time = jiffies;
717 entry->seq = seq;
718 entry->rx_queue = rx_queue;
719 entry->last_frag = frag;
720 entry->ccmp = 0;
721 entry->extra_len = 0;
722
723 return entry;
724}
725
726static inline struct ieee80211_fragment_entry *
727ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
728 u16 fc, unsigned int frag, unsigned int seq,
729 int rx_queue, struct ieee80211_hdr *hdr)
730{
731 struct ieee80211_fragment_entry *entry;
732 int i, idx;
733
734 idx = sdata->fragment_next;
735 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
736 struct ieee80211_hdr *f_hdr;
737 u16 f_fc;
738
739 idx--;
740 if (idx < 0)
741 idx = IEEE80211_FRAGMENT_MAX - 1;
742
743 entry = &sdata->fragments[idx];
744 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
745 entry->rx_queue != rx_queue ||
746 entry->last_frag + 1 != frag)
747 continue;
748
749 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
750 f_fc = le16_to_cpu(f_hdr->frame_control);
751
752 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
753 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
754 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
755 continue;
756
757 if (entry->first_frag_time + 2 * HZ < jiffies) {
758 __skb_queue_purge(&entry->skb_list);
759 continue;
760 }
761 return entry;
762 }
763
764 return NULL;
765}
766
767static ieee80211_txrx_result
768ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
769{
770 struct ieee80211_hdr *hdr;
771 u16 sc;
772 unsigned int frag, seq;
773 struct ieee80211_fragment_entry *entry;
774 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700775 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200776
777 hdr = (struct ieee80211_hdr *) rx->skb->data;
778 sc = le16_to_cpu(hdr->seq_ctrl);
779 frag = sc & IEEE80211_SCTL_FRAG;
780
781 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
782 (rx->skb)->len < 24 ||
783 is_multicast_ether_addr(hdr->addr1))) {
784 /* not fragmented */
785 goto out;
786 }
787 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
788
789 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
790
791 if (frag == 0) {
792 /* This is the first fragment of a new frame. */
793 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
794 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400795 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200796 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
797 /* Store CCMP PN so that we can verify that the next
798 * fragment has a sequential PN value. */
799 entry->ccmp = 1;
800 memcpy(entry->last_pn,
801 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
802 CCMP_PN_LEN);
803 }
804 return TXRX_QUEUED;
805 }
806
807 /* This is a fragment for a frame that should already be pending in
808 * fragment cache. Add this fragment to the end of the pending entry.
809 */
810 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
811 rx->u.rx.queue, hdr);
812 if (!entry) {
813 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
814 return TXRX_DROP;
815 }
816
817 /* Verify that MPDUs within one MSDU have sequential PN values.
818 * (IEEE 802.11i, 8.3.3.4.5) */
819 if (entry->ccmp) {
820 int i;
821 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400822 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200823 return TXRX_DROP;
824 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
825 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
826 pn[i]++;
827 if (pn[i])
828 break;
829 }
830 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
831 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400832 if (net_ratelimit())
833 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700834 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400835 " PN=%02x%02x%02x%02x%02x%02x "
836 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700837 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400838 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
839 rpn[5], pn[0], pn[1], pn[2], pn[3],
840 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200841 return TXRX_DROP;
842 }
843 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
844 }
845
846 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
847 __skb_queue_tail(&entry->skb_list, rx->skb);
848 entry->last_frag = frag;
849 entry->extra_len += rx->skb->len;
850 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
851 rx->skb = NULL;
852 return TXRX_QUEUED;
853 }
854
855 rx->skb = __skb_dequeue(&entry->skb_list);
856 if (skb_tailroom(rx->skb) < entry->extra_len) {
857 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
858 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
859 GFP_ATOMIC))) {
860 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
861 __skb_queue_purge(&entry->skb_list);
862 return TXRX_DROP;
863 }
864 }
865 while ((skb = __skb_dequeue(&entry->skb_list))) {
866 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
867 dev_kfree_skb(skb);
868 }
869
870 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400871 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200872
873 out:
874 if (rx->sta)
875 rx->sta->rx_packets++;
876 if (is_multicast_ether_addr(hdr->addr1))
877 rx->local->dot11MulticastReceivedFrameCount++;
878 else
879 ieee80211_led_rx(rx->local);
880 return TXRX_CONTINUE;
881}
882
883static ieee80211_txrx_result
884ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
885{
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200886 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg571ecf62007-07-27 15:43:22 +0200887 struct sk_buff *skb;
888 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700889 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200890
891 if (likely(!rx->sta ||
892 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
893 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400894 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200895 return TXRX_CONTINUE;
896
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200897 if ((sdata->type != IEEE80211_IF_TYPE_AP) &&
898 (sdata->type != IEEE80211_IF_TYPE_VLAN))
899 return TXRX_DROP;
900
Johannes Berg571ecf62007-07-27 15:43:22 +0200901 skb = skb_dequeue(&rx->sta->tx_filtered);
902 if (!skb) {
903 skb = skb_dequeue(&rx->sta->ps_tx_buf);
904 if (skb)
905 rx->local->total_ps_buffered--;
906 }
907 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
908 skb_queue_empty(&rx->sta->ps_tx_buf);
909
910 if (skb) {
911 struct ieee80211_hdr *hdr =
912 (struct ieee80211_hdr *) skb->data;
913
914 /* tell TX path to send one frame even though the STA may
915 * still remain is PS mode after this frame exchange */
916 rx->sta->pspoll = 1;
917
918#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700919 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
920 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200921 skb_queue_len(&rx->sta->ps_tx_buf));
922#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
923
924 /* Use MoreData flag to indicate whether there are more
925 * buffered frames for this STA */
926 if (no_pending_pkts) {
927 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
928 rx->sta->flags &= ~WLAN_STA_TIM;
929 } else
930 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
931
932 dev_queue_xmit(skb);
933
934 if (no_pending_pkts) {
935 if (rx->local->ops->set_tim)
936 rx->local->ops->set_tim(local_to_hw(rx->local),
937 rx->sta->aid, 0);
938 if (rx->sdata->bss)
939 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
940 }
941#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
942 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700943 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200944 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700945 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200946#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
947
948 }
949
950 /* Free PS Poll skb here instead of returning TXRX_DROP that would
951 * count as an dropped frame. */
952 dev_kfree_skb(rx->skb);
953
954 return TXRX_QUEUED;
955}
956
957static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200958ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
959{
960 u16 fc = rx->fc;
961 u8 *data = rx->skb->data;
962 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
963
964 if (!WLAN_FC_IS_QOS_DATA(fc))
965 return TXRX_CONTINUE;
966
967 /* remove the qos control field, update frame type and meta-data */
968 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
969 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
970 /* change frame type to non QOS */
971 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
972 hdr->frame_control = cpu_to_le16(fc);
973
974 return TXRX_CONTINUE;
975}
976
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200977static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100978ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200979{
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100980 if (unlikely(rx->sdata->ieee802_1x_pac &&
981 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200982#ifdef CONFIG_MAC80211_DEBUG
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200983 printk(KERN_DEBUG "%s: dropped frame "
984 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200985#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200986 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +0200987 }
988
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200989 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +0200990}
991
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200992static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100993ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200994{
Johannes Berg3017b802007-08-28 17:01:53 -0400995 /*
Johannes Berg7848ba72007-09-14 11:10:25 -0400996 * Pass through unencrypted frames if the hardware has
997 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -0400998 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400999 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001000 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001001
1002 /* Drop unencrypted frames if key is set. */
1003 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1004 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1005 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001006 (rx->key || rx->sdata->drop_unencrypted))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001007 if (net_ratelimit())
1008 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1009 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001010 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001011 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001012 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001013}
1014
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001015static int
1016ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001017{
1018 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001019 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1020 u16 fc, hdrlen, ethertype;
1021 u8 *payload;
1022 u8 dst[ETH_ALEN];
1023 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001024 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001025 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001026 DECLARE_MAC_BUF(mac);
1027 DECLARE_MAC_BUF(mac2);
1028 DECLARE_MAC_BUF(mac3);
1029 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001030
1031 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001032
1033 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001034 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001035
1036 hdrlen = ieee80211_get_hdrlen(fc);
1037
1038 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1039 * header
1040 * IEEE 802.11 address fields:
1041 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1042 * 0 0 DA SA BSSID n/a
1043 * 0 1 DA BSSID SA n/a
1044 * 1 0 BSSID SA DA n/a
1045 * 1 1 RA TA DA SA
1046 */
1047
1048 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1049 case IEEE80211_FCTL_TODS:
1050 /* BSSID SA DA */
1051 memcpy(dst, hdr->addr3, ETH_ALEN);
1052 memcpy(src, hdr->addr2, ETH_ALEN);
1053
1054 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1055 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001056 if (net_ratelimit())
1057 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001058 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001059 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001060 print_mac(mac, hdr->addr1),
1061 print_mac(mac2, hdr->addr2),
1062 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001063 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001064 }
1065 break;
1066 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1067 /* RA TA DA SA */
1068 memcpy(dst, hdr->addr3, ETH_ALEN);
1069 memcpy(src, hdr->addr4, ETH_ALEN);
1070
1071 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001072 if (net_ratelimit())
1073 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001074 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001075 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001076 print_mac(mac, hdr->addr1),
1077 print_mac(mac2, hdr->addr2),
1078 print_mac(mac3, hdr->addr3),
1079 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001080 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001081 }
1082 break;
1083 case IEEE80211_FCTL_FROMDS:
1084 /* DA BSSID SA */
1085 memcpy(dst, hdr->addr1, ETH_ALEN);
1086 memcpy(src, hdr->addr3, ETH_ALEN);
1087
John W. Linvilleb3316152007-08-28 17:01:55 -04001088 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1089 (is_multicast_ether_addr(dst) &&
1090 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001091 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001092 break;
1093 case 0:
1094 /* DA SA BSSID */
1095 memcpy(dst, hdr->addr1, ETH_ALEN);
1096 memcpy(src, hdr->addr2, ETH_ALEN);
1097
1098 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1099 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001100 printk(KERN_DEBUG "%s: dropped IBSS frame "
1101 "(DA=%s SA=%s BSSID=%s)\n",
1102 dev->name,
1103 print_mac(mac, hdr->addr1),
1104 print_mac(mac2, hdr->addr2),
1105 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001106 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001107 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001108 }
1109 break;
1110 }
1111
Johannes Berg571ecf62007-07-27 15:43:22 +02001112 if (unlikely(skb->len - hdrlen < 8)) {
1113 if (net_ratelimit()) {
1114 printk(KERN_DEBUG "%s: RX too short data frame "
1115 "payload\n", dev->name);
1116 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001117 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001118 }
1119
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001120 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001121 ethertype = (payload[6] << 8) | payload[7];
1122
1123 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1124 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1125 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1126 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1127 * replace EtherType */
1128 skb_pull(skb, hdrlen + 6);
1129 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1130 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1131 } else {
1132 struct ethhdr *ehdr;
1133 __be16 len;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001134
Johannes Berg571ecf62007-07-27 15:43:22 +02001135 skb_pull(skb, hdrlen);
1136 len = htons(skb->len);
1137 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1138 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1139 memcpy(ehdr->h_source, src, ETH_ALEN);
1140 ehdr->h_proto = len;
1141 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001142 return 0;
1143}
Johannes Berg571ecf62007-07-27 15:43:22 +02001144
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001145/*
1146 * requires that rx->skb is a frame with ethernet header
1147 */
1148static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1149{
1150 static const u8 pae_group_addr[ETH_ALEN]
1151 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1152 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1153
1154 /*
1155 * Allow EAPOL frames to us/the PAE group address regardless
1156 * of whether the frame was encrypted or not.
1157 */
1158 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1159 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1160 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1161 return true;
1162
1163 if (ieee80211_802_1x_port_control(rx) ||
1164 ieee80211_drop_unencrypted(rx))
1165 return false;
1166
1167 return true;
1168}
1169
1170/*
1171 * requires that rx->skb is a frame with ethernet header
1172 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001173static void
1174ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1175{
1176 struct net_device *dev = rx->dev;
1177 struct ieee80211_local *local = rx->local;
1178 struct sk_buff *skb, *xmit_skb;
1179 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001180 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1181 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001182
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001183 skb = rx->skb;
1184 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001185
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001186 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP ||
1187 sdata->type == IEEE80211_IF_TYPE_VLAN) &&
Jiri Slabybadffb72007-08-28 17:01:54 -04001188 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001189 if (is_multicast_ether_addr(ehdr->h_dest)) {
1190 /*
1191 * send multicast frames both to higher layers in
1192 * local net stack and back to the wireless medium
1193 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001194 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1195 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001196 printk(KERN_DEBUG "%s: failed to clone "
1197 "multicast frame\n", dev->name);
1198 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001199 dsta = sta_info_get(local, skb->data);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001200 if (dsta && dsta->dev == dev) {
1201 /*
1202 * The destination station is associated to
1203 * this AP (in this VLAN), so send the frame
1204 * directly to it and do not pass it to local
1205 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001206 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001207 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001208 skb = NULL;
1209 }
1210 if (dsta)
1211 sta_info_put(dsta);
1212 }
1213 }
1214
1215 if (skb) {
1216 /* deliver to local stack */
1217 skb->protocol = eth_type_trans(skb, dev);
1218 memset(skb->cb, 0, sizeof(skb->cb));
1219 netif_rx(skb);
1220 }
1221
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001222 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001223 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001224 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001225 skb_reset_network_header(xmit_skb);
1226 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001227 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001228 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001229}
1230
1231static ieee80211_txrx_result
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001232ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1233{
1234 struct net_device *dev = rx->dev;
1235 struct ieee80211_local *local = rx->local;
1236 u16 fc, ethertype;
1237 u8 *payload;
1238 struct sk_buff *skb = rx->skb, *frame = NULL;
1239 const struct ethhdr *eth;
1240 int remaining, err;
1241 u8 dst[ETH_ALEN];
1242 u8 src[ETH_ALEN];
1243 DECLARE_MAC_BUF(mac);
1244
1245 fc = rx->fc;
1246 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1247 return TXRX_CONTINUE;
1248
1249 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1250 return TXRX_DROP;
1251
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +02001252 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001253 return TXRX_CONTINUE;
1254
1255 err = ieee80211_data_to_8023(rx);
1256 if (unlikely(err))
1257 return TXRX_DROP;
1258
1259 skb->dev = dev;
1260
1261 dev->stats.rx_packets++;
1262 dev->stats.rx_bytes += skb->len;
1263
1264 /* skip the wrapping header */
1265 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1266 if (!eth)
1267 return TXRX_DROP;
1268
1269 while (skb != frame) {
1270 u8 padding;
1271 __be16 len = eth->h_proto;
1272 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1273
1274 remaining = skb->len;
1275 memcpy(dst, eth->h_dest, ETH_ALEN);
1276 memcpy(src, eth->h_source, ETH_ALEN);
1277
1278 padding = ((4 - subframe_len) & 0x3);
1279 /* the last MSDU has no padding */
1280 if (subframe_len > remaining) {
1281 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1282 return TXRX_DROP;
1283 }
1284
1285 skb_pull(skb, sizeof(struct ethhdr));
1286 /* if last subframe reuse skb */
1287 if (remaining <= subframe_len + padding)
1288 frame = skb;
1289 else {
1290 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1291 subframe_len);
1292
1293 if (frame == NULL)
1294 return TXRX_DROP;
1295
1296 skb_reserve(frame, local->hw.extra_tx_headroom +
1297 sizeof(struct ethhdr));
1298 memcpy(skb_put(frame, ntohs(len)), skb->data,
1299 ntohs(len));
1300
1301 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1302 padding);
1303 if (!eth) {
1304 printk(KERN_DEBUG "%s: wrong buffer size ",
1305 dev->name);
1306 dev_kfree_skb(frame);
1307 return TXRX_DROP;
1308 }
1309 }
1310
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001311 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001312 frame->dev = dev;
1313 frame->priority = skb->priority;
1314 rx->skb = frame;
1315
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001316 payload = frame->data;
1317 ethertype = (payload[6] << 8) | payload[7];
1318
1319 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001320 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1321 compare_ether_addr(payload,
1322 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001323 /* remove RFC1042 or Bridge-Tunnel
1324 * encapsulation and replace EtherType */
1325 skb_pull(frame, 6);
1326 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1327 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1328 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001329 memcpy(skb_push(frame, sizeof(__be16)),
1330 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001331 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1332 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1333 }
1334
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001335 if (!ieee80211_frame_allowed(rx)) {
1336 if (skb == frame) /* last frame */
1337 return TXRX_DROP;
1338 dev_kfree_skb(frame);
1339 continue;
1340 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001341
1342 ieee80211_deliver_skb(rx);
1343 }
1344
1345 return TXRX_QUEUED;
1346}
1347
1348static ieee80211_txrx_result
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001349ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1350{
1351 struct net_device *dev = rx->dev;
1352 u16 fc;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001353 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001354
1355 fc = rx->fc;
1356 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1357 return TXRX_CONTINUE;
1358
1359 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1360 return TXRX_DROP;
1361
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001362 err = ieee80211_data_to_8023(rx);
1363 if (unlikely(err))
1364 return TXRX_DROP;
1365
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001366 if (!ieee80211_frame_allowed(rx))
1367 return TXRX_DROP;
1368
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001369 rx->skb->dev = dev;
1370
1371 dev->stats.rx_packets++;
1372 dev->stats.rx_bytes += rx->skb->len;
1373
1374 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001375
1376 return TXRX_QUEUED;
1377}
1378
1379static ieee80211_txrx_result
1380ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1381{
1382 struct ieee80211_sub_if_data *sdata;
1383
Jiri Slabybadffb72007-08-28 17:01:54 -04001384 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001385 return TXRX_DROP;
1386
1387 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1388 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1389 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001390 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg571ecf62007-07-27 15:43:22 +02001391 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001392 else
1393 return TXRX_DROP;
1394
Johannes Berg571ecf62007-07-27 15:43:22 +02001395 return TXRX_QUEUED;
1396}
1397
1398static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1399 struct ieee80211_local *local,
1400 ieee80211_rx_handler *handlers,
1401 struct ieee80211_txrx_data *rx,
1402 struct sta_info *sta)
1403{
1404 ieee80211_rx_handler *handler;
1405 ieee80211_txrx_result res = TXRX_DROP;
1406
1407 for (handler = handlers; *handler != NULL; handler++) {
1408 res = (*handler)(rx);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001409
1410 switch (res) {
1411 case TXRX_CONTINUE:
1412 continue;
1413 case TXRX_DROP:
1414 I802_DEBUG_INC(local->rx_handlers_drop);
1415 if (sta)
1416 sta->rx_dropped++;
1417 break;
1418 case TXRX_QUEUED:
1419 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001420 break;
1421 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001422 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001423 }
1424
Johannes Berg8e6f00322007-07-27 15:43:22 +02001425 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001426 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001427 return res;
1428}
1429
1430static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1431 ieee80211_rx_handler *handlers,
1432 struct ieee80211_txrx_data *rx,
1433 struct sta_info *sta)
1434{
1435 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1436 TXRX_CONTINUE)
1437 dev_kfree_skb(rx->skb);
1438}
1439
1440static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1441 struct ieee80211_hdr *hdr,
1442 struct sta_info *sta,
1443 struct ieee80211_txrx_data *rx)
1444{
1445 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001446 DECLARE_MAC_BUF(mac);
1447 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001448
1449 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1450 if (rx->skb->len >= hdrlen + 4)
1451 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1452 else
1453 keyidx = -1;
1454
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001455 if (net_ratelimit())
1456 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001457 "failure from %s to %s keyidx=%d\n",
1458 dev->name, print_mac(mac, hdr->addr2),
1459 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001460
1461 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001462 /*
1463 * Some hardware seem to generate incorrect Michael MIC
1464 * reports; ignore them to avoid triggering countermeasures.
1465 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001466 if (net_ratelimit())
1467 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001468 "error for unknown address %s\n",
1469 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001470 goto ignore;
1471 }
1472
1473 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001474 if (net_ratelimit())
1475 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001476 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001477 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001478 goto ignore;
1479 }
1480
Johannes Berg7848ba72007-09-14 11:10:25 -04001481 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001482 /*
1483 * APs with pairwise keys should never receive Michael MIC
1484 * errors for non-zero keyidx because these are reserved for
1485 * group keys and only the AP is sending real multicast
1486 * frames in the BSS.
1487 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001488 if (net_ratelimit())
1489 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1490 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001491 " (src %s)\n", dev->name, keyidx,
1492 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001493 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001494 }
1495
1496 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1497 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1498 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001499 if (net_ratelimit())
1500 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1501 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001502 "(fc=0x%04x) (src %s)\n",
1503 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001504 goto ignore;
1505 }
1506
Johannes Bergeb063c12007-08-28 17:01:53 -04001507 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001508 ignore:
1509 dev_kfree_skb(rx->skb);
1510 rx->skb = NULL;
1511}
1512
1513ieee80211_rx_handler ieee80211_rx_handlers[] =
1514{
1515 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001516 ieee80211_rx_h_passive_scan,
1517 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001518 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001519 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001520 ieee80211_rx_h_defragment,
1521 ieee80211_rx_h_ps_poll,
1522 ieee80211_rx_h_michael_mic_verify,
1523 /* this must be after decryption - so header is counted in MPDU mic
1524 * must be before pae and data, so QOS_DATA format frames
1525 * are not passed to user space by these functions
1526 */
1527 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001528 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001529 ieee80211_rx_h_data,
1530 ieee80211_rx_h_mgmt,
1531 NULL
1532};
1533
1534/* main receive path */
1535
Johannes Berg23a24de2007-07-27 15:43:22 +02001536static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1537 u8 *bssid, struct ieee80211_txrx_data *rx,
1538 struct ieee80211_hdr *hdr)
1539{
1540 int multicast = is_multicast_ether_addr(hdr->addr1);
1541
1542 switch (sdata->type) {
1543 case IEEE80211_IF_TYPE_STA:
1544 if (!bssid)
1545 return 0;
1546 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001547 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001548 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001549 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001550 } else if (!multicast &&
1551 compare_ether_addr(sdata->dev->dev_addr,
1552 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001553 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001554 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001555 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001556 }
1557 break;
1558 case IEEE80211_IF_TYPE_IBSS:
1559 if (!bssid)
1560 return 0;
1561 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001562 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001563 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001564 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001565 } else if (!multicast &&
1566 compare_ether_addr(sdata->dev->dev_addr,
1567 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001568 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001569 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001570 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001571 } else if (!rx->sta)
1572 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1573 bssid, hdr->addr2);
1574 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001575 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001576 case IEEE80211_IF_TYPE_AP:
1577 if (!bssid) {
1578 if (compare_ether_addr(sdata->dev->dev_addr,
1579 hdr->addr1))
1580 return 0;
1581 } else if (!ieee80211_bssid_match(bssid,
1582 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001583 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001584 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001585 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001586 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001587 if (sdata->dev == sdata->local->mdev &&
1588 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001589 /* do not receive anything via
1590 * master device when not scanning */
1591 return 0;
1592 break;
1593 case IEEE80211_IF_TYPE_WDS:
1594 if (bssid ||
1595 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1596 return 0;
1597 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1598 return 0;
1599 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001600 case IEEE80211_IF_TYPE_MNTR:
1601 /* take everything */
1602 break;
Johannes Berga2897552007-09-28 14:01:25 +02001603 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001604 /* should never get here */
1605 WARN_ON(1);
1606 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001607 }
1608
1609 return 1;
1610}
1611
Johannes Berg571ecf62007-07-27 15:43:22 +02001612/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001613 * This is the actual Rx frames handler. as it blongs to Rx path it must
1614 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02001615 */
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001616void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw, struct sk_buff *skb,
1617 struct ieee80211_rx_status *status, u32 load)
Johannes Berg571ecf62007-07-27 15:43:22 +02001618{
1619 struct ieee80211_local *local = hw_to_local(hw);
1620 struct ieee80211_sub_if_data *sdata;
1621 struct sta_info *sta;
1622 struct ieee80211_hdr *hdr;
1623 struct ieee80211_txrx_data rx;
1624 u16 type;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001625 int prepares;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001626 struct ieee80211_sub_if_data *prev = NULL;
1627 struct sk_buff *skb_new;
1628 u8 *bssid;
David S. Millerd10f2152008-01-24 16:57:39 -08001629 int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001630
1631 hdr = (struct ieee80211_hdr *) skb->data;
1632 memset(&rx, 0, sizeof(rx));
1633 rx.skb = skb;
1634 rx.local = local;
1635
1636 rx.u.rx.status = status;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001637 rx.u.rx.load = load;
Johannes Bergb2e77712007-09-26 15:19:39 +02001638 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001639 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001640
David S. Millerd10f2152008-01-24 16:57:39 -08001641 /*
1642 * Drivers are required to align the payload data to a four-byte
1643 * boundary, so the last two bits of the address where it starts
1644 * may not be set. The header is required to be directly before
1645 * the payload data, padding like atheros hardware adds which is
1646 * inbetween the 802.11 header and the payload is not supported,
1647 * the driver is required to move the 802.11 header further back
1648 * in that case.
1649 */
1650 hdrlen = ieee80211_get_hdrlen(rx.fc);
1651 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1652
Johannes Bergb2e77712007-09-26 15:19:39 +02001653 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001654 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001655
Johannes Bergb2e77712007-09-26 15:19:39 +02001656 sta = rx.sta = sta_info_get(local, hdr->addr2);
1657 if (sta) {
1658 rx.dev = rx.sta->dev;
1659 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1660 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001661
Johannes Berg571ecf62007-07-27 15:43:22 +02001662 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1663 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1664 goto end;
1665 }
1666
Zhu Yiece8edd2007-11-22 10:53:21 +08001667 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001668 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001669
1670 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1671 sta) != TXRX_CONTINUE)
1672 goto end;
1673 skb = rx.skb;
1674
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001675 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001676 !atomic_read(&local->iff_promiscs) &&
1677 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001678 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001679 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001680 rx.sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001681 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001682 rcu_read_unlock();
Johannes Berg8e6f00322007-07-27 15:43:22 +02001683 return;
1684 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001685
Johannes Bergb2e77712007-09-26 15:19:39 +02001686 bssid = ieee80211_get_bssid(hdr, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001687
Johannes Berg79010422007-09-18 17:29:21 -04001688 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001689 if (!netif_running(sdata->dev))
1690 continue;
1691
Johannes Bergb2e77712007-09-26 15:19:39 +02001692 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1693 continue;
1694
1695 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001696 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02001697 /* prepare_for_handlers can change sta */
1698 sta = rx.sta;
1699
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001700 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02001701 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001702
Johannes Berg340e11f2007-07-27 15:43:22 +02001703 /*
1704 * frame is destined for this interface, but if it's not
1705 * also for the previous one we handle that after the
1706 * loop to avoid copying the SKB once too much
1707 */
1708
1709 if (!prev) {
1710 prev = sdata;
1711 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001712 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001713
1714 /*
1715 * frame was destined for the previous interface
1716 * so invoke RX handlers for it
1717 */
1718
1719 skb_new = skb_copy(skb, GFP_ATOMIC);
1720 if (!skb_new) {
1721 if (net_ratelimit())
1722 printk(KERN_DEBUG "%s: failed to copy "
1723 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001724 wiphy_name(local->hw.wiphy),
1725 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001726 continue;
1727 }
Helmut Schaa69f817b2007-12-21 15:16:35 +01001728 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg340e11f2007-07-27 15:43:22 +02001729 rx.skb = skb_new;
1730 rx.dev = prev->dev;
1731 rx.sdata = prev;
1732 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1733 &rx, sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001734 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001735 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001736 if (prev) {
Helmut Schaa69f817b2007-12-21 15:16:35 +01001737 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001738 rx.skb = skb;
1739 rx.dev = prev->dev;
1740 rx.sdata = prev;
1741 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1742 &rx, sta);
1743 } else
1744 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001745
Johannes Berg8e6f00322007-07-27 15:43:22 +02001746 end:
Johannes Berg571ecf62007-07-27 15:43:22 +02001747 if (sta)
1748 sta_info_put(sta);
1749}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001750
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001751#define SEQ_MODULO 0x1000
1752#define SEQ_MASK 0xfff
1753
1754static inline int seq_less(u16 sq1, u16 sq2)
1755{
1756 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1757}
1758
1759static inline u16 seq_inc(u16 sq)
1760{
1761 return ((sq + 1) & SEQ_MASK);
1762}
1763
1764static inline u16 seq_sub(u16 sq1, u16 sq2)
1765{
1766 return ((sq1 - sq2) & SEQ_MASK);
1767}
1768
1769
1770u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
1771 struct tid_ampdu_rx *tid_agg_rx,
1772 struct sk_buff *skb, u16 mpdu_seq_num,
1773 int bar_req)
1774{
1775 struct ieee80211_local *local = hw_to_local(hw);
1776 struct ieee80211_rx_status status;
1777 u16 head_seq_num, buf_size;
1778 int index;
1779 u32 pkt_load;
1780
1781 buf_size = tid_agg_rx->buf_size;
1782 head_seq_num = tid_agg_rx->head_seq_num;
1783
1784 /* frame with out of date sequence number */
1785 if (seq_less(mpdu_seq_num, head_seq_num)) {
1786 dev_kfree_skb(skb);
1787 return 1;
1788 }
1789
1790 /* if frame sequence number exceeds our buffering window size or
1791 * block Ack Request arrived - release stored frames */
1792 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
1793 /* new head to the ordering buffer */
1794 if (bar_req)
1795 head_seq_num = mpdu_seq_num;
1796 else
1797 head_seq_num =
1798 seq_inc(seq_sub(mpdu_seq_num, buf_size));
1799 /* release stored frames up to new head to stack */
1800 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1801 index = seq_sub(tid_agg_rx->head_seq_num,
1802 tid_agg_rx->ssn)
1803 % tid_agg_rx->buf_size;
1804
1805 if (tid_agg_rx->reorder_buf[index]) {
1806 /* release the reordered frames to stack */
1807 memcpy(&status,
1808 tid_agg_rx->reorder_buf[index]->cb,
1809 sizeof(status));
1810 pkt_load = ieee80211_rx_load_stats(local,
1811 tid_agg_rx->reorder_buf[index],
1812 &status);
1813 __ieee80211_rx_handle_packet(hw,
1814 tid_agg_rx->reorder_buf[index],
1815 &status, pkt_load);
1816 tid_agg_rx->stored_mpdu_num--;
1817 tid_agg_rx->reorder_buf[index] = NULL;
1818 }
1819 tid_agg_rx->head_seq_num =
1820 seq_inc(tid_agg_rx->head_seq_num);
1821 }
1822 if (bar_req)
1823 return 1;
1824 }
1825
1826 /* now the new frame is always in the range of the reordering */
1827 /* buffer window */
1828 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
1829 % tid_agg_rx->buf_size;
1830 /* check if we already stored this frame */
1831 if (tid_agg_rx->reorder_buf[index]) {
1832 dev_kfree_skb(skb);
1833 return 1;
1834 }
1835
1836 /* if arrived mpdu is in the right order and nothing else stored */
1837 /* release it immediately */
1838 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1839 tid_agg_rx->stored_mpdu_num == 0) {
1840 tid_agg_rx->head_seq_num =
1841 seq_inc(tid_agg_rx->head_seq_num);
1842 return 0;
1843 }
1844
1845 /* put the frame in the reordering buffer */
1846 tid_agg_rx->reorder_buf[index] = skb;
1847 tid_agg_rx->stored_mpdu_num++;
1848 /* release the buffer until next missing frame */
1849 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
1850 % tid_agg_rx->buf_size;
1851 while (tid_agg_rx->reorder_buf[index]) {
1852 /* release the reordered frame back to stack */
1853 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
1854 sizeof(status));
1855 pkt_load = ieee80211_rx_load_stats(local,
1856 tid_agg_rx->reorder_buf[index],
1857 &status);
1858 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
1859 &status, pkt_load);
1860 tid_agg_rx->stored_mpdu_num--;
1861 tid_agg_rx->reorder_buf[index] = NULL;
1862 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
1863 index = seq_sub(tid_agg_rx->head_seq_num,
1864 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
1865 }
1866 return 1;
1867}
1868
1869u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
1870 struct sk_buff *skb)
1871{
1872 struct ieee80211_hw *hw = &local->hw;
1873 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1874 struct sta_info *sta;
1875 struct tid_ampdu_rx *tid_agg_rx;
1876 u16 fc, sc;
1877 u16 mpdu_seq_num;
1878 u8 ret = 0, *qc;
1879 int tid;
1880
1881 sta = sta_info_get(local, hdr->addr2);
1882 if (!sta)
1883 return ret;
1884
1885 fc = le16_to_cpu(hdr->frame_control);
1886
1887 /* filter the QoS data rx stream according to
1888 * STA/TID and check if this STA/TID is on aggregation */
1889 if (!WLAN_FC_IS_QOS_DATA(fc))
1890 goto end_reorder;
1891
1892 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
1893 tid = qc[0] & QOS_CONTROL_TID_MASK;
1894 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
1895
1896 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1897 goto end_reorder;
1898
1899 /* null data frames are excluded */
1900 if (unlikely(fc & IEEE80211_STYPE_QOS_NULLFUNC))
1901 goto end_reorder;
1902
1903 /* new un-ordered ampdu frame - process it */
1904
1905 /* reset session timer */
1906 if (tid_agg_rx->timeout) {
1907 unsigned long expires =
1908 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1909 mod_timer(&tid_agg_rx->session_timer, expires);
1910 }
1911
1912 /* if this mpdu is fragmented - terminate rx aggregation session */
1913 sc = le16_to_cpu(hdr->seq_ctrl);
1914 if (sc & IEEE80211_SCTL_FRAG) {
1915 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
1916 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
1917 ret = 1;
1918 goto end_reorder;
1919 }
1920
1921 /* according to mpdu sequence number deal with reordering buffer */
1922 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1923 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
1924 mpdu_seq_num, 0);
1925end_reorder:
1926 if (sta)
1927 sta_info_put(sta);
1928 return ret;
1929}
1930
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001931/*
1932 * This is the receive path handler. It is called by a low level driver when an
1933 * 802.11 MPDU is received from the hardware.
1934 */
1935void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1936 struct ieee80211_rx_status *status)
1937{
1938 struct ieee80211_local *local = hw_to_local(hw);
1939 u32 pkt_load;
1940
1941 /*
1942 * key references and virtual interfaces are protected using RCU
1943 * and this requires that we are in a read-side RCU section during
1944 * receive processing
1945 */
1946 rcu_read_lock();
1947
1948 /*
1949 * Frames with failed FCS/PLCP checksum are not returned,
1950 * all other frames are returned without radiotap header
1951 * if it was previously present.
1952 * Also, frames with less than 16 bytes are dropped.
1953 */
1954 skb = ieee80211_rx_monitor(local, skb, status);
1955 if (!skb) {
1956 rcu_read_unlock();
1957 return;
1958 }
1959
1960 pkt_load = ieee80211_rx_load_stats(local, skb, status);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001961 local->channel_use_raw += pkt_load;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001962
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001963 if (!ieee80211_rx_reorder_ampdu(local, skb))
1964 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001965
1966 rcu_read_unlock();
1967}
Johannes Berg571ecf62007-07-27 15:43:22 +02001968EXPORT_SYMBOL(__ieee80211_rx);
1969
1970/* This is a version of the rx handler that can be called from hard irq
1971 * context. Post the skb on the queue and schedule the tasklet */
1972void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1973 struct ieee80211_rx_status *status)
1974{
1975 struct ieee80211_local *local = hw_to_local(hw);
1976
1977 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1978
1979 skb->dev = local->mdev;
1980 /* copy status into skb->cb for use by tasklet */
1981 memcpy(skb->cb, status, sizeof(*status));
1982 skb->pkt_type = IEEE80211_RX_MSG;
1983 skb_queue_tail(&local->skb_queue, skb);
1984 tasklet_schedule(&local->tasklet);
1985}
1986EXPORT_SYMBOL(ieee80211_rx_irqsafe);