bcmtlv.h 12 KB

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