~ubuntu-branches/ubuntu/hardy/linux-backports-modules-2.6.24/hardy-security

« back to all changes in this revision

Viewing changes to updates/wireless/iwlwifi/mac80211/modified/net/mac80211/ieee80211.c

  • Committer: Bazaar Package Importer
  • Author(s): , Ben Collins
  • Date: 2008-04-02 06:59:04 UTC
  • Revision ID: james.westby@ubuntu.com-20080402065904-e5knh2gn2hms3xbb
Tags: 2.6.24-14.11
[Ben Collins]

* iwlwifi: Update to iwlwifi-1.2.25 and mac80211-10.0.4
  - LP: #200950
* ubuntu: Slight cleanups to module hiearchy and Makefiles
* mac80211: Enable LED triggers
* iwlwifi: Add LED trigger support (rx and tx only)
  - LP: #176090

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2002-2005, Instant802 Networks, Inc.
 
3
 * Copyright 2005-2006, Devicescape Software, Inc.
 
4
 * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 2 as
 
8
 * published by the Free Software Foundation.
 
9
 */
 
10
 
 
11
#include <net/mac80211.h>
 
12
#include <net/ieee80211_radiotap.h>
 
13
#include <linux/module.h>
 
14
#include <linux/init.h>
 
15
#include <linux/netdevice.h>
 
16
#include <linux/types.h>
 
17
#include <linux/slab.h>
 
18
#include <linux/skbuff.h>
 
19
#include <linux/etherdevice.h>
 
20
#include <linux/if_arp.h>
 
21
#include <linux/wireless.h>
 
22
#include <linux/rtnetlink.h>
 
23
#include <net/iw_handler.h>
 
24
#include <linux/compiler.h>
 
25
#include <linux/bitmap.h>
 
26
#include <net/cfg80211.h>
 
27
#include <asm/unaligned.h>
 
28
 
 
29
#include "ieee80211_common.h"
 
30
#include "ieee80211_i.h"
 
31
#include "ieee80211_rate.h"
 
32
#include "wep.h"
 
33
#include "wpa.h"
 
34
#include "tkip.h"
 
35
#include "wme.h"
 
36
#include "aes_ccm.h"
 
37
#include "ieee80211_led.h"
 
38
#include "ieee80211_cfg.h"
 
39
#include "debugfs.h"
 
40
#include "debugfs_netdev.h"
 
41
#include "debugfs_key.h"
 
42
 
 
43
/* privid for wiphys to determine whether they belong to us or not */
 
44
void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
 
45
 
 
46
/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
 
47
/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
 
48
static const unsigned char rfc1042_header[] =
 
49
        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 
50
 
 
51
/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
 
52
static const unsigned char bridge_tunnel_header[] =
 
53
        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
 
54
 
 
55
/* No encapsulation header if EtherType < 0x600 (=length) */
 
56
static const unsigned char eapol_header[] =
 
57
        { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
 
58
 
 
59
 
 
60
/*
 
61
 * For seeing transmitted packets on monitor interfaces
 
62
 * we have a radiotap header too.
 
63
 */
 
64
struct iwlwifi_ieee80211_tx_status_rtap_hdr {
 
65
        struct ieee80211_radiotap_header hdr;
 
66
        __le16 tx_flags;
 
67
        u8 data_retries;
 
68
} __attribute__ ((packed));
 
69
 
 
70
 
 
71
static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
 
72
                                              struct ieee80211_hdr *hdr)
 
73
{
 
74
        /* Set the sequence number for this frame. */
 
75
        hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
 
76
 
 
77
        /* Increase the sequence number. */
 
78
        sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
 
79
}
 
80
 
 
81
struct ieee80211_key_conf *
 
82
ieee80211_key_data2conf(struct ieee80211_local *local,
 
83
                        const struct ieee80211_key *data)
 
84
{
 
85
        struct ieee80211_key_conf *conf;
 
86
 
 
87
        conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
 
88
        if (!conf)
 
89
                return NULL;
 
90
 
 
91
        conf->hw_key_idx = data->hw_key_idx;
 
92
        conf->alg = data->alg;
 
93
        conf->keylen = data->keylen;
 
94
        conf->flags = 0;
 
95
        if (data->force_sw_encrypt)
 
96
                conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
 
97
        conf->keyidx = data->keyidx;
 
98
        if (data->default_tx_key)
 
99
                conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
 
100
        if (local->default_wep_only)
 
101
                conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
 
102
        memcpy(conf->key, data->key, data->keylen);
 
103
 
 
104
        return conf;
 
105
}
 
106
 
 
107
struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
 
108
                                          int idx, size_t key_len, gfp_t flags)
 
109
{
 
110
        struct ieee80211_key *key;
 
111
 
 
112
        key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
 
113
        if (!key)
 
114
                return NULL;
 
115
        kref_init(&key->kref);
 
116
        return key;
 
117
}
 
118
 
 
119
static void ieee80211_key_release(struct kref *kref)
 
120
{
 
121
        struct ieee80211_key *key;
 
122
 
 
123
        key = container_of(kref, struct ieee80211_key, kref);
 
124
        if (key->alg == ALG_CCMP)
 
125
                ieee80211_aes_key_free(key->u.ccmp.tfm);
 
126
        ieee80211_debugfs_key_remove(key);
 
127
        kfree(key);
 
128
}
 
129
 
 
130
void ieee80211_key_free(struct ieee80211_key *key)
 
131
{
 
132
        if (key)
 
133
                kref_put(&key->kref, ieee80211_key_release);
 
134
}
 
135
 
 
136
static int rate_list_match(const int *rate_list, int rate)
 
137
{
 
138
        int i;
 
139
 
 
140
        if (!rate_list)
 
141
                return 0;
 
142
 
 
143
        for (i = 0; rate_list[i] >= 0; i++)
 
144
                if (rate_list[i] == rate)
 
145
                        return 1;
 
146
 
 
147
        return 0;
 
148
}
 
149
 
 
150
 
 
151
void ieee80211_prepare_rates(struct ieee80211_local *local,
 
152
                             struct ieee80211_hw_mode *mode)
 
153
{
 
154
        int i;
 
155
 
 
156
        for (i = 0; i < mode->num_rates; i++) {
 
157
                struct ieee80211_rate *rate = &mode->rates[i];
 
158
 
 
159
                rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
 
160
                                 IEEE80211_RATE_BASIC);
 
161
 
 
162
                if (local->supp_rates[mode->mode]) {
 
163
                        if (!rate_list_match(local->supp_rates[mode->mode],
 
164
                                             rate->rate))
 
165
                                continue;
 
166
                }
 
167
 
 
168
                rate->flags |= IEEE80211_RATE_SUPPORTED;
 
169
 
 
170
                /* Use configured basic rate set if it is available. If not,
 
171
                 * use defaults that are sane for most cases. */
 
172
                if (local->basic_rates[mode->mode]) {
 
173
                        if (rate_list_match(local->basic_rates[mode->mode],
 
174
                                            rate->rate))
 
175
                                rate->flags |= IEEE80211_RATE_BASIC;
 
176
                } else switch (mode->mode) {
 
177
                case MODE_IEEE80211A:
 
178
                        if (rate->rate == 60 || rate->rate == 120 ||
 
179
                            rate->rate == 240)
 
180
                                rate->flags |= IEEE80211_RATE_BASIC;
 
181
                        break;
 
182
                case MODE_IEEE80211B:
 
183
                        if (rate->rate == 10 || rate->rate == 20)
 
184
                                rate->flags |= IEEE80211_RATE_BASIC;
 
185
                        break;
 
186
                case MODE_ATHEROS_TURBO:
 
187
                        if (rate->rate == 120 || rate->rate == 240 ||
 
188
                            rate->rate == 480)
 
189
                                rate->flags |= IEEE80211_RATE_BASIC;
 
190
                        break;
 
191
                case MODE_IEEE80211G:
 
192
                        if (rate->rate == 10 || rate->rate == 20 ||
 
193
                            rate->rate == 55 || rate->rate == 110)
 
194
                                rate->flags |= IEEE80211_RATE_BASIC;
 
195
                        break;
 
196
                }
 
197
 
 
198
                /* Set ERP and MANDATORY flags based on phymode */
 
199
                switch (mode->mode) {
 
200
                case MODE_IEEE80211A:
 
201
                        if (rate->rate == 60 || rate->rate == 120 ||
 
202
                            rate->rate == 240)
 
203
                                rate->flags |= IEEE80211_RATE_MANDATORY;
 
204
                        break;
 
205
                case MODE_IEEE80211B:
 
206
                        if (rate->rate == 10)
 
207
                                rate->flags |= IEEE80211_RATE_MANDATORY;
 
208
                        break;
 
209
                case MODE_ATHEROS_TURBO:
 
210
                        break;
 
211
                case MODE_IEEE80211G:
 
212
                        if (rate->rate == 10 || rate->rate == 20 ||
 
213
                            rate->rate == 55 || rate->rate == 110 ||
 
214
                            rate->rate == 60 || rate->rate == 120 ||
 
215
                            rate->rate == 240)
 
216
                                rate->flags |= IEEE80211_RATE_MANDATORY;
 
217
                        break;
 
218
                }
 
219
                if (ieee80211_is_erp_rate(mode->mode, rate->rate))
 
220
                        rate->flags |= IEEE80211_RATE_ERP;
 
221
        }
 
222
}
 
223
 
 
224
 
 
225
static void ieee80211_key_threshold_notify(struct net_device *dev,
 
226
                                           struct ieee80211_key *key,
 
227
                                           struct sta_info *sta)
 
228
{
 
229
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
230
        struct sk_buff *skb;
 
231
        struct ieee80211_msg_key_notification *msg;
 
232
 
 
233
        /* if no one will get it anyway, don't even allocate it.
 
234
         * unlikely because this is only relevant for APs
 
235
         * where the device must be open... */
 
236
        if (unlikely(!local->apdev))
 
237
                return;
 
238
 
 
239
        skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
 
240
                            sizeof(struct ieee80211_msg_key_notification));
 
241
        if (!skb)
 
242
                return;
 
243
 
 
244
        skb_reserve(skb, sizeof(struct ieee80211_frame_info));
 
245
        msg = (struct ieee80211_msg_key_notification *)
 
246
                skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
 
247
        msg->tx_rx_count = key->tx_rx_count;
 
248
        memcpy(msg->ifname, dev->name, IFNAMSIZ);
 
249
        if (sta)
 
250
                memcpy(msg->addr, sta->addr, ETH_ALEN);
 
251
        else
 
252
                memset(msg->addr, 0xff, ETH_ALEN);
 
253
 
 
254
        key->tx_rx_count = 0;
 
255
 
 
256
        ieee80211_rx_mgmt(local, skb, NULL,
 
257
                          ieee80211_msg_key_threshold_notification);
 
258
}
 
259
 
 
260
 
 
261
static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
 
262
                                unsigned int type)
 
263
{
 
264
        u16 fc;
 
265
 
 
266
        fc = le16_to_cpu(hdr->frame_control);
 
267
 
 
268
        switch (fc & IEEE80211_FCTL_FTYPE) {
 
269
        case IEEE80211_FTYPE_DATA:
 
270
                switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 
271
                case IEEE80211_FCTL_TODS:
 
272
                        return hdr->addr1;
 
273
                case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 
274
                        return NULL;
 
275
                case IEEE80211_FCTL_FROMDS:
 
276
                        return hdr->addr2;
 
277
                case 0:
 
278
                        return hdr->addr3;
 
279
                }
 
280
                break;
 
281
        case IEEE80211_FTYPE_MGMT:
 
282
                return hdr->addr3;
 
283
        case IEEE80211_FTYPE_CTL:
 
284
                if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
 
285
                        return hdr->addr1;
 
286
                else if ((fc & IEEE80211_FCTL_STYPE) ==
 
287
                         IEEE80211_STYPE_BACK_REQ) {
 
288
                        switch (type) {
 
289
                                case IEEE80211_IF_TYPE_STA:
 
290
                                        return hdr->addr2;
 
291
                                case IEEE80211_IF_TYPE_AP:
 
292
                                        return hdr->addr1;
 
293
                        }
 
294
                }
 
295
                else
 
296
                        return NULL;
 
297
                break;
 
298
        }
 
299
 
 
300
        return NULL;
 
301
}
 
302
 
 
303
int iwlwifi_ieee80211_get_hdrlen(u16 fc)
 
304
{
 
305
        int hdrlen = 24;
 
306
 
 
307
        switch (fc & IEEE80211_FCTL_FTYPE) {
 
308
        case IEEE80211_FTYPE_DATA:
 
309
                if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
 
310
                        hdrlen = 30; /* Addr4 */
 
311
                /*
 
312
                 * The QoS Control field is two bytes and its presence is
 
313
                 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
 
314
                 * hdrlen if that bit is set.
 
315
                 * This works by masking out the bit and shifting it to
 
316
                 * bit position 1 so the result has the value 0 or 2.
 
317
                 */
 
318
                hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
 
319
                                >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
 
320
                break;
 
321
        case IEEE80211_FTYPE_CTL:
 
322
                /*
 
323
                 * ACK and CTS are 10 bytes, all others 16. To see how
 
324
                 * to get this condition consider
 
325
                 *   subtype mask:   0b0000000011110000 (0x00F0)
 
326
                 *   ACK subtype:    0b0000000011010000 (0x00D0)
 
327
                 *   CTS subtype:    0b0000000011000000 (0x00C0)
 
328
                 *   bits that matter:         ^^^      (0x00E0)
 
329
                 *   value of those: 0b0000000011000000 (0x00C0)
 
330
                 */
 
331
                if ((fc & 0xE0) == 0xC0)
 
332
                        hdrlen = 10;
 
333
                else
 
334
                        hdrlen = 16;
 
335
                break;
 
336
        }
 
337
 
 
338
        return hdrlen;
 
339
}
 
340
EXPORT_SYMBOL(iwlwifi_ieee80211_get_hdrlen);
 
341
 
 
342
int iwlwifi_iwlwifi_ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
 
343
{
 
344
        const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
 
345
        int hdrlen;
 
346
 
 
347
        if (unlikely(skb->len < 10))
 
348
                return 0;
 
349
        hdrlen = iwlwifi_ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
 
350
        if (unlikely(hdrlen > skb->len))
 
351
                return 0;
 
352
        return hdrlen;
 
353
}
 
354
EXPORT_SYMBOL(iwlwifi_iwlwifi_ieee80211_get_hdrlen_from_skb);
 
355
 
 
356
static int ieee80211_get_radiotap_len(struct sk_buff *skb)
 
357
{
 
358
        struct ieee80211_radiotap_header *hdr =
 
359
                (struct ieee80211_radiotap_header *) skb->data;
 
360
 
 
361
        return le16_to_cpu(hdr->it_len);
 
362
}
 
363
 
 
364
#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
 
365
static void ieee80211_dump_frame(const char *ifname, const char *title,
 
366
                                 const struct sk_buff *skb)
 
367
{
 
368
        const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
369
        u16 fc;
 
370
        int hdrlen;
 
371
 
 
372
        printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
 
373
        if (skb->len < 4) {
 
374
                printk("\n");
 
375
                return;
 
376
        }
 
377
 
 
378
        fc = le16_to_cpu(hdr->frame_control);
 
379
        hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
 
380
        if (hdrlen > skb->len)
 
381
                hdrlen = skb->len;
 
382
        if (hdrlen >= 4)
 
383
                printk(" FC=0x%04x DUR=0x%04x",
 
384
                       fc, le16_to_cpu(hdr->duration_id));
 
385
        if (hdrlen >= 10)
 
386
                printk(" A1=" MAC_FMT, MAC_ARG(hdr->addr1));
 
387
        if (hdrlen >= 16)
 
388
                printk(" A2=" MAC_FMT, MAC_ARG(hdr->addr2));
 
389
        if (hdrlen >= 24)
 
390
                printk(" A3=" MAC_FMT, MAC_ARG(hdr->addr3));
 
391
        if (hdrlen >= 30)
 
392
                printk(" A4=" MAC_FMT, MAC_ARG(hdr->addr4));
 
393
        printk("\n");
 
394
}
 
395
#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
 
396
static inline void ieee80211_dump_frame(const char *ifname, const char *title,
 
397
                                        struct sk_buff *skb)
 
398
{
 
399
}
 
400
#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
 
401
 
 
402
 
 
403
static int ieee80211_is_eapol(const struct sk_buff *skb)
 
404
{
 
405
        const struct ieee80211_hdr *hdr;
 
406
        u16 fc;
 
407
        int hdrlen;
 
408
 
 
409
        if (unlikely(skb->len < 10))
 
410
                return 0;
 
411
 
 
412
        hdr = (const struct ieee80211_hdr *) skb->data;
 
413
        fc = le16_to_cpu(hdr->frame_control);
 
414
 
 
415
        if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
 
416
                return 0;
 
417
 
 
418
        hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
 
419
 
 
420
        if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
 
421
                     memcmp(skb->data + hdrlen, eapol_header,
 
422
                            sizeof(eapol_header)) == 0))
 
423
                return 1;
 
424
 
 
425
        return 0;
 
426
}
 
427
 
 
428
 
 
429
static ieee80211_txrx_result
 
430
ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
 
431
{
 
432
        struct rate_control_extra extra;
 
433
 
 
434
        memset(&extra, 0, sizeof(extra));
 
435
        extra.mode = tx->u.tx.mode;
 
436
        extra.mgmt_data = tx->sdata &&
 
437
                tx->sdata->type == IEEE80211_IF_TYPE_MGMT;
 
438
        extra.ethertype = tx->ethertype;
 
439
 
 
440
        tx->u.tx.rate = rate_control_get_rate(tx->local, tx->dev, tx->skb,
 
441
                                              &extra);
 
442
        if (unlikely(extra.probe != NULL)) {
 
443
                tx->u.tx.control->flags |= IEEE80211_TXCTL_RATE_CTRL_PROBE;
 
444
                tx->u.tx.probe_last_frag = 1;
 
445
                tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
 
446
                tx->u.tx.rate = extra.probe;
 
447
        } else {
 
448
                tx->u.tx.control->alt_retry_rate = -1;
 
449
        }
 
450
        if (!tx->u.tx.rate)
 
451
                return TXRX_DROP;
 
452
        if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
 
453
            tx->sdata->use_protection && tx->fragmented &&
 
454
            extra.nonerp) {
 
455
                tx->u.tx.last_frag_rate = tx->u.tx.rate;
 
456
                tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
 
457
 
 
458
                tx->u.tx.rate = extra.nonerp;
 
459
                tx->u.tx.control->rate = extra.nonerp;
 
460
                tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
 
461
        } else {
 
462
                tx->u.tx.last_frag_rate = tx->u.tx.rate;
 
463
                tx->u.tx.control->rate = tx->u.tx.rate;
 
464
        }
 
465
        tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
 
466
        if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
 
467
            tx->local->short_preamble &&
 
468
            (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
 
469
                tx->u.tx.short_preamble = 1;
 
470
                tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
 
471
        }
 
472
 
 
473
        /* only data unicast frame */
 
474
        if ((tx->u.tx.rate) && tx->skb && tx->sdata && tx->u.tx.unicast &&
 
475
            (tx->sdata->type == IEEE80211_IF_TYPE_STA ||
 
476
             tx->sdata->type == IEEE80211_IF_TYPE_IBSS )&& !extra.mgmt_data) {
 
477
                struct ieee80211_hdr *hdr;
 
478
                u16 fc;
 
479
 
 
480
                hdr = (struct ieee80211_hdr *) tx->skb->data;
 
481
                fc = le16_to_cpu(hdr->frame_control);
 
482
 
 
483
                if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
 
484
                        tx->sdata->u.sta.last_rate = tx->u.tx.rate->rate *
 
485
                                                                     100000;
 
486
        }
 
487
 
 
488
        return TXRX_CONTINUE;
 
489
}
 
490
 
 
491
 
 
492
static ieee80211_txrx_result
 
493
ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
 
494
{
 
495
        if (tx->sta)
 
496
                tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
 
497
        else
 
498
                tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
 
499
 
 
500
        if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
 
501
                tx->key = NULL;
 
502
        else if (tx->sta && tx->sta->key)
 
503
                tx->key = tx->sta->key;
 
504
        else if (tx->sdata->default_key)
 
505
                tx->key = tx->sdata->default_key;
 
506
        else if (tx->sdata->drop_unencrypted &&
 
507
                 !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
 
508
                I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
 
509
                return TXRX_DROP;
 
510
        } else
 
511
                tx->key = NULL;
 
512
 
 
513
        if (tx->key) {
 
514
                tx->key->tx_rx_count++;
 
515
                if (unlikely(tx->local->key_tx_rx_threshold &&
 
516
                             tx->key->tx_rx_count >
 
517
                             tx->local->key_tx_rx_threshold)) {
 
518
                        ieee80211_key_threshold_notify(tx->dev, tx->key,
 
519
                                                       tx->sta);
 
520
                }
 
521
        }
 
522
 
 
523
        return TXRX_CONTINUE;
 
524
}
 
525
 
 
526
 
 
527
static ieee80211_txrx_result
 
528
ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
 
529
{
 
530
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
 
531
        size_t hdrlen, per_fragm, num_fragm, payload_len, left;
 
532
        struct sk_buff **frags, *first, *frag;
 
533
        int i;
 
534
        u16 seq;
 
535
        u8 *pos;
 
536
        int frag_threshold = tx->local->fragmentation_threshold;
 
537
 
 
538
        if (!tx->fragmented)
 
539
                return TXRX_CONTINUE;
 
540
 
 
541
        first = tx->skb;
 
542
 
 
543
        hdrlen = iwlwifi_ieee80211_get_hdrlen(tx->fc);
 
544
        payload_len = first->len - hdrlen;
 
545
        per_fragm = frag_threshold - hdrlen - FCS_LEN;
 
546
        num_fragm = (payload_len + per_fragm - 1) / per_fragm;
 
547
 
 
548
        frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
 
549
        if (!frags)
 
550
                goto fail;
 
551
 
 
552
        hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
 
553
        seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
 
554
        pos = first->data + hdrlen + per_fragm;
 
555
        left = payload_len - per_fragm;
 
556
        for (i = 0; i < num_fragm - 1; i++) {
 
557
                struct ieee80211_hdr *fhdr;
 
558
                size_t copylen;
 
559
 
 
560
                if (left <= 0)
 
561
                        goto fail;
 
562
 
 
563
                /* reserve enough extra head and tail room for possible
 
564
                 * encryption */
 
565
                frag = frags[i] =
 
566
                        dev_alloc_skb(tx->local->tx_headroom +
 
567
                                      frag_threshold +
 
568
                                      IEEE80211_ENCRYPT_HEADROOM +
 
569
                                      IEEE80211_ENCRYPT_TAILROOM);
 
570
                if (!frag)
 
571
                        goto fail;
 
572
                /* Make sure that all fragments use the same priority so
 
573
                 * that they end up using the same TX queue */
 
574
                frag->priority = first->priority;
 
575
                skb_reserve(frag, tx->local->tx_headroom +
 
576
                                  IEEE80211_ENCRYPT_HEADROOM);
 
577
                fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
 
578
                memcpy(fhdr, first->data, hdrlen);
 
579
                if (i == num_fragm - 2)
 
580
                        fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
 
581
                fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
 
582
                copylen = left > per_fragm ? per_fragm : left;
 
583
                memcpy(skb_put(frag, copylen), pos, copylen);
 
584
 
 
585
                pos += copylen;
 
586
                left -= copylen;
 
587
        }
 
588
        skb_trim(first, hdrlen + per_fragm);
 
589
 
 
590
        tx->u.tx.num_extra_frag = num_fragm - 1;
 
591
        tx->u.tx.extra_frag = frags;
 
592
 
 
593
        return TXRX_CONTINUE;
 
594
 
 
595
 fail:
 
596
        printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
 
597
        if (frags) {
 
598
                for (i = 0; i < num_fragm - 1; i++)
 
599
                        if (frags[i])
 
600
                                dev_kfree_skb(frags[i]);
 
601
                kfree(frags);
 
602
        }
 
603
        I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
 
604
        return TXRX_DROP;
 
605
}
 
606
 
 
607
 
 
608
static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
 
609
{
 
610
        if (tx->key->force_sw_encrypt) {
 
611
                if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
 
612
                        return -1;
 
613
        } else {
 
614
                tx->u.tx.control->key_idx = tx->key->hw_key_idx;
 
615
                if (tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
 
616
                        if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
 
617
                            NULL)
 
618
                                return -1;
 
619
                }
 
620
        }
 
621
        return 0;
 
622
}
 
623
 
 
624
 
 
625
void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
 
626
{
 
627
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
 
628
 
 
629
        hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
 
630
        if (tx->u.tx.extra_frag) {
 
631
                struct ieee80211_hdr *fhdr;
 
632
                int i;
 
633
                for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
 
634
                        fhdr = (struct ieee80211_hdr *)
 
635
                                tx->u.tx.extra_frag[i]->data;
 
636
                        fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
 
637
                }
 
638
        }
 
639
}
 
640
 
 
641
 
 
642
static ieee80211_txrx_result
 
643
ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
 
644
{
 
645
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
 
646
        u16 fc;
 
647
 
 
648
        fc = le16_to_cpu(hdr->frame_control);
 
649
 
 
650
        if (!tx->key || tx->key->alg != ALG_WEP ||
 
651
            ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
 
652
             ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
 
653
              (fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
 
654
                return TXRX_CONTINUE;
 
655
 
 
656
        tx->u.tx.control->iv_len = WEP_IV_LEN;
 
657
        tx->u.tx.control->icv_len = WEP_ICV_LEN;
 
658
        ieee80211_tx_set_iswep(tx);
 
659
 
 
660
        if (wep_encrypt_skb(tx, tx->skb) < 0) {
 
661
                I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
 
662
                return TXRX_DROP;
 
663
        }
 
664
 
 
665
        if (tx->u.tx.extra_frag) {
 
666
                int i;
 
667
                for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
 
668
                        if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
 
669
                                I802_DEBUG_INC(tx->local->
 
670
                                               tx_handlers_drop_wep);
 
671
                                return TXRX_DROP;
 
672
                        }
 
673
                }
 
674
        }
 
675
 
 
676
        return TXRX_CONTINUE;
 
677
}
 
678
 
 
679
 
 
680
static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
 
681
                                    int rate, int erp, int short_preamble)
 
682
{
 
683
        int dur;
 
684
 
 
685
        /* calculate duration (in microseconds, rounded up to next higher
 
686
         * integer if it includes a fractional microsecond) to send frame of
 
687
         * len bytes (does not include FCS) at the given rate. Duration will
 
688
         * also include SIFS.
 
689
         *
 
690
         * rate is in 100 kbps, so divident is multiplied by 10 in the
 
691
         * DIV_ROUND_UP() operations.
 
692
         */
 
693
 
 
694
        if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
 
695
            local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
 
696
                /*
 
697
                 * OFDM:
 
698
                 *
 
699
                 * N_DBPS = DATARATE x 4
 
700
                 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
 
701
                 *      (16 = SIGNAL time, 6 = tail bits)
 
702
                 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
 
703
                 *
 
704
                 * T_SYM = 4 usec
 
705
                 * 802.11a - 17.5.2: aSIFSTime = 16 usec
 
706
                 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
 
707
                 *      signal ext = 6 usec
 
708
                 */
 
709
                /* FIX: Atheros Turbo may have different (shorter) duration? */
 
710
                dur = 16; /* SIFS + signal ext */
 
711
                dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
 
712
                dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
 
713
                dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
 
714
                                        4 * rate); /* T_SYM x N_SYM */
 
715
        } else {
 
716
                /*
 
717
                 * 802.11b or 802.11g with 802.11b compatibility:
 
718
                 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
 
719
                 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
 
720
                 *
 
721
                 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
 
722
                 * aSIFSTime = 10 usec
 
723
                 * aPreambleLength = 144 usec or 72 usec with short preamble
 
724
                 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
 
725
                 */
 
726
                dur = 10; /* aSIFSTime = 10 usec */
 
727
                dur += short_preamble ? (72 + 24) : (144 + 48);
 
728
 
 
729
                dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
 
730
        }
 
731
 
 
732
        return dur;
 
733
}
 
734
 
 
735
 
 
736
/* Exported duration function for driver use */
 
737
__le16 iwlwifi_ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
 
738
                                        size_t frame_len, int rate)
 
739
{
 
740
        struct ieee80211_local *local = hw_to_local(hw);
 
741
        u16 dur;
 
742
        int erp;
 
743
 
 
744
        erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
 
745
        dur = ieee80211_frame_duration(local, frame_len, rate,
 
746
                                       erp, local->short_preamble);
 
747
 
 
748
        return cpu_to_le16(dur);
 
749
}
 
750
EXPORT_SYMBOL(iwlwifi_ieee80211_generic_frame_duration);
 
751
 
 
752
 
 
753
static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
 
754
                              int next_frag_len)
 
755
{
 
756
        int rate, mrate, erp, dur, i;
 
757
        struct ieee80211_rate *txrate = tx->u.tx.rate;
 
758
        struct ieee80211_local *local = tx->local;
 
759
        struct ieee80211_hw_mode *mode = tx->u.tx.mode;
 
760
 
 
761
        erp = txrate->flags & IEEE80211_RATE_ERP;
 
762
 
 
763
        /*
 
764
         * data and mgmt (except PS Poll):
 
765
         * - during CFP: 32768
 
766
         * - during contention period:
 
767
         *   if addr1 is group address: 0
 
768
         *   if more fragments = 0 and addr1 is individual address: time to
 
769
         *      transmit one ACK plus SIFS
 
770
         *   if more fragments = 1 and addr1 is individual address: time to
 
771
         *      transmit next fragment plus 2 x ACK plus 3 x SIFS
 
772
         *
 
773
         * IEEE 802.11, 9.6:
 
774
         * - control response frame (CTS or ACK) shall be transmitted using the
 
775
         *   same rate as the immediately previous frame in the frame exchange
 
776
         *   sequence, if this rate belongs to the PHY mandatory rates, or else
 
777
         *   at the highest possible rate belonging to the PHY rates in the
 
778
         *   BSSBasicRateSet
 
779
         */
 
780
 
 
781
        if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
 
782
                /* TODO: These control frames are not currently sent by
 
783
                 * 80211.o, but should they be implemented, this function
 
784
                 * needs to be updated to support duration field calculation.
 
785
                 *
 
786
                 * RTS: time needed to transmit pending data/mgmt frame plus
 
787
                 *    one CTS frame plus one ACK frame plus 3 x SIFS
 
788
                 * CTS: duration of immediately previous RTS minus time
 
789
                 *    required to transmit CTS and its SIFS
 
790
                 * ACK: 0 if immediately previous directed data/mgmt had
 
791
                 *    more=0, with more=1 duration in ACK frame is duration
 
792
                 *    from previous frame minus time needed to transmit ACK
 
793
                 *    and its SIFS
 
794
                 * PS Poll: BIT(15) | BIT(14) | aid
 
795
                 */
 
796
                return 0;
 
797
        }
 
