ss7: Create a SS7 application that is responsible for the routing
diff --git a/include/Makefile.am b/include/Makefile.am
index ce617e7..a4cdfb3 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -1,6 +1,6 @@
 noinst_HEADERS = mtp_level3.h mtp_data.h ipaccess.h thread.h mtp_pcap.h \
 		 mgcp_ss7.h bss_patch.h bssap_sccp.h bsc_data.h udp_input.h \
                  snmp_mtp.h cellmgr_debug.h bsc_sccp.h bsc_ussd.h sctp_m2ua.h \
-                 isup_types.h counter.h msc_connection.h
+                 isup_types.h counter.h msc_connection.h ss7_application.h
 
 SUBDIRS = mgcp
diff --git a/include/bsc_data.h b/include/bsc_data.h
index ac799f1..fc1dcdf 100644
--- a/include/bsc_data.h
+++ b/include/bsc_data.h
@@ -67,12 +67,6 @@
 	struct snmp_mtp_session *session;
 };
 
-enum {
-	APP_CELLMGR,
-	APP_RELAY,
-	APP_STP,
-};
-
 struct bsc_data {
 	int app;
 
@@ -116,6 +110,10 @@
 	/* MSCs */
 	struct llist_head mscs;
 	int num_mscs;
+
+	/* application */
+	struct llist_head apps;
+	int num_apps;
 };
 
 /* bsc related functions */
@@ -125,7 +123,6 @@
 void mtp_linkset_up(struct mtp_link_set *);
 
 /* connection tracking and action */
-void update_con_state(struct msc_connection *msc, int rc, struct sccp_parse_result *result, struct msgb *msg, int from_msc, int sls);
 
 /* udp init */
 int link_global_init(struct mtp_udp_data *data, int src_port);
diff --git a/include/bsc_sccp.h b/include/bsc_sccp.h
index 9c3bb11..f7489bb 100644
--- a/include/bsc_sccp.h
+++ b/include/bsc_sccp.h
@@ -30,7 +30,7 @@
 
 #include <osmocom/sccp/sccp.h>
 
-struct msc_connection;
+struct ss7_application;
 
 /*
  * One SCCP connection.
@@ -55,7 +55,8 @@
 	/* how often did we send a RLSD this */
 	unsigned int rls_tries;
 
-	/* MTP link this was coming in */
+	/* Link to the SS7 Application */
+	struct ss7_application *app;
 	struct mtp_link_set *link;
 
 	/* sls id */
@@ -63,10 +64,10 @@
 };
 
 void free_con(struct active_sccp_con *con);
-struct active_sccp_con *find_con_by_dest_ref(struct msc_connection *, struct sccp_source_reference *ref);
-struct active_sccp_con *find_con_by_src_ref(struct msc_connection *,struct sccp_source_reference *src_ref);
-struct active_sccp_con *find_con_by_src_dest_ref(struct msc_connection *, struct sccp_source_reference *src_ref,
+struct active_sccp_con *find_con_by_dest_ref(struct ss7_application *, struct sccp_source_reference *ref);
+struct active_sccp_con *find_con_by_src_ref(struct ss7_application *,struct sccp_source_reference *src_ref);
+struct active_sccp_con *find_con_by_src_dest_ref(struct ss7_application *, struct sccp_source_reference *src_ref,
 						 struct sccp_source_reference *dst_ref);
-unsigned int sls_for_src_ref(struct msc_connection *, struct sccp_source_reference *ref);
+unsigned int sls_for_src_ref(struct ss7_application *, struct sccp_source_reference *ref);
 
 #endif
diff --git a/include/msc_connection.h b/include/msc_connection.h
index 51a5326..c7957ec 100644
--- a/include/msc_connection.h
+++ b/include/msc_connection.h
@@ -55,16 +55,12 @@
 	int pong_time;
 	struct timer_list ping_timeout;
 	struct timer_list pong_timeout;
-	struct timer_list reset_timeout;
 
 	/* mgcp messgaes */
 	struct write_queue mgcp_agent;
 
 	/* application pointer */
-	struct llist_head sccp_connections;
-	struct mtp_link_set *target_link;
-	int forward_only;
-	int reset_count;
+	struct ss7_application *app;
 };
 
 /* msc related functions */
diff --git a/include/mtp_data.h b/include/mtp_data.h
index 6ed31c3..48b0ddb 100644
--- a/include/mtp_data.h
+++ b/include/mtp_data.h
@@ -25,10 +25,10 @@
 #include <osmocore/utils.h>
 
 struct bsc_data;
-struct msc_connection;
 struct mtp_link;
 struct mtp_level_3_mng *mng;
 struct rate_ctr_group;
+struct ss7_application;
 
 /* MTP Level3 timers */
 
@@ -76,8 +76,7 @@
 
 	/* custom data */
 	struct bsc_data *bsc;
