blob: fc588f5c265845ac5d2a9a735992e70faf25628e [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>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte6eafe912009-10-16 08:32:58 +020010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte6eafe912009-10-16 08:32:58 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte6eafe912009-10-16 08:32:58 +020019 *
20 */
21
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070022/**
23 * MSC-specific handling of call independent Supplementary
24 * Services messages (NC_SS) according to GSM TS 09.11
25 * "Signalling interworking for supplementary services".
26 */
Harald Welte6eafe912009-10-16 08:32:58 +020027
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <errno.h>
32
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/gsm_04_80.h>
34#include <osmocom/msc/gsm_subscriber.h>
35#include <osmocom/msc/debug.h>
36#include <osmocom/msc/osmo_msc.h>
37#include <osmocom/msc/vlr.h>
Max43b01b02017-09-15 11:22:30 +020038#include <osmocom/msc/gsm_04_08.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070039#include <osmocom/msc/transaction.h>
40
41/* FIXME: choose a proper range */
42static uint32_t new_callref = 0x20000001;
Harald Welte6eafe912009-10-16 08:32:58 +020043
44/* Declarations of USSD strings to be recognised */
45const char USSD_TEXT_OWN_NUMBER[] = "*#100#";
46
Vadim Yanitskiyb274d562018-01-10 19:47:21 +060047/* A network-specific handler function */
Vadim Yanitskiy12d221f2018-04-04 05:30:16 +070048static int send_own_number(struct gsm_subscriber_connection *conn,
49 const struct ss_request *req)
Vadim Yanitskiyb274d562018-01-10 19:47:21 +060050{
51 char *own_number = conn->vsub->msisdn;
52 char response_string[GSM_EXTENSION_LENGTH + 20];
Harald Welte6eafe912009-10-16 08:32:58 +020053
Vadim Yanitskiyb274d562018-01-10 19:47:21 +060054 DEBUGP(DMM, "%s: MSISDN = %s\n", vlr_subscr_name(conn->vsub),
55 own_number);
56
57 /* Need trailing CR as EOT character */
58 snprintf(response_string, sizeof(response_string), "Your extension is %s\r", own_number);
Vadim Yanitskiy12d221f2018-04-04 05:30:16 +070059 return gsm0480_send_ussd_response(conn, response_string, req);
Vadim Yanitskiyb274d562018-01-10 19:47:21 +060060}
Harald Welte6eafe912009-10-16 08:32:58 +020061
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070062/* Entry point for call independent MO SS messages */
63int gsm0911_rcv_nc_ss(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte6eafe912009-10-16 08:32:58 +020064{
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070065 struct gsm48_hdr *gh = msgb_l3(msg);
66 struct gsm_trans *trans;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +020067 struct ss_request req;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070068 uint8_t pdisc, tid;
69 uint8_t msg_type;
70 int rc;
Harald Welte6eafe912009-10-16 08:32:58 +020071
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070072 pdisc = gsm48_hdr_pdisc(gh);
73 msg_type = gsm48_hdr_msg_type(gh);
74 tid = gsm48_hdr_trans_id_flip_ti(gh);
Harald Welte2483f1b2016-06-19 18:06:02 +020075
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070076 /* Associate logging messages with this subscriber */
77 log_set_context(LOG_CTX_VLR_SUBSCR, conn->vsub);
78
79 DEBUGP(DMM, "Received SS/USSD data (trans_id=%x, msg_type=%s)\n",
80 tid, gsm48_pdisc_msgtype_name(pdisc, msg_type));
81
82 /* Reuse existing transaction, or create a new one */
83 trans = trans_find_by_id(conn, pdisc, tid);
84 if (!trans) {
85 /**
86 * According to GSM TS 04.80, section 2.4.2 "Register
87 * (mobile station to network direction)", the REGISTER
88 * message is sent by the mobile station to the network
89 * to assign a new transaction identifier for call independent
90 * supplementary service control and to request or acknowledge
91 * a supplementary service.
92 */
93 if (msg_type != GSM0480_MTYPE_REGISTER) {
94 LOGP(DMM, LOGL_ERROR, "Unexpected message (msg_type=%s), "
95 "transaction is not allocated yet\n",
96 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +070097 gsm48_tx_simple(conn,
98 GSM48_PDISC_NC_SS | (tid << 4),
99 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700100 return -EINVAL;
101 }
102
103 DEBUGP(DMM, " -> (new transaction)\n");
104 trans = trans_alloc(conn->network, conn->vsub,
105 pdisc, tid, new_callref++);
106 if (!trans) {
107 DEBUGP(DMM, " -> No memory for trans\n");
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700108 gsm48_tx_simple(conn,
109 GSM48_PDISC_NC_SS | (tid << 4),
110 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700111 return -ENOMEM;
112 }
113
114 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_NC_SS);
115 trans->dlci = OMSC_LINKID_CB(msg);
116 cm_service_request_concludes(conn, msg);
117 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200118
Holger Hans Peter Freyther06abe9f2010-06-30 11:30:01 +0800119 memset(&req, 0, sizeof(req));
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200120 rc = gsm0480_decode_ss_request(gh, msgb_l3len(msg), &req);
Tobias Engelea730322013-12-28 17:03:14 +0100121 if (!rc) {
Vadim Yanitskiyf95d5be2018-04-04 06:21:00 +0700122 LOGP(DMM, LOGL_ERROR, "SS/USSD message parsing error, "
123 "rejecting request...\n");
Vadim Yanitskiy635f98c2018-06-08 17:13:44 +0700124 gsm0480_send_ussd_reject(conn, &req, GSM_0480_PROBLEM_CODE_TAG_GENERAL,
125 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
Vadim Yanitskiy048eb302018-04-04 06:21:45 +0700126 /* The GSM 04.80 API uses inverted codes (0 means error) */
127 return -EPROTO;
Tobias Engelea730322013-12-28 17:03:14 +0100128 }
129
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200130 /* Interrogation or releaseComplete? */
131 if (req.ussd_text[0] == '\0' || req.ussd_text[0] == 0xFF) {
132 if (req.ss_code > 0) {
133 /* Assume interrogateSS or modification of it and reject */
Vadim Yanitskiy635f98c2018-06-08 17:13:44 +0700134 return gsm0480_send_ussd_return_error(conn, &req,
135 GSM0480_ERR_CODE_ILLEGAL_SS_OPERATION);
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200136 }
137 /* Still assuming a Release-Complete and returning */
Harald Welte6307b852009-10-16 08:41:51 +0200138 return 0;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200139 }
Harald Welte6eafe912009-10-16 08:32:58 +0200140
Harald Welte2483f1b2016-06-19 18:06:02 +0200141 msc_subscr_conn_communicating(conn);
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200142 if (!strcmp(USSD_TEXT_OWN_NUMBER, (const char *)req.ussd_text)) {
Harald Welte6eafe912009-10-16 08:32:58 +0200143 DEBUGP(DMM, "USSD: Own number requested\n");
Vadim Yanitskiy12d221f2018-04-04 05:30:16 +0700144 rc = send_own_number(conn, &req);
Harald Welte6eafe912009-10-16 08:32:58 +0200145 } else {
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200146 DEBUGP(DMM, "Unhandled USSD %s\n", req.ussd_text);
Vadim Yanitskiy635f98c2018-06-08 17:13:44 +0700147 rc = gsm0480_send_ussd_return_error(conn, &req,
148 GSM0480_ERR_CODE_UNEXPECTED_DATA_VALUE);
Harald Welte6eafe912009-10-16 08:32:58 +0200149 }
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800150
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700151 /**
152 * TODO: as we only handle *#100# for now, and always
153 * respond with RELEASE COMPLETE, let's manually free
154 * the transaction here, until the external interface
155 * is implemented.
156 */
157 trans_free(trans);
158
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800159 return rc;
Harald Welte6eafe912009-10-16 08:32:58 +0200160}