xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/include/bcmtlv.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * TLV and XTLV support
3  *
4  * Copyright (C) 2020, Broadcom.
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *
21  * <<Broadcom-WL-IPTag/Dual:>>
22  */
23 
24 #ifndef	_bcmtlv_h_
25 #define	_bcmtlv_h_
26 
27 #include <typedefs.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32 
33 /* begin tlvs - used in 802.11 IEs etc. */
34 
35 /* type(aka id)/length/value buffer triple */
36 typedef struct bcm_tlv {
37 	uint8	id;
38 	uint8	len;
39 	uint8	data[1];
40 } bcm_tlv_t;
41 
42 /* size of tlv including data */
43 #define BCM_TLV_SIZE(_tlv) ((_tlv) ? (OFFSETOF(bcm_tlv_t, data) + (_tlv)->len) : 0u)
44 
45 /* get next tlv - no length checks */
46 #define BCM_TLV_NEXT(_tlv) (bcm_tlv_t *)((uint8 *)(_tlv)+ BCM_TLV_SIZE(_tlv))
47 
48 /* tlv length is restricted to 1 byte */
49 #define BCM_TLV_MAX_DATA_SIZE (255)
50 
51 /* tlv header - two bytes */
52 #define BCM_TLV_HDR_SIZE (OFFSETOF(bcm_tlv_t, data))
53 
54 /* Check that bcm_tlv_t fits into the given buffer len */
55 #define bcm_valid_tlv(elt, buflen)					\
56 	((elt != NULL) &&						\
57 	 ((buflen) >= (uint)BCM_TLV_HDR_SIZE) &&			\
58 	 ((buflen) >= (uint)(BCM_TLV_HDR_SIZE + (elt)->len)))
59 
60 /* type(aka id)/length/ext/value buffer */
61 typedef struct bcm_tlv_ext {
62 	uint8	id;
63 	uint8	len;
64 	uint8	ext;
65 	uint8	data[1];
66 } bcm_tlv_ext_t;
67 
68 /* get next tlv_ext - no length checks */
69 #define BCM_TLV_EXT_NEXT(_tlv_ext) \
70 	(bcm_tlv_ext_t *)((uint8 *)(_tlv_ext)+ BCM_TLV_EXT_SIZE(_tlv_ext))
71 
72 /* tlv_ext length is restricted to 1 byte */
73 #define BCM_TLV_EXT_MAX_DATA_SIZE (254u)
74 
75 /* tlv_ext header - three bytes */
76 #define BCM_TLV_EXT_HDR_SIZE (OFFSETOF(bcm_tlv_ext_t, data))
77 
78 /* size of tlv_ext including data */
79 #define BCM_TLV_EXT_SIZE(_tlv_ext) (BCM_TLV_EXT_HDR_SIZE + (_tlv_ext)->len)
80 
81 /* find the next tlv */
82 bcm_tlv_t *bcm_next_tlv(const  bcm_tlv_t *elt, uint *buflen);
83 
84 /* move buffer/buflen up to the given tlv, or set to NULL/0 on error */
85 void bcm_tlv_buffer_advance_to(const bcm_tlv_t *elt, const uint8 **buffer, uint *buflen);
86 
87 /* move buffer/buflen past the given tlv, or set to NULL/0 on error */
88 void bcm_tlv_buffer_advance_past(const bcm_tlv_t *elt, const uint8 **buffer, uint *buflen);
89 
90 /* find the tlv for a given id */
91 bcm_tlv_t *bcm_parse_tlvs(const  void *buf, uint buflen, uint key);
92 
93 /* advancement modes for bcm_parse_tlvs_advance() */
94 typedef enum {
95 	BCM_TLV_ADVANCE_NONE = 0,  // do not adjust the buffer/buflen
96 	BCM_TLV_ADVANCE_TO = 1,    // advance to the found tlv
97 	BCM_TLV_ADVANCE_PAST = 2   // advance past the found tlv
98 } bcm_tlv_advance_mode_t;
99 
100 /* Find an IE of a specific type from a buffer.
101  * tlvs: buffer to search for IE
102  * tlvs_len: buffer length
103  * tag: IE tag
104  * oui_len: length of the OUI
105  * oui: Specific OUI to match
106  * type: OUI type
107  * Return the matched IE, else return null.
108 */
109 extern bcm_tlv_t *bcm_find_ie(const uint8 *tlvs, uint tlvs_len, uint8 tag,
110 	uint8 oui_len, const char *oui, uint8 type);
111 
112 /* search for a matching tlv id, and adjust the parse buffer pointer/length */
113 const bcm_tlv_t *bcm_parse_tlvs_advance(const uint8 **buf, uint *buflen, uint key,
114 	bcm_tlv_advance_mode_t advance);
115 
116 /*
117  * Traverse tlvs and return pointer to the first tlv that
118  * matches the key. Return NULL if not found or tlv len < min_bodylen
119  */
120 bcm_tlv_t *bcm_parse_tlvs_min_bodylen(const  void *buf, uint buflen, uint key, uint min_bodylen);
121 
122 /*
123  * Traverse tlvs and return pointer to the first tlv that
124  * matches the key. Return NULL if not found or tlv size > max_len or < min_len
125  */
126 bcm_tlv_t *bcm_parse_tlvs_minmax_len(const  void *buf, uint buflen, uint key,
127 	uint min_len, uint max_len);
128 
129 /* parse tlvs for dot11 - same as parse_tlvs but supports 802.11 id extension */
130 bcm_tlv_t *bcm_parse_tlvs_dot11(const  void *buf, uint buflen, uint key, bool id_ext);
131 
132 /* same as parse_tlvs, but stops when found id > key */
133 const  bcm_tlv_t *bcm_parse_ordered_tlvs(const  void *buf, uint buflen, uint key);
134 
135 /* find a tlv with DOT11_MNG_PROPR_ID as id, and the given oui and type */
136 	bcm_tlv_t *bcm_find_vendor_ie(const  void *tlvs, uint tlvs_len, const char *voui,
137 	                              uint8 *type, uint type_len);
138 
139 /* write tlv at dst and return next tlv ptr */
140 uint8 *bcm_write_tlv(int type, const void *data, uint datalen, uint8 *dst);
141 
142 /* write tlv_ext at dst and return next tlv ptr */
143 uint8 *bcm_write_tlv_ext(uint8 type, uint8 ext, const void *data, uint8 datalen, uint8 *dst);
144 
145 /* write tlv at dst if space permits and return next tlv ptr */
146 uint8 *bcm_write_tlv_safe(int type, const void *data, uint datalen, uint8 *dst,
147 	uint dst_maxlen);
148 
149 /* copy a tlv  and return next tlv ptr */
150 uint8 *bcm_copy_tlv(const void *src, uint8 *dst);
151 
152 /* copy a tlv if space permits and return next tlv ptr */
153 uint8 *bcm_copy_tlv_safe(const void *src, uint8 *dst, uint dst_maxlen);
154 
155 /* end tlvs */
156 
157 /* begin xtlv - used for iovars, nan attributes etc. */
158 
159 /* bcm type(id), length, value with w/16 bit id/len. The structure below
160  * is nominal, and is used to support variable length id and type. See
161  * xtlv options below.
162  */
163 typedef struct bcm_xtlv {
164 	uint16	id;
165 	uint16	len;
166 	uint8	data[1];
167 } bcm_xtlv_t;
168 
169 /* xtlv options */
170 #define BCM_XTLV_OPTION_NONE	0x0000u
171 #define BCM_XTLV_OPTION_ALIGN32	0x0001u /* 32bit alignment of type.len.data */
172 #define BCM_XTLV_OPTION_IDU8	0x0002u /* shorter id */
173 #define BCM_XTLV_OPTION_LENU8	0x0004u /* shorted length */
174 #define BCM_XTLV_OPTION_IDBE	0x0008u /* big endian format id */
175 #define BCM_XTLV_OPTION_LENBE	0x0010u /* big endian format length */
176 typedef uint16 bcm_xtlv_opts_t;
177 
178 /* header size. depends on options. Macros names ending w/ _EX are where
179  * options are explcitly specified that may be less common. The ones
180  * without use default values that correspond to ...OPTION_NONE
181  */
182 
183 /* xtlv header size depends on options */
184 #define BCM_XTLV_HDR_SIZE 4u
185 #define BCM_XTLV_HDR_SIZE_EX(_opts) bcm_xtlv_hdr_size(_opts)
186 
187 /* note: xtlv len only stores the value's length without padding */
188 #define BCM_XTLV_LEN(_elt) ltoh16_ua(&(_elt)->len)
189 #define BCM_XTLV_LEN_EX(_elt, _opts) bcm_xtlv_len(_elt, _opts)
190 
191 #define BCM_XTLV_ID(_elt) ltoh16_ua(&(_elt)->id)
192 #define BCM_XTLV_ID_EX(_elt, _opts) bcm_xtlv_id(_elt, _opts)
193 
194 /* entire size of the XTLV including header, data, and optional padding */
195 #define BCM_XTLV_SIZE(elt, opts) bcm_xtlv_size(elt, opts)
196 #define BCM_XTLV_SIZE_EX(_elt, _opts) bcm_xtlv_size(_elt, _opts)
197 
198 /* max xtlv data size */
199 #define BCM_XTLV_MAX_DATA_SIZE 65535u
200 #define BCM_XTLV_MAX_DATA_SIZE_EX(_opts) ((_opts & BCM_XTLV_OPTION_LENU8) ? \
201 	255u : 65535u)
202 
203 /* descriptor of xtlv data, packing(src) and unpacking(dst) support  */
204 typedef struct {
205 	uint16	type;
206 	uint16	len;
207 	void	*ptr; /* ptr to memory location */
208 } xtlv_desc_t;
209 
210 /* xtlv buffer - packing/unpacking support */
211 struct bcm_xtlvbuf {
212 	bcm_xtlv_opts_t opts;
213 	uint16 size;
214 	uint8 *head; /* point to head of buffer */
215 	uint8 *buf; /* current position of buffer */
216 	/* allocated buffer may follow, but not necessarily */
217 };
218 typedef struct bcm_xtlvbuf bcm_xtlvbuf_t;
219 
220 /* valid xtlv ? */
221 bool bcm_valid_xtlv(const bcm_xtlv_t *elt, int buf_len, bcm_xtlv_opts_t opts);
222 
223 /* return the next xtlv element, and update buffer len (remaining). Buffer length
224  * updated includes padding as specified by options
225  */
226 bcm_xtlv_t *bcm_next_xtlv(const bcm_xtlv_t *elt, int *buf_len, bcm_xtlv_opts_t opts);
227 
228 /* initialize an xtlv buffer. Use options specified for packing/unpacking using
229  * the buffer. Caller is responsible for allocating both buffers.
230  */
231 int bcm_xtlv_buf_init(bcm_xtlvbuf_t *tlv_buf, uint8 *buf, uint16 len,
232 	bcm_xtlv_opts_t opts);
233 
234 /* length of data in the xtlv buffer */
235 uint16 bcm_xtlv_buf_len(struct bcm_xtlvbuf *tbuf);
236 
237 /* remaining space in the xtlv buffer */
238 uint16 bcm_xtlv_buf_rlen(struct bcm_xtlvbuf *tbuf);
239 
240 /* write ptr */
241 uint8 *bcm_xtlv_buf(struct bcm_xtlvbuf *tbuf);
242 
243 /* head */
244 uint8 *bcm_xtlv_head(struct bcm_xtlvbuf *tbuf);
245 
246 /* put a data buffer into xtlv */
247 int bcm_xtlv_put_data(bcm_xtlvbuf_t *tbuf, uint16 type, const uint8 *data, int n);
248 
249 /* put one or more u16 elts into xtlv */
250 int bcm_xtlv_put16(bcm_xtlvbuf_t *tbuf, uint16 type, const uint16 *data, int n);
251 
252 /* put one or more u32 elts into xtlv */
253 int bcm_xtlv_put32(bcm_xtlvbuf_t *tbuf, uint16 type, const uint32 *data, int n);
254 
255 /* put one or more u64 elts into xtlv */
256 int bcm_xtlv_put64(bcm_xtlvbuf_t *tbuf, uint16 type, const uint64 *data, int n);
257 
258 /* note: there are no get equivalent of integer unpacking, becasuse bcmendian.h
259  * can be used directly using pointers returned in the buffer being processed.
260  */
261 
262 /* unpack a single xtlv entry, advances buffer and copies data to dst_data on match
263  * type and length match must be exact
264  */
265 int bcm_unpack_xtlv_entry(const uint8 **buf, uint16 expected_type, uint16 expected_len,
266 	uint8 *dst_data, bcm_xtlv_opts_t opts);
267 
268 /* packs an xtlv into buffer, and advances buffer, decreements buffer length.
269  * buffer length is checked and must be >= size of xtlv - otherwise BCME_BADLEN
270  */
271 int bcm_pack_xtlv_entry(uint8 **buf, uint16 *buflen, uint16 type, uint16 len,
272 	const uint8 *src_data, bcm_xtlv_opts_t opts);
273 
274 /* accessors and lengths for element given options */
275 int bcm_xtlv_size(const bcm_xtlv_t *elt, bcm_xtlv_opts_t opts);
276 int bcm_xtlv_hdr_size(bcm_xtlv_opts_t opts);
277 int bcm_xtlv_len(const bcm_xtlv_t *elt, bcm_xtlv_opts_t opts);
278 int bcm_xtlv_id(const bcm_xtlv_t *elt, bcm_xtlv_opts_t opts);
279 int bcm_xtlv_size_for_data(int dlen, bcm_xtlv_opts_t opts);
280 
281 /* compute size needed for number of tlvs whose total data len is given */
282 #define BCM_XTLV_SIZE_FOR_TLVS(_data_len, _num_tlvs, _opts) (\
283 	bcm_xtlv_size_for_data(_data_len, _opts) + (\
284 	(_num_tlvs) * BCM_XTLV_HDR_SIZE_EX(_opts)))
285 
286 /* unsafe copy xtlv */
287 #define BCM_XTLV_BCOPY(_src, _dst, _opts) \
288 	bcm_xtlv_bcopy(_src, _dst, BCM_XTLV_MAX_DATA_SIZE_EX(_opts), \
289 		BCM_XTLV_MAX_DATA_SIZE_EX(_opts), _opts)
290 
291 /* copy xtlv - note: src->dst bcopy order - to be compatible w/ tlv version */
292 bcm_xtlv_t* bcm_xtlv_bcopy(const bcm_xtlv_t *src, bcm_xtlv_t *dst,
293 	int src_buf_len, int dst_buf_len, bcm_xtlv_opts_t opts);
294 
295 /* callback for unpacking xtlv from a buffer into context. */
296 typedef int (bcm_xtlv_unpack_cbfn_t)(void *ctx, const uint8 *buf,
297 	uint16 type, uint16 len);
298 
299 /* unpack a tlv buffer using buffer, options, and callback */
300 int bcm_unpack_xtlv_buf(void *ctx, const uint8 *buf, uint16 buflen,
301 	bcm_xtlv_opts_t opts, bcm_xtlv_unpack_cbfn_t *cbfn);
302 
303 /* unpack a set of tlvs from the buffer using provided xtlv descriptors */
304 int bcm_unpack_xtlv_buf_to_mem(const uint8 *buf, int *buflen, xtlv_desc_t *items,
305 	bcm_xtlv_opts_t opts);
306 
307 /* pack a set of tlvs into buffer using provided xtlv descriptors */
308 int bcm_pack_xtlv_buf_from_mem(uint8 **buf, uint16 *buflen,
309 	const xtlv_desc_t *items, bcm_xtlv_opts_t opts);
310 
311 /* return data pointer and data length of a given id from xtlv buffer
312  * data_len may be NULL
313  */
314 const uint8* bcm_get_data_from_xtlv_buf(const uint8 *tlv_buf, uint16 buflen,
315 	uint16 id, uint16 *datalen, bcm_xtlv_opts_t opts);
316 
317 /* callback to return next tlv id and len to pack, if there is more tlvs to come and
318  * options e.g. alignment
319  */
320 typedef bool (*bcm_pack_xtlv_next_info_cbfn_t)(void *ctx, uint16 *tlv_id, uint16 *tlv_len);
321 
322 /* callback to pack the tlv into length validated buffer */
323 typedef void (*bcm_pack_xtlv_pack_next_cbfn_t)(void *ctx,
324 	uint16 tlv_id, uint16 tlv_len, uint8* buf);
325 
326 /* pack a set of tlvs into buffer using get_next to interate */
327 int bcm_pack_xtlv_buf(void *ctx, uint8 *tlv_buf, uint16 buflen,
328 	bcm_xtlv_opts_t opts, bcm_pack_xtlv_next_info_cbfn_t get_next,
329 	bcm_pack_xtlv_pack_next_cbfn_t pack_next, int *outlen);
330 
331 /* pack an xtlv. does not do any error checking. if data is not NULL
332  * data of given length is copied  to buffer (xtlv)
333  */
334 void bcm_xtlv_pack_xtlv(bcm_xtlv_t *xtlv, uint16 type, uint16 len,
335 	const uint8 *data, bcm_xtlv_opts_t opts);
336 
337 /* unpack an xtlv and return ptr to data, and data length */
338 void bcm_xtlv_unpack_xtlv(const bcm_xtlv_t *xtlv, uint16 *type, uint16 *len,
339 	const uint8 **data, bcm_xtlv_opts_t opts);
340 
341 /* end xtlvs */
342 
343 /* length value pairs */
344 struct bcm_xlv {
345 	uint16 len;
346 	uint8 data[1];
347 };
348 typedef struct bcm_xlv bcm_xlv_t;
349 
350 struct bcm_xlvp {
351 	uint16 len;
352 	uint8 *data;
353 };
354 typedef struct bcm_xlvp bcm_xlvp_t;
355 
356 struct bcm_const_xlvp {
357 	uint16 len;
358 	const uint8 *data;
359 };
360 
361 typedef struct bcm_const_xlvp bcm_const_xlvp_t;
362 
363 struct bcm_const_ulvp {
364 	uint32 len;
365 	const uint8 *data;
366 };
367 
368 typedef struct bcm_const_ulvp bcm_const_ulvp_t;
369 
370 /* end length value pairs */
371 #ifdef __cplusplus
372 }
373 #endif /* __cplusplus */
374 
375 #endif	/* _bcmtlv_h_ */
376