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 static inline int rtllib_networks_allocate(struct rtllib_device *ieee)
38 {
39 	if (ieee->networks)
40 		return 0;
41 
42 	ieee->networks = kcalloc(MAX_NETWORK_COUNT,
43 				 sizeof(struct rtllib_network), GFP_KERNEL);
44 	if (!ieee->networks)
45 		return -ENOMEM;
46 
47 	return 0;
48 }
49 
50 static inline void rtllib_networks_free(struct rtllib_device *ieee)
51 {
52 	if (!ieee->networks)
53 		return;
54 	kfree(ieee->networks);
55 	ieee->networks = NULL;
56 }
57 
58 static inline void rtllib_networks_initialize(struct rtllib_device *ieee)
59 {
60 	int i;
61 
62 	INIT_LIST_HEAD(&ieee->network_free_list);
63 	INIT_LIST_HEAD(&ieee->network_list);
64 	for (i = 0; i < MAX_NETWORK_COUNT; i++)
65 		list_add_tail(&ieee->networks[i].list,
66 			      &ieee->network_free_list);
67 }
68 
69 struct net_device *alloc_rtllib(int sizeof_priv)
70 {
71 	struct rtllib_device *ieee = NULL;
72 	struct net_device *dev;
73 	int i, err;
74 
75 	pr_debug("rtllib: Initializing...\n");
76 
77 	dev = alloc_etherdev(sizeof(struct rtllib_device) + sizeof_priv);
78 	if (!dev) {
79 		pr_err("Unable to allocate net_device.\n");
80 		return NULL;
81 	}
82 	ieee = (struct rtllib_device *)netdev_priv_rsl(dev);
83 	ieee->dev = dev;
84 
85 	err = rtllib_networks_allocate(ieee);
86 	if (err) {
87 		pr_err("Unable to allocate beacon storage: %d\n", err);
88 		goto free_netdev;
89 	}
90 	rtllib_networks_initialize(ieee);
91 
92 	/* Default fragmentation threshold is maximum payload size */
93 	ieee->fts = DEFAULT_FTS;
94 	ieee->scan_age = DEFAULT_MAX_SCAN_AGE;
95 	ieee->open_wep = 1;
96 
97 	ieee->ieee802_1x = 1; /* Default to supporting 802.1x */
98 
99 	ieee->rtllib_ap_sec_type = rtllib_ap_sec_type;
100 
101 	spin_lock_init(&ieee->lock);
102 	spin_lock_init(&ieee->wpax_suitlist_lock);
103 	spin_lock_init(&ieee->reorder_spinlock);
104 	atomic_set(&ieee->atm_swbw, 0);
105 
106 	/* SAM FIXME */
107 	lib80211_crypt_info_init(&ieee->crypt_info, "RTLLIB", &ieee->lock);
108 
109 	ieee->wpa_enabled = 0;
110 	ieee->tkip_countermeasures = 0;
111 	ieee->drop_unencrypted = 0;
112 	ieee->privacy_invoked = 0;
113 	ieee->ieee802_1x = 1;
114 	ieee->hwsec_active = 0;
115 
116 	memset(ieee->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
117 	err = rtllib_softmac_init(ieee);
118 	if (err)
119 		goto free_crypt_info;
120 
121 	ieee->ht_info = kzalloc(sizeof(struct rt_hi_throughput), GFP_KERNEL);
122 	if (!ieee->ht_info)
123 		goto free_softmac;
124 
125 	ht_update_default_setting(ieee);
126 	HTInitializeHTInfo(ieee);
127 	rtllib_ts_init(ieee);
128 	for (i = 0; i < IEEE_IBSS_MAC_HASH_SIZE; i++)
129 		INIT_LIST_HEAD(&ieee->ibss_mac_hash[i]);
130 
131 	for (i = 0; i < 17; i++) {
132 		ieee->last_rxseq_num[i] = -1;
133 		ieee->last_rxfrag_num[i] = -1;
134 		ieee->last_packet_time[i] = 0;
135 	}
136 
137 	return dev;
138 
139 free_softmac:
140 	rtllib_softmac_free(ieee);
141 free_crypt_info:
142 	lib80211_crypt_info_free(&ieee->crypt_info);
143 	rtllib_networks_free(ieee);
144 free_netdev:
145 	free_netdev(dev);
146 
147 	return NULL;
148 }
149 EXPORT_SYMBOL(alloc_rtllib);
150 
151 void free_rtllib(struct net_device *dev)
152 {
153 	struct rtllib_device *ieee = (struct rtllib_device *)
154 				      netdev_priv_rsl(dev);
155 
156 	kfree(ieee->ht_info);
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