blob: 7b41d2d73ac7628c997380ec2d29738b37918ef7 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef _TLV_H
2#define _TLV_H
3
4#include <stdint.h>
5#include <string.h>
6
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01007#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +01008
Harald Welte57c7d372011-08-17 17:50:55 +02009/*! \defgroup tlv GSM L3 compatible TLV parser
10 * @{
11 */
12/*! \file tlv.h */
13
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
23
24*/
25
Harald Welte57c7d372011-08-17 17:50:55 +020026/*! \brief gross length of a LV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010027#define LV_GROSS_LEN(x) (x+1)
Harald Welte57c7d372011-08-17 17:50:55 +020028/*! \brief gross length of a TLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010029#define TLV_GROSS_LEN(x) (x+2)
Harald Welte57c7d372011-08-17 17:50:55 +020030/*! \brief gross length of a TLV16 type field */
Harald Welteec8b4502010-02-20 20:34:29 +010031#define TLV16_GROSS_LEN(x) ((2*x)+2)
Harald Welte57c7d372011-08-17 17:50:55 +020032/*! \brief gross length of a TL16V type field */
Harald Welteec8b4502010-02-20 20:34:29 +010033#define TL16V_GROSS_LEN(x) (x+3)
Harald Welte57c7d372011-08-17 17:50:55 +020034/*! \brief gross length of a L16TV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010035#define L16TV_GROSS_LEN(x) (x+3)
36
Harald Welte57c7d372011-08-17 17:50:55 +020037/*! \brief maximum length of TLV of one byte length */
Harald Welteec8b4502010-02-20 20:34:29 +010038#define TVLV_MAX_ONEBYTE 0x7f
39
Harald Welte57c7d372011-08-17 17:50:55 +020040/*! \brief gross length of a TVLV type field */
Harald Welteec8b4502010-02-20 20:34:29 +010041static inline uint16_t TVLV_GROSS_LEN(uint16_t len)
42{
43 if (len <= TVLV_MAX_ONEBYTE)
44 return TLV_GROSS_LEN(len);
45 else
46 return TL16V_GROSS_LEN(len);
47}
48
49/* TLV generation */
50
Harald Welte57c7d372011-08-17 17:50:55 +020051/*! \brief put (append) a LV field */
Harald Welteec8b4502010-02-20 20:34:29 +010052static inline uint8_t *lv_put(uint8_t *buf, uint8_t len,
53 const uint8_t *val)
54{
55 *buf++ = len;
56 memcpy(buf, val, len);
57 return buf + len;
58}
59
Harald Welte57c7d372011-08-17 17:50:55 +020060/*! \brief put (append) a TLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010061static inline uint8_t *tlv_put(uint8_t *buf, uint8_t tag, uint8_t len,
62 const uint8_t *val)
63{
64 *buf++ = tag;
65 *buf++ = len;
66 memcpy(buf, val, len);
67 return buf + len;
68}
69
Harald Welte57c7d372011-08-17 17:50:55 +020070/*! \brief put (append) a TLV16 field */
Harald Welteec8b4502010-02-20 20:34:29 +010071static inline uint8_t *tlv16_put(uint8_t *buf, uint8_t tag, uint8_t len,
72 const uint16_t *val)
73{
74 *buf++ = tag;
75 *buf++ = len;
76 memcpy(buf, val, len*2);
77 return buf + len*2;
78}
79
Harald Welte57c7d372011-08-17 17:50:55 +020080/*! \brief put (append) a TL16V field */
Harald Welteec8b4502010-02-20 20:34:29 +010081static inline uint8_t *tl16v_put(uint8_t *buf, uint8_t tag, uint16_t len,
82 const uint8_t *val)
83{
84 *buf++ = tag;
85 *buf++ = len >> 8;
86 *buf++ = len & 0xff;
87 memcpy(buf, val, len);
88 return buf + len*2;
89}
90
Harald Welte57c7d372011-08-17 17:50:55 +020091/*! \brief put (append) a TvLV field */
Harald Welteec8b4502010-02-20 20:34:29 +010092static inline uint8_t *tvlv_put(uint8_t *buf, uint8_t tag, uint16_t len,
93 const uint8_t *val)
94{
95 uint8_t *ret;
96
97 if (len <= TVLV_MAX_ONEBYTE) {
98 ret = tlv_put(buf, tag, len, val);
99 buf[1] |= 0x80;
100 } else
101 ret = tl16v_put(buf, tag, len, val);
102
103 return ret;
104}
105
Harald Welte57c7d372011-08-17 17:50:55 +0200106/*! \brief put (append) a TLV16 field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100107static inline uint8_t *msgb_tlv16_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint16_t *val)
108{
109 uint8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
110 return tlv16_put(buf, tag, len, val);
111}
112
Harald Welte57c7d372011-08-17 17:50:55 +0200113/*! \brief put (append) a TL16V field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100114static inline uint8_t *msgb_tl16v_put(struct msgb *msg, uint8_t tag, uint16_t len,
115 const uint8_t *val)
116{
117 uint8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
118 return tl16v_put(buf, tag, len, val);
119}
120
Harald Welte57c7d372011-08-17 17:50:55 +0200121/*! \brief put (append) a TvLV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100122static inline uint8_t *msgb_tvlv_put(struct msgb *msg, uint8_t tag, uint16_t len,
123 const uint8_t *val)
124{
125 uint8_t *buf = msgb_put(msg, TVLV_GROSS_LEN(len));
126 return tvlv_put(buf, tag, len, val);
127}
128
Harald Welte57c7d372011-08-17 17:50:55 +0200129/*! \brief put (append) a L16TV field to \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100130static inline uint8_t *msgb_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
131 const uint8_t *val)
132{
133 uint8_t *buf = msgb_put(msg, L16TV_GROSS_LEN(len));
134
135 *buf++ = len >> 8;
136 *buf++ = len & 0xff;
137 *buf++ = tag;
138 memcpy(buf, val, len);
139 return buf + len;
140}
141
Harald Welte57c7d372011-08-17 17:50:55 +0200142/*! \brief put (append) a V field */
Harald Welteec8b4502010-02-20 20:34:29 +0100143static inline uint8_t *v_put(uint8_t *buf, uint8_t val)
144{
145 *buf++ = val;
146 return buf;
147}
148
Harald Welte57c7d372011-08-17 17:50:55 +0200149/*! \brief put (append) a TV field */
Harald Welteec8b4502010-02-20 20:34:29 +0100150static inline uint8_t *tv_put(uint8_t *buf, uint8_t tag,
151 uint8_t val)
152{
153 *buf++ = tag;
154 *buf++ = val;
155 return buf;
156}
157
Harald Welte57c7d372011-08-17 17:50:55 +0200158/*! \brief put (append) a TVfixed field */
Harald Welte63196de2011-03-05 14:32:50 +0100159static inline uint8_t *tv_fixed_put(uint8_t *buf, uint8_t tag,
160 unsigned int len, const uint8_t *val)
161{
162 *buf++ = tag;
163 memcpy(buf, val, len);
164 return buf + len;
165}
166
Harald Welte57c7d372011-08-17 17:50:55 +0200167/*! \brief put (append) a TV16 field
168 * \param[in,out] buf data buffer
169 * \param[in] tag Tag value
170 * \param[in] val Value (in host byte order!)
171 */
Harald Welteec8b4502010-02-20 20:34:29 +0100172static inline uint8_t *tv16_put(uint8_t *buf, uint8_t tag,
173 uint16_t val)
174{
175 *buf++ = tag;
176 *buf++ = val >> 8;
177 *buf++ = val & 0xff;
178 return buf;
179}
180
Harald Welte57c7d372011-08-17 17:50:55 +0200181/*! \brief put (append) a LV field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100182static inline uint8_t *msgb_lv_put(struct msgb *msg, uint8_t len, const uint8_t *val)
183{
184 uint8_t *buf = msgb_put(msg, LV_GROSS_LEN(len));
185 return lv_put(buf, len, val);
186}
187
Harald Welte57c7d372011-08-17 17:50:55 +0200188/*! \brief put (append) a TLV field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100189static inline uint8_t *msgb_tlv_put(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
190{
191 uint8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
192 return tlv_put(buf, tag, len, val);
193}
194
Harald Welte57c7d372011-08-17 17:50:55 +0200195/*! \brief put (append) a TV field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100196static inline uint8_t *msgb_tv_put(struct msgb *msg, uint8_t tag, uint8_t val)
197{
198 uint8_t *buf = msgb_put(msg, 2);
199 return tv_put(buf, tag, val);
200}
201
Harald Welte57c7d372011-08-17 17:50:55 +0200202/*! \brief put (append) a TVfixed field to a \ref msgb */
Harald Welte63196de2011-03-05 14:32:50 +0100203static inline uint8_t *msgb_tv_fixed_put(struct msgb *msg, uint8_t tag,
204 unsigned int len, const uint8_t *val)
205{
206 uint8_t *buf = msgb_put(msg, 1+len);
207 return tv_fixed_put(buf, tag, len, val);
208}
209
Harald Welte57c7d372011-08-17 17:50:55 +0200210/*! \brief put (append) a V field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100211static inline uint8_t *msgb_v_put(struct msgb *msg, uint8_t val)
212{
213 uint8_t *buf = msgb_put(msg, 1);
214 return v_put(buf, val);
215}
216
Harald Welte57c7d372011-08-17 17:50:55 +0200217/*! \brief put (append) a TV16 field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100218static inline uint8_t *msgb_tv16_put(struct msgb *msg, uint8_t tag, uint16_t val)
219{
220 uint8_t *buf = msgb_put(msg, 3);
221 return tv16_put(buf, tag, val);
222}
223
Harald Welte57c7d372011-08-17 17:50:55 +0200224/*! \brief push (prepend) a TLV field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100225static inline uint8_t *msgb_tlv_push(struct msgb *msg, uint8_t tag, uint8_t len, const uint8_t *val)
226{
227 uint8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
228 return tlv_put(buf, tag, len, val);
229}
230
Harald Welte57c7d372011-08-17 17:50:55 +0200231/*! \brief push (prepend) a TV field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100232static inline uint8_t *msgb_tv_push(struct msgb *msg, uint8_t tag, uint8_t val)
233{
234 uint8_t *buf = msgb_push(msg, 2);
235 return tv_put(buf, tag, val);
236}
237
Harald Welte57c7d372011-08-17 17:50:55 +0200238/*! \brief push (prepend) a TV16 field to a \ref msgb */
Harald Welteec8b4502010-02-20 20:34:29 +0100239static inline uint8_t *msgb_tv16_push(struct msgb *msg, uint8_t tag, uint16_t val)
240{
241 uint8_t *buf = msgb_push(msg, 3);
242 return tv16_put(buf, tag, val);
243}
244
Harald Welte57c7d372011-08-17 17:50:55 +0200245/*! \brief push (prepend) a TvLV field to a \ref msgb */
Harald Welte3415d412010-02-21 19:03:41 +0100246static inline uint8_t *msgb_tvlv_push(struct msgb *msg, uint8_t tag, uint16_t len,
247 const uint8_t *val)
248{
249 uint8_t *buf = msgb_push(msg, TVLV_GROSS_LEN(len));
250 return tvlv_put(buf, tag, len, val);
251}
252
Harald Welteec8b4502010-02-20 20:34:29 +0100253/* TLV parsing */
254
Harald Welte57c7d372011-08-17 17:50:55 +0200255/*! \brief Entry in a TLV parser array */
Harald Welteec8b4502010-02-20 20:34:29 +0100256struct tlv_p_entry {
Harald Welte57c7d372011-08-17 17:50:55 +0200257 uint16_t len; /*!< \brief length */
258 const uint8_t *val; /*!< \brief pointer to value */
Harald Welteec8b4502010-02-20 20:34:29 +0100259};
260
Harald Welte57c7d372011-08-17 17:50:55 +0200261/*! \brief TLV type */
Harald Welteec8b4502010-02-20 20:34:29 +0100262enum tlv_type {
Harald Welte57c7d372011-08-17 17:50:55 +0200263 TLV_TYPE_NONE, /*!< \brief no type */
264 TLV_TYPE_FIXED, /*!< \brief fixed-length value-only */
265 TLV_TYPE_T, /*!< \brief tag-only */
266 TLV_TYPE_TV, /*!< \brief tag-value (8bit) */
267 TLV_TYPE_TLV, /*!< \brief tag-length-value */
268 TLV_TYPE_TL16V, /*!< \brief tag, 16 bit length, value */
269 TLV_TYPE_TvLV, /*!< \brief tag, variable length, value */
270 TLV_TYPE_SINGLE_TV /*!< \brief tag and value (both 4 bit) in 1 byte */
Harald Welteec8b4502010-02-20 20:34:29 +0100271};
272
Harald Welte57c7d372011-08-17 17:50:55 +0200273/*! \brief Definition of a single IE (Information Element) */
Harald Welteec8b4502010-02-20 20:34:29 +0100274struct tlv_def {
Harald Welte57c7d372011-08-17 17:50:55 +0200275 enum tlv_type type; /*!< \brief TLV type */
276 uint8_t fixed_len; /*!< \brief length in case of \ref TLV_TYPE_FIXED */
Harald Welteec8b4502010-02-20 20:34:29 +0100277};
278
Harald Welte57c7d372011-08-17 17:50:55 +0200279/*! \brief Definition of All 256 IE / TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100280struct tlv_definition {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200281 struct tlv_def def[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100282};
283
Harald Welte57c7d372011-08-17 17:50:55 +0200284/*! \brief result of the TLV parser */
Harald Welteec8b4502010-02-20 20:34:29 +0100285struct tlv_parsed {
Harald Weltee0aa5bb2011-07-16 15:42:46 +0200286 struct tlv_p_entry lv[256];
Harald Welteec8b4502010-02-20 20:34:29 +0100287};
288
289extern struct tlv_definition tvlv_att_def;
290
291int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
292 const struct tlv_definition *def,
293 const uint8_t *buf, int buf_len);
294int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
295 const uint8_t *buf, int buf_len, uint8_t lv_tag, uint8_t lv_tag2);
296/* take a master (src) tlvdev and fill up all empty slots in 'dst' */
297void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src);
298
299#define TLVP_PRESENT(x, y) ((x)->lv[y].val)
300#define TLVP_LEN(x, y) (x)->lv[y].len
301#define TLVP_VAL(x, y) (x)->lv[y].val
302
Harald Welte57c7d372011-08-17 17:50:55 +0200303/*! }@ */
304
Harald Welteec8b4502010-02-20 20:34:29 +0100305#endif /* _TLV_H */