Commit Graph

4775 Commits

Author SHA1 Message Date
Vadim Yanitskiy d8070f57d7 soft_uart: demonstrate a problem with inefficient polling
As outlined in the test case, we pull a total of 50 bits from the
transmitter in two rounds, pulling 25 bits at a time.  In the default
8-N-1 configuration, 50 bits should ideally comprise 5 characters.
However, as observed, only a total of 4 characters are retrieved
from the application, leaving the remaining 10 bits (5 + 5) unused.

Change-Id: Ic2539681a4adf6c1822e0bc256e4c829813d0e21
2023-12-11 20:49:24 +00:00
Vadim Yanitskiy b392099602 soft_uart: demonstrate a problem with manual flush()ing
This problem can only happen if the user is flush()ing the Rx buffer
manually by calling osmo_soft_uart_flush_rx().  Let's demonstrate it
in the unit test, so that we don't forget about it (add FIXME).

Change-Id: Iad932a505d6fd98360f90510651501f8708ff5d2
2023-12-11 20:49:24 +00:00
Vadim Yanitskiy d76cc3704e soft_uart: fix the Rx flushing logic, add a unit test
Coverity tells us that with the current logic it's possible (in theory)
that we may dereference NULL pointer in osmo_soft_uart_flush_rx().  This
is highly unlikely, because the Rx buffer gets allocated once when the
Rx is enabled and remains even after the Rx gets disabled.  The Rx flags
cannot be anything than 0x00 before the Rx gets enabled.

Even though this NULL pointer dereference is unlikely, the Rx flushing
logic is still not entirely correct.  As can be seen from the unit test
output, the Rx callback of the application may be called with an empty
msgb if the following conditions are both met:

a) the osmo_soft_uart_flush_rx() is invoked manually, and
b) a parity and/or a framing error has occurred previously.

We should not be checking suart->rx.flags in osmo_soft_uart_flush_rx(),
since this is already done in suart_rx_ch(), which is calling it.
Removing this check also eliminates a theoretical possibility of the
NULL pointer dereference, so we're killing two birds with one stone.

- Do not check suart->rx.flags in osmo_soft_uart_flush_rx().
- Add a unit test for various flush()ing scenarios.

Change-Id: I5179f5fd2361e4e96ac9bf48e80b99e53a7e4712
Fixes: CID#336545
2023-12-11 20:49:24 +00:00
Pau Espin 19f27bb551 socket: Introduce API osmo_sock_sctp_get_peer_addr_info()
This is a convenience helper to reetrieve the whole set of remote
addresses and call getsockopt() on them, making it easy for users to
analyse the full set of remote addresses of a socket simply providing an
fd.

Related: SYS#6636
Change-Id: I3e1c84526b006baff435bbbca49dc6cf7d201cf5
2023-12-11 11:11:16 +01:00
Vadim Yanitskiy 9c603e64bf logging: fix NULL pointer dereference in _output_buf()
In the _output_buf() we explicitly initialize only the 'buf' and 'len'
fields of the struct osmo_strbuf, leaving the 'pos' field implicitly
initialized to NULL.  Later, in this function, 'sb.pos' is passed to
ctime_r() and strlen(), leading to a NULL pointer dereference (segfault)
in certain scenarios.

This situation can occur when color logging is disabled or when
a specific logging subsystem has no associated color.  Any application
using libosmocore's logging API would crash with the following config:

log stderr
 logging filter all 1
 logging timestamp 1
 logging color 0

Fix this by initializing the 'pos' field explicitly.

Change-Id: I7ec9badf525e03e54e10b725d820c636eaa3fd1c
Fixes: d71331bc "logging: fix nul octets in log output / use osmo_strbuf"
Fixes: CID#336550
2023-12-11 04:07:31 +00:00
Pau Espin d2e8f67f3d socket: Fix uninitialized mem ptr free in osmo_sock_init2_multiaddr2()
The goto tag was wrong, probably due to a copy-paste mistype while
reimplementing the function.

Closes: Coverity CID#336546
Change-Id: I06b810fde7bf750fcb42d6d9e6223883e26f5f0b
2023-12-10 11:29:58 +00:00
Vadim Yanitskiy 26dff79f8b soft_uart: cosmetic: use consistent naming for the Rx buffer msgb
In osmo_soft_uart_flush_rx() we use "soft_uart_rx", so be consistent.