798
 
 
799
        /* data/mgmt */
 
800
        if (0 /* FIX: data/mgmt during CFP */)
 
801
                return 32768;
 
802
 
 
803
        if (group_addr) /* Group address as the destination - no ACK */
 
804
                return 0;
 
805
 
 
806
        /* Individual destination address:
 
807
         * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
 
808
         * CTS and ACK frames shall be transmitted using the highest rate in
 
809
         * basic rate set that is less than or equal to the rate of the
 
810
         * immediately previous frame and that is using the same modulation
 
811
         * (CCK or OFDM). If no basic rate set matches with these requirements,
 
812
         * the highest mandatory rate of the PHY that is less than or equal to
 
813
         * the rate of the previous frame is used.
 
814
         * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
 
815
         */
 
816
        rate = -1;
 
817
        mrate = 10; /* use 1 Mbps if everything fails */
 
818
        for (i = 0; i < mode->num_rates; i++) {
 
819
                struct ieee80211_rate *r = &mode->rates[i];
 
820
                if (r->rate > txrate->rate)
 
821
                        break;
 
822
 
 
823
                if (IEEE80211_RATE_MODULATION(txrate->flags) !=
 
824
                    IEEE80211_RATE_MODULATION(r->flags))
 
825
                        continue;
 
826
 
 
827
                if (r->flags & IEEE80211_RATE_BASIC)
 
828
                        rate = r->rate;
 
829
                else if (r->flags & IEEE80211_RATE_MANDATORY)
 
830
                        mrate = r->rate;
 
831
        }
 
832
        if (rate == -1) {
 
833
                /* No matching basic rate found; use highest suitable mandatory
 
834
                 * PHY rate */
 
835
                rate = mrate;
 
836
        }
 
837
 
 
838
        /* Time needed to transmit ACK
 
839
         * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
 
840
         * to closest integer */
 
841
 
 
842
        dur = ieee80211_frame_duration(local, 10, rate, erp,
 
843
                                       local->short_preamble);
 
844
 
 
845
        if (next_frag_len) {
 
846
                /* Frame is fragmented: duration increases with time needed to
 
847
                 * transmit next fragment plus ACK and 2 x SIFS. */
 
848
                dur *= 2; /* ACK + SIFS */
 
849
                /* next fragment */
 
850
                dur += ieee80211_frame_duration(local, next_frag_len,
 
851
                                                txrate->rate, erp,
 
852
                                                local->short_preamble);
 
853
        }
 
854
 
 
855
        return dur;
 
856
}
 
857
 
 
858
 
 
859
static ieee80211_txrx_result
 
860
ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
 
861
{
 
862
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
 
863
        u16 dur;
 
864
        struct ieee80211_tx_control *control = tx->u.tx.control;
 
865
        struct ieee80211_hw_mode *mode = tx->u.tx.mode;
 
866
 
 
867
        if (!is_multicast_ether_addr(hdr->addr1)) {
 
868
                if (tx->skb->len + FCS_LEN > tx->local->rts_threshold &&
 
869
                    tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
 
870
                        control->flags |= IEEE80211_TXCTL_USE_RTS_CTS;
 
871
                        control->retry_limit =
 
872
                                tx->local->long_retry_limit;
 
873
                } else {
 
874
                        control->retry_limit =
 
875
                                tx->local->short_retry_limit;
 
876
                }
 
877
        } else {
 
878
                control->retry_limit = 1;
 
879
        }
 
880
 
 
881
        if (tx->fragmented) {
 
882
                /* Do not use multiple retry rates when sending fragmented
 
883
                 * frames.
 
884
                 * TODO: The last fragment could still use multiple retry
 
885
                 * rates. */
 
886
                control->alt_retry_rate = -1;
 
887
        }
 
888
 
 
889
        /* Use CTS protection for unicast frames sent using extended rates if
 
890
         * there are associated non-ERP stations and RTS/CTS is not configured
 
891
         * for the frame. */
 
892
        if (mode->mode == MODE_IEEE80211G &&
 
893
            (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
 
894
            tx->u.tx.unicast && tx->sdata->use_protection &&
 
895
            !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
 
896
                control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
 
897
 
 
898
        /* Setup duration field for the first fragment of the frame. Duration
 
899
         * for remaining fragments will be updated when they are being sent
 
900
         * to low-level driver in ieee80211_tx(). */
 
901
        dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
 
902
                                 tx->fragmented ? tx->u.tx.extra_frag[0]->len :
 
903
                                 0);
 
904
        hdr->duration_id = cpu_to_le16(dur);
 
905
 
 
906
        if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
 
907
            (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
 
908
                struct ieee80211_rate *rate;
 
909
 
 
910
                /* Do not use multiple retry rates when using RTS/CTS */
 
911
                control->alt_retry_rate = -1;
 
912
 
 
913
                /* Use min(data rate, max base rate) as CTS/RTS rate */
 
914
                rate = tx->u.tx.rate;
 
915
                while (rate > mode->rates &&
 
916
                       !(rate->flags & IEEE80211_RATE_BASIC))
 
917
                        rate--;
 
918
 
 
919
                control->rts_cts_rate = rate->val;
 
920
                control->rts_rate = rate;
 
921
        }
 
922
 
 
923
        if (tx->sta) {
 
924
                tx->sta->tx_packets++;
 
925
                tx->sta->tx_fragments++;
 
926
                tx->sta->tx_bytes += tx->skb->len;
 
927
                if (tx->u.tx.extra_frag) {
 
928
                        int i;
 
929
                        tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
 
930
                        for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
 
931
                                tx->sta->tx_bytes +=
 
932
                                        tx->u.tx.extra_frag[i]->len;
 
933
                        }
 
934
                }
 
935
        }
 
936
 
 
937
        return TXRX_CONTINUE;
 
938
}
 
939
 
 
940
 
 
941
static ieee80211_txrx_result
 
942
ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
 
943
{
 
944
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
945
        struct sk_buff *skb = tx->skb;
 
946
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
947
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
948
        u32 sta_flags;
 
949
 
 
950
        if (unlikely(tx->local->sta_sw_scanning != 0) &&
 
951
            ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
 
952
             (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
 
953
                return TXRX_DROP;
 
954
 
 
955
        if (tx->u.tx.ps_buffered)
 
956
                return TXRX_CONTINUE;
 
957
 
 
958
        sta_flags = tx->sta ? tx->sta->flags : 0;
 
959
 
 
960
        if (likely(tx->u.tx.unicast)) {
 
961
                if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
 
962
                             tx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
 
963
                             (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
 
964
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
965
                        printk(KERN_DEBUG "%s: dropped data frame to not "
 
966
                               "associated station " MAC_FMT "\n",
 
967
                               tx->dev->name, MAC_ARG(hdr->addr1));
 
968
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
969
                        I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
 
970
                        return TXRX_DROP;
 
971
                }
 
972
        } else {
 
973
                if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
 
974
                             tx->local->num_sta == 0 &&
 
975
                             !tx->local->allow_broadcast_always &&
 
976
                             tx->sdata->type != IEEE80211_IF_TYPE_IBSS)) {
 
977
                        /*
 
978
                         * No associated STAs - no need to send multicast
 
979
                         * frames.
 
980
                         */
 
981
                        return TXRX_DROP;
 
982
                }
 
983
                return TXRX_CONTINUE;
 
984
        }
 
985
 
 
986
        if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
 
987
                     !(sta_flags & WLAN_STA_AUTHORIZED))) {
 
988
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
989
                printk(KERN_DEBUG "%s: dropped frame to " MAC_FMT
 
990
                       " (unauthorized port)\n", tx->dev->name,
 
991
                       MAC_ARG(hdr->addr1));
 
992
#endif
 
993
                I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
 
994
                return TXRX_DROP;
 
995
        }
 
996
 
 
997
        return TXRX_CONTINUE;
 
998
}
 
999
 
 
1000
static ieee80211_txrx_result
 
1001
ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
 
1002
{
 
1003
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
 
1004
 
 
1005
        if (iwlwifi_ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
 
1006
                ieee80211_include_sequence(tx->sdata, hdr);
 
1007
 
 
1008
        return TXRX_CONTINUE;
 
1009
}
 
1010
 
 
1011
/* This function is called whenever the AP is about to exceed the maximum limit
 
1012
 * of buffered frames for power saving STAs. This situation should not really
 
1013
 * happen often during normal operation, so dropping the oldest buffered packet
 
1014
 * from each queue should be OK to make some room for new frames. */
 
1015
static void purge_old_ps_buffers(struct ieee80211_local *local)
 
1016
{
 
1017
        int total = 0, purged = 0;
 
1018
        struct sk_buff *skb;
 
1019
        struct ieee80211_sub_if_data *sdata;
 
1020
        struct sta_info *sta;
 
1021
 
 
1022
        read_lock(&local->sub_if_lock);
 
1023
        list_for_each_entry(sdata, &local->sub_if_list, list) {
 
1024
                struct ieee80211_if_ap *ap;
 
1025
                if (sdata->dev == local->mdev ||
 
1026
                    sdata->type != IEEE80211_IF_TYPE_AP)
 
1027
                        continue;
 
1028
                ap = &sdata->u.ap;
 
1029
                skb = skb_dequeue(&ap->ps_bc_buf);
 
1030
                if (skb) {
 
1031
                        purged++;
 
1032
                        dev_kfree_skb(skb);
 
1033
                }
 
1034
                total += skb_queue_len(&ap->ps_bc_buf);
 
1035
        }
 
1036
        read_unlock(&local->sub_if_lock);
 
1037
 
 
1038
        spin_lock_bh(&local->sta_lock);
 
1039
        list_for_each_entry(sta, &local->sta_list, list) {
 
1040
                skb = skb_dequeue(&sta->ps_tx_buf);
 
1041
                if (skb) {
 
1042
                        purged++;
 
1043
                        dev_kfree_skb(skb);
 
1044
                }
 
1045
                total += skb_queue_len(&sta->ps_tx_buf);
 
1046
        }
 
1047
        spin_unlock_bh(&local->sta_lock);
 
1048
 
 
1049
        local->total_ps_buffered = total;
 
1050
        printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
 
1051
               local->mdev->name, purged);
 
1052
}
 
1053
 
 
1054
 
 
1055
static inline ieee80211_txrx_result
 
1056
ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
 
1057
{
 
1058
        /* broadcast/multicast frame */
 
1059
        /* If any of the associated stations is in power save mode,
 
1060
         * the frame is buffered to be sent after DTIM beacon frame */
 
1061
        if ((tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) &&
 
1062
            tx->sdata->type != IEEE80211_IF_TYPE_WDS &&
 
1063
            tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
 
1064
            !(tx->fc & IEEE80211_FCTL_ORDER)) {
 
1065
                if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
 
1066
                        purge_old_ps_buffers(tx->local);
 
1067
                if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
 
1068
                    AP_MAX_BC_BUFFER) {
 
1069
                        if (net_ratelimit()) {
 
1070
                                printk(KERN_DEBUG "%s: BC TX buffer full - "
 
1071
                                       "dropping the oldest frame\n",
 
1072
                                       tx->dev->name);
 
1073
                        }
 
1074
                        dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
 
1075
                } else
 
1076
                        tx->local->total_ps_buffered++;
 
1077
                skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
 
1078
                return TXRX_QUEUED;
 
1079
        }
 
1080
 
 
1081
        return TXRX_CONTINUE;
 
1082
}
 
1083
 
 
1084
 
 
1085
static inline ieee80211_txrx_result
 
1086
ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
 
1087
{
 
1088
        struct sta_info *sta = tx->sta;
 
1089
 
 
1090
        if (unlikely(!sta ||
 
1091
                     ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
 
1092
                      (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
 
1093
                return TXRX_CONTINUE;
 
1094
 
 
1095
        if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
 
1096
                struct ieee80211_tx_packet_data *pkt_data;
 
1097
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
1098
                printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS buffer (entries "
 
1099
                       "before %d)\n",
 
1100
                       MAC_ARG(sta->addr), sta->aid,
 
1101
                       skb_queue_len(&sta->ps_tx_buf));
 
1102
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
1103
                sta->flags |= WLAN_STA_TIM;
 
1104
                if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
 
1105
                        purge_old_ps_buffers(tx->local);
 
1106
                if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
 
1107
                        struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
 
1108
                        if (net_ratelimit()) {
 
1109
                                printk(KERN_DEBUG "%s: STA " MAC_FMT " TX "
 
1110
                                       "buffer full - dropping oldest frame\n",
 
1111
                                       tx->dev->name, MAC_ARG(sta->addr));
 
1112
                        }
 
1113
                        dev_kfree_skb(old);
 
1114
                } else
 
1115
                        tx->local->total_ps_buffered++;
 
1116
                /* Queue frame to be sent after STA sends an PS Poll frame */
 
1117
                if (skb_queue_empty(&sta->ps_tx_buf)) {
 
1118
                        if (tx->local->ops->set_tim)
 
1119
                                tx->local->ops->set_tim(local_to_hw(tx->local),
 
1120
                                                       sta->aid, 1);
 
1121
                        if (tx->sdata->bss)
 
1122
                                bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
 
1123
                }
 
1124
                pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
 
1125
                pkt_data->jiffies = jiffies;
 
1126
                skb_queue_tail(&sta->ps_tx_buf, tx->skb);
 
1127
                return TXRX_QUEUED;
 
1128
        }
 
1129
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
1130
        else if (unlikely(sta->flags & WLAN_STA_PS)) {
 
1131
                printk(KERN_DEBUG "%s: STA " MAC_FMT " in PS mode, but pspoll "
 
1132
                       "set -> send frame\n", tx->dev->name,
 
1133
                       MAC_ARG(sta->addr));
 
1134
        }
 
1135
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
1136
        sta->pspoll = 0;
 
1137
 
 
1138
        return TXRX_CONTINUE;
 
1139
}
 
1140
 
 
1141
 
 
1142
static ieee80211_txrx_result
 
1143
ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
 
1144
{
 
1145
        if (unlikely(tx->u.tx.ps_buffered))
 
1146
                return TXRX_CONTINUE;
 
1147
 
 
1148
        if (tx->u.tx.unicast)
 
1149
                return ieee80211_tx_h_unicast_ps_buf(tx);
 
1150
        else
 
1151
                return ieee80211_tx_h_multicast_ps_buf(tx);
 
1152
}
 
1153
 
 
1154
 
 
1155
/*
 
1156
 * deal with packet injection down monitor interface
 
1157
 * with Radiotap Header -- only called for monitor mode interface
 
1158
 */
 
1159
 
 
1160
static ieee80211_txrx_result
 
1161
__ieee80211_parse_tx_radiotap(
 
1162
        struct ieee80211_txrx_data *tx,
 
1163
        struct sk_buff *skb, struct ieee80211_tx_control *control)
 
1164
{
 
1165
        /*
 
1166
         * this is the moment to interpret and discard the radiotap header that
 
1167
         * must be at the start of the packet injected in Monitor mode
 
1168
         *
 
1169
         * Need to take some care with endian-ness since radiotap
 
1170
         * args are little-endian
 
1171
         */
 
1172
 
 
1173
        struct ieee80211_radiotap_iterator iterator;
 
1174
        struct ieee80211_radiotap_header *rthdr =
 
1175
                (struct ieee80211_radiotap_header *) skb->data;
 
1176
        struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
 
1177
        int ret = iwlwifi_ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
 
1178
 
 
1179
        /*
 
1180
         * default control situation for all injected packets
 
1181
         * FIXME: this does not suit all usage cases, expand to allow control
 
1182
         */
 
1183
 
 
1184
        control->retry_limit = 1; /* no retry */
 
1185
        control->key_idx = -1; /* no encryption key */
 
1186
        control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
 
1187
                            IEEE80211_TXCTL_USE_CTS_PROTECT);
 
1188
        control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT |
 
1189
                          IEEE80211_TXCTL_NO_ACK;
 
1190
        control->antenna_sel_tx = 0; /* default to default antenna */
 
1191
 
 
1192
        /*
 
1193
         * for every radiotap entry that is present
 
1194
         * (iwlwifi_ieee80211_radiotap_iterator_next returns -ENOENT when no more
 
1195
         * entries present, or -EINVAL on error)
 
1196
         */
 
1197
 
 
1198
        while (!ret) {
 
1199
                int i, target_rate;
 
1200
 
 
1201
                ret = iwlwifi_ieee80211_radiotap_iterator_next(&iterator);
 
1202
 
 
1203
                if (ret)
 
1204
                        continue;
 
1205
 
 
1206
                /* see if this argument is something we can use */
 
1207
                switch (iterator.this_arg_index) {
 
1208
                /*
 
1209
                 * You must take care when dereferencing iterator.this_arg
 
1210
                 * for multibyte types... the pointer is not aligned.  Use
 
1211
                 * get_unaligned((type *)iterator.this_arg) to dereference
 
1212
                 * iterator.this_arg for type "type" safely on all arches.
 
1213
                */
 
1214
                case IEEE80211_RADIOTAP_RATE:
 
1215
                        /*
 
1216
                         * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
 
1217
                         * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
 
1218
                         */
 
1219
                        target_rate = (*iterator.this_arg) * 5;
 
1220
                        for (i = 0; i < mode->num_rates; i++) {
 
1221
                                struct ieee80211_rate *r = &mode->rates[i];
 
1222
 
 
1223
                                if (r->rate > target_rate)
 
1224
                                        continue;
 
1225
 
 
1226
                                control->rate = r;
 
1227
 
 
1228
                                if (r->flags & IEEE80211_RATE_PREAMBLE2)
 
1229
                                        control->tx_rate = r->val2;
 
1230
                                else
 
1231
                                        control->tx_rate = r->val;
 
1232
 
 
1233
                                /* end on exact match */
 
1234
                                if (r->rate == target_rate)
 
1235
                                        i = mode->num_rates;
 
1236
                        }
 
1237
                        break;
 
1238
 
 
1239
                case IEEE80211_RADIOTAP_ANTENNA:
 
1240
                        /*
 
1241
                         * radiotap uses 0 for 1st ant, mac80211 is 1 for
 
1242
                         * 1st ant
 
1243
                         */
 
1244
                        control->antenna_sel_tx = (*iterator.this_arg) + 1;
 
1245
                        break;
 
1246
 
 
1247
                case IEEE80211_RADIOTAP_DBM_TX_POWER:
 
1248
                        control->power_level = *iterator.this_arg;
 
1249
                        break;
 
1250
 
 
1251
                case IEEE80211_RADIOTAP_FLAGS:
 
1252
                        if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
 
1253
                                /*
 
1254
                                 * this indicates that the skb we have been
 
1255
                                 * handed has the 32-bit FCS CRC at the end...
 
1256
                                 * we should react to that by snipping it off
 
1257
                                 * because it will be recomputed and added
 
1258
                                 * on transmission
 
1259
                                 */
 
1260
                                if (skb->len < (iterator.max_length + FCS_LEN))
 
1261
                                        return TXRX_DROP;
 
1262
 
 
1263
                                skb_trim(skb, skb->len - FCS_LEN);
 
1264
                        }
 
1265
                        break;
 
1266
 
 
1267
                default:
 
1268
                        break;
 
1269
                }
 
1270
        }
 
1271
 
 
1272
        if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
 
1273
                return TXRX_DROP;
 
1274
 
 
1275
        /*
 
1276
         * remove the radiotap header
 
1277
         * iterator->max_length was sanity-checked against
 
1278
         * skb->len by iterator init
 
1279
         */
 
1280
        skb_pull(skb, iterator.max_length);
 
1281
 
 
1282
        return TXRX_CONTINUE;
 
1283
}
 
1284
 
 
1285
 
 
1286
static ieee80211_txrx_result inline
 
1287
__ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
 
1288
                       struct sk_buff *skb,
 
1289
                       struct net_device *dev,
 
1290
                       struct ieee80211_tx_control *control)
 
1291
{
 
1292
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
1293
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
1294
        struct ieee80211_sub_if_data *sdata;
 
1295
        ieee80211_txrx_result res = TXRX_CONTINUE;
 
1296
 
 
1297
        int hdrlen;
 
1298
 
 
1299
        memset(tx, 0, sizeof(*tx));
 
1300
        tx->skb = skb;
 
1301
        tx->dev = dev; /* use original interface */
 
1302
        tx->local = local;
 
1303
        tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
1304
        tx->sta = iwlwifi_sta_info_get(local, hdr->addr1);
 
1305
        tx->fc = le16_to_cpu(hdr->frame_control);
 
1306
 
 
1307
        /*
 
1308
         * set defaults for things that can be set by
 
1309
         * injected radiotap headers
 
1310
         */
 
1311
        control->power_level = local->hw.conf.power_level;
 
1312
        control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
 
1313
        if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
 
1314
                control->antenna_sel_tx = tx->sta->antenna_sel_tx;
 
1315
 
 
1316
        /* process and remove the injection radiotap header */
 
1317
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
1318
        if (unlikely(sdata->type == IEEE80211_IF_TYPE_MNTR)) {
 
1319
                if (__ieee80211_parse_tx_radiotap(tx, skb, control) ==
 
1320
                                                                TXRX_DROP) {
 
1321
                        return TXRX_DROP;
 
1322
                }
 
1323
                /*
 
1324
                 * we removed the radiotap header after this point,
 
1325
                 * we filled control with what we could use
 
1326
                 * set to the actual ieee header now
 
1327
                 */
 
1328
                hdr = (struct ieee80211_hdr *) skb->data;
 
1329
                res = TXRX_QUEUED; /* indication it was monitor packet */
 
1330
        }
 
1331
 
 
1332
        tx->u.tx.control = control;
 
1333
        tx->u.tx.unicast = !is_multicast_ether_addr(hdr->addr1);
 
1334
        if (is_multicast_ether_addr(hdr->addr1))
 
1335
                control->flags |= IEEE80211_TXCTL_NO_ACK;
 
1336
        else
 
1337
                control->flags &= ~IEEE80211_TXCTL_NO_ACK;
 
1338
        tx->fragmented = local->fragmentation_threshold <
 
1339
                IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
 
1340
                skb->len + FCS_LEN > local->fragmentation_threshold &&
 
1341
                (!local->ops->set_frag_threshold);
 
1342
        if (!tx->sta)
 
1343
                control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
 
1344
        else if (tx->sta->clear_dst_mask) {
 
1345
                control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
 
1346
                tx->sta->clear_dst_mask = 0;
 
1347
        }
 
1348
        hdrlen = iwlwifi_ieee80211_get_hdrlen(tx->fc);
 
1349
        if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
 
1350
                u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
 
1351
                tx->ethertype = (pos[0] << 8) | pos[1];
 
1352
        }
 
1353
        control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
 
1354
 
 
1355
        return res;
 
1356
}
 
1357
 
 
1358
static int inline is_ieee80211_device(struct net_device *dev,
 
1359
                                      struct net_device *master)
 
1360
{
 
1361
        return (wdev_priv(dev->ieee80211_ptr) ==
 
1362
                wdev_priv(master->ieee80211_ptr));
 
1363
}
 
1364
 
 
1365
/* Device in tx->dev has a reference added; use dev_put(tx->dev) when
 
1366
 * finished with it. */
 
1367
static int inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
 
1368
                                       struct sk_buff *skb,
 
1369
                                       struct net_device *mdev,
 
1370
                                       struct ieee80211_tx_control *control)
 
1371
{
 
1372
        struct ieee80211_tx_packet_data *pkt_data;
 
1373
        struct net_device *dev;
 
1374
 
 
1375
        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
 
1376
        dev = dev_get_by_index(&init_net, pkt_data->ifindex);
 
1377
        if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
 
1378
                dev_put(dev);
 
1379
                dev = NULL;
 
1380
        }
 
1381
        if (unlikely(!dev))
 
1382
                return -ENODEV;
 
1383
        __ieee80211_tx_prepare(tx, skb, dev, control);
 
1384
        return 0;
 
1385
}
 
1386
 
 
1387
static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
 
1388
                                            int queue)
 
1389
{
 
1390
        return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
 
1391
}
 
1392
 
 
1393
static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
 
1394
                                            int queue)
 
1395
{
 
1396
        return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
 
1397
}
 
1398
 
 
1399
#define IEEE80211_TX_OK         0
 
1400
#define IEEE80211_TX_AGAIN      1
 
1401
#define IEEE80211_TX_FRAG_AGAIN 2
 
1402
 
 
1403
static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
 
1404
                          struct ieee80211_txrx_data *tx)
 
1405
{
 
1406
        struct ieee80211_tx_control *control = tx->u.tx.control;
 
1407
        int ret, i;
 
1408
 
 
1409
        if (!ieee80211_qdisc_installed(local->mdev) &&
 
1410
            __ieee80211_queue_stopped(local, 0)) {
 
1411
                netif_stop_queue(local->mdev);
 
1412
                return IEEE80211_TX_AGAIN;
 
1413
        }
 
1414
        if (skb) {
 
1415
                ieee80211_dump_frame(local->mdev->name, "TX to low-level driver", skb);
 
1416
                ret = local->ops->tx(local_to_hw(local), skb, control);
 
1417
                if (ret)
 
1418
                        return IEEE80211_TX_AGAIN;
 
1419
                local->mdev->trans_start = jiffies;
 
1420
                ieee80211_led_tx(local, 1);
 
1421
        }
 
1422
        if (tx->u.tx.extra_frag) {
 
1423
                control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
 
1424
                                    IEEE80211_TXCTL_USE_CTS_PROTECT |
 
1425
                                    IEEE80211_TXCTL_CLEAR_DST_MASK |
 
1426
                                    IEEE80211_TXCTL_FIRST_FRAGMENT);
 
1427
                for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
 
1428
                        if (!tx->u.tx.extra_frag[i])
 
1429
                                continue;
 
1430
                        if (__ieee80211_queue_stopped(local, control->queue))
 
1431
                                return IEEE80211_TX_FRAG_AGAIN;
 
1432
                        if (i == tx->u.tx.num_extra_frag) {
 
1433
                                control->tx_rate = tx->u.tx.last_frag_hwrate;
 
1434
                                control->rate = tx->u.tx.last_frag_rate;
 
1435
                                if (tx->u.tx.probe_last_frag)
 
1436
                                        control->flags |=
 
1437
                                                IEEE80211_TXCTL_RATE_CTRL_PROBE;
 
1438
                                else
 
1439
                                        control->flags &=
 
1440
                                                ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
 
1441
                        }
 
1442
 
 
1443
                        ieee80211_dump_frame(local->mdev->name,
 
1444
                                             "TX to low-level driver",
 
1445
                                             tx->u.tx.extra_frag[i]);
 
1446
                        ret = local->ops->tx(local_to_hw(local),
 
1447
                                            tx->u.tx.extra_frag[i],
 
1448
                                            control);
 
1449
                        if (ret)
 
1450
                                return IEEE80211_TX_FRAG_AGAIN;
 
1451
                        local->mdev->trans_start = jiffies;
 
1452
                        ieee80211_led_tx(local, 1);
 
1453
                        tx->u.tx.extra_frag[i] = NULL;
 
1454
                }
 
1455
                kfree(tx->u.tx.extra_frag);
 
1456
                tx->u.tx.extra_frag = NULL;
 
1457
        }
 
1458
        return IEEE80211_TX_OK;
 
1459
}
 
1460
 
 
1461
static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
 
1462
                        struct ieee80211_tx_control *control, int mgmt)
 
1463
{
 
1464
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
1465
        struct sta_info *sta;
 
1466
        ieee80211_tx_handler *handler;
 
1467
        struct ieee80211_txrx_data tx;
 
1468
        ieee80211_txrx_result res = TXRX_DROP, res_prepare;
 
1469
        int ret, i;
 
1470
 
 
1471
        WARN_ON(__ieee80211_queue_pending(local, control->queue));
 
1472
 
 
1473
        if (unlikely(skb->len < 10)) {
 
1474
                dev_kfree_skb(skb);
 
1475
                return 0;
 
1476
        }
 
1477
 
 
1478
        res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
 
1479
 
 
1480
        if (res_prepare == TXRX_DROP) {
 
1481
                dev_kfree_skb(skb);
 
1482
                return 0;
 
1483
        }
 
1484
 
 
1485
        sta = tx.sta;
 
1486
        tx.u.tx.mgmt_interface = mgmt;
 
1487
        tx.u.tx.mode = local->hw.conf.mode;
 
1488
 
 
1489
        if (res_prepare == TXRX_QUEUED) { /* if it was an injected packet */
 
1490
                res = TXRX_CONTINUE;
 
1491
        } else {
 
1492
                for (handler = local->tx_handlers; *handler != NULL;
 
1493
                     handler++) {
 
1494
                        res = (*handler)(&tx);
 
1495
                        if (res != TXRX_CONTINUE)
 
1496
                                break;
 
1497
                }
 
1498
        }
 
1499
 
 
1500
        skb = tx.skb; /* handlers are allowed to change skb */
 
1501
 
 
1502
        if (sta)
 
1503
                iwlwifi_sta_info_put(sta);
 
1504
 
 
1505
        if (unlikely(res == TXRX_DROP)) {
 
1506
                I802_DEBUG_INC(local->tx_handlers_drop);
 
1507
                goto drop;
 
1508
        }
 
1509
 
 
1510
        if (unlikely(res == TXRX_QUEUED)) {
 
1511
                I802_DEBUG_INC(local->tx_handlers_queued);
 
1512
                return 0;
 
1513
        }
 
1514
 
 
1515
        if (tx.u.tx.extra_frag) {
 
1516
                for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
 
1517
                        int next_len, dur;
 
1518
                        struct ieee80211_hdr *hdr =
 
1519
                                (struct ieee80211_hdr *)
 
1520
                                tx.u.tx.extra_frag[i]->data;
 
1521
 
 
1522
                        if (i + 1 < tx.u.tx.num_extra_frag) {
 
1523
                                next_len = tx.u.tx.extra_frag[i + 1]->len;
 
1524
                        } else {
 
1525
                                next_len = 0;
 
1526
                                tx.u.tx.rate = tx.u.tx.last_frag_rate;
 
1527
                                tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
 
1528
                        }
 
1529
                        dur = ieee80211_duration(&tx, 0, next_len);
 
1530
                        hdr->duration_id = cpu_to_le16(dur);
 
1531
                }
 
1532
        }
 
