Change in osmo-msc[master]: WIP: SGs Interface initial code

dexter gerrit-no-reply at lists.osmocom.org
Tue Nov 6 17:39:11 UTC 2018


dexter has uploaded this change for review. ( https://gerrit.osmocom.org/11642


Change subject: WIP: SGs Interface initial code
......................................................................

WIP: SGs Interface initial code

- LU works
- ISMI detach and EPS detach work (but presumably not as it should)
- Paging works so far that if the MSC has to page it may send a paging
  request via SGs

Change-Id: I73359925fc1ca72b33a1466e6ac41307f2f0b11d
Related: OS#3615
---
M include/osmocom/msc/Makefile.am
M include/osmocom/msc/debug.h
M include/osmocom/msc/gsm_data.h
M include/osmocom/msc/gsm_subscriber.h
A include/osmocom/msc/sgs_iface.h
A include/osmocom/msc/sgs_vty.h
M include/osmocom/msc/vlr.h
A include/osmocom/msc/vlr_sgs.h
M include/osmocom/msc/vty.h
M src/libmsc/Makefile.am
M src/libmsc/gsm_04_08_cc.c
M src/libmsc/gsm_04_11.c
M src/libmsc/gsm_09_11.c
M src/libmsc/gsm_subscriber.c
M src/libmsc/msc_vty.c
A src/libmsc/sgs_iface.c
A src/libmsc/sgs_vty.c
M src/libmsc/silent_call.c
M src/libvlr/Makefile.am
M src/libvlr/vlr.c
M src/libvlr/vlr_lu_fsm.c
A src/libvlr/vlr_sgs.c
A src/libvlr/vlr_sgs_fsm.c
A src/libvlr/vlr_sgs_fsm.h
M src/osmo-msc/Makefile.am
M src/osmo-msc/msc_main.c
M tests/msc_vlr/msc_vlr_test_authen_reuse.c
M tests/msc_vlr/msc_vlr_test_call.c
M tests/msc_vlr/msc_vlr_test_gsm_authen.c
M tests/msc_vlr/msc_vlr_test_gsm_ciph.c
M tests/msc_vlr/msc_vlr_test_hlr_reject.c
M tests/msc_vlr/msc_vlr_test_hlr_timeout.c
M tests/msc_vlr/msc_vlr_test_ms_timeout.c
M tests/msc_vlr/msc_vlr_test_no_authen.c
M tests/msc_vlr/msc_vlr_test_reject_concurrency.c
M tests/msc_vlr/msc_vlr_test_rest.c
M tests/msc_vlr/msc_vlr_test_ss.c
M tests/msc_vlr/msc_vlr_test_umts_authen.c
M tests/sms_queue/sms_queue_test.c
39 files changed, 2,060 insertions(+), 17 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-msc refs/changes/42/11642/1

diff --git a/include/osmocom/msc/Makefile.am b/include/osmocom/msc/Makefile.am
index ebc946a..35e8c5e 100644
--- a/include/osmocom/msc/Makefile.am
+++ b/include/osmocom/msc/Makefile.am
@@ -24,11 +24,14 @@
 	a_reset.h \
 	osmo_msc.h \
 	rrlp.h \
+	sgs_iface.h \
+	sgs_vty.h \
 	signal.h \
 	silent_call.h \
 	smpp.h \
 	sms_queue.h \
 	transaction.h \
 	vlr.h \
+	vlr_sgs.h \
 	vty.h \
 	$(NULL)
diff --git a/include/osmocom/msc/debug.h b/include/osmocom/msc/debug.h
index 717cf74..28ba482 100644
--- a/include/osmocom/msc/debug.h
+++ b/include/osmocom/msc/debug.h
@@ -21,5 +21,6 @@
 	DVLR,
 	DIUCS,
 	DBSSAP,
+	DSGS,
 	Debug_LastEntry,
 };
diff --git a/include/osmocom/msc/gsm_data.h b/include/osmocom/msc/gsm_data.h
index 085248c..94b5474 100644
--- a/include/osmocom/msc/gsm_data.h
+++ b/include/osmocom/msc/gsm_data.h
@@ -55,6 +55,7 @@
        RAN_UNKNOWN,
        RAN_GERAN_A,	/* 2G / A-interface */
        RAN_UTRAN_IU,	/* 3G / Iu-interface (IuCS or IuPS) */
+       RAN_UTRAN_SGS,	/* 4G / SGs-interface (csfb) */
 };
 
 extern const struct value_string ran_type_names[];
@@ -124,7 +125,7 @@
 	/* back pointers */
 	struct gsm_network *network;
 
-	/* connected via 2G or 3G? */
+	/* connected via 2G, 3G or 4G csfb via SGs */
 	enum ran_type via_ran;
 
 	uint16_t lac;
diff --git a/include/osmocom/msc/gsm_subscriber.h b/include/osmocom/msc/gsm_subscriber.h
index 01d9c58..762c8e6 100644
--- a/include/osmocom/msc/gsm_subscriber.h
+++ b/include/osmocom/msc/gsm_subscriber.h
@@ -5,6 +5,7 @@
 
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/gsm/protocol/gsm_23_003.h>
+#include <osmocom/gsm/protocol/gsm_29_118.h>
 
 #include <osmocom/msc/gsm_data.h>
 
@@ -54,8 +55,8 @@
  */
 struct subscr_request *subscr_request_conn(struct vlr_subscr *vsub,
 					   gsm_cbfn *cbfn, void *param,
-					   const char *label);
-
+					   const char *label,
+					   enum sgsap_service_ind serv_ind);
 void subscr_remove_request(struct subscr_request *req);
 int subscr_rx_paging_response(struct msgb *msg,
 			      struct gsm_subscriber_connection *conn);
diff --git a/include/osmocom/msc/sgs_iface.h b/include/osmocom/msc/sgs_iface.h
new file mode 100644
index 0000000..ea2bd62
--- /dev/null
+++ b/include/osmocom/msc/sgs_iface.h
@@ -0,0 +1,103 @@
+#pragma once
+
+#include <osmocom/gsm/protocol/gsm_23_003.h>
+#include <osmocom/gsm/protocol/gsm_29_118.h>
+#include <osmocom/msc/vlr.h>
+
+enum {
+	SGS_STATE_TS5, /* Started when sending the SGsAP-PAGING-REQUEST */
+	SGS_STATE_TS6_2, /* TMSI reallocation, 5.2.3.5 */
+	SGS_STATE_TS7, /* Started when SGsAP-ALERT-REQUEST is sent 5.3.2.1 */
+	SGS_STATE_TS11, /* Reset ack timeout */
+	SGS_STATE_TS14, /* Started when SGsAP-SERVICE-REQUEST is received 5.15.1 */
+	SGS_STATE_TS15, /* Started when SGsAP-MO-CSFB-INDICATION is received 5.16.3 (UE fallback) */
+	_NUM_SGS_STATE_TIMERS
+};
+
+enum {
+	SGS_STATE_NS7, /* Alert request retransmit */
+	SGS_STATE_NS11, /* Reset repeat */
+	_NUM_SGS_STATE_COUNTERS
+};
+
+static const unsigned int sgs_state_timer_defaults[_NUM_SGS_STATE_TIMERS] = {
+	[SGS_STATE_TS5] = SGS_TS5_DEFAULT,
+	[SGS_STATE_TS6_2] = SGS_TS6_2_DEFAULT,
+	[SGS_STATE_TS7] = SGS_TS7_DEFAULT,
+	[SGS_STATE_TS11] = SGS_TS11_DEFAULT,
+	[SGS_STATE_TS14] = SGS_TS14_DEFAULT,
+	[SGS_STATE_TS15] = SGS_TS15_DEFAULT,
+};
+
+static const unsigned int sgs_state_counter_defaults[_NUM_SGS_STATE_COUNTERS] = {
+	[SGS_STATE_NS7] = SGS_NS7_DEFAULT,
+	[SGS_STATE_NS11] = SGS_NS11_DEFAULT,
+};
+
+/* global SGs state */
+struct sgs_state {
+	/* list of MMEs (sgs_mme_ctx) */
+	struct llist_head mme_list;
+
+	/* list of SCTP client connections */
+	struct llist_head conn_list;
+
+	/* SCTP server for inbound SGs connections */
+	struct osmo_stream_srv_link *srv_link;
+
+	struct {
+		char *local_addr;
+		uint16_t local_port;
+		/* user-configured VLR name (FQDN) */
+		char *vlr_name;
+		/* timers on VLR side */
+		unsigned int timer[_NUM_SGS_STATE_TIMERS];
+		/* countrs on VLR side */
+		unsigned int counter[_NUM_SGS_STATE_COUNTERS];
+	} cfg;
+};
+
+struct sgs_connection {
+	/* global list of SGs connections */
+	struct llist_head list;
+	/* back-pointer */
+	struct sgs_state *sgs;
+
+	/* Socket name from osmo_sock_get_name() */
+	char *sockname;
+
+	/* MME for this connection, if any.  This field is NULL until we
+	 * receive the first "MME name" IE from the MME, which could be part
+	 * of the RESET procedure, but also just a normal LU request. */
+	struct sgs_mme_ctx *mme;
+
+	/* represents the SCTP connection we accept()ed from this MME */
+	struct osmo_stream_srv *srv;
+};
+
+struct sgs_mme_ctx {
+	/* global list of MME contexts */
+	struct llist_head list;
+	/* back-pointer */
+	struct sgs_state *sgs;
+
+	/* MME name as string representation */
+	char fqdn[GSM23003_MME_DOMAIN_LEN + 1];
+
+	/* current connection for this MME, if any. Can be NULL if the SCTP
+	 * connection to the MME was lost and hasn't been re-established yet */
+	struct sgs_connection *conn;
+
+	/* FSM for the "VLR reset" procedure" */
+	struct osmo_fsm_inst *fi;
+	unsigned int ns11_remaining;
+};
+
+extern struct sgs_state *g_sgs;
+
+struct sgs_state *sgs_init(void *ctx, struct gsm_network *network);
+int sgs_open(struct sgs_state *sgs);
+int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind);
+
+extern const char *sgs_state_timer_names[_NUM_SGS_STATE_TIMERS];
+extern const char *sgs_state_counter_names[_NUM_SGS_STATE_COUNTERS];
diff --git a/include/osmocom/msc/sgs_vty.h b/include/osmocom/msc/sgs_vty.h
new file mode 100644
index 0000000..75a0f24
--- /dev/null
+++ b/include/osmocom/msc/sgs_vty.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void sgs_vty_init(void);
diff --git a/include/osmocom/msc/vlr.h b/include/osmocom/msc/vlr.h
index d52713c..d103be6 100644
--- a/include/osmocom/msc/vlr.h
+++ b/include/osmocom/msc/vlr.h
@@ -102,6 +102,13 @@
 
 struct sgsn_mm_ctx;
 struct vlr_instance;
+struct vlr_subscr;
+
+struct sgs_lu_response {
+	bool accepted;
+	struct vlr_subscr *vsub;
+};
+typedef void (*vlr_sgs_lu_response_cb_t) (struct sgs_lu_response * response);
 
 /* The VLR subscriber is the part of the GSM subscriber state in VLR (CS) or
  * SGSN (PS), particularly while interacting with the HLR via GSUP */
@@ -151,6 +158,7 @@
 	struct osmo_fsm_inst *lu_fsm;
 	struct osmo_fsm_inst *auth_fsm;
 	struct osmo_fsm_inst *proc_arq_fsm;
+	struct osmo_fsm_inst *sgs_fsm;
 
 	bool lu_complete;
 	time_t expire_lu;
@@ -174,6 +182,13 @@
 		uint8_t lac;
 		enum ran_type attached_via_ran;
 	} cs;
+	/* SGs (MME) specific parts */
+	struct {
+		/* FIXME: probably use cs.lac for instead of introducing another lai here? */
+		struct osmo_location_area_id lai;
+		vlr_sgs_lu_response_cb_t response_cb;
+		char mme_name[55 + 1];
+	} sgs;
 
 	struct gsm_classmark classmark;
 };
