blob: 91b7886bf797f4c64d9db3206d3e56f1a613db9b [file] [log] [blame]
Johannes Berg571ecf62007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040016#include <linux/rcupdate.h>
Johannes Berg571ecf62007-07-27 15:43:22 +020017#include <net/mac80211.h>
18#include <net/ieee80211_radiotap.h>
19
20#include "ieee80211_i.h"
21#include "ieee80211_led.h"
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
Johannes Berg52865dfd2007-07-27 15:43:22 +020031 * The sta value should also not be used because it may
32 * be NULL even though a STA (in IBSS mode) will be added.
Johannes Berg571ecf62007-07-27 15:43:22 +020033 */
34
35static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +020036ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
37{
38 u8 *data = rx->skb->data;
39 int tid;
40
41 /* does the frame have a qos control field? */
42 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
43 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
44 /* frame has qos control */
45 tid = qc[0] & QOS_CONTROL_TID_MASK;
46 } else {
47 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
48 /* Separate TID for management frames */
49 tid = NUM_RX_DATA_QUEUES - 1;
50 } else {
51 /* no qos control present */
52 tid = 0; /* 802.1d - Best Effort */
53 }
54 }
Johannes Berg52865dfd2007-07-27 15:43:22 +020055
Johannes Berg6e0d1142007-07-27 15:43:22 +020056 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865dfd2007-07-27 15:43:22 +020057 /* only a debug counter, sta might not be assigned properly yet */
58 if (rx->sta)
Johannes Berg6e0d1142007-07-27 15:43:22 +020059 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
Johannes Berg6e0d1142007-07-27 15:43:22 +020060
61 rx->u.rx.queue = tid;
62 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
63 * For now, set skb->priority to 0 for other cases. */
64 rx->skb->priority = (tid > 7) ? 0 : tid;
65
66 return TXRX_CONTINUE;
67}
68
69static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +020070ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
71{
72 struct ieee80211_local *local = rx->local;
73 struct sk_buff *skb = rx->skb;
74 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
75 u32 load = 0, hdrtime;
76 struct ieee80211_rate *rate;
77 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
78 int i;
79
80 /* Estimate total channel use caused by this frame */
81
82 if (unlikely(mode->num_rates < 0))
83 return TXRX_CONTINUE;
84
85 rate = &mode->rates[0];
86 for (i = 0; i < mode->num_rates; i++) {
87 if (mode->rates[i].val == rx->u.rx.status->rate) {
88 rate = &mode->rates[i];
89 break;
90 }
91 }
92
93 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
94 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
95
96 if (mode->mode == MODE_IEEE80211A ||
97 mode->mode == MODE_ATHEROS_TURBO ||
98 mode->mode == MODE_ATHEROS_TURBOG ||
99 (mode->mode == MODE_IEEE80211G &&
100 rate->flags & IEEE80211_RATE_ERP))
101 hdrtime = CHAN_UTIL_HDR_SHORT;
102 else
103 hdrtime = CHAN_UTIL_HDR_LONG;
104
105 load = hdrtime;
106 if (!is_multicast_ether_addr(hdr->addr1))
107 load += hdrtime;
108
109 load += skb->len * rate->rate_inv;
110
111 /* Divide channel_use by 8 to avoid wrapping around the counter */
112 load >>= CHAN_UTIL_SHIFT;
113 local->channel_use_raw += load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200114 rx->u.rx.load = load;
115
116 return TXRX_CONTINUE;
117}
118
119ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
120{
121 ieee80211_rx_h_parse_qos,
122 ieee80211_rx_h_load_stats,
123 NULL
124};
125
126/* rx handlers */
127
128static ieee80211_txrx_result
129ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
130{
Johannes Berg52865dfd2007-07-27 15:43:22 +0200131 if (rx->sta)
132 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200133 rx->sdata->channel_use_raw += rx->u.rx.load;
134 return TXRX_CONTINUE;
135}
136
137static void
138ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
139 struct ieee80211_rx_status *status)
140{
141 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
142 struct ieee80211_sub_if_data *sdata;
143 struct ieee80211_rate *rate;
144 struct ieee80211_rtap_hdr {
145 struct ieee80211_radiotap_header hdr;
146 u8 flags;
147 u8 rate;
148 __le16 chan_freq;
149 __le16 chan_flags;
150 u8 antsignal;
151 } __attribute__ ((packed)) *rthdr;
152
153 skb->dev = dev;
154
155 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
156
157 if (status->flag & RX_FLAG_RADIOTAP)
158 goto out;
159
160 if (skb_headroom(skb) < sizeof(*rthdr)) {
161 I802_DEBUG_INC(local->rx_expand_skb_head);
162 if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
163 dev_kfree_skb(skb);
164 return;
165 }
166 }
167
168 rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
169 memset(rthdr, 0, sizeof(*rthdr));
170 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
171 rthdr->hdr.it_present =
172 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
173 (1 << IEEE80211_RADIOTAP_RATE) |
174 (1 << IEEE80211_RADIOTAP_CHANNEL) |
175 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
176 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
177 IEEE80211_RADIOTAP_F_FCS : 0;
178 rate = ieee80211_get_rate(local, status->phymode, status->rate);
179 if (rate)
180 rthdr->rate = rate->rate / 5;
181 rthdr->chan_freq = cpu_to_le16(status->freq);
182 rthdr->chan_flags =
183 status->phymode == MODE_IEEE80211A ?
184 cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
185 cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
186 rthdr->antsignal = status->ssi;
187
188 out:
189 sdata->stats.rx_packets++;
190 sdata->stats.rx_bytes += skb->len;
191
192 skb_set_mac_header(skb, 0);
193 skb->ip_summed = CHECKSUM_UNNECESSARY;
194 skb->pkt_type = PACKET_OTHERHOST;
195 skb->protocol = htons(ETH_P_802_2);
196 memset(skb->cb, 0, sizeof(skb->cb));
197 netif_rx(skb);
198}
199
200static ieee80211_txrx_result
201ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
202{
203 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
204 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
205 return TXRX_QUEUED;
206 }
207
208 if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
209 skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb->data));
210
211 return TXRX_CONTINUE;
212}
213
214static ieee80211_txrx_result
215ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
216{
217 struct ieee80211_local *local = rx->local;
218 struct sk_buff *skb = rx->skb;
219
220 if (unlikely(local->sta_scanning != 0)) {
221 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
222 return TXRX_QUEUED;
223 }
224
Jiri Slabybadffb72007-08-28 17:01:54 -0400225 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200226 /* scanning finished during invoking of handlers */
227 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
228 return TXRX_DROP;
229 }
230
231 return TXRX_CONTINUE;
232}
233
234static ieee80211_txrx_result
235ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
236{
237 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200238 hdr = (struct ieee80211_hdr *) rx->skb->data;
239
240 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
241 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
242 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
243 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
244 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400245 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200246 rx->local->dot11FrameDuplicateCount++;
247 rx->sta->num_duplicates++;
248 }
249 return TXRX_DROP;
250 } else
251 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
252 }
253
254 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
255 rx->skb->len > FCS_LEN)
256 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
257
258 if (unlikely(rx->skb->len < 16)) {
259 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
260 return TXRX_DROP;
261 }
262
Jiri Slabybadffb72007-08-28 17:01:54 -0400263 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200264 rx->skb->pkt_type = PACKET_OTHERHOST;
265 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
266 rx->skb->pkt_type = PACKET_HOST;
267 else if (is_multicast_ether_addr(hdr->addr1)) {
268 if (is_broadcast_ether_addr(hdr->addr1))
269 rx->skb->pkt_type = PACKET_BROADCAST;
270 else
271 rx->skb->pkt_type = PACKET_MULTICAST;
272 } else
273 rx->skb->pkt_type = PACKET_OTHERHOST;
274
275 /* Drop disallowed frame classes based on STA auth/assoc state;
276 * IEEE 802.11, Chap 5.5.
277 *
278 * 80211.o does filtering only based on association state, i.e., it
279 * drops Class 3 frames from not associated stations. hostapd sends
280 * deauth/disassoc frames when needed. In addition, hostapd is
281 * responsible for filtering on both auth and assoc states.
282 */
283 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
284 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
285 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
286 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
287 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
288 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
289 !(rx->fc & IEEE80211_FCTL_TODS) &&
290 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400291 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200292 /* Drop IBSS frames and frames for other hosts
293 * silently. */
294 return TXRX_DROP;
295 }
296
297 if (!rx->local->apdev)
298 return TXRX_DROP;
299
300 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
301 ieee80211_msg_sta_not_assoc);
302 return TXRX_QUEUED;
303 }
304
Johannes Berg570bd532007-07-27 15:43:22 +0200305 return TXRX_CONTINUE;
306}
307
308
309static ieee80211_txrx_result
310ieee80211_rx_h_load_key(struct ieee80211_txrx_data *rx)
311{
312 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400313 int keyidx;
314 int hdrlen;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400315 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200316
Johannes Berg3017b802007-08-28 17:01:53 -0400317 /*
318 * Key selection 101
319 *
320 * There are three types of keys:
321 * - GTK (group keys)
322 * - PTK (pairwise keys)
323 * - STK (station-to-station pairwise keys)
324 *
325 * When selecting a key, we have to distinguish between multicast
326 * (including broadcast) and unicast frames, the latter can only
327 * use PTKs and STKs while the former always use GTKs. Unless, of
328 * course, actual WEP keys ("pre-RSNA") are used, then unicast
329 * frames can also use key indizes like GTKs. Hence, if we don't
330 * have a PTK/STK we check the key index for a WEP key.
331 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400332 * Note that in a regular BSS, multicast frames are sent by the
333 * AP only, associated stations unicast the frame to the AP first
334 * which then multicasts it on their behalf.
335 *
Johannes Berg3017b802007-08-28 17:01:53 -0400336 * There is also a slight problem in IBSS mode: GTKs are negotiated
337 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400338 * The spec seems to expect that one negotiates the same key with
339 * every station but there's no such requirement; VLANs could be
340 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400341 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200342
Johannes Berg3017b802007-08-28 17:01:53 -0400343 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
344 return TXRX_CONTINUE;
345
346 /*
347 * No point in finding a key if the frame is neither
348 * addressed to us nor a multicast frame.
349 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400350 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400351 return TXRX_CONTINUE;
352
Johannes Bergd4e46a32007-09-14 11:10:24 -0400353 if (rx->sta)
354 stakey = rcu_dereference(rx->sta->key);
355
356 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
357 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200358 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400359 /*
360 * The device doesn't give us the IV so we won't be
361 * able to look up the key. That's ok though, we
362 * don't need to decrypt the frame, we just won't
363 * be able to keep statistics accurate.
364 * Except for key threshold notifications, should
365 * we somehow allow the driver to tell us which key
366 * the hardware used if this flag is set?
367 */
368 if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
369 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200370
Johannes Berg3017b802007-08-28 17:01:53 -0400371 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200372
Johannes Berg3017b802007-08-28 17:01:53 -0400373 if (rx->skb->len < 8 + hdrlen)
374 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200375
Johannes Berg3017b802007-08-28 17:01:53 -0400376 /*
377 * no need to call ieee80211_wep_get_keyidx,
378 * it verifies a bunch of things we've done already
379 */
380 keyidx = rx->skb->data[hdrlen + 3] >> 6;
381
Johannes Bergd4e46a32007-09-14 11:10:24 -0400382 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400383
384 /*
385 * RSNA-protected unicast frames should always be sent with
386 * pairwise or station-to-station keys, but for WEP we allow
387 * using a key index as well.
388 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400389 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400390 !is_multicast_ether_addr(hdr->addr1))
391 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200392 }
393
Johannes Berg3017b802007-08-28 17:01:53 -0400394 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200395 rx->key->tx_rx_count++;
396 if (unlikely(rx->local->key_tx_rx_threshold &&
397 rx->key->tx_rx_count >
398 rx->local->key_tx_rx_threshold)) {
399 ieee80211_key_threshold_notify(rx->dev, rx->key,
400 rx->sta);
401 }
402 }
403
404 return TXRX_CONTINUE;
405}
406
407static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
408{
409 struct ieee80211_sub_if_data *sdata;
410 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
411
412 if (sdata->bss)
413 atomic_inc(&sdata->bss->num_sta_ps);
414 sta->flags |= WLAN_STA_PS;
415 sta->pspoll = 0;
416#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
417 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
418 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
419#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
420}
421
422static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
423{
424 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
425 struct sk_buff *skb;
426 int sent = 0;
427 struct ieee80211_sub_if_data *sdata;
428 struct ieee80211_tx_packet_data *pkt_data;
429
430 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
431 if (sdata->bss)
432 atomic_dec(&sdata->bss->num_sta_ps);
433 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
434 sta->pspoll = 0;
435 if (!skb_queue_empty(&sta->ps_tx_buf)) {
436 if (local->ops->set_tim)
437 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
438 if (sdata->bss)
439 bss_tim_clear(local, sdata->bss, sta->aid);
440 }
441#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
442 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
443 "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
444#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
445 /* Send all buffered frames to the station */
446 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
447 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
448 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400449 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200450 dev_queue_xmit(skb);
451 }
452 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
453 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
454 local->total_ps_buffered--;
455 sent++;
456#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
457 printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
458 "since STA not sleeping anymore\n", dev->name,
459 MAC_ARG(sta->addr), sta->aid);
460#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400461 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200462 dev_queue_xmit(skb);
463 }
464
465 return sent;
466}
467
468static ieee80211_txrx_result
469ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
470{
471 struct sta_info *sta = rx->sta;
472 struct net_device *dev = rx->dev;
473 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
474
475 if (!sta)
476 return TXRX_CONTINUE;
477
478 /* Update last_rx only for IBSS packets which are for the current
479 * BSSID to avoid keeping the current IBSS network alive in cases where
480 * other STAs are using different BSSID. */
481 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
482 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
483 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
484 sta->last_rx = jiffies;
485 } else
486 if (!is_multicast_ether_addr(hdr->addr1) ||
487 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
488 /* Update last_rx only for unicast frames in order to prevent
489 * the Probe Request frames (the only broadcast frames from a
490 * STA in infrastructure mode) from keeping a connection alive.
491 */
492 sta->last_rx = jiffies;
493 }
494
Jiri Slabybadffb72007-08-28 17:01:54 -0400495 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200496 return TXRX_CONTINUE;
497
498 sta->rx_fragments++;
499 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400500 sta->last_rssi = rx->u.rx.status->ssi;
501 sta->last_signal = rx->u.rx.status->signal;
502 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200503
504 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
505 /* Change STA power saving mode only in the end of a frame
506 * exchange sequence */
507 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
508 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
509 else if (!(sta->flags & WLAN_STA_PS) &&
510 (rx->fc & IEEE80211_FCTL_PM))
511 ap_sta_ps_start(dev, sta);
512 }
513
514 /* Drop data::nullfunc frames silently, since they are used only to
515 * control station power saving mode. */
516 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
517 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
518 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
519 /* Update counter and free packet here to avoid counting this
520 * as a dropped packed. */
521 sta->rx_packets++;
522 dev_kfree_skb(rx->skb);
523 return TXRX_QUEUED;
524 }
525
526 return TXRX_CONTINUE;
527} /* ieee80211_rx_h_sta_process */
528
529static ieee80211_txrx_result
530ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
531{
532 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
533 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400534 !rx->key || rx->key->conf.alg != ALG_WEP ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400535 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200536 return TXRX_CONTINUE;
537
538 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
539 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
Johannes Berg11a843b2007-08-28 17:01:55 -0400540 !(rx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg8f20fc22007-08-28 17:01:54 -0400541 if (ieee80211_wep_is_weak_iv(rx->skb, rx->key))
Johannes Berg571ecf62007-07-27 15:43:22 +0200542 rx->sta->wep_weak_iv_count++;
Johannes Berg571ecf62007-07-27 15:43:22 +0200543
544 return TXRX_CONTINUE;
545}
546
547static ieee80211_txrx_result
548ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
549{
Johannes Berg8f20fc22007-08-28 17:01:54 -0400550 if ((rx->key && rx->key->conf.alg != ALG_WEP) ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200551 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
552 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
553 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
554 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
555 return TXRX_CONTINUE;
556
557 if (!rx->key) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400558 if (net_ratelimit())
559 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
560 rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200561 return TXRX_DROP;
562 }
563
564 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
Johannes Berg11a843b2007-08-28 17:01:55 -0400565 !(rx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200566 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400567 if (net_ratelimit())
568 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
569 "failed\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200570 return TXRX_DROP;
571 }
572 } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
573 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
574 /* remove ICV */
575 skb_trim(rx->skb, rx->skb->len - 4);
576 }
577
578 return TXRX_CONTINUE;
579}
580
581static inline struct ieee80211_fragment_entry *
582ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
583 unsigned int frag, unsigned int seq, int rx_queue,
584 struct sk_buff **skb)
585{
586 struct ieee80211_fragment_entry *entry;
587 int idx;
588
589 idx = sdata->fragment_next;
590 entry = &sdata->fragments[sdata->fragment_next++];
591 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
592 sdata->fragment_next = 0;
593
594 if (!skb_queue_empty(&entry->skb_list)) {
595#ifdef CONFIG_MAC80211_DEBUG
596 struct ieee80211_hdr *hdr =
597 (struct ieee80211_hdr *) entry->skb_list.next->data;
598 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
599 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
600 "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
601 sdata->dev->name, idx,
602 jiffies - entry->first_frag_time, entry->seq,
603 entry->last_frag, MAC_ARG(hdr->addr1),
604 MAC_ARG(hdr->addr2));
605#endif /* CONFIG_MAC80211_DEBUG */
606 __skb_queue_purge(&entry->skb_list);
607 }
608
609 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
610 *skb = NULL;
611 entry->first_frag_time = jiffies;
612 entry->seq = seq;
613 entry->rx_queue = rx_queue;
614 entry->last_frag = frag;
615 entry->ccmp = 0;
616 entry->extra_len = 0;
617
618 return entry;
619}
620
621static inline struct ieee80211_fragment_entry *
622ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
623 u16 fc, unsigned int frag, unsigned int seq,
624 int rx_queue, struct ieee80211_hdr *hdr)
625{
626 struct ieee80211_fragment_entry *entry;
627 int i, idx;
628
629 idx = sdata->fragment_next;
630 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
631 struct ieee80211_hdr *f_hdr;
632 u16 f_fc;
633
634 idx--;
635 if (idx < 0)
636 idx = IEEE80211_FRAGMENT_MAX - 1;
637
638 entry = &sdata->fragments[idx];
639 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
640 entry->rx_queue != rx_queue ||
641 entry->last_frag + 1 != frag)
642 continue;
643
644 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
645 f_fc = le16_to_cpu(f_hdr->frame_control);
646
647 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
648 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
649 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
650 continue;
651
652 if (entry->first_frag_time + 2 * HZ < jiffies) {
653 __skb_queue_purge(&entry->skb_list);
654 continue;
655 }
656 return entry;
657 }
658
659 return NULL;
660}
661
662static ieee80211_txrx_result
663ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
664{
665 struct ieee80211_hdr *hdr;
666 u16 sc;
667 unsigned int frag, seq;
668 struct ieee80211_fragment_entry *entry;
669 struct sk_buff *skb;
670
671 hdr = (struct ieee80211_hdr *) rx->skb->data;
672 sc = le16_to_cpu(hdr->seq_ctrl);
673 frag = sc & IEEE80211_SCTL_FRAG;
674
675 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
676 (rx->skb)->len < 24 ||
677 is_multicast_ether_addr(hdr->addr1))) {
678 /* not fragmented */
679 goto out;
680 }
681 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
682
683 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
684
685 if (frag == 0) {
686 /* This is the first fragment of a new frame. */
687 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
688 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400689 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200690 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
691 /* Store CCMP PN so that we can verify that the next
692 * fragment has a sequential PN value. */
693 entry->ccmp = 1;
694 memcpy(entry->last_pn,
695 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
696 CCMP_PN_LEN);
697 }
698 return TXRX_QUEUED;
699 }
700
701 /* This is a fragment for a frame that should already be pending in
702 * fragment cache. Add this fragment to the end of the pending entry.
703 */
704 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
705 rx->u.rx.queue, hdr);
706 if (!entry) {
707 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
708 return TXRX_DROP;
709 }
710
711 /* Verify that MPDUs within one MSDU have sequential PN values.
712 * (IEEE 802.11i, 8.3.3.4.5) */
713 if (entry->ccmp) {
714 int i;
715 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400716 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200717 return TXRX_DROP;
718 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
719 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
720 pn[i]++;
721 if (pn[i])
722 break;
723 }
724 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
725 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400726 if (net_ratelimit())
727 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
728 "sequential A2=" MAC_FMT
729 " PN=%02x%02x%02x%02x%02x%02x "
730 "(expected %02x%02x%02x%02x%02x%02x)\n",
731 rx->dev->name, MAC_ARG(hdr->addr2),
732 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
733 rpn[5], pn[0], pn[1], pn[2], pn[3],
734 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200735 return TXRX_DROP;
736 }
737 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
738 }
739
740 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
741 __skb_queue_tail(&entry->skb_list, rx->skb);
742 entry->last_frag = frag;
743 entry->extra_len += rx->skb->len;
744 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
745 rx->skb = NULL;
746 return TXRX_QUEUED;
747 }
748
749 rx->skb = __skb_dequeue(&entry->skb_list);
750 if (skb_tailroom(rx->skb) < entry->extra_len) {
751 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
752 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
753 GFP_ATOMIC))) {
754 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
755 __skb_queue_purge(&entry->skb_list);
756 return TXRX_DROP;
757 }
758 }
759 while ((skb = __skb_dequeue(&entry->skb_list))) {
760 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
761 dev_kfree_skb(skb);
762 }
763
764 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400765 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200766
767 out:
768 if (rx->sta)
769 rx->sta->rx_packets++;
770 if (is_multicast_ether_addr(hdr->addr1))
771 rx->local->dot11MulticastReceivedFrameCount++;
772 else
773 ieee80211_led_rx(rx->local);
774 return TXRX_CONTINUE;
775}
776
777static ieee80211_txrx_result
778ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
779{
780 struct sk_buff *skb;
781 int no_pending_pkts;
782
783 if (likely(!rx->sta ||
784 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
785 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400786 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200787 return TXRX_CONTINUE;
788
789 skb = skb_dequeue(&rx->sta->tx_filtered);
790 if (!skb) {
791 skb = skb_dequeue(&rx->sta->ps_tx_buf);
792 if (skb)
793 rx->local->total_ps_buffered--;
794 }
795 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
796 skb_queue_empty(&rx->sta->ps_tx_buf);
797
798 if (skb) {
799 struct ieee80211_hdr *hdr =
800 (struct ieee80211_hdr *) skb->data;
801
802 /* tell TX path to send one frame even though the STA may
803 * still remain is PS mode after this frame exchange */
804 rx->sta->pspoll = 1;
805
806#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
807 printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
808 "after %d)\n",
809 MAC_ARG(rx->sta->addr), rx->sta->aid,
810 skb_queue_len(&rx->sta->ps_tx_buf));
811#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
812
813 /* Use MoreData flag to indicate whether there are more
814 * buffered frames for this STA */
815 if (no_pending_pkts) {
816 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
817 rx->sta->flags &= ~WLAN_STA_TIM;
818 } else
819 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
820
821 dev_queue_xmit(skb);
822
823 if (no_pending_pkts) {
824 if (rx->local->ops->set_tim)
825 rx->local->ops->set_tim(local_to_hw(rx->local),
826 rx->sta->aid, 0);
827 if (rx->sdata->bss)
828 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
829 }
830#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
831 } else if (!rx->u.rx.sent_ps_buffered) {
832 printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
833 "though there is no buffered frames for it\n",
834 rx->dev->name, MAC_ARG(rx->sta->addr));
835#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
836
837 }
838
839 /* Free PS Poll skb here instead of returning TXRX_DROP that would
840 * count as an dropped frame. */
841 dev_kfree_skb(rx->skb);
842
843 return TXRX_QUEUED;
844}
845
846static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200847ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
848{
849 u16 fc = rx->fc;
850 u8 *data = rx->skb->data;
851 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
852
853 if (!WLAN_FC_IS_QOS_DATA(fc))
854 return TXRX_CONTINUE;
855
856 /* remove the qos control field, update frame type and meta-data */
857 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
858 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
859 /* change frame type to non QOS */
860 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
861 hdr->frame_control = cpu_to_le16(fc);
862
863 return TXRX_CONTINUE;
864}
865
866static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200867ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
868{
869 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400870 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
871 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200872 /* Pass both encrypted and unencrypted EAPOL frames to user
873 * space for processing. */
874 if (!rx->local->apdev)
875 return TXRX_DROP;
876 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
877 ieee80211_msg_normal);
878 return TXRX_QUEUED;
879 }
880
881 if (unlikely(rx->sdata->ieee802_1x &&
882 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
883 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
884 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
885 !ieee80211_is_eapol(rx->skb))) {
886#ifdef CONFIG_MAC80211_DEBUG
887 struct ieee80211_hdr *hdr =
888 (struct ieee80211_hdr *) rx->skb->data;
889 printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
890 " (unauthorized port)\n", rx->dev->name,
891 MAC_ARG(hdr->addr2));
892#endif /* CONFIG_MAC80211_DEBUG */
893 return TXRX_DROP;
894 }
895
896 return TXRX_CONTINUE;
897}
898
899static ieee80211_txrx_result
900ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
901{
Johannes Berg3017b802007-08-28 17:01:53 -0400902 /*
903 * Pass through unencrypted frames if the hardware might have
904 * decrypted them already without telling us, but that can only
905 * be true if we either didn't find a key or the found key is
906 * uploaded to the hardware.
907 */
908 if ((rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) &&
Johannes Berg11a843b2007-08-28 17:01:55 -0400909 (!rx->key || (rx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200910 return TXRX_CONTINUE;
911
912 /* Drop unencrypted frames if key is set. */
913 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
914 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
915 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
916 (rx->key || rx->sdata->drop_unencrypted) &&
917 (rx->sdata->eapol == 0 ||
918 !ieee80211_is_eapol(rx->skb)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400919 if (net_ratelimit())
920 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
921 "encryption\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200922 return TXRX_DROP;
923 }
924 return TXRX_CONTINUE;
925}
926
927static ieee80211_txrx_result
928ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
929{
930 struct net_device *dev = rx->dev;
931 struct ieee80211_local *local = rx->local;
932 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
933 u16 fc, hdrlen, ethertype;
934 u8 *payload;
935 u8 dst[ETH_ALEN];
936 u8 src[ETH_ALEN];
937 struct sk_buff *skb = rx->skb, *skb2;
938 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
939
940 fc = rx->fc;
941 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
942 return TXRX_CONTINUE;
943
944 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
945 return TXRX_DROP;
946
947 hdrlen = ieee80211_get_hdrlen(fc);
948
949 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
950 * header
951 * IEEE 802.11 address fields:
952 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
953 * 0 0 DA SA BSSID n/a
954 * 0 1 DA BSSID SA n/a
955 * 1 0 BSSID SA DA n/a
956 * 1 1 RA TA DA SA
957 */
958
959 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
960 case IEEE80211_FCTL_TODS:
961 /* BSSID SA DA */
962 memcpy(dst, hdr->addr3, ETH_ALEN);
963 memcpy(src, hdr->addr2, ETH_ALEN);
964
965 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
966 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400967 if (net_ratelimit())
968 printk(KERN_DEBUG "%s: dropped ToDS frame "
969 "(BSSID=" MAC_FMT
970 " SA=" MAC_FMT
971 " DA=" MAC_FMT ")\n",
972 dev->name,
973 MAC_ARG(hdr->addr1),
974 MAC_ARG(hdr->addr2),
975 MAC_ARG(hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +0200976 return TXRX_DROP;
977 }
978 break;
979 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
980 /* RA TA DA SA */
981 memcpy(dst, hdr->addr3, ETH_ALEN);
982 memcpy(src, hdr->addr4, ETH_ALEN);
983
984 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400985 if (net_ratelimit())
986 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
987 "frame (RA=" MAC_FMT
988 " TA=" MAC_FMT " DA=" MAC_FMT
989 " SA=" MAC_FMT ")\n",
990 rx->dev->name,
991 MAC_ARG(hdr->addr1),
992 MAC_ARG(hdr->addr2),
993 MAC_ARG(hdr->addr3),
994 MAC_ARG(hdr->addr4));
Johannes Berg571ecf62007-07-27 15:43:22 +0200995 return TXRX_DROP;
996 }
997 break;
998 case IEEE80211_FCTL_FROMDS:
999 /* DA BSSID SA */
1000 memcpy(dst, hdr->addr1, ETH_ALEN);
1001 memcpy(src, hdr->addr3, ETH_ALEN);
1002
John W. Linvilleb3316152007-08-28 17:01:55 -04001003 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1004 (is_multicast_ether_addr(dst) &&
1005 !compare_ether_addr(src, dev->dev_addr)))
Johannes Berg571ecf62007-07-27 15:43:22 +02001006 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +02001007 break;
1008 case 0:
1009 /* DA SA BSSID */
1010 memcpy(dst, hdr->addr1, ETH_ALEN);
1011 memcpy(src, hdr->addr2, ETH_ALEN);
1012
1013 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1014 if (net_ratelimit()) {
1015 printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
1016 MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
1017 ")\n",
1018 dev->name, MAC_ARG(hdr->addr1),
1019 MAC_ARG(hdr->addr2),
1020 MAC_ARG(hdr->addr3));
1021 }
1022 return TXRX_DROP;
1023 }
1024 break;
1025 }
1026
1027 payload = skb->data + hdrlen;
1028
1029 if (unlikely(skb->len - hdrlen < 8)) {
1030 if (net_ratelimit()) {
1031 printk(KERN_DEBUG "%s: RX too short data frame "
1032 "payload\n", dev->name);
1033 }
1034 return TXRX_DROP;
1035 }
1036
1037 ethertype = (payload[6] << 8) | payload[7];
1038
1039 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1040 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1041 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1042 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1043 * replace EtherType */
1044 skb_pull(skb, hdrlen + 6);
1045 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1046 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1047 } else {
1048 struct ethhdr *ehdr;
1049 __be16 len;
1050 skb_pull(skb, hdrlen);
1051 len = htons(skb->len);
1052 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1053 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1054 memcpy(ehdr->h_source, src, ETH_ALEN);
1055 ehdr->h_proto = len;
1056 }
1057 skb->dev = dev;
1058
1059 skb2 = NULL;
1060
1061 sdata->stats.rx_packets++;
1062 sdata->stats.rx_bytes += skb->len;
1063
1064 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
Jiri Slabybadffb72007-08-28 17:01:54 -04001065 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1066 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001067 if (is_multicast_ether_addr(skb->data)) {
1068 /* send multicast frames both to higher layers in
1069 * local net stack and back to the wireless media */
1070 skb2 = skb_copy(skb, GFP_ATOMIC);
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001071 if (!skb2 && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001072 printk(KERN_DEBUG "%s: failed to clone "
1073 "multicast frame\n", dev->name);
1074 } else {
1075 struct sta_info *dsta;
1076 dsta = sta_info_get(local, skb->data);
1077 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001078 if (net_ratelimit())
1079 printk(KERN_DEBUG "Station with null "
1080 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001081 } else if (dsta && dsta->dev == dev) {
1082 /* Destination station is associated to this
1083 * AP, so send the frame directly to it and
1084 * do not pass the frame to local net stack.
1085 */
1086 skb2 = skb;
1087 skb = NULL;
1088 }
1089 if (dsta)
1090 sta_info_put(dsta);
1091 }
1092 }
1093
1094 if (skb) {
1095 /* deliver to local stack */
1096 skb->protocol = eth_type_trans(skb, dev);
1097 memset(skb->cb, 0, sizeof(skb->cb));
1098 netif_rx(skb);
1099 }
1100
1101 if (skb2) {
1102 /* send to wireless media */
1103 skb2->protocol = __constant_htons(ETH_P_802_3);
1104 skb_set_network_header(skb2, 0);
1105 skb_set_mac_header(skb2, 0);
1106 dev_queue_xmit(skb2);
1107 }
1108
1109 return TXRX_QUEUED;
1110}
1111
1112static ieee80211_txrx_result
1113ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1114{
1115 struct ieee80211_sub_if_data *sdata;
1116
Jiri Slabybadffb72007-08-28 17:01:54 -04001117 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001118 return TXRX_DROP;
1119
1120 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1121 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1122 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1123 !rx->local->user_space_mlme) {
1124 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1125 } else {
1126 /* Management frames are sent to hostapd for processing */
1127 if (!rx->local->apdev)
1128 return TXRX_DROP;
1129 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
1130 ieee80211_msg_normal);
1131 }
1132 return TXRX_QUEUED;
1133}
1134
1135static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1136 struct ieee80211_local *local,
1137 ieee80211_rx_handler *handlers,
1138 struct ieee80211_txrx_data *rx,
1139 struct sta_info *sta)
1140{
1141 ieee80211_rx_handler *handler;
1142 ieee80211_txrx_result res = TXRX_DROP;
1143
1144 for (handler = handlers; *handler != NULL; handler++) {
1145 res = (*handler)(rx);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001146
1147 switch (res) {
1148 case TXRX_CONTINUE:
1149 continue;
1150 case TXRX_DROP:
1151 I802_DEBUG_INC(local->rx_handlers_drop);
1152 if (sta)
1153 sta->rx_dropped++;
1154 break;
1155 case TXRX_QUEUED:
1156 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001157 break;
1158 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001159 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001160 }
1161
Johannes Berg8e6f00322007-07-27 15:43:22 +02001162 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001163 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001164 return res;
1165}
1166
1167static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1168 ieee80211_rx_handler *handlers,
1169 struct ieee80211_txrx_data *rx,
1170 struct sta_info *sta)
1171{
1172 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1173 TXRX_CONTINUE)
1174 dev_kfree_skb(rx->skb);
1175}
1176
1177static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1178 struct ieee80211_hdr *hdr,
1179 struct sta_info *sta,
1180 struct ieee80211_txrx_data *rx)
1181{
1182 int keyidx, hdrlen;
1183
1184 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1185 if (rx->skb->len >= hdrlen + 4)
1186 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1187 else
1188 keyidx = -1;
1189
1190 /* TODO: verify that this is not triggered by fragmented
1191 * frames (hw does not verify MIC for them). */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001192 if (net_ratelimit())
1193 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1194 "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
1195 dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1),
1196 keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001197
1198 if (!sta) {
1199 /* Some hardware versions seem to generate incorrect
1200 * Michael MIC reports; ignore them to avoid triggering
1201 * countermeasures. */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001202 if (net_ratelimit())
1203 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1204 "error for unknown address " MAC_FMT "\n",
1205 dev->name, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001206 goto ignore;
1207 }
1208
1209 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001210 if (net_ratelimit())
1211 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1212 "error for a frame with no ISWEP flag (src "
1213 MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001214 goto ignore;
1215 }
1216
1217 if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
Johannes Bergeb063c12007-08-28 17:01:53 -04001218 rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001219 /* AP with Pairwise keys support should never receive Michael
1220 * MIC errors for non-zero keyidx because these are reserved
1221 * for group keys and only the AP is sending real multicast
1222 * frames in BSS. */
Johannes Bergeb063c12007-08-28 17:01:53 -04001223 if (net_ratelimit())
1224 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1225 "a frame with non-zero keyidx (%d)"
1226 " (src " MAC_FMT ")\n", dev->name, keyidx,
1227 MAC_ARG(hdr->addr2));
1228 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001229 }
1230
1231 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1232 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1233 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001234 if (net_ratelimit())
1235 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1236 "error for a frame that cannot be encrypted "
1237 "(fc=0x%04x) (src " MAC_FMT ")\n",
1238 dev->name, rx->fc, MAC_ARG(hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001239 goto ignore;
1240 }
1241
Johannes Berg571ecf62007-07-27 15:43:22 +02001242 /* TODO: consider verifying the MIC error report with software
1243 * implementation if we get too many spurious reports from the
1244 * hardware. */
Johannes Berg571ecf62007-07-27 15:43:22 +02001245
Johannes Bergeb063c12007-08-28 17:01:53 -04001246 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001247 ignore:
1248 dev_kfree_skb(rx->skb);
1249 rx->skb = NULL;
1250}
1251
1252ieee80211_rx_handler ieee80211_rx_handlers[] =
1253{
1254 ieee80211_rx_h_if_stats,
1255 ieee80211_rx_h_monitor,
1256 ieee80211_rx_h_passive_scan,
1257 ieee80211_rx_h_check,
Johannes Berg570bd532007-07-27 15:43:22 +02001258 ieee80211_rx_h_load_key,
Johannes Berg571ecf62007-07-27 15:43:22 +02001259 ieee80211_rx_h_sta_process,
1260 ieee80211_rx_h_ccmp_decrypt,
1261 ieee80211_rx_h_tkip_decrypt,
1262 ieee80211_rx_h_wep_weak_iv_detection,
1263 ieee80211_rx_h_wep_decrypt,
1264 ieee80211_rx_h_defragment,
1265 ieee80211_rx_h_ps_poll,
1266 ieee80211_rx_h_michael_mic_verify,
1267 /* this must be after decryption - so header is counted in MPDU mic
1268 * must be before pae and data, so QOS_DATA format frames
1269 * are not passed to user space by these functions
1270 */
1271 ieee80211_rx_h_remove_qos_control,
1272 ieee80211_rx_h_802_1x_pae,
1273 ieee80211_rx_h_drop_unencrypted,
1274 ieee80211_rx_h_data,
1275 ieee80211_rx_h_mgmt,
1276 NULL
1277};
1278
1279/* main receive path */
1280
Johannes Berg23a24de2007-07-27 15:43:22 +02001281static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1282 u8 *bssid, struct ieee80211_txrx_data *rx,
1283 struct ieee80211_hdr *hdr)
1284{
1285 int multicast = is_multicast_ether_addr(hdr->addr1);
1286
1287 switch (sdata->type) {
1288 case IEEE80211_IF_TYPE_STA:
1289 if (!bssid)
1290 return 0;
1291 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001292 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001293 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001294 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001295 } else if (!multicast &&
1296 compare_ether_addr(sdata->dev->dev_addr,
1297 hdr->addr1) != 0) {
Jiri Slaby13262ff2007-08-28 17:01:54 -04001298 if (!(sdata->flags & IEEE80211_SDATA_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001299 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001300 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001301 }
1302 break;
1303 case IEEE80211_IF_TYPE_IBSS:
1304 if (!bssid)
1305 return 0;
1306 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001307 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001308 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001309 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001310 } else if (!multicast &&
1311 compare_ether_addr(sdata->dev->dev_addr,
1312 hdr->addr1) != 0) {
Jiri Slaby13262ff2007-08-28 17:01:54 -04001313 if (!(sdata->flags & IEEE80211_SDATA_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001314 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001315 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001316 } else if (!rx->sta)
1317 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1318 bssid, hdr->addr2);
1319 break;
1320 case IEEE80211_IF_TYPE_AP:
1321 if (!bssid) {
1322 if (compare_ether_addr(sdata->dev->dev_addr,
1323 hdr->addr1))
1324 return 0;
1325 } else if (!ieee80211_bssid_match(bssid,
1326 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001327 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001328 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001329 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001330 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001331 if (sdata->dev == sdata->local->mdev &&
1332 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001333 /* do not receive anything via
1334 * master device when not scanning */
1335 return 0;
1336 break;
1337 case IEEE80211_IF_TYPE_WDS:
1338 if (bssid ||
1339 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1340 return 0;
1341 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1342 return 0;
1343 break;
1344 }
1345
1346 return 1;
1347}
1348
Johannes Berg571ecf62007-07-27 15:43:22 +02001349/*
1350 * This is the receive path handler. It is called by a low level driver when an
1351 * 802.11 MPDU is received from the hardware.
1352 */
1353void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1354 struct ieee80211_rx_status *status)
1355{
1356 struct ieee80211_local *local = hw_to_local(hw);
1357 struct ieee80211_sub_if_data *sdata;
1358 struct sta_info *sta;
1359 struct ieee80211_hdr *hdr;
1360 struct ieee80211_txrx_data rx;
1361 u16 type;
Johannes Berg23a24de2007-07-27 15:43:22 +02001362 int radiotap_len = 0, prepres;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001363 struct ieee80211_sub_if_data *prev = NULL;
1364 struct sk_buff *skb_new;
1365 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001366
1367 if (status->flag & RX_FLAG_RADIOTAP) {
1368 radiotap_len = ieee80211_get_radiotap_len(skb->data);
1369 skb_pull(skb, radiotap_len);
1370 }
1371
Johannes Bergd4e46a32007-09-14 11:10:24 -04001372 /*
1373 * key references are protected using RCU and this requires that
1374 * we are in a read-site RCU section during receive processing
1375 */
1376 rcu_read_lock();
1377
Johannes Berg571ecf62007-07-27 15:43:22 +02001378 hdr = (struct ieee80211_hdr *) skb->data;
1379 memset(&rx, 0, sizeof(rx));
1380 rx.skb = skb;
1381 rx.local = local;
1382
1383 rx.u.rx.status = status;
1384 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
1385 type = rx.fc & IEEE80211_FCTL_FTYPE;
1386 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1387 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001388
Johannes Berg23a24de2007-07-27 15:43:22 +02001389 if (skb->len >= 16) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001390 sta = rx.sta = sta_info_get(local, hdr->addr2);
Johannes Berg23a24de2007-07-27 15:43:22 +02001391 if (sta) {
1392 rx.dev = rx.sta->dev;
1393 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1394 }
1395 } else
Johannes Berg571ecf62007-07-27 15:43:22 +02001396 sta = rx.sta = NULL;
1397
Johannes Berg571ecf62007-07-27 15:43:22 +02001398 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1399 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1400 goto end;
1401 }
1402
1403 if (unlikely(local->sta_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001404 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001405
1406 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1407 sta) != TXRX_CONTINUE)
1408 goto end;
1409 skb = rx.skb;
1410
1411 skb_push(skb, radiotap_len);
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001412 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg23a24de2007-07-27 15:43:22 +02001413 !local->iff_promiscs && !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001414 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001415 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001416 rx.sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001417 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001418 rcu_read_unlock();
Johannes Berg8e6f00322007-07-27 15:43:22 +02001419 return;
1420 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001421
Johannes Berg8e6f00322007-07-27 15:43:22 +02001422 bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001423
Johannes Berg8e6f00322007-07-27 15:43:22 +02001424 read_lock(&local->sub_if_lock);
1425 list_for_each_entry(sdata, &local->sub_if_list, list) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001426 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001427
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001428 if (!netif_running(sdata->dev))
1429 continue;
1430
Johannes Berg23a24de2007-07-27 15:43:22 +02001431 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1432 /* prepare_for_handlers can change sta */
1433 sta = rx.sta;
1434
1435 if (!prepres)
1436 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001437
Johannes Berg340e11f2007-07-27 15:43:22 +02001438 /*
1439 * frame is destined for this interface, but if it's not
1440 * also for the previous one we handle that after the
1441 * loop to avoid copying the SKB once too much
1442 */
1443
1444 if (!prev) {
1445 prev = sdata;
1446 continue;
Johannes Berg8e6f00322007-07-27 15:43:22 +02001447 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001448
1449 /*
1450 * frame was destined for the previous interface
1451 * so invoke RX handlers for it
1452 */
1453
1454 skb_new = skb_copy(skb, GFP_ATOMIC);
1455 if (!skb_new) {
1456 if (net_ratelimit())
1457 printk(KERN_DEBUG "%s: failed to copy "
1458 "multicast frame for %s",
1459 local->mdev->name, prev->dev->name);
1460 continue;
1461 }
1462 rx.skb = skb_new;
1463 rx.dev = prev->dev;
1464 rx.sdata = prev;
1465 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1466 &rx, sta);
Johannes Berg8e6f00322007-07-27 15:43:22 +02001467 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001468 }
Johannes Berg8e6f00322007-07-27 15:43:22 +02001469 if (prev) {
1470 rx.skb = skb;
1471 rx.dev = prev->dev;
1472 rx.sdata = prev;
1473 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1474 &rx, sta);
1475 } else
1476 dev_kfree_skb(skb);
1477 read_unlock(&local->sub_if_lock);
Johannes Berg571ecf62007-07-27 15:43:22 +02001478
Johannes Berg8e6f00322007-07-27 15:43:22 +02001479 end:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001480 rcu_read_unlock();
1481
Johannes Berg571ecf62007-07-27 15:43:22 +02001482 if (sta)
1483 sta_info_put(sta);
1484}
1485EXPORT_SYMBOL(__ieee80211_rx);
1486
1487/* This is a version of the rx handler that can be called from hard irq
1488 * context. Post the skb on the queue and schedule the tasklet */
1489void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1490 struct ieee80211_rx_status *status)
1491{
1492 struct ieee80211_local *local = hw_to_local(hw);
1493
1494 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1495
1496 skb->dev = local->mdev;
1497 /* copy status into skb->cb for use by tasklet */
1498 memcpy(skb->cb, status, sizeof(*status));
1499 skb->pkt_type = IEEE80211_RX_MSG;
1500 skb_queue_tail(&local->skb_queue, skb);
1501 tasklet_schedule(&local->tasklet);
1502}
1503EXPORT_SYMBOL(ieee80211_rx_irqsafe);