blob: 6973d5c8e9e06302c9e8139be550058aeef0bc8e [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
422ieee80211_rx_h_load_key(struct ieee80211_txrx_data *rx)
423{
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 /*
459 * No point in finding a key if the frame is neither
460 * 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 Berg571ecf62007-07-27 15:43:22 +0200510 }
511
512 return TXRX_CONTINUE;
513}
514
Johannes Berg70f08762007-09-26 17:53:14 +0200515static ieee80211_txrx_result
516ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
517{
518 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
519 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
520 !rx->key || rx->key->conf.alg != ALG_WEP ||
521 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
522 return TXRX_CONTINUE;
523
524 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
525 if (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
526 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED))
527 if (ieee80211_wep_is_weak_iv(rx->skb, rx->key))
528 rx->sta->wep_weak_iv_count++;
529
530 return TXRX_CONTINUE;
531}
532
533static ieee80211_txrx_result
534ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
535{
536 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
537 return TXRX_CONTINUE;
538
539 if (!rx->key) {
540 if (net_ratelimit())
541 printk(KERN_DEBUG "%s: RX protected frame,"
542 " but have no key\n", rx->dev->name);
543 return TXRX_DROP;
544 }
545
546 switch (rx->key->conf.alg) {
547 case ALG_WEP:
548 return ieee80211_crypto_wep_decrypt(rx);
549 case ALG_TKIP:
550 return ieee80211_crypto_tkip_decrypt(rx);
551 case ALG_CCMP:
552 return ieee80211_crypto_ccmp_decrypt(rx);
553 case ALG_NONE:
554 return TXRX_CONTINUE;
555 }
556
557 /* not reached */
558 WARN_ON(1);
559 return TXRX_DROP;
560}
561
Johannes Berg571ecf62007-07-27 15:43:22 +0200562static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
563{
564 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700565 DECLARE_MAC_BUF(mac);
566
Johannes Berg571ecf62007-07-27 15:43:22 +0200567 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
568
569 if (sdata->bss)
570 atomic_inc(&sdata->bss->num_sta_ps);
571 sta->flags |= WLAN_STA_PS;
572 sta->pspoll = 0;
573#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700574 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
575 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200576#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
577}
578
579static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
580{
581 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
582 struct sk_buff *skb;
583 int sent = 0;
584 struct ieee80211_sub_if_data *sdata;
585 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700586 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200587
588 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
589 if (sdata->bss)
590 atomic_dec(&sdata->bss->num_sta_ps);
591 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
592 sta->pspoll = 0;
593 if (!skb_queue_empty(&sta->ps_tx_buf)) {
594 if (local->ops->set_tim)
595 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
596 if (sdata->bss)
597 bss_tim_clear(local, sdata->bss, sta->aid);
598 }
599#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700600 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
601 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200602#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
603 /* Send all buffered frames to the station */
604 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
605 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
606 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400607 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200608 dev_queue_xmit(skb);
609 }
610 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
611 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
612 local->total_ps_buffered--;
613 sent++;
614#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700615 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200616 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700617 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200618#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400619 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200620 dev_queue_xmit(skb);
621 }
622
623 return sent;
624}
625
626static ieee80211_txrx_result
627ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
628{
629 struct sta_info *sta = rx->sta;
630 struct net_device *dev = rx->dev;
631 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
632
633 if (!sta)
634 return TXRX_CONTINUE;
635
636 /* Update last_rx only for IBSS packets which are for the current
637 * BSSID to avoid keeping the current IBSS network alive in cases where
638 * other STAs are using different BSSID. */
639 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
640 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
641 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
642 sta->last_rx = jiffies;
643 } else
644 if (!is_multicast_ether_addr(hdr->addr1) ||
645 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
646 /* Update last_rx only for unicast frames in order to prevent
647 * the Probe Request frames (the only broadcast frames from a
648 * STA in infrastructure mode) from keeping a connection alive.
649 */
650 sta->last_rx = jiffies;
651 }
652
Jiri Slabybadffb72007-08-28 17:01:54 -0400653 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200654 return TXRX_CONTINUE;
655
656 sta->rx_fragments++;
657 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400658 sta->last_rssi = rx->u.rx.status->ssi;
659 sta->last_signal = rx->u.rx.status->signal;
660 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200661
662 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
663 /* Change STA power saving mode only in the end of a frame
664 * exchange sequence */
665 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
666 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
667 else if (!(sta->flags & WLAN_STA_PS) &&
668 (rx->fc & IEEE80211_FCTL_PM))
669 ap_sta_ps_start(dev, sta);
670 }
671
672 /* Drop data::nullfunc frames silently, since they are used only to
673 * control station power saving mode. */
674 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
675 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
676 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
677 /* Update counter and free packet here to avoid counting this
678 * as a dropped packed. */
679 sta->rx_packets++;
680 dev_kfree_skb(rx->skb);
681 return TXRX_QUEUED;
682 }
683
684 return TXRX_CONTINUE;
685} /* ieee80211_rx_h_sta_process */
686
Johannes Berg571ecf62007-07-27 15:43:22 +0200687static inline struct ieee80211_fragment_entry *
688ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
689 unsigned int frag, unsigned int seq, int rx_queue,
690 struct sk_buff **skb)
691{
692 struct ieee80211_fragment_entry *entry;
693 int idx;
694
695 idx = sdata->fragment_next;
696 entry = &sdata->fragments[sdata->fragment_next++];
697 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
698 sdata->fragment_next = 0;
699
700 if (!skb_queue_empty(&entry->skb_list)) {
701#ifdef CONFIG_MAC80211_DEBUG
702 struct ieee80211_hdr *hdr =
703 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700704 DECLARE_MAC_BUF(mac);
705 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200706 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
707 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700708 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200709 sdata->dev->name, idx,
710 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700711 entry->last_frag, print_mac(mac, hdr->addr1),
712 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200713#endif /* CONFIG_MAC80211_DEBUG */
714 __skb_queue_purge(&entry->skb_list);
715 }
716
717 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
718 *skb = NULL;
719 entry->first_frag_time = jiffies;
720 entry->seq = seq;
721 entry->rx_queue = rx_queue;
722 entry->last_frag = frag;
723 entry->ccmp = 0;
724 entry->extra_len = 0;
725
726 return entry;
727}
728
729static inline struct ieee80211_fragment_entry *
730ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
731 u16 fc, unsigned int frag, unsigned int seq,
732 int rx_queue, struct ieee80211_hdr *hdr)
733{
734 struct ieee80211_fragment_entry *entry;
735 int i, idx;
736
737 idx = sdata->fragment_next;
738 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
739 struct ieee80211_hdr *f_hdr;
740 u16 f_fc;
741
742 idx--;
743 if (idx < 0)
744 idx = IEEE80211_FRAGMENT_MAX - 1;
745
746 entry = &sdata->fragments[idx];
747 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
748 entry->rx_queue != rx_queue ||
749 entry->last_frag + 1 != frag)
750 continue;
751
752 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
753 f_fc = le16_to_cpu(f_hdr->frame_control);
754
755 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
756 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
757 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
758 continue;
759
760 if (entry->first_frag_time + 2 * HZ < jiffies) {
761 __skb_queue_purge(&entry->skb_list);
762 continue;
763 }
764 return entry;
765 }
766
767 return NULL;
768}
769
770static ieee80211_txrx_result
771ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
772{
773 struct ieee80211_hdr *hdr;
774 u16 sc;
775 unsigned int frag, seq;
776 struct ieee80211_fragment_entry *entry;
777 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700778 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200779
780 hdr = (struct ieee80211_hdr *) rx->skb->data;
781 sc = le16_to_cpu(hdr->seq_ctrl);
782 frag = sc & IEEE80211_SCTL_FRAG;
783
784 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
785 (rx->skb)->len < 24 ||
786 is_multicast_ether_addr(hdr->addr1))) {
787 /* not fragmented */
788 goto out;
789 }
790 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
791
792 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
793
794 if (frag == 0) {
795 /* This is the first fragment of a new frame. */
796 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
797 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400798 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200799 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
800 /* Store CCMP PN so that we can verify that the next
801 * fragment has a sequential PN value. */
802 entry->ccmp = 1;
803 memcpy(entry->last_pn,
804 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
805 CCMP_PN_LEN);
806 }
807 return TXRX_QUEUED;
808 }
809
810 /* This is a fragment for a frame that should already be pending in
811 * fragment cache. Add this fragment to the end of the pending entry.
812 */
813 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
814 rx->u.rx.queue, hdr);
815 if (!entry) {
816 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
817 return TXRX_DROP;
818 }
819
820 /* Verify that MPDUs within one MSDU have sequential PN values.
821 * (IEEE 802.11i, 8.3.3.4.5) */
822 if (entry->ccmp) {
823 int i;
824 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400825 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200826 return TXRX_DROP;
827 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
828 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
829 pn[i]++;
830 if (pn[i])
831 break;
832 }
833 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
834 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400835 if (net_ratelimit())
836 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700837 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400838 " PN=%02x%02x%02x%02x%02x%02x "
839 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700840 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400841 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
842 rpn[5], pn[0], pn[1], pn[2], pn[3],
843 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200844 return TXRX_DROP;
845 }
846 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
847 }
848
849 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
850 __skb_queue_tail(&entry->skb_list, rx->skb);
851 entry->last_frag = frag;
852 entry->extra_len += rx->skb->len;
853 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
854 rx->skb = NULL;
855 return TXRX_QUEUED;
856 }
857
858 rx->skb = __skb_dequeue(&entry->skb_list);
859 if (skb_tailroom(rx->skb) < entry->extra_len) {
860 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
861 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
862 GFP_ATOMIC))) {
863 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
864 __skb_queue_purge(&entry->skb_list);
865 return TXRX_DROP;
866 }
867 }
868 while ((skb = __skb_dequeue(&entry->skb_list))) {
869 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
870 dev_kfree_skb(skb);
871 }
872
873 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400874 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200875
876 out:
877 if (rx->sta)
878 rx->sta->rx_packets++;
879 if (is_multicast_ether_addr(hdr->addr1))
880 rx->local->dot11MulticastReceivedFrameCount++;
881 else
882 ieee80211_led_rx(rx->local);
883 return TXRX_CONTINUE;
884}
885
886static ieee80211_txrx_result
887ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
888{
889 struct sk_buff *skb;
890 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700891 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200892
893 if (likely(!rx->sta ||
894 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
895 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400896 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200897 return TXRX_CONTINUE;
898
899 skb = skb_dequeue(&rx->sta->tx_filtered);
900 if (!skb) {
901 skb = skb_dequeue(&rx->sta->ps_tx_buf);
902 if (skb)
903 rx->local->total_ps_buffered--;
904 }
905 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
906 skb_queue_empty(&rx->sta->ps_tx_buf);
907
908 if (skb) {
909 struct ieee80211_hdr *hdr =
910 (struct ieee80211_hdr *) skb->data;
911
912 /* tell TX path to send one frame even though the STA may
913 * still remain is PS mode after this frame exchange */
914 rx->sta->pspoll = 1;
915
916#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700917 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
918 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200919 skb_queue_len(&rx->sta->ps_tx_buf));
920#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
921
922 /* Use MoreData flag to indicate whether there are more
923 * buffered frames for this STA */
924 if (no_pending_pkts) {
925 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
926 rx->sta->flags &= ~WLAN_STA_TIM;
927 } else
928 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
929
930 dev_queue_xmit(skb);
931
932 if (no_pending_pkts) {
933 if (rx->local->ops->set_tim)
934 rx->local->ops->set_tim(local_to_hw(rx->local),
935 rx->sta->aid, 0);
936 if (rx->sdata->bss)
937 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
938 }
939#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
940 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700941 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200942 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700943 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200944#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
945
946 }
947
948 /* Free PS Poll skb here instead of returning TXRX_DROP that would
949 * count as an dropped frame. */
950 dev_kfree_skb(rx->skb);
951
952 return TXRX_QUEUED;
953}
954
955static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200956ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
957{
958 u16 fc = rx->fc;
959 u8 *data = rx->skb->data;
960 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
961
962 if (!WLAN_FC_IS_QOS_DATA(fc))
963 return TXRX_CONTINUE;
964
965 /* remove the qos control field, update frame type and meta-data */
966 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
967 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
968 /* change frame type to non QOS */
969 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
970 hdr->frame_control = cpu_to_le16(fc);
971
972 return TXRX_CONTINUE;
973}
974
975static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200976ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
977{
978 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400979 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
Johannes Bergf9d540e2007-09-28 14:02:09 +0200980 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
981 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200982
983 if (unlikely(rx->sdata->ieee802_1x &&
984 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
985 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
986 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
987 !ieee80211_is_eapol(rx->skb))) {
988#ifdef CONFIG_MAC80211_DEBUG
989 struct ieee80211_hdr *hdr =
990 (struct ieee80211_hdr *) rx->skb->data;
Joe Perches0795af52007-10-03 17:59:30 -0700991 DECLARE_MAC_BUF(mac);
992 printk(KERN_DEBUG "%s: dropped frame from %s"
Johannes Berg571ecf62007-07-27 15:43:22 +0200993 " (unauthorized port)\n", rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700994 print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200995#endif /* CONFIG_MAC80211_DEBUG */
996 return TXRX_DROP;
997 }
998
999 return TXRX_CONTINUE;
1000}
1001
1002static ieee80211_txrx_result
1003ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
1004{
Johannes Berg3017b802007-08-28 17:01:53 -04001005 /*
Johannes Berg7848ba72007-09-14 11:10:25 -04001006 * Pass through unencrypted frames if the hardware has
1007 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -04001008 */
Johannes Berg7848ba72007-09-14 11:10:25 -04001009 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Johannes Berg571ecf62007-07-27 15:43:22 +02001010 return TXRX_CONTINUE;
1011
1012 /* Drop unencrypted frames if key is set. */
1013 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1014 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1015 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
1016 (rx->key || rx->sdata->drop_unencrypted) &&
1017 (rx->sdata->eapol == 0 ||
1018 !ieee80211_is_eapol(rx->skb)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001019 if (net_ratelimit())
1020 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1021 "encryption\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +02001022 return TXRX_DROP;
1023 }
1024 return TXRX_CONTINUE;
1025}
1026
1027static ieee80211_txrx_result
1028ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1029{
1030 struct net_device *dev = rx->dev;
1031 struct ieee80211_local *local = rx->local;
1032 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1033 u16 fc, hdrlen, ethertype;
1034 u8 *payload;
1035 u8 dst[ETH_ALEN];
1036 u8 src[ETH_ALEN];
1037 struct sk_buff *skb = rx->skb, *skb2;
1038 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001039 DECLARE_MAC_BUF(mac);
1040 DECLARE_MAC_BUF(mac2);
1041 DECLARE_MAC_BUF(mac3);
1042 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001043
1044 fc = rx->fc;
1045 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1046 return TXRX_CONTINUE;
1047
1048 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1049 return TXRX_DROP;
1050
1051 hdrlen = ieee80211_get_hdrlen(fc);
1052
1053 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1054 * header
1055 * IEEE 802.11 address fields:
1056 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1057 * 0 0 DA SA BSSID n/a
1058 * 0 1 DA BSSID SA n/a
1059 * 1 0 BSSID SA DA n/a
1060 * 1 1 RA TA DA SA
1061 */
1062
1063 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1064 case IEEE80211_FCTL_TODS:
1065 /* BSSID SA DA */
1066 memcpy(dst, hdr->addr3, ETH_ALEN);
1067 memcpy(src, hdr->addr2, ETH_ALEN);
1068
1069 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1070 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001071 if (net_ratelimit())
1072 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001073 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001074 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001075 print_mac(mac, hdr->addr1),
1076 print_mac(mac2, hdr->addr2),
1077 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001078 return TXRX_DROP;
1079 }
1080 break;
1081 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1082 /* RA TA DA SA */
1083 memcpy(dst, hdr->addr3, ETH_ALEN);
1084 memcpy(src, hdr->addr4, ETH_ALEN);
1085
1086 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001087 if (net_ratelimit())
1088 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001089 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001090 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001091 print_mac(mac, hdr->addr1),
1092 print_mac(mac2, hdr->addr2),
1093 print_mac(mac3, hdr->addr3),
1094 print_mac(mac4, hdr->addr4));
Johannes Berg571ecf62007-07-27 15:43:22 +02001095 return TXRX_DROP;
1096 }
1097 break;
1098 case IEEE80211_FCTL_FROMDS:
1099 /* DA BSSID SA */
1100 memcpy(dst, hdr->addr1, ETH_ALEN);
1101 memcpy(src, hdr->addr3, ETH_ALEN);
1102
John W. Linvilleb3316152007-08-28 17:01:55 -04001103 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1104 (is_multicast_ether_addr(dst) &&
1105 !compare_ether_addr(src, dev->dev_addr)))
Johannes Berg571ecf62007-07-27 15:43:22 +02001106 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +02001107 break;
1108 case 0:
1109 /* DA SA BSSID */
1110 memcpy(dst, hdr->addr1, ETH_ALEN);
1111 memcpy(src, hdr->addr2, ETH_ALEN);
1112
1113 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1114 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001115 printk(KERN_DEBUG "%s: dropped IBSS frame "
1116 "(DA=%s SA=%s BSSID=%s)\n",
1117 dev->name,
1118 print_mac(mac, hdr->addr1),
1119 print_mac(mac2, hdr->addr2),
1120 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001121 }
1122 return TXRX_DROP;
1123 }
1124 break;
1125 }
1126
1127 payload = skb->data + hdrlen;
1128
1129 if (unlikely(skb->len - hdrlen < 8)) {
1130 if (net_ratelimit()) {
1131 printk(KERN_DEBUG "%s: RX too short data frame "
1132 "payload\n", dev->name);
1133 }
1134 return TXRX_DROP;
1135 }
1136
1137 ethertype = (payload[6] << 8) | payload[7];
1138
1139 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1140 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1141 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1142 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1143 * replace EtherType */
1144 skb_pull(skb, hdrlen + 6);
1145 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1146 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1147 } else {
1148 struct ethhdr *ehdr;
1149 __be16 len;
1150 skb_pull(skb, hdrlen);
1151 len = htons(skb->len);
1152 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1153 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1154 memcpy(ehdr->h_source, src, ETH_ALEN);
1155 ehdr->h_proto = len;
1156 }
1157 skb->dev = dev;
1158
1159 skb2 = NULL;
1160
Stephen Hemminger68aae112007-08-24 11:29:34 -07001161 dev->stats.rx_packets++;
1162 dev->stats.rx_bytes += skb->len;
Johannes Berg571ecf62007-07-27 15:43:22 +02001163
1164 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
Jiri Slabybadffb72007-08-28 17:01:54 -04001165 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1166 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001167 if (is_multicast_ether_addr(skb->data)) {
1168 /* send multicast frames both to higher layers in
1169 * local net stack and back to the wireless media */
1170 skb2 = skb_copy(skb, GFP_ATOMIC);
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001171 if (!skb2 && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001172 printk(KERN_DEBUG "%s: failed to clone "
1173 "multicast frame\n", dev->name);
1174 } else {
1175 struct sta_info *dsta;
1176 dsta = sta_info_get(local, skb->data);
1177 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001178 if (net_ratelimit())
1179 printk(KERN_DEBUG "Station with null "
1180 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001181 } else if (dsta && dsta->dev == dev) {
1182 /* Destination station is associated to this
1183 * AP, so send the frame directly to it and
1184 * do not pass the frame to local net stack.
1185 */
1186 skb2 = skb;
1187 skb = NULL;
1188 }
1189 if (dsta)
1190 sta_info_put(dsta);
1191 }
1192 }
1193
1194 if (skb) {
1195 /* deliver to local stack */
1196 skb->protocol = eth_type_trans(skb, dev);
1197 memset(skb->cb, 0, sizeof(skb->cb));
1198 netif_rx(skb);
1199 }
1200
1201 if (skb2) {
1202 /* send to wireless media */
1203 skb2->protocol = __constant_htons(ETH_P_802_3);
1204 skb_set_network_header(skb2, 0);
1205 skb_set_mac_header(skb2, 0);
1206 dev_queue_xmit(skb2);
1207 }
1208
1209 return TXRX_QUEUED;
1210}
1211
1212static ieee80211_txrx_result
1213ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1214{
1215 struct ieee80211_sub_if_data *sdata;
1216
Jiri Slabybadffb72007-08-28 17:01:54 -04001217 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001218 return TXRX_DROP;
1219
1220 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1221 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1222 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergf9d540e2007-09-28 14:02:09 +02001223 !rx->local->user_space_mlme)
Johannes Berg571ecf62007-07-27 15:43:22 +02001224 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001225 else
1226 return TXRX_DROP;
1227
Johannes Berg571ecf62007-07-27 15:43:22 +02001228 return TXRX_QUEUED;
1229}
1230
1231static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1232 struct ieee80211_local *local,
1233 ieee80211_rx_handler *handlers,
1234 struct ieee80211_txrx_data *rx,
1235 struct sta_info *sta)
1236{
1237 ieee80211_rx_handler *handler;
1238 ieee80211_txrx_result res = TXRX_DROP;
1239
1240 for (handler = handlers; *handler != NULL; handler++) {
1241 res = (*handler)(rx);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001242
1243 switch (res) {
1244 case TXRX_CONTINUE:
1245 continue;
1246 case TXRX_DROP:
1247 I802_DEBUG_INC(local->rx_handlers_drop);
1248 if (sta)
1249 sta->rx_dropped++;
1250 break;
1251 case TXRX_QUEUED:
1252 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001253 break;
1254 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001255 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001256 }
1257
Johannes Berg8e6f00322007-07-27 15:43:22 +02001258 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001259 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001260 return res;
1261}
1262
1263static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1264 ieee80211_rx_handler *handlers,
1265 struct ieee80211_txrx_data *rx,
1266 struct sta_info *sta)
1267{
1268 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1269 TXRX_CONTINUE)
1270 dev_kfree_skb(rx->skb);
1271}
1272
1273static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1274 struct ieee80211_hdr *hdr,
1275 struct sta_info *sta,
1276 struct ieee80211_txrx_data *rx)
1277{
1278 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001279 DECLARE_MAC_BUF(mac);
1280 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001281
1282 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1283 if (rx->skb->len >= hdrlen + 4)
1284 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1285 else
1286 keyidx = -1;
1287
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001288 if (net_ratelimit())
1289 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001290 "failure from %s to %s keyidx=%d\n",
1291 dev->name, print_mac(mac, hdr->addr2),
1292 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001293
1294 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001295 /*
1296 * Some hardware seem to generate incorrect Michael MIC
1297 * reports; ignore them to avoid triggering countermeasures.
1298 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001299 if (net_ratelimit())
1300 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001301 "error for unknown address %s\n",
1302 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001303 goto ignore;
1304 }
1305
1306 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001307 if (net_ratelimit())
1308 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001309 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001310 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001311 goto ignore;
1312 }
1313
Johannes Berg7848ba72007-09-14 11:10:25 -04001314 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001315 /*
1316 * APs with pairwise keys should never receive Michael MIC
1317 * errors for non-zero keyidx because these are reserved for
1318 * group keys and only the AP is sending real multicast
1319 * frames in the BSS.
1320 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001321 if (net_ratelimit())
1322 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1323 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001324 " (src %s)\n", dev->name, keyidx,
1325 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001326 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001327 }
1328
1329 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1330 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1331 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001332 if (net_ratelimit())
1333 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1334 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001335 "(fc=0x%04x) (src %s)\n",
1336 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001337 goto ignore;
1338 }
1339
Johannes Bergeb063c12007-08-28 17:01:53 -04001340 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001341 ignore:
1342 dev_kfree_skb(rx->skb);
1343 rx->skb = NULL;
1344}
1345
1346ieee80211_rx_handler ieee80211_rx_handlers[] =
1347{
1348 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001349 ieee80211_rx_h_passive_scan,
1350 ieee80211_rx_h_check,
Johannes Berg570bd532007-07-27 15:43:22 +02001351 ieee80211_rx_h_load_key,
Johannes Berg571ecf62007-07-27 15:43:22 +02001352 ieee80211_rx_h_wep_weak_iv_detection,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001353 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001354 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001355 ieee80211_rx_h_defragment,
1356 ieee80211_rx_h_ps_poll,
1357 ieee80211_rx_h_michael_mic_verify,
1358 /* this must be after decryption - so header is counted in MPDU mic
1359 * must be before pae and data, so QOS_DATA format frames
1360 * are not passed to user space by these functions
1361 */
1362 ieee80211_rx_h_remove_qos_control,
1363 ieee80211_rx_h_802_1x_pae,
1364 ieee80211_rx_h_drop_unencrypted,
1365 ieee80211_rx_h_data,
1366 ieee80211_rx_h_mgmt,
1367 NULL
1368};
1369
1370/* main receive path */
1371
Johannes Berg23a24de2007-07-27 15:43:22 +02001372static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1373 u8 *bssid, struct ieee80211_txrx_data *rx,
1374 struct ieee80211_hdr *hdr)
1375{
1376 int multicast = is_multicast_ether_addr(hdr->addr1);
1377
1378 switch (sdata->type) {
1379 case IEEE80211_IF_TYPE_STA:
1380 if (!bssid)
1381 return 0;
1382 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001383 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001384 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001385 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001386 } else if (!multicast &&
1387 compare_ether_addr(sdata->dev->dev_addr,
1388 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001389 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001390 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001391 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001392 }
1393 break;
1394 case IEEE80211_IF_TYPE_IBSS:
1395 if (!bssid)
1396 return 0;
1397 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001398 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001399 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001400 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001401 } else if (!multicast &&
1402 compare_ether_addr(sdata->dev->dev_addr,
1403 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001404 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001405 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001406 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001407 } else if (!rx->sta)
1408 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1409 bssid, hdr->addr2);
1410 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001411 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001412 case IEEE80211_IF_TYPE_AP:
1413 if (!bssid) {
1414 if (compare_ether_addr(sdata->dev->dev_addr,
1415 hdr->addr1))
1416 return 0;
1417 } else if (!ieee80211_bssid_match(bssid,
1418 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001419 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001420 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001421 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001422 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001423 if (sdata->dev == sdata->local->mdev &&
1424 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001425 /* do not receive anything via
1426 * master device when not scanning */
1427 return 0;
1428 break;
1429 case IEEE80211_IF_TYPE_WDS:
1430 if (bssid ||
1431 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1432 return 0;
1433 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1434 return 0;
1435 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001436 case IEEE80211_IF_TYPE_MNTR:
1437 /* take everything */
1438 break;
Johannes Berga2897552007-09-28 14:01:25 +02001439 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001440 /* should never get here */
1441 WARN_ON(1);
1442 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001443 }
1444
1445 return 1;
1446}
1447
Johannes Berg571ecf62007-07-27 15:43:22 +02001448/*
1449 * This is the receive path handler. It is called by a low level driver when an
1450 * 802.11 MPDU is received from the hardware.
1451 */
1452void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1453 struct ieee80211_rx_status *status)
1454{
1455 struct ieee80211_local *local = hw_to_local(hw);
1456 struct ieee80211_sub_if_data *sdata;
1457 struct sta_info *sta;
1458 struct ieee80211_hdr *hdr;
1459 struct ieee80211_txrx_data rx;
1460 u16 type;
Johannes Bergb2e77712007-09-26 15:19:39 +02001461 int prepres;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001462 struct ieee80211_sub_if_data *prev = NULL;
1463 struct sk_buff *skb_new;
1464 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001465
Johannes Bergd4e46a32007-09-14 11:10:24 -04001466 /*
Johannes Berg79010422007-09-18 17:29:21 -04001467 * key references and virtual interfaces are protected using RCU
1468 * and this requires that we are in a read-side RCU section during
1469 * receive processing
Johannes Bergd4e46a32007-09-14 11:10:24 -04001470 */
1471 rcu_read_lock();
1472
Johannes Bergb2e77712007-09-26 15:19:39 +02001473 /*
1474 * Frames with failed FCS/PLCP checksum are not returned,
1475 * all other frames are returned without radiotap header
1476 * if it was previously present.
1477 * Also, frames with less than 16 bytes are dropped.
1478 */
1479 skb = ieee80211_rx_monitor(local, skb, status);
1480 if (!skb) {
1481 rcu_read_unlock();
1482 return;
1483 }
1484
Johannes Berg571ecf62007-07-27 15:43:22 +02001485 hdr = (struct ieee80211_hdr *) skb->data;
1486 memset(&rx, 0, sizeof(rx));
1487 rx.skb = skb;
1488 rx.local = local;
1489
1490 rx.u.rx.status = status;
Johannes Bergb2e77712007-09-26 15:19:39 +02001491 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001492 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001493
Johannes Bergb2e77712007-09-26 15:19:39 +02001494 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001495 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001496
Johannes Bergb2e77712007-09-26 15:19:39 +02001497 sta = rx.sta = sta_info_get(local, hdr->addr2);
1498 if (sta) {
1499 rx.dev = rx.sta->dev;
1500 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1501 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001502
Johannes Berg571ecf62007-07-27 15:43:22 +02001503 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1504 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1505 goto end;
1506 }
1507
1508 if (unlikely(local->sta_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001509 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001510
1511 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1512 sta) != TXRX_CONTINUE)
1513 goto end;
1514 skb = rx.skb;
1515
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001516 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001517 !atomic_read(&local->iff_promiscs) &&
1518 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001519 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001520 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001521 rx.sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001522 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001523 rcu_read_unlock();
Johannes Berg8e6f00322007-07-27 15:43:22 +02001524 return;
1525 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001526
Johannes Bergb2e77712007-09-26 15:19:39 +02001527 bssid = ieee80211_get_bssid(hdr, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001528
Johannes Berg79010422007-09-18 17:29:21 -04001529 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001530 if (!netif_running(sdata->dev))
1531 continue;
1532
Johannes Bergb2e77712007-09-26 15:19:39 +02001533 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1534 continue;
1535
1536 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001537 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1538 /* prepare_for_handlers can change sta */
1539 sta = rx.sta;
1540
1541 if (!prepres)
1542 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001543
Johannes Berg340e11f2007-07-27 15:43:22 +02001544 /*
1545 * frame is destined for this interface, but if it's not
1546 * also for the previous one we handle that after the
1547 * loop to avoid copying the SKB once too much
1548 */
1549
1550 if (!prev) {
1551 prev = sdata;
1552 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001553 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001554
1555 /*
1556 * frame was destined for the previous interface
1557 * so invoke RX handlers for it
1558 */
1559
1560 skb_new = skb_copy(skb, GFP_ATOMIC);
1561 if (!skb_new) {
1562 if (net_ratelimit())
1563 printk(KERN_DEBUG "%s: failed to copy "
1564 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001565 wiphy_name(local->hw.wiphy),
1566 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001567 continue;
1568 }
1569 rx.skb = skb_new;
1570 rx.dev = prev->dev;
1571 rx.sdata = prev;
1572 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1573 &rx, sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001574 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001575 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001576 if (prev) {
1577 rx.skb = skb;
1578 rx.dev = prev->dev;
1579 rx.sdata = prev;
1580 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1581 &rx, sta);
1582 } else
1583 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001584
Johannes Berg8e6f00322007-07-27 15:43:22 +02001585 end:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001586 rcu_read_unlock();
1587
Johannes Berg571ecf62007-07-27 15:43:22 +02001588 if (sta)
1589 sta_info_put(sta);
1590}
1591EXPORT_SYMBOL(__ieee80211_rx);
1592
1593/* This is a version of the rx handler that can be called from hard irq
1594 * context. Post the skb on the queue and schedule the tasklet */
1595void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1596 struct ieee80211_rx_status *status)
1597{
1598 struct ieee80211_local *local = hw_to_local(hw);
1599
1600 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1601
1602 skb->dev = local->mdev;
1603 /* copy status into skb->cb for use by tasklet */
1604 memcpy(skb->cb, status, sizeof(*status));
1605 skb->pkt_type = IEEE80211_RX_MSG;
1606 skb_queue_tail(&local->skb_queue, skb);
1607 tasklet_schedule(&local->tasklet);
1608}
1609EXPORT_SYMBOL(ieee80211_rx_irqsafe);