blob: 9423ec73298063358914df138b617e47210b4590 [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 Yanitskiy8a6ef552018-06-12 08:21:20 +070034#include <osmocom/core/utils.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/gsm/tlv.h>
Harald Welte6eafe912009-10-16 08:32:58 +020037
Neels Hofmeyr90843962017-09-04 15:04:35 +020038#include <osmocom/msc/gsm_04_80.h>
39#include <osmocom/msc/gsm_subscriber.h>
40#include <osmocom/msc/debug.h>
41#include <osmocom/msc/osmo_msc.h>
42#include <osmocom/msc/vlr.h>
Max43b01b02017-09-15 11:22:30 +020043#include <osmocom/msc/gsm_04_08.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070044#include <osmocom/msc/transaction.h>
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070045#include <osmocom/msc/gsup_client.h>
46#include <osmocom/msc/msc_ifaces.h>
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070047
48/* FIXME: choose a proper range */
49static uint32_t new_callref = 0x20000001;
Harald Welte6eafe912009-10-16 08:32:58 +020050
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070051/* Entry point for call independent MO SS messages */
52int gsm0911_rcv_nc_ss(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte6eafe912009-10-16 08:32:58 +020053{
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070054 struct gsm48_hdr *gh = msgb_l3(msg);
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070055 struct osmo_gsup_message gsup_msg;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070056 struct gsm_trans *trans;
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +070057 struct msgb *gsup_msgb;
58 uint16_t facility_ie_len;
59 uint8_t *facility_ie;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070060 uint8_t pdisc, tid;
61 uint8_t msg_type;
62 int rc;
Harald Welte6eafe912009-10-16 08:32:58 +020063
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070064 pdisc = gsm48_hdr_pdisc(gh);
65 msg_type = gsm48_hdr_msg_type(gh);
66 tid = gsm48_hdr_trans_id_flip_ti(gh);
Harald Welte2483f1b2016-06-19 18:06:02 +020067
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070068 /* Associate logging messages with this subscriber */
69 log_set_context(LOG_CTX_VLR_SUBSCR, conn->vsub);
70
71 DEBUGP(DMM, "Received SS/USSD data (trans_id=%x, msg_type=%s)\n",
72 tid, gsm48_pdisc_msgtype_name(pdisc, msg_type));
73
74 /* Reuse existing transaction, or create a new one */
75 trans = trans_find_by_id(conn, pdisc, tid);
76 if (!trans) {
77 /**
78 * According to GSM TS 04.80, section 2.4.2 "Register
79 * (mobile station to network direction)", the REGISTER
80 * message is sent by the mobile station to the network
81 * to assign a new transaction identifier for call independent
82 * supplementary service control and to request or acknowledge
83 * a supplementary service.
84 */
85 if (msg_type != GSM0480_MTYPE_REGISTER) {
86 LOGP(DMM, LOGL_ERROR, "Unexpected message (msg_type=%s), "
87 "transaction is not allocated yet\n",
88 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +070089 gsm48_tx_simple(conn,
90 GSM48_PDISC_NC_SS | (tid << 4),
91 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +070092 return -EINVAL;
93 }
94
95 DEBUGP(DMM, " -> (new transaction)\n");
96 trans = trans_alloc(conn->network, conn->vsub,
97 pdisc, tid, new_callref++);
98 if (!trans) {
99 DEBUGP(DMM, " -> No memory for trans\n");
Vadim Yanitskiy9aec25e2018-06-12 06:26:28 +0700100 gsm48_tx_simple(conn,
101 GSM48_PDISC_NC_SS | (tid << 4),
102 GSM0480_MTYPE_RELEASE_COMPLETE);
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700103 return -ENOMEM;
104 }
105
106 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_NC_SS);
107 trans->dlci = OMSC_LINKID_CB(msg);
108 cm_service_request_concludes(conn, msg);
109 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200110
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700111 /* Attempt to extract Facility IE */
112 rc = gsm0480_extract_ie_by_tag(gh, msgb_l3len(msg),
113 &facility_ie, &facility_ie_len, GSM0480_IE_FACILITY);
114 if (rc) {
115 LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, "
116 "couldn't extract Facility IE\n");
117 goto error;
Tobias Engelea730322013-12-28 17:03:14 +0100118 }
119
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700120 /* Facility IE is optional for RELEASE COMPLETE */
121 if (msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
122 if (!facility_ie || facility_ie_len < 2) {
123 LOGP(DMM, LOGL_ERROR, "GSM 04.80 message parsing error, "
124 "missing mandatory Facility IE\n");
125 rc = -EINVAL;
126 goto error;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200127 }
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700128 }
129
130 /* Compose a mew GSUP message */
131 memset(&gsup_msg, 0x00, sizeof(gsup_msg));
132 gsup_msg.message_type = OSMO_GSUP_MSGT_PROC_SS_REQUEST;
133 gsup_msg.session_id = trans->callref;
134
135 /**
136 * Perform A-interface to GSUP-interface mapping,
137 * according to GSM TS 09.11, table 4.2.
138 */
139 switch (msg_type) {
140 case GSM0480_MTYPE_REGISTER:
141 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_BEGIN;
142 break;
143 case GSM0480_MTYPE_FACILITY:
144 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
145 break;
146 case GSM0480_MTYPE_RELEASE_COMPLETE:
147 gsup_msg.session_state = OSMO_GSUP_SESSION_STATE_END;
148 break;
149 }
150
151 /* Fill in the (optional) message payload */
152 if (facility_ie) {
153 gsup_msg.ss_info_len = facility_ie_len;
154 gsup_msg.ss_info = facility_ie;
155 }
156
157 /* Fill in subscriber's IMSI */
158 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, conn->vsub->imsi);
159
160 /* Allocate GSUP message buffer */
161 gsup_msgb = gsup_client_msgb_alloc();
162 if (!gsup_msgb) {
163 LOGP(DMM, LOGL_ERROR, "Couldn't allocate GSUP message\n");
164 rc = -ENOMEM;
165 goto error;
166 }
167
168 /* Encode GSUP message */
169 rc = osmo_gsup_encode(gsup_msgb, &gsup_msg);
170 if (rc) {
171 LOGP(DMM, LOGL_ERROR, "Couldn't encode GSUP message\n");
172 goto error;
173 }
174
175 /* Finally send */
176 rc = gsup_client_send(conn->network->vlr->gsup_client, gsup_msgb);
177 if (rc) {
178 LOGP(DMM, LOGL_ERROR, "Couldn't send GSUP message\n");
179 goto error;
180 }
181
182 /* Don't release connection, wait for response */
183 msc_subscr_conn_communicating(conn);
184
185 return 0;
186
187error:
188 /* Abort transaction on DTAP-interface */
189 gsm0480_send_ussd_reject(conn, tid, -1,
190 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
191 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
192 if (trans)
193 trans_free(trans);
194
195 /* TODO: abort transaction on GSUP interface if any */
196 return rc;
197}
198
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700199/* Call-back from paging the B-end of the connection */
200static int handle_paging_event(unsigned int hooknum, unsigned int event,
201 struct msgb *msg, void *_conn, void *_transt)
202{
203 struct gsm_subscriber_connection *conn = _conn;
204 enum gsm_paging_event paging_event = event;
205 struct gsm_trans *transt = _transt;
206 struct gsm48_hdr *gh;
207 struct msgb *ss_msg;
208
209 OSMO_ASSERT(!transt->conn);
210 OSMO_ASSERT(transt->ss.msg);
211
212 switch (paging_event) {
213 case GSM_PAGING_SUCCEEDED:
214 DEBUGP(DMM, "Paging subscr %s succeeded!\n",
215 vlr_subscr_msisdn_or_name(transt->vsub));
216
217 /* Assign connection */
218 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_NC_SS);
219 transt->paging_request = NULL;
220
221 /* Send stored message */
222 ss_msg = transt->ss.msg;
223 OSMO_ASSERT(ss_msg);
224
225 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
226 gh->proto_discr = GSM48_PDISC_NC_SS;
227 gh->proto_discr |= transt->transaction_id << 4;
228 gh->msg_type = GSM0480_MTYPE_REGISTER;
229
230 /* Sent to the MS, give ownership of ss_msg */
231 msc_tx_dtap(transt->conn, ss_msg);
232 transt->ss.msg = NULL;
233 break;
234 case GSM_PAGING_EXPIRED:
235 case GSM_PAGING_BUSY:
236 DEBUGP(DMM, "Paging subscr %s %s!\n",
237 vlr_subscr_msisdn_or_name(transt->vsub),
238 paging_event == GSM_PAGING_EXPIRED ? "expired" : "busy");
239
240 /* TODO: inform HLR about this failure */
241
242 msgb_free(transt->ss.msg);
243 transt->ss.msg = NULL;
244
245 transt->callref = 0;
246 transt->paging_request = NULL;
247 trans_free(transt);
248 break;
249 }
250
251 return 0;
252}
253
254static struct gsm_trans *establish_nc_ss_trans(struct gsm_network *net,
255 struct vlr_subscr *vsub, struct osmo_gsup_message *gsup_msg)
256{
257 struct gsm_subscriber_connection *conn;
258 struct gsm_trans *trans, *transt;
259 int tid;
260
261 if (gsup_msg->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) {
262 LOGP(DMM, LOGL_ERROR, "Received non-BEGIN message "
263 "for non-existing transaction\n");
264 return NULL;
265 }
266
267 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
268 LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE\n");
269 return NULL;
270 }
271
272 /* If subscriber is not "attached" */
273 if (!vsub->lac) {
274 LOGP(DMM, LOGL_ERROR, "Network-originated session "
275 "rejected - subscriber is not attached\n");
276 return NULL;
277 }
278
279 DEBUGP(DMM, "Establishing network-originated session\n");
280
281 /* Allocate a new transaction */
282 trans = trans_alloc(net, vsub, GSM48_PDISC_NC_SS,
283 0xff, gsup_msg->session_id);
284 if (!trans) {
285 DEBUGP(DMM, " -> No memory for trans\n");
286 return NULL;
287 }
288
289 /* Assign transaction ID */
290 tid = trans_assign_trans_id(trans->net,
291 trans->vsub, GSM48_PDISC_NC_SS, 0);
292 if (tid < 0) {
293 LOGP(DMM, LOGL_ERROR, "No free transaction ID\n");
294 /* TODO: inform HLR about this */
295 /* TODO: release connection with subscriber */
296 trans->callref = 0;
297 trans_free(trans);
298 return NULL;
299 }
300 trans->transaction_id = tid;
301
302 /* Attempt to find connection */
303 conn = connection_for_subscr(vsub);
304 if (conn) {
305 /* Assign connection */
306 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_NC_SS);
307 trans->dlci = 0x00; /* SAPI=0, not SACCH */
308 return trans;
309 }
310
311 DEBUGP(DMM, "Triggering Paging Request\n");
312
313 /* Find transaction with this subscriber already paging */
314 llist_for_each_entry(transt, &net->trans_list, entry) {
315 /* Transaction of our conn? */
316 if (transt == trans || transt->vsub != vsub)
317 continue;
318
319 LOGP(DMM, LOGL_ERROR, "Paging already started, "
320 "rejecting message...\n");
321 trans_free(trans);
322 return NULL;
323 }
324
325 /* Trigger Paging Request */
326 trans->paging_request = subscr_request_conn(vsub,
327 &handle_paging_event, trans, "GSM 09.11 SS/USSD");
328 if (!trans->paging_request) {
329 LOGP(DMM, LOGL_ERROR, "Failed to allocate paging token\n");
330 trans_free(trans);
331 return NULL;
332 }
333
334 /* Store the Facility IE to be sent */
335 OSMO_ASSERT(trans->ss.msg == NULL);
336 trans->ss.msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
337 msgb_tlv_put(trans->ss.msg, GSM0480_IE_FACILITY,
338 gsup_msg->ss_info_len, gsup_msg->ss_info);
339
340 return NULL;
341}
342
343/* NC SS specific transaction release.
344 * Gets called by trans_free, DO NOT CALL YOURSELF! */
345void _gsm911_nc_ss_trans_free(struct gsm_trans *trans)
346{
347 /**
348 * TODO: if transaction wasn't properly terminated,
349 * we need to do it here by releasing the subscriber
350 * connection and sending notification via GSUP...
351 */
352 if (trans->ss.msg != NULL)
353 msgb_free(trans->ss.msg);
354}
355
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700356int gsm0911_gsup_handler(struct vlr_subscr *vsub,
357 struct osmo_gsup_message *gsup_msg)
358{
359 struct vlr_instance *vlr;
360 struct gsm_network *net;
361 struct gsm_trans *trans;
362 struct gsm48_hdr *gh;
363 struct msgb *ss_msg;
364 bool trans_end;
365
366 /* Associate logging messages with this subscriber */
367 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
368
369 /* Obtain pointer to vlr_instance */
370 vlr = vsub->vlr;
371 OSMO_ASSERT(vlr);
372
373 /* Obtain pointer to gsm_network */
374 net = (struct gsm_network *) vlr->user_ctx;
375 OSMO_ASSERT(net);
376
377 /* Handle errors */
378 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
379 /* FIXME: handle this error somehow! */
Harald Welte6307b852009-10-16 08:41:51 +0200380 return 0;
Holger Hans Peter Freyther5085e0b2016-07-12 17:53:26 +0200381 }
Harald Welte6eafe912009-10-16 08:32:58 +0200382
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700383 /* Attempt to find DTAP-transaction */
384 trans = trans_find_by_callref(net, gsup_msg->session_id);
385 if (!trans) {
Vadim Yanitskiyf2f83b02018-06-17 21:09:28 +0700386 /* Attempt to establish a new transaction */
387 trans = establish_nc_ss_trans(net, vsub, gsup_msg);
388 if (!trans) {
389 /* FIXME: send ERROR back to the HLR */
390 return -EINVAL;
391 }
392
393 /* Wait for Paging Response */
394 if (trans->paging_request)
395 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200396 }
Holger Hans Peter Freyther24866632010-06-30 12:15:19 +0800397
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700398 /* Allocate and prepare a new MT message */
399 ss_msg = gsm48_msgb_alloc_name("GSM 04.08 SS/USSD");
400 gh = (struct gsm48_hdr *) msgb_push(ss_msg, sizeof(*gh));
401 gh->proto_discr = GSM48_PDISC_NC_SS;
402 gh->proto_discr |= trans->transaction_id << 4;
Vadim Yanitskiy10c64192018-04-17 19:17:11 +0700403
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +0700404 /**
405 * Perform GSUP-interface to A-interface mapping,
406 * according to GSM TS 09.11, table 4.1.
407 *
408 * TODO: see (note 3), both CONTINUE and END may
409 * be also mapped to REGISTER if a new transaction
410 * has to be established.
411 */
412 switch (gsup_msg->session_state) {
413 case OSMO_GSUP_SESSION_STATE_BEGIN:
414 gh->msg_type = GSM0480_MTYPE_REGISTER;
415 break;
416 case OSMO_GSUP_SESSION_STATE_CONTINUE:
417 gh->msg_type = GSM0480_MTYPE_FACILITY;
418 break;
419 case OSMO_GSUP_SESSION_STATE_END:
420 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
421 break;
422
423 /* Missing or incorrect session state */
424 case OSMO_GSUP_SESSION_STATE_NONE:
425 default:
426 LOGP(DMM, LOGL_ERROR, "Unexpected session state %d\n",
427 gsup_msg->session_state);
428 /* FIXME: send ERROR back to the HLR */
429 msgb_free(ss_msg);
430 return -EINVAL;
431 }
432
433 /* Facility IE is optional only for RELEASE COMPLETE */
434 if (gh->msg_type != GSM0480_MTYPE_RELEASE_COMPLETE) {
435 if (!gsup_msg->ss_info || gsup_msg->ss_info_len < 2) {
436 LOGP(DMM, LOGL_ERROR, "Missing mandatory Facility IE "
437 "for mapped 0x%02x message\n", gh->msg_type);
438 /* FIXME: send ERROR back to the HLR */
439 msgb_free(ss_msg);
440 return -EINVAL;
441 }
442 }
443
444 /* Append Facility IE if preset */
445 if (gsup_msg->ss_info && gsup_msg->ss_info_len > 2) {
446 /* Facility IE carries LV, others carry TLV */
447 if (gh->msg_type == GSM0480_MTYPE_FACILITY)
448 msgb_lv_put(ss_msg, gsup_msg->ss_info_len, gsup_msg->ss_info);
449 else
450 msgb_tlv_put(ss_msg, GSM0480_IE_FACILITY,
451 gsup_msg->ss_info_len, gsup_msg->ss_info);
452 }
453
454 /* Should we release the transaction? */
455 trans_end = (gh->msg_type == GSM0480_MTYPE_RELEASE_COMPLETE);
456
457 /* Sent to the MS, give ownership of ss_msg */
458 msc_tx_dtap(trans->conn, ss_msg);
459
460 /* Release transaction if required */
461 if (trans_end)
462 trans_free(trans);
463
464 return 0;
Harald Welte6eafe912009-10-16 08:32:58 +0200465}