Change-Id: Id637a39bab8ecd04bca5580bb48f965b501f5b2e
2023-12-10 11:20:40 +00:00
Vadim Yanitskiy d0b575c059 tests/soft_uart: cosmetic: improve readability of the test output
Change-Id: Icf5410f0b292d41532e0cbd17e6ca0509c76cbd5
2023-12-10 11:20:40 +00:00
Vadim Yanitskiy c460deb2f1 tests/soft_uart: assert that osmo_soft_uart_rx_ubits() returns 0
According to Coverity, we check return value of this function in
all other cases except this one (9 out of 10 times), so let's add
the missing assert(), just to be sure.

Change-Id: I675f4089cc990be5fcda792276b6808742f6f0d7
Fixes: CID#336557
2023-12-10 11:20:40 +00:00
Vadim Yanitskiy 9c972934f9 core: fix wrong logic in _osmo_it_q_dequeue()
If the given queue is empty, queue->list.next points to &queue->list.
Current implementation would call llist_del() on the queue's llist_head,
decrement queue->current_length (which will be 0), and return a pointer
to &queue->list to the caller.  This is completely wrong.

- Use the existing item_dequeue(), which does exactly what we need.
- Do not decrement the current_length if nothing was dequeued.
- Uncomment code in the unit test, we should not crash anymore.

Change-Id: I63094df73b166b549616c869ad908e9f4f7d46d1
Fixes: CID#336557
2023-12-09 15:23:11 +07:00
Vadim Yanitskiy 09c8bfce07 tests/it_q: add tc_enqueue/dequeue testcase
This patch is adding a simple testcase, which does the following:

* enqueue up to the limit (12 items),
* dequeue up to the limit (12 items).

Everything works as expected, unless you attempt to dequeue from
an empty queue: the test binary segfaults.  The problem is explained
and fixed in a subsequent patch.

Change-Id: Ie0edbf00e656fbe231952bdbccfd37d143e8b2b1
Related: CID#336557
2023-12-09 15:23:09 +07:00
Vadim Yanitskiy 74b31acb5d tests/coding: fix -Wmaybe-uninitialized in test_pdtch()
I am seeing this when building with gcc v13.2.1:

tests/coding/coding_test.c: In function ‘test_pdtch’:
tests/coding/coding_test.c:444:23: warning: ‘*result[<unknown>]’
                                   may be used uninitialized
  444 |                 result[len - 1] &= 0x7f;
      |                 ~~~~~~^~~~~~~~~
tests/coding/coding_test.c:448:23: warning: ‘*result[39]’
                                   may be used uninitialized
  448 |                 result[len - 1] &= 0x07;
      |                 ~~~~~~^~~~~~~~~

The idea here is to pre-clear some bits in the resulting buffer,
because they're not going to be set during decoding of the burst
bits.  The problem is that result[] holds uninitialized data, so
we're basically taking a 'garbage' octet and clear some of its
bits.  The remaining 'garbage' bits of that octet are overwritten
by the decoder, so in the end we still get deterministic results.

Let's make GCC happy by clearing all bits in the last octet.

Change-Id: I24d79de8b3a5f4184b71414504657e5857498e0e
2023-12-08 03:58:37 +07:00
Vadim Yanitskiy c58e4851f4 coding: gsm0503_pdtch_decode(): implement USF decoding for CS1
This patch brings decoding of the CS1 in consistency with the other
three coding schemes, for which we support decoding USF independently.

Change-Id: I61a3628741c0ac68374fc7f077cf3a07e51277c3
2023-12-08 03:35:57 +07:00
Vadim Yanitskiy 93b8eb3153 coding: clarify the USF decoding for PDCH blocks
The USF (Uplink State Flag) field is present in the MAC header of all
Downlink PDCH blocks.  It is used by the network to indicate which MS
can transmit on subsequent Uplink PDCH block(s).  This field is of a
high importance for the MS, thus the decoder API allows the caller
to obtain USF value separately from the actual data bits.

In the case of gsm0503_pdtch_decode(), if the 'usf_p' pointer is not
NULL, the USF value would be assigned for CS2/CS3/CS4 (but not CS1)
even if the CRC check fails (negative return value).  A subsequent
patch is to bring the CS1 in consistency with CS2/CS3/CS4.

