Harald Welte has uploaded this change for review. ( 
https://gerrit.osmocom.org/9416


Change subject: WIP: Add initial 3GPP LCLS support to OsmoBSC
......................................................................

WIP: Add initial 3GPP LCLS support to OsmoBSC

This code doesn't yet actually change the media path to the local
loopback, but it contains the following code:
* receive/parse/interpret LCLS specific BSSMAP IEs and PDUs
* osmo_fsm handling the various states and their transitions
* call leg correlation (finding the other subscr_conn with same GCR)
* communication between the two call-leg LCLS FSMs
* detection of supported / unsupported LCLS configurations
* display of GCR / LCLS information in "show conns"

Change-Id: I614fade62834def5cafc94c4d2578cd747a3f9f7
---
M include/osmocom/bsc/Makefile.am
M include/osmocom/bsc/debug.h
M include/osmocom/bsc/gsm_data.h
A include/osmocom/bsc/osmo_bsc_lcls.h
M src/libbsc/Makefile.am
M src/libbsc/bsc_subscr_conn_fsm.c
M src/libbsc/bsc_vty.c
A src/libbsc/osmo_bsc_lcls.c
M src/osmo-bsc/osmo_bsc_bssap.c
M src/osmo-bsc/osmo_bsc_main.c
10 files changed, 818 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/16/9416/1

diff --git a/include/osmocom/bsc/Makefile.am b/include/osmocom/bsc/Makefile.am
index bae13f0..0987be9 100644
--- a/include/osmocom/bsc/Makefile.am
+++ b/include/osmocom/bsc/Makefile.am
@@ -47,4 +47,5 @@
        vty.h \
        bsc_api.h \
        penalty_timers.h \
+       osmo_bsc_lcls.h \
        $(NULL)
diff --git a/include/osmocom/bsc/debug.h b/include/osmocom/bsc/debug.h
index 2b3d150..1133bf6 100644
--- a/include/osmocom/bsc/debug.h
+++ b/include/osmocom/bsc/debug.h
@@ -23,5 +23,6 @@
        DCTRL,
        DFILTER,
        DPCU,
+       DLCLS,
        Debug_LastEntry,
 };
diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h
index 1cf79a5..b1fceb3 100644
--- a/include/osmocom/bsc/gsm_data.h
+++ b/include/osmocom/bsc/gsm_data.h
@@ -188,6 +188,18 @@
                enum gsm48_chan_mode chan_mode;

        } user_plane;
+
+       /* LCLS (local call, local switch) related state */
+       struct {
+               uint8_t global_call_ref[15];
+               uint8_t global_call_ref_len; /* length of global_call_ref */
+               uint8_t config; /* TS 48.008 3.2.2.116 */
+               uint8_t control;/* TS 48.008 3.2.2.117 */
+               /* LCLS FSM */
+               struct osmo_fsm_inst *fi;
+               /* pointer to "other" connection, if Call Leg Relocation was 
successful */
+               struct gsm_subscriber_connection *other;
+       } lcls;
 };


diff --git a/include/osmocom/bsc/osmo_bsc_lcls.h 
b/include/osmocom/bsc/osmo_bsc_lcls.h
new file mode 100644
index 0000000..cb43cf3
--- /dev/null
+++ b/include/osmocom/bsc/osmo_bsc_lcls.h
@@ -0,0 +1,36 @@
+#pragma once
+#include <osmocom/core/fsm.h>
+
+enum lcls_fsm_state {
+       ST_NO_LCLS,
+       ST_NOT_YET_LS,
+       ST_NOT_POSSIBLE_LS,
+       ST_NO_LONGER_LS,
+       ST_REQ_LCLS_NOT_SUPP,
+       ST_LOCALLY_SWITCHED,
+       /* locally switched; received remote break; wait for "local" break */
+       ST_LOCALLY_SWITCHED_WAIT_BREAK,
+       /* locally switched; received break; wait for "other" break */
+       ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK,
+};
+
+enum lcls_event {
+       /* update LCLS config/control based on some BSSMAP signaling */
+       LCLS_EV_UPDATE_CFG_CSC,
+       /* we have been identified as the correlation peer of another conn */
+       LCLS_EV_CORRELATED,
+       /* "other" LCLS connection has enabled local switching */
+       LCLS_EV_OTHER_ENABLED,
+       /* "other" LCLS connection is breaking local switch */
+       LCLS_EV_OTHER_BREAK,
+       /* "other" LCLS connection is dying */
+       LCLS_EV_OTHER_DEAD,
+};
+
+enum gsm0808_lcls_status lcls_get_status(struct gsm_subscriber_connection 
*conn);
+
+void lcls_update_config(struct gsm_subscriber_connection *conn,
+                       const uint8_t *config, const uint8_t *control);
+
+extern struct osmo_fsm lcls_fsm;
+
diff --git a/src/libbsc/Makefile.am b/src/libbsc/Makefile.am
index d215e14..2e44729 100644
--- a/src/libbsc/Makefile.am
+++ b/src/libbsc/Makefile.am
@@ -65,5 +65,6 @@
        handover_decision_2.c \
        bsc_subscr_conn_fsm.c \
        meas_feed.c \
