Merge branch 'linux-2.6'
[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/net_namespace.h>
25 #include <net/cfg80211.h>
26
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 "cfg.h"
34 #include "debugfs.h"
35 #include "debugfs_netdev.h"
36
37 /*
38  * For seeing transmitted packets on monitor interfaces
39  * we have a radiotap header too.
40  */
41 struct ieee80211_tx_status_rtap_hdr {
42         struct ieee80211_radiotap_header hdr;
43         __le16 tx_flags;
44         u8 data_retries;
45 } __attribute__ ((packed));
46
47 /* common interface routines */
48
49 static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
50 {
51         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
52         return ETH_ALEN;
53 }
54
55 /* must be called under mdev tx lock */
56 static void ieee80211_configure_filter(struct ieee80211_local *local)
57 {
58         unsigned int changed_flags;
59         unsigned int new_flags = 0;
60
61         if (atomic_read(&local->iff_promiscs))
62                 new_flags |= FIF_PROMISC_IN_BSS;
63
64         if (atomic_read(&local->iff_allmultis))
65                 new_flags |= FIF_ALLMULTI;
66
67         if (local->monitors)
68                 new_flags |= FIF_CONTROL |
69                              FIF_OTHER_BSS |
70                              FIF_BCN_PRBRESP_PROMISC;
71
72         changed_flags = local->filter_flags ^ new_flags;
73
74         /* be a bit nasty */
75         new_flags |= (1<<31);
76
77         local->ops->configure_filter(local_to_hw(local),
78                                      changed_flags, &new_flags,
79                                      local->mdev->mc_count,
80                                      local->mdev->mc_list);
81
82         WARN_ON(new_flags & (1<<31));
83
84         local->filter_flags = new_flags & ~(1<<31);
85 }
86
87 /* master interface */
88
89 static int ieee80211_master_open(struct net_device *dev)
90 {
91         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
92         struct ieee80211_sub_if_data *sdata;
93         int res = -EOPNOTSUPP;
94
95         /* we hold the RTNL here so can safely walk the list */
96         list_for_each_entry(sdata, &local->interfaces, list) {
97                 if (sdata->dev != dev && netif_running(sdata->dev)) {
98                         res = 0;
99                         break;
100                 }
101         }
102         return res;
103 }
104
105 static int ieee80211_master_stop(struct net_device *dev)
106 {
107         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
108         struct ieee80211_sub_if_data *sdata;
109
110         /* we hold the RTNL here so can safely walk the list */
111         list_for_each_entry(sdata, &local->interfaces, list)
112                 if (sdata->dev != dev && netif_running(sdata->dev))
113                         dev_close(sdata->dev);
114
115         return 0;
116 }
117
118 static void ieee80211_master_set_multicast_list(struct net_device *dev)
119 {
120         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
121
122         ieee80211_configure_filter(local);
123 }
124
125 /* regular interfaces */
126
127 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
128 {
129         /* FIX: what would be proper limits for MTU?
130          * This interface uses 802.3 frames. */
131         if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
132                 printk(KERN_WARNING "%s: invalid MTU %d\n",
133                        dev->name, new_mtu);
134                 return -EINVAL;
135         }
136
137 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
138         printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
139 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
140         dev->mtu = new_mtu;
141         return 0;
142 }
143
144 static inline int identical_mac_addr_allowed(int type1, int type2)
145 {
146         return (type1 == IEEE80211_IF_TYPE_MNTR ||
147                 type2 == IEEE80211_IF_TYPE_MNTR ||
148                 (type1 == IEEE80211_IF_TYPE_AP &&
149                  type2 == IEEE80211_IF_TYPE_WDS) ||
150                 (type1 == IEEE80211_IF_TYPE_WDS &&
151                  (type2 == IEEE80211_IF_TYPE_WDS ||
152                   type2 == IEEE80211_IF_TYPE_AP)) ||
153                 (type1 == IEEE80211_IF_TYPE_AP &&
154                  type2 == IEEE80211_IF_TYPE_VLAN) ||
155                 (type1 == IEEE80211_IF_TYPE_VLAN &&
156                  (type2 == IEEE80211_IF_TYPE_AP ||
157                   type2 == IEEE80211_IF_TYPE_VLAN)));
158 }
159
160 static int ieee80211_open(struct net_device *dev)
161 {
162         struct ieee80211_sub_if_data *sdata, *nsdata;
163         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
164         struct ieee80211_if_init_conf conf;
165         int res;
166
167         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
168
169         /* we hold the RTNL here so can safely walk the list */
170         list_for_each_entry(nsdata, &local->interfaces, list) {
171                 struct net_device *ndev = nsdata->dev;
172
173                 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
174                     compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
175                         /*
176                          * check whether it may have the same address
177                          */
178                         if (!identical_mac_addr_allowed(sdata->type,
179                                                         nsdata->type))
180                                 return -ENOTUNIQ;
181
182                         /*
183                          * can only add VLANs to enabled APs
184                          */
185                         if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
186                             nsdata->type == IEEE80211_IF_TYPE_AP &&
187                             netif_running(nsdata->dev))
188                                 sdata->u.vlan.ap = nsdata;
189                 }
190         }
191
192         switch (sdata->type) {
193         case IEEE80211_IF_TYPE_WDS:
194                 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
195                         return -ENOLINK;
196                 break;
197         case IEEE80211_IF_TYPE_VLAN:
198                 if (!sdata->u.vlan.ap)
199                         return -ENOLINK;
200                 break;
201         case IEEE80211_IF_TYPE_AP:
202         case IEEE80211_IF_TYPE_STA:
203         case IEEE80211_IF_TYPE_MNTR:
204         case IEEE80211_IF_TYPE_IBSS:
205                 /* no special treatment */
206                 break;
207         case IEEE80211_IF_TYPE_INVALID:
208                 /* cannot happen */
209                 WARN_ON(1);
210                 break;
211         }
212
213         if (local->open_count == 0) {
214                 res = 0;
215                 if (local->ops->start)
216                         res = local->ops->start(local_to_hw(local));
217                 if (res)
218                         return res;
219                 ieee80211_hw_config(local);
220         }
221
222         switch (sdata->type) {
223         case IEEE80211_IF_TYPE_VLAN:
224                 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
225                 /* no need to tell driver */
226                 break;
227         case IEEE80211_IF_TYPE_MNTR:
228                 /* must be before the call to ieee80211_configure_filter */
229                 local->monitors++;
230                 if (local->monitors == 1) {
231                         netif_tx_lock_bh(local->mdev);
232                         ieee80211_configure_filter(local);
233                         netif_tx_unlock_bh(local->mdev);
234
235                         local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
236                 }
237                 break;
238         case IEEE80211_IF_TYPE_STA:
239         case IEEE80211_IF_TYPE_IBSS:
240                 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
241                 /* fall through */
242         default:
243                 conf.if_id = dev->ifindex;
244                 conf.type = sdata->type;
245                 conf.mac_addr = dev->dev_addr;
246                 res = local->ops->add_interface(local_to_hw(local), &conf);
247                 if (res && !local->open_count && local->ops->stop)
248                         local->ops->stop(local_to_hw(local));
249                 if (res)
250                         return res;
251
252                 ieee80211_if_config(dev);
253                 ieee80211_reset_erp_info(dev);
254                 ieee80211_enable_keys(sdata);
255
256                 if (sdata->type == IEEE80211_IF_TYPE_STA &&
257                     !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
258                         netif_carrier_off(dev);
259                 else
260                         netif_carrier_on(dev);
261         }
262
263         if (local->open_count == 0) {
264                 res = dev_open(local->mdev);
265                 WARN_ON(res);
266                 tasklet_enable(&local->tx_pending_tasklet);
267                 tasklet_enable(&local->tasklet);
268         }
269
270         /*
271          * set_multicast_list will be invoked by the networking core
272          * which will check whether any increments here were done in
273          * error and sync them down to the hardware as filter flags.
274          */
275         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
276                 atomic_inc(&local->iff_allmultis);
277
278         if (sdata->flags & IEEE80211_SDATA_PROMISC)
279                 atomic_inc(&local->iff_promiscs);
280
281         local->open_count++;
282
283         netif_start_queue(dev);
284
285         return 0;
286 }
287
288 static int ieee80211_stop(struct net_device *dev)
289 {
290         struct ieee80211_sub_if_data *sdata;
291         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
292         struct ieee80211_if_init_conf conf;
293
294         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
295
296         netif_stop_queue(dev);
297
298         /*
299          * Don't count this interface for promisc/allmulti while it
300          * is down. dev_mc_unsync() will invoke set_multicast_list
301          * on the master interface which will sync these down to the
302          * hardware as filter flags.
303          */
304         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
305                 atomic_dec(&local->iff_allmultis);
306
307         if (sdata->flags & IEEE80211_SDATA_PROMISC)
308                 atomic_dec(&local->iff_promiscs);
309
310         dev_mc_unsync(local->mdev, dev);
311
312         /* down all dependent devices, that is VLANs */
313         if (sdata->type == IEEE80211_IF_TYPE_AP) {
314                 struct ieee80211_sub_if_data *vlan, *tmp;
315
316                 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
317                                          u.vlan.list)
318                         dev_close(vlan->dev);
319                 WARN_ON(!list_empty(&sdata->u.ap.vlans));
320         }
321
322         local->open_count--;
323
324         switch (sdata->type) {
325         case IEEE80211_IF_TYPE_VLAN:
326                 list_del(&sdata->u.vlan.list);
327                 sdata->u.vlan.ap = NULL;
328                 /* no need to tell driver */
329                 break;
330         case IEEE80211_IF_TYPE_MNTR:
331                 local->monitors--;
332                 if (local->monitors == 0) {
333                         netif_tx_lock_bh(local->mdev);
334                         ieee80211_configure_filter(local);
335                         netif_tx_unlock_bh(local->mdev);
336
337                         local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
338                 }
339                 break;
340         case IEEE80211_IF_TYPE_STA:
341         case IEEE80211_IF_TYPE_IBSS:
342                 sdata->u.sta.state = IEEE80211_DISABLED;
343                 del_timer_sync(&sdata->u.sta.timer);
344                 /*
345                  * When we get here, the interface is marked down.
346                  * Call synchronize_rcu() to wait for the RX path
347                  * should it be using the interface and enqueuing
348                  * frames at this very time on another CPU.
349                  */
350                 synchronize_rcu();
351                 skb_queue_purge(&sdata->u.sta.skb_queue);
352
353                 if (!local->ops->hw_scan &&
354                     local->scan_dev == sdata->dev) {
355                         local->sta_scanning = 0;
356                         cancel_delayed_work(&local->scan_work);
357                 }
358                 flush_workqueue(local->hw.workqueue);
359
360                 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
361                 kfree(sdata->u.sta.extra_ie);
362                 sdata->u.sta.extra_ie = NULL;
363                 sdata->u.sta.extra_ie_len = 0;
364                 /* fall through */
365         default:
366                 conf.if_id = dev->ifindex;
367                 conf.type = sdata->type;
368                 conf.mac_addr = dev->dev_addr;
369                 /* disable all keys for as long as this netdev is down */
370                 ieee80211_disable_keys(sdata);
371                 local->ops->remove_interface(local_to_hw(local), &conf);
372         }
373
374         if (local->open_count == 0) {
375                 if (netif_running(local->mdev))
376                         dev_close(local->mdev);
377
378                 if (local->ops->stop)
379                         local->ops->stop(local_to_hw(local));
380
381                 tasklet_disable(&local->tx_pending_tasklet);
382                 tasklet_disable(&local->tasklet);
383         }
384
385         return 0;
386 }
387
388 static void ieee80211_set_multicast_list(struct net_device *dev)
389 {
390         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
391         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
392         int allmulti, promisc, sdata_allmulti, sdata_promisc;
393
394         allmulti = !!(dev->flags & IFF_ALLMULTI);
395         promisc = !!(dev->flags & IFF_PROMISC);
396         sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
397         sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
398
399         if (allmulti != sdata_allmulti) {
400                 if (dev->flags & IFF_ALLMULTI)
401                         atomic_inc(&local->iff_allmultis);
402                 else
403                         atomic_dec(&local->iff_allmultis);
404                 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
405         }
406
407         if (promisc != sdata_promisc) {
408                 if (dev->flags & IFF_PROMISC)
409                         atomic_inc(&local->iff_promiscs);
410                 else
411                         atomic_dec(&local->iff_promiscs);
412                 sdata->flags ^= IEEE80211_SDATA_PROMISC;
413         }
414
415         dev_mc_sync(local->mdev, dev);
416 }
417
418 static const struct header_ops ieee80211_header_ops = {
419         .create         = eth_header,
420         .parse          = header_parse_80211,
421         .rebuild        = eth_rebuild_header,
422         .cache          = eth_header_cache,
423         .cache_update   = eth_header_cache_update,
424 };
425
426 /* Must not be called for mdev */
427 void ieee80211_if_setup(struct net_device *dev)
428 {
429         ether_setup(dev);
430         dev->header_ops = &ieee80211_header_ops;
431         dev->hard_start_xmit = ieee80211_subif_start_xmit;
432         dev->wireless_handlers = &ieee80211_iw_handler_def;
433         dev->set_multicast_list = ieee80211_set_multicast_list;
434         dev->change_mtu = ieee80211_change_mtu;
435         dev->open = ieee80211_open;
436         dev->stop = ieee80211_stop;
437         dev->destructor = ieee80211_if_free;
438 }
439
440 /* WDS specialties */
441
442 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
443 {
444         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
445         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
446         struct sta_info *sta;
447         DECLARE_MAC_BUF(mac);
448
449         if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
450                 return 0;
451
452         /* Create STA entry for the new peer */
453         sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
454         if (!sta)
455                 return -ENOMEM;
456         sta_info_put(sta);
457
458         /* Remove STA entry for the old peer */
459         sta = sta_info_get(local, sdata->u.wds.remote_addr);
460         if (sta) {
461                 sta_info_free(sta);
462                 sta_info_put(sta);
463         } else {
464                 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
465                        "peer %s\n",
466                        dev->name, print_mac(mac, sdata->u.wds.remote_addr));
467         }
468
469         /* Update WDS link data */
470         memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
471
472         return 0;
473 }
474
475 /* everything else */
476
477 static int __ieee80211_if_config(struct net_device *dev,
478                                  struct sk_buff *beacon,
479                                  struct ieee80211_tx_control *control)
480 {
481         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
482         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
483         struct ieee80211_if_conf conf;
484
485         if (!local->ops->config_interface || !netif_running(dev))
486                 return 0;
487
488         memset(&conf, 0, sizeof(conf));
489         conf.type = sdata->type;
490         if (sdata->type == IEEE80211_IF_TYPE_STA ||
491             sdata->type == IEEE80211_IF_TYPE_IBSS) {
492                 conf.bssid = sdata->u.sta.bssid;
493                 conf.ssid = sdata->u.sta.ssid;
494                 conf.ssid_len = sdata->u.sta.ssid_len;
495         } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
496                 conf.ssid = sdata->u.ap.ssid;
497                 conf.ssid_len = sdata->u.ap.ssid_len;
498                 conf.beacon = beacon;
499                 conf.beacon_control = control;
500         }
501         return local->ops->config_interface(local_to_hw(local),
502                                            dev->ifindex, &conf);
503 }
504
505 int ieee80211_if_config(struct net_device *dev)
506 {
507         return __ieee80211_if_config(dev, NULL, NULL);
508 }
509
510 int ieee80211_if_config_beacon(struct net_device *dev)
511 {
512         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
513         struct ieee80211_tx_control control;
514         struct sk_buff *skb;
515
516         if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
517                 return 0;
518         skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
519         if (!skb)
520                 return -ENOMEM;
521         return __ieee80211_if_config(dev, skb, &control);
522 }
523
524 int ieee80211_hw_config(struct ieee80211_local *local)
525 {
526         struct ieee80211_hw_mode *mode;
527         struct ieee80211_channel *chan;
528         int ret = 0;
529
530         if (local->sta_scanning) {
531                 chan = local->scan_channel;
532                 mode = local->scan_hw_mode;
533         } else {
534                 chan = local->oper_channel;
535                 mode = local->oper_hw_mode;
536         }
537
538         local->hw.conf.channel = chan->chan;
539         local->hw.conf.channel_val = chan->val;
540         if (!local->hw.conf.power_level) {
541                 local->hw.conf.power_level = chan->power_level;
542         } else {
543                 local->hw.conf.power_level = min(chan->power_level,
544                                                  local->hw.conf.power_level);
545         }
546         local->hw.conf.freq = chan->freq;
547         local->hw.conf.phymode = mode->mode;
548         local->hw.conf.antenna_max = chan->antenna_max;
549         local->hw.conf.chan = chan;
550         local->hw.conf.mode = mode;
551
552 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
553         printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
554                "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
555                local->hw.conf.phymode);
556 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
557
558         if (local->open_count)
559                 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
560
561         return ret;
562 }
563
564 void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
565 {
566         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
567         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
568         if (local->ops->erp_ie_changed)
569                 local->ops->erp_ie_changed(local_to_hw(local), changes,
570                         !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
571                         !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
572 }
573
574 void ieee80211_reset_erp_info(struct net_device *dev)
575 {
576         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
577
578         sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
579                         IEEE80211_SDATA_SHORT_PREAMBLE);
580         ieee80211_erp_info_change_notify(dev,
581                                          IEEE80211_ERP_CHANGE_PROTECTION |
582                                          IEEE80211_ERP_CHANGE_PREAMBLE);
583 }
584
585 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
586                                  struct sk_buff *skb,
587                                  struct ieee80211_tx_status *status)
588 {
589         struct ieee80211_local *local = hw_to_local(hw);
590         struct ieee80211_tx_status *saved;
591         int tmp;
592
593         skb->dev = local->mdev;
594         saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
595         if (unlikely(!saved)) {
596                 if (net_ratelimit())
597                         printk(KERN_WARNING "%s: Not enough memory, "
598                                "dropping tx status", skb->dev->name);
599                 /* should be dev_kfree_skb_irq, but due to this function being
600                  * named _irqsafe instead of just _irq we can't be sure that
601                  * people won't call it from non-irq contexts */
602                 dev_kfree_skb_any(skb);
603                 return;
604         }
605         memcpy(saved, status, sizeof(struct ieee80211_tx_status));
606         /* copy pointer to saved status into skb->cb for use by tasklet */
607         memcpy(skb->cb, &saved, sizeof(saved));
608
609         skb->pkt_type = IEEE80211_TX_STATUS_MSG;
610         skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
611                        &local->skb_queue : &local->skb_queue_unreliable, skb);
612         tmp = skb_queue_len(&local->skb_queue) +
613                 skb_queue_len(&local->skb_queue_unreliable);
614         while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
615                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
616                 memcpy(&saved, skb->cb, sizeof(saved));
617                 kfree(saved);
618                 dev_kfree_skb_irq(skb);
619                 tmp--;
620                 I802_DEBUG_INC(local->tx_status_drop);
621         }
622         tasklet_schedule(&local->tasklet);
623 }
624 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
625
626 static void ieee80211_tasklet_handler(unsigned long data)
627 {
628         struct ieee80211_local *local = (struct ieee80211_local *) data;
629         struct sk_buff *skb;
630         struct ieee80211_rx_status rx_status;
631         struct ieee80211_tx_status *tx_status;
632
633         while ((skb = skb_dequeue(&local->skb_queue)) ||
634                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
635                 switch (skb->pkt_type) {
636                 case IEEE80211_RX_MSG:
637                         /* status is in skb->cb */
638                         memcpy(&rx_status, skb->cb, sizeof(rx_status));
639                         /* Clear skb->type in order to not confuse kernel
640                          * netstack. */
641                         skb->pkt_type = 0;
642                         __ieee80211_rx(local_to_hw(local), skb, &rx_status);
643                         break;
644                 case IEEE80211_TX_STATUS_MSG:
645                         /* get pointer to saved status out of skb->cb */
646                         memcpy(&tx_status, skb->cb, sizeof(tx_status));
647                         skb->pkt_type = 0;
648                         ieee80211_tx_status(local_to_hw(local),
649                                             skb, tx_status);
650                         kfree(tx_status);
651                         break;
652                 default: /* should never get here! */
653                         printk(KERN_ERR "%s: Unknown message type (%d)\n",
654                                wiphy_name(local->hw.wiphy), skb->pkt_type);
655                         dev_kfree_skb(skb);
656                         break;
657                 }
658         }
659 }
660
661 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
662  * make a prepared TX frame (one that has been given to hw) to look like brand
663  * new IEEE 802.11 frame that is ready to go through TX processing again.
664  * Also, tx_packet_data in cb is restored from tx_control. */
665 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
666                                       struct ieee80211_key *key,
667                                       struct sk_buff *skb,
668                                       struct ieee80211_tx_control *control)
669 {
670         int hdrlen, iv_len, mic_len;
671         struct ieee80211_tx_packet_data *pkt_data;
672
673         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
674         pkt_data->ifindex = control->ifindex;
675         pkt_data->flags = 0;
676         if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
677                 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
678         if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
679                 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
680         if (control->flags & IEEE80211_TXCTL_REQUEUE)
681                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
682         pkt_data->queue = control->queue;
683
684         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
685
686         if (!key)
687                 goto no_key;
688
689         switch (key->conf.alg) {
690         case ALG_WEP:
691                 iv_len = WEP_IV_LEN;
692                 mic_len = WEP_ICV_LEN;
693                 break;
694         case ALG_TKIP:
695                 iv_len = TKIP_IV_LEN;
696                 mic_len = TKIP_ICV_LEN;
697                 break;
698         case ALG_CCMP:
699                 iv_len = CCMP_HDR_LEN;
700                 mic_len = CCMP_MIC_LEN;
701                 break;
702         default:
703                 goto no_key;
704         }
705
706         if (skb->len >= mic_len &&
707             !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
708                 skb_trim(skb, skb->len - mic_len);
709         if (skb->len >= iv_len && skb->len > hdrlen) {
710                 memmove(skb->data + iv_len, skb->data, hdrlen);
711                 skb_pull(skb, iv_len);
712         }
713
714 no_key:
715         {
716                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
717                 u16 fc = le16_to_cpu(hdr->frame_control);
718                 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
719                         fc &= ~IEEE80211_STYPE_QOS_DATA;
720                         hdr->frame_control = cpu_to_le16(fc);
721                         memmove(skb->data + 2, skb->data, hdrlen - 2);
722                         skb_pull(skb, 2);
723                 }
724         }
725 }
726
727 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
728                          struct ieee80211_tx_status *status)
729 {
730         struct sk_buff *skb2;
731         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
732         struct ieee80211_local *local = hw_to_local(hw);
733         u16 frag, type;
734         struct ieee80211_tx_status_rtap_hdr *rthdr;
735         struct ieee80211_sub_if_data *sdata;
736         int monitors;
737
738         if (!status) {
739                 printk(KERN_ERR
740                        "%s: ieee80211_tx_status called with NULL status\n",
741                        wiphy_name(local->hw.wiphy));
742                 dev_kfree_skb(skb);
743                 return;
744         }
745
746         if (status->excessive_retries) {
747                 struct sta_info *sta;
748                 sta = sta_info_get(local, hdr->addr1);
749                 if (sta) {
750                         if (sta->flags & WLAN_STA_PS) {
751                                 /* The STA is in power save mode, so assume
752                                  * that this TX packet failed because of that.
753                                  */
754                                 status->excessive_retries = 0;
755                                 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
756                         }
757                         sta_info_put(sta);
758                 }
759         }
760
761         if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
762                 struct sta_info *sta;
763                 sta = sta_info_get(local, hdr->addr1);
764                 if (sta) {
765                         sta->tx_filtered_count++;
766
767                         /* Clear the TX filter mask for this STA when sending
768                          * the next packet. If the STA went to power save mode,
769                          * this will happen when it is waking up for the next
770                          * time. */
771                         sta->clear_dst_mask = 1;
772
773                         /* TODO: Is the WLAN_STA_PS flag always set here or is
774                          * the race between RX and TX status causing some
775                          * packets to be filtered out before 80211.o gets an
776                          * update for PS status? This seems to be the case, so
777                          * no changes are likely to be needed. */
778                         if (sta->flags & WLAN_STA_PS &&
779                             skb_queue_len(&sta->tx_filtered) <
780                             STA_MAX_TX_BUFFER) {
781                                 ieee80211_remove_tx_extra(local, sta->key,
782                                                           skb,
783                                                           &status->control);
784                                 skb_queue_tail(&sta->tx_filtered, skb);
785                         } else if (!(sta->flags & WLAN_STA_PS) &&
786                                    !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
787                                 /* Software retry the packet once */
788                                 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
789                                 ieee80211_remove_tx_extra(local, sta->key,
790                                                           skb,
791                                                           &status->control);
792                                 dev_queue_xmit(skb);
793                         } else {
794                                 if (net_ratelimit()) {
795                                         printk(KERN_DEBUG "%s: dropped TX "
796                                                "filtered frame queue_len=%d "
797                                                "PS=%d @%lu\n",
798                                                wiphy_name(local->hw.wiphy),
799                                                skb_queue_len(
800                                                        &sta->tx_filtered),
801                                                !!(sta->flags & WLAN_STA_PS),
802                                                jiffies);
803                                 }
804                                 dev_kfree_skb(skb);
805                         }
806                         sta_info_put(sta);
807                         return;
808                 }
809         } else {
810                 /* FIXME: STUPID to call this with both local and local->mdev */
811                 rate_control_tx_status(local, local->mdev, skb, status);
812         }
813
814         ieee80211_led_tx(local, 0);
815
816         /* SNMP counters
817          * Fragments are passed to low-level drivers as separate skbs, so these
818          * are actually fragments, not frames. Update frame counters only for
819          * the first fragment of the frame. */
820
821         frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
822         type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
823
824         if (status->flags & IEEE80211_TX_STATUS_ACK) {
825                 if (frag == 0) {
826                         local->dot11TransmittedFrameCount++;
827                         if (is_multicast_ether_addr(hdr->addr1))
828                                 local->dot11MulticastTransmittedFrameCount++;
829                         if (status->retry_count > 0)
830                                 local->dot11RetryCount++;
831                         if (status->retry_count > 1)
832                                 local->dot11MultipleRetryCount++;
833                 }
834
835                 /* This counter shall be incremented for an acknowledged MPDU
836                  * with an individual address in the address 1 field or an MPDU
837                  * with a multicast address in the address 1 field of type Data
838                  * or Management. */
839                 if (!is_multicast_ether_addr(hdr->addr1) ||
840                     type == IEEE80211_FTYPE_DATA ||
841                     type == IEEE80211_FTYPE_MGMT)
842                         local->dot11TransmittedFragmentCount++;
843         } else {
844                 if (frag == 0)
845                         local->dot11FailedCount++;
846         }
847
848         /* this was a transmitted frame, but now we want to reuse it */
849         skb_orphan(skb);
850
851         if (!local->monitors) {
852                 dev_kfree_skb(skb);
853                 return;
854         }
855
856         /* send frame to monitor interfaces now */
857
858         if (skb_headroom(skb) < sizeof(*rthdr)) {
859                 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
860                 dev_kfree_skb(skb);
861                 return;
862         }
863
864         rthdr = (struct ieee80211_tx_status_rtap_hdr*)
865                                 skb_push(skb, sizeof(*rthdr));
866
867         memset(rthdr, 0, sizeof(*rthdr));
868         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
869         rthdr->hdr.it_present =
870                 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
871                             (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
872
873         if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
874             !is_multicast_ether_addr(hdr->addr1))
875                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
876
877         if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
878             (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
879                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
880         else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
881                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
882
883         rthdr->data_retries = status->retry_count;
884
885         rcu_read_lock();
886         monitors = local->monitors;
887         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
888                 /*
889                  * Using the monitors counter is possibly racy, but
890                  * if the value is wrong we simply either clone the skb
891                  * once too much or forget sending it to one monitor iface
892                  * The latter case isn't nice but fixing the race is much
893                  * more complicated.
894                  */
895                 if (!monitors || !skb)
896                         goto out;
897
898                 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
899                         if (!netif_running(sdata->dev))
900                                 continue;
901                         monitors--;
902                         if (monitors)
903                                 skb2 = skb_clone(skb, GFP_ATOMIC);
904                         else
905                                 skb2 = NULL;
906                         skb->dev = sdata->dev;
907                         /* XXX: is this sufficient for BPF? */
908                         skb_set_mac_header(skb, 0);
909                         skb->ip_summed = CHECKSUM_UNNECESSARY;
910                         skb->pkt_type = PACKET_OTHERHOST;
911                         skb->protocol = htons(ETH_P_802_2);
912                         memset(skb->cb, 0, sizeof(skb->cb));
913                         netif_rx(skb);
914                         skb = skb2;
915                 }
916         }
917  out:
918         rcu_read_unlock();
919         if (skb)
920                 dev_kfree_skb(skb);
921 }
922 EXPORT_SYMBOL(ieee80211_tx_status);
923
924 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
925                                         const struct ieee80211_ops *ops)
926 {
927         struct net_device *mdev;
928         struct ieee80211_local *local;
929         struct ieee80211_sub_if_data *sdata;
930         int priv_size;
931         struct wiphy *wiphy;
932
933         /* Ensure 32-byte alignment of our private data and hw private data.
934          * We use the wiphy priv data for both our ieee80211_local and for
935          * the driver's private data
936          *
937          * In memory it'll be like this:
938          *
939          * +-------------------------+
940          * | struct wiphy           |
941          * +-------------------------+
942          * | struct ieee80211_local  |
943          * +-------------------------+
944          * | driver's private data   |
945          * +-------------------------+
946          *
947          */
948         priv_size = ((sizeof(struct ieee80211_local) +
949                       NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
950                     priv_data_len;
951
952         wiphy = wiphy_new(&mac80211_config_ops, priv_size);
953
954         if (!wiphy)
955                 return NULL;
956
957         wiphy->privid = mac80211_wiphy_privid;
958
959         local = wiphy_priv(wiphy);
960         local->hw.wiphy = wiphy;
961
962         local->hw.priv = (char *)local +
963                          ((sizeof(struct ieee80211_local) +
964                            NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
965
966         BUG_ON(!ops->tx);
967         BUG_ON(!ops->start);
968         BUG_ON(!ops->stop);
969         BUG_ON(!ops->config);
970         BUG_ON(!ops->add_interface);
971         BUG_ON(!ops->remove_interface);
972         BUG_ON(!ops->configure_filter);
973         local->ops = ops;
974
975         /* for now, mdev needs sub_if_data :/ */
976         mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
977                             "wmaster%d", ether_setup);
978         if (!mdev) {
979                 wiphy_free(wiphy);
980                 return NULL;
981         }
982
983         sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
984         mdev->ieee80211_ptr = &sdata->wdev;
985         sdata->wdev.wiphy = wiphy;
986
987         local->hw.queues = 1; /* default */
988
989         local->mdev = mdev;
990         local->rx_pre_handlers = ieee80211_rx_pre_handlers;
991         local->rx_handlers = ieee80211_rx_handlers;
992         local->tx_handlers = ieee80211_tx_handlers;
993
994         local->bridge_packets = 1;
995
996         local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
997         local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
998         local->short_retry_limit = 7;
999         local->long_retry_limit = 4;
1000         local->hw.conf.radio_enabled = 1;
1001
1002         local->enabled_modes = ~0;
1003
1004         INIT_LIST_HEAD(&local->modes_list);
1005
1006         INIT_LIST_HEAD(&local->interfaces);
1007
1008         INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1009         ieee80211_rx_bss_list_init(mdev);
1010
1011         sta_info_init(local);
1012
1013         mdev->hard_start_xmit = ieee80211_master_start_xmit;
1014         mdev->open = ieee80211_master_open;
1015         mdev->stop = ieee80211_master_stop;
1016         mdev->type = ARPHRD_IEEE80211;
1017         mdev->header_ops = &ieee80211_header_ops;
1018         mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1019
1020         sdata->type = IEEE80211_IF_TYPE_AP;
1021         sdata->dev = mdev;
1022         sdata->local = local;
1023         sdata->u.ap.force_unicast_rateidx = -1;
1024         sdata->u.ap.max_ratectrl_rateidx = -1;
1025         ieee80211_if_sdata_init(sdata);
1026         /* no RCU needed since we're still during init phase */
1027         list_add_tail(&sdata->list, &local->interfaces);
1028
1029         tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1030                      (unsigned long)local);
1031         tasklet_disable(&local->tx_pending_tasklet);
1032
1033         tasklet_init(&local->tasklet,
1034                      ieee80211_tasklet_handler,
1035                      (unsigned long) local);
1036         tasklet_disable(&local->tasklet);
1037
1038         skb_queue_head_init(&local->skb_queue);
1039         skb_queue_head_init(&local->skb_queue_unreliable);
1040
1041         return local_to_hw(local);
1042 }
1043 EXPORT_SYMBOL(ieee80211_alloc_hw);
1044
1045 int ieee80211_register_hw(struct ieee80211_hw *hw)
1046 {
1047         struct ieee80211_local *local = hw_to_local(hw);
1048         const char *name;
1049         int result;
1050
1051         result = wiphy_register(local->hw.wiphy);
1052         if (result < 0)
1053                 return result;
1054
1055         name = wiphy_dev(local->hw.wiphy)->driver->name;
1056         local->hw.workqueue = create_singlethread_workqueue(name);
1057         if (!local->hw.workqueue) {
1058                 result = -ENOMEM;
1059                 goto fail_workqueue;
1060         }
1061
1062         /*
1063          * The hardware needs headroom for sending the frame,
1064          * and we need some headroom for passing the frame to monitor
1065          * interfaces, but never both at the same time.
1066          */
1067         local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1068                                    sizeof(struct ieee80211_tx_status_rtap_hdr));
1069
1070         debugfs_hw_add(local);
1071
1072         local->hw.conf.beacon_int = 1000;
1073
1074         local->wstats_flags |= local->hw.max_rssi ?
1075                                IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1076         local->wstats_flags |= local->hw.max_signal ?
1077                                IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1078         local->wstats_flags |= local->hw.max_noise ?
1079                                IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1080         if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1081                 local->wstats_flags |= IW_QUAL_DBM;
1082
1083         result = sta_info_start(local);
1084         if (result < 0)
1085                 goto fail_sta_info;
1086
1087         rtnl_lock();
1088         result = dev_alloc_name(local->mdev, local->mdev->name);
1089         if (result < 0)
1090                 goto fail_dev;
1091
1092         memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1093         SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1094
1095         result = register_netdevice(local->mdev);
1096         if (result < 0)
1097                 goto fail_dev;
1098
1099         ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1100         ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
1101
1102         result = ieee80211_init_rate_ctrl_alg(local,
1103                                               hw->rate_control_algorithm);
1104         if (result < 0) {
1105                 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1106                        "algorithm\n", wiphy_name(local->hw.wiphy));
1107                 goto fail_rate;
1108         }
1109
1110         result = ieee80211_wep_init(local);
1111
1112         if (result < 0) {
1113                 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1114                        wiphy_name(local->hw.wiphy));
1115                 goto fail_wep;
1116         }
1117
1118         ieee80211_install_qdisc(local->mdev);
1119
1120         /* add one default STA interface */
1121         result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1122                                   IEEE80211_IF_TYPE_STA);
1123         if (result)
1124                 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1125                        wiphy_name(local->hw.wiphy));
1126
1127         local->reg_state = IEEE80211_DEV_REGISTERED;
1128         rtnl_unlock();
1129
1130         ieee80211_led_init(local);
1131
1132         return 0;
1133
1134 fail_wep:
1135         rate_control_deinitialize(local);
1136 fail_rate:
1137         ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1138         unregister_netdevice(local->mdev);
1139 fail_dev:
1140         rtnl_unlock();
1141         sta_info_stop(local);
1142 fail_sta_info:
1143         debugfs_hw_del(local);
1144         destroy_workqueue(local->hw.workqueue);
1145 fail_workqueue:
1146         wiphy_unregister(local->hw.wiphy);
1147         return result;
1148 }
1149 EXPORT_SYMBOL(ieee80211_register_hw);
1150
1151 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1152                               struct ieee80211_hw_mode *mode)
1153 {
1154         struct ieee80211_local *local = hw_to_local(hw);
1155         struct ieee80211_rate *rate;
1156         int i;
1157
1158         INIT_LIST_HEAD(&mode->list);
1159         list_add_tail(&mode->list, &local->modes_list);
1160
1161         local->hw_modes |= (1 << mode->mode);
1162         for (i = 0; i < mode->num_rates; i++) {
1163                 rate = &(mode->rates[i]);
1164                 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1165         }
1166         ieee80211_prepare_rates(local, mode);
1167
1168         if (!local->oper_hw_mode) {
1169                 /* Default to this mode */
1170                 local->hw.conf.phymode = mode->mode;
1171                 local->oper_hw_mode = local->scan_hw_mode = mode;
1172                 local->oper_channel = local->scan_channel = &mode->channels[0];
1173                 local->hw.conf.mode = local->oper_hw_mode;
1174                 local->hw.conf.chan = local->oper_channel;
1175         }
1176
1177         if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1178                 ieee80211_set_default_regdomain(mode);
1179
1180         return 0;
1181 }
1182 EXPORT_SYMBOL(ieee80211_register_hwmode);
1183
1184 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1185 {
1186         struct ieee80211_local *local = hw_to_local(hw);
1187         struct ieee80211_sub_if_data *sdata, *tmp;
1188         int i;
1189
1190         tasklet_kill(&local->tx_pending_tasklet);
1191         tasklet_kill(&local->tasklet);
1192
1193         rtnl_lock();
1194
1195         BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1196
1197         local->reg_state = IEEE80211_DEV_UNREGISTERED;
1198
1199         /*
1200          * At this point, interface list manipulations are fine
1201          * because the driver cannot be handing us frames any
1202          * more and the tasklet is killed.
1203          */
1204
1205         /*
1206          * First, we remove all non-master interfaces. Do this because they
1207          * may have bss pointer dependency on the master, and when we free
1208          * the master these would be freed as well, breaking our list
1209          * iteration completely.
1210          */
1211         list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1212                 if (sdata->dev == local->mdev)
1213                         continue;
1214                 list_del(&sdata->list);
1215                 __ieee80211_if_del(local, sdata);
1216         }
1217
1218         /* then, finally, remove the master interface */
1219         __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
1220
1221         rtnl_unlock();
1222
1223         ieee80211_rx_bss_list_deinit(local->mdev);
1224         ieee80211_clear_tx_pending(local);
1225         sta_info_stop(local);
1226         rate_control_deinitialize(local);
1227         debugfs_hw_del(local);
1228
1229         for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1230                 kfree(local->supp_rates[i]);
1231                 kfree(local->basic_rates[i]);
1232         }
1233
1234         if (skb_queue_len(&local->skb_queue)
1235                         || skb_queue_len(&local->skb_queue_unreliable))
1236                 printk(KERN_WARNING "%s: skb_queue not empty\n",
1237                        wiphy_name(local->hw.wiphy));
1238         skb_queue_purge(&local->skb_queue);
1239         skb_queue_purge(&local->skb_queue_unreliable);
1240
1241         destroy_workqueue(local->hw.workqueue);
1242         wiphy_unregister(local->hw.wiphy);
1243         ieee80211_wep_free(local);
1244         ieee80211_led_exit(local);
1245 }
1246 EXPORT_SYMBOL(ieee80211_unregister_hw);
1247
1248 void ieee80211_free_hw(struct ieee80211_hw *hw)
1249 {
1250         struct ieee80211_local *local = hw_to_local(hw);
1251
1252         ieee80211_if_free(local->mdev);
1253         wiphy_free(local->hw.wiphy);
1254 }
1255 EXPORT_SYMBOL(ieee80211_free_hw);
1256
1257 static int __init ieee80211_init(void)
1258 {
1259         struct sk_buff *skb;
1260         int ret;
1261
1262         BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1263
1264 #ifdef CONFIG_MAC80211_RCSIMPLE
1265         ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1266         if (ret)
1267                 return ret;
1268 #endif
1269
1270         ret = ieee80211_wme_register();
1271         if (ret) {
1272 #ifdef CONFIG_MAC80211_RCSIMPLE
1273                 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1274 #endif
1275                 printk(KERN_DEBUG "ieee80211_init: failed to "
1276                        "initialize WME (err=%d)\n", ret);
1277                 return ret;
1278         }
1279
1280         ieee80211_debugfs_netdev_init();
1281         ieee80211_regdomain_init();
1282
1283         return 0;
1284 }
1285
1286 static void __exit ieee80211_exit(void)
1287 {
1288 #ifdef CONFIG_MAC80211_RCSIMPLE
1289         ieee80211_rate_control_unregister(&mac80211_rcsimple);
1290 #endif
1291
1292         ieee80211_wme_unregister();
1293         ieee80211_debugfs_netdev_exit();
1294 }
1295
1296
1297 subsys_initcall(ieee80211_init);
1298 module_exit(ieee80211_exit);
1299
1300 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1301 MODULE_LICENSE("GPL");