blob: ead856c9c4a037ae34a31ec702dfbd230d2d592c [file] [log] [blame]
Harald Weltee08da972017-11-13 01:00:26 +09001/* (C) 2008-2017 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
Harald Welte468b6432014-09-11 13:05:51 +08003 *
4 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Harald Welte468b6432014-09-11 13:05:51 +08008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Harald Welteec8b4502010-02-20 20:34:29 +010023#include <stdio.h>
24#include <stdint.h>
Maxdbd3a922017-01-02 14:10:30 +010025#include <errno.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/utils.h>
27#include <osmocom/gsm/tlv.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028
Harald Welte57c7d372011-08-17 17:50:55 +020029/*! \addtogroup tlv
30 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020031 * Osmocom TLV Parser
Harald Welte96e2a002017-06-12 21:44:18 +020032 *
33 * The Osmocom TLV parser is intended to operate as a low-level C
34 * implementation without dynamic memory allocations. Basically, it
35 * iterates over the IE (Information Elements) of the message and fills
36 * an array of pointers, indexed by the IEI (IE Identifier). The
37 * parser output is thus an array of pointers to the start of the
38 * respective IE inside the message.
39 *
40 * The TLV parser is configured by a TLV parser definition, which
41 * determines which if the IEIs for a given protocol are of which
42 * particular type. Types are e.g. TV (Tag + single byte value), Tag +
43 * fixed-length value, TLV with 8bit length, TLV with 16bit length, TLV
44 * with variable-length length field, etc.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020045 *
46 * \file tlv_parser.c */
Harald Welte57c7d372011-08-17 17:50:55 +020047
Harald Welteec8b4502010-02-20 20:34:29 +010048struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +020049struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +010050
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! Dump pasred TLV structure to stdout */
Harald Welteec8b4502010-02-20 20:34:29 +010052int tlv_dump(struct tlv_parsed *dec)
53{
54 int i;
55
56 for (i = 0; i <= 0xff; i++) {
57 if (!dec->lv[i].val)
58 continue;
59 printf("T=%02x L=%d\n", i, dec->lv[i].len);
60 }
61 return 0;
62}
63
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064/*! Copy \ref tlv_parsed using given talloc context
Maxdbd3a922017-01-02 14:10:30 +010065 * \param[in] tp_orig Parsed TLV structure
66 * \param[in] ctx Talloc context for allocations
67 * \returns NULL on errors, \ref tlv_parsed pointer otherwise
68 */
69struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx)
70{
71 struct tlv_parsed *tp_out;
72 size_t i, len;
73
74 tp_out = talloc_zero(ctx, struct tlv_parsed);
75 if (!tp_out)
76 return NULL;
77
78 /* if the original is NULL, return empty tlvp */
79 if (!tp_orig)
80 return tp_out;
81
82 for (i = 0; i < ARRAY_SIZE(tp_orig->lv); i++) {
83 len = tp_orig->lv[i].len;
84 tp_out->lv[i].len = len;
85 if (len && tp_out->lv[i].val) {
86 tp_out->lv[i].val = talloc_zero_size(tp_out, len);
87 if (!tp_out->lv[i].val) {
88 talloc_free(tp_out);
89 return NULL;
90 }
91 memcpy((uint8_t *)tp_out->lv[i].val, tp_orig->lv[i].val,
92 len);
93 }
94 }
95
96 return tp_out;
97}
98
Neels Hofmeyr87e45502017-06-20 00:17:59 +020099/*! Merge all \ref tlv_parsed attributes of 'src' into 'dst'
Maxdbd3a922017-01-02 14:10:30 +0100100 * \param[in] dst Parsed TLV structure to merge into
101 * \param[in] src Parsed TLV structure to merge from
102 * \returns 0 on success, negative on error
103 */
104int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src)
105{
106 size_t i, len;
107 for (i = 0; i < ARRAY_SIZE(dst->lv); i++) {
108 len = src->lv[i].len;
109 if (len == 0 || src->lv[i].val == NULL)
110 continue;
111 if (dst->lv[i].val) {
112 talloc_free((uint8_t *) dst->lv[i].val);
113 dst->lv[i].len = 0;
114 }
115 dst->lv[i].val = talloc_zero_size(dst, len);
116 if (!dst->lv[i].val)
117 return -ENOMEM;
118 memcpy((uint8_t *) dst->lv[i].val, src->lv[i].val, len);
119 }
120 return 0;
121}
122
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200123/*! Parse a single TLV encoded IE
Harald Welte57c7d372011-08-17 17:50:55 +0200124 * \param[out] o_tag the tag of the IE that was found
125 * \param[out] o_len length of the IE that was found
126 * \param[out] o_val pointer to the data of the IE that was found
127 * \param[in] def structure defining the valid TLV tags / configurations
128 * \param[in] buf the input data buffer to be parsed
129 * \param[in] buf_len length of the input data buffer
130 * \returns number of bytes consumed by the TLV entry / IE parsed
Harald Welteec8b4502010-02-20 20:34:29 +0100131 */
132int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
133 const struct tlv_definition *def,
134 const uint8_t *buf, int buf_len)
135{
136 uint8_t tag;
137 int len;
138
139 tag = *buf;
140 *o_tag = tag;
141
Andreas Eversbergcd2a74b2010-07-12 08:55:14 +0200142 /* single octet TV IE */
143 if (def->def[tag & 0xf0].type == TLV_TYPE_SINGLE_TV) {
144 *o_tag = tag & 0xf0;
145 *o_val = buf;
146 *o_len = 1;
147 return 1;
148 }
149
Neels Hofmeyr667e83d2015-11-02 20:18:11 +0100150 /* FIXME: use tables for known IEI */
Harald Welteec8b4502010-02-20 20:34:29 +0100151 switch (def->def[tag].type) {
152 case TLV_TYPE_T:
153 /* GSM TS 04.07 11.2.4: Type 1 TV or Type 2 T */
154 *o_val = buf;
155 *o_len = 0;
156 len = 1;
157 break;
158 case TLV_TYPE_TV:
159 *o_val = buf+1;
160 *o_len = 1;
161 len = 2;
162 break;
163 case TLV_TYPE_FIXED:
164 *o_val = buf+1;
165 *o_len = def->def[tag].fixed_len;
166 len = def->def[tag].fixed_len + 1;
167 break;
168 case TLV_TYPE_TLV:
Harald Welte2fe68472012-07-14 01:50:33 +0200169tlv: /* GSM TS 04.07 11.2.4: Type 4 TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100170 if (buf + 1 > buf + buf_len)
171 return -1;
172 *o_val = buf+2;
173 *o_len = *(buf+1);
174 len = *o_len + 2;
175 if (len > buf_len)
176 return -2;
177 break;
Harald Welte2fe68472012-07-14 01:50:33 +0200178 case TLV_TYPE_vTvLV_GAN: /* 44.318 / 11.1.4 */
179 /* FIXME: variable-length TAG! */
180 if (*(buf+1) & 0x80) {
181 /* like TL16Vbut without highest bit of len */
182 if (2 > buf_len)
183 return -1;
184 *o_val = buf+3;
185 *o_len = (*(buf+1) & 0x7F) << 8 | *(buf+2);
186 len = *o_len + 3;
187 if (len > buf_len)
188 return -2;
189 } else {
190 /* like TLV */
191 goto tlv;
192 }
193 break;
Harald Welteec8b4502010-02-20 20:34:29 +0100194 case TLV_TYPE_TvLV:
195 if (*(buf+1) & 0x80) {
196 /* like TLV, but without highest bit of len */
197 if (buf + 1 > buf + buf_len)
198 return -1;
199 *o_val = buf+2;
200 *o_len = *(buf+1) & 0x7f;
201 len = *o_len + 2;
202 if (len > buf_len)
203 return -2;
204 break;
205 }
206 /* like TL16V, fallthrough */
207 case TLV_TYPE_TL16V:
208 if (2 > buf_len)
209 return -1;
210 *o_val = buf+3;
211 *o_len = *(buf+1) << 8 | *(buf+2);
212 len = *o_len + 3;
213 if (len > buf_len)
214 return -2;
215 break;
216 default:
217 return -3;
218 }
219
220 return len;
221}
222
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223/*! Parse an entire buffer of TLV encoded Information Elements
Harald Welte57c7d372011-08-17 17:50:55 +0200224 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
225 * \param[in] def structure defining the valid TLV tags / configurations
226 * \param[in] buf the input data buffer to be parsed
227 * \param[in] buf_len length of the input data buffer
228 * \param[in] lv_tag an initial LV tag at the start of the buffer
229 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
230 * \returns number of bytes consumed by the TLV entry / IE parsed
Harald Welteec8b4502010-02-20 20:34:29 +0100231 */
232int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
233 const uint8_t *buf, int buf_len, uint8_t lv_tag,
234 uint8_t lv_tag2)
235{
236 int ofs = 0, num_parsed = 0;
237 uint16_t len;
238
239 memset(dec, 0, sizeof(*dec));
240
241 if (lv_tag) {
242 if (ofs > buf_len)
243 return -1;
244 dec->lv[lv_tag].val = &buf[ofs+1];
245 dec->lv[lv_tag].len = buf[ofs];
246 len = dec->lv[lv_tag].len + 1;
247 if (ofs + len > buf_len)
248 return -2;
249 num_parsed++;
250 ofs += len;
251 }
252 if (lv_tag2) {
253 if (ofs > buf_len)
254 return -1;
255 dec->lv[lv_tag2].val = &buf[ofs+1];
256 dec->lv[lv_tag2].len = buf[ofs];
257 len = dec->lv[lv_tag2].len + 1;
258 if (ofs + len > buf_len)
259 return -2;
260 num_parsed++;
261 ofs += len;
262 }
263
264 while (ofs < buf_len) {
265 int rv;
266 uint8_t tag;
267 const uint8_t *val;
268
269 rv = tlv_parse_one(&tag, &len, &val, def,
270 &buf[ofs], buf_len-ofs);
271 if (rv < 0)
272 return rv;
273 dec->lv[tag].val = val;
274 dec->lv[tag].len = len;
275 ofs += rv;
276 num_parsed++;
277 }
278 //tlv_dump(dec);
279 return num_parsed;
280}
281
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200282/*! take a master (src) tlvdev and fill up all empty slots in 'dst'
Harald Welte96e2a002017-06-12 21:44:18 +0200283 * \param dst TLV parser definition that is to be patched
284 * \param[in] src TLV parser definition whose content is patched into \a dst */
Harald Welteec8b4502010-02-20 20:34:29 +0100285void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src)
286{
287 int i;
288
289 for (i = 0; i < ARRAY_SIZE(dst->def); i++) {
290 if (src->def[i].type == TLV_TYPE_NONE)
291 continue;
292 if (dst->def[i].type == TLV_TYPE_NONE)
293 dst->def[i] = src->def[i];
294 }
295}
296
297static __attribute__((constructor)) void on_dso_load_tlv(void)
298{
299 int i;
300 for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++)
301 tvlv_att_def.def[i].type = TLV_TYPE_TvLV;
Harald Welte2fe68472012-07-14 01:50:33 +0200302
303 for (i = 0; i < ARRAY_SIZE(vtvlv_gan_att_def.def); i++)
304 vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN;
Harald Welteec8b4502010-02-20 20:34:29 +0100305}
Harald Welte57c7d372011-08-17 17:50:55 +0200306
Harald Weltefbd02fa2016-04-25 15:19:35 +0200307/*! Advance the data pointer, subtract length and assign value pointer
308 * \param data pointer to the pointer to data
309 * \param data_len pointer to size_t containing \arg data length
310 * \param[in] len the length that we expect the fixed IE to hav
311 * \param[out] value pointer to pointer of value part of IE
312 * \returns length of IE value; negative in case of error
313 */
314int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
315 size_t len, uint8_t **value)
316{
317 if (len > *data_len)
318 goto fail;
319
320 if (value)
321 *value = *data;
322
323 *data += len;
324 *data_len -= len;
325
326 return len;
327
328fail:
329 *data += *data_len;
330 *data_len = 0;
331 return -1;
332}
333
334/*! Match tag, check length and assign value pointer
335 * \param data pointer to the pointer to data
336 * \param data_len pointer to size_t containing \arg data length
337 * \param[in] tag the tag (IEI) that we expect at \arg data
338 * \param[in] len the length that we expect the fixed IE to have
339 * \param[out] value pointer to pointer of value part of IE
340 * \returns length of IE value; negative in case of error
341 */
342int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
343 uint8_t tag, size_t len,
344 uint8_t **value)
345{
346 size_t ie_len;
347
348 if (*data_len == 0)
349 goto fail;
350
351 if ((*data)[0] != tag)
352 return 0;
353
354 if (len > *data_len - 1)
355 goto fail;
356
357 if (value)
358 *value = *data + 1;
359
360 ie_len = len + 1;
361 *data += ie_len;
362 *data_len -= ie_len;
363
364 return ie_len;
365
366fail:
367 *data += *data_len;
368 *data_len = 0;
369 return -1;
370}
371
372/*! Verify TLV header and advance data / subtract length
373 * \param data pointer to the pointer to data
374 * \param data_len pointer to size_t containing \arg data length
375 * \param[in] expected_tag the tag (IEI) that we expect at \arg data
376 * \param[out] value pointer to pointer of value part of IE
377 * \param[out] value_len pointer to length of \arg value
378 * \returns length of IE value; negative in case of error
379 */
380int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
381 uint8_t expected_tag, uint8_t **value,
382 size_t *value_len)
383{
384 int rc;
385 uint8_t tag;
386 uint8_t *old_data = *data;
387 size_t old_data_len = *data_len;
388
389 rc = osmo_shift_tlv(data, data_len, &tag, value, value_len);
390
391 if (rc > 0 && tag != expected_tag) {
392 *data = old_data;
393 *data_len = old_data_len;
394 return 0;
395 }
396
397 return rc;
398}
399
400/*! Extract TLV and advance data pointer + subtract length
401 * \param data pointer to the pointer to data
402 * \param data_len pointer to size_t containing \arg data lengt
403 * \param[out] tag extract the tag (IEI) at start of \arg data
404 * \param[out] value extracted pointer to value part of TLV
405 * \param[out] value_len extracted length of \arg value
406 * \returns number of bytes subtracted
407 */
408int osmo_shift_tlv(uint8_t **data, size_t *data_len,
409 uint8_t *tag, uint8_t **value, size_t *value_len)
410{
411 size_t len;
412 size_t ie_len;
413
414 if (*data_len < 2)
415 goto fail;
416
417 len = (*data)[1];
418 if (len > *data_len - 2)
419 goto fail;
420
421 if (tag)
422 *tag = (*data)[0];
423 if (value)
424 *value = *data + 2;
425 if (value_len)
426 *value_len = len;
427
428 ie_len = len + 2;
429
430 *data += ie_len;
431 *data_len -= ie_len;
432
433 return ie_len;
434
435fail:
436 *data += *data_len;
437 *data_len = 0;
438 return -1;
439}
440
441/*! Extract LV and advance data pointer + subtract length
442 * \param data pointer to the pointer to data
443 * \param data_len pointer to size_t containing \arg data lengt
444 * \param[out] value extracted pointer to value part of TLV
445 * \param[out] value_len extracted length of \arg value
446 * \returns number of bytes subtracted
447 */
448int osmo_shift_lv(uint8_t **data, size_t *data_len,
449 uint8_t **value, size_t *value_len)
450{
451 size_t len;
452 size_t ie_len;
453
454 if (*data_len < 1)
455 goto fail;
456
457 len = (*data)[0];
458 if (len > *data_len - 1)
459 goto fail;
460
461 if (value)
462 *value = *data + 1;
463 if (value_len)
464 *value_len = len;
465
466 ie_len = len + 1;
467 *data += ie_len;
468 *data_len -= ie_len;
469
470 return ie_len;
471
472fail:
473 *data += *data_len;
474 *data_len = 0;
475 return -1;
476}
477
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200478/*! @} */