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> |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 4 | * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 11 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
Vadim Yanitskiy | 5b860fa | 2018-06-12 05:24:52 +0700 | [diff] [blame] | 23 | /** |
| 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 30 | #include <errno.h> |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 31 | #include <stdbool.h> |
| 32 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 34 | #include <osmocom/core/rate_ctr.h> |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 35 | #include <osmocom/core/utils.h> |
| 36 | #include <osmocom/core/msgb.h> |
Vadim Yanitskiy | 0622ef5 | 2018-08-03 04:39:04 +0700 | [diff] [blame] | 37 | |
| 38 | #include <osmocom/gsm/protocol/gsm_04_80.h> |
| 39 | #include <osmocom/gsm/gsm0480.h> |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 40 | #include <osmocom/gsm/tlv.h> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 42 | #include <osmocom/msc/gsm_04_80.h> |
| 43 | #include <osmocom/msc/gsm_subscriber.h> |
| 44 | #include <osmocom/msc/debug.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 45 | #include <osmocom/msc/vlr.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 46 | #include <osmocom/msc/gsm_04_08.h> |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 47 | #include <osmocom/msc/transaction.h> |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 48 | #include <osmocom/gsupclient/gsup_client.h> |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 49 | #include <osmocom/msc/msc_ifaces.h> |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 50 | |
| 51 | /* FIXME: choose a proper range */ |
| 52 | static uint32_t new_callref = 0x20000001; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 53 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 54 | static 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 Yanitskiy | 5b860fa | 2018-06-12 05:24:52 +0700 | [diff] [blame] | 87 | /* Entry point for call independent MO SS messages */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 88 | int gsm0911_rcv_nc_ss(struct ran_conn *conn, struct msgb *msg) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 89 | { |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 90 | struct gsm48_hdr *gh = msgb_l3(msg); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 91 | struct osmo_gsup_message gsup_msg; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 92 | struct gsm_trans *trans; |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 93 | struct msgb *gsup_msgb; |
| 94 | uint16_t facility_ie_len; |
| 95 | uint8_t *facility_ie; |
Max | 4a5cfa5 | 2019-01-10 17:41:05 +0100 | [diff] [blame] | 96 | uint8_t tid; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 97 | uint8_t msg_type; |
| 98 | int rc; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 99 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 100 | msg_type = gsm48_hdr_msg_type(gh); |
| 101 | tid = gsm48_hdr_trans_id_flip_ti(gh); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 102 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 103 | /* 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", |
Max | 4a5cfa5 | 2019-01-10 17:41:05 +0100 | [diff] [blame] | 107 | tid, gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type)); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 108 | |
| 109 | /* Reuse existing transaction, or create a new one */ |
Max | 4a5cfa5 | 2019-01-10 17:41:05 +0100 | [diff] [blame] | 110 | trans = trans_find_by_id(conn, GSM48_PDISC_NC_SS, tid); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 111 | if (!trans) { |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 112 | /* 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 Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 115 | /** |
| 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", |
Max | 4a5cfa5 | 2019-01-10 17:41:05 +0100 | [diff] [blame] | 126 | gsm48_pdisc_msgtype_name(GSM48_PDISC_NC_SS, msg_type)); |
Vadim Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 127 | gsm48_tx_simple(conn, |
| 128 | GSM48_PDISC_NC_SS | (tid << 4), |
| 129 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | DEBUGP(DMM, " -> (new transaction)\n"); |
| 134 | trans = trans_alloc(conn->network, conn->vsub, |
Max | 4a5cfa5 | 2019-01-10 17:41:05 +0100 | [diff] [blame] | 135 | GSM48_PDISC_NC_SS, tid, new_callref++); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 136 | if (!trans) { |
Max | 3614fd6 | 2019-01-17 13:38:10 +0100 | [diff] [blame] | 137 | LOGP(DMM, LOGL_ERROR, " -> No memory for trans\n"); |
Vadim Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 138 | gsm48_tx_simple(conn, |
| 139 | GSM48_PDISC_NC_SS | (tid << 4), |
| 140 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 141 | return -ENOMEM; |
| 142 | } |
| 143 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 144 | /* Init inactivity timer */ |
| 145 | osmo_timer_setup(&trans->ss.timer_guard, |
| 146 | ncss_session_timeout_handler, trans); |
| 147 | |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 148 | /* Count active NC SS/USSD sessions */ |
| 149 | osmo_counter_inc(conn->network->active_nc_ss); |
| 150 | |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 151 | trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 152 | trans->dlci = OMSC_LINKID_CB(msg); |
| 153 | cm_service_request_concludes(conn, msg); |
| 154 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 155 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 156 | /* (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 Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 162 | /* 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 Engel | ea73032 | 2013-12-28 17:03:14 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 171 | /* 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 Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 178 | } |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 179 | } |
| 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 Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 212 | gsup_msgb = osmo_gsup_client_msgb_alloc(); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 213 | 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 Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 227 | rc = osmo_gsup_client_send(conn->network->vlr->gsup_client, gsup_msgb); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 228 | if (rc) { |
| 229 | LOGP(DMM, LOGL_ERROR, "Couldn't send GSUP message\n"); |
| 230 | goto error; |
| 231 | } |
| 232 | |
Vadim Yanitskiy | fcc24ed | 2018-06-21 17:55:56 +0700 | [diff] [blame] | 233 | /* Should we release connection? Or wait for response? */ |
| 234 | if (msg_type == GSM0480_MTYPE_RELEASE_COMPLETE) |
| 235 | trans_free(trans); |
| 236 | else |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 237 | ran_conn_communicating(conn); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 238 | |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 239 | /* 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 Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 243 | return 0; |
| 244 | |
| 245 | error: |
| 246 | /* Abort transaction on DTAP-interface */ |
Vadim Yanitskiy | 0622ef5 | 2018-08-03 04:39:04 +0700 | [diff] [blame] | 247 | msc_send_ussd_reject(conn, tid, -1, |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 248 | 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 Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 257 | /* Call-back from paging the B-end of the connection */ |
| 258 | static int handle_paging_event(unsigned int hooknum, unsigned int event, |
| 259 | struct msgb *msg, void *_conn, void *_transt) |
| 260 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 261 | struct ran_conn *conn = _conn; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 262 | 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 Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 276 | transt->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 277 | transt->paging_request = NULL; |
| 278 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 279 | /* (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 Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 285 | /* Send stored message */ |
| 286 | ss_msg = transt->ss.msg; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 287 | 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 Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 295 | |
| 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 Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 298 | 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 | |
| 319 | static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net, |
| 320 | struct vlr_subscr *vsub, struct osmo_gsup_message *gsup_msg) |
| 321 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 322 | struct ran_conn *conn; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 323 | 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" */ |
Max | 7d41d87 | 2018-12-19 11:48:33 +0100 | [diff] [blame] | 338 | if (!vsub->cgi.lai.lac) { |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 339 | 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, |
Max | d8daaae | 2019-02-14 16:54:10 +0700 | [diff] [blame] | 348 | TRANS_ID_UNASSIGNED, gsup_msg->session_id); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 349 | if (!trans) { |
Max | 3614fd6 | 2019-01-17 13:38:10 +0100 | [diff] [blame] | 350 | LOGP(DMM, LOGL_ERROR, " -> No memory for trans\n"); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 351 | return NULL; |
| 352 | } |
| 353 | |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 354 | /* Count active NC SS/USSD sessions */ |
| 355 | osmo_counter_inc(net->active_nc_ss); |
| 356 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 357 | /* Assign transaction ID */ |
Max | 30fb97a | 2019-01-10 17:25:33 +0100 | [diff] [blame] | 358 | tid = trans_assign_trans_id(trans->net, trans->vsub, GSM48_PDISC_NC_SS); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 359 | 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 Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 369 | /* Init inactivity timer */ |
| 370 | osmo_timer_setup(&trans->ss.timer_guard, |
| 371 | ncss_session_timeout_handler, trans); |
| 372 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 373 | /* Attempt to find connection */ |
| 374 | conn = connection_for_subscr(vsub); |
| 375 | if (conn) { |
| 376 | /* Assign connection */ |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 377 | trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 378 | 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 Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 398 | &handle_paging_event, trans, "GSM 09.11 SS/USSD", |
| 399 | SGSAP_SERV_IND_CS_CALL); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 400 | 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! */ |
| 417 | void _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 Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 426 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 427 | /* Stop inactivity timer */ |
| 428 | osmo_timer_del(&trans->ss.timer_guard); |
| 429 | |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 430 | /* One session less */ |
| 431 | osmo_counter_dec(trans->net->active_nc_ss); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 432 | } |
| 433 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 434 | int 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 Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 458 | return 0; |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 459 | } |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 460 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 461 | /* Attempt to find DTAP-transaction */ |
| 462 | trans = trans_find_by_callref(net, gsup_msg->session_id); |
| 463 | if (!trans) { |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 464 | /* 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 Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 467 | /* 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 Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 477 | } |
Holger Hans Peter Freyther | 2486663 | 2010-06-30 12:15:19 +0800 | [diff] [blame] | 478 | |
Vadim Yanitskiy | 64623e1 | 2018-11-28 23:05:51 +0700 | [diff] [blame] | 479 | /* (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 Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 485 | /* 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 Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 490 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 491 | /** |
| 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 Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 551 | /* 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 Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 555 | return 0; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 556 | } |