1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3
4 Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
5
6 Portions of this file are based on the WEP enablement code provided by the
7 Host AP project hostap-drivers v0.1.3
8 Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
9 <j@w1.fi>
10 Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
11
12
13 Contact Information:
14 Intel Linux Wireless <ilw@linux.intel.com>
15 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
16
17 *******************************************************************************/
18
19 #include <linux/compiler.h>
20 #include <linux/errno.h>
21 #include <linux/if_arp.h>
22 #include <linux/in6.h>
23 #include <linux/in.h>
24 #include <linux/ip.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/netdevice.h>
28 #include <linux/proc_fs.h>
29 #include <linux/skbuff.h>
30 #include <linux/slab.h>
31 #include <linux/tcp.h>
32 #include <linux/types.h>
33 #include <linux/wireless.h>
34 #include <linux/etherdevice.h>
35 #include <linux/uaccess.h>
36 #include <net/net_namespace.h>
37 #include <net/arp.h>
38
39 #include "libipw.h"
40
41 #define DRV_DESCRIPTION "802.11 data/management/control stack"
42 #define DRV_NAME "libipw"
43 #define DRV_PROCNAME "ieee80211"
44 #define DRV_VERSION LIBIPW_VERSION
45 #define DRV_COPYRIGHT "Copyright (C) 2004-2005 Intel Corporation <jketreno@linux.intel.com>"
46
47 MODULE_VERSION(DRV_VERSION);
48 MODULE_DESCRIPTION(DRV_DESCRIPTION);
49 MODULE_AUTHOR(DRV_COPYRIGHT);
50 MODULE_LICENSE("GPL");
51
52 static struct cfg80211_ops libipw_config_ops = { };
53 static void *libipw_wiphy_privid = &libipw_wiphy_privid;
54
libipw_networks_allocate(struct libipw_device * ieee)55 static int libipw_networks_allocate(struct libipw_device *ieee)
56 {
57 int i, j;
58
59 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
60 ieee->networks[i] = kzalloc_obj(struct libipw_network);
61 if (!ieee->networks[i]) {
62 LIBIPW_ERROR("Out of memory allocating beacons\n");
63 for (j = 0; j < i; j++)
64 kfree(ieee->networks[j]);
65 return -ENOMEM;
66 }
67 }
68
69 return 0;
70 }
71
libipw_networks_free(struct libipw_device * ieee)72 static inline void libipw_networks_free(struct libipw_device *ieee)
73 {
74 int i;
75
76 for (i = 0; i < MAX_NETWORK_COUNT; i++)
77 kfree(ieee->networks[i]);
78 }
79
libipw_networks_age(struct libipw_device * ieee,unsigned long age_secs)80 void libipw_networks_age(struct libipw_device *ieee,
81 unsigned long age_secs)
82 {
83 struct libipw_network *network = NULL;
84 unsigned long flags;
85 unsigned long age_jiffies = secs_to_jiffies(age_secs);
86
87 spin_lock_irqsave(&ieee->lock, flags);
88 list_for_each_entry(network, &ieee->network_list, list) {
89 network->last_scanned -= age_jiffies;
90 }
91 spin_unlock_irqrestore(&ieee->lock, flags);
92 }
93 EXPORT_SYMBOL(libipw_networks_age);
94
libipw_networks_initialize(struct libipw_device * ieee)95 static void libipw_networks_initialize(struct libipw_device *ieee)
96 {
97 int i;
98
99 INIT_LIST_HEAD(&ieee->network_free_list);
100 INIT_LIST_HEAD(&ieee->network_list);
101 for (i = 0; i < MAX_NETWORK_COUNT; i++)
102 list_add_tail(&ieee->networks[i]->list,
103 &ieee->network_free_list);
104 }
105
alloc_libipw(int sizeof_priv,int monitor)106 struct net_device *alloc_libipw(int sizeof_priv, int monitor)
107 {
108 struct libipw_device *ieee;
109 struct net_device *dev;
110 int err;
111
112 LIBIPW_DEBUG_INFO("Initializing...\n");
113
114 dev = alloc_etherdev(sizeof(struct libipw_device) + sizeof_priv);
115 if (!dev)
116 goto failed;
117
118 ieee = netdev_priv(dev);
119
120 ieee->dev = dev;
121
122 if (!monitor) {
123 ieee->wdev.wiphy = wiphy_new(&libipw_config_ops, 0);
124 if (!ieee->wdev.wiphy) {
125 LIBIPW_ERROR("Unable to allocate wiphy.\n");
126 goto failed_free_netdev;
127 }
128
129 ieee->dev->ieee80211_ptr = &ieee->wdev;
130 ieee->wdev.iftype = NL80211_IFTYPE_STATION;
131
132 /* Fill-out wiphy structure bits we know... Not enough info
133 here to call set_wiphy_dev or set MAC address or channel info
134 -- have to do that in ->ndo_init... */
135 ieee->wdev.wiphy->privid = libipw_wiphy_privid;
136
137 ieee->wdev.wiphy->max_scan_ssids = 1;
138 ieee->wdev.wiphy->max_scan_ie_len = 0;
139 ieee->wdev.wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
140 | BIT(NL80211_IFTYPE_ADHOC);
141 }
142
143 err = libipw_networks_allocate(ieee);
144 if (err) {
145 LIBIPW_ERROR("Unable to allocate beacon storage: %d\n", err);
146 goto failed_free_wiphy;
147 }
148 libipw_networks_initialize(ieee);
149
150 /* Default fragmentation threshold is maximum payload size */
151 ieee->fts = DEFAULT_FTS;
152 ieee->rts = DEFAULT_FTS;
153 ieee->scan_age = DEFAULT_MAX_SCAN_AGE;
154 ieee->open_wep = 1;
155
156 /* Default to enabling full open WEP with host based encrypt/decrypt */
157 ieee->host_encrypt = 1;
158 ieee->host_decrypt = 1;
159 ieee->host_mc_decrypt = 1;
160
161 /* Host fragmentation in Open mode. Default is enabled.
162 * Note: host fragmentation is always enabled if host encryption
163 * is enabled. For cards can do hardware encryption, they must do
164 * hardware fragmentation as well. So we don't need a variable
165 * like host_enc_frag. */
166 ieee->host_open_frag = 1;
167 ieee->ieee802_1x = 1; /* Default to supporting 802.1x */
168
169 spin_lock_init(&ieee->lock);
170
171 libipw_crypt_info_init(&ieee->crypt_info, dev->name, &ieee->lock);
172
173 ieee->wpa_enabled = 0;
174 ieee->drop_unencrypted = 0;
175 ieee->privacy_invoked = 0;
176
177 return dev;
178
179 failed_free_wiphy:
180 if (!monitor)
181 wiphy_free(ieee->wdev.wiphy);
182 failed_free_netdev:
183 free_netdev(dev);
184 failed:
185 return NULL;
186 }
187 EXPORT_SYMBOL(alloc_libipw);
188
free_libipw(struct net_device * dev,int monitor)189 void free_libipw(struct net_device *dev, int monitor)
190 {
191 struct libipw_device *ieee = netdev_priv(dev);
192
193 libipw_crypt_info_free(&ieee->crypt_info);
194
195 libipw_networks_free(ieee);
196
197 /* free cfg80211 resources */
198 if (!monitor)
199 wiphy_free(ieee->wdev.wiphy);
200
201 free_netdev(dev);
202 }
203 EXPORT_SYMBOL(free_libipw);
204
205 #ifdef CONFIG_LIBIPW_DEBUG
206
207 static int debug = 0;
208 u32 libipw_debug_level = 0;
209 EXPORT_SYMBOL_GPL(libipw_debug_level);
210 static struct proc_dir_entry *libipw_proc = NULL;
211
debug_level_proc_show(struct seq_file * m,void * v)212 static int debug_level_proc_show(struct seq_file *m, void *v)
213 {
214 seq_printf(m, "0x%08X\n", libipw_debug_level);
215 return 0;
216 }
217
debug_level_proc_open(struct inode * inode,struct file * file)218 static int debug_level_proc_open(struct inode *inode, struct file *file)
219 {
220 return single_open(file, debug_level_proc_show, NULL);
221 }
222
debug_level_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)223 static ssize_t debug_level_proc_write(struct file *file,
224 const char __user *buffer, size_t count, loff_t *pos)
225 {
226 char buf[] = "0x00000000\n";
227 size_t len = min(sizeof(buf) - 1, count);
228 unsigned long val;
229
230 if (copy_from_user(buf, buffer, len))
231 return count;
232 buf[len] = 0;
233 if (sscanf(buf, "%li", &val) != 1)
234 printk(KERN_INFO DRV_NAME
235 ": %s is not in hex or decimal form.\n", buf);
236 else
237 libipw_debug_level = val;
238
239 return strnlen(buf, len);
240 }
241
242 static const struct proc_ops debug_level_proc_ops = {
243 .proc_open = debug_level_proc_open,
244 .proc_read = seq_read,
245 .proc_lseek = seq_lseek,
246 .proc_release = single_release,
247 .proc_write = debug_level_proc_write,
248 };
249 #endif /* CONFIG_LIBIPW_DEBUG */
250
libipw_init(void)251 static int __init libipw_init(void)
252 {
253 int err;
254 #ifdef CONFIG_LIBIPW_DEBUG
255 struct proc_dir_entry *e;
256
257 libipw_debug_level = debug;
258 libipw_proc = proc_mkdir(DRV_PROCNAME, init_net.proc_net);
259 if (libipw_proc == NULL) {
260 LIBIPW_ERROR("Unable to create " DRV_PROCNAME
261 " proc directory\n");
262 return -EIO;
263 }
264 e = proc_create("debug_level", 0644, libipw_proc,
265 &debug_level_proc_ops);
266 if (!e) {
267 remove_proc_entry(DRV_PROCNAME, init_net.proc_net);
268 libipw_proc = NULL;
269 return -EIO;
270 }
271 #endif /* CONFIG_LIBIPW_DEBUG */
272
273 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
274 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
275
276 err = libipw_crypto_init();
277 if (err)
278 goto remove_debugfs;
279 err = libipw_crypto_ccmp_init();
280 if (err)
281 goto uninit_crypto;
282 err = libipw_crypto_tkip_init();
283 if (err)
284 goto uninit_crypto_ccmp;
285 err = libipw_crypto_wep_init();
286 if (err)
287 goto uninit_crypto_tkip;
288
289 return 0;
290 uninit_crypto_tkip:
291 libipw_crypto_tkip_exit();
292 uninit_crypto_ccmp:
293 libipw_crypto_ccmp_exit();
294 uninit_crypto:
295 libipw_crypto_exit();
296 remove_debugfs:
297 #ifdef CONFIG_LIBIPW_DEBUG
298 remove_proc_entry("debug_level", libipw_proc);
299 remove_proc_entry(DRV_PROCNAME, init_net.proc_net);
300 libipw_proc = NULL;
301 #endif
302 return err;
303 }
304
libipw_exit(void)305 static void __exit libipw_exit(void)
306 {
307 #ifdef CONFIG_LIBIPW_DEBUG
308 if (libipw_proc) {
309 remove_proc_entry("debug_level", libipw_proc);
310 remove_proc_entry(DRV_PROCNAME, init_net.proc_net);
311 libipw_proc = NULL;
312 }
313 #endif /* CONFIG_LIBIPW_DEBUG */
314
315 libipw_crypto_ccmp_exit();
316 libipw_crypto_tkip_exit();
317 libipw_crypto_wep_exit();
318 libipw_crypto_exit();
319 }
320
321 #ifdef CONFIG_LIBIPW_DEBUG
322 #include <linux/moduleparam.h>
323 module_param(debug, int, 0444);
324 MODULE_PARM_DESC(debug, "debug output mask");
325 #endif /* CONFIG_LIBIPW_DEBUG */
326
327 module_exit(libipw_exit);
328 module_init(libipw_init);
329