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