In the case of gsm0503_pdtch_egprs_decode(), decoding of the USF
field separately from data bits is not implemented, and moreover
the function itself cannot be used for decoding Downlink blocks.

Change-Id: I43e8bfb4003f34766ace7c5c6080ca583ce5efbb
2023-12-08 03:27:23 +07:00
Vadim Yanitskiy ccf9aadc96 coding: fix doxygen docs for gsm0503_pdtch[_egprs]_decode()
Change-Id: I09a4c52f5704a6436eff524cd2ce5c35347aafde
2023-12-08 01:11:40 +07:00
Pau Espin 5ac8aa5852 socket: Introduce API osmo_sock_multiaddr_get_name_buf()
An extra osmo_multiaddr_ip_and_port_snprintf() API is introduced which
is used by osmo_sock_multiaddr_get_name_buf() but which will also be
used by other app uers willing to use
osmo_sock_multiaddr_get_ip_and_port() according to its needs (eg. only
printing the local side).

Related: SYS#6636
Change-Id: I48950754ed6f61ee5ffa04a447fab8903f10acc0
2023-12-07 13:23:06 +00:00
Pau Espin 6a2975c858 socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
This API will be used internally by osmo_sock_multiaddr_get_name_buf()
in a follow-up patch.
This API can also be used directly by user who wish to obtain a list of
local/remote IP addresses and port from an SCTP socket.

Related: SYS#6636
Related: OS#5581
Change-Id: I19d560ab4aadec18a4c0f94115675ec1d7ab14d7
2023-12-07 13:23:06 +00:00
Neels Hofmeyr d71331bc20 logging: fix nul octets in log output / use osmo_strbuf
Patch [1] merged few weeks ago, for yet unknown reasons, sprinkles nul
characters at seemingly randomly chosen log line ends.

Trying to figure out why that happens, i got tired of the unreadable
cruft, and decided to migrate the _output_buf() implementation to
osmo_strbuf first.

With osmo_strbuf in use and implementing 1:1 what the previous code did,
the odd nul octets have disappeared. So the bug was caused by unreadable
code.

[1] 11a416827d
Ia7de9d88aa5ac48ec0d5c1a931a89d21c02c5433
"logging: ensure ANSI color escape is sent in same line/before newline"

Related: OS#6284
Related: Ia7de9d88aa5ac48ec0d5c1a931a89d21c02c5433
Change-Id: Ib577a5e0d7450ce93ff21f37ba3262704cbf4752
2023-12-07 04:52:16 +01:00
Neels Hofmeyr d511a9d664 util: add osmo_strbuf macros to manipulate the strbuf tail
Upcoming patch adopts osmo_strbuf in logging.c, which sometimes needs to
steal and re-add trailing newline characters, and also needs to let
ctime_r() write to the buffer before updating the osmo_strbuf state.

Related: OS#6284
Related: Ib577a5e0d7450ce93ff21f37ba3262704cbf4752
Change-Id: I997707c328eab3ffa00a78fdb9a0a2cbe18404b4
2023-12-07 04:52:16 +01:00
Neels Hofmeyr 271baad068 util: add OSMO_STRBUF_REMAIN()
This code already exists twice, and upcoming patch will need this as
well in logging.c. Add a macro to remove the code dup.

Related: OS#6284
Related: Ib577a5e0d7450ce93ff21f37ba3262704cbf4752
Change-Id: I6f2991125882bff948708bbb4ae218f9f3d1e50c
2023-12-07 04:52:16 +01:00
Pau Espin cb5cec28ce socket: osmo_sock_init2_multiaddr2(): Apply params too if no OSMO_SOCK_F_BIND flag set
Those parameters are not related to binding and hence should be
applicable before binding. This allows a caller setting them while not
caring about explicit binding (OSMO_SOCK_F_BIND).
Until recently calling this function without OSMO_SOCK_F_BIND was not
really supported, so the previous placement setting these params in the
function didn't matter much. It does now.

Change-Id: Ia32510e8db1de0cc0dc36cebf8a94f09e44fda70
2023-12-06 13:37:31 +01:00
Pau Espin 1c797fca79 socket: Reimplement osmo_sock_init2_multiaddr()
This is an attempt to fix several downsides of current
osmo_sock_init2_multiaddr() API, mainly the requirement to pass an explicit
local address (!NULL). It also now works fine if OSMO_SOCK_F_BIND flag
is not used.

