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