osmith has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-bsc-nat/+/27712 )


Change subject: Add subscr_conn_fsm
......................................................................

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 ------'

Related: SYS#5560
Related: 
https://osmocom.org/projects/osmo-bscnat/wiki/Ladder_diagrams_for_key_procedures
Change-Id: I7e491aada6f5db0eb35ef2039869c6ba07f9ca3b
---
M include/osmocom/bsc_nat/Makefile.am
M include/osmocom/bsc_nat/bsc_nat.h
M include/osmocom/bsc_nat/bssap.h
M include/osmocom/bsc_nat/subscr_conn.h
A include/osmocom/bsc_nat/subscr_conn_fsm.h
M src/osmo-bsc-nat/Makefile.am
M src/osmo-bsc-nat/bssap_conn.c
M src/osmo-bsc-nat/subscr_conn.c
A src/osmo-bsc-nat/subscr_conn_fsm.c
9 files changed, 824 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc-nat refs/changes/12/27712/1

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 @@
        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 {
                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..5c11fc5 100644
--- a/include/osmocom/bsc_nat/bssap.h
+++ b/include/osmocom/bsc_nat/bssap.h
@@ -20,8 +20,46 @@
 #pragma once

 #include <osmocom/core/msgb.h>
+#include <osmocom/core/sockaddr_str.h>
+#include <osmocom/gsm/gsm0808_utils.h>
+#include <osmocom/gsm/protocol/gsm_08_08.h>
 #include <osmocom/bsc_nat/bsc_nat.h>

+/* Parsed BSSMAP messages, to be stored inside struct subscr_conn until
+ * forwarded with slight modification */
+struct bssmap_assignment_rqst {
+       struct gsm0808_channel_type ct;
+
+       struct osmo_sockaddr_str aoip_transp_addr;
+
+       struct gsm0808_speech_codec_list scl;
+
+       uint32_t ci;
+       bool ci_present;
+
+       uint8_t kc;
+       bool kc_present;
+
+       struct osmo_lcls lcls;
+       bool lcls_present;
+};
+
+struct bssmap_assignment_complete {
+       uint8_t rr_cause;
+       uint8_t chosen_channel;
+       uint8_t chosen_encr_alg;
+       uint8_t speech_version;
+
+       struct osmo_sockaddr_str aoip_transp_addr;
+
+       struct gsm0808_speech_codec sc;
+       struct gsm0808_speech_codec_list scl;
+       bool scl_present;
+
+       enum gsm0808_lcls_status lcls_bss_status;
+       bool lcls_bss_status_present;
+};
+
 /* connection-less */
 int bssap_handle_udt(struct bsc_nat_sccp_inst *sccp_inst, struct 
osmo_sccp_addr *addr, struct msgb *msgb,
                     unsigned int length);
@@ -32,3 +70,9 @@
 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);
diff --git a/include/osmocom/bsc_nat/subscr_conn.h 
b/include/osmocom/bsc_nat/subscr_conn.h
index 7a0dc44..aad207c 100644
--- a/include/osmocom/bsc_nat/subscr_conn.h
+++ b/include/osmocom/bsc_nat/subscr_conn.h
@@ -20,25 +20,35 @@
 #pragma once

 #include <osmocom/bsc_nat/bsc_nat.h>
+#include <osmocom/bsc_nat/bssap.h>
 
 enum subscr_conn_id_type {
        SUBSCR_CONN_ID_TYPE_CONN_ID_CN = 0,
        SUBSCR_CONN_ID_TYPE_CONN_ID_RAN,
-       /* SUBSCR_CONN_ID_TYPE_CALL_ID_MGW will be added in future patch */
+       SUBSCR_CONN_ID_TYPE_CALL_ID_MGW,
 };

 /* 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;
+
+               struct bssmap_assignment_rqst ass_req;
        } cn;

        struct {
                uint32_t id;
+               struct osmo_mgcpc_ep_ci *ci;
                struct bsc *bsc;
+
+               struct bssmap_assignment_complete ass_compl;
        } ran;
 };

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 <[email protected]>
+ * Author: Oliver Smith <[email protected]>
+ * 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/>.
+ *
+ */
+
+#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 @@
        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 659fb73..bd1302a 100644
--- a/src/osmo-bsc-nat/bssap_conn.c
+++ b/src/osmo-bsc-nat/bssap_conn.c
@@ -23,6 +23,30 @@
 #include <osmocom/sigtran/sccp_helpers.h>
 #include <osmocom/bsc_nat/logging.h>
 #include <osmocom/bsc_nat/subscr_conn.h>
