Commit Graph

31 Commits

Author SHA1 Message Date
Pau Espin c0691ad3fd amr: osmo_amr_bwe_to_oa(): validate input data is long enough
Change-Id: I2d11ce71c29ae046c2feab1e59045d97dc3e5099
2022-12-21 21:35:06 +01:00
Pau Espin 16d9d5dba8 amr: use OSMO_BYTES_FOR_BITS()
Change-Id: Ib92856ede3f493615c24d883954989e952c51157
2022-12-21 21:33:48 +01:00
Pau Espin 99e5470ece amr: Clarify size of AMR BWE header and ToC
Change-Id: I34c76b18ee45e12dba92864715e8ef91e6de3b01
2022-12-21 21:32:25 +01:00
Pau Espin 550aaa28dc amr: use struct bwe_hdr in osmo_amr_bwe_to_oa()
The header conversion is now much clearer. Take the chance to delay the
memset(buf) after the checks.

Change-Id: I5042dc628ac70eca62b4980f4acae991dd976528
2022-12-21 18:37:45 +01:00
Pau Espin 26a02deb12 amr: constify input buffer in osmo_amr_is_oa()
Change-Id: I72ef6fc700f628ea158ea888a5f1e9dc8776ab7e
2022-12-21 18:37:45 +01:00
Pau Espin 678c7fc8fe amr: Support all SID and NO_DATA amr formats
Related: SYS#6161
Change-Id: I0e766b3231f03ea87d89fad6ecdce3bd14769054
2022-12-19 19:44:16 +01:00
Pau Espin 09e62cc563 amr: osmo_amr_bwe_to_oa() define variable as unsigned
There's no need to have the variable as signed anymore since the loop
was modified in the previous patch. osmo_amr_bytes() returns a size_t
(unsigned).

Change-Id: I8aa6b5f6d3334e152a62b7c28aac3f881f027894
2022-12-19 14:52:44 +01:00
Pau Espin a7af7ecf0b amr: osmo_amr_bwe_to_oa(): Modify loop to allow osmo_amr_bytes()=0 (NO_DATA)
oa_payload_len can be 2 if osmo_amr_bytes() returns 0 (it will return 0
when FT NO_DATA is supported). In tha case, the loop condition
(oa_payload_len - 3) (signed) is compared against unsigned i which ends
up accessing i=256.

Change-Id: I1e513f493d7883a03acbfa3d9744ec63657810b3
2022-12-19 14:52:44 +01:00
Pau Espin f5001d4148 amr: Guard against incorrect AMR FT passed to osmo_amr_{bits,bytes}()
Fixes: Coverity CID#283433
Change-Id: Ib11d4b64c6df19a85c4374fde89e1b56f410e438
2022-11-21 10:56:52 +01:00
Pau Espin 2cd10fe9bb amr: Document SID frame length from spec
Change-Id: I15197857d17829c1dc7408b6f007fabb3a80a537
2022-11-02 11:21:14 +01:00
Philipp Maier d24a436bc7 amr: cosmetic: fix grammer in comment
Change-Id: I01ad26986d925acdcdb3760f89b8a85dccdc3d5b
2022-02-15 14:35:36 +01:00
Pau Espin 2687d8fb72 amr: Fix length check in bwe<->iuup converters
The check was wrong for format types containing extra bits not aligned
to byte boundaries, such as FT7 (AMR Code 12.20, 244 bits, 31 bytes).

if the source has 1-6 extra bits, they can be fit with one less byte
when shifting 10 bits to the left.

Change-Id: I0552d727585886d25f613e64ca815fb6dcd53f25
2022-01-05 20:29:57 +01:00
Pau Espin cee23a7c6f amr: Fix FormatType from parsing BWE AMR header
The proper order is CMR(4)+F(1)+FT(4)+Q(1).

Hence, FT is 3 least significant bits of first byte and 1 most
significant bit of secont byte.

Change-Id: I66f39d3b9a608f07c202e7a5084a8537e9978a94
2022-01-05 18:38:06 +01:00
Alexander Couzens acce44d40f amr: Introduce APIs to convert BE to IuUP/IuFP format
These APIs allow for easy re-formatting of received AMR to forward
between regular RTP and IuUP.