1533
 
 
1534
retry:
 
1535
        ret = __ieee80211_tx(local, skb, &tx);
 
1536
        if (ret) {
 
1537
                struct ieee80211_tx_stored_packet *store =
 
1538
                        &local->pending_packet[control->queue];
 
1539
 
 
1540
                if (ret == IEEE80211_TX_FRAG_AGAIN)
 
1541
                        skb = NULL;
 
1542
                set_bit(IEEE80211_LINK_STATE_PENDING,
 
1543
                        &local->state[control->queue]);
 
1544
                smp_mb();
 
1545
                /* When the driver gets out of buffers during sending of
 
1546
                 * fragments and calls iwlwifi_ieee80211_stop_queue, there is
 
1547
                 * a small window between IEEE80211_LINK_STATE_XOFF and
 
1548
                 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
 
1549
                 * gets available in that window (i.e. driver calls
 
1550
                 * iwlwifi_ieee80211_wake_queue), we would end up with ieee80211_tx
 
1551
                 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
 
1552
                 * continuing transmitting here when that situation is
 
1553
                 * possible to have happened. */
 
1554
                if (!__ieee80211_queue_stopped(local, control->queue)) {
 
1555
                        clear_bit(IEEE80211_LINK_STATE_PENDING,
 
1556
                                  &local->state[control->queue]);
 
1557
                        goto retry;
 
1558
                }
 
1559
                memcpy(&store->control, control,
 
1560
                       sizeof(struct ieee80211_tx_control));
 
1561
                store->skb = skb;
 
1562
                store->extra_frag = tx.u.tx.extra_frag;
 
1563
                store->num_extra_frag = tx.u.tx.num_extra_frag;
 
1564
                store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
 
1565
                store->last_frag_rate = tx.u.tx.last_frag_rate;
 
1566
                store->last_frag_rate_ctrl_probe = tx.u.tx.probe_last_frag;
 
1567
        }
 
1568
        return 0;
 
1569
 
 
1570
 drop:
 
1571
        if (skb)
 
1572
                dev_kfree_skb(skb);
 
1573
        for (i = 0; i < tx.u.tx.num_extra_frag; i++)
 
1574
                if (tx.u.tx.extra_frag[i])
 
1575
                        dev_kfree_skb(tx.u.tx.extra_frag[i]);
 
1576
        kfree(tx.u.tx.extra_frag);
 
1577
        return 0;
 
1578
}
 
1579
 
 
1580
static void ieee80211_tx_pending(unsigned long data)
 
1581
{
 
1582
        struct ieee80211_local *local = (struct ieee80211_local *)data;
 
1583
        struct net_device *dev = local->mdev;
 
1584
        struct ieee80211_tx_stored_packet *store;
 
1585
        struct ieee80211_txrx_data tx;
 
1586
        int i, ret, reschedule = 0;
 
1587
 
 
1588
        netif_tx_lock_bh(dev);
 
1589
        for (i = 0; i < local->hw.queues; i++) {
 
1590
                if (__ieee80211_queue_stopped(local, i))
 
1591
                        continue;
 
1592
                if (!__ieee80211_queue_pending(local, i)) {
 
1593
                        reschedule = 1;
 
1594
                        continue;
 
1595
                }
 
1596
                store = &local->pending_packet[i];
 
1597
                tx.u.tx.control = &store->control;
 
1598
                tx.u.tx.extra_frag = store->extra_frag;
 
1599
                tx.u.tx.num_extra_frag = store->num_extra_frag;
 
1600
                tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
 
1601
                tx.u.tx.last_frag_rate = store->last_frag_rate;
 
1602
                tx.u.tx.probe_last_frag = store->last_frag_rate_ctrl_probe;
 
1603
                ret = __ieee80211_tx(local, store->skb, &tx);
 
1604
                if (ret) {
 
1605
                        if (ret == IEEE80211_TX_FRAG_AGAIN)
 
1606
                                store->skb = NULL;
 
1607
                } else {
 
1608
                        clear_bit(IEEE80211_LINK_STATE_PENDING,
 
1609
                                  &local->state[i]);
 
1610
                        reschedule = 1;
 
1611
                }
 
1612
        }
 
1613
        netif_tx_unlock_bh(dev);
 
1614
        if (reschedule) {
 
1615
                if (!ieee80211_qdisc_installed(dev)) {
 
1616
                        if (!__ieee80211_queue_stopped(local, 0))
 
1617
                                netif_wake_queue(dev);
 
1618
                } else
 
1619
                        netif_schedule(dev);
 
1620
        }
 
1621
}
 
1622
 
 
1623
static void ieee80211_clear_tx_pending(struct ieee80211_local *local)
 
1624
{
 
1625
        int i, j;
 
1626
        struct ieee80211_tx_stored_packet *store;
 
1627
 
 
1628
        for (i = 0; i < local->hw.queues; i++) {
 
1629
                if (!__ieee80211_queue_pending(local, i))
 
1630
                        continue;
 
1631
                store = &local->pending_packet[i];
 
1632
                kfree_skb(store->skb);
 
1633
                for (j = 0; j < store->num_extra_frag; j++)
 
1634
                        kfree_skb(store->extra_frag[j]);
 
1635
                kfree(store->extra_frag);
 
1636
                clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
 
1637
        }
 
1638
}
 
1639
 
 
1640
static int ieee80211_master_start_xmit(struct sk_buff *skb,
 
1641
                                       struct net_device *dev)
 
1642
{
 
1643
        struct ieee80211_tx_control control;
 
1644
        struct ieee80211_tx_packet_data *pkt_data;
 
1645
        struct net_device *odev = NULL;
 
1646
        struct ieee80211_sub_if_data *osdata;
 
1647
        int headroom;
 
1648
        int ret;
 
1649
 
 
1650
        /*
 
1651
         * copy control out of the skb so other people can use skb->cb
 
1652
         */
 
1653
        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
 
1654
        memset(&control, 0, sizeof(struct ieee80211_tx_control));
 
1655
 
 
1656
        if (pkt_data->ifindex)
 
1657
                odev = dev_get_by_index(&init_net, pkt_data->ifindex);
 
1658
        if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
 
1659
                dev_put(odev);
 
1660
                odev = NULL;
 
1661
        }
 
1662
        if (unlikely(!odev)) {
 
1663
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
1664
                printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
 
1665
                       "originating device\n", dev->name);
 
1666
#endif
 
1667
                dev_kfree_skb(skb);
 
1668
                return 0;
 
1669
        }
 
1670
        osdata = IEEE80211_DEV_TO_SUB_IF(odev);
 
1671
 
 
1672
        headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
 
1673
        if (skb_headroom(skb) < headroom) {
 
1674
                if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
 
1675
                        dev_kfree_skb(skb);
 
1676
                        dev_put(odev);
 
1677
                        return 0;
 
1678
                }
 
1679
        }
 
1680
 
 
1681
        control.ifindex = odev->ifindex;
 
1682
        control.type = osdata->type;
 
1683
        if (pkt_data->req_tx_status)
 
1684
                control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
 
1685
        if (pkt_data->do_not_encrypt)
 
1686
                control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
 
1687
        if (pkt_data->requeue)
 
1688
                control.flags |= IEEE80211_TXCTL_REQUEUE;
 
1689
        if (pkt_data->ht_queue)
 
1690
                control.flags |= IEEE80211_TXCTL_HT_MPDU_AGG;
 
1691
 
 
1692
        control.queue = pkt_data->queue;
 
1693
 
 
1694
        ret = ieee80211_tx(odev, skb, &control,
 
1695
                           control.type == IEEE80211_IF_TYPE_MGMT);
 
1696
        dev_put(odev);
 
1697
 
 
1698
        return ret;
 
1699
}
 
1700
 
 
1701
 
 
1702
int ieee80211_monitor_start_xmit(struct sk_buff *skb,
 
1703
                                 struct net_device *dev)
 
1704
{
 
1705
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
1706
        struct ieee80211_tx_packet_data *pkt_data;
 
1707
        struct ieee80211_radiotap_header *prthdr =
 
1708
                (struct ieee80211_radiotap_header *)skb->data;
 
1709
        u16 len;
 
1710
 
 
1711
        /*
 
1712
         * there must be a radiotap header at the
 
1713
         * start in this case
 
1714
         */
 
1715
        if (unlikely(prthdr->it_version)) {
 
1716
                /* only version 0 is supported */
 
1717
                dev_kfree_skb(skb);
 
1718
                return NETDEV_TX_OK;
 
1719
        }
 
1720
 
 
1721
        skb->dev = local->mdev;
 
1722
 
 
1723
        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
 
1724
        memset(pkt_data, 0, sizeof(*pkt_data));
 
1725
        pkt_data->ifindex = dev->ifindex;
 
1726
        pkt_data->mgmt_iface = 0;
 
1727
        pkt_data->do_not_encrypt = 1;
 
1728
 
 
1729
        /* above needed because we set skb device to master */
 
1730
 
 
1731
        /*
 
1732
         * fix up the pointers accounting for the radiotap
 
1733
         * header still being in there.  We are being given
 
1734
         * a precooked IEEE80211 header so no need for
 
1735
         * normal processing
 
1736
         */
 
1737
        len = le16_to_cpu(get_unaligned(&prthdr->it_len));
 
1738
        skb_set_mac_header(skb, len);
 
1739
        skb_set_network_header(skb, len + sizeof(struct ieee80211_hdr));
 
1740
        skb_set_transport_header(skb, len + sizeof(struct ieee80211_hdr));
 
1741
 
 
1742
        /*
 
1743
         * pass the radiotap header up to
 
1744
         * the next stage intact
 
1745
         */
 
1746
        dev_queue_xmit(skb);
 
1747
 
 
1748
        return NETDEV_TX_OK;
 
1749
}
 
1750
 
 
1751
 
 
1752
/**
 
1753
 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
 
1754
 * subinterfaces (wlan#, WDS, and VLAN interfaces)
 
1755
 * @skb: packet to be sent
 
1756
 * @dev: incoming interface
 
1757
 *
 
1758
 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
 
1759
 * not be freed, and caller is responsible for either retrying later or freeing
 
1760
 * skb).
 
1761
 *
 
1762
 * This function takes in an Ethernet header and encapsulates it with suitable
 
1763
 * IEEE 802.11 header based on which interface the packet is coming in. The
 
1764
 * encapsulated packet will then be passed to master interface, wlan#.11, for
 
1765
 * transmission (through low-level driver).
 
1766
 */
 
1767
int ieee80211_subif_start_xmit(struct sk_buff *skb,
 
1768
                               struct net_device *dev)
 
1769
{
 
1770
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
1771
        struct ieee80211_tx_packet_data *pkt_data;
 
1772
        struct ieee80211_sub_if_data *sdata;
 
1773
        int ret = 1, head_need;
 
1774
        u16 ethertype, hdrlen, fc;
 
1775
        struct ieee80211_hdr hdr;
 
1776
        const u8 *encaps_data;
 
1777
        int encaps_len, skip_header_bytes;
 
1778
        int nh_pos, h_pos, no_encrypt = 0;
 
1779
        struct sta_info *sta;
 
1780
 
 
1781
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
1782
        if (unlikely(skb->len < ETH_HLEN)) {
 
1783
                printk(KERN_DEBUG "%s: short skb (len=%d)\n",
 
1784
                       dev->name, skb->len);
 
1785
                ret = 0;
 
1786
                goto fail;
 
1787
        }
 
1788
 
 
1789
        nh_pos = skb_network_header(skb) - skb->data;
 
1790
        h_pos = skb_transport_header(skb) - skb->data;
 
1791
 
 
1792
        /* convert Ethernet header to proper 802.11 header (based on
 
1793
         * operation mode) */
 
1794
        ethertype = (skb->data[12] << 8) | skb->data[13];
 
1795
        /* TODO: handling for 802.1x authorized/unauthorized port */
 
1796
        fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
 
1797
 
 
1798
        if (likely(sdata->type == IEEE80211_IF_TYPE_AP ||
 
1799
                   sdata->type == IEEE80211_IF_TYPE_VLAN)) {
 
1800
                fc |= IEEE80211_FCTL_FROMDS;
 
1801
                /* DA BSSID SA */
 
1802
                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 
1803
                memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
 
1804
                memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
 
1805
                hdrlen = 24;
 
1806
        } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
 
1807
                fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
 
1808
                /* RA TA DA SA */
 
1809
                memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
 
1810
                memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
 
1811
                memcpy(hdr.addr3, skb->data, ETH_ALEN);
 
1812
                memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
 
1813
                hdrlen = 30;
 
1814
        } else if (sdata->type == IEEE80211_IF_TYPE_STA) {
 
1815
                if (dls_link_status(local, skb->data) == DLS_STATUS_OK) {
 
1816
                        /* DA SA BSSID */
 
1817
                        memcpy(hdr.addr1, skb->data, ETH_ALEN);
 
1818
                        memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 
1819
                        memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
 
1820
                } else {
 
1821
                        fc |= IEEE80211_FCTL_TODS;
 
1822
                        /* BSSID SA DA */
 
1823
                        memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
 
1824
                        memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 
1825
                        memcpy(hdr.addr3, skb->data, ETH_ALEN);
 
1826
                }
 
1827
                hdrlen = 24;
 
1828
        } else if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
 
1829
                /* DA SA BSSID */
 
1830
                memcpy(hdr.addr1, skb->data, ETH_ALEN);
 
1831
                memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
 
1832
                memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
 
1833
                hdrlen = 24;
 
1834
        } else {
 
1835
                ret = 0;
 
1836
                goto fail;
 
1837
        }
 
1838
 
 
1839
        /* receiver is QoS enabled, use a QoS type frame */
 
1840
        sta = iwlwifi_sta_info_get(local, hdr.addr1);
 
1841
        if (sta) {
 
1842
                if (sta->flags & WLAN_STA_WME) {
 
1843
                        fc |= IEEE80211_STYPE_QOS_DATA;
 
1844
                        hdrlen += 2;
 
1845
                }
 
1846
                iwlwifi_sta_info_put(sta);
 
1847
        }
 
1848
 
 
1849
        hdr.frame_control = cpu_to_le16(fc);
 
1850
        hdr.duration_id = 0;
 
1851
        hdr.seq_ctrl = 0;
 
1852
 
 
1853
        skip_header_bytes = ETH_HLEN;
 
1854
        if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
 
1855
                encaps_data = bridge_tunnel_header;
 
1856
                encaps_len = sizeof(bridge_tunnel_header);
 
1857
                skip_header_bytes -= 2;
 
1858
        } else if (ethertype >= 0x600) {
 
1859
                encaps_data = rfc1042_header;
 
1860
                encaps_len = sizeof(rfc1042_header);
 
1861
                skip_header_bytes -= 2;
 
1862
        } else {
 
1863
                encaps_data = NULL;
 
1864
                encaps_len = 0;
 
1865
        }
 
1866
 
 
1867
        skb_pull(skb, skip_header_bytes);
 
1868
        nh_pos -= skip_header_bytes;
 
1869
        h_pos -= skip_header_bytes;
 
1870
 
 
1871
        /* TODO: implement support for fragments so that there is no need to
 
1872
         * reallocate and copy payload; it might be enough to support one
 
1873
         * extra fragment that would be copied in the beginning of the frame
 
1874
         * data.. anyway, it would be nice to include this into skb structure
 
1875
         * somehow
 
1876
         *
 
1877
         * There are few options for this:
 
1878
         * use skb->cb as an extra space for 802.11 header
 
1879
         * allocate new buffer if not enough headroom
 
1880
         * make sure that there is enough headroom in every skb by increasing
 
1881
         * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
 
1882
         * alloc_skb() (net/core/skbuff.c)
 
1883
         */
 
1884
        head_need = hdrlen + encaps_len + local->tx_headroom;
 
1885
        head_need -= skb_headroom(skb);
 
1886
 
 
1887
        /* We are going to modify skb data, so make a copy of it if happens to
 
1888
         * be cloned. This could happen, e.g., with Linux bridge code passing
 
1889
         * us broadcast frames. */
 
1890
 
 
1891
        if (head_need > 0 || skb_cloned(skb)) {
 
1892
#if 0
 
1893
                printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
 
1894
                       "of headroom\n", dev->name, head_need);
 
1895
#endif
 
1896
 
 
1897
                if (skb_cloned(skb))
 
1898
                        I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
 
1899
                else
 
1900
                        I802_DEBUG_INC(local->tx_expand_skb_head);
 
1901
                /* Since we have to reallocate the buffer, make sure that there
 
1902
                 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
 
1903
                 * before payload and 12 after). */
 
1904
                if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
 
1905
                                     12, GFP_ATOMIC)) {
 
1906
                        printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
 
1907
                               "\n", dev->name);
 
1908
                        goto fail;
 
1909
                }
 
1910
        }
 
1911
 
 
1912
        if (encaps_data) {
 
1913
                memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
 
1914
                nh_pos += encaps_len;
 
1915
                h_pos += encaps_len;
 
1916
        }
 
1917
        memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
 
1918
        nh_pos += hdrlen;
 
1919
        h_pos += hdrlen;
 
1920
 
 
1921
        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
 
1922
        memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
 
1923
        pkt_data->ifindex = dev->ifindex;
 
1924
        pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
 
1925
        pkt_data->do_not_encrypt = no_encrypt;
 
1926
 
 
1927
        skb->dev = local->mdev;
 
1928
        sdata->stats.tx_packets++;
 
1929
        sdata->stats.tx_bytes += skb->len;
 
1930
 
 
1931
        /* Update skb pointers to various headers since this modified frame
 
1932
         * is going to go through Linux networking code that may potentially
 
1933
         * need things like pointer to IP header. */
 
1934
        skb_set_mac_header(skb, 0);
 
1935
        skb_set_network_header(skb, nh_pos);
 
1936
        skb_set_transport_header(skb, h_pos);
 
1937
 
 
1938
        dev->trans_start = jiffies;
 
1939
        dev_queue_xmit(skb);
 
1940
 
 
1941
        return 0;
 
1942
 
 
1943
 fail:
 
1944
        if (!ret)
 
1945
                dev_kfree_skb(skb);
 
1946
 
 
1947
        return ret;
 
1948
}
 
1949
 
 
1950
 
 
1951
/*
 
1952
 * This is the transmit routine for the 802.11 type interfaces
 
1953
 * called by upper layers of the linux networking
 
1954
 * stack when it has a frame to transmit
 
1955
 */
 
1956
static int
 
1957
ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
1958
{
 
1959
        struct ieee80211_sub_if_data *sdata;
 
1960
        struct ieee80211_tx_packet_data *pkt_data;
 
1961
        struct ieee80211_hdr *hdr;
 
1962
        u16 fc;
 
1963
 
 
1964
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
1965
 
 
1966
        if (skb->len < 10) {
 
1967
                dev_kfree_skb(skb);
 
1968
                return 0;
 
1969
        }
 
1970
 
 
1971
        if (skb_headroom(skb) < sdata->local->tx_headroom) {
 
1972
                if (pskb_expand_head(skb, sdata->local->tx_headroom,
 
1973
                                     0, GFP_ATOMIC)) {
 
1974
                        dev_kfree_skb(skb);
 
1975
                        return 0;
 
1976
                }
 
1977
        }
 
1978
 
 
1979
        hdr = (struct ieee80211_hdr *) skb->data;
 
1980
        fc = le16_to_cpu(hdr->frame_control);
 
1981
 
 
1982
        pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
 
1983
        memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
 
1984
        pkt_data->ifindex = sdata->dev->ifindex;
 
1985
        pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
 
1986
 
 
1987
        skb->priority = 20; /* use hardcoded priority for mgmt TX queue */
 
1988
        skb->dev = sdata->local->mdev;
 
1989
 
 
1990
        /*
 
1991
         * We're using the protocol field of the the frame control header
 
1992
         * to request TX callback for hostapd. BIT(1) is checked.
 
1993
         */
 
1994
        if ((fc & BIT(1)) == BIT(1)) {
 
1995
                pkt_data->req_tx_status = 1;
 
1996
                fc &= ~BIT(1);
 
1997
                hdr->frame_control = cpu_to_le16(fc);
 
1998
        }
 
1999
 
 
2000
        pkt_data->do_not_encrypt = !(fc & IEEE80211_FCTL_PROTECTED);
 
2001
 
 
2002
        sdata->stats.tx_packets++;
 
2003
        sdata->stats.tx_bytes += skb->len;
 
2004
 
 
2005
        dev_queue_xmit(skb);
 
2006
 
 
2007
        return 0;
 
2008
}
 
2009
 
 
2010
 
 
2011
static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
 
2012
                                     struct ieee80211_if_ap *bss,
 
2013
                                     struct sk_buff *skb)
 
2014
{
 
2015
        u8 *pos, *tim;
 
2016
        int aid0 = 0;
 
2017
        int i, have_bits = 0, n1, n2;
 
2018
 
 
2019
        /* Generate bitmap for TIM only if there are any STAs in power save
 
2020
         * mode. */
 
2021
        spin_lock_bh(&local->sta_lock);
 
2022
        if (atomic_read(&bss->num_sta_ps) > 0)
 
2023
                /* in the hope that this is faster than
 
2024
                 * checking byte-for-byte */
 
2025
                have_bits = !bitmap_empty((unsigned long*)bss->tim,
 
2026
                                          IEEE80211_MAX_AID+1);
 
2027
 
 
2028
        if (bss->dtim_count == 0)
 
2029
                bss->dtim_count = bss->dtim_period - 1;
 
2030
        else
 
2031
                bss->dtim_count--;
 
2032
 
 
2033
        tim = pos = (u8 *) skb_put(skb, 6);
 
2034
        *pos++ = WLAN_EID_TIM;
 
2035
        *pos++ = 4;
 
2036
        *pos++ = bss->dtim_count;
 
2037
        *pos++ = bss->dtim_period;
 
2038
 
 
2039
        if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
 
2040
                aid0 = 1;
 
2041
 
 
2042
        if (have_bits) {
 
2043
                /* Find largest even number N1 so that bits numbered 1 through
 
2044
                 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
 
2045
                 * (N2 + 1) x 8 through 2007 are 0. */
 
2046
                n1 = 0;
 
2047
                for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
 
2048
                        if (bss->tim[i]) {
 
2049
                                n1 = i & 0xfe;
 
2050
                                break;
 
2051
                        }
 
2052
                }
 
2053
                n2 = n1;
 
2054
                for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
 
2055
                        if (bss->tim[i]) {
 
2056
                                n2 = i;
 
2057
                                break;
 
2058
                        }
 
2059
                }
 
2060
 
 
2061
                /* Bitmap control */
 
2062
                *pos++ = n1 | aid0;
 
2063
                /* Part Virt Bitmap */
 
2064
                memcpy(pos, bss->tim + n1, n2 - n1 + 1);
 
2065
 
 
2066
                tim[1] = n2 - n1 + 4;
 
2067
                skb_put(skb, n2 - n1);
 
2068
        } else {
 
2069
                *pos++ = aid0; /* Bitmap control */
 
2070
                *pos++ = 0; /* Part Virt Bitmap */
 
2071
        }
 
2072
        spin_unlock_bh(&local->sta_lock);
 
2073
}
 
2074
 
 
2075
 
 
2076
struct sk_buff * iwlwifi_ieee80211_beacon_get(struct ieee80211_hw *hw, int if_id,
 
2077
                                      struct ieee80211_tx_control *control)
 
2078
{
 
2079
        struct ieee80211_local *local = hw_to_local(hw);
 
2080
        struct sk_buff *skb;
 
2081
        struct net_device *bdev;
 
2082
        struct ieee80211_sub_if_data *sdata = NULL;
 
2083
        struct ieee80211_if_ap *ap = NULL;
 
2084
        struct ieee80211_rate *rate;
 
2085
        struct rate_control_extra extra;
 
2086
        u8 *b_head, *b_tail;
 
2087
        int bh_len, bt_len;
 
2088
 
 
2089
        bdev = dev_get_by_index(&init_net, if_id);
 
2090
        if (bdev) {
 
2091
                sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
 
2092
                ap = &sdata->u.ap;
 
2093
                dev_put(bdev);
 
2094
        }
 
2095
 
 
2096
        if (!ap || sdata->type != IEEE80211_IF_TYPE_AP ||
 
2097
            !ap->beacon_head) {
 
2098
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
2099
                if (net_ratelimit())
 
2100
                        printk(KERN_DEBUG "no beacon data avail for idx=%d "
 
2101
                               "(%s)\n", if_id, bdev ? bdev->name : "N/A");
 
2102
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
2103
                return NULL;
 
2104
        }
 
2105
 
 
2106
        /* Assume we are generating the normal beacon locally */
 
2107
        b_head = ap->beacon_head;
 
2108
        b_tail = ap->beacon_tail;
 
2109
        bh_len = ap->beacon_head_len;
 
2110
        bt_len = ap->beacon_tail_len;
 
2111
 
 
2112
        skb = dev_alloc_skb(local->tx_headroom +
 
2113
                bh_len + bt_len + 256 /* maximum TIM len */);
 
2114
        if (!skb)
 
2115
                return NULL;
 
2116
 
 
2117
        skb_reserve(skb, local->tx_headroom);
 
2118
        memcpy(skb_put(skb, bh_len), b_head, bh_len);
 
2119
 
 
2120
        ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
 
2121
 
 
2122
        ieee80211_beacon_add_tim(local, ap, skb);
 
2123
 
 
2124
        if (b_tail) {
 
2125
                memcpy(skb_put(skb, bt_len), b_tail, bt_len);
 
2126
        }
 
2127
 
 
2128
        if (control) {
 
2129
                memset(&extra, 0, sizeof(extra));
 
2130
                extra.mode = local->oper_hw_mode;
 
2131
 
 
2132
                rate = rate_control_get_rate(local, local->mdev, skb, &extra);
 
2133
                if (!rate) {
 
2134
                        if (net_ratelimit()) {
 
2135
                                printk(KERN_DEBUG "%s: iwlwifi_ieee80211_beacon_get: no rate "
 
2136
                                       "found\n", local->mdev->name);
 
2137
                        }
 
2138
                        dev_kfree_skb(skb);
 
2139
                        return NULL;
 
2140
                }
 
2141
 
 
2142
                control->tx_rate = (local->short_preamble &&
 
2143
                                    (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
 
2144
                        rate->val2 : rate->val;
 
2145
                control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
 
2146
                control->power_level = local->hw.conf.power_level;
 
2147
                control->flags |= IEEE80211_TXCTL_NO_ACK;
 
2148
                control->retry_limit = 1;
 
2149
                control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
 
2150
        }
 
2151
 
 
2152
        ap->num_beacons++;
 
2153
        return skb;
 
2154
}
 
2155
EXPORT_SYMBOL(iwlwifi_ieee80211_beacon_get);
 
2156
 
 
2157
__le16 iwlwifi_ieee80211_rts_duration(struct ieee80211_hw *hw,
 
2158
                              size_t frame_len,
 
2159
                              const struct ieee80211_tx_control *frame_txctl)
 
2160
{
 
2161
        struct ieee80211_local *local = hw_to_local(hw);
 
2162
        struct ieee80211_rate *rate;
 
2163
        int short_preamble = local->short_preamble;
 
2164
        int erp;
 
2165
        u16 dur;
 
2166
 
 
2167
        rate = frame_txctl->rts_rate;
 
2168
        erp = !!(rate->flags & IEEE80211_RATE_ERP);
 
2169
 
 
2170
        /* CTS duration */
 
2171
        dur = ieee80211_frame_duration(local, 10, rate->rate,
 
2172
                                       erp, short_preamble);
 
2173
        /* Data frame duration */
 
2174
        dur += ieee80211_frame_duration(local, frame_len, rate->rate,
 
2175
                                        erp, short_preamble);
 
2176
        /* ACK duration */
 
2177
        dur += ieee80211_frame_duration(local, 10, rate->rate,
 
2178
                                        erp, short_preamble);
 
2179
 
 
2180
        return cpu_to_le16(dur);
 
2181
}
 
2182
EXPORT_SYMBOL(iwlwifi_ieee80211_rts_duration);
 
2183
 
 
2184
 
 
2185
__le16 iwlwifi_ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
 
2186
                                    size_t frame_len,
 
2187
                                    const struct ieee80211_tx_control *frame_txctl)
 
2188
{
 
2189
        struct ieee80211_local *local = hw_to_local(hw);
 
2190
        struct ieee80211_rate *rate;
 
2191
        int short_preamble = local->short_preamble;
 
2192
        int erp;
 
2193
        u16 dur;
 
2194
 
 
2195
        rate = frame_txctl->rts_rate;
 
2196
        erp = !!(rate->flags & IEEE80211_RATE_ERP);
 
2197
 
 
2198
        /* Data frame duration */
 
2199
        dur = ieee80211_frame_duration(local, frame_len, rate->rate,
 
2200
                                       erp, short_preamble);
 
2201
        if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
 
2202
                /* ACK duration */
 
2203
                dur += ieee80211_frame_duration(local, 10, rate->rate,
 
2204
                                                erp, short_preamble);
 
2205
        }
 
2206
 
 
2207
        return cpu_to_le16(dur);
 
2208
}
 
2209
EXPORT_SYMBOL(iwlwifi_ieee80211_ctstoself_duration);
 
2210
 
 
2211
void iwlwifi_ieee80211_rts_get(struct ieee80211_hw *hw,
 
2212
                       const void *frame, size_t frame_len,
 
2213
                       const struct ieee80211_tx_control *frame_txctl,
 
2214
                       struct ieee80211_rts *rts)
 
2215
{
 
2216
        const struct ieee80211_hdr *hdr = frame;
 
2217
        u16 fctl;
 
2218
 
 
2219
        fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
 
2220
        rts->frame_control = cpu_to_le16(fctl);
 
2221
        rts->duration = iwlwifi_ieee80211_rts_duration(hw, frame_len, frame_txctl);
 
2222
        memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
 
2223
        memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
 
2224
}
 
2225
EXPORT_SYMBOL(iwlwifi_ieee80211_rts_get);
 
2226
 
 
2227
void iwlwifi_ieee80211_ctstoself_get(struct ieee80211_hw *hw,
 
2228
                             const void *frame, size_t frame_len,
 
2229
                             const struct ieee80211_tx_control *frame_txctl,
 
2230
                             struct ieee80211_cts *cts)
 
2231
{
 
2232
        const struct ieee80211_hdr *hdr = frame;
 
2233
        u16 fctl;
 
2234
 
 
2235
        fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
 
2236
        cts->frame_control = cpu_to_le16(fctl);
 
2237
        cts->duration = iwlwifi_ieee80211_ctstoself_duration(hw, frame_len, frame_txctl);
 
2238
        memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
 
2239
}
 
