diff options
-rw-r--r-- | include/osmocom/gsm/tlv.h | 11 | ||||
-rw-r--r-- | src/gsm/libosmogsm.map | 6 | ||||
-rw-r--r-- | src/gsm/tlv_parser.c | 171 | ||||
-rw-r--r-- | tests/Makefile.am | 7 | ||||
-rw-r--r-- | tests/testsuite.at | 6 | ||||
-rw-r--r-- | tests/tlv/tlv_test.c | 250 | ||||
-rw-r--r-- | tests/tlv/tlv_test.ok | 2 |
7 files changed, 451 insertions, 2 deletions
diff --git a/include/osmocom/gsm/tlv.h b/include/osmocom/gsm/tlv.h index c19034f0..cf09969d 100644 --- a/include/osmocom/gsm/tlv.h +++ b/include/osmocom/gsm/tlv.h @@ -436,4 +436,15 @@ static inline uint32_t tlvp_val32_unal(const struct tlv_parsed *tp, int pos) return res; } +int osmo_shift_v_fixed(uint8_t **data, size_t *data_len, + size_t len, uint8_t **value); +int osmo_match_shift_tv_fixed(uint8_t **data, size_t *data_len, + uint8_t tag, size_t len, uint8_t **value); +int osmo_shift_tlv(uint8_t **data, size_t *data_len, + uint8_t *tag, uint8_t **value, size_t *value_len); +int osmo_match_shift_tlv(uint8_t **data, size_t *data_len, + uint8_t tag, uint8_t **value, size_t *value_len); +int osmo_shift_lv(uint8_t **data, size_t *data_len, + uint8_t **value, size_t *value_len); + /*! @} */ diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map index 21d3c293..8bd0dbfe 100644 --- a/src/gsm/libosmogsm.map +++ b/src/gsm/libosmogsm.map @@ -278,6 +278,12 @@ tlv_parse_one; tvlv_att_def; vtvlv_gan_att_def; +osmo_shift_v_fixed; +osmo_match_shift_tv_fixed; +osmo_shift_tlv; +osmo_match_shift_tlv; +osmo_shift_lv; + gan_msgt_vals; gan_pdisc_vals; diff --git a/src/gsm/tlv_parser.c b/src/gsm/tlv_parser.c index 19d0c84d..e84edd97 100644 --- a/src/gsm/tlv_parser.c +++ b/src/gsm/tlv_parser.c @@ -225,4 +225,175 @@ static __attribute__((constructor)) void on_dso_load_tlv(void) vtvlv_gan_att_def.def[i].type = TLV_TYPE_vTvLV_GAN; } +/*! 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; +} + /*! @} */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 3aaa99b5..fa814c72 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -13,7 +13,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \ vty/vty_test comp128/comp128_test utils/utils_test \ smscb/gsm0341_test stats/stats_test \ bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test \ - sim/sim_test + sim/sim_test tlv/tlv_test if ENABLE_MSGFILE check_PROGRAMS += msgfile/msgfile_test @@ -109,6 +109,9 @@ vty_vty_test_LDADD = $(top_builddir)/src/vty/libosmovty.la $(top_builddir)/src/l sim_sim_test_SOURCES = sim/sim_test.c sim_sim_test_LDADD = $(top_builddir)/src/sim/libosmosim.la $(top_builddir)/src/libosmocore.la +tlv_tlv_test_SOURCES = tlv/tlv_test.c +tlv_tlv_test_LDADD = $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/libosmocore.la + # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac :;{ \ @@ -143,7 +146,7 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ vty/vty_test.ok comp128/comp128_test.ok \ utils/utils_test.ok stats/stats_test.ok \ bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \ - sim/sim_test.ok + sim/sim_test.ok tlv/tlv_test.ok DISTCLEANFILES = atconfig diff --git a/tests/testsuite.at b/tests/testsuite.at index 902c1fcc..762b10a0 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -184,3 +184,9 @@ AT_KEYWORDS([timer]) cat $abs_srcdir/timer/timer_test.ok > expout AT_CHECK([$abs_top_builddir/tests/timer/timer_test -s 5], [0], [expout], [ignore]) AT_CLEANUP + +AT_SETUP([tlv]) +AT_KEYWORDS([tlv]) +cat $abs_srcdir/tlv/tlv_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/tlv/tlv_test], [0], [expout], [ignore]) +AT_CLEANUP diff --git a/tests/tlv/tlv_test.c b/tests/tlv/tlv_test.c new file mode 100644 index 00000000..c571c3bd --- /dev/null +++ b/tests/tlv/tlv_test.c @@ -0,0 +1,250 @@ +#include <osmocom/gsm/tlv.h> + +static void check_tlv_parse(uint8_t **data, size_t *data_len, + uint8_t exp_tag, size_t exp_len, const uint8_t *exp_val) +{ + uint8_t *value; + size_t value_len; + uint8_t tag; + int rc; + uint8_t *saved_data = *data; + size_t saved_data_len = *data_len; + + rc = osmo_match_shift_tlv(data, data_len, exp_tag ^ 1, NULL, NULL); + OSMO_ASSERT(rc == 0); + + rc = osmo_match_shift_tlv(data, data_len, exp_tag, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 2); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); + + /* restore data/data_len */ + *data = saved_data; + *data_len = saved_data_len; + + rc = osmo_shift_tlv(data, data_len, &tag, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 2); + OSMO_ASSERT(tag == exp_tag); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); +} + +static void check_tv_fixed_match(uint8_t **data, size_t *data_len, + uint8_t tag, size_t len, const uint8_t *exp_val) +{ + uint8_t *value; + int rc; + + rc = osmo_match_shift_tv_fixed(data, data_len, tag ^ 1, len, NULL); + OSMO_ASSERT(rc == 0); + + rc = osmo_match_shift_tv_fixed(data, data_len, tag, len, &value); + OSMO_ASSERT(rc == (int)len + 1); + OSMO_ASSERT(memcmp(value, exp_val, len) == 0); +} + +static void check_v_fixed_shift(uint8_t **data, size_t *data_len, + size_t len, const uint8_t *exp_val) +{ + uint8_t *value; + int rc; + + rc = osmo_shift_v_fixed(data, data_len, len, &value); + OSMO_ASSERT(rc == (int)len); + OSMO_ASSERT(memcmp(value, exp_val, len) == 0); +} + +static void check_lv_shift(uint8_t **data, size_t *data_len, + size_t exp_len, const uint8_t *exp_val) +{ + uint8_t *value; + size_t value_len; + int rc; + + rc = osmo_shift_lv(data, data_len, &value, &value_len); + OSMO_ASSERT(rc == (int)value_len + 1); + OSMO_ASSERT(value_len == exp_len); + OSMO_ASSERT(memcmp(value, exp_val, exp_len) == 0); +} + +static void check_tlv_match_data_len(size_t data_len, uint8_t tag, size_t len, + const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t unchanged_len = 0xdead; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + size_t value_len = unchanged_len; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + tlv_put(data, tag, len, test_data); + if (data_len < len + 2) { + OSMO_ASSERT(-1 == osmo_match_shift_tlv(&data, &tmp_data_len, + tag, &value, &value_len)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + OSMO_ASSERT(value_len == unchanged_len); + } else { + OSMO_ASSERT(0 <= osmo_match_shift_tlv(&data, &tmp_data_len, + tag, &value, &value_len)); + OSMO_ASSERT(value != unchanged_ptr); + OSMO_ASSERT(value_len != unchanged_len); + } +} + +static void check_tv_fixed_match_data_len(size_t data_len, + uint8_t tag, size_t len, + const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + tv_fixed_put(data, tag, len, test_data); + + if (data_len < len + 1) { + OSMO_ASSERT(-1 == osmo_match_shift_tv_fixed(&data, &tmp_data_len, + tag, len, &value)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + } else { + OSMO_ASSERT(0 <= osmo_match_shift_tv_fixed(&data, &tmp_data_len, + tag, len, &value)); + OSMO_ASSERT(value != unchanged_ptr); + } +} + +static void check_v_fixed_shift_data_len(size_t data_len, + size_t len, const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + uint8_t *data = buf; + + OSMO_ASSERT(data_len <= sizeof(buf)); + + memcpy(data, test_data, len); + + if (data_len < len) { + OSMO_ASSERT(-1 == osmo_shift_v_fixed(&data, &tmp_data_len, + len, &value)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + } else { + OSMO_ASSERT(0 <= osmo_shift_v_fixed(&data, &tmp_data_len, + len, &value)); + OSMO_ASSERT(value != unchanged_ptr); + } +} + +static void check_lv_shift_data_len(size_t data_len, + size_t len, const uint8_t *test_data) +{ + uint8_t buf[300] = {0}; + + uint8_t *unchanged_ptr = buf - 1; + size_t unchanged_len = 0xdead; + size_t tmp_data_len = data_len; + uint8_t *value = unchanged_ptr; + size_t value_len = unchanged_len; + uint8_t *data = buf; + + lv_put(data, len, test_data); + if (data_len < len + 1) { + OSMO_ASSERT(-1 == osmo_shift_lv(&data, &tmp_data_len, + &value, &value_len)); + OSMO_ASSERT(tmp_data_len == 0); + OSMO_ASSERT(data == buf + data_len); + OSMO_ASSERT(value == unchanged_ptr); + OSMO_ASSERT(value_len == unchanged_len); + } else { + OSMO_ASSERT(0 <= osmo_shift_lv(&data, &tmp_data_len, + &value, &value_len)); + OSMO_ASSERT(value != unchanged_ptr); + OSMO_ASSERT(value_len != unchanged_len); + } +} + +static void test_tlv_shift_functions() +{ + uint8_t test_data[1024]; + uint8_t buf[1024]; + uint8_t *data_end; + unsigned i, len; + uint8_t *data; + size_t data_len; + const uint8_t tag = 0x1a; + + printf("Test shift functions\n"); + + for (i = 0; i < ARRAY_SIZE(test_data); i++) + test_data[i] = (uint8_t)i; + + for (len = 0; len < 256; len++) { + const unsigned iterations = sizeof(buf) / (len + 2) / 4; + + memset(buf, 0xee, sizeof(buf)); + data_end = data = buf; + + for (i = 0; i < iterations; i++) { + data_end = tlv_put(data_end, tag, len, test_data); + data_end = tv_fixed_put(data_end, tag, len, test_data); + /* v_fixed_put */ + memcpy(data_end, test_data, len); + data_end += len; + data_end = lv_put(data_end, len, test_data); + } + + data_len = data_end - data; + OSMO_ASSERT(data_len <= sizeof(buf)); + + for (i = 0; i < iterations; i++) { + check_tlv_parse(&data, &data_len, tag, len, test_data); + check_tv_fixed_match(&data, &data_len, tag, len, test_data); + check_v_fixed_shift(&data, &data_len, len, test_data); + check_lv_shift(&data, &data_len, len, test_data); + } + + OSMO_ASSERT(data == data_end); + + /* Test at end of data */ + + OSMO_ASSERT(-1 == osmo_match_shift_tlv(&data, &data_len, tag, NULL, NULL)); + OSMO_ASSERT(-1 == osmo_match_shift_tv_fixed(&data, &data_len, tag, len, NULL)); + OSMO_ASSERT((len ? -1 : 0) == osmo_shift_v_fixed(&data, &data_len, len, NULL)); + OSMO_ASSERT(-1 == osmo_shift_lv(&data, &data_len, NULL, NULL)); + + /* Test invalid data_len */ + for (data_len = 0; data_len <= len + 2 + 1; data_len += 1) { + check_tlv_match_data_len(data_len, tag, len, test_data); + check_tv_fixed_match_data_len(data_len, tag, len, test_data); + check_v_fixed_shift_data_len(data_len, len, test_data); + check_lv_shift_data_len(data_len, len, test_data); + } + } +} + +int main(int argc, char **argv) +{ + //osmo_init_logging(&info); + + test_tlv_shift_functions(); + + printf("Done.\n"); + return EXIT_SUCCESS; +} diff --git a/tests/tlv/tlv_test.ok b/tests/tlv/tlv_test.ok new file mode 100644 index 00000000..de159bfb --- /dev/null +++ b/tests/tlv/tlv_test.ok @@ -0,0 +1,2 @@ +Test shift functions +Done. |