Commit Graph

19 Commits

Author SHA1 Message Date
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
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
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
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
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
Pau Espin e4f34d8d0a socket.c: Fix compilation with --disable-libsctp
Fixes: 64ba9edf17
Related: OS#6234
Change-Id: I988d01461822d18b7350a6e69e3b504f7fd5b84f
2023-10-23 09:58:50 +00:00
Pau Espin 64ba9edf17 socket: Introduce APIs osmo_sock_multiaddr_{add,del}_local_addr()
These APIs are used to bind or unbind an active socket adding or
removing addresses from the existing set.

Related: OS#6077
Change-Id: Ifc6e7d643c2a0c53f479bfd0d5c36d08c0c01953
2023-10-03 18:38:02 +02:00
Pau Espin a45b0be25f socket: Support setsokopt SCTP_INITMSG in osmo_sock_init2_multiaddr2()
This allows setting several socket paramets which can only be set before
SCTP INIT phase (connect()).

Since no release with osmo_sock_init2_multiaddr2() happened yet, we are
allowed to extend struct osmo_sock_init2_multiaddr_pars without bumping
the version field.

Related: SYS#6558
Change-Id: Ibc55932d954279927d1e70ccce1e8bf32b180d44
2023-09-08 13:49:59 +02:00
Pau Espin 658c509061 socket: Remove OSMO_SOCK_F_SCTP_ASCONF_SUPPORTED, add osmo_sock_init2_multiaddr2()
The OSMO_SOCK_F_SCTP_ASCONF_SUPPORTED flag was added recently to enable
use of ASCONF in osmo_sock_inti2_multiaddr. Since we didn't release yet,
we are still in time to get rid of this flag.

It turns out that we'll want to add more features which must be set
before SCTP INIT (connect()) time, like setsockopt SCTP_INITMSG, which
in turn contains a struct with several parameters.
Hence, adding flags for all those makes no sense.
Instead, add a new API which allows passing an extensible struct which
allows much more fine-grained setup from the caller.

The new struct "pars" parameter is non-const on purpose, in order to be
able to extend the struct in the future if we wish to get/obtain some
data from the socket.

Related: SYS#6501
Related: SYS#6558
Change-Id: I1f6fd09a79b0a2bd794e5669d933be25bbf1eeaa
2023-09-08 13:49:08 +02:00
Pau Espin b437f80e89 socket: Avoid aborting socket creation if setsockopt for ASCONF fails
Avoid aborting the entire socket creation, since those sockopt are fairly
new in the kernel (v5.4), and the feature can still be enabled in older
versions system-wide with sysctl. The worst it can happen is that the
ability to submit address updates (such as Peer Primary Address) doesn't
work, which is not really critical.

Related: SYS#6501
Change-Id: Iff261c8592b6b3c4237c90c84b4e8e921e3c4a65
2023-08-28 13:59:03 +02:00
Pau Espin e83227f735 socket: Add osmo_sock_init flag to enable SCTP ASCONF features
The SCTP_ASCONF_SUPPORTED sockopt, which enables ASCONF features
for the SCTP associations managed by the SCTP socket, gets its default
value from to sysctl "net.sctp.addip_enable", which, at least up to
current kernel 6.4.8 is set to "0" (disabled) by default.
As a result, ASCONF related features such as
setsockopt(SCTP_SET_PEER_PRIMARY_ADDR) fail with -EPERM
since ASNCONF extensions where not negotiated during the SCTP init
handshake.
This commit adds support for setting SCTP_ASCONF_SUPPORTED=1 during
socket creation, since that needs to be applied before the first
bind()/connect() (before first assoc is created).

Furthermore, for ASCONF features to work properly, the assoc also needs
to announce/use the AUTH extension, as per RFC5061 section 4.2.7.
Otherwise, the peer receiving an SCTP INIT with
ExtensionFeatures=ASCONF,ASCONF_ACK but without AUTH, will reject the
assoc with an ABORT since it's not complying with spec (this behavior
can be tweaked through sysctl "net.sctp.addip_noauth_enable").

The AUTH extension is enabled through sockopt SCTP_AUTH_SUPPORTED, and
its default value is that of sysctl "net.sctp.auth_enable", which is
also disabled (0) by default.

Kernel support for those is relatively new:
SCTP_FUTURE_ASSOC added in: 80df2704a375bb4b3c9c5cce9c00052361b16d61
Follows: v5.0-rc4
Precedes: v5.1-rc1

SCTP_ASCONF_SUPPORTED added in: df2c71ffdfae58961981d7cbcccea93688fc4e96
Follows: v5.3-rc5
Precedes: v5.4-rc1

SCTP_AUTH_SUPPORTED added in: 56dd525abd56f7acd7b44a52935726e3ada4916c
Follows: v5.3-rc5
Precedes: v5.4-rc1

Related: OS#6076
Change-Id: Iac07031927b66a9d32d2bb2faab817e4c922a359
2023-08-21 10:28:11 +02:00
Pau Espin c3b772b61e socket: Cache errno before calling further functions
No issue is in principle expected with previous code since snprintf
doesn't seem to be setting errno according to "man snprintf", but better
be safe, specially in case someone calls the functions calling snprintf
in the future, or if snprintf sets errno in other implementations.

Change-Id: I6969be10db867b4821e05aed50bc22a7ccd4a633
2023-07-12 08:05:25 +00:00
Daniel Willmann 7052cc6b3b socket: Ensure fd is not negative in osmo_sock_get_name_buf()
Write to str even in case of error because this is already the current
behaviour and it's what osmo_stream_cli_get_sockname() and
osmo_sock_get_name2{,_c}() expect.

Change-Id: I76727993224ef87b475c33360c24966e82e866ec
Fixes: Coverity CID#321044
2023-06-19 15:12:24 +00:00
Neels Hofmeyr 5761c44609 error log: osmo_sock_init2_multiaddr() v4/v6 mix
Provide some more information for the error case when local and remote
addresses mix v4 and v6.

Change-Id: I0fd12b08e0788ce3af6dc519ff8c82ad196a115f
2023-03-14 20:15:05 +00:00
Max db7cb69736 socket: propagate error in osmo_sock_unix_init() to the caller
Change-Id: Ia90d2ca3106b58dc5953d930f13df829d5b6966f
2023-02-15 14:02:50 +00:00
Pau Espin 88955fb550 Fix all references to config.h
config.h is created in $(top_buildir)/config.h.
Let's make sure all CPPFLAGS add correct -Ipath includes,
and that all code includes the correct file.

Change-Id: Ie9ea38bb009bc715b01cde4d66d181f7bec2e7bd
2023-01-18 19:04:36 +01:00
Pau Espin d4c3dc8d59 Move src/*.{c,h} to src/core/
This way we have all libosmocore.so in an own subdir instead of having
lots of files in the parent dir, which also contains subdirs to other
libraries.
This also matches the schema under include/osmocom/.

Change-Id: I6c76fafebdd5e961aed88bbecd2c16bc69d580e2
2023-01-18 17:14:06 +01:00