2240
EXPORT_SYMBOL(iwlwifi_ieee80211_ctstoself_get);
 
2241
 
 
2242
struct sk_buff *
 
2243
iwlwifi_ieee80211_get_buffered_bc(struct ieee80211_hw *hw, int if_id,
 
2244
                          struct ieee80211_tx_control *control)
 
2245
{
 
2246
        struct ieee80211_local *local = hw_to_local(hw);
 
2247
        struct sk_buff *skb;
 
2248
        struct sta_info *sta;
 
2249
        ieee80211_tx_handler *handler;
 
2250
        struct ieee80211_txrx_data tx;
 
2251
        ieee80211_txrx_result res = TXRX_DROP;
 
2252
        struct net_device *bdev;
 
2253
        struct ieee80211_sub_if_data *sdata;
 
2254
        struct ieee80211_if_ap *bss = NULL;
 
2255
 
 
2256
        bdev = dev_get_by_index(&init_net, if_id);
 
2257
        if (bdev) {
 
2258
                sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
 
2259
                bss = &sdata->u.ap;
 
2260
                dev_put(bdev);
 
2261
        }
 
2262
        if (!bss || sdata->type != IEEE80211_IF_TYPE_AP || !bss->beacon_head)
 
2263
                return NULL;
 
2264
 
 
2265
        if (bss->dtim_count != 0)
 
2266
                return NULL; /* send buffered bc/mc only after DTIM beacon */
 
2267
        memset(control, 0, sizeof(*control));
 
2268
        while (1) {
 
2269
                skb = skb_dequeue(&bss->ps_bc_buf);
 
2270
                if (!skb)
 
2271
                        return NULL;
 
2272
                local->total_ps_buffered--;
 
2273
 
 
2274
                if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
 
2275
                        struct ieee80211_hdr *hdr =
 
2276
                                (struct ieee80211_hdr *) skb->data;
 
2277
                        /* more buffered multicast/broadcast frames ==> set
 
2278
                         * MoreData flag in IEEE 802.11 header to inform PS
 
2279
                         * STAs */
 
2280
                        hdr->frame_control |=
 
2281
                                cpu_to_le16(IEEE80211_FCTL_MOREDATA);
 
2282
                }
 
2283
 
 
2284
                if (ieee80211_tx_prepare(&tx, skb, local->mdev, control) == 0)
 
2285
                        break;
 
2286
                dev_kfree_skb_any(skb);
 
2287
        }
 
2288
        sta = tx.sta;
 
2289
        tx.u.tx.ps_buffered = 1;
 
2290
 
 
2291
        for (handler = local->tx_handlers; *handler != NULL; handler++) {
 
2292
                res = (*handler)(&tx);
 
2293
                if (res == TXRX_DROP || res == TXRX_QUEUED)
 
2294
                        break;
 
2295
        }
 
2296
        dev_put(tx.dev);
 
2297
        skb = tx.skb; /* handlers are allowed to change skb */
 
2298
 
 
2299
        if (res == TXRX_DROP) {
 
2300
                I802_DEBUG_INC(local->tx_handlers_drop);
 
2301
                dev_kfree_skb(skb);
 
2302
                skb = NULL;
 
2303
        } else if (res == TXRX_QUEUED) {
 
2304
                I802_DEBUG_INC(local->tx_handlers_queued);
 
2305
                skb = NULL;
 
2306
        }
 
2307
 
 
2308
        if (sta)
 
2309
                iwlwifi_sta_info_put(sta);
 
2310
 
 
2311
        return skb;
 
2312
}
 
2313
EXPORT_SYMBOL(iwlwifi_ieee80211_get_buffered_bc);
 
2314
 
 
2315
static int __ieee80211_if_config(struct net_device *dev,
 
2316
                                 struct sk_buff *beacon,
 
2317
                                 struct ieee80211_tx_control *control)
 
2318
{
 
2319
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2320
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2321
        struct ieee80211_if_conf conf;
 
2322
        static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
2323
 
 
2324
        if (!local->ops->config_interface || !netif_running(dev))
 
2325
                return 0;
 
2326
 
 
2327
        memset(&conf, 0, sizeof(conf));
 
2328
        conf.type = sdata->type;
 
2329
        if (sdata->type == IEEE80211_IF_TYPE_STA ||
 
2330
            sdata->type == IEEE80211_IF_TYPE_IBSS) {
 
2331
                if (local->sta_hw_scanning)
 
2332
                        conf.bssid = scan_bssid;
 
2333
                else
 
2334
                        conf.bssid = sdata->u.sta.bssid;
 
2335
                conf.ssid = sdata->u.sta.ssid;
 
2336
                conf.ssid_len = sdata->u.sta.ssid_len;
 
2337
                conf.generic_elem = sdata->u.sta.extra_ie;
 
2338
                conf.generic_elem_len = sdata->u.sta.extra_ie_len;
 
2339
        } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
 
2340
                conf.ssid = sdata->u.ap.ssid;
 
2341
                conf.ssid_len = sdata->u.ap.ssid_len;
 
2342
                conf.generic_elem = sdata->u.ap.generic_elem;
 
2343
                conf.generic_elem_len = sdata->u.ap.generic_elem_len;
 
2344
                conf.beacon = beacon;
 
2345
                conf.beacon_control = control;
 
2346
        }
 
2347
        return local->ops->config_interface(local_to_hw(local),
 
2348
                                           dev->ifindex, &conf);
 
2349
}
 
2350
 
 
2351
int ieee80211_if_config(struct net_device *dev)
 
2352
{
 
2353
        return __ieee80211_if_config(dev, NULL, NULL);
 
2354
}
 
2355
 
 
2356
int ieee80211_if_config_beacon(struct net_device *dev)
 
2357
{
 
2358
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2359
        struct ieee80211_tx_control control;
 
2360
        struct sk_buff *skb;
 
2361
 
 
2362
        if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
 
2363
                return 0;
 
2364
        skb = iwlwifi_ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
 
2365
        if (!skb)
 
2366
                return -ENOMEM;
 
2367
        return __ieee80211_if_config(dev, skb, &control);
 
2368
}
 
2369
 
 
2370
int ieee80211_hw_config(struct ieee80211_local *local)
 
2371
{
 
2372
        struct ieee80211_hw_mode *mode;
 
2373
        struct ieee80211_channel *chan;
 
2374
        int ret = 0;
 
2375
 
 
2376
        if (local->sta_sw_scanning) {
 
2377
                chan = local->scan_channel;
 
2378
                mode = local->scan_hw_mode;
 
2379
        } else {
 
2380
                chan = local->oper_channel;
 
2381
                mode = local->oper_hw_mode;
 
2382
        }
 
2383
 
 
2384
        local->hw.conf.channel = chan->chan;
 
2385
        local->hw.conf.channel_val = chan->val;
 
2386
        local->hw.conf.power_level =
 
2387
                (local->user_txpow < chan->power_level) ?
 
2388
                        local->user_txpow : chan->power_level;
 
2389
        local->hw.conf.freq = chan->freq;
 
2390
        local->hw.conf.phymode = mode->mode;
 
2391
        local->hw.conf.antenna_max = chan->antenna_max;
 
2392
        local->hw.conf.chan = chan;
 
2393
        local->hw.conf.mode = mode;
 
2394
 
 
2395
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
2396
        printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
 
2397
               "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
 
2398
               local->hw.conf.phymode);
 
2399
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
2400
 
 
2401
        if (local->ops->config)
 
2402
                ret = local->ops->config(local_to_hw(local), &local->hw.conf);
 
2403
 
 
2404
        return ret;
 
2405
}
 
2406
 
 
2407
 
 
2408
static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
 
2409
{
 
2410
        /* FIX: what would be proper limits for MTU?
 
2411
         * This interface uses 802.3 frames. */
 
2412
        if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
 
2413
                printk(KERN_WARNING "%s: invalid MTU %d\n",
 
2414
                       dev->name, new_mtu);
 
2415
                return -EINVAL;
 
2416
        }
 
2417
 
 
2418
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
2419
        printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
 
2420
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
2421
        dev->mtu = new_mtu;
 
2422
        return 0;
 
2423
}
 
2424
 
 
2425
 
 
2426
static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
 
2427
{
 
2428
        /* FIX: what would be proper limits for MTU?
 
2429
         * This interface uses 802.11 frames. */
 
2430
        if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
 
2431
                printk(KERN_WARNING "%s: invalid MTU %d\n",
 
2432
                       dev->name, new_mtu);
 
2433
                return -EINVAL;
 
2434
        }
 
2435
 
 
2436
#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
 
2437
        printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
 
2438
#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
 
2439
        dev->mtu = new_mtu;
 
2440
        return 0;
 
2441
}
 
2442
 
 
2443
enum netif_tx_lock_class {
 
2444
        TX_LOCK_NORMAL,
 
2445
        TX_LOCK_MASTER,
 
2446
};
 
2447
 
 
2448
static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
 
2449
{
 
2450
        spin_lock_nested(&dev->_xmit_lock, subclass);
 
2451
        dev->xmit_lock_owner = smp_processor_id();
 
2452
}
 
2453
 
 
2454
static void ieee80211_set_multicast_list(struct net_device *dev)
 
2455
{
 
2456
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2457
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2458
        unsigned short flags;
 
2459
 
 
2460
        netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
 
2461
        if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
 
2462
                if (sdata->allmulti) {
 
2463
                        sdata->allmulti = 0;
 
2464
                        local->iff_allmultis--;
 
2465
                } else {
 
2466
                        sdata->allmulti = 1;
 
2467
                        local->iff_allmultis++;
 
2468
                }
 
2469
        }
 
2470
        if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
 
2471
                if (sdata->promisc) {
 
2472
                        sdata->promisc = 0;
 
2473
                        local->iff_promiscs--;
 
2474
                } else {
 
2475
                        sdata->promisc = 1;
 
2476
                        local->iff_promiscs++;
 
2477
                }
 
2478
        }
 
2479
        if (dev->mc_count != sdata->mc_count) {
 
2480
                local->mc_count = local->mc_count - sdata->mc_count +
 
2481
                                  dev->mc_count;
 
2482
                sdata->mc_count = dev->mc_count;
 
2483
        }
 
2484
        if (local->ops->set_multicast_list) {
 
2485
                flags = local->mdev->flags;
 
2486
                if (local->iff_allmultis)
 
2487
                        flags |= IFF_ALLMULTI;
 
2488
                if (local->iff_promiscs)
 
2489
                        flags |= IFF_PROMISC;
 
2490
                read_lock(&local->sub_if_lock);
 
2491
                local->ops->set_multicast_list(local_to_hw(local), flags,
 
2492
                                              local->mc_count);
 
2493
                read_unlock(&local->sub_if_lock);
 
2494
        }
 
2495
        netif_tx_unlock(local->mdev);
 
2496
}
 
2497
 
 
2498
struct dev_mc_list *iwlwifi_ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
 
2499
                                               struct dev_mc_list *prev,
 
2500
                                               void **ptr)
 
2501
{
 
2502
        struct ieee80211_local *local = hw_to_local(hw);
 
2503
        struct ieee80211_sub_if_data *sdata = *ptr;
 
2504
        struct dev_mc_list *mc;
 
2505
 
 
2506
        if (!prev) {
 
2507
                WARN_ON(sdata);
 
2508
                sdata = NULL;
 
2509
        }
 
2510
        if (!prev || !prev->next) {
 
2511
                if (sdata)
 
2512
                        sdata = list_entry(sdata->list.next,
 
2513
                                           struct ieee80211_sub_if_data, list);
 
2514
                else
 
2515
                        sdata = list_entry(local->sub_if_list.next,
 
2516
                                           struct ieee80211_sub_if_data, list);
 
2517
                if (&sdata->list != &local->sub_if_list)
 
2518
                        mc = sdata->dev->mc_list;
 
2519
                else
 
2520
                        mc = NULL;
 
2521
        } else
 
2522
                mc = prev->next;
 
2523
 
 
2524
        *ptr = sdata;
 
2525
        return mc;
 
2526
}
 
2527
EXPORT_SYMBOL(iwlwifi_ieee80211_get_mc_list_item);
 
2528
 
 
2529
static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
 
2530
{
 
2531
        struct ieee80211_sub_if_data *sdata;
 
2532
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2533
        return &(sdata->stats);
 
2534
}
 
2535
 
 
2536
static void ieee80211_if_shutdown(struct net_device *dev)
 
2537
{
 
2538
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2539
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2540
 
 
2541
        ASSERT_RTNL();
 
2542
        switch (sdata->type) {
 
2543
        case IEEE80211_IF_TYPE_STA:
 
2544
        case IEEE80211_IF_TYPE_IBSS:
 
2545
                sdata->u.sta.state = IEEE80211_DISABLED;
 
2546
                del_timer_sync(&sdata->u.sta.timer);
 
2547
                del_timer_sync(&sdata->u.sta.admit_timer);
 
2548
                skb_queue_purge(&sdata->u.sta.skb_queue);
 
2549
                if (!local->ops->hw_scan &&
 
2550
                    local->scan_dev == sdata->dev) {
 
2551
                        local->sta_sw_scanning = 0;
 
2552
                        cancel_delayed_work(&local->scan_work);
 
2553
                }
 
2554
                flush_workqueue(local->hw.workqueue);
 
2555
 
 
2556
                kfree(sdata->u.sta.extra_ie);
 
2557
                sdata->u.sta.extra_ie = NULL;
 
2558
                break;
 
2559
        }
 
2560
}
 
2561
 
 
2562
static inline int identical_mac_addr_allowed(int type1, int type2)
 
2563
{
 
2564
        return (type1 == IEEE80211_IF_TYPE_MNTR ||
 
2565
                type2 == IEEE80211_IF_TYPE_MNTR ||
 
2566
                (type1 == IEEE80211_IF_TYPE_AP &&
 
2567
                 type2 == IEEE80211_IF_TYPE_WDS) ||
 
2568
                (type1 == IEEE80211_IF_TYPE_WDS &&
 
2569
                 (type2 == IEEE80211_IF_TYPE_WDS ||
 
2570
                  type2 == IEEE80211_IF_TYPE_AP)) ||
 
2571
                (type1 == IEEE80211_IF_TYPE_AP &&
 
2572
                 type2 == IEEE80211_IF_TYPE_VLAN) ||
 
2573
                (type1 == IEEE80211_IF_TYPE_VLAN &&
 
2574
                 (type2 == IEEE80211_IF_TYPE_AP ||
 
2575
                  type2 == IEEE80211_IF_TYPE_VLAN)));
 
2576
}
 
2577
 
 
2578
static int ieee80211_master_open(struct net_device *dev)
 
2579
{
 
2580
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2581
        struct ieee80211_sub_if_data *sdata;
 
2582
        int res = -EOPNOTSUPP;
 
2583
 
 
2584
        read_lock(&local->sub_if_lock);
 
2585
        list_for_each_entry(sdata, &local->sub_if_list, list) {
 
2586
                if (sdata->dev != dev && netif_running(sdata->dev)) {
 
2587
                        res = 0;
 
2588
                        break;
 
2589
                }
 
2590
        }
 
2591
        read_unlock(&local->sub_if_lock);
 
2592
        return res;
 
2593
}
 
2594
 
 
2595
static int ieee80211_master_stop(struct net_device *dev)
 
2596
{
 
2597
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2598
        struct ieee80211_sub_if_data *sdata;
 
2599
 
 
2600
        read_lock(&local->sub_if_lock);
 
2601
        list_for_each_entry(sdata, &local->sub_if_list, list)
 
2602
                if (sdata->dev != dev && netif_running(sdata->dev))
 
2603
                        dev_close(sdata->dev);
 
2604
        read_unlock(&local->sub_if_lock);
 
2605
 
 
2606
        return 0;
 
2607
}
 
2608
 
 
2609
static int ieee80211_mgmt_open(struct net_device *dev)
 
2610
{
 
2611
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2612
 
 
2613
        if (!netif_running(local->mdev))
 
2614
                return -EOPNOTSUPP;
 
2615
        return 0;
 
2616
}
 
2617
 
 
2618
static int ieee80211_mgmt_stop(struct net_device *dev)
 
2619
{
 
2620
        return 0;
 
2621
}
 
2622
 
 
2623
/* Check if running monitor interfaces should go to a "soft monitor" mode
 
2624
 * and switch them if necessary. */
 
2625
static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
 
2626
{
 
2627
        struct ieee80211_if_init_conf conf;
 
2628
 
 
2629
        if (local->open_count && local->open_count == local->monitors &&
 
2630
            !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
 
2631
            local->ops->remove_interface) {
 
2632
                conf.if_id = -1;
 
2633
                conf.type = IEEE80211_IF_TYPE_MNTR;
 
2634
                conf.mac_addr = NULL;
 
2635
                local->ops->remove_interface(local_to_hw(local), &conf);
 
2636
        }
 
2637
}
 
2638
 
 
2639
/* Check if running monitor interfaces should go to a "hard monitor" mode
 
2640
 * and switch them if necessary. */
 
2641
static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
 
2642
{
 
2643
        struct ieee80211_if_init_conf conf;
 
2644
 
 
2645
        if (local->open_count && local->open_count == local->monitors &&
 
2646
            !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
 
2647
                conf.if_id = -1;
 
2648
                conf.type = IEEE80211_IF_TYPE_MNTR;
 
2649
                conf.mac_addr = NULL;
 
2650
                local->ops->add_interface(local_to_hw(local), &conf);
 
2651
        }
 
2652
}
 
2653
 
 
2654
static int ieee80211_open(struct net_device *dev)
 
2655
{
 
2656
        struct ieee80211_sub_if_data *sdata, *nsdata;
 
2657
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2658
        struct ieee80211_if_init_conf conf;
 
2659
        int res;
 
2660
 
 
2661
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2662
        read_lock(&local->sub_if_lock);
 
2663
        list_for_each_entry(nsdata, &local->sub_if_list, list) {
 
2664
                struct net_device *ndev = nsdata->dev;
 
2665
 
 
2666
                if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
 
2667
                    compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
 
2668
                    !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
 
2669
                        read_unlock(&local->sub_if_lock);
 
2670
                        return -ENOTUNIQ;
 
2671
                }
 
2672
        }
 
2673
        read_unlock(&local->sub_if_lock);
 
2674
 
 
2675
        if (sdata->type == IEEE80211_IF_TYPE_WDS &&
 
2676
            is_zero_ether_addr(sdata->u.wds.remote_addr))
 
2677
                return -ENOLINK;
 
2678
 
 
2679
        if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
 
2680
            !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
 
2681
                /* run the interface in a "soft monitor" mode */
 
2682
                local->monitors++;
 
2683
                local->open_count++;
 
2684
                local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
 
2685
                return 0;
 
2686
        }
 
2687
        ieee80211_start_soft_monitor(local);
 
2688
 
 
2689
        conf.if_id = dev->ifindex;
 
2690
        conf.type = sdata->type;
 
2691
        conf.mac_addr = dev->dev_addr;
 
2692
        res = local->ops->add_interface(local_to_hw(local), &conf);
 
2693
        if (res) {
 
2694
                if (sdata->type == IEEE80211_IF_TYPE_MNTR)
 
2695
                        ieee80211_start_hard_monitor(local);
 
2696
                return res;
 
2697
        }
 
2698
 
 
2699
        if (local->open_count == 0) {
 
2700
                res = 0;
 
2701
                if (local->ops->open)
 
2702
                        res = local->ops->open(local_to_hw(local));
 
2703
                if (res == 0) {
 
2704
                        res = dev_open(local->mdev);
 
2705
                        if (res) {
 
2706
                                if (local->ops->stop)
 
2707
                                        local->ops->stop(local_to_hw(local));
 
2708
                        } else {
 
2709
                                res = ieee80211_hw_config(local);
 
2710
                                if (res && local->ops->stop)
 
2711
                                        local->ops->stop(local_to_hw(local));
 
2712
                                else if (!res && local->apdev)
 
2713
                                        dev_open(local->apdev);
 
2714
                        }
 
2715
                }
 
2716
                if (res) {
 
2717
                        if (local->ops->remove_interface)
 
2718
                                local->ops->remove_interface(local_to_hw(local),
 
2719
                                                            &conf);
 
2720
                        return res;
 
2721
                }
 
2722
                /* enable tasklets only if all callbacks return correctly */
 
2723
                tasklet_enable(&local->tx_pending_tasklet);
 
2724
                tasklet_enable(&local->tasklet);
 
2725
        }
 
2726
        local->open_count++;
 
2727
 
 
2728
        if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
 
2729
                local->monitors++;
 
2730
                local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
 
2731
        } else
 
2732
                ieee80211_if_config(dev);
 
2733
 
 
2734
        if (sdata->type == IEEE80211_IF_TYPE_STA &&
 
2735
            !local->user_space_mlme)
 
2736
                netif_carrier_off(dev);
 
2737
        else
 
2738
                netif_carrier_on(dev);
 
2739
 
 
2740
        netif_start_queue(dev);
 
2741
        return 0;
 
2742
}
 
2743
 
 
2744
 
 
2745
static int ieee80211_stop(struct net_device *dev)
 
2746
{
 
2747
        struct ieee80211_sub_if_data *sdata;
 
2748
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
2749
 
 
2750
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2751
 
 
2752
        if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
 
2753
            local->open_count > 1 &&
 
2754
            !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
 
2755
                /* remove "soft monitor" interface */
 
2756
                local->open_count--;
 
2757
                local->monitors--;
 
2758
                if (!local->monitors)
 
2759
                        local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
 
2760
                return 0;
 
2761
        }
 
2762
 
 
2763
        netif_stop_queue(dev);
 
2764
        ieee80211_if_shutdown(dev);
 
2765
 
 
2766
        if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
 
2767
                local->monitors--;
 
2768
                if (!local->monitors)
 
2769
                        local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
 
2770
        }
 
2771
 
 
2772
        local->open_count--;
 
2773
        if (local->open_count == 0) {
 
2774
                if (netif_running(local->mdev))
 
2775
                        dev_close(local->mdev);
 
2776
                if (local->apdev)
 
2777
                        dev_close(local->apdev);
 
2778
                if (local->ops->stop)
 
2779
                        local->ops->stop(local_to_hw(local));
 
2780
                tasklet_disable(&local->tx_pending_tasklet);
 
2781
                tasklet_disable(&local->tasklet);
 
2782
        }
 
2783
        if (local->ops->remove_interface) {
 
2784
                struct ieee80211_if_init_conf conf;
 
2785
 
 
2786
                conf.if_id = dev->ifindex;
 
2787
                conf.type = sdata->type;
 
2788
                conf.mac_addr = dev->dev_addr;
 
2789
                local->ops->remove_interface(local_to_hw(local), &conf);
 
2790
        }
 
2791
 
 
2792
        ieee80211_start_hard_monitor(local);
 
2793
 
 
2794
        return 0;
 
2795
}
 
2796
 
 
2797
 
 
2798
static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
 
2799
{
 
2800
        memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
 
2801
        return ETH_ALEN;
 
2802
}
 
2803
 
 
2804
static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
 
2805
{
 
2806
        return compare_ether_addr(raddr, addr) == 0 ||
 
2807
               is_broadcast_ether_addr(raddr);
 
2808
}
 
2809
 
 
2810
 
 
2811
inline static unsigned int calc_pad_len(unsigned int len)
 
2812
{
 
2813
        return ((4 - len) & 0x3);
 
2814
}
 
2815
 
 
2816
static ieee80211_txrx_result
 
2817
ieee80211_rx_h_data_agg(struct ieee80211_txrx_data *rx)
 
2818
{
 
2819
        struct net_device *dev = rx->dev;
 
2820
        struct ieee80211_local *local = rx->local;
 
2821
        u16 fc, hdrlen, ethertype;
 
2822
        u8 *payload;
 
2823
        struct sk_buff *skb = rx->skb, *skb2, *frame = NULL;
 
2824
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
2825
        const struct ethhdr* eth;
 
2826
        int remaining;
 
2827
        u8 dst[ETH_ALEN];
 
2828
        u8 src[ETH_ALEN];
 
2829
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
 
2830
 
 
2831
 
 
2832
        fc = rx->fc;
 
2833
        if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
 
2834
                return TXRX_CONTINUE;
 
2835
 
 
2836
        if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
 
2837
                return TXRX_DROP;
 
2838
 
 
2839
        if (!rx->u.rx.is_agg_frame)
 
2840
                return TXRX_CONTINUE;
 
2841
 
 
2842
        hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
 
2843
 
 
2844
        switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 
2845
        case IEEE80211_FCTL_TODS:
 
2846
                /* BSSID SA DA */
 
2847
                if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
 
2848
                    sdata->type != IEEE80211_IF_TYPE_VLAN)) {
 
2849
                        printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
 
2850
                               MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
 
2851
                               dev->name, MAC_ARG(hdr->addr1),
 
2852
                               MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
 
2853
                        return TXRX_DROP;
 
2854
                }
 
2855
                break;
 
2856
        case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 
2857
                /* RA TA DA SA */
 
2858
                if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
 
2859
                        printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
 
2860
                               MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
 
2861
                               MAC_FMT ")\n",
 
2862
                               rx->dev->name, MAC_ARG(hdr->addr1),
 
2863
                               MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
 
2864
                               MAC_ARG(hdr->addr4));
 
2865
                        return TXRX_DROP;
 
2866
                }
 
2867
                break;
 
2868
        case IEEE80211_FCTL_FROMDS:
 
2869
                /* DA BSSID SA */
 
2870
                if (sdata->type != IEEE80211_IF_TYPE_STA)
 
2871
                        return TXRX_DROP;
 
2872
                break;
 
2873
        case 0:
 
2874
                /* DA SA BSSID */
 
2875
                if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
 
2876
                        if (net_ratelimit()) {
 
2877
                                printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
 
2878
                                       MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
 
2879
                                       ")\n",
 
2880
                                       dev->name, MAC_ARG(hdr->addr1),
 
2881
                                       MAC_ARG(hdr->addr2),
 
2882
                                       MAC_ARG(hdr->addr3));
 
2883
                        }
 
2884
                        return TXRX_DROP;
 
2885
                }
 
2886
                break;
 
2887
        }
 
2888
 
 
2889
        if (unlikely((skb->len - hdrlen) < 8)) {
 
2890
                if (net_ratelimit())
 
2891
                        printk(KERN_DEBUG "%s: RX too short data frame "
 
2892
                               "payload\n", dev->name);
 
2893
                return TXRX_DROP;
 
2894
        }
 
2895
 
 
2896
        eth = (struct ethhdr *) skb_pull(skb, hdrlen);
 
2897
 
 
2898
        payload = skb->data;
 
2899
        ethertype = (payload[6] << 8) | payload[7];
 
2900
 
 
2901
        if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
 
2902
            ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 
2903
            compare_ether_addr(payload,
 
2904
            bridge_tunnel_header) == 0))
 
2905
                eth = (struct ethhdr *) skb_pull(skb, 8);
 
2906
 
 
2907
        if (!eth)
 
2908
                return TXRX_DROP;
 
2909
 
 
2910
        frame = NULL;
 
2911
        while (skb != frame) {
 
2912
                u8 padding;
 
2913
                __be16 len = eth->h_proto;
 
2914
                unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
 
2915
 
 
2916
                remaining = skb->len;
 
2917
                memcpy(dst, eth->h_dest, ETH_ALEN);
 
2918
                memcpy(src, eth->h_source, ETH_ALEN);
 
2919
 
 
2920
                padding = calc_pad_len(subframe_len);
 
2921
                /* the last MSDU has no padding */
 
2922
                if (subframe_len > remaining) {
 
2923
                        printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
 
2924
                        return TXRX_DROP;
 
2925
                }
 
2926
                
 
2927
                skb_pull(skb, sizeof(struct ethhdr));
 
2928
                /* if last subframe reuse skb */
 
2929
                if (remaining  <= subframe_len + padding)
 
2930
                        frame = skb;
 
2931
                else {
 
2932
                        frame = dev_alloc_skb(local->hw.extra_tx_headroom +
 
2933
                                              subframe_len);
 
2934
 
 
2935
                        if (frame == NULL)
 
2936
                                return TXRX_DROP;
 
2937
 
 
2938
                        skb_reserve(frame, local->hw.extra_tx_headroom +
 
2939
                                    sizeof(struct ethhdr));
 
2940
                        memcpy(skb_put(frame, ntohs(len)), skb->data,
 
2941
                                ntohs(len));
 
2942
 
 
2943
                        eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
 
2944
                                                        padding);
 
2945
                        if (!eth) {
 
2946
                                printk(KERN_DEBUG "%s: wrong buffer size ",
 
2947
                                       dev->name);
 
2948
                                dev_kfree_skb(frame);
 
2949
                                return TXRX_DROP;
 
2950
                        }
 
2951
                }
 
2952
                skb2 = NULL;
 
2953
 
 
2954
                payload = frame->data;
 
2955
                ethertype = (payload[6] << 8) | payload[7];
 
2956
 
 
2957
                if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
 
2958
                        ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 
2959
                        compare_ether_addr(payload,
 
2960
                                           bridge_tunnel_header) == 0)) {
 
2961
                        /* remove RFC1042 or Bridge-Tunnel
 
2962
                         * encapsulation and replace EtherType */
 
2963
                        skb_pull(frame, 6);
 
2964
                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 
2965
                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 
2966
                } else {
 
2967
                        memcpy(skb_push(frame, sizeof(__be16)), &len,
 
2968
                                sizeof(__be16));
 
2969
                        memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
 
2970
                        memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
 
2971
                }
 
2972
                sdata->stats.rx_packets++;
 
2973
                sdata->stats.rx_bytes += frame->len;
 
2974
 
 
2975
                if (local->bridge_packets &&
 
2976
                    (sdata->type == IEEE80211_IF_TYPE_AP ||
 
2977
                     sdata->type == IEEE80211_IF_TYPE_VLAN) &&
 
2978
                     rx->u.rx.ra_match) {
 
2979
                        if (is_multicast_ether_addr(frame->data)) {
 
2980
                                /* send multicast frames both to higher layers
 
2981
                                 * in local net stack and back to the wireless
 
2982
                                 * media */
 
2983
                                skb2 = skb_copy(frame, GFP_ATOMIC);
 
2984
                                if (!skb2)
 
2985
                                        printk(KERN_DEBUG "%s: failed to clone"
 
2986
                                               " multicast frame\n", dev->name);
 
2987
                        } else {
 
2988
                                struct sta_info *dsta;
 
2989
 
 
2990
                                dsta = iwlwifi_sta_info_get(local, frame->data);
 
2991
                                if (dsta && !dsta->dev)
 
2992
                                        printk(KERN_DEBUG "Station with null "
 
2993
                                               "dev structure!\n");
 
2994
                                else if (dsta && dsta->dev == dev) {
 
2995
                                        /* Destination station is associated
 
2996
                                         *to this AP, so send the frame
 
2997
                                         * directly to it and do not pass
 
2998
                                         * the frame to local net stack.
 
2999
                                         */
 
3000
                                        skb2 = frame;
 
3001
                                        frame = NULL;
 
3002
                                }
 
3003
                                if (dsta)
 
3004
                                        iwlwifi_sta_info_put(dsta);
 
3005
                        }
 
3006
                }
 
