xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/frag.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * IE/TLV fragmentation/defragmentation support for
3  * Broadcom 802.11bang Networking Device Driver
4  *
5  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
6  *
7  * Copyright (C) 1999-2017, Broadcom Corporation
8  *
9  *      Unless you and Broadcom execute a separate written software license
10  * agreement governing use of this software, this software is licensed to you
11  * under the terms of the GNU General Public License version 2 (the "GPL"),
12  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
13  * following added to such license:
14  *
15  *      As a special exception, the copyright holders of this software give you
16  * permission to link this software with independent modules, and to copy and
17  * distribute the resulting executable under terms of your choice, provided that
18  * you also meet, for each linked independent module, the terms and conditions of
19  * the license of that module.  An independent module is a module which is not
20  * derived from this software.  The special exception does not apply to any
21  * modifications of the software.
22  *
23  *      Notwithstanding the above, under no circumstances may you combine this
24  * software in any way with any other Broadcom software provided under a license
25  * other than the GPL, without Broadcom's express prior written consent.
26  * $Id$
27  *
28  * <<Broadcom-WL-IPTag/Open:>>
29  */
30 
31 #include <bcmutils.h>
32 #include <frag.h>
33 #include <802.11.h>
34 
35 /* defrag a fragmented dot11 ie/tlv. if space does not permit, return the needed
36  * ie length to contain all the fragments with status BCME_BUFTOOSHORT.
37  * out_len is in/out parameter, max length on input, used/required length on output
38  */
39 int
bcm_tlv_dot11_defrag(const void * buf,uint buf_len,uint8 id,bool id_ext,uint8 * out,uint * out_len)40 bcm_tlv_dot11_defrag(const void *buf, uint buf_len, uint8 id, bool id_ext,
41 	uint8 *out, uint *out_len)
42 {
43 	int err = BCME_OK;
44 	const bcm_tlv_t *ie;
45 	uint tot_len = 0;
46 	uint out_left;
47 
48 	/* find the ie; includes validation */
49 	ie = bcm_parse_tlvs_dot11(buf, buf_len, id, id_ext);
50 	if (!ie) {
51 		err = BCME_IE_NOTFOUND;
52 		goto done;
53 	}
54 
55 	out_left =  (out && out_len) ? *out_len : 0;
56 
57 	/* first fragment */
58 	tot_len = id_ext ? ie->len - 1 : ie->len;
59 
60 	/* copy out if output space permits */
61 	if (out_left < tot_len) {
62 		err = BCME_BUFTOOSHORT;
63 		out_left = 0; /* prevent further copy */
64 	} else {
65 		memcpy(out, &ie->data[id_ext ? 1 : 0], tot_len);
66 		out += tot_len;
67 		out_left -= tot_len;
68 	}
69 
70 	/* if not fragmened or not fragmentable per 802.11 table  9-77 11md0.1 bail
71 	 * we can introduce the latter check later
72 	 */
73 	if (ie->len != BCM_TLV_MAX_DATA_SIZE) {
74 		goto done;
75 	}
76 
77 	/* adjust buf_len to length after ie including it */
78 	buf_len -= (uint)(((const uint8 *)ie - (const uint8 *)buf));
79 
80 	/* update length from fragments, okay if no next ie */
81 	while ((ie = bcm_next_tlv(ie, &buf_len)) &&
82 			(ie->id == DOT11_MNG_FRAGMENT_ID)) {
83 		/* note: buf_len starts at next ie and last frag may be partial */
84 		if (out_left < ie->len) {
85 			err = BCME_BUFTOOSHORT;
86 			out_left = 0;
87 		} else {
88 			memcpy(out, &ie->data[0], ie->len);
89 			out += ie->len;
90 			out_left -= ie->len;
91 		}
92 
93 		tot_len += ie->len + BCM_TLV_HDR_SIZE;
94 
95 		/* all but last should be of max size */
96 		if (ie->len < BCM_TLV_MAX_DATA_SIZE) {
97 			break;
98 		}
99 	}
100 
101 done:
102 	if (out_len) {
103 		*out_len = tot_len;
104 	}
105 
106 	return err;
107 }
108 
109 int
bcm_tlv_dot11_frag_tot_len(const void * buf,uint buf_len,uint8 id,bool id_ext,uint * ie_len)110 bcm_tlv_dot11_frag_tot_len(const void *buf, uint buf_len,
111 	uint8 id, bool id_ext, uint *ie_len)
112 {
113 	return  bcm_tlv_dot11_defrag(buf, buf_len, id, id_ext, NULL, ie_len);
114 }
115