+       osmo_bsc_lcls.c \
        $(NULL)

diff --git a/src/libbsc/bsc_subscr_conn_fsm.c b/src/libbsc/bsc_subscr_conn_fsm.c
index 89ac482..46504c4 100644
--- a/src/libbsc/bsc_subscr_conn_fsm.c
+++ b/src/libbsc/bsc_subscr_conn_fsm.c
@@ -30,6 +30,7 @@
 #include <osmocom/bsc/chan_alloc.h>
 #include <osmocom/bsc/bsc_subscriber.h>
 #include <osmocom/bsc/osmo_bsc_sigtran.h>
+#include <osmocom/bsc/osmo_bsc_lcls.h>
 #include <osmocom/bsc/bsc_subscr_conn_fsm.h>
 #include <osmocom/bsc/osmo_bsc.h>
 #include <osmocom/bsc/penalty_timers.h>
@@ -223,6 +224,33 @@
        return channel_mode << 4 | channel;
 }

+/* Add the LCLS BSS Status IE to a BSSMAP message. We assume this is
+ * called on a msgb that was returned by gsm0808_create_ass_compl() */
+static void bssmap_add_lcls_status(struct msgb *msg, enum gsm0808_lcls_status 
status)
+{
+       OSMO_ASSERT(msg->l3h[0] == BSSAP_MSG_BSS_MANAGEMENT);
+       OSMO_ASSERT(msg->l3h[2] == BSS_MAP_MSG_ASSIGMENT_COMPLETE ||
+                   msg->l3h[2] == BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE ||
+                   msg->l3h[2] == BSS_MAP_MSG_HANDOVER_COMPLETE ||
+                   msg->l3h[2] == BSS_MAP_MSG_HANDOVER_PERFORMED);
+       OSMO_ASSERT(msgb_tailroom(msg) >= 2);
+
+       /* append IE to end of message */
+       msgb_tv_put(msg, GSM0808_IE_LCLS_BSS_STATUS, status);
+       /* increment the "length" byte in the BSSAP header */
+       msg->l3h[1] += 2;
+}
+
+/* Add (append) the LCLS BSS Status IE to a BSSMAP message, if there is any 
LCLS
+ * active on the given \a conn */
+static void bssmap_add_lcls_status_if_needed(struct gsm_subscriber_connection 
*conn,
+                                            struct msgb *msg)
+{
+       enum gsm0808_lcls_status status = lcls_get_status(conn);
+       if (status != 0xff)
+               bssmap_add_lcls_status(msg, status);
+}
+
 /* Generate and send assignment complete message */
 static void send_ass_compl(struct gsm_lchan *lchan, struct osmo_fsm_inst *fi, 
bool voice)
 {
@@ -268,6 +296,9 @@
                         conn->sccp.conn_id);
        }

+       /* Add LCLS BSS-Status IE in case there is any LCLS status for this 
connection */
+       bssmap_add_lcls_status_if_needed(conn, resp);
+
        sigtran_send(conn, resp, fi);
 }

@@ -1026,6 +1057,15 @@
                conn->lchan = NULL;
        }

+       if (conn->lcls.other) {
+               LOGPFSML(fi, LOGL_DEBUG, "Signaling death to LCLS peern\n");
+               osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_DEAD, conn);
+               conn->lcls.other = NULL;
+       }
+
+       if (conn->lcls.fi)
+               osmo_fsm_inst_term(conn->lcls.fi, cause, NULL);
+
        if (conn->bsub) {
                LOGPFSML(fi, LOGL_DEBUG, "Putting bsc_subscr\n");
                bsc_subscr_put(conn->bsub);
@@ -1117,6 +1157,7 @@

        if (!g_initialized) {
                osmo_fsm_register(&gscon_fsm);
+               osmo_fsm_register(&lcls_fsm);
                g_initialized = true;
        }

@@ -1137,6 +1178,15 @@
                return NULL;
        }

+       /* initialize to some magic values that indicate "IE not [yet] 
received" */
+       conn->lcls.config = 0xff;
+       conn->lcls.control = 0xff;
+       conn->lcls.fi = osmo_fsm_inst_alloc(&lcls_fsm, conn, conn, LOGL_NOTICE, 
NULL);
+       if (!conn->lcls.fi) {
+               osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
+               return NULL;
+       }
+
        llist_add_tail(&conn->entry, &net->subscr_conns);
        return conn;
 }
