Commit Graph

11 Commits

Author SHA1 Message Date
Pau Espin beb7c1741e Fix 'Fix parsing of TLV_TYPE_SINGLE_TV'
A commit was merged recently attempting to fix decoding of
TLV_TYPE_SINGLE_TV. It did mostly a good job, but missed updating the
o_tag pointer used to fill in the structures.
This commit fixes that specific part missing.

Fixes: 559a6ee683
Change-Id: Id619459c17976b77cd2c7e4179123bb06807285c
2023-05-02 15:20:53 +02:00
Pau Espin e2217ee098 tlv: Show bug in decoded tlv_parsed for type TLV_TYPE_SINGLE_TV
A commit was merged recently attempting to fix decoding of
TLV_TYPE_SINGLE_TV. It did mostly a good job, but missed updating the
o_tag pointer used to fill in the structures.
This new unit test showcases the mentioned problem.
A follow-up patch will fix the bug.

Change-Id: Ia17c84059a413f80c2bcf194034ebac586ecf7e1
2023-05-02 12:11:26 +02:00
Harald Welte e61d459cef Support building with -Werror=strict-prototypes / -Werror=old-style-definition
Unfortunately "-std=c99" is not sufficient to make gcc ignore code that
uses constructs of earlier C standards, which were abandoned in C99.

See https://lwn.net/ml/fedora-devel/Y1kvF35WozzGBpc8@redhat.com/ for
some related discussion.

Change-Id: I84fd99442d0cc400fa562fa33623c142649230e2
2022-11-03 12:44:28 +01:00
Daniel Willmann 2fa0e9df68 tlv: Fix length returned by t{l16,16l}v_put
Every other function returns a pointer to the first byte after the tlv
that was just written.

tl16v seems to be a copy and paste error from tlv16 above and t16lv seems
to count the 16-bit tag twice.

The new tests verify that the return value of *_put(buf, tag, len, val)
points to buf + *_GROSS_LEN(len).

Change-Id: I268a7e11fb5dce67ce1bd7974ab86c4d2bd002f7
2021-03-18 14:20:35 +01:00
Harald Welte efdd641c29 tlv_parser: Fix various out-of-bounds accesses
The libosmocore TLV parser had a number of insufficient bounds checks
leading to reads beyond the end of the respective input buffer.

This patch
* adds proper out-of-bounds checks to all TLV types
* simplifies some of the existing checks
* introduces test cases to test all the corner cases
  where either TAG, or length, or value are not fully contained
  in the input buffer.

Thanks to Ilja Van Sprundel for reporting these problems.

Change-Id: I98b02c914c9e3ecf56050af846292aa6979d7508
2021-01-12 21:11:20 +01:00
Harald Welte 8006f5393e TLV: Add one-shot TLV encoder
So far, the TLV code contained two types of functions
* tlp_parse() to parse all TLVs according to definition into tlvp_parsed
* various helper functions to encode individual TLVs during message
  generation

This patch implements the inverse of tlv_parse(): tlv_encode(), which
takes a full 'struct tlv_pared' and encodes all IEs found in it.  The
order of IEs is in numerically ascending order of the tag.

As many protocols have different IE/TLV ordering requirements, let's add
a tlv_encode_ordered() function where the caller can specify the TLV
ordering during the one-shot encode.

Change-Id: I761a30bf20355a9f80a4a8e0c60b0b0f78515efe
2019-05-19 07:33:32 +00:00
Neels Hofmeyr a78b22ba20 add tlv_parse2(), capable of multiple instances of the same IE
Allow passing multiple struct tlv_parsed in an array, to allow parsing as many
repeated IEs as are expected by the caller.

From tlv_parse(), call tlv_parse2() with dec_multiple = 1 to yield the previous
behavior. tlv_parse() remains valid API.

An example of multiple IEs is the BSSMAP Handover Request, containing Cell
Identifier (Serving) and Cell Identifier (Target), both defined by 3GPP TS
48.008 3.2.2.17 with identical IE tags; both are mandatory.

Related: OS#2283 (inter-BSC HO, BSC side)
Change-Id: Id04008eaf0a1cafdbdc11b7efc556e3035b1c84d
2018-04-13 05:28:09 +02:00
Neels Hofmeyr a829b45c85 use osmo_init_logging2() with proper talloc ctx
Ironically, when deprecating osmo_init_logging() in
I216837780e9405fdaec8059c63d10699c695b360, I forgot to change the callers
within libosmocore itself, i.e. in the various regression tests.

Change-Id: Ia36c248f99353d5baaa2533f46a2f60a8579bdf8
2018-04-06 04:37:50 +02:00
Stefan Sperling c9bebbd325 add a regression test for TLV parsing with repeated IEs
Since commit bf383a1d83 tlv_parse()
will return the first occurrence of a repeated IE. Add a test to
verify this behaviour. This test passes with the current code and
fails if bf383a1d83 is reverted.

While here, fix lies in documentation about the return value of tlv_parse()
and fix a typo in another comment.

Change-Id: I041f38548c5e4236920991d6c681c1c1e04de9ca
Related: OS#2904
2018-03-16 16:58:19 +01:00
Pau Espin 21be83794f tlv_test.c: Fix compilation warnings
Several warnings like the one below are fixed:
warning: array subscript is below array bounds [-Warray-bounds]
  uint8_t *unchanged_ptr = buf - 1;

Change-Id: I35d7d926939c14700cbca732bd64e588c75424b4
2017-06-18 10:40:18 +02:00
Harald Welte fbd02fa8cc tlv: Import osmo_shift_* and osmo_match_shift_* from openbsc
These routines have nothing to do with specifically the BSC, so import
them to the TLV parser we keep in libosmogsm.
2016-05-05 18:49:27 +02:00