blob: f5caa1a016ee28186c3451afa3e8278df3ea46b0 [file] [log] [blame]
Johannes Berg571ecf62007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040016#include <linux/rcupdate.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020017#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
Johannes Berg571ecf62007-07-27 15:43:22 +020022#include "wep.h"
23#include "wpa.h"
24#include "tkip.h"
25#include "wme.h"
26
Johannes Bergb2e77712007-09-26 15:19:39 +020027/*
28 * monitor mode reception
29 *
30 * This function cleans up the SKB, i.e. it removes all the stuff
31 * only useful for monitoring.
32 */
33static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
34 struct sk_buff *skb,
35 int rtap_len)
36{
37 skb_pull(skb, rtap_len);
38
39 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
40 if (likely(skb->len > FCS_LEN))
41 skb_trim(skb, skb->len - FCS_LEN);
42 else {
43 /* driver bug */
44 WARN_ON(1);
45 dev_kfree_skb(skb);
46 skb = NULL;
47 }
48 }
49
50 return skb;
51}
52
53static inline int should_drop_frame(struct ieee80211_rx_status *status,
54 struct sk_buff *skb,
55 int present_fcs_len,
56 int radiotap_len)
57{
58 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
59
60 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
61 return 1;
62 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
63 return 1;
64 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
65 cpu_to_le16(IEEE80211_FTYPE_CTL))
66 return 1;
67 return 0;
68}
69
70/*
71 * This function copies a received frame to all monitor interfaces and
72 * returns a cleaned-up SKB that no longer includes the FCS nor the
73 * radiotap header the driver might have added.
74 */
75static struct sk_buff *
76ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
77 struct ieee80211_rx_status *status)
78{
79 struct ieee80211_sub_if_data *sdata;
80 struct ieee80211_rate *rate;
81 int needed_headroom = 0;
82 struct ieee80211_rtap_hdr {
83 struct ieee80211_radiotap_header hdr;
84 u8 flags;
85 u8 rate;
86 __le16 chan_freq;
87 __le16 chan_flags;
88 u8 antsignal;
89 u8 padding_for_rxflags;
90 __le16 rx_flags;
91 } __attribute__ ((packed)) *rthdr;
92 struct sk_buff *skb, *skb2;
93 struct net_device *prev_dev = NULL;
94 int present_fcs_len = 0;
95 int rtap_len = 0;
96
97 /*
98 * First, we may need to make a copy of the skb because
99 * (1) we need to modify it for radiotap (if not present), and
100 * (2) the other RX handlers will modify the skb we got.
101 *
102 * We don't need to, of course, if we aren't going to return
103 * the SKB because it has a bad FCS/PLCP checksum.
104 */
105 if (status->flag & RX_FLAG_RADIOTAP)
106 rtap_len = ieee80211_get_radiotap_len(origskb->data);
107 else
108 needed_headroom = sizeof(*rthdr);
109
110 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
111 present_fcs_len = FCS_LEN;
112
113 if (!local->monitors) {
114 if (should_drop_frame(status, origskb, present_fcs_len,
115 rtap_len)) {
116 dev_kfree_skb(origskb);
117 return NULL;
118 }
119
120 return remove_monitor_info(local, origskb, rtap_len);
121 }
122
123 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
124 /* only need to expand headroom if necessary */
125 skb = origskb;
126 origskb = NULL;
127
128 /*
129 * This shouldn't trigger often because most devices have an
130 * RX header they pull before we get here, and that should
131 * be big enough for our radiotap information. We should
132 * probably export the length to drivers so that we can have
133 * them allocate enough headroom to start with.
134 */
135 if (skb_headroom(skb) < needed_headroom &&
136 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
137 dev_kfree_skb(skb);
138 return NULL;
139 }
140 } else {
141 /*
142 * Need to make a copy and possibly remove radiotap header
143 * and FCS from the original.
144 */
145 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
146
147 origskb = remove_monitor_info(local, origskb, rtap_len);
148
149 if (!skb)
150 return origskb;
151 }
152
153 /* if necessary, prepend radiotap information */
154 if (!(status->flag & RX_FLAG_RADIOTAP)) {
155 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
156 memset(rthdr, 0, sizeof(*rthdr));
157 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
158 rthdr->hdr.it_present =
159 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
160 (1 << IEEE80211_RADIOTAP_RATE) |
161 (1 << IEEE80211_RADIOTAP_CHANNEL) |
162 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
163 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
164 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
165 IEEE80211_RADIOTAP_F_FCS : 0;
166
167 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
168 rthdr->rx_flags = 0;
169 if (status->flag &
170 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
171 rthdr->rx_flags |=
172 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
173
174 rate = ieee80211_get_rate(local, status->phymode,
175 status->rate);
176 if (rate)
177 rthdr->rate = rate->rate / 5;
178
179 rthdr->chan_freq = cpu_to_le16(status->freq);
180
181 if (status->phymode == MODE_IEEE80211A)
182 rthdr->chan_flags =
183 cpu_to_le16(IEEE80211_CHAN_OFDM |
184 IEEE80211_CHAN_5GHZ);
185 else
186 rthdr->chan_flags =
187 cpu_to_le16(IEEE80211_CHAN_DYN |
188 IEEE80211_CHAN_2GHZ);
189
190 rthdr->antsignal = status->ssi;
191 }
192
193 skb_set_mac_header(skb, 0);
194 skb->ip_summed = CHECKSUM_UNNECESSARY;
195 skb->pkt_type = PACKET_OTHERHOST;
196 skb->protocol = htons(ETH_P_802_2);
197
198 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
199 if (!netif_running(sdata->dev))
200 continue;
201
202 if (sdata->type != IEEE80211_IF_TYPE_MNTR)
203 continue;
204
205 if (prev_dev) {
206 skb2 = skb_clone(skb, GFP_ATOMIC);
207 if (skb2) {
208 skb2->dev = prev_dev;
209 netif_rx(skb2);
210 }
211 }
212
213 prev_dev = sdata->dev;
214 sdata->dev->stats.rx_packets++;
215 sdata->dev->stats.rx_bytes += skb->len;
216 }
217
218 if (prev_dev) {
219 skb->dev = prev_dev;
220 netif_rx(skb);
221 } else
222 dev_kfree_skb(skb);
223
224 return origskb;
225}
226
227
Johannes Berg571ecf62007-07-27 15:43:22 +0200228/* pre-rx handlers
229 *
230 * these don't have dev/sdata fields in the rx data
Johannes Berg52865dfd2007-07-27 15:43:22 +0200231 * The sta value should also not be used because it may
232 * be NULL even though a STA (in IBSS mode) will be added.
Johannes Berg571ecf62007-07-27 15:43:22 +0200233 */
234
235static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200236ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
237{
238 u8 *data = rx->skb->data;
239 int tid;
240
241 /* does the frame have a qos control field? */
242 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
243 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
244 /* frame has qos control */
245 tid = qc[0] & QOS_CONTROL_TID_MASK;
246 } else {
247 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
248 /* Separate TID for management frames */
249 tid = NUM_RX_DATA_QUEUES - 1;
250 } else {
251 /* no qos control present */
252 tid = 0; /* 802.1d - Best Effort */
253 }
254 }
Johannes Berg52865dfd2007-07-27 15:43:22 +0200255
Johannes Berg6e0d1142007-07-27 15:43:22 +0200256 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865dfd2007-07-27 15:43:22 +0200257 /* only a debug counter, sta might not be assigned properly yet */
258 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200259 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200260
261 rx->u.rx.queue = tid;
262 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
263 * For now, set skb->priority to 0 for other cases. */
264 rx->skb->priority = (tid > 7) ? 0 : tid;
265
266 return TXRX_CONTINUE;
267}
268
269static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200270ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
271{
272 struct ieee80211_local *local = rx->local;
273 struct sk_buff *skb = rx->skb;
274 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
275 u32 load = 0, hdrtime;
276 struct ieee80211_rate *rate;
277 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
278 int i;
279
280 /* Estimate total channel use caused by this frame */
281
282 if (unlikely(mode->num_rates < 0))
283 return TXRX_CONTINUE;
284
285 rate = &mode->rates[0];
286 for (i = 0; i < mode->num_rates; i++) {
287 if (mode->rates[i].val == rx->u.rx.status->rate) {
288 rate = &mode->rates[i];
289 break;
290 }
291 }
292
293 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
294 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
295
296 if (mode->mode == MODE_IEEE80211A ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200297 (mode->mode == MODE_IEEE80211G &&
298 rate->flags & IEEE80211_RATE_ERP))
299 hdrtime = CHAN_UTIL_HDR_SHORT;
300 else
301 hdrtime = CHAN_UTIL_HDR_LONG;
302
303 load = hdrtime;
304 if (!is_multicast_ether_addr(hdr->addr1))
305 load += hdrtime;
306
307 load += skb->len * rate->rate_inv;
308
309 /* Divide channel_use by 8 to avoid wrapping around the counter */
310 load >>= CHAN_UTIL_SHIFT;
311 local->channel_use_raw += load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200312 rx->u.rx.load = load;
313
314 return TXRX_CONTINUE;
315}
316
317ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
318{
319 ieee80211_rx_h_parse_qos,
320 ieee80211_rx_h_load_stats,
321 NULL
322};
323
324/* rx handlers */
325
326static ieee80211_txrx_result
327ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
328{
Johannes Berg52865dfd2007-07-27 15:43:22 +0200329 if (rx->sta)
330 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200331 rx->sdata->channel_use_raw += rx->u.rx.load;
332 return TXRX_CONTINUE;
333}
334
Johannes Berg571ecf62007-07-27 15:43:22 +0200335static ieee80211_txrx_result
336ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
337{
338 struct ieee80211_local *local = rx->local;
339 struct sk_buff *skb = rx->skb;
340
341 if (unlikely(local->sta_scanning != 0)) {
342 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
343 return TXRX_QUEUED;
344 }
345
Jiri Slabybadffb72007-08-28 17:01:54 -0400346 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200347 /* scanning finished during invoking of handlers */
348 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
349 return TXRX_DROP;
350 }
351
352 return TXRX_CONTINUE;
353}
354
355static ieee80211_txrx_result
356ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
357{
358 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200359 hdr = (struct ieee80211_hdr *) rx->skb->data;
360
361 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
362 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
363 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
364 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
365 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400366 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200367 rx->local->dot11FrameDuplicateCount++;
368 rx->sta->num_duplicates++;
369 }
370 return TXRX_DROP;
371 } else
372 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
373 }
374
Johannes Berg571ecf62007-07-27 15:43:22 +0200375 if (unlikely(rx->skb->len < 16)) {
376 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
377 return TXRX_DROP;
378 }
379
Jiri Slabybadffb72007-08-28 17:01:54 -0400380 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200381 rx->skb->pkt_type = PACKET_OTHERHOST;
382 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
383 rx->skb->pkt_type = PACKET_HOST;
384 else if (is_multicast_ether_addr(hdr->addr1)) {
385 if (is_broadcast_ether_addr(hdr->addr1))
386 rx->skb->pkt_type = PACKET_BROADCAST;
387 else
388 rx->skb->pkt_type = PACKET_MULTICAST;
389 } else
390 rx->skb->pkt_type = PACKET_OTHERHOST;
391
392 /* Drop disallowed frame classes based on STA auth/assoc state;
393 * IEEE 802.11, Chap 5.5.
394 *
395 * 80211.o does filtering only based on association state, i.e., it
396 * drops Class 3 frames from not associated stations. hostapd sends
397 * deauth/disassoc frames when needed. In addition, hostapd is
398 * responsible for filtering on both auth and assoc states.
399 */
400 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
401 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
402 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
403 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
404 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
405 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
406 !(rx->fc & IEEE80211_FCTL_TODS) &&
407 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400408 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200409 /* Drop IBSS frames and frames for other hosts
410 * silently. */
411 return TXRX_DROP;
412 }
413
Johannes Bergf9d540e2007-09-28 14:02:09 +0200414 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +0200415 }
416
Johannes Berg570bd532007-07-27 15:43:22 +0200417 return TXRX_CONTINUE;
418}
419
420
421static ieee80211_txrx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200422ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200423{
424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400425 int keyidx;
426 int hdrlen;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400427 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200428
Johannes Berg3017b802007-08-28 17:01:53 -0400429 /*
430 * Key selection 101
431 *
432 * There are three types of keys:
433 * - GTK (group keys)
434 * - PTK (pairwise keys)
435 * - STK (station-to-station pairwise keys)
436 *
437 * When selecting a key, we have to distinguish between multicast
438 * (including broadcast) and unicast frames, the latter can only
439 * use PTKs and STKs while the former always use GTKs. Unless, of
440 * course, actual WEP keys ("pre-RSNA") are used, then unicast
441 * frames can also use key indizes like GTKs. Hence, if we don't
442 * have a PTK/STK we check the key index for a WEP key.
443 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400444 * Note that in a regular BSS, multicast frames are sent by the
445 * AP only, associated stations unicast the frame to the AP first
446 * which then multicasts it on their behalf.
447 *
Johannes Berg3017b802007-08-28 17:01:53 -0400448 * There is also a slight problem in IBSS mode: GTKs are negotiated
449 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400450 * The spec seems to expect that one negotiates the same key with
451 * every station but there's no such requirement; VLANs could be
452 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400453 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200454
Johannes Berg3017b802007-08-28 17:01:53 -0400455 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
456 return TXRX_CONTINUE;
457
458 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200459 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400460 * addressed to us nor a multicast frame.
461 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400462 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400463 return TXRX_CONTINUE;
464
Johannes Bergd4e46a32007-09-14 11:10:24 -0400465 if (rx->sta)
466 stakey = rcu_dereference(rx->sta->key);
467
468 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
469 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200470 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400471 /*
472 * The device doesn't give us the IV so we won't be
473 * able to look up the key. That's ok though, we
474 * don't need to decrypt the frame, we just won't
475 * be able to keep statistics accurate.
476 * Except for key threshold notifications, should
477 * we somehow allow the driver to tell us which key
478 * the hardware used if this flag is set?
479 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400480 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
481 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400482 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200483
Johannes Berg3017b802007-08-28 17:01:53 -0400484 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200485
Johannes Berg3017b802007-08-28 17:01:53 -0400486 if (rx->skb->len < 8 + hdrlen)
487 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200488
Johannes Berg3017b802007-08-28 17:01:53 -0400489 /*
490 * no need to call ieee80211_wep_get_keyidx,
491 * it verifies a bunch of things we've done already
492 */
493 keyidx = rx->skb->data[hdrlen + 3] >> 6;
494
Johannes Bergd4e46a32007-09-14 11:10:24 -0400495 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400496
497 /*
498 * RSNA-protected unicast frames should always be sent with
499 * pairwise or station-to-station keys, but for WEP we allow
500 * using a key index as well.
501 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400502 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400503 !is_multicast_ether_addr(hdr->addr1))
504 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200505 }
506
Johannes Berg3017b802007-08-28 17:01:53 -0400507 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200508 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400509 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200510 } else {
Johannes Berg70f08762007-09-26 17:53:14 +0200511 if (net_ratelimit())
512 printk(KERN_DEBUG "%s: RX protected frame,"
513 " but have no key\n", rx->dev->name);
514 return TXRX_DROP;
515 }
516
Johannes Berg1990af82007-09-26 17:53:15 +0200517 /* Check for weak IVs if possible */
518 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
519 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
520 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
521 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
522 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
523 rx->sta->wep_weak_iv_count++;
524
Johannes Berg640845a2007-09-26 17:53:16 +0200525 /* either the frame will be decrypted or dropped */
526 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
527
Johannes Berg70f08762007-09-26 17:53:14 +0200528 switch (rx->key->conf.alg) {
529 case ALG_WEP:
530 return ieee80211_crypto_wep_decrypt(rx);
531 case ALG_TKIP:
532 return ieee80211_crypto_tkip_decrypt(rx);
533 case ALG_CCMP:
534 return ieee80211_crypto_ccmp_decrypt(rx);
535 case ALG_NONE:
Johannes Berg1990af82007-09-26 17:53:15 +0200536 WARN_ON(1);
Johannes Berg70f08762007-09-26 17:53:14 +0200537 return TXRX_CONTINUE;
538 }
539
540 /* not reached */
541 WARN_ON(1);
542 return TXRX_DROP;
543}
544
Johannes Berg571ecf62007-07-27 15:43:22 +0200545static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
546{
547 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700548 DECLARE_MAC_BUF(mac);
549
Johannes Berg571ecf62007-07-27 15:43:22 +0200550 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
551
552 if (sdata->bss)
553 atomic_inc(&sdata->bss->num_sta_ps);
554 sta->flags |= WLAN_STA_PS;
555 sta->pspoll = 0;
556#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700557 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
558 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200559#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
560}
561
562static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
563{
564 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
565 struct sk_buff *skb;
566 int sent = 0;
567 struct ieee80211_sub_if_data *sdata;
568 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700569 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200570
571 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
572 if (sdata->bss)
573 atomic_dec(&sdata->bss->num_sta_ps);
574 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
575 sta->pspoll = 0;
576 if (!skb_queue_empty(&sta->ps_tx_buf)) {
577 if (local->ops->set_tim)
578 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
579 if (sdata->bss)
580 bss_tim_clear(local, sdata->bss, sta->aid);
581 }
582#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700583 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
584 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200585#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
586 /* Send all buffered frames to the station */
587 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
588 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
589 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400590 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200591 dev_queue_xmit(skb);
592 }
593 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
594 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
595 local->total_ps_buffered--;
596 sent++;
597#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700598 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200599 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700600 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200601#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400602 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200603 dev_queue_xmit(skb);
604 }
605
606 return sent;
607}
608
609static ieee80211_txrx_result
610ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
611{
612 struct sta_info *sta = rx->sta;
613 struct net_device *dev = rx->dev;
614 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
615
616 if (!sta)
617 return TXRX_CONTINUE;
618
619 /* Update last_rx only for IBSS packets which are for the current
620 * BSSID to avoid keeping the current IBSS network alive in cases where
621 * other STAs are using different BSSID. */
622 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
623 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
624 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
625 sta->last_rx = jiffies;
626 } else
627 if (!is_multicast_ether_addr(hdr->addr1) ||
628 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
629 /* Update last_rx only for unicast frames in order to prevent
630 * the Probe Request frames (the only broadcast frames from a
631 * STA in infrastructure mode) from keeping a connection alive.
632 */
633 sta->last_rx = jiffies;
634 }
635
Jiri Slabybadffb72007-08-28 17:01:54 -0400636 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200637 return TXRX_CONTINUE;
638
639 sta->rx_fragments++;
640 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400641 sta->last_rssi = rx->u.rx.status->ssi;
642 sta->last_signal = rx->u.rx.status->signal;
643 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200644
645 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
646 /* Change STA power saving mode only in the end of a frame
647 * exchange sequence */
648 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
649 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
650 else if (!(sta->flags & WLAN_STA_PS) &&
651 (rx->fc & IEEE80211_FCTL_PM))
652 ap_sta_ps_start(dev, sta);
653 }
654
655 /* Drop data::nullfunc frames silently, since they are used only to
656 * control station power saving mode. */
657 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
658 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
659 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
660 /* Update counter and free packet here to avoid counting this
661 * as a dropped packed. */
662 sta->rx_packets++;
663 dev_kfree_skb(rx->skb);
664 return TXRX_QUEUED;
665 }
666
667 return TXRX_CONTINUE;
668} /* ieee80211_rx_h_sta_process */
669
Johannes Berg571ecf62007-07-27 15:43:22 +0200670static inline struct ieee80211_fragment_entry *
671ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
672 unsigned int frag, unsigned int seq, int rx_queue,
673 struct sk_buff **skb)
674{
675 struct ieee80211_fragment_entry *entry;
676 int idx;
677
678 idx = sdata->fragment_next;
679 entry = &sdata->fragments[sdata->fragment_next++];
680 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
681 sdata->fragment_next = 0;
682
683 if (!skb_queue_empty(&entry->skb_list)) {
684#ifdef CONFIG_MAC80211_DEBUG
685 struct ieee80211_hdr *hdr =
686 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700687 DECLARE_MAC_BUF(mac);
688 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200689 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
690 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700691 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200692 sdata->dev->name, idx,
693 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700694 entry->last_frag, print_mac(mac, hdr->addr1),
695 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200696#endif /* CONFIG_MAC80211_DEBUG */
697 __skb_queue_purge(&entry->skb_list);
698 }
699
700 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
701 *skb = NULL;
702 entry->first_frag_time = jiffies;
703 entry->seq = seq;
704 entry->rx_queue = rx_queue;
705 entry->last_frag = frag;
706 entry->ccmp = 0;
707 entry->extra_len = 0;
708
709 return entry;
710}
711
712static inline struct ieee80211_fragment_entry *
713ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
714 u16 fc, unsigned int frag, unsigned int seq,
715 int rx_queue, struct ieee80211_hdr *hdr)
716{
717 struct ieee80211_fragment_entry *entry;
718 int i, idx;
719
720 idx = sdata->fragment_next;
721 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
722 struct ieee80211_hdr *f_hdr;
723 u16 f_fc;
724
725 idx--;
726 if (idx < 0)
727 idx = IEEE80211_FRAGMENT_MAX - 1;
728
729 entry = &sdata->fragments[idx];
730 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
731 entry->rx_queue != rx_queue ||
732 entry->last_frag + 1 != frag)
733 continue;
734
735 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
736 f_fc = le16_to_cpu(f_hdr->frame_control);
737
738 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
739 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
740 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
741 continue;
742
743 if (entry->first_frag_time + 2 * HZ < jiffies) {
744 __skb_queue_purge(&entry->skb_list);
745 continue;
746 }
747 return entry;
748 }
749
750 return NULL;
751}
752
753static ieee80211_txrx_result
754ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
755{
756 struct ieee80211_hdr *hdr;
757 u16 sc;
758 unsigned int frag, seq;
759 struct ieee80211_fragment_entry *entry;
760 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700761 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200762
763 hdr = (struct ieee80211_hdr *) rx->skb->data;
764 sc = le16_to_cpu(hdr->seq_ctrl);
765 frag = sc & IEEE80211_SCTL_FRAG;
766
767 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
768 (rx->skb)->len < 24 ||
769 is_multicast_ether_addr(hdr->addr1))) {
770 /* not fragmented */
771 goto out;
772 }
773 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
774
775 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
776
777 if (frag == 0) {
778 /* This is the first fragment of a new frame. */
779 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
780 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400781 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200782 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
783 /* Store CCMP PN so that we can verify that the next
784 * fragment has a sequential PN value. */
785 entry->ccmp = 1;
786 memcpy(entry->last_pn,
787 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
788 CCMP_PN_LEN);
789 }
790 return TXRX_QUEUED;
791 }
792
793 /* This is a fragment for a frame that should already be pending in
794 * fragment cache. Add this fragment to the end of the pending entry.
795 */
796 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
797 rx->u.rx.queue, hdr);
798 if (!entry) {
799 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
800 return TXRX_DROP;
801 }
802
803 /* Verify that MPDUs within one MSDU have sequential PN values.
804 * (IEEE 802.11i, 8.3.3.4.5) */
805 if (entry->ccmp) {
806 int i;
807 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400808 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200809 return TXRX_DROP;
810 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
811 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
812 pn[i]++;
813 if (pn[i])
814 break;
815 }
816 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
817 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400818 if (net_ratelimit())
819 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700820 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400821 " PN=%02x%02x%02x%02x%02x%02x "
822 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700823 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400824 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
825 rpn[5], pn[0], pn[1], pn[2], pn[3],
826 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200827 return TXRX_DROP;
828 }
829 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
830 }
831
832 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
833 __skb_queue_tail(&entry->skb_list, rx->skb);
834 entry->last_frag = frag;
835 entry->extra_len += rx->skb->len;
836 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
837 rx->skb = NULL;
838 return TXRX_QUEUED;
839 }
840
841 rx->skb = __skb_dequeue(&entry->skb_list);
842 if (skb_tailroom(rx->skb) < entry->extra_len) {
843 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
844 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
845 GFP_ATOMIC))) {
846 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
847 __skb_queue_purge(&entry->skb_list);
848 return TXRX_DROP;
849 }
850 }
851 while ((skb = __skb_dequeue(&entry->skb_list))) {
852 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
853 dev_kfree_skb(skb);
854 }
855
856 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400857 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200858
859 out:
860 if (rx->sta)
861 rx->sta->rx_packets++;
862 if (is_multicast_ether_addr(hdr->addr1))
863 rx->local->dot11MulticastReceivedFrameCount++;
864 else
865 ieee80211_led_rx(rx->local);
866 return TXRX_CONTINUE;
867}
868
869static ieee80211_txrx_result
870ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
871{
872 struct sk_buff *skb;
873 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700874 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200875
876 if (likely(!rx->sta ||
877 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
878 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400879 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200880 return TXRX_CONTINUE;
881
882 skb = skb_dequeue(&rx->sta->tx_filtered);
883 if (!skb) {
884 skb = skb_dequeue(&rx->sta->ps_tx_buf);
885 if (skb)
886 rx->local->total_ps_buffered--;
887 }
888 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
889 skb_queue_empty(&rx->sta->ps_tx_buf);
890
891 if (skb) {
892 struct ieee80211_hdr *hdr =
893 (struct ieee80211_hdr *) skb->data;
894
895 /* tell TX path to send one frame even though the STA may
896 * still remain is PS mode after this frame exchange */
897 rx->sta->pspoll = 1;
898
899#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700900 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
901 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200902 skb_queue_len(&rx->sta->ps_tx_buf));
903#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
904
905 /* Use MoreData flag to indicate whether there are more
906 * buffered frames for this STA */
907 if (no_pending_pkts) {
908 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
909 rx->sta->flags &= ~WLAN_STA_TIM;
910 } else
911 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
912
913 dev_queue_xmit(skb);
914
915 if (no_pending_pkts) {
916 if (rx->local->ops->set_tim)
917 rx->local->ops->set_tim(local_to_hw(rx->local),
918 rx->sta->aid, 0);
919 if (rx->sdata->bss)
920 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
921 }
922#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
923 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700924 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200925 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700926 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200927#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
928
929 }
930
931 /* Free PS Poll skb here instead of returning TXRX_DROP that would
932 * count as an dropped frame. */
933 dev_kfree_skb(rx->skb);
934
935 return TXRX_QUEUED;
936}
937
938static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200939ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
940{
941 u16 fc = rx->fc;
942 u8 *data = rx->skb->data;
943 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
944
945 if (!WLAN_FC_IS_QOS_DATA(fc))
946 return TXRX_CONTINUE;
947
948 /* remove the qos control field, update frame type and meta-data */
949 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
950 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
951 /* change frame type to non QOS */
952 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
953 hdr->frame_control = cpu_to_le16(fc);
954
955 return TXRX_CONTINUE;
956}
957
958static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200959ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
960{
961 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400962 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
Johannes Bergf9d540e2007-09-28 14:02:09 +0200963 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
964 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200965
966 if (unlikely(rx->sdata->ieee802_1x &&
967 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
968 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
969 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
970 !ieee80211_is_eapol(rx->skb))) {
971#ifdef CONFIG_MAC80211_DEBUG
972 struct ieee80211_hdr *hdr =
973 (struct ieee80211_hdr *) rx->skb->data;
Joe Perches0795af52007-10-03 17:59:30 -0700974 DECLARE_MAC_BUF(mac);
975 printk(KERN_DEBUG "%s: dropped frame from %s"
Johannes Berg571ecf62007-07-27 15:43:22 +0200976 " (unauthorized port)\n", rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700977 print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200978#endif /* CONFIG_MAC80211_DEBUG */
979 return TXRX_DROP;
980 }
981
982 return TXRX_CONTINUE;
983}
984
985static ieee80211_txrx_result
986ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
987{
Johannes Berg3017b802007-08-28 17:01:53 -0400988 /*
Johannes Berg7848ba72007-09-14 11:10:25 -0400989 * Pass through unencrypted frames if the hardware has
990 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -0400991 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400992 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Johannes Berg571ecf62007-07-27 15:43:22 +0200993 return TXRX_CONTINUE;
994
995 /* Drop unencrypted frames if key is set. */
996 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
997 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
998 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Berg640845a2007-09-26 17:53:16 +0200999 rx->sdata->drop_unencrypted &&
1000 (rx->sdata->eapol == 0 || !ieee80211_is_eapol(rx->skb)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001001 if (net_ratelimit())
1002 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1003 "encryption\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001004 return TXRX_DROP;
1005 }
1006 return TXRX_CONTINUE;
1007}
1008
1009static ieee80211_txrx_result
1010ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1011{
1012 struct net_device *dev = rx->dev;
1013 struct ieee80211_local *local = rx->local;
1014 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1015 u16 fc, hdrlen, ethertype;
1016 u8 *payload;
1017 u8 dst[ETH_ALEN];
1018 u8 src[ETH_ALEN];
1019 struct sk_buff *skb = rx->skb, *skb2;
1020 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001021 DECLARE_MAC_BUF(mac);
1022 DECLARE_MAC_BUF(mac2);
1023 DECLARE_MAC_BUF(mac3);
1024 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001025
1026 fc = rx->fc;
1027 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1028 return TXRX_CONTINUE;
1029
1030 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1031 return TXRX_DROP;
1032
1033 hdrlen = ieee80211_get_hdrlen(fc);
1034
1035 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1036 * header
1037 * IEEE 802.11 address fields:
1038 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1039 * 0 0 DA SA BSSID n/a
1040 * 0 1 DA BSSID SA n/a
1041 * 1 0 BSSID SA DA n/a
1042 * 1 1 RA TA DA SA
1043 */
1044
1045 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1046 case IEEE80211_FCTL_TODS:
1047 /* BSSID SA DA */
1048 memcpy(dst, hdr->addr3, ETH_ALEN);
1049 memcpy(src, hdr->addr2, ETH_ALEN);
1050
1051 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1052 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001053 if (net_ratelimit())
1054 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001055 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001056 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001057 print_mac(mac, hdr->addr1),
1058 print_mac(mac2, hdr->addr2),
1059 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001060 return TXRX_DROP;
1061 }
1062 break;
1063 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1064 /* RA TA DA SA */
1065 memcpy(dst, hdr->addr3, ETH_ALEN);
1066 memcpy(src, hdr->addr4, ETH_ALEN);
1067
1068 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001069 if (net_ratelimit())
1070 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001071 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001072 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001073 print_mac(mac, hdr->addr1),
1074 print_mac(mac2, hdr->addr2),
1075 print_mac(mac3, hdr->addr3),
1076 print_mac(mac4, hdr->addr4));
Johannes Berg571ecf62007-07-27 15:43:22 +02001077 return TXRX_DROP;
1078 }
1079 break;
1080 case IEEE80211_FCTL_FROMDS:
1081 /* DA BSSID SA */
1082 memcpy(dst, hdr->addr1, ETH_ALEN);
1083 memcpy(src, hdr->addr3, ETH_ALEN);
1084
John W. Linvilleb3316152007-08-28 17:01:55 -04001085 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1086 (is_multicast_ether_addr(dst) &&
1087 !compare_ether_addr(src, dev->dev_addr)))
Johannes Berg571ecf62007-07-27 15:43:22 +02001088 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +02001089 break;
1090 case 0:
1091 /* DA SA BSSID */
1092 memcpy(dst, hdr->addr1, ETH_ALEN);
1093 memcpy(src, hdr->addr2, ETH_ALEN);
1094
1095 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1096 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001097 printk(KERN_DEBUG "%s: dropped IBSS frame "
1098 "(DA=%s SA=%s BSSID=%s)\n",
1099 dev->name,
1100 print_mac(mac, hdr->addr1),
1101 print_mac(mac2, hdr->addr2),
1102 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001103 }
1104 return TXRX_DROP;
1105 }
1106 break;
1107 }
1108
1109 payload = skb->data + hdrlen;
1110
1111 if (unlikely(skb->len - hdrlen < 8)) {
1112 if (net_ratelimit()) {
1113 printk(KERN_DEBUG "%s: RX too short data frame "
1114 "payload\n", dev->name);
1115 }
1116 return TXRX_DROP;
1117 }
1118
1119 ethertype = (payload[6] << 8) | payload[7];
1120
1121 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1122 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1123 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1124 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1125 * replace EtherType */
1126 skb_pull(skb, hdrlen + 6);
1127 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1128 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1129 } else {
1130 struct ethhdr *ehdr;
1131 __be16 len;
1132 skb_pull(skb, hdrlen);
1133 len = htons(skb->len);
1134 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1135 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1136 memcpy(ehdr->h_source, src, ETH_ALEN);
1137 ehdr->h_proto = len;
1138 }
1139 skb->dev = dev;
1140
1141 skb2 = NULL;
1142
Stephen Hemminger68aae112007-08-24 11:29:34 -07001143 dev->stats.rx_packets++;
1144 dev->stats.rx_bytes += skb->len;
Johannes Berg571ecf62007-07-27 15:43:22 +02001145
1146 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
Jiri Slabybadffb72007-08-28 17:01:54 -04001147 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1148 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001149 if (is_multicast_ether_addr(skb->data)) {
1150 /* send multicast frames both to higher layers in
1151 * local net stack and back to the wireless media */
1152 skb2 = skb_copy(skb, GFP_ATOMIC);
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001153 if (!skb2 && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001154 printk(KERN_DEBUG "%s: failed to clone "
1155 "multicast frame\n", dev->name);
1156 } else {
1157 struct sta_info *dsta;
1158 dsta = sta_info_get(local, skb->data);
1159 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001160 if (net_ratelimit())
1161 printk(KERN_DEBUG "Station with null "
1162 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001163 } else if (dsta && dsta->dev == dev) {
1164 /* Destination station is associated to this
1165 * AP, so send the frame directly to it and
1166 * do not pass the frame to local net stack.
1167 */
1168 skb2 = skb;
1169 skb = NULL;
1170 }
1171 if (dsta)
1172 sta_info_put(dsta);
1173 }
1174 }
1175
1176 if (skb) {
1177 /* deliver to local stack */
1178 skb->protocol = eth_type_trans(skb, dev);
1179 memset(skb->cb, 0, sizeof(skb->cb));
1180 netif_rx(skb);
1181 }
1182
1183 if (skb2) {
1184 /* send to wireless media */
1185 skb2->protocol = __constant_htons(ETH_P_802_3);
1186 skb_set_network_header(skb2, 0);
1187 skb_set_mac_header(skb2, 0);
1188 dev_queue_xmit(skb2);
1189 }
1190
1191 return TXRX_QUEUED;
1192}
1193
1194static ieee80211_txrx_result
1195ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1196{
1197 struct ieee80211_sub_if_data *sdata;
1198
Jiri Slabybadffb72007-08-28 17:01:54 -04001199 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001200 return TXRX_DROP;
1201
1202 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1203 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1204 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergf9d540e2007-09-28 14:02:09 +02001205 !rx->local->user_space_mlme)
Johannes Berg571ecf62007-07-27 15:43:22 +02001206 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001207 else
1208 return TXRX_DROP;
1209
Johannes Berg571ecf62007-07-27 15:43:22 +02001210 return TXRX_QUEUED;
1211}
1212
1213static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1214 struct ieee80211_local *local,
1215 ieee80211_rx_handler *handlers,
1216 struct ieee80211_txrx_data *rx,
1217 struct sta_info *sta)
1218{
1219 ieee80211_rx_handler *handler;
1220 ieee80211_txrx_result res = TXRX_DROP;
1221
1222 for (handler = handlers; *handler != NULL; handler++) {
1223 res = (*handler)(rx);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001224
1225 switch (res) {
1226 case TXRX_CONTINUE:
1227 continue;
1228 case TXRX_DROP:
1229 I802_DEBUG_INC(local->rx_handlers_drop);
1230 if (sta)
1231 sta->rx_dropped++;
1232 break;
1233 case TXRX_QUEUED:
1234 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001235 break;
1236 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001237 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001238 }
1239
Johannes Berg8e6f00322007-07-27 15:43:22 +02001240 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001241 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001242 return res;
1243}
1244
1245static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1246 ieee80211_rx_handler *handlers,
1247 struct ieee80211_txrx_data *rx,
1248 struct sta_info *sta)
1249{
1250 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1251 TXRX_CONTINUE)
1252 dev_kfree_skb(rx->skb);
1253}
1254
1255static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1256 struct ieee80211_hdr *hdr,
1257 struct sta_info *sta,
1258 struct ieee80211_txrx_data *rx)
1259{
1260 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001261 DECLARE_MAC_BUF(mac);
1262 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001263
1264 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1265 if (rx->skb->len >= hdrlen + 4)
1266 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1267 else
1268 keyidx = -1;
1269
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001270 if (net_ratelimit())
1271 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001272 "failure from %s to %s keyidx=%d\n",
1273 dev->name, print_mac(mac, hdr->addr2),
1274 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001275
1276 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001277 /*
1278 * Some hardware seem to generate incorrect Michael MIC
1279 * reports; ignore them to avoid triggering countermeasures.
1280 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001281 if (net_ratelimit())
1282 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001283 "error for unknown address %s\n",
1284 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001285 goto ignore;
1286 }
1287
1288 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001289 if (net_ratelimit())
1290 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001291 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001292 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001293 goto ignore;
1294 }
1295
Johannes Berg7848ba72007-09-14 11:10:25 -04001296 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001297 /*
1298 * APs with pairwise keys should never receive Michael MIC
1299 * errors for non-zero keyidx because these are reserved for
1300 * group keys and only the AP is sending real multicast
1301 * frames in the BSS.
1302 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001303 if (net_ratelimit())
1304 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1305 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001306 " (src %s)\n", dev->name, keyidx,
1307 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001308 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001309 }
1310
1311 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1312 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1313 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001314 if (net_ratelimit())
1315 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1316 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001317 "(fc=0x%04x) (src %s)\n",
1318 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001319 goto ignore;
1320 }
1321
Johannes Bergeb063c12007-08-28 17:01:53 -04001322 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001323 ignore:
1324 dev_kfree_skb(rx->skb);
1325 rx->skb = NULL;
1326}
1327
1328ieee80211_rx_handler ieee80211_rx_handlers[] =
1329{
1330 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001331 ieee80211_rx_h_passive_scan,
1332 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001333 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001334 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001335 ieee80211_rx_h_defragment,
1336 ieee80211_rx_h_ps_poll,
1337 ieee80211_rx_h_michael_mic_verify,
1338 /* this must be after decryption - so header is counted in MPDU mic
1339 * must be before pae and data, so QOS_DATA format frames
1340 * are not passed to user space by these functions
1341 */
1342 ieee80211_rx_h_remove_qos_control,
1343 ieee80211_rx_h_802_1x_pae,
1344 ieee80211_rx_h_drop_unencrypted,
1345 ieee80211_rx_h_data,
1346 ieee80211_rx_h_mgmt,
1347 NULL
1348};
1349
1350/* main receive path */
1351
Johannes Berg23a24de2007-07-27 15:43:22 +02001352static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1353 u8 *bssid, struct ieee80211_txrx_data *rx,
1354 struct ieee80211_hdr *hdr)
1355{
1356 int multicast = is_multicast_ether_addr(hdr->addr1);
1357
1358 switch (sdata->type) {
1359 case IEEE80211_IF_TYPE_STA:
1360 if (!bssid)
1361 return 0;
1362 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001363 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001364 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001365 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001366 } else if (!multicast &&
1367 compare_ether_addr(sdata->dev->dev_addr,
1368 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001369 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001370 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001371 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001372 }
1373 break;
1374 case IEEE80211_IF_TYPE_IBSS:
1375 if (!bssid)
1376 return 0;
1377 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001378 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001379 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001380 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001381 } else if (!multicast &&
1382 compare_ether_addr(sdata->dev->dev_addr,
1383 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001384 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001385 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001386 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001387 } else if (!rx->sta)
1388 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1389 bssid, hdr->addr2);
1390 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001391 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001392 case IEEE80211_IF_TYPE_AP:
1393 if (!bssid) {
1394 if (compare_ether_addr(sdata->dev->dev_addr,
1395 hdr->addr1))
1396 return 0;
1397 } else if (!ieee80211_bssid_match(bssid,
1398 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001399 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001400 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001401 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001402 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001403 if (sdata->dev == sdata->local->mdev &&
1404 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001405 /* do not receive anything via
1406 * master device when not scanning */
1407 return 0;
1408 break;
1409 case IEEE80211_IF_TYPE_WDS:
1410 if (bssid ||
1411 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1412 return 0;
1413 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1414 return 0;
1415 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001416 case IEEE80211_IF_TYPE_MNTR:
1417 /* take everything */
1418 break;
Johannes Berga2897552007-09-28 14:01:25 +02001419 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001420 /* should never get here */
1421 WARN_ON(1);
1422 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001423 }
1424
1425 return 1;
1426}
1427
Johannes Berg571ecf62007-07-27 15:43:22 +02001428/*
1429 * This is the receive path handler. It is called by a low level driver when an
1430 * 802.11 MPDU is received from the hardware.
1431 */
1432void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1433 struct ieee80211_rx_status *status)
1434{
1435 struct ieee80211_local *local = hw_to_local(hw);
1436 struct ieee80211_sub_if_data *sdata;
1437 struct sta_info *sta;
1438 struct ieee80211_hdr *hdr;
1439 struct ieee80211_txrx_data rx;
1440 u16 type;
Johannes Bergb2e77712007-09-26 15:19:39 +02001441 int prepres;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001442 struct ieee80211_sub_if_data *prev = NULL;
1443 struct sk_buff *skb_new;
1444 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001445
Johannes Bergd4e46a32007-09-14 11:10:24 -04001446 /*
Johannes Berg79010422007-09-18 17:29:21 -04001447 * key references and virtual interfaces are protected using RCU
1448 * and this requires that we are in a read-side RCU section during
1449 * receive processing
Johannes Bergd4e46a32007-09-14 11:10:24 -04001450 */
1451 rcu_read_lock();
1452
Johannes Bergb2e77712007-09-26 15:19:39 +02001453 /*
1454 * Frames with failed FCS/PLCP checksum are not returned,
1455 * all other frames are returned without radiotap header
1456 * if it was previously present.
1457 * Also, frames with less than 16 bytes are dropped.
1458 */
1459 skb = ieee80211_rx_monitor(local, skb, status);
1460 if (!skb) {
1461 rcu_read_unlock();
1462 return;
1463 }
1464
Johannes Berg571ecf62007-07-27 15:43:22 +02001465 hdr = (struct ieee80211_hdr *) skb->data;
1466 memset(&rx, 0, sizeof(rx));
1467 rx.skb = skb;
1468 rx.local = local;
1469
1470 rx.u.rx.status = status;
Johannes Bergb2e77712007-09-26 15:19:39 +02001471 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001472 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001473
Johannes Bergb2e77712007-09-26 15:19:39 +02001474 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001475 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001476
Johannes Bergb2e77712007-09-26 15:19:39 +02001477 sta = rx.sta = sta_info_get(local, hdr->addr2);
1478 if (sta) {
1479 rx.dev = rx.sta->dev;
1480 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1481 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001482
Johannes Berg571ecf62007-07-27 15:43:22 +02001483 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1484 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1485 goto end;
1486 }
1487
1488 if (unlikely(local->sta_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001489 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001490
1491 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1492 sta) != TXRX_CONTINUE)
1493 goto end;
1494 skb = rx.skb;
1495
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001496 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001497 !atomic_read(&local->iff_promiscs) &&
1498 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001499 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001500 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001501 rx.sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001502 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001503 rcu_read_unlock();
Johannes Berg8e6f00322007-07-27 15:43:22 +02001504 return;
1505 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001506
Johannes Bergb2e77712007-09-26 15:19:39 +02001507 bssid = ieee80211_get_bssid(hdr, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001508
Johannes Berg79010422007-09-18 17:29:21 -04001509 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001510 if (!netif_running(sdata->dev))
1511 continue;
1512
Johannes Bergb2e77712007-09-26 15:19:39 +02001513 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1514 continue;
1515
1516 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001517 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1518 /* prepare_for_handlers can change sta */
1519 sta = rx.sta;
1520
1521 if (!prepres)
1522 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001523
Johannes Berg340e11f2007-07-27 15:43:22 +02001524 /*
1525 * frame is destined for this interface, but if it's not
1526 * also for the previous one we handle that after the
1527 * loop to avoid copying the SKB once too much
1528 */
1529
1530 if (!prev) {
1531 prev = sdata;
1532 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001533 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001534
1535 /*
1536 * frame was destined for the previous interface
1537 * so invoke RX handlers for it
1538 */
1539
1540 skb_new = skb_copy(skb, GFP_ATOMIC);
1541 if (!skb_new) {
1542 if (net_ratelimit())
1543 printk(KERN_DEBUG "%s: failed to copy "
1544 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001545 wiphy_name(local->hw.wiphy),
1546 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001547 continue;
1548 }
1549 rx.skb = skb_new;
1550 rx.dev = prev->dev;
1551 rx.sdata = prev;
1552 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1553 &rx, sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001554 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001555 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001556 if (prev) {
1557 rx.skb = skb;
1558 rx.dev = prev->dev;
1559 rx.sdata = prev;
1560 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1561 &rx, sta);
1562 } else
1563 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001564
Johannes Berg8e6f00322007-07-27 15:43:22 +02001565 end:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001566 rcu_read_unlock();
1567
Johannes Berg571ecf62007-07-27 15:43:22 +02001568 if (sta)
1569 sta_info_put(sta);
1570}
1571EXPORT_SYMBOL(__ieee80211_rx);
1572
1573/* This is a version of the rx handler that can be called from hard irq
1574 * context. Post the skb on the queue and schedule the tasklet */
1575void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1576 struct ieee80211_rx_status *status)
1577{
1578 struct ieee80211_local *local = hw_to_local(hw);
1579
1580 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1581
1582 skb->dev = local->mdev;
1583 /* copy status into skb->cb for use by tasklet */
1584 memcpy(skb->cb, status, sizeof(*status));
1585 skb->pkt_type = IEEE80211_RX_MSG;
1586 skb_queue_tail(&local->skb_queue, skb);
1587 tasklet_schedule(&local->tasklet);
1588}
1589EXPORT_SYMBOL(ieee80211_rx_irqsafe);