ss7: Split asp and xua_server into their own files

This allows having a clearer picture of the several entities involved,
as well as simplifying the files.

In the future, we can do the same for AS, route, etc. and leave osmo_ss7
for the "instance" object.

Change-Id: I3d43268459d61b0b9f9bec34bf31dc0851fa5e48
This commit is contained in:
Pau Espin 2023-09-07 12:46:49 +02:00
parent 08cb70e158
commit 0937875983
5 changed files with 1573 additions and 1449 deletions

View File

@ -31,7 +31,8 @@ libosmo_sigtran_la_SOURCES = sccp_sap.c sua.c m3ua.c xua_msg.c sccp_helpers.c \
sccp2sua.c sccp_scrc.c sccp_sclc.c sccp_scoc.c \
sccp_user.c sccp_types.c sccp_lbcs.c sccp_scmg.c \
xua_rkm.c xua_shared.c xua_default_lm_fsm.c \
osmo_ss7.c osmo_ss7_hmrt.c xua_asp_fsm.c xua_as_fsm.c \
osmo_ss7.c osmo_ss7_asp.c osmo_ss7_hmrt.c osmo_ss7_xua_srv.c \
xua_asp_fsm.c xua_as_fsm.c \
xua_snm.c osmo_ss7_vty.c sccp_vty.c ipa.c
libosmo_sigtran_la_LDFLAGS = -version-info $(LIBVERSION) -no-undefined -export-symbols-regex '^osmo_'
libosmo_sigtran_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) \

File diff suppressed because it is too large Load Diff

1246
src/osmo_ss7_asp.c Normal file

File diff suppressed because it is too large Load Diff

300
src/osmo_ss7_xua_srv.c Normal file
View File

