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 | 5b860fa | 2018-06-12 05:24:52 +0700 | [diff] [blame] | 54 | /* Entry point for call independent MO SS messages */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 55 | int gsm0911_rcv_nc_ss(struct ran_conn *conn, struct msgb *msg) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 56 | { |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 57 | struct gsm48_hdr *gh = msgb_l3(msg); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 58 | struct osmo_gsup_message gsup_msg; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 59 | struct gsm_trans *trans; |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 60 | struct msgb *gsup_msgb; |
| 61 | uint16_t facility_ie_len; |
| 62 | uint8_t *facility_ie; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 63 | uint8_t pdisc, tid; |
| 64 | uint8_t msg_type; |
| 65 | int rc; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 66 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 67 | pdisc = gsm48_hdr_pdisc(gh); |
| 68 | msg_type = gsm48_hdr_msg_type(gh); |
| 69 | tid = gsm48_hdr_trans_id_flip_ti(gh); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 70 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 71 | /* Associate logging messages with this subscriber */ |
| 72 | log_set_context(LOG_CTX_VLR_SUBSCR, conn->vsub); |
| 73 | |
| 74 | DEBUGP(DMM, "Received SS/USSD data (trans_id=%x, msg_type=%s)\n", |
| 75 | tid, gsm48_pdisc_msgtype_name(pdisc, msg_type)); |
| 76 | |
| 77 | /* Reuse existing transaction, or create a new one */ |
| 78 | trans = trans_find_by_id(conn, pdisc, tid); |
| 79 | if (!trans) { |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 80 | /* Count MS-initiated attempts to establish a NC SS/USSD session */ |
| 81 | rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_REQUESTS]); |
| 82 | |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 83 | /** |
| 84 | * According to GSM TS 04.80, section 2.4.2 "Register |
| 85 | * (mobile station to network direction)", the REGISTER |
| 86 | * message is sent by the mobile station to the network |
| 87 | * to assign a new transaction identifier for call independent |
| 88 | * supplementary service control and to request or acknowledge |
| 89 | * a supplementary service. |
| 90 | */ |
| 91 | if (msg_type != GSM0480_MTYPE_REGISTER) { |
| 92 | LOGP(DMM, LOGL_ERROR, "Unexpected message (msg_type=%s), " |
| 93 | "transaction is not allocated yet\n", |
| 94 | gsm48_pdisc_msgtype_name(pdisc, msg_type)); |
Vadim Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 95 | gsm48_tx_simple(conn, |
| 96 | GSM48_PDISC_NC_SS | (tid << 4), |
| 97 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | DEBUGP(DMM, " -> (new transaction)\n"); |
| 102 | trans = trans_alloc(conn->network, conn->vsub, |
| 103 | pdisc, tid, new_callref++); |
| 104 | if (!trans) { |
| 105 | DEBUGP(DMM, " -> No memory for trans\n"); |
Vadim Yanitskiy | 9aec25e | 2018-06-12 06:26:28 +0700 | [diff] [blame] | 106 | gsm48_tx_simple(conn, |
| 107 | GSM48_PDISC_NC_SS | (tid << 4), |
| 108 | GSM0480_MTYPE_RELEASE_COMPLETE); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 109 | return -ENOMEM; |
| 110 | } |
| 111 | |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 112 | /* Count active NC SS/USSD sessions */ |
| 113 | osmo_counter_inc(conn->network->active_nc_ss); |
| 114 | |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 115 | trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 116 | trans->dlci = OMSC_LINKID_CB(msg); |
| 117 | cm_service_request_concludes(conn, msg); |
| 118 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 119 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 120 | /* Attempt to extract Facility IE */ |
| 121 | rc = gsm0480_extract_ie_by_tag(gh, msgb_l3len(msg), |
| 122 | &facility_ie, &facility_ie_len, GSM0480_IE_FACILITY); |
| 123 | if (rc) { |
| 124 | LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, " |
| 125 | "couldn't extract Facility IE\n"); |
| 126 | goto error; |
Tobias Engel | ea73032 | 2013-12-28 17:03:14 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 129 | /* Facility IE is optional for RELEASE COMPLETE */ |
| 130 | if (msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) { |
| 131 | if (!facility_ie || facility_ie_len < 2) { |
| 132 | LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, " |
| 133 | "missing mandatory Facility IE\n"); |
| 134 | rc = -EINVAL; |
| 135 | goto error; |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 136 | } |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Compose a mew GSUP message */ |
| 140 | memset(&gsup_msg, 0x00, sizeof(gsup_msg)); |
| 141 | gsup_msg.message_type = OSMO_GSUP_MSGT_PROC_SS_REQUEST; |
| 142 | gsup_msg.session_id = trans->callref; |
| 143 | |
| 144 | /** |
| 145 | * Perform A-interface to GSUP-interface mapping, |
| 146 | * according to GSM TS 09.11, table 4.2. |
| 147 | */ |
| 148 | switch (msg_type) { |
| 149 | case GSM0480_MTYPE_REGISTER: |
| 150 | gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_BEGIN; |
| 151 | break; |
| 152 | case GSM0480_MTYPE_FACILITY: |
| 153 | gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE; |
| 154 | break; |
| 155 | case GSM0480_MTYPE_RELEASE_COMPLETE: |
| 156 | gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | /* Fill in the (optional) message payload */ |
| 161 | if (facility_ie) { |
| 162 | gsup_msg.ss_info_len = facility_ie_len; |
| 163 | gsup_msg.ss_info = facility_ie; |
| 164 | } |
| 165 | |
| 166 | /* Fill in subscriber's IMSI */ |
| 167 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, conn->vsub->imsi); |
| 168 | |
| 169 | /* Allocate GSUP message buffer */ |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 170 | gsup_msgb = osmo_gsup_client_msgb_alloc(); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 171 | if (!gsup_msgb) { |
| 172 | LOGP(DMM, LOGL_ERROR, "Couldn't allocate GSUP message\n"); |
| 173 | rc = -ENOMEM; |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | /* Encode GSUP message */ |
| 178 | rc = osmo_gsup_encode(gsup_msgb, &gsup_msg); |
| 179 | if (rc) { |
| 180 | LOGP(DMM, LOGL_ERROR, "Couldn't encode GSUP message\n"); |
| 181 | goto error; |
| 182 | } |
| 183 | |
| 184 | /* Finally send */ |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 185 | rc = osmo_gsup_client_send(conn->network->vlr->gsup_client, gsup_msgb); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 186 | if (rc) { |
| 187 | LOGP(DMM, LOGL_ERROR, "Couldn't send GSUP message\n"); |
| 188 | goto error; |
| 189 | } |
| 190 | |
Vadim Yanitskiy | fcc24ed | 2018-06-21 17:55:56 +0700 | [diff] [blame] | 191 | /* Should we release connection? Or wait for response? */ |
| 192 | if (msg_type == GSM0480_MTYPE_RELEASE_COMPLETE) |
| 193 | trans_free(trans); |
| 194 | else |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 195 | ran_conn_communicating(conn); |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 196 | |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 197 | /* Count established MS-initiated NC SS/USSD sessions */ |
| 198 | if (msg_type == GSM0480_MTYPE_REGISTER) |
| 199 | rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_NC_SS_MO_ESTABLISHED]); |
| 200 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 201 | return 0; |
| 202 | |
| 203 | error: |
| 204 | /* Abort transaction on DTAP-interface */ |
Vadim Yanitskiy | 0622ef5 | 2018-08-03 04:39:04 +0700 | [diff] [blame] | 205 | msc_send_ussd_reject(conn, tid, -1, |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 206 | GSM_0480_PROBLEM_CODE_TAG_GENERAL, |
| 207 | GSM_0480_GEN_PROB_CODE_UNRECOGNISED); |
| 208 | if (trans) |
| 209 | trans_free(trans); |
| 210 | |
| 211 | /* TODO: abort transaction on GSUP interface if any */ |
| 212 | return rc; |
| 213 | } |
| 214 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 215 | /* Call-back from paging the B-end of the connection */ |
| 216 | static int handle_paging_event(unsigned int hooknum, unsigned int event, |
| 217 | struct msgb *msg, void *_conn, void *_transt) |
| 218 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 219 | struct ran_conn *conn = _conn; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 220 | enum gsm_paging_event paging_event = event; |
| 221 | struct gsm_trans *transt = _transt; |
| 222 | struct gsm48_hdr *gh; |
| 223 | struct msgb *ss_msg; |
| 224 | |
| 225 | OSMO_ASSERT(!transt->conn); |
| 226 | OSMO_ASSERT(transt->ss.msg); |
| 227 | |
| 228 | switch (paging_event) { |
| 229 | case GSM_PAGING_SUCCEEDED: |
| 230 | DEBUGP(DMM, "Paging subscr %s succeeded!\n", |
| 231 | vlr_subscr_msisdn_or_name(transt->vsub)); |
| 232 | |
| 233 | /* Assign connection */ |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 234 | transt->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 235 | transt->paging_request = NULL; |
| 236 | |
| 237 | /* Send stored message */ |
| 238 | ss_msg = transt->ss.msg; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 239 | gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh)); |
| 240 | gh->proto_discr = GSM48_PDISC_NC_SS; |
| 241 | gh->proto_discr |= transt->transaction_id << 4; |
| 242 | gh->msg_type = GSM0480_MTYPE_REGISTER; |
| 243 | |
| 244 | /* Sent to the MS, give ownership of ss_msg */ |
| 245 | msc_tx_dtap(transt->conn, ss_msg); |
| 246 | transt->ss.msg = NULL; |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 247 | |
| 248 | /* Count established network-initiated NC SS/USSD sessions */ |
| 249 | 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] | 250 | break; |
| 251 | case GSM_PAGING_EXPIRED: |
| 252 | case GSM_PAGING_BUSY: |
| 253 | DEBUGP(DMM, "Paging subscr %s %s!\n", |
| 254 | vlr_subscr_msisdn_or_name(transt->vsub), |
| 255 | paging_event == GSM_PAGING_EXPIRED ? "expired" : "busy"); |
| 256 | |
| 257 | /* TODO: inform HLR about this failure */ |
| 258 | |
| 259 | msgb_free(transt->ss.msg); |
| 260 | transt->ss.msg = NULL; |
| 261 | |
| 262 | transt->callref = 0; |
| 263 | transt->paging_request = NULL; |
| 264 | trans_free(transt); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net, |
| 272 | struct vlr_subscr *vsub, struct osmo_gsup_message *gsup_msg) |
| 273 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 274 | struct ran_conn *conn; |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 275 | struct gsm_trans *trans, *transt; |
| 276 | int tid; |
| 277 | |
| 278 | if (gsup_msg->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) { |
| 279 | LOGP(DMM, LOGL_ERROR, "Received non-BEGIN message " |
| 280 | "for non-existing transaction\n"); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) { |
| 285 | LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE\n"); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | /* If subscriber is not "attached" */ |
| 290 | if (!vsub->lac) { |
| 291 | LOGP(DMM, LOGL_ERROR, "Network-originated session " |
| 292 | "rejected - subscriber is not attached\n"); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | DEBUGP(DMM, "Establishing network-originated session\n"); |
| 297 | |
| 298 | /* Allocate a new transaction */ |
| 299 | trans = trans_alloc(net, vsub, GSM48_PDISC_NC_SS, |
| 300 | 0xff, gsup_msg->session_id); |
| 301 | if (!trans) { |
| 302 | DEBUGP(DMM, " -> No memory for trans\n"); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 306 | /* Count active NC SS/USSD sessions */ |
| 307 | osmo_counter_inc(net->active_nc_ss); |
| 308 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 309 | /* Assign transaction ID */ |
| 310 | tid = trans_assign_trans_id(trans->net, |
| 311 | trans->vsub, GSM48_PDISC_NC_SS, 0); |
| 312 | if (tid < 0) { |
| 313 | LOGP(DMM, LOGL_ERROR, "No free transaction ID\n"); |
| 314 | /* TODO: inform HLR about this */ |
| 315 | /* TODO: release connection with subscriber */ |
| 316 | trans->callref = 0; |
| 317 | trans_free(trans); |
| 318 | return NULL; |
| 319 | } |
| 320 | trans->transaction_id = tid; |
| 321 | |
| 322 | /* Attempt to find connection */ |
| 323 | conn = connection_for_subscr(vsub); |
| 324 | if (conn) { |
| 325 | /* Assign connection */ |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 326 | trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_NC_SS); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 327 | trans->dlci = 0x00; /* SAPI=0, not SACCH */ |
| 328 | return trans; |
| 329 | } |
| 330 | |
| 331 | DEBUGP(DMM, "Triggering Paging Request\n"); |
| 332 | |
| 333 | /* Find transaction with this subscriber already paging */ |
| 334 | llist_for_each_entry(transt, &net->trans_list, entry) { |
| 335 | /* Transaction of our conn? */ |
| 336 | if (transt == trans || transt->vsub != vsub) |
| 337 | continue; |
| 338 | |
| 339 | LOGP(DMM, LOGL_ERROR, "Paging already started, " |
| 340 | "rejecting message...\n"); |
| 341 | trans_free(trans); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | /* Trigger Paging Request */ |
| 346 | trans->paging_request = subscr_request_conn(vsub, |
| 347 | &handle_paging_event, trans, "GSM 09.11 SS/USSD"); |
| 348 | if (!trans->paging_request) { |
| 349 | LOGP(DMM, LOGL_ERROR, "Failed to allocate paging token\n"); |
| 350 | trans_free(trans); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | /* Store the Facility IE to be sent */ |
| 355 | OSMO_ASSERT(trans->ss.msg == NULL); |
| 356 | trans->ss.msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD"); |
| 357 | msgb_tlv_put(trans->ss.msg, GSM0480_IE_FACILITY, |
| 358 | gsup_msg->ss_info_len, gsup_msg->ss_info); |
| 359 | |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | /* NC SS specific transaction release. |
| 364 | * Gets called by trans_free, DO NOT CALL YOURSELF! */ |
| 365 | void _gsm911_nc_ss_trans_free(struct gsm_trans *trans) |
| 366 | { |
| 367 | /** |
| 368 | * TODO: if transaction wasn't properly terminated, |
| 369 | * we need to do it here by releasing the subscriber |
| 370 | * connection and sending notification via GSUP... |
| 371 | */ |
| 372 | if (trans->ss.msg != NULL) |
| 373 | msgb_free(trans->ss.msg); |
Vadim Yanitskiy | ad64e2a | 2018-06-26 18:27:25 +0700 | [diff] [blame] | 374 | |
| 375 | /* One session less */ |
| 376 | osmo_counter_dec(trans->net->active_nc_ss); |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 377 | } |
| 378 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 379 | int gsm0911_gsup_handler(struct vlr_subscr *vsub, |
| 380 | struct osmo_gsup_message *gsup_msg) |
| 381 | { |
| 382 | struct vlr_instance *vlr; |
| 383 | struct gsm_network *net; |
| 384 | struct gsm_trans *trans; |
| 385 | struct gsm48_hdr *gh; |
| 386 | struct msgb *ss_msg; |
| 387 | bool trans_end; |
| 388 | |
| 389 | /* Associate logging messages with this subscriber */ |
| 390 | log_set_context(LOG_CTX_VLR_SUBSCR, vsub); |
| 391 | |
| 392 | /* Obtain pointer to vlr_instance */ |
| 393 | vlr = vsub->vlr; |
| 394 | OSMO_ASSERT(vlr); |
| 395 | |
| 396 | /* Obtain pointer to gsm_network */ |
| 397 | net = (struct gsm_network *) vlr->user_ctx; |
| 398 | OSMO_ASSERT(net); |
| 399 | |
| 400 | /* Handle errors */ |
| 401 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 402 | /* FIXME: handle this error somehow! */ |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 403 | return 0; |
Holger Hans Peter Freyther | 5085e0b | 2016-07-12 17:53:26 +0200 | [diff] [blame] | 404 | } |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 405 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 406 | /* Attempt to find DTAP-transaction */ |
| 407 | trans = trans_find_by_callref(net, gsup_msg->session_id); |
| 408 | if (!trans) { |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 409 | /* Count network-initiated attempts to establish a NC SS/USSD session */ |
| 410 | rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_REQUESTS]); |
| 411 | |
Vadim Yanitskiy | f2f83b0 | 2018-06-17 21:09:28 +0700 | [diff] [blame] | 412 | /* Attempt to establish a new transaction */ |
| 413 | trans = establish_nc_ss_trans(net, vsub, gsup_msg); |
| 414 | if (!trans) { |
| 415 | /* FIXME: send ERROR back to the HLR */ |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | /* Wait for Paging Response */ |
| 420 | if (trans->paging_request) |
| 421 | return 0; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 422 | } |
Holger Hans Peter Freyther | 2486663 | 2010-06-30 12:15:19 +0800 | [diff] [blame] | 423 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 424 | /* Allocate and prepare a new MT message */ |
| 425 | ss_msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD"); |
| 426 | gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh)); |
| 427 | gh->proto_discr = GSM48_PDISC_NC_SS; |
| 428 | gh->proto_discr |= trans->transaction_id << 4; |
Vadim Yanitskiy | 10c6419 | 2018-04-17 19:17:11 +0700 | [diff] [blame] | 429 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 430 | /** |
| 431 | * Perform GSUP-interface to A-interface mapping, |
| 432 | * according to GSM TS 09.11, table 4.1. |
| 433 | * |
| 434 | * TODO: see (note 3), both CONTINUE and END may |
| 435 | * be also mapped to REGISTER if a new transaction |
| 436 | * has to be established. |
| 437 | */ |
| 438 | switch (gsup_msg->session_state) { |
| 439 | case OSMO_GSUP_SESSION_STATE_BEGIN: |
| 440 | gh->msg_type = GSM0480_MTYPE_REGISTER; |
| 441 | break; |
| 442 | case OSMO_GSUP_SESSION_STATE_CONTINUE: |
| 443 | gh->msg_type = GSM0480_MTYPE_FACILITY; |
| 444 | break; |
| 445 | case OSMO_GSUP_SESSION_STATE_END: |
| 446 | gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE; |
| 447 | break; |
| 448 | |
| 449 | /* Missing or incorrect session state */ |
| 450 | case OSMO_GSUP_SESSION_STATE_NONE: |
| 451 | default: |
| 452 | LOGP(DMM, LOGL_ERROR, "Unexpected session state %d\n", |
| 453 | gsup_msg->session_state); |
| 454 | /* FIXME: send ERROR back to the HLR */ |
| 455 | msgb_free(ss_msg); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | |
| 459 | /* Facility IE is optional only for RELEASE COMPLETE */ |
| 460 | if (gh->msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) { |
| 461 | if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) { |
| 462 | LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE " |
| 463 | "for mapped 0x%02x message\n", gh->msg_type); |
| 464 | /* FIXME: send ERROR back to the HLR */ |
| 465 | msgb_free(ss_msg); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Append Facility IE if preset */ |
| 471 | if (gsup_msg->ss_info && gsup_msg->ss_info_len > 2) { |
| 472 | /* Facility IE carries LV, others carry TLV */ |
| 473 | if (gh->msg_type == GSM0480_MTYPE_FACILITY) |
| 474 | msgb_lv_put(ss_msg, gsup_msg->ss_info_len, gsup_msg->ss_info); |
| 475 | else |
| 476 | msgb_tlv_put(ss_msg, GSM0480_IE_FACILITY, |
| 477 | gsup_msg->ss_info_len, gsup_msg->ss_info); |
| 478 | } |
| 479 | |
| 480 | /* Should we release the transaction? */ |
| 481 | trans_end = (gh->msg_type == GSM0480_MTYPE_RELEASE_COMPLETE); |
| 482 | |
| 483 | /* Sent to the MS, give ownership of ss_msg */ |
| 484 | msc_tx_dtap(trans->conn, ss_msg); |
| 485 | |
| 486 | /* Release transaction if required */ |
| 487 | if (trans_end) |
| 488 | trans_free(trans); |
| 489 | |
Vadim Yanitskiy | 8e25cc5 | 2018-06-23 03:32:20 +0700 | [diff] [blame] | 490 | /* Count established network-initiated NC SS/USSD sessions */ |
| 491 | if (gsup_msg->session_state == OSMO_GSUP_SESSION_STATE_BEGIN) |
| 492 | rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_NC_SS_MT_ESTABLISHED]); |
| 493 | |
Vadim Yanitskiy | 8a6ef55 | 2018-06-12 08:21:20 +0700 | [diff] [blame] | 494 | return 0; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 495 | } |