blob: d1efc55329408842712208e0ecbb675b7f0317eb [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef _TLV_H
2#define _TLV_H
3
4#include <stdint.h>
5#include <string.h>
6
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01007#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +01008
Harald Welte57c7d372011-08-17 17:50:55 +02009/*! \defgroup tlv GSM L3 compatible TLV parser
10 * @{
11 */
12/*! \file tlv.h */
13
Harald Welteec8b4502010-02-20 20:34:29 +010014/* Terminology / wording
15 tag length value (in bits)
16
17 V - - 8
18 LV - 8 N * 8
19 TLV 8 8 N * 8
20 TL16V 8 16 N * 8
21 TLV16 8 8 N * 16
22 TvLV 8 8/16 N * 8
23
24*/
25
Harald Welte57c7d372011-08-17 17:50:55 +020026/*! \brief gross length of a LV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010027#define LV_GROSS_LEN(x) (x+1)
Harald Welte57c7d372011-08-17 17:50:55 +020028/*! \brief gross length of a TLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010029#define TLV_GROSS_LEN(x) (x+2)
Harald Welte57c7d372011-08-17 17:50:55 +020030/*! \brief gross length of a TLV16 type field */
Harald Welteec8b4502010-02-20 20:34:29 +010031#define TLV16_GROSS_LEN(x) ((2*x)+2)
Harald Welte57c7d372011-08-17 17:50:55 +020032/*! \brief gross length of a TL16V type field */
Harald Welteec8b4502010-02-20 20:34:29 +010033#define TL16V_GROSS_LEN(x) (x+3)
Harald Welte57c7d372011-08-17 17:50:55 +020034/*! \brief gross length of a L16TV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010035#define L16TV_GROSS_LEN(x) (x+3)
36
Harald Welte57c7d372011-08-17 17:50:55 +020037/*! \brief maximum length of TLV of one byte length */
Harald Welteec8b4502010-02-20 20:34:29 +010038#define TVLV_MAX_ONEBYTE 0x7f
39
Harald Welte57c7d372011-08-17 17:50:55 +020040/*! \brief gross length of a TVLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010041static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
42{
43 if (len <= TVLV_MAX_ONEBYTE)
44 return TLV_GROSS_LEN(len);
45 else
46 return TL16V_GROSS_LEN(len);
47}
48
49/* TLV generation */
50
Harald Welte57c7d372011-08-17 17:50:55 +020051/*! \brief put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010052static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
53 const uint8_t *val)
54{
55 *buf++ = len;
56 memcpy(buf, val, len);
57 return buf + len;
58}
59
Harald Welte57c7d372011-08-17 17:50:55 +020060/*! \brief put (append) a TLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010061static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
62 const uint8_t *val)
63{
64 *buf++ = tag;
65 *buf++ = len;
66 memcpy(buf, val, len);
67 return buf + len;
68}
69
Harald Welte57c7d372011-08-17 17:50:55 +020070/*! \brief put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +010071static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
72 const uint16_t *val)
73{
74 *buf++ = tag;
75 *buf++ = len;
76 memcpy(buf, val, len*2);
77 return buf + len*2;
78}
79
Harald Welte57c7d372011-08-17 17:50:55 +020080/*! \brief put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +010081static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
82 const uint8_t *val)
83{
84 *buf++ = tag;
85 *buf++ = len >> 8;
86 *buf++ = len & 0xff;
87 memcpy(buf, val, len);
88 return buf + len*2;
89}
90
Harald Welte57c7d372011-08-17 17:50:55 +020091/*! \brief put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010092static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
93 const uint8_t *val)
94{
95 uint8_t *ret;
96
97 if (len <= TVLV_MAX_ONEBYTE) {
98 ret = tlv_put(buf, tag, len, val);
99 buf[1] |= 0x80;
100 } else
101 ret = tl16v_put(buf, tag, len, val);
102
103 return ret;
104}
105
Harald Welte57c7d372011-08-17 17:50:55 +0200106/*! \brief put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100107static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
108{
109 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
110 return tlv16_put(buf, tag, len, val);
111}
112
Harald Welte57c7d372011-08-17 17:50:55 +0200113/*! \brief put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100114static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
115 const uint8_t *val)
116{
117 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
118 return tl16v_put(buf, tag, len, val);
119}
120
Harald Welte57c7d372011-08-17 17:50:55 +0200121/*! \brief put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100122static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
123 const uint8_t *val)
124{
125 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
126 return tvlv_put(buf, tag, len, val);
127}
128
Harald Welte57c7d372011-08-17 17:50:55 +0200129/*! \brief put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100130static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
131 const uint8_t *val)
132{
133 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
134
135 *buf++ = len >> 8;
136 *buf++ = len & 0xff;
137 *buf++ = tag;
138 memcpy(buf, val, len);
139 return buf + len;
140}
141
Harald Welte57c7d372011-08-17 17:50:55 +0200142/*! \brief put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100143static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
144{
145 *buf++ = val;
146 return buf;
147}
148
Harald Welte57c7d372011-08-17 17:50:55 +0200149/*! \brief put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100150static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
151 uint8_t val)
152{
153 *buf++ = tag;
154 *buf++ = val;
155 return buf;
156}
157
Harald Welte57c7d372011-08-17 17:50:55 +0200158/*! \brief put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100159static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
160 unsigned int len, const uint8_t *val)
161{
162 *buf++ = tag;
163 memcpy(buf, val, len);
164 return buf + len;
165}
166
Harald Welte57c7d372011-08-17 17:50:55 +0200167/*! \brief put (append) a TV16 field
168 * \param[in,out] buf data buffer
169 * \param[in] tag Tag value
170 * \param[in] val Value (in host byte order!)
171 */
Harald Welteec8b4502010-02-20 20:34:29 +0100172static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
173 uint16_t val)
174{
175 *buf++ = tag;
176 *buf++ = val >> 8;
177 *buf++ = val & 0xff;
178 return buf;
179}
180
Harald Welte2c020432012-01-22 23:03:38 +0100181/*! \brief put (append) a LV field to a \ref msgb
182 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100183static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
184{
185 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
186 return lv_put(buf, len, val);
187}
188
Harald Welte2c020432012-01-22 23:03:38 +0100189/*! \brief put (append) a TLV field to a \ref msgb
190 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100191static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
192{
193 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
194 return tlv_put(buf, tag, len, val);
195}
196
Harald Welte2c020432012-01-22 23:03:38 +0100197/*! \brief put (append) a TV field to a \ref msgb
198 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100199static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
200{
201 uint8_t *buf = msgb_put(msg, 2);
202 return tv_put(buf, tag, val);
203}
204
Harald Welte2c020432012-01-22 23:03:38 +0100205/*! \brief put (append) a TVfixed field to a \ref msgb
206 * \returns pointer to first byte after newly-put information */
Harald Welte63196de2011-03-05 14:32:50 +0100207static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
208 unsigned int len, const uint8_t *val)
209{
210 uint8_t *buf = msgb_put(msg, 1+len);
211 return tv_fixed_put(buf, tag, len, val);
212}
213
Harald Welte2c020432012-01-22 23:03:38 +0100214/*! \brief put (append) a V field to a \ref msgb
215 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100216static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
217{
218 uint8_t *buf = msgb_put(msg, 1);
219 return v_put(buf, val);
220}
221
Harald Welte2c020432012-01-22 23:03:38 +0100222/*! \brief put (append) a TV16 field to a \ref msgb
223 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100224static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
225{
226 uint8_t *buf = msgb_put(msg, 3);
227 return tv16_put(buf, tag, val);
228}
229
Harald Welte2c020432012-01-22 23:03:38 +0100230/*! \brief push (prepend) a TLV field to a \ref msgb
231 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100232static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
233{
234 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100235 tlv_put(buf, tag, len, val);
236 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100237}
238
Harald Welte2c020432012-01-22 23:03:38 +0100239/*! \brief push (prepend) a TV field to a \ref msgb
240 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100241static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
242{
243 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100244 tv_put(buf, tag, val);
245 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100246}
247
Harald Welte2c020432012-01-22 23:03:38 +0100248/*! \brief push (prepend) a TV16 field to a \ref msgb
249 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100250static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
251{
252 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100253 tv16_put(buf, tag, val);
254 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100255}
256
Harald Welte2c020432012-01-22 23:03:38 +0100257/*! \brief push (prepend) a TvLV field to a \ref msgb
258 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100259static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
260 const uint8_t *val)
261{
262 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100263 tvlv_put(buf, tag, len, val);
264 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100265}
266
Harald Welteec8b4502010-02-20 20:34:29 +0100267/* TLV parsing */
268
Harald Welte57c7d372011-08-17 17:50:55 +0200269/*! \brief Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100270struct tlv_p_entry {
Harald Welte57c7d372011-08-17 17:50:55 +0200271 uint16_t len; /*!< \brief length */
272 const uint8_t *val; /*!< \brief pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100273};
274
Harald Welte57c7d372011-08-17 17:50:55 +0200275/*! \brief TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100276enum tlv_type {
Harald Welte57c7d372011-08-17 17:50:55 +0200277 TLV_TYPE_NONE, /*!< \brief no type */
278 TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */
279 TLV_TYPE_T, /*!< \brief tag-only */
280 TLV_TYPE_TV, /*!< \brief tag-value (8bit) */
281 TLV_TYPE_TLV, /*!< \brief tag-length-value */
282 TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */
283 TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */
284 TLV_TYPE_SINGLE_TV /*!< \brief tag and value (both 4 bit) in 1 byte */
Harald Welteec8b4502010-02-20 20:34:29 +0100285};
286
Harald Welte57c7d372011-08-17 17:50:55 +0200287/*! \brief Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100288struct tlv_def {
Harald Welte57c7d372011-08-17 17:50:55 +0200289 enum tlv_type type; /*!< \brief TLV type */
290 uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100291};
292
Harald Welte57c7d372011-08-17 17:50:55 +0200293/*! \brief Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100294struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200295 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100296};
297
Harald Welte57c7d372011-08-17 17:50:55 +0200298/*! \brief result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100299struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200300 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100301};
302
303extern struct tlv_definition tvlv_att_def;
304
305int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
306 const struct tlv_definition *def,
307 const uint8_t *buf, int buf_len);
308int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
309 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
310/* take a master (src) tlvdev and fill up all empty slots in 'dst' */
311void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
312
313#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
314#define TLVP_LEN(x, y) (x)->lv[y].len
315#define TLVP_VAL(x, y) (x)->lv[y].val
316
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200317/*! @} */
Harald Welte57c7d372011-08-17 17:50:55 +0200318
Harald Welteec8b4502010-02-20 20:34:29 +0100319#endif /* _TLV_H */