-	struct msc_connection *fw;
-	struct mtp_link_set *forward;
+	struct ss7_application *app;
 };
 
 /**
diff --git a/include/ss7_application.h b/include/ss7_application.h
new file mode 100644
index 0000000..76117e6
--- /dev/null
+++ b/include/ss7_application.h
@@ -0,0 +1,82 @@
+/* Stuff to handle the SS7 application */
+/*
+ * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.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/>.
+ *
+ */
+
+#ifndef SS7_APPLICATION_H
+#define SS7_APPLICATION_H
+
+#include <osmocore/linuxlist.h>
+#include <osmocore/timer.h>
+
+struct bsc_data;
+struct msc_connection;
+struct mtp_link_set;
+struct mtp_link;
+
+enum ss7_set_type {
+	SS7_SET_LINKSET,
+	SS7_SET_MSC,
+};
+
+enum ss7_app_type {
+	APP_CELLMGR,
+	APP_RELAY,
+	APP_STP,
+};
+
+struct ss7_application_route {
+	int type;
+	int nr;
+
+	/* maybe they were resolved */
+	struct mtp_link_set *set;
+	struct msc_connection *msc;
+};
+
+struct ss7_application {
+	/* handling */
+	struct llist_head entry;
+	int nr;
+	char *name;
+
+	/* app type */
+	int type;
+
+	/* for the routing */
+	struct ss7_application_route route_src;
+	struct ss7_application_route route_dst;
+
+	struct bsc_data *bsc;
+
+	/* handling for the NAT/State handling */
+	struct llist_head sccp_connections;
+	struct timer_list reset_timeout;
+	struct mtp_link_set *target_link;
+	int forward_only;
+	int reset_count;
+};
+
+
+struct ss7_application *ss7_application_alloc(struct bsc_data *);
+struct ss7_application *ss7_application_num(struct bsc_data *, int nr);
+int ss7_application_setup(struct ss7_application *, int type,
+			  int src_type, int src_num,
+			  int dst_type, int dst_num);
+
+#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index edd1723..066ea91 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -12,12 +12,12 @@
 cellmgr_ng_SOURCES = main.c mtp_layer3.c thread.c input/ipaccess.c pcap.c \
 		     bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c links.c \
 		     msc_conn.c link_udp.c snmp_mtp.c debug.c vty_interface.c isup.c \
-		     mtp_link.c counter.c sccp_state.c bsc.c
+		     mtp_link.c counter.c sccp_state.c bsc.c ss7_application.c
 cellmgr_ng_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto
 
 osmo_stp_SOURCES = main_stp.c mtp_layer3.c thread.c pcap.c link_udp.c snmp_mtp.c \
 		   debug.c vty_interface.c links.c isup.c sctp_m2ua.c \
-		   mtp_link.c counter.c bsc.c
+		   mtp_link.c counter.c bsc.c ss7_application.c
 osmo_stp_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOSCCP_LIBS) $(LIBOSMOVTY_LIBS) $(NEXUSWARE_C7_LIBS) \
 		   -lpthread -lnetsnmp -lcrypto -lm2ua -lsctp
diff --git a/src/bsc.c b/src/bsc.c
index af2c1c7..63375cd 100644
--- a/src/bsc.c
+++ b/src/bsc.c
@@ -37,6 +37,7 @@
 
 	INIT_LLIST_HEAD(&bsc->linksets);
 	INIT_LLIST_HEAD(&bsc->mscs);
+	INIT_LLIST_HEAD(&bsc->apps);
 
 	bsc->dpc = 1;
 	bsc->opc = 0;
diff --git a/src/bsc_sccp.c b/src/bsc_sccp.c
index b8276d2..d2b5416 100644
--- a/src/bsc_sccp.c
+++ b/src/bsc_sccp.c
@@ -23,13 +23,14 @@
 #include "bsc_data.h"
 
 #include <cellmgr_debug.h>
-#include <msc_connection.h>
+#include <ss7_application.h>
+#include <ss7_application.h>
 
 #include <osmocore/talloc.h>
 
 #include <string.h>
 
-struct active_sccp_con *find_con_by_dest_ref(struct msc_connection *fw, struct sccp_source_reference *ref)
+struct active_sccp_con *find_con_by_dest_ref(struct ss7_application *fw, struct sccp_source_reference *ref)
 {
 	struct active_sccp_con *con;
 
@@ -48,7 +49,7 @@
 }
 
 
-struct active_sccp_con *find_con_by_src_ref(struct msc_connection *fw, struct sccp_source_reference *src_ref)
+struct active_sccp_con *find_con_by_src_ref(struct ss7_application *fw, struct sccp_source_reference *src_ref)
 {
 	struct active_sccp_con *con;
 
@@ -64,7 +65,7 @@
 	return NULL;
 }
 
