Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 1 | /* (C) 2008-2017 by Harald Welte <laforge@gnumonks.org> |
| 2 | * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 8 | * 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 25 | #include <errno.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/gsm/tlv.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 29 | /*! \addtogroup tlv |
| 30 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 31 | * Osmocom TLV Parser |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 32 | * |
| 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 45 | * |
| 46 | * \file tlv_parser.c */ |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 47 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 48 | struct tlv_definition tvlv_att_def; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 49 | struct tlv_definition vtvlv_gan_att_def; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 50 | |
Stefan Sperling | c9bebbd | 2018-03-16 15:59:01 +0100 | [diff] [blame] | 51 | /*! Dump parsed TLV structure to stdout */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | int 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 64 | /*! Copy \ref tlv_parsed using given talloc context |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 65 | * \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 | */ |
| 69 | struct 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 99 | /*! Merge all \ref tlv_parsed attributes of 'src' into 'dst' |
Max | dbd3a92 | 2017-01-02 14:10:30 +0100 | [diff] [blame] | 100 | * \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 | */ |
| 104 | int 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 123 | /*! Parse a single TLV encoded IE |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 124 | * \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 |
Stefan Sperling | 1e50e2a | 2018-01-08 19:20:02 +0100 | [diff] [blame] | 130 | * \returns number of bytes consumed by the TLV entry / IE parsed; negative in case of error |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 131 | */ |
| 132 | int 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 Eversberg | cd2a74b | 2010-07-12 08:55:14 +0200 | [diff] [blame] | 142 | /* 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 Hofmeyr | 667e83d | 2015-11-02 20:18:11 +0100 | [diff] [blame] | 150 | /* FIXME: use tables for known IEI */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 151 | 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 Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 169 | tlv: /* GSM TS 04.07 11.2.4: Type 4 TLV */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 170 | 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 Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 178 | 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 194 | 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 223 | /*! Parse an entire buffer of TLV encoded Information Elements |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 224 | * \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 |
Stefan Sperling | c9bebbd | 2018-03-16 15:59:01 +0100 | [diff] [blame] | 230 | * \returns number of TLV entries parsed; negative in case of error |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 231 | */ |
| 232 | int 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; |
Stefan Sperling | 1e50e2a | 2018-01-08 19:20:02 +0100 | [diff] [blame] | 247 | if (ofs + len > buf_len) { |
| 248 | dec->lv[lv_tag].val = NULL; |
| 249 | dec->lv[lv_tag].len = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 250 | return -2; |
Stefan Sperling | 1e50e2a | 2018-01-08 19:20:02 +0100 | [diff] [blame] | 251 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 252 | num_parsed++; |
| 253 | ofs += len; |
| 254 | } |
| 255 | if (lv_tag2) { |
| 256 | if (ofs > buf_len) |
| 257 | return -1; |
| 258 | dec->lv[lv_tag2].val = &buf[ofs+1]; |
| 259 | dec->lv[lv_tag2].len = buf[ofs]; |
| 260 | len = dec->lv[lv_tag2].len + 1; |
Stefan Sperling | 1e50e2a | 2018-01-08 19:20:02 +0100 | [diff] [blame] | 261 | if (ofs + len > buf_len) { |
| 262 | dec->lv[lv_tag2].val = NULL; |
| 263 | dec->lv[lv_tag2].len = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 264 | return -2; |
Stefan Sperling | 1e50e2a | 2018-01-08 19:20:02 +0100 | [diff] [blame] | 265 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 266 | num_parsed++; |
| 267 | ofs += len; |
| 268 | } |
| 269 | |
| 270 | while (ofs < buf_len) { |
| 271 | int rv; |
| 272 | uint8_t tag; |
| 273 | const uint8_t *val; |
| 274 | |
| 275 | rv = tlv_parse_one(&tag, &len, &val, def, |
| 276 | &buf[ofs], buf_len-ofs); |
| 277 | if (rv < 0) |
| 278 | return rv; |
Harald Welte | bf383a1 | 2018-02-02 12:11:14 +0100 | [diff] [blame] | 279 | /* Most GSM related protocols clearly indicate that in case of duplicate |
| 280 | * IEs, only the first occurrence shall be used, while any further occurrences |
| 281 | * shall be ignored. See e.g. 3GPP TS 24.008 Section 8.6.3 */ |
| 282 | if (dec->lv[tag].val == NULL) { |
| 283 | dec->lv[tag].val = val; |
| 284 | dec->lv[tag].len = len; |
| 285 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 286 | ofs += rv; |
| 287 | num_parsed++; |
| 288 | } |
| 289 | //tlv_dump(dec); |
| 290 | return num_parsed; |
| 291 | } |
| 292 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 293 | /*! take a master (src) tlvdev and fill up all empty slots in 'dst' |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 294 | * \param dst TLV parser definition that is to be patched |
| 295 | * \param[in] src TLV parser definition whose content is patched into \a dst */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 296 | void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | for (i = 0; i < ARRAY_SIZE(dst->def); i++) { |
| 301 | if (src->def[i].type == TLV_TYPE_NONE) |
| 302 | continue; |
| 303 | if (dst->def[i].type == TLV_TYPE_NONE) |
| 304 | dst->def[i] = src->def[i]; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static __attribute__((constructor)) void on_dso_load_tlv(void) |
| 309 | { |
| 310 | int i; |
| 311 | for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++) |
| 312 | tvlv_att_def.def[i].type = TLV_TYPE_TvLV; |
Harald Welte | 2fe6847 | 2012-07-14 01:50:33 +0200 | [diff] [blame] | 313 | |
| 314 | for (i = 0; i < ARRAY_SIZE(vtvlv_gan_att_def.def); i++) |
| 315 | vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 316 | } |
Harald Welte | 57c7d37 | 2011-08-17 17:50:55 +0200 | [diff] [blame] | 317 | |
Harald Welte | fbd02fa | 2016-04-25 15:19:35 +0200 | [diff] [blame] | 318 | /*! Advance the data pointer, subtract length and assign value pointer |
| 319 | * \param data pointer to the pointer to data |
| 320 | * \param data_len pointer to size_t containing \arg data length |
| 321 | * \param[in] len the length that we expect the fixed IE to hav |
| 322 | * \param[out] value pointer to pointer of value part of IE |
| 323 | * \returns length of IE value; negative in case of error |
| 324 | */ |
| 325 | int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, |
| 326 | size_t len, uint8_t **value) |
| 327 | { |
| 328 | if (len > *data_len) |
| 329 | goto fail; |
| 330 | |
| 331 | if (value) |
| 332 | *value = *data; |
| 333 | |
| 334 | *data += len; |
| 335 | *data_len -= len; |
| 336 | |
| 337 | return len; |
| 338 | |
| 339 | fail: |
| 340 | *data += *data_len; |
| 341 | *data_len = 0; |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | /*! Match tag, check length and assign value pointer |
| 346 | * \param data pointer to the pointer to data |
| 347 | * \param data_len pointer to size_t containing \arg data length |
| 348 | * \param[in] tag the tag (IEI) that we expect at \arg data |
| 349 | * \param[in] len the length that we expect the fixed IE to have |
| 350 | * \param[out] value pointer to pointer of value part of IE |
| 351 | * \returns length of IE value; negative in case of error |
| 352 | */ |
| 353 | int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, |
| 354 | uint8_t tag, size_t len, |
| 355 | uint8_t **value) |
| 356 | { |
| 357 | size_t ie_len; |
| 358 | |
| 359 | if (*data_len == 0) |
| 360 | goto fail; |
| 361 | |
| 362 | if ((*data)[0] != tag) |
| 363 | return 0; |
| 364 | |
| 365 | if (len > *data_len - 1) |
| 366 | goto fail; |
| 367 | |
| 368 | if (value) |
| 369 | *value = *data + 1; |
| 370 | |
| 371 | ie_len = len + 1; |
| 372 | *data += ie_len; |
| 373 | *data_len -= ie_len; |
| 374 | |
| 375 | return ie_len; |
| 376 | |
| 377 | fail: |
| 378 | *data += *data_len; |
| 379 | *data_len = 0; |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | /*! Verify TLV header and advance data / subtract length |
| 384 | * \param data pointer to the pointer to data |
| 385 | * \param data_len pointer to size_t containing \arg data length |
| 386 | * \param[in] expected_tag the tag (IEI) that we expect at \arg data |
| 387 | * \param[out] value pointer to pointer of value part of IE |
| 388 | * \param[out] value_len pointer to length of \arg value |
| 389 | * \returns length of IE value; negative in case of error |
| 390 | */ |
| 391 | int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, |
| 392 | uint8_t expected_tag, uint8_t **value, |
| 393 | size_t *value_len) |
| 394 | { |
| 395 | int rc; |
| 396 | uint8_t tag; |
| 397 | uint8_t *old_data = *data; |
| 398 | size_t old_data_len = *data_len; |
| 399 | |
| 400 | rc = osmo_shift_tlv(data, data_len, &tag, value, value_len); |
| 401 | |
| 402 | if (rc > 0 && tag != expected_tag) { |
| 403 | *data = old_data; |
| 404 | *data_len = old_data_len; |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | /*! Extract TLV and advance data pointer + subtract length |
| 412 | * \param data pointer to the pointer to data |
| 413 | * \param data_len pointer to size_t containing \arg data lengt |
| 414 | * \param[out] tag extract the tag (IEI) at start of \arg data |
| 415 | * \param[out] value extracted pointer to value part of TLV |
| 416 | * \param[out] value_len extracted length of \arg value |
| 417 | * \returns number of bytes subtracted |
| 418 | */ |
| 419 | int osmo_shift_tlv(uint8_t **data, size_t *data_len, |
| 420 | uint8_t *tag, uint8_t **value, size_t *value_len) |
| 421 | { |
| 422 | size_t len; |
| 423 | size_t ie_len; |
| 424 | |
| 425 | if (*data_len < 2) |
| 426 | goto fail; |
| 427 | |
| 428 | len = (*data)[1]; |
| 429 | if (len > *data_len - 2) |
| 430 | goto fail; |
| 431 | |
| 432 | if (tag) |
| 433 | *tag = (*data)[0]; |
| 434 | if (value) |
| 435 | *value = *data + 2; |
| 436 | if (value_len) |
| 437 | *value_len = len; |
| 438 | |
| 439 | ie_len = len + 2; |
| 440 | |
| 441 | *data += ie_len; |
| 442 | *data_len -= ie_len; |
| 443 | |
| 444 | return ie_len; |
| 445 | |
| 446 | fail: |
| 447 | *data += *data_len; |
| 448 | *data_len = 0; |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | /*! Extract LV and advance data pointer + subtract length |
| 453 | * \param data pointer to the pointer to data |
| 454 | * \param data_len pointer to size_t containing \arg data lengt |
| 455 | * \param[out] value extracted pointer to value part of TLV |
| 456 | * \param[out] value_len extracted length of \arg value |
| 457 | * \returns number of bytes subtracted |
| 458 | */ |
| 459 | int osmo_shift_lv(uint8_t **data, size_t *data_len, |
| 460 | uint8_t **value, size_t *value_len) |
| 461 | { |
| 462 | size_t len; |
| 463 | size_t ie_len; |
| 464 | |
| 465 | if (*data_len < 1) |
| 466 | goto fail; |
| 467 | |
| 468 | len = (*data)[0]; |
| 469 | if (len > *data_len - 1) |
| 470 | goto fail; |
| 471 | |
| 472 | if (value) |
| 473 | *value = *data + 1; |
| 474 | if (value_len) |
| 475 | *value_len = len; |
| 476 | |
| 477 | ie_len = len + 1; |
| 478 | *data += ie_len; |
| 479 | *data_len -= ie_len; |
| 480 | |
| 481 | return ie_len; |
| 482 | |
| 483 | fail: |
| 484 | *data += *data_len; |
| 485 | *data_len = 0; |
| 486 | return -1; |
| 487 | } |
| 488 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 489 | /*! @} */ |