1 /*
2  * Copyright (c) 2017-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: Private definitions for handling crypto params
22  */
23 #ifndef _WLAN_CRYPTO_DEF_I_H_
24 #define _WLAN_CRYPTO_DEF_I_H_
25 
26 #include <wlan_cmn_ieee80211.h>
27 #include "wlan_objmgr_pdev_obj.h"
28 #include "wlan_crypto_global_def.h"
29 #ifdef WLAN_CRYPTO_AES
30 #include "wlan_crypto_aes_i.h"
31 #endif
32 
33 /* Number of bits per byte */
34 #define CRYPTO_NBBY  8
35 /* Default link id for legacy connection */
36 #define CRYPTO_MAX_LINK_IDX 0xFF
37 
38 /* Macros for handling unaligned memory accesses */
39 
wlan_crypto_get_be16(const uint8_t * a)40 static inline uint16_t wlan_crypto_get_be16(const uint8_t *a)
41 {
42 	return (a[0] << 8) | a[1];
43 }
44 
wlan_crypto_put_be16(uint8_t * a,uint16_t val)45 static inline void wlan_crypto_put_be16(uint8_t *a, uint16_t val)
46 {
47 	a[0] = val >> 8;
48 	a[1] = val & 0xff;
49 }
50 
wlan_crypto_get_le16(const uint8_t * a)51 static inline uint16_t wlan_crypto_get_le16(const uint8_t *a)
52 {
53 	return (a[1] << 8) | a[0];
54 }
55 
wlan_crypto_put_le16(uint8_t * a,uint16_t val)56 static inline void wlan_crypto_put_le16(uint8_t *a, uint16_t val)
57 {
58 	a[1] = val >> 8;
59 	a[0] = val & 0xff;
60 }
61 
wlan_crypto_get_be32(const uint8_t * a)62 static inline uint32_t wlan_crypto_get_be32(const uint8_t *a)
63 {
64 	return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
65 }
66 
wlan_crypto_put_be32(uint8_t * a,uint32_t val)67 static inline void wlan_crypto_put_be32(uint8_t *a, uint32_t val)
68 {
69 	a[0] = (val >> 24) & 0xff;
70 	a[1] = (val >> 16) & 0xff;
71 	a[2] = (val >> 8) & 0xff;
72 	a[3] = val & 0xff;
73 }
74 
wlan_crypto_get_le32(const uint8_t * a)75 static inline uint32_t wlan_crypto_get_le32(const uint8_t *a)
76 {
77 	return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
78 }
79 
wlan_crypto_put_le32(uint8_t * a,uint32_t val)80 static inline void wlan_crypto_put_le32(uint8_t *a, uint32_t val)
81 {
82 	a[3] = (val >> 24) & 0xff;
83 	a[2] = (val >> 16) & 0xff;
84 	a[1] = (val >> 8) & 0xff;
85 	a[0] = val & 0xff;
86 }
87 
wlan_crypto_put_be64(u8 * a,u64 val)88 static inline void wlan_crypto_put_be64(u8 *a, u64 val)
89 {
90 	a[0] = val >> 56;
91 	a[1] = val >> 48;
92 	a[2] = val >> 40;
93 	a[3] = val >> 32;
94 	a[4] = val >> 24;
95 	a[5] = val >> 16;
96 	a[6] = val >> 8;
97 	a[7] = val & 0xff;
98 }
99 
100 #define WLAN_CRYPTO_TX_OPS_ALLOCKEY(tx_ops) \
101 	((tx_ops)->crypto_tx_ops.allockey)
102 #define WLAN_CRYPTO_TX_OPS_SETKEY(tx_ops) \
103 	((tx_ops)->crypto_tx_ops.setkey)
104 #define WLAN_CRYPTO_TX_OPS_DELKEY(tx_ops) \
105 	((tx_ops)->crypto_tx_ops.delkey)
106 #define WLAN_CRYPTO_TX_OPS_DEFAULTKEY(tx_ops) \
107 	((tx_ops)->crypto_tx_ops.defaultkey)
108 #define WLAN_CRYPTO_TX_OPS_SET_KEY(tx_ops) \
109 	((tx_ops)->crypto_tx_ops.set_key)
110 #define WLAN_CRYPTO_TX_OPS_SET_VDEV_PARAM(tx_ops) \
111 	((tx_ops)->crypto_tx_ops.set_vdev_param)
112 #define WLAN_CRYPTO_TX_OPS_GETPN(tx_ops) \
113 	((tx_ops)->crypto_tx_ops.getpn)
114 #define WLAN_CRYPTO_TX_OPS_SET_LTF_KEYSEED(tx_ops) \
115 	((tx_ops)->crypto_tx_ops.set_ltf_keyseed)
116 #define WLAN_CRYPTO_TX_OPS_REGISTER_EVENTS(tx_ops) \
117 	((tx_ops)->crypto_tx_ops.register_events)
118 #define WLAN_CRYPTO_TX_OPS_DEREGISTER_EVENTS(tx_ops) \
119 	((tx_ops)->crypto_tx_ops.deregister_events)
120 
121 /* unaligned little endian access */
122 #ifndef LE_READ_2
123 #define LE_READ_2(p) \
124 	((uint16_t)                          \
125 	((((const uint8_t *)(p))[0]) |       \
126 	(((const uint8_t *)(p))[1] <<  8)))
127 #endif
128 
129 #ifndef LE_READ_4
130 #define LE_READ_4(p)                   \
131 	((uint32_t)                          \
132 	((((const uint8_t *)(p))[0]) |       \
133 	(((const uint8_t *)(p))[1] <<  8) |  \
134 	(((const uint8_t *)(p))[2] << 16) |  \
135 	(((const uint8_t *)(p))[3] << 24)))
136 #endif
137 
138 #ifndef BE_READ_4
139 #define BE_READ_4(p)                        \
140 	((uint32_t)                              \
141 	((((const uint8_t *)(p))[0] << 24) |     \
142 	(((const uint8_t *)(p))[1] << 16) |      \
143 	(((const uint8_t *)(p))[2] <<  8) |      \
144 	(((const uint8_t *)(p))[3])))
145 #endif
146 
147 #ifndef READ_6
148 #define READ_6(b0, b1, b2, b3, b4, b5)  ({ \
149 	uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);\
150 	uint16_t iv16 = (b4 << 0) | (b5 << 8);\
151 	(((uint64_t)iv16) << 32) | iv32;\
152 })
153 #endif
154 
155 #define OUI_SIZE   (4)
156 #define WLAN_CRYPTO_ADDSHORT(frm, v)  \
157 	do {frm[0] = (v) & 0xff; frm[1] = (v) >> 8; frm += 2; } while (0)
158 
159 #define	WLAN_CRYPTO_ADDSELECTOR(frm, sel) \
160 	do { \
161 		uint32_t value = sel;\
162 		qdf_mem_copy(frm, (uint8_t *)&value, OUI_SIZE); \
163 	frm += OUI_SIZE; } while (0)
164 
165 #define WLAN_CRYPTO_SELECTOR(a, b, c, d) \
166 	((((uint32_t) (a)) << 24) | \
167 	 (((uint32_t) (b)) << 16) | \
168 	 (((uint32_t) (c)) << 8) | \
169 		(uint32_t) (d))
170 
171 #define WPA_TYPE_OUI                    WLAN_WPA_SEL(WLAN_WPA_OUI_TYPE)
172 
173 #define WLAN_CRYPTO_WAPI_IE_LEN      20
174 #define WLAN_CRYPTO_WAPI_SMS4_CIPHER 0x01
175 
176 #define WPA_AUTH_KEY_MGMT_NONE          WLAN_WPA_SEL(WLAN_ASE_NONE)
177 #define WPA_AUTH_KEY_MGMT_UNSPEC_802_1X WLAN_WPA_SEL(WLAN_ASE_8021X_UNSPEC)
178 #define WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X \
179 					WLAN_WPA_SEL(WLAN_ASE_8021X_PSK)
180 #define WPA_AUTH_KEY_MGMT_CCKM WLAN_WPA_CCKM_AKM
181 
182 #define WPA_CIPHER_SUITE_NONE   WLAN_WPA_SEL(WLAN_CSE_NONE)
183 #define WPA_CIPHER_SUITE_WEP40  WLAN_WPA_SEL(WLAN_CSE_WEP40)
184 #define WPA_CIPHER_SUITE_WEP104 WLAN_WPA_SEL(WLAN_CSE_WEP104)
185 #define WPA_CIPHER_SUITE_TKIP   WLAN_WPA_SEL(WLAN_CSE_TKIP)
186 #define WPA_CIPHER_SUITE_CCMP   WLAN_WPA_SEL(WLAN_CSE_CCMP)
187 
188 #define RSN_AUTH_KEY_MGMT_NONE          WLAN_RSN_SEL(0)
189 #define RSN_AUTH_KEY_MGMT_UNSPEC_802_1X WLAN_RSN_SEL(1)
190 #define RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X\
191 					WLAN_RSN_SEL(2)
192 #define RSN_AUTH_KEY_MGMT_FT_802_1X     WLAN_RSN_SEL(3)
193 #define RSN_AUTH_KEY_MGMT_FT_PSK        WLAN_RSN_SEL(4)
194 #define RSN_AUTH_KEY_MGMT_802_1X_SHA256\
195 					WLAN_RSN_SEL(5)
196 #define RSN_AUTH_KEY_MGMT_PSK_SHA256    WLAN_RSN_SEL(6)
197 #define RSN_AUTH_KEY_MGMT_WPS           WLAN_RSN_SEL(7)
198 #define RSN_AUTH_KEY_MGMT_SAE           WLAN_RSN_SEL(8)
199 #define RSN_AUTH_KEY_MGMT_FT_SAE        WLAN_RSN_SEL(9)
200 #define RSN_AUTH_KEY_MGMT_802_1X_SUITE_B\
201 					WLAN_RSN_SEL(11)
202 #define RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192\
203 					WLAN_RSN_SEL(12)
204 #define RSN_AUTH_KEY_MGMT_FT_802_1X_SUITE_B_384\
205 					WLAN_RSN_SEL(13)
206 #define RSN_AUTH_KEY_MGMT_FILS_SHA256   WLAN_RSN_SEL(14)
207 #define RSN_AUTH_KEY_MGMT_FILS_SHA384   WLAN_RSN_SEL(15)
208 #define RSN_AUTH_KEY_MGMT_FT_FILS_SHA256\
209 					WLAN_RSN_SEL(16)
210 #define RSN_AUTH_KEY_MGMT_FT_FILS_SHA384\
211 					WLAN_RSN_SEL(17)
212 #define RSN_AUTH_KEY_MGMT_OWE           WLAN_RSN_SEL(18)
213 #define RSN_AUTH_KEY_MGMT_FT_PSK_SHA384 WLAN_RSN_SEL(19)
214 #define RSN_AUTH_KEY_MGMT_PSK_SHA384    WLAN_RSN_SEL(20)
215 #define RSN_AUTH_KEY_MGMT_SAE_EXT_KEY   WLAN_RSN_SEL(24)
216 #define RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY WLAN_RSN_SEL(25)
217 
218 #define RSN_AUTH_KEY_MGMT_CCKM          (WLAN_RSN_CCKM_AKM)
219 #define RSN_AUTH_KEY_MGMT_OSEN          (0x019a6f50)
220 #define RSN_AUTH_KEY_MGMT_DPP           (WLAN_RSN_DPP_AKM)
221 
222 #define RSN_CIPHER_SUITE_NONE           WLAN_RSN_SEL(WLAN_CSE_NONE)
223 #define RSN_CIPHER_SUITE_WEP40          WLAN_RSN_SEL(WLAN_CSE_WEP40)
224 #define RSN_CIPHER_SUITE_TKIP           WLAN_RSN_SEL(WLAN_CSE_TKIP)
225 #define RSN_CIPHER_SUITE_WEP104         WLAN_RSN_SEL(WLAN_CSE_WEP104)
226 #define RSN_CIPHER_SUITE_CCMP           WLAN_RSN_SEL(WLAN_CSE_CCMP)
227 #define RSN_CIPHER_SUITE_AES_CMAC       WLAN_RSN_SEL(WLAN_CSE_AES_CMAC)
228 #define RSN_CIPHER_SUITE_GCMP           WLAN_RSN_SEL(WLAN_CSE_GCMP_128)
229 #define RSN_CIPHER_SUITE_GCMP_256       WLAN_RSN_SEL(WLAN_CSE_GCMP_256)
230 #define RSN_CIPHER_SUITE_CCMP_256       WLAN_RSN_SEL(WLAN_CSE_CCMP_256)
231 #define RSN_CIPHER_SUITE_BIP_GMAC_128   WLAN_RSN_SEL(WLAN_CSE_BIP_GMAC_128)
232 #define RSN_CIPHER_SUITE_BIP_GMAC_256   WLAN_RSN_SEL(WLAN_CSE_BIP_GMAC_256)
233 #define RSN_CIPHER_SUITE_BIP_CMAC_256   WLAN_RSN_SEL(WLAN_CSE_BIP_CMAC_256)
234 
235 #define RESET_PARAM(__param)         ((__param) = 0)
236 #define SET_PARAM(__param, __val)    ((__param) |= (1 << (__val)))
237 #define HAS_PARAM(__param, __val)    ((__param) &  (1 << (__val)))
238 #define CLEAR_PARAM(__param, __val)  ((__param) &= (~(1 << (__val))))
239 
240 
241 #define RESET_AUTHMODE(_param)       ((_param)->authmodeset = 0)
242 
243 #define SET_AUTHMODE(_param, _mode)  ((_param)->authmodeset |= (1 << (_mode)))
244 #define HAS_AUTHMODE(_param, _mode)  ((_param)->authmodeset &  (1 << (_mode)))
245 
246 #define AUTH_IS_OPEN(_param)   HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_OPEN)
247 #define AUTH_IS_SHARED_KEY(_param)  \
248 				HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_SHARED)
249 #define AUTH_IS_8021X(_param)  HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_8021X)
250 #define AUTH_IS_WPA(_param)    HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_WPA)
251 #define AUTH_IS_RSNA(_param)   HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_RSNA)
252 #define AUTH_IS_CCKM(_param)   HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_CCKM)
253 #define AUTH_IS_WAI(_param)    HAS_AUTHMODE((_param), WLAN_CRYPTO_AUTH_WAPI)
254 #define AUTH_IS_WPA2(_param)   AUTH_IS_RSNA(_param)
255 
256 #define AUTH_MATCH(_param1, _param2) \
257 		(((_param1)->authmodeset & (_param2)->authmodeset) != 0)
258 
259 
260 #define RESET_UCAST_CIPHERS(_param)   ((_param)->ucastcipherset = 0)
261 #define SET_UCAST_CIPHER(_param, _c)  ((_param)->ucastcipherset |= (1 << (_c)))
262 #define HAS_UCAST_CIPHER(_param, _c)  ((_param)->ucastcipherset & (1 << (_c)))
263 
264 #define UCIPHER_IS_CLEAR(_param)   \
265 		((_param)->ucastcipherset == 0)
266 #define UCIPHER_IS_WEP(_param)     \
267 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_WEP)
268 #define UCIPHER_IS_TKIP(_param)    \
269 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_TKIP)
270 #define UCIPHER_IS_CCMP128(_param) \
271 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CCM)
272 #define UCIPHER_IS_CCMP256(_param) \
273 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CCM_256)
274 #define UCIPHER_IS_GCMP128(_param) \
275 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GCM)
276 #define UCIPHER_IS_GCMP256(_param) \
277 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GCM_256)
278 #define UCIPHER_IS_SMS4(_param)    \
279 		HAS_UCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_WAPI_SMS4)
280 
281 #define RESET_MCAST_CIPHERS(_param)   ((_param)->mcastcipherset = 0)
282 #define SET_MCAST_CIPHER(_param, _c)  ((_param)->mcastcipherset |= (1 << (_c)))
283 #define HAS_MCAST_CIPHER(_param, _c)  ((_param)->mcastcipherset & (1 << (_c)))
284 #define HAS_ANY_MCAST_CIPHER(_param)  ((_param)->mcastcipherset)
285 #define CLEAR_MCAST_CIPHER(_param, _c)  \
286 			((_param)->mcastcipherset &= (~(1)<<(_c)))
287 
288 #define MCIPHER_IS_CLEAR(_param)   \
289 		((_param)->mcastcipherset == 0)
290 #define MCIPHER_IS_WEP(_param)     \
291 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_WEP)
292 #define MCIPHER_IS_TKIP(_param)    \
293 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_TKIP)
294 #define MCIPHER_IS_CCMP128(_param) \
295 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CCM)
296 #define MCIPHER_IS_CCMP256(_param) \
297 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CCM_256)
298 #define MCIPHER_IS_GCMP128(_param) \
299 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GCM)
300 #define MCIPHER_IS_GCMP256(_param) \
301 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GCM_256)
302 #define MCIPHER_IS_SMS4(_param)    \
303 		HAS_MCAST_CIPHER((_param), WLAN_CRYPTO_CIPHER_WAPI_SMS4)
304 
305 #define RESET_MGMT_CIPHERS(_param)   ((_param)->mgmtcipherset = 0)
306 #define SET_MGMT_CIPHER(_param, _c)  ((_param)->mgmtcipherset |= (1 << (_c)))
307 #define HAS_MGMT_CIPHER(_param, _c)  ((_param)->mgmtcipherset & (1 << (_c)))
308 #define IS_MGMT_CIPHER(_c)      ((_c == WLAN_CRYPTO_CIPHER_AES_CMAC) || \
309 				 (_c == WLAN_CRYPTO_CIPHER_AES_CMAC_256) || \
310 				 (_c == WLAN_CRYPTO_CIPHER_AES_GMAC) || \
311 				 (_c == WLAN_CRYPTO_CIPHER_AES_GMAC_256))
312 
313 #define IS_FILS_CIPHER(_c)      ((_c) == WLAN_CRYPTO_CIPHER_FILS_AEAD)
314 
315 #define MGMT_CIPHER_IS_CLEAR(_param)   \
316 		((_param)->mgmtcipherset == 0)
317 #define MGMT_CIPHER_IS_CMAC(_param)    \
318 		HAS_MGMT_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CMAC)
319 #define MGMT_CIPHER_IS_CMAC256(_param) \
320 		HAS_MGMT_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_CMAC_256)
321 #define MGMT_CIPHER_IS_GMAC(_param)    \
322 		HAS_MGMT_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GMAC)
323 #define MGMT_CIPHER_IS_GMAC256(_param) \
324 		HAS_MGMT_CIPHER((_param), WLAN_CRYPTO_CIPHER_AES_GMAC_256)
325 
326 #define RESET_KEY_MGMT(_param)   ((_param)->key_mgmt = 0)
327 #define SET_KEY_MGMT(_param, _c)  ((_param)->key_mgmt |= (1 << (_c)))
328 #define HAS_KEY_MGMT(_param, _c)  ((_param)->key_mgmt & (1 << (_c)))
329 
330 #define UCAST_CIPHER_MATCH(_param1, _param2)    \
331 	(((_param1)->ucastcipherset & (_param2)->ucastcipherset) != 0)
332 
333 #define MCAST_CIPHER_MATCH(_param1, _param2)    \
334 	(((_param1)->mcastcipherset & (_param2)->mcastcipherset) != 0)
335 
336 #define MGMT_CIPHER_MATCH(_param1, _param2)    \
337 	(((_param1)->mgmtcipherset & (_param2)->mgmtcipherset) != 0)
338 
339 #define KEY_MGMTSET_MATCH(_param1, _param2)      \
340 	(((_param1)->key_mgmt & (_param2)->key_mgmt) != 0 ||    \
341 	(!(_param1)->key_mgmt && !(_param2)->key_mgmt))
342 
343 #define RESET_CIPHER_CAP(_param)   ((_param)->cipher_caps = 0)
344 #define SET_CIPHER_CAP(_param, _c)  ((_param)->cipher_caps |= (1 << (_c)))
345 #define HAS_CIPHER_CAP(_param, _c)  ((_param)->cipher_caps & (1 << (_c)))
346 #define HAS_ANY_CIPHER_CAP(_param)  ((_param)->cipher_caps)
347 
348 #define crypto_err(params...) QDF_TRACE_ERROR(QDF_MODULE_ID_CRYPTO, params)
349 #define crypto_info(params...) QDF_TRACE_INFO(QDF_MODULE_ID_CRYPTO, params)
350 #define crypto_debug(params...) QDF_TRACE_DEBUG(QDF_MODULE_ID_CRYPTO, params)
351 
352 /**
353  * struct wlan_crypto_mmie - MMIE IE
354  * @element_id:      element id
355  * @length:          length of the ie
356  * @key_id:          igtk key_id used
357  * @sequence_number: igtk PN number
358  * @mic:             MIC for the frame
359  *
360  * This structure represents Management MIC information element (IEEE 802.11w)
361  */
362 struct wlan_crypto_mmie {
363 	uint8_t  element_id;
364 	uint8_t  length;
365 	uint16_t key_id;
366 	uint8_t  sequence_number[6];
367 	uint8_t  mic[16];
368 } __packed;
369 
370 /**
371  * struct crypto_add_key_result - add key result structure
372  * @vdev_id: unique id identifying the VDEV
373  * @key_ix: key index
374  * @key_flags: key flags
375  * @status: status of add key
376  * @peer_macaddr: MAC address of the peer
377  *
378  * Structure used for add key result.
379  */
380 struct crypto_add_key_result {
381 	uint32_t vdev_id;
382 	uint32_t key_ix;
383 	uint32_t key_flags;
384 	uint32_t status;
385 	uint8_t peer_macaddr[QDF_MAC_ADDR_SIZE];
386 };
387 
388 /**
389  * typedef crypto_add_key_callback - add key callback
390  * @context: opaque context that the client can use to associate the
391  *  callback with the request
392  * @result: result of add key
393  */
394 typedef void (*crypto_add_key_callback)(void *context,
395 					struct crypto_add_key_result *result);
396 
397 /**
398  * struct wlan_crypto_comp_priv - crypto component private structure
399  * @crypto_params:    crypto params for the peer
400  * @crypto_key: crypto keys structure for the peer
401  * @fils_aead_set:    fils params for this peer
402  * @add_key_ctx: Opaque context to be used by the caller to associate the
403  *  add key request with the response
404  * @add_key_cb: Callback function to be called with the add key result
405  *
406  */
407 struct wlan_crypto_comp_priv {
408 	struct wlan_crypto_params crypto_params;
409 	struct wlan_crypto_keys crypto_key;
410 	uint8_t fils_aead_set;
411 	void *add_key_ctx;
412 	crypto_add_key_callback add_key_cb;
413 };
414 
415 /**
416  * struct wlan_crypto_cipher - crypto cipher table
417  * @cipher_name: printable name
418  * @cipher:      cipher type WLAN_CRYPTO_CIPHER_*
419  * @header:      size of privacy header (bytes)
420  * @trailer:     size of privacy trailer (bytes)
421  * @miclen:      size of mic trailer (bytes)
422  * @keylen:      max key length
423  * @setkey:      function pointer for setkey
424  * @encap:       function pointer for encap
425  * @decap:       function pointer for decap
426  * @enmic:       function pointer for enmic
427  * @demic:       function pointer for demic
428  *
429  */
430 struct wlan_crypto_cipher {
431 	const char *cipher_name;
432 	wlan_crypto_cipher_type cipher;
433 	const uint8_t   header;
434 	const uint8_t   trailer;
435 	const uint8_t   miclen;
436 	const uint32_t  keylen;
437 	QDF_STATUS(*setkey)(struct wlan_crypto_key *);
438 	QDF_STATUS(*encap)(struct wlan_crypto_key *,
439 				qdf_nbuf_t, uint8_t,  uint8_t);
440 	QDF_STATUS(*decap)(struct wlan_crypto_key *,
441 				qdf_nbuf_t, uint8_t,  uint8_t);
442 	QDF_STATUS(*enmic)(struct wlan_crypto_key *,
443 				qdf_nbuf_t, uint8_t,  uint8_t);
444 	QDF_STATUS(*demic)(struct wlan_crypto_key *,
445 				qdf_nbuf_t, uint8_t,  uint8_t);
446 };
447 
448 
449 /**
450  * wlan_crypto_is_data_protected - check is frame is protected or not
451  * @data: frame
452  *
453  * This function check is frame is protected or not
454  *
455  * Return: TRUE/FALSE
456  */
wlan_crypto_is_data_protected(const void * data)457 static inline bool wlan_crypto_is_data_protected(const void *data)
458 {
459 	const struct wlan_frame_hdr *hdr = (const struct wlan_frame_hdr *)data;
460 	if (hdr->i_fc[1] & WLAN_FC1_ISWEP)
461 		return true;
462 	else
463 		return false;
464 }
465 
466 /**
467  * ieee80211_hdrsize - calculate frame header size
468  * @data: frame
469  *
470  * This function calculate frame header size
471  *
472  * Return: header size of the frame
473  */
ieee80211_hdrsize(const void * data)474 static inline uint8_t ieee80211_hdrsize(const void *data)
475 {
476 	const struct wlan_frame_hdr *hdr = (const struct wlan_frame_hdr *)data;
477 	uint8_t size = sizeof(struct wlan_frame_hdr);
478 
479 	if ((hdr->i_fc[1] & WLAN_FC1_DIR_MASK)
480 				== (WLAN_FC1_DSTODS)) {
481 		size += QDF_MAC_ADDR_SIZE;
482 	}
483 
484 	if (((WLAN_FC0_GET_STYPE(hdr->i_fc[0])
485 			== WLAN_FC0_STYPE_QOS_DATA))) {
486 		size += sizeof(uint16_t);
487 		/* Qos frame with Order bit set indicates an HTC frame */
488 		if (hdr->i_fc[1] & WLAN_FC1_ORDER)
489 			size += (sizeof(uint8_t)*4);
490 	}
491 	if (((WLAN_FC0_GET_STYPE(hdr->i_fc[0])
492 			== WLAN_FC0_STYPE_ACTION))) {
493 		/* Action frame with Order bit set indicates an HTC frame */
494 		if (hdr->i_fc[1] & WLAN_FC1_ORDER)
495 			size += (sizeof(uint8_t)*4);
496 	}
497 	return size;
498 }
499 
500 /**
501  * ieee80211_hdrspace - calculate frame header size with padding
502  * @pdev: pdev
503  * @data: frame header
504  *
505  * This function returns the space occupied by the 802.11 header
506  * and any padding required by the driver. This works for a management
507  * or data frame.
508  *
509  * Return: header size of the frame with padding
510  */
511 static inline uint8_t
ieee80211_hdrspace(struct wlan_objmgr_pdev * pdev,const void * data)512 ieee80211_hdrspace(struct wlan_objmgr_pdev *pdev, const void *data)
513 {
514 	uint8_t size = ieee80211_hdrsize(data);
515 
516 	if (wlan_pdev_nif_feat_cap_get(pdev, WLAN_PDEV_F_DATAPAD))
517 		size = roundup(size, sizeof(u_int32_t));
518 
519 	return size;
520 }
521 
522 /**
523  * wlan_get_tid - get tid of the frame
524  * @data: frame
525  *
526  * This function get tid of the frame
527  *
528  * Return: tid of the frame
529  */
wlan_get_tid(const void * data)530 static inline int wlan_get_tid(const void *data)
531 {
532 	const struct wlan_frame_hdr *hdr = (const struct wlan_frame_hdr *)data;
533 
534 	if (((WLAN_FC0_GET_STYPE(hdr->i_fc[0])
535 				== WLAN_FC0_STYPE_QOS_DATA))) {
536 		if ((hdr->i_fc[1] & WLAN_FC1_DIR_MASK)
537 					== (WLAN_FC1_DSTODS)) {
538 			return ((struct wlan_frame_hdr_qos_addr4 *)data)->i_qos[0]
539 							& WLAN_QOS_TID_MASK;
540 		} else {
541 			return ((struct wlan_frame_hdr_qos *)data)->i_qos[0]
542 							& WLAN_QOS_TID_MASK;
543 		}
544 	} else
545 		return WLAN_NONQOS_SEQ;
546 }
547 
548 struct crypto_psoc_priv_obj {
549 	/** @crypto_key_lock: lock for crypto key table */
550 	qdf_mutex_t crypto_key_lock;
551 	/** @crypto_key_lock: lock for crypto key table */
552 	qdf_atomic_t crypto_key_cnt;
553 	struct {
554 		/** @mask: mask bits */
555 		uint32_t mask;
556 		/** @idx_bits: index to shift bits */
557 		uint32_t idx_bits;
558 		/** @bins: crypto key table */
559 		TAILQ_HEAD(, wlan_crypto_key_entry) * bins;
560 	} crypto_key_holder;
561 };
562 
563 /**
564  * struct pdev_crypto - pdev object structure for crypto
565  * @pdev_obj: pdev object
566  */
567 struct pdev_crypto {
568 	struct wlan_objmgr_pdev *pdev_obj;
569 };
570 
571 /**
572  * wlan_crypto_add_key_entry() - Add a filled key entry to the hashing
573  * framework
574  * @psoc: PSOC pointer
575  * @new_entry: New entry
576  *
577  * Return: QDF_STATUS
578  */
579 QDF_STATUS wlan_crypto_add_key_entry(struct wlan_objmgr_psoc *psoc,
580 				     struct wlan_crypto_key_entry *new_entry);
581 
582 /**
583  * crypto_add_entry - add key entry to hashing framework
584  * @psoc: psoc handler
585  * @link_id: link id
586  * @mac_addr: mac addr
587  * @crypto_key: crypto key
588  * @key_index: key index
589  * Return: zero on success
590  */
591 QDF_STATUS crypto_add_entry(struct crypto_psoc_priv_obj *psoc,
592 			    uint8_t link_id,
593 			    uint8_t *mac_addr,
594 			    struct wlan_crypto_key *crypto_key,
595 			    uint8_t key_index);
596 /**
597  * crypto_hash_find_by_linkid_and_macaddr - find crypto entry by link id
598  * @psoc: psoc handler
599  * @link_id: link id
600  * @mac_addr: mac addr
601  * Return: crypto key entry on success
602  */
603 struct
604 wlan_crypto_key_entry * crypto_hash_find_by_linkid_and_macaddr(
605 				struct crypto_psoc_priv_obj *psoc,
606 				uint8_t link_id,
607 				uint8_t *mac_addr);
608 
609 #ifdef WLAN_FEATURE_11BE_MLO_ADV_FEATURE
610 /**
611  * wlan_crypto_free_key_by_link_id - free key by link id
612  * @psoc: psoc handler
613  * @link_addr: link address
614  * @link_id: link id
615  */
616 void wlan_crypto_free_key_by_link_id(struct wlan_objmgr_psoc *psoc,
617 				     struct qdf_mac_addr *link_addr,
618 				     uint8_t link_id);
619 #else
620 static inline
wlan_crypto_free_key_by_link_id(struct wlan_objmgr_psoc * psoc,struct qdf_mac_addr * link_addr,uint8_t link_id)621 void wlan_crypto_free_key_by_link_id(struct wlan_objmgr_psoc *psoc,
622 				     struct qdf_mac_addr *link_addr,
623 				     uint8_t link_id)
624 {}
625 #endif /* WLAN_FEATURE_11BE_MLO_ADV_FEATURE */
626 #endif /* end of _WLAN_CRYPTO_DEF_I_H_ */
627