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