blob: c133656dd4036b172f03347cb39675c9ca730156 [file] [log] [blame]
Harald Welte6eafe912009-10-16 08:32:58 +02001/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2009 by Mike Haben <michael.haben@btinternet.com>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +07004 * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com>
Harald Welte6eafe912009-10-16 08:32:58 +02005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte6eafe912009-10-16 08:32:58 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte6eafe912009-10-16 08:32:58 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte6eafe912009-10-16 08:32:58 +020020 *
21 */
22
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070023/**
24 * MSC-specific handling of call independent Supplementary
25 * Services messages (NC_SS) according to GSM TS 09.11
26 * "Signalling interworking for supplementary services".
27 */
Harald Welte6eafe912009-10-16 08:32:58 +020028
29#include <stdio.h>
Harald Welte6eafe912009-10-16 08:32:58 +020030#include <errno.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070031#include <stdbool.h>
32
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +070033#include <osmocom/core/linuxlist.h>
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +070034#include <osmocom/core/rate_ctr.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070035#include <osmocom/core/utils.h>
36#include <osmocom/core/msgb.h>
Vadim Yanitskiy0622ef52018-08-03 04:39:04 +070037
38#include <osmocom/gsm/protocol/gsm_04_80.h>
39#include <osmocom/gsm/gsm0480.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070040#include <osmocom/gsm/tlv.h>
Harald Welte6eafe912009-10-16 08:32:58 +020041
Neels Hofmeyr90843962017-09-04 15:04:35 +020042#include <osmocom/msc/gsm_04_80.h>
43#include <osmocom/msc/gsm_subscriber.h>
44#include <osmocom/msc/debug.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/vlr.h>
Max43b01b02017-09-15 11:22:30 +020046#include <osmocom/msc/gsm_04_08.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070047#include <osmocom/msc/transaction.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020048#include <osmocom/gsupclient/gsup_client.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070049#include <osmocom/msc/msc_ifaces.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070050
51/* FIXME: choose a proper range */
52static uint32_t new_callref = 0x20000001;
Harald Welte6eafe912009-10-16 08:32:58 +020053
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070054static void ncss_session_timeout_handler(void *_trans)
55{
56 struct gsm_trans *trans = (struct gsm_trans *) _trans;
57 struct osmo_gsup_message gsup_msg = { 0 };
58
59 /* The timeout might be disabled from the VTY */
60 if (trans->net->ncss_guard_timeout == 0)
61 return;
62
63 LOGP(DMM, LOGL_NOTICE, "SS/USSD session timeout, releasing "
64 "transaction (trans=%p, callref=%x)\n", trans, trans->callref);
65
66 /* Indicate connection release to subscriber (if active) */
67 if (trans->conn != NULL) {
68 /* This pair of cause location and value is used by commercial networks */
69 msc_send_ussd_release_complete_cause(trans->conn, trans->transaction_id,
70 GSM48_CAUSE_LOC_PUN_S_LU, GSM48_CC_CAUSE_NORMAL_UNSPEC);
71 }
72
73 /* Terminate GSUP session with EUSE */
74 gsup_msg.message_type = OSMO_GSUP_MSGT_PROC_SS_ERROR;
75 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, trans->vsub->imsi);
76
77 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END;
78 gsup_msg.session_id = trans->callref;
79 gsup_msg.cause = GMM_CAUSE_NET_FAIL;
80
81 osmo_gsup_client_enc_send(trans->net->vlr->gsup_client, &gsup_msg);
82
83 /* Finally, release this transaction */
84 trans_free(trans);
85}
86
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070087/* Entry point for call independent MO SS messages */
Neels Hofmeyrc036b792018-11-29 22:37:51 +010088int gsm0911_rcv_nc_ss(struct ran_conn *conn, struct msgb *msg)
Harald Welte6eafe912009-10-16 08:32:58 +020089{
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070090 struct gsm48_hdr *gh = msgb_l3(msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070091 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070092 struct gsm_trans *trans;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070093 struct msgb *gsup_msgb;
94 uint16_t facility_ie_len;
95 uint8_t *facility_ie;
Max4a5cfa52019-01-10 17:41:05 +010096 uint8_t tid;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070097 uint8_t msg_type;
98 int rc;
Harald Welte6eafe912009-10-16 08:32:58 +020099
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700100 msg_type = gsm48_hdr_msg_type(gh);
101 tid = gsm48_hdr_trans_id_flip_ti(gh);
Harald Welte2483f1b2016-06-19 18:06:02 +0200102
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700103 /* Associate logging messages with this subscriber */
104 log_set_context(LOG_CTX_VLR_SUBSCR, conn->vsub);
105
106 DEBUGP(DMM, "Received SS/USSD data (trans_id=%x, msg_type=%s)\n",
Max4a5cfa52019-01-10 17:41:05 +0100107 tid, gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type));
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700108
109 /* Reuse existing transaction, or create a new one */
Max4a5cfa52019-01-10 17:41:05 +0100110 trans = trans_find_by_id(conn, GSM48_PDISC_NC_SS, tid);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700111 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700112 /* Count MS-initiated attempts to establish a NC SS/USSD session */
113 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_REQUESTS]);
114
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700115 /**
116 * According to GSM TS 04.80, section 2.4.2 "Register
117 * (mobile station to network direction)", the REGISTER
118 * message is sent by the mobile station to the network
119 * to assign a new transaction identifier for call independent
120 * supplementary service control and to request or acknowledge
121 * a supplementary service.
122 */
123 if (msg_type != GSM0480_MTYPE_REGISTER) {
124 LOGP(DMM, LOGL_ERROR, "Unexpected message (msg_type=%s), "
125 "transaction is not allocated yet\n",
Max4a5cfa52019-01-10 17:41:05 +0100126 gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type));
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700127 gsm48_tx_simple(conn,
128 GSM48_PDISC_NC_SS | (tid << 4),
129 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700130 return -EINVAL;
131 }
132
133 DEBUGP(DMM, " -> (new transaction)\n");
134 trans = trans_alloc(conn->network, conn->vsub,
Max4a5cfa52019-01-10 17:41:05 +0100135 GSM48_PDISC_NC_SS, tid, new_callref++);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700136 if (!trans) {
Max3614fd62019-01-17 13:38:10 +0100137 LOGP(DMM, LOGL_ERROR, " -> No memory for trans\n");
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700138 gsm48_tx_simple(conn,
139 GSM48_PDISC_NC_SS | (tid << 4),
140 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700141 return -ENOMEM;
142 }
143
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700144 /* Init inactivity timer */
145 osmo_timer_setup(&trans->ss.timer_guard,
146 ncss_session_timeout_handler, trans);
147
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700148 /* Count active NC SS/USSD sessions */
149 osmo_counter_inc(conn->network->active_nc_ss);
150
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100151 trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700152 trans->dlci = OMSC_LINKID_CB(msg);
153 cm_service_request_concludes(conn, msg);
154 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200155
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700156 /* (Re)schedule the inactivity timer */
157 if (conn->network->ncss_guard_timeout > 0) {
158 osmo_timer_schedule(&trans->ss.timer_guard,
159 conn->network->ncss_guard_timeout, 0);
160 }
161
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700162 /* Attempt to extract Facility IE */
163 rc = gsm0480_extract_ie_by_tag(gh, msgb_l3len(msg),
164 &facility_ie, &facility_ie_len, GSM0480_IE_FACILITY);
165 if (rc) {
166 LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, "
167 "couldn't extract Facility IE\n");
168 goto error;
Tobias Engelea730322013-12-28 17:03:14 +0100169 }
170
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700171 /* Facility IE is optional for RELEASE COMPLETE */
172 if (msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
173 if (!facility_ie || facility_ie_len < 2) {
174 LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, "
175 "missing mandatory Facility IE\n");
176 rc = -EINVAL;
177 goto error;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200178 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700179 }
180
181 /* Compose a mew GSUP message */
182 memset(&gsup_msg, 0x00, sizeof(gsup_msg));
183 gsup_msg.message_type = OSMO_GSUP_MSGT_PROC_SS_REQUEST;
184 gsup_msg.session_id = trans->callref;
185
186 /**
187 * Perform A-interface to GSUP-interface mapping,
188 * according to GSM TS 09.11, table 4.2.
189 */
190 switch (msg_type) {
191 case GSM0480_MTYPE_REGISTER:
192 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_BEGIN;
193 break;
194 case GSM0480_MTYPE_FACILITY:
195 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
196 break;
197 case GSM0480_MTYPE_RELEASE_COMPLETE:
198 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END;
199 break;
200 }
201
202 /* Fill in the (optional) message payload */
203 if (facility_ie) {
204 gsup_msg.ss_info_len = facility_ie_len;
205 gsup_msg.ss_info = facility_ie;
206 }
207
208 /* Fill in subscriber's IMSI */
209 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, conn->vsub->imsi);
210
211 /* Allocate GSUP message buffer */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200212 gsup_msgb = osmo_gsup_client_msgb_alloc();
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700213 if (!gsup_msgb) {
214 LOGP(DMM, LOGL_ERROR, "Couldn't allocate GSUP message\n");
215 rc = -ENOMEM;
216 goto error;
217 }
218
219 /* Encode GSUP message */
220 rc = osmo_gsup_encode(gsup_msgb, &gsup_msg);
221 if (rc) {
222 LOGP(DMM, LOGL_ERROR, "Couldn't encode GSUP message\n");
223 goto error;
224 }
225
226 /* Finally send */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200227 rc = osmo_gsup_client_send(conn->network->vlr->gsup_client, gsup_msgb);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700228 if (rc) {
229 LOGP(DMM, LOGL_ERROR, "Couldn't send GSUP message\n");
230 goto error;
231 }
232
Vadim Yanitskiyfcc24ed2018-06-21 17:55:56 +0700233 /* Should we release connection? Or wait for response? */
234 if (msg_type == GSM0480_MTYPE_RELEASE_COMPLETE)
235 trans_free(trans);
236 else
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100237 ran_conn_communicating(conn);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700238
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700239 /* Count established MS-initiated NC SS/USSD sessions */
240 if (msg_type == GSM0480_MTYPE_REGISTER)
241 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_ESTABLISHED]);
242
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700243 return 0;
244
245error:
246 /* Abort transaction on DTAP-interface */
Vadim Yanitskiy0622ef52018-08-03 04:39:04 +0700247 msc_send_ussd_reject(conn, tid, -1,
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700248 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
249 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
250 if (trans)
251 trans_free(trans);
252
253 /* TODO: abort transaction on GSUP interface if any */
254 return rc;
255}
256
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700257/* Call-back from paging the B-end of the connection */
258static int handle_paging_event(unsigned int hooknum, unsigned int event,
259 struct msgb *msg, void *_conn, void *_transt)
260{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100261 struct ran_conn *conn = _conn;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700262 enum gsm_paging_event paging_event = event;
263 struct gsm_trans *transt = _transt;
264 struct gsm48_hdr *gh;
265 struct msgb *ss_msg;
266
267 OSMO_ASSERT(!transt->conn);
268 OSMO_ASSERT(transt->ss.msg);
269
270 switch (paging_event) {
271 case GSM_PAGING_SUCCEEDED:
272 DEBUGP(DMM, "Paging subscr %s succeeded!\n",
273 vlr_subscr_msisdn_or_name(transt->vsub));
274
275 /* Assign connection */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100276 transt->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700277 transt->paging_request = NULL;
278
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700279 /* (Re)schedule the inactivity timer */
280 if (conn->network->ncss_guard_timeout > 0) {
281 osmo_timer_schedule(&transt->ss.timer_guard,
282 conn->network->ncss_guard_timeout, 0);
283 }
284
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700285 /* Send stored message */
286 ss_msg = transt->ss.msg;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700287 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
288 gh->proto_discr = GSM48_PDISC_NC_SS;
289 gh->proto_discr |= transt->transaction_id << 4;
290 gh->msg_type = GSM0480_MTYPE_REGISTER;
291
292 /* Sent to the MS, give ownership of ss_msg */
293 msc_tx_dtap(transt->conn, ss_msg);
294 transt->ss.msg = NULL;
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700295
296 /* Count established network-initiated NC SS/USSD sessions */
297 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_ESTABLISHED]);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700298 break;
299 case GSM_PAGING_EXPIRED:
300 case GSM_PAGING_BUSY:
301 DEBUGP(DMM, "Paging subscr %s %s!\n",
302 vlr_subscr_msisdn_or_name(transt->vsub),
303 paging_event == GSM_PAGING_EXPIRED ? "expired" : "busy");
304
305 /* TODO: inform HLR about this failure */
306
307 msgb_free(transt->ss.msg);
308 transt->ss.msg = NULL;
309
310 transt->callref = 0;
311 transt->paging_request = NULL;
312 trans_free(transt);
313 break;
314 }
315
316 return 0;
317}
318
319static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net,
320 struct vlr_subscr *vsub, struct osmo_gsup_message *gsup_msg)
321{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100322 struct ran_conn *conn;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700323 struct gsm_trans *trans, *transt;
324 int tid;
325
326 if (gsup_msg->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) {
327 LOGP(DMM, LOGL_ERROR, "Received non-BEGIN message "
328 "for non-existing transaction\n");
329 return NULL;
330 }
331
332 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
333 LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE\n");
334 return NULL;
335 }
336
337 /* If subscriber is not "attached" */
Max7d41d872018-12-19 11:48:33 +0100338 if (!vsub->cgi.lai.lac) {
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700339 LOGP(DMM, LOGL_ERROR, "Network-originated session "
340 "rejected - subscriber is not attached\n");
341 return NULL;
342 }
343
344 DEBUGP(DMM, "Establishing network-originated session\n");
345
346 /* Allocate a new transaction */
347 trans = trans_alloc(net, vsub, GSM48_PDISC_NC_SS,
Maxd8daaae2019-02-14 16:54:10 +0700348 TRANS_ID_UNASSIGNED, gsup_msg->session_id);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700349 if (!trans) {
Max3614fd62019-01-17 13:38:10 +0100350 LOGP(DMM, LOGL_ERROR, " -> No memory for trans\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700351 return NULL;
352 }
353
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700354 /* Count active NC SS/USSD sessions */
355 osmo_counter_inc(net->active_nc_ss);
356
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700357 /* Assign transaction ID */
Max30fb97a2019-01-10 17:25:33 +0100358 tid = trans_assign_trans_id(trans->net, trans->vsub, GSM48_PDISC_NC_SS);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700359 if (tid < 0) {
360 LOGP(DMM, LOGL_ERROR, "No free transaction ID\n");
361 /* TODO: inform HLR about this */
362 /* TODO: release connection with subscriber */
363 trans->callref = 0;
364 trans_free(trans);
365 return NULL;
366 }
367 trans->transaction_id = tid;
368
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700369 /* Init inactivity timer */
370 osmo_timer_setup(&trans->ss.timer_guard,
371 ncss_session_timeout_handler, trans);
372
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700373 /* Attempt to find connection */
374 conn = connection_for_subscr(vsub);
375 if (conn) {
376 /* Assign connection */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100377 trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700378 trans->dlci = 0x00; /* SAPI=0, not SACCH */
379 return trans;
380 }
381
382 DEBUGP(DMM, "Triggering Paging Request\n");
383
384 /* Find transaction with this subscriber already paging */
385 llist_for_each_entry(transt, &net->trans_list, entry) {
386 /* Transaction of our conn? */
387 if (transt == trans || transt->vsub != vsub)
388 continue;
389
390 LOGP(DMM, LOGL_ERROR, "Paging already started, "
391 "rejecting message...\n");
392 trans_free(trans);
393 return NULL;
394 }
395
396 /* Trigger Paging Request */
397 trans->paging_request = subscr_request_conn(vsub,
Harald Welte0df904d2018-12-03 11:00:04 +0100398 &handle_paging_event, trans, "GSM 09.11 SS/USSD",
399 SGSAP_SERV_IND_CS_CALL);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700400 if (!trans->paging_request) {
401 LOGP(DMM, LOGL_ERROR, "Failed to allocate paging token\n");
402 trans_free(trans);
403 return NULL;
404 }
405
406 /* Store the Facility IE to be sent */
407 OSMO_ASSERT(trans->ss.msg == NULL);
408 trans->ss.msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
409 msgb_tlv_put(trans->ss.msg, GSM0480_IE_FACILITY,
410 gsup_msg->ss_info_len, gsup_msg->ss_info);
411
412 return NULL;
413}
414
415/* NC SS specific transaction release.
416 * Gets called by trans_free, DO NOT CALL YOURSELF! */
417void _gsm911_nc_ss_trans_free(struct gsm_trans *trans)
418{
419 /**
420 * TODO: if transaction wasn't properly terminated,
421 * we need to do it here by releasing the subscriber
422 * connection and sending notification via GSUP...
423 */
424 if (trans->ss.msg != NULL)
425 msgb_free(trans->ss.msg);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700426
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700427 /* Stop inactivity timer */
428 osmo_timer_del(&trans->ss.timer_guard);
429
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700430 /* One session less */
431 osmo_counter_dec(trans->net->active_nc_ss);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700432}
433
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700434int gsm0911_gsup_handler(struct vlr_subscr *vsub,
435 struct osmo_gsup_message *gsup_msg)
436{
437 struct vlr_instance *vlr;
438 struct gsm_network *net;
439 struct gsm_trans *trans;
440 struct gsm48_hdr *gh;
441 struct msgb *ss_msg;
442 bool trans_end;
443
444 /* Associate logging messages with this subscriber */
445 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
446
447 /* Obtain pointer to vlr_instance */
448 vlr = vsub->vlr;
449 OSMO_ASSERT(vlr);
450
451 /* Obtain pointer to gsm_network */
452 net = (struct gsm_network *) vlr->user_ctx;
453 OSMO_ASSERT(net);
454
455 /* Handle errors */
456 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
457 /* FIXME: handle this error somehow! */
Harald Welte6307b852009-10-16 08:41:51 +0200458 return 0;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200459 }
Harald Welte6eafe912009-10-16 08:32:58 +0200460
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700461 /* Attempt to find DTAP-transaction */
462 trans = trans_find_by_callref(net, gsup_msg->session_id);
463 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700464 /* Count network-initiated attempts to establish a NC SS/USSD session */
465 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_REQUESTS]);
466
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700467 /* Attempt to establish a new transaction */
468 trans = establish_nc_ss_trans(net, vsub, gsup_msg);
469 if (!trans) {
470 /* FIXME: send ERROR back to the HLR */
471 return -EINVAL;
472 }
473
474 /* Wait for Paging Response */
475 if (trans->paging_request)
476 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200477 }
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800478
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700479 /* (Re)schedule the inactivity timer */
480 if (net->ncss_guard_timeout > 0) {
481 osmo_timer_schedule(&trans->ss.timer_guard,
482 net->ncss_guard_timeout, 0);
483 }
484
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700485 /* Allocate and prepare a new MT message */
486 ss_msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
487 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
488 gh->proto_discr = GSM48_PDISC_NC_SS;
489 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700490
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700491 /**
492 * Perform GSUP-interface to A-interface mapping,
493 * according to GSM TS 09.11, table 4.1.
494 *
495 * TODO: see (note 3), both CONTINUE and END may
496 * be also mapped to REGISTER if a new transaction
497 * has to be established.
498 */
499 switch (gsup_msg->session_state) {
500 case OSMO_GSUP_SESSION_STATE_BEGIN:
501 gh->msg_type = GSM0480_MTYPE_REGISTER;
502 break;
503 case OSMO_GSUP_SESSION_STATE_CONTINUE:
504 gh->msg_type = GSM0480_MTYPE_FACILITY;
505 break;
506 case OSMO_GSUP_SESSION_STATE_END:
507 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
508 break;
509
510 /* Missing or incorrect session state */
511 case OSMO_GSUP_SESSION_STATE_NONE:
512 default:
513 LOGP(DMM, LOGL_ERROR, "Unexpected session state %d\n",
514 gsup_msg->session_state);
515 /* FIXME: send ERROR back to the HLR */
516 msgb_free(ss_msg);
517 return -EINVAL;
518 }
519
520 /* Facility IE is optional only for RELEASE COMPLETE */
521 if (gh->msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
522 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
523 LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE "
524 "for mapped 0x%02x message\n", gh->msg_type);
525 /* FIXME: send ERROR back to the HLR */
526 msgb_free(ss_msg);
527 return -EINVAL;
528 }
529 }
530
531 /* Append Facility IE if preset */
532 if (gsup_msg->ss_info && gsup_msg->ss_info_len > 2) {
533 /* Facility IE carries LV, others carry TLV */
534 if (gh->msg_type == GSM0480_MTYPE_FACILITY)
535 msgb_lv_put(ss_msg, gsup_msg->ss_info_len, gsup_msg->ss_info);
536 else
537 msgb_tlv_put(ss_msg, GSM0480_IE_FACILITY,
538 gsup_msg->ss_info_len, gsup_msg->ss_info);
539 }
540
541 /* Should we release the transaction? */
542 trans_end = (gh->msg_type == GSM0480_MTYPE_RELEASE_COMPLETE);
543
544 /* Sent to the MS, give ownership of ss_msg */
545 msc_tx_dtap(trans->conn, ss_msg);
546
547 /* Release transaction if required */
548 if (trans_end)
549 trans_free(trans);
550
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700551 /* Count established network-initiated NC SS/USSD sessions */
552 if (gsup_msg->session_state == OSMO_GSUP_SESSION_STATE_BEGIN)
553 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_ESTABLISHED]);
554
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700555 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200556}