[MAC80211]: remove some unnecessary includes
[safe/jmp/linux-2.6] / net / mac80211 / ieee80211.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/bitmap.h>
24 #include <net/cfg80211.h>
25
26 #include "ieee80211_common.h"
27 #include "ieee80211_i.h"
28 #include "ieee80211_rate.h"
29 #include "wep.h"
30 #include "wme.h"
31 #include "aes_ccm.h"
32 #include "ieee80211_led.h"
33 #include "ieee80211_cfg.h"
34 #include "debugfs.h"
35 #include "debugfs_netdev.h"
36 #include "debugfs_key.h"
37
38 /* privid for wiphys to determine whether they belong to us or not */
39 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
40
41 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
42 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
43 const unsigned char rfc1042_header[] =
44         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
45
46 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
47 const unsigned char bridge_tunnel_header[] =
48         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
49
50 /* No encapsulation header if EtherType < 0x600 (=length) */
51 static const unsigned char eapol_header[] =
52         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
53
54
55 /*
56  * For seeing transmitted packets on monitor interfaces
57  * we have a radiotap header too.
58  */
59 struct ieee80211_tx_status_rtap_hdr {
60         struct ieee80211_radiotap_header hdr;
61         __le16 tx_flags;
62         u8 data_retries;
63 } __attribute__ ((packed));
64
65
66 struct ieee80211_key_conf *
67 ieee80211_key_data2conf(struct ieee80211_local *local,
68                         const struct ieee80211_key *data)
69 {
70         struct ieee80211_key_conf *conf;
71
72         conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
73         if (!conf)
74                 return NULL;
75
76         conf->hw_key_idx = data->hw_key_idx;
77         conf->alg = data->alg;
78         conf->keylen = data->keylen;
79         conf->flags = 0;
80         if (data->force_sw_encrypt)
81                 conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
82         conf->keyidx = data->keyidx;
83         if (data->default_tx_key)
84                 conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
85         if (local->default_wep_only)
86                 conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
87         memcpy(conf->key, data->key, data->keylen);
88
89         return conf;
90 }
91
92 struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
93                                           int idx, size_t key_len, gfp_t flags)
94 {
95         struct ieee80211_key *key;
96
97         key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
98         if (!key)
99                 return NULL;
100         kref_init(&key->kref);
101         return key;
102 }
103
104 static void ieee80211_key_release(struct kref *kref)
105 {
106         struct ieee80211_key *key;
107
108         key = container_of(kref, struct ieee80211_key, kref);
109         if (key->alg == ALG_CCMP)
110                 ieee80211_aes_key_free(key->u.ccmp.tfm);
111         ieee80211_debugfs_key_remove(key);
112         kfree(key);
113 }
114
115 void ieee80211_key_free(struct ieee80211_key *key)
116 {
117         if (key)
118                 kref_put(&key->kref, ieee80211_key_release);
119 }
120
121 static int rate_list_match(const int *rate_list, int rate)
122 {
123         int i;
124
125         if (!rate_list)
126                 return 0;
127
128         for (i = 0; rate_list[i] >= 0; i++)
129                 if (rate_list[i] == rate)
130                         return 1;
131
132         return 0;
133 }
134
135
136 void ieee80211_prepare_rates(struct ieee80211_local *local,
137                              struct ieee80211_hw_mode *mode)
138 {
139         int i;
140
141         for (i = 0; i < mode->num_rates; i++) {
142                 struct ieee80211_rate *rate = &mode->rates[i];
143
144                 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
145                                  IEEE80211_RATE_BASIC);
146
147                 if (local->supp_rates[mode->mode]) {
148                         if (!rate_list_match(local->supp_rates[mode->mode],
149                                              rate->rate))
150                                 continue;
151                 }
152
153                 rate->flags |= IEEE80211_RATE_SUPPORTED;
154
155                 /* Use configured basic rate set if it is available. If not,
156                  * use defaults that are sane for most cases. */
157                 if (local->basic_rates[mode->mode]) {
158                         if (rate_list_match(local->basic_rates[mode->mode],
159                                             rate->rate))
160                                 rate->flags |= IEEE80211_RATE_BASIC;
161                 } else switch (mode->mode) {
162                 case MODE_IEEE80211A:
163                         if (rate->rate == 60 || rate->rate == 120 ||
164                             rate->rate == 240)
165                                 rate->flags |= IEEE80211_RATE_BASIC;
166                         break;
167                 case MODE_IEEE80211B:
168                         if (rate->rate == 10 || rate->rate == 20)
169                                 rate->flags |= IEEE80211_RATE_BASIC;
170                         break;
171                 case MODE_ATHEROS_TURBO:
172                         if (rate->rate == 120 || rate->rate == 240 ||
173                             rate->rate == 480)
174                                 rate->flags |= IEEE80211_RATE_BASIC;
175                         break;
176                 case MODE_IEEE80211G:
177                         if (rate->rate == 10 || rate->rate == 20 ||
178                             rate->rate == 55 || rate->rate == 110)
179                                 rate->flags |= IEEE80211_RATE_BASIC;
180                         break;
181                 }
182
183                 /* Set ERP and MANDATORY flags based on phymode */
184                 switch (mode->mode) {
185                 case MODE_IEEE80211A:
186                         if (rate->rate == 60 || rate->rate == 120 ||
187                             rate->rate == 240)
188                                 rate->flags |= IEEE80211_RATE_MANDATORY;
189                         break;
190                 case MODE_IEEE80211B:
191                         if (rate->rate == 10)
192                                 rate->flags |= IEEE80211_RATE_MANDATORY;
193                         break;
194                 case MODE_ATHEROS_TURBO:
195                         break;
196                 case MODE_IEEE80211G:
197                         if (rate->rate == 10 || rate->rate == 20 ||
198                             rate->rate == 55 || rate->rate == 110 ||
199                             rate->rate == 60 || rate->rate == 120 ||
200                             rate->rate == 240)
201                                 rate->flags |= IEEE80211_RATE_MANDATORY;
202                         break;
203                 }
204                 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
205                         rate->flags |= IEEE80211_RATE_ERP;
206         }
207 }
208
209
210 void ieee80211_key_threshold_notify(struct net_device *dev,
211                                     struct ieee80211_key *key,
212                                     struct sta_info *sta)
213 {
214         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
215         struct sk_buff *skb;
216         struct ieee80211_msg_key_notification *msg;
217
218         /* if no one will get it anyway, don't even allocate it.
219          * unlikely because this is only relevant for APs
220          * where the device must be open... */
221         if (unlikely(!local->apdev))
222                 return;
223
224         skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
225                             sizeof(struct ieee80211_msg_key_notification));
226         if (!skb)
227                 return;
228
229         skb_reserve(skb, sizeof(struct ieee80211_frame_info));
230         msg = (struct ieee80211_msg_key_notification *)
231                 skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
232         msg->tx_rx_count = key->tx_rx_count;
233         memcpy(msg->ifname, dev->name, IFNAMSIZ);
234         if (sta)
235                 memcpy(msg->addr, sta->addr, ETH_ALEN);
236         else
237                 memset(msg->addr, 0xff, ETH_ALEN);
238
239         key->tx_rx_count = 0;
240
241         ieee80211_rx_mgmt(local, skb, NULL,
242                           ieee80211_msg_key_threshold_notification);
243 }
244
245
246 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
247 {
248         u16 fc;
249
250         if (len < 24)
251                 return NULL;
252
253         fc = le16_to_cpu(hdr->frame_control);
254
255         switch (fc & IEEE80211_FCTL_FTYPE) {
256         case IEEE80211_FTYPE_DATA:
257                 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
258                 case IEEE80211_FCTL_TODS:
259                         return hdr->addr1;
260                 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
261                         return NULL;
262                 case IEEE80211_FCTL_FROMDS:
263                         return hdr->addr2;
264                 case 0:
265                         return hdr->addr3;
266                 }
267                 break;
268         case IEEE80211_FTYPE_MGMT:
269                 return hdr->addr3;
270         case IEEE80211_FTYPE_CTL:
271                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
272                         return hdr->addr1;
273                 else
274                         return NULL;
275         }
276
277         return NULL;
278 }
279
280 int ieee80211_get_hdrlen(u16 fc)
281 {
282         int hdrlen = 24;
283
284         switch (fc & IEEE80211_FCTL_FTYPE) {
285         case IEEE80211_FTYPE_DATA:
286                 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
287                         hdrlen = 30; /* Addr4 */
288                 /*
289                  * The QoS Control field is two bytes and its presence is
290                  * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
291                  * hdrlen if that bit is set.
292                  * This works by masking out the bit and shifting it to
293                  * bit position 1 so the result has the value 0 or 2.
294                  */
295                 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
296                                 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
297                 break;
298         case IEEE80211_FTYPE_CTL:
299                 /*
300                  * ACK and CTS are 10 bytes, all others 16. To see how
301                  * to get this condition consider
302                  *   subtype mask:   0b0000000011110000 (0x00F0)
303                  *   ACK subtype:    0b0000000011010000 (0x00D0)
304                  *   CTS subtype:    0b0000000011000000 (0x00C0)
305                  *   bits that matter:         ^^^      (0x00E0)
306                  *   value of those: 0b0000000011000000 (0x00C0)
307                  */
308                 if ((fc & 0xE0) == 0xC0)
309                         hdrlen = 10;
310                 else
311                         hdrlen = 16;
312                 break;
313         }
314
315         return hdrlen;
316 }
317 EXPORT_SYMBOL(ieee80211_get_hdrlen);
318
319 int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
320 {
321         const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
322         int hdrlen;
323
324         if (unlikely(skb->len < 10))
325                 return 0;
326         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
327         if (unlikely(hdrlen > skb->len))
328                 return 0;
329         return hdrlen;
330 }
331 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
332
333
334 int ieee80211_is_eapol(const struct sk_buff *skb)
335 {
336         const struct ieee80211_hdr *hdr;
337         u16 fc;
338         int hdrlen;
339
340         if (unlikely(skb->len < 10))
341                 return 0;
342
343         hdr = (const struct ieee80211_hdr *) skb->data;
344         fc = le16_to_cpu(hdr->frame_control);
345
346         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
347                 return 0;
348
349         hdrlen = ieee80211_get_hdrlen(fc);
350
351         if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
352                      memcmp(skb->data + hdrlen, eapol_header,
353                             sizeof(eapol_header)) == 0))
354                 return 1;
355
356         return 0;
357 }
358
359
360 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
361 {
362         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
363
364         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
365         if (tx->u.tx.extra_frag) {
366                 struct ieee80211_hdr *fhdr;
367                 int i;
368                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
369                         fhdr = (struct ieee80211_hdr *)
370                                 tx->u.tx.extra_frag[i]->data;
371                         fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
372                 }
373         }
374 }
375
376
377 static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
378                                     int rate, int erp, int short_preamble)
379 {
380         int dur;
381
382         /* calculate duration (in microseconds, rounded up to next higher
383          * integer if it includes a fractional microsecond) to send frame of
384          * len bytes (does not include FCS) at the given rate. Duration will
385          * also include SIFS.
386          *
387          * rate is in 100 kbps, so divident is multiplied by 10 in the
388          * DIV_ROUND_UP() operations.
389          */
390
391         if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
392             local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
393                 /*
394                  * OFDM:
395                  *
396                  * N_DBPS = DATARATE x 4
397                  * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
398                  *      (16 = SIGNAL time, 6 = tail bits)
399                  * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
400                  *
401                  * T_SYM = 4 usec
402                  * 802.11a - 17.5.2: aSIFSTime = 16 usec
403                  * 802.11g - 19.8.4: aSIFSTime = 10 usec +
404                  *      signal ext = 6 usec
405                  */
406                 /* FIX: Atheros Turbo may have different (shorter) duration? */
407                 dur = 16; /* SIFS + signal ext */
408                 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
409                 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
410                 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
411                                         4 * rate); /* T_SYM x N_SYM */
412         } else {
413                 /*
414                  * 802.11b or 802.11g with 802.11b compatibility:
415                  * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
416                  * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
417                  *
418                  * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
419                  * aSIFSTime = 10 usec
420                  * aPreambleLength = 144 usec or 72 usec with short preamble
421                  * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
422                  */
423                 dur = 10; /* aSIFSTime = 10 usec */
424                 dur += short_preamble ? (72 + 24) : (144 + 48);
425
426                 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
427         }
428
429         return dur;
430 }
431
432
433 /* Exported duration function for driver use */
434 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
435                                         size_t frame_len, int rate)
436 {
437         struct ieee80211_local *local = hw_to_local(hw);
438         u16 dur;
439         int erp;
440
441         erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
442         dur = ieee80211_frame_duration(local, frame_len, rate,
443                                        erp, local->short_preamble);
444
445         return cpu_to_le16(dur);
446 }
447 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
448
449
450 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
451                               size_t frame_len,
452                               const struct ieee80211_tx_control *frame_txctl)
453 {
454         struct ieee80211_local *local = hw_to_local(hw);
455         struct ieee80211_rate *rate;
456         int short_preamble = local->short_preamble;
457         int erp;
458         u16 dur;
459
460         rate = frame_txctl->rts_rate;
461         erp = !!(rate->flags & IEEE80211_RATE_ERP);
462
463         /* CTS duration */
464         dur = ieee80211_frame_duration(local, 10, rate->rate,
465                                        erp, short_preamble);
466         /* Data frame duration */
467         dur += ieee80211_frame_duration(local, frame_len, rate->rate,
468                                         erp, short_preamble);
469         /* ACK duration */
470         dur += ieee80211_frame_duration(local, 10, rate->rate,
471                                         erp, short_preamble);
472
473         return cpu_to_le16(dur);
474 }
475 EXPORT_SYMBOL(ieee80211_rts_duration);
476
477
478 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
479                                     size_t frame_len,
480                                     const struct ieee80211_tx_control *frame_txctl)
481 {
482         struct ieee80211_local *local = hw_to_local(hw);
483         struct ieee80211_rate *rate;
484         int short_preamble = local->short_preamble;
485         int erp;
486         u16 dur;
487
488         rate = frame_txctl->rts_rate;
489         erp = !!(rate->flags & IEEE80211_RATE_ERP);
490
491         /* Data frame duration */
492         dur = ieee80211_frame_duration(local, frame_len, rate->rate,
493                                        erp, short_preamble);
494         if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
495                 /* ACK duration */
496                 dur += ieee80211_frame_duration(local, 10, rate->rate,
497                                                 erp, short_preamble);
498         }
499
500         return cpu_to_le16(dur);
501 }
502 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
503
504 static int __ieee80211_if_config(struct net_device *dev,
505                                  struct sk_buff *beacon,
506                                  struct ieee80211_tx_control *control)
507 {
508         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
509         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
510         struct ieee80211_if_conf conf;
511         static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
512
513         if (!local->ops->config_interface || !netif_running(dev))
514                 return 0;
515
516         memset(&conf, 0, sizeof(conf));
517         conf.type = sdata->type;
518         if (sdata->type == IEEE80211_IF_TYPE_STA ||
519             sdata->type == IEEE80211_IF_TYPE_IBSS) {
520                 if (local->sta_scanning &&
521                     local->scan_dev == dev)
522                         conf.bssid = scan_bssid;
523                 else
524                         conf.bssid = sdata->u.sta.bssid;
525                 conf.ssid = sdata->u.sta.ssid;
526                 conf.ssid_len = sdata->u.sta.ssid_len;
527                 conf.generic_elem = sdata->u.sta.extra_ie;
528                 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
529         } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
530                 conf.ssid = sdata->u.ap.ssid;
531                 conf.ssid_len = sdata->u.ap.ssid_len;
532                 conf.generic_elem = sdata->u.ap.generic_elem;
533                 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
534                 conf.beacon = beacon;
535                 conf.beacon_control = control;
536         }
537         return local->ops->config_interface(local_to_hw(local),
538                                            dev->ifindex, &conf);
539 }
540
541 int ieee80211_if_config(struct net_device *dev)
542 {
543         return __ieee80211_if_config(dev, NULL, NULL);
544 }
545
546 int ieee80211_if_config_beacon(struct net_device *dev)
547 {
548         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
549         struct ieee80211_tx_control control;
550         struct sk_buff *skb;
551
552         if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
553                 return 0;
554         skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
555         if (!skb)
556                 return -ENOMEM;
557         return __ieee80211_if_config(dev, skb, &control);
558 }
559
560 int ieee80211_hw_config(struct ieee80211_local *local)
561 {
562         struct ieee80211_hw_mode *mode;
563         struct ieee80211_channel *chan;
564         int ret = 0;
565
566         if (local->sta_scanning) {
567                 chan = local->scan_channel;
568                 mode = local->scan_hw_mode;
569         } else {
570                 chan = local->oper_channel;
571                 mode = local->oper_hw_mode;
572         }
573
574         local->hw.conf.channel = chan->chan;
575         local->hw.conf.channel_val = chan->val;
576         local->hw.conf.power_level = chan->power_level;
577         local->hw.conf.freq = chan->freq;
578         local->hw.conf.phymode = mode->mode;
579         local->hw.conf.antenna_max = chan->antenna_max;
580         local->hw.conf.chan = chan;
581         local->hw.conf.mode = mode;
582
583 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
584         printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
585                "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
586                local->hw.conf.phymode);
587 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
588
589         if (local->ops->config)
590                 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
591
592         return ret;
593 }
594
595
596 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
597 {
598         /* FIX: what would be proper limits for MTU?
599          * This interface uses 802.3 frames. */
600         if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
601                 printk(KERN_WARNING "%s: invalid MTU %d\n",
602                        dev->name, new_mtu);
603                 return -EINVAL;
604         }
605
606 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
607         printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
608 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
609         dev->mtu = new_mtu;
610         return 0;
611 }
612
613
614 static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
615 {
616         /* FIX: what would be proper limits for MTU?
617          * This interface uses 802.11 frames. */
618         if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
619                 printk(KERN_WARNING "%s: invalid MTU %d\n",
620                        dev->name, new_mtu);
621                 return -EINVAL;
622         }
623
624 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
625         printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
626 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
627         dev->mtu = new_mtu;
628         return 0;
629 }
630
631 enum netif_tx_lock_class {
632         TX_LOCK_NORMAL,
633         TX_LOCK_MASTER,
634 };
635
636 static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
637 {
638         spin_lock_nested(&dev->_xmit_lock, subclass);
639         dev->xmit_lock_owner = smp_processor_id();
640 }
641
642 static void ieee80211_set_multicast_list(struct net_device *dev)
643 {
644         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
645         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
646         unsigned short flags;
647
648         netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
649         if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
650                 if (sdata->allmulti) {
651                         sdata->allmulti = 0;
652                         local->iff_allmultis--;
653                 } else {
654                         sdata->allmulti = 1;
655                         local->iff_allmultis++;
656                 }
657         }
658         if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
659                 if (sdata->promisc) {
660                         sdata->promisc = 0;
661                         local->iff_promiscs--;
662                 } else {
663                         sdata->promisc = 1;
664                         local->iff_promiscs++;
665                 }
666         }
667         if (dev->mc_count != sdata->mc_count) {
668                 local->mc_count = local->mc_count - sdata->mc_count +
669                                   dev->mc_count;
670                 sdata->mc_count = dev->mc_count;
671         }
672         if (local->ops->set_multicast_list) {
673                 flags = local->mdev->flags;
674                 if (local->iff_allmultis)
675                         flags |= IFF_ALLMULTI;
676                 if (local->iff_promiscs)
677                         flags |= IFF_PROMISC;
678                 read_lock(&local->sub_if_lock);
679                 local->ops->set_multicast_list(local_to_hw(local), flags,
680                                               local->mc_count);
681                 read_unlock(&local->sub_if_lock);
682         }
683         netif_tx_unlock(local->mdev);
684 }
685
686 struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
687                                                struct dev_mc_list *prev,
688                                                void **ptr)
689 {
690         struct ieee80211_local *local = hw_to_local(hw);
691         struct ieee80211_sub_if_data *sdata = *ptr;
692         struct dev_mc_list *mc;
693
694         if (!prev) {
695                 WARN_ON(sdata);
696                 sdata = NULL;
697         }
698         if (!prev || !prev->next) {
699                 if (sdata)
700                         sdata = list_entry(sdata->list.next,
701                                            struct ieee80211_sub_if_data, list);
702                 else
703                         sdata = list_entry(local->sub_if_list.next,
704                                            struct ieee80211_sub_if_data, list);
705                 if (&sdata->list != &local->sub_if_list)
706                         mc = sdata->dev->mc_list;
707                 else
708                         mc = NULL;
709         } else
710                 mc = prev->next;
711
712         *ptr = sdata;
713         return mc;
714 }
715 EXPORT_SYMBOL(ieee80211_get_mc_list_item);
716
717 static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
718 {
719         struct ieee80211_sub_if_data *sdata;
720         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
721         return &(sdata->stats);
722 }
723
724 static void ieee80211_if_shutdown(struct net_device *dev)
725 {
726         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
727         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
728
729         ASSERT_RTNL();
730         switch (sdata->type) {
731         case IEEE80211_IF_TYPE_STA:
732         case IEEE80211_IF_TYPE_IBSS:
733                 sdata->u.sta.state = IEEE80211_DISABLED;
734                 del_timer_sync(&sdata->u.sta.timer);
735                 skb_queue_purge(&sdata->u.sta.skb_queue);
736                 if (!local->ops->hw_scan &&
737                     local->scan_dev == sdata->dev) {
738                         local->sta_scanning = 0;
739                         cancel_delayed_work(&local->scan_work);
740                 }
741                 flush_workqueue(local->hw.workqueue);
742                 break;
743         }
744 }
745
746 static inline int identical_mac_addr_allowed(int type1, int type2)
747 {
748         return (type1 == IEEE80211_IF_TYPE_MNTR ||
749                 type2 == IEEE80211_IF_TYPE_MNTR ||
750                 (type1 == IEEE80211_IF_TYPE_AP &&
751                  type2 == IEEE80211_IF_TYPE_WDS) ||
752                 (type1 == IEEE80211_IF_TYPE_WDS &&
753                  (type2 == IEEE80211_IF_TYPE_WDS ||
754                   type2 == IEEE80211_IF_TYPE_AP)) ||
755                 (type1 == IEEE80211_IF_TYPE_AP &&
756                  type2 == IEEE80211_IF_TYPE_VLAN) ||
757                 (type1 == IEEE80211_IF_TYPE_VLAN &&
758                  (type2 == IEEE80211_IF_TYPE_AP ||
759                   type2 == IEEE80211_IF_TYPE_VLAN)));
760 }
761
762 static int ieee80211_master_open(struct net_device *dev)
763 {
764         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
765         struct ieee80211_sub_if_data *sdata;
766         int res = -EOPNOTSUPP;
767
768         read_lock(&local->sub_if_lock);
769         list_for_each_entry(sdata, &local->sub_if_list, list) {
770                 if (sdata->dev != dev && netif_running(sdata->dev)) {
771                         res = 0;
772                         break;
773                 }
774         }
775         read_unlock(&local->sub_if_lock);
776         return res;
777 }
778
779 static int ieee80211_master_stop(struct net_device *dev)
780 {
781         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
782         struct ieee80211_sub_if_data *sdata;
783
784         read_lock(&local->sub_if_lock);
785         list_for_each_entry(sdata, &local->sub_if_list, list)
786                 if (sdata->dev != dev && netif_running(sdata->dev))
787                         dev_close(sdata->dev);
788         read_unlock(&local->sub_if_lock);
789
790         return 0;
791 }
792
793 static int ieee80211_mgmt_open(struct net_device *dev)
794 {
795         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
796
797         if (!netif_running(local->mdev))
798                 return -EOPNOTSUPP;
799         return 0;
800 }
801
802 static int ieee80211_mgmt_stop(struct net_device *dev)
803 {
804         return 0;
805 }
806
807 /* Check if running monitor interfaces should go to a "soft monitor" mode
808  * and switch them if necessary. */
809 static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
810 {
811         struct ieee80211_if_init_conf conf;
812
813         if (local->open_count && local->open_count == local->monitors &&
814             !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
815             local->ops->remove_interface) {
816                 conf.if_id = -1;
817                 conf.type = IEEE80211_IF_TYPE_MNTR;
818                 conf.mac_addr = NULL;
819                 local->ops->remove_interface(local_to_hw(local), &conf);
820         }
821 }
822
823 /* Check if running monitor interfaces should go to a "hard monitor" mode
824  * and switch them if necessary. */
825 static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
826 {
827         struct ieee80211_if_init_conf conf;
828
829         if (local->open_count && local->open_count == local->monitors &&
830             !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
831                 conf.if_id = -1;
832                 conf.type = IEEE80211_IF_TYPE_MNTR;
833                 conf.mac_addr = NULL;
834                 local->ops->add_interface(local_to_hw(local), &conf);
835         }
836 }
837
838 static int ieee80211_open(struct net_device *dev)
839 {
840         struct ieee80211_sub_if_data *sdata, *nsdata;
841         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
842         struct ieee80211_if_init_conf conf;
843         int res;
844
845         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
846         read_lock(&local->sub_if_lock);
847         list_for_each_entry(nsdata, &local->sub_if_list, list) {
848                 struct net_device *ndev = nsdata->dev;
849
850                 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
851                     compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
852                     !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
853                         read_unlock(&local->sub_if_lock);
854                         return -ENOTUNIQ;
855                 }
856         }
857         read_unlock(&local->sub_if_lock);
858
859         if (sdata->type == IEEE80211_IF_TYPE_WDS &&
860             is_zero_ether_addr(sdata->u.wds.remote_addr))
861                 return -ENOLINK;
862
863         if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
864             !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
865                 /* run the interface in a "soft monitor" mode */
866                 local->monitors++;
867                 local->open_count++;
868                 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
869                 return 0;
870         }
871         ieee80211_start_soft_monitor(local);
872
873         conf.if_id = dev->ifindex;
874         conf.type = sdata->type;
875         conf.mac_addr = dev->dev_addr;
876         res = local->ops->add_interface(local_to_hw(local), &conf);
877         if (res) {
878                 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
879                         ieee80211_start_hard_monitor(local);
880                 return res;
881         }
882
883         if (local->open_count == 0) {
884                 res = 0;
885                 tasklet_enable(&local->tx_pending_tasklet);
886                 tasklet_enable(&local->tasklet);
887                 if (local->ops->open)
888                         res = local->ops->open(local_to_hw(local));
889                 if (res == 0) {
890                         res = dev_open(local->mdev);
891                         if (res) {
892                                 if (local->ops->stop)
893                                         local->ops->stop(local_to_hw(local));
894                         } else {
895                                 res = ieee80211_hw_config(local);
896                                 if (res && local->ops->stop)
897                                         local->ops->stop(local_to_hw(local));
898                                 else if (!res && local->apdev)
899                                         dev_open(local->apdev);
900                         }
901                 }
902                 if (res) {
903                         if (local->ops->remove_interface)
904                                 local->ops->remove_interface(local_to_hw(local),
905                                                             &conf);
906                         return res;
907                 }
908         }
909         local->open_count++;
910
911         if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
912                 local->monitors++;
913                 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
914         } else
915                 ieee80211_if_config(dev);
916
917         if (sdata->type == IEEE80211_IF_TYPE_STA &&
918             !local->user_space_mlme)
919                 netif_carrier_off(dev);
920         else
921                 netif_carrier_on(dev);
922
923         netif_start_queue(dev);
924         return 0;
925 }
926
927
928 static int ieee80211_stop(struct net_device *dev)
929 {
930         struct ieee80211_sub_if_data *sdata;
931         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
932
933         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
934
935         if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
936             local->open_count > 1 &&
937             !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
938                 /* remove "soft monitor" interface */
939                 local->open_count--;
940                 local->monitors--;
941                 if (!local->monitors)
942                         local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
943                 return 0;
944         }
945
946         netif_stop_queue(dev);
947         ieee80211_if_shutdown(dev);
948
949         if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
950                 local->monitors--;
951                 if (!local->monitors)
952                         local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
953         }
954
955         local->open_count--;
956         if (local->open_count == 0) {
957                 if (netif_running(local->mdev))
958                         dev_close(local->mdev);
959                 if (local->apdev)
960                         dev_close(local->apdev);
961                 if (local->ops->stop)
962                         local->ops->stop(local_to_hw(local));
963                 tasklet_disable(&local->tx_pending_tasklet);
964                 tasklet_disable(&local->tasklet);
965         }
966         if (local->ops->remove_interface) {
967                 struct ieee80211_if_init_conf conf;
968
969                 conf.if_id = dev->ifindex;
970                 conf.type = sdata->type;
971                 conf.mac_addr = dev->dev_addr;
972                 local->ops->remove_interface(local_to_hw(local), &conf);
973         }
974
975         ieee80211_start_hard_monitor(local);
976
977         return 0;
978 }
979
980
981 static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
982 {
983         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
984         return ETH_ALEN;
985 }
986
987 struct ieee80211_rate *
988 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
989 {
990         struct ieee80211_hw_mode *mode;
991         int r;
992
993         list_for_each_entry(mode, &local->modes_list, list) {
994                 if (mode->mode != phymode)
995                         continue;
996                 for (r = 0; r < mode->num_rates; r++) {
997                         struct ieee80211_rate *rate = &mode->rates[r];
998                         if (rate->val == hw_rate ||
999                             (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
1000                              rate->val2 == hw_rate))
1001                                 return rate;
1002                 }
1003         }
1004
1005         return NULL;
1006 }
1007
1008 static void
1009 ieee80211_fill_frame_info(struct ieee80211_local *local,
1010                           struct ieee80211_frame_info *fi,
1011                           struct ieee80211_rx_status *status)
1012 {
1013         if (status) {
1014                 struct timespec ts;
1015                 struct ieee80211_rate *rate;
1016
1017                 jiffies_to_timespec(jiffies, &ts);
1018                 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
1019                                            ts.tv_nsec / 1000);
1020                 fi->mactime = cpu_to_be64(status->mactime);
1021                 switch (status->phymode) {
1022                 case MODE_IEEE80211A:
1023                         fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
1024                         break;
1025                 case MODE_IEEE80211B:
1026                         fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
1027                         break;
1028                 case MODE_IEEE80211G:
1029                         fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
1030                         break;
1031                 case MODE_ATHEROS_TURBO:
1032                         fi->phytype =
1033                                 htonl(ieee80211_phytype_dsss_dot11_turbo);
1034                         break;
1035                 default:
1036                         fi->phytype = htonl(0xAAAAAAAA);
1037                         break;
1038                 }
1039                 fi->channel = htonl(status->channel);
1040                 rate = ieee80211_get_rate(local, status->phymode,
1041                                           status->rate);
1042                 if (rate) {
1043                         fi->datarate = htonl(rate->rate);
1044                         if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
1045                                 if (status->rate == rate->val)
1046                                         fi->preamble = htonl(2); /* long */
1047                                 else if (status->rate == rate->val2)
1048                                         fi->preamble = htonl(1); /* short */
1049                         } else
1050                                 fi->preamble = htonl(0);
1051                 } else {
1052                         fi->datarate = htonl(0);
1053                         fi->preamble = htonl(0);
1054                 }
1055
1056                 fi->antenna = htonl(status->antenna);
1057                 fi->priority = htonl(0xffffffff); /* no clue */
1058                 fi->ssi_type = htonl(ieee80211_ssi_raw);
1059                 fi->ssi_signal = htonl(status->ssi);
1060                 fi->ssi_noise = 0x00000000;
1061                 fi->encoding = 0;
1062         } else {
1063                 /* clear everything because we really don't know.
1064                  * the msg_type field isn't present on monitor frames
1065                  * so we don't know whether it will be present or not,
1066                  * but it's ok to not clear it since it'll be assigned
1067                  * anyway */
1068                 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
1069
1070                 fi->ssi_type = htonl(ieee80211_ssi_none);
1071         }
1072         fi->version = htonl(IEEE80211_FI_VERSION);
1073         fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
1074 }
1075
1076 /* this routine is actually not just for this, but also
1077  * for pushing fake 'management' frames into userspace.
1078  * it shall be replaced by a netlink-based system. */
1079 void
1080 ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
1081                   struct ieee80211_rx_status *status, u32 msg_type)
1082 {
1083         struct ieee80211_frame_info *fi;
1084         const size_t hlen = sizeof(struct ieee80211_frame_info);
1085         struct ieee80211_sub_if_data *sdata;
1086
1087         skb->dev = local->apdev;
1088
1089         sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
1090
1091         if (skb_headroom(skb) < hlen) {
1092                 I802_DEBUG_INC(local->rx_expand_skb_head);
1093                 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
1094                         dev_kfree_skb(skb);
1095                         return;
1096                 }
1097         }
1098
1099         fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
1100
1101         ieee80211_fill_frame_info(local, fi, status);
1102         fi->msg_type = htonl(msg_type);
1103
1104         sdata->stats.rx_packets++;
1105         sdata->stats.rx_bytes += skb->len;
1106
1107         skb_set_mac_header(skb, 0);
1108         skb->ip_summed = CHECKSUM_UNNECESSARY;
1109         skb->pkt_type = PACKET_OTHERHOST;
1110         skb->protocol = htons(ETH_P_802_2);
1111         memset(skb->cb, 0, sizeof(skb->cb));
1112         netif_rx(skb);
1113 }
1114
1115 int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
1116                            int radar, int radar_type)
1117 {
1118         struct sk_buff *skb;
1119         struct ieee80211_radar_info *msg;
1120         struct ieee80211_local *local = hw_to_local(hw);
1121
1122         if (!local->apdev)
1123                 return 0;
1124
1125         skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
1126                             sizeof(struct ieee80211_radar_info));
1127
1128         if (!skb)
1129                 return -ENOMEM;
1130         skb_reserve(skb, sizeof(struct ieee80211_frame_info));
1131
1132         msg = (struct ieee80211_radar_info *)
1133                 skb_put(skb, sizeof(struct ieee80211_radar_info));
1134         msg->channel = channel;
1135         msg->radar = radar;
1136         msg->radar_type = radar_type;
1137
1138         ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
1139         return 0;
1140 }
1141 EXPORT_SYMBOL(ieee80211_radar_status);
1142
1143
1144 static void ieee80211_stat_refresh(unsigned long data)
1145 {
1146         struct ieee80211_local *local = (struct ieee80211_local *) data;
1147         struct sta_info *sta;
1148         struct ieee80211_sub_if_data *sdata;
1149
1150         if (!local->stat_time)
1151                 return;
1152
1153         /* go through all stations */
1154         spin_lock_bh(&local->sta_lock);
1155         list_for_each_entry(sta, &local->sta_list, list) {
1156                 sta->channel_use = (sta->channel_use_raw / local->stat_time) /
1157                         CHAN_UTIL_PER_10MS;
1158                 sta->channel_use_raw = 0;
1159         }
1160         spin_unlock_bh(&local->sta_lock);
1161
1162         /* go through all subinterfaces */
1163         read_lock(&local->sub_if_lock);
1164         list_for_each_entry(sdata, &local->sub_if_list, list) {
1165                 sdata->channel_use = (sdata->channel_use_raw /
1166                                       local->stat_time) / CHAN_UTIL_PER_10MS;
1167                 sdata->channel_use_raw = 0;
1168         }
1169         read_unlock(&local->sub_if_lock);
1170
1171         /* hardware interface */
1172         local->channel_use = (local->channel_use_raw /
1173                               local->stat_time) / CHAN_UTIL_PER_10MS;
1174         local->channel_use_raw = 0;
1175
1176         local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
1177         add_timer(&local->stat_timer);
1178 }
1179
1180
1181 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1182                                  struct sk_buff *skb,
1183                                  struct ieee80211_tx_status *status)
1184 {
1185         struct ieee80211_local *local = hw_to_local(hw);
1186         struct ieee80211_tx_status *saved;
1187         int tmp;
1188
1189         skb->dev = local->mdev;
1190         saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1191         if (unlikely(!saved)) {
1192                 if (net_ratelimit())
1193                         printk(KERN_WARNING "%s: Not enough memory, "
1194                                "dropping tx status", skb->dev->name);
1195                 /* should be dev_kfree_skb_irq, but due to this function being
1196                  * named _irqsafe instead of just _irq we can't be sure that
1197                  * people won't call it from non-irq contexts */
1198                 dev_kfree_skb_any(skb);
1199                 return;
1200         }
1201         memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1202         /* copy pointer to saved status into skb->cb for use by tasklet */
1203         memcpy(skb->cb, &saved, sizeof(saved));
1204
1205         skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1206         skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1207                        &local->skb_queue : &local->skb_queue_unreliable, skb);
1208         tmp = skb_queue_len(&local->skb_queue) +
1209                 skb_queue_len(&local->skb_queue_unreliable);
1210         while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1211                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1212                 memcpy(&saved, skb->cb, sizeof(saved));
1213                 kfree(saved);
1214                 dev_kfree_skb_irq(skb);
1215                 tmp--;
1216                 I802_DEBUG_INC(local->tx_status_drop);
1217         }
1218         tasklet_schedule(&local->tasklet);
1219 }
1220 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1221
1222 static void ieee80211_tasklet_handler(unsigned long data)
1223 {
1224         struct ieee80211_local *local = (struct ieee80211_local *) data;
1225         struct sk_buff *skb;
1226         struct ieee80211_rx_status rx_status;
1227         struct ieee80211_tx_status *tx_status;
1228
1229         while ((skb = skb_dequeue(&local->skb_queue)) ||
1230                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1231                 switch (skb->pkt_type) {
1232                 case IEEE80211_RX_MSG:
1233                         /* status is in skb->cb */
1234                         memcpy(&rx_status, skb->cb, sizeof(rx_status));
1235                         /* Clear skb->type in order to not confuse kernel
1236                          * netstack. */
1237                         skb->pkt_type = 0;
1238                         __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1239                         break;
1240                 case IEEE80211_TX_STATUS_MSG:
1241                         /* get pointer to saved status out of skb->cb */
1242                         memcpy(&tx_status, skb->cb, sizeof(tx_status));
1243                         skb->pkt_type = 0;
1244                         ieee80211_tx_status(local_to_hw(local),
1245                                             skb, tx_status);
1246                         kfree(tx_status);
1247                         break;
1248                 default: /* should never get here! */
1249                         printk(KERN_ERR "%s: Unknown message type (%d)\n",
1250                                local->mdev->name, skb->pkt_type);
1251                         dev_kfree_skb(skb);
1252                         break;
1253                 }
1254         }
1255 }
1256
1257
1258 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1259  * make a prepared TX frame (one that has been given to hw) to look like brand
1260  * new IEEE 802.11 frame that is ready to go through TX processing again.
1261  * Also, tx_packet_data in cb is restored from tx_control. */
1262 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1263                                       struct ieee80211_key *key,
1264                                       struct sk_buff *skb,
1265                                       struct ieee80211_tx_control *control)
1266 {
1267         int hdrlen, iv_len, mic_len;
1268         struct ieee80211_tx_packet_data *pkt_data;
1269
1270         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1271         pkt_data->ifindex = control->ifindex;
1272         pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
1273         pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
1274         pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
1275         pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
1276         pkt_data->queue = control->queue;
1277
1278         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1279
1280         if (!key)
1281                 goto no_key;
1282
1283         switch (key->alg) {
1284         case ALG_WEP:
1285                 iv_len = WEP_IV_LEN;
1286                 mic_len = WEP_ICV_LEN;
1287                 break;
1288         case ALG_TKIP:
1289                 iv_len = TKIP_IV_LEN;
1290                 mic_len = TKIP_ICV_LEN;
1291                 break;
1292         case ALG_CCMP:
1293                 iv_len = CCMP_HDR_LEN;
1294                 mic_len = CCMP_MIC_LEN;
1295                 break;
1296         default:
1297                 goto no_key;
1298         }
1299
1300         if (skb->len >= mic_len && key->force_sw_encrypt)
1301                 skb_trim(skb, skb->len - mic_len);
1302         if (skb->len >= iv_len && skb->len > hdrlen) {
1303                 memmove(skb->data + iv_len, skb->data, hdrlen);
1304                 skb_pull(skb, iv_len);
1305         }
1306
1307 no_key:
1308         {
1309                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1310                 u16 fc = le16_to_cpu(hdr->frame_control);
1311                 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1312                         fc &= ~IEEE80211_STYPE_QOS_DATA;
1313                         hdr->frame_control = cpu_to_le16(fc);
1314                         memmove(skb->data + 2, skb->data, hdrlen - 2);
1315                         skb_pull(skb, 2);
1316                 }
1317         }
1318 }
1319
1320
1321 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1322                          struct ieee80211_tx_status *status)
1323 {
1324         struct sk_buff *skb2;
1325         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1326         struct ieee80211_local *local = hw_to_local(hw);
1327         u16 frag, type;
1328         u32 msg_type;
1329         struct ieee80211_tx_status_rtap_hdr *rthdr;
1330         struct ieee80211_sub_if_data *sdata;
1331         int monitors;
1332
1333         if (!status) {
1334                 printk(KERN_ERR
1335                        "%s: ieee80211_tx_status called with NULL status\n",
1336                        local->mdev->name);
1337                 dev_kfree_skb(skb);
1338                 return;
1339         }
1340
1341         if (status->excessive_retries) {
1342                 struct sta_info *sta;
1343                 sta = sta_info_get(local, hdr->addr1);
1344                 if (sta) {
1345                         if (sta->flags & WLAN_STA_PS) {
1346                                 /* The STA is in power save mode, so assume
1347                                  * that this TX packet failed because of that.
1348                                  */
1349                                 status->excessive_retries = 0;
1350                                 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
1351                         }
1352                         sta_info_put(sta);
1353                 }
1354         }
1355
1356         if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1357                 struct sta_info *sta;
1358                 sta = sta_info_get(local, hdr->addr1);
1359                 if (sta) {
1360                         sta->tx_filtered_count++;
1361
1362                         /* Clear the TX filter mask for this STA when sending
1363                          * the next packet. If the STA went to power save mode,
1364                          * this will happen when it is waking up for the next
1365                          * time. */
1366                         sta->clear_dst_mask = 1;
1367
1368                         /* TODO: Is the WLAN_STA_PS flag always set here or is
1369                          * the race between RX and TX status causing some
1370                          * packets to be filtered out before 80211.o gets an
1371                          * update for PS status? This seems to be the case, so
1372                          * no changes are likely to be needed. */
1373                         if (sta->flags & WLAN_STA_PS &&
1374                             skb_queue_len(&sta->tx_filtered) <
1375                             STA_MAX_TX_BUFFER) {
1376                                 ieee80211_remove_tx_extra(local, sta->key,
1377                                                           skb,
1378                                                           &status->control);
1379                                 skb_queue_tail(&sta->tx_filtered, skb);
1380                         } else if (!(sta->flags & WLAN_STA_PS) &&
1381                                    !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1382                                 /* Software retry the packet once */
1383                                 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1384                                 ieee80211_remove_tx_extra(local, sta->key,
1385                                                           skb,
1386                                                           &status->control);
1387                                 dev_queue_xmit(skb);
1388                         } else {
1389                                 if (net_ratelimit()) {
1390                                         printk(KERN_DEBUG "%s: dropped TX "
1391                                                "filtered frame queue_len=%d "
1392                                                "PS=%d @%lu\n",
1393                                                local->mdev->name,
1394                                                skb_queue_len(
1395                                                        &sta->tx_filtered),
1396                                                !!(sta->flags & WLAN_STA_PS),
1397                                                jiffies);
1398                                 }
1399                                 dev_kfree_skb(skb);
1400                         }
1401                         sta_info_put(sta);
1402                         return;
1403                 }
1404         } else {
1405                 /* FIXME: STUPID to call this with both local and local->mdev */
1406                 rate_control_tx_status(local, local->mdev, skb, status);
1407         }
1408
1409         ieee80211_led_tx(local, 0);
1410
1411         /* SNMP counters
1412          * Fragments are passed to low-level drivers as separate skbs, so these
1413          * are actually fragments, not frames. Update frame counters only for
1414          * the first fragment of the frame. */
1415
1416         frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1417         type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1418
1419         if (status->flags & IEEE80211_TX_STATUS_ACK) {
1420                 if (frag == 0) {
1421                         local->dot11TransmittedFrameCount++;
1422                         if (is_multicast_ether_addr(hdr->addr1))
1423                                 local->dot11MulticastTransmittedFrameCount++;
1424                         if (status->retry_count > 0)
1425                                 local->dot11RetryCount++;
1426                         if (status->retry_count > 1)
1427                                 local->dot11MultipleRetryCount++;
1428                 }
1429
1430                 /* This counter shall be incremented for an acknowledged MPDU
1431                  * with an individual address in the address 1 field or an MPDU
1432                  * with a multicast address in the address 1 field of type Data
1433                  * or Management. */
1434                 if (!is_multicast_ether_addr(hdr->addr1) ||
1435                     type == IEEE80211_FTYPE_DATA ||
1436                     type == IEEE80211_FTYPE_MGMT)
1437                         local->dot11TransmittedFragmentCount++;
1438         } else {
1439                 if (frag == 0)
1440                         local->dot11FailedCount++;
1441         }
1442
1443         msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
1444                 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
1445
1446         /* this was a transmitted frame, but now we want to reuse it */
1447         skb_orphan(skb);
1448
1449         if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
1450             local->apdev) {
1451                 if (local->monitors) {
1452                         skb2 = skb_clone(skb, GFP_ATOMIC);
1453                 } else {
1454                         skb2 = skb;
1455                         skb = NULL;
1456                 }
1457
1458                 if (skb2)
1459                         /* Send frame to hostapd */
1460                         ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
1461
1462                 if (!skb)
1463                         return;
1464         }
1465
1466         if (!local->monitors) {
1467                 dev_kfree_skb(skb);
1468                 return;
1469         }
1470
1471         /* send frame to monitor interfaces now */
1472
1473         if (skb_headroom(skb) < sizeof(*rthdr)) {
1474                 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1475                 dev_kfree_skb(skb);
1476                 return;
1477         }
1478
1479         rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1480                                 skb_push(skb, sizeof(*rthdr));
1481
1482         memset(rthdr, 0, sizeof(*rthdr));
1483         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1484         rthdr->hdr.it_present =
1485                 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1486                             (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1487
1488         if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1489             !is_multicast_ether_addr(hdr->addr1))
1490                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1491
1492         if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1493             (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1494                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1495         else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1496                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1497
1498         rthdr->data_retries = status->retry_count;
1499
1500         read_lock(&local->sub_if_lock);
1501         monitors = local->monitors;
1502         list_for_each_entry(sdata, &local->sub_if_list, list) {
1503                 /*
1504                  * Using the monitors counter is possibly racy, but
1505                  * if the value is wrong we simply either clone the skb
1506                  * once too much or forget sending it to one monitor iface
1507                  * The latter case isn't nice but fixing the race is much
1508                  * more complicated.
1509                  */
1510                 if (!monitors || !skb)
1511                         goto out;
1512
1513                 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
1514                         if (!netif_running(sdata->dev))
1515                                 continue;
1516                         monitors--;
1517                         if (monitors)
1518                                 skb2 = skb_clone(skb, GFP_KERNEL);
1519                         else
1520                                 skb2 = NULL;
1521                         skb->dev = sdata->dev;
1522                         /* XXX: is this sufficient for BPF? */
1523                         skb_set_mac_header(skb, 0);
1524                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1525                         skb->pkt_type = PACKET_OTHERHOST;
1526                         skb->protocol = htons(ETH_P_802_2);
1527                         memset(skb->cb, 0, sizeof(skb->cb));
1528                         netif_rx(skb);
1529                         skb = skb2;
1530                 }
1531         }
1532  out:
1533         read_unlock(&local->sub_if_lock);
1534         if (skb)
1535                 dev_kfree_skb(skb);
1536 }
1537 EXPORT_SYMBOL(ieee80211_tx_status);
1538
1539
1540 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
1541 {
1542         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1543         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1544         struct sta_info *sta;
1545
1546         if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
1547                 return 0;
1548
1549         /* Create STA entry for the new peer */
1550         sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
1551         if (!sta)
1552                 return -ENOMEM;
1553         sta_info_put(sta);
1554
1555         /* Remove STA entry for the old peer */
1556         sta = sta_info_get(local, sdata->u.wds.remote_addr);
1557         if (sta) {
1558                 sta_info_put(sta);
1559                 sta_info_free(sta, 0);
1560         } else {
1561                 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
1562                        "peer " MAC_FMT "\n",
1563                        dev->name, MAC_ARG(sdata->u.wds.remote_addr));
1564         }
1565
1566         /* Update WDS link data */
1567         memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
1568
1569         return 0;
1570 }
1571
1572 /* Must not be called for mdev and apdev */
1573 void ieee80211_if_setup(struct net_device *dev)
1574 {
1575         ether_setup(dev);
1576         dev->hard_start_xmit = ieee80211_subif_start_xmit;
1577         dev->wireless_handlers = &ieee80211_iw_handler_def;
1578         dev->set_multicast_list = ieee80211_set_multicast_list;
1579         dev->change_mtu = ieee80211_change_mtu;
1580         dev->get_stats = ieee80211_get_stats;
1581         dev->open = ieee80211_open;
1582         dev->stop = ieee80211_stop;
1583         dev->uninit = ieee80211_if_reinit;
1584         dev->destructor = ieee80211_if_free;
1585 }
1586
1587 void ieee80211_if_mgmt_setup(struct net_device *dev)
1588 {
1589         ether_setup(dev);
1590         dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
1591         dev->change_mtu = ieee80211_change_mtu_apdev;
1592         dev->get_stats = ieee80211_get_stats;
1593         dev->open = ieee80211_mgmt_open;
1594         dev->stop = ieee80211_mgmt_stop;
1595         dev->type = ARPHRD_IEEE80211_PRISM;
1596         dev->hard_header_parse = header_parse_80211;
1597         dev->uninit = ieee80211_if_reinit;
1598         dev->destructor = ieee80211_if_free;
1599 }
1600
1601 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
1602                                  const char *name)
1603 {
1604         struct rate_control_ref *ref, *old;
1605
1606         ASSERT_RTNL();
1607         if (local->open_count || netif_running(local->mdev) ||
1608             (local->apdev && netif_running(local->apdev)))
1609                 return -EBUSY;
1610
1611         ref = rate_control_alloc(name, local);
1612         if (!ref) {
1613                 printk(KERN_WARNING "%s: Failed to select rate control "
1614                        "algorithm\n", local->mdev->name);
1615                 return -ENOENT;
1616         }
1617
1618         old = local->rate_ctrl;
1619         local->rate_ctrl = ref;
1620         if (old) {
1621                 rate_control_put(old);
1622                 sta_info_flush(local, NULL);
1623         }
1624
1625         printk(KERN_DEBUG "%s: Selected rate control "
1626                "algorithm '%s'\n", local->mdev->name,
1627                ref->ops->name);
1628
1629
1630         return 0;
1631 }
1632
1633 static void rate_control_deinitialize(struct ieee80211_local *local)
1634 {
1635         struct rate_control_ref *ref;
1636
1637         ref = local->rate_ctrl;
1638         local->rate_ctrl = NULL;
1639         rate_control_put(ref);
1640 }
1641
1642 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1643                                         const struct ieee80211_ops *ops)
1644 {
1645         struct net_device *mdev;
1646         struct ieee80211_local *local;
1647         struct ieee80211_sub_if_data *sdata;
1648         int priv_size;
1649         struct wiphy *wiphy;
1650
1651         /* Ensure 32-byte alignment of our private data and hw private data.
1652          * We use the wiphy priv data for both our ieee80211_local and for
1653          * the driver's private data
1654          *
1655          * In memory it'll be like this:
1656          *
1657          * +-------------------------+
1658          * | struct wiphy           |
1659          * +-------------------------+
1660          * | struct ieee80211_local  |
1661          * +-------------------------+
1662          * | driver's private data   |
1663          * +-------------------------+
1664          *
1665          */
1666         priv_size = ((sizeof(struct ieee80211_local) +
1667                       NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1668                     priv_data_len;
1669
1670         wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1671
1672         if (!wiphy)
1673                 return NULL;
1674
1675         wiphy->privid = mac80211_wiphy_privid;
1676
1677         local = wiphy_priv(wiphy);
1678         local->hw.wiphy = wiphy;
1679
1680         local->hw.priv = (char *)local +
1681                          ((sizeof(struct ieee80211_local) +
1682                            NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1683
1684         BUG_ON(!ops->tx);
1685         BUG_ON(!ops->config);
1686         BUG_ON(!ops->add_interface);
1687         local->ops = ops;
1688
1689         /* for now, mdev needs sub_if_data :/ */
1690         mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1691                             "wmaster%d", ether_setup);
1692         if (!mdev) {
1693                 wiphy_free(wiphy);
1694                 return NULL;
1695         }
1696
1697         sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1698         mdev->ieee80211_ptr = &sdata->wdev;
1699         sdata->wdev.wiphy = wiphy;
1700
1701         local->hw.queues = 1; /* default */
1702
1703         local->mdev = mdev;
1704         local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1705         local->rx_handlers = ieee80211_rx_handlers;
1706         local->tx_handlers = ieee80211_tx_handlers;
1707
1708         local->bridge_packets = 1;
1709
1710         local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1711         local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1712         local->short_retry_limit = 7;
1713         local->long_retry_limit = 4;
1714         local->hw.conf.radio_enabled = 1;
1715
1716         local->enabled_modes = (unsigned int) -1;
1717
1718         INIT_LIST_HEAD(&local->modes_list);
1719
1720         rwlock_init(&local->sub_if_lock);
1721         INIT_LIST_HEAD(&local->sub_if_list);
1722
1723         INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1724         init_timer(&local->stat_timer);
1725         local->stat_timer.function = ieee80211_stat_refresh;
1726         local->stat_timer.data = (unsigned long) local;
1727         ieee80211_rx_bss_list_init(mdev);
1728
1729         sta_info_init(local);
1730
1731         mdev->hard_start_xmit = ieee80211_master_start_xmit;
1732         mdev->open = ieee80211_master_open;
1733         mdev->stop = ieee80211_master_stop;
1734         mdev->type = ARPHRD_IEEE80211;
1735         mdev->hard_header_parse = header_parse_80211;
1736
1737         sdata->type = IEEE80211_IF_TYPE_AP;
1738         sdata->dev = mdev;
1739         sdata->local = local;
1740         sdata->u.ap.force_unicast_rateidx = -1;
1741         sdata->u.ap.max_ratectrl_rateidx = -1;
1742         ieee80211_if_sdata_init(sdata);
1743         list_add_tail(&sdata->list, &local->sub_if_list);
1744
1745         tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1746                      (unsigned long)local);
1747         tasklet_disable(&local->tx_pending_tasklet);
1748
1749         tasklet_init(&local->tasklet,
1750                      ieee80211_tasklet_handler,
1751                      (unsigned long) local);
1752         tasklet_disable(&local->tasklet);
1753
1754         skb_queue_head_init(&local->skb_queue);
1755         skb_queue_head_init(&local->skb_queue_unreliable);
1756
1757         return local_to_hw(local);
1758 }
1759 EXPORT_SYMBOL(ieee80211_alloc_hw);
1760
1761 int ieee80211_register_hw(struct ieee80211_hw *hw)
1762 {
1763         struct ieee80211_local *local = hw_to_local(hw);
1764         const char *name;
1765         int result;
1766
1767         result = wiphy_register(local->hw.wiphy);
1768         if (result < 0)
1769                 return result;
1770
1771         name = wiphy_dev(local->hw.wiphy)->driver->name;
1772         local->hw.workqueue = create_singlethread_workqueue(name);
1773         if (!local->hw.workqueue) {
1774                 result = -ENOMEM;
1775                 goto fail_workqueue;
1776         }
1777
1778         /*
1779          * The hardware needs headroom for sending the frame,
1780          * and we need some headroom for passing the frame to monitor
1781          * interfaces, but never both at the same time.
1782          */
1783         local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1784                                    sizeof(struct ieee80211_tx_status_rtap_hdr));
1785
1786         debugfs_hw_add(local);
1787
1788         local->hw.conf.beacon_int = 1000;
1789
1790         local->wstats_flags |= local->hw.max_rssi ?
1791                                IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1792         local->wstats_flags |= local->hw.max_signal ?
1793                                IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1794         local->wstats_flags |= local->hw.max_noise ?
1795                                IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1796         if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1797                 local->wstats_flags |= IW_QUAL_DBM;
1798
1799         result = sta_info_start(local);
1800         if (result < 0)
1801                 goto fail_sta_info;
1802
1803         rtnl_lock();
1804         result = dev_alloc_name(local->mdev, local->mdev->name);
1805         if (result < 0)
1806                 goto fail_dev;
1807
1808         memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1809         SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1810
1811         result = register_netdevice(local->mdev);
1812         if (result < 0)
1813                 goto fail_dev;
1814
1815         ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1816
1817         result = ieee80211_init_rate_ctrl_alg(local, NULL);
1818         if (result < 0) {
1819                 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1820                        "algorithm\n", local->mdev->name);
1821                 goto fail_rate;
1822         }
1823
1824         result = ieee80211_wep_init(local);
1825
1826         if (result < 0) {
1827                 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1828                        local->mdev->name);
1829                 goto fail_wep;
1830         }
1831
1832         ieee80211_install_qdisc(local->mdev);
1833
1834         /* add one default STA interface */
1835         result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1836                                   IEEE80211_IF_TYPE_STA);
1837         if (result)
1838                 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1839                        local->mdev->name);
1840
1841         local->reg_state = IEEE80211_DEV_REGISTERED;
1842         rtnl_unlock();
1843
1844         ieee80211_led_init(local);
1845
1846         return 0;
1847
1848 fail_wep:
1849         rate_control_deinitialize(local);
1850 fail_rate:
1851         ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1852         unregister_netdevice(local->mdev);
1853 fail_dev:
1854         rtnl_unlock();
1855         sta_info_stop(local);
1856 fail_sta_info:
1857         debugfs_hw_del(local);
1858         destroy_workqueue(local->hw.workqueue);
1859 fail_workqueue:
1860         wiphy_unregister(local->hw.wiphy);
1861         return result;
1862 }
1863 EXPORT_SYMBOL(ieee80211_register_hw);
1864
1865 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1866                               struct ieee80211_hw_mode *mode)
1867 {
1868         struct ieee80211_local *local = hw_to_local(hw);
1869         struct ieee80211_rate *rate;
1870         int i;
1871
1872         INIT_LIST_HEAD(&mode->list);
1873         list_add_tail(&mode->list, &local->modes_list);
1874
1875         local->hw_modes |= (1 << mode->mode);
1876         for (i = 0; i < mode->num_rates; i++) {
1877                 rate = &(mode->rates[i]);
1878                 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1879         }
1880         ieee80211_prepare_rates(local, mode);
1881
1882         if (!local->oper_hw_mode) {
1883                 /* Default to this mode */
1884                 local->hw.conf.phymode = mode->mode;
1885                 local->oper_hw_mode = local->scan_hw_mode = mode;
1886                 local->oper_channel = local->scan_channel = &mode->channels[0];
1887                 local->hw.conf.mode = local->oper_hw_mode;
1888                 local->hw.conf.chan = local->oper_channel;
1889         }
1890
1891         if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1892                 ieee80211_set_default_regdomain(mode);
1893
1894         return 0;
1895 }
1896 EXPORT_SYMBOL(ieee80211_register_hwmode);
1897
1898 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1899 {
1900         struct ieee80211_local *local = hw_to_local(hw);
1901         struct ieee80211_sub_if_data *sdata, *tmp;
1902         struct list_head tmp_list;
1903         int i;
1904
1905         tasklet_kill(&local->tx_pending_tasklet);
1906         tasklet_kill(&local->tasklet);
1907
1908         rtnl_lock();
1909
1910         BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1911
1912         local->reg_state = IEEE80211_DEV_UNREGISTERED;
1913         if (local->apdev)
1914                 ieee80211_if_del_mgmt(local);
1915
1916         write_lock_bh(&local->sub_if_lock);
1917         list_replace_init(&local->sub_if_list, &tmp_list);
1918         write_unlock_bh(&local->sub_if_lock);
1919
1920         list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
1921                 __ieee80211_if_del(local, sdata);
1922
1923         rtnl_unlock();
1924
1925         if (local->stat_time)
1926                 del_timer_sync(&local->stat_timer);
1927
1928         ieee80211_rx_bss_list_deinit(local->mdev);
1929         ieee80211_clear_tx_pending(local);
1930         sta_info_stop(local);
1931         rate_control_deinitialize(local);
1932         debugfs_hw_del(local);
1933
1934         for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1935                 kfree(local->supp_rates[i]);
1936                 kfree(local->basic_rates[i]);
1937         }
1938
1939         if (skb_queue_len(&local->skb_queue)
1940                         || skb_queue_len(&local->skb_queue_unreliable))
1941                 printk(KERN_WARNING "%s: skb_queue not empty\n",
1942                        local->mdev->name);
1943         skb_queue_purge(&local->skb_queue);
1944         skb_queue_purge(&local->skb_queue_unreliable);
1945
1946         destroy_workqueue(local->hw.workqueue);
1947         wiphy_unregister(local->hw.wiphy);
1948         ieee80211_wep_free(local);
1949         ieee80211_led_exit(local);
1950 }
1951 EXPORT_SYMBOL(ieee80211_unregister_hw);
1952
1953 void ieee80211_free_hw(struct ieee80211_hw *hw)
1954 {
1955         struct ieee80211_local *local = hw_to_local(hw);
1956
1957         ieee80211_if_free(local->mdev);
1958         wiphy_free(local->hw.wiphy);
1959 }
1960 EXPORT_SYMBOL(ieee80211_free_hw);
1961
1962 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
1963 {
1964         struct ieee80211_local *local = hw_to_local(hw);
1965
1966         if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
1967                                &local->state[queue])) {
1968                 if (test_bit(IEEE80211_LINK_STATE_PENDING,
1969                              &local->state[queue]))
1970                         tasklet_schedule(&local->tx_pending_tasklet);
1971                 else
1972                         if (!ieee80211_qdisc_installed(local->mdev)) {
1973                                 if (queue == 0)
1974                                         netif_wake_queue(local->mdev);
1975                         } else
1976                                 __netif_schedule(local->mdev);
1977         }
1978 }
1979 EXPORT_SYMBOL(ieee80211_wake_queue);
1980
1981 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
1982 {
1983         struct ieee80211_local *local = hw_to_local(hw);
1984
1985         if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
1986                 netif_stop_queue(local->mdev);
1987         set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
1988 }
1989 EXPORT_SYMBOL(ieee80211_stop_queue);
1990
1991 void ieee80211_start_queues(struct ieee80211_hw *hw)
1992 {
1993         struct ieee80211_local *local = hw_to_local(hw);
1994         int i;
1995
1996         for (i = 0; i < local->hw.queues; i++)
1997                 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
1998         if (!ieee80211_qdisc_installed(local->mdev))
1999                 netif_start_queue(local->mdev);
2000 }
2001 EXPORT_SYMBOL(ieee80211_start_queues);
2002
2003 void ieee80211_stop_queues(struct ieee80211_hw *hw)
2004 {
2005         int i;
2006
2007         for (i = 0; i < hw->queues; i++)
2008                 ieee80211_stop_queue(hw, i);
2009 }
2010 EXPORT_SYMBOL(ieee80211_stop_queues);
2011
2012 void ieee80211_wake_queues(struct ieee80211_hw *hw)
2013 {
2014         int i;
2015
2016         for (i = 0; i < hw->queues; i++)
2017                 ieee80211_wake_queue(hw, i);
2018 }
2019 EXPORT_SYMBOL(ieee80211_wake_queues);
2020
2021 struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
2022 {
2023         struct ieee80211_sub_if_data *sdata;
2024         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2025         return &sdata->stats;
2026 }
2027
2028 static int __init ieee80211_init(void)
2029 {
2030         struct sk_buff *skb;
2031         int ret;
2032
2033         BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
2034
2035         ret = ieee80211_wme_register();
2036         if (ret) {
2037                 printk(KERN_DEBUG "ieee80211_init: failed to "
2038                        "initialize WME (err=%d)\n", ret);
2039                 return ret;
2040         }
2041
2042         ieee80211_debugfs_netdev_init();
2043         ieee80211_regdomain_init();
2044
2045         return 0;
2046 }
2047
2048
2049 static void __exit ieee80211_exit(void)
2050 {
2051         ieee80211_wme_unregister();
2052         ieee80211_debugfs_netdev_exit();
2053 }
2054
2055
2056 subsys_initcall(ieee80211_init);
2057 module_exit(ieee80211_exit);
2058
2059 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
2060 MODULE_LICENSE("GPL");