diff --git a/include/osmocom/msc/vlr_sgs.h b/include/osmocom/msc/vlr_sgs.h
new file mode 100644
index 0000000..d177516
--- /dev/null
+++ b/include/osmocom/msc/vlr_sgs.h
@@ -0,0 +1,15 @@
+#pragma once
+
+enum sgs_ue_fsm_state {
+	SGS_UE_ST_NULL,
+	SGS_UE_ST_ASSOCIATED,
+	SGS_UE_ST_LA_UPD_PRES,
+};
+
+void vlr_sgs_reset(struct vlr_instance *vlr);
+int vlr_sgs_loc_update(struct vlr_instance *vlr,
+		       vlr_sgs_lu_response_cb_t response_cb,
+		       char *mme_name, enum vlr_lu_type type, const char *imsi, struct osmo_location_area_id *new_lai);
+void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vlr_subscr);
+void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vlr_subscr);
+void vlr_sgs_detach(struct vlr_instance *vlr, const char *imsi, bool eps);
diff --git a/include/osmocom/msc/vty.h b/include/osmocom/msc/vty.h
index 6a55df7..2a3b18b 100644
--- a/include/osmocom/msc/vty.h
+++ b/include/osmocom/msc/vty.h
@@ -23,6 +23,7 @@
 	SMPP_NODE,
 	SMPP_ESME_NODE,
 	HLR_NODE,
+	CFG_SGS_NODE,
 };
 
 int bsc_vty_init_extra(void);
diff --git a/src/libmsc/Makefile.am b/src/libmsc/Makefile.am
index 90bbbf8..2cf34cf 100644
--- a/src/libmsc/Makefile.am
+++ b/src/libmsc/Makefile.am
@@ -51,6 +51,8 @@
 	osmo_msc.c \
 	ctrl_commands.c \
 	subscr_conn.c \
+	sgs_iface.c \
+	sgs_vty.c \
 	$(NULL)
 if BUILD_IU
 libmsc_a_SOURCES += \
diff --git a/src/libmsc/gsm_04_08_cc.c b/src/libmsc/gsm_04_08_cc.c
index f9888d7..54320d2 100644
--- a/src/libmsc/gsm_04_08_cc.c
+++ b/src/libmsc/gsm_04_08_cc.c
@@ -1995,7 +1995,8 @@
 							vsub,
 							setup_trig_pag_evt,
 							trans,
