Commit Graph

102 Commits

Author SHA1 Message Date
Neels Hofmeyr 1790f1763a follow-up to osmo_sockaddr_from/to_octets()
Forgot to apply one code review change before merge.

Related: OS#5599
Change-Id: Id202f3fda51bea9149ab3462a2efe35cf08d8030
2022-02-07 22:15:27 +01:00
Neels Hofmeyr cf7f7924d2 add osmo_sockaddr_from/to_octets()
Shorthand for the INET/INET6 switch() to get/put the addr part, useful
for encoding and decoding message buffers.

Related: OS#5599
Change-Id: Ie9e33bfac525c59c30714663d2bfcc62ec9eeb81
2022-02-07 22:13:20 +01:00
Neels Hofmeyr f3270f246f log: socket.c: rather use the osmo_sockaddr_str _FMT
The OSMO_SOCKADDR_STR_FMT() and _ARGS() macros properly place square
braces around IPv6 addresses, so that the port nr is clearly
distinguishable.

before: 1:2::3:4:5
after: [1:2::3:4]:5

When using a struct reference, the macro resolves to '(&sastr) ? .. : ..',
which the compiler complains about as "condition is always true". Shim
around that error with a pointer variable.

I considered using osmo_sockaddr_to_str_c() instead, but here in
socket.c we cannot assume that osmo_select_main_ctx() is being used and
hence can't just use OTC_SELECT for log string composition. The
struct osmo_sockaddr_str is a string representation in a local variable,
and hence doesn't need talloc for log strings.

I considered adding log_check_level() around the log string conversion,
but since all of these instances are on LOGL_ERROR, I didn't bother.

Related: SYS#5599
Change-Id: Idbe7582b2b7f14540919e911dad08af6d491f68f
2022-02-07 22:10:19 +01:00
Neels Hofmeyr 09d6574a45 add osmo_sockaddr_to_str_c(), osmo_sockaddr_to_str_buf2()
To easily log and print a sockaddr using OTC_SELECT, add
osmo_sockaddr_to_str_c().

Implement osmo_sockaddr_to_str_buf2() using osmo_strbuf, so that we can
return the chars_needed which osmo_sockaddr_to_str_c() uses.

From previous osmo_sockaddr_to_str_buf(), call
osmo_sockaddr_to_str_buf2() and return NULL if the buf_len was
insufficient, to mimick previous behavior. This makes it more
consistently returning NULL for insufficient buf_len, as shown in the
tweak that is needed in socket_test.c. Before osmo_sockaddr_to_str_buf()
would return a truncated port number, now it's all or NULL.

I will use osmo_sockaddr_to_str_c() in the new osmo-upf implementation.

Related: SYS#5599
Change-Id: I12771bf8a021e6785217b1faad03c09ec1cfef0e
2022-01-31 15:03:53 +00:00
Harald Welte 0b08d518f2 socket, select: Fix -Wsign-compare warnings
Change-Id: Idf6213e66e9bf609e930c6134292458a7d521871
2022-01-10 10:53:03 +01:00
Oliver Smith 04bfb7165b treewide: remove FSF address
Remove the paragraph about writing to the Free Software Foundation's
mailing address. The FSF has changed addresses in the past, and may do
so again. In 2021 this is not useful, let's rather have a bit less
boilerplate at the start of source files.

Change-Id: I5050285e75cf120407a1d883e99b3c4bcae8ffd7
2021-12-14 12:44:03 +01:00
Eric Wild 8ae40cbb91 gsmtap: allow 127.0.0.x local listeners
Even if not bound to a IF they just exist and work as expected, and make
distinguishing traffic for local setups easy.

Change-Id: I1043dfd8075f14481011f43db45c943e9320413c
2021-10-06 13:01:26 +00:00
Pau Espin 6fe865daae Make gcc 11.1.0 false positivies happy
After my system's gcc was upgraded, I get false positivies in a couple
places. Let's initialize those to make gcc happy.

