mac80211: A-MPDU Rx MLME data initialization
[deliverable/linux.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;
333 local->channel_use_raw += load;
571ecf67 334
6368e4b1 335 return load;
571ecf67
JB
336}
337
338ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
339{
340 ieee80211_rx_h_parse_qos,
571ecf67
JB
341 NULL
342};
343
344/* rx handlers */
345
346static ieee80211_txrx_result
347ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
348{
52865dfd
JB
349 if (rx->sta)
350 rx->sta->channel_use_raw += rx->u.rx.load;
571ecf67
JB
351 rx->sdata->channel_use_raw += rx->u.rx.load;
352 return TXRX_CONTINUE;
353}
354
571ecf67
JB
355static ieee80211_txrx_result
356ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
357{
358 struct ieee80211_local *local = rx->local;
359 struct sk_buff *skb = rx->skb;
360
ece8eddd
ZY
361 if (unlikely(local->sta_hw_scanning))
362 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
363
364 if (unlikely(local->sta_sw_scanning)) {
365 /* drop all the other packets during a software scan anyway */
366 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
367 != TXRX_QUEUED)
368 dev_kfree_skb(skb);
571ecf67
JB
369 return TXRX_QUEUED;
370 }
371
badffb72 372 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
571ecf67
JB
373 /* scanning finished during invoking of handlers */
374 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
375 return TXRX_DROP;
376 }
377
378 return TXRX_CONTINUE;
379}
380
381static ieee80211_txrx_result
382ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
383{
384 struct ieee80211_hdr *hdr;
571ecf67
JB
385 hdr = (struct ieee80211_hdr *) rx->skb->data;
386
387 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
388 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
389 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
390 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
391 hdr->seq_ctrl)) {
badffb72 392 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
571ecf67
JB
393 rx->local->dot11FrameDuplicateCount++;
394 rx->sta->num_duplicates++;
395 }
396 return TXRX_DROP;
397 } else
398 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
399 }
400
571ecf67
JB
401 if (unlikely(rx->skb->len < 16)) {
402 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
403 return TXRX_DROP;
404 }
405
571ecf67
JB
406 /* Drop disallowed frame classes based on STA auth/assoc state;
407 * IEEE 802.11, Chap 5.5.
408 *
409 * 80211.o does filtering only based on association state, i.e., it
410 * drops Class 3 frames from not associated stations. hostapd sends
411 * deauth/disassoc frames when needed. In addition, hostapd is
412 * responsible for filtering on both auth and assoc states.
413 */
414 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
415 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
416 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
417 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
418 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
419 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
420 !(rx->fc & IEEE80211_FCTL_TODS) &&
421 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
badffb72 422 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
571ecf67
JB
423 /* Drop IBSS frames and frames for other hosts
424 * silently. */
425 return TXRX_DROP;
426 }
427
f9d540ee 428 return TXRX_DROP;
571ecf67
JB
429 }
430
570bd537
JB
431 return TXRX_CONTINUE;
432}
433
434
435static ieee80211_txrx_result
1990af8d 436ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
570bd537
JB
437{
438 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3017b80b
JB
439 int keyidx;
440 int hdrlen;
e2f036da 441 ieee80211_txrx_result result = TXRX_DROP;
d4e46a3d 442 struct ieee80211_key *stakey = NULL;
570bd537 443
3017b80b
JB
444 /*
445 * Key selection 101
446 *
447 * There are three types of keys:
448 * - GTK (group keys)
449 * - PTK (pairwise keys)
450 * - STK (station-to-station pairwise keys)
451 *
452 * When selecting a key, we have to distinguish between multicast
453 * (including broadcast) and unicast frames, the latter can only
454 * use PTKs and STKs while the former always use GTKs. Unless, of
455 * course, actual WEP keys ("pre-RSNA") are used, then unicast
456 * frames can also use key indizes like GTKs. Hence, if we don't
457 * have a PTK/STK we check the key index for a WEP key.
458 *
8dc06a1c
JB
459 * Note that in a regular BSS, multicast frames are sent by the
460 * AP only, associated stations unicast the frame to the AP first
461 * which then multicasts it on their behalf.
462 *
3017b80b
JB
463 * There is also a slight problem in IBSS mode: GTKs are negotiated
464 * with each station, that is something we don't currently handle.
8dc06a1c
JB
465 * The spec seems to expect that one negotiates the same key with
466 * every station but there's no such requirement; VLANs could be
467 * possible.
3017b80b
JB
468 */
469
470 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
471 return TXRX_CONTINUE;
571ecf67 472
3017b80b 473 /*
1990af8d 474 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
475 * addressed to us nor a multicast frame.
476 */
badffb72 477 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
3017b80b
JB
478 return TXRX_CONTINUE;
479
d4e46a3d
JB
480 if (rx->sta)
481 stakey = rcu_dereference(rx->sta->key);
482
483 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
484 rx->key = stakey;
571ecf67 485 } else {
3017b80b
JB
486 /*
487 * The device doesn't give us the IV so we won't be
488 * able to look up the key. That's ok though, we
489 * don't need to decrypt the frame, we just won't
490 * be able to keep statistics accurate.
491 * Except for key threshold notifications, should
492 * we somehow allow the driver to tell us which key
493 * the hardware used if this flag is set?
494 */
7848ba7d
JB
495 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
496 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
3017b80b
JB
497 return TXRX_CONTINUE;
498
499 hdrlen = ieee80211_get_hdrlen(rx->fc);
500
501 if (rx->skb->len < 8 + hdrlen)
502 return TXRX_DROP; /* TODO: count this? */
503
504 /*
505 * no need to call ieee80211_wep_get_keyidx,
506 * it verifies a bunch of things we've done already
507 */
508 keyidx = rx->skb->data[hdrlen + 3] >> 6;
509
d4e46a3d 510 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
3017b80b
JB
511
512 /*
513 * RSNA-protected unicast frames should always be sent with
514 * pairwise or station-to-station keys, but for WEP we allow
515 * using a key index as well.
516 */
8f20fc24 517 if (rx->key && rx->key->conf.alg != ALG_WEP &&
3017b80b
JB
518 !is_multicast_ether_addr(hdr->addr1))
519 rx->key = NULL;
571ecf67
JB
520 }
521
3017b80b 522 if (rx->key) {
571ecf67 523 rx->key->tx_rx_count++;
011bfcc4 524 /* TODO: add threshold stuff again */
1990af8d 525 } else {
7f3ad894 526#ifdef CONFIG_MAC80211_DEBUG
70f08765
JB
527 if (net_ratelimit())
528 printk(KERN_DEBUG "%s: RX protected frame,"
529 " but have no key\n", rx->dev->name);
7f3ad894 530#endif /* CONFIG_MAC80211_DEBUG */
70f08765
JB
531 return TXRX_DROP;
532 }
533
1990af8d
JB
534 /* Check for weak IVs if possible */
535 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
536 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
537 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
538 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
539 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
540 rx->sta->wep_weak_iv_count++;
541
70f08765
JB
542 switch (rx->key->conf.alg) {
543 case ALG_WEP:
e2f036da
MN
544 result = ieee80211_crypto_wep_decrypt(rx);
545 break;
70f08765 546 case ALG_TKIP:
e2f036da
MN
547 result = ieee80211_crypto_tkip_decrypt(rx);
548 break;
70f08765 549 case ALG_CCMP:
e2f036da
MN
550 result = ieee80211_crypto_ccmp_decrypt(rx);
551 break;
70f08765
JB
552 }
553
e2f036da
MN
554 /* either the frame has been decrypted or will be dropped */
555 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
556
557 return result;
70f08765
JB
558}
559
571ecf67
JB
560static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
561{
562 struct ieee80211_sub_if_data *sdata;
0795af57
JP
563 DECLARE_MAC_BUF(mac);
564
571ecf67
JB
565 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
566
567 if (sdata->bss)
568 atomic_inc(&sdata->bss->num_sta_ps);
569 sta->flags |= WLAN_STA_PS;
570 sta->pspoll = 0;
571#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
572 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
573 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
574#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
575}
576
577static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
578{
579 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
580 struct sk_buff *skb;
581 int sent = 0;
582 struct ieee80211_sub_if_data *sdata;
583 struct ieee80211_tx_packet_data *pkt_data;
0795af57 584 DECLARE_MAC_BUF(mac);
571ecf67
JB
585
586 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
587 if (sdata->bss)
588 atomic_dec(&sdata->bss->num_sta_ps);
589 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
590 sta->pspoll = 0;
591 if (!skb_queue_empty(&sta->ps_tx_buf)) {
592 if (local->ops->set_tim)
593 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
594 if (sdata->bss)
595 bss_tim_clear(local, sdata->bss, sta->aid);
596 }
597#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
598 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
599 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
600#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
601 /* Send all buffered frames to the station */
602 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
603 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
604 sent++;
e8bf9649 605 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
606 dev_queue_xmit(skb);
607 }
608 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
609 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
610 local->total_ps_buffered--;
611 sent++;
612#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57 613 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
571ecf67 614 "since STA not sleeping anymore\n", dev->name,
0795af57 615 print_mac(mac, sta->addr), sta->aid);
571ecf67 616#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e8bf9649 617 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
618 dev_queue_xmit(skb);
619 }
620
621 return sent;
622}
623
624static ieee80211_txrx_result
625ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
626{
627 struct sta_info *sta = rx->sta;
628 struct net_device *dev = rx->dev;
629 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
630
631 if (!sta)
632 return TXRX_CONTINUE;
633
634 /* Update last_rx only for IBSS packets which are for the current
635 * BSSID to avoid keeping the current IBSS network alive in cases where
636 * other STAs are using different BSSID. */
637 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
638 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
639 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
640 sta->last_rx = jiffies;
641 } else
642 if (!is_multicast_ether_addr(hdr->addr1) ||
643 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
644 /* Update last_rx only for unicast frames in order to prevent
645 * the Probe Request frames (the only broadcast frames from a
646 * STA in infrastructure mode) from keeping a connection alive.
647 */
648 sta->last_rx = jiffies;
649 }
650
badffb72 651 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
652 return TXRX_CONTINUE;
653
654 sta->rx_fragments++;
655 sta->rx_bytes += rx->skb->len;
6c55aa97
LF
656 sta->last_rssi = rx->u.rx.status->ssi;
657 sta->last_signal = rx->u.rx.status->signal;
658 sta->last_noise = rx->u.rx.status->noise;
571ecf67
JB
659
660 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
661 /* Change STA power saving mode only in the end of a frame
662 * exchange sequence */
663 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
664 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
665 else if (!(sta->flags & WLAN_STA_PS) &&
666 (rx->fc & IEEE80211_FCTL_PM))
667 ap_sta_ps_start(dev, sta);
668 }
669
670 /* Drop data::nullfunc frames silently, since they are used only to
671 * control station power saving mode. */
672 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
673 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
674 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
675 /* Update counter and free packet here to avoid counting this
676 * as a dropped packed. */
677 sta->rx_packets++;
678 dev_kfree_skb(rx->skb);
679 return TXRX_QUEUED;
680 }
681
682 return TXRX_CONTINUE;
683} /* ieee80211_rx_h_sta_process */
684
571ecf67
JB
685static inline struct ieee80211_fragment_entry *
686ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
687 unsigned int frag, unsigned int seq, int rx_queue,
688 struct sk_buff **skb)
689{
690 struct ieee80211_fragment_entry *entry;
691 int idx;
692
693 idx = sdata->fragment_next;
694 entry = &sdata->fragments[sdata->fragment_next++];
695 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
696 sdata->fragment_next = 0;
697
698 if (!skb_queue_empty(&entry->skb_list)) {
699#ifdef CONFIG_MAC80211_DEBUG
700 struct ieee80211_hdr *hdr =
701 (struct ieee80211_hdr *) entry->skb_list.next->data;
0795af57
JP
702 DECLARE_MAC_BUF(mac);
703 DECLARE_MAC_BUF(mac2);
571ecf67
JB
704 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
705 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
0795af57 706 "addr1=%s addr2=%s\n",
571ecf67
JB
707 sdata->dev->name, idx,
708 jiffies - entry->first_frag_time, entry->seq,
0795af57
JP
709 entry->last_frag, print_mac(mac, hdr->addr1),
710 print_mac(mac2, hdr->addr2));
571ecf67
JB
711#endif /* CONFIG_MAC80211_DEBUG */
712 __skb_queue_purge(&entry->skb_list);
713 }
714
715 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
716 *skb = NULL;
717 entry->first_frag_time = jiffies;
718 entry->seq = seq;
719 entry->rx_queue = rx_queue;
720 entry->last_frag = frag;
721 entry->ccmp = 0;
722 entry->extra_len = 0;
723
724 return entry;
725}
726
727static inline struct ieee80211_fragment_entry *
728ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
729 u16 fc, unsigned int frag, unsigned int seq,
730 int rx_queue, struct ieee80211_hdr *hdr)
731{
732 struct ieee80211_fragment_entry *entry;
733 int i, idx;
734
735 idx = sdata->fragment_next;
736 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
737 struct ieee80211_hdr *f_hdr;
738 u16 f_fc;
739
740 idx--;
741 if (idx < 0)
742 idx = IEEE80211_FRAGMENT_MAX - 1;
743
744 entry = &sdata->fragments[idx];
745 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
746 entry->rx_queue != rx_queue ||
747 entry->last_frag + 1 != frag)
748 continue;
749
750 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
751 f_fc = le16_to_cpu(f_hdr->frame_control);
752
753 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
754 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
755 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
756 continue;
757
758 if (entry->first_frag_time + 2 * HZ < jiffies) {
759 __skb_queue_purge(&entry->skb_list);
760 continue;
761 }
762 return entry;
763 }
764
765 return NULL;
766}
767
768static ieee80211_txrx_result
769ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
770{
771 struct ieee80211_hdr *hdr;
772 u16 sc;
773 unsigned int frag, seq;
774 struct ieee80211_fragment_entry *entry;
775 struct sk_buff *skb;
0795af57 776 DECLARE_MAC_BUF(mac);
571ecf67
JB
777
778 hdr = (struct ieee80211_hdr *) rx->skb->data;
779 sc = le16_to_cpu(hdr->seq_ctrl);
780 frag = sc & IEEE80211_SCTL_FRAG;
781
782 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
783 (rx->skb)->len < 24 ||
784 is_multicast_ether_addr(hdr->addr1))) {
785 /* not fragmented */
786 goto out;
787 }
788 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
789
790 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
791
792 if (frag == 0) {
793 /* This is the first fragment of a new frame. */
794 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
795 rx->u.rx.queue, &(rx->skb));
8f20fc24 796 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
571ecf67
JB
797 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
798 /* Store CCMP PN so that we can verify that the next
799 * fragment has a sequential PN value. */
800 entry->ccmp = 1;
801 memcpy(entry->last_pn,
802 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
803 CCMP_PN_LEN);
804 }
805 return TXRX_QUEUED;
806 }
807
808 /* This is a fragment for a frame that should already be pending in
809 * fragment cache. Add this fragment to the end of the pending entry.
810 */
811 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
812 rx->u.rx.queue, hdr);
813 if (!entry) {
814 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
815 return TXRX_DROP;
816 }
817
818 /* Verify that MPDUs within one MSDU have sequential PN values.
819 * (IEEE 802.11i, 8.3.3.4.5) */
820 if (entry->ccmp) {
821 int i;
822 u8 pn[CCMP_PN_LEN], *rpn;
8f20fc24 823 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
571ecf67
JB
824 return TXRX_DROP;
825 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
826 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
827 pn[i]++;
828 if (pn[i])
829 break;
830 }
831 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
832 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
1a84f3fd
JB
833 if (net_ratelimit())
834 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
0795af57 835 "sequential A2=%s"
1a84f3fd
JB
836 " PN=%02x%02x%02x%02x%02x%02x "
837 "(expected %02x%02x%02x%02x%02x%02x)\n",
0795af57 838 rx->dev->name, print_mac(mac, hdr->addr2),
1a84f3fd
JB
839 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
840 rpn[5], pn[0], pn[1], pn[2], pn[3],
841 pn[4], pn[5]);
571ecf67
JB
842 return TXRX_DROP;
843 }
844 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
845 }
846
847 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
848 __skb_queue_tail(&entry->skb_list, rx->skb);
849 entry->last_frag = frag;
850 entry->extra_len += rx->skb->len;
851 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
852 rx->skb = NULL;
853 return TXRX_QUEUED;
854 }
855
856 rx->skb = __skb_dequeue(&entry->skb_list);
857 if (skb_tailroom(rx->skb) < entry->extra_len) {
858 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
859 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
860 GFP_ATOMIC))) {
861 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
862 __skb_queue_purge(&entry->skb_list);
863 return TXRX_DROP;
864 }
865 }
866 while ((skb = __skb_dequeue(&entry->skb_list))) {
867 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
868 dev_kfree_skb(skb);
869 }
870
871 /* Complete frame has been reassembled - process it now */
badffb72 872 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
571ecf67
JB
873
874 out:
875 if (rx->sta)
876 rx->sta->rx_packets++;
877 if (is_multicast_ether_addr(hdr->addr1))
878 rx->local->dot11MulticastReceivedFrameCount++;
879 else
880 ieee80211_led_rx(rx->local);
881 return TXRX_CONTINUE;
882}
883
884static ieee80211_txrx_result
885ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
886{
98f0b0a3 887 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
571ecf67
JB
888 struct sk_buff *skb;
889 int no_pending_pkts;
0795af57 890 DECLARE_MAC_BUF(mac);
571ecf67
JB
891
892 if (likely(!rx->sta ||
893 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
894 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
badffb72 895 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
571ecf67
JB
896 return TXRX_CONTINUE;
897
98f0b0a3
RR
898 if ((sdata->type != IEEE80211_IF_TYPE_AP) &&
899 (sdata->type != IEEE80211_IF_TYPE_VLAN))
900 return TXRX_DROP;
901
571ecf67
JB
902 skb = skb_dequeue(&rx->sta->tx_filtered);
903 if (!skb) {
904 skb = skb_dequeue(&rx->sta->ps_tx_buf);
905 if (skb)
906 rx->local->total_ps_buffered--;
907 }
908 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
909 skb_queue_empty(&rx->sta->ps_tx_buf);
910
911 if (skb) {
912 struct ieee80211_hdr *hdr =
913 (struct ieee80211_hdr *) skb->data;
914
915 /* tell TX path to send one frame even though the STA may
916 * still remain is PS mode after this frame exchange */
917 rx->sta->pspoll = 1;
918
919#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
920 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
921 print_mac(mac, rx->sta->addr), rx->sta->aid,
571ecf67
JB
922 skb_queue_len(&rx->sta->ps_tx_buf));
923#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
924
925 /* Use MoreData flag to indicate whether there are more
926 * buffered frames for this STA */
927 if (no_pending_pkts) {
928 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
929 rx->sta->flags &= ~WLAN_STA_TIM;
930 } else
931 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
932
933 dev_queue_xmit(skb);
934
935 if (no_pending_pkts) {
936 if (rx->local->ops->set_tim)
937 rx->local->ops->set_tim(local_to_hw(rx->local),
938 rx->sta->aid, 0);
939 if (rx->sdata->bss)
940 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
941 }
942#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
943 } else if (!rx->u.rx.sent_ps_buffered) {
0795af57 944 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
571ecf67 945 "though there is no buffered frames for it\n",
0795af57 946 rx->dev->name, print_mac(mac, rx->sta->addr));
571ecf67
JB
947#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
948
949 }
950
951 /* Free PS Poll skb here instead of returning TXRX_DROP that would
952 * count as an dropped frame. */
953 dev_kfree_skb(rx->skb);
954
955 return TXRX_QUEUED;
956}
957
6e0d114d
JB
958static ieee80211_txrx_result
959ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
960{
961 u16 fc = rx->fc;
962 u8 *data = rx->skb->data;
963 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
964
965 if (!WLAN_FC_IS_QOS_DATA(fc))
966 return TXRX_CONTINUE;
967
968 /* remove the qos control field, update frame type and meta-data */
969 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
970 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
971 /* change frame type to non QOS */
972 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
973 hdr->frame_control = cpu_to_le16(fc);
974
975 return TXRX_CONTINUE;
976}
977
76ee65bf 978static int
ce3edf6d 979ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
571ecf67 980{
ce3edf6d
JB
981 if (unlikely(rx->sdata->ieee802_1x_pac &&
982 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
571ecf67 983#ifdef CONFIG_MAC80211_DEBUG
76ee65bf
RR
984 printk(KERN_DEBUG "%s: dropped frame "
985 "(unauthorized port)\n", rx->dev->name);
571ecf67 986#endif /* CONFIG_MAC80211_DEBUG */
76ee65bf 987 return -EACCES;
571ecf67
JB
988 }
989
76ee65bf 990 return 0;
571ecf67
JB
991}
992
76ee65bf 993static int
ce3edf6d 994ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
571ecf67 995{
3017b80b 996 /*
7848ba7d
JB
997 * Pass through unencrypted frames if the hardware has
998 * decrypted them already.
3017b80b 999 */
7848ba7d 1000 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1001 return 0;
571ecf67
JB
1002
1003 /* Drop unencrypted frames if key is set. */
1004 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1005 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1006 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
ce3edf6d 1007 (rx->key || rx->sdata->drop_unencrypted))) {
1a84f3fd
JB
1008 if (net_ratelimit())
1009 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1010 "encryption\n", rx->dev->name);
76ee65bf 1011 return -EACCES;
571ecf67 1012 }
76ee65bf 1013 return 0;
571ecf67
JB
1014}
1015
76ee65bf
RR
1016static int
1017ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
571ecf67
JB
1018{
1019 struct net_device *dev = rx->dev;
571ecf67
JB
1020 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1021 u16 fc, hdrlen, ethertype;
1022 u8 *payload;
1023 u8 dst[ETH_ALEN];
1024 u8 src[ETH_ALEN];
76ee65bf 1025 struct sk_buff *skb = rx->skb;
571ecf67 1026 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0795af57
JP
1027 DECLARE_MAC_BUF(mac);
1028 DECLARE_MAC_BUF(mac2);
1029 DECLARE_MAC_BUF(mac3);
1030 DECLARE_MAC_BUF(mac4);
571ecf67
JB
1031
1032 fc = rx->fc;
571ecf67
JB
1033
1034 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
76ee65bf 1035 return -1;
571ecf67
JB
1036
1037 hdrlen = ieee80211_get_hdrlen(fc);
1038
1039 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1040 * header
1041 * IEEE 802.11 address fields:
1042 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1043 * 0 0 DA SA BSSID n/a
1044 * 0 1 DA BSSID SA n/a
1045 * 1 0 BSSID SA DA n/a
1046 * 1 1 RA TA DA SA
1047 */
1048
1049 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1050 case IEEE80211_FCTL_TODS:
1051 /* BSSID SA DA */
1052 memcpy(dst, hdr->addr3, ETH_ALEN);
1053 memcpy(src, hdr->addr2, ETH_ALEN);
1054
1055 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1056 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
1a84f3fd
JB
1057 if (net_ratelimit())
1058 printk(KERN_DEBUG "%s: dropped ToDS frame "
0795af57 1059 "(BSSID=%s SA=%s DA=%s)\n",
1a84f3fd 1060 dev->name,
0795af57
JP
1061 print_mac(mac, hdr->addr1),
1062 print_mac(mac2, hdr->addr2),
1063 print_mac(mac3, hdr->addr3));
76ee65bf 1064 return -1;
571ecf67
JB
1065 }
1066 break;
1067 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1068 /* RA TA DA SA */
1069 memcpy(dst, hdr->addr3, ETH_ALEN);
1070 memcpy(src, hdr->addr4, ETH_ALEN);
1071
1072 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
1a84f3fd
JB
1073 if (net_ratelimit())
1074 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
0795af57 1075 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1a84f3fd 1076 rx->dev->name,
0795af57
JP
1077 print_mac(mac, hdr->addr1),
1078 print_mac(mac2, hdr->addr2),
1079 print_mac(mac3, hdr->addr3),
1080 print_mac(mac4, hdr->addr4));
76ee65bf 1081 return -1;
571ecf67
JB
1082 }
1083 break;
1084 case IEEE80211_FCTL_FROMDS:
1085 /* DA BSSID SA */
1086 memcpy(dst, hdr->addr1, ETH_ALEN);
1087 memcpy(src, hdr->addr3, ETH_ALEN);
1088
b3316157
JL
1089 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1090 (is_multicast_ether_addr(dst) &&
1091 !compare_ether_addr(src, dev->dev_addr)))
76ee65bf 1092 return -1;
571ecf67
JB
1093 break;
1094 case 0:
1095 /* DA SA BSSID */
1096 memcpy(dst, hdr->addr1, ETH_ALEN);
1097 memcpy(src, hdr->addr2, ETH_ALEN);
1098
1099 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1100 if (net_ratelimit()) {
0795af57
JP
1101 printk(KERN_DEBUG "%s: dropped IBSS frame "
1102 "(DA=%s SA=%s BSSID=%s)\n",
1103 dev->name,
1104 print_mac(mac, hdr->addr1),
1105 print_mac(mac2, hdr->addr2),
1106 print_mac(mac3, hdr->addr3));
571ecf67 1107 }
76ee65bf 1108 return -1;
571ecf67
JB
1109 }
1110 break;
1111 }
1112
571ecf67
JB
1113 if (unlikely(skb->len - hdrlen < 8)) {
1114 if (net_ratelimit()) {
1115 printk(KERN_DEBUG "%s: RX too short data frame "
1116 "payload\n", dev->name);
1117 }
76ee65bf 1118 return -1;
571ecf67
JB
1119 }
1120
76ee65bf 1121 payload = skb->data + hdrlen;
571ecf67
JB
1122 ethertype = (payload[6] << 8) | payload[7];
1123
1124 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1125 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1126 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1127 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1128 * replace EtherType */
1129 skb_pull(skb, hdrlen + 6);
1130 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1131 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1132 } else {
1133 struct ethhdr *ehdr;
1134 __be16 len;
ce3edf6d 1135
571ecf67
JB
1136 skb_pull(skb, hdrlen);
1137 len = htons(skb->len);
1138 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1139 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1140 memcpy(ehdr->h_source, src, ETH_ALEN);
1141 ehdr->h_proto = len;
1142 }
76ee65bf
RR
1143 return 0;
1144}
571ecf67 1145
ce3edf6d
JB
1146/*
1147 * requires that rx->skb is a frame with ethernet header
1148 */
1149static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1150{
1151 static const u8 pae_group_addr[ETH_ALEN]
1152 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1153 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1154
1155 /*
1156 * Allow EAPOL frames to us/the PAE group address regardless
1157 * of whether the frame was encrypted or not.
1158 */
1159 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1160 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1161 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1162 return true;
1163
1164 if (ieee80211_802_1x_port_control(rx) ||
1165 ieee80211_drop_unencrypted(rx))
1166 return false;
1167
1168 return true;
1169}
1170
1171/*
1172 * requires that rx->skb is a frame with ethernet header
1173 */
76ee65bf
RR
1174static void
1175ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1176{
1177 struct net_device *dev = rx->dev;
1178 struct ieee80211_local *local = rx->local;
1179 struct sk_buff *skb, *xmit_skb;
1180 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
ce3edf6d
JB
1181 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1182 struct sta_info *dsta;
571ecf67 1183
76ee65bf
RR
1184 skb = rx->skb;
1185 xmit_skb = NULL;
571ecf67 1186
ce3edf6d
JB
1187 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP ||
1188 sdata->type == IEEE80211_IF_TYPE_VLAN) &&
badffb72 1189 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
ce3edf6d
JB
1190 if (is_multicast_ether_addr(ehdr->h_dest)) {
1191 /*
1192 * send multicast frames both to higher layers in
1193 * local net stack and back to the wireless medium
1194 */
76ee65bf
RR
1195 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1196 if (!xmit_skb && net_ratelimit())
571ecf67
JB
1197 printk(KERN_DEBUG "%s: failed to clone "
1198 "multicast frame\n", dev->name);
1199 } else {
571ecf67 1200 dsta = sta_info_get(local, skb->data);
ce3edf6d
JB
1201 if (dsta && dsta->dev == dev) {
1202 /*
1203 * The destination station is associated to
1204 * this AP (in this VLAN), so send the frame
1205 * directly to it and do not pass it to local
1206 * net stack.
571ecf67 1207 */
76ee65bf 1208 xmit_skb = skb;
571ecf67
JB
1209 skb = NULL;
1210 }
1211 if (dsta)
1212 sta_info_put(dsta);
1213 }
1214 }
1215
1216 if (skb) {
1217 /* deliver to local stack */
1218 skb->protocol = eth_type_trans(skb, dev);
1219 memset(skb->cb, 0, sizeof(skb->cb));
1220 netif_rx(skb);
1221 }
1222
76ee65bf 1223 if (xmit_skb) {
571ecf67 1224 /* send to wireless media */
f831e909 1225 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
1226 skb_reset_network_header(xmit_skb);
1227 skb_reset_mac_header(xmit_skb);
76ee65bf 1228 dev_queue_xmit(xmit_skb);
571ecf67 1229 }
76ee65bf
RR
1230}
1231
fd4c7f2f
RR
1232static ieee80211_txrx_result
1233ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1234{
1235 struct net_device *dev = rx->dev;
1236 struct ieee80211_local *local = rx->local;
1237 u16 fc, ethertype;
1238 u8 *payload;
1239 struct sk_buff *skb = rx->skb, *frame = NULL;
1240 const struct ethhdr *eth;
1241 int remaining, err;
1242 u8 dst[ETH_ALEN];
1243 u8 src[ETH_ALEN];
1244 DECLARE_MAC_BUF(mac);
1245
1246 fc = rx->fc;
1247 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1248 return TXRX_CONTINUE;
1249
1250 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1251 return TXRX_DROP;
1252
64bd4b69 1253 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
fd4c7f2f
RR
1254 return TXRX_CONTINUE;
1255
1256 err = ieee80211_data_to_8023(rx);
1257 if (unlikely(err))
1258 return TXRX_DROP;
1259
1260 skb->dev = dev;
1261
1262 dev->stats.rx_packets++;
1263 dev->stats.rx_bytes += skb->len;
1264
1265 /* skip the wrapping header */
1266 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1267 if (!eth)
1268 return TXRX_DROP;
1269
1270 while (skb != frame) {
1271 u8 padding;
1272 __be16 len = eth->h_proto;
1273 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1274
1275 remaining = skb->len;
1276 memcpy(dst, eth->h_dest, ETH_ALEN);
1277 memcpy(src, eth->h_source, ETH_ALEN);
1278
1279 padding = ((4 - subframe_len) & 0x3);
1280 /* the last MSDU has no padding */
1281 if (subframe_len > remaining) {
1282 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1283 return TXRX_DROP;
1284 }
1285
1286 skb_pull(skb, sizeof(struct ethhdr));
1287 /* if last subframe reuse skb */
1288 if (remaining <= subframe_len + padding)
1289 frame = skb;
1290 else {
1291 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1292 subframe_len);
1293
1294 if (frame == NULL)
1295 return TXRX_DROP;
1296
1297 skb_reserve(frame, local->hw.extra_tx_headroom +
1298 sizeof(struct ethhdr));
1299 memcpy(skb_put(frame, ntohs(len)), skb->data,
1300 ntohs(len));
1301
1302 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1303 padding);
1304 if (!eth) {
1305 printk(KERN_DEBUG "%s: wrong buffer size ",
1306 dev->name);
1307 dev_kfree_skb(frame);
1308 return TXRX_DROP;
1309 }
1310 }
1311
ce3edf6d 1312 skb_reset_network_header(frame);
fd4c7f2f
RR
1313 frame->dev = dev;
1314 frame->priority = skb->priority;
1315 rx->skb = frame;
1316
fd4c7f2f
RR
1317 payload = frame->data;
1318 ethertype = (payload[6] << 8) | payload[7];
1319
1320 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
ce3edf6d
JB
1321 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1322 compare_ether_addr(payload,
1323 bridge_tunnel_header) == 0)) {
fd4c7f2f
RR
1324 /* remove RFC1042 or Bridge-Tunnel
1325 * encapsulation and replace EtherType */
1326 skb_pull(frame, 6);
1327 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1328 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1329 } else {
ce3edf6d
JB
1330 memcpy(skb_push(frame, sizeof(__be16)),
1331 &len, sizeof(__be16));
fd4c7f2f
RR
1332 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1333 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1334 }
1335
ce3edf6d
JB
1336 if (!ieee80211_frame_allowed(rx)) {
1337 if (skb == frame) /* last frame */
1338 return TXRX_DROP;
1339 dev_kfree_skb(frame);
1340 continue;
1341 }
fd4c7f2f
RR
1342
1343 ieee80211_deliver_skb(rx);
1344 }
1345
1346 return TXRX_QUEUED;
1347}
1348
76ee65bf
RR
1349static ieee80211_txrx_result
1350ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1351{
1352 struct net_device *dev = rx->dev;
1353 u16 fc;
ce3edf6d 1354 int err;
76ee65bf
RR
1355
1356 fc = rx->fc;
1357 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1358 return TXRX_CONTINUE;
1359
1360 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1361 return TXRX_DROP;
1362
76ee65bf
RR
1363 err = ieee80211_data_to_8023(rx);
1364 if (unlikely(err))
1365 return TXRX_DROP;
1366
ce3edf6d
JB
1367 if (!ieee80211_frame_allowed(rx))
1368 return TXRX_DROP;
1369
76ee65bf
RR
1370 rx->skb->dev = dev;
1371
1372 dev->stats.rx_packets++;
1373 dev->stats.rx_bytes += rx->skb->len;
1374
1375 ieee80211_deliver_skb(rx);
571ecf67
JB
1376
1377 return TXRX_QUEUED;
1378}
1379
1380static ieee80211_txrx_result
1381ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1382{
1383 struct ieee80211_sub_if_data *sdata;
1384
badffb72 1385 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
571ecf67
JB
1386 return TXRX_DROP;
1387
1388 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1389 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1390 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
ddd3d2be 1391 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
571ecf67 1392 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
f9d540ee
JB
1393 else
1394 return TXRX_DROP;
1395
571ecf67
JB
1396 return TXRX_QUEUED;
1397}
1398
1399static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1400 struct ieee80211_local *local,
1401 ieee80211_rx_handler *handlers,
1402 struct ieee80211_txrx_data *rx,
1403 struct sta_info *sta)
1404{
1405 ieee80211_rx_handler *handler;
1406 ieee80211_txrx_result res = TXRX_DROP;
1407
1408 for (handler = handlers; *handler != NULL; handler++) {
1409 res = (*handler)(rx);
8e6f0032
JB
1410
1411 switch (res) {
1412 case TXRX_CONTINUE:
1413 continue;
1414 case TXRX_DROP:
1415 I802_DEBUG_INC(local->rx_handlers_drop);
1416 if (sta)
1417 sta->rx_dropped++;
1418 break;
1419 case TXRX_QUEUED:
1420 I802_DEBUG_INC(local->rx_handlers_queued);
571ecf67
JB
1421 break;
1422 }
8e6f0032 1423 break;
571ecf67
JB
1424 }
1425
8e6f0032 1426 if (res == TXRX_DROP)
571ecf67 1427 dev_kfree_skb(rx->skb);
571ecf67
JB
1428 return res;
1429}
1430
1431static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1432 ieee80211_rx_handler *handlers,
1433 struct ieee80211_txrx_data *rx,
1434 struct sta_info *sta)
1435{
1436 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1437 TXRX_CONTINUE)
1438 dev_kfree_skb(rx->skb);
1439}
1440
1441static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1442 struct ieee80211_hdr *hdr,
1443 struct sta_info *sta,
1444 struct ieee80211_txrx_data *rx)
1445{
1446 int keyidx, hdrlen;
0795af57
JP
1447 DECLARE_MAC_BUF(mac);
1448 DECLARE_MAC_BUF(mac2);
571ecf67
JB
1449
1450 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1451 if (rx->skb->len >= hdrlen + 4)
1452 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1453 else
1454 keyidx = -1;
1455
1a84f3fd
JB
1456 if (net_ratelimit())
1457 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
0795af57
JP
1458 "failure from %s to %s keyidx=%d\n",
1459 dev->name, print_mac(mac, hdr->addr2),
1460 print_mac(mac2, hdr->addr1), keyidx);
571ecf67
JB
1461
1462 if (!sta) {
aa0daf0e
JB
1463 /*
1464 * Some hardware seem to generate incorrect Michael MIC
1465 * reports; ignore them to avoid triggering countermeasures.
1466 */
1a84f3fd
JB
1467 if (net_ratelimit())
1468 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
0795af57
JP
1469 "error for unknown address %s\n",
1470 dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1471 goto ignore;
1472 }
1473
1474 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1a84f3fd
JB
1475 if (net_ratelimit())
1476 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
aa0daf0e 1477 "error for a frame with no PROTECTED flag (src "
0795af57 1478 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1479 goto ignore;
1480 }
1481
7848ba7d 1482 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
aa0daf0e
JB
1483 /*
1484 * APs with pairwise keys should never receive Michael MIC
1485 * errors for non-zero keyidx because these are reserved for
1486 * group keys and only the AP is sending real multicast
1487 * frames in the BSS.
1488 */
eb063c17
JB
1489 if (net_ratelimit())
1490 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1491 "a frame with non-zero keyidx (%d)"
0795af57
JP
1492 " (src %s)\n", dev->name, keyidx,
1493 print_mac(mac, hdr->addr2));
eb063c17 1494 goto ignore;
571ecf67
JB
1495 }
1496
1497 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1498 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1499 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1a84f3fd
JB
1500 if (net_ratelimit())
1501 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1502 "error for a frame that cannot be encrypted "
0795af57
JP
1503 "(fc=0x%04x) (src %s)\n",
1504 dev->name, rx->fc, print_mac(mac, hdr->addr2));
571ecf67
JB
1505 goto ignore;
1506 }
1507
eb063c17 1508 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
571ecf67
JB
1509 ignore:
1510 dev_kfree_skb(rx->skb);
1511 rx->skb = NULL;
1512}
1513
1514ieee80211_rx_handler ieee80211_rx_handlers[] =
1515{
1516 ieee80211_rx_h_if_stats,
571ecf67
JB
1517 ieee80211_rx_h_passive_scan,
1518 ieee80211_rx_h_check,
4f0d18e2 1519 ieee80211_rx_h_decrypt,
70f08765 1520 ieee80211_rx_h_sta_process,
571ecf67
JB
1521 ieee80211_rx_h_defragment,
1522 ieee80211_rx_h_ps_poll,
1523 ieee80211_rx_h_michael_mic_verify,
1524 /* this must be after decryption - so header is counted in MPDU mic
1525 * must be before pae and data, so QOS_DATA format frames
1526 * are not passed to user space by these functions
1527 */
1528 ieee80211_rx_h_remove_qos_control,
fd4c7f2f 1529 ieee80211_rx_h_amsdu,
571ecf67
JB
1530 ieee80211_rx_h_data,
1531 ieee80211_rx_h_mgmt,
1532 NULL
1533};
1534
1535/* main receive path */
1536
23a24def
JB
1537static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1538 u8 *bssid, struct ieee80211_txrx_data *rx,
1539 struct ieee80211_hdr *hdr)
1540{
1541 int multicast = is_multicast_ether_addr(hdr->addr1);
1542
1543 switch (sdata->type) {
1544 case IEEE80211_IF_TYPE_STA:
1545 if (!bssid)
1546 return 0;
1547 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
badffb72 1548 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1549 return 0;
badffb72 1550 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1551 } else if (!multicast &&
1552 compare_ether_addr(sdata->dev->dev_addr,
1553 hdr->addr1) != 0) {
4150c572 1554 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1555 return 0;
badffb72 1556 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1557 }
1558 break;
1559 case IEEE80211_IF_TYPE_IBSS:
1560 if (!bssid)
1561 return 0;
1562 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
badffb72 1563 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1564 return 0;
badffb72 1565 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1566 } else if (!multicast &&
1567 compare_ether_addr(sdata->dev->dev_addr,
1568 hdr->addr1) != 0) {
4150c572 1569 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1570 return 0;
badffb72 1571 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def
JB
1572 } else if (!rx->sta)
1573 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1574 bssid, hdr->addr2);
1575 break;
fb1c1cd6 1576 case IEEE80211_IF_TYPE_VLAN:
23a24def
JB
1577 case IEEE80211_IF_TYPE_AP:
1578 if (!bssid) {
1579 if (compare_ether_addr(sdata->dev->dev_addr,
1580 hdr->addr1))
1581 return 0;
1582 } else if (!ieee80211_bssid_match(bssid,
1583 sdata->dev->dev_addr)) {
badffb72 1584 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def 1585 return 0;
badffb72 1586 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
23a24def 1587 }
badffb72
JS
1588 if (sdata->dev == sdata->local->mdev &&
1589 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
23a24def
JB
1590 /* do not receive anything via
1591 * master device when not scanning */
1592 return 0;
1593 break;
1594 case IEEE80211_IF_TYPE_WDS:
1595 if (bssid ||
1596 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1597 return 0;
1598 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1599 return 0;
1600 break;
fb1c1cd6
JB
1601 case IEEE80211_IF_TYPE_MNTR:
1602 /* take everything */
1603 break;
a2897552 1604 case IEEE80211_IF_TYPE_INVALID:
fb1c1cd6
JB
1605 /* should never get here */
1606 WARN_ON(1);
1607 break;
23a24def
JB
1608 }
1609
1610 return 1;
1611}
1612
571ecf67 1613/*
6368e4b1
RR
1614 * This is the actual Rx frames handler. as it blongs to Rx path it must
1615 * be called with rcu_read_lock protection.
571ecf67 1616 */
6368e4b1
RR
1617void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw, struct sk_buff *skb,
1618 struct ieee80211_rx_status *status, u32 load)
571ecf67
JB
1619{
1620 struct ieee80211_local *local = hw_to_local(hw);
1621 struct ieee80211_sub_if_data *sdata;
1622 struct sta_info *sta;
1623 struct ieee80211_hdr *hdr;
1624 struct ieee80211_txrx_data rx;
1625 u16 type;
6368e4b1 1626 int prepares;
8e6f0032
JB
1627 struct ieee80211_sub_if_data *prev = NULL;
1628 struct sk_buff *skb_new;
1629 u8 *bssid;
d10f2150 1630 int hdrlen;
571ecf67
JB
1631
1632 hdr = (struct ieee80211_hdr *) skb->data;
1633 memset(&rx, 0, sizeof(rx));
1634 rx.skb = skb;
1635 rx.local = local;
1636
1637 rx.u.rx.status = status;
6368e4b1 1638 rx.u.rx.load = load;
b2e7771e 1639 rx.fc = le16_to_cpu(hdr->frame_control);
571ecf67 1640 type = rx.fc & IEEE80211_FCTL_FTYPE;
72abd81b 1641
d10f2150
DM
1642 /*
1643 * Drivers are required to align the payload data to a four-byte
1644 * boundary, so the last two bits of the address where it starts
1645 * may not be set. The header is required to be directly before
1646 * the payload data, padding like atheros hardware adds which is
1647 * inbetween the 802.11 header and the payload is not supported,
1648 * the driver is required to move the 802.11 header further back
1649 * in that case.
1650 */
1651 hdrlen = ieee80211_get_hdrlen(rx.fc);
1652 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1653
b2e7771e 1654 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
571ecf67 1655 local->dot11ReceivedFragmentCount++;
571ecf67 1656
b2e7771e
JB
1657 sta = rx.sta = sta_info_get(local, hdr->addr2);
1658 if (sta) {
1659 rx.dev = rx.sta->dev;
1660 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1661 }
571ecf67 1662
571ecf67
JB
1663 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1664 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1665 goto end;
1666 }
1667
ece8eddd 1668 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
badffb72 1669 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
571ecf67
JB
1670
1671 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1672 sta) != TXRX_CONTINUE)
1673 goto end;
1674 skb = rx.skb;
1675
c9ee23df 1676 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
53918994
JB
1677 !atomic_read(&local->iff_promiscs) &&
1678 !is_multicast_ether_addr(hdr->addr1)) {
badffb72 1679 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
571ecf67 1680 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
23a24def 1681 rx.sta);
8e6f0032 1682 sta_info_put(sta);
d4e46a3d 1683 rcu_read_unlock();
8e6f0032
JB
1684 return;
1685 }
1686
b2e7771e 1687 bssid = ieee80211_get_bssid(hdr, skb->len);
8e6f0032 1688
79010420 1689 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2a8a9a88
JB
1690 if (!netif_running(sdata->dev))
1691 continue;
1692
b2e7771e
JB
1693 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1694 continue;
1695
1696 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
6368e4b1 1697 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
23a24def
JB
1698 /* prepare_for_handlers can change sta */
1699 sta = rx.sta;
1700
6368e4b1 1701 if (!prepares)
23a24def 1702 continue;
8e6f0032 1703
340e11f3
JB
1704 /*
1705 * frame is destined for this interface, but if it's not
1706 * also for the previous one we handle that after the
1707 * loop to avoid copying the SKB once too much
1708 */
1709
1710 if (!prev) {
1711 prev = sdata;
1712 continue;
8e6f0032 1713 }
340e11f3
JB
1714
1715 /*
1716 * frame was destined for the previous interface
1717 * so invoke RX handlers for it
1718 */
1719
1720 skb_new = skb_copy(skb, GFP_ATOMIC);
1721 if (!skb_new) {
1722 if (net_ratelimit())
1723 printk(KERN_DEBUG "%s: failed to copy "
1724 "multicast frame for %s",
dd1cd4c6
JB
1725 wiphy_name(local->hw.wiphy),
1726 prev->dev->name);
340e11f3
JB
1727 continue;
1728 }
69f817b6 1729 rx.fc = le16_to_cpu(hdr->frame_control);
340e11f3
JB
1730 rx.skb = skb_new;
1731 rx.dev = prev->dev;
1732 rx.sdata = prev;
1733 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1734 &rx, sta);
8e6f0032 1735 prev = sdata;
571ecf67 1736 }
8e6f0032 1737 if (prev) {
69f817b6 1738 rx.fc = le16_to_cpu(hdr->frame_control);
8e6f0032
JB
1739 rx.skb = skb;
1740 rx.dev = prev->dev;
1741 rx.sdata = prev;
1742 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1743 &rx, sta);
1744 } else
1745 dev_kfree_skb(skb);
571ecf67 1746
8e6f0032 1747 end:
571ecf67
JB
1748 if (sta)
1749 sta_info_put(sta);
1750}
6368e4b1
RR
1751
1752/*
1753 * This is the receive path handler. It is called by a low level driver when an
1754 * 802.11 MPDU is received from the hardware.
1755 */
1756void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1757 struct ieee80211_rx_status *status)
1758{
1759 struct ieee80211_local *local = hw_to_local(hw);
1760 u32 pkt_load;
1761
1762 /*
1763 * key references and virtual interfaces are protected using RCU
1764 * and this requires that we are in a read-side RCU section during
1765 * receive processing
1766 */
1767 rcu_read_lock();
1768
1769 /*
1770 * Frames with failed FCS/PLCP checksum are not returned,
1771 * all other frames are returned without radiotap header
1772 * if it was previously present.
1773 * Also, frames with less than 16 bytes are dropped.
1774 */
1775 skb = ieee80211_rx_monitor(local, skb, status);
1776 if (!skb) {
1777 rcu_read_unlock();
1778 return;
1779 }
1780
1781 pkt_load = ieee80211_rx_load_stats(local, skb, status);
1782
1783 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
1784
1785 rcu_read_unlock();
1786}
571ecf67
JB
1787EXPORT_SYMBOL(__ieee80211_rx);
1788
1789/* This is a version of the rx handler that can be called from hard irq
1790 * context. Post the skb on the queue and schedule the tasklet */
1791void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1792 struct ieee80211_rx_status *status)
1793{
1794 struct ieee80211_local *local = hw_to_local(hw);
1795
1796 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1797
1798 skb->dev = local->mdev;
1799 /* copy status into skb->cb for use by tasklet */
1800 memcpy(skb->cb, status, sizeof(*status));
1801 skb->pkt_type = IEEE80211_RX_MSG;
1802 skb_queue_tail(&local->skb_queue, skb);
1803 tasklet_schedule(&local->tasklet);
1804}
1805EXPORT_SYMBOL(ieee80211_rx_irqsafe);
This page took 0.289038 seconds and 5 git commands to generate.