-							"MNCC: establish call");
+							"MNCC: establish call",
+							SGSAP_SERV_IND_CS_CALL);
 			if (!trans->paging_request) {
 				LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
 				vlr_subscr_put(vsub);
diff --git a/src/libmsc/gsm_04_11.c b/src/libmsc/gsm_04_11.c
index 8d9169b..cb37db6 100644
--- a/src/libmsc/gsm_04_11.c
+++ b/src/libmsc/gsm_04_11.c
@@ -1132,7 +1132,8 @@
 	/* if not, we have to start paging */
 	LOGP(DLSMS, LOGL_DEBUG, "Sending SMS: no connection open, start paging %s\n",
 	     vlr_subscr_name(vsub));
-	res = subscr_request_conn(vsub, paging_cb_send_sms, sms, "send SMS");
+	res = subscr_request_conn(vsub, paging_cb_send_sms, sms, "send SMS",
+				  SGSAP_SERV_IND_SMS);
 	if (!res) {
 		send_signal(S_SMS_UNKNOWN_ERROR, NULL, sms, GSM_PAGING_BUSY);
 		sms_free(sms);
diff --git a/src/libmsc/gsm_09_11.c b/src/libmsc/gsm_09_11.c
index b863ce8..9adf5b7 100644
--- a/src/libmsc/gsm_09_11.c
+++ b/src/libmsc/gsm_09_11.c
@@ -347,7 +347,10 @@
 
 	/* Trigger Paging Request */
 	trans->paging_request = subscr_request_conn(vsub,
-		&handle_paging_event, trans, "GSM 09.11 SS/USSD");
+						    &handle_paging_event,
+						    trans,
+						    "GSM 09.11 SS/USSD",
+						    SGSAP_SERV_IND_SMS);
 	if (!trans->paging_request) {
 		LOGP(DMM, LOGL_ERROR, "Failed to allocate paging token\n");
 		trans_free(trans);
diff --git a/src/libmsc/gsm_subscriber.c b/src/libmsc/gsm_subscriber.c
index fb61209..0645fa9 100644
--- a/src/libmsc/gsm_subscriber.c
+++ b/src/libmsc/gsm_subscriber.c
@@ -49,6 +49,7 @@
 #include <osmocom/msc/osmo_msc.h>
 #include <osmocom/msc/msc_ifaces.h>
 #include <osmocom/msc/a_iface.h>
+#include <osmocom/msc/sgs_iface.h>
 
 void subscr_paging_cancel(struct vlr_subscr *vsub, enum gsm_paging_event event)
 {
@@ -110,7 +111,7 @@
 	return 0;
 }
 
-int msc_paging_request(struct vlr_subscr *vsub)
+static int msc_paging_request(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
 {
 	/* The subscriber was last seen in subscr->lac. Find out which
 	 * BSCs/RNCs are responsible and send them a paging request via open
@@ -123,6 +124,8 @@
 					vsub->tmsi == GSM_RESERVED_TMSI?
 					NULL : &vsub->tmsi,
 					vsub->lac);
+	case RAN_UTRAN_SGS:
+		return sgs_iface_tx_paging(vsub, serv_ind);
 	default:
 		break;
 	}
@@ -143,10 +146,13 @@
  * \param cbfn  function to call when the conn is established.
  * \param param  caller defined param to pass to cbfn().
  * \param label  human readable label of the request kind used for logging.
+ * \param label  human readable label of the request kind used for logging.
+ * \param serv_ind  sgsap service indicator (in case SGs interface is used to page)
  */
 struct subscr_request *subscr_request_conn(struct vlr_subscr *vsub,
 					   gsm_cbfn *cbfn, void *param,
-					   const char *label)
+					   const char *label,
+					   enum sgsap_service_ind serv_ind)
 {
 	int rc;
 	struct subscr_request *request;
@@ -156,7 +162,7 @@
 	if (!vsub->cs.is_paging) {
 		LOGP(DMM, LOGL_DEBUG, "Subscriber %s not paged yet, start paging.\n",
 		     vlr_subscr_name(vsub));
-		rc = msc_paging_request(vsub);
+		rc = msc_paging_request(vsub, serv_ind);
 		if (rc <= 0) {
 			LOGP(DMM, LOGL_ERROR, "Subscriber %s paging failed: %d\n",
 			     vlr_subscr_name(vsub), rc);
diff --git a/src/libmsc/msc_vty.c b/src/libmsc/msc_vty.c
index a16cec8..4683695 100644
--- a/src/libmsc/msc_vty.c
+++ b/src/libmsc/msc_vty.c
@@ -54,6 +54,7 @@
 #include <osmocom/msc/signal.h>
 #include <osmocom/msc/mncc_int.h>
 #include <osmocom/msc/rrlp.h>
+#include <osmocom/msc/vlr_sgs.h>
 
 static struct gsm_network *gsmnet = NULL;
 
@@ -608,6 +609,17 @@
 		reqs += 1;
 	vty_out(vty, "    Paging: %s paging for %d requests%s",
 		vsub->cs.is_paging ? "is" : "not", reqs, VTY_NEWLINE);
+
+	/* SGs related */
+	vty_out(vty, "    SGs-state: %s%s",
+		osmo_fsm_state_name(vsub->sgs_fsm->fsm, vsub->sgs_fsm->state),
+		VTY_NEWLINE);
+	if (vsub->sgs.mme_name && strlen(vsub->sgs.mme_name))
+		vty_out(vty, "    SGs-MME: %s%s", vsub->sgs.mme_name,
+			VTY_NEWLINE);
+	else
+		vty_out(vty, "    SGs-MME: (none)%s", VTY_NEWLINE);
+
 	vty_out(vty, "    Use count: %u%s", vsub->use_count, VTY_NEWLINE);
 
 	/* Connection */
@@ -1035,7 +1047,7 @@
 		return CMD_WARNING;
 	}
 
-	req = subscr_request_conn(vsub, NULL, NULL, "manual Paging from VTY");
+	req = subscr_request_conn(vsub, NULL, NULL, "manual Paging from VTY", SGSAP_SERV_IND_CS_CALL);
 	if (req)
 		vty_out(vty, "%% paging subscriber%s", VTY_NEWLINE);
 	else
@@ -1401,6 +1413,8 @@
 	return CMD_SUCCESS;
 }
 
+extern void sgs_vty_init(void);
+
 void msc_vty_init(struct gsm_network *msc_network)
 {
 	OSMO_ASSERT(gsmnet == NULL);
@@ -1440,6 +1454,7 @@
 #ifdef BUILD_IU
 	ranap_iu_vty_init(MSC_NODE, &msc_network->iu.rab_assign_addr_enc);
 #endif
+	sgs_vty_init();
 	osmo_fsm_vty_add_cmds();
 
 	osmo_signal_register_handler(SS_SCALL, scall_cbfn, NULL);
diff --git a/src/libmsc/sgs_iface.c b/src/libmsc/sgs_iface.c
new file mode 100644
index 0000000..69597be
--- /dev/null
+++ b/src/libmsc/sgs_iface.c
@@ -0,0 +1,976 @@
+/* SGs Interface according to 3GPP TS 23.272 + TS 29.118 */
+
+/* (C 2018 by Harald Welte <laforge at gnumonks.org>
+ *
+ * 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/msgb.h>
+#include <osmocom/core/fsm.h>
+#include <osmocom/core/socket.h>
+#include <osmocom/core/select.h>
+
+#include <osmocom/gsm/tlv.h>
+#include <osmocom/gsm/gsm48.h>
+#include <osmocom/gsm/gsm23003.h>
+
+#include <osmocom/netif/stream.h>
+
+#include <osmocom/msc/vlr.h>
+#include <osmocom/msc/vlr_sgs.h>
+
+#include <osmocom/msc/debug.h>
+#include <osmocom/msc/sgs_iface.h>
+
+#define S(x) (1 << (x))
+
+static struct osmo_fsm sgs_vlr_reset_fsm;
+
+/* See also comment in a_iface.c */
+static struct gsm_network *gsm_network = NULL;
+
+struct sgs_state *g_sgs;
+
+/***********************************************************************
+ * SGs state per MME connection
+ ***********************************************************************/
+
+struct sgs_state;
+struct sgs_connection;
+
+const char *sgs_state_timer_names[_NUM_SGS_STATE_TIMERS] = {
+	[SGS_STATE_TS5] = "Ts5",
+	[SGS_STATE_TS6_2] = "Ts6-2",
+	[SGS_STATE_TS7] = "Ts7",
+	[SGS_STATE_TS11] = "Ts11",
+	[SGS_STATE_TS14] = "Ts14",
+	[SGS_STATE_TS15] = "Ts15",
+};
+
+const char *sgs_state_counter_names[_NUM_SGS_STATE_COUNTERS] = {
+	[SGS_STATE_NS7] = "Ns7",
+	[SGS_STATE_NS11] = "Ns11",
+};
+
+#define LOGSGC(sgc, lvl, fmt, args...) \
+	LOGP(DSGS, lvl, "%s: " fmt, (sgc)->sockname, ## args)
+
+#define LOGMME(mme, lvl, fmt, args...) \
+	LOGP(DSGS, lvl, "%s: " fmt, (mme)->fqdn ? (mme)->fqdn : (mme)->conn->sockname, ## args)
+
+enum sgs_vlr_reset_fsm_state {
+	SGS_VLRR_ST_NULL,
+	SGS_VLRR_ST_WAIT_ACK,
+	SGS_VLRR_ST_COMPLETE,
+};
+
+enum sgs_vlr_reset_fsm_event {
+	SGS_VLRR_E_START_RESET,
+	SGS_VLRR_E_RX_RESET_ACK,
+};
+
+/***********************************************************************
+ * SGsAP transmit functions
+ ***********************************************************************/
+
+#include <osmocom/gsm/apn.h>
+
+static struct msgb *sgs_msgb_alloc(void)
+{
+	/* by far sufficient for the maximum size message of 298 bytes
+	 * (9+7+5+3+10+253+10+1) SGsAP-UP-UD */
+	return msgb_alloc_headroom(1024, 128, "SGsAP");
+}
+
+/*! Encode VLR/MME name from string and append to SGsAP msg */
+static void msgb_sgsap_name_put(struct msgb *msg, enum sgsap_iei iei, const char *name)
+{
+	uint8_t buf[APN_MAXLEN];
+	uint8_t len;
+
+	/* encoding is like DNS names, which is like APN fields */
+	memset(buf, 0, sizeof(buf));
+	len = osmo_apn_from_str(buf, sizeof(buf), name);
+	if (iei == SGSAP_IE_MME_NAME)
+		len = 55;
+	msgb_tlv_put(msg, iei, len, buf);
+}
+
+/*! Encode IMSI from string representation and append to SGSaAP msg */
+static void msgb_sgsap_imsi_put(struct msgb *msg, const char *imsi)
+{
+	uint8_t buf[16];
+	uint8_t len;
+
+	/* encoding is just like TS 04.08 */
+	len = gsm48_generate_mid_from_imsi(buf, imsi);
+	/* skip first two bytes (tag+length) so we can use msgb_tlv_put */
+	msgb_tlv_put(msg, SGSAP_IE_IMSI, len - 2, buf + 2);
+}
+
+/*! Encode IMSI from string representation and append to SGSaAP msg */
+static void msgb_sgsap_imsi_push(struct msgb *msg, const char *imsi)
+{
+	uint8_t buf[16];
+	uint8_t len;
+
+	/* encoding is just like TS 04.08 */
+	len = gsm48_generate_mid_from_imsi(buf, imsi);
+	/* skip first two bytes (tag+length) so we can use msgb_tlv_put */
+	msgb_tlv_push(msg, SGSAP_IE_IMSI, len - 2, buf + 2);
+}
+
+static void sgs_tx(struct sgs_connection *sgc, enum sgsap_msg_type msg_type, struct msgb *msg)
+{
+	uint8_t *cur = msgb_push(msg, 1);
+	*cur = msg_type;
+	msgb_sctp_ppid(msg) = 0;
+	if (!sgc) {
+		LOGSGC(sgc, LOGL_NOTICE, "Cannot transmit %s: connection dead. Discarding\n",
+		       sgsap_msg_type_name(msg_type));
+		msgb_free(msg);
+		return;
+	}
+	osmo_stream_srv_send(sgc->srv, msg);
+}
+
+/*! Transmit simple SGsAP message (only IE: IMSI) */
+static void sgs_tx_simple(struct sgs_connection *sgc, enum sgsap_msg_type msgt, const char *imsi)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	sgs_tx(sgc, msgt, msg);
+}
+
+/* 8.3 */
+void sgs_tx_alert_req(struct sgs_connection *sgc, const char *imsi)
+{
+	sgs_tx_simple(sgc, SGSAP_MSGT_ALERT_REQ, imsi);
+}
+
+/* 8.4 */
+void sgs_tx_dl_ud(struct sgs_connection *sgc, const char *imsi, struct msgb *msg)
+{
+	msgb_sgsap_imsi_push(msg, imsi);
+	sgs_tx(sgc, SGSAP_MSGT_DL_UD, msg);
+}
+
+/* 8.5 */
+void sgs_tx_eps_det_ack(struct sgs_connection *sgc, const char *imsi)
+{
+	sgs_tx_simple(sgc, SGSAP_MSGT_EPS_DET_ACK, imsi);
+}
+
+/* 8.7 */
+void sgs_tx_imsi_det_ack(struct sgs_connection *sgc, const char *imsi)
+{
+	sgs_tx_simple(sgc, SGSAP_MSGT_IMSI_DET_ACK, imsi);
+}
+
+/*! 8.9 SGsAP-LOCATION-UPDATE-ACCEPT.
+ *  \param[in] sgc SGs Connection.
+ *  \param[in] imsi IMSI of the subscriber.
+ *  \param[in] lai Location Area Identity (optional, may be NULL).
+ *  \param[in] new_id value part of new Mobile Identity (optional, may be NULL).
+ *  \param[in] new_id_len length of \a new_id in octets. */
+void sgs_tx_lu_ack(struct sgs_connection *sgc, const char *imsi,
+		   const struct osmo_location_area_id *lai, const uint8_t *new_id, unsigned int new_id_len)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	if (lai) {
+		struct gsm48_loc_area_id lai_enc;
+		gsm48_generate_lai2(&lai_enc, lai);
+		msgb_tlv_put(msg, SGSAP_IE_LAI, 5, (uint8_t *) & lai_enc);
+	}
+	if (new_id && new_id_len)
+		msgb_tlv_put(msg, SGSAP_IE_MOBILE_ID, new_id_len, new_id);
+	sgs_tx(sgc, SGSAP_MSGT_LOC_UPD_ACK, msg);
+}
+
+/* 8.10 */
+void sgs_tx_lu_rej(struct sgs_connection *sgc, const char *imsi, uint8_t rej_cause,
+		   const struct osmo_location_area_id *lai)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	msgb_tlv_put(msg, SGSAP_IE_REJECT_CAUSE, 1, &rej_cause);
+	if (lai) {
+		struct gsm48_loc_area_id lai_enc;
+		gsm48_generate_lai2(&lai_enc, lai);
+		msgb_tlv_put(msg, SGSAP_IE_LAI, 5, (uint8_t *) & lai_enc);
+	}
+	sgs_tx(sgc, SGSAP_MSGT_LOC_UPD_REJ, msg);
+}
+
+/* 8.12 */
+void sgs_tx_mm_info_req(struct sgs_connection *sgc, const char *imsi, const uint8_t *mm_info, uint8_t len)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	msgb_tlv_put(msg, SGSAP_IE_MM_INFO, len, mm_info);
+	sgs_tx(sgc, SGSAP_MSGT_MM_INFO_REQ, msg);
+}
+
+/* 8.14 */
+void sgs_tx_paging_req(struct sgs_connection *sgc, const char *imsi, const char *vlr_name, uint8_t serv_ind)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	msgb_sgsap_name_put(msg, SGSAP_IE_VLR_NAME, vlr_name);
+	msgb_tlv_put(msg, SGSAP_IE_SERVICE_INDICATOR, 1, &serv_ind);
+	sgs_tx(sgc, SGSAP_MSGT_PAGING_REQ, msg);
+}
+
+/* 8.15 */
+void sgs_tx_reset_ack_vlr(struct sgs_connection *sgc, const char *vlr_name)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_name_put(msg, SGSAP_IE_VLR_NAME, vlr_name);
+	sgs_tx(sgc, SGSAP_MSGT_RESET_ACK, msg);
+}
+
+/* 8.16 */
+void sgs_tx_reset_ind_vlr(struct sgs_connection *sgc, const char *vlr_name)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_name_put(msg, SGSAP_IE_VLR_NAME, vlr_name);
+	sgs_tx(sgc, SGSAP_MSGT_RESET_IND, msg);
+}
+
+/* 8.18 */
+void sgs_tx_status(struct sgs_connection *sgc, enum sgsap_sgs_cause cause, const char *imsi, const struct msgb *err_msg)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	uint8_t c8 = cause;
+
+	msgb_tlv_put(msg, SGSAP_IE_SGS_CAUSE, 1, &c8);
+	if (imsi)
+		msgb_sgsap_imsi_put(msg, imsi);
+	if (err_msg)
+		msgb_tlv_put(msg, SGSAP_IE_ERR_MSG, msgb_l2len(msg), msgb_l2(msg));
+	sgs_tx(sgc, SGSAP_MSGT_STATUS, msg);
+}
+
+/* same as above, but get IMSI from tlv_parsed */
+void sgs_tx_status_tp(struct sgs_connection *sgc, enum sgsap_sgs_cause cause,
+		      const struct tlv_parsed *tp, const struct msgb *err_msg)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	uint8_t c8 = cause;
+
+	msgb_tlv_put(msg, SGSAP_IE_SGS_CAUSE, 1, &c8);
+	if (tp && TLVP_PRESENT(tp, SGSAP_IE_IMSI)) {
+		msgb_tlv_put(msg, SGSAP_IE_IMSI, TLVP_LEN(tp, SGSAP_IE_IMSI), TLVP_VAL(tp, SGSAP_IE_IMSI));
+	}
+	if (err_msg)
+		msgb_tlv_put(msg, SGSAP_IE_ERR_MSG, msgb_l2len(msg), msgb_l2(msg));
+	sgs_tx(sgc, SGSAP_MSGT_STATUS, msg);
+}
+
+/* 8.23 */
+void sgs_tx_release_req(struct sgs_connection *sgc, const char *imsi, const uint8_t *sgs_cause)
+{
+	struct msgb *msg = sgs_msgb_alloc();
+	msgb_sgsap_imsi_put(msg, imsi);
+	if (sgs_cause)
+		msgb_tlv_put(msg, SGSAP_IE_SGS_CAUSE, 1, sgs_cause);
+	sgs_tx(sgc, SGSAP_MSGT_RELEASE_REQ, msg);
+}
+
+/* 8.24 */
+void sgs_tx_service_abort_req(struct sgs_connection *sgc, const char *imsi)
+{
+	sgs_tx_simple(sgc, SGSAP_MSGT_SERVICE_ABORT_REQ, imsi);
+}
+
+/***********************************************************************
+ * SGs utility functions
+ ***********************************************************************/
+
+struct sgs_state *sgs_state_alloc(void *ctx)
+{
+	struct sgs_state *sgs = talloc_zero(ctx, struct sgs_state);
+
+	INIT_LLIST_HEAD(&sgs->mme_list);
+	INIT_LLIST_HEAD(&sgs->conn_list);
+
+	memcpy(sgs->cfg.timer, sgs_state_timer_defaults, sizeof(sgs->cfg.timer));
+	memcpy(sgs->cfg.counter, sgs_state_counter_defaults, sizeof(sgs->cfg.counter));
+
+	sgs->cfg.vlr_name = talloc_strdup(sgs, "mmec02.mmegi0002.mme.epc.mnc070.mcc901.3gppnetwork.org");
+
+	return sgs;
+}
+
+struct sgs_mme_ctx *sgs_mme_by_fqdn(struct sgs_state *sgs, const char *mme_fqdn)
+{
+	struct sgs_mme_ctx *mme;
+
+	llist_for_each_entry(mme, &sgs->mme_list, list) {
+		if (!strcasecmp(mme_fqdn, mme->fqdn))
+			return mme;
+	}
+	return NULL;
+}
+
+static struct sgs_mme_ctx *sgs_mme_alloc(struct sgs_state *sgs, const char *mme_fqdn, const struct osmo_gummei *gummei)
+{
+	struct sgs_mme_ctx *mme;
+
+	OSMO_ASSERT(sgs_mme_by_fqdn(sgs, mme_fqdn) == NULL);
+
+	mme = talloc_zero(sgs, struct sgs_mme_ctx);
+	if (!mme)
+		return NULL;
+	mme->sgs = sgs;
+	OSMO_STRLCPY_ARRAY(mme->fqdn, mme_fqdn);
+	mme->fi = osmo_fsm_inst_alloc(&sgs_vlr_reset_fsm, mme, mme, LOGL_INFO, osmo_gummei_name(gummei));
+	if (!mme->fi) {
+		talloc_free(mme);
+		return NULL;
+	}
+	llist_add_tail(&mme->list, &sgs->mme_list);
+	return mme;
+}
+
+/* Decode and verify MME name */
+static int decode_mme_name(char *mme_name, const struct tlv_parsed *tp)
+{
+	const uint8_t *mme_name_enc = TLVP_VAL_MINLEN(tp, SGSAP_IE_MME_NAME, 55);
+	struct osmo_gummei gummei;
+
+	if (!mme_name_enc)
+		return -EINVAL;
+
+	/* decode the MME name from DNS labels to string */
+	osmo_apn_to_str(mme_name, TLVP_VAL(tp, SGSAP_IE_MME_NAME), TLVP_LEN(tp, SGSAP_IE_MME_NAME));
+
+	/* try to parse the MME name into a GUMMEI as a test for the format */
+	if (osmo_parse_mme_domain(&gummei, mme_name) < 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+/* A MME FQDN was received (e.g. RESET-IND/RESET-ACK/LU-REQ) */
+static int sgs_mme_fqdn_received(struct sgs_connection *sgc, const char *mme_fqdn)
+{
+	struct sgs_mme_ctx *mme;
+	struct osmo_gummei gummei;
+
+	/* caller must pass in a valid FQDN string syntax */
+	OSMO_ASSERT(osmo_parse_mme_domain(&gummei, mme_fqdn) == 0);
+
+	if (!sgc->mme) {
+		/* attempt to find MME with given name */
+		mme = sgs_mme_by_fqdn(sgc->sgs, mme_fqdn);
+		if (!mme)
+			mme = sgs_mme_alloc(sgc->sgs, mme_fqdn, &gummei);
+		OSMO_ASSERT(mme);
+
+		if (mme->conn) {
+			/* The MME context has another connection !?! */
+			LOGSGC(sgc, LOGL_ERROR, "Rx MME name %s, but that MME already has other "
+			       "SCTP connection?!?\n", mme_fqdn);
+			return -1;
+		} else {
+			/* associate the two */
+			mme->conn = sgc;
+			sgc->mme = mme;
+		}
+	} else {
+		mme = sgc->mme;
+		if (strcasecmp(mme->fqdn, mme_fqdn) != 0) {
+			LOGMME(mme, LOGL_ERROR, "Rx MME name \"%s\" in packet from MME \"%s\" ?!?\n", mme_fqdn,
+			       mme->fqdn);
+			return -2;
+		}
+	}
+	return 0;
+}
+
+/* Safely get the mme-name for an sgs-connection */
+static char *sgs_mme_fqdn_get(struct sgs_connection *sgc)
+{
+	if (!sgc)
+		return NULL;
+	if (!sgc->mme)
+		return NULL;
+	if (strlen(sgc->mme->fqdn) <= 0)
+		return NULL;
+	return sgc->mme->fqdn;
+}
+
+/* Find an sgs_mme_ctx for a given vlr subscriber, also check result */
+struct sgs_mme_ctx *sgs_mme_ctx_by_vsub(struct vlr_subscr *vsub, uint8_t msg_type)
+{
+	struct sgs_mme_ctx *mme;
+
+	/* Find SGS connection by MME name */
+	mme = sgs_mme_by_fqdn(g_sgs, vsub->sgs.mme_name);
+	if (!mme) {
+		LOGP(DSGS, LOGL_ERROR, "(sub %s) Tx %s can not find suitable MME!\n",
+		     vlr_subscr_msisdn_or_name(vsub), sgsap_msg_type_name(msg_type));
+		return NULL;
+	}
+	if (!mme->conn) {
+		LOGP(DSGS, LOGL_ERROR,
+		     "(sub %s) Tx %s suitable MME found, but no SGS connection present!\n",
+		     vlr_subscr_msisdn_or_name(vsub), sgsap_msg_type_name(msg_type));
+		return NULL;
+	}
+	if (!mme->sgs) {
+		LOGP(DSGS, LOGL_ERROR,
+		     "(sub %s) Tx %s suitable MME found, but no SGS state present!\n",
+		     vlr_subscr_msisdn_or_name(vsub), sgsap_msg_type_name(msg_type));
+		return NULL;
+	}
+
+	return mme;
+}
+
+/***********************************************************************
+ * SGs incoming messages from the MME
+ ***********************************************************************/
+
+static void sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, struct tlv_parsed *tp)
+{
+	uint8_t cause;
+	const uint8_t *cause_ptr;
+	const uint8_t *err_msg;
+	char imsi[32];
+
+	cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
+	if (cause_ptr)
+		cause = *cause_ptr;
+
+	if (TLVP_PRESENT(tp, SGSAP_IE_IMSI)) {
+		gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(tp, SGSAP_IE_IMSI), TLVP_LEN(tp, SGSAP_IE_IMSI));
+	} else
+		OSMO_STRLCPY_ARRAY(imsi, "<none>");
+
+	if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
+		err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
+	else
+		err_msg = NULL;
+
+	LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, orig_msg=%s\n",
+	       sgsap_sgs_cause_name(cause), imsi, err_msg ? osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG)) : "");
+}
+
+static void sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
+{
+/* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
+	vlr_sgs_reset(gsm_network->vlr);
+	sgs_tx_reset_ack_vlr(sgc, sgc->sgs->cfg.vlr_name);
+}
+
+static void sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
+{
+	/* dispatch event to VLR reset FSM for this MME */
+	if (sgc->mme && sgc->mme->fi)
+		osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
+}
+
+static void sgs_rx_log_upd_resp_cb(struct sgs_lu_response *response)
+{
+	struct vlr_subscr *vsub = response->vsub;
+	struct sgs_mme_ctx *mme;
+	uint8_t new_id[2 + GSM48_TMSI_LEN];
+	uint8_t *new_id_ptr = new_id;
+	unsigned int new_id_len = 0;
+	uint8_t resp_msg_type;
+
+	if (response->accepted)
+		resp_msg_type = SGSAP_MSGT_LOC_UPD_ACK;
+	else
+		resp_msg_type = SGSAP_MSGT_LOC_UPD_REJ;
+
+	mme = sgs_mme_ctx_by_vsub(vsub, resp_msg_type);
+	if (!mme)
+		return;
+
+	if (response->accepted) {
+		if (vsub->tmsi != GSM_RESERVED_TMSI) {
+			new_id_len = gsm48_generate_mid_from_tmsi(new_id, vsub->tmsi);
+			new_id_ptr = new_id + 2;
+			new_id_len -= 2;
+		}
+		sgs_tx_lu_ack(mme->conn, vsub->imsi, &vsub->sgs.lai, new_id_ptr, new_id_len);
+		vlr_sgs_loc_update_acc_sent(vsub);
+	} else {
+		sgs_tx_lu_rej(mme->conn, vsub->imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, &vsub->sgs.lai);
+		vlr_sgs_loc_update_rej_sent(vsub);
+	}
+}
+
+static void sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
+{
+	const uint8_t *lu_type_ie;
+	enum vlr_lu_type type;
+	struct osmo_location_area_id new_lai;
+	const struct gsm48_loc_area_id *gsm48_lai;
+	int rc;
+	char *mme_name;
+
+	/* Determine MME-Name */
+	mme_name = sgs_mme_fqdn_get(sgc);
+	if (!mme_name) {
+		sgs_tx_lu_rej(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
+		return;
+	}
+
+	/* Parse LU-Type */
+	lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
+	if (!lu_type_ie) {
+		LOGSGC(sgc, LOGL_ERROR, "Rx SGsAP-LOCATION-UPDATE-REQ with no/invalid LU type!\n");
+		sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_MISSING_MAND_IE, tp, msg);
+		return;
+	}
+	if (lu_type_ie[0] == 0x01)
+		type = VLR_LU_TYPE_IMSI_ATTACH;
+	else
+		type = VLR_LU_TYPE_REGULAR;
+
+	/* Parse LAI of the new location */
+	gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
+	if (!gsm48_lai) {
+		LOGSGC(sgc, LOGL_ERROR, "Rx SGsAP-LOCATION-UPDATE-REQ with no/invalid NEW LAI type!\n");
+		sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_MISSING_MAND_IE, tp, msg);
+		return;
+	}
+	gsm48_decode_lai2(gsm48_lai, &new_lai);
+
+	/* Perform actual location update */
+	rc = vlr_sgs_loc_update(gsm_network->vlr, sgs_rx_log_upd_resp_cb, mme_name, type, imsi, &new_lai);
+	if (rc != 0)
+		sgs_tx_lu_rej(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
+}
+
+static void sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
+{
+	vlr_sgs_detach(gsm_network->vlr, imsi, false);
+	sgs_tx_imsi_det_ack(sgc, imsi);
+}
+
+static void sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
+{
+	vlr_sgs_detach(gsm_network->vlr, imsi, true);
+	sgs_tx_eps_det_ack(sgc, imsi);
+}
+
+/* Page a subscriber via SGs-interface */
+int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
+{
+	struct sgs_mme_ctx *mme;
+
+	mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
+	if (!mme)
+		return -EINVAL;
+
+	sgs_tx_paging_req(mme->conn, vsub->imsi, mme->sgs->cfg.vlr_name, serv_ind);
+	return 0;
+}
+
+int sgs_rx(struct sgs_connection *sgc, struct msgb *msg)
+{
+	uint8_t msg_type = msg->l2h[0];
+	struct tlv_parsed tp;
+	int rc;
+	char imsi[GSM48_MI_SIZE];
+	char mme_name[55 + 1];
+
+	/* When the receiving entity receives a message that is too short to contain a complete
+	 * message type information element, the receiving entity shall ignore that message. */
+	if (msgb_l2len(msg) < 1) {
+		sgs_tx_status(sgc, SGSAP_SGS_CAUSE_MISSING_MAND_IE, NULL, msg);
+		msgb_free(msg);
+		return 0;
+	}
+
+	/* Parse TLV elements */
+	rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
+	if (rc < 0) {
+		LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s parsing error\n", sgsap_msg_type_name(msg_type));
+		sgs_tx_status(sgc, SGSAP_SGS_CAUSE_SEMANT_INCORR_MSG, NULL, msg);
+		msgb_free(msg);
+		return 0;
+	}
+
+	/* Most of the messages contain an IMSI as mandatory IE, parse it right here */
+	if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
+	    msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
+		/* reject the message; all but the three above have mandatory IMSI */
+		LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s without IMSI, dropping\n", sgsap_msg_type_name(msg_type));
+		sgs_tx_status(sgc, SGSAP_SGS_CAUSE_MISSING_MAND_IE, NULL, msg);
+		msgb_free(msg);
+		return 0;
+	}
+	memset(imsi, 0, sizeof(imsi));
+	if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
+		gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(&tp, SGSAP_IE_IMSI), TLVP_LEN(&tp, SGSAP_IE_IMSI));
+		/* FIXME: Is there a well defined minimum length (MCC=3 + MNC=2 + MSIN=1 => 6?) */
+		if (strlen(imsi) <= 6) {
+			LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s with short IMSI, dropping\n",
+			       sgsap_msg_type_name(msg_type));
+			sgs_tx_status(sgc, SGSAP_SGS_CAUSE_INVALID_MAND_IE, NULL, msg);
+			msgb_free(msg);
+			return 0;
+		}
+	}
+
+	/* Some messages contain an MME-NAME as mandatore IE, parse it right here. The
+	 * MME-NAME is als immediately registered with the sgc, so it will be implicitly
+	 * known to all functions that have access to the sgc context. */
+	if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
+	    && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
+		|| msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
+		|| msg_type == SGSAP_MSGT_EPS_DET_IND)) {
+		LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s without MME-Name, dropping\n",
+		       sgsap_msg_type_name(msg_type));
+		sgs_tx_status(sgc, SGSAP_SGS_CAUSE_MISSING_MAND_IE, NULL, msg);
+		msgb_free(msg);
+		return 0;
+	}
+	memset(mme_name, 0, sizeof(mme_name));
+	if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
+		if (decode_mme_name(mme_name, &tp) != 0) {
+			LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s with invalid MME-Name, dropping\n",
+			       sgsap_msg_type_name(msg_type));
+			sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_INVALID_MAND_IE, &tp, msg);
+			msgb_free(msg);
+			return 0;
+		}
+		/* Regsister/check mme_name with sgc */
+		if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
+			sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, &tp, msg);
+			LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s with invalid MME-Name, dropping\n",
+			       sgsap_msg_type_name(msg_type));
+			msgb_free(msg);
+			return 0;
+		}
+	}
+
+	/* dispatch msg to various handler functions.  msgb ownership remains here! */
+	switch (msg_type) {
+	case SGSAP_MSGT_STATUS:
+		sgs_rx_status(sgc, msg, &tp);
+		break;
+	case SGSAP_MSGT_RESET_IND:
+		sgs_rx_reset_ind(sgc, msg, &tp);
+		break;
+	case SGSAP_MSGT_RESET_ACK:
+		sgs_rx_reset_ack(sgc, msg, &tp);
+		break;
+	case SGSAP_MSGT_LOC_UPD_REQ:
+		sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
+		break;
+	case SGSAP_MSGT_IMSI_DET_IND:
+		sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
+		break;
+	case SGSAP_MSGT_EPS_DET_IND:
+		sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
+		break;
+	case SGSAP_MSGT_TMSI_REALL_CMPL:
+	case SGSAP_MSGT_ALERT_ACK:
+	case SGSAP_MSGT_ALERT_REJ:
+	case SGSAP_MSGT_PAGING_REJ:
+	case SGSAP_MSGT_UE_ACT_IND:
+	case SGSAP_MSGT_UE_UNREACHABLE:
+	case SGSAP_MSGT_UL_UD:
+	case SGSAP_MSGT_SERVICE_REQ:
+	case SGSAP_MSGT_MO_CSFB_IND:
+		LOGSGC(sgc, LOGL_NOTICE, "Rx unmplemented SGsAP %s: %s\n",
+		       sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
+		sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_MSG_UNKNOWN, &tp, msg);
+		break;
+	default:
+		LOGSGC(sgc, LOGL_NOTICE, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
+		sgs_tx_status_tp(sgc, SGSAP_SGS_CAUSE_MSG_UNKNOWN, &tp, msg);
+		break;
+	}
+	msgb_free(msg);
+	return 0;
+}
+
+/***********************************************************************
+ * SGs connection "VLR Reset Procedure" FSM
+ ***********************************************************************/
+
+static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
+	{SGS_VLRR_E_START_RESET, "START-RESET"},
+	{SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
+	{0, NULL}
+};
+
+static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case SGS_VLRR_E_RX_RESET_ACK:
+		break;
+	default:
+		OSMO_ASSERT(0);
+		break;
+	}
+}
+
+static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case SGS_VLRR_E_RX_RESET_ACK:
+		osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
+		break;
+	default:
+		OSMO_ASSERT(0);
+		break;
+	}
+}
+
+static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	switch (event) {
+	case SGS_VLRR_E_RX_RESET_ACK:
+		break;
+	default:
+		OSMO_ASSERT(0);
+		break;
+	}
+}
+
+static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
+	struct sgs_connection *sgc = mme->conn;
+	struct sgs_state *sgs = mme->sgs;
+
+	switch (event) {
+	case SGS_VLRR_E_START_RESET:
+		osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
+		mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
+		/* send a reset message and enter WAIT_ACK state */
+		sgs_tx_reset_ind_vlr(sgc, sgs->cfg.vlr_name);
+		osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
+		break;
+	default:
+		OSMO_ASSERT(0);
+		break;
+	}
+}
+
+static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+	struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
+	struct sgs_connection *sgc = mme->conn;
+	struct sgs_state *sgs = mme->sgs;
+
+	switch (fi->T) {
+	case 11:
+		if (mme->ns11_remaining >= 1) {
+			sgs_tx_reset_ind_vlr(sgc, sgc->sgs->cfg.vlr_name);
+			osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
+			mme->ns11_remaining--;
+		} else {
+			LOGMME(mme, LOGL_ERROR, "Ts11 expired more than Ns11 times, giving up\n");
+			osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
+		}
+		break;
+	default:
+		OSMO_ASSERT(0);
+		break;
+	}
+	return 0;
+}
+
+static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
+	[SGS_VLRR_ST_NULL] = {
+			      /* We haven't even tried yet to send a RESET */
+			      .name = "NULL",
+			      .action = sgs_vlr_reset_fsm_null,
+			      .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
+			      .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
+			      },
+	[SGS_VLRR_ST_WAIT_ACK] = {
+				  /* We're waiting for a SGsAP_RESET_ACK */
+				  .name = "WAIT-ACK",
+				  .action = sgs_vlr_reset_fsm_wait_ack,
+				  .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
+				  .out_state_mask = S(SGS_VLRR_ST_NULL) |
+				  S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
+				  },
+	[SGS_VLRR_ST_COMPLETE] = {
+				  /* Reset procedure to this MME has been completed */
+				  .name = "COMPLETE",
+				  .action = sgs_vlr_reset_fsm_complete,
+				  .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
+				  .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
+				  },
+};
+
+static struct osmo_fsm sgs_vlr_reset_fsm = {
+	.name = "SGs-VLR-RESET",
+	.states = sgs_vlr_reset_fsm_states,
+	.allstate_event_mask = S(SGS_VLRR_E_START_RESET),
+	.allstate_action = sgs_vlr_reset_fsm_allstate,
+	.timer_cb = sgs_vlr_reset_fsm_timer_cb,
+	.log_subsys = DSGS,
+	.event_names = sgs_vlr_reset_fsm_event_names,
+};
+
+/***********************************************************************
+ * SGs connection server
+ ***********************************************************************/
+
+#include <netinet/sctp.h>
+
+static int sgs_conn_readable_cb(struct osmo_stream_srv *conn)
+{
+	struct osmo_fd *ofd = osmo_stream_srv_get_ofd(conn);
+	struct sgs_connection *sgc = osmo_stream_srv_get_data(conn);
+	struct msgb *msg = sgs_msgb_alloc();
+	struct sctp_sndrcvinfo sinfo;
+	int flags = 0;
+	int rc;
+
+	/* we cannot use osmo_stream_srv_recv() here, as we might get some out-of-band info from
+	 * SCTP.  FIXME: add something like osmo_stream_srv_recv_sctp() to libosmo-netif and use
+	 * it here as well as in libosmo-sigtran */
+	rc = sctp_recvmsg(ofd->fd, msgb_data(msg), msgb_tailroom(msg), NULL, NULL, &sinfo, &flags);
+	if (rc < 0) {
+		osmo_stream_srv_destroy(conn);
+		goto out;
+	} else if (rc == 0) {
+		osmo_stream_srv_destroy(conn);
+		goto out;
+	} else {
+		msgb_put(msg, rc);
+	}
+
+	if (flags & MSG_NOTIFICATION) {
+		union sctp_notification *notif = (union sctp_notification *)msgb_data(msg);
+
+		switch (notif->sn_header.sn_type) {
+		case SCTP_SHUTDOWN_EVENT:
+			osmo_stream_srv_destroy(conn);
+			break;
+		case SCTP_ASSOC_CHANGE:
+			/* do we have to notify the SGs code about this? */
+			break;
+		default:
+			break;
+		}
+		rc = 0;
+		goto out;
+	}
+
+	/* set l2 header, as that's what we use in SGs code */
+	msg->l2h = msgb_data(msg);
+
+	if (msgb_sctp_ppid(msg) != 0) {
+		LOGSGC(sgc, LOGL_NOTICE, "Ignoring SCTP PPID %ld (spec violation)\n", msgb_sctp_ppid(msg));
+		msgb_free(msg);
+		return 0;
+	}
+	/* handle message */
+	sgs_rx(sgc, msg);
+
+	return 0;
+out:
+	msgb_free(msg);
+	return rc;
+}
+
+static int sgs_conn_closed_cb(struct osmo_stream_srv *conn)
+{
+	struct sgs_connection *sgc = osmo_stream_srv_get_data(conn);
+
+	LOGSGC(sgc, LOGL_NOTICE, "Connection lost\n");
+	if (sgc->mme) {
+		/* unlink ourselves from the MME context */
+		if (sgc->mme->conn == sgc)
+			sgc->mme->conn = NULL;
+	}
+	llist_del(&sgc->list);
+	return 0;
+}
+
+/* call-back when new connection is accept() ed on SGs */
+static int sgs_accept_cb(struct osmo_stream_srv_link *link, int fd)
+{
+	struct sgs_state *sgs = osmo_stream_srv_link_get_data(link);
+	struct sgs_connection *sgc = talloc_zero(link, struct sgs_connection);
+	OSMO_ASSERT(sgc);
+	sgc->sgs = sgs;
+	sgc->sockname = osmo_sock_get_name(sgc, fd);
+	sgc->srv = osmo_stream_srv_create(sgc, link, fd, sgs_conn_readable_cb, sgs_conn_closed_cb, sgc);
+	if (!sgc->srv) {
+		talloc_free(sgc);
+		return -1;
+	}
+	LOGSGC(sgc, LOGL_INFO, "Accepted new SGs connection\n");
+	llist_add_tail(&sgc->list, &sgs->conn_list);
+
+	return 0;
+}
+
+/* global init function */
+struct sgs_state *sgs_init(void *ctx, struct gsm_network *network)
+{
+	struct sgs_state *sgs;
+	struct osmo_stream_srv_link *link;
+
+	gsm_network = network;
+
+	sgs = sgs_state_alloc(ctx);
+	OSMO_ASSERT(sgs);
+
+	/* We currently only support one SGs instance */
+	OSMO_ASSERT(!g_sgs);
+	g_sgs = sgs;
+
+	osmo_fsm_register(&sgs_vlr_reset_fsm);
+
+	sgs->srv_link = link = osmo_stream_srv_link_create(ctx);
+	OSMO_ASSERT(sgs->srv_link);
+	osmo_stream_srv_link_set_nodelay(link, true);
+	//osmo_stream_srv_link_set_addr(link, local_ip);
+	osmo_stream_srv_link_set_port(link, 29118);
+	osmo_stream_srv_link_set_proto(link, IPPROTO_SCTP);
+	osmo_stream_srv_link_set_data(link, sgs);
+	osmo_stream_srv_link_set_accept_cb(link, sgs_accept_cb);
+	sgs_open(sgs);
+
+	return sgs;
+}
+
+int sgs_open(struct sgs_state *sgs)
+{
+	int rc;
+	struct osmo_fd *ofd = osmo_stream_srv_link_get_ofd(sgs->srv_link);
+	char *name;
+
+	rc = osmo_stream_srv_link_open(sgs->srv_link);
+	if (rc < 0) {
+		LOGP(DSGS, LOGL_ERROR, "SGs socket cannot be opened: %s\n", strerror(errno));
+		return rc;
+	}
+	name = osmo_sock_get_name(sgs, ofd->fd);
+	LOGP(DSGS, LOGL_NOTICE, "SGs socket bound to %s\n", name);
+	talloc_free(name);
+	return 0;
+}
diff --git a/src/libmsc/sgs_vty.c b/src/libmsc/sgs_vty.c
new file mode 100644
index 0000000..6a0b1ad
--- /dev/null
+++ b/src/libmsc/sgs_vty.c
@@ -0,0 +1,176 @@
+/* SGs Interface according to 3GPP TS 23.272 + TS 29.118 */
+
+/* (C 2018 by Harald Welte <laforge at gnumonks.org>
+ *
+ * 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 <string.h>
+#include <errno.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/msc/vty.h>
+#include <osmocom/netif/stream.h>
+#include <osmocom/msc/sgs_iface.h>
+#include <osmocom/msc/debug.h>
+#include <osmocom/gsm/tlv.h>
+
+struct cmd_node cfg_sgs_node = {
+	CFG_SGS_NODE,
+	"%s(config-sgs)# ",
+	1
+};
+
+DEFUN(cfg_sgs, cfg_sgs_cmd, "sgs", "Configure the SGs interface\n")
+{
+	vty->index = g_sgs;
+	vty->node = CFG_SGS_NODE;
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sgs_local_ip, cfg_sgs_local_ip_cmd,
+      "local-ip A.B.C.D", "Set the Local IP Address of the SGs interface\n" "Local IP Address of the SGs interface\n")
+{
+	struct sgs_state *sgs = vty->index;
+	int rc;
+
+	osmo_stream_srv_link_set_addr(sgs->srv_link, argv[0]);
+
+	rc = sgs_open(sgs);
+	if (rc < 0) {
+		vty_out(vty, "%% SGs socket cannot be opened: %s%s", strerror(errno), VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sgs_local_port, cfg_sgs_local_port_cmd,
+      "local-port <0-65535>", "Set the local SCTP port of the SGs interface\n" "Local SCTP port of the SGs interface\n")
+{
+	struct sgs_state *sgs = vty->index;
+	int rc;
+
+	osmo_stream_srv_link_set_port(sgs->srv_link, atoi(argv[0]));
+
+	rc = sgs_open(sgs);
+	if (rc < 0) {
+		vty_out(vty, "%% SGs socket cannot be opened: %s%s", strerror(errno), VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sgs_vlr_name, cfg_sgs_vlr_name_cmd,
+      "vlr-name FQDN", "Set the SGs VLR Name as per TS 29.118 9.4.22\n" "Fully-Qualified Domain Name of this VLR\n")
+{
+	struct sgs_state *sgs = vty->index;
+	osmo_talloc_replace_string(sgs, &sgs->cfg.vlr_name, argv[0]);
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sgs_timer, cfg_sgs_timer_cmd,
+      "timer (Ts5|TS6-2|Ts7|Ts11|Ts14|Ts15) <1-120>",
+      "Configure SGs Timer\n"
+      "Paging procedure guard timer\n"
+      "TMSI reallocation guard timer\n"
+      "Non-EPS alert procedure guard timer\n"
+      "VLR reset guard timer\n"
+      "UE fallback prcoedure timer\n" "MO UE fallback procedure guard timer\n" "Time in seconds\n")
+{
+	struct sgs_state *sgs = vty->index;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(sgs->cfg.timer); i++) {
+		if (!strcmp(argv[0], sgs_state_timer_names[i])) {
+			sgs->cfg.timer[i] = atoi(argv[1]);
+			return CMD_SUCCESS;
+		}
+	}
+
+	return CMD_WARNING;
+}
+
+DEFUN(cfg_sgs_counter, cfg_sgs_counter_cmd,
+      "counter (Ns7|Ns11) <0-255>",
+      "Configure SGs Counter\n" "Non-EPS alert request retry counter\n" "VLR reset retry counter\n" "Counter value\n")
+{
+	struct sgs_state *sgs = vty->index;
+	unsigned int i = 0;
+
+	for (i = 0; i < ARRAY_SIZE(sgs->cfg.counter); i++) {
+		if (!strcmp(argv[0], sgs_state_counter_names[i])) {
+			sgs->cfg.counter[i] = atoi(argv[1]);
+			return CMD_SUCCESS;
+		}
+	}
+
+	return CMD_WARNING;
+}
+
+DEFUN(show_sgs_conn, show_sgs_conn_cmd, "show sgs-connections", SHOW_STR "Show SGs interface connections / MMEs\n")
+{
+	struct sgs_connection *sgc;
+
+	llist_for_each_entry(sgc, &g_sgs->conn_list, list) {
+		vty_out(vty, " %s %s%s", sgc->sockname, sgc->mme ? sgc->mme->fqdn : "", VTY_NEWLINE);
+	}
+	return CMD_SUCCESS;
+}
+
+static int config_write_sgs(struct vty *vty)
+{
+	struct sgs_state *sgs = g_sgs;
+	unsigned int i;
+
+	vty_out(vty, "sgs%s", VTY_NEWLINE);
+	if (sgs->cfg.local_port != 29118)
+		vty_out(vty, " local-port %u%s", sgs->cfg.local_port, VTY_NEWLINE);
+	if (sgs->cfg.local_addr)
+		vty_out(vty, " local-ip %s%s", sgs->cfg.local_addr, VTY_NEWLINE);
+	if (sgs->cfg.vlr_name)
+		vty_out(vty, " vlr-name %s%s", sgs->cfg.vlr_name, VTY_NEWLINE);
+
+	for (i = 0; i < ARRAY_SIZE(sgs->cfg.timer); i++) {
+		if (sgs->cfg.timer[i] == sgs_state_timer_defaults[i])
+			continue;
+		vty_out(vty, " timer %s %u%s", sgs_state_timer_names[i], sgs->cfg.timer[i], VTY_NEWLINE);
+	}
+
+	for (i = 0; i < ARRAY_SIZE(sgs->cfg.counter); i++) {
+		if (sgs->cfg.timer[i] == sgs_state_counter_defaults[i])
+			continue;
+		vty_out(vty, " counter %s %u%s", sgs_state_counter_names[i], sgs->cfg.counter[i], VTY_NEWLINE);
+	}
+
+	return 1;
+}
+
+void sgs_vty_init(void)
+{
+	/* configuration commands / nodes */
+	install_element(CONFIG_NODE, &cfg_sgs_cmd);
+	install_node(&cfg_sgs_node, config_write_sgs);
+	install_element(CFG_SGS_NODE, &cfg_sgs_local_ip_cmd);
+	install_element(CFG_SGS_NODE, &cfg_sgs_local_port_cmd);
+	install_element(CFG_SGS_NODE, &cfg_sgs_timer_cmd);
+	install_element(CFG_SGS_NODE, &cfg_sgs_counter_cmd);
+	install_element(CFG_SGS_NODE, &cfg_sgs_vlr_name_cmd);
+
+	install_element_ve(&show_sgs_conn_cmd);
+}
diff --git a/src/libmsc/silent_call.c b/src/libmsc/silent_call.c
index 9888894..a173a0f 100644
--- a/src/libmsc/silent_call.c
+++ b/src/libmsc/silent_call.c
@@ -129,7 +129,8 @@
 	 * This doesn't apply to the situation after MSCSPLIT with an
 	 * A-interface. */
 	req = subscr_request_conn(vsub, paging_cb_silent, data,
-				  "establish silent call");
+				  "establish silent call",
+				  SGSAP_SERV_IND_CS_CALL);
 	if (!req)
 		return -ENODEV;
 	return 0;