@ -0,0 +1,300 @@
/* Core SS7 xUA Server */
/* (C) 2015-2017 by Harald Welte <laforge@gnumonks.org>
* (C) 2023 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
* All Rights Reserved
*
* SPDX-License-Identifier: GPL-2.0+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <inttypes.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <osmocom/sigtran/osmo_ss7.h>
#include <osmocom/sigtran/mtp_sap.h>
#include <osmocom/sigtran/protocol/mtp.h>
#include <osmocom/sigtran/protocol/sua.h>
#include <osmocom/sigtran/protocol/m3ua.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/select.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/sockaddr_str.h>
#include <osmocom/netif/stream.h>
#include <osmocom/netif/ipa.h>
#include <osmocom/netif/sctp.h>
#include "sccp_internal.h"
#include "xua_internal.h"
#include "ss7_internal.h"
#include "xua_asp_fsm.h"
#include "xua_as_fsm.h"
/***********************************************************************
* SS7 xUA Server
***********************************************************************/
/* server has accept()ed a new SCTP association, let's find the ASP for
* it (if any) */
static int xua_accept_cb(struct osmo_stream_srv_link *link, int fd)
{
struct osmo_xua_server *oxs = osmo_stream_srv_link_get_data(link);
struct osmo_stream_srv *srv;
struct osmo_ss7_asp *asp;
char *sock_name = osmo_sock_get_name(link, fd);
const char *proto_name = get_value_string(osmo_ss7_asp_protocol_vals, oxs->cfg.proto);
int rc = 0;
LOGP(DLSS7, LOGL_INFO, "%s: New %s connection accepted\n", sock_name, proto_name);
if (oxs->cfg.proto == OSMO_SS7_ASP_PROT_IPA) {
srv = osmo_stream_srv_create(oxs, link, fd,
ss7_asp_ipa_srv_conn_cb,
ss7_asp_xua_srv_conn_closed_cb, NULL);
} else {
srv = osmo_stream_srv_create(oxs, link, fd,
ss7_asp_xua_srv_conn_cb,
ss7_asp_xua_srv_conn_closed_cb, NULL);
}
if (!srv) {
LOGP(DLSS7, LOGL_ERROR, "%s: Unable to create stream server "
"for connection\n", sock_name);
close(fd);
talloc_free(sock_name);
return -1;
}
asp = ss7_asp_find_by_socket_addr(fd);
if (asp) {
LOGP(DLSS7, LOGL_INFO, "%s: matched connection to ASP %s\n",
sock_name, asp->cfg.name);
/* we need to check if we already have a socket associated, and close it. Otherwise it might
* happen that both the listen-fd for this accept() and the old socket are marked 'readable'
* during the same scheduling interval, and we're processing them in the "wrong" order, i.e.
* we first see the accept of the new fd before we see the close on the old fd */
if (asp->server) {
LOGPASP(asp, DLSS7, LOGL_FATAL, "accept of new connection from %s before old was closed "
"-> close old one\n", sock_name);
osmo_stream_srv_set_data(asp->server, NULL);
osmo_stream_srv_destroy(asp->server);
asp->server = NULL;
}
} else {
if (!oxs->cfg.accept_dyn_reg) {
LOGP(DLSS7, LOGL_NOTICE, "%s: %s connection without matching "
"ASP definition and no dynamic registration enabled, terminating\n",
sock_name, proto_name);
} else {
char namebuf[32];
static uint32_t dyn_asp_num = 0;
snprintf(namebuf, sizeof(namebuf), "asp-dyn-%u", dyn_asp_num++);
asp = osmo_ss7_asp_find_or_create(oxs->inst, namebuf, 0, 0,
oxs->cfg.proto);
if (asp) {
char hostbuf[INET6_ADDRSTRLEN];
const char *hostbuf_ptr = &hostbuf[0];
char portbuf[16];
osmo_sock_get_ip_and_port(fd, hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf), false);
LOGP(DLSS7, LOGL_INFO, "%s: created dynamic ASP %s\n",
sock_name, asp->cfg.name);
asp->cfg.is_server = true;
asp->cfg.role = OSMO_SS7_ASP_ROLE_SG;
asp->cfg.local.port = oxs->cfg.local.port;
asp->cfg.remote.port = atoi(portbuf);
asp->dyn_allocated = true;
asp->server = srv;
osmo_ss7_asp_peer_set_hosts(&asp->cfg.local, asp,
(const char * const*)oxs->cfg.local.host,
oxs->cfg.local.host_cnt);
osmo_ss7_asp_peer_set_hosts(&asp->cfg.remote, asp,
&hostbuf_ptr, 1);
osmo_ss7_asp_restart(asp);
}
}
if (!asp) {
osmo_stream_srv_destroy(srv);
talloc_free(sock_name);
return -1;
}
llist_add_tail(&asp->siblings, &oxs->asp_list);
}
/* update the ASP reference back to the server over which the
* connection came in */
asp->server = srv;
asp->xua_server = oxs;
/* update the ASP socket name */
talloc_free(asp->sock_name);
asp->sock_name = talloc_reparent(link, asp, sock_name);
osmo_stream_srv_set_name(asp->server, asp->cfg.name);
/* make sure the conn_cb() is called with the asp as private
* data */
osmo_stream_srv_set_data(srv, asp);
if (oxs->cfg.proto != OSMO_SS7_ASP_PROT_IPA) {
rc = ss7_asp_apply_peer_primary_address(asp);
rc = ss7_asp_apply_primary_address(asp);
}
/* send M-SCTP_ESTABLISH.ind to Layer Manager */
osmo_fsm_inst_dispatch(asp->fi, XUA_ASP_E_SCTP_EST_IND, 0);
xua_asp_send_xlm_prim_simple(asp, OSMO_XLM_PRIM_M_SCTP_ESTABLISH, PRIM_OP_INDICATION);
return rc;
}
/*! \brief create a new xUA server configured with given ip/port
* \param[in] ctx talloc allocation context
* \param[in] proto protocol (xUA variant) to use
* \param[in] local_port local SCTP port to bind/listen to
* \param[in] local_host local IP address to bind/listen to (optional)
* \returns callee-allocated \ref osmo_xua_server in case of success
*/
struct osmo_xua_server *
osmo_ss7_xua_server_create(struct osmo_ss7_instance *inst, enum osmo_ss7_asp_protocol proto,
uint16_t local_port, const char *local_host)
{
struct osmo_xua_server *oxs = talloc_zero(inst, struct osmo_xua_server);
OSMO_ASSERT(ss7_initialized);
if (!oxs)
return NULL;
LOGP(DLSS7, LOGL_INFO, "Creating %s Server %s:%u\n",
get_value_string(osmo_ss7_asp_protocol_vals, proto), local_host, local_port);
INIT_LLIST_HEAD(&oxs->asp_list);
oxs->cfg.proto = proto;
oxs->cfg.local.port = local_port;
oxs->server = osmo_stream_srv_link_create(oxs);
osmo_stream_srv_link_set_name(oxs->server, osmo_ss7_asp_protocol_name(proto));
osmo_stream_srv_link_set_data(oxs->server, oxs);
osmo_stream_srv_link_set_accept_cb(oxs->server, xua_accept_cb);
osmo_stream_srv_link_set_nodelay(oxs->server, true);
osmo_stream_srv_link_set_port(oxs->server, oxs->cfg.local.port);
osmo_stream_srv_link_set_proto(oxs->server, ss7_asp_proto_to_ip_proto(proto));
osmo_ss7_xua_server_set_local_host(oxs, local_host);
LOGP(DLSS7, LOGL_INFO, "Created %s server on %s:%" PRIu16 "\n",
get_value_string(osmo_ss7_asp_protocol_vals, proto), local_host, local_port);
oxs->inst = inst;
llist_add_tail(&oxs->list, &inst->xua_servers);
/* The SUA code internally needs SCCP to work */
if (proto == OSMO_SS7_ASP_PROT_SUA)
osmo_ss7_ensure_sccp(inst);
return oxs;
}
/*! \brief Set the xUA server to bind/listen to the currently configured ip/port
* \param[in] xs xUA server to operate
* \returns 0 on success, negative value on error.
*/
int
osmo_ss7_xua_server_bind(struct osmo_xua_server *xs)
{
char buf[512];
int rc;
const char *proto = get_value_string(osmo_ss7_asp_protocol_vals, xs->cfg.proto);
rc = osmo_ss7_asp_peer_snprintf(buf, sizeof(buf), &xs->cfg.local);
if (rc < 0) {
LOGP(DLSS7, LOGL_INFO, "Failed parsing %s Server osmo_ss7_asp_peer\n", proto);
} else {
LOGP(DLSS7, LOGL_INFO, "(Re)binding %s Server to %s\n",
proto, buf);
}
return osmo_stream_srv_link_open(xs->server);
}
int
osmo_ss7_xua_server_set_local_host(struct osmo_xua_server *xs, const char *local_host)
{
return osmo_ss7_xua_server_set_local_hosts(xs, &local_host, 1);
}
int
osmo_ss7_xua_server_set_local_hosts(struct osmo_xua_server *xs, const char **local_hosts, size_t local_host_cnt)
{
int rc;
OSMO_ASSERT(ss7_initialized);
rc = osmo_ss7_asp_peer_set_hosts(&xs->cfg.local, xs, local_hosts, local_host_cnt);
if (rc < 0)
return rc;
return osmo_stream_srv_link_set_addrs(xs->server, (const char **)xs->cfg.local.host, xs->cfg.local.host_cnt);
}
int
osmo_ss7_xua_server_add_local_host(struct osmo_xua_server *xs, const char *local_host)
{
int rc;
rc = osmo_ss7_asp_peer_add_host(&xs->cfg.local, xs, local_host);
if (rc < 0)
return rc;
return osmo_stream_srv_link_set_addrs(xs->server, (const char **)xs->cfg.local.host, xs->cfg.local.host_cnt);
}
bool ss7_xua_server_set_default_local_hosts(struct osmo_xua_server *oxs)
{
/* If no local addr was set, or erased after _create(): */
if (!oxs->cfg.local.host_cnt) {
/* "::" Covers both IPv4 and IPv6 */
if (ss7_ipv6_sctp_supported("::", true))
osmo_ss7_xua_server_set_local_host(oxs, "::");
else
osmo_ss7_xua_server_set_local_host(oxs, "0.0.0.0");
return true;
}
return false;
}
void osmo_ss7_xua_server_destroy(struct osmo_xua_server *xs)
{
struct osmo_ss7_asp *asp, *asp2;
if (xs->server) {
osmo_stream_srv_link_close(xs->server);
osmo_stream_srv_link_destroy(xs->server);
}
/* iterate and close all connections established in relation
* with this server */
llist_for_each_entry_safe(asp, asp2, &xs->asp_list, siblings)
osmo_ss7_asp_destroy(asp);
llist_del(&xs->list);
talloc_free(xs);
}