3007
                if (frame) {
 
3008
                        /* deliver to local stack */
 
3009
                        skb_set_network_header(frame, 0);
 
3010
                        frame->protocol = eth_type_trans(frame, dev);
 
3011
                        frame->priority = skb->priority;
 
3012
                        netif_rx(frame);
 
3013
                }
 
3014
 
 
3015
                if (skb2) {
 
3016
                        /* send to wireless media */
 
3017
                        skb2->protocol = __constant_htons(ETH_P_802_3);
 
3018
                        skb_set_network_header(skb2, 0);
 
3019
                        skb_set_mac_header(skb2, 0);
 
3020
                        skb2->priority = skb->priority;
 
3021
                        skb2->dev = dev;
 
3022
                        dev_queue_xmit(skb2);
 
3023
                }
 
3024
 
 
3025
        }
 
3026
        return TXRX_QUEUED;
 
3027
}
 
3028
 
 
3029
static ieee80211_txrx_result
 
3030
ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
 
3031
{
 
3032
        struct net_device *dev = rx->dev;
 
3033
        struct ieee80211_local *local = rx->local;
 
3034
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
 
3035
        u16 fc, hdrlen, ethertype;
 
3036
        u8 *payload;
 
3037
        u8 dst[ETH_ALEN];
 
3038
        u8 src[ETH_ALEN];
 
3039
        struct sk_buff *skb = rx->skb, *skb2;
 
3040
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
3041
 
 
3042
        fc = rx->fc;
 
3043
        if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
 
3044
                return TXRX_CONTINUE;
 
3045
 
 
3046
        if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
 
3047
                return TXRX_DROP;
 
3048
 
 
3049
        hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
 
3050
 
 
3051
        /* convert IEEE 802.11 header + possible LLC headers into Ethernet
 
3052
         * header
 
3053
         * IEEE 802.11 address fields:
 
3054
         * ToDS FromDS Addr1 Addr2 Addr3 Addr4
 
3055
         *   0     0   DA    SA    BSSID n/a
 
3056
         *   0     1   DA    BSSID SA    n/a
 
3057
         *   1     0   BSSID SA    DA    n/a
 
3058
         *   1     1   RA    TA    DA    SA
 
3059
         */
 
3060
 
 
3061
        switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
 
3062
        case IEEE80211_FCTL_TODS:
 
3063
                /* BSSID SA DA */
 
3064
                memcpy(dst, hdr->addr3, ETH_ALEN);
 
3065
                memcpy(src, hdr->addr2, ETH_ALEN);
 
3066
 
 
3067
                if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
 
3068
                             sdata->type != IEEE80211_IF_TYPE_VLAN)) {
 
3069
                        printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
 
3070
                               MAC_FMT " SA=" MAC_FMT " DA=" MAC_FMT ")\n",
 
3071
                               dev->name, MAC_ARG(hdr->addr1),
 
3072
                               MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3));
 
3073
                        return TXRX_DROP;
 
3074
                }
 
3075
                break;
 
3076
        case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
 
3077
                /* RA TA DA SA */
 
3078
                memcpy(dst, hdr->addr3, ETH_ALEN);
 
3079
                memcpy(src, hdr->addr4, ETH_ALEN);
 
3080
 
 
3081
                if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
 
3082
                        printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
 
3083
                               MAC_FMT " TA=" MAC_FMT " DA=" MAC_FMT " SA="
 
3084
                               MAC_FMT ")\n",
 
3085
                               rx->dev->name, MAC_ARG(hdr->addr1),
 
3086
                               MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr3),
 
3087
                               MAC_ARG(hdr->addr4));
 
3088
                        return TXRX_DROP;
 
3089
                }
 
3090
                break;
 
3091
        case IEEE80211_FCTL_FROMDS:
 
3092
                /* DA BSSID SA */
 
3093
                memcpy(dst, hdr->addr1, ETH_ALEN);
 
3094
                memcpy(src, hdr->addr3, ETH_ALEN);
 
3095
 
 
3096
                if (sdata->type != IEEE80211_IF_TYPE_STA ||
 
3097
                    (is_multicast_ether_addr(dst) &&
 
3098
                     !compare_ether_addr(src, dev->dev_addr)))
 
3099
                        return TXRX_DROP;
 
3100
                break;
 
3101
        case 0:
 
3102
                /* DA SA BSSID */
 
3103
                memcpy(dst, hdr->addr1, ETH_ALEN);
 
3104
                memcpy(src, hdr->addr2, ETH_ALEN);
 
3105
 
 
3106
                if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
 
3107
                        if (net_ratelimit()) {
 
3108
                                printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
 
3109
                                       MAC_FMT " SA=" MAC_FMT " BSSID=" MAC_FMT
 
3110
                                       ")\n",
 
3111
                                       dev->name, MAC_ARG(hdr->addr1),
 
3112
                                       MAC_ARG(hdr->addr2),
 
3113
                                       MAC_ARG(hdr->addr3));
 
3114
                        }
 
3115
                        return TXRX_DROP;
 
3116
                }
 
3117
                break;
 
3118
        }
 
3119
 
 
3120
        payload = skb->data + hdrlen;
 
3121
 
 
3122
        if (unlikely(skb->len - hdrlen < 8)) {
 
3123
                if (net_ratelimit()) {
 
3124
                        printk(KERN_DEBUG "%s: RX too short data frame "
 
3125
                               "payload\n", dev->name);
 
3126
                }
 
3127
                return TXRX_DROP;
 
3128
        }
 
3129
 
 
3130
        ethertype = (payload[6] << 8) | payload[7];
 
3131
 
 
3132
        if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
 
3133
                    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
 
3134
                   compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
 
3135
                /* remove RFC1042 or Bridge-Tunnel encapsulation and
 
3136
                 * replace EtherType */
 
3137
                skb_pull(skb, hdrlen + 6);
 
3138
                memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
 
3139
                memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
 
3140
        } else {
 
3141
                struct ethhdr *ehdr;
 
3142
                __be16 len;
 
3143
                skb_pull(skb, hdrlen);
 
3144
                len = htons(skb->len);
 
3145
                ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
 
3146
                memcpy(ehdr->h_dest, dst, ETH_ALEN);
 
3147
                memcpy(ehdr->h_source, src, ETH_ALEN);
 
3148
                ehdr->h_proto = len;
 
3149
        }
 
3150
        skb->dev = dev;
 
3151
 
 
3152
        skb2 = NULL;
 
3153
 
 
3154
        sdata->stats.rx_packets++;
 
3155
        sdata->stats.rx_bytes += skb->len;
 
3156
 
 
3157
        if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP
 
3158
            || sdata->type == IEEE80211_IF_TYPE_VLAN) && rx->u.rx.ra_match) {
 
3159
                if (is_multicast_ether_addr(skb->data)) {
 
3160
                        /* send multicast frames both to higher layers in
 
3161
                         * local net stack and back to the wireless media */
 
3162
                        skb2 = skb_copy(skb, GFP_ATOMIC);
 
3163
                        if (!skb2)
 
3164
                                printk(KERN_DEBUG "%s: failed to clone "
 
3165
                                       "multicast frame\n", dev->name);
 
3166
                } else {
 
3167
                        struct sta_info *dsta;
 
3168
                        dsta = iwlwifi_sta_info_get(local, skb->data);
 
3169
                        if (dsta && !dsta->dev) {
 
3170
                                printk(KERN_DEBUG "Station with null dev "
 
3171
                                       "structure!\n");
 
3172
                        } else if (dsta && dsta->dev == dev) {
 
3173
                                /* Destination station is associated to this
 
3174
                                 * AP, so send the frame directly to it and
 
3175
                                 * do not pass the frame to local net stack.
 
3176
                                 */
 
3177
                                skb2 = skb;
 
3178
                                skb = NULL;
 
3179
                        }
 
3180
                        if (dsta)
 
3181
                                iwlwifi_sta_info_put(dsta);
 
3182
                }
 
3183
        }
 
3184
 
 
3185
        if (skb) {
 
3186
                /* deliver to local stack */
 
3187
                skb->protocol = eth_type_trans(skb, dev);
 
3188
                memset(skb->cb, 0, sizeof(skb->cb));
 
3189
                netif_rx(skb);
 
3190
        }
 
3191
 
 
3192
        if (skb2) {
 
3193
                /* send to wireless media */
 
3194
                skb2->protocol = __constant_htons(ETH_P_802_3);
 
3195
                skb_set_network_header(skb2, 0);
 
3196
                skb_set_mac_header(skb2, 0);
 
3197
                dev_queue_xmit(skb2);
 
3198
        }
 
3199
 
 
3200
        return TXRX_QUEUED;
 
3201
}
 
3202
 
 
3203
 
 
3204
static struct ieee80211_rate *
 
3205
ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
 
3206
{
 
3207
        struct ieee80211_hw_mode *mode;
 
3208
        int r;
 
3209
 
 
3210
        list_for_each_entry(mode, &local->modes_list, list) {
 
3211
                if (mode->mode != phymode)
 
3212
                        continue;
 
3213
                for (r = 0; r < mode->num_rates; r++) {
 
3214
                        struct ieee80211_rate *rate = &mode->rates[r];
 
3215
                        if (rate->val == hw_rate ||
 
3216
                            (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
 
3217
                             rate->val2 == hw_rate))
 
3218
                                return rate;
 
3219
                }
 
3220
        }
 
3221
 
 
3222
        return NULL;
 
3223
}
 
3224
 
 
3225
static void
 
3226
ieee80211_fill_frame_info(struct ieee80211_local *local,
 
3227
                          struct ieee80211_frame_info *fi,
 
3228
                          struct ieee80211_rx_status *status)
 
3229
{
 
3230
        if (status) {
 
3231
                struct timespec ts;
 
3232
                struct ieee80211_rate *rate;
 
3233
 
 
3234
                jiffies_to_timespec(jiffies, &ts);
 
3235
                fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
 
3236
                                           ts.tv_nsec / 1000);
 
3237
                fi->mactime = cpu_to_be64(status->mactime);
 
3238
                switch (status->phymode) {
 
3239
                case MODE_IEEE80211A:
 
3240
                        fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
 
3241
                        break;
 
3242
                case MODE_IEEE80211B:
 
3243
                        fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
 
3244
                        break;
 
3245
                case MODE_IEEE80211G:
 
3246
                        fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
 
3247
                        break;
 
3248
                case MODE_ATHEROS_TURBO:
 
3249
                        fi->phytype =
 
3250
                                htonl(ieee80211_phytype_dsss_dot11_turbo);
 
3251
                        break;
 
3252
                default:
 
3253
                        fi->phytype = htonl(0xAAAAAAAA);
 
3254
                        break;
 
3255
                }
 
3256
                fi->channel = htonl(status->channel);
 
3257
                rate = ieee80211_get_rate(local, status->phymode,
 
3258
                                          status->rate);
 
3259
                if (rate) {
 
3260
                        fi->datarate = htonl(rate->rate);
 
3261
                        if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
 
3262
                                if (status->rate == rate->val)
 
3263
                                        fi->preamble = htonl(2); /* long */
 
3264
                                else if (status->rate == rate->val2)
 
3265
                                        fi->preamble = htonl(1); /* short */
 
3266
                        } else
 
3267
                                fi->preamble = htonl(0);
 
3268
                } else {
 
3269
                        fi->datarate = htonl(0);
 
3270
                        fi->preamble = htonl(0);
 
3271
                }
 
3272
 
 
3273
                fi->antenna = htonl(status->antenna);
 
3274
                fi->priority = htonl(0xffffffff); /* no clue */
 
3275
                fi->ssi_type = htonl(ieee80211_ssi_raw);
 
3276
                fi->ssi_signal = htonl(status->ssi);
 
3277
                fi->ssi_noise = 0x00000000;
 
3278
                fi->encoding = 0;
 
3279
        } else {
 
3280
                /* clear everything because we really don't know.
 
3281
                 * the msg_type field isn't present on monitor frames
 
3282
                 * so we don't know whether it will be present or not,
 
3283
                 * but it's ok to not clear it since it'll be assigned
 
3284
                 * anyway */
 
3285
                memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
 
3286
 
 
3287
                fi->ssi_type = htonl(ieee80211_ssi_none);
 
3288
        }
 
3289
        fi->version = htonl(IEEE80211_FI_VERSION);
 
3290
        fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
 
3291
}
 
3292
 
 
3293
/* this routine is actually not just for this, but also
 
3294
 * for pushing fake 'management' frames into userspace.
 
3295
 * it shall be replaced by a netlink-based system. */
 
3296
void
 
3297
ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
 
3298
                  struct ieee80211_rx_status *status, u32 msg_type)
 
3299
{
 
3300
        struct ieee80211_frame_info *fi;
 
3301
        const size_t hlen = sizeof(struct ieee80211_frame_info);
 
3302
        struct ieee80211_sub_if_data *sdata;
 
3303
 
 
3304
        skb->dev = local->apdev;
 
3305
 
 
3306
        sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
 
3307
 
 
3308
        if (skb_headroom(skb) < hlen) {
 
3309
                I802_DEBUG_INC(local->rx_expand_skb_head);
 
3310
                if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
 
3311
                        dev_kfree_skb(skb);
 
3312
                        return;
 
3313
                }
 
3314
        }
 
3315
 
 
3316
        fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
 
3317
 
 
3318
        ieee80211_fill_frame_info(local, fi, status);
 
3319
        fi->msg_type = htonl(msg_type);
 
3320
 
 
3321
        sdata->stats.rx_packets++;
 
3322
        sdata->stats.rx_bytes += skb->len;
 
3323
 
 
3324
        skb_set_mac_header(skb, 0);
 
3325
        skb->ip_summed = CHECKSUM_UNNECESSARY;
 
3326
        skb->pkt_type = PACKET_OTHERHOST;
 
3327
        skb->protocol = htons(ETH_P_802_2);
 
3328
        memset(skb->cb, 0, sizeof(skb->cb));
 
3329
        netif_rx(skb);
 
3330
}
 
3331
 
 
3332
static void
 
3333
ieee80211_rx_monitor(struct net_device *dev, struct sk_buff *skb,
 
3334
                     struct ieee80211_rx_status *status)
 
3335
{
 
3336
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
3337
        struct ieee80211_sub_if_data *sdata;
 
3338
        struct ieee80211_rate *rate;
 
3339
        struct ieee80211_rtap_hdr {
 
3340
                struct ieee80211_radiotap_header hdr;
 
3341
                u8 flags;
 
3342
                u8 rate;
 
3343
                __le16 chan_freq;
 
3344
                __le16 chan_flags;
 
3345
                u8 antsignal;
 
3346
        } __attribute__ ((packed)) *rthdr;
 
3347
 
 
3348
        skb->dev = dev;
 
3349
 
 
3350
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
3351
 
 
3352
        if (status->flag & RX_FLAG_RADIOTAP)
 
3353
                goto out;
 
3354
 
 
3355
        if (skb_headroom(skb) < sizeof(*rthdr)) {
 
3356
                I802_DEBUG_INC(local->rx_expand_skb_head);
 
3357
                if (pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC)) {
 
3358
                        dev_kfree_skb(skb);
 
3359
                        return;
 
3360
                }
 
3361
        }
 
3362
 
 
3363
        rthdr = (struct ieee80211_rtap_hdr *) skb_push(skb, sizeof(*rthdr));
 
3364
        memset(rthdr, 0, sizeof(*rthdr));
 
3365
        rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
 
3366
        rthdr->hdr.it_present =
 
3367
                cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
 
3368
                            (1 << IEEE80211_RADIOTAP_RATE) |
 
3369
                            (1 << IEEE80211_RADIOTAP_CHANNEL) |
 
3370
                            (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL));
 
3371
        rthdr->flags = local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS ?
 
3372
                       IEEE80211_RADIOTAP_F_FCS : 0;
 
3373
        rate = ieee80211_get_rate(local, status->phymode, status->rate);
 
3374
        if (rate)
 
3375
                rthdr->rate = rate->rate / 5;
 
3376
        rthdr->chan_freq = cpu_to_le16(status->freq);
 
3377
        rthdr->chan_flags =
 
3378
                status->phymode == MODE_IEEE80211A ?
 
3379
                cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ) :
 
3380
                cpu_to_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ);
 
3381
        rthdr->antsignal = status->ssi;
 
3382
 
 
3383
 out:
 
3384
        sdata->stats.rx_packets++;
 
3385
        sdata->stats.rx_bytes += skb->len;
 
3386
 
 
3387
        skb_set_mac_header(skb, 0);
 
3388
        skb->ip_summed = CHECKSUM_UNNECESSARY;
 
3389
        skb->pkt_type = PACKET_OTHERHOST;
 
3390
        skb->protocol = htons(ETH_P_802_2);
 
3391
        memset(skb->cb, 0, sizeof(skb->cb));
 
3392
        netif_rx(skb);
 
3393
}
 
3394
 
 
3395
int iwlwifi_ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
 
3396
                           int radar, int radar_type)
 
3397
{
 
3398
        struct sk_buff *skb;
 
3399
        struct ieee80211_radar_info *msg;
 
3400
        struct ieee80211_local *local = hw_to_local(hw);
 
3401
 
 
3402
        if (!local->apdev)
 
3403
                return 0;
 
3404
 
 
3405
        skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
 
3406
                            sizeof(struct ieee80211_radar_info));
 
3407
 
 
3408
        if (!skb)
 
3409
                return -ENOMEM;
 
3410
        skb_reserve(skb, sizeof(struct ieee80211_frame_info));
 
3411
 
 
3412
        msg = (struct ieee80211_radar_info *)
 
3413
                skb_put(skb, sizeof(struct ieee80211_radar_info));
 
3414
        msg->channel = channel;
 
3415
        msg->radar = radar;
 
3416
        msg->radar_type = radar_type;
 
3417
 
 
3418
        ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
 
3419
        return 0;
 
3420
}
 
3421
EXPORT_SYMBOL(iwlwifi_ieee80211_radar_status);
 
3422
 
 
3423
 
 
3424
static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
 
3425
{
 
3426
        struct ieee80211_sub_if_data *sdata;
 
3427
        sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
 
3428
 
 
3429
        if (sdata->bss)
 
3430
                atomic_inc(&sdata->bss->num_sta_ps);
 
3431
        sta->flags |= WLAN_STA_PS;
 
3432
        sta->pspoll = 0;
 
3433
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
3434
        printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d enters power "
 
3435
               "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
 
3436
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
3437
}
 
3438
 
 
3439
 
 
3440
static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
 
3441
{
 
3442
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
3443
        struct sk_buff *skb;
 
3444
        int sent = 0;
 
3445
        struct ieee80211_sub_if_data *sdata;
 
3446
        struct ieee80211_tx_packet_data *pkt_data;
 
3447
 
 
3448
        sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
 
3449
        if (sdata->bss)
 
3450
                atomic_dec(&sdata->bss->num_sta_ps);
 
3451
        sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
 
3452
        sta->pspoll = 0;
 
3453
        if (!skb_queue_empty(&sta->ps_tx_buf)) {
 
3454
                if (local->ops->set_tim)
 
3455
                        local->ops->set_tim(local_to_hw(local), sta->aid, 0);
 
3456
                if (sdata->bss)
 
3457
                        bss_tim_clear(local, sdata->bss, sta->aid);
 
3458
        }
 
3459
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
3460
        printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d exits power "
 
3461
               "save mode\n", dev->name, MAC_ARG(sta->addr), sta->aid);
 
3462
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
3463
        /* Send all buffered frames to the station */
 
3464
        while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
 
3465
                pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
 
3466
                sent++;
 
3467
                pkt_data->requeue = 1;
 
3468
                dev_queue_xmit(skb);
 
3469
        }
 
3470
        while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
 
3471
                pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
 
3472
                local->total_ps_buffered--;
 
3473
                sent++;
 
3474
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
3475
                printk(KERN_DEBUG "%s: STA " MAC_FMT " aid %d send PS frame "
 
3476
                       "since STA not sleeping anymore\n", dev->name,
 
3477
                       MAC_ARG(sta->addr), sta->aid);
 
3478
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
3479
                pkt_data->requeue = 1;
 
3480
                dev_queue_xmit(skb);
 
3481
        }
 
3482
 
 
3483
        return sent;
 
3484
}
 
3485
 
 
3486
 
 
3487
static ieee80211_txrx_result
 
3488
ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
 
3489
{
 
3490
        struct sk_buff *skb;
 
3491
        int no_pending_pkts;
 
3492
 
 
3493
        if (likely(!rx->sta ||
 
3494
                   (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
 
3495
                   (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
 
3496
                   !rx->u.rx.ra_match))
 
3497
                return TXRX_CONTINUE;
 
3498
 
 
3499
        skb = skb_dequeue(&rx->sta->tx_filtered);
 
3500
        if (!skb) {
 
3501
                skb = skb_dequeue(&rx->sta->ps_tx_buf);
 
3502
                if (skb)
 
3503
                        rx->local->total_ps_buffered--;
 
3504
        }
 
3505
        no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
 
3506
                skb_queue_empty(&rx->sta->ps_tx_buf);
 
3507
 
 
3508
        if (skb) {
 
3509
                struct ieee80211_hdr *hdr =
 
3510
                        (struct ieee80211_hdr *) skb->data;
 
3511
 
 
3512
                /* tell TX path to send one frame even though the STA may
 
3513
                 * still remain is PS mode after this frame exchange */
 
3514
                rx->sta->pspoll = 1;
 
3515
 
 
3516
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
3517
                printk(KERN_DEBUG "STA " MAC_FMT " aid %d: PS Poll (entries "
 
3518
                       "after %d)\n",
 
3519
                       MAC_ARG(rx->sta->addr), rx->sta->aid,
 
3520
                       skb_queue_len(&rx->sta->ps_tx_buf));
 
3521
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
3522
 
 
3523
                /* Use MoreData flag to indicate whether there are more
 
3524
                 * buffered frames for this STA */
 
3525
                if (no_pending_pkts) {
 
3526
                        hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
 
3527
                        rx->sta->flags &= ~WLAN_STA_TIM;
 
3528
                } else
 
3529
                        hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
 
3530
 
 
3531
                dev_queue_xmit(skb);
 
3532
 
 
3533
                if (no_pending_pkts) {
 
3534
                        if (rx->local->ops->set_tim)
 
3535
                                rx->local->ops->set_tim(local_to_hw(rx->local),
 
3536
                                                       rx->sta->aid, 0);
 
3537
                        if (rx->sdata->bss)
 
3538
                                bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
 
3539
                }
 
3540
#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
 
3541
        } else if (!rx->u.rx.sent_ps_buffered) {
 
3542
                printk(KERN_DEBUG "%s: STA " MAC_FMT " sent PS Poll even "
 
3543
                       "though there is no buffered frames for it\n",
 
3544
                       rx->dev->name, MAC_ARG(rx->sta->addr));
 
3545
#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
 
3546
 
 
3547
        }
 
3548
 
 
3549
        /* Free PS Poll skb here instead of returning TXRX_DROP that would
 
3550
         * count as an dropped frame. */
 
3551
        dev_kfree_skb(rx->skb);
 
3552
 
 
3553
        return TXRX_QUEUED;
 
3554
}
 
3555
 
 
3556
 
 
3557
static inline struct ieee80211_fragment_entry *
 
3558
ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
 
3559
                         unsigned int frag, unsigned int seq, int rx_queue,
 
3560
                         struct sk_buff **skb)
 
3561
{
 
3562
        struct ieee80211_fragment_entry *entry;
 
3563
        int idx;
 
3564
 
 
3565
        idx = sdata->fragment_next;
 
3566
        entry = &sdata->fragments[sdata->fragment_next++];
 
3567
        if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
 
3568
                sdata->fragment_next = 0;
 
3569
 
 
3570
        if (!skb_queue_empty(&entry->skb_list)) {
 
3571
#ifdef CONFIG_MAC80211_DEBUG
 
3572
                struct ieee80211_hdr *hdr =
 
3573
                        (struct ieee80211_hdr *) entry->skb_list.next->data;
 
3574
                printk(KERN_DEBUG "%s: RX reassembly removed oldest "
 
3575
                       "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
 
3576
                       "addr1=" MAC_FMT " addr2=" MAC_FMT "\n",
 
3577
                       sdata->dev->name, idx,
 
3578
                       jiffies - entry->first_frag_time, entry->seq,
 
3579
                       entry->last_frag, MAC_ARG(hdr->addr1),
 
3580
                       MAC_ARG(hdr->addr2));
 
3581
#endif /* CONFIG_MAC80211_DEBUG */
 
3582
                __skb_queue_purge(&entry->skb_list);
 
3583
        }
 
3584
 
 
3585
        __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
 
3586
        *skb = NULL;
 
3587
        entry->first_frag_time = jiffies;
 
3588
        entry->seq = seq;
 
3589
        entry->rx_queue = rx_queue;
 
3590
        entry->last_frag = frag;
 
3591
        entry->ccmp = 0;
 
3592
        entry->extra_len = 0;
 
3593
 
 
3594
        return entry;
 
3595
}
 
3596
 
 
3597
 
 
3598
static inline struct ieee80211_fragment_entry *
 
3599
ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
 
3600
                          u16 fc, unsigned int frag, unsigned int seq,
 
3601
                          int rx_queue, struct ieee80211_hdr *hdr)
 
3602
{
 
3603
        struct ieee80211_fragment_entry *entry;
 
3604
        int i, idx;
 
3605
 
 
3606
        idx = sdata->fragment_next;
 
3607
        for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
 
3608
                struct ieee80211_hdr *f_hdr;
 
3609
                u16 f_fc;
 
3610
 
 
3611
                idx--;
 
3612
                if (idx < 0)
 
3613
                        idx = IEEE80211_FRAGMENT_MAX - 1;
 
3614
 
 
3615
                entry = &sdata->fragments[idx];
 
3616
                if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
 
3617
                    entry->rx_queue != rx_queue ||
 
3618
                    entry->last_frag + 1 != frag)
 
3619
                        continue;
 
3620
 
 
3621
                f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
 
3622
                f_fc = le16_to_cpu(f_hdr->frame_control);
 
3623
 
 
3624
                if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
 
3625
                    compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
 
3626
                    compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
 
3627
                        continue;
 
3628
 
 
3629
                if (entry->first_frag_time + 2 * HZ < jiffies) {
 
3630
                        __skb_queue_purge(&entry->skb_list);
 
3631
                        continue;
 
3632
                }
 
3633
                return entry;
 
3634
        }
 
3635
 
 
3636
        return NULL;
 
3637
}
 
3638
 
 
3639
 
 
3640
static ieee80211_txrx_result
 
3641
ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
 
3642
{
 
3643
        struct ieee80211_hdr *hdr;
 
3644
        u16 sc;
 
3645
        unsigned int frag, seq;
 
3646
        struct ieee80211_fragment_entry *entry;
 
3647
        struct sk_buff *skb;
 
3648
 
 
3649
        hdr = (struct ieee80211_hdr *) rx->skb->data;
 
3650
        sc = le16_to_cpu(hdr->seq_ctrl);
 
3651
        frag = sc & IEEE80211_SCTL_FRAG;
 
3652
 
 
3653
        if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
 
3654
                   (rx->skb)->len < 24 ||
 
3655
                   is_multicast_ether_addr(hdr->addr1))) {
 
3656
                /* not fragmented */
 
3657
                goto out;
 
3658
        }
 
3659
        I802_DEBUG_INC(rx->local->rx_handlers_fragments);
 
3660
 
 
3661
        seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
 
3662
 
 
3663
        if (frag == 0) {
 
3664
                /* This is the first fragment of a new frame. */
 
3665
                entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
 
3666
                                                 rx->u.rx.queue, &(rx->skb));
 
3667
                if (rx->key && rx->key->alg == ALG_CCMP &&
 
3668
                    (rx->fc & IEEE80211_FCTL_PROTECTED)) {
 
3669
                        /* Store CCMP PN so that we can verify that the next
 
3670
                         * fragment has a sequential PN value. */
 
3671
                        entry->ccmp = 1;
 
3672
                        memcpy(entry->last_pn,
 
3673
                               rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
 
3674
                               CCMP_PN_LEN);
 
3675
                }
 
3676
                return TXRX_QUEUED;
 
3677
        }
 
3678
 
 
3679
        /* This is a fragment for a frame that should already be pending in
 
3680
         * fragment cache. Add this fragment to the end of the pending entry.
 
3681
         */
 
3682
        entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
 
3683
                                          rx->u.rx.queue, hdr);
 
3684
        if (!entry) {
 
3685
                I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
 
3686
                return TXRX_DROP;
 
3687
        }
 
3688
 
 
3689
        /* Verify that MPDUs within one MSDU have sequential PN values.
 
3690
         * (IEEE 802.11i, 8.3.3.4.5) */
 
3691
        if (entry->ccmp) {
 
3692
                int i;
 
3693
                u8 pn[CCMP_PN_LEN], *rpn;
 
3694
                if (!rx->key || rx->key->alg != ALG_CCMP)
 
3695
                        return TXRX_DROP;
 
3696
                memcpy(pn, entry->last_pn, CCMP_PN_LEN);
 
3697
                for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
 
3698
                        pn[i]++;
 
3699
                        if (pn[i])
 
3700
                                break;
 
3701
                }
 
3702
                rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
 
3703
                if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
 
3704
                        printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
 
3705
                               " A2=" MAC_FMT " PN=%02x%02x%02x%02x%02x%02x "
 
3706
                               "(expected %02x%02x%02x%02x%02x%02x)\n",
 
3707
                               rx->dev->name, MAC_ARG(hdr->addr2),
 
3708
                               rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
 
3709
                               pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
 
3710
                        return TXRX_DROP;
 
3711
                }
 
3712
                memcpy(entry->last_pn, pn, CCMP_PN_LEN);
 
3713
        }
 
3714
 
 
3715
        skb_pull(rx->skb, iwlwifi_ieee80211_get_hdrlen(rx->fc));
 
3716
        __skb_queue_tail(&entry->skb_list, rx->skb);
 
