blob: bb0e8fc98b05c90e10eb24650a135e61f67ab391 [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 Welte50ef7332017-05-15 12:45:59 +02007#include <osmocom/core/bit16gen.h>
8#include <osmocom/core/bit32gen.h>
Harald Welteec8b4502010-02-20 20:34:29 +01009
Harald Welte9325d862017-10-16 15:32:43 +020010/*! \defgroup tlv TLV parser
Harald Welte57c7d372011-08-17 17:50:55 +020011 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020012 * \file tlv.h */
Harald Welte57c7d372011-08-17 17:50:55 +020013
Harald Welteec8b4502010-02-20 20:34:29 +010014/* Terminology / wording
15 tag length value (in bits)
16
17 V - - 8
18 LV - 8 N * 8
19 TLV 8 8 N * 8
20 TL16V 8 16 N * 8
21 TLV16 8 8 N * 16
22 TvLV 8 8/16 N * 8
Harald Welte2fe68472012-07-14 01:50:33 +020023 vTvLV 8/16 8/16 N * 8
Harald Welte4a29f342017-08-09 19:00:09 +020024 T16LV 16 8 N * 8
Harald Welteec8b4502010-02-20 20:34:29 +010025*/
26
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027/*! gross length of a LV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010028#define LV_GROSS_LEN(x) (x+1)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029/*! gross length of a TLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010030#define TLV_GROSS_LEN(x) (x+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020031/*! gross length of a TLV16 type field */
Harald Welteec8b4502010-02-20 20:34:29 +010032#define TLV16_GROSS_LEN(x) ((2*x)+2)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! gross length of a TL16V type field */
Harald Welteec8b4502010-02-20 20:34:29 +010034#define TL16V_GROSS_LEN(x) (x+3)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020035/*! gross length of a L16TV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010036#define L16TV_GROSS_LEN(x) (x+3)
Harald Welte4a29f342017-08-09 19:00:09 +020037/*! gross length of a T16LV type field */
38#define T16LV_GROSS_LEN(x) (x+3)
Harald Welteec8b4502010-02-20 20:34:29 +010039
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! maximum length of TLV of one byte length */
Harald Welteec8b4502010-02-20 20:34:29 +010041#define TVLV_MAX_ONEBYTE 0x7f
42
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043/*! gross length of a TVLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010044static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
45{
46 if (len <= TVLV_MAX_ONEBYTE)
47 return TLV_GROSS_LEN(len);
48 else
49 return TL16V_GROSS_LEN(len);
50}
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! gross length of vTvL header (tag+len) */
Harald Welte2fe68472012-07-14 01:50:33 +020053static inline uint16_t VTVL_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
54{
55 uint16_t ret = 2;
56
57 if (tag > TVLV_MAX_ONEBYTE)
58 ret++;
59
60 if (len > TVLV_MAX_ONEBYTE)
61 ret++;
62
63 return ret;
64}
65
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! gross length of vTvLV (tag+len+val) */
Harald Welte2fe68472012-07-14 01:50:33 +020067static inline uint16_t VTVLV_GAN_GROSS_LEN(uint16_t tag, uint16_t len)
68{
69 uint16_t ret;
70
71 if (len <= TVLV_MAX_ONEBYTE)
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030072 ret = TLV_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020073 else
Vadim Yanitskiyac9e2d82017-05-14 20:52:46 +030074 ret = TL16V_GROSS_LEN(len);
Harald Welte2fe68472012-07-14 01:50:33 +020075
76 if (tag > TVLV_MAX_ONEBYTE)
77 ret += 1;
78
79 return ret;
80}
81
Harald Welteec8b4502010-02-20 20:34:29 +010082/* TLV generation */
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084/*! put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010085static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
86 const uint8_t *val)
87{
88 *buf++ = len;
89 memcpy(buf, val, len);
90 return buf + len;
91}
92
Neels Hofmeyre7509802017-11-16 23:34:33 +010093/*! Append a TLV field, a Tag-Length-Value field.
94 * \param[out] buf Location in a buffer to append TLV at.
95 * \param[in] tag Tag id to write.
96 * \param[in] len Length field to write and amount of bytes to append.
97 * \param[in] val Pointer to data to append, or NULL to append zero data.
98 * Always append tag and length. Append \a len bytes read from \a val. If val is NULL, append \a len zero
99 * bytes instead. If \a len is zero, do not append any data apart from tag and length. */
Harald Welteec8b4502010-02-20 20:34:29 +0100100static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
101 const uint8_t *val)
102{
103 *buf++ = tag;
104 *buf++ = len;
Neels Hofmeyre7509802017-11-16 23:34:33 +0100105 if (len) {
106 if (val)
107 memcpy(buf, val, len);
108 else
109 memset(buf, 0, len);
110 }
Harald Welteec8b4502010-02-20 20:34:29 +0100111 return buf + len;
112}
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +0100115static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
116 const uint16_t *val)
117{
118 *buf++ = tag;
119 *buf++ = len;
120 memcpy(buf, val, len*2);
121 return buf + len*2;
122}
123
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200124/*! put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100125static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
126 const uint8_t *val)
127{
128 *buf++ = tag;
129 *buf++ = len >> 8;
130 *buf++ = len & 0xff;
131 memcpy(buf, val, len);
132 return buf + len*2;
133}
134
Harald Welte4a29f342017-08-09 19:00:09 +0200135/*! put (append) a TL16V field */
136static inline uint8_t *t16lv_put(uint8_t *buf, uint16_t tag, uint8_t len,
137 const uint8_t *val)
138{
139 *buf++ = tag >> 8;
140 *buf++ = tag & 0xff;
141 *buf++ = len;
142 memcpy(buf, val, len);
143 return buf + len + 2;
144}
145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146/*! put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100147static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
148 const uint8_t *val)
149{
150 uint8_t *ret;
151
152 if (len <= TVLV_MAX_ONEBYTE) {
153 ret = tlv_put(buf, tag, len, val);
154 buf[1] |= 0x80;
155 } else
156 ret = tl16v_put(buf, tag, len, val);
157
158 return ret;
159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! put (append) a variable-length tag or variable-length length * */
Harald Welte2fe68472012-07-14 01:50:33 +0200162static inline uint8_t *vt_gan_put(uint8_t *buf, uint16_t tag)
163{
164 if (tag > TVLV_MAX_ONEBYTE) {
165 /* two-byte TAG */
166 *buf++ = 0x80 | (tag >> 8);
167 *buf++ = (tag & 0xff);
168 } else
169 *buf++ = tag;
170
171 return buf;
172}
173
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174/* put (append) vTvL (GAN) field (tag + length)*/
Harald Welte2fe68472012-07-14 01:50:33 +0200175static inline uint8_t *vtvl_gan_put(uint8_t *buf, uint16_t tag, uint16_t len)
176{
177 uint8_t *ret;
178
179 ret = vt_gan_put(buf, tag);
180 return vt_gan_put(ret, len);
181}
182
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200183/* put (append) vTvLV (GAN) field (tag + length + val) */
Harald Welte2fe68472012-07-14 01:50:33 +0200184static inline uint8_t *vtvlv_gan_put(uint8_t *buf, uint16_t tag, uint16_t len,
185 const uint8_t *val)
186{
187 uint8_t *ret;
188
189 ret = vtvl_gan_put(buf, tag, len );
190
191 memcpy(ret, val, len);
192 ret = buf + len;
193
194 return ret;
195}
196
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200197/*! put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100198static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
199{
200 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
201 return tlv16_put(buf, tag, len, val);
202}
203
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200204/*! put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100205static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
206 const uint8_t *val)
207{
208 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
209 return tl16v_put(buf, tag, len, val);
210}
211
Harald Welte4a29f342017-08-09 19:00:09 +0200212static inline uint8_t *msgb_t16lv_put(struct msgb *msg, uint16_t tag, uint8_t len, const uint8_t *val)
213{
214 uint8_t *buf = msgb_put(msg, T16LV_GROSS_LEN(len));
215 return t16lv_put(buf, tag, len, val);
216}
217
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218/*! put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100219static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
220 const uint8_t *val)
221{
222 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
223 return tvlv_put(buf, tag, len, val);
224}
225
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200226/*! put (append) a vTvLV field to \ref msgb */
Harald Welte2fe68472012-07-14 01:50:33 +0200227static inline uint8_t *msgb_vtvlv_gan_put(struct msgb *msg, uint16_t tag,
228 uint16_t len, const uint8_t *val)
229{
230 uint8_t *buf = msgb_put(msg, VTVLV_GAN_GROSS_LEN(tag, len));
231 return vtvlv_gan_put(buf, tag, len, val);
232}
233
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200234/*! put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100235static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
236 const uint8_t *val)
237{
238 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
239
240 *buf++ = len >> 8;
241 *buf++ = len & 0xff;
242 *buf++ = tag;
243 memcpy(buf, val, len);
244 return buf + len;
245}
246
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200247/*! put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100248static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
249{
250 *buf++ = val;
251 return buf;
252}
253
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254/*! put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100255static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
256 uint8_t val)
257{
258 *buf++ = tag;
259 *buf++ = val;
260 return buf;
261}
262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100264static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
265 unsigned int len, const uint8_t *val)
266{
267 *buf++ = tag;
268 memcpy(buf, val, len);
269 return buf + len;
270}
271
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200272/*! put (append) a TV16 field
Harald Welte57c7d372011-08-17 17:50:55 +0200273 * \param[in,out] buf data buffer
274 * \param[in] tag Tag value
275 * \param[in] val Value (in host byte order!)
276 */
Harald Welteec8b4502010-02-20 20:34:29 +0100277static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
278 uint16_t val)
279{
280 *buf++ = tag;
281 *buf++ = val >> 8;
282 *buf++ = val & 0xff;
283 return buf;
284}
285
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200286/*! put (append) a LV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100287 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100288static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
289{
290 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
291 return lv_put(buf, len, val);
292}
293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294/*! put (append) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100295 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100296static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
297{
298 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
299 return tlv_put(buf, tag, len, val);
300}
301
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200302/*! put (append) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100303 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100304static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
305{
306 uint8_t *buf = msgb_put(msg, 2);
307 return tv_put(buf, tag, val);
308}
309
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200310/*! put (append) a TVfixed field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100311 * \returns pointer to first byte after newly-put information */
Harald Welte63196de2011-03-05 14:32:50 +0100312static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
313 unsigned int len, const uint8_t *val)
314{
315 uint8_t *buf = msgb_put(msg, 1+len);
316 return tv_fixed_put(buf, tag, len, val);
317}
318
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200319/*! put (append) a V field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100320 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100321static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
322{
323 uint8_t *buf = msgb_put(msg, 1);
324 return v_put(buf, val);
325}
326
Maxd82070c2018-12-03 15:33:52 +0100327/*! put (append) a TL fields to a \ref msgb
328 * \returns pointer to the length field so it can be updated after adding new information under specified tag */
329static inline uint8_t *msgb_tl_put(struct msgb *msg, uint8_t tag)
330{
331 uint8_t *len = msgb_v_put(msg, tag);
332
333 /* reserve space for length, len points to this reserved space already */
334 msgb_v_put(msg, 0);
335
336 return len;
337}
338
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200339/*! put (append) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100340 * \returns pointer to first byte after newly-put information */
Harald Welteec8b4502010-02-20 20:34:29 +0100341static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
342{
343 uint8_t *buf = msgb_put(msg, 3);
344 return tv16_put(buf, tag, val);
345}
346
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200347/*! push (prepend) a TLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100348 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100349static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
350{
351 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100352 tlv_put(buf, tag, len, val);
353 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100354}
355
Max84fb5bb2018-11-08 13:23:59 +0100356/*! push 1-byte tagged value */
357static inline uint8_t *msgb_tlv1_push(struct msgb *msg, uint8_t tag, uint8_t val)
358{
359 return msgb_tlv_push(msg, tag, 1, &val);
360}
361
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200362/*! push (prepend) a TV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100363 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100364static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
365{
366 uint8_t *buf = msgb_push(msg, 2);
Harald Welte2c020432012-01-22 23:03:38 +0100367 tv_put(buf, tag, val);
368 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100369}
370
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200371/*! push (prepend) a TV16 field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100372 * \returns pointer to first byte of newly-pushed information */
Harald Welteec8b4502010-02-20 20:34:29 +0100373static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
374{
375 uint8_t *buf = msgb_push(msg, 3);
Harald Welte2c020432012-01-22 23:03:38 +0100376 tv16_put(buf, tag, val);
377 return buf;
Harald Welteec8b4502010-02-20 20:34:29 +0100378}
379
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200380/*! push (prepend) a TvLV field to a \ref msgb
Harald Welte2c020432012-01-22 23:03:38 +0100381 * \returns pointer to first byte of newly-pushed information */
Harald Welte3415d412010-02-21 19:03:41 +0100382static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
383 const uint8_t *val)
384{
385 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
Harald Welte2c020432012-01-22 23:03:38 +0100386 tvlv_put(buf, tag, len, val);
387 return buf;
Harald Welte3415d412010-02-21 19:03:41 +0100388}
389
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200390/* push (prepend) a vTvL header to a \ref msgb
Harald Welte2fe68472012-07-14 01:50:33 +0200391 */
392static inline uint8_t *msgb_vtvl_gan_push(struct msgb *msg, uint16_t tag,
393 uint16_t len)
394{
395 uint8_t *buf = msgb_push(msg, VTVL_GAN_GROSS_LEN(tag, len));
396 vtvl_gan_put(buf, tag, len);
397 return buf;
398}
399
400
401static inline uint8_t *msgb_vtvlv_gan_push(struct msgb *msg, uint16_t tag,
402 uint16_t len, const uint8_t *val)
403{
404 uint8_t *buf = msgb_push(msg, VTVLV_GAN_GROSS_LEN(tag, len));
405 vtvlv_gan_put(buf, tag, len, val);
406 return buf;
407}
408
Harald Welteec8b4502010-02-20 20:34:29 +0100409/* TLV parsing */
410
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200411/*! Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100412struct tlv_p_entry {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200413 uint16_t len; /*!< length */
414 const uint8_t *val; /*!< pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100415};
416
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200417/*! TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100418enum tlv_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200419 TLV_TYPE_NONE, /*!< no type */
420 TLV_TYPE_FIXED, /*!< fixed-length value-only */
421 TLV_TYPE_T, /*!< tag-only */
422 TLV_TYPE_TV, /*!< tag-value (8bit) */
423 TLV_TYPE_TLV, /*!< tag-length-value */
424 TLV_TYPE_TL16V, /*!< tag, 16 bit length, value */
425 TLV_TYPE_TvLV, /*!< tag, variable length, value */
426 TLV_TYPE_SINGLE_TV, /*!< tag and value (both 4 bit) in 1 byte */
427 TLV_TYPE_vTvLV_GAN, /*!< variable-length tag, variable-length length */
Harald Welteec8b4502010-02-20 20:34:29 +0100428};
429
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200430/*! Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100431struct tlv_def {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200432 enum tlv_type type; /*!< TLV type */
433 uint8_t fixed_len; /*!< length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100434};
435
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200436/*! Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100437struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200438 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100439};
440
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100442struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200443 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100444};
445
446extern struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +0200447extern struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +0100448
449int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
450 const struct tlv_definition *def,
451 const uint8_t *buf, int buf_len);
452int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
453 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200454int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
455 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
456 uint8_t lv_tag, uint8_t lv_tag2);
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +0100457/* take a master (src) tlv def and fill up all empty slots in 'dst' */
Harald Welteec8b4502010-02-20 20:34:29 +0100458void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
459
Harald Welte8006f532019-02-13 22:23:13 +0100460int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
461 unsigned int len, const uint8_t *val);
462int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp);
463int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
464 const uint8_t *tag_order, unsigned int tag_order_len);
465
Harald Welteec8b4502010-02-20 20:34:29 +0100466#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
467#define TLVP_LEN(x, y) (x)->lv[y].len
468#define TLVP_VAL(x, y) (x)->lv[y].val
469
Harald Weltecc27fa62014-08-18 15:31:04 +0200470#define TLVP_PRES_LEN(tp, tag, min_len) \
471 (TLVP_PRESENT(tp, tag) && TLVP_LEN(tp, tag) >= min_len)
472
Neels Hofmeyr74e4ed62018-04-13 03:55:06 +0200473/*! Return pointer to a TLV element if it is present.
474 * Usage:
475 * struct tlv_p_entry *e = TVLP_GET(&tp, TAG);
476 * if (!e)
477 * return -ENOENT;
478 * hexdump(e->val, e->len);
479 * \param[in] _tp pointer to \ref tlv_parsed.
480 * \param[in] tag IE tag to return.
481 * \returns struct tlv_p_entry pointer, or NULL if not present.
482 */
483#define TLVP_GET(_tp, tag) (TLVP_PRESENT(_tp, tag)? &(_tp)->lv[tag] : NULL)
484
485/*! Like TLVP_GET(), but enforcing a minimum val length.
486 * \param[in] _tp pointer to \ref tlv_parsed.
487 * \param[in] tag IE tag to return.
488 * \param[in] min_len Minimum value length in bytes.
489 * \returns struct tlv_p_entry pointer, or NULL if not present or too short.
490 */
491#define TLVP_GET_MINLEN(_tp, tag, min_len) \
492 (TLVP_PRES_LEN(_tp, tag, min_len)? &(_tp)->lv[tag] : NULL)
493
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200494/*! Like TLVP_VAL(), but enforcing a minimum val length.
495 * \param[in] _tp pointer to \ref tlv_parsed.
496 * \param[in] tag IE tag to return.
497 * \param[in] min_len Minimum value length in bytes.
Maxe6f63f22018-12-19 19:00:23 +0100498 * \returns const uint8_t pointer to value, or NULL if not present or too short.
Harald Welte1fbe3eb2018-06-02 13:34:06 +0200499 */
500#define TLVP_VAL_MINLEN(_tp, tag, min_len) \
501 (TLVP_PRES_LEN(_tp, tag, min_len)? (_tp)->lv[tag].val : NULL)
502
503
Maxbd6d5c12018-12-19 19:01:03 +0100504/*! Obtain 1-byte TLV element.
505 * \param[in] tp pointer to \ref tlv_parsed
506 * \param[in] tag the Tag to look for
507 * \param[in] default_val default value to use if tag not available
508 * \returns the 1st byte of value with a given tag or default_val if tag was not found
509 */
510static inline uint8_t tlvp_val8(const struct tlv_parsed *tp, uint8_t tag, uint8_t default_val)
511{
512 const uint8_t *res = TLVP_VAL_MINLEN(tp, tag, 1);
513
514 if (res)
515 return res[0];
516
517 return default_val;
518}
519
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200520/*! Align given TLV element with 16 bit value to an even address
Andreas Eversberg01675962012-11-06 12:02:59 +0100521 * \param[in] tp pointer to \ref tlv_parsed
522 * \param[in] pos element to return
523 * \returns aligned 16 bit value
524 */
525static inline uint16_t tlvp_val16_unal(const struct tlv_parsed *tp, int pos)
526{
527 uint16_t res;
528 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
529 return res;
530}
531
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200532/*! Align given TLV element with 32 bit value to an address that is a multiple of 4
Andreas Eversberg01675962012-11-06 12:02:59 +0100533 * \param[in] tp pointer to \ref tlv_parsed
534 * \param[in] pos element to return
535 * \returns aligned 32 bit value
536 */
537static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos)
538{
539 uint32_t res;
540 memcpy(&res, TLVP_VAL(tp, pos), sizeof(res));
541 return res;
542}
543
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200544/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200545 * \param[in] tp pointer to \ref tlv_parsed
546 * \param[in] pos element to return
547 * \returns aligned 16 bit value in host byte order
548 */
549static inline uint16_t tlvp_val16be(const struct tlv_parsed *tp, int pos)
550{
551 return osmo_load16be(TLVP_VAL(tp, pos));
552}
553
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200554/*! Retrieve (possibly unaligned) TLV element and convert to host byte order
Harald Welte50ef7332017-05-15 12:45:59 +0200555 * \param[in] tp pointer to \ref tlv_parsed
556 * \param[in] pos element to return
557 * \returns aligned 32 bit value in host byte order
558 */
559static inline uint32_t tlvp_val32be(const struct tlv_parsed *tp, int pos)
560{
561 return osmo_load32be(TLVP_VAL(tp, pos));
562}
563
564
Maxdbd3a922017-01-02 14:10:30 +0100565struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx);
566int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src);
Harald Weltefbd02fa2016-04-25 15:19:35 +0200567int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
568 size_t len, uint8_t **value);
569int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
570 uint8_t tag, size_t len, uint8_t **value);
571int osmo_shift_tlv(uint8_t **data, size_t *data_len,
572 uint8_t *tag, uint8_t **value, size_t *value_len);
573int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
574 uint8_t tag, uint8_t **value, size_t *value_len);
575int osmo_shift_lv(uint8_t **data, size_t *data_len,
576 uint8_t **value, size_t *value_len);
577
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200578/*! @} */