2021-12-03 08:25:49 +00:00
|
|
|
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
|
|
|
|
* Author: Oliver Smith <osmith@sysmocom.de>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/lienses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/fsm.h>
|
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
|
2022-01-19 09:51:27 +00:00
|
|
|
#include <osmocom/gsm/gsm0808.h>
|
|
|
|
|
2021-12-03 08:25:49 +00:00
|
|
|
#include <osmocom/sigtran/osmo_ss7.h>
|
2022-01-19 09:51:27 +00:00
|
|
|
#include <osmocom/sigtran/sccp_helpers.h>
|
2021-12-03 08:25:49 +00:00
|
|
|
|
|
|
|
#include <osmocom/bsc_nat/bsc_nat.h>
|
|
|
|
#include <osmocom/bsc_nat/bsc_nat_fsm.h>
|
|
|
|
#include <osmocom/bsc_nat/logging.h>
|
|
|
|
|
|
|
|
#define DEFAULT_PC_RAN "0.23.1" /* same as default for OsmoMSC */
|
|
|
|
#define DEFAULT_PC_CN "0.23.3" /* same as default for OsmoBSC */
|
|
|
|
|
2022-01-19 09:51:27 +00:00
|
|
|
static char log_buf[255];
|
|
|
|
|
2022-02-18 10:26:11 +00:00
|
|
|
#define LOG_SCCP(ss7_inst, peer_addr_in, level, fmt, args...) do { \
|
|
|
|
if (peer_addr_in) \
|
|
|
|
osmo_sccp_addr_to_str_buf(log_buf, sizeof(log_buf), NULL, peer_addr_in); \
|
2022-01-19 09:51:27 +00:00
|
|
|
LOGP(DMAIN, level, "(%s%s%s) " fmt, \
|
2022-02-18 10:26:11 +00:00
|
|
|
peer_addr_in ? log_buf : "", \
|
|
|
|
peer_addr_in ? " from " : "", \
|
2022-01-19 09:51:27 +00:00
|
|
|
ss7_inst == g_bsc_nat->ran ? "RAN" : "CN", \
|
|
|
|
## args); \
|
|
|
|
} while (0)
|
|
|
|
|
2021-12-03 08:25:49 +00:00
|
|
|
#define X(s) (1 << (s))
|
|
|
|
|
|
|
|
enum bsc_nat_fsm_states {
|
|
|
|
BSC_NAT_FSM_ST_STOPPED,
|
|
|
|
BSC_NAT_FSM_ST_STARTING,
|
|
|
|
BSC_NAT_FSM_ST_STARTED,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum bsc_nat_fsm_events {
|
|
|
|
BSC_NAT_FSM_EV_START,
|
|
|
|
BSC_NAT_FSM_EV_STOP,
|
|
|
|
};
|
|
|
|
|
2022-01-19 09:51:27 +00:00
|
|
|
static struct bsc_nat_ss7_inst *ss7_inst_dest(struct bsc_nat_ss7_inst *src)
|
2021-12-03 08:25:49 +00:00
|
|
|
{
|
2022-01-19 09:51:27 +00:00
|
|
|
if (src == g_bsc_nat->cn)
|
|
|
|
return g_bsc_nat->ran;
|
|
|
|
return g_bsc_nat->cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sccp_sap_up(struct osmo_prim_hdr *oph, void *scu)
|
|
|
|
{
|
|
|
|
struct bsc_nat_ss7_inst *src = osmo_sccp_user_get_priv(scu);
|
|
|
|
struct bsc_nat_ss7_inst *dest = ss7_inst_dest(src);
|
|
|
|
struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
|
2022-02-18 10:26:11 +00:00
|
|
|
struct osmo_sccp_addr *peer_addr_in;
|
|
|
|
struct osmo_sccp_addr peer_addr_out;
|
2022-01-19 09:51:27 +00:00
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
switch (OSMO_PRIM_HDR(oph)) {
|
|
|
|
case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
|
|
|
|
/* indication of new inbound connection request */
|
2022-02-18 10:26:11 +00:00
|
|
|
peer_addr_in = &prim->u.connect.calling_addr;
|
|
|
|
LOG_SCCP(src, peer_addr_in, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
|
2022-01-19 09:51:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
|
|
|
|
/* connection-oriented data received */
|
|
|
|
LOG_SCCP(src, NULL, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
|
|
|
|
/* indication of disconnect */
|
|
|
|
LOG_SCCP(src, NULL, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
|
|
|
|
/* connection-less data received */
|
2022-02-18 10:26:11 +00:00
|
|
|
peer_addr_in = &prim->u.unitdata.calling_addr;
|
|
|
|
LOG_SCCP(src, peer_addr_in, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
|
2022-01-19 09:51:27 +00:00
|
|
|
|
|
|
|
/* Figure out called party in dest. TODO: build and use a
|
|
|
|
* mapping of peer_addr + conn_id <--> dest_ss7. For now, this
|
|
|
|
* is simplified by assuming there is only one MSC, one BSC. */
|
|
|
|
|
|
|
|
struct osmo_ss7_instance *dest_ss7;
|
|
|
|
|
|
|
|
dest_ss7 = osmo_ss7_instance_find(dest->ss7_id);
|
|
|
|
OSMO_ASSERT(dest_ss7);
|
|
|
|
|
|
|
|
if (src == g_bsc_nat->ran) {
|
2022-02-18 10:26:11 +00:00
|
|
|
if (osmo_sccp_addr_by_name_local(&peer_addr_out, "msc", dest_ss7) < 0) {
|
|
|
|
LOG_SCCP(src, peer_addr_in, LOGL_ERROR, "Could not find MSC in address book\n");
|
2022-01-19 09:51:27 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-18 10:26:11 +00:00
|
|
|
if (osmo_sccp_addr_by_name_local(&peer_addr_out, "bsc", dest_ss7) < 0) {
|
|
|
|
LOG_SCCP(src, peer_addr_in, LOGL_ERROR, "Could not find BSC in address book\n");
|
2022-01-19 09:51:27 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 10:26:11 +00:00
|
|
|
LOG_SCCP(src, peer_addr_in, LOGL_NOTICE, "Forwarding to %s in %s\n",
|
|
|
|
osmo_sccp_inst_addr_name(NULL, &peer_addr_out),
|
2022-01-19 09:51:27 +00:00
|
|
|
dest == g_bsc_nat->ran ? "RAN" : "CN");
|
|
|
|
|
|
|
|
/* oph->msg stores oph and unitdata msg. Move oph->msg->data to
|
|
|
|
* unitdata msg and send it again. */
|
|
|
|
msgb_pull_to_l2(oph->msg);
|
2022-02-18 10:26:11 +00:00
|
|
|
osmo_sccp_tx_unitdata(dest->scu, &dest->local_sccp_addr, &peer_addr_out, oph->msg->data,
|
2022-01-19 09:51:27 +00:00
|
|
|
msgb_length(oph->msg));
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-02-17 16:57:45 +00:00
|
|
|
LOG_SCCP(src, NULL, LOGL_ERROR, "%s(%s) is not implemented!\n", __func__, osmo_scu_prim_name(oph));
|
2022-01-19 09:51:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-12-03 08:25:49 +00:00
|
|
|
|
2022-01-19 09:51:27 +00:00
|
|
|
error:
|
|
|
|
msgb_free(oph->msg);
|
|
|
|
return rc;
|
2021-12-03 08:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ss7_inst_init(struct bsc_nat_ss7_inst *inst, const char *name, const char *default_pc_str,
|
|
|
|
enum osmo_sccp_ssn ssn)
|
|
|
|
{
|
|
|
|
int default_pc;
|
|
|
|
struct osmo_sccp_instance *sccp;
|
|
|
|
|
|
|
|
default_pc = osmo_ss7_pointcode_parse(NULL, default_pc_str);
|
|
|
|
OSMO_ASSERT(default_pc >= 0);
|
|
|
|
|
|
|
|
sccp = osmo_sccp_simple_client_on_ss7_id(inst, inst->ss7_id, name, default_pc, OSMO_SS7_ASP_PROT_M3UA, 0, NULL,
|
|
|
|
0, NULL);
|
|
|
|
if (!sccp) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "%s: failed to request sccp client instance for sccp user\n", name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
osmo_sccp_local_addr_by_instance(&inst->local_sccp_addr, sccp, ssn);
|
|
|
|
|
|
|
|
inst->scu = osmo_sccp_user_bind(sccp, name, sccp_sap_up, ssn);
|
|
|
|
if (!inst->scu) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "%s: failed to bind sccp user\n", name);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
osmo_sccp_user_set_priv(inst->scu, inst);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ss7_inst_free(struct bsc_nat_ss7_inst *inst)
|
|
|
|
{
|
|
|
|
if (inst->scu) {
|
|
|
|
osmo_sccp_user_unbind(inst->scu);
|
|
|
|
inst->scu = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct osmo_ss7_instance *ss7 = osmo_ss7_instance_find(inst->ss7_id);
|
|
|
|
if (ss7)
|
|
|
|
osmo_ss7_instance_destroy(ss7);
|
|
|
|
|
|
|
|
talloc_free(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void st_starting_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
|
|
|
|
{
|
|
|
|
struct bsc_nat *bsc_nat = (struct bsc_nat *)fi->priv;
|
|
|
|
|
2022-02-17 09:51:51 +00:00
|
|
|
if (ss7_inst_init(bsc_nat->cn, "OsmoBSCNAT-CN", DEFAULT_PC_CN, OSMO_SCCP_SSN_BSSAP) < 0) {
|
2021-12-03 08:25:49 +00:00
|
|
|
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPED, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-31 16:08:07 +00:00
|
|
|
if (ss7_inst_init(bsc_nat->ran, "OsmoBSCNAT-RAN", DEFAULT_PC_RAN, OSMO_SCCP_SSN_BSSAP) < 0) {
|
2021-12-03 08:25:49 +00:00
|
|
|
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPED, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STARTED, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void st_started(struct osmo_fsm_inst *fi, uint32_t event, void *data)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case BSC_NAT_FSM_EV_STOP:
|
|
|
|
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPED, 0, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OSMO_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void st_stopped_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
|
|
|
|
{
|
|
|
|
struct bsc_nat *bsc_nat = (struct bsc_nat *)fi->priv;
|
|
|
|
|
|
|
|
ss7_inst_free(bsc_nat->cn);
|
|
|
|
bsc_nat->cn = NULL;
|
|
|
|
|
|
|
|
ss7_inst_free(bsc_nat->ran);
|
|
|
|
bsc_nat->ran = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void st_stopped(struct osmo_fsm_inst *fi, uint32_t event, void *data)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case BSC_NAT_FSM_EV_START:
|
|
|
|
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STARTING, 0, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OSMO_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct osmo_fsm_state bsc_nat_fsm_states[] = {
|
|
|
|
[BSC_NAT_FSM_ST_STOPPED] = {
|
|
|
|
.name = "STOPPED",
|
|
|
|
.in_event_mask = 0
|
|
|
|
| X(BSC_NAT_FSM_EV_START)
|
|
|
|
,
|
|
|
|
.out_state_mask = 0
|
|
|
|
| X(BSC_NAT_FSM_ST_STARTING)
|
|
|
|
,
|
|
|
|
.action = st_stopped,
|
|
|
|
.onenter = st_stopped_on_enter,
|
|
|
|
},
|
|
|
|
[BSC_NAT_FSM_ST_STARTING] = {
|
|
|
|
.name = "STARTING",
|
|
|
|
.out_state_mask = 0
|
|
|
|
| X(BSC_NAT_FSM_ST_STARTED)
|
|
|
|
| X(BSC_NAT_FSM_ST_STOPPED)
|
|
|
|
,
|
|
|
|
.onenter = st_starting_on_enter,
|
|
|
|
},
|
|
|
|
[BSC_NAT_FSM_ST_STARTED] = {
|
|
|
|
.name = "STARTED",
|
|
|
|
.in_event_mask = 0
|
|
|
|
| X(BSC_NAT_FSM_EV_STOP)
|
|
|
|
,
|
|
|
|
.out_state_mask = 0
|
|
|
|
| X(BSC_NAT_FSM_ST_STOPPED)
|
|
|
|
,
|
|
|
|
.action = st_started,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct value_string bsc_nat_fsm_event_names[] = {
|
|
|
|
OSMO_VALUE_STRING(BSC_NAT_FSM_EV_START),
|
|
|
|
OSMO_VALUE_STRING(BSC_NAT_FSM_EV_STOP),
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct osmo_fsm bsc_nat_fsm = {
|
|
|
|
.name = "BSC_NAT",
|
|
|
|
.states = bsc_nat_fsm_states,
|
|
|
|
.num_states = ARRAY_SIZE(bsc_nat_fsm_states),
|
|
|
|
.log_subsys = DMAIN,
|
|
|
|
.event_names = bsc_nat_fsm_event_names,
|
|
|
|
};
|
|
|
|
|
|
|
|
static __attribute__((constructor)) void bsc_nat_fsm_init(void)
|
|
|
|
{
|
|
|
|
OSMO_ASSERT(osmo_fsm_register(&bsc_nat_fsm) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bsc_nat_fsm_alloc(struct bsc_nat *bsc_nat)
|
|
|
|
{
|
|
|
|
bsc_nat->fi = osmo_fsm_inst_alloc(&bsc_nat_fsm, bsc_nat, bsc_nat, LOGL_INFO, NULL);
|
|
|
|
OSMO_ASSERT(bsc_nat->fi);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bsc_nat_fsm_start(struct bsc_nat *bsc_nat)
|
|
|
|
{
|
|
|
|
osmo_fsm_inst_dispatch(bsc_nat->fi, BSC_NAT_FSM_EV_START, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bsc_nat_fsm_stop(struct bsc_nat *bsc_nat)
|
|
|
|
{
|
|
|
|
osmo_fsm_inst_dispatch(bsc_nat->fi, BSC_NAT_FSM_EV_STOP, NULL);
|
|
|
|
}
|