3717
        entry->last_frag = frag;
 
3718
        entry->extra_len += rx->skb->len;
 
3719
        if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
 
3720
                rx->skb = NULL;
 
3721
                return TXRX_QUEUED;
 
3722
        }
 
3723
 
 
3724
        rx->skb = __skb_dequeue(&entry->skb_list);
 
3725
        if (skb_tailroom(rx->skb) < entry->extra_len) {
 
3726
                I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
 
3727
                if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
 
3728
                                              GFP_ATOMIC))) {
 
3729
                        I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
 
3730
                        __skb_queue_purge(&entry->skb_list);
 
3731
                        return TXRX_DROP;
 
3732
                }
 
3733
        }
 
3734
        while ((skb = __skb_dequeue(&entry->skb_list))) {
 
3735
                memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
 
3736
                dev_kfree_skb(skb);
 
3737
        }
 
3738
 
 
3739
        /* Complete frame has been reassembled - process it now */
 
3740
        rx->fragmented = 1;
 
3741
 
 
3742
 out:
 
3743
        if (rx->sta)
 
3744
                rx->sta->rx_packets++;
 
3745
        if (is_multicast_ether_addr(hdr->addr1))
 
3746
                rx->local->dot11MulticastReceivedFrameCount++;
 
3747
        else
 
3748
                ieee80211_led_rx(rx->local);
 
3749
        return TXRX_CONTINUE;
 
3750
}
 
3751
 
 
3752
 
 
3753
static ieee80211_txrx_result
 
3754
ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
 
3755
{
 
3756
        if (rx->sdata->type == IEEE80211_IF_TYPE_MNTR) {
 
3757
                ieee80211_rx_monitor(rx->dev, rx->skb, rx->u.rx.status);
 
3758
                return TXRX_QUEUED;
 
3759
        }
 
3760
 
 
3761
        if (rx->u.rx.status->flag & RX_FLAG_RADIOTAP)
 
3762
                skb_pull(rx->skb, ieee80211_get_radiotap_len(rx->skb));
 
3763
 
 
3764
        return TXRX_CONTINUE;
 
3765
}
 
3766
 
 
3767
 
 
3768
static ieee80211_txrx_result
 
3769
ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
 
3770
{
 
3771
        struct ieee80211_hdr *hdr;
 
3772
        int always_sta_key;
 
3773
        hdr = (struct ieee80211_hdr *) rx->skb->data;
 
3774
 
 
3775
        /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
 
3776
        if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
 
3777
                if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
 
3778
                             rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
 
3779
                             hdr->seq_ctrl)) {
 
3780
                        if (rx->u.rx.ra_match) {
 
3781
                                rx->local->dot11FrameDuplicateCount++;
 
3782
                                rx->sta->num_duplicates++;
 
3783
                        }
 
3784
                        return TXRX_DROP;
 
3785
                } else
 
3786
                        rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
 
3787
        }
 
3788
 
 
3789
        if ((rx->local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) &&
 
3790
            rx->skb->len > FCS_LEN)
 
3791
                skb_trim(rx->skb, rx->skb->len - FCS_LEN);
 
3792
 
 
3793
        if (unlikely(rx->skb->len < 16)) {
 
3794
                I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
 
3795
                return TXRX_DROP;
 
3796
        }
 
3797
 
 
3798
        if (!rx->u.rx.ra_match)
 
3799
                rx->skb->pkt_type = PACKET_OTHERHOST;
 
3800
        else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0)
 
3801
                rx->skb->pkt_type = PACKET_HOST;
 
3802
        else if (is_multicast_ether_addr(hdr->addr1)) {
 
3803
                if (is_broadcast_ether_addr(hdr->addr1))
 
3804
                        rx->skb->pkt_type = PACKET_BROADCAST;
 
3805
                else
 
3806
                        rx->skb->pkt_type = PACKET_MULTICAST;
 
3807
        } else
 
3808
                rx->skb->pkt_type = PACKET_OTHERHOST;
 
3809
 
 
3810
        /* Drop disallowed frame classes based on STA auth/assoc state;
 
3811
         * IEEE 802.11, Chap 5.5.
 
3812
         *
 
3813
         * 80211.o does filtering only based on association state, i.e., it
 
3814
         * drops Class 3 frames from not associated stations. hostapd sends
 
3815
         * deauth/disassoc frames when needed. In addition, hostapd is
 
3816
         * responsible for filtering on both auth and assoc states.
 
3817
         */
 
3818
        if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
 
3819
                      ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
 
3820
                       (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
 
3821
                     rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
 
3822
                     (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
 
3823
                if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
 
3824
                     !(rx->fc & IEEE80211_FCTL_TODS) &&
 
3825
                     (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
 
3826
                    || !rx->u.rx.ra_match) {
 
3827
                        /* Drop IBSS frames and frames for other hosts
 
3828
                         * silently. */
 
3829
                        return TXRX_DROP;
 
3830
                }
 
3831
 
 
3832
                if (!rx->local->apdev)
 
3833
                        return TXRX_DROP;
 
3834
 
 
3835
                ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
 
3836
                                  ieee80211_msg_sta_not_assoc);
 
3837
                return TXRX_QUEUED;
 
3838
        }
 
3839
 
 
3840
        if (rx->sdata->type == IEEE80211_IF_TYPE_STA)
 
3841
                always_sta_key = 0;
 
3842
        else
 
3843
                always_sta_key = 1;
 
3844
 
 
3845
        if (rx->sta && rx->sta->key && always_sta_key) {
 
3846
                rx->key = rx->sta->key;
 
3847
        } else {
 
3848
                if (rx->sta && rx->sta->key)
 
3849
                        rx->key = rx->sta->key;
 
3850
                else
 
3851
                        rx->key = rx->sdata->default_key;
 
3852
 
 
3853
                if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
 
3854
                    rx->fc & IEEE80211_FCTL_PROTECTED) {
 
3855
                        int keyidx = ieee80211_wep_get_keyidx(rx->skb);
 
3856
 
 
3857
                        if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
 
3858
                            (!rx->sta || !rx->sta->key || keyidx > 0))
 
3859
                                rx->key = rx->sdata->keys[keyidx];
 
3860
 
 
3861
                        if (!rx->key) {
 
3862
                                if (!rx->u.rx.ra_match)
 
3863
                                        return TXRX_DROP;
 
3864
                                printk(KERN_DEBUG "%s: RX WEP frame with "
 
3865
                                       "unknown keyidx %d (A1=" MAC_FMT " A2="
 
3866
                                       MAC_FMT " A3=" MAC_FMT ")\n",
 
3867
                                       rx->dev->name, keyidx,
 
3868
                                       MAC_ARG(hdr->addr1),
 
3869
                                       MAC_ARG(hdr->addr2),
 
3870
                                       MAC_ARG(hdr->addr3));
 
3871
                                if (!rx->local->apdev)
 
3872
                                        return TXRX_DROP;
 
3873
                                ieee80211_rx_mgmt(
 
3874
                                        rx->local, rx->skb, rx->u.rx.status,
 
3875
                                        ieee80211_msg_wep_frame_unknown_key);
 
3876
                                return TXRX_QUEUED;
 
3877
                        }
 
3878
                }
 
3879
        }
 
3880
 
 
3881
        if (rx->fc & IEEE80211_FCTL_PROTECTED && rx->key && rx->u.rx.ra_match) {
 
3882
                rx->key->tx_rx_count++;
 
3883
                if (unlikely(rx->local->key_tx_rx_threshold &&
 
3884
                             rx->key->tx_rx_count >
 
3885
                             rx->local->key_tx_rx_threshold)) {
 
3886
                        ieee80211_key_threshold_notify(rx->dev, rx->key,
 
3887
                                                       rx->sta);
 
3888
                }
 
3889
        }
 
3890
 
 
3891
        return TXRX_CONTINUE;
 
3892
}
 
3893
 
 
3894
 
 
3895
static ieee80211_txrx_result
 
3896
ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
 
3897
{
 
3898
        struct sta_info *sta = rx->sta;
 
3899
        struct net_device *dev = rx->dev;
 
3900
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
 
3901
 
 
3902
        if (!sta)
 
3903
                return TXRX_CONTINUE;
 
3904
 
 
3905
        /* Update last_rx only for IBSS packets which are for the current
 
3906
         * BSSID to avoid keeping the current IBSS network alive in cases where
 
3907
         * other STAs are using different BSSID. */
 
3908
        if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
 
3909
                u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,IEEE80211_IF_TYPE_IBSS);
 
3910
                if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
 
3911
                        sta->last_rx = jiffies;
 
3912
        } else
 
3913
        if (!is_multicast_ether_addr(hdr->addr1) ||
 
3914
            rx->sdata->type == IEEE80211_IF_TYPE_STA) {
 
3915
                /* Update last_rx only for unicast frames in order to prevent
 
3916
                 * the Probe Request frames (the only broadcast frames from a
 
3917
                 * STA in infrastructure mode) from keeping a connection alive.
 
3918
                 */
 
3919
                sta->last_rx = jiffies;
 
3920
        }
 
3921
 
 
3922
        if (!rx->u.rx.ra_match)
 
3923
                return TXRX_CONTINUE;
 
3924
 
 
3925
        sta->rx_fragments++;
 
3926
        sta->rx_bytes += rx->skb->len;
 
3927
 
 
3928
        /* Low pass filter:  15/16 current avg + new.
 
3929
         * Accumulated values here are 16x values sent from driver. */
 
3930
        sta->accum_rssi = sta->accum_rssi - (sta->accum_rssi >> 4) +
 
3931
                          rx->u.rx.status->ssi;
 
3932
        sta->accum_signal = sta->accum_signal - (sta->accum_signal >> 4) +
 
3933
                          rx->u.rx.status->signal;
 
3934
        sta->accum_noise = sta->accum_noise - (sta->accum_noise >> 4) +
 
3935
                          rx->u.rx.status->noise;
 
3936
 
 
3937
        /* Quantize the averages (divide by 16) */
 
3938
        sta->last_rssi = sta->accum_rssi >> 4;
 
3939
        sta->last_signal = sta->accum_signal >> 4;
 
3940
        sta->last_noise = sta->accum_noise >> 4;
 
3941
 
 
3942
        if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
 
3943
                /* Change STA power saving mode only in the end of a frame
 
3944
                 * exchange sequence */
 
3945
                if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
 
3946
                        rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
 
3947
                else if (!(sta->flags & WLAN_STA_PS) &&
 
3948
                         (rx->fc & IEEE80211_FCTL_PM))
 
3949
                        ap_sta_ps_start(dev, sta);
 
3950
        }
 
3951
 
 
3952
        /* Drop data::nullfunc frames silently, since they are used only to
 
3953
         * control station power saving mode. */
 
3954
        if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
 
3955
            (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
 
3956
                I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
 
3957
                /* Update counter and free packet here to avoid counting this
 
3958
                 * as a dropped packed. */
 
3959
                sta->rx_packets++;
 
3960
                dev_kfree_skb(rx->skb);
 
3961
                return TXRX_QUEUED;
 
3962
        }
 
3963
 
 
3964
        return TXRX_CONTINUE;
 
3965
} /* ieee80211_rx_h_sta_process */
 
3966
 
 
3967
 
 
3968
static ieee80211_txrx_result
 
3969
ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
 
3970
{
 
3971
        if (!rx->sta || !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
 
3972
            (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
 
3973
            !rx->key || rx->key->alg != ALG_WEP || !rx->u.rx.ra_match)
 
3974
                return TXRX_CONTINUE;
 
3975
 
 
3976
        /* Check for weak IVs, if hwaccel did not remove IV from the frame */
 
3977
        if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) ||
 
3978
            rx->key->force_sw_encrypt) {
 
3979
                u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
 
3980
                if (iv) {
 
3981
                        rx->sta->wep_weak_iv_count++;
 
3982
                }
 
3983
        }
 
3984
 
 
3985
        return TXRX_CONTINUE;
 
3986
}
 
3987
 
 
3988
 
 
3989
static ieee80211_txrx_result
 
3990
ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
 
3991
{
 
3992
        /* If the device handles decryption totally, skip this test */
 
3993
        if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
 
3994
                return TXRX_CONTINUE;
 
3995
 
 
3996
        if ((rx->key && rx->key->alg != ALG_WEP) ||
 
3997
            !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
 
3998
            ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
 
3999
             ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
 
4000
              (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)))
 
4001
                return TXRX_CONTINUE;
 
4002
 
 
4003
        if (!rx->key) {
 
4004
                printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
 
4005
                       rx->dev->name);
 
4006
                return TXRX_DROP;
 
4007
        }
 
4008
 
 
4009
        if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
 
4010
            rx->key->force_sw_encrypt) {
 
4011
                if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
 
4012
                        printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
 
4013
                               "failed\n", rx->dev->name);
 
4014
                        return TXRX_DROP;
 
4015
                }
 
4016
        } else if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
 
4017
                ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
 
4018
                /* remove ICV */
 
4019
                skb_trim(rx->skb, rx->skb->len - 4);
 
4020
        }
 
4021
 
 
4022
        return TXRX_CONTINUE;
 
4023
}
 
4024
 
 
4025
 
 
4026
static ieee80211_txrx_result
 
4027
ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
 
4028
{
 
4029
        if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
 
4030
            rx->sdata->type != IEEE80211_IF_TYPE_STA && rx->u.rx.ra_match) {
 
4031
                /* Pass both encrypted and unencrypted EAPOL frames to user
 
4032
                 * space for processing. */
 
4033
                if (!rx->local->apdev)
 
4034
                        return TXRX_DROP;
 
4035
                ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
 
4036
                                  ieee80211_msg_normal);
 
4037
                return TXRX_QUEUED;
 
4038
        }
 
4039
 
 
4040
        if (unlikely(rx->sdata->ieee802_1x &&
 
4041
                     (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
 
4042
                     (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
 
4043
                     (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
 
4044
                     !ieee80211_is_eapol(rx->skb))) {
 
4045
#ifdef CONFIG_MAC80211_DEBUG
 
4046
                struct ieee80211_hdr *hdr =
 
4047
                        (struct ieee80211_hdr *) rx->skb->data;
 
4048
                printk(KERN_DEBUG "%s: dropped frame from " MAC_FMT
 
4049
                       " (unauthorized port)\n", rx->dev->name,
 
4050
                       MAC_ARG(hdr->addr2));
 
4051
#endif /* CONFIG_MAC80211_DEBUG */
 
4052
                return TXRX_DROP;
 
4053
        }
 
4054
 
 
4055
        return TXRX_CONTINUE;
 
4056
}
 
4057
 
 
4058
 
 
4059
static ieee80211_txrx_result
 
4060
ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
 
4061
{
 
4062
        /*  If the device handles decryption totally, skip this test */
 
4063
        if (rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP)
 
4064
                return TXRX_CONTINUE;
 
4065
 
 
4066
        /* Drop unencrypted frames if key is set. */
 
4067
        if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
 
4068
                     (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
 
4069
                     (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
 
4070
                     (rx->key || rx->sdata->drop_unencrypted) &&
 
4071
                     (rx->sdata->eapol == 0 ||
 
4072
                      !ieee80211_is_eapol(rx->skb)))) {
 
4073
                printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
 
4074
                       "encryption\n", rx->dev->name);
 
4075
                return TXRX_DROP;
 
4076
        }
 
4077
        return TXRX_CONTINUE;
 
4078
}
 
4079
 
 
4080
static ieee80211_txrx_result
 
4081
ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
 
4082
{
 
4083
        struct ieee80211_local *local = rx->local;
 
4084
        struct ieee80211_hw *hw = &local->hw;
 
4085
        struct sk_buff *skb = rx->skb;
 
4086
        struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
 
4087
        struct tid_ht_agg_info_rx *tid_rx_info;
 
4088
        u16 start_seq_num;
 
4089
        u16 tid;
 
4090
 
 
4091
        if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
 
4092
                return TXRX_CONTINUE;
 
4093
 
 
4094
        if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
 
4095
                if (!rx->sta)
 
4096
                        return TXRX_CONTINUE;
 
4097
                tid = le16_to_cpu(bar->control) >> 12;
 
4098
                tid_rx_info = &(rx->sta->ht_ba_mlme.tid_agg_info_rx[tid]);
 
4099
                if (tid_rx_info->state != HT_AGG_STATE_OPERATIONAL)
 
4100
                        return TXRX_CONTINUE;
 
4101
 
 
4102
                start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
 
4103
 
 
4104
                /* reset session timer */
 
4105
                if (tid_rx_info->timeout) {
 
4106
                        unsigned long expires = jiffies +
 
4107
                                                                        (tid_rx_info->timeout / 1000) * HZ;
 
4108
                        mod_timer(&tid_rx_info->session_timer,expires);
 
4109
                }
 
4110
 
 
4111
                /* manage reordering buffer according to requested sequence number */
 
4112
                ieee80211_sta_manage_reorder_buf(hw,rx,tid_rx_info,NULL,
 
4113
                                                                                        start_seq_num,1);
 
4114
 
 
4115
                return TXRX_DROP;
 
4116
        }
 
4117
 
 
4118
        return TXRX_CONTINUE;
 
4119
}
 
4120
 
 
4121
static ieee80211_txrx_result
 
4122
ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
 
4123
{
 
4124
        struct ieee80211_sub_if_data *sdata;
 
4125
        struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
 
4126
 
 
4127
        if (!rx->u.rx.ra_match)
 
4128
                return TXRX_DROP;
 
4129
 
 
4130
        sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
 
4131
        if ((sdata->type == IEEE80211_IF_TYPE_STA ||
 
4132
             sdata->type == IEEE80211_IF_TYPE_IBSS) &&
 
4133
            !rx->local->user_space_mlme) {
 
4134
                ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
 
4135
        } else if ((sdata->type == IEEE80211_IF_TYPE_AP) &&
 
4136
                           ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
 
4137
                           ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ACTION) &&
 
4138
                           (mgmt->u.action.category == WLAN_CATEGORY_BACK)){
 
4139
                /* 11n is not implemented yet in hostapd,so process in mac80211 */
 
4140
                ieee80211_rx_mgmt_action(rx->dev,NULL,mgmt,rx->skb->len);
 
4141
        } else {
 
4142
                /* Management frames are sent to hostapd for processing */
 
4143
                if (!rx->local->apdev)
 
4144
                        return TXRX_DROP;
 
4145
                ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
 
4146
                                  ieee80211_msg_normal);
 
4147
        }
 
4148
        return TXRX_QUEUED;
 
4149
}
 
4150
 
 
4151
 
 
4152
static ieee80211_txrx_result
 
4153
ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
 
4154
{
 
4155
        struct ieee80211_local *local = rx->local;
 
4156
        struct sk_buff *skb = rx->skb;
 
4157
 
 
4158
        if (unlikely(local->sta_hw_scanning != 0))
 
4159
                return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
 
4160
 
 
4161
        if (unlikely(local->sta_sw_scanning != 0)) {
 
4162
                /* drop all the other packets during a software scan anyway */
 
4163
                if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
 
4164
                    != TXRX_QUEUED)
 
4165
                        dev_kfree_skb(skb);
 
4166
                return TXRX_QUEUED;
 
4167
        }
 
4168
 
 
4169
        if (unlikely(rx->u.rx.in_scan)) {
 
4170
                /* scanning finished during invoking of handlers */
 
4171
                I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
 
4172
                return TXRX_DROP;
 
4173
        }
 
4174
 
 
4175
        return TXRX_CONTINUE;
 
4176
}
 
4177
 
 
4178
 
 
4179
static void ieee80211_rx_michael_mic_report(struct net_device *dev,
 
4180
                                            struct ieee80211_hdr *hdr,
 
4181
                                            struct sta_info *sta,
 
4182
                                            struct ieee80211_txrx_data *rx)
 
4183
{
 
4184
        int keyidx, hdrlen;
 
4185
 
 
4186
        hdrlen = iwlwifi_iwlwifi_ieee80211_get_hdrlen_from_skb(rx->skb);
 
4187
        if (rx->skb->len >= hdrlen + 4)
 
4188
                keyidx = rx->skb->data[hdrlen + 3] >> 6;
 
4189
        else
 
4190
                keyidx = -1;
 
4191
 
 
4192
        /* TODO: verify that this is not triggered by fragmented
 
4193
         * frames (hw does not verify MIC for them). */
 
4194
        printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
 
4195
               "failure from " MAC_FMT " to " MAC_FMT " keyidx=%d\n",
 
4196
               dev->name, MAC_ARG(hdr->addr2), MAC_ARG(hdr->addr1), keyidx);
 
4197
 
 
4198
        if (!sta) {
 
4199
                /* Some hardware versions seem to generate incorrect
 
4200
                 * Michael MIC reports; ignore them to avoid triggering
 
4201
                 * countermeasures. */
 
4202
                printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
 
4203
                       "error for unknown address " MAC_FMT "\n",
 
4204
                       dev->name, MAC_ARG(hdr->addr2));
 
4205
                goto ignore;
 
4206
        }
 
4207
 
 
4208
        if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
 
4209
                printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
 
4210
                       "error for a frame with no ISWEP flag (src "
 
4211
                       MAC_FMT ")\n", dev->name, MAC_ARG(hdr->addr2));
 
4212
                goto ignore;
 
4213
        }
 
4214
 
 
4215
        if ((rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
 
4216
            rx->sdata->type == IEEE80211_IF_TYPE_AP) {
 
4217
                keyidx = ieee80211_wep_get_keyidx(rx->skb);
 
4218
                /* AP with Pairwise keys support should never receive Michael
 
4219
                 * MIC errors for non-zero keyidx because these are reserved
 
4220
                 * for group keys and only the AP is sending real multicast
 
4221
                 * frames in BSS. */
 
4222
                if (keyidx) {
 
4223
                        printk(KERN_DEBUG "%s: ignored Michael MIC error for "
 
4224
                               "a frame with non-zero keyidx (%d) (src " MAC_FMT
 
4225
                               ")\n", dev->name, keyidx, MAC_ARG(hdr->addr2));
 
4226
                        goto ignore;
 
4227
                }
 
4228
        }
 
4229
 
 
4230
        if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
 
4231
            ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
 
4232
             (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
 
4233
                printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
 
4234
                       "error for a frame that cannot be encrypted "
 
4235
                       "(fc=0x%04x) (src " MAC_FMT ")\n",
 
4236
                       dev->name, rx->fc, MAC_ARG(hdr->addr2));
 
4237
                goto ignore;
 
4238
        }
 
4239
 
 
4240
        do {
 
4241
                union iwreq_data wrqu;
 
4242
                char *buf = kmalloc(128, GFP_ATOMIC);
 
4243
                if (!buf)
 
4244
                        break;
 
4245
 
 
4246
                /* TODO: needed parameters: count, key type, TSC */
 
4247
                sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
 
4248
                        "keyid=%d %scast addr=" MAC_FMT ")",
 
4249
                        keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
 
4250
                        MAC_ARG(hdr->addr2));
 
4251
                memset(&wrqu, 0, sizeof(wrqu));
 
4252
                wrqu.data.length = strlen(buf);
 
4253
                wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
 
4254
                kfree(buf);
 
4255
        } while (0);
 
4256
 
 
4257
        /* TODO: consider verifying the MIC error report with software
 
4258
         * implementation if we get too many spurious reports from the
 
4259
         * hardware. */
 
4260
        if (!rx->local->apdev)
 
4261
                goto ignore;
 
4262
        ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
 
4263
                          ieee80211_msg_michael_mic_failure);
 
4264
        return;
 
4265
 
 
4266
 ignore:
 
4267
        dev_kfree_skb(rx->skb);
 
4268
        rx->skb = NULL;
 
4269
}
 
4270
 
 
4271
static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
 
4272
                                struct ieee80211_local *local,
 
4273
                                ieee80211_rx_handler *handlers,
 
4274
                                struct ieee80211_txrx_data *rx,
 
4275
                                struct sta_info *sta)
 
4276
{
 
4277
        ieee80211_rx_handler *handler;
 
4278
        ieee80211_txrx_result res = TXRX_DROP;
 
4279
 
 
4280
        for (handler = handlers; *handler != NULL; handler++) {
 
4281
                res = (*handler)(rx);
 
4282
                if (res != TXRX_CONTINUE) {
 
4283
                        if (res == TXRX_DROP) {
 
4284
                                I802_DEBUG_INC(local->rx_handlers_drop);
 
4285
                                if (sta)
 
4286
                                        sta->rx_dropped++;
 
4287
                        }
 
4288
                        if (res == TXRX_QUEUED)
 
4289
                                I802_DEBUG_INC(local->rx_handlers_queued);
 
4290
                        break;
 
4291
                }
 
4292
        }
 
4293
 
 
4294
        if (res == TXRX_DROP) {
 
4295
                dev_kfree_skb(rx->skb);
 
4296
        }
 
4297
        return res;
 
4298
}
 
4299
 
 
4300
static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
 
4301
                                                ieee80211_rx_handler *handlers,
 
4302
                                                struct ieee80211_txrx_data *rx,
 
4303
                                                struct sta_info *sta)
 
4304
{
 
4305
        if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
 
4306
            TXRX_CONTINUE)
 
4307
                dev_kfree_skb(rx->skb);
 
4308
}
 
4309
 
 
4310
/*
 
4311
 * This is the receive path handler. It is called by a low level driver when an
 
4312
 * 802.11 MPDU is received from the hardware.
 
4313
 */
 
4314
void iwlwifi___ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
 
4315
                    struct ieee80211_rx_status *status)
 
4316
{
 
4317
        struct ieee80211_local *local = hw_to_local(hw);
 
4318
        struct ieee80211_sub_if_data *sdata;
 
4319
        struct sta_info *sta;
 
4320
        struct ieee80211_hdr *hdr;
 
4321
        struct ieee80211_txrx_data rx;
 
4322
        u16 type;
 
4323
        int multicast;
 
4324
        int radiotap_len = 0;
 
4325
 
 
4326
        if (status->flag & RX_FLAG_RADIOTAP) {
 
4327
                radiotap_len = ieee80211_get_radiotap_len(skb);
 
4328
                skb_pull(skb, radiotap_len);
 
4329
        }
 
4330
 
 
4331
        hdr = (struct ieee80211_hdr *) skb->data;
 
4332
        memset(&rx, 0, sizeof(rx));
 
4333
        rx.skb = skb;
 
4334
        rx.local = local;
 
4335
 
 
4336
        rx.u.rx.status = status;
 
4337
        rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
 
4338
        type = rx.fc & IEEE80211_FCTL_FTYPE;
 
4339
        if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
 
4340
                local->dot11ReceivedFragmentCount++;
 
4341
        multicast = is_multicast_ether_addr(hdr->addr1);
 
4342
 
 
4343
        if (skb->len >= 16)
 
4344
                sta = rx.sta = iwlwifi_sta_info_get(local, hdr->addr2);
 
4345
        else
 
4346
                sta = rx.sta = NULL;
 
4347
 
 
4348
        if (sta) {
 
4349
                rx.dev = sta->dev;
 
4350
                rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
 
4351
        }
 
4352
 
 
4353
        if ((status->flag & RX_FLAG_MMIC_ERROR)) {
 
4354
                ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
 
4355
                goto end;
 
4356
        }
 
4357
 
 
4358
        if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
 
4359
                rx.u.rx.in_scan = 1;
 
4360
 
 
4361
        if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
 
4362
                                           sta) != TXRX_CONTINUE)
 
4363
                goto end;
 
4364
        skb = rx.skb;
 
4365
 
 
4366
        skb_push(skb, radiotap_len);
 
4367
        if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS) &&
 
4368
            !local->iff_promiscs && !multicast) {
 
4369
                rx.u.rx.ra_match = 1;
 
4370
                ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
 
4371
                                             sta);
 
