From 88cc1a2dbad497c84f09ec3b513cdca57b314160 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Fri, 8 Apr 2022 17:29:10 +0200 Subject: [PATCH] Add subscr_conn_fsm Insert the BSCNAT's MGW into phone calls by replacing the AoIP transport layer address IE inside BSSMAP Assignment Request and Assignment Confirm. Accomplish this with a new subscr_conn_fsm that parses and stores the original ass_req / ass_conf messages, communicates with the BSCNAT MGW, and then creates new ass_req / ass_conf messages based on the original ones but with new RTP information. With this patch it is possible to do a successful voice call with the following network: MS1 --- BTS1 --- BSC1 --. | | BSCNAT ----------- MSC | | | | | '-- MGW-BSC1 --|-- MGW-BSCNAT --- MGW-MSC | | MS2 --- BTS2 --- BSC2 --' | | | | '-- MGW-BSC2 ------' Depends: libosmocore I3caa15666f2dde9ee02182a89edc5daa70e8c3fc Related: SYS#5560 Related: https://osmocom.org/projects/osmo-bscnat/wiki/Ladder_diagrams_for_key_procedures Change-Id: I7e491aada6f5db0eb35ef2039869c6ba07f9ca3b --- include/osmocom/bsc_nat/Makefile.am | 1 + include/osmocom/bsc_nat/bsc_nat.h | 1 + include/osmocom/bsc_nat/bssap.h | 10 + include/osmocom/bsc_nat/subscr_conn.h | 19 + include/osmocom/bsc_nat/subscr_conn_fsm.h | 31 ++ src/osmo-bsc-nat/Makefile.am | 1 + src/osmo-bsc-nat/bssap_conn.c | 190 +++++++++ src/osmo-bsc-nat/subscr_conn.c | 29 ++ src/osmo-bsc-nat/subscr_conn_fsm.c | 489 ++++++++++++++++++++++ 9 files changed, 771 insertions(+) create mode 100644 include/osmocom/bsc_nat/subscr_conn_fsm.h create mode 100644 src/osmo-bsc-nat/subscr_conn_fsm.c diff --git a/include/osmocom/bsc_nat/Makefile.am b/include/osmocom/bsc_nat/Makefile.am index 81dacad..ebf3b0b 100644 --- a/include/osmocom/bsc_nat/Makefile.am +++ b/include/osmocom/bsc_nat/Makefile.am @@ -6,5 +6,6 @@ noinst_HEADERS = \ logging.h \ msc.h \ subscr_conn.h \ + subscr_conn_fsm.h \ vty.h \ $(NULL) diff --git a/include/osmocom/bsc_nat/bsc_nat.h b/include/osmocom/bsc_nat/bsc_nat.h index 032386a..fd2c3fb 100644 --- a/include/osmocom/bsc_nat/bsc_nat.h +++ b/include/osmocom/bsc_nat/bsc_nat.h @@ -44,6 +44,7 @@ struct bsc_nat { struct { struct mgcp_client_pool *pool; struct osmo_tdef *tdefs; + uint32_t call_id_next; } mgw; struct { diff --git a/include/osmocom/bsc_nat/bssap.h b/include/osmocom/bsc_nat/bssap.h index 3fecc7d..5b7e753 100644 --- a/include/osmocom/bsc_nat/bssap.h +++ b/include/osmocom/bsc_nat/bssap.h @@ -20,6 +20,8 @@ #pragma once #include +#include +#include #include /* connection-less */ @@ -32,3 +34,11 @@ int bssmap_tx_reset(struct bsc_nat_sccp_inst *sccp_inst, struct osmo_sccp_addr * struct subscr_conn; int bssap_handle_dt(enum bsc_nat_net net, struct subscr_conn *subscr_conn, struct msgb *msgb, unsigned int length); + +#define bssmap_tx_assignment_failure_cn(subscr_conn, cause) \ + bssmap_tx_assignment_failure(BSC_NAT_NET_CN, subscr_conn, cause) +#define bssmap_tx_assignment_failure_ran(subscr_conn, cause) \ + bssmap_tx_assignment_failure(BSC_NAT_NET_RAN, subscr_conn, cause) +int bssmap_tx_assignment_failure(enum bsc_nat_net net, struct subscr_conn *subscr_conn, enum gsm0808_cause cause); + +int bssmap_replace_ie_aoip_transp_addr(struct msgb **msg, struct sockaddr_storage *ss); diff --git a/include/osmocom/bsc_nat/subscr_conn.h b/include/osmocom/bsc_nat/subscr_conn.h index 02a0019..f740aea 100644 --- a/include/osmocom/bsc_nat/subscr_conn.h +++ b/include/osmocom/bsc_nat/subscr_conn.h @@ -20,26 +20,45 @@ #pragma once #include +#include /* connection for one subscriber */ struct subscr_conn { struct llist_head list; + struct osmo_fsm_inst *fi; + struct osmo_mgcpc_ep *ep; + uint32_t mgw_call_id; struct { uint32_t id; + struct osmo_mgcpc_ep_ci *ci; struct msc *msc; } cn; struct { uint32_t id; + struct osmo_mgcpc_ep_ci *ci; struct bsc *bsc; } ran; + + /* Copy of BSSMAP Assignment Request/Complete while being processed by + * subscr_conn_fsm. */ + struct { + struct msgb *msg; + struct osmo_sockaddr_str aoip_transp_addr; + } ass; }; int subscr_conn_get_next_id_ran(); +int subscr_conn_get_next_id_mgw(); struct subscr_conn *subscr_conn_alloc(struct msc *msc, struct bsc *bsc, uint32_t id_cn, uint32_t id_ran); struct subscr_conn *subscr_conn_get_by_id(uint32_t id, enum bsc_nat_net net); +int subscr_conn_rx_ass_req(struct subscr_conn *subscr_conn, const struct osmo_sockaddr_str *aoip_transp_addr, + struct msgb *msg); +int subscr_conn_rx_ass_compl(struct subscr_conn *subscr_conn, const struct osmo_sockaddr_str *aoip_transp_addr, + struct msgb *msg); + void subscr_conn_free(struct subscr_conn *subscr_conn); diff --git a/include/osmocom/bsc_nat/subscr_conn_fsm.h b/include/osmocom/bsc_nat/subscr_conn_fsm.h new file mode 100644 index 0000000..f0c903b --- /dev/null +++ b/include/osmocom/bsc_nat/subscr_conn_fsm.h @@ -0,0 +1,31 @@ +/* (C) 2022 by sysmocom - s.f.m.c. GmbH + * Author: Oliver Smith + * 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 . + * + */ + +#pragma once + +enum subscr_conn_fsm_events { + SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST, + SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE, + SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE, + SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND, + SUBSCR_CONN_FSM_EV_MGCP_EP_OK, + SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL, + SUBSCR_CONN_FSM_EV_MGCP_EP_TERM, +}; + diff --git a/src/osmo-bsc-nat/Makefile.am b/src/osmo-bsc-nat/Makefile.am index e3f7305..f632a7d 100644 --- a/src/osmo-bsc-nat/Makefile.am +++ b/src/osmo-bsc-nat/Makefile.am @@ -36,6 +36,7 @@ osmo_bsc_nat_SOURCES = \ msc.c \ msc_fsm.c \ subscr_conn.c \ + subscr_conn_fsm.c \ vty.c \ $(NULL) diff --git a/src/osmo-bsc-nat/bssap_conn.c b/src/osmo-bsc-nat/bssap_conn.c index 10b3a01..b26d139 100644 --- a/src/osmo-bsc-nat/bssap_conn.c +++ b/src/osmo-bsc-nat/bssap_conn.c @@ -23,6 +23,69 @@ #include #include #include +#include + +int bssmap_replace_ie_aoip_transp_addr(struct msgb **msg, struct sockaddr_storage *ss) +{ + struct msgb *msg_new; + struct msgb *msg_old = *msg; + const struct tlv_definition *def = gsm0808_att_tlvdef(); + struct tlv_parsed tp; + uint8_t tag_order[256]; + int tag_order_len; + int rc; + + tlv_parse(&tp, def, msg_old->l3h + 1, msgb_l3len(msg_old) - 1, 0, 0); + tag_order_len = tlv_parse_order(tag_order, ARRAY_SIZE(tag_order), def, msg_old->l3h + 1, + msgb_l3len(msg_old) - 1); + + msg_new = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, talloc_get_name(msg_old)); + OSMO_ASSERT(msg_new); + msgb_v_put(msg_new, msg_old->l3h[0]); /* bssmap message type */ + + for (int i = 0; i < tag_order_len; i++) { + uint8_t tag = tag_order[i]; + + if (tag == GSM0808_IE_AOIP_TRASP_ADDR) + rc = gsm0808_enc_aoip_trasp_addr(msg_new, ss); + else + rc = tlv_encode_one(msg_new, def->def[tag].type, tag, TLVP_LEN(&tp, tag), TLVP_VAL(&tp, tag)); + + if (rc < 0) { + LOGP(DMAIN, LOGL_ERROR, "Failed to encode tag %d into copy of bssmap msg\n", tag); + msgb_free(msg_new); + return rc; + } + } + + msg_new->l3h = msgb_tv_push(msg_new, BSSAP_MSG_BSS_MANAGEMENT, msgb_length(msg_new)); + msgb_free(msg_old); + *msg = msg_new; + return 0; +} + +int bssmap_tx_assignment_failure(enum bsc_nat_net net, struct subscr_conn *subscr_conn, enum gsm0808_cause cause) +{ + struct bsc_nat_sccp_inst *sccp_inst; + uint32_t id; + struct msgb *msg; + + LOGP(DMAIN, LOGL_ERROR, "Tx BSSMAP assignment failure %s to %s via %s\n", + gsm0808_cause_name(cause), + net == BSC_NAT_NET_CN ? "CN" : "RAN", + talloc_get_name(subscr_conn)); + + if (net == BSC_NAT_NET_CN) { + sccp_inst = g_bsc_nat->cn.sccp_inst; + id = subscr_conn->cn.id; + } else { + sccp_inst = g_bsc_nat->ran.sccp_inst; + id = subscr_conn->ran.id; + } + + msg = gsm0808_create_ass_fail(cause, NULL, NULL); + return osmo_sccp_tx_data_msg(sccp_inst->scu, id, msg); +} static int bssap_fwd_to_cn(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) { @@ -42,11 +105,132 @@ static int bssap_fwd_to_ran(struct subscr_conn *subscr_conn, struct msgb *msg, u return osmo_sccp_tx_data(g_bsc_nat->ran.sccp_inst->scu, subscr_conn->ran.id, msg->data, msgb_length(msg)); } +static int bssmap_cn_handle_ass_req(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) +{ + struct gsm0808_channel_type ct; + struct tlv_parsed tp; + struct sockaddr_storage ss; + struct tlv_p_entry *e; + struct osmo_sockaddr_str aoip_transp_addr; + + tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, length - 1, 0, 0); + + /* Get channel type */ + if (!(e = TLVP_GET(&tp, GSM0808_IE_CHANNEL_TYPE))) { + LOGP(DMAIN, LOGL_ERROR, "Missing IE: channel type\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING); + return -1; + } + if (gsm0808_dec_channel_type(&ct, e->val, e->len) <= 0) { + LOGP(DMAIN, LOGL_ERROR, "Invalid IE: channel type\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_INCORRECT_VALUE); + return -1; + } + + /* Not speech: fwd directly */ + if ((ct.ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH) { + LOGP(DMAIN, LOGL_DEBUG, "Channel type is not speech, forwarding without modification\n"); + if (bssap_fwd_to_ran(subscr_conn, msg, length) < 0) { + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + return -1; + } + return 0; + } + + /* Get AoIP transport layer address */ + if (!(e = TLVP_GET(&tp, GSM0808_IE_AOIP_TRASP_ADDR))) { + LOGP(DMAIN, LOGL_ERROR, "Missing IE: AoIP transport layer address\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING); + return -1; + } + if (gsm0808_dec_aoip_trasp_addr(&ss, e->val, e->len) <= 0 + || osmo_sockaddr_str_from_sockaddr(&aoip_transp_addr, &ss) < 0) { + LOGP(DMAIN, LOGL_ERROR, "Invalid IE: AoIP transport layer address\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_INCORRECT_VALUE); + return -1; + } + + /* Don't forward the message directly. Instead, let the subscr_conn FSM + * allocate new MGCP connections in the BSCNAT's MGW and then send a + * similar assignment request, but with the RTP address replaced. */ + if (subscr_conn_rx_ass_req(subscr_conn, &aoip_transp_addr, msg) < 0) { + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + return -1; + } + + return 0; +} + +static void bssmap_ran_error_ass_compl(struct subscr_conn *subscr_conn, enum gsm0808_cause cause_ran) +{ + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + bssmap_tx_assignment_failure_ran(subscr_conn, cause_ran); + + /* For the FSM, treat this the same as if the BSC had responded with + * assignment failure. */ + osmo_fsm_inst_dispatch(subscr_conn->fi, SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE, NULL); +} + +static int bssmap_ran_handle_ass_compl(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) +{ + struct tlv_parsed tp; + struct sockaddr_storage ss; + struct tlv_p_entry *e; + struct osmo_sockaddr_str aoip_transp_addr; + + tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, length - 1, 0, 0); + + /* Get AoIP transport layer address */ + if (!(e = TLVP_GET(&tp, GSM0808_IE_AOIP_TRASP_ADDR))) { + LOGP(DMAIN, LOGL_ERROR, "Missing IE: AoIP transport layer address\n"); + bssmap_ran_error_ass_compl(subscr_conn, GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING); + return -1; + } + if (gsm0808_dec_aoip_trasp_addr(&ss, e->val, e->len) <= 0 + || osmo_sockaddr_str_from_sockaddr(&aoip_transp_addr, &ss) < 0) { + LOGP(DMAIN, LOGL_ERROR, "Invalid IE: AoIP transport layer address\n"); + bssmap_ran_error_ass_compl(subscr_conn, GSM0808_CAUSE_INCORRECT_VALUE); + return -1; + } + + /* Don't forward the message directly. Instead, let the subscr_conn FSM + * use the RTP info to MDCX the BSC-side connection in the BSCNAT's MGW + * and then send a similar assignment complete to the MSC, but with the + * RTP address replaced. */ + if (subscr_conn_rx_ass_compl(subscr_conn, &aoip_transp_addr, msg) < 0) { + bssmap_ran_error_ass_compl(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + return -1; + } + + return 0; +} + +static int bssmap_ran_handle_assignment_failure(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) +{ + osmo_fsm_inst_dispatch(subscr_conn->fi, SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE, NULL); + bssap_fwd_to_ran(subscr_conn, msg, length); + return 0; +} + +static int bssmap_cn_handle_clear_cmd(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) +{ + osmo_fsm_inst_dispatch(subscr_conn->fi, SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND, NULL); + bssap_fwd_to_ran(subscr_conn, msg, length); + return 0; +} + + static int bssmap_cn_rcvmsg_dt(struct subscr_conn *subscr_conn, struct msgb *msg, unsigned int length) { int ret = 0; switch (msg->l3h[0]) { + case BSS_MAP_MSG_ASSIGNMENT_RQST: + ret = bssmap_cn_handle_ass_req(subscr_conn, msg, length); + break; + case BSS_MAP_MSG_CLEAR_CMD: + ret = bssmap_cn_handle_clear_cmd(subscr_conn, msg, length); + break; default: ret = bssap_fwd_to_ran(subscr_conn, msg, length); break; @@ -60,6 +244,12 @@ static int bssmap_ran_rcvmsg_dt(struct subscr_conn *subscr_conn, struct msgb *ms int ret = 0; switch (msg->l3h[0]) { + case BSS_MAP_MSG_ASSIGNMENT_COMPLETE: + ret = bssmap_ran_handle_ass_compl(subscr_conn, msg, length); + break; + case BSS_MAP_MSG_ASSIGNMENT_FAILURE: + ret = bssmap_ran_handle_assignment_failure(subscr_conn, msg, length); + break; default: ret = bssap_fwd_to_cn(subscr_conn, msg, length); break; diff --git a/src/osmo-bsc-nat/subscr_conn.c b/src/osmo-bsc-nat/subscr_conn.c index a94287c..ed35899 100644 --- a/src/osmo-bsc-nat/subscr_conn.c +++ b/src/osmo-bsc-nat/subscr_conn.c @@ -26,6 +26,8 @@ #include #include +extern struct osmo_fsm subscr_conn_fsm; + int subscr_conn_get_next_id_ran() { uint32_t *id = &g_bsc_nat->ran.subscr_conn_id_next; @@ -49,6 +51,29 @@ int subscr_conn_get_next_id_ran() return -1; } +int subscr_conn_get_next_id_mgw() +{ + uint32_t *id = &g_bsc_nat->mgw.call_id_next; + + for (int i = 0; i < 0xFFFFFF; i++) { + struct subscr_conn *subscr_conn; + bool already_used = false; + + *id = (*id + 1) & 0xffffff; + + llist_for_each_entry(subscr_conn, &g_bsc_nat->subscr_conns, list) { + if (*id == subscr_conn->mgw_call_id) { + already_used = true; + break; + } + } + + if (!already_used) + return *id; + } + return -1; +} + struct subscr_conn *subscr_conn_alloc(struct msc *msc, struct bsc *bsc, uint32_t id_cn, uint32_t id_ran) { struct subscr_conn *subscr_conn = talloc_zero(g_bsc_nat, struct subscr_conn); @@ -60,6 +85,9 @@ struct subscr_conn *subscr_conn_alloc(struct msc *msc, struct bsc *bsc, uint32_t LOGP(DMAIN, LOGL_DEBUG, "Add %s\n", talloc_get_name(subscr_conn)); + subscr_conn->fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, subscr_conn, subscr_conn, LOGL_INFO, NULL); + OSMO_ASSERT(subscr_conn->fi); + subscr_conn->cn.id = id_cn; subscr_conn->cn.msc = msc; subscr_conn->ran.id = id_ran; @@ -88,5 +116,6 @@ void subscr_conn_free(struct subscr_conn *subscr_conn) { LOGP(DMAIN, LOGL_DEBUG, "Del %s\n", talloc_get_name(subscr_conn)); llist_del(&subscr_conn->list); + osmo_fsm_inst_free(subscr_conn->fi); talloc_free(subscr_conn); } diff --git a/src/osmo-bsc-nat/subscr_conn_fsm.c b/src/osmo-bsc-nat/subscr_conn_fsm.c new file mode 100644 index 0000000..9fbce04 --- /dev/null +++ b/src/osmo-bsc-nat/subscr_conn_fsm.c @@ -0,0 +1,489 @@ +/* (C) 2022 by sysmocom - s.f.m.c. GmbH + * Author: Oliver Smith + * 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 . + * + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define X(s) (1 << (s)) +#define TIMEOUT_MGW 10 +#define TIMEOUT_BSC 20 + +enum subscr_conn_fsm_states { + SUBSCR_CONN_FSM_ST_IDLE, + SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_CN, + SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_MDCX_CN, + SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_RAN, + SUBSCR_CONN_FSM_ST_WAITING_FOR_ASSIGNMENT_COMPLETE, + SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_COMPLETE_MDCX_RAN, + SUBSCR_CONN_FSM_ST_WAITING_FOR_CLEAR_COMMAND, +}; + +static int tx_ass_req_to_ran(struct subscr_conn *subscr_conn) +{ + const struct mgcp_conn_peer *rtp_info; + struct sockaddr_storage ss; + struct osmo_sockaddr_str aoip_transp_addr; + int rc; + + /* Fill sockaddr_storage from rtp_info */ + rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(subscr_conn->ran.ci); + if (!rtp_info) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Failed to get RTP info from MGW, aborting assignment request\n"); + return -1; + } + if (osmo_sockaddr_str_from_str(&aoip_transp_addr, rtp_info->addr, rtp_info->port) < 0 + || osmo_sockaddr_str_to_sockaddr((const struct osmo_sockaddr_str *)&aoip_transp_addr, &ss) < 0) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "RTP info from MGW is invalid, aborting assignment request\n"); + return -1; + } + + if (bssmap_replace_ie_aoip_transp_addr(&subscr_conn->ass.msg, &ss) < 0) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Failed to replace AoIP transport layer address, aborting" + " assignment request\n"); + return -1; + } + + rc = osmo_sccp_tx_data_msg(g_bsc_nat->ran.sccp_inst->scu, subscr_conn->ran.id, subscr_conn->ass.msg); + subscr_conn->ass.msg = NULL; + return rc; +} + +static int tx_ass_compl_to_cn(struct subscr_conn *subscr_conn) +{ + const struct mgcp_conn_peer *rtp_info; + struct sockaddr_storage ss; + struct osmo_sockaddr_str aoip_transp_addr; + int rc; + + /* Fill sockaddr_storage from rtp_info */ + rtp_info = osmo_mgcpc_ep_ci_get_rtp_info(subscr_conn->cn.ci); + if (!rtp_info) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Failed to get RTP info from MGW, aborting assignment complete\n"); + return -1; + } + if (osmo_sockaddr_str_from_str(&aoip_transp_addr, rtp_info->addr, rtp_info->port) < 0 + || osmo_sockaddr_str_to_sockaddr((const struct osmo_sockaddr_str *)&aoip_transp_addr, &ss) < 0) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "RTP info from MGW is invalid, aborting assignment complete\n"); + return -1; + } + + if (bssmap_replace_ie_aoip_transp_addr(&subscr_conn->ass.msg, &ss) < 0) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Failed to replace AoIP transport layer address, aborting" + " assignment complete\n"); + return -1; + } + + rc = osmo_sccp_tx_data_msg(g_bsc_nat->cn.sccp_inst->scu, subscr_conn->cn.id, subscr_conn->ass.msg); + subscr_conn->ass.msg = NULL; + return rc; +} + + +static void st_idle_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + if (!subscr_conn->ep) + return; + + osmo_mgcpc_ep_clear(subscr_conn->ep); + + subscr_conn->ep = NULL; + subscr_conn->ran.ci = NULL; + subscr_conn->cn.ci = NULL; +} + +static void st_idle(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + case SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST: + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_CN, TIMEOUT_MGW, 0); + break; + case SUBSCR_CONN_FSM_EV_MGCP_EP_TERM: + /* This event is expected, as we terminate the MGCP EP in + * st_idle_on_enter(). Nothing to do here. */ + break; + case SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND: + /* Clear commands sent to this FSM in idle state are not + * relevant, so ignore them here. bssmap_ran_handle_clear_cmd() + * forwards them from RAN to CN. */ + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_processing_ass_req_crcx_cn_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_stat) +{ + struct mgcp_client *mgcp_client; + struct mgcp_conn_peer crcx_info = {}; + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + int call_id = subscr_conn_get_next_id_mgw(); + + if (call_id < 0) { + LOGPFSML(fi, LOGL_ERROR, "Failed to get next_id_mgw, aborting assignment request processing\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + } + + /* MGCP EP */ + mgcp_client = mgcp_client_pool_get(g_bsc_nat->mgw.pool); + OSMO_ASSERT(mgcp_client); + subscr_conn->ep = osmo_mgcpc_ep_alloc(subscr_conn->fi, SUBSCR_CONN_FSM_EV_MGCP_EP_TERM, mgcp_client, + g_bsc_nat->mgw.tdefs, "SUBSCR-CONN-EP", + mgcp_client_rtpbridge_wildcard(mgcp_client)); + + /* MGCP CRCX CN */ + LOGPFSML(fi, LOGL_DEBUG, "Set MGW call_id=%d for %s\n", call_id, talloc_get_name(subscr_conn)); + crcx_info.call_id = subscr_conn->mgw_call_id = call_id; + subscr_conn->cn.ci = osmo_mgcpc_ep_ci_add(subscr_conn->ep, "CI-%"PRIu32"-CN", crcx_info.call_id); + osmo_mgcpc_ep_ci_request(subscr_conn->cn.ci, MGCP_VERB_CRCX, &crcx_info, subscr_conn->fi, + SUBSCR_CONN_FSM_EV_MGCP_EP_OK, SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL, NULL); +} + +static void st_processing_ass_req_crcx_cn(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + + switch (event) { + case SUBSCR_CONN_FSM_EV_MGCP_EP_OK: + LOGPFSML(fi, LOGL_DEBUG, "Rx MGCP OK\n"); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_MDCX_CN, TIMEOUT_MGW, 0); + break; + case SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL: + LOGPFSML(fi, LOGL_ERROR, "MGCP failure, aborting assignment request processing\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_processing_ass_req_mdcx_cn_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_stat) +{ + struct mgcp_conn_peer mdcx_info = {}; + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + struct osmo_sockaddr_str *addr = &subscr_conn->ass.aoip_transp_addr; + + /* Set RTP addr + port */ + osmo_static_assert(sizeof(mdcx_info.addr) == sizeof(addr->ip), sizeof_addr); + memcpy(mdcx_info.addr, addr->ip, sizeof(mdcx_info.addr)); + mdcx_info.port = addr->port; + + /* MGCP MDCX CN */ + osmo_mgcpc_ep_ci_request(subscr_conn->cn.ci, MGCP_VERB_MDCX, &mdcx_info, subscr_conn->fi, + SUBSCR_CONN_FSM_EV_MGCP_EP_OK, SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL, NULL); +} + +static void st_processing_ass_req_mdcx_cn(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + + switch (event) { + case SUBSCR_CONN_FSM_EV_MGCP_EP_OK: + LOGPFSML(fi, LOGL_DEBUG, "Rx MGCP OK\n"); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_RAN, TIMEOUT_MGW, 0); + break; + case SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL: + LOGPFSML(fi, LOGL_ERROR, "MGCP failure, aborting assignment request processing\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_processing_ass_req_crcx_ran_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_stat) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + struct mgcp_conn_peer crcx_info = {}; + crcx_info.call_id = subscr_conn->mgw_call_id; + + subscr_conn->ran.ci = osmo_mgcpc_ep_ci_add(subscr_conn->ep, "CI-%"PRIu32"-RAN", crcx_info.call_id); + osmo_mgcpc_ep_ci_request(subscr_conn->ran.ci, MGCP_VERB_CRCX, &crcx_info, subscr_conn->fi, + SUBSCR_CONN_FSM_EV_MGCP_EP_OK, SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL, NULL); +} + +static void st_processing_ass_req_crcx_ran(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + + switch (event) { + case SUBSCR_CONN_FSM_EV_MGCP_EP_OK: + LOGPFSML(fi, LOGL_DEBUG, "Rx MGCP OK\n"); + if (tx_ass_req_to_ran(subscr_conn) < 0) { + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + return; + } + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_WAITING_FOR_ASSIGNMENT_COMPLETE, TIMEOUT_BSC, 0); + break; + case SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL: + LOGPFSML(fi, LOGL_ERROR, "MGCP failure, aborting assignment request processing\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_waiting_for_ass_compl(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + case SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE: + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_COMPLETE_MDCX_RAN, TIMEOUT_MGW, 0); + break; + case SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE: + /* The original bssmap message gets forwarded from RAN to CN by + * bssmap_ran_handle_assignment_failure() already, so just + * reset the FSM to idle here (clears the mgw endpoint). */ + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_processing_ass_compl_mdcx_ran_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_stat) +{ + struct mgcp_conn_peer mdcx_info = {}; + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + struct osmo_sockaddr_str *aoip_transp_addr = &subscr_conn->ass.aoip_transp_addr; + + /* Set RTP addr + port */ + osmo_static_assert(sizeof(mdcx_info.addr) == sizeof(aoip_transp_addr->ip), sizeof_addr); + memcpy(mdcx_info.addr, aoip_transp_addr->ip, sizeof(mdcx_info.addr)); + mdcx_info.port = aoip_transp_addr->port; + + /* MGCP MDCX RAN */ + osmo_mgcpc_ep_ci_request(subscr_conn->ran.ci, MGCP_VERB_MDCX, &mdcx_info, subscr_conn->fi, + SUBSCR_CONN_FSM_EV_MGCP_EP_OK, SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL, NULL); +} + +static void st_processing_ass_compl_mdcx_ran(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv; + + switch (event) { + case SUBSCR_CONN_FSM_EV_MGCP_EP_OK: + LOGPFSML(fi, LOGL_DEBUG, "Rx MGCP EP OK\n"); + if (tx_ass_compl_to_cn(subscr_conn) < 0) { + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + bssmap_tx_assignment_failure_ran(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + return; + } + /* No timeout for ST_WAITING_FOR_CLEAR_COMMAND, as the FSM will + * stay in this state until the call is done. */ + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_WAITING_FOR_CLEAR_COMMAND, 0, 0); + break; + case SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL: + LOGPFSML(fi, LOGL_ERROR, "MGCP failure, aborting assignment complete processing\n"); + bssmap_tx_assignment_failure_cn(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + bssmap_tx_assignment_failure_ran(subscr_conn, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +static void st_waiting_for_clear_command(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch (event) { + case SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND: + /* The original bssmap message gets forwarded from RAN to CN by + * bssmap_ran_handle_clear_cmd() already, so just reset the FSM + * to idle here (clears the mgw endpoint). */ + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + break; + default: + OSMO_ASSERT(false); + } +} + +int subscr_conn_fsm_timer_cb(struct osmo_fsm_inst *fi) +{ + LOGPFSML(fi, LOGL_ERROR, "Timeout reached, reset FSM to idle\n"); + osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_FSM_ST_IDLE, 0, 0); + return 0; +} + +static struct osmo_fsm_state subscr_conn_fsm_states[] = { + [SUBSCR_CONN_FSM_ST_IDLE] = { + .name = "IDLE", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST) + | X(SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND) + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_TERM) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_CN) + , + .action = st_idle, + .onenter = st_idle_on_enter, + }, + [SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_CN] = { + .name = "PROCESSING_ASSIGNMENT_REQUEST_CRCX_CN", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_OK) + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_IDLE) + | X(SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_MDCX_CN) + , + .action = st_processing_ass_req_crcx_cn, + .onenter = st_processing_ass_req_crcx_cn_on_enter, + }, + [SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_MDCX_CN] = { + .name = "PROCESSING_ASSIGNMENT_REQUEST_MDCX_CN", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_OK) + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_IDLE) + | X(SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_RAN) + , + .action = st_processing_ass_req_mdcx_cn, + .onenter = st_processing_ass_req_mdcx_cn_on_enter, + }, + [SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_REQUEST_CRCX_RAN] = { + .name = "PROCESSING_ASSIGNMENT_REQUEST_CRCX_RAN", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_OK) + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_IDLE) + | X(SUBSCR_CONN_FSM_ST_WAITING_FOR_ASSIGNMENT_COMPLETE) + , + .action = st_processing_ass_req_crcx_ran, + .onenter = st_processing_ass_req_crcx_ran_on_enter, + }, + [SUBSCR_CONN_FSM_ST_WAITING_FOR_ASSIGNMENT_COMPLETE] = { + .name = "WAITING_FOR_ASSIGNMENT_COMPLETE", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE) + | X(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_COMPLETE_MDCX_RAN) + , + .action = st_waiting_for_ass_compl, + }, + [SUBSCR_CONN_FSM_ST_PROCESSING_ASSIGNMENT_COMPLETE_MDCX_RAN] = { + .name = "PROCESSING_ASSIGNMENT_COMPLETE", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_OK) + | X(SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_IDLE) + | X(SUBSCR_CONN_FSM_ST_WAITING_FOR_CLEAR_COMMAND) + , + .action = st_processing_ass_compl_mdcx_ran, + .onenter = st_processing_ass_compl_mdcx_ran_on_enter, + }, + [SUBSCR_CONN_FSM_ST_WAITING_FOR_CLEAR_COMMAND] = { + .name = "WAITING_FOR_CLEAR_COMMAND", + .in_event_mask = 0 + | X(SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND) + , + .out_state_mask = 0 + | X(SUBSCR_CONN_FSM_ST_IDLE) + , + .action = st_waiting_for_clear_command, + }, +}; + +const struct value_string subscr_conn_fsm_event_names[] = { + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_FAILURE), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_BSSMAP_CLEAR_COMMAND), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_MGCP_EP_OK), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_MGCP_EP_FAIL), + OSMO_VALUE_STRING(SUBSCR_CONN_FSM_EV_MGCP_EP_TERM), + { 0, NULL } +}; + +struct osmo_fsm subscr_conn_fsm = { + .name = "SUBSCR_CONN", + .states = subscr_conn_fsm_states, + .num_states = ARRAY_SIZE(subscr_conn_fsm_states), + .log_subsys = DMAIN, + .event_names = subscr_conn_fsm_event_names, + .timer_cb = subscr_conn_fsm_timer_cb, +}; + +static void ass_update(struct subscr_conn *subscr_conn, const struct osmo_sockaddr_str *aoip_transp_addr, + struct msgb *msg) +{ + struct msgb *copy = msgb_copy_c(subscr_conn, msg, talloc_get_name(msg)); + OSMO_ASSERT(copy); + + if (subscr_conn->ass.msg) + msgb_free(subscr_conn->ass.msg); + + subscr_conn->ass.msg = copy; + subscr_conn->ass.aoip_transp_addr = *aoip_transp_addr; +} + +int subscr_conn_rx_ass_req(struct subscr_conn *subscr_conn, const struct osmo_sockaddr_str *aoip_transp_addr, + struct msgb *msg) +{ + if (subscr_conn->fi->state != SUBSCR_CONN_FSM_ST_IDLE) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Unexpected Rx BSSMAP assignment request\n"); + return -1; + } + + ass_update(subscr_conn, aoip_transp_addr, msg); + return osmo_fsm_inst_dispatch(subscr_conn->fi, SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST, NULL); +} + +int subscr_conn_rx_ass_compl(struct subscr_conn *subscr_conn, const struct osmo_sockaddr_str *aoip_transp_addr, + struct msgb *msg) +{ + if (subscr_conn->fi->state != SUBSCR_CONN_FSM_ST_WAITING_FOR_ASSIGNMENT_COMPLETE) { + LOGPFSML(subscr_conn->fi, LOGL_ERROR, "Unexpected Rx BSSMAP assignment complete\n"); + return -1; + } + + ass_update(subscr_conn, aoip_transp_addr, msg); + return osmo_fsm_inst_dispatch(subscr_conn->fi, SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE, NULL); +} + +static __attribute__((constructor)) void subscr_conn_fsm_init(void) +{ + OSMO_ASSERT(osmo_fsm_register(&subscr_conn_fsm) == 0); +}