Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*******************************************************************************
0003 
0004   Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
0005 
0006   Portions of this file are based on the WEP enablement code provided by the
0007   Host AP project hostap-drivers v0.1.3
0008   Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
0009   <j@w1.fi>
0010   Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
0011 
0012 
0013   Contact Information:
0014   Intel Linux Wireless <ilw@linux.intel.com>
0015   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
0016 
0017 *******************************************************************************/
0018 
0019 #include <linux/compiler.h>
0020 #include <linux/errno.h>
0021 #include <linux/if_arp.h>
0022 #include <linux/in6.h>
0023 #include <linux/in.h>
0024 #include <linux/ip.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/netdevice.h>
0028 #include <linux/proc_fs.h>
0029 #include <linux/skbuff.h>
0030 #include <linux/slab.h>
0031 #include <linux/tcp.h>
0032 #include <linux/types.h>
0033 #include <linux/wireless.h>
0034 #include <linux/etherdevice.h>
0035 #include <linux/uaccess.h>
0036 #include <net/net_namespace.h>
0037 #include <net/arp.h>
0038 
0039 #include "libipw.h"
0040 
0041 #define DRV_DESCRIPTION "802.11 data/management/control stack"
0042 #define DRV_NAME        "libipw"
0043 #define DRV_PROCNAME    "ieee80211"
0044 #define DRV_VERSION LIBIPW_VERSION
0045 #define DRV_COPYRIGHT   "Copyright (C) 2004-2005 Intel Corporation <jketreno@linux.intel.com>"
0046 
0047 MODULE_VERSION(DRV_VERSION);
0048 MODULE_DESCRIPTION(DRV_DESCRIPTION);
0049 MODULE_AUTHOR(DRV_COPYRIGHT);
0050 MODULE_LICENSE("GPL");
0051 
0052 static struct cfg80211_ops libipw_config_ops = { };
0053 static void *libipw_wiphy_privid = &libipw_wiphy_privid;
0054 
0055 static int libipw_networks_allocate(struct libipw_device *ieee)
0056 {
0057     int i, j;
0058 
0059     for (i = 0; i < MAX_NETWORK_COUNT; i++) {
0060         ieee->networks[i] = kzalloc(sizeof(struct libipw_network),
0061                         GFP_KERNEL);
0062         if (!ieee->networks[i]) {
0063             LIBIPW_ERROR("Out of memory allocating beacons\n");
0064             for (j = 0; j < i; j++)
0065                 kfree(ieee->networks[j]);
0066             return -ENOMEM;
0067         }
0068     }
0069 
0070     return 0;
0071 }
0072 
0073 static inline void libipw_networks_free(struct libipw_device *ieee)
0074 {
0075     int i;
0076 
0077     for (i = 0; i < MAX_NETWORK_COUNT; i++)
0078         kfree(ieee->networks[i]);
0079 }
0080 
0081 void libipw_networks_age(struct libipw_device *ieee,
0082                             unsigned long age_secs)
0083 {
0084     struct libipw_network *network = NULL;
0085     unsigned long flags;
0086     unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
0087 
0088     spin_lock_irqsave(&ieee->lock, flags);
0089     list_for_each_entry(network, &ieee->network_list, list) {
0090         network->last_scanned -= age_jiffies;
0091     }
0092     spin_unlock_irqrestore(&ieee->lock, flags);
0093 }
0094 EXPORT_SYMBOL(libipw_networks_age);
0095 
0096 static void libipw_networks_initialize(struct libipw_device *ieee)
0097 {
0098     int i;
0099 
0100     INIT_LIST_HEAD(&ieee->network_free_list);
0101     INIT_LIST_HEAD(&ieee->network_list);
0102     for (i = 0; i < MAX_NETWORK_COUNT; i++)
0103         list_add_tail(&ieee->networks[i]->list,
0104                   &ieee->network_free_list);
0105 }
0106 
0107 struct net_device *alloc_libipw(int sizeof_priv, int monitor)
0108 {
0109     struct libipw_device *ieee;
0110     struct net_device *dev;
0111     int err;
0112 
0113     LIBIPW_DEBUG_INFO("Initializing...\n");
0114 
0115     dev = alloc_etherdev(sizeof(struct libipw_device) + sizeof_priv);
0116     if (!dev)
0117         goto failed;
0118 
0119     ieee = netdev_priv(dev);
0120 
0121     ieee->dev = dev;
0122 
0123     if (!monitor) {
0124         ieee->wdev.wiphy = wiphy_new(&libipw_config_ops, 0);
0125         if (!ieee->wdev.wiphy) {
0126             LIBIPW_ERROR("Unable to allocate wiphy.\n");
0127             goto failed_free_netdev;
0128         }
0129 
0130         ieee->dev->ieee80211_ptr = &ieee->wdev;
0131         ieee->wdev.iftype = NL80211_IFTYPE_STATION;
0132 
0133         /* Fill-out wiphy structure bits we know...  Not enough info
0134            here to call set_wiphy_dev or set MAC address or channel info
0135            -- have to do that in ->ndo_init... */
0136         ieee->wdev.wiphy->privid = libipw_wiphy_privid;
0137 
0138         ieee->wdev.wiphy->max_scan_ssids = 1;
0139         ieee->wdev.wiphy->max_scan_ie_len = 0;
0140         ieee->wdev.wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
0141                         | BIT(NL80211_IFTYPE_ADHOC);
0142     }
0143 
0144     err = libipw_networks_allocate(ieee);
0145     if (err) {
0146         LIBIPW_ERROR("Unable to allocate beacon storage: %d\n", err);
0147         goto failed_free_wiphy;
0148     }
0149     libipw_networks_initialize(ieee);
0150 
0151     /* Default fragmentation threshold is maximum payload size */
0152     ieee->fts = DEFAULT_FTS;
0153     ieee->rts = DEFAULT_FTS;
0154     ieee->scan_age = DEFAULT_MAX_SCAN_AGE;
0155     ieee->open_wep = 1;
0156 
0157     /* Default to enabling full open WEP with host based encrypt/decrypt */
0158     ieee->host_encrypt = 1;
0159     ieee->host_decrypt = 1;
0160     ieee->host_mc_decrypt = 1;
0161 
0162     /* Host fragmentation in Open mode. Default is enabled.
0163      * Note: host fragmentation is always enabled if host encryption
0164      * is enabled. For cards can do hardware encryption, they must do
0165      * hardware fragmentation as well. So we don't need a variable
0166      * like host_enc_frag. */
0167     ieee->host_open_frag = 1;
0168     ieee->ieee802_1x = 1;   /* Default to supporting 802.1x */
0169 
0170     spin_lock_init(&ieee->lock);
0171 
0172     lib80211_crypt_info_init(&ieee->crypt_info, dev->name, &ieee->lock);
0173 
0174     ieee->wpa_enabled = 0;
0175     ieee->drop_unencrypted = 0;
0176     ieee->privacy_invoked = 0;
0177 
0178     return dev;
0179 
0180 failed_free_wiphy:
0181     if (!monitor)
0182         wiphy_free(ieee->wdev.wiphy);
0183 failed_free_netdev:
0184     free_netdev(dev);
0185 failed:
0186     return NULL;
0187 }
0188 EXPORT_SYMBOL(alloc_libipw);
0189 
0190 void free_libipw(struct net_device *dev, int monitor)
0191 {
0192     struct libipw_device *ieee = netdev_priv(dev);
0193 
0194     lib80211_crypt_info_free(&ieee->crypt_info);
0195 
0196     libipw_networks_free(ieee);
0197 
0198     /* free cfg80211 resources */
0199     if (!monitor)
0200         wiphy_free(ieee->wdev.wiphy);
0201 
0202     free_netdev(dev);
0203 }
0204 EXPORT_SYMBOL(free_libipw);
0205 
0206 #ifdef CONFIG_LIBIPW_DEBUG
0207 
0208 static int debug = 0;
0209 u32 libipw_debug_level = 0;
0210 EXPORT_SYMBOL_GPL(libipw_debug_level);
0211 static struct proc_dir_entry *libipw_proc = NULL;
0212 
0213 static int debug_level_proc_show(struct seq_file *m, void *v)
0214 {
0215     seq_printf(m, "0x%08X\n", libipw_debug_level);
0216     return 0;
0217 }
0218 
0219 static int debug_level_proc_open(struct inode *inode, struct file *file)
0220 {
0221     return single_open(file, debug_level_proc_show, NULL);
0222 }
0223 
0224 static ssize_t debug_level_proc_write(struct file *file,
0225         const char __user *buffer, size_t count, loff_t *pos)
0226 {
0227     char buf[] = "0x00000000\n";
0228     size_t len = min(sizeof(buf) - 1, count);
0229     unsigned long val;
0230 
0231     if (copy_from_user(buf, buffer, len))
0232         return count;
0233     buf[len] = 0;
0234     if (sscanf(buf, "%li", &val) != 1)
0235         printk(KERN_INFO DRV_NAME
0236                ": %s is not in hex or decimal form.\n", buf);
0237     else
0238         libipw_debug_level = val;
0239 
0240     return strnlen(buf, len);
0241 }
0242 
0243 static const struct proc_ops debug_level_proc_ops = {
0244     .proc_open  = debug_level_proc_open,
0245     .proc_read  = seq_read,
0246     .proc_lseek = seq_lseek,
0247     .proc_release   = single_release,
0248     .proc_write = debug_level_proc_write,
0249 };
0250 #endif              /* CONFIG_LIBIPW_DEBUG */
0251 
0252 static int __init libipw_init(void)
0253 {
0254 #ifdef CONFIG_LIBIPW_DEBUG
0255     struct proc_dir_entry *e;
0256 
0257     libipw_debug_level = debug;
0258     libipw_proc = proc_mkdir(DRV_PROCNAME, init_net.proc_net);
0259     if (libipw_proc == NULL) {
0260         LIBIPW_ERROR("Unable to create " DRV_PROCNAME
0261                 " proc directory\n");
0262         return -EIO;
0263     }
0264     e = proc_create("debug_level", 0644, libipw_proc,
0265             &debug_level_proc_ops);
0266     if (!e) {
0267         remove_proc_entry(DRV_PROCNAME, init_net.proc_net);
0268         libipw_proc = NULL;
0269         return -EIO;
0270     }
0271 #endif              /* CONFIG_LIBIPW_DEBUG */
0272 
0273     printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
0274     printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
0275 
0276     return 0;
0277 }
0278 
0279 static void __exit libipw_exit(void)
0280 {
0281 #ifdef CONFIG_LIBIPW_DEBUG
0282     if (libipw_proc) {
0283         remove_proc_entry("debug_level", libipw_proc);
0284         remove_proc_entry(DRV_PROCNAME, init_net.proc_net);
0285         libipw_proc = NULL;
0286     }
0287 #endif              /* CONFIG_LIBIPW_DEBUG */
0288 }
0289 
0290 #ifdef CONFIG_LIBIPW_DEBUG
0291 #include <linux/moduleparam.h>
0292 module_param(debug, int, 0444);
0293 MODULE_PARM_DESC(debug, "debug output mask");
0294 #endif              /* CONFIG_LIBIPW_DEBUG */
0295 
0296 module_exit(libipw_exit);
0297 module_init(libipw_init);