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