-struct active_sccp_con *find_con_by_src_dest_ref(struct msc_connection *fw,
+struct active_sccp_con *find_con_by_src_dest_ref(struct ss7_application *fw,
 						 struct sccp_source_reference *src_ref,
 						 struct sccp_source_reference *dst_ref)
 {
@@ -80,7 +81,7 @@
 	return NULL;
 }
 
-unsigned int sls_for_src_ref(struct msc_connection *fw, struct sccp_source_reference *ref)
+unsigned int sls_for_src_ref(struct ss7_application *fw, struct sccp_source_reference *ref)
 {
 	struct active_sccp_con *con;
 
diff --git a/src/main.c b/src/main.c
index 2962d94..8cc37d4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -29,6 +29,7 @@
 #include <bsc_data.h>
 #include <cellmgr_debug.h>
 #include <bsc_sccp.h>
+#include <ss7_application.h>
 
 #include <osmocore/talloc.h>
 
@@ -174,6 +175,7 @@
 	int rc;
 	struct msc_connection *msc;
 	struct mtp_link_set *set;
+	struct ss7_application *app;
 
 	thread_init();
 
@@ -226,8 +228,15 @@
 	if (!set)
 		return -1;
 
-	set->fw = msc;
-	msc->target_link = set;
+	app = ss7_application_alloc(bsc);
+	if (!app) {
+		LOGP(DINP, LOGL_ERROR, "Failed to create the SS7 application.\n");
+		return -1;
+	}
+
+	ss7_application_setup(app, APP_CELLMGR,
+			      SS7_SET_LINKSET, 0,
+			      SS7_SET_MSC, 0);
 
         while (1) {
 		bsc_select_main(0);
diff --git a/src/main_stp.c b/src/main_stp.c
index 95007f6..77a90f0 100644
--- a/src/main_stp.c
+++ b/src/main_stp.c
@@ -28,6 +28,7 @@
 #include <snmp_mtp.h>
 #include <cellmgr_debug.h>
 #include <sctp_m2ua.h>
+#include <ss7_application.h>
 
 #include <osmocom/m2ua/m2ua_msg.h>
 
@@ -71,12 +72,30 @@
  */
 void mtp_link_set_forward_sccp(struct mtp_link_set *set, struct msgb *_msg, int sls)
 {
-	mtp_link_set_submit_sccp_data(set->forward, sls, _msg->l2h, msgb_l2len(_msg));
+	struct mtp_link_set *other;
+	if (!set->app) {
+		LOGP(DINP, LOGL_ERROR, "Linkset %d/%s does not have an app.\n",
+		     set->no, set->name);
+		return;
+	}
+
+	other = set->app->route_src.set == set ?
+			set->app->route_dst.set : set->app->route_src.set;
+	mtp_link_set_submit_sccp_data(other, sls, _msg->l2h, msgb_l2len(_msg));
 }
 
 void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
 {
-	mtp_link_set_submit_isup_data(set->forward, sls, msg->l3h, msgb_l3len(msg));
+	struct mtp_link_set *other;
+	if (!set->app) {
+		LOGP(DINP, LOGL_ERROR, "Linkset %d/%s does not have an app.\n",
+		     set->no, set->name);
+		return;
+	}
+
+	other = set->app->route_src.set == set ?
+			set->app->route_dst.set : set->app->route_src.set;
+	mtp_link_set_submit_isup_data(other, sls, msg->l3h, msgb_l3len(msg));
 }
 
 void mtp_linkset_down(struct mtp_link_set *set)
@@ -307,6 +326,7 @@
 	struct mtp_link_set *set;
 	struct mtp_link_set *m2ua_set;
 	struct mtp_m2ua_link *lnk;
+	struct ss7_application *app;
 
 	thread_init();
 
@@ -352,6 +372,10 @@
 		return -1;
 	}
 
+	app = ss7_application_alloc(bsc);
+	if (!app)
+		return -1;
+
 	set = link_init(bsc);
 	if (!set)
 		return -1;
@@ -374,14 +398,16 @@
 
 	/* setup things */
 	set->pass_all_isup = bsc->isup_pass;
-	set->forward = m2ua_set;
 	m2ua_set->pass_all_isup = bsc->isup_pass;
-	m2ua_set->forward = set;
 
 	lnk = mtp_m2ua_link_create(m2ua_set);
 	lnk->base.pcap_fd = -1;
 	mtp_link_set_add_link(m2ua_set, (struct mtp_link *) lnk);
 
+	ss7_application_setup(app, APP_STP,
+			      SS7_SET_LINKSET, 0,
+			      SS7_SET_LINKSET, 1);
+
 	llist_for_each_entry(data, &m2ua_set->links, entry)
 		data->start(data);
 
diff --git a/src/msc_conn.c b/src/msc_conn.c
index 84f6161..06d0d88 100644
--- a/src/msc_conn.c
+++ b/src/msc_conn.c
@@ -549,7 +549,6 @@
 		return NULL;
 	}
 
-	INIT_LLIST_HEAD(&msc->sccp_connections);
 	llist_add(&msc->entry, &bsc->mscs);
 	msc->nr = bsc->num_mscs++;
 
diff --git a/src/sccp_state.c b/src/sccp_state.c
index 27cef5e..37cba46 100644
--- a/src/sccp_state.c
+++ b/src/sccp_state.c
@@ -28,6 +28,7 @@
 #include <cellmgr_debug.h>
 #include <bsc_sccp.h>
 #include <bsc_ussd.h>
+#include <ss7_application.h>
 
 #include <osmocore/talloc.h>
 
@@ -46,10 +47,11 @@
 #include <unistd.h>
 
 static void send_reset_ack(struct mtp_link_set *link, int sls);
-static void bsc_resources_released(struct msc_connection *bsc);
+static void app_resources_released(struct ss7_application *ss7);
+static void app_clear_connections(struct ss7_application *ss7);
 static void handle_local_sccp(struct mtp_link_set *link, struct msgb *inp, struct sccp_parse_result *res, int sls);
-static void clear_connections(struct msc_connection *bsc);
 static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res);
