blob: 5a66269d943532ce2ec5fcb1032b64e8f3029ef7 [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 Berg571ecf62007-07-27 15:43:22 +020016#include <net/mac80211.h>
17#include <net/ieee80211_radiotap.h>
18
19#include "ieee80211_i.h"
20#include "ieee80211_led.h"
21#include "ieee80211_common.h"
22#include "wep.h"
23#include "wpa.h"
24#include "tkip.h"
25#include "wme.h"
26
27/* pre-rx handlers
28 *
29 * these don't have dev/sdata fields in the rx data
Johannes Berg52865dfd2007-07-27 15:43:22 +020030 * The sta value should also not be used because it may
31 * be NULL even though a STA (in IBSS mode) will be added.
Johannes Berg571ecf62007-07-27 15:43:22 +020032 */
33
34static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +020035ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
36{
37 u8 *data = rx->skb->data;
38 int tid;
39
40 /* does the frame have a qos control field? */
41 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
42 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
43 /* frame has qos control */
44 tid = qc[0] & QOS_CONTROL_TID_MASK;
45 } else {
46 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
47 /* Separate TID for management frames */
48 tid = NUM_RX_DATA_QUEUES - 1;
49 } else {
50 /* no qos control present */
51 tid = 0; /* 802.1d - Best Effort */
52 }
53 }
Johannes Berg52865dfd2007-07-27 15:43:22 +020054
Johannes Berg6e0d1142007-07-27 15:43:22 +020055 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865dfd2007-07-27 15:43:22 +020056 /* only a debug counter, sta might not be assigned properly yet */
57 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +020058 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +020059
60 rx->u.rx.queue = tid;
61 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
62 * For now, set skb->priority to 0 for other cases. */
63 rx->skb->priority = (tid > 7) ? 0 : tid;
64
65 return TXRX_CONTINUE;
66}
67
68static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +020069ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
70{
71 struct ieee80211_local *local = rx->local;
72 struct sk_buff *skb = rx->skb;
73 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
74 u32 load = 0, hdrtime;
75 struct ieee80211_rate *rate;
76 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
77 int i;
78
79 /* Estimate total channel use caused by this frame */
80
81 if (unlikely(mode->num_rates < 0))
82 return TXRX_CONTINUE;
83
84 rate = &mode->rates[0];
85 for (i = 0; i < mode->num_rates; i++) {
86 if (mode->rates[i].val == rx->u.rx.status->rate) {
87 rate = &mode->rates[i];
88 break;
89 }
90 }
91
92 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
93 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
94
95 if (mode->mode == MODE_IEEE80211A ||
96 mode->mode == MODE_ATHEROS_TURBO ||
97 mode->mode == MODE_ATHEROS_TURBOG ||
98 (mode->mode == MODE_IEEE80211G &&
99 rate->flags & IEEE80211_RATE_ERP))
100 hdrtime = CHAN_UTIL_HDR_SHORT;
101 else
102 hdrtime = CHAN_UTIL_HDR_LONG;
103
104 load = hdrtime;
105 if (!is_multicast_ether_addr(hdr->addr1))
106 load += hdrtime;
107
108 load += skb->len * rate->rate_inv;
109
110 /* Divide channel_use by 8 to avoid wrapping around the counter */
111 load >>= CHAN_UTIL_SHIFT;
112 local->channel_use_raw += load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200113 rx->u.rx.load = load;
114
115 return TXRX_CONTINUE;
116}
117
118ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
119{
120 ieee80211_rx_h_parse_qos,
121 ieee80211_rx_h_load_stats,
122 NULL
123};
124
125/* rx handlers */
126
127static ieee80211_txrx_result
128ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
129{
Johannes Berg52865dfd2007-07-27 15:43:22 +0200130 if (rx->sta)
131 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200132 rx->sdata->channel_use_raw += rx->u.rx.load;
133 return TXRX_CONTINUE;
134}
135
136static void
137ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
138 struct ieee80211_rx_status *status)
139{
140 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
141 struct ieee80211_sub_if_data *sdata;
142 struct ieee80211_rate *rate;
143 struct ieee80211_rtap_hdr {
144 struct ieee80211_radiotap_header hdr;
145 u8 flags;
146 u8 rate;
147 __le16 chan_freq;
148 __le16 chan_flags;
149 u8 antsignal;
150 } __attribute__ ((packed)) *rthdr;
151
152 skb->dev = dev;
153
154 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
155
156 if (status->flag & RX_FLAG_RADIOTAP)
157 goto out;
158
159 if (skb_headroom(skb) < sizeof(*rthdr)) {
160 I802_DEBUG_INC(local->rx_expand_skb_head);
161 if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
162 dev_kfree_skb(skb);
163 return;
164 }
165 }
166
167 rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
168 memset(rthdr, 0, sizeof(*rthdr));
169 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
170 rthdr->hdr.it_present =
171 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
172 (1 << IEEE80211_RADIOTAP_RATE) |
173 (1 << IEEE80211_RADIOTAP_CHANNEL) |
174 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
175 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
176 IEEE80211_RADIOTAP_F_FCS : 0;
177 rate = ieee80211_get_rate(local, status->phymode, status->rate);
178 if (rate)
179 rthdr->rate = rate->rate / 5;
180 rthdr->chan_freq = cpu_to_le16(status->freq);
181 rthdr->chan_flags =
182 status->phymode == MODE_IEEE80211A ?
183 cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
184 cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
185 rthdr->antsignal = status->ssi;
186
187 out:
188 sdata->stats.rx_packets++;
189 sdata->stats.rx_bytes += skb->len;
190
191 skb_set_mac_header(skb, 0);
192 skb->ip_summed = CHECKSUM_UNNECESSARY;
193 skb->pkt_type = PACKET_OTHERHOST;
194 skb->protocol = htons(ETH_P_802_2);
195 memset(skb->cb, 0, sizeof(skb->cb));
196 netif_rx(skb);
197}
198
199static ieee80211_txrx_result
200ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
201{
202 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
203 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
204 return TXRX_QUEUED;
205 }
206
207 if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
208 skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb->data));
209
210 return TXRX_CONTINUE;
211}
212
213static ieee80211_txrx_result
214ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
215{
216 struct ieee80211_local *local = rx->local;
217 struct sk_buff *skb = rx->skb;
218
219 if (unlikely(local->sta_scanning != 0)) {
220 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
221 return TXRX_QUEUED;
222 }
223
224 if (unlikely(rx->u.rx.in_scan)) {
225 /* scanning finished during invoking of handlers */
226 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
227 return TXRX_DROP;
228 }
229
230 return TXRX_CONTINUE;
231}
232
233static ieee80211_txrx_result
234ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
235{
236 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200237 hdr = (struct ieee80211_hdr *) rx->skb->data;
238
239 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
240 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
241 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
242 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
243 hdr->seq_ctrl)) {
244 if (rx->u.rx.ra_match) {
245 rx->local->dot11FrameDuplicateCount++;
246 rx->sta->num_duplicates++;
247 }
248 return TXRX_DROP;
249 } else
250 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
251 }
252
253 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
254 rx->skb->len > FCS_LEN)
255 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
256
257 if (unlikely(rx->skb->len < 16)) {
258 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
259 return TXRX_DROP;
260 }
261
262 if (!rx->u.rx.ra_match)
263 rx->skb->pkt_type = PACKET_OTHERHOST;
264 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
265 rx->skb->pkt_type = PACKET_HOST;
266 else if (is_multicast_ether_addr(hdr->addr1)) {
267 if (is_broadcast_ether_addr(hdr->addr1))
268 rx->skb->pkt_type = PACKET_BROADCAST;
269 else
270 rx->skb->pkt_type = PACKET_MULTICAST;
271 } else
272 rx->skb->pkt_type = PACKET_OTHERHOST;
273
274 /* Drop disallowed frame classes based on STA auth/assoc state;
275 * IEEE 802.11, Chap 5.5.
276 *
277 * 80211.o does filtering only based on association state, i.e., it
278 * drops Class 3 frames from not associated stations. hostapd sends
279 * deauth/disassoc frames when needed. In addition, hostapd is
280 * responsible for filtering on both auth and assoc states.
281 */
282 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
283 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
284 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
285 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
286 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
287 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
288 !(rx->fc & IEEE80211_FCTL_TODS) &&
289 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
290 || !rx->u.rx.ra_match) {
291 /* Drop IBSS frames and frames for other hosts
292 * silently. */
293 return TXRX_DROP;
294 }
295
296 if (!rx->local->apdev)
297 return TXRX_DROP;
298
299 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
300 ieee80211_msg_sta_not_assoc);
301 return TXRX_QUEUED;
302 }
303
Johannes Berg570bd532007-07-27 15:43:22 +0200304 return TXRX_CONTINUE;
305}
306
307
308static ieee80211_txrx_result
309ieee80211_rx_h_load_key(struct ieee80211_txrx_data *rx)
310{
311 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400312 int keyidx;
313 int hdrlen;
Johannes Berg570bd532007-07-27 15:43:22 +0200314
Johannes Berg3017b802007-08-28 17:01:53 -0400315 /*
316 * Key selection 101
317 *
318 * There are three types of keys:
319 * - GTK (group keys)
320 * - PTK (pairwise keys)
321 * - STK (station-to-station pairwise keys)
322 *
323 * When selecting a key, we have to distinguish between multicast
324 * (including broadcast) and unicast frames, the latter can only
325 * use PTKs and STKs while the former always use GTKs. Unless, of
326 * course, actual WEP keys ("pre-RSNA") are used, then unicast
327 * frames can also use key indizes like GTKs. Hence, if we don't
328 * have a PTK/STK we check the key index for a WEP key.
329 *
330 * There is also a slight problem in IBSS mode: GTKs are negotiated
331 * with each station, that is something we don't currently handle.
332 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200333
Johannes Berg3017b802007-08-28 17:01:53 -0400334 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
335 return TXRX_CONTINUE;
336
337 /*
338 * No point in finding a key if the frame is neither
339 * addressed to us nor a multicast frame.
340 */
341 if (!rx->u.rx.ra_match)
342 return TXRX_CONTINUE;
343
344 if (!is_multicast_ether_addr(hdr->addr1) && rx->sta && rx->sta->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200345 rx->key = rx->sta->key;
346 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400347 /*
348 * The device doesn't give us the IV so we won't be
349 * able to look up the key. That's ok though, we
350 * don't need to decrypt the frame, we just won't
351 * be able to keep statistics accurate.
352 * Except for key threshold notifications, should
353 * we somehow allow the driver to tell us which key
354 * the hardware used if this flag is set?
355 */
356 if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
357 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200358
Johannes Berg3017b802007-08-28 17:01:53 -0400359 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200360
Johannes Berg3017b802007-08-28 17:01:53 -0400361 if (rx->skb->len < 8 + hdrlen)
362 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200363
Johannes Berg3017b802007-08-28 17:01:53 -0400364 /*
365 * no need to call ieee80211_wep_get_keyidx,
366 * it verifies a bunch of things we've done already
367 */
368 keyidx = rx->skb->data[hdrlen + 3] >> 6;
369
370 rx->key = rx->sdata->keys[keyidx];
371
372 /*
373 * RSNA-protected unicast frames should always be sent with
374 * pairwise or station-to-station keys, but for WEP we allow
375 * using a key index as well.
376 */
377 if (rx->key && rx->key->alg != ALG_WEP &&
378 !is_multicast_ether_addr(hdr->addr1))
379 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200380 }
381
Johannes Berg3017b802007-08-28 17:01:53 -0400382 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200383 rx->key->tx_rx_count++;
384 if (unlikely(rx->local->key_tx_rx_threshold &&
385 rx->key->tx_rx_count >
386 rx->local->key_tx_rx_threshold)) {
387 ieee80211_key_threshold_notify(rx->dev, rx->key,
388 rx->sta);
389 }
390 }
391
392 return TXRX_CONTINUE;
393}
394
395static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
396{
397 struct ieee80211_sub_if_data *sdata;
398 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
399
400 if (sdata->bss)
401 atomic_inc(&sdata->bss->num_sta_ps);
402 sta->flags |= WLAN_STA_PS;
403 sta->pspoll = 0;
404#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
405 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
406 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
407#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
408}
409
410static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
411{
412 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
413 struct sk_buff *skb;
414 int sent = 0;
415 struct ieee80211_sub_if_data *sdata;
416 struct ieee80211_tx_packet_data *pkt_data;
417
418 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
419 if (sdata->bss)
420 atomic_dec(&sdata->bss->num_sta_ps);
421 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
422 sta->pspoll = 0;
423 if (!skb_queue_empty(&sta->ps_tx_buf)) {
424 if (local->ops->set_tim)
425 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
426 if (sdata->bss)
427 bss_tim_clear(local, sdata->bss, sta->aid);
428 }
429#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
430 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
431 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
432#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
433 /* Send all buffered frames to the station */
434 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
435 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
436 sent++;
437 pkt_data->requeue = 1;
438 dev_queue_xmit(skb);
439 }
440 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
441 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
442 local->total_ps_buffered--;
443 sent++;
444#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
445 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
446 "since STA not sleeping anymore\n", dev->name,
447 MAC_ARG(sta->addr), sta->aid);
448#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
449 pkt_data->requeue = 1;
450 dev_queue_xmit(skb);
451 }
452
453 return sent;
454}
455
456static ieee80211_txrx_result
457ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
458{
459 struct sta_info *sta = rx->sta;
460 struct net_device *dev = rx->dev;
461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
462
463 if (!sta)
464 return TXRX_CONTINUE;
465
466 /* Update last_rx only for IBSS packets which are for the current
467 * BSSID to avoid keeping the current IBSS network alive in cases where
468 * other STAs are using different BSSID. */
469 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
470 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
471 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
472 sta->last_rx = jiffies;
473 } else
474 if (!is_multicast_ether_addr(hdr->addr1) ||
475 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
476 /* Update last_rx only for unicast frames in order to prevent
477 * the Probe Request frames (the only broadcast frames from a
478 * STA in infrastructure mode) from keeping a connection alive.
479 */
480 sta->last_rx = jiffies;
481 }
482
483 if (!rx->u.rx.ra_match)
484 return TXRX_CONTINUE;
485
486 sta->rx_fragments++;
487 sta->rx_bytes += rx->skb->len;
488 sta->last_rssi = (sta->last_rssi * 15 +
489 rx->u.rx.status->ssi) / 16;
490 sta->last_signal = (sta->last_signal * 15 +
491 rx->u.rx.status->signal) / 16;
492 sta->last_noise = (sta->last_noise * 15 +
493 rx->u.rx.status->noise) / 16;
494
495 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
496 /* Change STA power saving mode only in the end of a frame
497 * exchange sequence */
498 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
499 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
500 else if (!(sta->flags & WLAN_STA_PS) &&
501 (rx->fc & IEEE80211_FCTL_PM))
502 ap_sta_ps_start(dev, sta);
503 }
504
505 /* Drop data::nullfunc frames silently, since they are used only to
506 * control station power saving mode. */
507 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
508 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
509 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
510 /* Update counter and free packet here to avoid counting this
511 * as a dropped packed. */
512 sta->rx_packets++;
513 dev_kfree_skb(rx->skb);
514 return TXRX_QUEUED;
515 }
516
517 return TXRX_CONTINUE;
518} /* ieee80211_rx_h_sta_process */
519
520static ieee80211_txrx_result
521ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
522{
523 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
524 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
525 !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
526 return TXRX_CONTINUE;
527
528 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
529 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
530 rx->key->force_sw_encrypt) {
531 u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
532 if (iv) {
533 rx->sta->wep_weak_iv_count++;
534 }
535 }
536
537 return TXRX_CONTINUE;
538}
539
540static ieee80211_txrx_result
541ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
542{
Johannes Berg571ecf62007-07-27 15:43:22 +0200543 if ((rx->key && rx->key->alg != ALG_WEP) ||
544 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
545 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
546 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
547 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
548 return TXRX_CONTINUE;
549
550 if (!rx->key) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400551 if (net_ratelimit())
552 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
553 rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200554 return TXRX_DROP;
555 }
556
557 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
558 rx->key->force_sw_encrypt) {
559 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400560 if (net_ratelimit())
561 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
562 "failed\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200563 return TXRX_DROP;
564 }
565 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
566 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
567 /* remove ICV */
568 skb_trim(rx->skb, rx->skb->len - 4);
569 }
570
571 return TXRX_CONTINUE;
572}
573
574static inline struct ieee80211_fragment_entry *
575ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
576 unsigned int frag, unsigned int seq, int rx_queue,
577 struct sk_buff **skb)
578{
579 struct ieee80211_fragment_entry *entry;
580 int idx;
581
582 idx = sdata->fragment_next;
583 entry = &sdata->fragments[sdata->fragment_next++];
584 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
585 sdata->fragment_next = 0;
586
587 if (!skb_queue_empty(&entry->skb_list)) {
588#ifdef CONFIG_MAC80211_DEBUG
589 struct ieee80211_hdr *hdr =
590 (struct ieee80211_hdr *) entry->skb_list.next->data;
591 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
592 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
593 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
594 sdata->dev->name, idx,
595 jiffies - entry->first_frag_time, entry->seq,
596 entry->last_frag, MAC_ARG(hdr->addr1),
597 MAC_ARG(hdr->addr2));
598#endif /* CONFIG_MAC80211_DEBUG */
599 __skb_queue_purge(&entry->skb_list);
600 }
601
602 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
603 *skb = NULL;
604 entry->first_frag_time = jiffies;
605 entry->seq = seq;
606 entry->rx_queue = rx_queue;
607 entry->last_frag = frag;
608 entry->ccmp = 0;
609 entry->extra_len = 0;
610
611 return entry;
612}
613
614static inline struct ieee80211_fragment_entry *
615ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
616 u16 fc, unsigned int frag, unsigned int seq,
617 int rx_queue, struct ieee80211_hdr *hdr)
618{
619 struct ieee80211_fragment_entry *entry;
620 int i, idx;
621
622 idx = sdata->fragment_next;
623 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
624 struct ieee80211_hdr *f_hdr;
625 u16 f_fc;
626
627 idx--;
628 if (idx < 0)
629 idx = IEEE80211_FRAGMENT_MAX - 1;
630
631 entry = &sdata->fragments[idx];
632 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
633 entry->rx_queue != rx_queue ||
634 entry->last_frag + 1 != frag)
635 continue;
636
637 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
638 f_fc = le16_to_cpu(f_hdr->frame_control);
639
640 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
641 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
642 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
643 continue;
644
645 if (entry->first_frag_time + 2 * HZ < jiffies) {
646 __skb_queue_purge(&entry->skb_list);
647 continue;
648 }
649 return entry;
650 }
651
652 return NULL;
653}
654
655static ieee80211_txrx_result
656ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
657{
658 struct ieee80211_hdr *hdr;
659 u16 sc;
660 unsigned int frag, seq;
661 struct ieee80211_fragment_entry *entry;
662 struct sk_buff *skb;
663
664 hdr = (struct ieee80211_hdr *) rx->skb->data;
665 sc = le16_to_cpu(hdr->seq_ctrl);
666 frag = sc & IEEE80211_SCTL_FRAG;
667
668 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
669 (rx->skb)->len < 24 ||
670 is_multicast_ether_addr(hdr->addr1))) {
671 /* not fragmented */
672 goto out;
673 }
674 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
675
676 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
677
678 if (frag == 0) {
679 /* This is the first fragment of a new frame. */
680 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
681 rx->u.rx.queue, &(rx->skb));
682 if (rx->key && rx->key->alg == ALG_CCMP &&
683 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
684 /* Store CCMP PN so that we can verify that the next
685 * fragment has a sequential PN value. */
686 entry->ccmp = 1;
687 memcpy(entry->last_pn,
688 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
689 CCMP_PN_LEN);
690 }
691 return TXRX_QUEUED;
692 }
693
694 /* This is a fragment for a frame that should already be pending in
695 * fragment cache. Add this fragment to the end of the pending entry.
696 */
697 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
698 rx->u.rx.queue, hdr);
699 if (!entry) {
700 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
701 return TXRX_DROP;
702 }
703
704 /* Verify that MPDUs within one MSDU have sequential PN values.
705 * (IEEE 802.11i, 8.3.3.4.5) */
706 if (entry->ccmp) {
707 int i;
708 u8 pn[CCMP_PN_LEN], *rpn;
709 if (!rx->key || rx->key->alg != ALG_CCMP)
710 return TXRX_DROP;
711 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
712 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
713 pn[i]++;
714 if (pn[i])
715 break;
716 }
717 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
718 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400719 if (net_ratelimit())
720 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
721 "sequential A2=" MAC_FMT
722 " PN=%02x%02x%02x%02x%02x%02x "
723 "(expected %02x%02x%02x%02x%02x%02x)\n",
724 rx->dev->name, MAC_ARG(hdr->addr2),
725 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
726 rpn[5], pn[0], pn[1], pn[2], pn[3],
727 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200728 return TXRX_DROP;
729 }
730 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
731 }
732
733 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
734 __skb_queue_tail(&entry->skb_list, rx->skb);
735 entry->last_frag = frag;
736 entry->extra_len += rx->skb->len;
737 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
738 rx->skb = NULL;
739 return TXRX_QUEUED;
740 }
741
742 rx->skb = __skb_dequeue(&entry->skb_list);
743 if (skb_tailroom(rx->skb) < entry->extra_len) {
744 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
745 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
746 GFP_ATOMIC))) {
747 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
748 __skb_queue_purge(&entry->skb_list);
749 return TXRX_DROP;
750 }
751 }
752 while ((skb = __skb_dequeue(&entry->skb_list))) {
753 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
754 dev_kfree_skb(skb);
755 }
756
757 /* Complete frame has been reassembled - process it now */
758 rx->fragmented = 1;
759
760 out:
761 if (rx->sta)
762 rx->sta->rx_packets++;
763 if (is_multicast_ether_addr(hdr->addr1))
764 rx->local->dot11MulticastReceivedFrameCount++;
765 else
766 ieee80211_led_rx(rx->local);
767 return TXRX_CONTINUE;
768}
769
770static ieee80211_txrx_result
771ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
772{
773 struct sk_buff *skb;
774 int no_pending_pkts;
775
776 if (likely(!rx->sta ||
777 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
778 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
779 !rx->u.rx.ra_match))
780 return TXRX_CONTINUE;
781
782 skb = skb_dequeue(&rx->sta->tx_filtered);
783 if (!skb) {
784 skb = skb_dequeue(&rx->sta->ps_tx_buf);
785 if (skb)
786 rx->local->total_ps_buffered--;
787 }
788 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
789 skb_queue_empty(&rx->sta->ps_tx_buf);
790
791 if (skb) {
792 struct ieee80211_hdr *hdr =
793 (struct ieee80211_hdr *) skb->data;
794
795 /* tell TX path to send one frame even though the STA may
796 * still remain is PS mode after this frame exchange */
797 rx->sta->pspoll = 1;
798
799#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
800 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
801 "after %d)\n",
802 MAC_ARG(rx->sta->addr), rx->sta->aid,
803 skb_queue_len(&rx->sta->ps_tx_buf));
804#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
805
806 /* Use MoreData flag to indicate whether there are more
807 * buffered frames for this STA */
808 if (no_pending_pkts) {
809 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
810 rx->sta->flags &= ~WLAN_STA_TIM;
811 } else
812 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
813
814 dev_queue_xmit(skb);
815
816 if (no_pending_pkts) {
817 if (rx->local->ops->set_tim)
818 rx->local->ops->set_tim(local_to_hw(rx->local),
819 rx->sta->aid, 0);
820 if (rx->sdata->bss)
821 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
822 }
823#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
824 } else if (!rx->u.rx.sent_ps_buffered) {
825 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
826 "though there is no buffered frames for it\n",
827 rx->dev->name, MAC_ARG(rx->sta->addr));
828#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
829
830 }
831
832 /* Free PS Poll skb here instead of returning TXRX_DROP that would
833 * count as an dropped frame. */
834 dev_kfree_skb(rx->skb);
835
836 return TXRX_QUEUED;
837}
838
839static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200840ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
841{
842 u16 fc = rx->fc;
843 u8 *data = rx->skb->data;
844 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
845
846 if (!WLAN_FC_IS_QOS_DATA(fc))
847 return TXRX_CONTINUE;
848
849 /* remove the qos control field, update frame type and meta-data */
850 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
851 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
852 /* change frame type to non QOS */
853 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
854 hdr->frame_control = cpu_to_le16(fc);
855
856 return TXRX_CONTINUE;
857}
858
859static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200860ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
861{
862 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
863 rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
864 /* Pass both encrypted and unencrypted EAPOL frames to user
865 * space for processing. */
866 if (!rx->local->apdev)
867 return TXRX_DROP;
868 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
869 ieee80211_msg_normal);
870 return TXRX_QUEUED;
871 }
872
873 if (unlikely(rx->sdata->ieee802_1x &&
874 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
875 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
876 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
877 !ieee80211_is_eapol(rx->skb))) {
878#ifdef CONFIG_MAC80211_DEBUG
879 struct ieee80211_hdr *hdr =
880 (struct ieee80211_hdr *) rx->skb->data;
881 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
882 " (unauthorized port)\n", rx->dev->name,
883 MAC_ARG(hdr->addr2));
884#endif /* CONFIG_MAC80211_DEBUG */
885 return TXRX_DROP;
886 }
887
888 return TXRX_CONTINUE;
889}
890
891static ieee80211_txrx_result
892ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
893{
Johannes Berg3017b802007-08-28 17:01:53 -0400894 /*
895 * Pass through unencrypted frames if the hardware might have
896 * decrypted them already without telling us, but that can only
897 * be true if we either didn't find a key or the found key is
898 * uploaded to the hardware.
899 */
900 if ((rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) &&
901 (!rx->key || !rx->key->force_sw_encrypt))
Johannes Berg571ecf62007-07-27 15:43:22 +0200902 return TXRX_CONTINUE;
903
904 /* Drop unencrypted frames if key is set. */
905 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
906 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
907 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
908 (rx->key || rx->sdata->drop_unencrypted) &&
909 (rx->sdata->eapol == 0 ||
910 !ieee80211_is_eapol(rx->skb)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400911 if (net_ratelimit())
912 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
913 "encryption\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200914 return TXRX_DROP;
915 }
916 return TXRX_CONTINUE;
917}
918
919static ieee80211_txrx_result
920ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
921{
922 struct net_device *dev = rx->dev;
923 struct ieee80211_local *local = rx->local;
924 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
925 u16 fc, hdrlen, ethertype;
926 u8 *payload;
927 u8 dst[ETH_ALEN];
928 u8 src[ETH_ALEN];
929 struct sk_buff *skb = rx->skb, *skb2;
930 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
931
932 fc = rx->fc;
933 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
934 return TXRX_CONTINUE;
935
936 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
937 return TXRX_DROP;
938
939 hdrlen = ieee80211_get_hdrlen(fc);
940
941 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
942 * header
943 * IEEE 802.11 address fields:
944 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
945 * 0 0 DA SA BSSID n/a
946 * 0 1 DA BSSID SA n/a
947 * 1 0 BSSID SA DA n/a
948 * 1 1 RA TA DA SA
949 */
950
951 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
952 case IEEE80211_FCTL_TODS:
953 /* BSSID SA DA */
954 memcpy(dst, hdr->addr3, ETH_ALEN);
955 memcpy(src, hdr->addr2, ETH_ALEN);
956
957 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
958 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400959 if (net_ratelimit())
960 printk(KERN_DEBUG "%s: dropped ToDS frame "
961 "(BSSID=" MAC_FMT
962 " SA=" MAC_FMT
963 " DA=" MAC_FMT ")\n",
964 dev->name,
965 MAC_ARG(hdr->addr1),
966 MAC_ARG(hdr->addr2),
967 MAC_ARG(hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +0200968 return TXRX_DROP;
969 }
970 break;
971 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
972 /* RA TA DA SA */
973 memcpy(dst, hdr->addr3, ETH_ALEN);
974 memcpy(src, hdr->addr4, ETH_ALEN);
975
976 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400977 if (net_ratelimit())
978 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
979 "frame (RA=" MAC_FMT
980 " TA=" MAC_FMT " DA=" MAC_FMT
981 " SA=" MAC_FMT ")\n",
982 rx->dev->name,
983 MAC_ARG(hdr->addr1),
984 MAC_ARG(hdr->addr2),
985 MAC_ARG(hdr->addr3),
986 MAC_ARG(hdr->addr4));
Johannes Berg571ecf62007-07-27 15:43:22 +0200987 return TXRX_DROP;
988 }
989 break;
990 case IEEE80211_FCTL_FROMDS:
991 /* DA BSSID SA */
992 memcpy(dst, hdr->addr1, ETH_ALEN);
993 memcpy(src, hdr->addr3, ETH_ALEN);
994
995 if (sdata->type != IEEE80211_IF_TYPE_STA) {
996 return TXRX_DROP;
997 }
998 break;
999 case 0:
1000 /* DA SA BSSID */
1001 memcpy(dst, hdr->addr1, ETH_ALEN);
1002 memcpy(src, hdr->addr2, ETH_ALEN);
1003
1004 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1005 if (net_ratelimit()) {
1006 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
1007 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
1008 ")\n",
1009 dev->name, MAC_ARG(hdr->addr1),
1010 MAC_ARG(hdr->addr2),
1011 MAC_ARG(hdr->addr3));
1012 }
1013 return TXRX_DROP;
1014 }
1015 break;
1016 }
1017
1018 payload = skb->data + hdrlen;
1019
1020 if (unlikely(skb->len - hdrlen < 8)) {
1021 if (net_ratelimit()) {
1022 printk(KERN_DEBUG "%s: RX too short data frame "
1023 "payload\n", dev->name);
1024 }
1025 return TXRX_DROP;
1026 }
1027
1028 ethertype = (payload[6] << 8) | payload[7];
1029
1030 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1031 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1032 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1033 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1034 * replace EtherType */
1035 skb_pull(skb, hdrlen + 6);
1036 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1037 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1038 } else {
1039 struct ethhdr *ehdr;
1040 __be16 len;
1041 skb_pull(skb, hdrlen);
1042 len = htons(skb->len);
1043 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1044 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1045 memcpy(ehdr->h_source, src, ETH_ALEN);
1046 ehdr->h_proto = len;
1047 }
1048 skb->dev = dev;
1049
1050 skb2 = NULL;
1051
1052 sdata->stats.rx_packets++;
1053 sdata->stats.rx_bytes += skb->len;
1054
1055 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
1056 || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
1057 if (is_multicast_ether_addr(skb->data)) {
1058 /* send multicast frames both to higher layers in
1059 * local net stack and back to the wireless media */
1060 skb2 = skb_copy(skb, GFP_ATOMIC);
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001061 if (!skb2 && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001062 printk(KERN_DEBUG "%s: failed to clone "
1063 "multicast frame\n", dev->name);
1064 } else {
1065 struct sta_info *dsta;
1066 dsta = sta_info_get(local, skb->data);
1067 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001068 if (net_ratelimit())
1069 printk(KERN_DEBUG "Station with null "
1070 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001071 } else if (dsta && dsta->dev == dev) {
1072 /* Destination station is associated to this
1073 * AP, so send the frame directly to it and
1074 * do not pass the frame to local net stack.
1075 */
1076 skb2 = skb;
1077 skb = NULL;
1078 }
1079 if (dsta)
1080 sta_info_put(dsta);
1081 }
1082 }
1083
1084 if (skb) {
1085 /* deliver to local stack */
1086 skb->protocol = eth_type_trans(skb, dev);
1087 memset(skb->cb, 0, sizeof(skb->cb));
1088 netif_rx(skb);
1089 }
1090
1091 if (skb2) {
1092 /* send to wireless media */
1093 skb2->protocol = __constant_htons(ETH_P_802_3);
1094 skb_set_network_header(skb2, 0);
1095 skb_set_mac_header(skb2, 0);
1096 dev_queue_xmit(skb2);
1097 }
1098
1099 return TXRX_QUEUED;
1100}
1101
1102static ieee80211_txrx_result
1103ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1104{
1105 struct ieee80211_sub_if_data *sdata;
1106
1107 if (!rx->u.rx.ra_match)
1108 return TXRX_DROP;
1109
1110 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1111 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1112 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1113 !rx->local->user_space_mlme) {
1114 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1115 } else {
1116 /* Management frames are sent to hostapd for processing */
1117 if (!rx->local->apdev)
1118 return TXRX_DROP;
1119 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
1120 ieee80211_msg_normal);
1121 }
1122 return TXRX_QUEUED;
1123}
1124
1125static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1126 struct ieee80211_local *local,
1127 ieee80211_rx_handler *handlers,
1128 struct ieee80211_txrx_data *rx,
1129 struct sta_info *sta)
1130{
1131 ieee80211_rx_handler *handler;
1132 ieee80211_txrx_result res = TXRX_DROP;
1133
1134 for (handler = handlers; *handler != NULL; handler++) {
1135 res = (*handler)(rx);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001136
1137 switch (res) {
1138 case TXRX_CONTINUE:
1139 continue;
1140 case TXRX_DROP:
1141 I802_DEBUG_INC(local->rx_handlers_drop);
1142 if (sta)
1143 sta->rx_dropped++;
1144 break;
1145 case TXRX_QUEUED:
1146 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001147 break;
1148 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001149 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001150 }
1151
Johannes Berg8e6f00322007-07-27 15:43:22 +02001152 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001153 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001154 return res;
1155}
1156
1157static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1158 ieee80211_rx_handler *handlers,
1159 struct ieee80211_txrx_data *rx,
1160 struct sta_info *sta)
1161{
1162 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1163 TXRX_CONTINUE)
1164 dev_kfree_skb(rx->skb);
1165}
1166
1167static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1168 struct ieee80211_hdr *hdr,
1169 struct sta_info *sta,
1170 struct ieee80211_txrx_data *rx)
1171{
1172 int keyidx, hdrlen;
1173
1174 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1175 if (rx->skb->len >= hdrlen + 4)
1176 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1177 else
1178 keyidx = -1;
1179
1180 /* TODO: verify that this is not triggered by fragmented
1181 * frames (hw does not verify MIC for them). */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001182 if (net_ratelimit())
1183 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1184 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
1185 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1),
1186 keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001187
1188 if (!sta) {
1189 /* Some hardware versions seem to generate incorrect
1190 * Michael MIC reports; ignore them to avoid triggering
1191 * countermeasures. */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001192 if (net_ratelimit())
1193 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1194 "error for unknown address " MAC_FMT "\n",
1195 dev->name, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001196 goto ignore;
1197 }
1198
1199 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001200 if (net_ratelimit())
1201 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1202 "error for a frame with no ISWEP flag (src "
1203 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001204 goto ignore;
1205 }
1206
1207 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
Johannes Bergeb063c12007-08-28 17:01:53 -04001208 rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001209 /* AP with Pairwise keys support should never receive Michael
1210 * MIC errors for non-zero keyidx because these are reserved
1211 * for group keys and only the AP is sending real multicast
1212 * frames in BSS. */
Johannes Bergeb063c12007-08-28 17:01:53 -04001213 if (net_ratelimit())
1214 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1215 "a frame with non-zero keyidx (%d)"
1216 " (src " MAC_FMT ")\n", dev->name, keyidx,
1217 MAC_ARG(hdr->addr2));
1218 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001219 }
1220
1221 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1222 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1223 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001224 if (net_ratelimit())
1225 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1226 "error for a frame that cannot be encrypted "
1227 "(fc=0x%04x) (src " MAC_FMT ")\n",
1228 dev->name, rx->fc, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001229 goto ignore;
1230 }
1231
Johannes Berg571ecf62007-07-27 15:43:22 +02001232 /* TODO: consider verifying the MIC error report with software
1233 * implementation if we get too many spurious reports from the
1234 * hardware. */
Johannes Berg571ecf62007-07-27 15:43:22 +02001235
Johannes Bergeb063c12007-08-28 17:01:53 -04001236 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001237 ignore:
1238 dev_kfree_skb(rx->skb);
1239 rx->skb = NULL;
1240}
1241
1242ieee80211_rx_handler ieee80211_rx_handlers[] =
1243{
1244 ieee80211_rx_h_if_stats,
1245 ieee80211_rx_h_monitor,
1246 ieee80211_rx_h_passive_scan,
1247 ieee80211_rx_h_check,
Johannes Berg570bd532007-07-27 15:43:22 +02001248 ieee80211_rx_h_load_key,
Johannes Berg571ecf62007-07-27 15:43:22 +02001249 ieee80211_rx_h_sta_process,
1250 ieee80211_rx_h_ccmp_decrypt,
1251 ieee80211_rx_h_tkip_decrypt,
1252 ieee80211_rx_h_wep_weak_iv_detection,
1253 ieee80211_rx_h_wep_decrypt,
1254 ieee80211_rx_h_defragment,
1255 ieee80211_rx_h_ps_poll,
1256 ieee80211_rx_h_michael_mic_verify,
1257 /* this must be after decryption - so header is counted in MPDU mic
1258 * must be before pae and data, so QOS_DATA format frames
1259 * are not passed to user space by these functions
1260 */
1261 ieee80211_rx_h_remove_qos_control,
1262 ieee80211_rx_h_802_1x_pae,
1263 ieee80211_rx_h_drop_unencrypted,
1264 ieee80211_rx_h_data,
1265 ieee80211_rx_h_mgmt,
1266 NULL
1267};
1268
1269/* main receive path */
1270
Johannes Berg23a24de2007-07-27 15:43:22 +02001271static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1272 u8 *bssid, struct ieee80211_txrx_data *rx,
1273 struct ieee80211_hdr *hdr)
1274{
1275 int multicast = is_multicast_ether_addr(hdr->addr1);
1276
1277 switch (sdata->type) {
1278 case IEEE80211_IF_TYPE_STA:
1279 if (!bssid)
1280 return 0;
1281 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1282 if (!rx->u.rx.in_scan)
1283 return 0;
1284 rx->u.rx.ra_match = 0;
1285 } else if (!multicast &&
1286 compare_ether_addr(sdata->dev->dev_addr,
1287 hdr->addr1) != 0) {
1288 if (!sdata->promisc)
1289 return 0;
1290 rx->u.rx.ra_match = 0;
1291 }
1292 break;
1293 case IEEE80211_IF_TYPE_IBSS:
1294 if (!bssid)
1295 return 0;
1296 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1297 if (!rx->u.rx.in_scan)
1298 return 0;
1299 rx->u.rx.ra_match = 0;
1300 } else if (!multicast &&
1301 compare_ether_addr(sdata->dev->dev_addr,
1302 hdr->addr1) != 0) {
1303 if (!sdata->promisc)
1304 return 0;
1305 rx->u.rx.ra_match = 0;
1306 } else if (!rx->sta)
1307 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1308 bssid, hdr->addr2);
1309 break;
1310 case IEEE80211_IF_TYPE_AP:
1311 if (!bssid) {
1312 if (compare_ether_addr(sdata->dev->dev_addr,
1313 hdr->addr1))
1314 return 0;
1315 } else if (!ieee80211_bssid_match(bssid,
1316 sdata->dev->dev_addr)) {
1317 if (!rx->u.rx.in_scan)
1318 return 0;
1319 rx->u.rx.ra_match = 0;
1320 }
1321 if (sdata->dev == sdata->local->mdev && !rx->u.rx.in_scan)
1322 /* do not receive anything via
1323 * master device when not scanning */
1324 return 0;
1325 break;
1326 case IEEE80211_IF_TYPE_WDS:
1327 if (bssid ||
1328 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1329 return 0;
1330 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1331 return 0;
1332 break;
1333 }
1334
1335 return 1;
1336}
1337
Johannes Berg571ecf62007-07-27 15:43:22 +02001338/*
1339 * This is the receive path handler. It is called by a low level driver when an
1340 * 802.11 MPDU is received from the hardware.
1341 */
1342void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1343 struct ieee80211_rx_status *status)
1344{
1345 struct ieee80211_local *local = hw_to_local(hw);
1346 struct ieee80211_sub_if_data *sdata;
1347 struct sta_info *sta;
1348 struct ieee80211_hdr *hdr;
1349 struct ieee80211_txrx_data rx;
1350 u16 type;
Johannes Berg23a24de2007-07-27 15:43:22 +02001351 int radiotap_len = 0, prepres;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001352 struct ieee80211_sub_if_data *prev = NULL;
1353 struct sk_buff *skb_new;
1354 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001355
1356 if (status->flag & RX_FLAG_RADIOTAP) {
1357 radiotap_len = ieee80211_get_radiotap_len(skb->data);
1358 skb_pull(skb, radiotap_len);
1359 }
1360
1361 hdr = (struct ieee80211_hdr *) skb->data;
1362 memset(&rx, 0, sizeof(rx));
1363 rx.skb = skb;
1364 rx.local = local;
1365
1366 rx.u.rx.status = status;
1367 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
1368 type = rx.fc & IEEE80211_FCTL_FTYPE;
1369 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1370 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001371
Johannes Berg23a24de2007-07-27 15:43:22 +02001372 if (skb->len >= 16) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001373 sta = rx.sta = sta_info_get(local, hdr->addr2);
Johannes Berg23a24de2007-07-27 15:43:22 +02001374 if (sta) {
1375 rx.dev = rx.sta->dev;
1376 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1377 }
1378 } else
Johannes Berg571ecf62007-07-27 15:43:22 +02001379 sta = rx.sta = NULL;
1380
Johannes Berg571ecf62007-07-27 15:43:22 +02001381 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1382 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1383 goto end;
1384 }
1385
1386 if (unlikely(local->sta_scanning))
1387 rx.u.rx.in_scan = 1;
1388
1389 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1390 sta) != TXRX_CONTINUE)
1391 goto end;
1392 skb = rx.skb;
1393
1394 skb_push(skb, radiotap_len);
1395 if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
Johannes Berg23a24de2007-07-27 15:43:22 +02001396 !local->iff_promiscs && !is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001397 rx.u.rx.ra_match = 1;
1398 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001399 rx.sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001400 sta_info_put(sta);
1401 return;
1402 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001403
Johannes Berg8e6f00322007-07-27 15:43:22 +02001404 bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001405
Johannes Berg8e6f00322007-07-27 15:43:22 +02001406 read_lock(&local->sub_if_lock);
1407 list_for_each_entry(sdata, &local->sub_if_list, list) {
1408 rx.u.rx.ra_match = 1;
Johannes Berg23a24de2007-07-27 15:43:22 +02001409
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001410 if (!netif_running(sdata->dev))
1411 continue;
1412
Johannes Berg23a24de2007-07-27 15:43:22 +02001413 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1414 /* prepare_for_handlers can change sta */
1415 sta = rx.sta;
1416
1417 if (!prepres)
1418 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001419
Johannes Berg340e11f2007-07-27 15:43:22 +02001420 /*
1421 * frame is destined for this interface, but if it's not
1422 * also for the previous one we handle that after the
1423 * loop to avoid copying the SKB once too much
1424 */
1425
1426 if (!prev) {
1427 prev = sdata;
1428 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001429 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001430
1431 /*
1432 * frame was destined for the previous interface
1433 * so invoke RX handlers for it
1434 */
1435
1436 skb_new = skb_copy(skb, GFP_ATOMIC);
1437 if (!skb_new) {
1438 if (net_ratelimit())
1439 printk(KERN_DEBUG "%s: failed to copy "
1440 "multicast frame for %s",
1441 local->mdev->name, prev->dev->name);
1442 continue;
1443 }
1444 rx.skb = skb_new;
1445 rx.dev = prev->dev;
1446 rx.sdata = prev;
1447 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1448 &rx, sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001449 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001450 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001451 if (prev) {
1452 rx.skb = skb;
1453 rx.dev = prev->dev;
1454 rx.sdata = prev;
1455 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1456 &rx, sta);
1457 } else
1458 dev_kfree_skb(skb);
1459 read_unlock(&local->sub_if_lock);
Johannes Berg571ecf62007-07-27 15:43:22 +02001460
Johannes Berg8e6f00322007-07-27 15:43:22 +02001461 end:
Johannes Berg571ecf62007-07-27 15:43:22 +02001462 if (sta)
1463 sta_info_put(sta);
1464}
1465EXPORT_SYMBOL(__ieee80211_rx);
1466
1467/* This is a version of the rx handler that can be called from hard irq
1468 * context. Post the skb on the queue and schedule the tasklet */
1469void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1470 struct ieee80211_rx_status *status)
1471{
1472 struct ieee80211_local *local = hw_to_local(hw);
1473
1474 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1475
1476 skb->dev = local->mdev;
1477 /* copy status into skb->cb for use by tasklet */
1478 memcpy(skb->cb, status, sizeof(*status));
1479 skb->pkt_type = IEEE80211_RX_MSG;
1480 skb_queue_tail(&local->skb_queue, skb);
1481 tasklet_schedule(&local->tasklet);
1482}
1483EXPORT_SYMBOL(ieee80211_rx_irqsafe);