This reimplementation is based on the follwing logic:
- If caller passed family=AF_INET or family=AF_INET6, that same family
  is used and kernel will fail if something is wrong.
- If caller passes family=AF_UNSPEC, the function will try to find the
  required family to create the socket. The decision is taken on the
assumption that an AF_INET6 socket can handle both AF_INET6 and AF_INET
addresses (through v4v6 mapping). Hence, if any of the addresses in the
local or remote set of addresses resolves through getaddrinfo() to an
IPv6 address, then AF_INET6 is used; AF_INET is used otherwise.

Related: OS#6279
Change-Id: I2641fbaca6f477404b094dbc53c0c1a3dd3fd2fd
2023-12-06 13:36:48 +01:00
Pau Espin 5b4135cf63 tests/socket: Avoid keeping unneeded sockets open during next test cases
Change-Id: I59cde0843545428e2bc490cc9470b76cbd7f8393
2023-12-05 21:54:55 +00:00
Alexander Couzens 75cc2466b0 gsup: fix error log message
The error happened in auth info and not in PDP info

Change-Id: I37ac6b5d7d34ba2ab0f83bcc1701b86b1a245014
2023-12-05 18:36:29 +00:00
Vadim Yanitskiy 6587dd0abb soft_uart: implement modem status lines and flow control
Change-Id: I26b93ce76f2f6b6fbf017f2684312007db3c6d48
Related: OS#4396
2023-12-04 04:23:07 +07:00
Vadim Yanitskiy ffb8d5e026 soft_uart: osmo_soft_uart_tx_ubits(): return number of bits pulled
This is a partial revert of 0887188c6b.

We actually want to return number of bits pulled, because in the upcoming
commit implementing the flow control we want to be able to signal to the
caller that the buffer was not completely filled, but only partly.

Change-Id: I47a56f0fc36f2bc8f5a797d7fec64dfb56842388
Related: OS#4396
2023-12-04 03:17:34 +07:00
Vadim Yanitskiy 275c86e7ee soft_uart: improve doxygen documentation
Change-Id: I415a5e5392a7f91da4bf117d59694278779acc94
Related: OS#4396
2023-12-04 03:14:33 +07:00
Vadim Yanitskiy 2203db5b9a soft_uart: cosmetic: do not use 'osmo_' prefix for static symbols
Change-Id: I2b450b715dbfd0f8d6ddb4994ae0be3b890ed4fc
Related: OS#4396
2023-12-04 03:14:33 +07:00
Vadim Yanitskiy 811638c005 soft_uart: check Rx/Tx state once in osmo_soft_uart_{rx,tx}_ubits()
Check it once rather than doing this in a loop.  Return -EAGAIN if
Rx or Tx is not enabled when calling osmo_soft_uart_{rx,tx}_ubits().

This [theoretically] improves performance by reducing the number of
conditional statements in loops.  In the Tx path, this also prevents
calling the .tx_cb() when the transmitter is disabled, so that we
don't loose the application data.

Change-Id: I70f93b3655eb21c2323e451052c40cd305c016c8
Related: OS#4396
2023-12-03 02:19:33 +00:00
Vadim Yanitskiy 0887188c6b soft_uart: fix doxygen doc for osmo_soft_uart_tx_ubits()
Change-Id: I648db45bbde39c6b3d839c744a56d01a562e4129
Related: OS#4396
2023-12-03 02:19:33 +00:00
Vadim Yanitskiy 59afb8f14c soft_uart: add osmo_soft_uart_{get,set}_name()
Change-Id: Iabf29f42d876035481011fa8e8a51c0590fe63b8
Related: OS#4396
2023-12-03 02:19:33 +00:00
Daniel Willmann 84611881c9 osmo_io: Factor out and use common send function from backend
This handles reenqueuing a message on EAGAIN and incomplete write

Change-Id: I6da2653d32aedd0e7872be0cf90a841b56462e59
2023-11-30 14:27:39 +01:00
Harald Welte be3c38ca55 Add a GSM RLP decoder and encoder
This code implements a decoder and encoder for the RLP (Radio Link
Protocol) as used in the bearer channel of GSM CSD (Circuit Switched
Data).

