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