Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 1 | /* (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 10 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Vadim Yanitskiy | 5b860fa | 2018-06-12 05:24:52 +0700 | [diff] [blame] | 22 | /** |
| 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 33 | #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> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 38 | #include <osmocom/msc/gsm_04_08.h> |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 39 | #include <osmocom/msc/transaction.h> |
| 40 | |
| 41 | /* FIXME: choose a proper range */ |
| 42 | static uint32_t new_callref = 0x20000001; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 43 | |
| 44 | /* Declarations of USSD strings to be recognised */ |
| 45 | const char USSD_TEXT_OWN_NUMBER[] = "*#100#"; |
| 46 | |
Vadim Yanitskiy | b274d56 | 2018-01-10 19:47:21 +0600 | [diff] [blame] | 47 | /* A network-specific handler function */ |
Vadim Yanitskiy | 12d221f | 2018-04-04 05:30:16 +0700 | [diff] [blame] | 48 | static int send_own_number(struct gsm_subscriber_connection *conn, |
Vadim Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 49 | uint8_t tid, uint8_t invoke_id) |
Vadim Yanitskiy | b274d56 | 2018-01-10 19:47:21 +0600 | [diff] [blame] | 50 | { |
| 51 | char *own_number = conn->vsub->msisdn; |
| 52 | char response_string[GSM_EXTENSION_LENGTH + 20]; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 53 | |
Vadim Yanitskiy | b274d56 | 2018-01-10 19:47:21 +0600 | [diff] [blame] | 54 | 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 Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 59 | return gsm0480_send_ussd_response(conn, tid, invoke_id, response_string); |
Vadim Yanitskiy | b274d56 | 2018-01-10 19:47:21 +0600 | [diff] [blame] | 60 | } |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 61 | |
Vadim Yanitskiy | 5b860fa | 2018-06-12 05:24:52 +0700 | [diff] [blame] | 62 | /* Entry point for call independent MO SS messages */ |
| 63 | int gsm0911_rcv_nc_ss(struct gsm_subscriber_connection *conn, struct msgb *msg) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 64 | { |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 65 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 66 | struct gsm_trans *trans; |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 67 | struct ss_request req; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 68 | uint8_t pdisc, tid; |
| 69 | uint8_t msg_type; |
| 70 | int rc; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 71 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 72 | pdisc = gsm48_hdr_pdisc(gh); |
| 73 | msg_type = gsm48_hdr_msg_type(gh); |
| 74 | tid = gsm48_hdr_trans_id_flip_ti(gh); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 75 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 76 | /* 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 Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 97 | gsm48_tx_simple(conn, |
| 98 | GSM48_PDISC_NC_SS | (tid << 4), |
| 99 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 100 | 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 Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 108 | gsm48_tx_simple(conn, |
| 109 | GSM48_PDISC_NC_SS | (tid << 4), |
| 110 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 111 | 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 118 | |
Holger Hans Peter Freyther | 06abe9f | 2010-06-30 11:30:01 +0800 | [diff] [blame] | 119 | memset(&req, 0, sizeof(req)); |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 120 | rc = gsm0480_decode_ss_request(gh, msgb_l3len(msg), &req); |
Tobias Engel | ea73032 | 2013-12-28 17:03:14 +0100 | [diff] [blame] | 121 | if (!rc) { |
Vadim Yanitskiy | f95d5be | 2018-04-04 06:21:00 +0700 | [diff] [blame] | 122 | LOGP(DMM, LOGL_ERROR, "SS/USSD message parsing error, " |
| 123 | "rejecting request...\n"); |
Vadim Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 124 | gsm0480_send_ussd_reject(conn, tid, -1, |
| 125 | GSM_0480_PROBLEM_CODE_TAG_GENERAL, |
Vadim Yanitskiy | 635f98c | 2018-06-08 17:13:44 +0700 | [diff] [blame] | 126 | GSM_0480_GEN_PROB_CODE_UNRECOGNISED); |
Vadim Yanitskiy | 048eb30 | 2018-04-04 06:21:45 +0700 | [diff] [blame] | 127 | /* The GSM 04.80 API uses inverted codes (0 means error) */ |
| 128 | return -EPROTO; |
Tobias Engel | ea73032 | 2013-12-28 17:03:14 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 131 | /* Interrogation or releaseComplete? */ |
| 132 | if (req.ussd_text[0] == '\0' || req.ussd_text[0] == 0xFF) { |
| 133 | if (req.ss_code > 0) { |
| 134 | /* Assume interrogateSS or modification of it and reject */ |
Vadim Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 135 | return gsm0480_send_ussd_return_error(conn, tid, |
| 136 | req.invoke_id, GSM0480_ERR_CODE_ILLEGAL_SS_OPERATION); |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 137 | } |
| 138 | /* Still assuming a Release-Complete and returning */ |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 139 | return 0; |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 140 | } |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 141 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 142 | msc_subscr_conn_communicating(conn); |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 143 | if (!strcmp(USSD_TEXT_OWN_NUMBER, (const char *)req.ussd_text)) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 144 | DEBUGP(DMM, "USSD: Own number requested\n"); |
Vadim Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 145 | rc = send_own_number(conn, tid, req.invoke_id); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 146 | } else { |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 147 | DEBUGP(DMM, "Unhandled USSD %s\n", req.ussd_text); |
Vadim Yanitskiy | 5df4e4d | 2018-06-12 08:03:53 +0700 | [diff] [blame] | 148 | rc = gsm0480_send_ussd_return_error(conn, |
| 149 | tid, req.invoke_id, |
Vadim Yanitskiy | 635f98c | 2018-06-08 17:13:44 +0700 | [diff] [blame] | 150 | GSM0480_ERR_CODE_UNEXPECTED_DATA_VALUE); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 151 | } |
Holger Hans Peter Freyther | 2486663 | 2010-06-30 12:15:19 +0800 | [diff] [blame] | 152 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 153 | /** |
| 154 | * TODO: as we only handle *#100# for now, and always |
| 155 | * respond with RELEASE COMPLETE, let's manually free |
| 156 | * the transaction here, until the external interface |
| 157 | * is implemented. |
| 158 | */ |
| 159 | trans_free(trans); |
| 160 | |
Holger Hans Peter Freyther | 2486663 | 2010-06-30 12:15:19 +0800 | [diff] [blame] | 161 | return rc; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 162 | } |