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