+#include <osmocom/bsc_nat/subscr_conn_fsm.h>
+
+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 +66,248 @@
        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_assignment_rqst(struct subscr_conn *subscr_conn, 
struct msgb *msg, unsigned int length)
+{
+       struct bssmap_assignment_rqst ass_req = {};
+       struct tlv_parsed tp;
+       struct sockaddr_storage ss;
+       struct tlv_p_entry *e;
+
+       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(&ass_req.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 ((ass_req.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(&ass_req.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;
+       }
+
+       /* Get speech codec list */
+       if (!(e = TLVP_GET(&tp, GSM0808_IE_SPEECH_CODEC_LIST))) {
+               LOGP(DMAIN, LOGL_ERROR, "Missing IE: speech codec list\n");
+               bssmap_tx_assignment_failure_cn(subscr_conn, 
GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING);
+               return -1;
+       }
+       if (gsm0808_dec_speech_codec_list(&ass_req.scl, e->val, e->len) <= 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: speech codec list\n");
+               bssmap_tx_assignment_failure_cn(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+
+       /* Get call ID */
+       if ((e = TLVP_GET(&tp, GSM0808_IE_CALL_ID))) {
+               if (e->len != 4) {
+                       LOGP(DMAIN, LOGL_ERROR, "Invalid IE: call ID (len %u 
instead of 4)\n", e->len);
+                       bssmap_tx_assignment_failure_cn(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+                       return -1;
+               }
+               memcpy(&ass_req.ci, e->val, 32);
+               ass_req.ci_present = true;
+       }
+
+       /* Get Kc */
+       if ((e = TLVP_GET(&tp, GSM0808_IE_KC_128))) {
+               if (e->len != 16) {
+                       LOGP(DMAIN, LOGL_ERROR, "Invalid IE: Kc128 (len %u 
instead of 16)\n", e->len);
+                       bssmap_tx_assignment_failure_cn(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+                       return -1;
+               }
+               memcpy(&ass_req.kc, e->val, 16);
+               ass_req.kc_present = true;
+       }
+
+       /* Get LCLS */
+       if (TLVP_PRESENT(&tp, GSM0808_IE_GLOBAL_CALL_REF)) {
+               if (gsm0808_dec_lcls(&ass_req.lcls, &tp) < 0) {
+                       LOGP(DMAIN, LOGL_ERROR, "Invalid IEs for LCLS\n");
+                       bssmap_tx_assignment_failure_cn(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+                       return -1;
+               }
+               ass_req.lcls_present = true;
+       }
+
+       /* 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 (osmo_fsm_inst_dispatch(subscr_conn->fi, 
SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST, &ass_req) < 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_assignment_complete(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_assignment_complete(struct subscr_conn 
*subscr_conn, struct msgb *msg, unsigned int length)
+{
+       struct bssmap_assignment_complete ass_compl = {};
+       struct tlv_parsed tp;
+       struct sockaddr_storage ss;
+       struct tlv_p_entry *e;
+
+       tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, length - 1, 0, 0);
+
+       /* Get RR cause */
+       if (!(e = TLVP_GET(&tp, GSM0808_IE_RR_CAUSE))) {
+               LOGP(DMAIN, LOGL_ERROR, "Missing IE: RR cause\n");
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING);
+               return -1;
+       }
+       if (e->len != 1) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: RR cause (len %u instead 
of 1)\n", e->len);
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+       ass_compl.rr_cause = e->val[0];
+
+       /* Get chosen channel */
+       if (!(e = TLVP_GET(&tp, GSM0808_IE_CHOSEN_CHANNEL))) {
+               LOGP(DMAIN, LOGL_ERROR, "Missing IE: chosen channel\n");
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING);
+               return -1;
+       }
+       if (e->len != 1) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: chosen channel (len %u 
instead of 1)\n", e->len);
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+       ass_compl.chosen_channel = e->val[0];
+
+       /* Get chosen encr alg */
+       if (!(e = TLVP_GET(&tp, GSM0808_IE_CHOSEN_ENCR_ALG))) {
+               LOGP(DMAIN, LOGL_ERROR, "Missing IE: chosen encr alg\n");
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING);
+               return -1;
+       }
+       if (e->len != 1) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: chosen encr alg (len %u 
instead of 1)\n", e->len);
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+       ass_compl.chosen_encr_alg = e->val[0];
+
+       /* Get speech version */
+       if (!(e = TLVP_GET(&tp, GSM0808_IE_SPEECH_VERSION))) {
+               LOGP(DMAIN, LOGL_ERROR, "Missing IE: speech version\n");
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING);
+               return -1;
+       }
+       if (e->len != 1) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: speech version (len %u 
instead of 1)\n", e->len);
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+       ass_compl.speech_version = e->val[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_assignment_complete(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(&ass_compl.aoip_transp_addr, 
&ss) < 0) {
+               LOGP(DMAIN, LOGL_ERROR, "Invalid IE: AoIP transport layer 
address\n");
+               bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+               return -1;
+       }
+
+       /* Get speech codec list */
+       if ((e = TLVP_GET(&tp, GSM0808_IE_SPEECH_CODEC_LIST))) {
+               if (gsm0808_dec_speech_codec_list(&ass_compl.scl, e->val, 
e->len) <= 0) {
+                       LOGP(DMAIN, LOGL_ERROR, "Invalid IE: speech codec 
list\n");
+                       bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+                       return -1;
+               }
+               ass_compl.scl_present = true;
+       }
+
+       /* Get LCLS */
+       if ((e = TLVP_GET(&tp, GSM0808_IE_LCLS_BSS_STATUS))) {
+               if (e->len != 1) {
+                       LOGP(DMAIN, LOGL_ERROR, "Invalid IE: LCLS BSS status 
(len %u instead of 1)\n", e->len);
+                       bssmap_ran_error_assignment_complete(subscr_conn, 
GSM0808_CAUSE_INCORRECT_VALUE);
+                       return -1;
+               }
+               ass_compl.lcls_bss_status = e->val[0];
+               ass_compl.lcls_bss_status_present = true;
+       }
+
+       /* 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 (osmo_fsm_inst_dispatch(subscr_conn->fi, 
SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_COMPLETE, &ass_compl) < 0) {
+               bssmap_ran_error_assignment_complete(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_assignment_rqst(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 +321,12 @@
        int ret = 0;

        switch (msg->l3h[0]) {
+       case BSS_MAP_MSG_ASSIGNMENT_COMPLETE:
+               ret = bssmap_ran_handle_assignment_complete(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 5a918ba..541dfc3 100644
--- a/src/osmo-bsc-nat/subscr_conn.c
+++ b/src/osmo-bsc-nat/subscr_conn.c
@@ -26,6 +26,8 @@
 #include <osmocom/bsc_nat/subscr_conn.h>
 #include <osmocom/bsc_nat/logging.h>

+extern struct osmo_fsm subscr_conn_fsm;
+
 /* Get the next available id in either CN or RAN. */
 int subscr_conn_get_next_id(enum subscr_conn_id_type id_type)
 {
@@ -38,6 +40,9 @@
        case SUBSCR_CONN_ID_TYPE_CONN_ID_RAN:
                id = &g_bsc_nat->ran.subscr_conn_id_next;
                break;
+       case SUBSCR_CONN_ID_TYPE_CALL_ID_MGW:
+               id = &g_bsc_nat->mgw.call_id_next;
+               break;
        default:
                OSMO_ASSERT(false);
        }
@@ -58,6 +63,9 @@
                        case SUBSCR_CONN_ID_TYPE_CONN_ID_RAN:
                                subscr_id = subscr_conn->ran.id;
                                break;
+                       case SUBSCR_CONN_ID_TYPE_CALL_ID_MGW:
+                               subscr_id = subscr_conn->mgw_call_id;
+                               break;
                        default:
                                OSMO_ASSERT(false);
                        }
@@ -84,6 +92,9 @@

        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;
@@ -112,5 +123,6 @@
 {
        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..b372379
--- /dev/null
+++ b/src/osmo-bsc-nat/subscr_conn_fsm.c
@@ -0,0 +1,456 @@
+/* (C) 2022 by sysmocom - s.f.m.c. GmbH <[email protected]>
+ * Author: Oliver Smith <[email protected]>
+ * 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 <inttypes.h>
+#include <osmocom/core/fsm.h>
+#include <osmocom/gsm/gsm0808.h>
+#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
+#include <osmocom/mgcp_client/mgcp_client_fsm.h>
+#include <osmocom/sigtran/sccp_helpers.h>
+#include <osmocom/bsc_nat/bsc_nat.h>
+#include <osmocom/bsc_nat/bssap.h>
+#include <osmocom/bsc_nat/logging.h>
+#include <osmocom/bsc_nat/subscr_conn.h>
+#include <osmocom/bsc_nat/subscr_conn_fsm.h>
+
+#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 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)
+{
+       struct subscr_conn *subscr_conn = (struct subscr_conn *)fi->priv;
+
+       switch (event) {
+       case SUBSCR_CONN_FSM_EV_BSSMAP_ASSIGNMENT_REQUEST:
+               subscr_conn->cn.ass_req = *(struct bssmap_assignment_rqst 
*)data;
+               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. */
+       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_assignment_request_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;
+
+       /* 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));
+
+       subscr_conn->mgw_call_id = 
subscr_conn_get_next_id(SUBSCR_CONN_ID_TYPE_CALL_ID_MGW);
+       crcx_info.call_id = subscr_conn->mgw_call_id;
+
+       LOGPFSML(fi, LOGL_DEBUG, "Set MGW call_id=%" PRIu32 " for %s\n", 
subscr_conn->mgw_call_id,
+                talloc_get_name(subscr_conn));
+
+       /* MGCP CRCX CN */
+       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_assignment_request_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_assignment_request_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 bssmap_assignment_rqst *ass_req = &subscr_conn->cn.ass_req;
+
+       /* Set RTP addr + port */
+       osmo_static_assert(sizeof(mdcx_info.addr) == 
sizeof(ass_req->aoip_transp_addr.ip), sizeof_addr);
+       memcpy(mdcx_info.addr, ass_req->aoip_transp_addr.ip, 
sizeof(mdcx_info.addr));
+       mdcx_info.port = ass_req->aoip_transp_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_assignment_request_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_assignment_request_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 int tx_assignment_request_to_ran(const struct subscr_conn *subscr_conn)
+{
+       const struct mgcp_conn_peer *rtp_info;
+       const struct bssmap_assignment_rqst *ass_req = &subscr_conn->cn.ass_req;
+       struct msgb *msg;
+       struct sockaddr_storage ss;
+       struct osmo_sockaddr_str aoip_transp_addr;
+
+       /* 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;
+       }
+
+       msg = gsm0808_create_ass2(&ass_req->ct,
+                                 NULL,
+                                 &ss,
+                                 &ass_req->scl,
+                                 ass_req->ci_present ? &ass_req->ci : NULL,
+                                 ass_req->kc_present ? &ass_req->kc : NULL,
+                                 ass_req->lcls_present ? &ass_req->lcls : 
NULL);
+
+       return osmo_sccp_tx_data_msg(g_bsc_nat->ran.sccp_inst->scu, 
subscr_conn->ran.id, msg);
+}
+
+static void st_processing_assignment_request_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_assignment_request_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_assignment_complete(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_BSSMAP_ASSIGNMENT_COMPLETE:
+               subscr_conn->ran.ass_compl = *(struct 
bssmap_assignment_complete *)data;
+               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_assignment_complete_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 bssmap_assignment_complete *ass_compl = 
&subscr_conn->ran.ass_compl;
+
+       /* Set RTP addr + port */
+       osmo_static_assert(sizeof(mdcx_info.addr) == 
sizeof(ass_compl->aoip_transp_addr.ip), sizeof_addr);
+       memcpy(mdcx_info.addr, ass_compl->aoip_transp_addr.ip, 
sizeof(mdcx_info.addr));
+       mdcx_info.port = ass_compl->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 int tx_assignment_complete_to_cn(const struct subscr_conn *subscr_conn)
+{
+       const struct mgcp_conn_peer *rtp_info;
+       const struct bssmap_assignment_complete *ass_compl = 
&subscr_conn->ran.ass_compl;
+       struct msgb *msg;
+       struct sockaddr_storage ss;
+       struct osmo_sockaddr_str aoip_transp_addr;
+
+       /* 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;
+       }
+
+       msg = gsm0808_create_ass_compl2(ass_compl->rr_cause,
+                                       ass_compl->chosen_channel,
+                                       ass_compl->chosen_encr_alg,
+                                       ass_compl->speech_version,
+                                       &ss,
+                                       &ass_compl->sc,
+                                       ass_compl->scl_present ? 
&ass_compl->scl : NULL,
+                                       ass_compl->lcls_bss_status_present ? 
ass_compl->lcls_bss_status : 0);
+
+       return osmo_sccp_tx_data_msg(g_bsc_nat->cn.sccp_inst->scu, 
subscr_conn->cn.id, msg);
+}
+
+static void st_processing_assignment_complete_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_assignment_complete_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_assignment_request_crcx_cn,
+               .onenter = st_processing_assignment_request_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_assignment_request_mdcx_cn,
+               .onenter = st_processing_assignment_request_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_assignment_request_crcx_ran,
+               .onenter = st_processing_assignment_request_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_assignment_complete,
+       },
+       [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_assignment_complete_mdcx_ran,
+               .onenter = st_processing_assignment_complete_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 __attribute__((constructor)) void subscr_conn_fsm_init(void)
+{
+       OSMO_ASSERT(osmo_fsm_register(&subscr_conn_fsm) == 0);
+}

--
To view, visit https://gerrit.osmocom.org/c/osmo-bsc-nat/+/27712
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-bsc-nat
Gerrit-Branch: master
Gerrit-Change-Id: I7e491aada6f5db0eb35ef2039869c6ba07f9ca3b
Gerrit-Change-Number: 27712
Gerrit-PatchSet: 1
Gerrit-Owner: osmith <[email protected]>
Gerrit-MessageType: newchange

Reply via email to