blob: e2938901568af4594b79feaa2719baeb7a6d7d33 [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>
Alexander Couzensefa7b972019-04-27 23:45:37 +020035#include <osmocom/core/stat_item.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070036#include <osmocom/core/utils.h>
37#include <osmocom/core/msgb.h>
Vadim Yanitskiy0622ef52018-08-03 04:39:04 +070038
39#include <osmocom/gsm/protocol/gsm_04_80.h>
40#include <osmocom/gsm/gsm0480.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070041#include <osmocom/gsm/tlv.h>
Harald Welte6eafe912009-10-16 08:32:58 +020042
Neels Hofmeyr90843962017-09-04 15:04:35 +020043#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_subscriber.h>
45#include <osmocom/msc/debug.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/vlr.h>
Max43b01b02017-09-15 11:22:30 +020047#include <osmocom/msc/gsm_04_08.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070048#include <osmocom/msc/transaction.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020049#include <osmocom/gsupclient/gsup_client.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010050#include <osmocom/msc/msc_a.h>
51#include <osmocom/msc/msub.h>
52#include <osmocom/msc/paging.h>
53#include <osmocom/msc/gsup_client_mux.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070054
55/* FIXME: choose a proper range */
56static uint32_t new_callref = 0x20000001;
Harald Welte6eafe912009-10-16 08:32:58 +020057
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070058static void ncss_session_timeout_handler(void *_trans)
59{
60 struct gsm_trans *trans = (struct gsm_trans *) _trans;
Vadim Yanitskiy944d6a22019-06-17 21:57:08 +070061 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070062
63 /* The timeout might be disabled from the VTY */
64 if (trans->net->ncss_guard_timeout == 0)
65 return;
66
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010067 LOG_TRANS(trans, LOGL_NOTICE, "SS/USSD session timeout, releasing\n");
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070068
69 /* Indicate connection release to subscriber (if active) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010070 if (trans->msc_a != NULL) {
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070071 /* This pair of cause location and value is used by commercial networks */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010072 msc_send_ussd_release_complete_cause(trans->msc_a, trans->transaction_id,
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070073 GSM48_CAUSE_LOC_PUN_S_LU, GSM48_CC_CAUSE_NORMAL_UNSPEC);
74 }
75
76 /* Terminate GSUP session with EUSE */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010077 gsup_msg = (struct osmo_gsup_message){
78 .message_type = OSMO_GSUP_MSGT_PROC_SS_ERROR,
79
80 .session_state = OSMO_GSUP_SESSION_STATE_END,
81 .session_id = trans->callref,
82 .cause = GMM_CAUSE_NET_FAIL,
83
84 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
85 };
86
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070087 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, trans->vsub->imsi);
88
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010089 gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +070090
91 /* Finally, release this transaction */
92 trans_free(trans);
93}
94
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070095/* Entry point for call independent MO SS messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010096int gsm0911_rcv_nc_ss(struct msc_a *msc_a, struct msgb *msg)
Harald Welte6eafe912009-10-16 08:32:58 +020097{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010098 struct gsm_network *net;
99 struct vlr_subscr *vsub;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700100 struct gsm48_hdr *gh = msgb_l3(msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700101 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700102 struct gsm_trans *trans;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700103 uint16_t facility_ie_len;
104 uint8_t *facility_ie;
Max4a5cfa52019-01-10 17:41:05 +0100105 uint8_t tid;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700106 uint8_t msg_type;
107 int rc;
Harald Welte6eafe912009-10-16 08:32:58 +0200108
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100109 net = msc_a_net(msc_a);
110 OSMO_ASSERT(net);
111
112 vsub = msc_a_vsub(msc_a);
113 if (!vsub) {
114 LOG_MSC_A(msc_a, LOGL_ERROR, "No vlr_subscr set for this conn\n");
115 return -EINVAL;
116 }
117
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700118 msg_type = gsm48_hdr_msg_type(gh);
119 tid = gsm48_hdr_trans_id_flip_ti(gh);
Harald Welte2483f1b2016-06-19 18:06:02 +0200120
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700121 /* Associate logging messages with this subscriber */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100122 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700123
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700124 /* Reuse existing transaction, or create a new one */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100125 trans = trans_find_by_id(msc_a, TRANS_USSD, tid);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700126 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700127 /* Count MS-initiated attempts to establish a NC SS/USSD session */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200128 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_NC_SS_MO_REQUESTS));
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700129
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700130 /**
131 * According to GSM TS 04.80, section 2.4.2 "Register
132 * (mobile station to network direction)", the REGISTER
133 * message is sent by the mobile station to the network
134 * to assign a new transaction identifier for call independent
135 * supplementary service control and to request or acknowledge
136 * a supplementary service.
137 */
138 if (msg_type != GSM0480_MTYPE_REGISTER) {
Vadim Yanitskiy66322512019-06-17 22:12:54 +0700139 LOGP(DSS, LOGL_ERROR, "Rx %s message for non-existing transaction (tid-%u)\n",
140 gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type),
141 gsm48_hdr_trans_id(gh));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100142 gsm48_tx_simple(msc_a,
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700143 GSM48_PDISC_NC_SS | (tid << 4),
144 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700145 return -EINVAL;
146 }
147
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 trans = trans_alloc(net, vsub, TRANS_USSD, tid, new_callref++);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700149 if (!trans) {
Vadim Yanitskiy66322512019-06-17 22:12:54 +0700150 LOGP(DSS, LOGL_ERROR, " -> No memory for trans\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100151 gsm48_tx_simple(msc_a,
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700152 GSM48_PDISC_NC_SS | (tid << 4),
153 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700154 return -ENOMEM;
155 }
156
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700157 /* Init inactivity timer */
158 osmo_timer_setup(&trans->ss.timer_guard,
159 ncss_session_timeout_handler, trans);
160
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700161 /* Count active NC SS/USSD sessions */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200162 osmo_stat_item_inc(osmo_stat_item_group_get_item(net->statg, MSC_STAT_ACTIVE_NC_SS), 1);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700163
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700164 trans->dlci = OMSC_LINKID_CB(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100165 trans->msc_a = msc_a;
166 msc_a_get(msc_a, MSC_A_USE_NC_SS);
167
168 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
169
170 /* An earlier CM Service Request for this SS message now has concluded */
171 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS))
172 LOG_MSC_A(msc_a, LOGL_ERROR,
173 "Creating new MO SS transaction without prior CM Service Request\n");
174 else
175 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_SS);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700176 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200177
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100178 LOG_TRANS(trans, LOGL_DEBUG, "Received SS/USSD msg %s\n",
179 gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type));
180
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700181 /* (Re)schedule the inactivity timer */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100182 if (net->ncss_guard_timeout > 0) {
183 osmo_timer_schedule(&trans->ss.timer_guard, net->ncss_guard_timeout, 0);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700184 }
185
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700186 /* Attempt to extract Facility IE */
187 rc = gsm0480_extract_ie_by_tag(gh, msgb_l3len(msg),
188 &facility_ie, &facility_ie_len, GSM0480_IE_FACILITY);
189 if (rc) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100190 LOG_TRANS(trans, LOGL_ERROR, "GSM 04.80 message parsing error, couldn't extract Facility IE\n");
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700191 goto error;
Tobias Engelea730322013-12-28 17:03:14 +0100192 }
193
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700194 /* Facility IE is optional for RELEASE COMPLETE */
195 if (msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
196 if (!facility_ie || facility_ie_len < 2) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100197 LOG_TRANS(trans, LOGL_ERROR, "GSM 04.80 message parsing error,"
198 " missing mandatory Facility IE\n");
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700199 rc = -EINVAL;
200 goto error;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200201 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700202 }
203
204 /* Compose a mew GSUP message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100205 gsup_msg = (struct osmo_gsup_message){
206 .message_type = OSMO_GSUP_MSGT_PROC_SS_REQUEST,
207 .session_id = trans->callref,
208 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
209 };
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700210
211 /**
212 * Perform A-interface to GSUP-interface mapping,
213 * according to GSM TS 09.11, table 4.2.
214 */
215 switch (msg_type) {
216 case GSM0480_MTYPE_REGISTER:
217 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_BEGIN;
218 break;
219 case GSM0480_MTYPE_FACILITY:
220 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
221 break;
222 case GSM0480_MTYPE_RELEASE_COMPLETE:
223 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END;
224 break;
225 }
226
227 /* Fill in the (optional) message payload */
228 if (facility_ie) {
229 gsup_msg.ss_info_len = facility_ie_len;
230 gsup_msg.ss_info = facility_ie;
231 }
232
233 /* Fill in subscriber's IMSI */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100234 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700235
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100236 rc = gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700237
Vadim Yanitskiyfcc24ed2018-06-21 17:55:56 +0700238 /* Should we release connection? Or wait for response? */
239 if (msg_type == GSM0480_MTYPE_RELEASE_COMPLETE)
240 trans_free(trans);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700241
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700242 /* Count established MS-initiated NC SS/USSD sessions */
243 if (msg_type == GSM0480_MTYPE_REGISTER)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200244 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_NC_SS_MO_ESTABLISHED));
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700245
Vadim Yanitskiyb3bdc762019-06-17 22:23:09 +0700246 return rc;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700247
248error:
249 /* Abort transaction on DTAP-interface */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100250 msc_send_ussd_reject(msc_a, tid, -1,
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700251 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
252 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
253 if (trans)
254 trans_free(trans);
255
256 /* TODO: abort transaction on GSUP interface if any */
257 return rc;
258}
259
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700260/* Call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100261static void ss_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700262{
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700263 struct gsm48_hdr *gh;
264 struct msgb *ss_msg;
265
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100266 if (trans->msc_a) {
267 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100268 "Handle paging error: transaction already associated with subscriber,"
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100269 " apparently it was already handled. Skip.\n");
270 return;
271 }
272 OSMO_ASSERT(trans->ss.msg);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700273
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100274 if (msc_a) {
275 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200276 LOG_MSC_A_CAT(msc_a, DSS, LOGL_DEBUG, "Paging succeeded\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700277
278 /* Assign connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100279 msc_a_get(msc_a, MSC_A_USE_NC_SS);
280 trans->msc_a = msc_a;
281 trans->paging_request = NULL;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700282
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700283 /* (Re)schedule the inactivity timer */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100284 if (net->ncss_guard_timeout > 0) {
285 osmo_timer_schedule(&trans->ss.timer_guard, net->ncss_guard_timeout, 0);
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700286 }
287
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700288 /* Send stored message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100289 ss_msg = trans->ss.msg;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700290 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
291 gh->proto_discr = GSM48_PDISC_NC_SS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100292 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700293 gh->msg_type = GSM0480_MTYPE_REGISTER;
294
295 /* Sent to the MS, give ownership of ss_msg */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100296 msc_a_tx_dtap_to_i(msc_a, ss_msg);
297 trans->ss.msg = NULL;
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700298
299 /* Count established network-initiated NC SS/USSD sessions */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200300 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_NC_SS_MT_ESTABLISHED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100301 } else {
Vadim Yanitskiy74808522019-06-16 02:35:28 +0700302 struct osmo_gsup_message gsup_msg;
303
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200304 LOG_MSC_A_CAT(msc_a, DSS, LOGL_DEBUG, "Paging expired\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700305
Vadim Yanitskiy74808522019-06-16 02:35:28 +0700306 gsup_msg = (struct osmo_gsup_message){
307 .message_class = OSMO_GSUP_MESSAGE_CLASS_USSD,
308 .message_type = OSMO_GSUP_MSGT_PROC_SS_ERROR,
309
310 .session_state = OSMO_GSUP_SESSION_STATE_END,
311 .session_id = trans->callref,
312 /* FIXME: we need message class specific cause values */
313 .cause = GMM_CAUSE_IMPL_DETACHED,
314 };
315
316 /* Fill in subscriber's IMSI */
317 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, trans->vsub->imsi);
318
319 /* Inform HLR/EUSE about the failure */
320 gsup_client_mux_tx(trans->net->gcm, &gsup_msg);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700321
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100322 msgb_free(trans->ss.msg);
323 trans->ss.msg = NULL;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700324
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100325 trans->callref = 0;
326 trans->paging_request = NULL;
327 trans_free(trans);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700328 }
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700329}
330
331static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100332 struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700333{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334 struct msc_a *msc_a;
Vadim Yanitskiy95b040c2019-06-15 16:56:20 +0700335 struct gsm_trans *trans;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700336 int tid;
337
338 if (gsup_msg->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) {
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700339 LOGP(DSS, LOGL_ERROR, "Received non-BEGIN message for non-existing transaction\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700340 return NULL;
341 }
342
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700343 LOGP(DSS, LOGL_DEBUG, "(%s) Establishing a network-originated session (id=0x%x)\n",
344 vlr_subscr_name(vsub), gsup_msg->session_id);
345
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700346 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700347 LOGP(DSS, LOGL_ERROR, "Missing mandatory Facility IE\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700348 return NULL;
349 }
350
Vadim Yanitskiy10c3ce52019-06-15 15:52:23 +0700351 /* Obtain an unused transaction ID */
352 tid = trans_assign_trans_id(net, vsub, TRANS_USSD);
353 if (tid < 0) {
354 LOGP(DSS, LOGL_ERROR, "No free transaction ID\n");
355 return NULL;
356 }
357
358 /* Allocate a new NCSS transaction */
359 trans = trans_alloc(net, vsub, TRANS_USSD, tid, gsup_msg->session_id);
360 if (!trans) {
361 LOGP(DSS, LOGL_ERROR, " -> No memory for trans\n");
362 return NULL;
363 }
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700364
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700365 /* Count active NC SS/USSD sessions */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200366 osmo_stat_item_inc(osmo_stat_item_group_get_item(net->statg, MSC_STAT_ACTIVE_NC_SS), 1);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700367
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700368 /* Init inactivity timer */
369 osmo_timer_setup(&trans->ss.timer_guard,
370 ncss_session_timeout_handler, trans);
371
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700372 /* Attempt to find connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100373 msc_a = msc_a_for_vsub(vsub, true);
374 if (msc_a) {
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700375 /* Assign connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 msc_a_get(msc_a, MSC_A_USE_NC_SS);
377 trans->msc_a = msc_a;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700378 trans->dlci = 0x00; /* SAPI=0, not SACCH */
379 return trans;
380 }
381
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100382 LOG_TRANS(trans, LOGL_DEBUG, "Triggering Paging Request\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700383
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700384 /* Trigger Paging Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100385 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_SIGNALLING_HIGH_PRIO,
386 ss_paging_cb, trans, "GSM 09.11 SS/USSD");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700387 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100388 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token\n");
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700389 trans_free(trans);
390 return NULL;
391 }
392
393 /* Store the Facility IE to be sent */
394 OSMO_ASSERT(trans->ss.msg == NULL);
395 trans->ss.msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
396 msgb_tlv_put(trans->ss.msg, GSM0480_IE_FACILITY,
397 gsup_msg->ss_info_len, gsup_msg->ss_info);
398
Vadim Yanitskiy04bbfb82019-06-15 15:15:55 +0700399 return trans;
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700400}
401
402/* NC SS specific transaction release.
403 * Gets called by trans_free, DO NOT CALL YOURSELF! */
404void _gsm911_nc_ss_trans_free(struct gsm_trans *trans)
405{
406 /**
407 * TODO: if transaction wasn't properly terminated,
408 * we need to do it here by releasing the subscriber
409 * connection and sending notification via GSUP...
410 */
411 if (trans->ss.msg != NULL)
412 msgb_free(trans->ss.msg);
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700413
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700414 /* Stop inactivity timer */
415 osmo_timer_del(&trans->ss.timer_guard);
416
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +0700417 /* One session less */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200418 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_NC_SS),
419 1);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700420}
421
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100422int gsm0911_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup_msg)
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700423{
Vadim Yanitskiy805eca22019-06-15 17:30:23 +0700424 struct gsm_network *net = (struct gsm_network *) data;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700425 struct gsm_trans *trans;
426 struct gsm48_hdr *gh;
427 struct msgb *ss_msg;
428 bool trans_end;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100429 struct msc_a *msc_a;
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700430 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100431
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700432 vsub = vlr_subscr_find_by_imsi(net->vlr, gsup_msg->imsi, __func__);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100433 if (!vsub) {
Vadim Yanitskiy3d603032019-06-15 00:54:30 +0700434 LOGP(DSS, LOGL_ERROR, "Rx %s for unknown subscriber, rejecting\n",
435 osmo_gsup_message_type_name(gsup_msg->message_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100436 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
437 return -GMM_CAUSE_IMSI_UNKNOWN;
438 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700439
440 /* Associate logging messages with this subscriber */
441 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
442
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700443 /* Attempt to find DTAP-transaction */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +0200444 trans = trans_find_by_callref(net, TRANS_USSD, gsup_msg->session_id);
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700445
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700446 /* Handle errors */
447 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700448 LOGP(DSS, LOGL_NOTICE, "Rx %s from HLR/EUSE (cause=0x%02x, sid=0x%x)\n",
449 osmo_gsup_message_type_name(gsup_msg->message_type),
450 gsup_msg->cause, gsup_msg->session_id);
451
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700452 /* We don't need subscriber info anymore */
453 vlr_subscr_put(vsub, __func__);
454
Vadim Yanitskiyf1775902019-06-16 15:34:28 +0700455 if (!trans) {
456 LOGP(DSS, LOGL_ERROR, "No transaction found for "
457 "sid=0x%x, nothing to abort\n", gsup_msg->session_id);
458 return -ENODEV;
459 }
460
461 LOG_TRANS(trans, LOGL_NOTICE, "Aborting the session: sending RELEASE COMPLETE\n");
462
463 /* Indicate connection release to subscriber (if active) */
464 if (trans->msc_a != NULL) {
465 /* TODO: implement GSUP - GSM 04.80 cause mapping */
466 msc_send_ussd_release_complete_cause(trans->msc_a, trans->transaction_id,
467 GSM48_CAUSE_LOC_PUN_S_LU, GSM48_CC_CAUSE_TEMP_FAILURE);
468 }
469
470 /* Terminate transaction */
471 trans_free(trans);
472
Harald Welte6307b852009-10-16 08:41:51 +0200473 return 0;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200474 }
Harald Welte6eafe912009-10-16 08:32:58 +0200475
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700476 if (!trans) {
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700477 /* Count network-initiated attempts to establish a NC SS/USSD session */
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200478 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_NC_SS_MT_REQUESTS));
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700479
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700480 /* Attempt to establish a new transaction */
481 trans = establish_nc_ss_trans(net, vsub, gsup_msg);
482 if (!trans) {
Vadim Yanitskiydb5bc702019-06-15 16:13:08 +0700483 LOGP(DSS, LOGL_ERROR, "Failed to establish a network-originated "
484 "SS/USSD transaction, rejecting %s\n",
485 osmo_gsup_message_type_name(gsup_msg->message_type));
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700486 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_NET_FAIL);
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700487 vlr_subscr_put(vsub, __func__);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700488 return -EINVAL;
489 }
490
491 /* Wait for Paging Response */
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700492 if (trans->paging_request) {
493 vlr_subscr_put(vsub, __func__);
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700494 return 0;
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700495 }
Harald Welte6eafe912009-10-16 08:32:58 +0200496 }
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800497
Vadim Yanitskiye0da4462019-06-17 21:51:12 +0700498 /* We don't need subscriber info anymore */
499 vlr_subscr_put(vsub, __func__);
500
Vadim Yanitskiy64623e12018-11-28 23:05:51 +0700501 /* (Re)schedule the inactivity timer */
502 if (net->ncss_guard_timeout > 0) {
503 osmo_timer_schedule(&trans->ss.timer_guard,
504 net->ncss_guard_timeout, 0);
505 }
506
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700507 /* Allocate and prepare a new MT message */
508 ss_msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
509 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
510 gh->proto_discr = GSM48_PDISC_NC_SS;
511 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700512
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700513 /**
514 * Perform GSUP-interface to A-interface mapping,
515 * according to GSM TS 09.11, table 4.1.
516 *
517 * TODO: see (note 3), both CONTINUE and END may
518 * be also mapped to REGISTER if a new transaction
519 * has to be established.
520 */
521 switch (gsup_msg->session_state) {
522 case OSMO_GSUP_SESSION_STATE_BEGIN:
523 gh->msg_type = GSM0480_MTYPE_REGISTER;
524 break;
525 case OSMO_GSUP_SESSION_STATE_CONTINUE:
526 gh->msg_type = GSM0480_MTYPE_FACILITY;
527 break;
528 case OSMO_GSUP_SESSION_STATE_END:
529 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
530 break;
531
532 /* Missing or incorrect session state */
533 case OSMO_GSUP_SESSION_STATE_NONE:
534 default:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100535 LOG_TRANS(trans, LOGL_ERROR, "Unexpected session state %d\n",
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700536 gsup_msg->session_state);
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700537 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_MSGT_INCOMP_P_STATE);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700538 msgb_free(ss_msg);
539 return -EINVAL;
540 }
541
542 /* Facility IE is optional only for RELEASE COMPLETE */
543 if (gh->msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
544 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100545 LOG_TRANS(trans, LOGL_ERROR, "Missing mandatory Facility IE "
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700546 "for mapped 0x%02x message\n", gh->msg_type);
Vadim Yanitskiyefb1f602019-06-15 14:44:46 +0700547 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_INV_MAND_INFO);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700548 msgb_free(ss_msg);
549 return -EINVAL;
550 }
551 }
552
553 /* Append Facility IE if preset */
554 if (gsup_msg->ss_info && gsup_msg->ss_info_len > 2) {
555 /* Facility IE carries LV, others carry TLV */
556 if (gh->msg_type == GSM0480_MTYPE_FACILITY)
557 msgb_lv_put(ss_msg, gsup_msg->ss_info_len, gsup_msg->ss_info);
558 else
559 msgb_tlv_put(ss_msg, GSM0480_IE_FACILITY,
560 gsup_msg->ss_info_len, gsup_msg->ss_info);
561 }
562
563 /* Should we release the transaction? */
564 trans_end = (gh->msg_type == GSM0480_MTYPE_RELEASE_COMPLETE);
565
566 /* Sent to the MS, give ownership of ss_msg */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100567 msc_a = trans->msc_a;
568 if (!msc_a) {
569 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 +0700570 gsup_client_mux_tx_error_reply(gcm, gsup_msg, GMM_CAUSE_NET_FAIL);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100571 msgb_free(ss_msg);
572 return -EINVAL;
573 }
574 msc_a_tx_dtap_to_i(msc_a, ss_msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700575
576 /* Release transaction if required */
577 if (trans_end)
578 trans_free(trans);
579
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700580 /* Count established network-initiated NC SS/USSD sessions */
581 if (gsup_msg->session_state == OSMO_GSUP_SESSION_STATE_BEGIN)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200582 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_NC_SS_MT_ESTABLISHED));
Vadim Yanitskiy8e25cc52018-06-23 03:32:20 +0700583
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700584 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200585}