View File

@ -5,10 +5,31 @@
#include <stdbool.h>
#include <osmocom/sigtran/osmo_ss7.h>
extern bool ss7_initialized;
bool ss7_ipv6_sctp_supported(const char *host, bool bind);
struct osmo_ss7_asp *ss7_asp_alloc(struct osmo_ss7_instance *inst, const char *name,
uint16_t remote_port, uint16_t local_port,
enum osmo_ss7_asp_protocol proto);
bool ss7_asp_set_default_peer_hosts(struct osmo_ss7_asp *asp);
struct osmo_ss7_asp *ss7_asp_find_by_socket_addr(int fd);
int ss7_asp_proto_to_ip_proto(enum osmo_ss7_asp_protocol proto);
int ss7_asp_ipa_srv_conn_cb(struct osmo_stream_srv *conn);
int ss7_asp_xua_srv_conn_cb(struct osmo_stream_srv *conn);
int ss7_asp_xua_srv_conn_closed_cb(struct osmo_stream_srv *srv);
int ss7_asp_apply_peer_primary_address(const struct osmo_ss7_asp *asp);
int ss7_asp_apply_primary_address(const struct osmo_ss7_asp *asp);
bool ss7_xua_server_set_default_local_hosts(struct osmo_xua_server *oxs);
enum ss7_as_ctr {
SS7_AS_CTR_RX_MSU_TOTAL,
SS7_AS_CTR_TX_MSU_TOTAL,
};
enum ss7_asp_ctr {
SS7_ASP_CTR_PKT_RX_TOTAL,
SS7_ASP_CTR_PKT_RX_UNKNOWN,
SS7_ASP_CTR_PKT_TX_TOTAL,
};