diff --git a/src/libvlr/Makefile.am b/src/libvlr/Makefile.am
index dcae1c7..790943c 100644
--- a/src/libvlr/Makefile.am
+++ b/src/libvlr/Makefile.am
@@ -16,6 +16,7 @@
 	vlr_auth_fsm.h \
 	vlr_core.h \
 	vlr_lu_fsm.h \
+	vlr_sgs_fsm.h \
 	$(NULL)
 
 noinst_LIBRARIES = libvlr.a
@@ -25,4 +26,6 @@
 	vlr_access_req_fsm.c \
 	vlr_auth_fsm.c \
 	vlr_lu_fsm.c \
+	vlr_sgs.c \
+	vlr_sgs_fsm.c \
 	$(NULL)
diff --git a/src/libvlr/vlr.c b/src/libvlr/vlr.c
index f7572f2..920c332 100644
--- a/src/libvlr/vlr.c
+++ b/src/libvlr/vlr.c
@@ -30,6 +30,7 @@
 #include <osmocom/msc/gsm_subscriber.h>
 #include <osmocom/gsupclient/gsup_client.h>
 #include <osmocom/msc/vlr.h>
+#include <osmocom/msc/vlr_sgs.h>
 #include <osmocom/msc/debug.h>
 
 #include <netinet/in.h>
