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