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