@@ -41,6 +42,7 @@
 #include "vlr_auth_fsm.h"
 #include "vlr_lu_fsm.h"
 #include "vlr_access_req_fsm.h"
+#include "vlr_sgs_fsm.h"
 
 #define SGSN_SUBSCR_MAX_RETRIES 3
 #define SGSN_SUBSCR_RETRY_INTERVAL 10
@@ -247,6 +249,11 @@
 	INIT_LLIST_HEAD(&vsub->cs.requests);
 	INIT_LLIST_HEAD(&vsub->ps.pdp_list);
 
+	/* Create an SGs FSM, which is needed to control CSFB,
+	 * in cases where CSFB/SGs is not in use, this FSM will
+	 * just do nothing. (see also: sgs_iface.c) */
+	vlr_sgs_fsm_create(vsub);
+
 	llist_add_tail(&vsub->list, &vlr->subscribers);
 	return vsub;
 }
@@ -292,6 +299,10 @@
 {
 	llist_del(&vsub->list);
 	DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
+
+	/* Remove SGs FSM (see also: sgs_iface.c) */
+	vlr_sgs_fsm_remove(vsub);
+
 	talloc_free(vsub);
 }
 
@@ -816,7 +827,13 @@
 static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
 				    const struct osmo_gsup_message *gsup)
 {
-	if (!vsub->lu_fsm) {
+	struct sgs_lu_response sgs_lu_response;
+	bool sgs_lu_in_progress = false;
+
+	if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
+		sgs_lu_in_progress = true;
+
+	if (!vsub->lu_fsm && !sgs_lu_in_progress) {
 		LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
 			 "without LU in progress\n");
 		return -ENODEV;
@@ -827,7 +844,12 @@
 	 * nested INSERT SUBSCRIBER DATA transaction */
 	vlr_subscr_gsup_insert_data(vsub, gsup);
 
-	osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
+	if (sgs_lu_in_progress) {
+		sgs_lu_response.accepted = true;
+		sgs_lu_response.vsub = vsub;
+		vsub->sgs.response_cb(&sgs_lu_response);
+	} else
+		osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
 
 	return 0;
 }
@@ -836,7 +858,13 @@
 static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
 				    const struct osmo_gsup_message *gsup)
 {
-	if (!vsub->lu_fsm) {
+	struct sgs_lu_response sgs_lu_response;
+	bool sgs_lu_in_progress = false;
+
+	if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
+		sgs_lu_in_progress = true;
+
+	if (!vsub->lu_fsm && !sgs_lu_in_progress) {
 		LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
 			 "without LU in progress\n");
 		return -ENODEV;
@@ -845,9 +873,13 @@
 	LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
 		 get_value_string(gsm48_gmm_cause_names, gsup->cause));
 
-	osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
-				(void *)&gsup->cause);
-
+	if (sgs_lu_in_progress) {
+		sgs_lu_response.accepted = false;
+		sgs_lu_response.vsub = vsub;
+		vsub->sgs.response_cb(&sgs_lu_response);
+	} else
+		osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
+					(void *)&gsup->cause);
 	return 0;
 }
 