Related: OS#1937
Change-Id: Id2bd32d5f2060abe581730996dc4251381bf7d4f
2022-01-04 14:50:51 +01:00
Alexander Couzens f2c1d4c328 amr: don't rely on pad bits to be zero
Change-Id: I354fbbcbaf7ff7e7d498b22c175a57a79b6b8856
2021-12-31 00:31:32 +01:00
Philipp Maier b61eaaccc3 amr: fix off-by-one in osmo_amr_bwe_to_oa()
The for loop in osmo_amr_bwe_to_oa, that converts the body part of the
AMR payload runs one byte too far. This may cause that some of the
padding bits in the end are not set to zero. The loop is designed to
convert n-1 bytes and the nth byte is done separately at the end.

Change-Id: I91e755b83aaac722079879c026d913cc446812d1
2020-05-25 17:05:04 +02:00
Alexander Chemeris 09c71b04f5 amr: Whitespace fix
Change-Id: I7da6813a501ee97ca54611f627870c7eb842ddbe
2020-05-16 20:21:48 +00:00
Alexander Chemeris 4752930972 amr: Fix OA<->BWE conversion.
Size of a single AMR frame doesn't always shrink by a byte when
converted from octet-aligned to bandwidth-efficient mode. It does
shrink for AMR modes 2, 3, 4, 6, and 7 but doesn't shrink for
AMR modes 0, 1, 5, and SID frames because we only remove 6 bits.
So old code generated truncated AMR packets for those AMR modes.
This patch fixes the length calculation by properly counting bits.

Proper bit counting is also bringing us one small step closer to
properly handlig multi-frame AMR packets.

Change-Id: I0462e054a0adc9080456f3eeea9cab7c229cdb70
2020-05-16 20:21:48 +00:00
Neels Hofmeyr 8b77ad940a Revert "amr: Fix OA<->BWE conversion."
This reverts commit 002a51e218.

Reason for revert: amr_test fails with sanitizer build:

Sample No.: 6
   bw-efficient:  a7bfc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03fc03f
                  1010011110111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111
../../../src/libosmo-netif/src/amr.c:63:24: runtime error: index 15 out of bounds for type 'size_t [9]'
../../../src/libosmo-netif/src/amr.c:63:24: runtime error: load of address 0x7f69498e56b8 with insufficient space for an object of type 'size_t'
0x7f69498e56b8: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  5f 00 00 00 00 00 00 00  67 00 00 00 00 00 00 00  76 00 00 00
              ^ 
=================================================================
==489935==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7f69498e56b8 at pc 0x7f69498abec7 bp 0x7ffeafb35330 sp 0x7ffeafb35328
READ of size 8 at 0x7f69498e56b8 thread T0
    #0 0x7f69498abec6 in osmo_amr_bytes ../../../src/libosmo-netif/src/amr.c:63
    #1 0x7f69498ac661 in osmo_amr_bwe_to_oa ../../../src/libosmo-netif/src/amr.c:193
    #2 0x5648b11afb96 in osmo_amr_bwe_to_oa_test ../../../src/libosmo-netif/tests/amr/amr_test.c:134
    #3 0x5648b11af31d in main ../../../src/libosmo-netif/tests/amr/amr_test.c:235
    #4 0x7f6948d5de0a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26e0a)
    #5 0x5648b11af3d9 in _start (/n/s/dev/make/libosmo-netif/tests/amr/amr_test+0x43d9)

0x7f69498e56b8 is located 8 bytes to the left of global variable 'amr_ft_to_bits' defined in '../../../src/libosmo-netif/src/amr.c:32:15' (0x7f69498e56c0) of size 72
0x7f69498e56b8 is located 48 bytes to the right of global variable 'amr_ft_to_bytes' defined in '../../../src/libosmo-netif/src/amr.c:44:15' (0x7f69498e5640) of size 72
SUMMARY: AddressSanitizer: global-buffer-overflow ../../../src/libosmo-netif/src/amr.c:63 in osmo_amr_bytes

Change-Id: I8232521c513722435e71dc90bdbfee10f8f83496
2020-05-14 23:22:54 +00:00
Alexander Chemeris 002a51e218 amr: Fix OA<->BWE conversion.
Size of a single AMR frame doesn't always shrink by a byte when
converted from octet-aligned to bandwidth-efficient mode. It does
shrink for AMR modes 2, 3, 4, 6, and 7 but doesn't shrink for
AMR modes 0, 1, 5, and SID frames because we only remove 6 bits.
So old code generated truncated AMR packets for those AMR modes.
This patch fixes the length calculation by properly counting bits.

