1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2004 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are based on the WEP enablement code provided by the
6  * Host AP project hostap-drivers v0.1.3
7  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
8  * <jkmaline@cc.hut.fi>
9  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
10  *
11  * Contact Information:
12  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
13  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
14  */
15 
16 #include <linux/compiler.h>
17 #include <linux/errno.h>
18 #include <linux/if_arp.h>
19 #include <linux/in6.h>
20 #include <linux/in.h>
21 #include <linux/ip.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/pci.h>
26 #include <linux/proc_fs.h>
27 #include <linux/skbuff.h>
28 #include <linux/slab.h>
29 #include <linux/tcp.h>
30 #include <linux/types.h>
31 #include <linux/wireless.h>
32 #include <linux/etherdevice.h>
33 #include <linux/uaccess.h>
34 #include <net/arp.h>
35 #include "rtllib.h"
36 
37 u32 rt_global_debug_component = COMP_ERR;
38 EXPORT_SYMBOL(rt_global_debug_component);
39 
40 static inline int rtllib_networks_allocate(struct rtllib_device *ieee)
41 {
42 	if (ieee->networks)
43 		return 0;
44 
45 	ieee->networks = kcalloc(MAX_NETWORK_COUNT,
46 				 sizeof(struct rtllib_network), GFP_KERNEL);
47 	if (!ieee->networks)
48 		return -ENOMEM;
49 
50 	return 0;
51 }
52 
53 static inline void rtllib_networks_free(struct rtllib_device *ieee)
54 {
55 	if (!ieee->networks)
56 		return;
57 	kfree(ieee->networks);
58 	ieee->networks = NULL;
59 }
60 
61 static inline void rtllib_networks_initialize(struct rtllib_device *ieee)
62 {
63 	int i;
64 
65 	INIT_LIST_HEAD(&ieee->network_free_list);
66 	INIT_LIST_HEAD(&ieee->network_list);
67 	for (i = 0; i < MAX_NETWORK_COUNT; i++)
68 		list_add_tail(&ieee->networks[i].list,
69 			      &ieee->network_free_list);
70 }
71 
72 struct net_device *alloc_rtllib(int sizeof_priv)
73 {
74 	struct rtllib_device *ieee = NULL;
75 	struct net_device *dev;
76 	int i, err;
77 
78 	pr_debug("rtllib: Initializing...\n");
79 
80 	dev = alloc_etherdev(sizeof(struct rtllib_device) + sizeof_priv);
81 	if (!dev) {
82 		pr_err("Unable to allocate net_device.\n");
83 		return NULL;
84 	}
85 	ieee = (struct rtllib_device *)netdev_priv_rsl(dev);
86 	ieee->dev = dev;
87 
88 	err = rtllib_networks_allocate(ieee);
89 	if (err) {
90 		pr_err("Unable to allocate beacon storage: %d\n", err);
91 		goto failed;
92 	}
93 	rtllib_networks_initialize(ieee);
94 
95 	/* Default fragmentation threshold is maximum payload size */
96 	ieee->fts = DEFAULT_FTS;
97 	ieee->scan_age = DEFAULT_MAX_SCAN_AGE;
98 	ieee->open_wep = 1;
99 
100 	/* Default to enabling full open WEP with host based encrypt/decrypt */
101 	ieee->host_encrypt = 1;
102 	ieee->host_decrypt = 1;
103 	ieee->ieee802_1x = 1; /* Default to supporting 802.1x */
104 
105 	ieee->rtllib_ap_sec_type = rtllib_ap_sec_type;
106 
107 	spin_lock_init(&ieee->lock);
108 	spin_lock_init(&ieee->wpax_suitlist_lock);
109 	spin_lock_init(&ieee->reorder_spinlock);
110 	atomic_set(&(ieee->atm_swbw), 0);
111 
112 	/* SAM FIXME */
113 	lib80211_crypt_info_init(&ieee->crypt_info, "RTLLIB", &ieee->lock);
114 
115 	ieee->wpa_enabled = 0;
116 	ieee->tkip_countermeasures = 0;
117 	ieee->drop_unencrypted = 0;
118 	ieee->privacy_invoked = 0;
119 	ieee->ieee802_1x = 1;
120 	ieee->raw_tx = 0;
121 	ieee->hwsec_active = 0;
122 
123 	memset(ieee->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
124 	rtllib_softmac_init(ieee);
125 
126 	ieee->pHTInfo = kzalloc(sizeof(struct rt_hi_throughput), GFP_KERNEL);
127 	if (!ieee->pHTInfo)
128 		return NULL;
129 
130 	HTUpdateDefaultSetting(ieee);
131 	HTInitializeHTInfo(ieee);
132 	TSInitialize(ieee);
133 	for (i = 0; i < IEEE_IBSS_MAC_HASH_SIZE; i++)
134 		INIT_LIST_HEAD(&ieee->ibss_mac_hash[i]);
135 
136 	for (i = 0; i < 17; i++) {
137 		ieee->last_rxseq_num[i] = -1;
138 		ieee->last_rxfrag_num[i] = -1;
139 		ieee->last_packet_time[i] = 0;
140 	}
141 
142 	return dev;
143 
144  failed:
145 	free_netdev(dev);
146 	return NULL;
147 }
148 EXPORT_SYMBOL(alloc_rtllib);
149 
150 void free_rtllib(struct net_device *dev)
151 {
152 	struct rtllib_device *ieee = (struct rtllib_device *)
153 				      netdev_priv_rsl(dev);
154 
155 	kfree(ieee->pHTInfo);
156 	ieee->pHTInfo = NULL;
157 	rtllib_softmac_free(ieee);
158 
159 	lib80211_crypt_info_free(&ieee->crypt_info);
160 
161 	rtllib_networks_free(ieee);
162 	free_netdev(dev);
163 }
164 EXPORT_SYMBOL(free_rtllib);
165 
166 static int __init rtllib_init(void)
167 {
168 	return 0;
169 }
170 
171 static void __exit rtllib_exit(void)
172 {
173 }
174 
175 module_init(rtllib_init);
176 module_exit(rtllib_exit);
177 
178 MODULE_LICENSE("GPL");
179