blob: 4525d7332c88205e2c455cac25aa16639fa93294 [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 Berg52865df2007-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;
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +0200246 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
247 rx->u.rx.amsdu_frame = 1;
248 else
249 rx->u.rx.amsdu_frame = 0;
Johannes Berg6e0d1142007-07-27 15:43:22 +0200250 } else {
251 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
252 /* Separate TID for management frames */
253 tid = NUM_RX_DATA_QUEUES - 1;
254 } else {
255 /* no qos control present */
256 tid = 0; /* 802.1d - Best Effort */
257 }
258 }
Johannes Berg52865df2007-07-27 15:43:22 +0200259
Johannes Berg6e0d1142007-07-27 15:43:22 +0200260 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865df2007-07-27 15:43:22 +0200261 /* only a debug counter, sta might not be assigned properly yet */
262 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +0200263 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +0200264
265 rx->u.rx.queue = tid;
266 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
267 * For now, set skb->priority to 0 for other cases. */
268 rx->skb->priority = (tid > 7) ? 0 : tid;
269
270 return TXRX_CONTINUE;
271}
272
273static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200274ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
275{
276 struct ieee80211_local *local = rx->local;
277 struct sk_buff *skb = rx->skb;
278 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
279 u32 load = 0, hdrtime;
280 struct ieee80211_rate *rate;
281 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
282 int i;
283
284 /* Estimate total channel use caused by this frame */
285
286 if (unlikely(mode->num_rates < 0))
287 return TXRX_CONTINUE;
288
289 rate = &mode->rates[0];
290 for (i = 0; i < mode->num_rates; i++) {
291 if (mode->rates[i].val == rx->u.rx.status->rate) {
292 rate = &mode->rates[i];
293 break;
294 }
295 }
296
297 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
298 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
299
300 if (mode->mode == MODE_IEEE80211A ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200301 (mode->mode == MODE_IEEE80211G &&
302 rate->flags & IEEE80211_RATE_ERP))
303 hdrtime = CHAN_UTIL_HDR_SHORT;
304 else
305 hdrtime = CHAN_UTIL_HDR_LONG;
306
307 load = hdrtime;
308 if (!is_multicast_ether_addr(hdr->addr1))
309 load += hdrtime;
310
311 load += skb->len * rate->rate_inv;
312
313 /* Divide channel_use by 8 to avoid wrapping around the counter */
314 load >>= CHAN_UTIL_SHIFT;
315 local->channel_use_raw += load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200316 rx->u.rx.load = load;
317
318 return TXRX_CONTINUE;
319}
320
321ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
322{
323 ieee80211_rx_h_parse_qos,
324 ieee80211_rx_h_load_stats,
325 NULL
326};
327
328/* rx handlers */
329
330static ieee80211_txrx_result
331ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
332{
Johannes Berg52865df2007-07-27 15:43:22 +0200333 if (rx->sta)
334 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200335 rx->sdata->channel_use_raw += rx->u.rx.load;
336 return TXRX_CONTINUE;
337}
338
Johannes Berg571ecf62007-07-27 15:43:22 +0200339static ieee80211_txrx_result
340ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
341{
342 struct ieee80211_local *local = rx->local;
343 struct sk_buff *skb = rx->skb;
344
Zhu Yiece8edd2007-11-22 10:53:21 +0800345 if (unlikely(local->sta_hw_scanning))
346 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
347
348 if (unlikely(local->sta_sw_scanning)) {
349 /* drop all the other packets during a software scan anyway */
350 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
351 != TXRX_QUEUED)
352 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +0200353 return TXRX_QUEUED;
354 }
355
Jiri Slabybadffb72007-08-28 17:01:54 -0400356 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200357 /* scanning finished during invoking of handlers */
358 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
359 return TXRX_DROP;
360 }
361
362 return TXRX_CONTINUE;
363}
364
365static ieee80211_txrx_result
366ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
367{
368 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200369 hdr = (struct ieee80211_hdr *) rx->skb->data;
370
371 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
372 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
373 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
374 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
375 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400376 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200377 rx->local->dot11FrameDuplicateCount++;
378 rx->sta->num_duplicates++;
379 }
380 return TXRX_DROP;
381 } else
382 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
383 }
384
Johannes Berg571ecf62007-07-27 15:43:22 +0200385 if (unlikely(rx->skb->len < 16)) {
386 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
387 return TXRX_DROP;
388 }
389
Jiri Slabybadffb72007-08-28 17:01:54 -0400390 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200391 rx->skb->pkt_type = PACKET_OTHERHOST;
392 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
393 rx->skb->pkt_type = PACKET_HOST;
394 else if (is_multicast_ether_addr(hdr->addr1)) {
395 if (is_broadcast_ether_addr(hdr->addr1))
396 rx->skb->pkt_type = PACKET_BROADCAST;
397 else
398 rx->skb->pkt_type = PACKET_MULTICAST;
399 } else
400 rx->skb->pkt_type = PACKET_OTHERHOST;
401
402 /* Drop disallowed frame classes based on STA auth/assoc state;
403 * IEEE 802.11, Chap 5.5.
404 *
405 * 80211.o does filtering only based on association state, i.e., it
406 * drops Class 3 frames from not associated stations. hostapd sends
407 * deauth/disassoc frames when needed. In addition, hostapd is
408 * responsible for filtering on both auth and assoc states.
409 */
410 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
411 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
412 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
413 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
414 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
415 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
416 !(rx->fc & IEEE80211_FCTL_TODS) &&
417 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400418 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200419 /* Drop IBSS frames and frames for other hosts
420 * silently. */
421 return TXRX_DROP;
422 }
423
Johannes Bergf9d540e2007-09-28 14:02:09 +0200424 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +0200425 }
426
Johannes Berg570bd532007-07-27 15:43:22 +0200427 return TXRX_CONTINUE;
428}
429
430
431static ieee80211_txrx_result
Johannes Berg1990af82007-09-26 17:53:15 +0200432ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
Johannes Berg570bd532007-07-27 15:43:22 +0200433{
434 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400435 int keyidx;
436 int hdrlen;
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200437 ieee80211_txrx_result result = TXRX_DROP;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400438 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200439
Johannes Berg3017b802007-08-28 17:01:53 -0400440 /*
441 * Key selection 101
442 *
443 * There are three types of keys:
444 * - GTK (group keys)
445 * - PTK (pairwise keys)
446 * - STK (station-to-station pairwise keys)
447 *
448 * When selecting a key, we have to distinguish between multicast
449 * (including broadcast) and unicast frames, the latter can only
450 * use PTKs and STKs while the former always use GTKs. Unless, of
451 * course, actual WEP keys ("pre-RSNA") are used, then unicast
452 * frames can also use key indizes like GTKs. Hence, if we don't
453 * have a PTK/STK we check the key index for a WEP key.
454 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400455 * Note that in a regular BSS, multicast frames are sent by the
456 * AP only, associated stations unicast the frame to the AP first
457 * which then multicasts it on their behalf.
458 *
Johannes Berg3017b802007-08-28 17:01:53 -0400459 * There is also a slight problem in IBSS mode: GTKs are negotiated
460 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400461 * The spec seems to expect that one negotiates the same key with
462 * every station but there's no such requirement; VLANs could be
463 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400464 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200465
Johannes Berg3017b802007-08-28 17:01:53 -0400466 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
467 return TXRX_CONTINUE;
468
469 /*
Johannes Berg1990af82007-09-26 17:53:15 +0200470 * No point in finding a key and decrypting if the frame is neither
Johannes Berg3017b802007-08-28 17:01:53 -0400471 * addressed to us nor a multicast frame.
472 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400473 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400474 return TXRX_CONTINUE;
475
Johannes Bergd4e46a32007-09-14 11:10:24 -0400476 if (rx->sta)
477 stakey = rcu_dereference(rx->sta->key);
478
479 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
480 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200481 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400482 /*
483 * The device doesn't give us the IV so we won't be
484 * able to look up the key. That's ok though, we
485 * don't need to decrypt the frame, we just won't
486 * be able to keep statistics accurate.
487 * Except for key threshold notifications, should
488 * we somehow allow the driver to tell us which key
489 * the hardware used if this flag is set?
490 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400491 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
492 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400493 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200494
Johannes Berg3017b802007-08-28 17:01:53 -0400495 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200496
Johannes Berg3017b802007-08-28 17:01:53 -0400497 if (rx->skb->len < 8 + hdrlen)
498 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200499
Johannes Berg3017b802007-08-28 17:01:53 -0400500 /*
501 * no need to call ieee80211_wep_get_keyidx,
502 * it verifies a bunch of things we've done already
503 */
504 keyidx = rx->skb->data[hdrlen + 3] >> 6;
505
Johannes Bergd4e46a32007-09-14 11:10:24 -0400506 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400507
508 /*
509 * RSNA-protected unicast frames should always be sent with
510 * pairwise or station-to-station keys, but for WEP we allow
511 * using a key index as well.
512 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400513 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400514 !is_multicast_ether_addr(hdr->addr1))
515 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200516 }
517
Johannes Berg3017b802007-08-28 17:01:53 -0400518 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200519 rx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400520 /* TODO: add threshold stuff again */
Johannes Berg1990af82007-09-26 17:53:15 +0200521 } else {
John W. Linville7f3ad892007-11-06 17:12:31 -0500522#ifdef CONFIG_MAC80211_DEBUG
Johannes Berg70f08762007-09-26 17:53:14 +0200523 if (net_ratelimit())
524 printk(KERN_DEBUG "%s: RX protected frame,"
525 " but have no key\n", rx->dev->name);
John W. Linville7f3ad892007-11-06 17:12:31 -0500526#endif /* CONFIG_MAC80211_DEBUG */
Johannes Berg70f08762007-09-26 17:53:14 +0200527 return TXRX_DROP;
528 }
529
Johannes Berg1990af82007-09-26 17:53:15 +0200530 /* Check for weak IVs if possible */
531 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
532 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
533 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
534 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
535 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
536 rx->sta->wep_weak_iv_count++;
537
Johannes Berg70f08762007-09-26 17:53:14 +0200538 switch (rx->key->conf.alg) {
539 case ALG_WEP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200540 result = ieee80211_crypto_wep_decrypt(rx);
541 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200542 case ALG_TKIP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200543 result = ieee80211_crypto_tkip_decrypt(rx);
544 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200545 case ALG_CCMP:
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200546 result = ieee80211_crypto_ccmp_decrypt(rx);
547 break;
Johannes Berg70f08762007-09-26 17:53:14 +0200548 }
549
Mattias Nisslere2f036d2007-10-07 16:35:31 +0200550 /* either the frame has been decrypted or will be dropped */
551 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
552
553 return result;
Johannes Berg70f08762007-09-26 17:53:14 +0200554}
555
Johannes Berg571ecf62007-07-27 15:43:22 +0200556static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
557{
558 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700559 DECLARE_MAC_BUF(mac);
560
Johannes Berg571ecf62007-07-27 15:43:22 +0200561 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
562
563 if (sdata->bss)
564 atomic_inc(&sdata->bss->num_sta_ps);
565 sta->flags |= WLAN_STA_PS;
566 sta->pspoll = 0;
567#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700568 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
569 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200570#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
571}
572
573static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
574{
575 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
576 struct sk_buff *skb;
577 int sent = 0;
578 struct ieee80211_sub_if_data *sdata;
579 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700580 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200581
582 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
583 if (sdata->bss)
584 atomic_dec(&sdata->bss->num_sta_ps);
585 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
586 sta->pspoll = 0;
587 if (!skb_queue_empty(&sta->ps_tx_buf)) {
588 if (local->ops->set_tim)
589 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
590 if (sdata->bss)
591 bss_tim_clear(local, sdata->bss, sta->aid);
592 }
593#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700594 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
595 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200596#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
597 /* Send all buffered frames to the station */
598 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
599 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
600 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400601 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200602 dev_queue_xmit(skb);
603 }
604 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
605 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
606 local->total_ps_buffered--;
607 sent++;
608#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700609 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200610 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700611 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200612#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400613 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200614 dev_queue_xmit(skb);
615 }
616
617 return sent;
618}
619
620static ieee80211_txrx_result
621ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
622{
623 struct sta_info *sta = rx->sta;
624 struct net_device *dev = rx->dev;
625 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
626
627 if (!sta)
628 return TXRX_CONTINUE;
629
630 /* Update last_rx only for IBSS packets which are for the current
631 * BSSID to avoid keeping the current IBSS network alive in cases where
632 * other STAs are using different BSSID. */
633 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
634 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
635 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
636 sta->last_rx = jiffies;
637 } else
638 if (!is_multicast_ether_addr(hdr->addr1) ||
639 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
640 /* Update last_rx only for unicast frames in order to prevent
641 * the Probe Request frames (the only broadcast frames from a
642 * STA in infrastructure mode) from keeping a connection alive.
643 */
644 sta->last_rx = jiffies;
645 }
646
Jiri Slabybadffb72007-08-28 17:01:54 -0400647 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200648 return TXRX_CONTINUE;
649
650 sta->rx_fragments++;
651 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400652 sta->last_rssi = rx->u.rx.status->ssi;
653 sta->last_signal = rx->u.rx.status->signal;
654 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200655
656 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
657 /* Change STA power saving mode only in the end of a frame
658 * exchange sequence */
659 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
660 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
661 else if (!(sta->flags & WLAN_STA_PS) &&
662 (rx->fc & IEEE80211_FCTL_PM))
663 ap_sta_ps_start(dev, sta);
664 }
665
666 /* Drop data::nullfunc frames silently, since they are used only to
667 * control station power saving mode. */
668 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
669 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
670 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
671 /* Update counter and free packet here to avoid counting this
672 * as a dropped packed. */
673 sta->rx_packets++;
674 dev_kfree_skb(rx->skb);
675 return TXRX_QUEUED;
676 }
677
678 return TXRX_CONTINUE;
679} /* ieee80211_rx_h_sta_process */
680
Johannes Berg571ecf62007-07-27 15:43:22 +0200681static inline struct ieee80211_fragment_entry *
682ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
683 unsigned int frag, unsigned int seq, int rx_queue,
684 struct sk_buff **skb)
685{
686 struct ieee80211_fragment_entry *entry;
687 int idx;
688
689 idx = sdata->fragment_next;
690 entry = &sdata->fragments[sdata->fragment_next++];
691 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
692 sdata->fragment_next = 0;
693
694 if (!skb_queue_empty(&entry->skb_list)) {
695#ifdef CONFIG_MAC80211_DEBUG
696 struct ieee80211_hdr *hdr =
697 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700698 DECLARE_MAC_BUF(mac);
699 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200700 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
701 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700702 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200703 sdata->dev->name, idx,
704 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700705 entry->last_frag, print_mac(mac, hdr->addr1),
706 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200707#endif /* CONFIG_MAC80211_DEBUG */
708 __skb_queue_purge(&entry->skb_list);
709 }
710
711 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
712 *skb = NULL;
713 entry->first_frag_time = jiffies;
714 entry->seq = seq;
715 entry->rx_queue = rx_queue;
716 entry->last_frag = frag;
717 entry->ccmp = 0;
718 entry->extra_len = 0;
719
720 return entry;
721}
722
723static inline struct ieee80211_fragment_entry *
724ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
725 u16 fc, unsigned int frag, unsigned int seq,
726 int rx_queue, struct ieee80211_hdr *hdr)
727{
728 struct ieee80211_fragment_entry *entry;
729 int i, idx;
730
731 idx = sdata->fragment_next;
732 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
733 struct ieee80211_hdr *f_hdr;
734 u16 f_fc;
735
736 idx--;
737 if (idx < 0)
738 idx = IEEE80211_FRAGMENT_MAX - 1;
739
740 entry = &sdata->fragments[idx];
741 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
742 entry->rx_queue != rx_queue ||
743 entry->last_frag + 1 != frag)
744 continue;
745
746 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
747 f_fc = le16_to_cpu(f_hdr->frame_control);
748
749 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
750 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
751 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
752 continue;
753
754 if (entry->first_frag_time + 2 * HZ < jiffies) {
755 __skb_queue_purge(&entry->skb_list);
756 continue;
757 }
758 return entry;
759 }
760
761 return NULL;
762}
763
764static ieee80211_txrx_result
765ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
766{
767 struct ieee80211_hdr *hdr;
768 u16 sc;
769 unsigned int frag, seq;
770 struct ieee80211_fragment_entry *entry;
771 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700772 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200773
774 hdr = (struct ieee80211_hdr *) rx->skb->data;
775 sc = le16_to_cpu(hdr->seq_ctrl);
776 frag = sc & IEEE80211_SCTL_FRAG;
777
778 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
779 (rx->skb)->len < 24 ||
780 is_multicast_ether_addr(hdr->addr1))) {
781 /* not fragmented */
782 goto out;
783 }
784 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
785
786 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
787
788 if (frag == 0) {
789 /* This is the first fragment of a new frame. */
790 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
791 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400792 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200793 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
794 /* Store CCMP PN so that we can verify that the next
795 * fragment has a sequential PN value. */
796 entry->ccmp = 1;
797 memcpy(entry->last_pn,
798 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
799 CCMP_PN_LEN);
800 }
801 return TXRX_QUEUED;
802 }
803
804 /* This is a fragment for a frame that should already be pending in
805 * fragment cache. Add this fragment to the end of the pending entry.
806 */
807 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
808 rx->u.rx.queue, hdr);
809 if (!entry) {
810 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
811 return TXRX_DROP;
812 }
813
814 /* Verify that MPDUs within one MSDU have sequential PN values.
815 * (IEEE 802.11i, 8.3.3.4.5) */
816 if (entry->ccmp) {
817 int i;
818 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400819 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200820 return TXRX_DROP;
821 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
822 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
823 pn[i]++;
824 if (pn[i])
825 break;
826 }
827 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
828 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400829 if (net_ratelimit())
830 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700831 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400832 " PN=%02x%02x%02x%02x%02x%02x "
833 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700834 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400835 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
836 rpn[5], pn[0], pn[1], pn[2], pn[3],
837 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200838 return TXRX_DROP;
839 }
840 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
841 }
842
843 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
844 __skb_queue_tail(&entry->skb_list, rx->skb);
845 entry->last_frag = frag;
846 entry->extra_len += rx->skb->len;
847 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
848 rx->skb = NULL;
849 return TXRX_QUEUED;
850 }
851
852 rx->skb = __skb_dequeue(&entry->skb_list);
853 if (skb_tailroom(rx->skb) < entry->extra_len) {
854 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
855 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
856 GFP_ATOMIC))) {
857 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
858 __skb_queue_purge(&entry->skb_list);
859 return TXRX_DROP;
860 }
861 }
862 while ((skb = __skb_dequeue(&entry->skb_list))) {
863 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
864 dev_kfree_skb(skb);
865 }
866
867 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400868 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200869
870 out:
871 if (rx->sta)
872 rx->sta->rx_packets++;
873 if (is_multicast_ether_addr(hdr->addr1))
874 rx->local->dot11MulticastReceivedFrameCount++;
875 else
876 ieee80211_led_rx(rx->local);
877 return TXRX_CONTINUE;
878}
879
880static ieee80211_txrx_result
881ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
882{
883 struct sk_buff *skb;
884 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700885 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200886
887 if (likely(!rx->sta ||
888 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
889 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400890 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200891 return TXRX_CONTINUE;
892
893 skb = skb_dequeue(&rx->sta->tx_filtered);
894 if (!skb) {
895 skb = skb_dequeue(&rx->sta->ps_tx_buf);
896 if (skb)
897 rx->local->total_ps_buffered--;
898 }
899 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
900 skb_queue_empty(&rx->sta->ps_tx_buf);
901
902 if (skb) {
903 struct ieee80211_hdr *hdr =
904 (struct ieee80211_hdr *) skb->data;
905
906 /* tell TX path to send one frame even though the STA may
907 * still remain is PS mode after this frame exchange */
908 rx->sta->pspoll = 1;
909
910#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700911 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
912 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200913 skb_queue_len(&rx->sta->ps_tx_buf));
914#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
915
916 /* Use MoreData flag to indicate whether there are more
917 * buffered frames for this STA */
918 if (no_pending_pkts) {
919 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
920 rx->sta->flags &= ~WLAN_STA_TIM;
921 } else
922 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
923
924 dev_queue_xmit(skb);
925
926 if (no_pending_pkts) {
927 if (rx->local->ops->set_tim)
928 rx->local->ops->set_tim(local_to_hw(rx->local),
929 rx->sta->aid, 0);
930 if (rx->sdata->bss)
931 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
932 }
933#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
934 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700935 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200936 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700937 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200938#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
939
940 }
941
942 /* Free PS Poll skb here instead of returning TXRX_DROP that would
943 * count as an dropped frame. */
944 dev_kfree_skb(rx->skb);
945
946 return TXRX_QUEUED;
947}
948
949static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200950ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
951{
952 u16 fc = rx->fc;
953 u8 *data = rx->skb->data;
954 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
955
956 if (!WLAN_FC_IS_QOS_DATA(fc))
957 return TXRX_CONTINUE;
958
959 /* remove the qos control field, update frame type and meta-data */
960 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
961 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
962 /* change frame type to non QOS */
963 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
964 hdr->frame_control = cpu_to_le16(fc);
965
966 return TXRX_CONTINUE;
967}
968
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200969static int
970ieee80211_drop_802_1x_pae(struct ieee80211_txrx_data *rx, int hdrlen)
Johannes Berg571ecf62007-07-27 15:43:22 +0200971{
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200972 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb, hdrlen) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400973 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
Johannes Bergf9d540e2007-09-28 14:02:09 +0200974 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200975 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +0200976
977 if (unlikely(rx->sdata->ieee802_1x &&
978 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
979 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
980 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200981 !ieee80211_is_eapol(rx->skb, hdrlen))) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200982#ifdef CONFIG_MAC80211_DEBUG
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200983 printk(KERN_DEBUG "%s: dropped frame "
984 "(unauthorized port)\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200985#endif /* CONFIG_MAC80211_DEBUG */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200986 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +0200987 }
988
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200989 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +0200990}
991
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200992static int
993ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx, int hdrlen)
Johannes Berg571ecf62007-07-27 15:43:22 +0200994{
Johannes Berg3017b802007-08-28 17:01:53 -0400995 /*
Johannes Berg7848ba72007-09-14 11:10:25 -0400996 * Pass through unencrypted frames if the hardware has
997 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -0400998 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400999 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001000 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001001
1002 /* Drop unencrypted frames if key is set. */
1003 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1004 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1005 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
Johannes Berg83125122007-11-28 11:07:57 +01001006 (rx->key || rx->sdata->drop_unencrypted) &&
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001007 (rx->sdata->eapol == 0 ||
1008 !ieee80211_is_eapol(rx->skb, hdrlen)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001009 if (net_ratelimit())
1010 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1011 "encryption\n", rx->dev->name);
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001012 return -EACCES;
Johannes Berg571ecf62007-07-27 15:43:22 +02001013 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001014 return 0;
Johannes Berg571ecf62007-07-27 15:43:22 +02001015}
1016
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001017static int
1018ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
Johannes Berg571ecf62007-07-27 15:43:22 +02001019{
1020 struct net_device *dev = rx->dev;
Johannes Berg571ecf62007-07-27 15:43:22 +02001021 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1022 u16 fc, hdrlen, ethertype;
1023 u8 *payload;
1024 u8 dst[ETH_ALEN];
1025 u8 src[ETH_ALEN];
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001026 struct sk_buff *skb = rx->skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001027 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001028 DECLARE_MAC_BUF(mac);
1029 DECLARE_MAC_BUF(mac2);
1030 DECLARE_MAC_BUF(mac3);
1031 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +02001032
1033 fc = rx->fc;
Johannes Berg571ecf62007-07-27 15:43:22 +02001034
1035 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001036 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001037
1038 hdrlen = ieee80211_get_hdrlen(fc);
1039
1040 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1041 * header
1042 * IEEE 802.11 address fields:
1043 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1044 * 0 0 DA SA BSSID n/a
1045 * 0 1 DA BSSID SA n/a
1046 * 1 0 BSSID SA DA n/a
1047 * 1 1 RA TA DA SA
1048 */
1049
1050 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1051 case IEEE80211_FCTL_TODS:
1052 /* BSSID SA DA */
1053 memcpy(dst, hdr->addr3, ETH_ALEN);
1054 memcpy(src, hdr->addr2, ETH_ALEN);
1055
1056 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1057 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001058 if (net_ratelimit())
1059 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -07001060 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001061 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001062 print_mac(mac, hdr->addr1),
1063 print_mac(mac2, hdr->addr2),
1064 print_mac(mac3, hdr->addr3));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001065 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001066 }
1067 break;
1068 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1069 /* RA TA DA SA */
1070 memcpy(dst, hdr->addr3, ETH_ALEN);
1071 memcpy(src, hdr->addr4, ETH_ALEN);
1072
1073 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001074 if (net_ratelimit())
1075 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -07001076 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001077 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001078 print_mac(mac, hdr->addr1),
1079 print_mac(mac2, hdr->addr2),
1080 print_mac(mac3, hdr->addr3),
1081 print_mac(mac4, hdr->addr4));
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001082 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001083 }
1084 break;
1085 case IEEE80211_FCTL_FROMDS:
1086 /* DA BSSID SA */
1087 memcpy(dst, hdr->addr1, ETH_ALEN);
1088 memcpy(src, hdr->addr3, ETH_ALEN);
1089
John W. Linvilleb3316152007-08-28 17:01:55 -04001090 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1091 (is_multicast_ether_addr(dst) &&
1092 !compare_ether_addr(src, dev->dev_addr)))
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001093 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001094 break;
1095 case 0:
1096 /* DA SA BSSID */
1097 memcpy(dst, hdr->addr1, ETH_ALEN);
1098 memcpy(src, hdr->addr2, ETH_ALEN);
1099
1100 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1101 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001102 printk(KERN_DEBUG "%s: dropped IBSS frame "
1103 "(DA=%s SA=%s BSSID=%s)\n",
1104 dev->name,
1105 print_mac(mac, hdr->addr1),
1106 print_mac(mac2, hdr->addr2),
1107 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001108 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001109 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001110 }
1111 break;
1112 }
1113
Johannes Berg571ecf62007-07-27 15:43:22 +02001114 if (unlikely(skb->len - hdrlen < 8)) {
1115 if (net_ratelimit()) {
1116 printk(KERN_DEBUG "%s: RX too short data frame "
1117 "payload\n", dev->name);
1118 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001119 return -1;
Johannes Berg571ecf62007-07-27 15:43:22 +02001120 }
1121
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001122 payload = skb->data + hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001123 ethertype = (payload[6] << 8) | payload[7];
1124
1125 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1126 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1127 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1128 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1129 * replace EtherType */
1130 skb_pull(skb, hdrlen + 6);
1131 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1132 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1133 } else {
1134 struct ethhdr *ehdr;
1135 __be16 len;
1136 skb_pull(skb, hdrlen);
1137 len = htons(skb->len);
1138 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1139 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1140 memcpy(ehdr->h_source, src, ETH_ALEN);
1141 ehdr->h_proto = len;
1142 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001143 return 0;
1144}
Johannes Berg571ecf62007-07-27 15:43:22 +02001145
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001146static void
1147ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1148{
1149 struct net_device *dev = rx->dev;
1150 struct ieee80211_local *local = rx->local;
1151 struct sk_buff *skb, *xmit_skb;
1152 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg571ecf62007-07-27 15:43:22 +02001153
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001154 skb = rx->skb;
1155 xmit_skb = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +02001156
1157 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
Jiri Slabybadffb72007-08-28 17:01:54 -04001158 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1159 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001160 if (is_multicast_ether_addr(skb->data)) {
1161 /* send multicast frames both to higher layers in
1162 * local net stack and back to the wireless media */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001163 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1164 if (!xmit_skb && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001165 printk(KERN_DEBUG "%s: failed to clone "
1166 "multicast frame\n", dev->name);
1167 } else {
1168 struct sta_info *dsta;
1169 dsta = sta_info_get(local, skb->data);
1170 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001171 if (net_ratelimit())
1172 printk(KERN_DEBUG "Station with null "
1173 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001174 } else if (dsta && dsta->dev == dev) {
1175 /* Destination station is associated to this
1176 * AP, so send the frame directly to it and
1177 * do not pass the frame to local net stack.
1178 */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001179 xmit_skb = skb;
Johannes Berg571ecf62007-07-27 15:43:22 +02001180 skb = NULL;
1181 }
1182 if (dsta)
1183 sta_info_put(dsta);
1184 }
1185 }
1186
1187 if (skb) {
1188 /* deliver to local stack */
1189 skb->protocol = eth_type_trans(skb, dev);
1190 memset(skb->cb, 0, sizeof(skb->cb));
1191 netif_rx(skb);
1192 }
1193
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001194 if (xmit_skb) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001195 /* send to wireless media */
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001196 xmit_skb->protocol = __constant_htons(ETH_P_802_3);
1197 skb_set_network_header(xmit_skb, 0);
1198 skb_set_mac_header(xmit_skb, 0);
1199 dev_queue_xmit(xmit_skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001200 }
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001201}
1202
1203static ieee80211_txrx_result
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001204ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1205{
1206 struct net_device *dev = rx->dev;
1207 struct ieee80211_local *local = rx->local;
1208 u16 fc, ethertype;
1209 u8 *payload;
1210 struct sk_buff *skb = rx->skb, *frame = NULL;
1211 const struct ethhdr *eth;
1212 int remaining, err;
1213 u8 dst[ETH_ALEN];
1214 u8 src[ETH_ALEN];
1215 DECLARE_MAC_BUF(mac);
1216
1217 fc = rx->fc;
1218 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1219 return TXRX_CONTINUE;
1220
1221 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1222 return TXRX_DROP;
1223
1224 if (!rx->u.rx.amsdu_frame)
1225 return TXRX_CONTINUE;
1226
1227 err = ieee80211_data_to_8023(rx);
1228 if (unlikely(err))
1229 return TXRX_DROP;
1230
1231 skb->dev = dev;
1232
1233 dev->stats.rx_packets++;
1234 dev->stats.rx_bytes += skb->len;
1235
1236 /* skip the wrapping header */
1237 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1238 if (!eth)
1239 return TXRX_DROP;
1240
1241 while (skb != frame) {
1242 u8 padding;
1243 __be16 len = eth->h_proto;
1244 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1245
1246 remaining = skb->len;
1247 memcpy(dst, eth->h_dest, ETH_ALEN);
1248 memcpy(src, eth->h_source, ETH_ALEN);
1249
1250 padding = ((4 - subframe_len) & 0x3);
1251 /* the last MSDU has no padding */
1252 if (subframe_len > remaining) {
1253 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1254 return TXRX_DROP;
1255 }
1256
1257 skb_pull(skb, sizeof(struct ethhdr));
1258 /* if last subframe reuse skb */
1259 if (remaining <= subframe_len + padding)
1260 frame = skb;
1261 else {
1262 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1263 subframe_len);
1264
1265 if (frame == NULL)
1266 return TXRX_DROP;
1267
1268 skb_reserve(frame, local->hw.extra_tx_headroom +
1269 sizeof(struct ethhdr));
1270 memcpy(skb_put(frame, ntohs(len)), skb->data,
1271 ntohs(len));
1272
1273 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1274 padding);
1275 if (!eth) {
1276 printk(KERN_DEBUG "%s: wrong buffer size ",
1277 dev->name);
1278 dev_kfree_skb(frame);
1279 return TXRX_DROP;
1280 }
1281 }
1282
1283 skb_set_network_header(frame, 0);
1284 frame->dev = dev;
1285 frame->priority = skb->priority;
1286 rx->skb = frame;
1287
1288 if ((ieee80211_drop_802_1x_pae(rx, 0)) ||
1289 (ieee80211_drop_unencrypted(rx, 0))) {
1290 if (skb == frame) /* last frame */
1291 return TXRX_DROP;
1292 dev_kfree_skb(frame);
1293 continue;
1294 }
1295
1296 payload = frame->data;
1297 ethertype = (payload[6] << 8) | payload[7];
1298
1299 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1300 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1301 compare_ether_addr(payload,
1302 bridge_tunnel_header) == 0)) {
1303 /* remove RFC1042 or Bridge-Tunnel
1304 * encapsulation and replace EtherType */
1305 skb_pull(frame, 6);
1306 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1307 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1308 } else {
1309 memcpy(skb_push(frame, sizeof(__be16)), &len,
1310 sizeof(__be16));
1311 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1312 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1313 }
1314
1315
1316 ieee80211_deliver_skb(rx);
1317 }
1318
1319 return TXRX_QUEUED;
1320}
1321
1322static ieee80211_txrx_result
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +02001323ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1324{
1325 struct net_device *dev = rx->dev;
1326 u16 fc;
1327 int err, hdrlen;
1328
1329 fc = rx->fc;
1330 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1331 return TXRX_CONTINUE;
1332
1333 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1334 return TXRX_DROP;
1335
1336 hdrlen = ieee80211_get_hdrlen(fc);
1337
1338 if ((ieee80211_drop_802_1x_pae(rx, hdrlen)) ||
1339 (ieee80211_drop_unencrypted(rx, hdrlen)))
1340 return TXRX_DROP;
1341
1342 err = ieee80211_data_to_8023(rx);
1343 if (unlikely(err))
1344 return TXRX_DROP;
1345
1346 rx->skb->dev = dev;
1347
1348 dev->stats.rx_packets++;
1349 dev->stats.rx_bytes += rx->skb->len;
1350
1351 ieee80211_deliver_skb(rx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001352
1353 return TXRX_QUEUED;
1354}
1355
1356static ieee80211_txrx_result
1357ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1358{
1359 struct ieee80211_sub_if_data *sdata;
1360
Jiri Slabybadffb72007-08-28 17:01:54 -04001361 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001362 return TXRX_DROP;
1363
1364 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1365 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1366 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +02001367 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg571ecf62007-07-27 15:43:22 +02001368 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
Johannes Bergf9d540e2007-09-28 14:02:09 +02001369 else
1370 return TXRX_DROP;
1371
Johannes Berg571ecf62007-07-27 15:43:22 +02001372 return TXRX_QUEUED;
1373}
1374
1375static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1376 struct ieee80211_local *local,
1377 ieee80211_rx_handler *handlers,
1378 struct ieee80211_txrx_data *rx,
1379 struct sta_info *sta)
1380{
1381 ieee80211_rx_handler *handler;
1382 ieee80211_txrx_result res = TXRX_DROP;
1383
1384 for (handler = handlers; *handler != NULL; handler++) {
1385 res = (*handler)(rx);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001386
1387 switch (res) {
1388 case TXRX_CONTINUE:
1389 continue;
1390 case TXRX_DROP:
1391 I802_DEBUG_INC(local->rx_handlers_drop);
1392 if (sta)
1393 sta->rx_dropped++;
1394 break;
1395 case TXRX_QUEUED:
1396 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001397 break;
1398 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001399 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001400 }
1401
Johannes Berg8e6f0032007-07-27 15:43:22 +02001402 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001403 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001404 return res;
1405}
1406
1407static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1408 ieee80211_rx_handler *handlers,
1409 struct ieee80211_txrx_data *rx,
1410 struct sta_info *sta)
1411{
1412 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1413 TXRX_CONTINUE)
1414 dev_kfree_skb(rx->skb);
1415}
1416
1417static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1418 struct ieee80211_hdr *hdr,
1419 struct sta_info *sta,
1420 struct ieee80211_txrx_data *rx)
1421{
1422 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001423 DECLARE_MAC_BUF(mac);
1424 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001425
1426 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1427 if (rx->skb->len >= hdrlen + 4)
1428 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1429 else
1430 keyidx = -1;
1431
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001432 if (net_ratelimit())
1433 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001434 "failure from %s to %s keyidx=%d\n",
1435 dev->name, print_mac(mac, hdr->addr2),
1436 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001437
1438 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001439 /*
1440 * Some hardware seem to generate incorrect Michael MIC
1441 * reports; ignore them to avoid triggering countermeasures.
1442 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001443 if (net_ratelimit())
1444 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001445 "error for unknown address %s\n",
1446 dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001447 goto ignore;
1448 }
1449
1450 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001451 if (net_ratelimit())
1452 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001453 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001454 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001455 goto ignore;
1456 }
1457
Johannes Berg7848ba72007-09-14 11:10:25 -04001458 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001459 /*
1460 * APs with pairwise keys should never receive Michael MIC
1461 * errors for non-zero keyidx because these are reserved for
1462 * group keys and only the AP is sending real multicast
1463 * frames in the BSS.
1464 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001465 if (net_ratelimit())
1466 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1467 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001468 " (src %s)\n", dev->name, keyidx,
1469 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001470 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001471 }
1472
1473 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1474 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1475 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001476 if (net_ratelimit())
1477 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1478 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001479 "(fc=0x%04x) (src %s)\n",
1480 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001481 goto ignore;
1482 }
1483
Johannes Bergeb063c12007-08-28 17:01:53 -04001484 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001485 ignore:
1486 dev_kfree_skb(rx->skb);
1487 rx->skb = NULL;
1488}
1489
1490ieee80211_rx_handler ieee80211_rx_handlers[] =
1491{
1492 ieee80211_rx_h_if_stats,
Johannes Berg571ecf62007-07-27 15:43:22 +02001493 ieee80211_rx_h_passive_scan,
1494 ieee80211_rx_h_check,
Johannes Berg4f0d18e2007-09-26 15:19:40 +02001495 ieee80211_rx_h_decrypt,
Johannes Berg70f08762007-09-26 17:53:14 +02001496 ieee80211_rx_h_sta_process,
Johannes Berg571ecf62007-07-27 15:43:22 +02001497 ieee80211_rx_h_defragment,
1498 ieee80211_rx_h_ps_poll,
1499 ieee80211_rx_h_michael_mic_verify,
1500 /* this must be after decryption - so header is counted in MPDU mic
1501 * must be before pae and data, so QOS_DATA format frames
1502 * are not passed to user space by these functions
1503 */
1504 ieee80211_rx_h_remove_qos_control,
Ron Rindjunskyfd4c7f22007-11-26 16:14:33 +02001505 ieee80211_rx_h_amsdu,
Johannes Berg571ecf62007-07-27 15:43:22 +02001506 ieee80211_rx_h_data,
1507 ieee80211_rx_h_mgmt,
1508 NULL
1509};
1510
1511/* main receive path */
1512
Johannes Berg23a24de2007-07-27 15:43:22 +02001513static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1514 u8 *bssid, struct ieee80211_txrx_data *rx,
1515 struct ieee80211_hdr *hdr)
1516{
1517 int multicast = is_multicast_ether_addr(hdr->addr1);
1518
1519 switch (sdata->type) {
1520 case IEEE80211_IF_TYPE_STA:
1521 if (!bssid)
1522 return 0;
1523 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001524 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001525 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001526 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001527 } else if (!multicast &&
1528 compare_ether_addr(sdata->dev->dev_addr,
1529 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001530 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001531 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001532 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001533 }
1534 break;
1535 case IEEE80211_IF_TYPE_IBSS:
1536 if (!bssid)
1537 return 0;
1538 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001539 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001540 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001541 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001542 } else if (!multicast &&
1543 compare_ether_addr(sdata->dev->dev_addr,
1544 hdr->addr1) != 0) {
Johannes Berg4150c572007-09-17 01:29:23 -04001545 if (!(sdata->dev->flags & IFF_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001546 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001547 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001548 } else if (!rx->sta)
1549 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1550 bssid, hdr->addr2);
1551 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001552 case IEEE80211_IF_TYPE_VLAN:
Johannes Berg23a24de2007-07-27 15:43:22 +02001553 case IEEE80211_IF_TYPE_AP:
1554 if (!bssid) {
1555 if (compare_ether_addr(sdata->dev->dev_addr,
1556 hdr->addr1))
1557 return 0;
1558 } else if (!ieee80211_bssid_match(bssid,
1559 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001560 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001561 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001562 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001563 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001564 if (sdata->dev == sdata->local->mdev &&
1565 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001566 /* do not receive anything via
1567 * master device when not scanning */
1568 return 0;
1569 break;
1570 case IEEE80211_IF_TYPE_WDS:
1571 if (bssid ||
1572 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1573 return 0;
1574 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1575 return 0;
1576 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001577 case IEEE80211_IF_TYPE_MNTR:
1578 /* take everything */
1579 break;
Johannes Berga2897552007-09-28 14:01:25 +02001580 case IEEE80211_IF_TYPE_INVALID:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +02001581 /* should never get here */
1582 WARN_ON(1);
1583 break;
Johannes Berg23a24de2007-07-27 15:43:22 +02001584 }
1585
1586 return 1;
1587}
1588
Johannes Berg571ecf62007-07-27 15:43:22 +02001589/*
1590 * This is the receive path handler. It is called by a low level driver when an
1591 * 802.11 MPDU is received from the hardware.
1592 */
1593void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1594 struct ieee80211_rx_status *status)
1595{
1596 struct ieee80211_local *local = hw_to_local(hw);
1597 struct ieee80211_sub_if_data *sdata;
1598 struct sta_info *sta;
1599 struct ieee80211_hdr *hdr;
1600 struct ieee80211_txrx_data rx;
1601 u16 type;
Johannes Bergb2e77712007-09-26 15:19:39 +02001602 int prepres;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001603 struct ieee80211_sub_if_data *prev = NULL;
1604 struct sk_buff *skb_new;
1605 u8 *bssid;
David S. Millerd10f2152008-01-24 16:57:39 -08001606 int hdrlen;
Johannes Berg571ecf62007-07-27 15:43:22 +02001607
Johannes Bergd4e46a32007-09-14 11:10:24 -04001608 /*
Johannes Berg79010422007-09-18 17:29:21 -04001609 * key references and virtual interfaces are protected using RCU
1610 * and this requires that we are in a read-side RCU section during
1611 * receive processing
Johannes Bergd4e46a32007-09-14 11:10:24 -04001612 */
1613 rcu_read_lock();
1614
Johannes Bergb2e77712007-09-26 15:19:39 +02001615 /*
1616 * Frames with failed FCS/PLCP checksum are not returned,
1617 * all other frames are returned without radiotap header
1618 * if it was previously present.
1619 * Also, frames with less than 16 bytes are dropped.
1620 */
1621 skb = ieee80211_rx_monitor(local, skb, status);
1622 if (!skb) {
1623 rcu_read_unlock();
1624 return;
1625 }
1626
Johannes Berg571ecf62007-07-27 15:43:22 +02001627 hdr = (struct ieee80211_hdr *) skb->data;
1628 memset(&rx, 0, sizeof(rx));
1629 rx.skb = skb;
1630 rx.local = local;
1631
1632 rx.u.rx.status = status;
Johannes Bergb2e77712007-09-26 15:19:39 +02001633 rx.fc = le16_to_cpu(hdr->frame_control);
Johannes Berg571ecf62007-07-27 15:43:22 +02001634 type = rx.fc & IEEE80211_FCTL_FTYPE;
Johannes Berg72abd812007-09-17 01:29:22 -04001635
David S. Millerd10f2152008-01-24 16:57:39 -08001636 /*
1637 * Drivers are required to align the payload data to a four-byte
1638 * boundary, so the last two bits of the address where it starts
1639 * may not be set. The header is required to be directly before
1640 * the payload data, padding like atheros hardware adds which is
1641 * inbetween the 802.11 header and the payload is not supported,
1642 * the driver is required to move the 802.11 header further back
1643 * in that case.
1644 */
1645 hdrlen = ieee80211_get_hdrlen(rx.fc);
1646 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1647
Johannes Bergb2e77712007-09-26 15:19:39 +02001648 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
Johannes Berg571ecf62007-07-27 15:43:22 +02001649 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001650
Johannes Bergb2e77712007-09-26 15:19:39 +02001651 sta = rx.sta = sta_info_get(local, hdr->addr2);
1652 if (sta) {
1653 rx.dev = rx.sta->dev;
1654 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1655 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001656
Johannes Berg571ecf62007-07-27 15:43:22 +02001657 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1658 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1659 goto end;
1660 }
1661
Zhu Yiece8edd2007-11-22 10:53:21 +08001662 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001663 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001664
1665 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1666 sta) != TXRX_CONTINUE)
1667 goto end;
1668 skb = rx.skb;
1669
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001670 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg53918992007-09-26 15:19:47 +02001671 !atomic_read(&local->iff_promiscs) &&
1672 !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001673 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001674 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001675 rx.sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001676 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001677 rcu_read_unlock();
Johannes Berg8e6f0032007-07-27 15:43:22 +02001678 return;
1679 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001680
Johannes Bergb2e77712007-09-26 15:19:39 +02001681 bssid = ieee80211_get_bssid(hdr, skb->len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001682
Johannes Berg79010422007-09-18 17:29:21 -04001683 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001684 if (!netif_running(sdata->dev))
1685 continue;
1686
Johannes Bergb2e77712007-09-26 15:19:39 +02001687 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1688 continue;
1689
1690 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001691 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1692 /* prepare_for_handlers can change sta */
1693 sta = rx.sta;
1694
1695 if (!prepres)
1696 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001697
Johannes Berg340e11f2007-07-27 15:43:22 +02001698 /*
1699 * frame is destined for this interface, but if it's not
1700 * also for the previous one we handle that after the
1701 * loop to avoid copying the SKB once too much
1702 */
1703
1704 if (!prev) {
1705 prev = sdata;
1706 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001707 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001708
1709 /*
1710 * frame was destined for the previous interface
1711 * so invoke RX handlers for it
1712 */
1713
1714 skb_new = skb_copy(skb, GFP_ATOMIC);
1715 if (!skb_new) {
1716 if (net_ratelimit())
1717 printk(KERN_DEBUG "%s: failed to copy "
1718 "multicast frame for %s",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001719 wiphy_name(local->hw.wiphy),
1720 prev->dev->name);
Johannes Berg340e11f2007-07-27 15:43:22 +02001721 continue;
1722 }
1723 rx.skb = skb_new;
1724 rx.dev = prev->dev;
1725 rx.sdata = prev;
1726 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1727 &rx, sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001728 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001729 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001730 if (prev) {
1731 rx.skb = skb;
1732 rx.dev = prev->dev;
1733 rx.sdata = prev;
1734 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1735 &rx, sta);
1736 } else
1737 dev_kfree_skb(skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001738
Johannes Berg8e6f0032007-07-27 15:43:22 +02001739 end:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001740 rcu_read_unlock();
1741
Johannes Berg571ecf62007-07-27 15:43:22 +02001742 if (sta)
1743 sta_info_put(sta);
1744}
1745EXPORT_SYMBOL(__ieee80211_rx);
1746
1747/* This is a version of the rx handler that can be called from hard irq
1748 * context. Post the skb on the queue and schedule the tasklet */
1749void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1750 struct ieee80211_rx_status *status)
1751{
1752 struct ieee80211_local *local = hw_to_local(hw);
1753
1754 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1755
1756 skb->dev = local->mdev;
1757 /* copy status into skb->cb for use by tasklet */
1758 memcpy(skb->cb, status, sizeof(*status));
1759 skb->pkt_type = IEEE80211_RX_MSG;
1760 skb_queue_tail(&local->skb_queue, skb);
1761 tasklet_schedule(&local->tasklet);
1762}
1763EXPORT_SYMBOL(ieee80211_rx_irqsafe);