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