Proper bit counting is also bringing us one small step closer to
properly handlig multi-frame AMR packets.

Change-Id: I9fc5fb92e9bada22a47a82fcfb0925e892e50ced
2020-05-14 12:03:42 +00:00
Philipp Maier e380b38e28 amr: use sizeof(struct amr_hdr) when working with amr header length
The header of an AMR header payload is 2 bytes long. At the moment we
use just a constant of 2 when we refer to the header length, but we have a
struct amr_hdr defined. Lets use sizeof(struct amr_hdr) to make it more
clear that we are refering to the header length.

Change-Id: Ic7ca04b99a97d7d3b91717b0c3e6c55ef3001a3e
2019-03-20 15:45:05 +01:00
Philipp Maier 5ca043655a amr: be sure result of osmo_amr_bwe_to_oa() fits into int buf
osmo_amr_bwe_to_oa() uses an internal buffer with static size to store
intermediate results. The buffer is large enough for any real world
situation, but the check that tests if the result would fit into the
internal buffer is incorrect. It checks if there is enough room for the
existing payload, but does not include the expected growth of the
payload. Eventually the buffer could be overrun by one byte if one would
put a 256 byte long AMR payload.

Fixes: CID#195926
Change-Id: I4d7ac570a0b48368a82183673c46bca5f235f228
2019-03-12 09:29:06 +01:00
Philipp Maier 3e77d57281 amr: cosmetic: correct sourcecode formatting
Change-Id: Ie4ad6b1a2382da4dc21e66a45c2a96224cab5752
2019-03-12 09:29:06 +01:00
Philipp Maier fa7df87260 AMR: add functions to convert between bw-efficient and octet-aligned
RFC 3267 describes two different AMR frame formats. Octet Aligned and
Bandwidth efficient mode. In Bandwith efficient mode the padding bits,
which are used to align CMR, TOC and payload on octet boundaries are
saved and the fielda are packed directly one after another.

- Add functions to convert from one mode to the other and vice versa.
- Add function to detect in which mode an AMR frame is encoded.

Change-Id: I5b5a0fa644d8dbb1f04f9d7e35312683c7b3d196
Related: SYS#4470
2019-03-07 10:22:22 +01:00
Philipp Maier 0fe9b3664a AMR: add define constants for AMR payload length
AMR uses different payload sizes, those sizes are well defined in RFC
3267. Lets add define constants and replace the magic values with the
define constants.

Also correct the value for AMR_FT_SID in amr_ft_to_bytes from 6 to 5
(39bits / 8 = 4.875 bytes ==> 5 byte, see also RFC 3267, chapter 3.6)

Change-Id: I65b5da920d58015b875d6dcf17aacdc04b58955e
2019-03-01 09:12:20 +01:00
Harald Welte bea215a565 Add SPDX-License-Identifier + Copyright statements
Change-Id: I43bb1c4a889421907a1a08eb29c96f2330ab00ec
2017-11-13 01:21:03 +09:00
Pablo Neira Ayuso ec4beebc07 amr: use AMR_FT_MAX instead of hardcoded number
Use AMR_FT_MAX instead of 9. This patch is a cleanup.
2013-12-14 22:41:03 +01:00
Pablo Neira Ayuso 70214a15d1 osmux: further sanity checkings for AMR FT
According to RFC3267, AMR FT upper 9 should be discarded. This patch
adds extra validation to make sure that input RTP traffic encapsulating
AMR payload and OSMUX amr_ft field are OK with regards to that
restriction.
2013-05-24 12:16:49 +02:00
Pablo Neira Ayuso b1f92341c3 amr: add AMR FT SID definition
According to RFC3267, AMR FT 8 is reserved to SID and its size is
8 bytes.
2013-05-24 11:18:40 +02:00
Pablo Neira Ayuso 92601d0a20 osmux: use ft instead of the cmr
The cmr is the requested codec for the other peer, the ft actually
contains the current codec mode. cmr may contain 15 which means
"don't care".
2013-02-12 19:29:42 +01:00
Pablo Neira Ayuso 42915068af src: add function to obtain bytes depending on AMR frame code 2012-07-13 21:12:32 +02:00