blob: e47b94f1117490f4fc2c23acb3d0658676e8f0b0 [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
Vadim Yanitskiy64277a02023-02-28 03:30:27 +0700129 * \param[in] len Length of TLV to be encoded
Harald Welte8006f532019-02-13 22:23:13 +0100130 * \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;
Harald Welteefdd6412021-01-12 18:07:18 +0100235 int len; /* number of bytes consumed by TLV entry */
236
237 if (buf_len < 1)
238 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100239
240 tag = *buf;
241 *o_tag = tag;
242
Andreas Eversbergcd2a74b2010-07-12 08:55:14 +0200243 /* single octet TV IE */
Pau Espin Pedrol559a6ee2023-03-22 13:17:09 +0100244 if (def->def[tag >> 4].type == TLV_TYPE_SINGLE_TV
245 /* backward compat for old IEs with half-octet tag defined as 0xN0: */
246 || ((tag > 0x0f) && (def->def[tag & 0xf0].type == TLV_TYPE_SINGLE_TV))) {
Andreas Eversbergcd2a74b2010-07-12 08:55:14 +0200247 *o_tag = tag & 0xf0;
248 *o_val = buf;
249 *o_len = 1;
250 return 1;
251 }
252
Neels Hofmeyr667e83d2015-11-02 20:18:11 +0100253 /* FIXME: use tables for known IEI */
Harald Welteec8b4502010-02-20 20:34:29 +0100254 switch (def->def[tag].type) {
255 case TLV_TYPE_T:
256 /* GSM TS 04.07 11.2.4: Type 1 TV or Type 2 T */
257 *o_val = buf;
258 *o_len = 0;
259 len = 1;
260 break;
261 case TLV_TYPE_TV:
262 *o_val = buf+1;
263 *o_len = 1;
264 len = 2;
265 break;
266 case TLV_TYPE_FIXED:
267 *o_val = buf+1;
268 *o_len = def->def[tag].fixed_len;
269 len = def->def[tag].fixed_len + 1;
270 break;
271 case TLV_TYPE_TLV:
Harald Welte2fe68472012-07-14 01:50:33 +0200272tlv: /* GSM TS 04.07 11.2.4: Type 4 TLV */
Harald Welteefdd6412021-01-12 18:07:18 +0100273 if (buf_len < 2)
Harald Welte30a92942020-12-04 14:09:22 +0100274 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100275 *o_val = buf+2;
276 *o_len = *(buf+1);
277 len = *o_len + 2;
Harald Welteec8b4502010-02-20 20:34:29 +0100278 break;
Harald Welte2fe68472012-07-14 01:50:33 +0200279 case TLV_TYPE_vTvLV_GAN: /* 44.318 / 11.1.4 */
280 /* FIXME: variable-length TAG! */
Harald Welteefdd6412021-01-12 18:07:18 +0100281 if (buf_len < 2)
282 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welte2fe68472012-07-14 01:50:33 +0200283 if (*(buf+1) & 0x80) {
Harald Welteefdd6412021-01-12 18:07:18 +0100284 if (buf_len < 3)
Harald Welte30a92942020-12-04 14:09:22 +0100285 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welteefdd6412021-01-12 18:07:18 +0100286 /* like TL16Vbut without highest bit of len */
Harald Welte2fe68472012-07-14 01:50:33 +0200287 *o_val = buf+3;
288 *o_len = (*(buf+1) & 0x7F) << 8 | *(buf+2);
289 len = *o_len + 3;
Harald Welte2fe68472012-07-14 01:50:33 +0200290 } else {
291 /* like TLV */
292 goto tlv;
293 }
294 break;
Harald Welteec8b4502010-02-20 20:34:29 +0100295 case TLV_TYPE_TvLV:
Harald Welteefdd6412021-01-12 18:07:18 +0100296 if (buf_len < 2)
297 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100298 if (*(buf+1) & 0x80) {
299 /* like TLV, but without highest bit of len */
Harald Welteec8b4502010-02-20 20:34:29 +0100300 *o_val = buf+2;
301 *o_len = *(buf+1) & 0x7f;
302 len = *o_len + 2;
Harald Welteec8b4502010-02-20 20:34:29 +0100303 break;
304 }
305 /* like TL16V, fallthrough */
306 case TLV_TYPE_TL16V:
Harald Welteefdd6412021-01-12 18:07:18 +0100307 if (buf_len < 3)
Harald Welte30a92942020-12-04 14:09:22 +0100308 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100309 *o_val = buf+3;
310 *o_len = *(buf+1) << 8 | *(buf+2);
311 len = *o_len + 3;
Harald Welteec8b4502010-02-20 20:34:29 +0100312 break;
313 default:
Harald Welte30a92942020-12-04 14:09:22 +0100314 return OSMO_TLVP_ERR_UNKNOWN_TLV_TYPE;
Harald Welteec8b4502010-02-20 20:34:29 +0100315 }
316
Harald Welteefdd6412021-01-12 18:07:18 +0100317 if (buf_len < len) {
318 *o_val = NULL;
319 return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
320 }
Harald Welteec8b4502010-02-20 20:34:29 +0100321 return len;
322}
323
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200324/*! Parse an entire buffer of TLV encoded Information Elements.
325 * In case of multiple occurences of an IE, keep only the first occurence.
326 * Most GSM related protocols clearly indicate that in case of duplicate
327 * IEs, only the first occurrence shall be used, while any further occurrences
328 * shall be ignored. See e.g. 3GPP TS 24.008 Section 8.6.3.
329 * For multiple occurences, use tlv_parse2().
Harald Welte57c7d372011-08-17 17:50:55 +0200330 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
331 * \param[in] def structure defining the valid TLV tags / configurations
332 * \param[in] buf the input data buffer to be parsed
333 * \param[in] buf_len length of the input data buffer
334 * \param[in] lv_tag an initial LV tag at the start of the buffer
335 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
Stefan Sperlingc9bebbd2018-03-16 15:59:01 +0100336 * \returns number of TLV entries parsed; negative in case of error
Harald Welteec8b4502010-02-20 20:34:29 +0100337 */
338int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
339 const uint8_t *buf, int buf_len, uint8_t lv_tag,
340 uint8_t lv_tag2)
341{
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200342 return tlv_parse2(dec, 1, def, buf, buf_len, lv_tag, lv_tag2);
343}
344
345/*! Like tlv_parse(), but capable of decoding multiple occurences of the same IE.
346 * Parse an entire buffer of TLV encoded Information Elements.
347 * To decode multiple occurences of IEs, provide in dec an _array_ of tlv_parsed, and
348 * pass the size of that array in dec_multiples. The first occurence of each IE
349 * is stored in dec[0], the second in dec[1] and so forth. If there are more
350 * occurences than the array length given in dec_multiples, the remaining
351 * occurences are dropped.
352 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
353 * \param[in] dec_multiples length of the tlv_parsed[] in \a dec.
354 * \param[in] def structure defining the valid TLV tags / configurations
355 * \param[in] buf the input data buffer to be parsed
356 * \param[in] buf_len length of the input data buffer
357 * \param[in] lv_tag an initial LV tag at the start of the buffer
358 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
359 * \returns number of TLV entries parsed; negative in case of error
360 */
361int tlv_parse2(struct tlv_parsed *dec, int dec_multiples,
362 const struct tlv_definition *def, const uint8_t *buf, int buf_len,
363 uint8_t lv_tag, uint8_t lv_tag2)
364{
Harald Welteec8b4502010-02-20 20:34:29 +0100365 int ofs = 0, num_parsed = 0;
366 uint16_t len;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200367 int dec_i;
Harald Welteec8b4502010-02-20 20:34:29 +0100368
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200369 for (dec_i = 0; dec_i < dec_multiples; dec_i++)
370 memset(&dec[dec_i], 0, sizeof(*dec));
Harald Welteec8b4502010-02-20 20:34:29 +0100371
372 if (lv_tag) {
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200373 const uint8_t *val;
374 uint16_t parsed_len;
Harald Welteec8b4502010-02-20 20:34:29 +0100375 if (ofs > buf_len)
Harald Welte30a92942020-12-04 14:09:22 +0100376 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200377 val = &buf[ofs+1];
378 len = buf[ofs];
379 parsed_len = len + 1;
380 if (ofs + parsed_len > buf_len)
Harald Welte30a92942020-12-04 14:09:22 +0100381 return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100382 num_parsed++;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200383 ofs += parsed_len;
384 /* store the resulting val and len */
385 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
386 if (dec[dec_i].lv[lv_tag].val != NULL)
387 continue;
388 dec->lv[lv_tag].val = val;
389 dec->lv[lv_tag].len = len;
390 break;
391 }
Harald Welteec8b4502010-02-20 20:34:29 +0100392 }
393 if (lv_tag2) {
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200394 const uint8_t *val;
395 uint16_t parsed_len;
Harald Welteec8b4502010-02-20 20:34:29 +0100396 if (ofs > buf_len)
Harald Welte30a92942020-12-04 14:09:22 +0100397 return OSMO_TLVP_ERR_OFS_BEYOND_BUFFER;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200398 val = &buf[ofs+1];
399 len = buf[ofs];
400 parsed_len = len + 1;
401 if (ofs + parsed_len > buf_len)
Harald Welte30a92942020-12-04 14:09:22 +0100402 return OSMO_TLVP_ERR_OFS_LEN_BEYOND_BUFFER;
Harald Welteec8b4502010-02-20 20:34:29 +0100403 num_parsed++;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200404 ofs += parsed_len;
405 /* store the resulting val and len */
406 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
407 if (dec[dec_i].lv[lv_tag2].val != NULL)
408 continue;
409 dec->lv[lv_tag2].val = val;
410 dec->lv[lv_tag2].len = len;
411 break;
412 }
Harald Welteec8b4502010-02-20 20:34:29 +0100413 }
414
415 while (ofs < buf_len) {
416 int rv;
417 uint8_t tag;
418 const uint8_t *val;
419
420 rv = tlv_parse_one(&tag, &len, &val, def,
421 &buf[ofs], buf_len-ofs);
422 if (rv < 0)
423 return rv;
Neels Hofmeyra78b22b2018-04-13 03:36:49 +0200424 for (dec_i = 0; dec_i < dec_multiples; dec_i++) {
425 if (dec[dec_i].lv[tag].val != NULL)
426 continue;
427 dec[dec_i].lv[tag].val = val;
428 dec[dec_i].lv[tag].len = len;
429 break;
Harald Weltebf383a12018-02-02 12:11:14 +0100430 }
Harald Welteec8b4502010-02-20 20:34:29 +0100431 ofs += rv;
432 num_parsed++;
433 }
434 //tlv_dump(dec);
435 return num_parsed;
436}
437
Pau Espin Pedrold1105292021-04-14 17:21:02 +0200438/*! take a master (src) tlv_definition and fill up all empty slots in 'dst'
Harald Welte96e2a002017-06-12 21:44:18 +0200439 * \param dst TLV parser definition that is to be patched
440 * \param[in] src TLV parser definition whose content is patched into \a dst */
Harald Welteec8b4502010-02-20 20:34:29 +0100441void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src)
442{
443 int i;
444
445 for (i = 0; i < ARRAY_SIZE(dst->def); i++) {
446 if (src->def[i].type == TLV_TYPE_NONE)
447 continue;
448 if (dst->def[i].type == TLV_TYPE_NONE)
449 dst->def[i] = src->def[i];
450 }
451}
452
453static __attribute__((constructor)) void on_dso_load_tlv(void)
454{
455 int i;
456 for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++)
457 tvlv_att_def.def[i].type = TLV_TYPE_TvLV;
Harald Welte2fe68472012-07-14 01:50:33 +0200458
459 for (i = 0; i < ARRAY_SIZE(vtvlv_gan_att_def.def); i++)
460 vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN;
Harald Welteec8b4502010-02-20 20:34:29 +0100461}
Harald Welte57c7d372011-08-17 17:50:55 +0200462
Harald Weltefbd02fa2016-04-25 15:19:35 +0200463/*! Advance the data pointer, subtract length and assign value pointer
464 * \param data pointer to the pointer to data
465 * \param data_len pointer to size_t containing \arg data length
466 * \param[in] len the length that we expect the fixed IE to hav
467 * \param[out] value pointer to pointer of value part of IE
468 * \returns length of IE value; negative in case of error
469 */
470int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
471 size_t len, uint8_t **value)
472{
473 if (len > *data_len)
474 goto fail;
475
476 if (value)
477 *value = *data;
478
479 *data += len;
480 *data_len -= len;
481
482 return len;
483
484fail:
485 *data += *data_len;
486 *data_len = 0;
487 return -1;
488}
489
490/*! Match tag, check length and assign value pointer
491 * \param data pointer to the pointer to data
492 * \param data_len pointer to size_t containing \arg data length
493 * \param[in] tag the tag (IEI) that we expect at \arg data
494 * \param[in] len the length that we expect the fixed IE to have
495 * \param[out] value pointer to pointer of value part of IE
496 * \returns length of IE value; negative in case of error
497 */
498int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
499 uint8_t tag, size_t len,
500 uint8_t **value)
501{
502 size_t ie_len;
503
504 if (*data_len == 0)
505 goto fail;
506
507 if ((*data)[0] != tag)
508 return 0;
509
510 if (len > *data_len - 1)
511 goto fail;
512
513 if (value)
514 *value = *data + 1;
515
516 ie_len = len + 1;
517 *data += ie_len;
518 *data_len -= ie_len;
519
520 return ie_len;
521
522fail:
523 *data += *data_len;
524 *data_len = 0;
525 return -1;
526}
527
528/*! Verify TLV header and advance data / subtract length
529 * \param data pointer to the pointer to data
530 * \param data_len pointer to size_t containing \arg data length
531 * \param[in] expected_tag the tag (IEI) that we expect at \arg data
532 * \param[out] value pointer to pointer of value part of IE
533 * \param[out] value_len pointer to length of \arg value
534 * \returns length of IE value; negative in case of error
535 */
536int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
537 uint8_t expected_tag, uint8_t **value,
538 size_t *value_len)
539{
540 int rc;
541 uint8_t tag;
542 uint8_t *old_data = *data;
543 size_t old_data_len = *data_len;
544
545 rc = osmo_shift_tlv(data, data_len, &tag, value, value_len);
546
547 if (rc > 0 && tag != expected_tag) {
548 *data = old_data;
549 *data_len = old_data_len;
550 return 0;
551 }
552
553 return rc;
554}
555
556/*! Extract TLV and advance data pointer + subtract length
557 * \param data pointer to the pointer to data
558 * \param data_len pointer to size_t containing \arg data lengt
559 * \param[out] tag extract the tag (IEI) at start of \arg data
560 * \param[out] value extracted pointer to value part of TLV
561 * \param[out] value_len extracted length of \arg value
562 * \returns number of bytes subtracted
563 */
564int osmo_shift_tlv(uint8_t **data, size_t *data_len,
565 uint8_t *tag, uint8_t **value, size_t *value_len)
566{
567 size_t len;
568 size_t ie_len;
569
570 if (*data_len < 2)
571 goto fail;
572
573 len = (*data)[1];
574 if (len > *data_len - 2)
575 goto fail;
576
577 if (tag)
578 *tag = (*data)[0];
579 if (value)
580 *value = *data + 2;
581 if (value_len)
582 *value_len = len;
583
584 ie_len = len + 2;
585
586 *data += ie_len;
587 *data_len -= ie_len;
588
589 return ie_len;
590
591fail:
592 *data += *data_len;
593 *data_len = 0;
594 return -1;
595}
596
597/*! Extract LV and advance data pointer + subtract length
598 * \param data pointer to the pointer to data
599 * \param data_len pointer to size_t containing \arg data lengt
600 * \param[out] value extracted pointer to value part of TLV
601 * \param[out] value_len extracted length of \arg value
602 * \returns number of bytes subtracted
603 */
604int osmo_shift_lv(uint8_t **data, size_t *data_len,
605 uint8_t **value, size_t *value_len)
606{
607 size_t len;
608 size_t ie_len;
609
610 if (*data_len < 1)
611 goto fail;
612
613 len = (*data)[0];
614 if (len > *data_len - 1)
615 goto fail;
616
617 if (value)
618 *value = *data + 1;
619 if (value_len)
620 *value_len = len;
621
622 ie_len = len + 1;
623 *data += ie_len;
624 *data_len -= ie_len;
625
626 return ie_len;
627
628fail:
629 *data += *data_len;
630 *data_len = 0;
631 return -1;
632}
633
Harald Welte95109922020-12-04 13:55:38 +0100634static __thread char ienamebuf[32];
635static __thread char msgnamebuf[32];
636
637/*! get the message name for given msg_type in protocol pdef */
638const char *osmo_tlv_prot_msg_name(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type)
639{
640 if (pdef->msg_def[msg_type].name) {
641 return pdef->msg_def[msg_type].name;
642 } else if (pdef->msgt_names) {
643 return get_value_string(pdef->msgt_names, msg_type);
644 } else {
645 snprintf(msgnamebuf, sizeof(msgnamebuf), "Unknown msg_type 0x%02x", msg_type);
646 return msgnamebuf;
647 }
648}
649
650/*! get the IE name for given IEI in protocol pdef */
651const char *osmo_tlv_prot_ie_name(const struct osmo_tlv_prot_def *pdef, uint8_t iei)
652{
653 if (pdef->ie_def[iei].name) {
654 return pdef->ie_def[iei].name;
655 } else {
656 snprintf(ienamebuf, sizeof(ienamebuf), "Unknown IEI 0x%02x", iei);
657 return ienamebuf;
658 }
659}
660
661/*! Validate an already TLV-decoded message against the protocol definition.
662 * \param[in] pdef protocol definition of given protocol
663 * \param[in] msg_type message type of the parsed message
664 * \param[in] tp TLV parser result
665 * \param[in] log_subsys logging sub-system for log messages
666 * \param[in] log_pfx prefix for log messages
667 * \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
668 */
669int osmo_tlv_prot_validate_tp(const struct osmo_tlv_prot_def *pdef, uint8_t msg_type,
670 const struct tlv_parsed *tp, int log_subsys, const char *log_pfx)
671{
672 const struct osmo_tlv_prot_msg_def *msg_def= &pdef->msg_def[msg_type];
Harald Welte30a92942020-12-04 14:09:22 +0100673 unsigned int err = 0;
Harald Welte95109922020-12-04 13:55:38 +0100674 unsigned int i;
675
676 if (msg_def->mand_ies) {
677 for (i = 0; i < msg_def->mand_count; i++) {
678 uint8_t iei = msg_def->mand_ies[i];
679 if (!TLVP_PRESENT(tp, iei)) {
680 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Missing Mandatory IE: %s\n",
681 log_pfx, pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
682 osmo_tlv_prot_ie_name(pdef, iei));
Harald Welte30a92942020-12-04 14:09:22 +0100683 if (!err)
684 err = OSMO_TLVP_ERR_MAND_IE_MISSING;
Harald Welte95109922020-12-04 13:55:38 +0100685 }
686 }
687 }
688
689 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
690 uint16_t min_len;
691
692 if (!TLVP_PRESENT(tp, i))
693 continue;
694
695 min_len = pdef->ie_def[i].min_len;
696 if (TLVP_LEN(tp, i) < min_len) {
697 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: Short IE %s: %u < %u\n", log_pfx,
698 pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type),
699 osmo_tlv_prot_ie_name(pdef, i), TLVP_LEN(tp, i), min_len);
Harald Welte30a92942020-12-04 14:09:22 +0100700 if (!err)
701 err = OSMO_TLVP_ERR_IE_TOO_SHORT;
Harald Welte95109922020-12-04 13:55:38 +0100702 }
703 }
704
Harald Welte30a92942020-12-04 14:09:22 +0100705 return err;
Harald Welte95109922020-12-04 13:55:38 +0100706}
707
708/*! Parse + Validate a TLV-encoded message against the protocol definition.
709 * \param[in] pdef protocol definition of given protocol
710 * \param[out] dec caller-allocated pointer to \ref tlv_parsed
711 * \param[in] dec_multiples length of the tlv_parsed[] in \a dec.
712 * \param[in] msg_type message type of the parsed message
713 * \param[in] buf the input data buffer to be parsed
714 * \param[in] buf_len length of the input data buffer
715 * \param[in] lv_tag an initial LV tag at the start of the buffer
716 * \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
717 * \param[in] log_subsys logging sub-system for log messages
718 * \param[in] log_pfx prefix for log messages
719 * \returns 0 in case of success; negative osmo_tlv_parser_error in case of error
720 */
721int osmo_tlv_prot_parse(const struct osmo_tlv_prot_def *pdef,
722 struct tlv_parsed *dec, unsigned int dec_multiples, uint8_t msg_type,
723 const uint8_t *buf, unsigned int buf_len, uint8_t lv_tag, uint8_t lv_tag2,
724 int log_subsys, const char *log_pfx)
725{
726 int rc;
727
728 rc = tlv_parse2(dec, dec_multiples, pdef->tlv_def, buf, buf_len, lv_tag, lv_tag2);
729 if (rc < 0) {
730 LOGP(log_subsys, LOGL_ERROR, "%s %s %s: TLV parser error %d\n", log_pfx,
731 pdef->name, osmo_tlv_prot_msg_name(pdef, msg_type), rc);
732 return rc;
733 }
734
735 return osmo_tlv_prot_validate_tp(pdef, msg_type, dec, log_subsys, log_pfx);
736}
737
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200738/*! @} */