"""
/git/libosmocore/src/socket.c: In function ‘osmo_sock_init’:
/git/libosmocore/src/socket.c:958:25: error: ‘sfd’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  958 |                         close(sfd);
      |                         ^~~~~~~~~~

/git/libosmocore/src/gsm/gsm48.c: In function ‘osmo_mobile_identity_decode’:
/git/libosmocore/src/gsm/gsm48.c:690:20: error: ‘str_size’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  690 |         if (rc < 1 || rc >= str_size) {
      |             ~~~~~~~^~~~~~~~~~~~~~~~~
/git/libosmocore/src/gsm/gsm48.c:679:22: error: ‘str’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  679 |                 rc = osmo_bcd2str(str, str_size, mi_data, 1, 1 + nibbles_len, allow_hex);
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""

Change-Id: I8aacfbc21e23f63a65e8baee3fd536a1fe1bdd8a
2021-07-16 16:01:30 +00:00
Harald Welte 24980baac9 osmo_sock_*_ofd(): Mark OSMO_FD_WRITE on non-blocking connect()
When we perform a non-blocking connect, the completion of the connect
will be signaled by marking the fd as WRITE-able.  So we should
automatically set OSMO_FD_WRITE to make sure the user gets notified
on completion.

Change-Id: I22964c5d5da849abcd97a900bd86ab5b4ada05da
2021-05-08 10:40:13 +00:00
Harald Welte 903e670c54 socket: IPv6 support for osmo_sock_set_dscp()
IPv6 has the analogous to DSCP: The "traffic class" field.

See https://tools.ietf.org/html/draft-itojun-ipv6-tclass-api-03

Change-Id: Ib31b977f67d60aa7f30ca4ab6eceba3d1d5eeee1
Related: SYS#5427
2021-04-28 14:39:19 +02:00
Harald Welte c545ff6f3e socket: QoS support for all our socket init functions
Every socket function that can be passed a 'flags' argument now
supports the following two additional macros that can be or-ed in
with the flags:
* OSMO_SOCK_F_DSCP(x) -- specify the IP DSCP of the socket
* OSMO_SOCK_F_PRIO(x) -- specify the priority of the socket

The existing osmo_sock_set_{dscp,priority}() functions are useful,
but  you cannot call them in between the socket creation and the
connect() operation when using our socket helpers.  This means that
the first packet sent will have the default DSCP/priority, and only
later packets would have the desired values.

When using the functionality introduced by this patch, we can ensure
that even the very first packet of e.g. a TCP or SCTP connect()
will have the correct DSCP/priority applied.

Change-Id: If22988735fe05e51226c6b091a5348dcf1208cdf
Related: SYS#5427
2021-04-28 13:15:20 +02:00
Harald Welte faf6b70b0e socket: reduce code duplication, introduce socket_helper_tail()
Common bits shared by various functions (currently setting
non-blocking) should not be copy+pasted around.

Change-Id: I95056940ddc26b65f63eedaeaab6882edaef6317
2021-04-28 13:15:20 +02:00
Harald Welte ecc0bd8d42 socket: Introduce osmo_sock_set_priority() helper function
In some situations we want to set the SO_PRIORITY socket option
to determine the in-kernel priority of packets generated by this
socket.

Change-Id: I89abffcd125e6d073338a5c6437b9433220e1823
Related: SYS#5427
2021-04-27 22:25:13 +02:00
Harald Welte ce53e03dc9 socket: Introduce osmo_sock_set_dscp() to set socket DSCP value
At least on Linux, sockets have a IP_TOS socket option that can be
configured to set the TOS.  However, TOS (of RFC791) was replaced
by the DSCP (of RFC2474) in 1998.

As the DCSP bits are only the upper 6 bits of the TOS bits, let's
introduce a helper to get, mask and set the DSCP values in the TOS
bits.

Related: OS#5136, SYS#5427
Change-Id: Ia4ba389a5b7e3e9d5f17a742a900d6fd68c08e40
2021-04-27 21:53:32 +02:00
Alexander Couzens 80788fac9e add osmo_sockaddr_to_str_buf/osmo_sockaddr_to_str
Add helper to format osmo_sockaddr into a string.

Change-Id: I917f25ebd1239eae5855d973ced15b93731e33a0
2020-10-12 15:13:43 +00:00
Vadim Yanitskiy def5a407c1 socket: make the arguments of osmo_sockaddr_cmp() const
Change-Id: Ibfdfdd40c52709b32ac934974cc78ee821fa83ba
2020-10-09 15:22:49 +00:00
Alexander Couzens 0f36421ba8 add osmo_sockaddr_local_ip() to determine the local address for a remote.
Similiar to osmo_sock_local_ip but for osmo_sockaddr.

Change-Id: I9cd2c5ceb28183e2fd2d28f9c9088c3fcac643d2
2020-09-07 16:22:21 +02:00
Alexander Couzens e4181ea8a2 socket: add osmo_sockaddr_cmp()
Compare two osmo_sockaddr.

Change-Id: I2d12ebae2710ffd17cf071e6ada0804e73f87dd6
2020-09-02 18:42:21 +02:00
Alexander Couzens 43957e69ca socket: introduce osmo_sock_init_osa & osmo_sock_init_osa_ofd
osmo_sock_init_osa() takes osmo_sockaddr* as local and remote endpoints
to setup a socket.

Change-Id: I1eece543e3241ef0e095eb63bb831f7c15a16794
2020-09-02 18:42:21 +02:00
Pau Espin d8cf52b993 socket: Fix bug in osmo_sock_init2(AF_UNSPEC) matching IP versions
See previous commit for a bug description.

Fixes: 2c962f5de1

Change-Id: I59bf4b4b3ed14766a5a5285923d1ffa9fc8b2294
2020-08-31 19:01:02 +02:00
Pau Espin 5cc4fe4706 socket: Add some osmo_sockaddr print helpers
These are APIs useful to inline in log calls.

Change-Id: Ie07a38b05b7888885dba4ae795e9f3d9a561543d
2020-08-31 12:52:09 +02:00
Pau Espin 7bda8549a4 socket: fix wrong ipv6 dst buf size passed in osmo_sock_local_ip
Change-Id: I13939fda9b3d642f9fa1413e59458aba0628a4a6
2020-08-31 11:21:30 +02:00
Pau Espin 81e7a6c859 socket: Fix stack-buffer-overflow in osmo_sock_local_ip()
On IPv6 sockets, getsockname() and inet_ntop() would act upon a
structure struct sockaddr_in.

First getsockname() would succeed but truncate the address, and later on
inet_ntop would read out of the scope of the structure.

Change-Id: If781d56680758a97643b1b38e78d3431ea649020
2020-08-31 08:50:42 +00:00
Pau Espin 308ab792a3 socket: Use AF_UNSPEC instead of PF_UNSPEC calling getaddrinfo
man getaddrinfo entry doesn't mention PF_UNSPEC, but AF_UNSPEC.

Change-Id: Icab676ff7ee9d813d7b7fe807fbe172512f5e397
2020-08-31 08:50:42 +00:00
Pau Espin 1a3d24e8a5 socket: Add support for AF_INET6 in osmo_sockaddr_to_str_and_uint()
Related: SYS#4915
Change-Id: I439c7fa52a3a30eebc3d35e78be7f1724fb69294
2020-08-31 08:50:42 +00:00
Pau Espin 4f463c5ccc socket: Allow binding to :: (IPv6) and connecting to IPv4-only on the remote
sctp_bindx() fails if passed both "0.0.0.0" and "::", only "::" must
be passed instead, which covers both.
As a result, it is fine in this case and makes sense having only IPv6
formatted IPs in the local side (which actually also includes all IPv4
ones in th system) and IPv4-only addresses on the remote side.

Change-Id: I0b590113e5def20edcbcb098426b19cd504eabff
2020-08-30 16:08:35 +00:00
Pau Espin 3119d47603 socker: Remove AI_ADDRCONFIG from getaddrinfo flags
The flag was added recently in cd133316cf,
and it is causing issues while running unit tests in environement like
OBS where probably there's no non-loopback address configured.

Change-Id: I47c31953f1db39fcd2870bde0b984057b8e7b4c4
2020-08-25 11:43:02 +00:00
Pau Espin 4541cf2b96 osmo_sock_inti2_multiaddr: Fix memleak and free uninitialized mem
Under some specific cases, res_loc could be leaked. Under some others,
res_loc and res_rem were freed without being initialized previously.

Fixes: CID#212863
Fixes: CID#212861
Change-Id: Id9c4eda6fd1172e7324aa23c81e8658967a8dd0b
2020-08-25 11:43:02 +00:00
Pau Espin ed42a88900 osmo_sock_get_ip_and_port(): Support IPv6 sockets
Change-Id: I255a71cf047fd022285fc9aa2f24e5070aa3920b
2020-08-24 08:29:19 +00:00
Pau Espin ba828c30a0 socket: Log proper getaddrinfo() error
Drop one of the two log lines since it would be a duplicated and anyway
it misses context (return code).

Change-Id: I4620bf86f4bcda58ae85209278ac9ae9a0f3012a
2020-08-24 08:29:19 +00:00
Pau Espin cd133316cf socket: multiaddr: Support IPv4 + IPv6 addresses in SCTP associations
The function is improved to support AF_INET:v4->v4, AF_INET6:v6->v6 and
AF_UNSPEC:v4+v6->v4+v6.
Unit tests for the function are added to make sure function behaves
correctly in several scenarios.

Change-Id: I36d8ab85d92bba4d6adb83bc1875eb61094ed2ef
2020-08-24 08:29:19 +00:00
Pau Espin 796c651372 sock: osmo_sock_init2_multiaddr: decouple addr resolution from socket creation
Address resolution is done first and once we have the information, it
proceeds to create the socket. This separation in steps will help when
adding support for IPv6+IPv4 sets, where AF_UNSPEC is passed and created
socket needs to be AF_INET6 in order to handle addr of both versions.

Change-Id: I03147e3033a0c1fd04c9ac61d2ffbd78a1bb784a
2020-08-24 08:29:19 +00:00
Alexander Couzens 2c962f5de1 osmo_sock_init2: improve support for AF_UNSPEC
osmo_sock_init2 abstract two calls of getaddrinfo into one.
While there aren't problems with AF_INET or AF_INET6. When using
AF_UNSPEC there are corner cases when this fails. E.g. calling
local_host with "" and remote_host with an IPv6 only address results in
setting up a local socket with AF_INET while trying to connect from there towards
AF_INET6 will most likely fail.
To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site
supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6

Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766
2020-08-06 16:41:20 +00:00
Alexander Couzens b20d852a95 socket: osmo_sock_local_ip: correct doxygen comment
There is no parameter fd.

Change-Id: I1e31fcbf40d8abf99214b87e70e867b5ab66bd75
2020-07-28 13:58:48 +02:00
Harald Welte 44b9926c84 socket: Add osmo_sock_mcast_iface_set() to bind multicast to device
Change-Id: Ib52d22710020b56965aefcef09bde8247ace4a9c
Related: OS#2966
2020-03-10 19:08:51 +01:00
Pau Espin 8fac511a6e socket.c: build multiaddr socket API helpers only if used by public APIs
Those two functions are only used by osmo_sock_init2_multiaddr(), which
is only built if HAVE_LIBSCTP is defined. Avoid compiler warning about
unusued function helpers if osmo_sock_init2_multiaddr() is not being
built.

Change-Id: I52769d6b8f70af1a8bda23d60b3230a932e71fab
2019-10-24 15:39:27 +02:00
Pau Espin 272dfc1f02 socket: Remove unneeded condition check in osmo_sock_init2_multiaddr()
Since we return error at the start of the function if proto !=
IPPROTO_SCTP, it makes no sense to check for proto != IPPROTO_UDP later
on.

Fixes: CID#205088
Change-Id: Ibba7eacaa9debb77d536d47dc85170c5ee79e479
2019-10-21 11:12:54 +02:00
Pau Espin 3f464fc007 socket: Introduce API osmo_sock_init2_multiaddr()
This API will be used by libosmo-netif's osmo_stream for SCTP sockets,
which in turn will be used by libosmo-sccp to support multi-homed
connections.

Related: OS#3608
Change-Id: Ic8681d9e093216c99c6bca4be81c31ef83688ed1
2019-10-18 09:21:48 +00:00
Pau Espin ff42852d22 socket.c: Move glibc workarounds to same place in addrinfo_helper()
Change-Id: Ifc3a30881f865f88bcfc1307a3c89c1ab79eecd4
2019-10-10 17:41:27 +02:00
Eric Wild eb5769b8b3 tell ubsan to ignore SUN_LEN
ubsan will report undefined behavior due to the SUN_LEN macros interaction with a null pointer,
so let's tell ubsan to ignore this function. After carefully reviewing the final publically
availlable drafts of the C99,C11 and C18 standards I can confirm that dereferencing null pointers
is still undefined behavior, as such ubsan will always warn with absolutely every existing compiler
version. Since the sanitizers are periodically synced between llvm and gcc I'm also fairly confident
that rebuilding everything with compiler_rt to use the integrated sanitizers would result in the same message.
I sincerly hope that this explanation provides to be sufficient, If not I'd be willing to show up at
the next llvm dev meeting to provide quotes from actual sanitizer developers to back up these claims.

Change-Id: I0ff445072f1b46390c9f70b21d61c789e39358d5
2019-07-21 15:49:58 +00:00
Harald Welte 171ef826e1 make all library-internal static buffers thread-local
We have a number of library-internal static global buffers which are
mainly used for various stringification functions.  This worked as
all of the related Osmocom programs were strictly single-threaded.

Let's make those buffers at least thread-local.  This way every thread
gets their own set of buffers, and it's safe for multiple threads to
execute the same functions once.  They're of course still not
re-entrant.  If you need re-entrancy, you will need to use the _c()
or _buf() suffix version of those functions and work with your own
(stack or heap) buffers.

Change-Id: I50eb2436a7c1261d79a9d2955584dce92780ca07
2019-06-04 10:47:30 +02:00
Vadim Yanitskiy b606d76813 socket.c: fix Doxygen doc for osmo_sock_unix_init_ofd()
One typo s/optionall/optionally/, and a few incorrect references.

Change-Id: Iab42aa376b5cf4cf36413fede46e001c6b2d1525
2019-06-01 19:03:07 +07:00
Harald Welte ea9ea52bbb osmo_sock_get_name_*: Ensure string is returned in error case
osmo_sock_get_name_buf():
In case the getsockname() call is failing for some weird reason,
we shouldn't return an uninitialized, non-zero-terminated string
buffer to the caller, as most callers will be too lazy to test the
return value.

This holds even more true for users of the internal
osmo_sock_get_name2() and osmo_sock_get_name2_c() functions which indeed
very much ignore the return value of osmo_sock_get_name_buf().

Change-Id: I2d56327e96b7a6783cca38b828c5ee74aed776ae
2019-05-10 13:36:08 +02:00
Vadim Yanitskiy 4f619c202c Fix incorrect buffer size calculation
Calling sizeof() on a pointer to dynamically allocated memory would
result in getting size of the pointer (usually 4 or 8 bytes) itself,
but not the size of allocated memory.

Change-Id: I8ffda4dea2b7f9b4b76dfeecad1fab6384c5a62c
Fixes: CID#197629, CID#197628, CID#197627
Fixes: CID#197626, CID#197625, CID#197624
2019-04-12 21:48:11 +07:00
Harald Welte 179f35702e Add _c versions of functions that otherwise return static buffers
We have a habit of returning static buffers from some functions,
particularly when generating some kind of string values.  This is
convenient in terms of memory management, but it comes at the expense
of not being thread-safe, and not allowing for two calls of the
related function within one printf() statement.

Let's introduce _c suffix versions of those functions where the
caller passes in a talloc context from which the output buffer shall
be allocated.

Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b
2019-04-10 22:42:32 +00:00
Harald Welte 1688699c3f select: Rename BSC_FD_* constants to OSMO_FD_*
The naming of these constants dates back to when the code was private
within OpenBSC.  Everything else was renamed (bsc_fd -> osmo_fd) at
the time, but somehow the BSC_FD_* defines have been missed at the
time.

Keep compatibility #defines around, but allow us to migrate the
applications to a less confusing naming meanwhile.

Change-Id: Ifae33ed61a7cf0ae54ad487399e7dd2489986436
2019-03-21 16:02:01 +00:00
Harald Welte c0dfc9d885 socket: osmo_sock_get_name() Use "const void *" as talloc context
Change-Id: Ie6877277cddb0a9e049449c260afe3314ba65050
2019-03-19 14:04:00 +00:00
Philipp Maier 64b51eb68b socket: add define for socket name length
The function osmo_sock_get_name_buf() can be used to write a string
representation to a user provided memory. Unfortunately the proper
length for the user provided memory is not obvious. To make using
osmo_sock_get_name_buf() more practical, add a define constant that
defines the length of the required memory. Also use this define in
socket.c.

Change-Id: If8be8c2c0d4935da17ab13b2c2127b719ceefbcc
2019-01-17 17:46:08 +01:00
Neels Hofmeyr 0145751a97 add to osmo_sock_get_name*() API
Basically, I am applying code review that I would have given had I not been on
vacation when the last osmo_sock_get_name* stuff was merged.

osmo_sock_get_name2() is so far a static internal function. However, it is
nothing like osmo_sock_get_name(), so instead rename it to
osmo_sock_get_ip_and_port(). Also make it public API, no need to hide it.  I'm
adding an "and" in the name to hopefully clarify: "ip_port" vs. "ip_and_port"
-- there already are _get_X_ip_port() functions that only return the port
string, despite "ip" in the name.

Add new public osmo_sock_get_name2(), which is like osmo_sock_get_name(),
except it uses a static string instead of talloc, and omits the braces. This
is most convenient for log statement formats, avoiding dyn allocations.

Add new osmo_sock_get_name_buf(), which is like osmo_sock_get_name2() but
writes to a caller provided char buffer.

Use osmo_sock_get_name_buf() in the implementation of osmo_sock_get_name(),
but use another (non-static) local string buffer, because adding braces is too
complex without talloc_snprintf().

Rationale:

I want to improve the logging of socket errors, e.g. change

  DLMGCP ERROR Failed to read: 111/Connection refused (mgcp_client.c:720)

to

  DLMGCP ERROR Failed to read: r=10.0.99.2:2427<->l=10.0.99.2:2728: 111='Connection refused' (mgcp_client.c:721)

but it is just not handy to compose logging with the current API:

- osmo_sock_get_name() requires a talloc_free().
- all the others require output buffers.
- the only way to conveniently compose a logging string and,
- notably, the only trivial way to skip the string composition if the logging
  level is currently muted, is to have a function that returns a static string:
  the new osmo_sock_get_name2().
- (I think the osmo_sock_get_{local,remote}_* convenience wrappers should never
  have been added, because they encourage the caller to invoke the same code
  twice, for IP addr and port, and throw away one half each time.)

Related: Iae728192f499330d16836d9435648f6b8ed213b6 (osmo-mgw)
Change-Id: I8ad89ac447c9c582742e70d082072bdd40a5a398
2018-12-19 03:25:55 +01:00
Max 9d7a247da4 Add osmo_sockaddr_in_to_str_and_uint()
It's similar to osmo_sockaddr_to_str_and_uint() but does not require odd
typecasting for AF_INET case. Make osmo_sockaddr_to_str_and_uint() into
wrapper around new function and make sure to check for address family
before typecasting. Also use proper return type.

Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
2018-12-07 10:33:29 +00:00