1 /*
2 * IE/TLV fragmentation/defragmentation support for
3 * Broadcom 802.11bang Networking Device Driver
4 *
5 * Copyright (C) 1999-2019, 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
17 * of the license of that module. An independent module is a module which is
18 * not derived from this software. The special exception does not apply to any
19 * modifications of the software.
20 *
21 * Notwithstanding the above, under no circumstances may you combine this
22 * software in any way with any other Broadcom software provided under a license
23 * other than the GPL, without Broadcom's express prior written consent.
24 * $Id$
25 *
26 * <<Broadcom-WL-IPTag/Open:>>
27 */
28
29 #include <bcmutils.h>
30 #include <frag.h>
31 #include <802.11.h>
32
33 /* defrag a fragmented dot11 ie/tlv. if space does not permit, return the needed
34 * ie length to contain all the fragments with status BCME_BUFTOOSHORT.
35 * out_len is in/out parameter, max length on input, used/required length on
36 * output
37 */
bcm_tlv_dot11_defrag(const void * buf,uint buf_len,uint8 id,bool id_ext,uint8 * out,uint * out_len)38 int bcm_tlv_dot11_defrag(const void *buf, uint buf_len, uint8 id, bool id_ext,
39 uint8 *out, uint *out_len)
40 {
41 int err = BCME_OK;
42 const bcm_tlv_t *ie;
43 uint tot_len = 0;
44 uint out_left;
45
46 /* find the ie; includes validation */
47 ie = bcm_parse_tlvs_dot11(buf, buf_len, id, id_ext);
48 if (!ie) {
49 err = BCME_IE_NOTFOUND;
50 goto done;
51 }
52
53 out_left = (out && out_len) ? *out_len : 0;
54
55 /* first fragment */
56 tot_len = id_ext ? ie->len - 1 : ie->len;
57
58 /* copy out if output space permits */
59 if (out_left < tot_len) {
60 err = BCME_BUFTOOSHORT;
61 out_left = 0; /* prevent further copy */
62 } else {
63 memcpy(out, &ie->data[id_ext ? 1 : 0], tot_len);
64 out += tot_len;
65 out_left -= tot_len;
66 }
67
68 /* if not fragmened or not fragmentable per 802.11 table 9-77 11md0.1 bail
69 * we can introduce the latter check later
70 */
71 if (ie->len != BCM_TLV_MAX_DATA_SIZE) {
72 goto done;
73 }
74
75 /* adjust buf_len to length after ie including it */
76 buf_len -= (uint)(((const uint8 *)ie - (const uint8 *)buf));
77
78 /* update length from fragments, okay if no next ie */
79 while ((ie = bcm_next_tlv(ie, &buf_len)) &&
80 (ie->id == DOT11_MNG_FRAGMENT_ID)) {
81 /* note: buf_len starts at next ie and last frag may be partial */
82 if (out_left < ie->len) {
83 err = BCME_BUFTOOSHORT;
84 out_left = 0;
85 } else {
86 memcpy(out, &ie->data[0], ie->len);
87 out += ie->len;
88 out_left -= ie->len;
89 }
90
91 tot_len += ie->len + BCM_TLV_HDR_SIZE;
92
93 /* all but last should be of max size */
94 if (ie->len < BCM_TLV_MAX_DATA_SIZE) {
95 break;
96 }
97 }
98
99 done:
100 if (out_len) {
101 *out_len = tot_len;
102 }
103
104 return err;
105 }
106
bcm_tlv_dot11_frag_tot_len(const void * buf,uint buf_len,uint8 id,bool id_ext,uint * ie_len)107 int bcm_tlv_dot11_frag_tot_len(const void *buf, uint buf_len, uint8 id,
108 bool id_ext, uint *ie_len)
109 {
110 return bcm_tlv_dot11_defrag(buf, buf_len, id, id_ext, NULL, ie_len);
111 }
112