Change-Id: I2d9bd8eb4f0cd0f72c436996767b199429596917
2023-11-29 14:35:36 +00:00
Andreas Eversberg 285f3693de Add flag to enable RTS based polling
RTS based polling in LAPDm code is disabled by default. Make libosmogsm
stay compatible with existing applications that do not use RTS based
polling.

This patch fixes the issue that LAPDM_ENT_F_POLLING_ONLY did enable RTS
based polling too, which breaks existing applications like older
versions of osmo-bts.

Change-Id: I2a75c192bbc24e85bfc1656b2be21cea7a92814a
2023-11-29 14:19:48 +01:00
Andreas Eversberg 5560c040bd Fix union abis_rsl_chan_nr and abis_rsl_link_id
All bit fields in a union will overlap in memory, as all elements in a
union do. To prevent the bit fields from overlapping, wrap them into a
packed structure.

Change-Id: I5ef5aa62be8310da4ebe9ea73ebbedcbcc2ba49c
2023-11-29 10:15:33 +00:00
Harald Welte d6e16decad libosmosim: Support Microsoft smart card discovery process
Our class/instruction tables (used mainly by simtrace cardem host
software) only contain support for those instructions permitted
in the related card specification.

Microsoft blindly tries CLA=0xCA with INS=0x00 which is not somethin
that the GSM SIM, ETSI UICC or 3GPP USIM specs specify, and which
hence results in log output like this:

DLINP DEBUG [0] <= osmo_st2_cardem_request_sw_tx(sw=6a88)
DLGLOBAL INFO => DATA: flags=0x01 (HDR ), 00 ca 7f 68 00
DLGLOBAL ERROR Unknown APDU case 0
DLGLOBAL FATAL Failed to recognize APDU, terminating

Let's adjust to microsoft and *always* support their instructions
no matter which osim_cla_ins_card_profile was used.

Special thanks to Eric Wild for pointing me to this unexpected
behaviour of PC/SC on modern Windows.

Change-Id: I424964c0afab643e6a5d7824d91c2c86b0d3f25b
Related: SYS#6617
2023-11-28 22:50:01 +01:00
Harald Welte 42602ec932 sim/class_tables: Prevent out-of-bounds access
The INS tables always must be declared with 256 elements, even
if they only use some of them.  We never know which random INS
code might be used to index them.

This was not discovered until now as the only known user of those
tables is always using the SIM/UICC/USIM table and not the plain
ISO7816 table.

Change-Id: I6739f469f6e84eed9aab403cc481ebff86df0181
2023-11-28 21:22:19 +00:00
Alexander Couzens 505f70552a gsup: add message type for osmo-epdg CEAI interface
The CEIA interface is an interface between osmo-epdg and
strongswan.
It is used by the osmo-epdg to synchronize state.

Related: OS#6091
Change-Id: I6f7c20340c99f94b1326a8a7dc99c86cf6a0dbc3
2023-11-28 17:05:22 +00:00
Andreas Eversberg ebdf3dcac7 coding: gsm0503_tch_{afs,ahs}_encode(): add ability to emit BFI
Similar to Change ID Iade3310e16b906efb6892d28f474a0d15204e861, add
BFI support to TCH AFS and TCH AHS encoder.

Every BTS needs to have some graceful handling for the scenario
where it is time to send out a speech frame on TCH DL, but there is
no frame to be sent. One possible solution is to transmit dummy
FACCH, but this option is unattractive for TCH/AHS where FACCH
displaces two speech frames rather than one. A more elegant solution
is to emit a speech frame with inverted CRC6, causing the MS receiver
to declare a BFI condition to trigger substitution and muting procedure.
Setting all u(k) bits to 0 is one way to produce such an inverted-CRC
speech frame (normal TCH FR/HR CRC6 for an all-zeros frame would be
111111). This patch adds the abiliy to gsm0503_tch_{afs,ahs}_encode()
functions, indicated by payload length of 0.

See §6.2 of 3GPP TS 26.191 for substitution and muting procedure.

Related: OS#6049
Change-Id: I82ce2adf995a4b42d1f378c5819f88d773b9104a
2023-11-27 16:27:32 +00:00
Andreas Eversberg 6cc5301e4c LAPD: Indicate sequence error after indicating received data
First indicated the received data in an I frame, if possible. Then
indicate the sequence error using MDL-ERROR-INDICATION. This way the
data is delivered before the error is handled by BSC.