@@ -1157,6 +1189,9 @@
 	vsub->imsi_detached_flag = true;
 	vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
 
+	/* Inform the UE-SGs FSM that the subscriber has been detached */
+	osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
+
 	/* balancing the get from vlr_lu_compl_fsm_success() */
 	vlr_subscr_expire(vsub);
 
@@ -1206,6 +1241,8 @@
 	vlr_lu_fsm_init();
 	/* vlr_access_request_fsm.c */
 	vlr_parq_fsm_init();
+	/* vlr_sgs_fsm.c */
+	vlr_sgs_fsm_init();
 
 	return vlr;
 }
diff --git a/src/libvlr/vlr_lu_fsm.c b/src/libvlr/vlr_lu_fsm.c
index 717db42..10cb656 100644
--- a/src/libvlr/vlr_lu_fsm.c
+++ b/src/libvlr/vlr_lu_fsm.c
@@ -27,6 +27,7 @@
 #include "vlr_core.h"
 #include "vlr_auth_fsm.h"
 #include "vlr_lu_fsm.h"
+#include "vlr_sgs_fsm.h"
 
 #define S(x)	(1 << (x))
 
@@ -1045,6 +1046,11 @@
 
 	OSMO_ASSERT(lfp->vsub);
 
+	/* At this point we know for which subscriber the location update is,
+	 * we now must inform SGs-UE FSM that we received a location update
+	 * via A, IU or Gs interface. */
+	osmo_fsm_inst_dispatch(lfp->vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_A_IU_GS, NULL);
+
 	/* See 3GPP TS 23.012, procedure Retrieve_IMEISV_If_Required */
 	if ((!vlr->cfg.retrieve_imeisv_early)
 	    || (lfp->type == VLR_LU_TYPE_PERIODIC && lfp->vsub->imeisv[0])) {
diff --git a/src/libvlr/vlr_sgs.c b/src/libvlr/vlr_sgs.c
new file mode 100644
index 0000000..d8c3a36
--- /dev/null
+++ b/src/libvlr/vlr_sgs.c
@@ -0,0 +1,124 @@
+/* (C) 2018 by sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Harald Welte, Philipp Maier
+ *
+ * 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/fsm.h>
+#include <osmocom/msc/debug.h>
+#include <osmocom/msc/vlr.h>
+#include <osmocom/msc/vlr_sgs.h>
+#include "vlr_sgs_fsm.h"
+
+/* Reset all SGs-Associations back to zero.
+ * \param[in] vlr VLR instace. */
+void vlr_sgs_reset(struct vlr_instance *vlr)
+{
+	struct vlr_subscr *vsub;
+
+	OSMO_ASSERT(vlr);
+
+	LOGP(DVLR, LOGL_INFO, "dropping all SGs associations.\n");
+
+	llist_for_each_entry(vsub, &vlr->subscribers, list) {
+		osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_RESET_FROM_MME, NULL);
+	}
+}
+
+/*! Perform an SGs location update.
+ * \param[in] vlr VLR instace.
+ * \param[in] response_cb calback function that is called when LU is done.
+ * \param[in] mme_name fqdn of the requesting MME (mme-name).
+ * \param[in] type location update type (normal or IMSI attach).
+ * \param[in] imsi mobile identity (IMSI).
+ * \param[in] new_lai identifier of the new location area.
+ * \returns 0 in case of success, -EINVAL in case of error. */
+int vlr_sgs_loc_update(struct vlr_instance *vlr,
+		       vlr_sgs_lu_response_cb_t response_cb,
+		       char *mme_name, enum vlr_lu_type type, const char *imsi, struct osmo_location_area_id *new_lai)
+{
+	struct vlr_subscr *vsub = NULL;
+	vsub = vlr_subscr_find_or_create_by_imsi(vlr, imsi, NULL);
+
+	OSMO_ASSERT(response_cb);
+
+	if (!vsub) {
+		LOGP(DSGS, LOGL_ERROR, "VLR subscriber allocation failed\n");
+		return -EINVAL;
+	}
+
+	vsub->sgs.response_cb = response_cb;
+	vlr_subscr_set_imsi(vsub, imsi);
+	osmo_strlcpy(vsub->sgs.mme_name, mme_name, sizeof(vsub->sgs.mme_name));
+
+	osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_MME, NULL);
+
+	/* FIXME: put type parameter to the right place! */
+	printf("==============================TYPE %x\n", type);
+
+	/* FIXME: see note for struct member in vlr.h */
+	memcpy(&vsub->sgs.lai, new_lai, sizeof(vsub->sgs.lai));
+
+	return 0;
+}
+
+/*! Notify that the SGs Location Update accept message has been sent to MME.
+ *  \param[in] vlr_subscr VLR subscriber. */
+void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vlr_subscr)
+{
+	osmo_fsm_inst_dispatch(vlr_subscr->sgs_fsm, SGS_UE_E_TX_LU_ACCEPT, NULL);
+}
+
+/*! Notify that the SGs Location Update reject message has been sent to MME.
+ *  \param[in] vlr_subscr VLR subscriber. */
+void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vlr_subscr)
+{
+	osmo_fsm_inst_dispatch(vlr_subscr->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
+}
+
+/*! Perform an SGs ISMI/EPS detach */
+void vlr_sgs_detach(struct vlr_instance *vlr, const char *imsi, bool eps)
+{
+	struct vlr_subscr *vsub;
+	vsub = vlr_subscr_find_by_imsi(vlr, imsi);
+	if (!vsub) {
+		/* Subscriber never existed or got already (implicitly) detached. */
+		return;
+	}
+
+	/* FIXME: What about the eps parameter? How is EPS Detach different
+	 * from IMSI Detach? */
+
+	vsub->imsi_detached_flag = true;
+	vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
+
+	/* Inform the UE-SGs FSM that the subscriber has been detached */
+	osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_MME, NULL);
+
+	/* balancing the get from vlr_lu_compl_fsm_success() */
+	vlr_subscr_expire(vsub);
+}
+
+/*! Check if the has an SGs association.
+ * \returns true if a an SGs association is present, otherwise false */
+bool vlr_sgs_assoc(struct vlr_subscr * vlr_subscr)
+{
+	if (vlr_subscr->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
+		return true;
+	return false;
+}
diff --git a/src/libvlr/vlr_sgs_fsm.c b/src/libvlr/vlr_sgs_fsm.c
new file mode 100644
index 0000000..ebfc1ba
--- /dev/null
+++ b/src/libvlr/vlr_sgs_fsm.c
@@ -0,0 +1,236 @@
+/* (C) 2018 by sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Harald Welte, Philipp Maier
+ *
+ * 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/fsm.h>
+#include <osmocom/msc/debug.h>
+#include <osmocom/msc/vlr.h>
+#include <osmocom/msc/vlr_sgs.h>
+
+#include "vlr_sgs_fsm.h"
+#include "vlr_core.h"
+
+#define S(x) (1 << (x))
+
+static const struct value_string sgs_ue_fsm_event_names[] = {
+	{SGS_UE_E_VLR_FAILURE, "VLR_FAILURE"},
+	{SGS_UE_E_RX_RESET_FROM_MME, "RX_RESET_FROM_MME"},
+	{SGS_UE_E_RX_DETACH_IND_FROM_MME, "RX_DETACH_IND_FROM_MME"},
+	{SGS_UE_E_RX_DETACH_IND_FROM_UE, "RX_DETACH_IND_FROM_UE"},	/* vlr.c */
+	{SGS_UE_E_RX_LU_FROM_A_IU_GS, "RX_LU_FROM_A_Iu_Gs"},	/* vlr_lu_fsm.c */
+	{SGS_UE_E_RX_PAGING_FAILURE, "RX_PAGING_FAILURE"},
+	{SGS_UE_E_RX_ALERT_FAILURE, "RX_ALERT_FAILURE"},
+	{SGS_UE_E_RX_LU_FROM_MME, "RX_LU_FROM_MME"},
+	{SGS_UE_E_TX_LU_REJECT, "TX_LU_REJECT"},
+	{SGS_UE_E_TX_LU_ACCEPT, "TX_LU_ACCEPT"},
+	{SGS_UE_E_TX_PAGING, "TX_PAGING"},
+	{SGS_UE_E_RX_SGSAP_UE_UNRECHABLE, "RX_SGSAP_UE_UNREACH"},
+	{SGS_UE_E_RX_TMSI_REALLOC, "RX_TMSI_REALLOC"},
+	{0, NULL}
+};
+
+/* Figure 4.2.2.1 SGs-NULL */
+static void sgs_ue_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct vlr_subscr *vsub = fi->priv;
+	int rc;
+
+	switch (event) {
+	case SGS_UE_E_RX_LU_FROM_MME:
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_LA_UPD_PRES, 0, 0);
+		rc = vlr_subscr_req_lu(vsub, vsub->vlr->cfg.is_ps);
+		if (rc != 0)
+			LOGPFSML(fi, LOGL_ERROR, "(sub %s) HLR LU request failed\n", vlr_subscr_msisdn_or_name(vsub));
+		break;
+	case SGS_UE_E_TX_PAGING:
+		/* do nothing */
+		break;
+	case SGS_UE_E_RX_PAGING_FAILURE:
+		/* do nothing */
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+/* Figure 4.2.2.1 SGs-LA-UPDATE-PRESENT */
+static void sgs_ue_fsm_lau_present(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct vlr_subscr *vsub = fi->priv;
+
+	switch (event) {
+	case SGS_UE_E_TX_LU_ACCEPT:
+		vsub->cs.attached_via_ran = RAN_UTRAN_SGS;
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, 0, 0);
+		vsub->conf_by_radio_contact_ind = true;
+		break;
+	case SGS_UE_E_TX_LU_REJECT:
+	case SGS_UE_E_RX_PAGING_FAILURE:
+	case SGS_UE_E_RX_ALERT_FAILURE:
+		/* FIXME: Not sure if we really must reset this, it will
+		 * be overwritten anyway when RAN changes to 2G or 3G? */
+		if (vsub->cs.attached_via_ran == RAN_UTRAN_SGS)
+			vsub->cs.attached_via_ran = RAN_UNKNOWN;
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_NULL, 0, 0);
+		break;
+	case SGS_UE_E_TX_PAGING:
+		/* do nothing */
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+/* Figure 4.2.2.1 SGs-ASSOCIATED */
+static void sgs_ue_fsm_associated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct vlr_subscr *vsub = fi->priv;
+	int rc;
+
+	switch (event) {
+	case SGS_UE_E_TX_PAGING:
+		/* do nothing */
+		break;
+	case SGS_UE_E_RX_TMSI_REALLOC:
+		/* do nothing */
+		break;
+	case SGS_UE_E_RX_SGSAP_UE_UNRECHABLE:
+		/* do nothing */
+		break;
+	case SGS_UE_E_RX_PAGING_FAILURE:
+	case SGS_UE_E_RX_ALERT_FAILURE:
+		/* FIXME: Not sure if we really must reset this, it will
+		 * be overwritten anyway when RAN changes to 2G or 3G? */
+		if (vsub->cs.attached_via_ran == RAN_UTRAN_SGS)
+			vsub->cs.attached_via_ran = RAN_UNKNOWN;
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_NULL, 0, 0);
+		break;
+	case SGS_UE_E_RX_LU_FROM_MME:
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_LA_UPD_PRES, 0, 0);
+		rc = vlr_subscr_req_lu(vsub, vsub->vlr->cfg.is_ps);
+		if (rc != 0)
+			LOGPFSML(fi, LOGL_ERROR, "(sub %s) HLR LU request failed\n", vlr_subscr_msisdn_or_name(vsub));
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+/* Figure 4.2.2.1 From any of the three states (at the VLR) */
+static void sgs_ue_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
+{
+	struct vlr_subscr *vsub = fi->priv;
+
+	osmo_fsm_inst_update_id(fi, vlr_subscr_msisdn_or_name(vsub));
+
+	switch (event) {
+	case SGS_UE_E_VLR_FAILURE:
+	case SGS_UE_E_RX_RESET_FROM_MME:
+	case SGS_UE_E_RX_DETACH_IND_FROM_MME:
+	case SGS_UE_E_RX_DETACH_IND_FROM_UE:
+	case SGS_UE_E_RX_LU_FROM_A_IU_GS:
+		osmo_fsm_inst_state_chg(fi, SGS_UE_ST_NULL, 0, 0);
+		break;
+	default:
+		OSMO_ASSERT(0);
+	}
+}
+
+static int sgs_ue_fsm_timer_cb(struct osmo_fsm_inst *fi)
+{
+	return 0;
+}
+
+static const struct osmo_fsm_state sgs_ue_fsm_states[] = {
+	[SGS_UE_ST_NULL] = {
+			    .name = "SGs-NULL",
+			    .action = sgs_ue_fsm_null,
+			    .in_event_mask = S(SGS_UE_E_RX_LU_FROM_MME) |
+			    S(SGS_UE_E_TX_PAGING) | S(SGS_UE_E_RX_PAGING_FAILURE),
+			    .out_state_mask = S(SGS_UE_ST_NULL) | S(SGS_UE_ST_LA_UPD_PRES),
+			    },
+	[SGS_UE_ST_ASSOCIATED] = {
+				  .name = "SGs-ASSOCIATED",
+				  .action = sgs_ue_fsm_associated,
+				  .in_event_mask = S(SGS_UE_E_TX_PAGING) |
+				  S(SGS_UE_E_RX_TMSI_REALLOC) |
+				  S(SGS_UE_E_RX_SGSAP_UE_UNRECHABLE) |
+				  S(SGS_UE_E_RX_PAGING_FAILURE) |
+				  S(SGS_UE_E_RX_ALERT_FAILURE) | S(SGS_UE_E_RX_LU_FROM_MME),
+				  .out_state_mask = S(SGS_UE_ST_NULL) |
+				  S(SGS_UE_ST_ASSOCIATED) | S(SGS_UE_ST_LA_UPD_PRES),
+				  },
+	[SGS_UE_ST_LA_UPD_PRES] = {
+				   .name = "SGs-LA-UPDATE-PRESENT",
+				   .action = sgs_ue_fsm_lau_present,
+				   .in_event_mask = S(SGS_UE_E_TX_LU_ACCEPT) |
+				   S(SGS_UE_E_TX_LU_REJECT) |
+				   S(SGS_UE_E_TX_PAGING) | S(SGS_UE_E_RX_PAGING_FAILURE) | S(SGS_UE_E_RX_ALERT_FAILURE),
+				   .out_state_mask = S(SGS_UE_ST_NULL) |
+				   S(SGS_UE_ST_ASSOCIATED) | S(SGS_UE_ST_LA_UPD_PRES),
+				   },
+};
+
+static struct osmo_fsm sgs_ue_fsm = {
+	.name = "SGs-UE",
+	.states = sgs_ue_fsm_states,
+	.num_states = ARRAY_SIZE(sgs_ue_fsm_states),
+	.allstate_event_mask = S(SGS_UE_E_RX_RESET_FROM_MME) |
+	    S(SGS_UE_E_VLR_FAILURE) |
+	    S(SGS_UE_E_RX_DETACH_IND_FROM_MME) | S(SGS_UE_E_RX_DETACH_IND_FROM_UE) | S(SGS_UE_E_RX_LU_FROM_A_IU_GS),
+	.allstate_action = sgs_ue_fsm_allstate,
+	.timer_cb = sgs_ue_fsm_timer_cb,
+	.log_subsys = DSGS,
+	.event_names = sgs_ue_fsm_event_names,
+};
+
+/*! Initalize/Register SGs FSM in osmo-fsm subsystem */
+void vlr_sgs_fsm_init(void)
+{
+	if (osmo_fsm_find_by_name(sgs_ue_fsm.name) != &sgs_ue_fsm)
+		osmo_fsm_register(&sgs_ue_fsm);
+}
+
+/*! Crate SGs FSM in struct vlr_subscr.
+ *  \param[in] vsub VLR subscriber for which the SGs FSM should be created. */
+void vlr_sgs_fsm_create(struct vlr_subscr *vsub)
+{
+	/* An SGSs FSM must not be created twice! */
+	OSMO_ASSERT(!vsub->sgs_fsm);
+
+	vsub->sgs_fsm = osmo_fsm_inst_alloc(&sgs_ue_fsm, vsub, vsub, LOGL_INFO, vlr_subscr_msisdn_or_name(vsub));
+	OSMO_ASSERT(vsub->sgs_fsm);
+
+	osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
+
+//      DEBUGP(DSGS, "(sub %s) SGs FSM created\n", vlr_subscr_msisdn_or_name(vsub));
+}
+
+/*! Remove SGs FSM from struct vlr_subscr.
+ *  \param[in] vsub VLR subscriber from which the SGs FSM should be removed. */
+void vlr_sgs_fsm_remove(struct vlr_subscr *vsub)
+{
+	/* An SGSs FSM must exist! */
+	OSMO_ASSERT(vsub->sgs_fsm);
+
+	osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
+	osmo_fsm_inst_term(vsub->sgs_fsm, OSMO_FSM_TERM_REGULAR, NULL);
+	vsub->sgs_fsm = NULL;
+}
diff --git a/src/libvlr/vlr_sgs_fsm.h b/src/libvlr/vlr_sgs_fsm.h
new file mode 100644
index 0000000..2233ceb
--- /dev/null
+++ b/src/libvlr/vlr_sgs_fsm.h
@@ -0,0 +1,41 @@
+/* (C) 2018 by sysmocom s.f.m.c. GmbH
+ * All Rights Reserved
+ *
+ * Author: Harald Welte, Philipp Maier
+ *
+ * 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/>.
+ *
+ */
+
+#pragma once
+
+enum sgs_ue_fsm_event {
+	SGS_UE_E_VLR_FAILURE,
+	SGS_UE_E_RX_RESET_FROM_MME,
+	SGS_UE_E_RX_DETACH_IND_FROM_MME,
+	SGS_UE_E_RX_DETACH_IND_FROM_UE,
+	SGS_UE_E_RX_LU_FROM_A_IU_GS,
+	SGS_UE_E_RX_PAGING_FAILURE,
+	SGS_UE_E_RX_ALERT_FAILURE,
+	SGS_UE_E_RX_LU_FROM_MME,
+	SGS_UE_E_TX_LU_REJECT,
+	SGS_UE_E_TX_LU_ACCEPT,
+	SGS_UE_E_TX_PAGING,
+	SGS_UE_E_RX_SGSAP_UE_UNRECHABLE,
+	SGS_UE_E_RX_TMSI_REALLOC,
+};
+
+void vlr_sgs_fsm_init(void);
+void vlr_sgs_fsm_create(struct vlr_subscr *vsub);
+void vlr_sgs_fsm_remove(struct vlr_subscr *vsub);
diff --git a/src/osmo-msc/Makefile.am b/src/osmo-msc/Makefile.am
index bcc4ada..abe9ad3 100644
--- a/src/osmo-msc/Makefile.am
+++ b/src/osmo-msc/Makefile.am
@@ -12,6 +12,7 @@
 	$(LIBOSMOVTY_CFLAGS) \
 	$(LIBOSMOCTRL_CFLAGS) \
 	$(LIBOSMOABIS_CFLAGS) \
