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