Also there is no reason to indicate sequence error on supervisory
frames.

See §8.7.4 of 3GPP TS 44.006.

Related: OS#5968
Change-Id: I535c18018bf0df4124a5e9618238028fa31be289
2023-11-27 16:25:53 +00:00
Andreas Eversberg bd2b897b72 LAPDm: Add a flag to enable suppression of subsequent REJ frame
This behaviour was default in earlier versions of LAPDm/LAPD. Because it
is only required for osmocom-bb, a flag is added to enable it there.

Related: OS#5969
Change-Id: I93994dbbd1fc2c9edb8f3015c6b18ecd0fce0565
2023-11-27 16:25:53 +00:00
Andreas Eversberg f51f916e1c LAPDm: Add an extra queue for UI frames
The extra queue is used to transmit the UI frame only when there is no
frame in the regular TX queue. This allows to give LAPD frames prioity
over UI frame.

Related: OS#4074
Change-Id: I00c8ee73be8b7c564a4dee3fca3e893484f567da
2023-11-27 16:25:53 +00:00
Andreas Eversberg 2d7119d85b LAPDm: Add support for RTS based polling
The lower layer must set the 'POLLING_ONLY' flag and provide frame
number when polling a frame. If T200 is pending, it is started with a
timeout frame number in advance to given frame number.

The lower layer must call lapdm_t200_fn() after a frame has been
received or if a frame has not been received. Also it must be called
after a TCH frame has been received. LAPDm uses this to check the T200
timeout condition.

A new function is used to set the frame number based timeout values.

Related: OS#4074
Change-Id: I6ebe83f829d7751ea9de1d90eb478c7a628db64c
2023-11-27 16:25:53 +00:00
Andreas Eversberg 49b7087360 LAPD: Add support for RTS based polling and T200
The T200 timer is started when the current frame is polled at
PH-READY-TO-SEND event.

A flag is used to enable this feature. The user of LAPD core must track
frame numbers to check the timeout condition. Then it must call the
external timeout function.

Related: OS#4074
Change-Id: Ib961b5a44911b99b0487641533301749c0286995
2023-11-27 16:25:53 +00:00
Daniel Willmann 0266b531c5 Disable uring when building for embedded
Change-Id: Iec2503986c6d3487761ba592daef0fd42478aa7d
Related: OS#6233
2023-11-24 16:13:07 +00:00
Vadim Yanitskiy d4b94ceb88 soft_uart: fix spelling in doxygen docs
Change-Id: Ib719d1fe4ee6c058860e861c91ec2417d9dff0af
2023-11-24 03:00:02 +07:00
Harald Welte c380f29c0c osmo_io: Reject unknown/unsupported modes in osmo_iofd_setup()
The current code does not check the value range of the 'mode' parameter
and would later run into OSMO_ASSERT(), rather than rejecting such a
mode from the very beginning.

Change-Id: I10dd612487638f456d0ad59c2cca203f1e098da3
Related: OS#5751
2023-11-22 12:20:12 +00:00
Harald Welte 38d8170fa4 osmo_io: rename unsupported SCTP mode to OSMO_IO_FD_MODE_SCTP_RECVMSG_SEND
The two functions of the SCTP socket interface we use in osmo-* are
sctp_send() and sctp_recvmsg().  We do not use sctp_sendmsg() at all,
so let's make sure the mode is named correctly.

Change-Id: Ie2d1c7ce6f211dbe025a0e843ad733443102ea15
Related: OS#5751
2023-11-22 12:20:12 +00:00
Harald Welte 987a86af88 io_uring: add some more source code comments/docs
Change-Id: I8ba77a18b51f67a9edbd1fa488b9791f8bf6e40a
2023-11-22 12:20:12 +00:00
Harald Welte 641cc3c60d add new osmo_sockaddr_from_str_and_uint() function
The function is basically a shortcut for getaddrinfo with storing the
output data into our 'struct osmo_sockaddr'.

Change-Id: I6b5c0bf8ca97e6358d992fb2ff45ffd53ba15197
Related: SYS#6657
2023-11-22 12:18:26 +00:00