m3ua: Move find_as_for_asp() to shared xua_find_as_for_asp()

This way the function can be re-used by SUA.

Change-Id: I0dfc5a7a24dd068002e837dc47eb0778c503cac5
This commit is contained in:
Harald Welte 2021-02-07 13:12:32 +01:00
parent aabb38a77b
commit a0228a74e0
4 changed files with 108 additions and 56 deletions

View File

@ -29,7 +29,7 @@ LIBVERSION=5:0:0
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 xua_rkm.c xua_default_lm_fsm.c \
sccp_user.c sccp_types.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_vty.c sccp_vty.c ipa.c
libosmo_sigtran_la_LDFLAGS = -version-info $(LIBVERSION) -no-undefined -export-symbols-regex '^osmo_'

View File

@ -532,60 +532,6 @@ struct m3ua_data_hdr *data_hdr_from_m3ua(struct xua_msg *xua)
return data_hdr;
}
/* if given ASP only has one AS, return that AS */
static struct osmo_ss7_as *find_single_as_for_asp(const struct osmo_ss7_asp *asp)
{
struct osmo_ss7_as *as, *as_found = NULL;
llist_for_each_entry(as, &asp->inst->as_list, list) {
if (!osmo_ss7_as_has_asp(as, asp))
continue;
/* check if we already had found another AS within this ASP -> not unique */
if (as_found)
return NULL;
as_found = as;
}
return as_found;
}
static int find_as_for_asp(struct osmo_ss7_as **as, const struct osmo_ss7_asp *asp,
const struct xua_msg_part *rctx_ie)
{
*as = NULL;
if (rctx_ie) {
uint32_t rctx = xua_msg_part_get_u32(rctx_ie);
/* Use routing context IE to look up the AS for which the
* message was received. */
*as = osmo_ss7_as_find_by_rctx(asp->inst, rctx);
if (!*as) {
LOGPASP(asp, DLM3UA, LOGL_ERROR, "%s(): invalid routing context: %u\n",
__func__, rctx);
return M3UA_ERR_INVAL_ROUT_CTX;
}
/* Verify that this ASP is part of the AS. */
if (!osmo_ss7_as_has_asp(*as, asp)) {
LOGPASP(asp, DLM3UA, LOGL_ERROR,
"%s(): This Application Server Process is not part of the AS %s "
"resolved by routing context %u\n", __func__, (*as)->cfg.name, rctx);
return M3UA_ERR_NO_CONFGD_AS_FOR_ASP;
}
} else {
/* no explicit routing context; this only works if there is only one AS in the ASP */
*as = find_single_as_for_asp(asp);
if (!*as) {
LOGPASP(asp, DLM3UA, LOGL_ERROR,
"%s(): ASP sent M3UA without Routing Context IE but unable to uniquely "
"identify the AS for this message\n", __func__);
return M3UA_ERR_INVAL_ROUT_CTX;
}
}
return 0;
}
static int m3ua_rx_xfer(struct osmo_ss7_asp *asp, struct xua_msg *xua)
{
struct xua_msg_part *rctx_ie = xua_msg_find_tag(xua, M3UA_IEI_ROUTE_CTX);
@ -603,7 +549,7 @@ static int m3ua_rx_xfer(struct osmo_ss7_asp *asp, struct xua_msg *xua)
return M3UA_ERR_UNSUPP_MSG_TYPE;
}
rc = find_as_for_asp(&as, asp, rctx_ie);
rc = xua_find_as_for_asp(&as, asp, rctx_ie);
if (rc)
return rc;

View File

@ -69,6 +69,8 @@ extern const struct value_string m3ua_rkm_dereg_status_vals[];
int xua_as_transmit_msg(struct osmo_ss7_as *as, struct msgb *msg);
int xua_find_as_for_asp(struct osmo_ss7_as **as, const struct osmo_ss7_asp *asp,
const struct xua_msg_part *rctx_ie);
int ipa_tx_xua_as(struct osmo_ss7_as *as, struct xua_msg *xua);
int ipa_rx_msg(struct osmo_ss7_asp *asp, struct msgb *msg);

104
src/xua_shared.c Normal file
View File

@ -0,0 +1,104 @@
/* Shared code between M3UA and SUA implementation */
/* (C) 2015-2021 by Harald Welte <laforge@gnumonks.org>
* 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 <stdint.h>
#include <unistd.h>
#include <string.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/logging.h>
#include <osmocom/sigtran/xua_msg.h>
#include <osmocom/sigtran/osmo_ss7.h>
#include <osmocom/sigtran/protocol/m3ua.h>
#include <osmocom/sigtran/protocol/sua.h>
#include "xua_internal.h"
/* if given ASP only has one AS, return that AS */
static struct osmo_ss7_as *find_single_as_for_asp(const struct osmo_ss7_asp *asp)
{
struct osmo_ss7_as *as, *as_found = NULL;
llist_for_each_entry(as, &asp->inst->as_list, list) {
if (!osmo_ss7_as_has_asp(as, asp))
continue;
/* check if we already had found another AS within this ASP -> not unique */
if (as_found)
return NULL;
as_found = as;
}
return as_found;
}
/* this is why we can use the M3UA constants below in a function shared between M3UA + SUA */
osmo_static_assert(M3UA_ERR_INVAL_ROUT_CTX == SUA_ERR_INVAL_ROUT_CTX, _err_rctx);
osmo_static_assert(M3UA_ERR_NO_CONFGD_AS_FOR_ASP == SUA_ERR_NO_CONFGD_AS_FOR_ASP, _err_as_for_asp);
/*! Find the AS for given ASP + optional routing context IE.
* if rctx_ie == NULL, we assume that this ASP is only part of a single AS;
* if rctx_ie is given, then we look-up the ASP based on the routing context,
* and verify that this ASP is part of it.
* \param[out] as caller-provided address-of-pointer to store the found AS
* \param[in] asp ASP for which we want to look-up the AS
* \param[in] rctx_ie routing context IE (may be NULL) to use for look-up
* \returns 0 in case of success; {M3UA,SUA}_ERR_* code in case of error. */
int xua_find_as_for_asp(struct osmo_ss7_as **as, const struct osmo_ss7_asp *asp,
const struct xua_msg_part *rctx_ie)
{
int log_ss = osmo_ss7_asp_get_log_subsys(asp);
*as = NULL;
if (rctx_ie) {
uint32_t rctx = xua_msg_part_get_u32(rctx_ie);
/* Use routing context IE to look up the AS for which the
* message was received. */
*as = osmo_ss7_as_find_by_rctx(asp->inst, rctx);
if (!*as) {
LOGPASP(asp, log_ss, LOGL_ERROR, "%s(): invalid routing context: %u\n",
__func__, rctx);
return M3UA_ERR_INVAL_ROUT_CTX;
}
/* Verify that this ASP is part of the AS. */
if (!osmo_ss7_as_has_asp(*as, asp)) {
LOGPASP(asp, log_ss, LOGL_ERROR,
"%s(): This Application Server Process is not part of the AS %s "
"resolved by routing context %u\n", __func__, (*as)->cfg.name, rctx);
return M3UA_ERR_NO_CONFGD_AS_FOR_ASP;
}
} else {
/* no explicit routing context; this only works if there is only one AS in the ASP */
*as = find_single_as_for_asp(asp);
if (!*as) {
LOGPASP(asp, log_ss, LOGL_ERROR,
"%s(): ASP sent M3UA without Routing Context IE but unable to uniquely "
"identify the AS for this message\n", __func__);
return M3UA_ERR_INVAL_ROUT_CTX;
}
}
return 0;
}