diff --git a/src/libbsc/bsc_vty.c b/src/libbsc/bsc_vty.c
index 1b76578..42d0af4 100644
--- a/src/libbsc/bsc_vty.c
+++ b/src/libbsc/bsc_vty.c
@@ -1521,6 +1521,14 @@
                conn->sccp.conn_id, conn->sccp.msc->nr, conn->hodec2.failures,
                get_value_string(gsm48_chan_mode_names, 
conn->user_plane.chan_mode),
                conn->user_plane.mgw_endpoint, VTY_NEWLINE);
+       if (conn->lcls.global_call_ref_len) {
+               vty_out(vty, " LCLS GCR: %s%s",
+                       osmo_hexdump_nospc(conn->lcls.global_call_ref, 
conn->lcls.global_call_ref_len),
+                       VTY_NEWLINE);
+               vty_out(vty, " LCLS Config: 0x%02x, LCLS Control: 0x%02x, LCLS 
BSS Status: %s%s",
+                       conn->lcls.config, conn->lcls.control, 
osmo_fsm_inst_state_name(conn->lcls.fi),
+                       VTY_NEWLINE);
+       }
        lchan_dump_full_vty(vty, conn->lchan);
        if (conn->secondary_lchan)
                lchan_dump_full_vty(vty, conn->secondary_lchan);
