blob: 28e897d790e000ddf22d3d76a0a831589d4dc591 [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);
Daniel Willmann2fa0e9d2021-03-17 16:24:15 +0100151 return buf + len;
Harald Welteec8b4502010-02-20 20:34:29 +0100152}
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);
Daniel Willmann2fa0e9d2021-03-17 16:24:15 +0100171 return buf + len;
Harald Welte4a29f342017-08-09 19:00:09 +0200172}
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 */
Pau Espin Pedrol6960b6a2022-08-08 17:34:50 +0200325static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
Harald Welteec8b4502010-02-20 20:34:29 +0100326 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 */
Pau Espin Pedrol6960b6a2022-08-08 17:34:50 +0200347static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
Harald Welteec8b4502010-02-20 20:34:29 +0100348 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
Vadim Yanitskiy53aff132021-02-06 16:50:34 +0100409/*! put (append) a TV16 field (network order) to the given 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
Vadim Yanitskiy53aff132021-02-06 16:50:34 +0100417/*! put (append) a TV32 field (network order) to the given msgb
Vadim Yanitskiyd397a532021-02-06 16:46:03 +0100418 * \returns pointer to first byte after newly-put information */
419static inline uint8_t *msgb_tv32_put(struct msgb *msg, uint8_t tag, uint32_t val)
420{
421 uint8_t *buf = msgb_put(msg, 1 + 4);
422 *buf++ = tag;
423 osmo_store32be(val, buf);
424 return msg->tail;
425}
426
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200427/*! push (prepend) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100428 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100429static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
430{
431 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100432 tlv_put(buf, tag, len, val);
433 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100434}
435
Max84fb5bb2018-11-08 13:23:59 +0100436/*! push 1-byte tagged value */
437static inline uint8_t *msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val)
438{
439 return msgb_tlv_push(msg, tag, 1, &val);
440}
441
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200442/*! push (prepend) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100443 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100444static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
445{
446 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100447 tv_put(buf, tag, val);
448 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100449}
450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! push (prepend) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100452 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100453static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
454{
455 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100456 tv16_put(buf, tag, val);
457 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100458}
459
Pau Espin Pedrol6df53dc2023-08-09 17:21:01 +0200460/*! push (prepend) a TV32 field to a \ref msgb
461 * \returns pointer to first byte of newly-pushed information */
462static inline uint8_t *msgb_tv32_push(struct msgb *msg, uint8_t tag, uint32_t val)
463{
464 uint8_t *buf = msgb_push(msg, 5);
465 *buf++ = tag;
466 osmo_store32be(val, buf);
467 return buf;
468}
469
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200470/*! push (prepend) a TvLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100471 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100472static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
473 const uint8_t *val)
474{
475 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100476 tvlv_put(buf, tag, len, val);
477 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100478}
479
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200480/* push (prepend) a vTvL header to a \ref msgb
Harald Welte2fe68472012-07-14 01:50:33 +0200481 */
482static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
483 uint16_t len)
484{
485 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
486 vtvl_gan_put(buf, tag, len);
487 return buf;
488}
489
490
491static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
492 uint16_t len, const uint8_t *val)
493{
494 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
495 vtvlv_gan_put(buf, tag, len, val);
496 return buf;
497}
498
Harald Welteec8b4502010-02-20 20:34:29 +0100499/* TLV parsing */
500
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200501/*! Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100502struct tlv_p_entry {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200503 uint16_t len; /*!< length */
504 const uint8_t *val; /*!< pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100505};
506
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200507/*! TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100508enum tlv_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200509 TLV_TYPE_NONE, /*!< no type */
510 TLV_TYPE_FIXED, /*!< fixed-length value-only */
511 TLV_TYPE_T, /*!< tag-only */
512 TLV_TYPE_TV, /*!< tag-value (8bit) */
513 TLV_TYPE_TLV, /*!< tag-length-value */
514 TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */
515 TLV_TYPE_TvLV, /*!< tag, variable length, value */
516 TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */
517 TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100518};
519
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200520/*! Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100521struct tlv_def {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200522 enum tlv_type type; /*!< TLV type */
523 uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100524};
525
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200526/*! Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100527struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200528 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100529};
530
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200531/*! result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100532struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200533 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100534};
535
536extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200537extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100538
539int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
540 const struct tlv_definition *def,
541 const uint8_t *buf, int buf_len);
542int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
543 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200544int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
545 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
546 uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100547/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100548void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
549
Harald Welte8006f532019-02-13 22:23:13 +0100550int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
551 unsigned int len, const uint8_t *val);
552int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp);
553int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
554 const uint8_t *tag_order, unsigned int tag_order_len);
555
Pau Espin Pedrol862dd362022-08-08 17:35:07 +0200556#define TLVP_PRESENT(x, y) (!!((x)->lv[y].val))
Harald Welteec8b4502010-02-20 20:34:29 +0100557#define TLVP_LEN(x, y) (x)->lv[y].len
558#define TLVP_VAL(x, y) (x)->lv[y].val
559
Harald Weltecc27fa62014-08-18 15:31:04 +0200560#define TLVP_PRES_LEN(tp, tag, min_len) \
561 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
562
Neels Hofmeyr74e4ed62018-04-13 03:55:06 +0200563/*! Return pointer to a TLV element if it is present.
564 * Usage:
565 * struct tlv_p_entry *e = TVLP_GET(&tp, TAG);
566 * if (!e)
567 * return -ENOENT;
568 * hexdump(e->val, e->len);
569 * \param[in] _tp pointer to \ref tlv_parsed.
570 * \param[in] tag IE tag to return.
571 * \returns struct tlv_p_entry pointer, or NULL if not present.
572 */
573#define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
574
575/*! Like TLVP_GET(), but enforcing a minimum val length.
576 * \param[in] _tp pointer to \ref tlv_parsed.
577 * \param[in] tag IE tag to return.
578 * \param[in] min_len Minimum value length in bytes.
579 * \returns struct tlv_p_entry pointer, or NULL if not present or too short.
580 */
581#define TLVP_GET_MINLEN(_tp, tag, min_len) \
582 (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
583
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200584/*! Like TLVP_VAL(), but enforcing a minimum val length.
585 * \param[in] _tp pointer to \ref tlv_parsed.
586 * \param[in] tag IE tag to return.
587 * \param[in] min_len Minimum value length in bytes.
Maxe6f63f22018-12-19 19:00:23 +0100588 * \returns const uint8_t pointer to value, or NULL if not present or too short.
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200589 */
590#define TLVP_VAL_MINLEN(_tp, tag, min_len) \
591 (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL)
592
593
Maxbd6d5c12018-12-19 19:01:03 +0100594/*! Obtain 1-byte TLV element.
595 * \param[in] tp pointer to \ref tlv_parsed
596 * \param[in] tag the Tag to look for
597 * \param[in] default_val default value to use if tag not available
598 * \returns the 1st byte of value with a given tag or default_val if tag was not found
599 */
600static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
601{
602 const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1);
603
604 if (res)
605 return res[0];
606
607 return default_val;
608}
609
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200610/*! Align given TLV element with 16 bit value to an even address
Andreas Eversberg01675962012-11-06 12:02:59 +0100611 * \param[in] tp pointer to \ref tlv_parsed
612 * \param[in] pos element to return
613 * \returns aligned 16 bit value
614 */
615static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
616{
617 uint16_t res;
618 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
619 return res;
620}
621
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200622/*! Align given TLV element with 32 bit value to an address that is a multiple of 4
Andreas Eversberg01675962012-11-06 12:02:59 +0100623 * \param[in] tp pointer to \ref tlv_parsed
624 * \param[in] pos element to return
625 * \returns aligned 32 bit value
626 */
627static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
628{
629 uint32_t res;
630 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
631 return res;
632}
633
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200634/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200635 * \param[in] tp pointer to \ref tlv_parsed
636 * \param[in] pos element to return
637 * \returns aligned 16 bit value in host byte order
638 */
639static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
640{
641 return osmo_load16be(TLVP_VAL(tp, pos));
642}
643
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200644/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200645 * \param[in] tp pointer to \ref tlv_parsed
646 * \param[in] pos element to return
647 * \returns aligned 32 bit value in host byte order
648 */
649static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
650{
651 return osmo_load32be(TLVP_VAL(tp, pos));
652}
653
654
Maxdbd3a922017-01-02 14:10:30 +0100655struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
656int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
Harald Weltefbd02fa2016-04-25 15:19:35 +0200657int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
658 size_t len, uint8_t **value);
659int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
660 uint8_t tag, size_t len, uint8_t **value);
661int osmo_shift_tlv(uint8_t **data, size_t *data_len,
662 uint8_t *tag, uint8_t **value, size_t *value_len);
663int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
664 uint8_t tag, uint8_t **value, size_t *value_len);
665int osmo_shift_lv(uint8_t **data, size_t *data_len,
666 uint8_t **value, size_t *value_len);
667
Harald Welte95109922020-12-04 13:55:38 +0100668#define MSG_DEF(name, mand_ies, flags) { name, mand_ies, ARRAY_SIZE(mand_ies), flags }
669
670struct osmo_tlv_prot_msg_def {
671 /*! human-readable name of message type (optional) */
672 const char *name;
673 /*! array of mandatory IEs */
674 const uint8_t *mand_ies;
675 /*! number of entries in 'mand_ies' above */
676 uint8_t mand_count;
677 /*! user-defined flags (like uplink/downlink/...) */
678 uint32_t flags;
679};
680struct osmo_tlv_prot_ie_def {
681 /*! minimum length of IE value part, in octets */
682 uint16_t min_len;
683 /*! huamn-readable name (optional) */
684 const char *name;
685};
686
687/*! Osmocom TLV protocol definition */
688struct osmo_tlv_prot_def {
689 /*! human-readable name of protocol */
690 const char *name;
691 /*! TLV parser definition (optional) */
692 const struct tlv_definition *tlv_def;
693 /*! definition of each message (8-bit message type) */
694 struct osmo_tlv_prot_msg_def msg_def[256];
695 /*! definition of IE for each 8-bit tag */
696 struct osmo_tlv_prot_ie_def ie_def[256];
697 /*! value_string array of message type names (legacy, if not populated in msg_def) */
698 const struct value_string *msgt_names;
699};
700
701const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type);
702const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei);
703
704int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
705 const struct tlv_parsed *tp, int log_subsys, const char *log_pfx);
706
707int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
708 struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
709 const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
710 int log_subsys, const char *log_pfx);
711
712static inline uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
713{
714 return pdef->msg_def[msg_type].flags;
715}
716
717
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200718/*! @} */