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