diff --git a/src/libbsc/osmo_bsc_lcls.c b/src/libbsc/osmo_bsc_lcls.c
new file mode 100644
index 0000000..c00c9cd
--- /dev/null
+++ b/src/libbsc/osmo_bsc_lcls.c
@@ -0,0 +1,646 @@
+/* (C) 2018 by Harald Welte <[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/licenses/>.
+ *
+ */
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/gsm/gsm0808.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/bsc/bsc_msc_data.h>
+#include <osmocom/bsc/debug.h>
+#include <osmocom/bsc/osmo_bsc.h>
+#include <osmocom/bsc/bsc_subscr_conn_fsm.h>
+#include <osmocom/bsc/gsm_data.h>
+#include <osmocom/bsc/osmo_bsc_lcls.h>
+
+struct value_string lcls_event_names[] = {
+       { LCLS_EV_UPDATE_CFG_CSC,       "UPDATE_CFG_CSC" },
+       { LCLS_EV_CORRELATED,           "CORRELATED" },
+       { LCLS_EV_OTHER_ENABLED,        "OTHER_ENABLED" },
+       { LCLS_EV_OTHER_BREAK,          "OTHER_BREAK" },
+       { LCLS_EV_OTHER_DEAD,           "OTHER_DEAD" },
+       { 0, NULL }
+};
+
+
+/***********************************************************************
+ * Utility functions
+ ***********************************************************************/
+
+enum gsm0808_lcls_status lcls_get_status(struct gsm_subscriber_connection 
*conn)
+{
+       if (!conn->lcls.fi)
+               return 0xff;
+
+       switch (conn->lcls.fi->state) {
+       case ST_NO_LCLS:
+               return 0xff;
+       case ST_NOT_YET_LS:
+               return GSM0808_LCLS_STS_NOT_YET_LS;
+       case ST_NOT_POSSIBLE_LS:
+               return GSM0808_LCLS_STS_NOT_POSSIBLE_LS;
+       case ST_NO_LONGER_LS:
+               return GSM0808_LCLS_STS_NO_LONGER_LS;
+       case ST_REQ_LCLS_NOT_SUPP:
+               return GSM0808_LCLS_STS_REQ_LCLS_NOT_SUPP;
+       case ST_LOCALLY_SWITCHED:
+       case ST_LOCALLY_SWITCHED_WAIT_BREAK:
+       case ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK:
+               return GSM0808_LCLS_STS_LOCALLY_SWITCHED;
+       }
+       OSMO_ASSERT(0);
+}
+
+static void lcls_send_notify(struct gsm_subscriber_connection *conn)
+{
+       enum gsm0808_lcls_status status = lcls_get_status(conn);
+       struct msgb *msg;
+
+       if (status == 0xff)
+               return;
+
+       LOGPFSM(conn->lcls.fi, "Sending BSSMAP LCLS NOTIFICATION\n");
+       msg = gsm0808_create_lcls_notification(status, false);
+       osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_TX_SCCP, msg);
+}
+
+static struct gsm_subscriber_connection *
+find_conn_with_same_gcr(struct gsm_subscriber_connection *conn_local)
+{
+       struct gsm_network *net = conn_local->network;
+       struct gsm_subscriber_connection *conn_other;
+
+       llist_for_each_entry(conn_other, &net->subscr_conns, entry) {
+               /* don't report back the same connection */
+               if (conn_other == conn_local)
+                       continue;
+               /* don't consider any conn where GCR length is not the same as 
before */
+               if (conn_other->lcls.global_call_ref_len != 
conn_local->lcls.global_call_ref_len)
+                       continue;
+               if (!memcmp(conn_other->lcls.global_call_ref, 
conn_local->lcls.global_call_ref,
+                           conn_local->lcls.global_call_ref_len))
+                       return conn_other;
+       }
+       return NULL;
+}
+
+static bool lcls_is_supported_config(enum gsm0808_lcls_config cfg)
+{
+       /* this is the only configuration that we support for now */
+       if (cfg == GSM0808_LCLS_CFG_BOTH_WAY)
+               return true;
+       else
+               return false;
+}
+
+/* LCLS Call Leg Correlation as per 23.284 4.3 / 48.008 3.1.33.2.1 */
+static int lcls_perform_correlation(struct gsm_subscriber_connection 
*conn_local)
+{
+       struct gsm_subscriber_connection *conn_other;
+
+       OSMO_ASSERT(conn_local->lcls.global_call_ref_len);
+       OSMO_ASSERT(conn_local->lcls.other == NULL);
+
+       conn_other = find_conn_with_same_gcr(conn_local);
+       if (!conn_other) {
+               /* we found no other call with same GCR: not possible */
+               return -ENODEV;
+       }
+
+       /* store pointer to "other" in "local" */
+       conn_local->lcls.other = conn_other;
+       /* notify other conn about our correlation */
+       osmo_fsm_inst_dispatch(conn_other->lcls.fi, LCLS_EV_CORRELATED, 
conn_local);
+
+       return 0;
+}
+
+
+struct lcls_cfg_csc {
+       enum gsm0808_lcls_config config;
+       enum gsm0808_lcls_control control;
+};
+
+/* Update the connections LCLS configuration and return old/previous 
configuration.
+ * \returns (staticallly allocated) old configuration; NULL if new config not 
supported */
+static struct lcls_cfg_csc *update_lcls_cfg_csc(struct 
gsm_subscriber_connection *conn,
+                                               struct lcls_cfg_csc 
*new_cfg_csc)
+{
+       static struct lcls_cfg_csc old_cfg_csc;
+       old_cfg_csc.config = conn->lcls.config;
+       old_cfg_csc.control = conn->lcls.control;
+
+       if (new_cfg_csc->config != 0xff) {
+               if (!lcls_is_supported_config(new_cfg_csc->config))
+                       return NULL;
+               if (conn->lcls.config != new_cfg_csc->config) {
+                       /* TODO: logging */
+                       conn->lcls.config = new_cfg_csc->config;
+               }
+       }
+       if (new_cfg_csc->control != 0xff) {
+               if (conn->lcls.control != new_cfg_csc->control) {
+                       /* TODO: logging */
+                       conn->lcls.control = new_cfg_csc->control;
+               }
+       }
+
+       return &old_cfg_csc;
+}
+
+/* Attempt to update conn->lcls with the new config/csc provided. If new 
config is
+ * unsupported, change into LCLS NOT SUPPORTED state and return -EINVAL. */
+static int lcls_handle_cfg_update(struct gsm_subscriber_connection *conn, void 
*data)
+{
+       struct lcls_cfg_csc *new_cfg_csc, *old_cfg_csc;
+
+       new_cfg_csc = (struct lcls_cfg_csc *) data;
+       old_cfg_csc = update_lcls_cfg_csc(conn, new_cfg_csc);
+       if (!old_cfg_csc) {
+               osmo_fsm_inst_state_chg(conn->lcls.fi, ST_REQ_LCLS_NOT_SUPP, 0, 
0);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+/* notify the LCLS FSM about new LCLS Config and/or CSC */
+void lcls_update_config(struct gsm_subscriber_connection *conn,
+                       const uint8_t *config, const uint8_t *control)
+{
+       struct lcls_cfg_csc new_cfg = {
+               .config = 0xff,
+               .control = 0xff,
+       };
+       /* nothing to update, skip it */
+       if (!config && !control)
+               return;
+       if (config)
+               new_cfg.config = *config;
+       if (control)
+               new_cfg.control = *control;
+       osmo_fsm_inst_dispatch(conn->lcls.fi, LCLS_EV_UPDATE_CFG_CSC, &new_cfg);
+}
+
+static int lcls_try_enable(struct gsm_subscriber_connection *conn)
+{
+       struct gsm_subscriber_connection *other_conn = conn->lcls.other;
+       OSMO_ASSERT(other_conn);
+
+       if (!lcls_is_supported_config(conn->lcls.config)) {
+               LOGPFSM(conn->lcls.fi, "Not enabling due to unsupported local 
config\n");
+               return -1;
+       }
+
+       if (!lcls_is_supported_config(other_conn->lcls.config)) {
+               LOGPFSM(conn->lcls.fi, "Not enabling due to unsupported other 
config\n");
+               return -1;
+       }
+
+       if (conn->lcls.control != GSM0808_LCLS_CSC_CONNECT) {
+               LOGPFSM(conn->lcls.fi, "Not enabling due to insufficient local 
control\n");
+               return -1;
+       }
+
+       if (other_conn->lcls.control != GSM0808_LCLS_CSC_CONNECT) {
+               LOGPFSM(conn->lcls.fi, "Not enabling due to insufficient other 
control\n");
+               return -1;
+       }
+
+       return 0;
+}
+
+/***********************************************************************
+ * State callback functions
+ ***********************************************************************/
+
+static void lcls_no_lcls_fn(struct osmo_fsm_inst *fi, uint32_t event, void 
*data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       /* we're just starting and cannot yet have a correlated call */
+       OSMO_ASSERT(conn->lcls.other == NULL);
+
+       /* If there's no GCR set, we can never leave this state */
+       if (conn->lcls.global_call_ref_len == 0) {
+               LOGPFSML(fi, LOGL_NOTICE, "No GCR set, ignoring %s\n",
+                        osmo_fsm_event_name(fi->fsm, event));
+               return;
+       }
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               if (lcls_perform_correlation(conn) != 0) {
+                       /* Correlation leads to no result: Not Possible to LS */
+                       osmo_fsm_inst_state_chg(fi, ST_NOT_POSSIBLE_LS, 0, 0);
+                       return;
+               }
+               /* we now have two correlated calls */
+               OSMO_ASSERT(conn->lcls.other);
+               if (lcls_try_enable(conn) != 0) {
+                       /* Couldn't be enabled: Not yet LS */
+                       osmo_fsm_inst_state_chg(fi, ST_NOT_YET_LS, 0, 0);
+               } else {
+                       /* Local Switching now active */
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_ENABLED, conn);
+               }
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+}
+
+static void lcls_not_yet_ls_fn(struct osmo_fsm_inst *fi, uint32_t event, void 
*data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       /* not yet locally switched means that we have correlation but no 
instruction
+        * to actually connect them yet */
+       OSMO_ASSERT(conn->lcls.other);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               if (lcls_try_enable(conn) == 0) {
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_ENABLED, conn);
+               }
+               break;
+       case LCLS_EV_OTHER_ENABLED:
+               OSMO_ASSERT(conn->lcls.other == data);
+               if (lcls_try_enable(conn) == 0) {
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       /* Send LCLS-NOTIFY to inform MSC */
+                       lcls_send_notify(conn);
+               } else {
+                       /* we couldn't enable our side, so ask other side to 
break */
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_BREAK, conn);
+               }
+               break;
+       case LCLS_EV_OTHER_DEAD:
+               OSMO_ASSERT(conn->lcls.other == data);
+               conn->lcls.other = NULL;
+               osmo_fsm_inst_state_chg(fi, ST_NOT_POSSIBLE_LS, 0, 0);
+               /* Send LCLS-NOTIFY to inform MSC */
+               lcls_send_notify(conn);
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+}
+
+static void lcls_not_possible_ls_fn(struct osmo_fsm_inst *fi, uint32_t event, 
void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       OSMO_ASSERT(conn->lcls.other == NULL);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               if (lcls_perform_correlation(conn) != 0) {
+                       /* no correlation result: Remain in NOT_POSSIBLE_LS */
+                       return;
+               }
+               /* we now have two correlated calls */
+               OSMO_ASSERT(conn->lcls.other);
+               if (lcls_try_enable(conn) != 0)
+                       osmo_fsm_inst_state_chg(fi, ST_NOT_YET_LS, 0, 0);
+               else {
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_ENABLED, conn);
+               }
+               break;
+       case LCLS_EV_CORRELATED:
+               /* other call informs us that he correlated with us */
+               conn->lcls.other = data;
+               osmo_fsm_inst_state_chg(fi, ST_NOT_YET_LS, 0, 0);
+               /* Send NOTIFY about the fact that correlation happened */
+               lcls_send_notify(conn);
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+}
+
+static void lcls_no_longer_ls_fn(struct osmo_fsm_inst *fi, uint32_t event, 
void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       OSMO_ASSERT(conn->lcls.other);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               if (lcls_try_enable(conn) == 0) {
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_ENABLED, conn);
+               }
+               break;
+       case LCLS_EV_OTHER_ENABLED:
+               OSMO_ASSERT(conn->lcls.other == data);
+               if (lcls_try_enable(conn) == 0) {
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+                       /* Send LCLS-NOTIFY to inform MSC */
+                       lcls_send_notify(conn);
+               } else {
+                       /* we couldn't enable our side, so ask other side to 
break */
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_BREAK, conn);
+               }
+               break;
+       case LCLS_EV_OTHER_DEAD:
+               OSMO_ASSERT(conn->lcls.other == data);
+               conn->lcls.other = NULL;
+               osmo_fsm_inst_state_chg(fi, ST_NOT_POSSIBLE_LS, 0, 0);
+               /* Send LCLS-NOTIFY to inform MSC */
+               lcls_send_notify(conn);
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+}
+
+static void lcls_req_lcls_not_supp_fn(struct osmo_fsm_inst *fi, uint32_t 
event, void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       /* we could have a correlated other call or not */
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               if (lcls_perform_correlation(conn) != 0) {
+                       osmo_fsm_inst_state_chg(fi, ST_NOT_POSSIBLE_LS, 0, 0);
+                       return;
+               }
+               /* we now have two correlated calls */
+               OSMO_ASSERT(conn->lcls.other);
+               if (lcls_try_enable(conn) != 0)
+                       osmo_fsm_inst_state_chg(fi, ST_NOT_YET_LS, 0, 0);
+               else
+                       osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED, 0, 0);
+               break;
+       case LCLS_EV_OTHER_DEAD:
+               OSMO_ASSERT(conn->lcls.other == data);
+               conn->lcls.other = NULL;
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+
+}
+
+static void lcls_locally_switched_fn(struct osmo_fsm_inst *fi, uint32_t event, 
void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+       struct lcls_cfg_csc *cfg = NULL;
+
+       OSMO_ASSERT(conn->lcls.other);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               cfg = data;
+               if (cfg->control == GSM0808_LCLS_CSC_RELEASE_LCLS) {
+                       osmo_fsm_inst_state_chg(fi, 
ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_BREAK, conn);
+                       /* FIXME: what if there's a new config included? */
+                       return;
+               }
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               /* TODO: Handle any changes of "config" once we support 
bi-casting etc. */
+               break;
+       case LCLS_EV_OTHER_BREAK:
+               OSMO_ASSERT(conn->lcls.other == data);
+               osmo_fsm_inst_state_chg(fi, ST_LOCALLY_SWITCHED_WAIT_BREAK, 0, 
0);
+               break;
+       case LCLS_EV_OTHER_DEAD:
+               OSMO_ASSERT(conn->lcls.other == data);
+               conn->lcls.other = NULL;
+               osmo_fsm_inst_state_chg(fi, ST_NOT_POSSIBLE_LS, 0, 0);
+               /* Send LCLS-NOTIFY to inform MSC */
+               lcls_send_notify(conn);
+               break;
+       default:
+               OSMO_ASSERT(0);
+               break;
+       }
+}
+
+static void lcls_locally_switched_onenter(struct osmo_fsm_inst *fi, uint32_t 
prev_state)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       OSMO_ASSERT(conn->lcls.other);
+
+       /* FIXME: actually enable local switching */
+       LOGPFSM(fi, "=== HERE IS WHERE WE ENABLE LCLS\n");
+}
+
+static void lcls_break_local_switching(struct gsm_subscriber_connection *conn)
+{
+       OSMO_ASSERT(conn->lcls.other);
+
+       /* FIXME: actually disable local switching */
+       LOGPFSM(conn->lcls.fi, "=== HERE IS WHERE WE DISABLE LCLS\n");
+}
+
+static void lcls_locally_switched_wait_break_fn(struct osmo_fsm_inst *fi, 
uint32_t event, void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+       struct lcls_cfg_csc *cfg = NULL;
+
+       OSMO_ASSERT(conn->lcls.other);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               cfg = data;
+               if (cfg->control == GSM0808_LCLS_CSC_RELEASE_LCLS) {
+                       lcls_break_local_switching(conn);
+                       osmo_fsm_inst_state_chg(fi, ST_NO_LONGER_LS, 0, 0);
+                       osmo_fsm_inst_dispatch(conn->lcls.other->lcls.fi, 
LCLS_EV_OTHER_BREAK, conn);
+                       /* no NOTIFY here, as the caller will be returning 
status in LCLS-CTRL-ACK */
+                       /* FIXME: what if there's a new config included? */
+                       return;
+               }
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               /* TODO: Handle any changes of "config" once we support 
bi-casting etc. */
+               break;
+       case LCLS_EV_OTHER_BREAK:
+               /* we simply ignore it, must be a re-transmission */
+               break;
+       case LCLS_EV_OTHER_DEAD:
+       default:
+               lcls_locally_switched_fn(fi, event, data);
+               break;
+       }
+}
+
+static void lcls_locally_switched_wait_other_break_fn(struct osmo_fsm_inst 
*fi, uint32_t event, void *data)
+{
+       struct gsm_subscriber_connection *conn = fi->priv;
+
+       OSMO_ASSERT(conn->lcls.other);
+
+       switch (event) {
+       case LCLS_EV_UPDATE_CFG_CSC:
+               if (lcls_handle_cfg_update(conn, data) != 0)
+                       return;
+               /* TODO: Handle any changes of "config" once we support 
bi-casting etc. */
+               break;
+       case LCLS_EV_OTHER_BREAK:
+               OSMO_ASSERT(conn->lcls.other == data);
+               lcls_break_local_switching(conn);
+               osmo_fsm_inst_state_chg(fi, ST_NO_LONGER_LS, 0, 0);
+               /* Send LCLS-NOTIFY to inform MSC */
+               lcls_send_notify(conn);
+               break;
+       case LCLS_EV_OTHER_DEAD:
+       default:
+               lcls_locally_switched_fn(fi, event, data);
+               break;
+       }
+}
+
+
+
+/***********************************************************************
+ * FSM Definition
+ ***********************************************************************/
+
+#define S(x) (1 << (x))
+
+static const struct osmo_fsm_state lcls_fsm_states[] = {
+       [ST_NO_LCLS] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_CORRELATED),
+               .out_state_mask = S(ST_NO_LCLS) |
+                                 S(ST_NOT_YET_LS) |
+                                 S(ST_NOT_POSSIBLE_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "NO_LCLS",
+               .action = lcls_no_lcls_fn,
+       },
+       [ST_NOT_YET_LS] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_CORRELATED) |
+                                S(LCLS_EV_OTHER_ENABLED) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NOT_YET_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "NOT_YET_LS",
+               .action = lcls_not_yet_ls_fn,
+       },
+       [ST_NOT_POSSIBLE_LS] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_CORRELATED),
+               .out_state_mask = S(ST_NOT_YET_LS) |
+                                 S(ST_NOT_POSSIBLE_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "NOT_POSSIBLE_LS",
+               .action = lcls_not_possible_ls_fn,
+       },
+       [ST_NO_LONGER_LS] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_CORRELATED) |
+                                S(LCLS_EV_OTHER_ENABLED) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NO_LONGER_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "NO_LONGER_LS",
+               .action = lcls_no_longer_ls_fn,
+       },
+       [ST_REQ_LCLS_NOT_SUPP] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NOT_YET_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "REQ_LCLS_NOT_SUPP",
+               .action = lcls_req_lcls_not_supp_fn,
+       },
+       [ST_LOCALLY_SWITCHED] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_OTHER_BREAK) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NO_LONGER_LS) |
+                                 S(ST_NOT_POSSIBLE_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED_WAIT_BREAK) |
+                                 S(ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK) |
+                                 S(ST_LOCALLY_SWITCHED),
+               .name = "LOCALLY_SWITCHED",
+               .action = lcls_locally_switched_fn,
+               .onenter = lcls_locally_switched_onenter,
+       },
+       /* received an "other" break, waiting for the local break */
+       [ST_LOCALLY_SWITCHED_WAIT_BREAK] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_OTHER_BREAK) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NO_LONGER_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED) |
+                                 S(ST_LOCALLY_SWITCHED_WAIT_BREAK),
+               .name = "LOCALLY_SWITCHED_WAIT_BREAK",
+               .action = lcls_locally_switched_wait_break_fn,
+       },
+       /* received a local break, waiting for the "other" break */
+       [ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK] = {
+               .in_event_mask = S(LCLS_EV_UPDATE_CFG_CSC) |
+                                S(LCLS_EV_OTHER_BREAK) |
+                                S(LCLS_EV_OTHER_DEAD),
+               .out_state_mask = S(ST_NO_LONGER_LS) |
+                                 S(ST_REQ_LCLS_NOT_SUPP) |
+                                 S(ST_LOCALLY_SWITCHED) |
+                                 S(ST_LOCALLY_SWITCHED_WAIT_OTHER_BREAK),
+               .name = "LOCALLY_SWITCHED_WAIT_OTHER_BREAK",
+               .action = lcls_locally_switched_wait_other_break_fn,
+       },
+
+
+};
+
+struct osmo_fsm lcls_fsm = {
+       .name = "LCLS",
+       .states = lcls_fsm_states,
+       .num_states = ARRAY_SIZE(lcls_fsm_states),
+       .allstate_event_mask = 0,
+       .allstate_action = NULL,
+       .cleanup = NULL,
+       .timer_cb = NULL,
+       .log_subsys = DLCLS,
+       .event_names = lcls_event_names,
+};
diff --git a/src/osmo-bsc/osmo_bsc_bssap.c b/src/osmo-bsc/osmo_bsc_bssap.c
index f7f99fa..ae8407d 100644
--- a/src/osmo-bsc/osmo_bsc_bssap.c
+++ b/src/osmo-bsc/osmo_bsc_bssap.c
@@ -34,6 +34,7 @@
 #include <osmocom/gsm/gsm0808_utils.h>
 #include <osmocom/gsm/gsm48.h>
 #include <osmocom/bsc/osmo_bsc_sigtran.h>
