blob: 21d401523d13cf0a4d200ebe4f83889c7f8977fe [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>
16#include <net/iw_handler.h>
17#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
22#include "ieee80211_common.h"
23#include "wep.h"
24#include "wpa.h"
25#include "tkip.h"
26#include "wme.h"
27
28/* pre-rx handlers
29 *
30 * these don't have dev/sdata fields in the rx data
31 */
32
33static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +020034ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
35{
36 u8 *data = rx->skb->data;
37 int tid;
38
39 /* does the frame have a qos control field? */
40 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
41 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
42 /* frame has qos control */
43 tid = qc[0] & QOS_CONTROL_TID_MASK;
44 } else {
45 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
46 /* Separate TID for management frames */
47 tid = NUM_RX_DATA_QUEUES - 1;
48 } else {
49 /* no qos control present */
50 tid = 0; /* 802.1d - Best Effort */
51 }
52 }
53#ifdef CONFIG_MAC80211_DEBUG_COUNTERS
54 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
55 if (rx->sta) {
56 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
57 }
58#endif /* CONFIG_MAC80211_DEBUG_COUNTERS */
59
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;
113 if (rx->sta)
114 rx->sta->channel_use_raw += load;
115 rx->u.rx.load = load;
116
117 return TXRX_CONTINUE;
118}
119
120ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
121{
122 ieee80211_rx_h_parse_qos,
123 ieee80211_rx_h_load_stats,
124 NULL
125};
126
127/* rx handlers */
128
129static ieee80211_txrx_result
130ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
131{
132 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;
312 int always_sta_key;
313
Johannes Berg571ecf62007-07-27 15:43:22 +0200314 if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
315 always_sta_key = 0;
316 else
317 always_sta_key = 1;
318
319 if (rx->sta && rx->sta->key && always_sta_key) {
320 rx->key = rx->sta->key;
321 } else {
322 if (rx->sta && rx->sta->key)
323 rx->key = rx->sta->key;
324 else
325 rx->key = rx->sdata->default_key;
326
327 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
328 rx->fc & IEEE80211_FCTL_PROTECTED) {
329 int keyidx = ieee80211_wep_get_keyidx(rx->skb);
330
331 if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
332 (!rx->sta || !rx->sta->key || keyidx > 0))
333 rx->key = rx->sdata->keys[keyidx];
334
335 if (!rx->key) {
336 if (!rx->u.rx.ra_match)
337 return TXRX_DROP;
338 printk(KERN_DEBUG "%s: RX WEP frame with "
339 "unknown keyidx %d (A1=" MAC_FMT " A2="
340 MAC_FMT " A3=" MAC_FMT ")\n",
341 rx->dev->name, keyidx,
342 MAC_ARG(hdr->addr1),
343 MAC_ARG(hdr->addr2),
344 MAC_ARG(hdr->addr3));
345 if (!rx->local->apdev)
346 return TXRX_DROP;
347 ieee80211_rx_mgmt(
348 rx->local, rx->skb, rx->u.rx.status,
349 ieee80211_msg_wep_frame_unknown_key);
350 return TXRX_QUEUED;
351 }
352 }
353 }
354
355 if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
356 rx->key->tx_rx_count++;
357 if (unlikely(rx->local->key_tx_rx_threshold &&
358 rx->key->tx_rx_count >
359 rx->local->key_tx_rx_threshold)) {
360 ieee80211_key_threshold_notify(rx->dev, rx->key,
361 rx->sta);
362 }
363 }
364
365 return TXRX_CONTINUE;
366}
367
368static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
369{
370 struct ieee80211_sub_if_data *sdata;
371 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
372
373 if (sdata->bss)
374 atomic_inc(&sdata->bss->num_sta_ps);
375 sta->flags |= WLAN_STA_PS;
376 sta->pspoll = 0;
377#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
378 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
379 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
380#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
381}
382
383static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
384{
385 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
386 struct sk_buff *skb;
387 int sent = 0;
388 struct ieee80211_sub_if_data *sdata;
389 struct ieee80211_tx_packet_data *pkt_data;
390
391 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
392 if (sdata->bss)
393 atomic_dec(&sdata->bss->num_sta_ps);
394 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
395 sta->pspoll = 0;
396 if (!skb_queue_empty(&sta->ps_tx_buf)) {
397 if (local->ops->set_tim)
398 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
399 if (sdata->bss)
400 bss_tim_clear(local, sdata->bss, sta->aid);
401 }
402#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
403 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
404 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
405#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
406 /* Send all buffered frames to the station */
407 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
408 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
409 sent++;
410 pkt_data->requeue = 1;
411 dev_queue_xmit(skb);
412 }
413 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
414 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
415 local->total_ps_buffered--;
416 sent++;
417#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
418 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
419 "since STA not sleeping anymore\n", dev->name,
420 MAC_ARG(sta->addr), sta->aid);
421#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
422 pkt_data->requeue = 1;
423 dev_queue_xmit(skb);
424 }
425
426 return sent;
427}
428
429static ieee80211_txrx_result
430ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
431{
432 struct sta_info *sta = rx->sta;
433 struct net_device *dev = rx->dev;
434 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
435
436 if (!sta)
437 return TXRX_CONTINUE;
438
439 /* Update last_rx only for IBSS packets which are for the current
440 * BSSID to avoid keeping the current IBSS network alive in cases where
441 * other STAs are using different BSSID. */
442 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
443 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
444 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
445 sta->last_rx = jiffies;
446 } else
447 if (!is_multicast_ether_addr(hdr->addr1) ||
448 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
449 /* Update last_rx only for unicast frames in order to prevent
450 * the Probe Request frames (the only broadcast frames from a
451 * STA in infrastructure mode) from keeping a connection alive.
452 */
453 sta->last_rx = jiffies;
454 }
455
456 if (!rx->u.rx.ra_match)
457 return TXRX_CONTINUE;
458
459 sta->rx_fragments++;
460 sta->rx_bytes += rx->skb->len;
461 sta->last_rssi = (sta->last_rssi * 15 +
462 rx->u.rx.status->ssi) / 16;
463 sta->last_signal = (sta->last_signal * 15 +
464 rx->u.rx.status->signal) / 16;
465 sta->last_noise = (sta->last_noise * 15 +
466 rx->u.rx.status->noise) / 16;
467
468 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
469 /* Change STA power saving mode only in the end of a frame
470 * exchange sequence */
471 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
472 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
473 else if (!(sta->flags & WLAN_STA_PS) &&
474 (rx->fc & IEEE80211_FCTL_PM))
475 ap_sta_ps_start(dev, sta);
476 }
477
478 /* Drop data::nullfunc frames silently, since they are used only to
479 * control station power saving mode. */
480 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
481 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
482 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
483 /* Update counter and free packet here to avoid counting this
484 * as a dropped packed. */
485 sta->rx_packets++;
486 dev_kfree_skb(rx->skb);
487 return TXRX_QUEUED;
488 }
489
490 return TXRX_CONTINUE;
491} /* ieee80211_rx_h_sta_process */
492
493static ieee80211_txrx_result
494ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
495{
496 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
497 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
498 !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
499 return TXRX_CONTINUE;
500
501 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
502 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
503 rx->key->force_sw_encrypt) {
504 u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
505 if (iv) {
506 rx->sta->wep_weak_iv_count++;
507 }
508 }
509
510 return TXRX_CONTINUE;
511}
512
513static ieee80211_txrx_result
514ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
515{
516 /* If the device handles decryption totally, skip this test */
517 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
518 return TXRX_CONTINUE;
519
520 if ((rx->key && rx->key->alg != ALG_WEP) ||
521 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
522 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
523 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
524 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
525 return TXRX_CONTINUE;
526
527 if (!rx->key) {
528 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
529 rx->dev->name);
530 return TXRX_DROP;
531 }
532
533 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
534 rx->key->force_sw_encrypt) {
535 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
536 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
537 "failed\n", rx->dev->name);
538 return TXRX_DROP;
539 }
540 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
541 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
542 /* remove ICV */
543 skb_trim(rx->skb, rx->skb->len - 4);
544 }
545
546 return TXRX_CONTINUE;
547}
548
549static inline struct ieee80211_fragment_entry *
550ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
551 unsigned int frag, unsigned int seq, int rx_queue,
552 struct sk_buff **skb)
553{
554 struct ieee80211_fragment_entry *entry;
555 int idx;
556
557 idx = sdata->fragment_next;
558 entry = &sdata->fragments[sdata->fragment_next++];
559 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
560 sdata->fragment_next = 0;
561
562 if (!skb_queue_empty(&entry->skb_list)) {
563#ifdef CONFIG_MAC80211_DEBUG
564 struct ieee80211_hdr *hdr =
565 (struct ieee80211_hdr *) entry->skb_list.next->data;
566 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
567 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
568 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
569 sdata->dev->name, idx,
570 jiffies - entry->first_frag_time, entry->seq,
571 entry->last_frag, MAC_ARG(hdr->addr1),
572 MAC_ARG(hdr->addr2));
573#endif /* CONFIG_MAC80211_DEBUG */
574 __skb_queue_purge(&entry->skb_list);
575 }
576
577 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
578 *skb = NULL;
579 entry->first_frag_time = jiffies;
580 entry->seq = seq;
581 entry->rx_queue = rx_queue;
582 entry->last_frag = frag;
583 entry->ccmp = 0;
584 entry->extra_len = 0;
585
586 return entry;
587}
588
589static inline struct ieee80211_fragment_entry *
590ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
591 u16 fc, unsigned int frag, unsigned int seq,
592 int rx_queue, struct ieee80211_hdr *hdr)
593{
594 struct ieee80211_fragment_entry *entry;
595 int i, idx;
596
597 idx = sdata->fragment_next;
598 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
599 struct ieee80211_hdr *f_hdr;
600 u16 f_fc;
601
602 idx--;
603 if (idx < 0)
604 idx = IEEE80211_FRAGMENT_MAX - 1;
605
606 entry = &sdata->fragments[idx];
607 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
608 entry->rx_queue != rx_queue ||
609 entry->last_frag + 1 != frag)
610 continue;
611
612 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
613 f_fc = le16_to_cpu(f_hdr->frame_control);
614
615 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
616 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
617 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
618 continue;
619
620 if (entry->first_frag_time + 2 * HZ < jiffies) {
621 __skb_queue_purge(&entry->skb_list);
622 continue;
623 }
624 return entry;
625 }
626
627 return NULL;
628}
629
630static ieee80211_txrx_result
631ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
632{
633 struct ieee80211_hdr *hdr;
634 u16 sc;
635 unsigned int frag, seq;
636 struct ieee80211_fragment_entry *entry;
637 struct sk_buff *skb;
638
639 hdr = (struct ieee80211_hdr *) rx->skb->data;
640 sc = le16_to_cpu(hdr->seq_ctrl);
641 frag = sc & IEEE80211_SCTL_FRAG;
642
643 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
644 (rx->skb)->len < 24 ||
645 is_multicast_ether_addr(hdr->addr1))) {
646 /* not fragmented */
647 goto out;
648 }
649 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
650
651 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
652
653 if (frag == 0) {
654 /* This is the first fragment of a new frame. */
655 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
656 rx->u.rx.queue, &(rx->skb));
657 if (rx->key && rx->key->alg == ALG_CCMP &&
658 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
659 /* Store CCMP PN so that we can verify that the next
660 * fragment has a sequential PN value. */
661 entry->ccmp = 1;
662 memcpy(entry->last_pn,
663 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
664 CCMP_PN_LEN);
665 }
666 return TXRX_QUEUED;
667 }
668
669 /* This is a fragment for a frame that should already be pending in
670 * fragment cache. Add this fragment to the end of the pending entry.
671 */
672 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
673 rx->u.rx.queue, hdr);
674 if (!entry) {
675 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
676 return TXRX_DROP;
677 }
678
679 /* Verify that MPDUs within one MSDU have sequential PN values.
680 * (IEEE 802.11i, 8.3.3.4.5) */
681 if (entry->ccmp) {
682 int i;
683 u8 pn[CCMP_PN_LEN], *rpn;
684 if (!rx->key || rx->key->alg != ALG_CCMP)
685 return TXRX_DROP;
686 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
687 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
688 pn[i]++;
689 if (pn[i])
690 break;
691 }
692 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
693 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
694 printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
695 " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
696 "(expected %02x%02x%02x%02x%02x%02x)\n",
697 rx->dev->name, MAC_ARG(hdr->addr2),
698 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
699 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
700 return TXRX_DROP;
701 }
702 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
703 }
704
705 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
706 __skb_queue_tail(&entry->skb_list, rx->skb);
707 entry->last_frag = frag;
708 entry->extra_len += rx->skb->len;
709 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
710 rx->skb = NULL;
711 return TXRX_QUEUED;
712 }
713
714 rx->skb = __skb_dequeue(&entry->skb_list);
715 if (skb_tailroom(rx->skb) < entry->extra_len) {
716 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
717 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
718 GFP_ATOMIC))) {
719 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
720 __skb_queue_purge(&entry->skb_list);
721 return TXRX_DROP;
722 }
723 }
724 while ((skb = __skb_dequeue(&entry->skb_list))) {
725 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
726 dev_kfree_skb(skb);
727 }
728
729 /* Complete frame has been reassembled - process it now */
730 rx->fragmented = 1;
731
732 out:
733 if (rx->sta)
734 rx->sta->rx_packets++;
735 if (is_multicast_ether_addr(hdr->addr1))
736 rx->local->dot11MulticastReceivedFrameCount++;
737 else
738 ieee80211_led_rx(rx->local);
739 return TXRX_CONTINUE;
740}
741
742static ieee80211_txrx_result
743ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
744{
745 struct sk_buff *skb;
746 int no_pending_pkts;
747
748 if (likely(!rx->sta ||
749 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
750 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
751 !rx->u.rx.ra_match))
752 return TXRX_CONTINUE;
753
754 skb = skb_dequeue(&rx->sta->tx_filtered);
755 if (!skb) {
756 skb = skb_dequeue(&rx->sta->ps_tx_buf);
757 if (skb)
758 rx->local->total_ps_buffered--;
759 }
760 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
761 skb_queue_empty(&rx->sta->ps_tx_buf);
762
763 if (skb) {
764 struct ieee80211_hdr *hdr =
765 (struct ieee80211_hdr *) skb->data;
766
767 /* tell TX path to send one frame even though the STA may
768 * still remain is PS mode after this frame exchange */
769 rx->sta->pspoll = 1;
770
771#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
772 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
773 "after %d)\n",
774 MAC_ARG(rx->sta->addr), rx->sta->aid,
775 skb_queue_len(&rx->sta->ps_tx_buf));
776#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
777
778 /* Use MoreData flag to indicate whether there are more
779 * buffered frames for this STA */
780 if (no_pending_pkts) {
781 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
782 rx->sta->flags &= ~WLAN_STA_TIM;
783 } else
784 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
785
786 dev_queue_xmit(skb);
787
788 if (no_pending_pkts) {
789 if (rx->local->ops->set_tim)
790 rx->local->ops->set_tim(local_to_hw(rx->local),
791 rx->sta->aid, 0);
792 if (rx->sdata->bss)
793 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
794 }
795#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
796 } else if (!rx->u.rx.sent_ps_buffered) {
797 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
798 "though there is no buffered frames for it\n",
799 rx->dev->name, MAC_ARG(rx->sta->addr));
800#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
801
802 }
803
804 /* Free PS Poll skb here instead of returning TXRX_DROP that would
805 * count as an dropped frame. */
806 dev_kfree_skb(rx->skb);
807
808 return TXRX_QUEUED;
809}
810
811static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200812ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
813{
814 u16 fc = rx->fc;
815 u8 *data = rx->skb->data;
816 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
817
818 if (!WLAN_FC_IS_QOS_DATA(fc))
819 return TXRX_CONTINUE;
820
821 /* remove the qos control field, update frame type and meta-data */
822 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
823 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
824 /* change frame type to non QOS */
825 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
826 hdr->frame_control = cpu_to_le16(fc);
827
828 return TXRX_CONTINUE;
829}
830
831static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200832ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
833{
834 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
835 rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
836 /* Pass both encrypted and unencrypted EAPOL frames to user
837 * space for processing. */
838 if (!rx->local->apdev)
839 return TXRX_DROP;
840 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
841 ieee80211_msg_normal);
842 return TXRX_QUEUED;
843 }
844
845 if (unlikely(rx->sdata->ieee802_1x &&
846 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
847 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
848 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
849 !ieee80211_is_eapol(rx->skb))) {
850#ifdef CONFIG_MAC80211_DEBUG
851 struct ieee80211_hdr *hdr =
852 (struct ieee80211_hdr *) rx->skb->data;
853 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
854 " (unauthorized port)\n", rx->dev->name,
855 MAC_ARG(hdr->addr2));
856#endif /* CONFIG_MAC80211_DEBUG */
857 return TXRX_DROP;
858 }
859
860 return TXRX_CONTINUE;
861}
862
863static ieee80211_txrx_result
864ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
865{
866 /* If the device handles decryption totally, skip this test */
867 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
868 return TXRX_CONTINUE;
869
870 /* Drop unencrypted frames if key is set. */
871 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
872 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
873 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
874 (rx->key || rx->sdata->drop_unencrypted) &&
875 (rx->sdata->eapol == 0 ||
876 !ieee80211_is_eapol(rx->skb)))) {
877 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
878 "encryption\n", rx->dev->name);
879 return TXRX_DROP;
880 }
881 return TXRX_CONTINUE;
882}
883
884static ieee80211_txrx_result
885ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
886{
887 struct net_device *dev = rx->dev;
888 struct ieee80211_local *local = rx->local;
889 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
890 u16 fc, hdrlen, ethertype;
891 u8 *payload;
892 u8 dst[ETH_ALEN];
893 u8 src[ETH_ALEN];
894 struct sk_buff *skb = rx->skb, *skb2;
895 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
896
897 fc = rx->fc;
898 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
899 return TXRX_CONTINUE;
900
901 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
902 return TXRX_DROP;
903
904 hdrlen = ieee80211_get_hdrlen(fc);
905
906 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
907 * header
908 * IEEE 802.11 address fields:
909 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
910 * 0 0 DA SA BSSID n/a
911 * 0 1 DA BSSID SA n/a
912 * 1 0 BSSID SA DA n/a
913 * 1 1 RA TA DA SA
914 */
915
916 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
917 case IEEE80211_FCTL_TODS:
918 /* BSSID SA DA */
919 memcpy(dst, hdr->addr3, ETH_ALEN);
920 memcpy(src, hdr->addr2, ETH_ALEN);
921
922 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
923 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
924 printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
925 MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
926 dev->name, MAC_ARG(hdr->addr1),
927 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
928 return TXRX_DROP;
929 }
930 break;
931 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
932 /* RA TA DA SA */
933 memcpy(dst, hdr->addr3, ETH_ALEN);
934 memcpy(src, hdr->addr4, ETH_ALEN);
935
936 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
937 printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
938 MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
939 MAC_FMT ")\n",
940 rx->dev->name, MAC_ARG(hdr->addr1),
941 MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
942 MAC_ARG(hdr->addr4));
943 return TXRX_DROP;
944 }
945 break;
946 case IEEE80211_FCTL_FROMDS:
947 /* DA BSSID SA */
948 memcpy(dst, hdr->addr1, ETH_ALEN);
949 memcpy(src, hdr->addr3, ETH_ALEN);
950
951 if (sdata->type != IEEE80211_IF_TYPE_STA) {
952 return TXRX_DROP;
953 }
954 break;
955 case 0:
956 /* DA SA BSSID */
957 memcpy(dst, hdr->addr1, ETH_ALEN);
958 memcpy(src, hdr->addr2, ETH_ALEN);
959
960 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
961 if (net_ratelimit()) {
962 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
963 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
964 ")\n",
965 dev->name, MAC_ARG(hdr->addr1),
966 MAC_ARG(hdr->addr2),
967 MAC_ARG(hdr->addr3));
968 }
969 return TXRX_DROP;
970 }
971 break;
972 }
973
974 payload = skb->data + hdrlen;
975
976 if (unlikely(skb->len - hdrlen < 8)) {
977 if (net_ratelimit()) {
978 printk(KERN_DEBUG "%s: RX too short data frame "
979 "payload\n", dev->name);
980 }
981 return TXRX_DROP;
982 }
983
984 ethertype = (payload[6] << 8) | payload[7];
985
986 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
987 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
988 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
989 /* remove RFC1042 or Bridge-Tunnel encapsulation and
990 * replace EtherType */
991 skb_pull(skb, hdrlen + 6);
992 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
993 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
994 } else {
995 struct ethhdr *ehdr;
996 __be16 len;
997 skb_pull(skb, hdrlen);
998 len = htons(skb->len);
999 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1000 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1001 memcpy(ehdr->h_source, src, ETH_ALEN);
1002 ehdr->h_proto = len;
1003 }
1004 skb->dev = dev;
1005
1006 skb2 = NULL;
1007
1008 sdata->stats.rx_packets++;
1009 sdata->stats.rx_bytes += skb->len;
1010
1011 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
1012 || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
1013 if (is_multicast_ether_addr(skb->data)) {
1014 /* send multicast frames both to higher layers in
1015 * local net stack and back to the wireless media */
1016 skb2 = skb_copy(skb, GFP_ATOMIC);
1017 if (!skb2)
1018 printk(KERN_DEBUG "%s: failed to clone "
1019 "multicast frame\n", dev->name);
1020 } else {
1021 struct sta_info *dsta;
1022 dsta = sta_info_get(local, skb->data);
1023 if (dsta && !dsta->dev) {
1024 printk(KERN_DEBUG "Station with null dev "
1025 "structure!\n");
1026 } else if (dsta && dsta->dev == dev) {
1027 /* Destination station is associated to this
1028 * AP, so send the frame directly to it and
1029 * do not pass the frame to local net stack.
1030 */
1031 skb2 = skb;
1032 skb = NULL;
1033 }
1034 if (dsta)
1035 sta_info_put(dsta);
1036 }
1037 }
1038
1039 if (skb) {
1040 /* deliver to local stack */
1041 skb->protocol = eth_type_trans(skb, dev);
1042 memset(skb->cb, 0, sizeof(skb->cb));
1043 netif_rx(skb);
1044 }
1045
1046 if (skb2) {
1047 /* send to wireless media */
1048 skb2->protocol = __constant_htons(ETH_P_802_3);
1049 skb_set_network_header(skb2, 0);
1050 skb_set_mac_header(skb2, 0);
1051 dev_queue_xmit(skb2);
1052 }
1053
1054 return TXRX_QUEUED;
1055}
1056
1057static ieee80211_txrx_result
1058ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1059{
1060 struct ieee80211_sub_if_data *sdata;
1061
1062 if (!rx->u.rx.ra_match)
1063 return TXRX_DROP;
1064
1065 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1066 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1067 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1068 !rx->local->user_space_mlme) {
1069 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1070 } else {
1071 /* Management frames are sent to hostapd for processing */
1072 if (!rx->local->apdev)
1073 return TXRX_DROP;
1074 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
1075 ieee80211_msg_normal);
1076 }
1077 return TXRX_QUEUED;
1078}
1079
1080static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1081 struct ieee80211_local *local,
1082 ieee80211_rx_handler *handlers,
1083 struct ieee80211_txrx_data *rx,
1084 struct sta_info *sta)
1085{
1086 ieee80211_rx_handler *handler;
1087 ieee80211_txrx_result res = TXRX_DROP;
1088
1089 for (handler = handlers; *handler != NULL; handler++) {
1090 res = (*handler)(rx);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001091
1092 switch (res) {
1093 case TXRX_CONTINUE:
1094 continue;
1095 case TXRX_DROP:
1096 I802_DEBUG_INC(local->rx_handlers_drop);
1097 if (sta)
1098 sta->rx_dropped++;
1099 break;
1100 case TXRX_QUEUED:
1101 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001102 break;
1103 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001104 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001105 }
1106
Johannes Berg8e6f0032007-07-27 15:43:22 +02001107 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001108 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001109 return res;
1110}
1111
1112static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1113 ieee80211_rx_handler *handlers,
1114 struct ieee80211_txrx_data *rx,
1115 struct sta_info *sta)
1116{
1117 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1118 TXRX_CONTINUE)
1119 dev_kfree_skb(rx->skb);
1120}
1121
1122static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1123 struct ieee80211_hdr *hdr,
1124 struct sta_info *sta,
1125 struct ieee80211_txrx_data *rx)
1126{
1127 int keyidx, hdrlen;
1128
1129 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1130 if (rx->skb->len >= hdrlen + 4)
1131 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1132 else
1133 keyidx = -1;
1134
1135 /* TODO: verify that this is not triggered by fragmented
1136 * frames (hw does not verify MIC for them). */
1137 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1138 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
1139 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
1140
1141 if (!sta) {
1142 /* Some hardware versions seem to generate incorrect
1143 * Michael MIC reports; ignore them to avoid triggering
1144 * countermeasures. */
1145 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1146 "error for unknown address " MAC_FMT "\n",
1147 dev->name, MAC_ARG(hdr->addr2));
1148 goto ignore;
1149 }
1150
1151 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1152 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1153 "error for a frame with no ISWEP flag (src "
1154 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
1155 goto ignore;
1156 }
1157
1158 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
1159 rx->sdata->type == IEEE80211_IF_TYPE_AP) {
1160 keyidx = ieee80211_wep_get_keyidx(rx->skb);
1161 /* AP with Pairwise keys support should never receive Michael
1162 * MIC errors for non-zero keyidx because these are reserved
1163 * for group keys and only the AP is sending real multicast
1164 * frames in BSS. */
1165 if (keyidx) {
1166 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1167 "a frame with non-zero keyidx (%d) (src " MAC_FMT
1168 ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
1169 goto ignore;
1170 }
1171 }
1172
1173 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1174 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1175 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1176 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1177 "error for a frame that cannot be encrypted "
1178 "(fc=0x%04x) (src " MAC_FMT ")\n",
1179 dev->name, rx->fc, MAC_ARG(hdr->addr2));
1180 goto ignore;
1181 }
1182
1183 do {
1184 union iwreq_data wrqu;
1185 char *buf = kmalloc(128, GFP_ATOMIC);
1186 if (!buf)
1187 break;
1188
1189 /* TODO: needed parameters: count, key type, TSC */
1190 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
1191 "keyid=%d %scast addr=" MAC_FMT ")",
1192 keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
1193 MAC_ARG(hdr->addr2));
1194 memset(&wrqu, 0, sizeof(wrqu));
1195 wrqu.data.length = strlen(buf);
1196 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
1197 kfree(buf);
1198 } while (0);
1199
1200 /* TODO: consider verifying the MIC error report with software
1201 * implementation if we get too many spurious reports from the
1202 * hardware. */
1203 if (!rx->local->apdev)
1204 goto ignore;
1205 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
1206 ieee80211_msg_michael_mic_failure);
1207 return;
1208
1209 ignore:
1210 dev_kfree_skb(rx->skb);
1211 rx->skb = NULL;
1212}
1213
1214ieee80211_rx_handler ieee80211_rx_handlers[] =
1215{
1216 ieee80211_rx_h_if_stats,
1217 ieee80211_rx_h_monitor,
1218 ieee80211_rx_h_passive_scan,
1219 ieee80211_rx_h_check,
Johannes Berg570bd532007-07-27 15:43:22 +02001220 ieee80211_rx_h_load_key,
Johannes Berg571ecf62007-07-27 15:43:22 +02001221 ieee80211_rx_h_sta_process,
1222 ieee80211_rx_h_ccmp_decrypt,
1223 ieee80211_rx_h_tkip_decrypt,
1224 ieee80211_rx_h_wep_weak_iv_detection,
1225 ieee80211_rx_h_wep_decrypt,
1226 ieee80211_rx_h_defragment,
1227 ieee80211_rx_h_ps_poll,
1228 ieee80211_rx_h_michael_mic_verify,
1229 /* this must be after decryption - so header is counted in MPDU mic
1230 * must be before pae and data, so QOS_DATA format frames
1231 * are not passed to user space by these functions
1232 */
1233 ieee80211_rx_h_remove_qos_control,
1234 ieee80211_rx_h_802_1x_pae,
1235 ieee80211_rx_h_drop_unencrypted,
1236 ieee80211_rx_h_data,
1237 ieee80211_rx_h_mgmt,
1238 NULL
1239};
1240
1241/* main receive path */
1242
Johannes Berg23a24de2007-07-27 15:43:22 +02001243static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1244 u8 *bssid, struct ieee80211_txrx_data *rx,
1245 struct ieee80211_hdr *hdr)
1246{
1247 int multicast = is_multicast_ether_addr(hdr->addr1);
1248
1249 switch (sdata->type) {
1250 case IEEE80211_IF_TYPE_STA:
1251 if (!bssid)
1252 return 0;
1253 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1254 if (!rx->u.rx.in_scan)
1255 return 0;
1256 rx->u.rx.ra_match = 0;
1257 } else if (!multicast &&
1258 compare_ether_addr(sdata->dev->dev_addr,
1259 hdr->addr1) != 0) {
1260 if (!sdata->promisc)
1261 return 0;
1262 rx->u.rx.ra_match = 0;
1263 }
1264 break;
1265 case IEEE80211_IF_TYPE_IBSS:
1266 if (!bssid)
1267 return 0;
1268 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1269 if (!rx->u.rx.in_scan)
1270 return 0;
1271 rx->u.rx.ra_match = 0;
1272 } else if (!multicast &&
1273 compare_ether_addr(sdata->dev->dev_addr,
1274 hdr->addr1) != 0) {
1275 if (!sdata->promisc)
1276 return 0;
1277 rx->u.rx.ra_match = 0;
1278 } else if (!rx->sta)
1279 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1280 bssid, hdr->addr2);
1281 break;
1282 case IEEE80211_IF_TYPE_AP:
1283 if (!bssid) {
1284 if (compare_ether_addr(sdata->dev->dev_addr,
1285 hdr->addr1))
1286 return 0;
1287 } else if (!ieee80211_bssid_match(bssid,
1288 sdata->dev->dev_addr)) {
1289 if (!rx->u.rx.in_scan)
1290 return 0;
1291 rx->u.rx.ra_match = 0;
1292 }
1293 if (sdata->dev == sdata->local->mdev && !rx->u.rx.in_scan)
1294 /* do not receive anything via
1295 * master device when not scanning */
1296 return 0;
1297 break;
1298 case IEEE80211_IF_TYPE_WDS:
1299 if (bssid ||
1300 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1301 return 0;
1302 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1303 return 0;
1304 break;
1305 }
1306
1307 return 1;
1308}
1309
Johannes Berg571ecf62007-07-27 15:43:22 +02001310/*
1311 * This is the receive path handler. It is called by a low level driver when an
1312 * 802.11 MPDU is received from the hardware.
1313 */
1314void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1315 struct ieee80211_rx_status *status)
1316{
1317 struct ieee80211_local *local = hw_to_local(hw);
1318 struct ieee80211_sub_if_data *sdata;
1319 struct sta_info *sta;
1320 struct ieee80211_hdr *hdr;
1321 struct ieee80211_txrx_data rx;
1322 u16 type;
Johannes Berg23a24de2007-07-27 15:43:22 +02001323 int radiotap_len = 0, prepres;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001324 struct ieee80211_sub_if_data *prev = NULL;
1325 struct sk_buff *skb_new;
1326 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001327
1328 if (status->flag & RX_FLAG_RADIOTAP) {
1329 radiotap_len = ieee80211_get_radiotap_len(skb->data);
1330 skb_pull(skb, radiotap_len);
1331 }
1332
1333 hdr = (struct ieee80211_hdr *) skb->data;
1334 memset(&rx, 0, sizeof(rx));
1335 rx.skb = skb;
1336 rx.local = local;
1337
1338 rx.u.rx.status = status;
1339 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
1340 type = rx.fc & IEEE80211_FCTL_FTYPE;
1341 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1342 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001343
Johannes Berg23a24de2007-07-27 15:43:22 +02001344 if (skb->len >= 16) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001345 sta = rx.sta = sta_info_get(local, hdr->addr2);
Johannes Berg23a24de2007-07-27 15:43:22 +02001346 if (sta) {
1347 rx.dev = rx.sta->dev;
1348 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1349 }
1350 } else
Johannes Berg571ecf62007-07-27 15:43:22 +02001351 sta = rx.sta = NULL;
1352
Johannes Berg571ecf62007-07-27 15:43:22 +02001353 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1354 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1355 goto end;
1356 }
1357
1358 if (unlikely(local->sta_scanning))
1359 rx.u.rx.in_scan = 1;
1360
1361 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1362 sta) != TXRX_CONTINUE)
1363 goto end;
1364 skb = rx.skb;
1365
1366 skb_push(skb, radiotap_len);
1367 if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
Johannes Berg23a24de2007-07-27 15:43:22 +02001368 !local->iff_promiscs && !is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001369 rx.u.rx.ra_match = 1;
1370 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001371 rx.sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001372 sta_info_put(sta);
1373 return;
1374 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001375
Johannes Berg8e6f0032007-07-27 15:43:22 +02001376 bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001377
Johannes Berg8e6f0032007-07-27 15:43:22 +02001378 read_lock(&local->sub_if_lock);
1379 list_for_each_entry(sdata, &local->sub_if_list, list) {
1380 rx.u.rx.ra_match = 1;
Johannes Berg23a24de2007-07-27 15:43:22 +02001381
1382 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1383 /* prepare_for_handlers can change sta */
1384 sta = rx.sta;
1385
1386 if (!prepres)
1387 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001388
Johannes Berg571ecf62007-07-27 15:43:22 +02001389 if (prev) {
Johannes Berg8e6f0032007-07-27 15:43:22 +02001390 skb_new = skb_copy(skb, GFP_ATOMIC);
1391 if (!skb_new) {
1392 if (net_ratelimit())
1393 printk(KERN_DEBUG "%s: failed to copy "
1394 "multicast frame for %s",
1395 local->mdev->name, prev->dev->name);
1396 continue;
1397 }
1398 rx.skb = skb_new;
Johannes Berg571ecf62007-07-27 15:43:22 +02001399 rx.dev = prev->dev;
1400 rx.sdata = prev;
1401 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1402 &rx, sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001403 }
1404 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001405 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001406 if (prev) {
1407 rx.skb = skb;
1408 rx.dev = prev->dev;
1409 rx.sdata = prev;
1410 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1411 &rx, sta);
1412 } else
1413 dev_kfree_skb(skb);
1414 read_unlock(&local->sub_if_lock);
Johannes Berg571ecf62007-07-27 15:43:22 +02001415
Johannes Berg8e6f0032007-07-27 15:43:22 +02001416 end:
Johannes Berg571ecf62007-07-27 15:43:22 +02001417 if (sta)
1418 sta_info_put(sta);
1419}
1420EXPORT_SYMBOL(__ieee80211_rx);
1421
1422/* This is a version of the rx handler that can be called from hard irq
1423 * context. Post the skb on the queue and schedule the tasklet */
1424void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1425 struct ieee80211_rx_status *status)
1426{
1427 struct ieee80211_local *local = hw_to_local(hw);
1428
1429 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1430
1431 skb->dev = local->mdev;
1432 /* copy status into skb->cb for use by tasklet */
1433 memcpy(skb->cb, status, sizeof(*status));
1434 skb->pkt_type = IEEE80211_RX_MSG;
1435 skb_queue_tail(&local->skb_queue, skb);
1436 tasklet_schedule(&local->tasklet);
1437}
1438EXPORT_SYMBOL(ieee80211_rx_irqsafe);