4372
        } else {
 
4373
                struct ieee80211_sub_if_data *prev = NULL;
 
4374
                struct sk_buff *skb_new;
 
4375
                u8 *bssid;
 
4376
 
 
4377
                read_lock(&local->sub_if_lock);
 
4378
                list_for_each_entry(sdata, &local->sub_if_list, list) {
 
4379
                        bssid = ieee80211_get_bssid(hdr,
 
4380
                                                    skb->len - radiotap_len,
 
4381
                                                    sdata->type);
 
4382
                        rx.u.rx.ra_match = 1;
 
4383
                        switch (sdata->type) {
 
4384
                        case IEEE80211_IF_TYPE_STA:
 
4385
                                if (!bssid)
 
4386
                                        continue;
 
4387
                                if (!ieee80211_bssid_match(bssid,
 
4388
                                                        sdata->u.sta.bssid)) {
 
4389
                                        if (!rx.u.rx.in_scan)
 
4390
                                                continue;
 
4391
                                        rx.u.rx.ra_match = 0;
 
4392
                                } else if (!multicast &&
 
4393
                                           compare_ether_addr(sdata->dev->dev_addr,
 
4394
                                                              hdr->addr1) != 0) {
 
4395
                                        if (!sdata->promisc)
 
4396
                                                continue;
 
4397
                                        rx.u.rx.ra_match = 0;
 
4398
                                }
 
4399
                                break;
 
4400
                        case IEEE80211_IF_TYPE_IBSS:
 
4401
                                if (!bssid)
 
4402
                                        continue;
 
4403
                                if (!ieee80211_bssid_match(bssid,
 
4404
                                                        sdata->u.sta.bssid)) {
 
4405
                                        if (!rx.u.rx.in_scan)
 
4406
                                                continue;
 
4407
                                        rx.u.rx.ra_match = 0;
 
4408
                                } else if (!multicast &&
 
4409
                                           compare_ether_addr(sdata->dev->dev_addr,
 
4410
                                                              hdr->addr1) != 0) {
 
4411
                                        if (!sdata->promisc)
 
4412
                                                continue;
 
4413
                                        rx.u.rx.ra_match = 0;
 
4414
                                } else if (!sta)
 
4415
                                        sta = rx.sta =
 
4416
                                                ieee80211_ibss_add_sta(sdata->dev,
 
4417
                                                                       skb, bssid,
 
4418
                                                                       hdr->addr2);
 
4419
                                break;
 
4420
                        case IEEE80211_IF_TYPE_AP:
 
4421
                                if (!bssid) {
 
4422
                                        if (compare_ether_addr(sdata->dev->dev_addr,
 
4423
                                                               hdr->addr1) != 0)
 
4424
                                                continue;
 
4425
                                } else if (!ieee80211_bssid_match(bssid,
 
4426
                                                        sdata->dev->dev_addr)) {
 
4427
                                        if (!rx.u.rx.in_scan)
 
4428
                                                continue;
 
4429
                                        rx.u.rx.ra_match = 0;
 
4430
                                }
 
4431
                                if (sdata->dev == local->mdev &&
 
4432
                                    !rx.u.rx.in_scan)
 
4433
                                        /* do not receive anything via
 
4434
                                         * master device when not scanning */
 
4435
                                        continue;
 
4436
                                break;
 
4437
                        case IEEE80211_IF_TYPE_WDS:
 
4438
                                if (bssid ||
 
4439
                                    (rx.fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
 
4440
                                        continue;
 
4441
                                if (compare_ether_addr(sdata->u.wds.remote_addr,
 
4442
                                                       hdr->addr2) != 0)
 
4443
                                        continue;
 
4444
                                break;
 
4445
                        }
 
4446
 
 
4447
                        if (prev) {
 
4448
                                skb_new = skb_copy(skb, GFP_ATOMIC);
 
4449
                                if (!skb_new) {
 
4450
                                        if (net_ratelimit())
 
4451
                                                printk(KERN_DEBUG "%s: failed to copy "
 
4452
                                                       "multicast frame for %s",
 
4453
                                                       local->mdev->name, prev->dev->name);
 
4454
                                        continue;
 
4455
                                }
 
4456
                                rx.skb = skb_new;
 
4457
                                rx.dev = prev->dev;
 
4458
                                rx.sdata = prev;
 
4459
                                ieee80211_invoke_rx_handlers(local,
 
4460
                                                             local->rx_handlers,
 
4461
                                                             &rx, sta);
 
4462
                        }
 
4463
                        prev = sdata;
 
4464
                }
 
4465
                if (prev) {
 
4466
                        rx.skb = skb;
 
4467
                        rx.dev = prev->dev;
 
4468
                        rx.sdata = prev;
 
4469
                        ieee80211_invoke_rx_handlers(local, local->rx_handlers,
 
4470
                                                     &rx, sta);
 
4471
                } else
 
4472
                        dev_kfree_skb(skb);
 
4473
                read_unlock(&local->sub_if_lock);
 
4474
        }
 
4475
 
 
4476
  end:
 
4477
        if (sta)
 
4478
                iwlwifi_sta_info_put(sta);
 
4479
}
 
4480
EXPORT_SYMBOL(iwlwifi___ieee80211_rx);
 
4481
 
 
4482
static ieee80211_txrx_result
 
4483
ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
 
4484
{
 
4485
        struct ieee80211_local *local = tx->local;
 
4486
        struct ieee80211_hw_mode *mode = tx->u.tx.mode;
 
4487
        struct sk_buff *skb = tx->skb;
 
4488
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
4489
        u32 load = 0, hdrtime;
 
4490
 
 
4491
        /* TODO: this could be part of tx_status handling, so that the number
 
4492
         * of retries would be known; TX rate should in that case be stored
 
4493
         * somewhere with the packet */
 
4494
 
 
4495
        /* Estimate total channel use caused by this frame */
 
4496
 
 
4497
        /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
 
4498
         * 1 usec = 1/8 * (1080 / 10) = 13.5 */
 
4499
 
 
4500
        if (mode->mode == MODE_IEEE80211A ||
 
4501
            mode->mode == MODE_ATHEROS_TURBO ||
 
4502
            mode->mode == MODE_ATHEROS_TURBOG ||
 
4503
            (mode->mode == MODE_IEEE80211G &&
 
4504
             tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
 
4505
                hdrtime = CHAN_UTIL_HDR_SHORT;
 
4506
        else
 
4507
                hdrtime = CHAN_UTIL_HDR_LONG;
 
4508
 
 
4509
        load = hdrtime;
 
4510
        if (!is_multicast_ether_addr(hdr->addr1))
 
4511
                load += hdrtime;
 
4512
 
 
4513
        if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
 
4514
                load += 2 * hdrtime;
 
4515
        else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
 
4516
                load += hdrtime;
 
4517
 
 
4518
        load += skb->len * tx->u.tx.rate->rate_inv;
 
4519
 
 
4520
        if (tx->u.tx.extra_frag) {
 
4521
                int i;
 
4522
                for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
 
4523
                        load += 2 * hdrtime;
 
4524
                        load += tx->u.tx.extra_frag[i]->len *
 
4525
                                tx->u.tx.rate->rate;
 
4526
                }
 
4527
        }
 
4528
 
 
4529
        /* Divide channel_use by 8 to avoid wrapping around the counter */
 
4530
        load >>= CHAN_UTIL_SHIFT;
 
4531
        local->channel_use_raw += load;
 
4532
        if (tx->sta)
 
4533
                tx->sta->channel_use_raw += load;
 
4534
        tx->sdata->channel_use_raw += load;
 
4535
 
 
4536
        return TXRX_CONTINUE;
 
4537
}
 
4538
 
 
4539
 
 
4540
static ieee80211_txrx_result
 
4541
ieee80211_rx_h_reorder_ampdu(struct ieee80211_txrx_data *rx)
 
4542
{
 
4543
        struct ieee80211_local *local = rx->local;
 
4544
        struct ieee80211_hw *hw = &local->hw;
 
4545
        struct sk_buff *skb = rx->skb;
 
4546
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
4547
        struct tid_ht_agg_info_rx *tid_rx_info;
 
4548
        u16 sc;
 
4549
        u16 mpdu_seq_num;
 
4550
 
 
4551
        if (!rx->sta)
 
4552
                return TXRX_CONTINUE;
 
4553
 
 
4554
        tid_rx_info = &(rx->sta->ht_ba_mlme.tid_agg_info_rx[rx->u.rx.queue]);
 
4555
 
 
4556
        /* filter the QoS data rx stream according to STA/TID
 
4557
         * and check if this STA/TID is on aggregation */
 
4558
        if ((tid_rx_info->state != HT_AGG_STATE_OPERATIONAL)
 
4559
                || ((rx->fc & 0x00CC) != (IEEE80211_STYPE_QOS_DATA |
 
4560
                                                                        IEEE80211_FTYPE_DATA)))
 
4561
                return TXRX_CONTINUE;
 
4562
 
 
4563
        /* check if this frame has already been queued and ordered */
 
4564
        if(rx->u.rx.status->ordered == TXRX_QUEUED)
 
4565
                return TXRX_CONTINUE;
 
4566
 
 
4567
        /* check if this frame has already been queued and ordered but
 
4568
         * should be deleted */
 
4569
        if(rx->u.rx.status->ordered == TXRX_DROP) {
 
4570
                if(skb)
 
4571
                        dev_kfree_skb(skb);
 
4572
                return TXRX_DROP;
 
4573
        }
 
4574
 
 
4575
        /* new un-ordered ampdu frame - process it */
 
4576
 
 
4577
        /* reset session timer */
 
4578
        if (tid_rx_info->timeout) {
 
4579
                unsigned long expires = jiffies + (tid_rx_info->timeout / 1000) * HZ;
 
4580
                mod_timer(&tid_rx_info->session_timer,expires);
 
4581
        }
 
4582
 
 
4583
        /* if this mpdu is fragmented - terminate rx aggregation session */
 
4584
        sc = le16_to_cpu(hdr->seq_ctrl);
 
4585
        if (sc & IEEE80211_SCTL_FRAG) {
 
4586
                ieee80211_sta_stop_rx_BA_session(rx->dev,rx->sta->addr,rx->u.rx.queue,
 
4587
                                                                                 0,WLAN_REASON_QSTA_REQUIRE_SETUP);
 
4588
                return TXRX_CONTINUE;
 
4589
        }
 
4590
 
 
4591
        /* according to mpdu sequence number deal with reordering buffer */
 
4592
        mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
 
4593
        return ieee80211_sta_manage_reorder_buf(hw,rx,tid_rx_info,skb,
 
4594
                                                                                        mpdu_seq_num,0);
 
4595
}
 
4596
 
 
4597
 
 
4598
static ieee80211_txrx_result
 
4599
ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
 
4600
{
 
4601
        struct ieee80211_local *local = rx->local;
 
4602
        struct sk_buff *skb = rx->skb;
 
4603
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
4604
        u32 load = 0, hdrtime;
 
4605
        struct ieee80211_rate *rate;
 
4606
        struct ieee80211_hw_mode *mode = local->hw.conf.mode;
 
4607
        int i;
 
4608
 
 
4609
        /* Estimate total channel use caused by this frame */
 
4610
 
 
4611
        if (unlikely(mode->num_rates < 0) || (rx->u.rx.status->ordered != 0))
 
4612
                return TXRX_CONTINUE;
 
4613
 
 
4614
        rate = &mode->rates[0];
 
4615
        for (i = 0; i < mode->num_rates; i++) {
 
4616
                if (mode->rates[i].val == rx->u.rx.status->rate) {
 
4617
                        rate = &mode->rates[i];
 
4618
                        break;
 
4619
                }
 
4620
        }
 
4621
 
 
4622
        /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
 
4623
         * 1 usec = 1/8 * (1080 / 10) = 13.5 */
 
4624
 
 
4625
        if (mode->mode == MODE_IEEE80211A ||
 
4626
            mode->mode == MODE_ATHEROS_TURBO ||
 
4627
            mode->mode == MODE_ATHEROS_TURBOG ||
 
4628
            (mode->mode == MODE_IEEE80211G &&
 
4629
             rate->flags & IEEE80211_RATE_ERP))
 
4630
                hdrtime = CHAN_UTIL_HDR_SHORT;
 
4631
        else
 
4632
                hdrtime = CHAN_UTIL_HDR_LONG;
 
4633
 
 
4634
        load = hdrtime;
 
4635
        if (!is_multicast_ether_addr(hdr->addr1))
 
4636
                load += hdrtime;
 
4637
 
 
4638
        load += skb->len * rate->rate_inv;
 
4639
 
 
4640
        /* Divide channel_use by 8 to avoid wrapping around the counter */
 
4641
        load >>= CHAN_UTIL_SHIFT;
 
4642
        local->channel_use_raw += load;
 
4643
        if (rx->sta)
 
4644
                rx->sta->channel_use_raw += load;
 
4645
        rx->u.rx.load = load;
 
4646
 
 
4647
        return TXRX_CONTINUE;
 
4648
}
 
4649
 
 
4650
static ieee80211_txrx_result
 
4651
ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
 
4652
{
 
4653
        rx->sdata->channel_use_raw += rx->u.rx.load;
 
4654
        return TXRX_CONTINUE;
 
4655
}
 
4656
 
 
4657
static void ieee80211_stat_refresh(unsigned long data)
 
4658
{
 
4659
        struct ieee80211_local *local = (struct ieee80211_local *) data;
 
4660
        struct sta_info *sta;
 
4661
        struct ieee80211_sub_if_data *sdata;
 
4662
 
 
4663
        if (!local->stat_time)
 
4664
                return;
 
4665
 
 
4666
        /* go through all stations */
 
4667
        spin_lock_bh(&local->sta_lock);
 
4668
        list_for_each_entry(sta, &local->sta_list, list) {
 
4669
                sta->channel_use = (sta->channel_use_raw / local->stat_time) /
 
4670
                        CHAN_UTIL_PER_10MS;
 
4671
                sta->channel_use_raw = 0;
 
4672
        }
 
4673
        spin_unlock_bh(&local->sta_lock);
 
4674
 
 
4675
        /* go through all subinterfaces */
 
4676
        read_lock(&local->sub_if_lock);
 
4677
        list_for_each_entry(sdata, &local->sub_if_list, list) {
 
4678
                sdata->channel_use = (sdata->channel_use_raw /
 
4679
                                      local->stat_time) / CHAN_UTIL_PER_10MS;
 
4680
                sdata->channel_use_raw = 0;
 
4681
        }
 
4682
        read_unlock(&local->sub_if_lock);
 
4683
 
 
4684
        /* hardware interface */
 
4685
        local->channel_use = (local->channel_use_raw /
 
4686
                              local->stat_time) / CHAN_UTIL_PER_10MS;
 
4687
        local->channel_use_raw = 0;
 
4688
 
 
4689
        local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
 
4690
        add_timer(&local->stat_timer);
 
4691
}
 
4692
 
 
4693
 
 
4694
/* This is a version of the rx handler that can be called from hard irq
 
4695
 * context. Post the skb on the queue and schedule the tasklet */
 
4696
void iwlwifi_ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
 
4697
                          struct ieee80211_rx_status *status)
 
4698
{
 
4699
        struct ieee80211_local *local = hw_to_local(hw);
 
4700
 
 
4701
        BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
 
4702
 
 
4703
        skb->dev = local->mdev;
 
4704
        /* copy status into skb->cb for use by tasklet */
 
4705
        memcpy(skb->cb, status, sizeof(*status));
 
4706
        skb->pkt_type = IEEE80211_RX_MSG;
 
4707
        skb_queue_tail(&local->skb_queue, skb);
 
4708
        tasklet_schedule(&local->tasklet);
 
4709
}
 
4710
EXPORT_SYMBOL(iwlwifi_ieee80211_rx_irqsafe);
 
4711
 
 
4712
void iwlwifi_iwlwifi_ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
 
4713
                                 struct sk_buff *skb,
 
4714
                                 struct iwlwifi_ieee80211_tx_status *status)
 
4715
{
 
4716
        struct ieee80211_local *local = hw_to_local(hw);
 
4717
        struct iwlwifi_ieee80211_tx_status *saved;
 
4718
        int tmp;
 
4719
 
 
4720
        skb->dev = local->mdev;
 
4721
        saved = kmalloc(sizeof(struct iwlwifi_ieee80211_tx_status), GFP_ATOMIC);
 
4722
        if (unlikely(!saved)) {
 
4723
                if (net_ratelimit())
 
4724
                        printk(KERN_WARNING "%s: Not enough memory, "
 
4725
                               "dropping tx status", skb->dev->name);
 
4726
                /* should be dev_kfree_skb_irq, but due to this function being
 
4727
                 * named _irqsafe instead of just _irq we can't be sure that
 
4728
                 * people won't call it from non-irq contexts */
 
4729
                dev_kfree_skb_any(skb);
 
4730
                return;
 
4731
        }
 
4732
        memcpy(saved, status, sizeof(struct iwlwifi_ieee80211_tx_status));
 
4733
        /* copy pointer to saved status into skb->cb for use by tasklet */
 
4734
        memcpy(skb->cb, &saved, sizeof(saved));
 
4735
 
 
4736
        skb->pkt_type = IEEE80211_TX_STATUS_MSG;
 
4737
        skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
 
4738
                       &local->skb_queue : &local->skb_queue_unreliable, skb);
 
4739
        tmp = skb_queue_len(&local->skb_queue) +
 
4740
                skb_queue_len(&local->skb_queue_unreliable);
 
4741
        while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
 
4742
               (skb = skb_dequeue(&local->skb_queue_unreliable))) {
 
4743
                memcpy(&saved, skb->cb, sizeof(saved));
 
4744
                kfree(saved);
 
4745
                dev_kfree_skb_irq(skb);
 
4746
                tmp--;
 
4747
                I802_DEBUG_INC(local->tx_status_drop);
 
4748
        }
 
4749
        tasklet_schedule(&local->tasklet);
 
4750
}
 
4751
EXPORT_SYMBOL(iwlwifi_iwlwifi_ieee80211_tx_status_irqsafe);
 
4752
 
 
4753
static void ieee80211_tasklet_handler(unsigned long data)
 
4754
{
 
4755
        struct ieee80211_local *local = (struct ieee80211_local *) data;
 
4756
        struct sk_buff *skb;
 
4757
        struct ieee80211_rx_status rx_status;
 
4758
        struct iwlwifi_ieee80211_tx_status *tx_status;
 
4759
 
 
4760
        while ((skb = skb_dequeue(&local->skb_queue)) ||
 
4761
               (skb = skb_dequeue(&local->skb_queue_unreliable))) {
 
4762
                switch (skb->pkt_type) {
 
4763
                case IEEE80211_RX_MSG:
 
4764
                        /* status is in skb->cb */
 
4765
                        memcpy(&rx_status, skb->cb, sizeof(rx_status));
 
4766
                        /* Clear skb->type in order to not confuse kernel
 
4767
                         * netstack. */
 
4768
                        skb->pkt_type = 0;
 
4769
                        iwlwifi___ieee80211_rx(local_to_hw(local), skb, &rx_status);
 
4770
                        break;
 
4771
                case IEEE80211_TX_STATUS_MSG:
 
4772
                        /* get pointer to saved status out of skb->cb */
 
4773
                        memcpy(&tx_status, skb->cb, sizeof(tx_status));
 
4774
                        skb->pkt_type = 0;
 
4775
                        iwlwifi_ieee80211_tx_status(local_to_hw(local),
 
4776
                                            skb, tx_status);
 
4777
                        kfree(tx_status);
 
4778
                        break;
 
4779
                default: /* should never get here! */
 
4780
                        printk(KERN_ERR "%s: Unknown message type (%d)\n",
 
4781
                               local->mdev->name, skb->pkt_type);
 
4782
                        dev_kfree_skb(skb);
 
4783
                        break;
 
4784
                }
 
4785
        }
 
4786
}
 
4787
 
 
4788
 
 
4789
/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
 
4790
 * make a prepared TX frame (one that has been given to hw) to look like brand
 
4791
 * new IEEE 802.11 frame that is ready to go through TX processing again.
 
4792
 * Also, tx_packet_data in cb is restored from tx_control. */
 
4793
static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
 
4794
                                      struct ieee80211_key *key,
 
4795
                                      struct sk_buff *skb,
 
4796
                                      struct ieee80211_tx_control *control)
 
4797
{
 
4798
        int hdrlen, iv_len, mic_len;
 
4799
        struct ieee80211_tx_packet_data *pkt_data;
 
4800
 
 
4801
        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
 
4802
        pkt_data->ifindex = control->ifindex;
 
4803
        pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
 
4804
        pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
 
4805
        pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
 
4806
        pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
 
4807
        pkt_data->queue = control->queue;
 
4808
 
 
4809
        hdrlen = iwlwifi_iwlwifi_ieee80211_get_hdrlen_from_skb(skb);
 
4810
 
 
4811
        if (!key)
 
4812
                goto no_key;
 
4813
 
 
4814
        switch (key->alg) {
 
4815
        case ALG_WEP:
 
4816
                iv_len = WEP_IV_LEN;
 
4817
                mic_len = WEP_ICV_LEN;
 
4818
                break;
 
4819
        case ALG_TKIP:
 
4820
                iv_len = TKIP_IV_LEN;
 
4821
                mic_len = TKIP_ICV_LEN;
 
4822
                break;
 
4823
        case ALG_CCMP:
 
4824
                iv_len = CCMP_HDR_LEN;
 
4825
                mic_len = CCMP_MIC_LEN;
 
4826
                break;
 
4827
        default:
 
4828
                goto no_key;
 
4829
        }
 
4830
 
 
4831
        if (skb->len >= mic_len && key->force_sw_encrypt)
 
4832
                skb_trim(skb, skb->len - mic_len);
 
4833
        if (skb->len >= iv_len && skb->len > hdrlen) {
 
4834
                memmove(skb->data + iv_len, skb->data, hdrlen);
 
4835
                skb_pull(skb, iv_len);
 
4836
        }
 
4837
 
 
4838
no_key:
 
4839
        {
 
4840
                struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
4841
                u16 fc = le16_to_cpu(hdr->frame_control);
 
4842
                if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
 
4843
                        fc &= ~IEEE80211_STYPE_QOS_DATA;
 
4844
                        hdr->frame_control = cpu_to_le16(fc);
 
4845
                        memmove(skb->data + 2, skb->data, hdrlen - 2);
 
4846
                        skb_pull(skb, 2);
 
4847
                }
 
4848
        }
 
4849
}
 
4850
 
 
4851
 
 
4852
void iwlwifi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
 
4853
                         struct iwlwifi_ieee80211_tx_status *status)
 
4854
{
 
4855
        struct sk_buff *skb2;
 
4856
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
 
4857
        struct ieee80211_local *local = hw_to_local(hw);
 
4858
        u16 frag, type;
 
4859
        u32 msg_type;
 
4860
        struct iwlwifi_ieee80211_tx_status_rtap_hdr *rthdr;
 
4861
        struct ieee80211_sub_if_data *sdata;
 
4862
        int monitors;
 
4863
 
 
4864
        if (!status) {
 
4865
                printk(KERN_ERR
 
4866
                       "%s: iwlwifi_ieee80211_tx_status called with NULL status\n",
 
4867
                       local->mdev->name);
 
4868
                dev_kfree_skb(skb);
 
4869
                return;
 
4870
        }
 
4871
 
 
4872
        if (status->excessive_retries) {
 
4873
                struct sta_info *sta;
 
4874
                sta = iwlwifi_sta_info_get(local, hdr->addr1);
 
4875
                if (sta) {
 
4876
                        if (sta->flags & WLAN_STA_PS) {
 
4877
                                /* The STA is in power save mode, so assume
 
4878
                                 * that this TX packet failed because of that.
 
4879
                                 */
 
4880
                                status->excessive_retries = 0;
 
4881
                                status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
 
4882
                        }
 
4883
                        iwlwifi_sta_info_put(sta);
 
4884
                }
 
4885
        }
 
4886
 
 
4887
        if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
 
4888
                struct sta_info *sta;
 
4889
                sta = iwlwifi_sta_info_get(local, hdr->addr1);
 
4890
                if (sta) {
 
4891
                        sta->tx_filtered_count++;
 
4892
 
 
4893
                        /* Clear the TX filter mask for this STA when sending
 
4894
                         * the next packet. If the STA went to power save mode,
 
4895
                         * this will happen when it is waking up for the next
 
4896
                         * time. */
 
4897
                        sta->clear_dst_mask = 1;
 
4898
 
 
4899
                        /* TODO: Is the WLAN_STA_PS flag always set here or is
 
4900
                         * the race between RX and TX status causing some
 
4901
                         * packets to be filtered out before 80211.o gets an
 
4902
                         * update for PS status? This seems to be the case, so
 
4903
                         * no changes are likely to be needed. */
 
4904
                        if (sta->flags & WLAN_STA_PS &&
 
4905
                            skb_queue_len(&sta->tx_filtered) <
 
4906
                            STA_MAX_TX_BUFFER) {
 
4907
                                ieee80211_remove_tx_extra(local, sta->key,
 
4908
                                                          skb,
 
4909
                                                          &status->control);
 
4910
                                skb_queue_tail(&sta->tx_filtered, skb);
 
4911
                        } else if (!(sta->flags & WLAN_STA_PS) &&
 
4912
                                   !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
 
4913
                                /* Software retry the packet once */
 
4914
                                status->control.flags |= IEEE80211_TXCTL_REQUEUE;
 
4915
                                ieee80211_remove_tx_extra(local, sta->key,
 
4916
                                                          skb,
 
4917
                                                          &status->control);
 
4918
                                dev_queue_xmit(skb);
 
4919
                        } else {
 
4920
                                if (net_ratelimit()) {
 
4921
                                        printk(KERN_DEBUG "%s: dropped TX "
 
4922
                                               "filtered frame queue_len=%d "
 
4923
                                               "PS=%d @%lu\n",
 
4924
                                               local->mdev->name,
 
4925
                                               skb_queue_len(
 
4926
                                                       &sta->tx_filtered),
 
4927
                                               !!(sta->flags & WLAN_STA_PS),
 
4928
                                               jiffies);
 
4929
                                }
 
4930
                                dev_kfree_skb(skb);
 
4931
                        }
 
4932
                        iwlwifi_sta_info_put(sta);
 
4933
                        return;
 
4934
                }
 
4935
        } else {
 
4936
                /* FIXME: STUPID to call this with both local and local->mdev */
 
4937
                rate_control_tx_status(local, local->mdev, skb, status);
 
4938
        }
 
4939
 
 
4940
        ieee80211_led_tx(local, 0);
 
4941
 
 
4942
        /* SNMP counters
 
4943
         * Fragments are passed to low-level drivers as separate skbs, so these
 
4944
         * are actually fragments, not frames. Update frame counters only for
 
4945
         * the first fragment of the frame. */
 
4946
 
 
4947
        frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
 
4948
        type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
 
4949
 
 
4950
        if (status->flags & IEEE80211_TX_STATUS_ACK) {
 
4951
                if (frag == 0) {
 
4952
                        local->dot11TransmittedFrameCount++;
 
4953
                        if (is_multicast_ether_addr(hdr->addr1))
 
4954
                                local->dot11MulticastTransmittedFrameCount++;
 
4955
                        if (status->retry_count > 0)
 
4956
                                local->dot11RetryCount++;
 
4957
                        if (status->retry_count > 1)
 
4958
                                local->dot11MultipleRetryCount++;
 
4959
                }
 
4960
 
 
4961
                /* This counter shall be incremented for an acknowledged MPDU
 
4962
                 * with an individual address in the address 1 field or an MPDU
 
4963
                 * with a multicast address in the address 1 field of type Data
 
4964
                 * or Management. */
 
4965
                if (!is_multicast_ether_addr(hdr->addr1) ||
 
4966
                    type == IEEE80211_FTYPE_DATA ||
 
4967
                    type == IEEE80211_FTYPE_MGMT)
 
4968
                        local->dot11TransmittedFragmentCount++;
 
4969
        } else {
 
4970
                if (frag == 0)
 
4971
                        local->dot11FailedCount++;
 
4972
        }
 
4973
 
 
4974
        msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
 
4975
                ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
 
4976
 
 
4977
        /* this was a transmitted frame, but now we want to reuse it */
 
4978
        skb_orphan(skb);
 
4979
 
 
4980
        if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
 
4981
            local->apdev) {
 
4982
                if (local->monitors) {
 
4983
                        skb2 = skb_clone(skb, GFP_ATOMIC);
 
4984
                } else {
 
4985
                        skb2 = skb;
 
4986
                        skb = NULL;
 
4987
                }
 
4988
 
 
4989
                if (skb2)
 
4990
                        /* Send frame to hostapd */
 
4991
                        ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
 
4992
 
 
4993
                if (!skb)
 
4994
                        return;
 
4995
        }
 
4996
 
 
4997
        if (!local->monitors) {
 
4998
                dev_kfree_skb(skb);
 
4999
                return;
 
5000
        }
 
5001
 
 
5002
        /* send frame to monitor interfaces now */
 
5003
 
 
5004
        if (skb_headroom(skb) < sizeof(*rthdr)) {
 
5005
                printk(KERN_ERR "iwlwifi_ieee80211_tx_status: headroom too small\n");
 
5006
                dev_kfree_skb(skb);
 
5007
                return;
 
5008
        }
 
5009
 
 
5010
        rthdr = (struct iwlwifi_ieee80211_tx_status_rtap_hdr*)
 
5011
                                skb_push(skb, sizeof(*rthdr));
 
5012
 
 
5013
        memset(rthdr, 0, sizeof(*rthdr));
 
5014
        rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
 
5015
        rthdr->hdr.it_present =
 
5016
                cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
 
5017
                            (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
 
5018
 
 
5019
        if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
 
5020
            !is_multicast_ether_addr(hdr->addr1))
 
5021
                rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
 
5022
 
 
5023
        if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
 
5024
            (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
 
5025
                rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
 
5026
        else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
 
5027
                rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
 
5028
 
 
5029
        rthdr->data_retries = status->retry_count;
 
5030
 
 
5031
        read_lock(&local->sub_if_lock);
 
5032
        monitors = local->monitors;
 
5033
        list_for_each_entry(sdata, &local->sub_if_list, list) {
 
5034
                /*
 
5035
                 * Using the monitors counter is possibly racy, but
 
5036
                 * if the value is wrong we simply either clone the skb
 
5037
                 * once too much or forget sending it to one monitor iface
 
5038
                 * The latter case isn't nice but fixing the race is much
 
5039
                 * more complicated.
 
5040
                 */
 
5041
                if (!monitors || !skb)
 
5042
                        goto out;
 
5043
 
 
5044
                if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
 
5045
                        if (!netif_running(sdata->dev))
 
5046
                                continue;
 
5047
                        monitors--;
 
5048
                        if (monitors)
 
5049
                                skb2 = skb_clone(skb, GFP_KERNEL);
 
5050
                        else
 
5051
                                skb2 = NULL;
 
5052
                        skb->dev = sdata->dev;
 
5053
                        /* XXX: is this sufficient for BPF? */
 
5054
                        skb_set_mac_header(skb, 0);
 
5055
                        skb->ip_summed = CHECKSUM_UNNECESSARY;
 
5056
                        skb->pkt_type = PACKET_OTHERHOST;
 
5057
                        skb->protocol = htons(ETH_P_802_2);
 
5058
                        memset(skb->cb, 0, sizeof(skb->cb));
 
5059
                        netif_rx(skb);
 
5060
                        skb = skb2;
 
5061
                }
 
5062
        }
 
5063
 out:
 
5064
        read_unlock(&local->sub_if_lock);
 
5065
        if (skb)
 
5066
                dev_kfree_skb(skb);
 
5067
}
 
5068
EXPORT_SYMBOL(iwlwifi_ieee80211_tx_status);
 
5069
 
 
5070
/* TODO: implement register/unregister functions for adding TX/RX handlers
 
5071
 * into ordered list */
 
5072
 
 
5073
/* rx_pre handlers don't have dev and sdata fields available in
 
5074
 * ieee80211_txrx_data */
 
5075
static ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
 
5076
{
 
5077
        ieee80211_rx_h_parse_qos,
 
5078
        ieee80211_rx_h_load_stats,
 
5079
        ieee80211_rx_h_reorder_ampdu,
 
5080
        NULL
 
5081
};
 
5082
 
 
5083
static ieee80211_rx_handler ieee80211_rx_handlers[] =
 
5084
{
 
5085
        ieee80211_rx_h_if_stats,
 
5086
        ieee80211_rx_h_monitor,
 
5087
        ieee80211_rx_h_passive_scan,
 
5088
        ieee80211_rx_h_check,
 
5089
        ieee80211_rx_h_sta_process,
 
5090
        ieee80211_rx_h_ccmp_decrypt,
 
5091
        ieee80211_rx_h_tkip_decrypt,
 
5092
        ieee80211_rx_h_wep_weak_iv_detection,
 
5093
        ieee80211_rx_h_wep_decrypt,
 
5094
        ieee80211_rx_h_defragment,
 
5095
        ieee80211_rx_h_ps_poll,
 
5096
        ieee80211_rx_h_michael_mic_verify,
 
5097
        /* this must be after decryption - so header is counted in MPDU mic
 
5098
         * must be before pae and data, so QOS_DATA format frames
 
5099
         * are not passed to user space by these functions
 
5100
         */
 
5101
        ieee80211_rx_h_remove_qos_control,
 
5102
        ieee80211_rx_h_802_1x_pae,
 
5103
        ieee80211_rx_h_drop_unencrypted,
 
5104
        ieee80211_rx_h_data_agg,
 
5105
        ieee80211_rx_h_data,
 
5106
        ieee80211_rx_h_ctrl,
 
5107
        ieee80211_rx_h_mgmt,
 
5108
        NULL
 
5109
};
 
