blob: 04b9ece7d3ab16d50959da87cab02b2346d183f5 [file] [log] [blame]
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001/* Code to manage MSC subscriber connections over IuCS interface */
2
3/*
4 * (C) 2016,2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
5 *
6 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <inttypes.h>
26
27#include <osmocom/core/logging.h>
Neels Hofmeyr28295b92017-07-05 15:19:52 +020028#include <osmocom/ranap/iu_client.h>
Neels Hofmeyra1756f32016-05-20 21:59:55 +020029#include <openbsc/debug.h>
30
31#include <openbsc/gsm_data.h>
Neels Hofmeyra1756f32016-05-20 21:59:55 +020032#include <openbsc/gsm_subscriber.h>
33#include <openbsc/osmo_msc.h>
34#include <openbsc/vlr.h>
35
36/* For A-interface see libbsc/bsc_api.c subscr_con_allocate() */
37static struct gsm_subscriber_connection *subscr_conn_allocate_iu(struct gsm_network *network,
Neels Hofmeyr28295b92017-07-05 15:19:52 +020038 struct ranap_ue_conn_ctx *ue,
Neels Hofmeyra1756f32016-05-20 21:59:55 +020039 uint16_t lac)
40{
41 struct gsm_subscriber_connection *conn;
42
Philipp Maier4b60d072017-04-09 12:32:51 +020043 DEBUGP(DIUCS, "Allocating IuCS subscriber conn: lac %d, conn_id %" PRIx32 "\n",
44 lac, ue->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +020045
46 conn = talloc_zero(network, struct gsm_subscriber_connection);
47 if (!conn)
48 return NULL;
49
50 conn->network = network;
51 conn->via_ran = RAN_UTRAN_IU;
52 conn->iu.ue_ctx = ue;
53 conn->iu.ue_ctx->rab_assign_addr_enc = network->iu.rab_assign_addr_enc;
54 conn->lac = lac;
55
56 llist_add_tail(&conn->entry, &network->subscr_conns);
57 return conn;
58}
59
Neels Hofmeyr28295b92017-07-05 15:19:52 +020060static int same_ue_conn(struct ranap_ue_conn_ctx *a, struct ranap_ue_conn_ctx *b)
Neels Hofmeyra1756f32016-05-20 21:59:55 +020061{
62 if (a == b)
63 return 1;
Philipp Maier4b60d072017-04-09 12:32:51 +020064 return (a->conn_id == b->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +020065}
66
67static inline void log_subscribers(struct gsm_network *network)
68{
69 if (!log_check_level(DIUCS, LOGL_DEBUG))
70 return;
71
72 struct gsm_subscriber_connection *conn;
73 int i = 0;
74 llist_for_each_entry(conn, &network->subscr_conns, entry) {
75 DEBUGP(DIUCS, "%3d: %s", i, vlr_subscr_name(conn->vsub));
76 switch (conn->via_ran) {
77 case RAN_UTRAN_IU:
78 DEBUGPC(DIUCS, " Iu");
79 if (conn->iu.ue_ctx) {
Philipp Maier4b60d072017-04-09 12:32:51 +020080 DEBUGPC(DIUCS, " conn_id %d",
Neels Hofmeyra1756f32016-05-20 21:59:55 +020081 conn->iu.ue_ctx->conn_id
82 );
83 }
84 break;
85 case RAN_GERAN_A:
86 DEBUGPC(DIUCS, " A");
87 /* TODO log A-interface connection details */
88 break;
89 case RAN_UNKNOWN:
90 DEBUGPC(DIUCS, " ?");
91 break;
92 default:
93 DEBUGPC(DIUCS, " invalid");
94 break;
95 }
96 DEBUGPC(DIUCS, "\n");
97 i++;
98 }
99 DEBUGP(DIUCS, "subscribers registered: %d\n", i);
100}
101
Philipp Maier4b60d072017-04-09 12:32:51 +0200102/* Return an existing IuCS subscriber connection record for the given
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200103 * connection IDs, or return NULL if not found. */
104struct gsm_subscriber_connection *subscr_conn_lookup_iu(
105 struct gsm_network *network,
Neels Hofmeyr28295b92017-07-05 15:19:52 +0200106 struct ranap_ue_conn_ctx *ue)
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200107{
108 struct gsm_subscriber_connection *conn;
109
Philipp Maier4b60d072017-04-09 12:32:51 +0200110 DEBUGP(DIUCS, "Looking for IuCS subscriber: conn_id %" PRIx32 "\n",
111 ue->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200112 log_subscribers(network);
113
114 llist_for_each_entry(conn, &network->subscr_conns, entry) {
115 if (conn->via_ran != RAN_UTRAN_IU)
116 continue;
117 if (!same_ue_conn(conn->iu.ue_ctx, ue))
118 continue;
Philipp Maier4b60d072017-04-09 12:32:51 +0200119 DEBUGP(DIUCS, "Found IuCS subscriber for conn_id %" PRIx32 "\n",
120 ue->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200121 return conn;
122 }
Philipp Maier4b60d072017-04-09 12:32:51 +0200123 DEBUGP(DIUCS, "No IuCS subscriber found for conn_id %" PRIx32 "\n",
124 ue->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200125 return NULL;
126}
127
128/* Receive MM/CC/... message from IuCS (SCCP user SAP).
Neels Hofmeyr28295b92017-07-05 15:19:52 +0200129 * msg->dst must reference a struct ranap_ue_conn_ctx, which identifies the peer that
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200130 * sent the msg.
131 *
132 * For A-interface see libbsc/bsc_api.c gsm0408_rcvmsg(). */
133int gsm0408_rcvmsg_iucs(struct gsm_network *network, struct msgb *msg,
134 uint16_t *lac)
135{
136 int rc;
Neels Hofmeyr28295b92017-07-05 15:19:52 +0200137 struct ranap_ue_conn_ctx *ue_ctx;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200138 struct gsm_subscriber_connection *conn;
139
Neels Hofmeyr28295b92017-07-05 15:19:52 +0200140 ue_ctx = (struct ranap_ue_conn_ctx*)msg->dst;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200141
142 /* TODO: are there message types that could allow us to skip this
143 * search? */
144 conn = subscr_conn_lookup_iu(network, ue_ctx);
145
146 if (conn && lac && (conn->lac != *lac)) {
147 LOGP(DIUCS, LOGL_ERROR, "IuCS subscriber has changed LAC"
148 " within the same connection, discarding connection:"
149 " %s from LAC %d to %d\n",
150 vlr_subscr_name(conn->vsub), conn->lac, *lac);
151 /* Deallocate conn with previous LAC */
152 msc_subscr_conn_close(conn, GSM_CAUSE_INV_MAND_INFO);
153 /* At this point we could be tolerant and allocate a new
154 * connection, but changing the LAC within the same connection
155 * is shifty. Rather cancel everything. */
156 return -1;
157 }
158
159 if (conn) {
160 /* Make sure we don't receive RR over IuCS; otherwise all
161 * messages handled by gsm0408_dispatch() are of interest (CC,
162 * MM, SMS, NS_SS, maybe even MM_GPRS and SM_GPRS). */
163 struct gsm48_hdr *gh = msgb_l3(msg);
164 uint8_t pdisc = gh->proto_discr & 0x0f;
165 OSMO_ASSERT(pdisc != GSM48_PDISC_RR);
166
167 msc_dtap(conn, ue_ctx->conn_id, msg);
168 rc = 0;
169 } else {
170 /* allocate a new connection */
171
172 if (!lac) {
173 LOGP(DIUCS, LOGL_ERROR, "New IuCS subscriber"
174 " but no LAC available. Expecting an InitialUE"
175 " message containing a LAI IE."
176 " Dropping connection.\n");
177 return -1;
178 }
179
180 conn = subscr_conn_allocate_iu(network, ue_ctx, *lac);
181 if (!conn)
182 abort();
183
184 /* ownership of conn hereby goes to the MSC: */
185 rc = msc_compl_l3(conn, msg, 0);
186 }
187
188 return rc;
189}