blob: fe057f4b3f1015d44d62642ee7299ff977967a7f [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 Weltedec201a2020-12-08 20:32:12 +01007#include <osmocom/core/byteswap.h>
Harald Welte50ef7332017-05-15 12:45:59 +02008#include <osmocom/core/bit16gen.h>
9#include <osmocom/core/bit32gen.h>
Harald Welteec8b4502010-02-20 20:34:29 +010010
Harald Welte9325d862017-10-16 15:32:43 +020011/*! \defgroup tlv TLV parser
Harald Welte57c7d372011-08-17 17:50:55 +020012 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020013 * \file tlv.h */
Harald Welte57c7d372011-08-17 17:50:55 +020014
Harald Welteec8b4502010-02-20 20:34:29 +010015/* Terminology / wording
16 tag length value (in bits)
17
18 V - - 8
19 LV - 8 N * 8
20 TLV 8 8 N * 8
21 TL16V 8 16 N * 8
22 TLV16 8 8 N * 16
23 TvLV 8 8/16 N * 8
Harald Welte2fe68472012-07-14 01:50:33 +020024 vTvLV 8/16 8/16 N * 8
Harald Welte4a29f342017-08-09 19:00:09 +020025 T16LV 16 8 N * 8
Harald Welteec8b4502010-02-20 20:34:29 +010026*/
27
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028/*! gross length of a LV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010029#define LV_GROSS_LEN(x) (x+1)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! gross length of a TLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010031#define TLV_GROSS_LEN(x) (x+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032/*! gross length of a TLV16 type field */
Harald Welteec8b4502010-02-20 20:34:29 +010033#define TLV16_GROSS_LEN(x) ((2*x)+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034/*! gross length of a TL16V type field */
Harald Welteec8b4502010-02-20 20:34:29 +010035#define TL16V_GROSS_LEN(x) (x+3)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036/*! gross length of a L16TV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010037#define L16TV_GROSS_LEN(x) (x+3)
Harald Welte4a29f342017-08-09 19:00:09 +020038/*! gross length of a T16LV type field */
39#define T16LV_GROSS_LEN(x) (x+3)
Harald Welteec8b4502010-02-20 20:34:29 +010040
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041/*! maximum length of TLV of one byte length */
Harald Welteec8b4502010-02-20 20:34:29 +010042#define TVLV_MAX_ONEBYTE 0x7f
43
Harald Welte30a92942020-12-04 14:09:22 +010044/*! error return codes of various TLV parser functions */
45enum osmo_tlv_parser_error {
46 OSMO_TLVP_ERR_OFS_BEYOND_BUFFER = -1,
47 OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER = -2,
48 OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE = -3,
49
50 OSMO_TLVP_ERR_MAND_IE_MISSING = -50,
51 OSMO_TLVP_ERR_IE_TOO_SHORT = -51,
52};
53
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054/*! gross length of a TVLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010055static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
56{
57 if (len <= TVLV_MAX_ONEBYTE)
58 return TLV_GROSS_LEN(len);
59 else
60 return TL16V_GROSS_LEN(len);
61}
62
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! gross length of vTvL header (tag+len) */
Harald Welte2fe68472012-07-14 01:50:33 +020064static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
65{
66 uint16_t ret = 2;
67
68 if (tag > TVLV_MAX_ONEBYTE)
69 ret++;
70
71 if (len > TVLV_MAX_ONEBYTE)
72 ret++;
73
74 return ret;
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! gross length of vTvLV (tag+len+val) */
Harald Welte2fe68472012-07-14 01:50:33 +020078static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
79{
80 uint16_t ret;
81
82 if (len <= TVLV_MAX_ONEBYTE)
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030083 ret = TLV_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020084 else
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030085 ret = TL16V_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020086
87 if (tag > TVLV_MAX_ONEBYTE)
88 ret += 1;
89
90 return ret;
91}
92
Harald Welteec8b4502010-02-20 20:34:29 +010093/* TLV generation */
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010096static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
97 const uint8_t *val)
98{
99 *buf++ = len;
100 memcpy(buf, val, len);
101 return buf + len;
102}
103
Neels Hofmeyre7509802017-11-16 23:34:33 +0100104/*! Append a TLV field, a Tag-Length-Value field.
105 * \param[out] buf Location in a buffer to append TLV at.
106 * \param[in] tag Tag id to write.
107 * \param[in] len Length field to write and amount of bytes to append.
108 * \param[in] val Pointer to data to append, or NULL to append zero data.
109 * Always append tag and length. Append \a len bytes read from \a val. If val is NULL, append \a len zero
110 * bytes instead. If \a len is zero, do not append any data apart from tag and length. */
Harald Welteec8b4502010-02-20 20:34:29 +0100111static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
112 const uint8_t *val)
113{
114 *buf++ = tag;
115 *buf++ = len;
Neels Hofmeyre7509802017-11-16 23:34:33 +0100116 if (len) {
117 if (val)
118 memcpy(buf, val, len);
119 else
120 memset(buf, 0, len);
121 }
Harald Welteec8b4502010-02-20 20:34:29 +0100122 return buf + len;
123}
124
Neels Hofmeyrc71f7712020-05-29 23:58:01 +0200125/*! put (append) a TL field (a TLV field but omitting the value part). */
126static inline uint8_t *tl_put(uint8_t *buf, uint8_t tag, uint8_t len)
127{
128 *buf++ = tag;
129 *buf++ = len;
130 return buf;
131}
132
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200133/*! put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +0100134static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
135 const uint16_t *val)
136{
137 *buf++ = tag;
138 *buf++ = len;
139 memcpy(buf, val, len*2);
140 return buf + len*2;
141}
142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143/*! put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100144static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
145 const uint8_t *val)
146{
147 *buf++ = tag;
148 *buf++ = len >> 8;
149 *buf++ = len & 0xff;
150 memcpy(buf, val, len);
151 return buf + len*2;
152}
153
Neels Hofmeyrc71f7712020-05-29 23:58:01 +0200154/*! put (append) a TL16 field. */
155static inline uint8_t *tl16_put(uint8_t *buf, uint8_t tag, uint16_t len)
156{
157 *buf++ = tag;
158 *buf++ = len >> 8;
159 *buf++ = len & 0xff;
160 return buf;
161}
162
Harald Welte4a29f342017-08-09 19:00:09 +0200163/*! put (append) a TL16V field */
164static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len,
165 const uint8_t *val)
166{
167 *buf++ = tag >> 8;
168 *buf++ = tag & 0xff;
169 *buf++ = len;
170 memcpy(buf, val, len);
171 return buf + len + 2;
172}
173
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174/*! put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100175static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
176 const uint8_t *val)
177{
178 uint8_t *ret;
179
180 if (len <= TVLV_MAX_ONEBYTE) {
181 ret = tlv_put(buf, tag, len, val);
182 buf[1] |= 0x80;
183 } else
184 ret = tl16v_put(buf, tag, len, val);
185
186 return ret;
187}
188
Neels Hofmeyrc71f7712020-05-29 23:58:01 +0200189/*! put (append) a TvL field (a TvLV with variable-size length, where the value part's length is already known, but will
190 * be put() later).
191 * \returns pointer to the value's start position.
192 */
193static inline uint8_t *tvl_put(uint8_t *buf, uint8_t tag, uint16_t len)
194{
195 uint8_t *ret;
196
197 if (len <= TVLV_MAX_ONEBYTE) {
198 ret = tl_put(buf, tag, len);
199 buf[1] |= 0x80;
200 } else
201 ret = tl16_put(buf, tag, len);
202
203 return ret;
204}
205
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200206/*! put (append) a variable-length tag or variable-length length * */
Harald Welte2fe68472012-07-14 01:50:33 +0200207static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag)
208{
209 if (tag > TVLV_MAX_ONEBYTE) {
210 /* two-byte TAG */
211 *buf++ = 0x80 | (tag >> 8);
212 *buf++ = (tag & 0xff);
213 } else
214 *buf++ = tag;
215
216 return buf;
217}
218
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200219/* put (append) vTvL (GAN) field (tag + length)*/
Harald Welte2fe68472012-07-14 01:50:33 +0200220static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
221{
222 uint8_t *ret;
223
224 ret = vt_gan_put(buf, tag);
225 return vt_gan_put(ret, len);
226}
227
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200228/* put (append) vTvLV (GAN) field (tag + length + val) */
Harald Welte2fe68472012-07-14 01:50:33 +0200229static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len,
230 const uint8_t *val)
231{
232 uint8_t *ret;
233
234 ret = vtvl_gan_put(buf, tag, len );
235
236 memcpy(ret, val, len);
237 ret = buf + len;
238
239 return ret;
240}
241
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200242/*! put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100243static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
244{
245 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
246 return tlv16_put(buf, tag, len, val);
247}
248
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200249/*! put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100250static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
251 const uint8_t *val)
252{
253 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
254 return tl16v_put(buf, tag, len, val);
255}
256
Harald Welte4a29f342017-08-09 19:00:09 +0200257static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val)
258{
259 uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len));
260 return t16lv_put(buf, tag, len, val);
261}
262
Neels Hofmeyrc71f7712020-05-29 23:58:01 +0200263/*! put (append) a TvL field to \ref msgb, i.e. a TvLV with variable-size length, where the value's length is already
264 * known, but will be put() later. The value section is not yet reserved, only tag and variable-length are put in the
265 * msgb.
266 * \returns pointer to the value's start position and end of the msgb.
267 */
268static inline uint8_t *msgb_tvl_put(struct msgb *msg, uint8_t tag, uint16_t len)
269{
270 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
271 return tvl_put(buf, tag, len);
272}
273
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200274/*! put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100275static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
276 const uint8_t *val)
277{
278 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
279 return tvlv_put(buf, tag, len, val);
280}
281
Harald Weltedec201a2020-12-08 20:32:12 +0100282/*! put (append) a TvLV field containing a big-endian 16bit value to msgb. */
283static inline uint8_t *msgb_tvlv_put_16be(struct msgb *msg, uint8_t tag, uint16_t val)
284{
285 uint16_t val_be = osmo_htons(val);
286 return msgb_tvlv_put(msg, tag, 2, (const uint8_t *)&val_be);
287}
288
289/*! put (append) a TvLV field containing a big-endian 16bit value to msgb. */
290static inline uint8_t *msgb_tvlv_put_32be(struct msgb *msg, uint8_t tag, uint32_t val)
291{
292 uint32_t val_be = osmo_htonl(val);
293 return msgb_tvlv_put(msg, tag, 4, (const uint8_t *)&val_be);
294}
295
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200296/*! put (append) a vTvLV field to \ref msgb */
Harald Welte2fe68472012-07-14 01:50:33 +0200297static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag,
298 uint16_t len, const uint8_t *val)
299{
300 uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len));
301 return vtvlv_gan_put(buf, tag, len, val);
302}
303
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200304/*! put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100305static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
306 const uint8_t *val)
307{
308 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
309
310 *buf++ = len >> 8;
311 *buf++ = len & 0xff;
312 *buf++ = tag;
313 memcpy(buf, val, len);
314 return buf + len;
315}
316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100318static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
319{
320 *buf++ = val;
321 return buf;
322}
323
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200324/*! put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100325static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
326 uint8_t val)
327{
328 *buf++ = tag;
329 *buf++ = val;
330 return buf;
331}
332
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200333/*! put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100334static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
335 unsigned int len, const uint8_t *val)
336{
337 *buf++ = tag;
338 memcpy(buf, val, len);
339 return buf + len;
340}
341
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200342/*! put (append) a TV16 field
Harald Welte57c7d372011-08-17 17:50:55 +0200343 * \param[in,out] buf data buffer
344 * \param[in] tag Tag value
345 * \param[in] val Value (in host byte order!)
346 */
Harald Welteec8b4502010-02-20 20:34:29 +0100347static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
348 uint16_t val)
349{
350 *buf++ = tag;
351 *buf++ = val >> 8;
352 *buf++ = val & 0xff;
353 return buf;
354}
355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! put (append) a LV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100357 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100358static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
359{
360 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
361 return lv_put(buf, len, val);
362}
363
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200364/*! put (append) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100365 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100366static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
367{
368 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
369 return tlv_put(buf, tag, len, val);
370}
371
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200372/*! put (append) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100373 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100374static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
375{
376 uint8_t *buf = msgb_put(msg, 2);
377 return tv_put(buf, tag, val);
378}
379
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200380/*! put (append) a TVfixed field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100381 * \returns pointer to first byte after newly-put information */
Harald Welte63196de2011-03-05 14:32:50 +0100382static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
383 unsigned int len, const uint8_t *val)
384{
385 uint8_t *buf = msgb_put(msg, 1+len);
386 return tv_fixed_put(buf, tag, len, val);
387}
388
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200389/*! put (append) a V field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100390 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100391static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
392{
393 uint8_t *buf = msgb_put(msg, 1);
394 return v_put(buf, val);
395}
396
Maxd82070c2018-12-03 15:33:52 +0100397/*! put (append) a TL fields to a \ref msgb
398 * \returns pointer to the length field so it can be updated after adding new information under specified tag */
399static inline uint8_t *msgb_tl_put(struct msgb *msg, uint8_t tag)
400{
401 uint8_t *len = msgb_v_put(msg, tag);
402
403 /* reserve space for length, len points to this reserved space already */
404 msgb_v_put(msg, 0);
405
406 return len;
407}
408
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200409/*! put (append) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100410 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100411static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
412{
413 uint8_t *buf = msgb_put(msg, 3);
414 return tv16_put(buf, tag, val);
415}
416
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200417/*! push (prepend) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100418 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100419static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
420{
421 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100422 tlv_put(buf, tag, len, val);
423 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100424}
425
Max84fb5bb2018-11-08 13:23:59 +0100426/*! push 1-byte tagged value */
427static inline uint8_t *msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val)
428{
429 return msgb_tlv_push(msg, tag, 1, &val);
430}
431
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200432/*! push (prepend) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100433 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100434static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
435{
436 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100437 tv_put(buf, tag, val);
438 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100439}
440
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! push (prepend) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100442 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100443static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
444{
445 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100446 tv16_put(buf, tag, val);
447 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100448}
449
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200450/*! push (prepend) a TvLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100451 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100452static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
453 const uint8_t *val)
454{
455 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100456 tvlv_put(buf, tag, len, val);
457 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100458}
459
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200460/* push (prepend) a vTvL header to a \ref msgb
Harald Welte2fe68472012-07-14 01:50:33 +0200461 */
462static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
463 uint16_t len)
464{
465 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
466 vtvl_gan_put(buf, tag, len);
467 return buf;
468}
469
470
471static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
472 uint16_t len, const uint8_t *val)
473{
474 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
475 vtvlv_gan_put(buf, tag, len, val);
476 return buf;
477}
478
Harald Welteec8b4502010-02-20 20:34:29 +0100479/* TLV parsing */
480
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200481/*! Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100482struct tlv_p_entry {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200483 uint16_t len; /*!< length */
484 const uint8_t *val; /*!< pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100485};
486
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200487/*! TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100488enum tlv_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200489 TLV_TYPE_NONE, /*!< no type */
490 TLV_TYPE_FIXED, /*!< fixed-length value-only */
491 TLV_TYPE_T, /*!< tag-only */
492 TLV_TYPE_TV, /*!< tag-value (8bit) */
493 TLV_TYPE_TLV, /*!< tag-length-value */
494 TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */
495 TLV_TYPE_TvLV, /*!< tag, variable length, value */
496 TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */
497 TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100498};
499
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200500/*! Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100501struct tlv_def {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200502 enum tlv_type type; /*!< TLV type */
503 uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100504};
505
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200506/*! Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100507struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200508 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100509};
510
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200511/*! result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100512struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200513 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100514};
515
516extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200517extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100518
519int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
520 const struct tlv_definition *def,
521 const uint8_t *buf, int buf_len);
522int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
523 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200524int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
525 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
526 uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100527/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100528void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
529
Harald Welte8006f532019-02-13 22:23:13 +0100530int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
531 unsigned int len, const uint8_t *val);
532int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp);
533int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
534 const uint8_t *tag_order, unsigned int tag_order_len);
535
Harald Welteec8b4502010-02-20 20:34:29 +0100536#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
537#define TLVP_LEN(x, y) (x)->lv[y].len
538#define TLVP_VAL(x, y) (x)->lv[y].val
539
Harald Weltecc27fa62014-08-18 15:31:04 +0200540#define TLVP_PRES_LEN(tp, tag, min_len) \
541 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
542
Neels Hofmeyr74e4ed62018-04-13 03:55:06 +0200543/*! Return pointer to a TLV element if it is present.
544 * Usage:
545 * struct tlv_p_entry *e = TVLP_GET(&tp, TAG);
546 * if (!e)
547 * return -ENOENT;
548 * hexdump(e->val, e->len);
549 * \param[in] _tp pointer to \ref tlv_parsed.
550 * \param[in] tag IE tag to return.
551 * \returns struct tlv_p_entry pointer, or NULL if not present.
552 */
553#define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
554
555/*! Like TLVP_GET(), but enforcing a minimum val length.
556 * \param[in] _tp pointer to \ref tlv_parsed.
557 * \param[in] tag IE tag to return.
558 * \param[in] min_len Minimum value length in bytes.
559 * \returns struct tlv_p_entry pointer, or NULL if not present or too short.
560 */
561#define TLVP_GET_MINLEN(_tp, tag, min_len) \
562 (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
563
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200564/*! Like TLVP_VAL(), but enforcing a minimum val length.
565 * \param[in] _tp pointer to \ref tlv_parsed.
566 * \param[in] tag IE tag to return.
567 * \param[in] min_len Minimum value length in bytes.
Maxe6f63f22018-12-19 19:00:23 +0100568 * \returns const uint8_t pointer to value, or NULL if not present or too short.
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200569 */
570#define TLVP_VAL_MINLEN(_tp, tag, min_len) \
571 (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL)
572
573
Maxbd6d5c12018-12-19 19:01:03 +0100574/*! Obtain 1-byte TLV element.
575 * \param[in] tp pointer to \ref tlv_parsed
576 * \param[in] tag the Tag to look for
577 * \param[in] default_val default value to use if tag not available
578 * \returns the 1st byte of value with a given tag or default_val if tag was not found
579 */
580static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
581{
582 const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1);
583
584 if (res)
585 return res[0];
586
587 return default_val;
588}
589
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200590/*! Align given TLV element with 16 bit value to an even address
Andreas Eversberg01675962012-11-06 12:02:59 +0100591 * \param[in] tp pointer to \ref tlv_parsed
592 * \param[in] pos element to return
593 * \returns aligned 16 bit value
594 */
595static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
596{
597 uint16_t res;
598 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
599 return res;
600}
601
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200602/*! Align given TLV element with 32 bit value to an address that is a multiple of 4
Andreas Eversberg01675962012-11-06 12:02:59 +0100603 * \param[in] tp pointer to \ref tlv_parsed
604 * \param[in] pos element to return
605 * \returns aligned 32 bit value
606 */
607static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
608{
609 uint32_t res;
610 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
611 return res;
612}
613
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200614/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200615 * \param[in] tp pointer to \ref tlv_parsed
616 * \param[in] pos element to return
617 * \returns aligned 16 bit value in host byte order
618 */
619static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
620{
621 return osmo_load16be(TLVP_VAL(tp, pos));
622}
623
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200624/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200625 * \param[in] tp pointer to \ref tlv_parsed
626 * \param[in] pos element to return
627 * \returns aligned 32 bit value in host byte order
628 */
629static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
630{
631 return osmo_load32be(TLVP_VAL(tp, pos));
632}
633
634
Maxdbd3a922017-01-02 14:10:30 +0100635struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
636int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
Harald Weltefbd02fa2016-04-25 15:19:35 +0200637int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
638 size_t len, uint8_t **value);
639int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
640 uint8_t tag, size_t len, uint8_t **value);
641int osmo_shift_tlv(uint8_t **data, size_t *data_len,
642 uint8_t *tag, uint8_t **value, size_t *value_len);
643int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
644 uint8_t tag, uint8_t **value, size_t *value_len);
645int osmo_shift_lv(uint8_t **data, size_t *data_len,
646 uint8_t **value, size_t *value_len);
647
Harald Welte95109922020-12-04 13:55:38 +0100648#define MSG_DEF(name, mand_ies, flags) { name, mand_ies, ARRAY_SIZE(mand_ies), flags }
649
650struct osmo_tlv_prot_msg_def {
651 /*! human-readable name of message type (optional) */
652 const char *name;
653 /*! array of mandatory IEs */
654 const uint8_t *mand_ies;
655 /*! number of entries in 'mand_ies' above */
656 uint8_t mand_count;
657 /*! user-defined flags (like uplink/downlink/...) */
658 uint32_t flags;
659};
660struct osmo_tlv_prot_ie_def {
661 /*! minimum length of IE value part, in octets */
662 uint16_t min_len;
663 /*! huamn-readable name (optional) */
664 const char *name;
665};
666
667/*! Osmocom TLV protocol definition */
668struct osmo_tlv_prot_def {
669 /*! human-readable name of protocol */
670 const char *name;
671 /*! TLV parser definition (optional) */
672 const struct tlv_definition *tlv_def;
673 /*! definition of each message (8-bit message type) */
674 struct osmo_tlv_prot_msg_def msg_def[256];
675 /*! definition of IE for each 8-bit tag */
676 struct osmo_tlv_prot_ie_def ie_def[256];
677 /*! value_string array of message type names (legacy, if not populated in msg_def) */
678 const struct value_string *msgt_names;
679};
680
681const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type);
682const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei);
683
684int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
685 const struct tlv_parsed *tp, int log_subsys, const char *log_pfx);
686
687int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
688 struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
689 const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
690 int log_subsys, const char *log_pfx);
691
692static inline uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
693{
694 return pdef->msg_def[msg_type].flags;
695}
696
697
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200698/*! @} */