+#include <osmocom/bsc/osmo_bsc_lcls.h>
 #include <osmocom/bsc/a_reset.h>
 #include <osmocom/core/byteswap.h>

@@ -634,6 +635,57 @@
        return -1;
 }

+/* handle LCLS specific IES in BSSMAP ASS REQ */
+static void bssmap_handle_ass_req_lcls(struct gsm_subscriber_connection *conn,
+                                       const struct tlv_parsed *tp)
+{
+       const struct tlv_p_entry *tlv;
+
+       tlv = TLVP_GET(tp, GSM0808_IE_GLOBAL_CALL_REF);
+       if (tlv) {
+               if (tlv->len > sizeof(conn->lcls.global_call_ref))
+                       LOGP(DMSC, LOGL_ERROR, "Global Call Ref IE of %u bytes 
too long.\n", tlv->len);
+               else {
+                       memcpy(&conn->lcls.global_call_ref, tlv->val, tlv->len);
+                       conn->lcls.global_call_ref_len = tlv->len;
+               }
+       }
+
+       /* Update the LCLS state with Config + CSC (if any) */
+       lcls_update_config(conn, TLVP_VAL_MINLEN(tp, GSM0808_IE_LCLS_CONFIG, 1),
+                               TLVP_VAL_MINLEN(tp, 
GSM0808_IE_LCLS_CONN_STATUS_CTRL, 1));
+}
+
+/* TS 48.008 3.2.1.91 */
+static int bssmap_handle_lcls_connect_ctrl(struct gsm_subscriber_connection 
*conn,
+                                          struct msgb *msg, unsigned int 
length)
+{
+       struct msgb *resp;
+       struct tlv_parsed tp;
+       int rc;
+
+       OSMO_ASSERT(conn);
+
+       if (conn->lcls.global_call_ref_len == 0) {
+               /* no LCLS if no GCR set previously! */
+               return 0;
+       }
+
+       rc = tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, length - 1, 0, 
0);
+       if (rc < 0)
+               return rc;
+
+       /* Update the LCLS state with Config + CSC (if any) */
+       lcls_update_config(conn, TLVP_VAL_MINLEN(&tp, GSM0808_IE_LCLS_CONFIG, 
1),
+                               TLVP_VAL_MINLEN(&tp, 
GSM0808_IE_LCLS_CONN_STATUS_CTRL, 1));
+
+       resp = gsm0808_create_lcls_conn_ctrl_ack(lcls_get_status(conn));
+       osmo_fsm_inst_dispatch(conn->fi, GSCON_EV_TX_SCCP, resp);
+
+       return 0;
+}
+
+
 /*
  * Handle the assignment request message.
  *
@@ -682,6 +734,8 @@
                goto reject;
        }

+       bssmap_handle_ass_req_lcls(conn, &tp);
+
        /* Currently we only support a limited subset of all
         * possible channel types, such as multi-slot or CSD */
        switch (ct.ch_indctr) {
@@ -852,6 +906,9 @@
        case BSS_MAP_MSG_ASSIGMENT_RQST:
                ret = bssmap_handle_assignm_req(conn, msg, length);
                break;
+       case BSS_MAP_MSG_LCLS_CONNECT_CTRL:
+               ret = bssmap_handle_lcls_connect_ctrl(conn, msg, length);
+               break;
        default:
                LOGP(DMSC, LOGL_NOTICE, "Unimplemented msg type: %s\n",
                        gsm0808_bssmap_name(msg->l4h[0]));
diff --git a/src/osmo-bsc/osmo_bsc_main.c b/src/osmo-bsc/osmo_bsc_main.c
index f3daccc..5d949b8 100644
--- a/src/osmo-bsc/osmo_bsc_main.c
+++ b/src/osmo-bsc/osmo_bsc_main.c
@@ -358,6 +358,12 @@
                .description = "PCU Interface",
                .enabled = 1, .loglevel = LOGL_DEBUG,
        },
+       [DLCLS] = {
+               .name = "DLCLS",
+               .description = "Local Call, Local Switch",
+               .enabled = 1, .loglevel = LOGL_NOTICE,
+       },
+
 };

 static int filter_fn(const struct log_context *ctx, struct log_target *tar)

--
To view, visit https://gerrit.osmocom.org/9416
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I614fade62834def5cafc94c4d2578cd747a3f9f7
Gerrit-Change-Number: 9416
Gerrit-PatchSet: 1
Gerrit-Owner: Harald Welte <[email protected]>

Reply via email to