+	$(LIBOSMONETIF_CFLAGS) \
 	$(LIBSMPP34_CFLAGS) \
 	$(LIBOSMORANAP_CFLAGS) \
 	$(LIBASN1C_CFLAGS) \
@@ -40,11 +41,13 @@
 	$(LIBOSMOCORE_LIBS) \
 	$(LIBOSMOCTRL_LIBS) \
 	$(LIBOSMOABIS_LIBS) \
+	$(LIBOSMONETIF_LIBS) \
 	$(LIBSMPP34_LIBS) \
 	$(LIBOSMOSIGTRAN_LIBS) \
 	$(LIBOSMOMGCPCLIENT_LIBS) \
 	$(LIBOSMOGSUPCLIENT_LIBS) \
 	-ldbi \
+	-lsctp \
 	$(NULL)
 if BUILD_IU
 osmo_msc_LDADD += \
diff --git a/src/osmo-msc/msc_main.c b/src/osmo-msc/msc_main.c
index 53d27d3..f5bec7d 100644
--- a/src/osmo-msc/msc_main.c
+++ b/src/osmo-msc/msc_main.c
@@ -61,6 +61,7 @@
 #include <osmocom/msc/smpp.h>
 #include <osmocom/sigtran/osmo_ss7.h>
 #include <osmocom/mgcp_client/mgcp_client.h>
