blob: 89e1e3070ec17e048bf9b79606d7b07935c14990 [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 Berg571ecf62007-07-27 15:43:22 +0200252/* pre-rx handlers
253 *
254 * these don't have dev/sdata fields in the rx data
Johannes Berg52865df2007-07-27 15:43:22 +0200255 * The sta value should also not be used because it may
256 * be NULL even though a STA (in IBSS mode) will be added.
Johannes Berg571ecf62007-07-27 15:43:22 +0200257 */
258
259static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200260ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
261{
262 u8 *data = rx->skb->data;
263 int tid;
264
265 /* does the frame have a qos control field? */
266 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
267 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
268 /* frame has qos control */
269 tid = qc[0] & QOS_CONTROL_TID_MASK;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200270 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200271 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200272 else
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +0200273 rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200274 } else {
275 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
276 /* Separate TID for management frames */
277 tid = NUM_RX_DATA_QUEUES - 1;
278 } else {
279 /* no qos control present */
280 tid = 0; /* 802.1d - Best Effort */
281 }
282 }
Johannes Berg52865df2007-07-27 15:43:22 +0200283
Johannes Berg6e0d1142007-07-27 15:43:22 +0200284 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865df2007-07-27 15:43:22 +0200285 /* only a debug counter, sta might not be assigned properly yet */
286 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200287 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200288
289 rx->u.rx.queue = tid;
290 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
291 * For now, set skb->priority to 0 for other cases. */
292 rx->skb->priority = (tid > 7) ? 0 : tid;
293
294 return TXRX_CONTINUE;
295}
296
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200297
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +0200298static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200299 struct sk_buff *skb,
300 struct ieee80211_rx_status *status)
Johannes Berg571ecf62007-07-27 15:43:22 +0200301{
Johannes Berg571ecf62007-07-27 15:43:22 +0200302 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
303 u32 load = 0, hdrtime;
304 struct ieee80211_rate *rate;
305 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
306 int i;
307
308 /* Estimate total channel use caused by this frame */
309
310 if (unlikely(mode->num_rates < 0))
311 return TXRX_CONTINUE;
312
313 rate = &mode->rates[0];
314 for (i = 0; i < mode->num_rates; i++) {
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200315 if (mode->rates[i].val == status->rate) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200316 rate = &mode->rates[i];
317 break;
318 }
319 }
320
321 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
322 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
323
324 if (mode->mode == MODE_IEEE80211A ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200325 (mode->mode == MODE_IEEE80211G &&
326 rate->flags & IEEE80211_RATE_ERP))
327 hdrtime = CHAN_UTIL_HDR_SHORT;
328 else
329 hdrtime = CHAN_UTIL_HDR_LONG;
330
331 load = hdrtime;
332 if (!is_multicast_ether_addr(hdr->addr1))
333 load += hdrtime;
334
335 load += skb->len * rate->rate_inv;
336
337 /* Divide channel_use by 8 to avoid wrapping around the counter */
338 load >>= CHAN_UTIL_SHIFT;
Johannes Berg571ecf62007-07-27 15:43:22 +0200339
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +0200340 return load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200341}
342
343ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
344{
345 ieee80211_rx_h_parse_qos,
Johannes Berg571ecf62007-07-27 15:43:22 +0200346 NULL
347};
348
349/* rx handlers */
350
351static ieee80211_txrx_result
352ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
353{
Johannes Berg52865df2007-07-27 15:43:22 +0200354 if (rx->sta)
355 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200356 rx->sdata->channel_use_raw += rx->u.rx.load;
357 return TXRX_CONTINUE;
358}
359
Johannes Berg571ecf62007-07-27 15:43:22 +0200360static ieee80211_txrx_result
361ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
362{
363 struct ieee80211_local *local = rx->local;
364 struct sk_buff *skb = rx->skb;
365
Zhu Yiece8edd2007-11-22 10:53:21 +0800366 if (unlikely(local->sta_hw_scanning))
367 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
368
369 if (unlikely(local->sta_sw_scanning)) {
370 /* drop all the other packets during a software scan anyway */
371 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
372 != TXRX_QUEUED)
373 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +0200374 return TXRX_QUEUED;
375 }
376
Jiri Slabybadffb72007-08-28 17:01:54 -0400377 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200378 /* scanning finished during invoking of handlers */
379 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
380 return TXRX_DROP;
381 }
382
383 return TXRX_CONTINUE;
384}
385
386static ieee80211_txrx_result
387ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
388{
389 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200390 hdr = (struct ieee80211_hdr *) rx->skb->data;
391
392 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
393 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
394 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
395 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
396 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400397 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200398 rx->local->dot11FrameDuplicateCount++;
399 rx->sta->num_duplicates++;
400 }
401 return TXRX_DROP;
402 } else
403 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
404 }
405
Johannes Berg571ecf62007-07-27 15:43:22 +0200406 if (unlikely(rx->skb->len < 16)) {
407 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
408 return TXRX_DROP;
409 }
410
Johannes Berg571ecf62007-07-27 15:43:22 +0200411 /* Drop disallowed frame classes based on STA auth/assoc state;
412 * IEEE 802.11, Chap 5.5.
413 *
414 * 80211.o does filtering only based on association state, i.e., it
415 * drops Class 3 frames from not associated stations. hostapd sends
416 * deauth/disassoc frames when needed. In addition, hostapd is
417 * responsible for filtering on both auth and assoc states.
418 */
419 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
420 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
421 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100422 rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200423 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
424 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
425 !(rx->fc & IEEE80211_FCTL_TODS) &&
426 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400427 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200428 /* Drop IBSS frames and frames for other hosts
429 * silently. */
430 return TXRX_DROP;
431 }
432
Johannes Bergf9d540e2007-09-28 14:02:09 +0200433 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +0200434 }
435
Johannes Berg570bd532007-07-27 15:43:22 +0200436 return TXRX_CONTINUE;
437}
438
439
440static ieee80211_txrx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200441ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200442{
443 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400444 int keyidx;
445 int hdrlen;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200446 ieee80211_txrx_result result = TXRX_DROP;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400447 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200448
Johannes Berg3017b802007-08-28 17:01:53 -0400449 /*
450 * Key selection 101
451 *
452 * There are three types of keys:
453 * - GTK (group keys)
454 * - PTK (pairwise keys)
455 * - STK (station-to-station pairwise keys)
456 *
457 * When selecting a key, we have to distinguish between multicast
458 * (including broadcast) and unicast frames, the latter can only
459 * use PTKs and STKs while the former always use GTKs. Unless, of
460 * course, actual WEP keys ("pre-RSNA") are used, then unicast
461 * frames can also use key indizes like GTKs. Hence, if we don't
462 * have a PTK/STK we check the key index for a WEP key.
463 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400464 * Note that in a regular BSS, multicast frames are sent by the
465 * AP only, associated stations unicast the frame to the AP first
466 * which then multicasts it on their behalf.
467 *
Johannes Berg3017b802007-08-28 17:01:53 -0400468 * There is also a slight problem in IBSS mode: GTKs are negotiated
469 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400470 * The spec seems to expect that one negotiates the same key with
471 * every station but there's no such requirement; VLANs could be
472 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400473 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200474
Johannes Berg3017b802007-08-28 17:01:53 -0400475 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
476 return TXRX_CONTINUE;
477
478 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200479 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400480 * addressed to us nor a multicast frame.
481 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400482 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400483 return TXRX_CONTINUE;
484
Johannes Bergd4e46a32007-09-14 11:10:24 -0400485 if (rx->sta)
486 stakey = rcu_dereference(rx->sta->key);
487
488 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
489 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200490 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400491 /*
492 * The device doesn't give us the IV so we won't be
493 * able to look up the key. That's ok though, we
494 * don't need to decrypt the frame, we just won't
495 * be able to keep statistics accurate.
496 * Except for key threshold notifications, should
497 * we somehow allow the driver to tell us which key
498 * the hardware used if this flag is set?
499 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400500 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
501 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400502 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200503
Johannes Berg3017b802007-08-28 17:01:53 -0400504 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200505
Johannes Berg3017b802007-08-28 17:01:53 -0400506 if (rx->skb->len < 8 + hdrlen)
507 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200508
Johannes Berg3017b802007-08-28 17:01:53 -0400509 /*
510 * no need to call ieee80211_wep_get_keyidx,
511 * it verifies a bunch of things we've done already
512 */
513 keyidx = rx->skb->data[hdrlen + 3] >> 6;
514
Johannes Bergd4e46a32007-09-14 11:10:24 -0400515 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400516
517 /*
518 * RSNA-protected unicast frames should always be sent with
519 * pairwise or station-to-station keys, but for WEP we allow
520 * using a key index as well.
521 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400522 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400523 !is_multicast_ether_addr(hdr->addr1))
524 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200525 }
526
Johannes Berg3017b802007-08-28 17:01:53 -0400527 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200528 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400529 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200530 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500531#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200532 if (net_ratelimit())
533 printk(KERN_DEBUG "%s: RX protected frame,"
534 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500535#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berg70f08762007-09-26 17:53:14 +0200536 return TXRX_DROP;
537 }
538
Johannes Berg1990af82007-09-26 17:53:15 +0200539 /* Check for weak IVs if possible */
540 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
541 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
542 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
543 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
544 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
545 rx->sta->wep_weak_iv_count++;
546
Johannes Berg70f08762007-09-26 17:53:14 +0200547 switch (rx->key->conf.alg) {
548 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200549 result = ieee80211_crypto_wep_decrypt(rx);
550 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200551 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200552 result = ieee80211_crypto_tkip_decrypt(rx);
553 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200554 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200555 result = ieee80211_crypto_ccmp_decrypt(rx);
556 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200557 }
558
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200559 /* either the frame has been decrypted or will be dropped */
560 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
561
562 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200563}
564
Johannes Berg571ecf62007-07-27 15:43:22 +0200565static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
566{
567 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700568 DECLARE_MAC_BUF(mac);
569
Johannes Berg571ecf62007-07-27 15:43:22 +0200570 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
571
572 if (sdata->bss)
573 atomic_inc(&sdata->bss->num_sta_ps);
574 sta->flags |= WLAN_STA_PS;
575 sta->pspoll = 0;
576#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700577 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
578 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200579#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
580}
581
582static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
583{
584 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
585 struct sk_buff *skb;
586 int sent = 0;
587 struct ieee80211_sub_if_data *sdata;
588 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700589 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200590
591 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
592 if (sdata->bss)
593 atomic_dec(&sdata->bss->num_sta_ps);
594 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
595 sta->pspoll = 0;
596 if (!skb_queue_empty(&sta->ps_tx_buf)) {
597 if (local->ops->set_tim)
598 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
599 if (sdata->bss)
600 bss_tim_clear(local, sdata->bss, sta->aid);
601 }
602#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700603 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
604 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200605#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
606 /* Send all buffered frames to the station */
607 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
608 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
609 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400610 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200611 dev_queue_xmit(skb);
612 }
613 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
614 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
615 local->total_ps_buffered--;
616 sent++;
617#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700618 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200619 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700620 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200621#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400622 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200623 dev_queue_xmit(skb);
624 }
625
626 return sent;
627}
628
629static ieee80211_txrx_result
630ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
631{
632 struct sta_info *sta = rx->sta;
633 struct net_device *dev = rx->dev;
634 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
635
636 if (!sta)
637 return TXRX_CONTINUE;
638
639 /* Update last_rx only for IBSS packets which are for the current
640 * BSSID to avoid keeping the current IBSS network alive in cases where
641 * other STAs are using different BSSID. */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100642 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Ron Rindjunsky71364712007-12-25 17:00:36 +0200643 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
644 IEEE80211_IF_TYPE_IBSS);
Johannes Berg571ecf62007-07-27 15:43:22 +0200645 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
646 sta->last_rx = jiffies;
647 } else
648 if (!is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100649 rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200650 /* Update last_rx only for unicast frames in order to prevent
651 * the Probe Request frames (the only broadcast frames from a
652 * STA in infrastructure mode) from keeping a connection alive.
653 */
654 sta->last_rx = jiffies;
655 }
656
Jiri Slabybadffb72007-08-28 17:01:54 -0400657 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200658 return TXRX_CONTINUE;
659
660 sta->rx_fragments++;
661 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400662 sta->last_rssi = rx->u.rx.status->ssi;
663 sta->last_signal = rx->u.rx.status->signal;
664 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200665
666 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
667 /* Change STA power saving mode only in the end of a frame
668 * exchange sequence */
669 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
670 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
671 else if (!(sta->flags & WLAN_STA_PS) &&
672 (rx->fc & IEEE80211_FCTL_PM))
673 ap_sta_ps_start(dev, sta);
674 }
675
676 /* Drop data::nullfunc frames silently, since they are used only to
677 * control station power saving mode. */
678 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
679 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
680 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
681 /* Update counter and free packet here to avoid counting this
682 * as a dropped packed. */
683 sta->rx_packets++;
684 dev_kfree_skb(rx->skb);
685 return TXRX_QUEUED;
686 }
687
688 return TXRX_CONTINUE;
689} /* ieee80211_rx_h_sta_process */
690
Johannes Berg571ecf62007-07-27 15:43:22 +0200691static inline struct ieee80211_fragment_entry *
692ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
693 unsigned int frag, unsigned int seq, int rx_queue,
694 struct sk_buff **skb)
695{
696 struct ieee80211_fragment_entry *entry;
697 int idx;
698
699 idx = sdata->fragment_next;
700 entry = &sdata->fragments[sdata->fragment_next++];
701 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
702 sdata->fragment_next = 0;
703
704 if (!skb_queue_empty(&entry->skb_list)) {
705#ifdef CONFIG_MAC80211_DEBUG
706 struct ieee80211_hdr *hdr =
707 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700708 DECLARE_MAC_BUF(mac);
709 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200710 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
711 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700712 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200713 sdata->dev->name, idx,
714 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700715 entry->last_frag, print_mac(mac, hdr->addr1),
716 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200717#endif /* CONFIG_MAC80211_DEBUG */
718 __skb_queue_purge(&entry->skb_list);
719 }
720
721 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
722 *skb = NULL;
723 entry->first_frag_time = jiffies;
724 entry->seq = seq;
725 entry->rx_queue = rx_queue;
726 entry->last_frag = frag;
727 entry->ccmp = 0;
728 entry->extra_len = 0;
729
730 return entry;
731}
732
733static inline struct ieee80211_fragment_entry *
734ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
735 u16 fc, unsigned int frag, unsigned int seq,
736 int rx_queue, struct ieee80211_hdr *hdr)
737{
738 struct ieee80211_fragment_entry *entry;
739 int i, idx;
740
741 idx = sdata->fragment_next;
742 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
743 struct ieee80211_hdr *f_hdr;
744 u16 f_fc;
745
746 idx--;
747 if (idx < 0)
748 idx = IEEE80211_FRAGMENT_MAX - 1;
749
750 entry = &sdata->fragments[idx];
751 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
752 entry->rx_queue != rx_queue ||
753 entry->last_frag + 1 != frag)
754 continue;
755
756 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
757 f_fc = le16_to_cpu(f_hdr->frame_control);
758
759 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
760 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
761 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
762 continue;
763
764 if (entry->first_frag_time + 2 * HZ < jiffies) {
765 __skb_queue_purge(&entry->skb_list);
766 continue;
767 }
768 return entry;
769 }
770
771 return NULL;
772}
773
774static ieee80211_txrx_result
775ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
776{
777 struct ieee80211_hdr *hdr;
778 u16 sc;
779 unsigned int frag, seq;
780 struct ieee80211_fragment_entry *entry;
781 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700782 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200783
784 hdr = (struct ieee80211_hdr *) rx->skb->data;
785 sc = le16_to_cpu(hdr->seq_ctrl);
786 frag = sc & IEEE80211_SCTL_FRAG;
787
788 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
789 (rx->skb)->len < 24 ||
790 is_multicast_ether_addr(hdr->addr1))) {
791 /* not fragmented */
792 goto out;
793 }
794 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
795
796 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
797
798 if (frag == 0) {
799 /* This is the first fragment of a new frame. */
800 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
801 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400802 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200803 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
804 /* Store CCMP PN so that we can verify that the next
805 * fragment has a sequential PN value. */
806 entry->ccmp = 1;
807 memcpy(entry->last_pn,
808 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
809 CCMP_PN_LEN);
810 }
811 return TXRX_QUEUED;
812 }
813
814 /* This is a fragment for a frame that should already be pending in
815 * fragment cache. Add this fragment to the end of the pending entry.
816 */
817 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
818 rx->u.rx.queue, hdr);
819 if (!entry) {
820 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
821 return TXRX_DROP;
822 }
823
824 /* Verify that MPDUs within one MSDU have sequential PN values.
825 * (IEEE 802.11i, 8.3.3.4.5) */
826 if (entry->ccmp) {
827 int i;
828 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400829 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200830 return TXRX_DROP;
831 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
832 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
833 pn[i]++;
834 if (pn[i])
835 break;
836 }
837 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
838 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400839 if (net_ratelimit())
840 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700841 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400842 " PN=%02x%02x%02x%02x%02x%02x "
843 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700844 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400845 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
846 rpn[5], pn[0], pn[1], pn[2], pn[3],
847 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200848 return TXRX_DROP;
849 }
850 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
851 }
852
853 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
854 __skb_queue_tail(&entry->skb_list, rx->skb);
855 entry->last_frag = frag;
856 entry->extra_len += rx->skb->len;
857 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
858 rx->skb = NULL;
859 return TXRX_QUEUED;
860 }
861
862 rx->skb = __skb_dequeue(&entry->skb_list);
863 if (skb_tailroom(rx->skb) < entry->extra_len) {
864 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
865 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
866 GFP_ATOMIC))) {
867 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
868 __skb_queue_purge(&entry->skb_list);
869 return TXRX_DROP;
870 }
871 }
872 while ((skb = __skb_dequeue(&entry->skb_list))) {
873 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
874 dev_kfree_skb(skb);
875 }
876
877 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400878 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200879
880 out:
881 if (rx->sta)
882 rx->sta->rx_packets++;
883 if (is_multicast_ether_addr(hdr->addr1))
884 rx->local->dot11MulticastReceivedFrameCount++;
885 else
886 ieee80211_led_rx(rx->local);
887 return TXRX_CONTINUE;
888}
889
890static ieee80211_txrx_result
891ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
892{
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200893 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg571ecf62007-07-27 15:43:22 +0200894 struct sk_buff *skb;
895 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700896 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200897
898 if (likely(!rx->sta ||
899 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
900 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400901 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200902 return TXRX_CONTINUE;
903
Johannes Berg51fb61e2007-12-19 01:31:27 +0100904 if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
905 (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +0200906 return TXRX_DROP;
907
Johannes Berg571ecf62007-07-27 15:43:22 +0200908 skb = skb_dequeue(&rx->sta->tx_filtered);
909 if (!skb) {
910 skb = skb_dequeue(&rx->sta->ps_tx_buf);
911 if (skb)
912 rx->local->total_ps_buffered--;
913 }
914 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
915 skb_queue_empty(&rx->sta->ps_tx_buf);
916
917 if (skb) {
918 struct ieee80211_hdr *hdr =
919 (struct ieee80211_hdr *) skb->data;
920
921 /* tell TX path to send one frame even though the STA may
922 * still remain is PS mode after this frame exchange */
923 rx->sta->pspoll = 1;
924
925#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700926 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
927 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200928 skb_queue_len(&rx->sta->ps_tx_buf));
929#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
930
931 /* Use MoreData flag to indicate whether there are more
932 * buffered frames for this STA */
933 if (no_pending_pkts) {
934 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
935 rx->sta->flags &= ~WLAN_STA_TIM;
936 } else
937 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
938
939 dev_queue_xmit(skb);
940
941 if (no_pending_pkts) {
942 if (rx->local->ops->set_tim)
943 rx->local->ops->set_tim(local_to_hw(rx->local),
944 rx->sta->aid, 0);
945 if (rx->sdata->bss)
946 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
947 }
948#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
949 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700950 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200951 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700952 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200953#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
954
955 }
956
957 /* Free PS Poll skb here instead of returning TXRX_DROP that would
958 * count as an dropped frame. */
959 dev_kfree_skb(rx->skb);
960
961 return TXRX_QUEUED;
962}
963
964static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200965ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
966{
967 u16 fc = rx->fc;
968 u8 *data = rx->skb->data;
969 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
970
971 if (!WLAN_FC_IS_QOS_DATA(fc))
972 return TXRX_CONTINUE;
973
974 /* remove the qos control field, update frame type and meta-data */
975 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
976 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
977 /* change frame type to non QOS */
978 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
979 hdr->frame_control = cpu_to_le16(fc);
980
981 return TXRX_CONTINUE;
982}
983
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200984static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100985ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +0200986{
Johannes Bergce3edf6d02007-12-19 01:31:22 +0100987 if (unlikely(rx->sdata->ieee802_1x_pac &&
988 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200989#ifdef CONFIG_MAC80211_DEBUG
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200990 printk(KERN_DEBUG "%s: dropped frame "
991 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200992#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200993 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +0200994 }
995
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200996 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +0200997}
998
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200999static int
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001000ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001001{
Johannes Berg3017b802007-08-28 17:01:53 -04001002 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001003 * Pass through unencrypted frames if the hardware has
1004 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001005 */
Johannes Berg7848ba72007-09-14 11:10:25 -04001006 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001007 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001008
1009 /* Drop unencrypted frames if key is set. */
1010 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1011 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1012 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001013 (rx->key || rx->sdata->drop_unencrypted))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001014 if (net_ratelimit())
1015 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1016 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001017 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001018 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001019 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001020}
1021
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001022static int
1023ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001024{
1025 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001026 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1027 u16 fc, hdrlen, ethertype;
1028 u8 *payload;
1029 u8 dst[ETH_ALEN];
1030 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001031 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001032 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001033 DECLARE_MAC_BUF(mac);
1034 DECLARE_MAC_BUF(mac2);
1035 DECLARE_MAC_BUF(mac3);
1036 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001037
1038 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001039
1040 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001041 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001042
1043 hdrlen = ieee80211_get_hdrlen(fc);
1044
1045 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1046 * header
1047 * IEEE 802.11 address fields:
1048 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1049 * 0 0 DA SA BSSID n/a
1050 * 0 1 DA BSSID SA n/a
1051 * 1 0 BSSID SA DA n/a
1052 * 1 1 RA TA DA SA
1053 */
1054
1055 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1056 case IEEE80211_FCTL_TODS:
1057 /* BSSID SA DA */
1058 memcpy(dst, hdr->addr3, ETH_ALEN);
1059 memcpy(src, hdr->addr2, ETH_ALEN);
1060
Johannes Berg51fb61e2007-12-19 01:31:27 +01001061 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
1062 sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001063 if (net_ratelimit())
1064 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001065 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001066 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001067 print_mac(mac, hdr->addr1),
1068 print_mac(mac2, hdr->addr2),
1069 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001070 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001071 }
1072 break;
1073 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1074 /* RA TA DA SA */
1075 memcpy(dst, hdr->addr3, ETH_ALEN);
1076 memcpy(src, hdr->addr4, ETH_ALEN);
1077
Johannes Berg51fb61e2007-12-19 01:31:27 +01001078 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001079 if (net_ratelimit())
1080 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001081 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001082 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001083 print_mac(mac, hdr->addr1),
1084 print_mac(mac2, hdr->addr2),
1085 print_mac(mac3, hdr->addr3),
1086 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001087 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001088 }
1089 break;
1090 case IEEE80211_FCTL_FROMDS:
1091 /* DA BSSID SA */
1092 memcpy(dst, hdr->addr1, ETH_ALEN);
1093 memcpy(src, hdr->addr3, ETH_ALEN);
1094
Johannes Berg51fb61e2007-12-19 01:31:27 +01001095 if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
John W. Linvilleb3316152007-08-28 17:01:55 -04001096 (is_multicast_ether_addr(dst) &&
1097 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001098 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001099 break;
1100 case 0:
1101 /* DA SA BSSID */
1102 memcpy(dst, hdr->addr1, ETH_ALEN);
1103 memcpy(src, hdr->addr2, ETH_ALEN);
1104
Johannes Berg51fb61e2007-12-19 01:31:27 +01001105 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001106 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001107 printk(KERN_DEBUG "%s: dropped IBSS frame "
1108 "(DA=%s SA=%s BSSID=%s)\n",
1109 dev->name,
1110 print_mac(mac, hdr->addr1),
1111 print_mac(mac2, hdr->addr2),
1112 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001113 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001114 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001115 }
1116 break;
1117 }
1118
Johannes Berg571ecf62007-07-27 15:43:22 +02001119 if (unlikely(skb->len - hdrlen < 8)) {
1120 if (net_ratelimit()) {
1121 printk(KERN_DEBUG "%s: RX too short data frame "
1122 "payload\n", dev->name);
1123 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001124 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001125 }
1126
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001127 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001128 ethertype = (payload[6] << 8) | payload[7];
1129
1130 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1131 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1132 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1133 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1134 * replace EtherType */
1135 skb_pull(skb, hdrlen + 6);
1136 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1137 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1138 } else {
1139 struct ethhdr *ehdr;
1140 __be16 len;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001141
Johannes Berg571ecf62007-07-27 15:43:22 +02001142 skb_pull(skb, hdrlen);
1143 len = htons(skb->len);
1144 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1145 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1146 memcpy(ehdr->h_source, src, ETH_ALEN);
1147 ehdr->h_proto = len;
1148 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001149 return 0;
1150}
Johannes Berg571ecf62007-07-27 15:43:22 +02001151
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001152/*
1153 * requires that rx->skb is a frame with ethernet header
1154 */
1155static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1156{
1157 static const u8 pae_group_addr[ETH_ALEN]
1158 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1159 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1160
1161 /*
1162 * Allow EAPOL frames to us/the PAE group address regardless
1163 * of whether the frame was encrypted or not.
1164 */
1165 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1166 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1167 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1168 return true;
1169
1170 if (ieee80211_802_1x_port_control(rx) ||
1171 ieee80211_drop_unencrypted(rx))
1172 return false;
1173
1174 return true;
1175}
1176
1177/*
1178 * requires that rx->skb is a frame with ethernet header
1179 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001180static void
1181ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1182{
1183 struct net_device *dev = rx->dev;
1184 struct ieee80211_local *local = rx->local;
1185 struct sk_buff *skb, *xmit_skb;
1186 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001187 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1188 struct sta_info *dsta;
Johannes Berg571ecf62007-07-27 15:43:22 +02001189
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001190 skb = rx->skb;
1191 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001192
Johannes Berg51fb61e2007-12-19 01:31:27 +01001193 if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
1194 sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
Jiri Slabybadffb72007-08-28 17:01:54 -04001195 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001196 if (is_multicast_ether_addr(ehdr->h_dest)) {
1197 /*
1198 * send multicast frames both to higher layers in
1199 * local net stack and back to the wireless medium
1200 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001201 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1202 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001203 printk(KERN_DEBUG "%s: failed to clone "
1204 "multicast frame\n", dev->name);
1205 } else {
Johannes Berg571ecf62007-07-27 15:43:22 +02001206 dsta = sta_info_get(local, skb->data);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001207 if (dsta && dsta->dev == dev) {
1208 /*
1209 * The destination station is associated to
1210 * this AP (in this VLAN), so send the frame
1211 * directly to it and do not pass it to local
1212 * net stack.
Johannes Berg571ecf62007-07-27 15:43:22 +02001213 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001214 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001215 skb = NULL;
1216 }
1217 if (dsta)
1218 sta_info_put(dsta);
1219 }
1220 }
1221
1222 if (skb) {
1223 /* deliver to local stack */
1224 skb->protocol = eth_type_trans(skb, dev);
1225 memset(skb->cb, 0, sizeof(skb->cb));
1226 netif_rx(skb);
1227 }
1228
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001229 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001230 /* send to wireless media */
YOSHIFUJI Hideakif831e902007-12-12 03:54:23 +09001231 xmit_skb->protocol = htons(ETH_P_802_3);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001232 skb_reset_network_header(xmit_skb);
1233 skb_reset_mac_header(xmit_skb);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001234 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001235 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001236}
1237
1238static ieee80211_txrx_result
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001239ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1240{
1241 struct net_device *dev = rx->dev;
1242 struct ieee80211_local *local = rx->local;
1243 u16 fc, ethertype;
1244 u8 *payload;
1245 struct sk_buff *skb = rx->skb, *frame = NULL;
1246 const struct ethhdr *eth;
1247 int remaining, err;
1248 u8 dst[ETH_ALEN];
1249 u8 src[ETH_ALEN];
1250 DECLARE_MAC_BUF(mac);
1251
1252 fc = rx->fc;
1253 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1254 return TXRX_CONTINUE;
1255
1256 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1257 return TXRX_DROP;
1258
Ron Rindjunsky64bd4b62007-11-29 10:35:53 +02001259 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001260 return TXRX_CONTINUE;
1261
1262 err = ieee80211_data_to_8023(rx);
1263 if (unlikely(err))
1264 return TXRX_DROP;
1265
1266 skb->dev = dev;
1267
1268 dev->stats.rx_packets++;
1269 dev->stats.rx_bytes += skb->len;
1270
1271 /* skip the wrapping header */
1272 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1273 if (!eth)
1274 return TXRX_DROP;
1275
1276 while (skb != frame) {
1277 u8 padding;
1278 __be16 len = eth->h_proto;
1279 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1280
1281 remaining = skb->len;
1282 memcpy(dst, eth->h_dest, ETH_ALEN);
1283 memcpy(src, eth->h_source, ETH_ALEN);
1284
1285 padding = ((4 - subframe_len) & 0x3);
1286 /* the last MSDU has no padding */
1287 if (subframe_len > remaining) {
1288 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1289 return TXRX_DROP;
1290 }
1291
1292 skb_pull(skb, sizeof(struct ethhdr));
1293 /* if last subframe reuse skb */
1294 if (remaining <= subframe_len + padding)
1295 frame = skb;
1296 else {
1297 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1298 subframe_len);
1299
1300 if (frame == NULL)
1301 return TXRX_DROP;
1302
1303 skb_reserve(frame, local->hw.extra_tx_headroom +
1304 sizeof(struct ethhdr));
1305 memcpy(skb_put(frame, ntohs(len)), skb->data,
1306 ntohs(len));
1307
1308 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1309 padding);
1310 if (!eth) {
1311 printk(KERN_DEBUG "%s: wrong buffer size ",
1312 dev->name);
1313 dev_kfree_skb(frame);
1314 return TXRX_DROP;
1315 }
1316 }
1317
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001318 skb_reset_network_header(frame);
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001319 frame->dev = dev;
1320 frame->priority = skb->priority;
1321 rx->skb = frame;
1322
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001323 payload = frame->data;
1324 ethertype = (payload[6] << 8) | payload[7];
1325
1326 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001327 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1328 compare_ether_addr(payload,
1329 bridge_tunnel_header) == 0)) {
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001330 /* remove RFC1042 or Bridge-Tunnel
1331 * encapsulation and replace EtherType */
1332 skb_pull(frame, 6);
1333 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1334 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1335 } else {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001336 memcpy(skb_push(frame, sizeof(__be16)),
1337 &len, sizeof(__be16));
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001338 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1339 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1340 }
1341
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001342 if (!ieee80211_frame_allowed(rx)) {
1343 if (skb == frame) /* last frame */
1344 return TXRX_DROP;
1345 dev_kfree_skb(frame);
1346 continue;
1347 }
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001348
1349 ieee80211_deliver_skb(rx);
1350 }
1351
1352 return TXRX_QUEUED;
1353}
1354
1355static ieee80211_txrx_result
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001356ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1357{
1358 struct net_device *dev = rx->dev;
1359 u16 fc;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001360 int err;
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001361
1362 fc = rx->fc;
1363 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1364 return TXRX_CONTINUE;
1365
1366 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1367 return TXRX_DROP;
1368
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001369 err = ieee80211_data_to_8023(rx);
1370 if (unlikely(err))
1371 return TXRX_DROP;
1372
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001373 if (!ieee80211_frame_allowed(rx))
1374 return TXRX_DROP;
1375
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001376 rx->skb->dev = dev;
1377
1378 dev->stats.rx_packets++;
1379 dev->stats.rx_bytes += rx->skb->len;
1380
1381 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001382
1383 return TXRX_QUEUED;
1384}
1385
1386static ieee80211_txrx_result
Ron Rindjunsky71364712007-12-25 17:00:36 +02001387ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
1388{
1389 struct ieee80211_local *local = rx->local;
1390 struct ieee80211_hw *hw = &local->hw;
1391 struct sk_buff *skb = rx->skb;
1392 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
1393 struct tid_ampdu_rx *tid_agg_rx;
1394 u16 start_seq_num;
1395 u16 tid;
1396
1397 if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
1398 return TXRX_CONTINUE;
1399
1400 if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
1401 if (!rx->sta)
1402 return TXRX_CONTINUE;
1403 tid = le16_to_cpu(bar->control) >> 12;
1404 tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
1405 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1406 return TXRX_CONTINUE;
1407
1408 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1409
1410 /* reset session timer */
1411 if (tid_agg_rx->timeout) {
1412 unsigned long expires =
1413 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1414 mod_timer(&tid_agg_rx->session_timer, expires);
1415 }
1416
1417 /* manage reordering buffer according to requested */
1418 /* sequence number */
1419 rcu_read_lock();
1420 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1421 start_seq_num, 1);
1422 rcu_read_unlock();
1423 return TXRX_DROP;
1424 }
1425
1426 return TXRX_CONTINUE;
1427}
1428
1429static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +02001430ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1431{
1432 struct ieee80211_sub_if_data *sdata;
1433
Jiri Slabybadffb72007-08-28 17:01:54 -04001434 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001435 return TXRX_DROP;
1436
1437 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001438 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
1439 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001440 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg571ecf62007-07-27 15:43:22 +02001441 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001442 else
1443 return TXRX_DROP;
1444
Johannes Berg571ecf62007-07-27 15:43:22 +02001445 return TXRX_QUEUED;
1446}
1447
1448static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1449 struct ieee80211_local *local,
1450 ieee80211_rx_handler *handlers,
1451 struct ieee80211_txrx_data *rx,
1452 struct sta_info *sta)
1453{
1454 ieee80211_rx_handler *handler;
1455 ieee80211_txrx_result res = TXRX_DROP;
1456
1457 for (handler = handlers; *handler != NULL; handler++) {
1458 res = (*handler)(rx);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001459
1460 switch (res) {
1461 case TXRX_CONTINUE:
1462 continue;
1463 case TXRX_DROP:
1464 I802_DEBUG_INC(local->rx_handlers_drop);
1465 if (sta)
1466 sta->rx_dropped++;
1467 break;
1468 case TXRX_QUEUED:
1469 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001470 break;
1471 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001472 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001473 }
1474
Johannes Berg8e6f0032007-07-27 15:43:22 +02001475 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001476 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001477 return res;
1478}
1479
1480static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1481 ieee80211_rx_handler *handlers,
1482 struct ieee80211_txrx_data *rx,
1483 struct sta_info *sta)
1484{
1485 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1486 TXRX_CONTINUE)
1487 dev_kfree_skb(rx->skb);
1488}
1489
1490static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1491 struct ieee80211_hdr *hdr,
1492 struct sta_info *sta,
1493 struct ieee80211_txrx_data *rx)
1494{
1495 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001496 DECLARE_MAC_BUF(mac);
1497 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001498
1499 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1500 if (rx->skb->len >= hdrlen + 4)
1501 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1502 else
1503 keyidx = -1;
1504
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001505 if (net_ratelimit())
1506 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001507 "failure from %s to %s keyidx=%d\n",
1508 dev->name, print_mac(mac, hdr->addr2),
1509 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001510
1511 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001512 /*
1513 * Some hardware seem to generate incorrect Michael MIC
1514 * reports; ignore them to avoid triggering countermeasures.
1515 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001516 if (net_ratelimit())
1517 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001518 "error for unknown address %s\n",
1519 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001520 goto ignore;
1521 }
1522
1523 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001524 if (net_ratelimit())
1525 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001526 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001527 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001528 goto ignore;
1529 }
1530
Johannes Berg51fb61e2007-12-19 01:31:27 +01001531 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001532 /*
1533 * APs with pairwise keys should never receive Michael MIC
1534 * errors for non-zero keyidx because these are reserved for
1535 * group keys and only the AP is sending real multicast
1536 * frames in the BSS.
1537 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001538 if (net_ratelimit())
1539 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1540 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001541 " (src %s)\n", dev->name, keyidx,
1542 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001543 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001544 }
1545
1546 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1547 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1548 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001549 if (net_ratelimit())
1550 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1551 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001552 "(fc=0x%04x) (src %s)\n",
1553 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001554 goto ignore;
1555 }
1556
Johannes Bergeb063c12007-08-28 17:01:53 -04001557 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001558 ignore:
1559 dev_kfree_skb(rx->skb);
1560 rx->skb = NULL;
1561}
1562
1563ieee80211_rx_handler ieee80211_rx_handlers[] =
1564{
1565 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001566 ieee80211_rx_h_passive_scan,
1567 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001568 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001569 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001570 ieee80211_rx_h_defragment,
1571 ieee80211_rx_h_ps_poll,
1572 ieee80211_rx_h_michael_mic_verify,
1573 /* this must be after decryption - so header is counted in MPDU mic
1574 * must be before pae and data, so QOS_DATA format frames
1575 * are not passed to user space by these functions
1576 */
1577 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001578 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001579 ieee80211_rx_h_data,
Ron Rindjunsky71364712007-12-25 17:00:36 +02001580 ieee80211_rx_h_ctrl,
Johannes Berg571ecf62007-07-27 15:43:22 +02001581 ieee80211_rx_h_mgmt,
1582 NULL
1583};
1584
1585/* main receive path */
1586
Johannes Berg23a24de2007-07-27 15:43:22 +02001587static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1588 u8 *bssid, struct ieee80211_txrx_data *rx,
1589 struct ieee80211_hdr *hdr)
1590{
1591 int multicast = is_multicast_ether_addr(hdr->addr1);
1592
Johannes Berg51fb61e2007-12-19 01:31:27 +01001593 switch (sdata->vif.type) {
Johannes Berg23a24de2007-07-27 15:43:22 +02001594 case IEEE80211_IF_TYPE_STA:
1595 if (!bssid)
1596 return 0;
1597 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001598 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001599 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001600 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001601 } else if (!multicast &&
1602 compare_ether_addr(sdata->dev->dev_addr,
1603 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001604 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001605 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001606 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001607 }
1608 break;
1609 case IEEE80211_IF_TYPE_IBSS:
1610 if (!bssid)
1611 return 0;
1612 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001613 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001614 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001615 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001616 } else if (!multicast &&
1617 compare_ether_addr(sdata->dev->dev_addr,
1618 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001619 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001620 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001621 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001622 } else if (!rx->sta)
1623 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1624 bssid, hdr->addr2);
1625 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001626 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001627 case IEEE80211_IF_TYPE_AP:
1628 if (!bssid) {
1629 if (compare_ether_addr(sdata->dev->dev_addr,
1630 hdr->addr1))
1631 return 0;
1632 } else if (!ieee80211_bssid_match(bssid,
1633 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001634 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001635 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001636 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001637 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001638 if (sdata->dev == sdata->local->mdev &&
1639 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001640 /* do not receive anything via
1641 * master device when not scanning */
1642 return 0;
1643 break;
1644 case IEEE80211_IF_TYPE_WDS:
1645 if (bssid ||
1646 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1647 return 0;
1648 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1649 return 0;
1650 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001651 case IEEE80211_IF_TYPE_MNTR:
1652 /* take everything */
1653 break;
Johannes Berga2897552007-09-28 14:01:25 +02001654 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001655 /* should never get here */
1656 WARN_ON(1);
1657 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001658 }
1659
1660 return 1;
1661}
1662
Johannes Berg571ecf62007-07-27 15:43:22 +02001663/*
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001664 * This is the actual Rx frames handler. as it blongs to Rx path it must
1665 * be called with rcu_read_lock protection.
Johannes Berg571ecf62007-07-27 15:43:22 +02001666 */
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001667static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
1668 struct sk_buff *skb,
1669 struct ieee80211_rx_status *status,
1670 u32 load)
Johannes Berg571ecf62007-07-27 15:43:22 +02001671{
1672 struct ieee80211_local *local = hw_to_local(hw);
1673 struct ieee80211_sub_if_data *sdata;
1674 struct sta_info *sta;
1675 struct ieee80211_hdr *hdr;
1676 struct ieee80211_txrx_data rx;
1677 u16 type;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001678 int prepares;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001679 struct ieee80211_sub_if_data *prev = NULL;
1680 struct sk_buff *skb_new;
1681 u8 *bssid;
David S. Millerd10f2152008-01-24 16:57:39 -08001682 int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001683
1684 hdr = (struct ieee80211_hdr *) skb->data;
1685 memset(&rx, 0, sizeof(rx));
1686 rx.skb = skb;
1687 rx.local = local;
1688
1689 rx.u.rx.status = status;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001690 rx.u.rx.load = load;
Johannes Bergb2e77712007-09-26 15:19:39 +02001691 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001692 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001693
David S. Millerd10f2152008-01-24 16:57:39 -08001694 /*
1695 * Drivers are required to align the payload data to a four-byte
1696 * boundary, so the last two bits of the address where it starts
1697 * may not be set. The header is required to be directly before
1698 * the payload data, padding like atheros hardware adds which is
1699 * inbetween the 802.11 header and the payload is not supported,
1700 * the driver is required to move the 802.11 header further back
1701 * in that case.
1702 */
1703 hdrlen = ieee80211_get_hdrlen(rx.fc);
1704 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1705
Johannes Bergb2e77712007-09-26 15:19:39 +02001706 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001707 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001708
Johannes Bergb2e77712007-09-26 15:19:39 +02001709 sta = rx.sta = sta_info_get(local, hdr->addr2);
1710 if (sta) {
1711 rx.dev = rx.sta->dev;
1712 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1713 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001714
Johannes Berg571ecf62007-07-27 15:43:22 +02001715 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1716 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1717 goto end;
1718 }
1719
Zhu Yiece8edd2007-11-22 10:53:21 +08001720 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001721 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001722
1723 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1724 sta) != TXRX_CONTINUE)
1725 goto end;
1726 skb = rx.skb;
1727
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001728 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001729 !atomic_read(&local->iff_promiscs) &&
1730 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001731 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001732 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001733 rx.sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001734 sta_info_put(sta);
1735 return;
1736 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001737
Johannes Berg79010422007-09-18 17:29:21 -04001738 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001739 if (!netif_running(sdata->dev))
1740 continue;
1741
Johannes Berg51fb61e2007-12-19 01:31:27 +01001742 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
Johannes Bergb2e77712007-09-26 15:19:39 +02001743 continue;
1744
Johannes Berg51fb61e2007-12-19 01:31:27 +01001745 bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
Johannes Bergb2e77712007-09-26 15:19:39 +02001746 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001747 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
Johannes Berg23a24de2007-07-27 15:43:22 +02001748 /* prepare_for_handlers can change sta */
1749 sta = rx.sta;
1750
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001751 if (!prepares)
Johannes Berg23a24de2007-07-27 15:43:22 +02001752 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001753
Johannes Berg340e11f2007-07-27 15:43:22 +02001754 /*
1755 * frame is destined for this interface, but if it's not
1756 * also for the previous one we handle that after the
1757 * loop to avoid copying the SKB once too much
1758 */
1759
1760 if (!prev) {
1761 prev = sdata;
1762 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001763 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001764
1765 /*
1766 * frame was destined for the previous interface
1767 * so invoke RX handlers for it
1768 */
1769
1770 skb_new = skb_copy(skb, GFP_ATOMIC);
1771 if (!skb_new) {
1772 if (net_ratelimit())
1773 printk(KERN_DEBUG "%s: failed to copy "
1774 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001775 wiphy_name(local->hw.wiphy),
1776 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001777 continue;
1778 }
Helmut Schaa69f817b2007-12-21 15:16:35 +01001779 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg340e11f2007-07-27 15:43:22 +02001780 rx.skb = skb_new;
1781 rx.dev = prev->dev;
1782 rx.sdata = prev;
1783 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1784 &rx, sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001785 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001786 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001787 if (prev) {
Helmut Schaa69f817b2007-12-21 15:16:35 +01001788 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001789 rx.skb = skb;
1790 rx.dev = prev->dev;
1791 rx.sdata = prev;
1792 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1793 &rx, sta);
1794 } else
1795 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001796
Johannes Berg8e6f0032007-07-27 15:43:22 +02001797 end:
Johannes Berg571ecf62007-07-27 15:43:22 +02001798 if (sta)
1799 sta_info_put(sta);
1800}
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001801
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001802#define SEQ_MODULO 0x1000
1803#define SEQ_MASK 0xfff
1804
1805static inline int seq_less(u16 sq1, u16 sq2)
1806{
1807 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1808}
1809
1810static inline u16 seq_inc(u16 sq)
1811{
1812 return ((sq + 1) & SEQ_MASK);
1813}
1814
1815static inline u16 seq_sub(u16 sq1, u16 sq2)
1816{
1817 return ((sq1 - sq2) & SEQ_MASK);
1818}
1819
1820
Ron Rindjunsky71364712007-12-25 17:00:36 +02001821/*
1822 * As it function blongs to Rx path it must be called with
1823 * the proper rcu_read_lock protection for its flow.
1824 */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001825u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
1826 struct tid_ampdu_rx *tid_agg_rx,
1827 struct sk_buff *skb, u16 mpdu_seq_num,
1828 int bar_req)
1829{
1830 struct ieee80211_local *local = hw_to_local(hw);
1831 struct ieee80211_rx_status status;
1832 u16 head_seq_num, buf_size;
1833 int index;
1834 u32 pkt_load;
1835
1836 buf_size = tid_agg_rx->buf_size;
1837 head_seq_num = tid_agg_rx->head_seq_num;
1838
1839 /* frame with out of date sequence number */
1840 if (seq_less(mpdu_seq_num, head_seq_num)) {
1841 dev_kfree_skb(skb);
1842 return 1;
1843 }
1844
1845 /* if frame sequence number exceeds our buffering window size or
1846 * block Ack Request arrived - release stored frames */
1847 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
1848 /* new head to the ordering buffer */
1849 if (bar_req)
1850 head_seq_num = mpdu_seq_num;
1851 else
1852 head_seq_num =
1853 seq_inc(seq_sub(mpdu_seq_num, buf_size));
1854 /* release stored frames up to new head to stack */
1855 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1856 index = seq_sub(tid_agg_rx->head_seq_num,
1857 tid_agg_rx->ssn)
1858 % tid_agg_rx->buf_size;
1859
1860 if (tid_agg_rx->reorder_buf[index]) {
1861 /* release the reordered frames to stack */
1862 memcpy(&status,
1863 tid_agg_rx->reorder_buf[index]->cb,
1864 sizeof(status));
1865 pkt_load = ieee80211_rx_load_stats(local,
1866 tid_agg_rx->reorder_buf[index],
1867 &status);
1868 __ieee80211_rx_handle_packet(hw,
1869 tid_agg_rx->reorder_buf[index],
1870 &status, pkt_load);
1871 tid_agg_rx->stored_mpdu_num--;
1872 tid_agg_rx->reorder_buf[index] = NULL;
1873 }
1874 tid_agg_rx->head_seq_num =
1875 seq_inc(tid_agg_rx->head_seq_num);
1876 }
1877 if (bar_req)
1878 return 1;
1879 }
1880
1881 /* now the new frame is always in the range of the reordering */
1882 /* buffer window */
1883 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
1884 % tid_agg_rx->buf_size;
1885 /* check if we already stored this frame */
1886 if (tid_agg_rx->reorder_buf[index]) {
1887 dev_kfree_skb(skb);
1888 return 1;
1889 }
1890
1891 /* if arrived mpdu is in the right order and nothing else stored */
1892 /* release it immediately */
1893 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1894 tid_agg_rx->stored_mpdu_num == 0) {
1895 tid_agg_rx->head_seq_num =
1896 seq_inc(tid_agg_rx->head_seq_num);
1897 return 0;
1898 }
1899
1900 /* put the frame in the reordering buffer */
1901 tid_agg_rx->reorder_buf[index] = skb;
1902 tid_agg_rx->stored_mpdu_num++;
1903 /* release the buffer until next missing frame */
1904 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
1905 % tid_agg_rx->buf_size;
1906 while (tid_agg_rx->reorder_buf[index]) {
1907 /* release the reordered frame back to stack */
1908 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
1909 sizeof(status));
1910 pkt_load = ieee80211_rx_load_stats(local,
1911 tid_agg_rx->reorder_buf[index],
1912 &status);
1913 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
1914 &status, pkt_load);
1915 tid_agg_rx->stored_mpdu_num--;
1916 tid_agg_rx->reorder_buf[index] = NULL;
1917 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
1918 index = seq_sub(tid_agg_rx->head_seq_num,
1919 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
1920 }
1921 return 1;
1922}
1923
Ron Rindjunsky71ebb4a2008-01-21 12:39:12 +02001924static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
1925 struct sk_buff *skb)
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001926{
1927 struct ieee80211_hw *hw = &local->hw;
1928 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1929 struct sta_info *sta;
1930 struct tid_ampdu_rx *tid_agg_rx;
1931 u16 fc, sc;
1932 u16 mpdu_seq_num;
1933 u8 ret = 0, *qc;
1934 int tid;
1935
1936 sta = sta_info_get(local, hdr->addr2);
1937 if (!sta)
1938 return ret;
1939
1940 fc = le16_to_cpu(hdr->frame_control);
1941
1942 /* filter the QoS data rx stream according to
1943 * STA/TID and check if this STA/TID is on aggregation */
1944 if (!WLAN_FC_IS_QOS_DATA(fc))
1945 goto end_reorder;
1946
1947 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
1948 tid = qc[0] & QOS_CONTROL_TID_MASK;
1949 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
1950
1951 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1952 goto end_reorder;
1953
1954 /* null data frames are excluded */
1955 if (unlikely(fc & IEEE80211_STYPE_QOS_NULLFUNC))
1956 goto end_reorder;
1957
1958 /* new un-ordered ampdu frame - process it */
1959
1960 /* reset session timer */
1961 if (tid_agg_rx->timeout) {
1962 unsigned long expires =
1963 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1964 mod_timer(&tid_agg_rx->session_timer, expires);
1965 }
1966
1967 /* if this mpdu is fragmented - terminate rx aggregation session */
1968 sc = le16_to_cpu(hdr->seq_ctrl);
1969 if (sc & IEEE80211_SCTL_FRAG) {
1970 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
1971 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
1972 ret = 1;
1973 goto end_reorder;
1974 }
1975
1976 /* according to mpdu sequence number deal with reordering buffer */
1977 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1978 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
1979 mpdu_seq_num, 0);
1980end_reorder:
1981 if (sta)
1982 sta_info_put(sta);
1983 return ret;
1984}
1985
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02001986/*
1987 * This is the receive path handler. It is called by a low level driver when an
1988 * 802.11 MPDU is received from the hardware.
1989 */
1990void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1991 struct ieee80211_rx_status *status)
1992{
1993 struct ieee80211_local *local = hw_to_local(hw);
1994 u32 pkt_load;
1995
1996 /*
1997 * key references and virtual interfaces are protected using RCU
1998 * and this requires that we are in a read-side RCU section during
1999 * receive processing
2000 */
2001 rcu_read_lock();
2002
2003 /*
2004 * Frames with failed FCS/PLCP checksum are not returned,
2005 * all other frames are returned without radiotap header
2006 * if it was previously present.
2007 * Also, frames with less than 16 bytes are dropped.
2008 */
2009 skb = ieee80211_rx_monitor(local, skb, status);
2010 if (!skb) {
2011 rcu_read_unlock();
2012 return;
2013 }
2014
2015 pkt_load = ieee80211_rx_load_stats(local, skb, status);
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002016 local->channel_use_raw += pkt_load;
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002017
Ron Rindjunskyb5807812007-12-25 17:00:35 +02002018 if (!ieee80211_rx_reorder_ampdu(local, skb))
2019 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
Ron Rindjunsky6368e4b2007-12-24 13:36:39 +02002020
2021 rcu_read_unlock();
2022}
Johannes Berg571ecf62007-07-27 15:43:22 +02002023EXPORT_SYMBOL(__ieee80211_rx);
2024
2025/* This is a version of the rx handler that can be called from hard irq
2026 * context. Post the skb on the queue and schedule the tasklet */
2027void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
2028 struct ieee80211_rx_status *status)
2029{
2030 struct ieee80211_local *local = hw_to_local(hw);
2031
2032 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2033
2034 skb->dev = local->mdev;
2035 /* copy status into skb->cb for use by tasklet */
2036 memcpy(skb->cb, status, sizeof(*status));
2037 skb->pkt_type = IEEE80211_RX_MSG;
2038 skb_queue_tail(&local->skb_queue, skb);
2039 tasklet_schedule(&local->tasklet);
2040}
2041EXPORT_SYMBOL(ieee80211_rx_irqsafe);