blob: 79fcb5a97e4a97949a84aec7ebac36f7f098a681 [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>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010049#include <osmocom/msc/msc_a.h>
50#include <osmocom/msc/msub.h>
51#include <osmocom/msc/paging.h>
52#include <osmocom/msc/gsup_client_mux.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070053
54/* FIXME: choose a proper range */
55static uint32_t new_callref = 0x20000001;
Harald Welte6eafe912009-10-16 08:32:58 +020056
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070057static void ncss_session_timeout_handler(void *_trans)
58{
59 struct gsm_trans *trans = (struct gsm_trans *) _trans;
Vadim Yanitskiy944d6a22019-06-17 21:57:08 +070060 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070061
62 /* The timeout might be disabled from the VTY */
63 if (trans->net->ncss_guard_timeout == 0)
64 return;
65
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010066 LOG_TRANS(trans, LOGL_NOTICE, "SS/USSD session timeout, releasing\n");
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070067
68 /* Indicate connection release to subscriber (if active) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010069 if (trans->msc_a != NULL) {
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070070 /* This pair of cause location and value is used by commercial networks */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010071 msc_send_ussd_release_complete_cause(trans->msc_a, trans->transaction_id,
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070072 GSM48_CAUSE_LOC_PUN_S_LU, GSM48_CC_CAUSE_NORMAL_UNSPEC);
73 }
74
75 /* Terminate GSUP session with EUSE */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010076 gsup_msg = (struct osmo_gsup_message){
77 .message_type = OSMO_GSUP_MSGT_PROC_SS_ERROR,
78
79 .session_state = OSMO_GSUP_SESSION_STATE_END,
80 .session_id = trans->callref,
81 .cause = GMM_CAUSE_NET_FAIL,
82
83 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
84 };
85
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070086 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, trans->vsub->imsi);
87
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010088 gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070089
90 /* Finally, release this transaction */
91 trans_free(trans);
92}
93
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070094/* Entry point for call independent MO SS messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010095int gsm0911_rcv_nc_ss(struct msc_a *msc_a, struct msgb *msg)
Harald Welte6eafe912009-10-16 08:32:58 +020096{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010097 struct gsm_network *net;
98 struct vlr_subscr *vsub;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070099 struct gsm48_hdr *gh = msgb_l3(msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700100 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700101 struct gsm_trans *trans;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700102 uint16_t facility_ie_len;
103 uint8_t *facility_ie;
Max4a5cfa52019-01-10 17:41:05 +0100104 uint8_t tid;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700105 uint8_t msg_type;
106 int rc;
Harald Welte6eafe912009-10-16 08:32:58 +0200107
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100108 net = msc_a_net(msc_a);
109 OSMO_ASSERT(net);
110
111 vsub = msc_a_vsub(msc_a);
112 if (!vsub) {
113 LOG_MSC_A(msc_a, LOGL_ERROR, "No vlr_subscr set for this conn\n");
114 return -EINVAL;
115 }
116
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700117 msg_type = gsm48_hdr_msg_type(gh);
118 tid = gsm48_hdr_trans_id_flip_ti(gh);
Harald Welte2483f1b2016-06-19 18:06:02 +0200119
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700120 /* Associate logging messages with this subscriber */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100121 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700122
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700123 /* Reuse existing transaction, or create a new one */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100124 trans = trans_find_by_id(msc_a, TRANS_USSD, tid);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700125 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700126 /* Count MS-initiated attempts to establish a NC SS/USSD session */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100127 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_REQUESTS]);
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700128
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700129 /**
130 * According to GSM TS 04.80, section 2.4.2 "Register
131 * (mobile station to network direction)", the REGISTER
132 * message is sent by the mobile station to the network
133 * to assign a new transaction identifier for call independent
134 * supplementary service control and to request or acknowledge
135 * a supplementary service.
136 */
137 if (msg_type != GSM0480_MTYPE_REGISTER) {
Vadim Yanitskiy66322512019-06-17 22:12:54 +0700138 LOGP(DSS, LOGL_ERROR, "Rx %s message for non-existing transaction (tid-%u)\n",
139 gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type),
140 gsm48_hdr_trans_id(gh));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100141 gsm48_tx_simple(msc_a,
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700142 GSM48_PDISC_NC_SS | (tid << 4),
143 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700144 return -EINVAL;
145 }
146
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100147 trans = trans_alloc(net, vsub, TRANS_USSD, tid, new_callref++);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700148 if (!trans) {
Vadim Yanitskiy66322512019-06-17 22:12:54 +0700149 LOGP(DSS, LOGL_ERROR, " -> No memory for trans\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100150 gsm48_tx_simple(msc_a,
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700151 GSM48_PDISC_NC_SS | (tid << 4),
152 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700153 return -ENOMEM;
154 }
155
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700156 /* Init inactivity timer */
157 osmo_timer_setup(&trans->ss.timer_guard,
158 ncss_session_timeout_handler, trans);
159
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700160 /* Count active NC SS/USSD sessions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100161 osmo_counter_inc(net->active_nc_ss);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700162
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700163 trans->dlci = OMSC_LINKID_CB(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100164 trans->msc_a = msc_a;
165 msc_a_get(msc_a, MSC_A_USE_NC_SS);
166
167 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
168
169 /* An earlier CM Service Request for this SS message now has concluded */
170 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS))
171 LOG_MSC_A(msc_a, LOGL_ERROR,
172 "Creating new MO SS transaction without prior CM Service Request\n");
173 else
174 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_SS);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700175 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200176
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100177 LOG_TRANS(trans, LOGL_DEBUG, "Received SS/USSD msg %s\n",
178 gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type));
179
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700180 /* (Re)schedule the inactivity timer */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100181 if (net->ncss_guard_timeout > 0) {
182 osmo_timer_schedule(&trans->ss.timer_guard, net->ncss_guard_timeout, 0);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700183 }
184
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700185 /* Attempt to extract Facility IE */
186 rc = gsm0480_extract_ie_by_tag(gh, msgb_l3len(msg),
187 &facility_ie, &facility_ie_len, GSM0480_IE_FACILITY);
188 if (rc) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100189 LOG_TRANS(trans, LOGL_ERROR, "GSM 04.80 message parsing error, couldn't extract Facility IE\n");
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700190 goto error;
Tobias Engelea730322013-12-28 17:03:14 +0100191 }
192
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700193 /* Facility IE is optional for RELEASE COMPLETE */
194 if (msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
195 if (!facility_ie || facility_ie_len < 2) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100196 LOG_TRANS(trans, LOGL_ERROR, "GSM 04.80 message parsing error,"
197 " missing mandatory Facility IE\n");
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700198 rc = -EINVAL;
199 goto error;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200200 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700201 }
202
203 /* Compose a mew GSUP message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100204 gsup_msg = (struct osmo_gsup_message){
205 .message_type = OSMO_GSUP_MSGT_PROC_SS_REQUEST,
206 .session_id = trans->callref,
207 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
208 };
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700209
210 /**
211 * Perform A-interface to GSUP-interface mapping,
212 * according to GSM TS 09.11, table 4.2.
213 */
214 switch (msg_type) {
215 case GSM0480_MTYPE_REGISTER:
216 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_BEGIN;
217 break;
218 case GSM0480_MTYPE_FACILITY:
219 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
220 break;
221 case GSM0480_MTYPE_RELEASE_COMPLETE:
222 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END;
223 break;
224 }
225
226 /* Fill in the (optional) message payload */
227 if (facility_ie) {
228 gsup_msg.ss_info_len = facility_ie_len;
229 gsup_msg.ss_info = facility_ie;
230 }
231
232 /* Fill in subscriber's IMSI */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100233 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700234
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100235 rc = gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700236
Vadim Yanitskiyfcc24ed2018-06-21 17:55:56 +0700237 /* Should we release connection? Or wait for response? */
238 if (msg_type == GSM0480_MTYPE_RELEASE_COMPLETE)
239 trans_free(trans);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700240
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700241 /* Count established MS-initiated NC SS/USSD sessions */
242 if (msg_type == GSM0480_MTYPE_REGISTER)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100243 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_ESTABLISHED]);
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700244
Vadim Yanitskiyb3bdc762019-06-17 22:23:09 +0700245 return rc;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700246
247error:
248 /* Abort transaction on DTAP-interface */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100249 msc_send_ussd_reject(msc_a, tid, -1,
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700250 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
251 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
252 if (trans)
253 trans_free(trans);
254
255 /* TODO: abort transaction on GSUP interface if any */
256 return rc;
257}
258
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700259/* Call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100260static void ss_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700261{
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700262 struct gsm48_hdr *gh;
263 struct msgb *ss_msg;
264
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100265 if (trans->msc_a) {
266 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
267 "Handle paging error: transaction already associated with subsciber,"
268 " apparently it was already handled. Skip.\n");
269 return;
270 }
271 OSMO_ASSERT(trans->ss.msg);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700272
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100273 if (msc_a) {
274 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200275 LOG_MSC_A_CAT(msc_a, DSS, LOGL_DEBUG, "Paging succeeded\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700276
277 /* Assign connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100278 msc_a_get(msc_a, MSC_A_USE_NC_SS);
279 trans->msc_a = msc_a;
280 trans->paging_request = NULL;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700281
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700282 /* (Re)schedule the inactivity timer */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100283 if (net->ncss_guard_timeout > 0) {
284 osmo_timer_schedule(&trans->ss.timer_guard, net->ncss_guard_timeout, 0);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700285 }
286
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700287 /* Send stored message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100288 ss_msg = trans->ss.msg;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700289 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
290 gh->proto_discr = GSM48_PDISC_NC_SS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100291 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700292 gh->msg_type = GSM0480_MTYPE_REGISTER;
293
294 /* Sent to the MS, give ownership of ss_msg */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100295 msc_a_tx_dtap_to_i(msc_a, ss_msg);
296 trans->ss.msg = NULL;
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700297
298 /* Count established network-initiated NC SS/USSD sessions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100299 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_ESTABLISHED]);
300 } else {
Vadim Yanitskiy74808522019-06-16 02:35:28 +0700301 struct osmo_gsup_message gsup_msg;
302
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200303 LOG_MSC_A_CAT(msc_a, DSS, LOGL_DEBUG, "Paging expired\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700304
Vadim Yanitskiy74808522019-06-16 02:35:28 +0700305 gsup_msg = (struct osmo_gsup_message){
306 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
307 .message_type = OSMO_GSUP_MSGT_PROC_SS_ERROR,
308
309 .session_state = OSMO_GSUP_SESSION_STATE_END,
310 .session_id = trans->callref,
311 /* FIXME: we need message class specific cause values */
312 .cause = GMM_CAUSE_IMPL_DETACHED,
313 };
314
315 /* Fill in subscriber's IMSI */
316 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, trans->vsub->imsi);
317
318 /* Inform HLR/EUSE about the failure */
319 gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700320
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100321 msgb_free(trans->ss.msg);
322 trans->ss.msg = NULL;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700323
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100324 trans->callref = 0;
325 trans->paging_request = NULL;
326 trans_free(trans);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700327 }
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700328}
329
330static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100331 struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700332{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100333 struct msc_a *msc_a;
Vadim Yanitskiy95b040c2019-06-15 16:56:20 +0700334 struct gsm_trans *trans;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700335 int tid;
336
337 if (gsup_msg->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) {
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700338 LOGP(DSS, LOGL_ERROR, "Received non-BEGIN message for non-existing transaction\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700339 return NULL;
340 }
341
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700342 LOGP(DSS, LOGL_DEBUG, "(%s) Establishing a network-originated session (id=0x%x)\n",
343 vlr_subscr_name(vsub), gsup_msg->session_id);
344
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700345 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700346 LOGP(DSS, LOGL_ERROR, "Missing mandatory Facility IE\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700347 return NULL;
348 }
349
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700350 /* Obtain an unused transaction ID */
351 tid = trans_assign_trans_id(net, vsub, TRANS_USSD);
352 if (tid < 0) {
353 LOGP(DSS, LOGL_ERROR, "No free transaction ID\n");
354 return NULL;
355 }
356
357 /* Allocate a new NCSS transaction */
358 trans = trans_alloc(net, vsub, TRANS_USSD, tid, gsup_msg->session_id);
359 if (!trans) {
360 LOGP(DSS, LOGL_ERROR, " -> No memory for trans\n");
361 return NULL;
362 }
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700363
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700364 /* Count active NC SS/USSD sessions */
365 osmo_counter_inc(net->active_nc_ss);
366
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700367 /* Init inactivity timer */
368 osmo_timer_setup(&trans->ss.timer_guard,
369 ncss_session_timeout_handler, trans);
370
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700371 /* Attempt to find connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100372 msc_a = msc_a_for_vsub(vsub, true);
373 if (msc_a) {
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700374 /* Assign connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100375 msc_a_get(msc_a, MSC_A_USE_NC_SS);
376 trans->msc_a = msc_a;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700377 trans->dlci = 0x00; /* SAPI=0, not SACCH */
378 return trans;
379 }
380
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100381 LOG_TRANS(trans, LOGL_DEBUG, "Triggering Paging Request\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700382
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700383 /* Trigger Paging Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100384 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_SIGNALLING_HIGH_PRIO,
385 ss_paging_cb, trans, "GSM 09.11 SS/USSD");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700386 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100387 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700388 trans_free(trans);
389 return NULL;
390 }
391
392 /* Store the Facility IE to be sent */
393 OSMO_ASSERT(trans->ss.msg == NULL);
394 trans->ss.msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
395 msgb_tlv_put(trans->ss.msg, GSM0480_IE_FACILITY,
396 gsup_msg->ss_info_len, gsup_msg->ss_info);
397
Vadim Yanitskiy04bbfb82019-06-15 15:15:55 +0700398 return trans;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700399}
400
401/* NC SS specific transaction release.
402 * Gets called by trans_free, DO NOT CALL YOURSELF! */
403void _gsm911_nc_ss_trans_free(struct gsm_trans *trans)
404{
405 /**
406 * TODO: if transaction wasn't properly terminated,
407 * we need to do it here by releasing the subscriber
408 * connection and sending notification via GSUP...
409 */
410 if (trans->ss.msg != NULL)
411 msgb_free(trans->ss.msg);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700412
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700413 /* Stop inactivity timer */
414 osmo_timer_del(&trans->ss.timer_guard);
415
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700416 /* One session less */
417 osmo_counter_dec(trans->net->active_nc_ss);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700418}
419
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100420int gsm0911_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700421{
Vadim Yanitskiy805eca22019-06-15 17:30:23 +0700422 struct gsm_network *net = (struct gsm_network *) data;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700423 struct gsm_trans *trans;
424 struct gsm48_hdr *gh;
425 struct msgb *ss_msg;
426 bool trans_end;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100427 struct msc_a *msc_a;
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700428 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100429
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700430 vsub = vlr_subscr_find_by_imsi(net->vlr, gsup_msg->imsi, __func__);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100431 if (!vsub) {
Vadim Yanitskiy3d603032019-06-15 00:54:30 +0700432 LOGP(DSS, LOGL_ERROR, "Rx %s for unknown subscriber, rejecting\n",
433 osmo_gsup_message_type_name(gsup_msg->message_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100434 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
435 return -GMM_CAUSE_IMSI_UNKNOWN;
436 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700437
438 /* Associate logging messages with this subscriber */
439 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
440
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700441 /* Attempt to find DTAP-transaction */
442 trans = trans_find_by_callref(net, gsup_msg->session_id);
443
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700444 /* Handle errors */
445 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700446 LOGP(DSS, LOGL_NOTICE, "Rx %s from HLR/EUSE (cause=0x%02x, sid=0x%x)\n",
447 osmo_gsup_message_type_name(gsup_msg->message_type),
448 gsup_msg->cause, gsup_msg->session_id);
449
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700450 /* We don't need subscriber info anymore */
451 vlr_subscr_put(vsub, __func__);
452
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700453 if (!trans) {
454 LOGP(DSS, LOGL_ERROR, "No transaction found for "
455 "sid=0x%x, nothing to abort\n", gsup_msg->session_id);
456 return -ENODEV;
457 }
458
459 LOG_TRANS(trans, LOGL_NOTICE, "Aborting the session: sending RELEASE COMPLETE\n");
460
461 /* Indicate connection release to subscriber (if active) */
462 if (trans->msc_a != NULL) {
463 /* TODO: implement GSUP - GSM 04.80 cause mapping */
464 msc_send_ussd_release_complete_cause(trans->msc_a, trans->transaction_id,
465 GSM48_CAUSE_LOC_PUN_S_LU, GSM48_CC_CAUSE_TEMP_FAILURE);
466 }
467
468 /* Terminate transaction */
469 trans_free(trans);
470
Harald Welte6307b852009-10-16 08:41:51 +0200471 return 0;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200472 }
Harald Welte6eafe912009-10-16 08:32:58 +0200473
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700474 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700475 /* Count network-initiated attempts to establish a NC SS/USSD session */
476 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_REQUESTS]);
477
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700478 /* Attempt to establish a new transaction */
479 trans = establish_nc_ss_trans(net, vsub, gsup_msg);
480 if (!trans) {
Vadim Yanitskiydb5bc702019-06-15 16:13:08 +0700481 LOGP(DSS, LOGL_ERROR, "Failed to establish a network-originated "
482 "SS/USSD transaction, rejecting %s\n",
483 osmo_gsup_message_type_name(gsup_msg->message_type));
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700484 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_NET_FAIL);
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700485 vlr_subscr_put(vsub, __func__);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700486 return -EINVAL;
487 }
488
489 /* Wait for Paging Response */
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700490 if (trans->paging_request) {
491 vlr_subscr_put(vsub, __func__);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700492 return 0;
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700493 }
Harald Welte6eafe912009-10-16 08:32:58 +0200494 }
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800495
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700496 /* We don't need subscriber info anymore */
497 vlr_subscr_put(vsub, __func__);
498
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700499 /* (Re)schedule the inactivity timer */
500 if (net->ncss_guard_timeout > 0) {
501 osmo_timer_schedule(&trans->ss.timer_guard,
502 net->ncss_guard_timeout, 0);
503 }
504
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700505 /* Allocate and prepare a new MT message */
506 ss_msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
507 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
508 gh->proto_discr = GSM48_PDISC_NC_SS;
509 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700510
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700511 /**
512 * Perform GSUP-interface to A-interface mapping,
513 * according to GSM TS 09.11, table 4.1.
514 *
515 * TODO: see (note 3), both CONTINUE and END may
516 * be also mapped to REGISTER if a new transaction
517 * has to be established.
518 */
519 switch (gsup_msg->session_state) {
520 case OSMO_GSUP_SESSION_STATE_BEGIN:
521 gh->msg_type = GSM0480_MTYPE_REGISTER;
522 break;
523 case OSMO_GSUP_SESSION_STATE_CONTINUE:
524 gh->msg_type = GSM0480_MTYPE_FACILITY;
525 break;
526 case OSMO_GSUP_SESSION_STATE_END:
527 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
528 break;
529
530 /* Missing or incorrect session state */
531 case OSMO_GSUP_SESSION_STATE_NONE:
532 default:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100533 LOG_TRANS(trans, LOGL_ERROR, "Unexpected session state %d\n",
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700534 gsup_msg->session_state);
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700535 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_MSGT_INCOMP_P_STATE);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700536 msgb_free(ss_msg);
537 return -EINVAL;
538 }
539
540 /* Facility IE is optional only for RELEASE COMPLETE */
541 if (gh->msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
542 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100543 LOG_TRANS(trans, LOGL_ERROR, "Missing mandatory Facility IE "
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700544 "for mapped 0x%02x message\n", gh->msg_type);
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700545 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_INV_MAND_INFO);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700546 msgb_free(ss_msg);
547 return -EINVAL;
548 }
549 }
550
551 /* Append Facility IE if preset */
552 if (gsup_msg->ss_info && gsup_msg->ss_info_len > 2) {
553 /* Facility IE carries LV, others carry TLV */
554 if (gh->msg_type == GSM0480_MTYPE_FACILITY)
555 msgb_lv_put(ss_msg, gsup_msg->ss_info_len, gsup_msg->ss_info);
556 else
557 msgb_tlv_put(ss_msg, GSM0480_IE_FACILITY,
558 gsup_msg->ss_info_len, gsup_msg->ss_info);
559 }
560
561 /* Should we release the transaction? */
562 trans_end = (gh->msg_type == GSM0480_MTYPE_RELEASE_COMPLETE);
563
564 /* Sent to the MS, give ownership of ss_msg */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100565 msc_a = trans->msc_a;
566 if (!msc_a) {
567 LOG_TRANS(trans, LOGL_ERROR, "Cannot send SS message, no local MSC-A role defined for subscriber\n");
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700568 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_NET_FAIL);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100569 msgb_free(ss_msg);
570 return -EINVAL;
571 }
572 msc_a_tx_dtap_to_i(msc_a, ss_msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700573
574 /* Release transaction if required */
575 if (trans_end)
576 trans_free(trans);
577
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700578 /* Count established network-initiated NC SS/USSD sessions */
579 if (gsup_msg->session_state == OSMO_GSUP_SESSION_STATE_BEGIN)
580 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_ESTABLISHED]);
581
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700582 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200583}