blob: bcdebb3f50f81fb3fe934eea00b977ae9effd364 [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
Vadim Yanitskiyd397a532021-02-06 16:46:03 +0100417/*! put (append) a TV32 field to a \ref msgb
418 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200460/*! push (prepend) a TvLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100461 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100462static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
463 const uint8_t *val)
464{
465 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100466 tvlv_put(buf, tag, len, val);
467 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100468}
469
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200470/* push (prepend) a vTvL header to a \ref msgb
Harald Welte2fe68472012-07-14 01:50:33 +0200471 */
472static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
473 uint16_t len)
474{
475 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
476 vtvl_gan_put(buf, tag, len);
477 return buf;
478}
479
480
481static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
482 uint16_t len, const uint8_t *val)
483{
484 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
485 vtvlv_gan_put(buf, tag, len, val);
486 return buf;
487}
488
Harald Welteec8b4502010-02-20 20:34:29 +0100489/* TLV parsing */
490
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200491/*! Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100492struct tlv_p_entry {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200493 uint16_t len; /*!< length */
494 const uint8_t *val; /*!< pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100495};
496
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200497/*! TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100498enum tlv_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200499 TLV_TYPE_NONE, /*!< no type */
500 TLV_TYPE_FIXED, /*!< fixed-length value-only */
501 TLV_TYPE_T, /*!< tag-only */
502 TLV_TYPE_TV, /*!< tag-value (8bit) */
503 TLV_TYPE_TLV, /*!< tag-length-value */
504 TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */
505 TLV_TYPE_TvLV, /*!< tag, variable length, value */
506 TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */
507 TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100508};
509
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200510/*! Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100511struct tlv_def {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200512 enum tlv_type type; /*!< TLV type */
513 uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100514};
515
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200516/*! Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100517struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200518 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100519};
520
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200521/*! result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100522struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200523 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100524};
525
526extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200527extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100528
529int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
530 const struct tlv_definition *def,
531 const uint8_t *buf, int buf_len);
532int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
533 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200534int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
535 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
536 uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100537/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100538void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
539
Harald Welte8006f532019-02-13 22:23:13 +0100540int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
541 unsigned int len, const uint8_t *val);
542int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp);
543int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
544 const uint8_t *tag_order, unsigned int tag_order_len);
545
Harald Welteec8b4502010-02-20 20:34:29 +0100546#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
547#define TLVP_LEN(x, y) (x)->lv[y].len
548#define TLVP_VAL(x, y) (x)->lv[y].val
549
Harald Weltecc27fa62014-08-18 15:31:04 +0200550#define TLVP_PRES_LEN(tp, tag, min_len) \
551 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
552
Neels Hofmeyr74e4ed62018-04-13 03:55:06 +0200553/*! Return pointer to a TLV element if it is present.
554 * Usage:
555 * struct tlv_p_entry *e = TVLP_GET(&tp, TAG);
556 * if (!e)
557 * return -ENOENT;
558 * hexdump(e->val, e->len);
559 * \param[in] _tp pointer to \ref tlv_parsed.
560 * \param[in] tag IE tag to return.
561 * \returns struct tlv_p_entry pointer, or NULL if not present.
562 */
563#define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
564
565/*! Like TLVP_GET(), but enforcing a minimum val length.
566 * \param[in] _tp pointer to \ref tlv_parsed.
567 * \param[in] tag IE tag to return.
568 * \param[in] min_len Minimum value length in bytes.
569 * \returns struct tlv_p_entry pointer, or NULL if not present or too short.
570 */
571#define TLVP_GET_MINLEN(_tp, tag, min_len) \
572 (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
573
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200574/*! Like TLVP_VAL(), but enforcing a minimum val length.
575 * \param[in] _tp pointer to \ref tlv_parsed.
576 * \param[in] tag IE tag to return.
577 * \param[in] min_len Minimum value length in bytes.
Maxe6f63f22018-12-19 19:00:23 +0100578 * \returns const uint8_t pointer to value, or NULL if not present or too short.
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200579 */
580#define TLVP_VAL_MINLEN(_tp, tag, min_len) \
581 (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL)
582
583
Maxbd6d5c12018-12-19 19:01:03 +0100584/*! Obtain 1-byte TLV element.
585 * \param[in] tp pointer to \ref tlv_parsed
586 * \param[in] tag the Tag to look for
587 * \param[in] default_val default value to use if tag not available
588 * \returns the 1st byte of value with a given tag or default_val if tag was not found
589 */
590static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
591{
592 const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1);
593
594 if (res)
595 return res[0];
596
597 return default_val;
598}
599
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200600/*! Align given TLV element with 16 bit value to an even address
Andreas Eversberg01675962012-11-06 12:02:59 +0100601 * \param[in] tp pointer to \ref tlv_parsed
602 * \param[in] pos element to return
603 * \returns aligned 16 bit value
604 */
605static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
606{
607 uint16_t res;
608 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
609 return res;
610}
611
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200612/*! Align given TLV element with 32 bit value to an address that is a multiple of 4
Andreas Eversberg01675962012-11-06 12:02:59 +0100613 * \param[in] tp pointer to \ref tlv_parsed
614 * \param[in] pos element to return
615 * \returns aligned 32 bit value
616 */
617static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
618{
619 uint32_t res;
620 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
621 return res;
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 16 bit value in host byte order
628 */
629static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
630{
631 return osmo_load16be(TLVP_VAL(tp, pos));
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 32 bit value in host byte order
638 */
639static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
640{
641 return osmo_load32be(TLVP_VAL(tp, pos));
642}
643
644
Maxdbd3a922017-01-02 14:10:30 +0100645struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
646int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
Harald Weltefbd02fa2016-04-25 15:19:35 +0200647int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
648 size_t len, uint8_t **value);
649int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
650 uint8_t tag, size_t len, uint8_t **value);
651int osmo_shift_tlv(uint8_t **data, size_t *data_len,
652 uint8_t *tag, uint8_t **value, size_t *value_len);
653int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
654 uint8_t tag, uint8_t **value, size_t *value_len);
655int osmo_shift_lv(uint8_t **data, size_t *data_len,
656 uint8_t **value, size_t *value_len);
657
Harald Welte95109922020-12-04 13:55:38 +0100658#define MSG_DEF(name, mand_ies, flags) { name, mand_ies, ARRAY_SIZE(mand_ies), flags }
659
660struct osmo_tlv_prot_msg_def {
661 /*! human-readable name of message type (optional) */
662 const char *name;
663 /*! array of mandatory IEs */
664 const uint8_t *mand_ies;
665 /*! number of entries in 'mand_ies' above */
666 uint8_t mand_count;
667 /*! user-defined flags (like uplink/downlink/...) */
668 uint32_t flags;
669};
670struct osmo_tlv_prot_ie_def {
671 /*! minimum length of IE value part, in octets */
672 uint16_t min_len;
673 /*! huamn-readable name (optional) */
674 const char *name;
675};
676
677/*! Osmocom TLV protocol definition */
678struct osmo_tlv_prot_def {
679 /*! human-readable name of protocol */
680 const char *name;
681 /*! TLV parser definition (optional) */
682 const struct tlv_definition *tlv_def;
683 /*! definition of each message (8-bit message type) */
684 struct osmo_tlv_prot_msg_def msg_def[256];
685 /*! definition of IE for each 8-bit tag */
686 struct osmo_tlv_prot_ie_def ie_def[256];
687 /*! value_string array of message type names (legacy, if not populated in msg_def) */
688 const struct value_string *msgt_names;
689};
690
691const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type);
692const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei);
693
694int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
695 const struct tlv_parsed *tp, int log_subsys, const char *log_pfx);
696
697int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
698 struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
699 const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
700 int log_subsys, const char *log_pfx);
701
702static inline uint32_t osmo_tlv_prot_msgt_flags(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
703{
704 return pdef->msg_def[msg_type].flags;
705}
706
707
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200708/*! @} */