xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/crypto/src/wlan_nl_to_crypto_params.c (revision 97f44cd39e4ff816eaa1710279d28cf6b9e65ad9)
1 /*
2  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: wlan_nl_to_crypto_params.c
21  *
22  * Conversion of NL param type to Crypto param type APIs implementation
23  *
24  */
25 
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/version.h>
29 #include <linux/netdevice.h>
30 #include <net/netlink.h>
31 #include <net/cfg80211.h>
32 
33 #include <qdf_types.h>
34 #include "wlan_objmgr_vdev_obj.h"
35 #include <qdf_module.h>
36 
37 #include "wlan_nl_to_crypto_params.h"
38 #include "wlan_crypto_global_def.h"
39 
40 /**
41  * struct osif_akm_crypto_mapping - mapping akm type received from
42  *                                 NL to internal crypto type
43  * @akm_suite: NL akm type
44  * @akm_type_crypto: akm crypto type
45  *
46  * mapping akm type received from NL to internal crypto type
47  */
48 struct osif_akm_type_crypto_mapping {
49 	u32 akm_suite;
50 	wlan_crypto_key_mgmt akm_type_crypto;
51 };
52 
53 /**
54  * struct osif_cipher_crypto_mapping - mapping cipher type received from NL
55  *                                    to internal crypto cipher type
56  * @cipher_suite: NL cipher type
57  * @cipher_crypto: cipher crypto type
58  * @cipher_len: Length of the cipher
59  *
60  * mapping cipher type received from NL to internal crypto cipher type
61  */
62 struct osif_cipher_crypto_mapping {
63 	u32 cipher_suite;
64 	wlan_crypto_cipher_type cipher_crypto;
65 	u32 cipher_len;
66 };
67 
68 /**
69  * mapping table for auth type received from NL and cryto auth type
70  */
71 static const wlan_crypto_auth_mode
72 	osif_auth_type_crypto_mapping[] = {
73 	[NL80211_AUTHTYPE_AUTOMATIC] = WLAN_CRYPTO_AUTH_AUTO,
74 	[NL80211_AUTHTYPE_OPEN_SYSTEM] = WLAN_CRYPTO_AUTH_OPEN,
75 	[NL80211_AUTHTYPE_FT] = WLAN_CRYPTO_AUTH_OPEN,
76 	[NL80211_AUTHTYPE_SHARED_KEY] = WLAN_CRYPTO_AUTH_SHARED,
77 	[NL80211_AUTHTYPE_NETWORK_EAP] = WLAN_CRYPTO_AUTH_8021X,
78 #if defined(WLAN_FEATURE_FILS_SK) && \
79 	(defined(CFG80211_FILS_SK_OFFLOAD_SUPPORT) || \
80 		 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)))
81 	[NL80211_AUTHTYPE_FILS_SK] = WLAN_CRYPTO_AUTH_FILS_SK,
82 #endif
83 	[NL80211_AUTHTYPE_SAE] = WLAN_CRYPTO_AUTH_SAE,
84 };
85 
86 /* mapping table for akm type received from NL and cryto akm type */
87 static const struct osif_akm_type_crypto_mapping
88 	osif_akm_type_crypto_mapping[] = {
89 	{
90 		.akm_suite = WLAN_AKM_SUITE_8021X,
91 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X,
92 	},
93 	{
94 		.akm_suite = WLAN_AKM_SUITE_PSK,
95 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_PSK,
96 	},
97 	{
98 		.akm_suite = WLAN_AKM_SUITE_8021X_SHA256,
99 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SHA256,
100 	},
101 	{
102 		.akm_suite = WLAN_AKM_SUITE_PSK_SHA256,
103 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_PSK_SHA256,
104 	},
105 	{
106 		.akm_suite = WLAN_AKM_SUITE_SAE,
107 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_SAE,
108 	},
109 	{
110 		.akm_suite = WLAN_AKM_SUITE_FT_OVER_SAE,
111 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_SAE,
112 	},
113 #if defined(WLAN_AKM_SUITE_FT_8021X) || \
114 			defined(FEATURE_WLAN_FT_IEEE8021X)
115 	{
116 		.akm_suite = WLAN_AKM_SUITE_FT_8021X,
117 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_IEEE8021X,
118 	},
119 #endif
120 #if defined(WLAN_AKM_SUITE_FT_PSK) || \
121 			defined(FEATURE_WLAN_FT_PSK)
122 	{
123 		.akm_suite = WLAN_AKM_SUITE_FT_PSK,
124 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_PSK,
125 	},
126 #endif
127 #ifdef FEATURE_WLAN_ESE
128 	{
129 #ifndef WLAN_AKM_SUITE_CCKM
130 #define WLAN_AKM_SUITE_CCKM         0x00409600
131 #endif
132 		.akm_suite = WLAN_AKM_SUITE_CCKM,
133 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_CCKM,
134 	},
135 #endif
136 	{
137 #ifndef WLAN_AKM_SUITE_OSEN
138 #define WLAN_AKM_SUITE_OSEN         0x506f9a01
139 #endif
140 		.akm_suite = WLAN_AKM_SUITE_OSEN,
141 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_OSEN,
142 	},
143 #if defined(WLAN_AKM_SUITE_8021X_SUITE_B) || \
144 		defined(FEATURE_WLAN_IEEE8021X_SUITE_B)
145 	{
146 		.akm_suite = WLAN_AKM_SUITE_8021X_SUITE_B,
147 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SUITE_B,
148 	},
149 #endif
150 #if defined(WLAN_AKM_SUITE_8021X_SUITE_B_192) || \
151 		defined(FEATURE_WLAN_IEEE8021X_SUITE_B)
152 	{
153 		.akm_suite = WLAN_AKM_SUITE_8021X_SUITE_B_192,
154 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SUITE_B_192,
155 	},
156 #endif
157 #if defined(WLAN_AKM_SUITE_FILS_SHA256) || \
158 				defined(FEATURE_WLAN_FILS)
159 	{
160 		.akm_suite = WLAN_AKM_SUITE_FILS_SHA256,
161 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FILS_SHA256,
162 	},
163 #endif
164 #if defined(WLAN_AKM_SUITE_FILS_SHA384) || \
165 				defined(FEATURE_WLAN_FILS)
166 	{
167 		.akm_suite = WLAN_AKM_SUITE_FILS_SHA384,
168 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FILS_SHA384,
169 	},
170 #endif
171 #if defined(WLAN_AKM_SUITE_FT_FILS_SHA256) || \
172 				defined(FEATURE_WLAN_FILS)
173 	{
174 		.akm_suite = WLAN_AKM_SUITE_FT_FILS_SHA256,
175 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_FILS_SHA256,
176 	},
177 #endif
178 #if defined(WLAN_AKM_SUITE_FT_FILS_SHA384) || \
179 				defined(FEATURE_WLAN_FILS)
180 	{
181 		.akm_suite = WLAN_AKM_SUITE_FT_FILS_SHA384,
182 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_FILS_SHA384,
183 	},
184 #endif
185 	{
186 #ifndef WLAN_AKM_SUITE_OWE
187 #define WLAN_AKM_SUITE_OWE          0x000FAC12
188 #endif
189 		.akm_suite = WLAN_AKM_SUITE_OWE,
190 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_OWE,
191 	},
192 	{
193 #ifndef WLAN_AKM_SUITE_DPP
194 #define WLAN_AKM_SUITE_DPP      0x506f9a02
195 #endif
196 		.akm_suite = WLAN_AKM_SUITE_DPP,
197 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_DPP,
198 	},
199 };
200 
201 /* mapping table for cipher type received from NL and cryto cipher type */
202 static const struct osif_cipher_crypto_mapping
203 	osif_cipher_crypto_mapping[] = {
204 	{
205 		.cipher_suite = IW_AUTH_CIPHER_NONE,
206 		.cipher_crypto = WLAN_CRYPTO_CIPHER_NONE,
207 		.cipher_len = 0,
208 	},
209 	{
210 		.cipher_suite = WLAN_CIPHER_SUITE_WEP40,
211 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WEP_40,
212 		.cipher_len = WLAN_CRYPTO_KEY_WEP40_LEN,
213 	},
214 	{
215 		.cipher_suite = WLAN_CIPHER_SUITE_TKIP,
216 		.cipher_crypto = WLAN_CRYPTO_CIPHER_TKIP,
217 		.cipher_len = WLAN_CRYPTO_KEY_TKIP_LEN,
218 	},
219 	{
220 		.cipher_suite = WLAN_CIPHER_SUITE_CCMP,
221 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CCM,
222 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_LEN,
223 	},
224 	{
225 		.cipher_suite = WLAN_CIPHER_SUITE_WEP104,
226 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WEP_104,
227 		.cipher_len = WLAN_CRYPTO_KEY_WEP104_LEN,
228 	},
229 	{
230 		.cipher_suite = WLAN_CIPHER_SUITE_GCMP,
231 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GCM,
232 		.cipher_len = WLAN_CRYPTO_KEY_GCMP_LEN,
233 	},
234 	{
235 		.cipher_suite = WLAN_CIPHER_SUITE_GCMP_256,
236 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GCM_256,
237 		.cipher_len = WLAN_CRYPTO_KEY_GCMP_256_LEN,
238 	},
239 	{
240 		.cipher_suite = WLAN_CIPHER_SUITE_CCMP_256,
241 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CCM_256,
242 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_256_LEN,
243 	},
244 	{
245 		.cipher_suite = WLAN_CIPHER_SUITE_AES_CMAC,
246 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CMAC,
247 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_LEN,
248 	},
249 #ifdef WLAN_CIPHER_SUITE_BIP_GMAC_128
250 	{
251 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_GMAC_128,
252 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GMAC,
253 		.cipher_len = WLAN_CRYPTO_KEY_GMAC_LEN,
254 	},
255 #endif
256 #ifdef WLAN_CIPHER_SUITE_BIP_GMAC_256
257 	{
258 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_GMAC_256,
259 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GMAC_256,
260 		.cipher_len = WLAN_CRYPTO_KEY_GMAC_256_LEN,
261 	},
262 #endif
263 #ifdef WLAN_CIPHER_SUITE_BIP_CMAC_256
264 	{
265 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_CMAC_256,
266 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CMAC_256,
267 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_256_LEN,
268 	},
269 #endif
270 #ifdef FEATURE_WLAN_WAPI
271 	{
272 		.cipher_suite = WLAN_CIPHER_SUITE_SMS4,
273 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WAPI_SMS4,
274 		.cipher_len = WLAN_CRYPTO_KEY_WAPI_LEN,
275 	},
276 #endif
277 };
278 
279 wlan_crypto_auth_mode
280 osif_nl_to_crypto_auth_type(enum nl80211_auth_type auth_type)
281 {
282 	wlan_crypto_auth_mode crypto_auth_type = WLAN_CRYPTO_AUTH_NONE;
283 
284 	if (auth_type < NL80211_AUTHTYPE_OPEN_SYSTEM ||
285 	    auth_type >= QDF_ARRAY_SIZE(osif_auth_type_crypto_mapping)) {
286 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
287 				auth_type);
288 		return crypto_auth_type;
289 	}
290 
291 	crypto_auth_type = osif_auth_type_crypto_mapping[auth_type];
292 	QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Auth type, NL: %d, crypto: %d",
293 			auth_type, crypto_auth_type);
294 
295 	return crypto_auth_type;
296 }
297 
298 wlan_crypto_key_mgmt osif_nl_to_crypto_akm_type(u32 key_mgmt)
299 {
300 	uint8_t index;
301 	wlan_crypto_key_mgmt crypto_akm_type = WLAN_CRYPTO_KEY_MGMT_NONE;
302 	bool akm_type_crypto_exist = false;
303 
304 	for (index = 0; index < QDF_ARRAY_SIZE(osif_akm_type_crypto_mapping);
305 	     index++) {
306 		if (osif_akm_type_crypto_mapping[index].akm_suite == key_mgmt) {
307 			crypto_akm_type = osif_akm_type_crypto_mapping[index].
308 							akm_type_crypto;
309 			akm_type_crypto_exist = true;
310 			break;
311 		}
312 	}
313 	if (!akm_type_crypto_exist)
314 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
315 				key_mgmt);
316 	else
317 		QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Akm suite, NL: %d, crypto: %d",
318 				key_mgmt, crypto_akm_type);
319 
320 	return crypto_akm_type;
321 }
322 
323 enum wlan_crypto_cipher_type osif_nl_to_crypto_cipher_type(u32 cipher)
324 {
325 	uint8_t index;
326 	bool cipher_crypto_exist = false;
327 	wlan_crypto_cipher_type crypto_cipher_type = WLAN_CRYPTO_CIPHER_NONE;
328 
329 	for (index = 0; index < QDF_ARRAY_SIZE(osif_cipher_crypto_mapping);
330 	     index++) {
331 		if (osif_cipher_crypto_mapping[index].cipher_suite == cipher) {
332 			crypto_cipher_type = osif_cipher_crypto_mapping[index].
333 								cipher_crypto;
334 			cipher_crypto_exist = true;
335 			break;
336 		}
337 	}
338 	if (!cipher_crypto_exist) {
339 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
340 				cipher);
341 		return WLAN_CRYPTO_CIPHER_INVALID;
342 	}
343 	QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Cipher suite, NL: %d, crypto: %d",
344 			cipher, crypto_cipher_type);
345 
346 	return crypto_cipher_type;
347 }
348 
349 int osif_nl_to_crypto_cipher_len(u32 cipher)
350 {
351 	uint8_t index;
352 
353 	for (index = 0; index < QDF_ARRAY_SIZE(osif_cipher_crypto_mapping);
354 	     index++) {
355 		if (osif_cipher_crypto_mapping[index].cipher_suite == cipher)
356 			return osif_cipher_crypto_mapping[index].cipher_len;
357 	}
358 
359 	return -EINVAL;
360 }
361 
362