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