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