osmo-bsc-nat/src/osmo-bsc-nat/bsc_nat_fsm.c

304 lines
8.3 KiB
C

/* (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>
#include <osmocom/gsm/gsm0808.h>
#include <osmocom/sigtran/osmo_ss7.h>
#include <osmocom/sigtran/sccp_helpers.h>
#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 */
static char log_buf[255];
#define LOG_SCCP(ss7_inst, peer_addr, level, fmt, args...) do { \
if (peer_addr) \
osmo_sccp_addr_to_str_buf(log_buf, sizeof(log_buf), NULL, peer_addr); \
LOGP(DMAIN, level, "(%s%s%s) " fmt, \
peer_addr ? log_buf : "", \
peer_addr ? " from " : "", \
ss7_inst == g_bsc_nat->ran ? "RAN" : "CN", \
## args); \
} while (0)
#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,
};
static struct bsc_nat_ss7_inst *ss7_inst_dest(struct bsc_nat_ss7_inst *src)
{
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;
struct osmo_sccp_addr *peer_addr;
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 */
peer_addr = &prim->u.connect.calling_addr;
LOG_SCCP(src, peer_addr, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
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 */
peer_addr = &prim->u.unitdata.calling_addr;
LOG_SCCP(src, peer_addr, LOGL_DEBUG, "%s(%s)\n", __func__, osmo_scu_prim_name(oph));
/* 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;
struct osmo_sccp_addr dest_called;
dest_ss7 = osmo_ss7_instance_find(dest->ss7_id);
OSMO_ASSERT(dest_ss7);
if (src == g_bsc_nat->ran) {
if (osmo_sccp_addr_by_name_local(&dest_called, "msc", dest_ss7) < 0) {
LOG_SCCP(src, peer_addr, LOGL_ERROR, "Could not find MSC in address book\n");
goto error;
}
} else {
if (osmo_sccp_addr_by_name_local(&dest_called, "bsc", dest_ss7) < 0) {
LOG_SCCP(src, peer_addr, LOGL_ERROR, "Could not find BSC in address book\n");
goto error;
}
}
LOG_SCCP(src, peer_addr, LOGL_NOTICE, "Forwarding to %s in %s\n",
osmo_sccp_inst_addr_name(NULL, &dest_called),
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);
osmo_sccp_tx_unitdata(dest->scu, &dest->local_sccp_addr, &dest_called, oph->msg->data,
msgb_length(oph->msg));
rc = 0;
break;
default:
LOG_SCCP(src, NULL, LOGL_ERROR, "%s(%s) is not implemented!\n", __func__, osmo_scu_prim_name(oph));
break;
}
error:
msgb_free(oph->msg);
return rc;
}
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;
if (ss7_inst_init(bsc_nat->cn, "OsmoBSCNAT-CN", DEFAULT_PC_CN, OSMO_SCCP_SSN_BSSAP) < 0) {
osmo_fsm_inst_state_chg(fi, BSC_NAT_FSM_ST_STOPPED, 0, 0);
return;
}
if (ss7_inst_init(bsc_nat->ran, "OsmoBSCNAT-RAN", DEFAULT_PC_RAN, OSMO_SCCP_SSN_BSSAP) < 0) {
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);
}