blob: 24edd0cf991728b1d4b0ace8b676338d137de76d [file] [log] [blame]
Harald Welte95109922020-12-04 13:55:38 +01001/* (C) 2008-2020 by Harald Welte <laforge@gnumonks.org>
Harald Weltee08da972017-11-13 01:00:26 +09002 * (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>
Harald Welte95109922020-12-04 13:55:38 +010027#include <osmocom/core/logging.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/gsm/tlv.h>
Harald Welteec8b4502010-02-20 20:34:29 +010029
Harald Welte57c7d372011-08-17 17:50:55 +020030/*! \addtogroup tlv
31 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032 * Osmocom TLV Parser
Harald Welte96e2a002017-06-12 21:44:18 +020033 *
34 * The Osmocom TLV parser is intended to operate as a low-level C
35 * implementation without dynamic memory allocations. Basically, it
36 * iterates over the IE (Information Elements) of the message and fills
37 * an array of pointers, indexed by the IEI (IE Identifier). The
38 * parser output is thus an array of pointers to the start of the
39 * respective IE inside the message.
40 *
41 * The TLV parser is configured by a TLV parser definition, which
42 * determines which if the IEIs for a given protocol are of which
43 * particular type. Types are e.g. TV (Tag + single byte value), Tag +
44 * fixed-length value, TLV with 8bit length, TLV with 16bit length, TLV
45 * with variable-length length field, etc.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020046 *
47 * \file tlv_parser.c */
Harald Welte57c7d372011-08-17 17:50:55 +020048
Harald Welteec8b4502010-02-20 20:34:29 +010049struct tlv_definition tvlv_att_def;
Harald Welte2fe68472012-07-14 01:50:33 +020050struct tlv_definition vtvlv_gan_att_def;
Harald Welteec8b4502010-02-20 20:34:29 +010051
Stefan Sperlingc9bebbd2018-03-16 15:59:01 +010052/*! Dump parsed TLV structure to stdout */
Harald Welteec8b4502010-02-20 20:34:29 +010053int tlv_dump(struct tlv_parsed *dec)
54{
55 int i;
56
57 for (i = 0; i <= 0xff; i++) {
58 if (!dec->lv[i].val)
59 continue;
60 printf("T=%02x L=%d\n", i, dec->lv[i].len);
61 }
62 return 0;
63}
64
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065/*! Copy \ref tlv_parsed using given talloc context
Maxdbd3a922017-01-02 14:10:30 +010066 * \param[in] tp_orig Parsed TLV structure
67 * \param[in] ctx Talloc context for allocations
68 * \returns NULL on errors, \ref tlv_parsed pointer otherwise
69 */
70struct tlv_parsed *osmo_tlvp_copy(const struct tlv_parsed *tp_orig, void *ctx)
71{
72 struct tlv_parsed *tp_out;
73 size_t i, len;
74
75 tp_out = talloc_zero(ctx, struct tlv_parsed);
76 if (!tp_out)
77 return NULL;
78
79 /* if the original is NULL, return empty tlvp */
80 if (!tp_orig)
81 return tp_out;
82
83 for (i = 0; i < ARRAY_SIZE(tp_orig->lv); i++) {
84 len = tp_orig->lv[i].len;
85 tp_out->lv[i].len = len;
86 if (len && tp_out->lv[i].val) {
87 tp_out->lv[i].val = talloc_zero_size(tp_out, len);
88 if (!tp_out->lv[i].val) {
89 talloc_free(tp_out);
90 return NULL;
91 }
92 memcpy((uint8_t *)tp_out->lv[i].val, tp_orig->lv[i].val,
93 len);
94 }
95 }
96
97 return tp_out;
98}
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100/*! Merge all \ref tlv_parsed attributes of 'src' into 'dst'
Maxdbd3a922017-01-02 14:10:30 +0100101 * \param[in] dst Parsed TLV structure to merge into
102 * \param[in] src Parsed TLV structure to merge from
103 * \returns 0 on success, negative on error
104 */
105int osmo_tlvp_merge(struct tlv_parsed *dst, const struct tlv_parsed *src)
106{
107 size_t i, len;
108 for (i = 0; i < ARRAY_SIZE(dst->lv); i++) {
109 len = src->lv[i].len;
110 if (len == 0 || src->lv[i].val == NULL)
111 continue;
112 if (dst->lv[i].val) {
113 talloc_free((uint8_t *) dst->lv[i].val);
114 dst->lv[i].len = 0;
115 }
116 dst->lv[i].val = talloc_zero_size(dst, len);
117 if (!dst->lv[i].val)
118 return -ENOMEM;
119 memcpy((uint8_t *) dst->lv[i].val, src->lv[i].val, len);
120 }
121 return 0;
122}
123
Harald Welte8006f532019-02-13 22:23:13 +0100124
125/*! Encode a single TLV into given message buffer
126 * \param[inout] msg Caller-allocated message buffer with sufficient tailroom
127 * \param[in] type TLV type/format to use during encode
128 * \param[in] tag Tag of TLV to be encoded
129 * \parma[in] len Length of TLV to be encoded
130 * \param[in] val Value part of TLV to be encoded
131 * \returns 0 on success; negative in case of error */
132int tlv_encode_one(struct msgb *msg, enum tlv_type type, uint8_t tag,
133 unsigned int len, const uint8_t *val)
134{
135 switch (type) {
136 case TLV_TYPE_NONE:
137 break;
138 case TLV_TYPE_FIXED:
139 msgb_tv_fixed_put(msg, tag, len, val);
140 break;
141 case TLV_TYPE_T:
142 msgb_v_put(msg, tag);
143 break;
144 case TLV_TYPE_TV:
145 msgb_tv_put(msg, tag, val[0]);
146 break;
147 case TLV_TYPE_TLV:
148 msgb_tlv_put(msg, tag, len, val);
149 break;
150 case TLV_TYPE_TL16V:
151 msgb_tl16v_put(msg, tag, len, val);
152 break;
153 case TLV_TYPE_TvLV:
154 msgb_tvlv_put(msg, tag, len, val);
155 break;
156 case TLV_TYPE_SINGLE_TV:
157 msgb_v_put(msg, (tag << 4) | (val[0] & 0xf));
158 break;
159 case TLV_TYPE_vTvLV_GAN:
160 msgb_vtvlv_gan_put(msg, tag, len, val);
161 break;
162 default:
163 return -EINVAL;
164 }
165 return 0;
166}
167
168/*! Encode a set of decoded TLVs according to a given definition into a message buffer
169 * \param[inout] msg Caller-allocated message buffer with sufficient tailroom
170 * \param[in] def structure defining the valid TLV tags / configurations
171 * \param[in] tp decoded values to be encoded
172 * \returns number of bytes consumed in msg; negative in case of error */
173int tlv_encode(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp)
174{
175 unsigned int tailroom_before = msgb_tailroom(msg);
176 unsigned int i;
177 int rc;
178
179 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
180 /* skip entries in the array that aren't used/filled */
181 if (!TLVP_PRESENT(tp, i))
182 continue;
183
184 rc = tlv_encode_one(msg, def->def[i].type, i, TLVP_LEN(tp, i), TLVP_VAL(tp, i));
185 if (rc < 0)
186 return rc;
187 }
188
189 return tailroom_before - msgb_tailroom(msg);
190}
191
192/*! Encode a set of decoded TLVs according to a given definition and IE order into a message buffer
193 * \param[inout] msg Caller-allocated message buffer with sufficient tailroom
194 * \param[in] def structure defining the valid TLV tags / configurations
195 * \param[in] tp decoded values to be encoded
196 * \param[in] tag_order array of tags determining the IE encoding order
197 * \param[in] tag_order_len length of tag_order
198 * \returns number of bytes consumed in msg; negative in case of error */
199int tlv_encode_ordered(struct msgb *msg, const struct tlv_definition *def, const struct tlv_parsed *tp,
200 const uint8_t *tag_order, unsigned int tag_order_len)
201{
202
203 unsigned int tailroom_before = msgb_tailroom(msg);
204 unsigned int i;
205 int rc;
206
207 for (i = 0; i < tag_order_len; i++) {
208 uint8_t tag = tag_order[i];
209
210 /* skip entries in the array that aren't used/filled */
211 if (!TLVP_PRESENT(tp, tag))
212 continue;
213
214 rc = tlv_encode_one(msg, def->def[tag].type, tag, TLVP_LEN(tp, tag), TLVP_VAL(tp, tag));
215 if (rc < 0)
216 return rc;
217 }
218 return tailroom_before - msgb_tailroom(msg);
219}
220
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200221/*! Parse a single TLV encoded IE
Harald Welte57c7d372011-08-17 17:50:55 +0200222 * \param[out] o_tag the tag of the IE that was found
223 * \param[out] o_len length of the IE that was found
224 * \param[out] o_val pointer to the data of the IE that was found
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
Stefan Sperling1e50e2a2018-01-08 19:20:02 +0100228 * \returns number of bytes consumed by the TLV entry / IE parsed; negative in case of error
Harald Welteec8b4502010-02-20 20:34:29 +0100229 */
230int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
231 const struct tlv_definition *def,
232 const uint8_t *buf, int buf_len)
233{
234 uint8_t tag;
235 int len;
236
237 tag = *buf;
238 *o_tag = tag;
239
Andreas Eversbergcd2a74b2010-07-12 08:55:14 +0200240 /* single octet TV IE */
241 if (def->def[tag & 0xf0].type == TLV_TYPE_SINGLE_TV) {
242 *o_tag = tag & 0xf0;
243 *o_val = buf;
244 *o_len = 1;
245 return 1;
246 }
247
Neels Hofmeyr667e83d2015-11-02 20:18:11 +0100248 /* FIXME: use tables for known IEI */
Harald Welteec8b4502010-02-20 20:34:29 +0100249 switch (def->def[tag].type) {
250 case TLV_TYPE_T:
251 /* GSM TS 04.07 11.2.4: Type 1 TV or Type 2 T */
252 *o_val = buf;
253 *o_len = 0;
254 len = 1;
255 break;
256 case TLV_TYPE_TV:
257 *o_val = buf+1;
258 *o_len = 1;
259 len = 2;
260 break;
261 case TLV_TYPE_FIXED:
262 *o_val = buf+1;
263 *o_len = def->def[tag].fixed_len;
264 len = def->def[tag].fixed_len + 1;
265 break;
266 case TLV_TYPE_TLV:
Harald Welte2fe68472012-07-14 01:50:33 +0200267tlv: /* GSM TS 04.07 11.2.4: Type 4 TLV */
Harald Welteec8b4502010-02-20 20:34:29 +0100268 if (buf + 1 > buf + buf_len)
269 return -1;
270 *o_val = buf+2;
271 *o_len = *(buf+1);
272 len = *o_len + 2;
273 if (len > buf_len)
274 return -2;
275 break;
Harald Welte2fe68472012-07-14 01:50:33 +0200276 case TLV_TYPE_vTvLV_GAN: /* 44.318 / 11.1.4 */
277 /* FIXME: variable-length TAG! */
278 if (*(buf+1) & 0x80) {
279 /* like TL16Vbut without highest bit of len */
280 if (2 > buf_len)
281 return -1;
282 *o_val = buf+3;
283 *o_len = (*(buf+1) & 0x7F) << 8 | *(buf+2);
284 len = *o_len + 3;
285 if (len > buf_len)
286 return -2;
287 } else {
288 /* like TLV */
289 goto tlv;
290 }
291 break;
Harald Welteec8b4502010-02-20 20:34:29 +0100292 case TLV_TYPE_TvLV:
293 if (*(buf+1) & 0x80) {
294 /* like TLV, but without highest bit of len */
295 if (buf + 1 > buf + buf_len)
296 return -1;
297 *o_val = buf+2;
298 *o_len = *(buf+1) & 0x7f;
299 len = *o_len + 2;
300 if (len > buf_len)
301 return -2;
302 break;
303 }
304 /* like TL16V, fallthrough */
305 case TLV_TYPE_TL16V:
306 if (2 > buf_len)
307 return -1;
308 *o_val = buf+3;
309 *o_len = *(buf+1) << 8 | *(buf+2);
310 len = *o_len + 3;
311 if (len > buf_len)
312 return -2;
313 break;
314 default:
315 return -3;
316 }
317
318 return len;
319}
320
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200321/*! Parse an entire buffer of TLV encoded Information Elements.
322 * In case of multiple occurences of an IE, keep only the first occurence.
323 * Most GSM related protocols clearly indicate that in case of duplicate
324 * IEs, only the first occurrence shall be used, while any further occurrences
325 * shall be ignored. See e.g. 3GPP TS 24.008 Section 8.6.3.
326 * For multiple occurences, use tlv_parse2().
Harald Welte57c7d372011-08-17 17:50:55 +0200327 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
328 * \param[in] def structure defining the valid TLV tags / configurations
329 * \param[in] buf the input data buffer to be parsed
330 * \param[in] buf_len length of the input data buffer
331 * \param[in] lv_tag an initial LV tag at the start of the buffer
332 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
Stefan Sperlingc9bebbd2018-03-16 15:59:01 +0100333 * \returns number of TLV entries parsed; negative in case of error
Harald Welteec8b4502010-02-20 20:34:29 +0100334 */
335int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
336 const uint8_t *buf, int buf_len, uint8_t lv_tag,
337 uint8_t lv_tag2)
338{
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200339 return tlv_parse2(dec, 1, def, buf, buf_len, lv_tag, lv_tag2);
340}
341
342/*! Like tlv_parse(), but capable of decoding multiple occurences of the same IE.
343 * Parse an entire buffer of TLV encoded Information Elements.
344 * To decode multiple occurences of IEs, provide in dec an _array_ of tlv_parsed, and
345 * pass the size of that array in dec_multiples. The first occurence of each IE
346 * is stored in dec[0], the second in dec[1] and so forth. If there are more
347 * occurences than the array length given in dec_multiples, the remaining
348 * occurences are dropped.
349 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
350 * \param[in] dec_multiples length of the tlv_parsed[] in \a dec.
351 * \param[in] def structure defining the valid TLV tags / configurations
352 * \param[in] buf the input data buffer to be parsed
353 * \param[in] buf_len length of the input data buffer
354 * \param[in] lv_tag an initial LV tag at the start of the buffer
355 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
356 * \returns number of TLV entries parsed; negative in case of error
357 */
358int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
359 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
360 uint8_t lv_tag, uint8_t lv_tag2)
361{
Harald Welteec8b4502010-02-20 20:34:29 +0100362 int ofs = 0, num_parsed = 0;
363 uint16_t len;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200364 int dec_i;
Harald Welteec8b4502010-02-20 20:34:29 +0100365
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200366 for (dec_i = 0; dec_i < dec_multiples; dec_i++)
367 memset(&dec[dec_i], 0, sizeof(*dec));
Harald Welteec8b4502010-02-20 20:34:29 +0100368
369 if (lv_tag) {
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200370 const uint8_t *val;
371 uint16_t parsed_len;
Harald Welteec8b4502010-02-20 20:34:29 +0100372 if (ofs > buf_len)
373 return -1;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200374 val = &buf[ofs+1];
375 len = buf[ofs];
376 parsed_len = len + 1;
377 if (ofs + parsed_len > buf_len)
Harald Welteec8b4502010-02-20 20:34:29 +0100378 return -2;
379 num_parsed++;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200380 ofs += parsed_len;
381 /* store the resulting val and len */
382 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
383 if (dec[dec_i].lv[lv_tag].val != NULL)
384 continue;
385 dec->lv[lv_tag].val = val;
386 dec->lv[lv_tag].len = len;
387 break;
388 }
Harald Welteec8b4502010-02-20 20:34:29 +0100389 }
390 if (lv_tag2) {
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200391 const uint8_t *val;
392 uint16_t parsed_len;
Harald Welteec8b4502010-02-20 20:34:29 +0100393 if (ofs > buf_len)
394 return -1;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200395 val = &buf[ofs+1];
396 len = buf[ofs];
397 parsed_len = len + 1;
398 if (ofs + parsed_len > buf_len)
Harald Welteec8b4502010-02-20 20:34:29 +0100399 return -2;
400 num_parsed++;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200401 ofs += parsed_len;
402 /* store the resulting val and len */
403 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
404 if (dec[dec_i].lv[lv_tag2].val != NULL)
405 continue;
406 dec->lv[lv_tag2].val = val;
407 dec->lv[lv_tag2].len = len;
408 break;
409 }
Harald Welteec8b4502010-02-20 20:34:29 +0100410 }
411
412 while (ofs < buf_len) {
413 int rv;
414 uint8_t tag;
415 const uint8_t *val;
416
417 rv = tlv_parse_one(&tag, &len, &val, def,
418 &buf[ofs], buf_len-ofs);
419 if (rv < 0)
420 return rv;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200421 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
422 if (dec[dec_i].lv[tag].val != NULL)
423 continue;
424 dec[dec_i].lv[tag].val = val;
425 dec[dec_i].lv[tag].len = len;
426 break;
Harald Weltebf383a12018-02-02 12:11:14 +0100427 }
Harald Welteec8b4502010-02-20 20:34:29 +0100428 ofs += rv;
429 num_parsed++;
430 }
431 //tlv_dump(dec);
432 return num_parsed;
433}
434
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200435/*! take a master (src) tlvdev and fill up all empty slots in 'dst'
Harald Welte96e2a002017-06-12 21:44:18 +0200436 * \param dst TLV parser definition that is to be patched
437 * \param[in] src TLV parser definition whose content is patched into \a dst */
Harald Welteec8b4502010-02-20 20:34:29 +0100438void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src)
439{
440 int i;
441
442 for (i = 0; i < ARRAY_SIZE(dst->def); i++) {
443 if (src->def[i].type == TLV_TYPE_NONE)
444 continue;
445 if (dst->def[i].type == TLV_TYPE_NONE)
446 dst->def[i] = src->def[i];
447 }
448}
449
450static __attribute__((constructor)) void on_dso_load_tlv(void)
451{
452 int i;
453 for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++)
454 tvlv_att_def.def[i].type = TLV_TYPE_TvLV;
Harald Welte2fe68472012-07-14 01:50:33 +0200455
456 for (i = 0; i < ARRAY_SIZE(vtvlv_gan_att_def.def); i++)
457 vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN;
Harald Welteec8b4502010-02-20 20:34:29 +0100458}
Harald Welte57c7d372011-08-17 17:50:55 +0200459
Harald Weltefbd02fa2016-04-25 15:19:35 +0200460/*! Advance the data pointer, subtract length and assign value pointer
461 * \param data pointer to the pointer to data
462 * \param data_len pointer to size_t containing \arg data length
463 * \param[in] len the length that we expect the fixed IE to hav
464 * \param[out] value pointer to pointer of value part of IE
465 * \returns length of IE value; negative in case of error
466 */
467int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
468 size_t len, uint8_t **value)
469{
470 if (len > *data_len)
471 goto fail;
472
473 if (value)
474 *value = *data;
475
476 *data += len;
477 *data_len -= len;
478
479 return len;
480
481fail:
482 *data += *data_len;
483 *data_len = 0;
484 return -1;
485}
486
487/*! Match tag, check length and assign value pointer
488 * \param data pointer to the pointer to data
489 * \param data_len pointer to size_t containing \arg data length
490 * \param[in] tag the tag (IEI) that we expect at \arg data
491 * \param[in] len the length that we expect the fixed IE to have
492 * \param[out] value pointer to pointer of value part of IE
493 * \returns length of IE value; negative in case of error
494 */
495int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
496 uint8_t tag, size_t len,
497 uint8_t **value)
498{
499 size_t ie_len;
500
501 if (*data_len == 0)
502 goto fail;
503
504 if ((*data)[0] != tag)
505 return 0;
506
507 if (len > *data_len - 1)
508 goto fail;
509
510 if (value)
511 *value = *data + 1;
512
513 ie_len = len + 1;
514 *data += ie_len;
515 *data_len -= ie_len;
516
517 return ie_len;
518
519fail:
520 *data += *data_len;
521 *data_len = 0;
522 return -1;
523}
524
525/*! Verify TLV header and advance data / subtract length
526 * \param data pointer to the pointer to data
527 * \param data_len pointer to size_t containing \arg data length
528 * \param[in] expected_tag the tag (IEI) that we expect at \arg data
529 * \param[out] value pointer to pointer of value part of IE
530 * \param[out] value_len pointer to length of \arg value
531 * \returns length of IE value; negative in case of error
532 */
533int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
534 uint8_t expected_tag, uint8_t **value,
535 size_t *value_len)
536{
537 int rc;
538 uint8_t tag;
539 uint8_t *old_data = *data;
540 size_t old_data_len = *data_len;
541
542 rc = osmo_shift_tlv(data, data_len, &tag, value, value_len);
543
544 if (rc > 0 && tag != expected_tag) {
545 *data = old_data;
546 *data_len = old_data_len;
547 return 0;
548 }
549
550 return rc;
551}
552
553/*! Extract TLV and advance data pointer + subtract length
554 * \param data pointer to the pointer to data
555 * \param data_len pointer to size_t containing \arg data lengt
556 * \param[out] tag extract the tag (IEI) at start of \arg data
557 * \param[out] value extracted pointer to value part of TLV
558 * \param[out] value_len extracted length of \arg value
559 * \returns number of bytes subtracted
560 */
561int osmo_shift_tlv(uint8_t **data, size_t *data_len,
562 uint8_t *tag, uint8_t **value, size_t *value_len)
563{
564 size_t len;
565 size_t ie_len;
566
567 if (*data_len < 2)
568 goto fail;
569
570 len = (*data)[1];
571 if (len > *data_len - 2)
572 goto fail;
573
574 if (tag)
575 *tag = (*data)[0];
576 if (value)
577 *value = *data + 2;
578 if (value_len)
579 *value_len = len;
580
581 ie_len = len + 2;
582
583 *data += ie_len;
584 *data_len -= ie_len;
585
586 return ie_len;
587
588fail:
589 *data += *data_len;
590 *data_len = 0;
591 return -1;
592}
593
594/*! Extract LV and advance data pointer + subtract length
595 * \param data pointer to the pointer to data
596 * \param data_len pointer to size_t containing \arg data lengt
597 * \param[out] value extracted pointer to value part of TLV
598 * \param[out] value_len extracted length of \arg value
599 * \returns number of bytes subtracted
600 */
601int osmo_shift_lv(uint8_t **data, size_t *data_len,
602 uint8_t **value, size_t *value_len)
603{
604 size_t len;
605 size_t ie_len;
606
607 if (*data_len < 1)
608 goto fail;
609
610 len = (*data)[0];
611 if (len > *data_len - 1)
612 goto fail;
613
614 if (value)
615 *value = *data + 1;
616 if (value_len)
617 *value_len = len;
618
619 ie_len = len + 1;
620 *data += ie_len;
621 *data_len -= ie_len;
622
623 return ie_len;
624
625fail:
626 *data += *data_len;
627 *data_len = 0;
628 return -1;
629}
630
Harald Welte95109922020-12-04 13:55:38 +0100631static __thread char ienamebuf[32];
632static __thread char msgnamebuf[32];
633
634/*! get the message name for given msg_type in protocol pdef */
635const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
636{
637 if (pdef->msg_def[msg_type].name) {
638 return pdef->msg_def[msg_type].name;
639 } else if (pdef->msgt_names) {
640 return get_value_string(pdef->msgt_names, msg_type);
641 } else {
642 snprintf(msgnamebuf, sizeof(msgnamebuf), "Unknown msg_type 0x%02x", msg_type);
643 return msgnamebuf;
644 }
645}
646
647/*! get the IE name for given IEI in protocol pdef */
648const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei)
649{
650 if (pdef->ie_def[iei].name) {
651 return pdef->ie_def[iei].name;
652 } else {
653 snprintf(ienamebuf, sizeof(ienamebuf), "Unknown IEI 0x%02x", iei);
654 return ienamebuf;
655 }
656}
657
658/*! Validate an already TLV-decoded message against the protocol definition.
659 * \param[in] pdef protocol definition of given protocol
660 * \param[in] msg_type message type of the parsed message
661 * \param[in] tp TLV parser result
662 * \param[in] log_subsys logging sub-system for log messages
663 * \param[in] log_pfx prefix for log messages
664 * \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
665 */
666int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
667 const struct tlv_parsed *tp, int log_subsys, const char *log_pfx)
668{
669 const struct osmo_tlv_prot_msg_def *msg_def= &pdef->msg_def[msg_type];
670 unsigned int num_err = 0;
671 unsigned int i;
672
673 if (msg_def->mand_ies) {
674 for (i = 0; i < msg_def->mand_count; i++) {
675 uint8_t iei = msg_def->mand_ies[i];
676 if (!TLVP_PRESENT(tp, iei)) {
677 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Missing Mandatory IE: %s\n",
678 log_pfx, pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
679 osmo_tlv_prot_ie_name(pdef, iei));
680 num_err++;
681 }
682 }
683 }
684
685 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
686 uint16_t min_len;
687
688 if (!TLVP_PRESENT(tp, i))
689 continue;
690
691 min_len = pdef->ie_def[i].min_len;
692 if (TLVP_LEN(tp, i) < min_len) {
693 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Short IE %s: %u < %u\n", log_pfx,
694 pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
695 osmo_tlv_prot_ie_name(pdef, i), TLVP_LEN(tp, i), min_len);
696 num_err++;
697 }
698 }
699
700 return -num_err;
701}
702
703/*! Parse + Validate a TLV-encoded message against the protocol definition.
704 * \param[in] pdef protocol definition of given protocol
705 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
706 * \param[in] dec_multiples length of the tlv_parsed[] in \a dec.
707 * \param[in] msg_type message type of the parsed message
708 * \param[in] buf the input data buffer to be parsed
709 * \param[in] buf_len length of the input data buffer
710 * \param[in] lv_tag an initial LV tag at the start of the buffer
711 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
712 * \param[in] log_subsys logging sub-system for log messages
713 * \param[in] log_pfx prefix for log messages
714 * \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
715 */
716int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
717 struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
718 const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
719 int log_subsys, const char *log_pfx)
720{
721 int rc;
722
723 rc = tlv_parse2(dec, dec_multiples, pdef->tlv_def, buf, buf_len, lv_tag, lv_tag2);
724 if (rc < 0) {
725 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: TLV parser error %d\n", log_pfx,
726 pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type), rc);
727 return rc;
728 }
729
730 return osmo_tlv_prot_validate_tp(pdef, msg_type, dec, log_subsys, log_pfx);
731}
732
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200733/*! @} */