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