5110
 
 
5111
static ieee80211_tx_handler ieee80211_tx_handlers[] =
 
5112
{
 
5113
        ieee80211_tx_h_check_assoc,
 
5114
        ieee80211_tx_h_sequence,
 
5115
        ieee80211_tx_h_ps_buf,
 
5116
        ieee80211_tx_h_select_key,
 
5117
        ieee80211_tx_h_michael_mic_add,
 
5118
        ieee80211_tx_h_fragment,
 
5119
        ieee80211_tx_h_tkip_encrypt,
 
5120
        ieee80211_tx_h_ccmp_encrypt,
 
5121
        ieee80211_tx_h_wep_encrypt,
 
5122
        ieee80211_tx_h_rate_ctrl,
 
5123
        ieee80211_tx_h_misc,
 
5124
        ieee80211_tx_h_load_stats,
 
5125
        NULL
 
5126
};
 
5127
 
 
5128
 
 
5129
int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
 
5130
{
 
5131
        struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
 
5132
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
5133
        struct sta_info *sta;
 
5134
 
 
5135
        if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
 
5136
                return 0;
 
5137
 
 
5138
        /* Create STA entry for the new peer */
 
5139
        sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
 
5140
        if (!sta)
 
5141
                return -ENOMEM;
 
5142
        iwlwifi_sta_info_put(sta);
 
5143
 
 
5144
        /* Remove STA entry for the old peer */
 
5145
        sta = iwlwifi_sta_info_get(local, sdata->u.wds.remote_addr);
 
5146
        if (sta) {
 
5147
                iwlwifi_sta_info_put(sta);
 
5148
                sta_info_free(sta, 0);
 
5149
        } else {
 
5150
                printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
 
5151
                       "peer " MAC_FMT "\n",
 
5152
                       dev->name, MAC_ARG(sdata->u.wds.remote_addr));
 
5153
        }
 
5154
 
 
5155
        /* Update WDS link data */
 
5156
        memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
 
5157
 
 
5158
        return 0;
 
5159
}
 
5160
 
 
5161
static const struct header_ops ieee80211_header_ops = {
 
5162
        .create         = eth_header,
 
5163
        .parse          = header_parse_80211,
 
5164
        .rebuild        = eth_rebuild_header,
 
5165
        .cache          = eth_header_cache,
 
5166
        .cache_update   = eth_header_cache_update,
 
5167
};
 
5168
 
 
5169
/* Must not be called for mdev and apdev */
 
5170
void ieee80211_if_setup(struct net_device *dev)
 
5171
{
 
5172
        ether_setup(dev);
 
5173
        dev->hard_start_xmit = ieee80211_subif_start_xmit;
 
5174
        dev->wireless_handlers = &ieee80211_iw_handler_def;
 
5175
        dev->set_multicast_list = ieee80211_set_multicast_list;
 
5176
        dev->change_mtu = ieee80211_change_mtu;
 
5177
        dev->get_stats = ieee80211_get_stats;
 
5178
        dev->open = ieee80211_open;
 
5179
        dev->stop = ieee80211_stop;
 
5180
        dev->uninit = ieee80211_if_reinit;
 
5181
        dev->destructor = ieee80211_if_free;
 
5182
}
 
5183
 
 
5184
void ieee80211_if_mgmt_setup(struct net_device *dev)
 
5185
{
 
5186
        ether_setup(dev);
 
5187
        dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
 
5188
        dev->change_mtu = ieee80211_change_mtu_apdev;
 
5189
        dev->get_stats = ieee80211_get_stats;
 
5190
        dev->open = ieee80211_mgmt_open;
 
5191
        dev->stop = ieee80211_mgmt_stop;
 
5192
        dev->type = ARPHRD_IEEE80211_PRISM;
 
5193
        dev->uninit = ieee80211_if_reinit;
 
5194
        dev->destructor = ieee80211_if_free;
 
5195
}
 
5196
 
 
5197
int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
 
5198
                                 const char *name)
 
5199
{
 
5200
        struct rate_control_ref *ref, *old;
 
5201
 
 
5202
        ASSERT_RTNL();
 
5203
        if (local->open_count || netif_running(local->mdev) ||
 
5204
            (local->apdev && netif_running(local->apdev)))
 
5205
                return -EBUSY;
 
5206
 
 
5207
        ref = rate_control_alloc(name, local);
 
5208
        if (!ref) {
 
5209
                printk(KERN_WARNING "%s: Failed to select rate control "
 
5210
                       "algorithm\n", local->mdev->name);
 
5211
                return -ENOENT;
 
5212
        }
 
5213
 
 
5214
        old = local->rate_ctrl;
 
5215
        local->rate_ctrl = ref;
 
5216
        if (old) {
 
5217
                rate_control_put(old);
 
5218
                sta_info_flush(local, NULL);
 
5219
        }
 
5220
 
 
5221
        printk(KERN_DEBUG "%s: Selected rate control "
 
5222
               "algorithm '%s'\n", local->mdev->name,
 
5223
               ref->ops->name);
 
5224
 
 
5225
 
 
5226
        return 0;
 
5227
}
 
5228
 
 
5229
static void rate_control_deinitialize(struct ieee80211_local *local)
 
5230
{
 
5231
        struct rate_control_ref *ref;
 
5232
 
 
5233
        ref = local->rate_ctrl;
 
5234
        local->rate_ctrl = NULL;
 
5235
        rate_control_put(ref);
 
5236
}
 
5237
 
 
5238
struct ieee80211_hw *iwlwifi_ieee80211_alloc_hw(size_t priv_data_len,
 
5239
                                        const struct ieee80211_ops *ops)
 
5240
{
 
5241
        struct net_device *mdev;
 
5242
        struct ieee80211_local *local;
 
5243
        struct ieee80211_sub_if_data *sdata;
 
5244
        int priv_size;
 
5245
        struct wiphy *wiphy;
 
5246
 
 
5247
        /* Ensure 32-byte alignment of our private data and hw private data.
 
5248
         * We use the wiphy priv data for both our ieee80211_local and for
 
5249
         * the driver's private data
 
5250
         *
 
5251
         * In memory it'll be like this:
 
5252
         *
 
5253
         * +-------------------------+
 
5254
         * | struct wiphy           |
 
5255
         * +-------------------------+
 
5256
         * | struct ieee80211_local  |
 
5257
         * +-------------------------+
 
5258
         * | driver's private data   |
 
5259
         * +-------------------------+
 
5260
         *
 
5261
         */
 
5262
        priv_size = ((sizeof(struct ieee80211_local) +
 
5263
                      NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
 
5264
                    priv_data_len;
 
5265
 
 
5266
        wiphy = wiphy_new(&mac80211_config_ops, priv_size);
 
5267
 
 
5268
        if (!wiphy)
 
5269
                return NULL;
 
5270
 
 
5271
        wiphy->privid = mac80211_wiphy_privid;
 
5272
 
 
5273
        local = wiphy_priv(wiphy);
 
5274
        local->hw.wiphy = wiphy;
 
5275
 
 
5276
        local->hw.priv = (char *)local +
 
5277
                         ((sizeof(struct ieee80211_local) +
 
5278
                           NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
 
5279
 
 
5280
        BUG_ON(!ops->tx);
 
5281
        BUG_ON(!ops->config);
 
5282
        BUG_ON(!ops->add_interface);
 
5283
        local->ops = ops;
 
5284
 
 
5285
        /* for now, mdev needs sub_if_data :/ */
 
5286
        mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
 
5287
                            "wmaster%d", ether_setup);
 
5288
        if (!mdev) {
 
5289
                wiphy_free(wiphy);
 
5290
                return NULL;
 
5291
        }
 
5292
 
 
5293
        sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
 
5294
        mdev->ieee80211_ptr = &sdata->wdev;
 
5295
        sdata->wdev.wiphy = wiphy;
 
5296
 
 
5297
        local->hw.queues = 1; /* default */
 
5298
 
 
5299
        local->mdev = mdev;
 
5300
        local->rx_pre_handlers = ieee80211_rx_pre_handlers;
 
5301
        local->rx_handlers = ieee80211_rx_handlers;
 
5302
        local->tx_handlers = ieee80211_tx_handlers;
 
5303
 
 
5304
        local->bridge_packets = 1;
 
5305
 
 
5306
        local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
 
5307
        local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
 
5308
        local->short_retry_limit = 7;
 
5309
        local->long_retry_limit = 4;
 
5310
        local->hw.conf.radio_enabled = 1;
 
5311
        local->hw.conf.power_management_enable = 0;
 
5312
 
 
5313
        local->enabled_modes = (unsigned int) -1;
 
5314
 
 
5315
        INIT_LIST_HEAD(&local->modes_list);
 
5316
 
 
5317
        rwlock_init(&local->sub_if_lock);
 
5318
        INIT_LIST_HEAD(&local->sub_if_list);
 
5319
 
 
5320
        INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
 
5321
        init_timer(&local->stat_timer);
 
5322
        local->stat_timer.function = ieee80211_stat_refresh;
 
5323
        local->stat_timer.data = (unsigned long) local;
 
5324
        ieee80211_rx_bss_list_init(mdev);
 
5325
 
 
5326
        sta_info_init(local);
 
5327
 
 
5328
        mdev->hard_start_xmit = ieee80211_master_start_xmit;
 
5329
        mdev->open = ieee80211_master_open;
 
5330
        mdev->stop = ieee80211_master_stop;
 
5331
        mdev->type = ARPHRD_IEEE80211;
 
5332
        mdev->header_ops = &ieee80211_header_ops;
 
5333
 
 
5334
        sdata->type = 0xFFFFFFFF;
 
5335
        sdata->dev = mdev;
 
5336
        sdata->local = local;
 
5337
        sdata->u.ap.force_unicast_rateidx = -1;
 
5338
        sdata->u.ap.max_ratectrl_rateidx = -1;
 
5339
        ieee80211_if_sdata_init(sdata);
 
5340
        list_add_tail(&sdata->list, &local->sub_if_list);
 
5341
 
 
5342
        tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
 
5343
                     (unsigned long)local);
 
5344
        tasklet_disable(&local->tx_pending_tasklet);
 
5345
 
 
5346
        tasklet_init(&local->tasklet,
 
5347
                     ieee80211_tasklet_handler,
 
5348
                     (unsigned long) local);
 
5349
        tasklet_disable(&local->tasklet);
 
5350
 
 
5351
        skb_queue_head_init(&local->skb_queue);
 
5352
        skb_queue_head_init(&local->skb_queue_unreliable);
 
5353
 
 
5354
        return local_to_hw(local);
 
5355
}
 
5356
EXPORT_SYMBOL(iwlwifi_ieee80211_alloc_hw);
 
5357
 
 
5358
int iwlwifi_ieee80211_register_hw(struct ieee80211_hw *hw)
 
5359
{
 
5360
        struct ieee80211_local *local = hw_to_local(hw);
 
5361
        const char *name;
 
5362
        int result;
 
5363
 
 
5364
        result = wiphy_register(local->hw.wiphy);
 
5365
        if (result < 0)
 
5366
                return result;
 
5367
 
 
5368
        name = wiphy_dev(local->hw.wiphy)->driver->name;
 
5369
        local->hw.workqueue = create_singlethread_workqueue(name);
 
5370
        if (!local->hw.workqueue) {
 
5371
                result = -ENOMEM;
 
5372
                goto fail_workqueue;
 
5373
        }
 
5374
 
 
5375
        /*
 
5376
         * The hardware needs headroom for sending the frame,
 
5377
         * and we need some headroom for passing the frame to monitor
 
5378
         * interfaces, but never both at the same time.
 
5379
         */
 
5380
        local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
 
5381
                                   sizeof(struct iwlwifi_ieee80211_tx_status_rtap_hdr));
 
5382
 
 
5383
        debugfs_hw_add(local);
 
5384
 
 
5385
        local->hw.conf.beacon_int = 1000;
 
5386
 
 
5387
        local->wstats_flags |= local->hw.max_rssi ?
 
5388
                               IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
 
5389
        local->wstats_flags |= local->hw.max_signal ?
 
5390
                               IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
 
5391
        local->wstats_flags |= local->hw.max_noise ?
 
5392
                               IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
 
5393
        if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
 
5394
                local->wstats_flags |= IW_QUAL_DBM;
 
5395
 
 
5396
        local->user_txpow = IEEE80211_MAX_TXPOWER;
 
5397
        result = sta_info_start(local);
 
5398
        if (result < 0)
 
5399
                goto fail_sta_info;
 
5400
 
 
5401
        rtnl_lock();
 
5402
        result = dev_alloc_name(local->mdev, local->mdev->name);
 
5403
        if (result < 0)
 
5404
                goto fail_dev;
 
5405
 
 
5406
        memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
 
5407
        SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
 
5408
 
 
5409
        result = register_netdevice(local->mdev);
 
5410
        if (result < 0)
 
5411
                goto fail_dev;
 
5412
 
 
5413
        ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
 
5414
 
 
5415
        result = ieee80211_init_rate_ctrl_alg(local,
 
5416
                                hw->preferred_rate_control);
 
5417
        if (result < 0) {
 
5418
                printk(KERN_DEBUG "%s: Failed to initialize %s rate control "
 
5419
                        "algorithm\n", local->mdev->name,
 
5420
                        hw->preferred_rate_control ?
 
5421
                        hw->preferred_rate_control : "default");
 
5422
                goto fail_rate;
 
5423
        }
 
5424
 
 
5425
        result = ieee80211_wep_init(local);
 
5426
 
 
5427
        if (result < 0) {
 
5428
                printk(KERN_DEBUG "%s: Failed to initialize wep\n",
 
5429
                       local->mdev->name);
 
5430
                goto fail_wep;
 
5431
        }
 
5432
 
 
5433
        ieee80211_install_qdisc(local->mdev);
 
5434
 
 
5435
        /* add one default STA interface */
 
5436
        result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
 
5437
                                  IEEE80211_IF_TYPE_STA);
 
5438
        if (result)
 
5439
                printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
 
5440
                       local->mdev->name);
 
5441
 
 
5442
        local->reg_state = IEEE80211_DEV_REGISTERED;
 
5443
        rtnl_unlock();
 
5444
 
 
5445
        ieee80211_led_init(local);
 
5446
 
 
5447
        return 0;
 
5448
 
 
5449
fail_wep:
 
5450
        rate_control_deinitialize(local);
 
5451
fail_rate:
 
5452
        ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
 
5453
        unregister_netdevice(local->mdev);
 
5454
fail_dev:
 
5455
        rtnl_unlock();
 
5456
        sta_info_stop(local);
 
5457
fail_sta_info:
 
5458
        debugfs_hw_del(local);
 
5459
        destroy_workqueue(local->hw.workqueue);
 
5460
fail_workqueue:
 
5461
        wiphy_unregister(local->hw.wiphy);
 
5462
        return result;
 
5463
}
 
5464
EXPORT_SYMBOL(iwlwifi_ieee80211_register_hw);
 
5465
 
 
5466
int iwlwifi_iwlwifi_ieee80211_register_hwmode(struct ieee80211_hw *hw,
 
5467
                              struct ieee80211_hw_mode *mode)
 
5468
{
 
5469
        struct ieee80211_local *local = hw_to_local(hw);
 
5470
        struct ieee80211_rate *rate;
 
5471
        int i;
 
5472
 
 
5473
        INIT_LIST_HEAD(&mode->list);
 
5474
        list_add_tail(&mode->list, &local->modes_list);
 
5475
 
 
5476
        local->hw_modes |= (1 << mode->mode);
 
5477
        for (i = 0; i < mode->num_rates; i++) {
 
5478
                rate = &(mode->rates[i]);
 
5479
                rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
 
5480
        }
 
5481
        ieee80211_prepare_rates(local, mode);
 
5482
 
 
5483
        if (!local->oper_hw_mode) {
 
5484
                /* Default to this mode */
 
5485
                local->hw.conf.phymode = mode->mode;
 
5486
                local->oper_hw_mode = local->scan_hw_mode = mode;
 
5487
                local->oper_channel = local->scan_channel = &mode->channels[0];
 
5488
                local->hw.conf.mode = local->oper_hw_mode;
 
5489
                local->hw.conf.chan = local->oper_channel;
 
5490
        }
 
5491
 
 
5492
        if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
 
5493
                ieee80211_set_default_regdomain(mode);
 
5494
 
 
5495
        return 0;
 
5496
}
 
5497
EXPORT_SYMBOL(iwlwifi_iwlwifi_ieee80211_register_hwmode);
 
5498
 
 
5499
void iwlwifi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
 
5500
{
 
5501
        struct ieee80211_local *local = hw_to_local(hw);
 
5502
        struct ieee80211_sub_if_data *sdata, *tmp;
 
5503
        struct list_head tmp_list;
 
5504
        int i;
 
5505
 
 
5506
        tasklet_kill(&local->tx_pending_tasklet);
 
5507
        tasklet_kill(&local->tasklet);
 
5508
 
 
5509
        rtnl_lock();
 
5510
 
 
5511
        BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
 
5512
 
 
5513
        local->reg_state = IEEE80211_DEV_UNREGISTERED;
 
5514
        if (local->apdev)
 
5515
                ieee80211_if_del_mgmt(local);
 
5516
 
 
5517
        write_lock_bh(&local->sub_if_lock);
 
5518
        list_replace_init(&local->sub_if_list, &tmp_list);
 
5519
        write_unlock_bh(&local->sub_if_lock);
 
5520
 
 
5521
        list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
 
5522
                __ieee80211_if_del(local, sdata);
 
5523
 
 
5524
        rtnl_unlock();
 
5525
 
 
5526
        if (local->stat_time)
 
5527
                del_timer_sync(&local->stat_timer);
 
5528
 
 
5529
        ieee80211_rx_bss_list_deinit(local->mdev);
 
5530
        ieee80211_clear_tx_pending(local);
 
5531
        sta_info_stop(local);
 
5532
        rate_control_deinitialize(local);
 
5533
        debugfs_hw_del(local);
 
5534
 
 
5535
        for (i = 0; i < NUM_IEEE80211_MODES; i++) {
 
5536
                kfree(local->supp_rates[i]);
 
5537
                kfree(local->basic_rates[i]);
 
5538
        }
 
5539
 
 
5540
        if (skb_queue_len(&local->skb_queue)
 
5541
                        || skb_queue_len(&local->skb_queue_unreliable))
 
5542
                printk(KERN_WARNING "%s: skb_queue not empty\n",
 
5543
                       local->mdev->name);
 
5544
        skb_queue_purge(&local->skb_queue);
 
5545
        skb_queue_purge(&local->skb_queue_unreliable);
 
5546
 
 
5547
        destroy_workqueue(local->hw.workqueue);
 
5548
        wiphy_unregister(local->hw.wiphy);
 
5549
        ieee80211_wep_free(local);
 
5550
        ieee80211_led_exit(local);
 
5551
}
 
5552
EXPORT_SYMBOL(iwlwifi_ieee80211_unregister_hw);
 
5553
 
 
5554
void iwlwifi_ieee80211_free_hw(struct ieee80211_hw *hw)
 
5555
{
 
5556
        struct ieee80211_local *local = hw_to_local(hw);
 
5557
 
 
5558
        ieee80211_if_free(local->mdev);
 
5559
        wiphy_free(local->hw.wiphy);
 
5560
}
 
5561
EXPORT_SYMBOL(iwlwifi_ieee80211_free_hw);
 
5562
 
 
5563
void iwlwifi_ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
 
5564
{
 
5565
        struct ieee80211_local *local = hw_to_local(hw);
 
5566
 
 
5567
        if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
 
5568
                               &local->state[queue])) {
 
5569
                if (test_bit(IEEE80211_LINK_STATE_PENDING,
 
5570
                             &local->state[queue]))
 
5571
                        tasklet_schedule(&local->tx_pending_tasklet);
 
5572
                else
 
5573
                        if (!ieee80211_qdisc_installed(local->mdev)) {
 
5574
                                if (queue == 0)
 
5575
                                        netif_wake_queue(local->mdev);
 
5576
                        } else
 
5577
                                __netif_schedule(local->mdev);
 
5578
        }
 
5579
}
 
5580
EXPORT_SYMBOL(iwlwifi_ieee80211_wake_queue);
 
5581
 
 
5582
void iwlwifi_ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
 
5583
{
 
5584
        struct ieee80211_local *local = hw_to_local(hw);
 
5585
 
 
5586
        if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
 
5587
                netif_stop_queue(local->mdev);
 
5588
        set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
 
5589
}
 
5590
EXPORT_SYMBOL(iwlwifi_ieee80211_stop_queue);
 
5591
 
 
5592
void iwlwifi_ieee80211_start_queues(struct ieee80211_hw *hw)
 
5593
{
 
5594
        struct ieee80211_local *local = hw_to_local(hw);
 
5595
        int i;
 
5596
 
 
5597
        for (i = 0; i < local->hw.queues; i++)
 
5598
                clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
 
5599
        if (!ieee80211_qdisc_installed(local->mdev))
 
5600
                netif_start_queue(local->mdev);
 
5601
}
 
5602
EXPORT_SYMBOL(iwlwifi_ieee80211_start_queues);
 
5603
 
 
5604
void iwlwifi_iwlwifi_ieee80211_stop_queues(struct ieee80211_hw *hw)
 
5605
{
 
5606
        int i;
 
5607
 
 
5608
        for (i = 0; i < hw->queues; i++)
 
5609
                iwlwifi_ieee80211_stop_queue(hw, i);
 
5610
}
 
5611
EXPORT_SYMBOL(iwlwifi_iwlwifi_ieee80211_stop_queues);
 
5612
 
 
5613
void iwlwifi_iwlwifi_ieee80211_wake_queues(struct ieee80211_hw *hw)
 
5614
{
 
5615
        int i;
 
5616
 
 
5617
        for (i = 0; i < hw->queues; i++)
 
5618
                iwlwifi_ieee80211_wake_queue(hw, i);
 
5619
}
 
5620
EXPORT_SYMBOL(iwlwifi_iwlwifi_ieee80211_wake_queues);
 
5621
 
 
5622
int iwlwifi_ieee80211_start_BA_session(struct ieee80211_hw *hw, u8 *da, u16 tid)
 
5623
{
 
5624
        struct ieee80211_local *local = hw_to_local(hw);
 
5625
        struct sta_info *sta;
 
5626
        struct net_device *dev;
 
5627
        struct ieee80211_sub_if_data *sdata;
 
5628
        u16 start_seq_num=0;
 
5629
        int rc = -EAGAIN;
 
5630
 
 
5631
        if (tid >= STA_TID_NUM)
 
5632
                return -EINVAL;
 
5633
        else
 
5634
                printk (KERN_DEBUG "Open a BA session requested on DA " MAC_FMT " tid %d\n",MAC_ARG(da),tid);
 
5635
 
 
5636
        sta = iwlwifi_sta_info_get(local, da);
 
5637
        if (!sta)
 
5638
                return -ENOENT;
 
5639
 
 
5640
        /* check if the TID is not in aggregation flow already */
 
5641
        spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5642
        if (sta->ht_ba_mlme.tid_agg_info_tx[tid].state == HT_AGG_STATE_IDLE)
 
5643
                sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_START_BA;
 
5644
        else {
 
5645
                spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5646
                printk (KERN_DEBUG "BA request denied - aggregation is not idle on tid %d\n",tid);
 
5647
                goto start_BA_exit;
 
5648
        }
 
5649
        spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5650
 
 
5651
        /* create a new queue for this aggregation */
 
5652
        rc = ieee80211_ht_agg_queue_add(local,sta,tid);
 
5653
 
 
5654
        /* case no queue is available to aggregation don't switch to aggregation */
 
5655
        if(rc) {
 
5656
                printk (KERN_DEBUG "BA request denied - no queue available for tid %d\n",tid);
 
5657
        } else { /* try to move HW to aggregation */
 
5658
                rc = -EAGAIN;
 
5659
                dev = sta->dev;
 
5660
                sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
5661
                start_seq_num = sdata->sequence >> 4; /* this default value can be changed by driver */
 
5662
                if (local->ops->ht_tx_agg_start)
 
5663
                        rc = local->ops->ht_tx_agg_start(hw,da,tid,&start_seq_num);
 
5664
                if(rc)
 
5665
                        /* remove the queue for this aggregation */
 
5666
                        ieee80211_ht_agg_queue_remove(local,sta,tid);
 
5667
        }
 
5668
 
 
5669
        /* case we denied aggregation go back to legacy */
 
5670
        if(rc) {
 
5671
                spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5672
                sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_IDLE;
 
5673
                spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5674
                printk (KERN_DEBUG "BA request denied - HW or queue unavailable for tid %d\n",tid);
 
5675
                goto start_BA_exit;
 
5676
        }
 
5677
 
 
5678
        /* send an addBA request */
 
5679
        spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5680
        sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_ADDBA_REQUESTED;
 
5681
        sta->ht_ba_mlme.dialog_token_allocator++;
 
5682
        sta->ht_ba_mlme.tid_agg_info_tx[tid].dialog_token = sta->ht_ba_mlme.dialog_token_allocator;
 
5683
        sta->ht_ba_mlme.tid_agg_info_tx[tid].start_seq_num = start_seq_num;
 
5684
        spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5685
 
 
5686
        ieee80211_send_addba_request(sta->dev, da, tid,
 
5687
                                                                 sta->ht_ba_mlme.tid_agg_info_tx[tid].dialog_token,
 
5688
                                                                 sta->ht_ba_mlme.tid_agg_info_tx[tid].start_seq_num,
 
5689
                                                                 0x40,5000);
 
5690
 
 
5691
        /* activate the timer for the recipient's addBA response */
 
5692
        sta->ht_ba_mlme.tid_agg_info_tx[tid].addba_resp_timer.expires = jiffies + ADDBA_RESP_INTERVAL;
 
5693
        add_timer(&sta->ht_ba_mlme.tid_agg_info_tx[tid].addba_resp_timer);
 
5694
        printk(KERN_DEBUG "activated addBA response timer on tid %d\n",tid);
 
5695
 
 
5696
        rc = 0;
 
5697
start_BA_exit:
 
5698
        iwlwifi_sta_info_put(sta);
 
5699
        return rc;
 
5700
}
 
5701
EXPORT_SYMBOL(iwlwifi_ieee80211_start_BA_session);
 
5702
 
 
5703
int iwlwifi_ieee80211_stop_BA_session(struct ieee80211_hw *hw, u8 *da, u16 tid)
 
5704
{
 
5705
        struct ieee80211_local *local = hw_to_local(hw);
 
5706
        struct sta_info *sta;
 
5707
        int rc = -EAGAIN;
 
5708
 
 
5709
        if (tid >= STA_TID_NUM)
 
5710
                return -EINVAL;
 
5711
        else
 
5712
                printk (KERN_DEBUG "Stop a BA session requested on DA " MAC_FMT " tid %d\n",MAC_ARG(da),tid);
 
5713
 
 
5714
        sta = iwlwifi_sta_info_get(local, da);
 
5715
        if (!sta)
 
5716
                return -ENOENT;
 
5717
 
 
5718
        /* check if the TID is in aggregation */
 
5719
        spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5720
        if (sta->ht_ba_mlme.tid_agg_info_tx[tid].state == HT_AGG_STATE_OPERATIONAL)
 
5721
                sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_INITIATOR_REQ_STOP_BA;
 
5722
        else {
 
5723
                spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5724
                printk (KERN_DEBUG "request denied - aggregation is not opertional yet on tid %d\n",tid);
 
5725
                goto stop_BA_exit;
 
5726
        }
 
5727
        spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5728
 
 
5729
        if (local->ops->ht_tx_agg_stop)
 
5730
                rc = local->ops->ht_tx_agg_stop(hw,da,tid,1 /*WLAN_BACK_INITIATOR*/);
 
5731
 
 
5732
        /* case HW denied going back to legacy */
 
5733
        if(rc) {
 
5734
                spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5735
                sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_OPERATIONAL;
 
5736
                spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5737
                printk (KERN_ERR "HW problem - can not stop aggergation for tid %d\n",tid);
 
5738
                goto stop_BA_exit;
 
5739
        }
 
5740
 
 
5741
        /* remove the queue for this aggregation */
 
5742
        ieee80211_ht_agg_queue_remove(local,sta,tid);
 
5743
 
 
5744
        /* send a delBA */
 
5745
        ieee80211_send_delba(sta->dev, da, tid, 1 /*WLAN_BACK_INITIATOR*/, WLAN_REASON_QSTA_NOT_USE);
 
5746
        spin_lock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5747
        sta->ht_ba_mlme.tid_agg_info_tx[tid].state = HT_AGG_STATE_IDLE;
 
5748
        spin_unlock_bh(&sta->ht_ba_mlme.agg_data_lock_tx);
 
5749
 
 
5750
        rc = 0;
 
5751
stop_BA_exit:
 
5752
        iwlwifi_sta_info_put(sta);
 
5753
        return rc;
 
5754
}
 
5755
EXPORT_SYMBOL(iwlwifi_ieee80211_stop_BA_session);
 
5756
 
 
5757
struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
 
5758
{
 
5759
        struct ieee80211_sub_if_data *sdata;
 
5760
        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 
5761
        return &sdata->stats;
 
5762
}
 
5763
 
 
5764
static int __init ieee80211_init(void)
 
5765
{
 
5766
        struct sk_buff *skb;
 
5767
        int ret;
 
5768
 
 
5769
        BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
 
5770
 
 
5771
        ret = ieee80211_wme_register();
 
5772
        if (ret) {
 
5773
                printk(KERN_DEBUG "ieee80211_init: failed to "
 
5774
                       "initialize WME (err=%d)\n", ret);
 
5775
                return ret;
 
5776
        }
 
5777
 
 
5778
        ieee80211_debugfs_netdev_init();
 
5779
        ieee80211_regdomain_init();
 
5780
 
 
5781
        return 0;
 
5782
}
 
5783
 
 
5784
 
 
5785
static void __exit ieee80211_exit(void)
 
5786
{
 
5787
        ieee80211_wme_unregister();
 
5788
        ieee80211_debugfs_netdev_exit();
 
5789
}
 
5790
 
 
5791
 
 
5792
module_init(ieee80211_init);
 
5793
module_exit(ieee80211_exit);
 
5794
 
 
5795
MODULE_DESCRIPTION("IEEE 802.11 subsystem");
 
5796
MODULE_LICENSE("GPL");