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