2014-09-11 05:05:51 +00:00
|
|
|
/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/gsm/tlv.h>
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-08-17 15:50:55 +00:00
|
|
|
/*! \addtogroup tlv
|
|
|
|
* @{
|
|
|
|
*/
|
2013-02-15 12:27:59 +00:00
|
|
|
/*! \file tlv_parser.c */
|
2011-08-17 15:50:55 +00:00
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
struct tlv_definition tvlv_att_def;
|
2012-07-13 23:50:33 +00:00
|
|
|
struct tlv_definition vtvlv_gan_att_def;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-08-17 15:50:55 +00:00
|
|
|
/*! \brief Dump pasred TLV structure to stdout */
|
2010-02-20 19:34:29 +00:00
|
|
|
int tlv_dump(struct tlv_parsed *dec)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= 0xff; i++) {
|
|
|
|
if (!dec->lv[i].val)
|
|
|
|
continue;
|
|
|
|
printf("T=%02x L=%d\n", i, dec->lv[i].len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-17 15:50:55 +00:00
|
|
|
/*! \brief Parse a single TLV encoded IE
|
|
|
|
* \param[out] o_tag the tag of the IE that was found
|
|
|
|
* \param[out] o_len length of the IE that was found
|
|
|
|
* \param[out] o_val pointer to the data of the IE that was found
|
|
|
|
* \param[in] def structure defining the valid TLV tags / configurations
|
|
|
|
* \param[in] buf the input data buffer to be parsed
|
|
|
|
* \param[in] buf_len length of the input data buffer
|
|
|
|
* \returns number of bytes consumed by the TLV entry / IE parsed
|
2010-02-20 19:34:29 +00:00
|
|
|
*/
|
|
|
|
int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
|
|
|
|
const struct tlv_definition *def,
|
|
|
|
const uint8_t *buf, int buf_len)
|
|
|
|
{
|
|
|
|
uint8_t tag;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
tag = *buf;
|
|
|
|
*o_tag = tag;
|
|
|
|
|
Added single octet TV (type + value) to libosmocore.
In case of a single octet, the upper nibble is the type, the lower nibble
carries the value.
[import from accidentially committed changes to osmocom-bb.git]
diff --git a/src/shared/libosmocore/include/osmocore/tlv.h b/src/shared/libosmocore/include/osmocore/tlv.h
index c733dbc..4cfce87 100644
--- a/src/shared/libosmocore/include/osmocore/tlv.h
+++ b/src/shared/libosmocore/include/osmocore/tlv.h
@@ -212,6 +212,7 @@ enum tlv_type {
TLV_TYPE_TLV,
TLV_TYPE_TL16V,
TLV_TYPE_TvLV,
+ TLV_TYPE_SINGLE_TV
};
struct tlv_def {
diff --git a/src/shared/libosmocore/src/tlv_parser.c b/src/shared/libosmocore/src/tlv_parser.c
index 407e57a..bbef7a9 100644
--- a/src/shared/libosmocore/src/tlv_parser.c
+++ b/src/shared/libosmocore/src/tlv_parser.c
@@ -36,6 +36,14 @@ int tlv_parse_one(uint8_t *o_tag, uint16_t *o_len, const uint8_t **o_val,
tag = *buf;
*o_tag = tag;
+ /* single octet TV IE */
+ if (def->def[tag & 0xf0].type == TLV_TYPE_SINGLE_TV) {
+ *o_tag = tag & 0xf0;
+ *o_val = buf;
+ *o_len = 1;
+ return 1;
+ }
+
/* FIXME: use tables for knwon IEI */
switch (def->def[tag].type) {
case TLV_TYPE_T:
2010-07-12 06:55:14 +00:00
|
|
|
/* single octet TV IE */
|
|
|
|
if (def->def[tag & 0xf0].type == TLV_TYPE_SINGLE_TV) {
|
|
|
|
*o_tag = tag & 0xf0;
|
|
|
|
*o_val = buf;
|
|
|
|
*o_len = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:18:11 +00:00
|
|
|
/* FIXME: use tables for known IEI */
|
2010-02-20 19:34:29 +00:00
|
|
|
switch (def->def[tag].type) {
|
|
|
|
case TLV_TYPE_T:
|
|
|
|
/* GSM TS 04.07 11.2.4: Type 1 TV or Type 2 T */
|
|
|
|
*o_val = buf;
|
|
|
|
*o_len = 0;
|
|
|
|
len = 1;
|
|
|
|
break;
|
|
|
|
case TLV_TYPE_TV:
|
|
|
|
*o_val = buf+1;
|
|
|
|
*o_len = 1;
|
|
|
|
len = 2;
|
|
|
|
break;
|
|
|
|
case TLV_TYPE_FIXED:
|
|
|
|
*o_val = buf+1;
|
|
|
|
*o_len = def->def[tag].fixed_len;
|
|
|
|
len = def->def[tag].fixed_len + 1;
|
|
|
|
break;
|
|
|
|
case TLV_TYPE_TLV:
|
2012-07-13 23:50:33 +00:00
|
|
|
tlv: /* GSM TS 04.07 11.2.4: Type 4 TLV */
|
2010-02-20 19:34:29 +00:00
|
|
|
if (buf + 1 > buf + buf_len)
|
|
|
|
return -1;
|
|
|
|
*o_val = buf+2;
|
|
|
|
*o_len = *(buf+1);
|
|
|
|
len = *o_len + 2;
|
|
|
|
if (len > buf_len)
|
|
|
|
return -2;
|
|
|
|
break;
|
2012-07-13 23:50:33 +00:00
|
|
|
case TLV_TYPE_vTvLV_GAN: /* 44.318 / 11.1.4 */
|
|
|
|
/* FIXME: variable-length TAG! */
|
|
|
|
if (*(buf+1) & 0x80) {
|
|
|
|
/* like TL16Vbut without highest bit of len */
|
|
|
|
if (2 > buf_len)
|
|
|
|
return -1;
|
|
|
|
*o_val = buf+3;
|
|
|
|
*o_len = (*(buf+1) & 0x7F) << 8 | *(buf+2);
|
|
|
|
len = *o_len + 3;
|
|
|
|
if (len > buf_len)
|
|
|
|
return -2;
|
|
|
|
} else {
|
|
|
|
/* like TLV */
|
|
|
|
goto tlv;
|
|
|
|
}
|
|
|
|
break;
|
2010-02-20 19:34:29 +00:00
|
|
|
case TLV_TYPE_TvLV:
|
|
|
|
if (*(buf+1) & 0x80) {
|
|
|
|
/* like TLV, but without highest bit of len */
|
|
|
|
if (buf + 1 > buf + buf_len)
|
|
|
|
return -1;
|
|
|
|
*o_val = buf+2;
|
|
|
|
*o_len = *(buf+1) & 0x7f;
|
|
|
|
len = *o_len + 2;
|
|
|
|
if (len > buf_len)
|
|
|
|
return -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* like TL16V, fallthrough */
|
|
|
|
case TLV_TYPE_TL16V:
|
|
|
|
if (2 > buf_len)
|
|
|
|
return -1;
|
|
|
|
*o_val = buf+3;
|
|
|
|
*o_len = *(buf+1) << 8 | *(buf+2);
|
|
|
|
len = *o_len + 3;
|
|
|
|
if (len > buf_len)
|
|
|
|
return -2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:18:11 +00:00
|
|
|
/*! \brief Parse an entire buffer of TLV encoded Information Elements
|
2011-08-17 15:50:55 +00:00
|
|
|
* \param[out] dec caller-allocated pointer to \ref tlv_parsed
|
|
|
|
* \param[in] def structure defining the valid TLV tags / configurations
|
|
|
|
* \param[in] buf the input data buffer to be parsed
|
|
|
|
* \param[in] buf_len length of the input data buffer
|
|
|
|
* \param[in] lv_tag an initial LV tag at the start of the buffer
|
|
|
|
* \param[in] lv_tag2 a second initial LV tag following the \a lv_tag
|
|
|
|
* \returns number of bytes consumed by the TLV entry / IE parsed
|
2010-02-20 19:34:29 +00:00
|
|
|
*/
|
|
|
|
int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
|
|
|
|
const uint8_t *buf, int buf_len, uint8_t lv_tag,
|
|
|
|
uint8_t lv_tag2)
|
|
|
|
{
|
|
|
|
int ofs = 0, num_parsed = 0;
|
|
|
|
uint16_t len;
|
|
|
|
|
|
|
|
memset(dec, 0, sizeof(*dec));
|
|
|
|
|
|
|
|
if (lv_tag) {
|
|
|
|
if (ofs > buf_len)
|
|
|
|
return -1;
|
|
|
|
dec->lv[lv_tag].val = &buf[ofs+1];
|
|
|
|
dec->lv[lv_tag].len = buf[ofs];
|
|
|
|
len = dec->lv[lv_tag].len + 1;
|
|
|
|
if (ofs + len > buf_len)
|
|
|
|
return -2;
|
|
|
|
num_parsed++;
|
|
|
|
ofs += len;
|
|
|
|
}
|
|
|
|
if (lv_tag2) {
|
|
|
|
if (ofs > buf_len)
|
|
|
|
return -1;
|
|
|
|
dec->lv[lv_tag2].val = &buf[ofs+1];
|
|
|
|
dec->lv[lv_tag2].len = buf[ofs];
|
|
|
|
len = dec->lv[lv_tag2].len + 1;
|
|
|
|
if (ofs + len > buf_len)
|
|
|
|
return -2;
|
|
|
|
num_parsed++;
|
|
|
|
ofs += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ofs < buf_len) {
|
|
|
|
int rv;
|
|
|
|
uint8_t tag;
|
|
|
|
const uint8_t *val;
|
|
|
|
|
|
|
|
rv = tlv_parse_one(&tag, &len, &val, def,
|
|
|
|
&buf[ofs], buf_len-ofs);
|
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
|
|
|
dec->lv[tag].val = val;
|
|
|
|
dec->lv[tag].len = len;
|
|
|
|
ofs += rv;
|
|
|
|
num_parsed++;
|
|
|
|
}
|
|
|
|
//tlv_dump(dec);
|
|
|
|
return num_parsed;
|
|
|
|
}
|
|
|
|
|
2011-08-17 15:50:55 +00:00
|
|
|
/*! \brief take a master (src) tlvdev and fill up all empty slots in 'dst' */
|
2010-02-20 19:34:29 +00:00
|
|
|
void tlv_def_patch(struct tlv_definition *dst, const struct tlv_definition *src)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dst->def); i++) {
|
|
|
|
if (src->def[i].type == TLV_TYPE_NONE)
|
|
|
|
continue;
|
|
|
|
if (dst->def[i].type == TLV_TYPE_NONE)
|
|
|
|
dst->def[i] = src->def[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((constructor)) void on_dso_load_tlv(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(tvlv_att_def.def); i++)
|
|
|
|
tvlv_att_def.def[i].type = TLV_TYPE_TvLV;
|
2012-07-13 23:50:33 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(vtvlv_gan_att_def.def); i++)
|
|
|
|
vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN;
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
2011-08-17 15:50:55 +00:00
|
|
|
|
2016-04-25 13:19:35 +00:00
|
|
|
/*! Advance the data pointer, subtract length and assign value pointer
|
|
|
|
* \param data pointer to the pointer to data
|
|
|
|
* \param data_len pointer to size_t containing \arg data length
|
|
|
|
* \param[in] len the length that we expect the fixed IE to hav
|
|
|
|
* \param[out] value pointer to pointer of value part of IE
|
|
|
|
* \returns length of IE value; negative in case of error
|
|
|
|
*/
|
|
|
|
int osmo_shift_v_fixed(uint8_t **data, size_t *data_len,
|
|
|
|
size_t len, uint8_t **value)
|
|
|
|
{
|
|
|
|
if (len > *data_len)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
*value = *data;
|
|
|
|
|
|
|
|
*data += len;
|
|
|
|
*data_len -= len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*data += *data_len;
|
|
|
|
*data_len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Match tag, check length and assign value pointer
|
|
|
|
* \param data pointer to the pointer to data
|
|
|
|
* \param data_len pointer to size_t containing \arg data length
|
|
|
|
* \param[in] tag the tag (IEI) that we expect at \arg data
|
|
|
|
* \param[in] len the length that we expect the fixed IE to have
|
|
|
|
* \param[out] value pointer to pointer of value part of IE
|
|
|
|
* \returns length of IE value; negative in case of error
|
|
|
|
*/
|
|
|
|
int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len,
|
|
|
|
uint8_t tag, size_t len,
|
|
|
|
uint8_t **value)
|
|
|
|
{
|
|
|
|
size_t ie_len;
|
|
|
|
|
|
|
|
if (*data_len == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if ((*data)[0] != tag)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (len > *data_len - 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
*value = *data + 1;
|
|
|
|
|
|
|
|
ie_len = len + 1;
|
|
|
|
*data += ie_len;
|
|
|
|
*data_len -= ie_len;
|
|
|
|
|
|
|
|
return ie_len;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*data += *data_len;
|
|
|
|
*data_len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Verify TLV header and advance data / subtract length
|
|
|
|
* \param data pointer to the pointer to data
|
|
|
|
* \param data_len pointer to size_t containing \arg data length
|
|
|
|
* \param[in] expected_tag the tag (IEI) that we expect at \arg data
|
|
|
|
* \param[out] value pointer to pointer of value part of IE
|
|
|
|
* \param[out] value_len pointer to length of \arg value
|
|
|
|
* \returns length of IE value; negative in case of error
|
|
|
|
*/
|
|
|
|
int osmo_match_shift_tlv(uint8_t **data, size_t *data_len,
|
|
|
|
uint8_t expected_tag, uint8_t **value,
|
|
|
|
size_t *value_len)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
uint8_t tag;
|
|
|
|
uint8_t *old_data = *data;
|
|
|
|
size_t old_data_len = *data_len;
|
|
|
|
|
|
|
|
rc = osmo_shift_tlv(data, data_len, &tag, value, value_len);
|
|
|
|
|
|
|
|
if (rc > 0 && tag != expected_tag) {
|
|
|
|
*data = old_data;
|
|
|
|
*data_len = old_data_len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Extract TLV and advance data pointer + subtract length
|
|
|
|
* \param data pointer to the pointer to data
|
|
|
|
* \param data_len pointer to size_t containing \arg data lengt
|
|
|
|
* \param[out] tag extract the tag (IEI) at start of \arg data
|
|
|
|
* \param[out] value extracted pointer to value part of TLV
|
|
|
|
* \param[out] value_len extracted length of \arg value
|
|
|
|
* \returns number of bytes subtracted
|
|
|
|
*/
|
|
|
|
int osmo_shift_tlv(uint8_t **data, size_t *data_len,
|
|
|
|
uint8_t *tag, uint8_t **value, size_t *value_len)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
size_t ie_len;
|
|
|
|
|
|
|
|
if (*data_len < 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = (*data)[1];
|
|
|
|
if (len > *data_len - 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (tag)
|
|
|
|
*tag = (*data)[0];
|
|
|
|
if (value)
|
|
|
|
*value = *data + 2;
|
|
|
|
if (value_len)
|
|
|
|
*value_len = len;
|
|
|
|
|
|
|
|
ie_len = len + 2;
|
|
|
|
|
|
|
|
*data += ie_len;
|
|
|
|
*data_len -= ie_len;
|
|
|
|
|
|
|
|
return ie_len;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*data += *data_len;
|
|
|
|
*data_len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Extract LV and advance data pointer + subtract length
|
|
|
|
* \param data pointer to the pointer to data
|
|
|
|
* \param data_len pointer to size_t containing \arg data lengt
|
|
|
|
* \param[out] value extracted pointer to value part of TLV
|
|
|
|
* \param[out] value_len extracted length of \arg value
|
|
|
|
* \returns number of bytes subtracted
|
|
|
|
*/
|
|
|
|
int osmo_shift_lv(uint8_t **data, size_t *data_len,
|
|
|
|
uint8_t **value, size_t *value_len)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
size_t ie_len;
|
|
|
|
|
|
|
|
if (*data_len < 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = (*data)[0];
|
|
|
|
if (len > *data_len - 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
*value = *data + 1;
|
|
|
|
if (value_len)
|
|
|
|
*value_len = len;
|
|
|
|
|
|
|
|
ie_len = len + 1;
|
|
|
|
*data += ie_len;
|
|
|
|
*data_len -= ie_len;
|
|
|
|
|
|
|
|
return ie_len;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*data += *data_len;
|
|
|
|
*data_len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|