blob: c19034f0d2ac08a34479dfbe17f21497c66091b1 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3#include <stdint.h>
4#include <string.h>
5
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01006#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +01007
Harald Welte57c7d372011-08-17 17:50:55 +02008/*! \defgroup tlv GSM L3 compatible TLV parser
9 * @{
10 */
11/*! \file tlv.h */
12
Harald Welteec8b4502010-02-20 20:34:29 +010013/* Terminology / wording
14 tag length value (in bits)
15
16 V - - 8
17 LV - 8 N * 8
18 TLV 8 8 N * 8
19 TL16V 8 16 N * 8
20 TLV16 8 8 N * 16
21 TvLV 8 8/16 N * 8
Harald Welte2fe68472012-07-14 01:50:33 +020022 vTvLV 8/16 8/16 N * 8
Harald Welteec8b4502010-02-20 20:34:29 +010023
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
Harald Welte2fe68472012-07-14 01:50:33 +020049/*! \brief gross length of vTvL header (tag+len) */
50static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
51{
52 uint16_t ret = 2;
53
54 if (tag > TVLV_MAX_ONEBYTE)
55 ret++;
56
57 if (len > TVLV_MAX_ONEBYTE)
58 ret++;
59
60 return ret;
61}
62
63/*! \brief gross length of vTvLV (tag+len+val) */
64static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
65{
66 uint16_t ret;
67
68 if (len <= TVLV_MAX_ONEBYTE)
69 return TLV_GROSS_LEN(len);
70 else
71 return TL16V_GROSS_LEN(len);
72
73 if (tag > TVLV_MAX_ONEBYTE)
74 ret += 1;
75
76 return ret;
77}
78
Harald Welteec8b4502010-02-20 20:34:29 +010079/* TLV generation */
80
Harald Welte57c7d372011-08-17 17:50:55 +020081/*! \brief put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010082static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
83 const uint8_t *val)
84{
85 *buf++ = len;
86 memcpy(buf, val, len);
87 return buf + len;
88}
89
Harald Welte57c7d372011-08-17 17:50:55 +020090/*! \brief put (append) a TLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010091static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
92 const uint8_t *val)
93{
94 *buf++ = tag;
95 *buf++ = len;
96 memcpy(buf, val, len);
97 return buf + len;
98}
99
Harald Welte57c7d372011-08-17 17:50:55 +0200100/*! \brief put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +0100101static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
102 const uint16_t *val)
103{
104 *buf++ = tag;
105 *buf++ = len;
106 memcpy(buf, val, len*2);
107 return buf + len*2;
108}
109
Harald Welte57c7d372011-08-17 17:50:55 +0200110/*! \brief put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100111static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
112 const uint8_t *val)
113{
114 *buf++ = tag;
115 *buf++ = len >> 8;
116 *buf++ = len & 0xff;
117 memcpy(buf, val, len);
118 return buf + len*2;
119}
120
Harald Welte57c7d372011-08-17 17:50:55 +0200121/*! \brief put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100122static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
123 const uint8_t *val)
124{
125 uint8_t *ret;
126
127 if (len <= TVLV_MAX_ONEBYTE) {
128 ret = tlv_put(buf, tag, len, val);
129 buf[1] |= 0x80;
130 } else
131 ret = tl16v_put(buf, tag, len, val);
132
133 return ret;
134}
135
Harald Welte2fe68472012-07-14 01:50:33 +0200136/*! \brief put (append) a variable-length tag or variable-length length * */
137static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag)
138{
139 if (tag > TVLV_MAX_ONEBYTE) {
140 /* two-byte TAG */
141 *buf++ = 0x80 | (tag >> 8);
142 *buf++ = (tag & 0xff);
143 } else
144 *buf++ = tag;
145
146 return buf;
147}
148
149/* \brief put (append) vTvL (GAN) field (tag + length)*/
150static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
151{
152 uint8_t *ret;
153
154 ret = vt_gan_put(buf, tag);
155 return vt_gan_put(ret, len);
156}
157
158/* \brief put (append) vTvLV (GAN) field (tag + length + val) */
159static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len,
160 const uint8_t *val)
161{
162 uint8_t *ret;
163
164 ret = vtvl_gan_put(buf, tag, len );
165
166 memcpy(ret, val, len);
167 ret = buf + len;
168
169 return ret;
170}
171
Harald Welte57c7d372011-08-17 17:50:55 +0200172/*! \brief put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100173static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
174{
175 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
176 return tlv16_put(buf, tag, len, val);
177}
178
Harald Welte57c7d372011-08-17 17:50:55 +0200179/*! \brief put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100180static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
181 const uint8_t *val)
182{
183 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
184 return tl16v_put(buf, tag, len, val);
185}
186
Harald Welte57c7d372011-08-17 17:50:55 +0200187/*! \brief put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100188static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
189 const uint8_t *val)
190{
191 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
192 return tvlv_put(buf, tag, len, val);
193}
194
Harald Welte2fe68472012-07-14 01:50:33 +0200195/*! \brief put (append) a vTvLV field to \ref msgb */
196static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag,
197 uint16_t len, const uint8_t *val)
198{
199 uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len));
200 return vtvlv_gan_put(buf, tag, len, val);
201}
202
Harald Welte57c7d372011-08-17 17:50:55 +0200203/*! \brief put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100204static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
205 const uint8_t *val)
206{
207 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
208
209 *buf++ = len >> 8;
210 *buf++ = len & 0xff;
211 *buf++ = tag;
212 memcpy(buf, val, len);
213 return buf + len;
214}
215
Harald Welte57c7d372011-08-17 17:50:55 +0200216/*! \brief put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100217static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
218{
219 *buf++ = val;
220 return buf;
221}
222
Harald Welte57c7d372011-08-17 17:50:55 +0200223/*! \brief put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100224static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
225 uint8_t val)
226{
227 *buf++ = tag;
228 *buf++ = val;
229 return buf;
230}
231
Harald Welte57c7d372011-08-17 17:50:55 +0200232/*! \brief put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100233static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
234 unsigned int len, const uint8_t *val)
235{
236 *buf++ = tag;
237 memcpy(buf, val, len);
238 return buf + len;
239}
240
Harald Welte57c7d372011-08-17 17:50:55 +0200241/*! \brief put (append) a TV16 field
242 * \param[in,out] buf data buffer
243 * \param[in] tag Tag value
244 * \param[in] val Value (in host byte order!)
245 */
Harald Welteec8b4502010-02-20 20:34:29 +0100246static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
247 uint16_t val)
248{
249 *buf++ = tag;
250 *buf++ = val >> 8;
251 *buf++ = val & 0xff;
252 return buf;
253}
254
Harald Welte2c020432012-01-22 23:03:38 +0100255/*! \brief put (append) a LV field to a \ref msgb
256 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100257static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
258{
259 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
260 return lv_put(buf, len, val);
261}
262
Harald Welte2c020432012-01-22 23:03:38 +0100263/*! \brief put (append) a TLV field to a \ref msgb
264 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100265static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
266{
267 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
268 return tlv_put(buf, tag, len, val);
269}
270
Harald Welte2c020432012-01-22 23:03:38 +0100271/*! \brief put (append) a TV field to a \ref msgb
272 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100273static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
274{
275 uint8_t *buf = msgb_put(msg, 2);
276 return tv_put(buf, tag, val);
277}
278
Harald Welte2c020432012-01-22 23:03:38 +0100279/*! \brief put (append) a TVfixed field to a \ref msgb
280 * \returns pointer to first byte after newly-put information */
Harald Welte63196de2011-03-05 14:32:50 +0100281static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
282 unsigned int len, const uint8_t *val)
283{
284 uint8_t *buf = msgb_put(msg, 1+len);
285 return tv_fixed_put(buf, tag, len, val);
286}
287
Harald Welte2c020432012-01-22 23:03:38 +0100288/*! \brief put (append) a V field to a \ref msgb
289 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100290static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
291{
292 uint8_t *buf = msgb_put(msg, 1);
293 return v_put(buf, val);
294}
295
Harald Welte2c020432012-01-22 23:03:38 +0100296/*! \brief put (append) a TV16 field to a \ref msgb
297 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100298static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
299{
300 uint8_t *buf = msgb_put(msg, 3);
301 return tv16_put(buf, tag, val);
302}
303
Harald Welte2c020432012-01-22 23:03:38 +0100304/*! \brief push (prepend) a TLV field to a \ref msgb
305 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100306static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
307{
308 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100309 tlv_put(buf, tag, len, val);
310 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100311}
312
Harald Welte2c020432012-01-22 23:03:38 +0100313/*! \brief push (prepend) a TV field to a \ref msgb
314 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100315static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
316{
317 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100318 tv_put(buf, tag, val);
319 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100320}
321
Harald Welte2c020432012-01-22 23:03:38 +0100322/*! \brief push (prepend) a TV16 field to a \ref msgb
323 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100324static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
325{
326 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100327 tv16_put(buf, tag, val);
328 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100329}
330
Harald Welte2c020432012-01-22 23:03:38 +0100331/*! \brief push (prepend) a TvLV field to a \ref msgb
332 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100333static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
334 const uint8_t *val)
335{
336 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100337 tvlv_put(buf, tag, len, val);
338 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100339}
340
Harald Welte2fe68472012-07-14 01:50:33 +0200341/* \brief push (prepend) a vTvL header to a \ref msgb
342 */
343static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
344 uint16_t len)
345{
346 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
347 vtvl_gan_put(buf, tag, len);
348 return buf;
349}
350
351
352static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
353 uint16_t len, const uint8_t *val)
354{
355 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
356 vtvlv_gan_put(buf, tag, len, val);
357 return buf;
358}
359
Harald Welteec8b4502010-02-20 20:34:29 +0100360/* TLV parsing */
361
Harald Welte57c7d372011-08-17 17:50:55 +0200362/*! \brief Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100363struct tlv_p_entry {
Harald Welte57c7d372011-08-17 17:50:55 +0200364 uint16_t len; /*!< \brief length */
365 const uint8_t *val; /*!< \brief pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100366};
367
Harald Welte57c7d372011-08-17 17:50:55 +0200368/*! \brief TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100369enum tlv_type {
Harald Welte57c7d372011-08-17 17:50:55 +0200370 TLV_TYPE_NONE, /*!< \brief no type */
371 TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */
372 TLV_TYPE_T, /*!< \brief tag-only */
373 TLV_TYPE_TV, /*!< \brief tag-value (8bit) */
374 TLV_TYPE_TLV, /*!< \brief tag-length-value */
375 TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */
376 TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */
Harald Welte2fe68472012-07-14 01:50:33 +0200377 TLV_TYPE_SINGLE_TV, /*!< \brief tag and value (both 4 bit) in 1 byte */
378 TLV_TYPE_vTvLV_GAN, /*!< \brief variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100379};
380
Harald Welte57c7d372011-08-17 17:50:55 +0200381/*! \brief Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100382struct tlv_def {
Harald Welte57c7d372011-08-17 17:50:55 +0200383 enum tlv_type type; /*!< \brief TLV type */
384 uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100385};
386
Harald Welte57c7d372011-08-17 17:50:55 +0200387/*! \brief Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100388struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200389 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100390};
391
Harald Welte57c7d372011-08-17 17:50:55 +0200392/*! \brief result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100393struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200394 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100395};
396
397extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200398extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100399
400int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
401 const struct tlv_definition *def,
402 const uint8_t *buf, int buf_len);
403int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
404 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100405/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100406void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
407
408#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
409#define TLVP_LEN(x, y) (x)->lv[y].len
410#define TLVP_VAL(x, y) (x)->lv[y].val
411
Harald Weltecc27fa62014-08-18 15:31:04 +0200412#define TLVP_PRES_LEN(tp, tag, min_len) \
413 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
414
Andreas Eversberg01675962012-11-06 12:02:59 +0100415/*! \brief Align given TLV element with 16 bit value to an even address
416 * \param[in] tp pointer to \ref tlv_parsed
417 * \param[in] pos element to return
418 * \returns aligned 16 bit value
419 */
420static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
421{
422 uint16_t res;
423 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
424 return res;
425}
426
427/*! \brief Align given TLV element with 32 bit value to an address that is a multiple of 4
428 * \param[in] tp pointer to \ref tlv_parsed
429 * \param[in] pos element to return
430 * \returns aligned 32 bit value
431 */
432static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
433{
434 uint32_t res;
435 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
436 return res;
437}
438
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200439/*! @} */