+static void update_con_state(struct ss7_application *ss7, int rc, struct sccp_parse_result *result, struct msgb *msg, int from_msc, int sls);
 
 /* send a RSIP to the MGCP GW */
 static void mgcp_reset(struct msc_connection *fw)
@@ -68,14 +70,27 @@
 {
 	int rc;
 	struct sccp_parse_result result;
-	struct msc_connection *fw = link->fw;
+	struct msc_connection *fw;
+
 	struct msgb *msg;
 
-	if (fw->forward_only) {
-		msc_send_direct(fw, _msg);
+	if (!link->app) {
+		LOGP(DINP, LOGL_ERROR, "The linkset %d/%s has no application.\n",
+		     link->no, link->name);
 		return;
 	}
 
+	fw = link->app->route_dst.msc;
+	if (!fw) {
+		LOGP(DINP, LOGL_ERROR, "The application %d/%s has no MSC.\n",
+		     link->app->nr, link->app->name);
+		return;
+	}
+
+	if (link->app->forward_only) {
+		msc_send_direct(fw, _msg);
+		return;
+	}
 
 	rc = bss_patch_filter_msg(_msg, &result);
 	if (rc == BSS_FILTER_RESET) {
@@ -87,10 +102,10 @@
 
 	/* special responder */
 	if (fw->msc_link_down) {
-		if (rc == BSS_FILTER_RESET_ACK && fw->reset_count > 0) {
+		if (rc == BSS_FILTER_RESET_ACK && link->app->reset_count > 0) {
 			LOGP(DMSC, LOGL_ERROR, "Received reset ack for closing.\n");
-			clear_connections(fw);
-			bsc_resources_released(fw);
+			app_clear_connections(link->app);
+			app_resources_released(link->app);
 			return;
 		}
 
@@ -103,7 +118,7 @@
 	}
 
 	/* update the connection state */
-	update_con_state(link->fw, rc, &result, _msg, 0, sls);
+	update_con_state(link->app, rc, &result, _msg, 0, sls);
 
 	if (rc == BSS_FILTER_CLEAR_COMPL) {
 		send_local_rlsd(link, &result);
@@ -113,7 +128,7 @@
 	}
 
 	/* now send it out */
-	bsc_ussd_handle_out_msg(link->fw, &result, _msg);
+	bsc_ussd_handle_out_msg(fw, &result, _msg);
 
 	msg = msgb_alloc_headroom(4096, 128, "SCCP to MSC");
 	if (!msg) {
@@ -122,7 +137,7 @@
 	}
 
 	bss_rewrite_header_for_msc(rc, msg, _msg, &result);
-	msc_send_direct(link->fw, msg);
+	msc_send_direct(fw, msg);
 }
 
 void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
@@ -158,7 +173,7 @@
 
 			form1 = (struct sccp_data_form1 *) inpt->l2h;
 
-			llist_for_each_entry(con, &link->fw->sccp_connections, entry) {
+			llist_for_each_entry(con, &link->app->sccp_connections, entry) {
 				if (memcmp(&form1->destination_local_reference,
 					   &con->dst_ref, sizeof(con->dst_ref)) == 0) {
 					LOGP(DINP, LOGL_DEBUG, "Sending a release request now.\n");
@@ -176,60 +191,61 @@
 	} else if (inpt->l2h[0] == SCCP_MSG_TYPE_UDT && result->data_len >= 3) {
 		if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_RESET_ACKNOWLEDGE) {
 			LOGP(DINP, LOGL_NOTICE, "Reset ACK. Connecting to the MSC again.\n");
-			bsc_resources_released(link->fw);
+			app_resources_released(link->app);
 			return;
 		}
 	}
 
 
 	/* Update the state, maybe the connection was released? */
-	update_con_state(link->fw, 0, result, inpt, 0, sls);
-	if (llist_empty(&link->fw->sccp_connections))
-		bsc_resources_released(link->fw);
+	update_con_state(link->app, 0, result, inpt, 0, sls);
+	if (llist_empty(&link->app->sccp_connections))
+		app_resources_released(link->app);
 	return;
 }
 
-static void clear_connections(struct msc_connection *fw)
+static void app_clear_connections(struct ss7_application *app)
 {
 	struct active_sccp_con *tmp, *con;
 
-	llist_for_each_entry_safe(con, tmp, &fw->sccp_connections, entry) {
+	llist_for_each_entry_safe(con, tmp, &app->sccp_connections, entry) {
 		free_con(con);
 	}
 
-	link_clear_all(fw->target_link);
+	link_clear_all(app->route_src.set);
 }
 
-void bsc_resources_released(struct msc_connection *fw)
+void app_resources_released(struct ss7_application *app)
 {
-	bsc_del_timer(&fw->reset_timeout);
+	bsc_del_timer(&app->reset_timeout);
 }
 
-static void bsc_reset_timeout(void *_data)
+static void bsc_reset_timeout(void *_app)
 {
 	struct msgb *msg;
-	struct msc_connection *fw = _data;
+	struct ss7_application *app = _app;
+	struct mtp_link_set *set = app->route_src.set;
 
 	/* no reset */
-	if (fw->reset_count > 0) {
+	if (app->reset_count > 0) {
 		LOGP(DINP, LOGL_ERROR, "The BSC did not answer the GSM08.08 reset. Restart MTP\n");
-		mtp_link_set_stop(fw->target_link);
-		clear_connections(fw);
-		link_reset_all(fw->target_link);
-		bsc_resources_released(fw);
+		mtp_link_set_stop(app->route_src.set);
+		app_clear_connections(app);
+		link_reset_all(app->route_src.set);
+		app_resources_released(app);
 		return;
 	}
 
 	msg = create_reset();
 	if (!msg) {
-		bsc_schedule_timer(&fw->reset_timeout, 10, 0);
+		bsc_schedule_timer(&app->reset_timeout, 10, 0);
 		return;
 	}
 
-	++fw->reset_count;
-	mtp_link_set_submit_sccp_data(fw->target_link, -1, msg->l2h, msgb_l2len(msg));
+	++app->reset_count;
+	mtp_link_set_submit_sccp_data(set, -1, msg->l2h, msgb_l2len(msg));
 	msgb_free(msg);
-	bsc_schedule_timer(&fw->reset_timeout, 20, 0);
+	bsc_schedule_timer(&app->reset_timeout, 20, 0);
 }
 
 /*
@@ -249,19 +265,33 @@
  * this means we need to parse response message. In the case the
  * MTP link is going down while we are sending. We will simply
  * reconnect to the MSC.
+ *
+ * This could be called for the relay type and the cellmgr type, in case
+ * of the relay type the list of connections should be empty so we can
+ * avoid branching out.
  */
 void release_bsc_resources(struct msc_connection *fw)
 {
+	struct ss7_application *app;
+	struct mtp_link_set *set;
 	struct active_sccp_con *tmp;
 	struct active_sccp_con *con;
 
-	bsc_del_timer(&fw->reset_timeout);
+	if (!fw->app) {
+		LOGP(DINP, LOGL_ERROR, "No app assigned to the MSC connection %d/%s\n",
+		     fw->nr, fw->name);
+		return;
+	}
+
+	app = fw->app;
+	set = app->route_src.set;
+	bsc_del_timer(&app->reset_timeout);
 
 	/* 2. clear the MGCP endpoints */
 	mgcp_reset(fw);
 
 	/* 1. send BSSMAP Cleanup.. if we have any connection */
-	llist_for_each_entry_safe(con, tmp, &fw->sccp_connections, entry) {
+	llist_for_each_entry_safe(con, tmp, &app->sccp_connections, entry) {
 		if (!con->has_dst_ref) {
 			free_con(con);
 			continue;
@@ -272,18 +302,18 @@
 			continue;
 
 		/* wait for the clear commands */
-		mtp_link_set_submit_sccp_data(fw->target_link, con->sls, msg->l2h, msgb_l2len(msg));
+		mtp_link_set_submit_sccp_data(set, con->sls, msg->l2h, msgb_l2len(msg));
 		msgb_free(msg);
 	}
 
-	if (llist_empty(&fw->sccp_connections)) {
-		bsc_resources_released(fw);
+	if (llist_empty(&app->sccp_connections)) {
+		app_resources_released(app);
 	} else {
 		/* Send a reset in 20 seconds if we fail to bring everything down */
-		fw->reset_timeout.cb = bsc_reset_timeout;
-		fw->reset_timeout.data = fw;
-		fw->reset_count = 0;
-		bsc_schedule_timer(&fw->reset_timeout, 10, 0);
+		app->reset_timeout.cb = bsc_reset_timeout;
+		app->reset_timeout.data = app;
+		app->reset_count = 0;
+		bsc_schedule_timer(&app->reset_timeout, 10, 0);
 	}
 }
 
@@ -291,11 +321,14 @@
 {
 	set->available = 0;
 	mtp_link_set_stop(set);
-	clear_connections(set->fw);
-	mgcp_reset(set->fw);
 
-	/* If we have an A link send a reset to the MSC */
-	msc_send_reset(set->fw);
+	if (set->app) {
+		app_clear_connections(set->app);
+
+		/* If we have an A link send a reset to the MSC */
+		mgcp_reset(set->app->route_dst.msc);
+		msc_send_reset(set->app->route_dst.msc);
+	}
 }
 
 void mtp_linkset_up(struct mtp_link_set *set)
@@ -303,9 +336,9 @@
 	set->available = 1;
 
 	/* we have not gone through link down */
-	if (set->fw->msc_link_down) {
-		clear_connections(set->fw);
-		bsc_resources_released(set->fw);
+	if (set->app && set->app->route_dst.msc->msc_link_down) {
+		app_clear_connections(set->app);
+		app_resources_released(set->app);
 	}
 
 	mtp_link_set_reset(set);
@@ -314,7 +347,7 @@
 /**
  * update the connection state and helpers below
  */
-static void send_rlc_to_bsc(struct msc_connection *fw,
+static void send_rlc_to_bsc(struct mtp_link_set *set,
 			    unsigned int sls, struct sccp_source_reference *src,
 			    struct sccp_source_reference *dst)
 {
@@ -324,17 +357,19 @@
 	if (!msg)
 		return;
 
-	mtp_link_set_submit_sccp_data(fw->target_link, sls, msg->l2h, msgb_l2len(msg));
+	mtp_link_set_submit_sccp_data(set, sls, msg->l2h, msgb_l2len(msg));
 	msgb_free(msg);
 }
 
-static void handle_rlsd(struct msc_connection *fw, struct sccp_connection_released *rlsd, int from_msc)
+static void handle_rlsd(struct ss7_application *app, struct sccp_connection_released *rlsd, int from_msc)
 {
 	struct active_sccp_con *con;
+	struct msc_connection *msc = app->route_dst.msc;
+	struct mtp_link_set *set = app->route_src.set;
 
 	if (from_msc) {
 		/* search for a connection, reverse src/dest for MSC */
-		con = find_con_by_src_dest_ref(fw, &rlsd->destination_local_reference,
+		con = find_con_by_src_dest_ref(app, &rlsd->destination_local_reference,
 					       &rlsd->source_local_reference);
 		if (con) {
 			LOGP(DINP, LOGL_DEBUG, "RLSD conn still alive: local: 0x%x remote: 0x%x\n",
@@ -346,19 +381,19 @@
 			LOGP(DINP, LOGL_DEBUG, "Sending RLC for MSC: src: 0x%x dst: 0x%x\n",
 			     sccp_src_ref_to_int(&rlsd->destination_local_reference),
 			     sccp_src_ref_to_int(&rlsd->source_local_reference));
-			msc_send_rlc(fw, &rlsd->destination_local_reference,
+			msc_send_rlc(msc, &rlsd->destination_local_reference,
 				 &rlsd->source_local_reference);
 		}
 	} else {
 		unsigned int sls = -1;
-		con = find_con_by_src_dest_ref(fw, &rlsd->source_local_reference,
+		con = find_con_by_src_dest_ref(app, &rlsd->source_local_reference,
 					       &rlsd->destination_local_reference);
 		if (con) {
 			LOGP(DINP, LOGL_DEBUG, "Timeout on BSC. Sending RLC. src: 0x%x\n",
 			     sccp_src_ref_to_int(&rlsd->source_local_reference));
 
 			if (con->released_from_msc)
-				msc_send_rlc(fw, &con->src_ref, &con->dst_ref);
+				msc_send_rlc(msc, &con->src_ref, &con->dst_ref);
 			sls = con->sls;
 			free_con(con);
 		} else {
@@ -367,7 +402,7 @@
 		}
 
 		/* now send a rlc back to the BSC */
-		send_rlc_to_bsc(fw, sls, &rlsd->destination_local_reference, &rlsd->source_local_reference);
+		send_rlc_to_bsc(set, sls, &rlsd->destination_local_reference, &rlsd->source_local_reference);
 	}
 }
 
@@ -385,18 +420,21 @@
  *      1.) We are destroying the connection, we might send a RLC to
  *          the MSC if we are waiting for one.
  */
-void update_con_state(struct msc_connection *fw, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
+void update_con_state(struct ss7_application *app, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
 {
 	struct active_sccp_con *con;
 	struct sccp_connection_request *cr;
 	struct sccp_connection_confirm *cc;
 	struct sccp_connection_release_complete *rlc;
 	struct sccp_connection_refused *cref;
+	struct msc_connection *msc;
 
 	/* was the header okay? */
 	if (rc < 0)
 		return;
 
+	msc = app->route_dst.msc;
+
 	/* the header was size checked */
 	switch (msg->l2h[0]) {
 	case SCCP_MSG_TYPE_CR:
@@ -406,7 +444,7 @@
 		}
 
 		cr = (struct sccp_connection_request *) msg->l2h;
-		con = find_con_by_src_ref(fw, &cr->source_local_reference);
+		con = find_con_by_src_ref(app, &cr->source_local_reference);
 		if (con) {
 			LOGP(DINP, LOGL_ERROR, "Duplicate SRC reference for: 0x%x. Reusing\n",
 				sccp_src_ref_to_int(&con->src_ref));
@@ -421,8 +459,8 @@
 
 		con->src_ref = cr->source_local_reference;
 		con->sls = sls;
-		con->link = fw->target_link;
-		llist_add_tail(&con->entry, &fw->sccp_connections);
+		con->app = app;
+		llist_add_tail(&con->entry, &app->sccp_connections);
 		LOGP(DINP, LOGL_DEBUG, "Adding CR: local ref: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
 		break;
 	case SCCP_MSG_TYPE_CC:
@@ -432,7 +470,7 @@
 		}
 
 		cc = (struct sccp_connection_confirm *) msg->l2h;
-		con = find_con_by_src_ref(fw, &cc->destination_local_reference);
+		con = find_con_by_src_ref(app, &cc->destination_local_reference);
 		if (con) {
 			con->dst_ref = cc->source_local_reference;
 			con->has_dst_ref = 1;
@@ -451,7 +489,7 @@
 		}
 
 		cref = (struct sccp_connection_refused *) msg->l2h;
-		con = find_con_by_src_ref(fw, &cref->destination_local_reference);
+		con = find_con_by_src_ref(app, &cref->destination_local_reference);
 		if (con) {
 			LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
 			free_con(con);
@@ -461,7 +499,7 @@
 		LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n");
 		break;
 	case SCCP_MSG_TYPE_RLSD:
-		handle_rlsd(fw, (struct sccp_connection_released *) msg->l2h, from_msc);
+		handle_rlsd(app, (struct sccp_connection_released *) msg->l2h, from_msc);
 		break;
 	case SCCP_MSG_TYPE_RLC:
 		if (from_msc) {
@@ -470,12 +508,12 @@
 		}
 
 		rlc = (struct sccp_connection_release_complete *) msg->l2h;
-		con = find_con_by_src_dest_ref(fw, &rlc->source_local_reference,
+		con = find_con_by_src_dest_ref(app, &rlc->source_local_reference,
 					       &rlc->destination_local_reference);
 		if (con) {
 			LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
 			if (con->released_from_msc)
-				msc_send_rlc(fw, &con->src_ref, &con->dst_ref);
+				msc_send_rlc(msc, &con->src_ref, &con->dst_ref);
 			free_con(con);
 			return;
 		}
@@ -515,7 +553,7 @@
 
 	LOGP(DINP, LOGL_DEBUG, "Received GSM Clear Complete. Sending RLSD locally.\n");
 
-	con = find_con_by_dest_ref(link->fw, res->destination_local_reference);
+	con = find_con_by_dest_ref(link->app, res->destination_local_reference);
 	if (!con)
 		return;
 	con->rls_tries = 0;
@@ -535,11 +573,22 @@
 
 void msc_dispatch_sccp(struct msc_connection *msc, struct msgb *msg)
 {
+	struct mtp_link_set *set;
+
+	if (!msc->app) {
+		LOGP(DINP, LOGL_ERROR, "The MSC Connection %d/%s has no app assigned.\n",
+		     msc->nr, msc->name);
+		return;
+	}
+
+
+	set = msc->app->route_src.set;
+
 	/* we can not forward it right now */
-	if (msc->forward_only) {
-		if (!msc->target_link->sccp_up)
+	if (msc->app->forward_only) {
+		if (!set->sccp_up)
 			return;
-		mtp_link_set_submit_sccp_data(msc->target_link, -1,
+		mtp_link_set_submit_sccp_data(set, -1,
 					      msg->l2h, msgb_l2len(msg));
 	} else {
 		struct sccp_parse_result result;
@@ -551,27 +600,26 @@
 			LOGP(DMSC, LOGL_NOTICE, "Filtering reset ack from the MSC\n");
 		} else if (rc == BSS_FILTER_RLSD) {
 			LOGP(DMSC, LOGL_DEBUG, "Filtering RLSD from the MSC\n");
-			update_con_state(msc, rc, &result, msg, 1, 0);
+			update_con_state(msc->app, rc, &result, msg, 1, 0);
 		} else if (rc == BSS_FILTER_RLC) {
 			/* if we receive this we have forwarded a RLSD to the network */
 			LOGP(DMSC, LOGL_ERROR, "RLC from the network. BAD!\n");
 		} else if (rc == BSS_FILTER_CLEAR_COMPL) {
 			LOGP(DMSC, LOGL_ERROR, "Clear Complete from the network.\n");
-		} else if (msc->target_link->sccp_up) {
+		} else if (set->sccp_up) {
 			unsigned int sls;
 
-			update_con_state(msc, rc, &result, msg, 1, 0);
-			sls = sls_for_src_ref(msc, result.destination_local_reference);
+			update_con_state(msc->app, rc, &result, msg, 1, 0);
+			sls = sls_for_src_ref(msc->app, result.destination_local_reference);
 
 			/* Check for Location Update Accept */
 			bsc_ussd_handle_in_msg(msc, &result, msg);
 
 			/* patch a possible PC */
-			bss_rewrite_header_to_bsc(msg, msc->target_link->opc,
-						  msc->target_link->dpc);
+			bss_rewrite_header_to_bsc(msg, set->opc, set->dpc);
 
 			/* we can not forward it right now */
-			mtp_link_set_submit_sccp_data(msc->target_link, sls,
+			mtp_link_set_submit_sccp_data(set, sls,
 						      msg->l2h, msgb_l2len(msg));
 		}
 	}
diff --git a/src/ss7_application.c b/src/ss7_application.c
new file mode 100644
index 0000000..49ae4c1
--- /dev/null
+++ b/src/ss7_application.c
@@ -0,0 +1,227 @@
+/*
+ * The SS7 Application part for forwarding or nat...
+ *
+ * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2010-2011 by On-Waves
+ * 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 <ss7_application.h>
+#include <bsc_data.h>
+#include <cellmgr_debug.h>
+#include <msc_connection.h>
+#include <sctp_m2ua.h>
+
+#include <osmocore/talloc.h>
+
+struct ss7_application *ss7_application_alloc(struct bsc_data *bsc)
+{
+	struct ss7_application *app;
+
+	app = talloc_zero(bsc, struct ss7_application);
+	if (!app) {
+		LOGP(DINP, LOGL_ERROR, "Failed to create SS7 Application.\n");
+		return NULL;
+	}
+
+	INIT_LLIST_HEAD(&app->sccp_connections);
+	llist_add(&app->entry, &bsc->apps);
+	app->nr = bsc->num_apps++;
+	app->bsc = bsc;
+
+	return app;
+}
+
+struct ss7_application *ss7_application_num(struct bsc_data *bsc, int num)
+{
+	struct ss7_application *ss7;
+
+	llist_for_each_entry(ss7, &bsc->apps, entry)
+		if (ss7->nr == num)
+			return ss7;
+
+	return NULL;
+}
+
+static int ss7_app_setup_stp(struct ss7_application *app,
+			     int src_type, int src_num,
+			     int dst_type, int dst_num)
+{
+	struct mtp_link_set *src, *dst;
+
+	if (src_type != SS7_SET_LINKSET) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s source needs to be a linkset.\n",
+		     app->nr, app->name);
+		return -1;
+	}
+
+	if (dst_type != SS7_SET_LINKSET) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s destination needs to be a linkset.\n",
+		     app->nr, app->name);
+		return -1;
+	}
+
+	/* veryify the MTP Linkset */
+	src = mtp_link_set_num(app->bsc, src_num);
+	if (!src) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s source linkset not found with nr: %d.\n",
+		     app->nr, app->name, src_num);
+		return -2;
+	}
+
+	if (src->app) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s is using linkset %d/%s\n",
+		      src->app->nr, src->app->name,
+		      src->no, src->name);
+		return -3;
+	}
+
+	/* veryify the MTP Linkset */
+	dst = mtp_link_set_num(app->bsc, dst_num);
+	if (!dst) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s destionation linkset not found with nr: %d.\n",
+		     app->nr, app->name, dst_num);
+		return -2;
+	}
+
+	if (dst->app) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s is using linkset %d/%s\n",
+		      dst->app->nr, dst->app->name,
+		      dst->no, dst->name);
+		return -3;
+	}
+
+	/* now connect it */
+	src->app = app;
+	app->route_src.type = src_type;
+	app->route_src.nr = src_num;
+	app->route_src.set = src;
+	app->route_src.msc = NULL;
+
+	dst->app = app;
+	app->route_dst.type = dst_type;
+	app->route_dst.nr = dst_num;
+	app->route_dst.set = dst;
+	app->route_dst.msc = NULL;
+
+	app->type = APP_STP;
+	app->bsc->m2ua_trans->started = 1;
+
+	/* TODO: start the applications here */
+	return 0;
+}
+
+static int ss7_app_setup_relay(struct ss7_application *app, int type,
+			       int src_type, int src_num, int dst_type, int dst_num)
+{
+	struct mtp_link_set *mtp;
+	struct msc_connection *msc;
+
+	/* verify the types */
+	if (src_type != SS7_SET_LINKSET) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s source needs to be a linkset.\n",
+		     app->nr, app->name);
+		return -1;
+	}
+
+	if (dst_type != SS7_SET_MSC) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s dest needs to be a MSC.\n",
+		     app->nr, app->name);
+		return -1;
+	}
+
+	/* veryify the MTP Linkset */
+	mtp = mtp_link_set_num(app->bsc, src_num);
+	if (!mtp) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s source linkset not found with nr: %d.\n",
+		     app->nr, app->name, src_num);
+		return -2;
+	}
+
+	if (mtp->app) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s is using linkset %d/%s\n",
+		      mtp->app->nr, mtp->app->name,
+		      mtp->no, mtp->name);
+		return -3;
+	}
+
+	/* verify the MSC connection */
+	msc = msc_connection_num(app->bsc, dst_num);
+	if (!msc) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s dest MSC not found with nr: %d.\n",
+		     app->nr, app->name, dst_num);
+		return -4;
+	}
+
+	if (msc->app) {
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 %d/%s is using MSC connection %d/%s\n",
+		      msc->app->nr, msc->app->name,
+		      msc->nr, msc->name);
+		return -5;
+	}
+
+
+	/* now connect it and run the app */
+	mtp->app = app;
+	app->route_src.type = src_type;
+	app->route_src.nr = src_num;
+	app->route_src.set = mtp;
+	app->route_src.msc = NULL;
+
+	msc->app = app;
+	app->route_dst.type = dst_type;
+	app->route_dst.nr = dst_num;
+	app->route_dst.set = NULL;
+	app->route_dst.msc = msc;
+
+	app->type = type;
+
+	/* TODO: start the applications here */
+	return 0;
+}
+
+int ss7_application_setup(struct ss7_application *ss7, int type,
+			  int src_type, int src_num,
+			  int dst_type, int dst_num)
+{
+	switch (type) {
+	case APP_CELLMGR:
+	case APP_RELAY:
+		return ss7_app_setup_relay(ss7, type, src_type, src_num,
+					   dst_type, dst_num);
+		break;
+	case APP_STP:
+		return ss7_app_setup_stp(ss7, src_type, src_num,
+					 dst_type, dst_num);
+	default:
+		LOGP(DINP, LOGL_ERROR,
+		     "SS7 Application %d is not supported.\n", type);
+		return -1;
+	}
+}
diff --git a/src/vty_interface.c b/src/vty_interface.c
index e7193f4..7328b10 100644
--- a/src/vty_interface.c
+++ b/src/vty_interface.c
@@ -593,9 +593,7 @@
 	install_element_ve(&show_linksets_cmd);
 	install_element_ve(&show_slc_cmd);
 
-	if (bsc->app != APP_STP) {
-		install_element_ve(&show_msc_cmd);
-	}
+	install_element_ve(&show_msc_cmd);
 }
 
 const char *openbsc_copyright = "";