mac80211: A-MPDU Rx handling aggregation reordering
[linux-2.6-block.git] / net / mac80211 / rx.c
CommitLineData
571ecf67
JB
1/*
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>
d4e46a3d 16#include <linux/rcupdate.h>
571ecf67
JB
17#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
571ecf67
JB
22#include "wep.h"
23#include "wpa.h"
24#include "tkip.h"
25#include "wme.h"
26
b2e7771e
JB
27/*
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;
98f0b0a3
RR
64 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)))
b2e7771e
JB
68 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;
c49e5ea3
JB
84 struct ieee80211_radiotap_header *rthdr;
85 __le64 *rttsft = NULL;
86 struct ieee80211_rtap_fixed_data {
b2e7771e
JB
87 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;
c49e5ea3 94 } __attribute__ ((packed)) *rtfixed;
b2e7771e
JB
95 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
c49e5ea3
JB
111 /* room for radiotap header, always present fields and TSFT */
112 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
b2e7771e
JB
113
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 &&
c49e5ea3 140 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
141 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)) {
c49e5ea3
JB
159 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 }
b2e7771e
JB
165 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
166 memset(rthdr, 0, sizeof(*rthdr));
c49e5ea3
JB
167 memset(rtfixed, 0, sizeof(*rtfixed));
168 rthdr->it_present =
b2e7771e
JB
169 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));
c49e5ea3
JB
174 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 }
b2e7771e
JB
183
184 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
c49e5ea3 185 rtfixed->rx_flags = 0;
b2e7771e
JB
186 if (status->flag &
187 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
c49e5ea3 188 rtfixed->rx_flags |=
b2e7771e
JB
189 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
190
191 rate = ieee80211_get_rate(local, status->phymode,
192 status->rate);
193 if (rate)
c49e5ea3 194 rtfixed->rate = rate->rate / 5;
b2e7771e 195
c49e5ea3 196 rtfixed->chan_freq = cpu_to_le16(status->freq);
b2e7771e
JB
197
198 if (status->phymode == MODE_IEEE80211A)
c49e5ea3 199 rtfixed->chan_flags =
b2e7771e
JB
200 cpu_to_le16(IEEE80211_CHAN_OFDM |
201 IEEE80211_CHAN_5GHZ);
202 else
c49e5ea3 203 rtfixed->chan_flags =
b2e7771e
JB
204 cpu_to_le16(IEEE80211_CHAN_DYN |
205 IEEE80211_CHAN_2GHZ);
206
c49e5ea3
JB
207 rtfixed->antsignal = status->ssi;
208 rthdr->it_len = cpu_to_le16(rtap_len);
b2e7771e
JB
209 }
210
ce3edf6d 211 skb_reset_mac_header(skb);
b2e7771e
JB
212 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
571ecf67
JB
246/* pre-rx handlers
247 *
248 * these don't have dev/sdata fields in the rx data
52865dfd
JB
249 * The sta value should also not be used because it may
250 * be NULL even though a STA (in IBSS mode) will be added.
571ecf67
JB
251 */
252
6e0d114d
JB
253static ieee80211_txrx_result
254ieee80211_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;
fd4c7f2f 264 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
64bd4b69 265 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
fd4c7f2f 266 else
64bd4b69 267 rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
6e0d114d
JB
268 } 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 }
52865dfd 277
6e0d114d 278 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
52865dfd
JB
279 /* only a debug counter, sta might not be assigned properly yet */
280 if (rx->sta)
6e0d114d 281 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
6e0d114d
JB
282
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
6368e4b1
RR
291
292u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
293 struct sk_buff *skb,
294 struct ieee80211_rx_status *status)
571ecf67 295{
571ecf67
JB
296 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++) {
6368e4b1 309 if (mode->rates[i].val == status->rate) {
571ecf67
JB
310 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 ||
571ecf67
JB
319 (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;
571ecf67 333
6368e4b1 334 return load;
571ecf67
JB
335}
336
337ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
338{
339 ieee80211_rx_h_parse_qos,
571ecf67
JB
340 NULL
341};
342
343/* rx handlers */
344
345static ieee80211_txrx_result
346ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
347{
52865dfd
JB
348 if (rx->sta)
349 rx->sta->channel_use_raw += rx->u.rx.load;
571ecf67
JB
350 rx->sdata->channel_use_raw += rx->u.rx.load;
351 return TXRX_CONTINUE;
352}
353
571ecf67
JB
354static 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
ece8eddd
ZY
360 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);
571ecf67
JB
368 return TXRX_QUEUED;
369 }
370
badffb72 371 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
571ecf67
JB
372 /* 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;
571ecf67
JB
384 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)) {
badffb72 391 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
571ecf67
JB
392 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
571ecf67
JB
400 if (unlikely(rx->skb->len < 16)) {
401 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
402 return TXRX_DROP;
403 }
404
571ecf67
JB
405 /* 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)
badffb72 421 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
571ecf67
JB
422 /* Drop IBSS frames and frames for other hosts
423 * silently. */
424 return TXRX_DROP;
425 }
426
f9d540ee 427 return TXRX_DROP;
571ecf67
JB
428 }
429
570bd537
JB
430 return TXRX_CONTINUE;
431}
432
433
434static ieee80211_txrx_result
1990af8d 435ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
570bd537
JB
436{
437 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3017b80b
JB
438 int keyidx;
439 int hdrlen;
e2f036da 440 ieee80211_txrx_result result = TXRX_DROP;
d4e46a3d 441 struct ieee80211_key *stakey = NULL;
570bd537 442
3017b80b
JB
443 /*
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 *
8dc06a1c
JB
458 * 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 *
3017b80b
JB
462 * There is also a slight problem in IBSS mode: GTKs are negotiated
463 * with each station, that is something we don't currently handle.
8dc06a1c
JB
464 * 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.
3017b80b
JB
467 */
468
469 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
470 return TXRX_CONTINUE;
571ecf67 471
3017b80b 472 /*
1990af8d 473 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
474 * addressed to us nor a multicast frame.
475 */
badffb72 476 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
3017b80b
JB
477 return TXRX_CONTINUE;
478
d4e46a3d
JB
479 if (rx->sta)
480 stakey = rcu_dereference(rx->sta->key);
481
482 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
483 rx->key = stakey;
571ecf67 484 } else {
3017b80b
JB
485 /*
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 */
7848ba7d
JB
494 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
495 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
3017b80b
JB
496 return TXRX_CONTINUE;
497
498 hdrlen = ieee80211_get_hdrlen(rx->fc);
499
500 if (rx->skb->len < 8 + hdrlen)
501 return TXRX_DROP; /* TODO: count this? */
502
503 /*
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
d4e46a3d 509 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
3017b80b
JB
510
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 */
8f20fc24 516 if (rx->key && rx->key->conf.alg != ALG_WEP &&
3017b80b
JB
517 !is_multicast_ether_addr(hdr->addr1))
518 rx->key = NULL;
571ecf67
JB
519 }
520
3017b80b 521 if (rx->key) {
571ecf67 522 rx->key->tx_rx_count++;
011bfcc4 523 /* TODO: add threshold stuff again */
1990af8d 524 } else {
7f3ad894 525#ifdef CONFIG_MAC80211_DEBUG
70f08765
JB
526 if (net_ratelimit())
527 printk(KERN_DEBUG "%s: RX protected frame,"
528 " but have no key\n", rx->dev->name);
7f3ad894 529#endif /* CONFIG_MAC80211_DEBUG */
70f08765
JB
530 return TXRX_DROP;
531 }
532
1990af8d
JB
533 /* 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
70f08765
JB
541 switch (rx->key->conf.alg) {
542 case ALG_WEP:
e2f036da
MN
543 result = ieee80211_crypto_wep_decrypt(rx);
544 break;
70f08765 545 case ALG_TKIP:
e2f036da
MN
546 result = ieee80211_crypto_tkip_decrypt(rx);
547 break;
70f08765 548 case ALG_CCMP:
e2f036da
MN
549 result = ieee80211_crypto_ccmp_decrypt(rx);
550 break;
70f08765
JB
551 }
552
e2f036da
MN
553 /* either the frame has been decrypted or will be dropped */
554 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
555
556 return result;
70f08765
JB
557}
558
571ecf67
JB
559static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
560{
561 struct ieee80211_sub_if_data *sdata;
0795af57
JP
562 DECLARE_MAC_BUF(mac);
563
571ecf67
JB
564 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
0795af57
JP
571 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
572 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
573#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;
0795af57 583 DECLARE_MAC_BUF(mac);
571ecf67
JB
584
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
0795af57
JP
597 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
598 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
599#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++;
e8bf9649 604 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
605 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
0795af57 612 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
571ecf67 613 "since STA not sleeping anymore\n", dev->name,
0795af57 614 print_mac(mac, sta->addr), sta->aid);
571ecf67 615#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e8bf9649 616 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
617 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
badffb72 650 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
651 return TXRX_CONTINUE;
652
653 sta->rx_fragments++;
654 sta->rx_bytes += rx->skb->len;
6c55aa97
LF
655 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;
571ecf67
JB
658
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
571ecf67
JB
684static 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;
0795af57
JP
701 DECLARE_MAC_BUF(mac);
702 DECLARE_MAC_BUF(mac2);
571ecf67
JB
703 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
704 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
0795af57 705 "addr1=%s addr2=%s\n",
571ecf67
JB
706 sdata->dev->name, idx,
707 jiffies - entry->first_frag_time, entry->seq,
0795af57
JP
708 entry->last_frag, print_mac(mac, hdr->addr1),
709 print_mac(mac2, hdr->addr2));
571ecf67
JB
710#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;
0795af57 775 DECLARE_MAC_BUF(mac);
571ecf67
JB
776
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));
8f20fc24 795 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
571ecf67
JB
796 (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;
8f20fc24 822 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
571ecf67
JB
823 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) {
1a84f3fd
JB
832 if (net_ratelimit())
833 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
0795af57 834 "sequential A2=%s"
1a84f3fd
JB
835 " PN=%02x%02x%02x%02x%02x%02x "
836 "(expected %02x%02x%02x%02x%02x%02x)\n",
0795af57 837 rx->dev->name, print_mac(mac, hdr->addr2),
1a84f3fd
JB
838 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]);
571ecf67
JB
841 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 */
badffb72 871 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
571ecf67
JB
872
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{
98f0b0a3 886 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
571ecf67
JB
887 struct sk_buff *skb;
888 int no_pending_pkts;
0795af57 889 DECLARE_MAC_BUF(mac);
571ecf67
JB
890
891 if (likely(!rx->sta ||
892 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
893 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
badffb72 894 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
571ecf67
JB
895 return TXRX_CONTINUE;
896
98f0b0a3
RR
897 if ((sdata->type != IEEE80211_IF_TYPE_AP) &&
898 (sdata->type != IEEE80211_IF_TYPE_VLAN))
899 return TXRX_DROP;
900
571ecf67
JB
901 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
0795af57
JP
919 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
920 print_mac(mac, rx->sta->addr), rx->sta->aid,
571ecf67
JB
921 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) {
0795af57 943 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
571ecf67 944 "though there is no buffered frames for it\n",
0795af57 945 rx->dev->name, print_mac(mac, rx->sta->addr));
571ecf67
JB
946#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
6e0d114d
JB
957static ieee80211_txrx_result
958ieee80211_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
76ee65bf 977static int
ce3edf6d 978ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
571ecf67 979{
ce3edf6d
JB
980 if (unlikely(rx->sdata->ieee802_1x_pac &&
981 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
571ecf67 982#ifdef CONFIG_MAC80211_DEBUG
76ee65bf
RR
983 printk(KERN_DEBUG "%s: dropped frame "
984 "(unauthorized port)\n", rx->dev->name);
571ecf67 985#endif /* CONFIG_MAC80211_DEBUG */
76ee65bf 986 return -EACCES;
571ecf67
JB
987 }
988
76ee65bf 989 return 0;
571ecf67
JB
990}
991
76ee65bf 992static int
ce3edf6d 993ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
571ecf67 994{
3017b80b 995 /*
7848ba7d
JB
996 * Pass through unencrypted frames if the hardware has
997 * decrypted them already.
3017b80b 998 */
7848ba7d 999 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1000 return 0;
571ecf67
JB
1001
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 &&
ce3edf6d 1006 (rx->key || rx->sdata->drop_unencrypted))) {
1a84f3fd
JB
1007 if (net_ratelimit())
1008 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1009 "encryption\n", rx->dev->name);
76ee65bf 1010 return -EACCES;
571ecf67 1011 }
76ee65bf 1012 return 0;
571ecf67
JB
1013}
1014
76ee65bf
RR
1015static int
1016ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
571ecf67
JB
1017{
1018 struct net_device *dev = rx->dev;
571ecf67
JB
1019 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];
76ee65bf 1024 struct sk_buff *skb = rx->skb;
571ecf67 1025 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0795af57
JP
1026 DECLARE_MAC_BUF(mac);
1027 DECLARE_MAC_BUF(mac2);
1028 DECLARE_MAC_BUF(mac3);
1029 DECLARE_MAC_BUF(mac4);
571ecf67
JB
1030
1031 fc = rx->fc;
571ecf67
JB
1032
1033 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
76ee65bf 1034 return -1;
571ecf67
JB
1035
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)) {
1a84f3fd
JB
1056 if (net_ratelimit())
1057 printk(KERN_DEBUG "%s: dropped ToDS frame "
0795af57 1058 "(BSSID=%s SA=%s DA=%s)\n",
1a84f3fd 1059 dev->name,
0795af57
JP
1060 print_mac(mac, hdr->addr1),
1061 print_mac(mac2, hdr->addr2),
1062 print_mac(mac3, hdr->addr3));
76ee65bf 1063 return -1;
571ecf67
JB
1064 }
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)) {
1a84f3fd
JB
1072 if (net_ratelimit())
1073 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
0795af57 1074 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1a84f3fd 1075 rx->dev->name,
0795af57
JP
1076 print_mac(mac, hdr->addr1),
1077 print_mac(mac2, hdr->addr2),
1078 print_mac(mac3, hdr->addr3),
1079 print_mac(mac4, hdr->addr4));
76ee65bf 1080 return -1;
571ecf67
JB
1081 }
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
b3316157
JL
1088 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1089 (is_multicast_ether_addr(dst) &&
1090 !compare_ether_addr(src, dev->dev_addr)))
76ee65bf 1091 return -1;
571ecf67
JB
1092 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()) {
0795af57
JP
1100 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));
571ecf67 1106 }
76ee65bf 1107 return -1;
571ecf67
JB
1108 }
1109 break;
1110 }
1111
571ecf67
JB
1112 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 }
76ee65bf 1117 return -1;
571ecf67
JB
1118 }
1119
76ee65bf 1120 payload = skb->data + hdrlen;
571ecf67
JB
1121 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;
ce3edf6d 1134
571ecf67
JB
1135 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 }
76ee65bf
RR
1142 return 0;
1143}
571ecf67 1144
ce3edf6d
JB
1145/*
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 */
76ee65bf
RR
1173static 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);
ce3edf6d
JB
1180 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1181 struct sta_info *dsta;
571ecf67 1182
76ee65bf
RR
1183 skb = rx->skb;
1184 xmit_skb = NULL;
571ecf67 1185
ce3edf6d
JB
1186 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP ||
1187 sdata->type == IEEE80211_IF_TYPE_VLAN) &&
badffb72 1188 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
ce3edf6d
JB
1189 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 */
76ee65bf
RR
1194 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1195 if (!xmit_skb && net_ratelimit())
571ecf67
JB
1196 printk(KERN_DEBUG "%s: failed to clone "
1197 "multicast frame\n", dev->name);
1198 } else {
571ecf67 1199 dsta = sta_info_get(local, skb->data);
ce3edf6d
JB
1200 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.
571ecf67 1206 */
76ee65bf 1207 xmit_skb = skb;
571ecf67
JB
1208 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
76ee65bf 1222 if (xmit_skb) {
571ecf67 1223 /* send to wireless media */
f831e909 1224 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
1225 skb_reset_network_header(xmit_skb);
1226 skb_reset_mac_header(xmit_skb);
76ee65bf 1227 dev_queue_xmit(xmit_skb);
571ecf67 1228 }
76ee65bf
RR
1229}
1230
fd4c7f2f
RR
1231static ieee80211_txrx_result
1232ieee80211_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
64bd4b69 1252 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
fd4c7f2f
RR
1253 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
ce3edf6d 1311 skb_reset_network_header(frame);
fd4c7f2f
RR
1312 frame->dev = dev;
1313 frame->priority = skb->priority;
1314 rx->skb = frame;
1315
fd4c7f2f
RR
1316 payload = frame->data;
1317 ethertype = (payload[6] << 8) | payload[7];
1318
1319 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
ce3edf6d
JB
1320 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1321 compare_ether_addr(payload,
1322 bridge_tunnel_header) == 0)) {
fd4c7f2f
RR
1323 /* 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 {
ce3edf6d
JB
1329 memcpy(skb_push(frame, sizeof(__be16)),
1330 &len, sizeof(__be16));
fd4c7f2f
RR
1331 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1332 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1333 }
1334
ce3edf6d
JB
1335 if (!ieee80211_frame_allowed(rx)) {
1336 if (skb == frame) /* last frame */
1337 return TXRX_DROP;
1338 dev_kfree_skb(frame);
1339 continue;
1340 }
fd4c7f2f
RR
1341
1342 ieee80211_deliver_skb(rx);
1343 }
1344
1345 return TXRX_QUEUED;
1346}
1347
76ee65bf
RR
1348static ieee80211_txrx_result
1349ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1350{
1351 struct net_device *dev = rx->dev;
1352 u16 fc;
ce3edf6d 1353 int err;
76ee65bf
RR
1354
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
76ee65bf
RR
1362 err = ieee80211_data_to_8023(rx);
1363 if (unlikely(err))
1364 return TXRX_DROP;
1365
ce3edf6d
JB
1366 if (!ieee80211_frame_allowed(rx))
1367 return TXRX_DROP;
1368
76ee65bf
RR
1369 rx->skb->dev = dev;
1370
1371 dev->stats.rx_packets++;
1372 dev->stats.rx_bytes += rx->skb->len;
1373
1374 ieee80211_deliver_skb(rx);
571ecf67
JB
1375
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
badffb72 1384 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
1385 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) &&
ddd3d2be 1390 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
571ecf67 1391 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
f9d540ee
JB
1392 else
1393 return TXRX_DROP;
1394
571ecf67
JB
1395 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);
8e6f0032
JB
1409
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);
571ecf67
JB
1420 break;
1421 }
8e6f0032 1422 break;
571ecf67
JB
1423 }
1424
8e6f0032 1425 if (res == TXRX_DROP)
571ecf67 1426 dev_kfree_skb(rx->skb);
571ecf67
JB
1427 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;
0795af57
JP
1446 DECLARE_MAC_BUF(mac);
1447 DECLARE_MAC_BUF(mac2);
571ecf67
JB
1448
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
1a84f3fd
JB
1455 if (net_ratelimit())
1456 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
0795af57
JP
1457 "failure from %s to %s keyidx=%d\n",
1458 dev->name, print_mac(mac, hdr->addr2),
1459 print_mac(mac2, hdr->addr1), keyidx);
571ecf67
JB
1460
1461 if (!sta) {
aa0daf0e
JB
1462 /*
1463 * Some hardware seem to generate incorrect Michael MIC
1464 * reports; ignore them to avoid triggering countermeasures.
1465 */
1a84f3fd
JB
1466 if (net_ratelimit())
1467 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
0795af57
JP
1468 "error for unknown address %s\n",
1469 dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1470 goto ignore;
1471 }
1472
1473 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1a84f3fd
JB
1474 if (net_ratelimit())
1475 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
aa0daf0e 1476 "error for a frame with no PROTECTED flag (src "
0795af57 1477 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1478 goto ignore;
1479 }
1480
7848ba7d 1481 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
aa0daf0e
JB
1482 /*
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 */
eb063c17
JB
1488 if (net_ratelimit())
1489 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1490 "a frame with non-zero keyidx (%d)"
0795af57
JP
1491 " (src %s)\n", dev->name, keyidx,
1492 print_mac(mac, hdr->addr2));
eb063c17 1493 goto ignore;
571ecf67
JB
1494 }
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)) {
1a84f3fd
JB
1499 if (net_ratelimit())
1500 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1501 "error for a frame that cannot be encrypted "
0795af57
JP
1502 "(fc=0x%04x) (src %s)\n",
1503 dev->name, rx->fc, print_mac(mac, hdr->addr2));
571ecf67
JB
1504 goto ignore;
1505 }
1506
eb063c17 1507 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
571ecf67
JB
1508 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,
571ecf67
JB
1516 ieee80211_rx_h_passive_scan,
1517 ieee80211_rx_h_check,
4f0d18e2 1518 ieee80211_rx_h_decrypt,
70f08765 1519 ieee80211_rx_h_sta_process,
571ecf67
JB
1520 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,
fd4c7f2f 1528 ieee80211_rx_h_amsdu,
571ecf67
JB
1529 ieee80211_rx_h_data,
1530 ieee80211_rx_h_mgmt,
1531 NULL
1532};
1533
1534/* main receive path */
1535
23a24def
JB
1536static 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)) {
badffb72 1547 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1548 return 0;
badffb72 1549 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1550 } else if (!multicast &&
1551 compare_ether_addr(sdata->dev->dev_addr,
1552 hdr->addr1) != 0) {
4150c572 1553 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1554 return 0;
badffb72 1555 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1556 }
1557 break;
1558 case IEEE80211_IF_TYPE_IBSS:
1559 if (!bssid)
1560 return 0;
1561 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
badffb72 1562 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1563 return 0;
badffb72 1564 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1565 } else if (!multicast &&
1566 compare_ether_addr(sdata->dev->dev_addr,
1567 hdr->addr1) != 0) {
4150c572 1568 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1569 return 0;
badffb72 1570 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1571 } else if (!rx->sta)
1572 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1573 bssid, hdr->addr2);
1574 break;
fb1c1cd6 1575 case IEEE80211_IF_TYPE_VLAN:
23a24def
JB
1576 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)) {
badffb72 1583 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1584 return 0;
badffb72 1585 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def 1586 }
badffb72
JS
1587 if (sdata->dev == sdata->local->mdev &&
1588 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def
JB
1589 /* 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;
fb1c1cd6
JB
1600 case IEEE80211_IF_TYPE_MNTR:
1601 /* take everything */
1602 break;
a2897552 1603 case IEEE80211_IF_TYPE_INVALID:
fb1c1cd6
JB
1604 /* should never get here */
1605 WARN_ON(1);
1606 break;
23a24def
JB
1607 }
1608
1609 return 1;
1610}
1611
571ecf67 1612/*
6368e4b1
RR
1613 * This is the actual Rx frames handler. as it blongs to Rx path it must
1614 * be called with rcu_read_lock protection.
571ecf67 1615 */
6368e4b1
RR
1616void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw, struct sk_buff *skb,
1617 struct ieee80211_rx_status *status, u32 load)
571ecf67
JB
1618{
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;
6368e4b1 1625 int prepares;
8e6f0032
JB
1626 struct ieee80211_sub_if_data *prev = NULL;
1627 struct sk_buff *skb_new;
1628 u8 *bssid;
d10f2150 1629 int hdrlen;
571ecf67
JB
1630
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;
6368e4b1 1637 rx.u.rx.load = load;
b2e7771e 1638 rx.fc = le16_to_cpu(hdr->frame_control);
571ecf67 1639 type = rx.fc & IEEE80211_FCTL_FTYPE;
72abd81b 1640
d10f2150
DM
1641 /*
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
b2e7771e 1653 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
571ecf67 1654 local->dot11ReceivedFragmentCount++;
571ecf67 1655
b2e7771e
JB
1656 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 }
571ecf67 1661
571ecf67
JB
1662 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1663 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1664 goto end;
1665 }
1666
ece8eddd 1667 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
badffb72 1668 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
571ecf67
JB
1669
1670 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1671 sta) != TXRX_CONTINUE)
1672 goto end;
1673 skb = rx.skb;
1674
c9ee23df 1675 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
53918994
JB
1676 !atomic_read(&local->iff_promiscs) &&
1677 !is_multicast_ether_addr(hdr->addr1)) {
badffb72 1678 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
571ecf67 1679 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
23a24def 1680 rx.sta);
8e6f0032 1681 sta_info_put(sta);
d4e46a3d 1682 rcu_read_unlock();
8e6f0032
JB
1683 return;
1684 }
1685
b2e7771e 1686 bssid = ieee80211_get_bssid(hdr, skb->len);
8e6f0032 1687
79010420 1688 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2a8a9a88
JB
1689 if (!netif_running(sdata->dev))
1690 continue;
1691
b2e7771e
JB
1692 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1693 continue;
1694
1695 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
6368e4b1 1696 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
23a24def
JB
1697 /* prepare_for_handlers can change sta */
1698 sta = rx.sta;
1699
6368e4b1 1700 if (!prepares)
23a24def 1701 continue;
8e6f0032 1702
340e11f3
JB
1703 /*
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;
8e6f0032 1712 }
340e11f3
JB
1713
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",
dd1cd4c6
JB
1724 wiphy_name(local->hw.wiphy),
1725 prev->dev->name);
340e11f3
JB
1726 continue;
1727 }
69f817b6 1728 rx.fc = le16_to_cpu(hdr->frame_control);
340e11f3
JB
1729 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);
8e6f0032 1734 prev = sdata;
571ecf67 1735 }
8e6f0032 1736 if (prev) {
69f817b6 1737 rx.fc = le16_to_cpu(hdr->frame_control);
8e6f0032
JB
1738 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);
571ecf67 1745
8e6f0032 1746 end:
571ecf67
JB
1747 if (sta)
1748 sta_info_put(sta);
1749}
6368e4b1 1750
b580781e
RR
1751#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
6368e4b1
RR
1931/*
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);
b580781e 1961 local->channel_use_raw += pkt_load;
6368e4b1 1962
b580781e
RR
1963 if (!ieee80211_rx_reorder_ampdu(local, skb))
1964 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
6368e4b1
RR
1965
1966 rcu_read_unlock();
1967}
571ecf67
JB
1968EXPORT_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);