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