+#include <osmocom/msc/sgs_iface.h>
 
 #ifdef BUILD_IU
 #include <osmocom/ranap/iu_client.h>
@@ -493,6 +494,12 @@
 		.description = "BSSAP Protocol (A Interface)",
 		.enabled = 1, .loglevel = LOGL_NOTICE,
 	},
+	[DSGS] = {
+		.name = "DSGS",
+		.description = "SGs Interface (SGsAP)",
+		.enabled = 1, .loglevel = LOGL_NOTICE,
+	},
+
 
 };
 
@@ -570,6 +577,7 @@
 	if (smpp_openbsc_alloc_init(tall_msc_ctx) < 0)
 		return -1;
 #endif
+	sgs_init(tall_msc_ctx, msc_network);
 
 	rc = vty_read_config_file(msc_cmdline_config.config_file, NULL);
 	if (rc < 0) {
diff --git a/tests/msc_vlr/msc_vlr_test_authen_reuse.c b/tests/msc_vlr/msc_vlr_test_authen_reuse.c
index 6591557..cf5d67a 100644
--- a/tests/msc_vlr/msc_vlr_test_authen_reuse.c
+++ b/tests/msc_vlr/msc_vlr_test_authen_reuse.c
@@ -320,3 +320,23 @@
 	test_no_auth_reuse_utran,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_call.c b/tests/msc_vlr/msc_vlr_test_call.c
index 5819e51..486aa30 100644
--- a/tests/msc_vlr/msc_vlr_test_call.c
+++ b/tests/msc_vlr/msc_vlr_test_call.c
@@ -588,3 +588,23 @@
 	test_call_mo_to_unknown_timeout,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_gsm_authen.c b/tests/msc_vlr/msc_vlr_test_gsm_authen.c
index 8129781..fd5dd10 100644
--- a/tests/msc_vlr/msc_vlr_test_gsm_authen.c
+++ b/tests/msc_vlr/msc_vlr_test_gsm_authen.c
@@ -1003,3 +1003,23 @@
 	test_wrong_sres_length,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_gsm_ciph.c b/tests/msc_vlr/msc_vlr_test_gsm_ciph.c
index 8f256b1..b1ea4d0 100644
--- a/tests/msc_vlr/msc_vlr_test_gsm_ciph.c
+++ b/tests/msc_vlr/msc_vlr_test_gsm_ciph.c
@@ -1512,3 +1512,23 @@
 	test_cm_service_needs_classmark_update,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_hlr_reject.c b/tests/msc_vlr/msc_vlr_test_hlr_reject.c
index 13bed42..51c7d03 100644
--- a/tests/msc_vlr/msc_vlr_test_hlr_reject.c
+++ b/tests/msc_vlr/msc_vlr_test_hlr_reject.c
@@ -456,3 +456,23 @@
 	test_hlr_no_insert_data,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_hlr_timeout.c b/tests/msc_vlr/msc_vlr_test_hlr_timeout.c
index a237695..ae5f154 100644
--- a/tests/msc_vlr/msc_vlr_test_hlr_timeout.c
+++ b/tests/msc_vlr/msc_vlr_test_hlr_timeout.c
@@ -118,3 +118,23 @@
 	test_hlr_timeout_lu_upd_loc_result,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_ms_timeout.c b/tests/msc_vlr/msc_vlr_test_ms_timeout.c
index a4851b3..7ea2e79 100644
--- a/tests/msc_vlr/msc_vlr_test_ms_timeout.c
+++ b/tests/msc_vlr/msc_vlr_test_ms_timeout.c
@@ -368,3 +368,23 @@
 	test_classmark_update_timeout,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_no_authen.c b/tests/msc_vlr/msc_vlr_test_no_authen.c
index b34ddd7..c3e3cd1 100644
--- a/tests/msc_vlr/msc_vlr_test_no_authen.c
+++ b/tests/msc_vlr/msc_vlr_test_no_authen.c
@@ -983,3 +983,23 @@
 	test_no_authen_subscr_expire,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_reject_concurrency.c b/tests/msc_vlr/msc_vlr_test_reject_concurrency.c
index d206571..4af6571 100644
--- a/tests/msc_vlr/msc_vlr_test_reject_concurrency.c
+++ b/tests/msc_vlr/msc_vlr_test_reject_concurrency.c
@@ -404,3 +404,23 @@
 	test_reject_paging_resp_during_paging_resp,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_rest.c b/tests/msc_vlr/msc_vlr_test_rest.c
index 247e7ae..7af526f 100644
--- a/tests/msc_vlr/msc_vlr_test_rest.c
+++ b/tests/msc_vlr/msc_vlr_test_rest.c
@@ -203,3 +203,24 @@
 	test_lu_unknown_tmsi,
 	NULL
 };
+
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_ss.c b/tests/msc_vlr/msc_vlr_test_ss.c
index 34aa634..ab43002 100644
--- a/tests/msc_vlr/msc_vlr_test_ss.c
+++ b/tests/msc_vlr/msc_vlr_test_ss.c
@@ -213,3 +213,23 @@
 	test_ss_ussd_no_geran, /* Network-originated */
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/msc_vlr/msc_vlr_test_umts_authen.c b/tests/msc_vlr/msc_vlr_test_umts_authen.c
index 62e280e..ffd209a 100644
--- a/tests/msc_vlr/msc_vlr_test_umts_authen.c
+++ b/tests/msc_vlr/msc_vlr_test_umts_authen.c
@@ -880,3 +880,23 @@
 	test_umts_authen_only_sres_utran,
 	NULL
 };
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }
diff --git a/tests/sms_queue/sms_queue_test.c b/tests/sms_queue/sms_queue_test.c
index 6ce0b88..5f87c17 100644
--- a/tests/sms_queue/sms_queue_test.c
+++ b/tests/sms_queue/sms_queue_test.c
@@ -236,3 +236,23 @@
 
 	return 0;
 }
+
+void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
+struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
+void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
+struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
+					       int fd, int (*cb)(struct osmo_stream_srv *conn),
+					       int (*closed_cb)(struct osmo_stream_srv *conn),
+					       void *data) { return NULL; }
+void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
+void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
+struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
+struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
+void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
+void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
+void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
+					(struct osmo_stream_srv_link *link, int fd)) {}
+int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
+void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
+void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
+int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }

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

Gerrit-Project: osmo-msc
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I73359925fc1ca72b33a1466e6ac41307f2f0b11d
Gerrit-Change-Number: 11642
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181106/c8a60481/attachment-0001.html>


More information about the gerrit-log mailing list