blob: e9dcc6229c3c50ca8f7a509983465a4caf6aa5f1 [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 Berg52865df2007-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 Berg52865df2007-07-27 15:43:22 +020055
Johannes Berg6e0d1142007-07-27 15:43:22 +020056 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
Johannes Berg52865df2007-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 ||
Johannes Berg571ecf62007-07-27 15:43:22 +020097 (mode->mode == MODE_IEEE80211G &&
98 rate->flags & IEEE80211_RATE_ERP))
99 hdrtime = CHAN_UTIL_HDR_SHORT;
100 else
101 hdrtime = CHAN_UTIL_HDR_LONG;
102
103 load = hdrtime;
104 if (!is_multicast_ether_addr(hdr->addr1))
105 load += hdrtime;
106
107 load += skb->len * rate->rate_inv;
108
109 /* Divide channel_use by 8 to avoid wrapping around the counter */
110 load >>= CHAN_UTIL_SHIFT;
111 local->channel_use_raw += load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200112 rx->u.rx.load = load;
113
114 return TXRX_CONTINUE;
115}
116
117ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
118{
119 ieee80211_rx_h_parse_qos,
120 ieee80211_rx_h_load_stats,
121 NULL
122};
123
124/* rx handlers */
125
126static ieee80211_txrx_result
127ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
128{
Johannes Berg52865df2007-07-27 15:43:22 +0200129 if (rx->sta)
130 rx->sta->channel_use_raw += rx->u.rx.load;
Johannes Berg571ecf62007-07-27 15:43:22 +0200131 rx->sdata->channel_use_raw += rx->u.rx.load;
132 return TXRX_CONTINUE;
133}
134
135static void
136ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
137 struct ieee80211_rx_status *status)
138{
139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg571ecf62007-07-27 15:43:22 +0200140 struct ieee80211_rate *rate;
141 struct ieee80211_rtap_hdr {
142 struct ieee80211_radiotap_header hdr;
143 u8 flags;
144 u8 rate;
145 __le16 chan_freq;
146 __le16 chan_flags;
147 u8 antsignal;
148 } __attribute__ ((packed)) *rthdr;
149
150 skb->dev = dev;
151
Johannes Berg571ecf62007-07-27 15:43:22 +0200152 if (status->flag & RX_FLAG_RADIOTAP)
153 goto out;
154
155 if (skb_headroom(skb) < sizeof(*rthdr)) {
156 I802_DEBUG_INC(local->rx_expand_skb_head);
157 if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
158 dev_kfree_skb(skb);
159 return;
160 }
161 }
162
163 rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
164 memset(rthdr, 0, sizeof(*rthdr));
165 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
166 rthdr->hdr.it_present =
167 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
168 (1 << IEEE80211_RADIOTAP_RATE) |
169 (1 << IEEE80211_RADIOTAP_CHANNEL) |
170 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
171 rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
172 IEEE80211_RADIOTAP_F_FCS : 0;
173 rate = ieee80211_get_rate(local, status->phymode, status->rate);
174 if (rate)
175 rthdr->rate = rate->rate / 5;
176 rthdr->chan_freq = cpu_to_le16(status->freq);
177 rthdr->chan_flags =
178 status->phymode == MODE_IEEE80211A ?
179 cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
180 cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
181 rthdr->antsignal = status->ssi;
182
183 out:
Stephen Hemminger68aae112007-08-24 11:29:34 -0700184 dev->stats.rx_packets++;
185 dev->stats.rx_bytes += skb->len;
Johannes Berg571ecf62007-07-27 15:43:22 +0200186
187 skb_set_mac_header(skb, 0);
188 skb->ip_summed = CHECKSUM_UNNECESSARY;
189 skb->pkt_type = PACKET_OTHERHOST;
190 skb->protocol = htons(ETH_P_802_2);
191 memset(skb->cb, 0, sizeof(skb->cb));
192 netif_rx(skb);
193}
194
195static ieee80211_txrx_result
196ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
197{
198 if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
199 ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
200 return TXRX_QUEUED;
201 }
202
203 if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
204 skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb->data));
205
206 return TXRX_CONTINUE;
207}
208
209static ieee80211_txrx_result
210ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
211{
212 struct ieee80211_local *local = rx->local;
213 struct sk_buff *skb = rx->skb;
214
215 if (unlikely(local->sta_scanning != 0)) {
216 ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
217 return TXRX_QUEUED;
218 }
219
Jiri Slabybadffb72007-08-28 17:01:54 -0400220 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200221 /* scanning finished during invoking of handlers */
222 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
223 return TXRX_DROP;
224 }
225
226 return TXRX_CONTINUE;
227}
228
229static ieee80211_txrx_result
230ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
231{
232 struct ieee80211_hdr *hdr;
Johannes Berg571ecf62007-07-27 15:43:22 +0200233 hdr = (struct ieee80211_hdr *) rx->skb->data;
234
235 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
236 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
237 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
238 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
239 hdr->seq_ctrl)) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400240 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200241 rx->local->dot11FrameDuplicateCount++;
242 rx->sta->num_duplicates++;
243 }
244 return TXRX_DROP;
245 } else
246 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
247 }
248
249 if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
250 rx->skb->len > FCS_LEN)
251 skb_trim(rx->skb, rx->skb->len - FCS_LEN);
252
253 if (unlikely(rx->skb->len < 16)) {
254 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
255 return TXRX_DROP;
256 }
257
Jiri Slabybadffb72007-08-28 17:01:54 -0400258 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200259 rx->skb->pkt_type = PACKET_OTHERHOST;
260 else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
261 rx->skb->pkt_type = PACKET_HOST;
262 else if (is_multicast_ether_addr(hdr->addr1)) {
263 if (is_broadcast_ether_addr(hdr->addr1))
264 rx->skb->pkt_type = PACKET_BROADCAST;
265 else
266 rx->skb->pkt_type = PACKET_MULTICAST;
267 } else
268 rx->skb->pkt_type = PACKET_OTHERHOST;
269
270 /* Drop disallowed frame classes based on STA auth/assoc state;
271 * IEEE 802.11, Chap 5.5.
272 *
273 * 80211.o does filtering only based on association state, i.e., it
274 * drops Class 3 frames from not associated stations. hostapd sends
275 * deauth/disassoc frames when needed. In addition, hostapd is
276 * responsible for filtering on both auth and assoc states.
277 */
278 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
279 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
280 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
281 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
282 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
283 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
284 !(rx->fc & IEEE80211_FCTL_TODS) &&
285 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
Jiri Slabybadffb72007-08-28 17:01:54 -0400286 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200287 /* Drop IBSS frames and frames for other hosts
288 * silently. */
289 return TXRX_DROP;
290 }
291
292 if (!rx->local->apdev)
293 return TXRX_DROP;
294
295 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
296 ieee80211_msg_sta_not_assoc);
297 return TXRX_QUEUED;
298 }
299
Johannes Berg570bd532007-07-27 15:43:22 +0200300 return TXRX_CONTINUE;
301}
302
303
304static ieee80211_txrx_result
305ieee80211_rx_h_load_key(struct ieee80211_txrx_data *rx)
306{
307 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
Johannes Berg3017b802007-08-28 17:01:53 -0400308 int keyidx;
309 int hdrlen;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400310 struct ieee80211_key *stakey = NULL;
Johannes Berg570bd532007-07-27 15:43:22 +0200311
Johannes Berg3017b802007-08-28 17:01:53 -0400312 /*
313 * Key selection 101
314 *
315 * There are three types of keys:
316 * - GTK (group keys)
317 * - PTK (pairwise keys)
318 * - STK (station-to-station pairwise keys)
319 *
320 * When selecting a key, we have to distinguish between multicast
321 * (including broadcast) and unicast frames, the latter can only
322 * use PTKs and STKs while the former always use GTKs. Unless, of
323 * course, actual WEP keys ("pre-RSNA") are used, then unicast
324 * frames can also use key indizes like GTKs. Hence, if we don't
325 * have a PTK/STK we check the key index for a WEP key.
326 *
Johannes Berg8dc06a12007-08-28 17:01:55 -0400327 * Note that in a regular BSS, multicast frames are sent by the
328 * AP only, associated stations unicast the frame to the AP first
329 * which then multicasts it on their behalf.
330 *
Johannes Berg3017b802007-08-28 17:01:53 -0400331 * There is also a slight problem in IBSS mode: GTKs are negotiated
332 * with each station, that is something we don't currently handle.
Johannes Berg8dc06a12007-08-28 17:01:55 -0400333 * The spec seems to expect that one negotiates the same key with
334 * every station but there's no such requirement; VLANs could be
335 * possible.
Johannes Berg3017b802007-08-28 17:01:53 -0400336 */
Johannes Berg571ecf62007-07-27 15:43:22 +0200337
Johannes Berg3017b802007-08-28 17:01:53 -0400338 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
339 return TXRX_CONTINUE;
340
341 /*
342 * No point in finding a key if the frame is neither
343 * addressed to us nor a multicast frame.
344 */
Jiri Slabybadffb72007-08-28 17:01:54 -0400345 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg3017b802007-08-28 17:01:53 -0400346 return TXRX_CONTINUE;
347
Johannes Bergd4e46a32007-09-14 11:10:24 -0400348 if (rx->sta)
349 stakey = rcu_dereference(rx->sta->key);
350
351 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
352 rx->key = stakey;
Johannes Berg571ecf62007-07-27 15:43:22 +0200353 } else {
Johannes Berg3017b802007-08-28 17:01:53 -0400354 /*
355 * The device doesn't give us the IV so we won't be
356 * able to look up the key. That's ok though, we
357 * don't need to decrypt the frame, we just won't
358 * be able to keep statistics accurate.
359 * Except for key threshold notifications, should
360 * we somehow allow the driver to tell us which key
361 * the hardware used if this flag is set?
362 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400363 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
364 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Johannes Berg3017b802007-08-28 17:01:53 -0400365 return TXRX_CONTINUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200366
Johannes Berg3017b802007-08-28 17:01:53 -0400367 hdrlen = ieee80211_get_hdrlen(rx->fc);
Johannes Berg571ecf62007-07-27 15:43:22 +0200368
Johannes Berg3017b802007-08-28 17:01:53 -0400369 if (rx->skb->len < 8 + hdrlen)
370 return TXRX_DROP; /* TODO: count this? */
Johannes Berg571ecf62007-07-27 15:43:22 +0200371
Johannes Berg3017b802007-08-28 17:01:53 -0400372 /*
373 * no need to call ieee80211_wep_get_keyidx,
374 * it verifies a bunch of things we've done already
375 */
376 keyidx = rx->skb->data[hdrlen + 3] >> 6;
377
Johannes Bergd4e46a32007-09-14 11:10:24 -0400378 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
Johannes Berg3017b802007-08-28 17:01:53 -0400379
380 /*
381 * RSNA-protected unicast frames should always be sent with
382 * pairwise or station-to-station keys, but for WEP we allow
383 * using a key index as well.
384 */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400385 if (rx->key && rx->key->conf.alg != ALG_WEP &&
Johannes Berg3017b802007-08-28 17:01:53 -0400386 !is_multicast_ether_addr(hdr->addr1))
387 rx->key = NULL;
Johannes Berg571ecf62007-07-27 15:43:22 +0200388 }
389
Johannes Berg3017b802007-08-28 17:01:53 -0400390 if (rx->key) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200391 rx->key->tx_rx_count++;
392 if (unlikely(rx->local->key_tx_rx_threshold &&
393 rx->key->tx_rx_count >
394 rx->local->key_tx_rx_threshold)) {
395 ieee80211_key_threshold_notify(rx->dev, rx->key,
396 rx->sta);
397 }
398 }
399
400 return TXRX_CONTINUE;
401}
402
403static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
404{
405 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700406 DECLARE_MAC_BUF(mac);
407
Johannes Berg571ecf62007-07-27 15:43:22 +0200408 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
409
410 if (sdata->bss)
411 atomic_inc(&sdata->bss->num_sta_ps);
412 sta->flags |= WLAN_STA_PS;
413 sta->pspoll = 0;
414#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700415 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
416 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200417#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
418}
419
420static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
421{
422 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
423 struct sk_buff *skb;
424 int sent = 0;
425 struct ieee80211_sub_if_data *sdata;
426 struct ieee80211_tx_packet_data *pkt_data;
Joe Perches0795af52007-10-03 17:59:30 -0700427 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200428
429 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
430 if (sdata->bss)
431 atomic_dec(&sdata->bss->num_sta_ps);
432 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
433 sta->pspoll = 0;
434 if (!skb_queue_empty(&sta->ps_tx_buf)) {
435 if (local->ops->set_tim)
436 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
437 if (sdata->bss)
438 bss_tim_clear(local, sdata->bss, sta->aid);
439 }
440#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700441 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
442 dev->name, print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200443#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
444 /* Send all buffered frames to the station */
445 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
446 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
447 sent++;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400448 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200449 dev_queue_xmit(skb);
450 }
451 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
452 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
453 local->total_ps_buffered--;
454 sent++;
455#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700456 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
Johannes Berg571ecf62007-07-27 15:43:22 +0200457 "since STA not sleeping anymore\n", dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700458 print_mac(mac, sta->addr), sta->aid);
Johannes Berg571ecf62007-07-27 15:43:22 +0200459#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Jiri Slabye8bf9642007-08-28 17:01:54 -0400460 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg571ecf62007-07-27 15:43:22 +0200461 dev_queue_xmit(skb);
462 }
463
464 return sent;
465}
466
467static ieee80211_txrx_result
468ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
469{
470 struct sta_info *sta = rx->sta;
471 struct net_device *dev = rx->dev;
472 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
473
474 if (!sta)
475 return TXRX_CONTINUE;
476
477 /* Update last_rx only for IBSS packets which are for the current
478 * BSSID to avoid keeping the current IBSS network alive in cases where
479 * other STAs are using different BSSID. */
480 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
481 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
482 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
483 sta->last_rx = jiffies;
484 } else
485 if (!is_multicast_ether_addr(hdr->addr1) ||
486 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
487 /* Update last_rx only for unicast frames in order to prevent
488 * the Probe Request frames (the only broadcast frames from a
489 * STA in infrastructure mode) from keeping a connection alive.
490 */
491 sta->last_rx = jiffies;
492 }
493
Jiri Slabybadffb72007-08-28 17:01:54 -0400494 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200495 return TXRX_CONTINUE;
496
497 sta->rx_fragments++;
498 sta->rx_bytes += rx->skb->len;
Larry Finger6c55aa92007-08-28 17:01:55 -0400499 sta->last_rssi = rx->u.rx.status->ssi;
500 sta->last_signal = rx->u.rx.status->signal;
501 sta->last_noise = rx->u.rx.status->noise;
Johannes Berg571ecf62007-07-27 15:43:22 +0200502
503 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
504 /* Change STA power saving mode only in the end of a frame
505 * exchange sequence */
506 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
507 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
508 else if (!(sta->flags & WLAN_STA_PS) &&
509 (rx->fc & IEEE80211_FCTL_PM))
510 ap_sta_ps_start(dev, sta);
511 }
512
513 /* Drop data::nullfunc frames silently, since they are used only to
514 * control station power saving mode. */
515 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
516 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
517 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
518 /* Update counter and free packet here to avoid counting this
519 * as a dropped packed. */
520 sta->rx_packets++;
521 dev_kfree_skb(rx->skb);
522 return TXRX_QUEUED;
523 }
524
525 return TXRX_CONTINUE;
526} /* ieee80211_rx_h_sta_process */
527
528static ieee80211_txrx_result
529ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
530{
531 if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
532 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400533 !rx->key || rx->key->conf.alg != ALG_WEP ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400534 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +0200535 return TXRX_CONTINUE;
536
537 /* Check for weak IVs, if hwaccel did not remove IV from the frame */
Johannes Berg7848ba72007-09-14 11:10:25 -0400538 if (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
539 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED))
Johannes Berg8f20fc22007-08-28 17:01:54 -0400540 if (ieee80211_wep_is_weak_iv(rx->skb, rx->key))
Johannes Berg571ecf62007-07-27 15:43:22 +0200541 rx->sta->wep_weak_iv_count++;
Johannes Berg571ecf62007-07-27 15:43:22 +0200542
543 return TXRX_CONTINUE;
544}
545
546static ieee80211_txrx_result
547ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
548{
Johannes Berg8f20fc22007-08-28 17:01:54 -0400549 if ((rx->key && rx->key->conf.alg != ALG_WEP) ||
Johannes Berg571ecf62007-07-27 15:43:22 +0200550 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
551 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
552 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
553 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
554 return TXRX_CONTINUE;
555
556 if (!rx->key) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400557 if (net_ratelimit())
558 printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
559 rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200560 return TXRX_DROP;
561 }
562
Johannes Berg7848ba72007-09-14 11:10:25 -0400563 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200564 if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400565 if (net_ratelimit())
566 printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
567 "failed\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200568 return TXRX_DROP;
569 }
Johannes Berg7848ba72007-09-14 11:10:25 -0400570 } else if (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200571 ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
572 /* remove ICV */
573 skb_trim(rx->skb, rx->skb->len - 4);
574 }
575
576 return TXRX_CONTINUE;
577}
578
579static inline struct ieee80211_fragment_entry *
580ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
581 unsigned int frag, unsigned int seq, int rx_queue,
582 struct sk_buff **skb)
583{
584 struct ieee80211_fragment_entry *entry;
585 int idx;
586
587 idx = sdata->fragment_next;
588 entry = &sdata->fragments[sdata->fragment_next++];
589 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
590 sdata->fragment_next = 0;
591
592 if (!skb_queue_empty(&entry->skb_list)) {
593#ifdef CONFIG_MAC80211_DEBUG
594 struct ieee80211_hdr *hdr =
595 (struct ieee80211_hdr *) entry->skb_list.next->data;
Joe Perches0795af52007-10-03 17:59:30 -0700596 DECLARE_MAC_BUF(mac);
597 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +0200598 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
599 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700600 "addr1=%s addr2=%s\n",
Johannes Berg571ecf62007-07-27 15:43:22 +0200601 sdata->dev->name, idx,
602 jiffies - entry->first_frag_time, entry->seq,
Joe Perches0795af52007-10-03 17:59:30 -0700603 entry->last_frag, print_mac(mac, hdr->addr1),
604 print_mac(mac2, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200605#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;
Joe Perches0795af52007-10-03 17:59:30 -0700670 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200671
672 hdr = (struct ieee80211_hdr *) rx->skb->data;
673 sc = le16_to_cpu(hdr->seq_ctrl);
674 frag = sc & IEEE80211_SCTL_FRAG;
675
676 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
677 (rx->skb)->len < 24 ||
678 is_multicast_ether_addr(hdr->addr1))) {
679 /* not fragmented */
680 goto out;
681 }
682 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
683
684 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
685
686 if (frag == 0) {
687 /* This is the first fragment of a new frame. */
688 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
689 rx->u.rx.queue, &(rx->skb));
Johannes Berg8f20fc22007-08-28 17:01:54 -0400690 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
Johannes Berg571ecf62007-07-27 15:43:22 +0200691 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
692 /* Store CCMP PN so that we can verify that the next
693 * fragment has a sequential PN value. */
694 entry->ccmp = 1;
695 memcpy(entry->last_pn,
696 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
697 CCMP_PN_LEN);
698 }
699 return TXRX_QUEUED;
700 }
701
702 /* This is a fragment for a frame that should already be pending in
703 * fragment cache. Add this fragment to the end of the pending entry.
704 */
705 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
706 rx->u.rx.queue, hdr);
707 if (!entry) {
708 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
709 return TXRX_DROP;
710 }
711
712 /* Verify that MPDUs within one MSDU have sequential PN values.
713 * (IEEE 802.11i, 8.3.3.4.5) */
714 if (entry->ccmp) {
715 int i;
716 u8 pn[CCMP_PN_LEN], *rpn;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400717 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
Johannes Berg571ecf62007-07-27 15:43:22 +0200718 return TXRX_DROP;
719 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
720 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
721 pn[i]++;
722 if (pn[i])
723 break;
724 }
725 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
726 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400727 if (net_ratelimit())
728 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
Joe Perches0795af52007-10-03 17:59:30 -0700729 "sequential A2=%s"
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400730 " PN=%02x%02x%02x%02x%02x%02x "
731 "(expected %02x%02x%02x%02x%02x%02x)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700732 rx->dev->name, print_mac(mac, hdr->addr2),
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400733 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
734 rpn[5], pn[0], pn[1], pn[2], pn[3],
735 pn[4], pn[5]);
Johannes Berg571ecf62007-07-27 15:43:22 +0200736 return TXRX_DROP;
737 }
738 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
739 }
740
741 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
742 __skb_queue_tail(&entry->skb_list, rx->skb);
743 entry->last_frag = frag;
744 entry->extra_len += rx->skb->len;
745 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
746 rx->skb = NULL;
747 return TXRX_QUEUED;
748 }
749
750 rx->skb = __skb_dequeue(&entry->skb_list);
751 if (skb_tailroom(rx->skb) < entry->extra_len) {
752 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
753 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
754 GFP_ATOMIC))) {
755 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
756 __skb_queue_purge(&entry->skb_list);
757 return TXRX_DROP;
758 }
759 }
760 while ((skb = __skb_dequeue(&entry->skb_list))) {
761 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
762 dev_kfree_skb(skb);
763 }
764
765 /* Complete frame has been reassembled - process it now */
Jiri Slabybadffb72007-08-28 17:01:54 -0400766 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berg571ecf62007-07-27 15:43:22 +0200767
768 out:
769 if (rx->sta)
770 rx->sta->rx_packets++;
771 if (is_multicast_ether_addr(hdr->addr1))
772 rx->local->dot11MulticastReceivedFrameCount++;
773 else
774 ieee80211_led_rx(rx->local);
775 return TXRX_CONTINUE;
776}
777
778static ieee80211_txrx_result
779ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
780{
781 struct sk_buff *skb;
782 int no_pending_pkts;
Joe Perches0795af52007-10-03 17:59:30 -0700783 DECLARE_MAC_BUF(mac);
Johannes Berg571ecf62007-07-27 15:43:22 +0200784
785 if (likely(!rx->sta ||
786 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
787 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
Jiri Slabybadffb72007-08-28 17:01:54 -0400788 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
Johannes Berg571ecf62007-07-27 15:43:22 +0200789 return TXRX_CONTINUE;
790
791 skb = skb_dequeue(&rx->sta->tx_filtered);
792 if (!skb) {
793 skb = skb_dequeue(&rx->sta->ps_tx_buf);
794 if (skb)
795 rx->local->total_ps_buffered--;
796 }
797 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
798 skb_queue_empty(&rx->sta->ps_tx_buf);
799
800 if (skb) {
801 struct ieee80211_hdr *hdr =
802 (struct ieee80211_hdr *) skb->data;
803
804 /* tell TX path to send one frame even though the STA may
805 * still remain is PS mode after this frame exchange */
806 rx->sta->pspoll = 1;
807
808#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700809 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
810 print_mac(mac, rx->sta->addr), rx->sta->aid,
Johannes Berg571ecf62007-07-27 15:43:22 +0200811 skb_queue_len(&rx->sta->ps_tx_buf));
812#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
813
814 /* Use MoreData flag to indicate whether there are more
815 * buffered frames for this STA */
816 if (no_pending_pkts) {
817 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
818 rx->sta->flags &= ~WLAN_STA_TIM;
819 } else
820 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
821
822 dev_queue_xmit(skb);
823
824 if (no_pending_pkts) {
825 if (rx->local->ops->set_tim)
826 rx->local->ops->set_tim(local_to_hw(rx->local),
827 rx->sta->aid, 0);
828 if (rx->sdata->bss)
829 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
830 }
831#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
832 } else if (!rx->u.rx.sent_ps_buffered) {
Joe Perches0795af52007-10-03 17:59:30 -0700833 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
Johannes Berg571ecf62007-07-27 15:43:22 +0200834 "though there is no buffered frames for it\n",
Joe Perches0795af52007-10-03 17:59:30 -0700835 rx->dev->name, print_mac(mac, rx->sta->addr));
Johannes Berg571ecf62007-07-27 15:43:22 +0200836#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
837
838 }
839
840 /* Free PS Poll skb here instead of returning TXRX_DROP that would
841 * count as an dropped frame. */
842 dev_kfree_skb(rx->skb);
843
844 return TXRX_QUEUED;
845}
846
847static ieee80211_txrx_result
Johannes Berg6e0d1142007-07-27 15:43:22 +0200848ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
849{
850 u16 fc = rx->fc;
851 u8 *data = rx->skb->data;
852 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
853
854 if (!WLAN_FC_IS_QOS_DATA(fc))
855 return TXRX_CONTINUE;
856
857 /* remove the qos control field, update frame type and meta-data */
858 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
859 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
860 /* change frame type to non QOS */
861 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
862 hdr->frame_control = cpu_to_le16(fc);
863
864 return TXRX_CONTINUE;
865}
866
867static ieee80211_txrx_result
Johannes Berg571ecf62007-07-27 15:43:22 +0200868ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
869{
870 if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400871 rx->sdata->type != IEEE80211_IF_TYPE_STA &&
872 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +0200873 /* Pass both encrypted and unencrypted EAPOL frames to user
874 * space for processing. */
875 if (!rx->local->apdev)
876 return TXRX_DROP;
877 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
878 ieee80211_msg_normal);
879 return TXRX_QUEUED;
880 }
881
882 if (unlikely(rx->sdata->ieee802_1x &&
883 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
884 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
885 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
886 !ieee80211_is_eapol(rx->skb))) {
887#ifdef CONFIG_MAC80211_DEBUG
888 struct ieee80211_hdr *hdr =
889 (struct ieee80211_hdr *) rx->skb->data;
Joe Perches0795af52007-10-03 17:59:30 -0700890 DECLARE_MAC_BUF(mac);
891 printk(KERN_DEBUG "%s: dropped frame from %s"
Johannes Berg571ecf62007-07-27 15:43:22 +0200892 " (unauthorized port)\n", rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700893 print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +0200894#endif /* CONFIG_MAC80211_DEBUG */
895 return TXRX_DROP;
896 }
897
898 return TXRX_CONTINUE;
899}
900
901static ieee80211_txrx_result
902ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
903{
Johannes Berg3017b802007-08-28 17:01:53 -0400904 /*
Johannes Berg7848ba72007-09-14 11:10:25 -0400905 * Pass through unencrypted frames if the hardware has
906 * decrypted them already.
Johannes Berg3017b802007-08-28 17:01:53 -0400907 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400908 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
Johannes Berg571ecf62007-07-27 15:43:22 +0200909 return TXRX_CONTINUE;
910
911 /* Drop unencrypted frames if key is set. */
912 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
913 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
914 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
915 (rx->key || rx->sdata->drop_unencrypted) &&
916 (rx->sdata->eapol == 0 ||
917 !ieee80211_is_eapol(rx->skb)))) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400918 if (net_ratelimit())
919 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
920 "encryption\n", rx->dev->name);
Johannes Berg571ecf62007-07-27 15:43:22 +0200921 return TXRX_DROP;
922 }
923 return TXRX_CONTINUE;
924}
925
926static ieee80211_txrx_result
927ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
928{
929 struct net_device *dev = rx->dev;
930 struct ieee80211_local *local = rx->local;
931 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
932 u16 fc, hdrlen, ethertype;
933 u8 *payload;
934 u8 dst[ETH_ALEN];
935 u8 src[ETH_ALEN];
936 struct sk_buff *skb = rx->skb, *skb2;
937 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -0700938 DECLARE_MAC_BUF(mac);
939 DECLARE_MAC_BUF(mac2);
940 DECLARE_MAC_BUF(mac3);
941 DECLARE_MAC_BUF(mac4);
Johannes Berg571ecf62007-07-27 15:43:22 +0200942
943 fc = rx->fc;
944 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
945 return TXRX_CONTINUE;
946
947 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
948 return TXRX_DROP;
949
950 hdrlen = ieee80211_get_hdrlen(fc);
951
952 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
953 * header
954 * IEEE 802.11 address fields:
955 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
956 * 0 0 DA SA BSSID n/a
957 * 0 1 DA BSSID SA n/a
958 * 1 0 BSSID SA DA n/a
959 * 1 1 RA TA DA SA
960 */
961
962 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
963 case IEEE80211_FCTL_TODS:
964 /* BSSID SA DA */
965 memcpy(dst, hdr->addr3, ETH_ALEN);
966 memcpy(src, hdr->addr2, ETH_ALEN);
967
968 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
969 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400970 if (net_ratelimit())
971 printk(KERN_DEBUG "%s: dropped ToDS frame "
Joe Perches0795af52007-10-03 17:59:30 -0700972 "(BSSID=%s SA=%s DA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400973 dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700974 print_mac(mac, hdr->addr1),
975 print_mac(mac2, hdr->addr2),
976 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +0200977 return TXRX_DROP;
978 }
979 break;
980 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
981 /* RA TA DA SA */
982 memcpy(dst, hdr->addr3, ETH_ALEN);
983 memcpy(src, hdr->addr4, ETH_ALEN);
984
985 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400986 if (net_ratelimit())
987 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
Joe Perches0795af52007-10-03 17:59:30 -0700988 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
Johannes Berg1a84f3f2007-08-28 17:01:52 -0400989 rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700990 print_mac(mac, hdr->addr1),
991 print_mac(mac2, hdr->addr2),
992 print_mac(mac3, hdr->addr3),
993 print_mac(mac4, hdr->addr4));
Johannes Berg571ecf62007-07-27 15:43:22 +0200994 return TXRX_DROP;
995 }
996 break;
997 case IEEE80211_FCTL_FROMDS:
998 /* DA BSSID SA */
999 memcpy(dst, hdr->addr1, ETH_ALEN);
1000 memcpy(src, hdr->addr3, ETH_ALEN);
1001
John W. Linvilleb3316152007-08-28 17:01:55 -04001002 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1003 (is_multicast_ether_addr(dst) &&
1004 !compare_ether_addr(src, dev->dev_addr)))
Johannes Berg571ecf62007-07-27 15:43:22 +02001005 return TXRX_DROP;
Johannes Berg571ecf62007-07-27 15:43:22 +02001006 break;
1007 case 0:
1008 /* DA SA BSSID */
1009 memcpy(dst, hdr->addr1, ETH_ALEN);
1010 memcpy(src, hdr->addr2, ETH_ALEN);
1011
1012 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1013 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -07001014 printk(KERN_DEBUG "%s: dropped IBSS frame "
1015 "(DA=%s SA=%s BSSID=%s)\n",
1016 dev->name,
1017 print_mac(mac, hdr->addr1),
1018 print_mac(mac2, hdr->addr2),
1019 print_mac(mac3, hdr->addr3));
Johannes Berg571ecf62007-07-27 15:43:22 +02001020 }
1021 return TXRX_DROP;
1022 }
1023 break;
1024 }
1025
1026 payload = skb->data + hdrlen;
1027
1028 if (unlikely(skb->len - hdrlen < 8)) {
1029 if (net_ratelimit()) {
1030 printk(KERN_DEBUG "%s: RX too short data frame "
1031 "payload\n", dev->name);
1032 }
1033 return TXRX_DROP;
1034 }
1035
1036 ethertype = (payload[6] << 8) | payload[7];
1037
1038 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1039 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1040 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1041 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1042 * replace EtherType */
1043 skb_pull(skb, hdrlen + 6);
1044 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1045 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1046 } else {
1047 struct ethhdr *ehdr;
1048 __be16 len;
1049 skb_pull(skb, hdrlen);
1050 len = htons(skb->len);
1051 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1052 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1053 memcpy(ehdr->h_source, src, ETH_ALEN);
1054 ehdr->h_proto = len;
1055 }
1056 skb->dev = dev;
1057
1058 skb2 = NULL;
1059
Stephen Hemminger68aae112007-08-24 11:29:34 -07001060 dev->stats.rx_packets++;
1061 dev->stats.rx_bytes += skb->len;
Johannes Berg571ecf62007-07-27 15:43:22 +02001062
1063 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
Jiri Slabybadffb72007-08-28 17:01:54 -04001064 || sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1065 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001066 if (is_multicast_ether_addr(skb->data)) {
1067 /* send multicast frames both to higher layers in
1068 * local net stack and back to the wireless media */
1069 skb2 = skb_copy(skb, GFP_ATOMIC);
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001070 if (!skb2 && net_ratelimit())
Johannes Berg571ecf62007-07-27 15:43:22 +02001071 printk(KERN_DEBUG "%s: failed to clone "
1072 "multicast frame\n", dev->name);
1073 } else {
1074 struct sta_info *dsta;
1075 dsta = sta_info_get(local, skb->data);
1076 if (dsta && !dsta->dev) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001077 if (net_ratelimit())
1078 printk(KERN_DEBUG "Station with null "
1079 "dev structure!\n");
Johannes Berg571ecf62007-07-27 15:43:22 +02001080 } else if (dsta && dsta->dev == dev) {
1081 /* Destination station is associated to this
1082 * AP, so send the frame directly to it and
1083 * do not pass the frame to local net stack.
1084 */
1085 skb2 = skb;
1086 skb = NULL;
1087 }
1088 if (dsta)
1089 sta_info_put(dsta);
1090 }
1091 }
1092
1093 if (skb) {
1094 /* deliver to local stack */
1095 skb->protocol = eth_type_trans(skb, dev);
1096 memset(skb->cb, 0, sizeof(skb->cb));
1097 netif_rx(skb);
1098 }
1099
1100 if (skb2) {
1101 /* send to wireless media */
1102 skb2->protocol = __constant_htons(ETH_P_802_3);
1103 skb_set_network_header(skb2, 0);
1104 skb_set_mac_header(skb2, 0);
1105 dev_queue_xmit(skb2);
1106 }
1107
1108 return TXRX_QUEUED;
1109}
1110
1111static ieee80211_txrx_result
1112ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1113{
1114 struct ieee80211_sub_if_data *sdata;
1115
Jiri Slabybadffb72007-08-28 17:01:54 -04001116 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Johannes Berg571ecf62007-07-27 15:43:22 +02001117 return TXRX_DROP;
1118
1119 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1120 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1121 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1122 !rx->local->user_space_mlme) {
1123 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1124 } else {
1125 /* Management frames are sent to hostapd for processing */
1126 if (!rx->local->apdev)
1127 return TXRX_DROP;
1128 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
1129 ieee80211_msg_normal);
1130 }
1131 return TXRX_QUEUED;
1132}
1133
1134static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1135 struct ieee80211_local *local,
1136 ieee80211_rx_handler *handlers,
1137 struct ieee80211_txrx_data *rx,
1138 struct sta_info *sta)
1139{
1140 ieee80211_rx_handler *handler;
1141 ieee80211_txrx_result res = TXRX_DROP;
1142
1143 for (handler = handlers; *handler != NULL; handler++) {
1144 res = (*handler)(rx);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001145
1146 switch (res) {
1147 case TXRX_CONTINUE:
1148 continue;
1149 case TXRX_DROP:
1150 I802_DEBUG_INC(local->rx_handlers_drop);
1151 if (sta)
1152 sta->rx_dropped++;
1153 break;
1154 case TXRX_QUEUED:
1155 I802_DEBUG_INC(local->rx_handlers_queued);
Johannes Berg571ecf62007-07-27 15:43:22 +02001156 break;
1157 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001158 break;
Johannes Berg571ecf62007-07-27 15:43:22 +02001159 }
1160
Johannes Berg8e6f0032007-07-27 15:43:22 +02001161 if (res == TXRX_DROP)
Johannes Berg571ecf62007-07-27 15:43:22 +02001162 dev_kfree_skb(rx->skb);
Johannes Berg571ecf62007-07-27 15:43:22 +02001163 return res;
1164}
1165
1166static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1167 ieee80211_rx_handler *handlers,
1168 struct ieee80211_txrx_data *rx,
1169 struct sta_info *sta)
1170{
1171 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1172 TXRX_CONTINUE)
1173 dev_kfree_skb(rx->skb);
1174}
1175
1176static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1177 struct ieee80211_hdr *hdr,
1178 struct sta_info *sta,
1179 struct ieee80211_txrx_data *rx)
1180{
1181 int keyidx, hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -07001182 DECLARE_MAC_BUF(mac);
1183 DECLARE_MAC_BUF(mac2);
Johannes Berg571ecf62007-07-27 15:43:22 +02001184
1185 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1186 if (rx->skb->len >= hdrlen + 4)
1187 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1188 else
1189 keyidx = -1;
1190
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001191 if (net_ratelimit())
1192 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001193 "failure from %s to %s keyidx=%d\n",
1194 dev->name, print_mac(mac, hdr->addr2),
1195 print_mac(mac2, hdr->addr1), keyidx);
Johannes Berg571ecf62007-07-27 15:43:22 +02001196
1197 if (!sta) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001198 /*
1199 * Some hardware seem to generate incorrect Michael MIC
1200 * reports; ignore them to avoid triggering countermeasures.
1201 */
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001202 if (net_ratelimit())
1203 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
Joe Perches0795af52007-10-03 17:59:30 -07001204 "error for unknown address %s\n",
1205 dev->name, print_mac(mac, 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 "
Johannes Bergaa0daf02007-09-10 14:15:25 +02001212 "error for a frame with no PROTECTED flag (src "
Joe Perches0795af52007-10-03 17:59:30 -07001213 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001214 goto ignore;
1215 }
1216
Johannes Berg7848ba72007-09-14 11:10:25 -04001217 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
Johannes Bergaa0daf02007-09-10 14:15:25 +02001218 /*
1219 * APs with pairwise keys should never receive Michael MIC
1220 * errors for non-zero keyidx because these are reserved for
1221 * group keys and only the AP is sending real multicast
1222 * frames in the BSS.
1223 */
Johannes Bergeb063c12007-08-28 17:01:53 -04001224 if (net_ratelimit())
1225 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1226 "a frame with non-zero keyidx (%d)"
Joe Perches0795af52007-10-03 17:59:30 -07001227 " (src %s)\n", dev->name, keyidx,
1228 print_mac(mac, hdr->addr2));
Johannes Bergeb063c12007-08-28 17:01:53 -04001229 goto ignore;
Johannes Berg571ecf62007-07-27 15:43:22 +02001230 }
1231
1232 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1233 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1234 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
Johannes Berg1a84f3f2007-08-28 17:01:52 -04001235 if (net_ratelimit())
1236 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1237 "error for a frame that cannot be encrypted "
Joe Perches0795af52007-10-03 17:59:30 -07001238 "(fc=0x%04x) (src %s)\n",
1239 dev->name, rx->fc, print_mac(mac, hdr->addr2));
Johannes Berg571ecf62007-07-27 15:43:22 +02001240 goto ignore;
1241 }
1242
Johannes Bergeb063c12007-08-28 17:01:53 -04001243 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
Johannes Berg571ecf62007-07-27 15:43:22 +02001244 ignore:
1245 dev_kfree_skb(rx->skb);
1246 rx->skb = NULL;
1247}
1248
1249ieee80211_rx_handler ieee80211_rx_handlers[] =
1250{
1251 ieee80211_rx_h_if_stats,
1252 ieee80211_rx_h_monitor,
1253 ieee80211_rx_h_passive_scan,
1254 ieee80211_rx_h_check,
Johannes Berg570bd532007-07-27 15:43:22 +02001255 ieee80211_rx_h_load_key,
Johannes Berg571ecf62007-07-27 15:43:22 +02001256 ieee80211_rx_h_sta_process,
1257 ieee80211_rx_h_ccmp_decrypt,
1258 ieee80211_rx_h_tkip_decrypt,
1259 ieee80211_rx_h_wep_weak_iv_detection,
1260 ieee80211_rx_h_wep_decrypt,
1261 ieee80211_rx_h_defragment,
1262 ieee80211_rx_h_ps_poll,
1263 ieee80211_rx_h_michael_mic_verify,
1264 /* this must be after decryption - so header is counted in MPDU mic
1265 * must be before pae and data, so QOS_DATA format frames
1266 * are not passed to user space by these functions
1267 */
1268 ieee80211_rx_h_remove_qos_control,
1269 ieee80211_rx_h_802_1x_pae,
1270 ieee80211_rx_h_drop_unencrypted,
1271 ieee80211_rx_h_data,
1272 ieee80211_rx_h_mgmt,
1273 NULL
1274};
1275
1276/* main receive path */
1277
Johannes Berg23a24de2007-07-27 15:43:22 +02001278static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1279 u8 *bssid, struct ieee80211_txrx_data *rx,
1280 struct ieee80211_hdr *hdr)
1281{
1282 int multicast = is_multicast_ether_addr(hdr->addr1);
1283
1284 switch (sdata->type) {
1285 case IEEE80211_IF_TYPE_STA:
1286 if (!bssid)
1287 return 0;
1288 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001289 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001290 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001291 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001292 } else if (!multicast &&
1293 compare_ether_addr(sdata->dev->dev_addr,
1294 hdr->addr1) != 0) {
Jiri Slaby13262ff2007-08-28 17:01:54 -04001295 if (!(sdata->flags & IEEE80211_SDATA_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001296 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001297 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001298 }
1299 break;
1300 case IEEE80211_IF_TYPE_IBSS:
1301 if (!bssid)
1302 return 0;
1303 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001304 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001305 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001306 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001307 } else if (!multicast &&
1308 compare_ether_addr(sdata->dev->dev_addr,
1309 hdr->addr1) != 0) {
Jiri Slaby13262ff2007-08-28 17:01:54 -04001310 if (!(sdata->flags & IEEE80211_SDATA_PROMISC))
Johannes Berg23a24de2007-07-27 15:43:22 +02001311 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001312 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001313 } else if (!rx->sta)
1314 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1315 bssid, hdr->addr2);
1316 break;
1317 case IEEE80211_IF_TYPE_AP:
1318 if (!bssid) {
1319 if (compare_ether_addr(sdata->dev->dev_addr,
1320 hdr->addr1))
1321 return 0;
1322 } else if (!ieee80211_bssid_match(bssid,
1323 sdata->dev->dev_addr)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001324 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001325 return 0;
Jiri Slabybadffb72007-08-28 17:01:54 -04001326 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001327 }
Jiri Slabybadffb72007-08-28 17:01:54 -04001328 if (sdata->dev == sdata->local->mdev &&
1329 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
Johannes Berg23a24de2007-07-27 15:43:22 +02001330 /* do not receive anything via
1331 * master device when not scanning */
1332 return 0;
1333 break;
1334 case IEEE80211_IF_TYPE_WDS:
1335 if (bssid ||
1336 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1337 return 0;
1338 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1339 return 0;
1340 break;
1341 }
1342
1343 return 1;
1344}
1345
Johannes Berg571ecf62007-07-27 15:43:22 +02001346/*
1347 * This is the receive path handler. It is called by a low level driver when an
1348 * 802.11 MPDU is received from the hardware.
1349 */
1350void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1351 struct ieee80211_rx_status *status)
1352{
1353 struct ieee80211_local *local = hw_to_local(hw);
1354 struct ieee80211_sub_if_data *sdata;
1355 struct sta_info *sta;
1356 struct ieee80211_hdr *hdr;
1357 struct ieee80211_txrx_data rx;
1358 u16 type;
Johannes Berg23a24de2007-07-27 15:43:22 +02001359 int radiotap_len = 0, prepres;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001360 struct ieee80211_sub_if_data *prev = NULL;
1361 struct sk_buff *skb_new;
1362 u8 *bssid;
Johannes Berg571ecf62007-07-27 15:43:22 +02001363
1364 if (status->flag & RX_FLAG_RADIOTAP) {
1365 radiotap_len = ieee80211_get_radiotap_len(skb->data);
1366 skb_pull(skb, radiotap_len);
1367 }
1368
Johannes Bergd4e46a32007-09-14 11:10:24 -04001369 /*
1370 * key references are protected using RCU and this requires that
1371 * we are in a read-site RCU section during receive processing
1372 */
1373 rcu_read_lock();
1374
Johannes Berg571ecf62007-07-27 15:43:22 +02001375 hdr = (struct ieee80211_hdr *) skb->data;
1376 memset(&rx, 0, sizeof(rx));
1377 rx.skb = skb;
1378 rx.local = local;
1379
1380 rx.u.rx.status = status;
1381 rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
1382 type = rx.fc & IEEE80211_FCTL_FTYPE;
1383 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1384 local->dot11ReceivedFragmentCount++;
Johannes Berg571ecf62007-07-27 15:43:22 +02001385
Johannes Berg23a24de2007-07-27 15:43:22 +02001386 if (skb->len >= 16) {
Johannes Berg571ecf62007-07-27 15:43:22 +02001387 sta = rx.sta = sta_info_get(local, hdr->addr2);
Johannes Berg23a24de2007-07-27 15:43:22 +02001388 if (sta) {
1389 rx.dev = rx.sta->dev;
1390 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1391 }
1392 } else
Johannes Berg571ecf62007-07-27 15:43:22 +02001393 sta = rx.sta = NULL;
1394
Johannes Berg571ecf62007-07-27 15:43:22 +02001395 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1396 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1397 goto end;
1398 }
1399
1400 if (unlikely(local->sta_scanning))
Jiri Slabybadffb72007-08-28 17:01:54 -04001401 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
Johannes Berg571ecf62007-07-27 15:43:22 +02001402
1403 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1404 sta) != TXRX_CONTINUE)
1405 goto end;
1406 skb = rx.skb;
1407
1408 skb_push(skb, radiotap_len);
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001409 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
Johannes Berg23a24de2007-07-27 15:43:22 +02001410 !local->iff_promiscs && !is_multicast_ether_addr(hdr->addr1)) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001411 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg571ecf62007-07-27 15:43:22 +02001412 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
Johannes Berg23a24de2007-07-27 15:43:22 +02001413 rx.sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001414 sta_info_put(sta);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001415 rcu_read_unlock();
Johannes Berg8e6f0032007-07-27 15:43:22 +02001416 return;
1417 }
Johannes Berg571ecf62007-07-27 15:43:22 +02001418
Johannes Berg8e6f0032007-07-27 15:43:22 +02001419 bssid = ieee80211_get_bssid(hdr, skb->len - radiotap_len);
Johannes Berg571ecf62007-07-27 15:43:22 +02001420
Johannes Berg8e6f0032007-07-27 15:43:22 +02001421 read_lock(&local->sub_if_lock);
1422 list_for_each_entry(sdata, &local->sub_if_list, list) {
Jiri Slabybadffb72007-08-28 17:01:54 -04001423 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
Johannes Berg23a24de2007-07-27 15:43:22 +02001424
Johannes Berg2a8a9a82007-08-28 17:01:52 -04001425 if (!netif_running(sdata->dev))
1426 continue;
1427
Johannes Berg23a24de2007-07-27 15:43:22 +02001428 prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
1429 /* prepare_for_handlers can change sta */
1430 sta = rx.sta;
1431
1432 if (!prepres)
1433 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001434
Johannes Berg340e11f2007-07-27 15:43:22 +02001435 /*
1436 * frame is destined for this interface, but if it's not
1437 * also for the previous one we handle that after the
1438 * loop to avoid copying the SKB once too much
1439 */
1440
1441 if (!prev) {
1442 prev = sdata;
1443 continue;
Johannes Berg8e6f0032007-07-27 15:43:22 +02001444 }
Johannes Berg340e11f2007-07-27 15:43:22 +02001445
1446 /*
1447 * frame was destined for the previous interface
1448 * so invoke RX handlers for it
1449 */
1450
1451 skb_new = skb_copy(skb, GFP_ATOMIC);
1452 if (!skb_new) {
1453 if (net_ratelimit())
1454 printk(KERN_DEBUG "%s: failed to copy "
1455 "multicast frame for %s",
1456 local->mdev->name, prev->dev->name);
1457 continue;
1458 }
1459 rx.skb = skb_new;
1460 rx.dev = prev->dev;
1461 rx.sdata = prev;
1462 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1463 &rx, sta);
Johannes Berg8e6f0032007-07-27 15:43:22 +02001464 prev = sdata;
Johannes Berg571ecf62007-07-27 15:43:22 +02001465 }
Johannes Berg8e6f0032007-07-27 15:43:22 +02001466 if (prev) {
1467 rx.skb = skb;
1468 rx.dev = prev->dev;
1469 rx.sdata = prev;
1470 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1471 &rx, sta);
1472 } else
1473 dev_kfree_skb(skb);
1474 read_unlock(&local->sub_if_lock);
Johannes Berg571ecf62007-07-27 15:43:22 +02001475
Johannes Berg8e6f0032007-07-27 15:43:22 +02001476 end:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001477 rcu_read_unlock();
1478
Johannes Berg571ecf62007-07-27 15:43:22 +02001479 if (sta)
1480 sta_info_put(sta);
1481}
1482EXPORT_SYMBOL(__ieee80211_rx);
1483
1484/* This is a version of the rx handler that can be called from hard irq
1485 * context. Post the skb on the queue and schedule the tasklet */
1486void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1487 struct ieee80211_rx_status *status)
1488{
1489 struct ieee80211_local *local = hw_to_local(hw);
1490
1491 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1492
1493 skb->dev = local->mdev;
1494 /* copy status into skb->cb for use by tasklet */
1495 memcpy(skb->cb, status, sizeof(*status));
1496 skb->pkt_type = IEEE80211_RX_MSG;
1497 skb_queue_tail(&local->skb_queue, skb);
1498 tasklet_schedule(&local->tasklet);
1499}
1500EXPORT_SYMBOL(ieee80211_rx_irqsafe);