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