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