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