blob: 155e47b6fed8e64c56399c2bc871a9267cab88ce [file] [log] [blame]
Harald Weltebb779392018-06-16 20:21:10 +02001/* OsmoHLR SS/USSD implementation */
Harald Welte4956ae12018-06-15 22:04:28 +02002
3/* (C) 2018 Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
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
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
15 * GNU Affero General Public License for more details.
16 *
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/>.
19 *
20 */
21
22
23#include <osmocom/core/talloc.h>
Harald Weltebb779392018-06-16 20:21:10 +020024#include <osmocom/core/timer.h>
25#include <osmocom/gsm/gsup.h>
26#include <osmocom/gsm/gsm0480.h>
27#include <osmocom/gsm/protocol/gsm_04_80.h>
Harald Welte4956ae12018-06-15 22:04:28 +020028#include <stdint.h>
29#include <string.h>
Harald Weltedab544e2018-07-29 16:14:48 +020030#include <errno.h>
Harald Welte4956ae12018-06-15 22:04:28 +020031
Neels Hofmeyr2f758032019-11-20 00:37:07 +010032#include <osmocom/hlr/hlr.h>
33#include <osmocom/hlr/hlr_ussd.h>
34#include <osmocom/hlr/gsup_server.h>
35#include <osmocom/hlr/gsup_router.h>
36#include <osmocom/hlr/logging.h>
37#include <osmocom/hlr/db.h>
Harald Weltebb779392018-06-16 20:21:10 +020038
39/***********************************************************************
40 * core data structures expressing config from VTY
41 ***********************************************************************/
Harald Welte4956ae12018-06-15 22:04:28 +020042
43struct hlr_euse *euse_find(struct hlr *hlr, const char *name)
44{
45 struct hlr_euse *euse;
46
47 llist_for_each_entry(euse, &hlr->euse_list, list) {
48 if (!strcmp(euse->name, name))
49 return euse;
50 }
51 return NULL;
52}
53
54struct hlr_euse *euse_alloc(struct hlr *hlr, const char *name)
55{
56 struct hlr_euse *euse = euse_find(hlr, name);
57 if (euse)
58 return NULL;
59
60 euse = talloc_zero(hlr, struct hlr_euse);
61 euse->name = talloc_strdup(euse, name);
62 euse->hlr = hlr;
Harald Welte4956ae12018-06-15 22:04:28 +020063 llist_add_tail(&euse->list, &hlr->euse_list);
64
65 return euse;
66}
67
68void euse_del(struct hlr_euse *euse)
69{
70 llist_del(&euse->list);
71 talloc_free(euse);
72}
73
74
Harald Weltedab544e2018-07-29 16:14:48 +020075struct hlr_ussd_route *ussd_route_find_prefix(struct hlr *hlr, const char *prefix)
Harald Welte4956ae12018-06-15 22:04:28 +020076{
Harald Weltedab544e2018-07-29 16:14:48 +020077 struct hlr_ussd_route *rt;
Harald Welte4956ae12018-06-15 22:04:28 +020078
Harald Weltedab544e2018-07-29 16:14:48 +020079 llist_for_each_entry(rt, &hlr->ussd_routes, list) {
Harald Welte4956ae12018-06-15 22:04:28 +020080 if (!strcmp(rt->prefix, prefix))
81 return rt;
82 }
83 return NULL;
84}
85
Harald Weltedab544e2018-07-29 16:14:48 +020086struct hlr_ussd_route *ussd_route_prefix_alloc_int(struct hlr *hlr, const char *prefix,
87 const struct hlr_iuse *iuse)
Harald Welte4956ae12018-06-15 22:04:28 +020088{
Harald Weltedab544e2018-07-29 16:14:48 +020089 struct hlr_ussd_route *rt;
Harald Welte4956ae12018-06-15 22:04:28 +020090
Harald Weltedab544e2018-07-29 16:14:48 +020091 if (ussd_route_find_prefix(hlr, prefix))
Harald Welte4956ae12018-06-15 22:04:28 +020092 return NULL;
93
Harald Weltedab544e2018-07-29 16:14:48 +020094 rt = talloc_zero(hlr, struct hlr_ussd_route);
Harald Welte4956ae12018-06-15 22:04:28 +020095 rt->prefix = talloc_strdup(rt, prefix);
Harald Weltedab544e2018-07-29 16:14:48 +020096 rt->u.iuse = iuse;
97 llist_add_tail(&rt->list, &hlr->ussd_routes);
Harald Welte4956ae12018-06-15 22:04:28 +020098
99 return rt;
100}
101
Harald Weltedab544e2018-07-29 16:14:48 +0200102struct hlr_ussd_route *ussd_route_prefix_alloc_ext(struct hlr *hlr, const char *prefix,
103 struct hlr_euse *euse)
104{
105 struct hlr_ussd_route *rt;
106
107 if (ussd_route_find_prefix(hlr, prefix))
108 return NULL;
109
110 rt = talloc_zero(hlr, struct hlr_ussd_route);
111 rt->prefix = talloc_strdup(rt, prefix);
112 rt->is_external = true;
113 rt->u.euse = euse;
114 llist_add_tail(&rt->list, &hlr->ussd_routes);
115
116 return rt;
117}
118
119void ussd_route_del(struct hlr_ussd_route *rt)
Harald Welte4956ae12018-06-15 22:04:28 +0200120{
121 llist_del(&rt->list);
122 talloc_free(rt);
123}
Harald Weltebb779392018-06-16 20:21:10 +0200124
Harald Weltedab544e2018-07-29 16:14:48 +0200125static struct hlr_ussd_route *ussd_route_lookup_7bit(struct hlr *hlr, const char *ussd_code)
Harald Weltebb779392018-06-16 20:21:10 +0200126{
Harald Weltedab544e2018-07-29 16:14:48 +0200127 struct hlr_ussd_route *rt;
128 llist_for_each_entry(rt, &hlr->ussd_routes, list) {
129 if (!strncmp(ussd_code, rt->prefix, strlen(rt->prefix))) {
Vadim Yanitskiye6c839e2018-08-02 23:59:51 +0700130 LOGP(DSS, LOGL_DEBUG, "Found %s '%s' (prefix '%s') for USSD "
131 "Code '%s'\n", rt->is_external ? "EUSE" : "IUSE",
132 rt->is_external ? rt->u.euse->name : rt->u.iuse->name,
133 rt->prefix, ussd_code);
Harald Weltedab544e2018-07-29 16:14:48 +0200134 return rt;
Harald Weltebb779392018-06-16 20:21:10 +0200135 }
136 }
137
Harald Weltedab544e2018-07-29 16:14:48 +0200138 LOGP(DSS, LOGL_DEBUG, "Could not find Route for USSD Code '%s'\n", ussd_code);
Harald Weltebb779392018-06-16 20:21:10 +0200139 return NULL;
140}
141
142/***********************************************************************
143 * handling functions for individual GSUP messages
144 ***********************************************************************/
145
Harald Welte97bfb652018-07-29 12:28:11 +0200146#define LOGPSS(ss, lvl, fmt, args...) \
Harald Welte95b96d42018-07-29 12:47:39 +0200147 LOGP(DSS, lvl, "%s/0x%08x: " fmt, (ss)->imsi, (ss)->session_id, ## args)
Harald Welte97bfb652018-07-29 12:28:11 +0200148
Harald Weltebb779392018-06-16 20:21:10 +0200149struct ss_session {
150 /* link us to hlr->ss_sessions */
151 struct llist_head list;
152 /* imsi of this session */
Neels Hofmeyre21b45a2019-03-18 21:04:23 +0100153 char imsi[OSMO_IMSI_BUF_SIZE];
Harald Weltebb779392018-06-16 20:21:10 +0200154 /* ID of this session (unique per IMSI) */
155 uint32_t session_id;
156 /* state of the session */
157 enum osmo_gsup_session_state state;
158 /* time-out when we will delete the session */
159 struct osmo_timer_list timeout;
160
Harald Weltedab544e2018-07-29 16:14:48 +0200161 /* is this USSD for an external handler (EUSE): true */
162 bool is_external;
163 union {
164 /* external USSD Entity responsible for this session */
165 struct hlr_euse *euse;
166 /* internal USSD Entity responsible for this session */
167 const struct hlr_iuse *iuse;
168 } u;
169
Oliver Smith95abc2b2019-04-04 12:00:24 +0200170 /* subscriber's vlr_number
171 * MO USSD: originating MSC's vlr_number
172 * MT USSD: looked up once per session and cached here */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100173 struct osmo_ipa_name vlr_name;
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200174
Harald Weltebb779392018-06-16 20:21:10 +0200175 /* we don't keep a pointer to the osmo_gsup_{route,conn} towards the MSC/VLR here,
176 * as this might change during inter-VLR hand-over, and we simply look-up the serving MSC/VLR
177 * every time we receive an USSD component from the EUSE */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100178
179 struct osmo_gsup_req *initial_req_from_ms;
180 struct osmo_gsup_req *initial_req_from_euse;
Harald Weltebb779392018-06-16 20:21:10 +0200181};
182
183struct ss_session *ss_session_find(struct hlr *hlr, const char *imsi, uint32_t session_id)
184{
185 struct ss_session *ss;
186 llist_for_each_entry(ss, &hlr->ss_sessions, list) {
187 if (!strcmp(ss->imsi, imsi) && ss->session_id == session_id)
188 return ss;
189 }
190 return NULL;
191}
192
193void ss_session_free(struct ss_session *ss)
194{
195 osmo_timer_del(&ss->timeout);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100196 if (ss->initial_req_from_ms)
197 osmo_gsup_req_free(ss->initial_req_from_ms);
198 if (ss->initial_req_from_euse)
199 osmo_gsup_req_free(ss->initial_req_from_euse);
Harald Weltebb779392018-06-16 20:21:10 +0200200 llist_del(&ss->list);
201 talloc_free(ss);
202}
203
204static void ss_session_timeout(void *data)
205{
206 struct ss_session *ss = data;
207
Harald Welte97bfb652018-07-29 12:28:11 +0200208 LOGPSS(ss, LOGL_NOTICE, "SS Session Timeout, destroying\n");
Harald Weltebb779392018-06-16 20:21:10 +0200209 /* FIXME: should we send a ReturnError component to the MS? */
210 ss_session_free(ss);
211}
212
213struct ss_session *ss_session_alloc(struct hlr *hlr, const char *imsi, uint32_t session_id)
214{
215 struct ss_session *ss;
216
217 OSMO_ASSERT(!ss_session_find(hlr, imsi, session_id));
218
219 ss = talloc_zero(hlr, struct ss_session);
220 OSMO_ASSERT(ss);
221
222 OSMO_STRLCPY_ARRAY(ss->imsi, imsi);
223 ss->session_id = session_id;
Vadim Yanitskiye6ce52b2018-12-01 00:16:44 +0700224
225 /* Schedule self-destruction timer */
Harald Weltebb779392018-06-16 20:21:10 +0200226 osmo_timer_setup(&ss->timeout, ss_session_timeout, ss);
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700227 if (g_hlr->ncss_guard_timeout > 0)
228 osmo_timer_schedule(&ss->timeout, g_hlr->ncss_guard_timeout, 0);
Harald Weltebb779392018-06-16 20:21:10 +0200229
230 llist_add_tail(&ss->list, &hlr->ss_sessions);
231 return ss;
232}
233
234/***********************************************************************
Harald Welte72667312018-07-29 12:38:09 +0200235 * handling functions for encoding SS messages + wrapping them in GSUP
236 ***********************************************************************/
237
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200238/* Resolve the target MSC by ss->imsi and send GSUP message. */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100239static int ss_gsup_send_to_ms(struct ss_session *ss, struct osmo_gsup_server *gs, struct osmo_gsup_message *gsup)
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200240{
241 struct hlr_subscriber subscr = {};
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100242 struct msgb *msg;
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200243 int rc;
244
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100245 if (ss->initial_req_from_ms) {
246 /* Use non-final osmo_gsup_req_respond() to not deallocate the ss->initial_req_from_ms */
247 osmo_gsup_req_respond(ss->initial_req_from_ms, gsup, false, false);
248 return 0;
249 }
250
251 msg = osmo_gsup_msgb_alloc("GSUP USSD FW");
252 rc = osmo_gsup_encode(msg, gsup);
253 if (rc) {
254 LOGPSS(ss, LOGL_ERROR, "Failed to encode GSUP message\n");
255 msgb_free(msg);
256 return rc;
257 }
258
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200259 /* Use vlr_number as looked up by the caller, or look up now. */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100260 if (!ss->vlr_name.len) {
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200261 rc = db_subscr_get_by_imsi(g_hlr->dbc, ss->imsi, &subscr);
262 if (rc < 0) {
263 LOGPSS(ss, LOGL_ERROR, "Cannot find subscriber, cannot route GSUP message\n");
264 msgb_free(msg);
265 return -EINVAL;
266 }
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100267 osmo_ipa_name_set_str(&ss->vlr_name, subscr.vlr_number);
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200268 }
269
Oliver Smith95abc2b2019-04-04 12:00:24 +0200270 /* Check for empty string (all vlr_number strings end in "\0", because otherwise gsup_route_find() fails) */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100271 if (ss->vlr_name.len <= 1) {
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200272 LOGPSS(ss, LOGL_ERROR, "Cannot send GSUP message, no VLR number stored for subscriber\n");
273 msgb_free(msg);
274 return -EINVAL;
275 }
276
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100277 LOGPSS(ss, LOGL_DEBUG, "Tx SS/USSD to VLR %s\n", osmo_ipa_name_to_str(&ss->vlr_name));
278 return osmo_gsup_send_to_ipa_name(gs, &ss->vlr_name, msg);
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200279}
280
Harald Welte72667312018-07-29 12:38:09 +0200281static int ss_tx_to_ms(struct ss_session *ss, enum osmo_gsup_message_type gsup_msg_type,
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700282 struct msgb *ss_msg)
Harald Welte72667312018-07-29 12:38:09 +0200283
284{
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700285 struct osmo_gsup_message resp;
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100286 int rc;
Harald Welte72667312018-07-29 12:38:09 +0200287
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700288 resp = (struct osmo_gsup_message) {
289 .message_type = gsup_msg_type,
290 .session_id = ss->session_id,
291 .session_state = ss->state,
292 };
293
Harald Welte72667312018-07-29 12:38:09 +0200294 OSMO_STRLCPY_ARRAY(resp.imsi, ss->imsi);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700295
Harald Welte72667312018-07-29 12:38:09 +0200296 if (ss_msg) {
297 resp.ss_info = msgb_data(ss_msg);
298 resp.ss_info_len = msgb_length(ss_msg);
299 }
300
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100301 rc = ss_gsup_send_to_ms(ss, g_hlr->gs, &resp);
Harald Welte72667312018-07-29 12:38:09 +0200302
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100303 msgb_free(ss_msg);
304 return rc;
Harald Welte72667312018-07-29 12:38:09 +0200305}
306
307#if 0
308static int ss_tx_reject(struct ss_session *ss, int invoke_id, uint8_t problem_tag,
309 uint8_t problem_code)
310{
311 struct msgb *msg = gsm0480_gen_reject(invoke_id, problem_tag, problem_code);
312 LOGPSS(ss, LOGL_NOTICE, "Tx Reject(%u, 0x%02x, 0x%02x)\n", invoke_id,
313 problem_tag, problem_code);
314 OSMO_ASSERT(msg);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700315 ss->state = OSMO_GSUP_SESSION_STATE_END;
316 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg);
Harald Welte72667312018-07-29 12:38:09 +0200317}
318#endif
319
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100320static int ss_tx_to_ms_error(struct ss_session *ss, uint8_t invoke_id, uint8_t error_code)
Harald Welte72667312018-07-29 12:38:09 +0200321{
322 struct msgb *msg = gsm0480_gen_return_error(invoke_id, error_code);
323 LOGPSS(ss, LOGL_NOTICE, "Tx ReturnError(%u, 0x%02x)\n", invoke_id, error_code);
324 OSMO_ASSERT(msg);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700325 ss->state = OSMO_GSUP_SESSION_STATE_END;
326 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg);
Harald Welte72667312018-07-29 12:38:09 +0200327}
328
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700329static int ss_tx_to_ms_ussd_7bit(struct ss_session *ss, uint8_t invoke_id, const char *text)
Harald Weltedab544e2018-07-29 16:14:48 +0200330{
331 struct msgb *msg = gsm0480_gen_ussd_resp_7bit(invoke_id, text);
332 LOGPSS(ss, LOGL_INFO, "Tx USSD '%s'\n", text);
333 OSMO_ASSERT(msg);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700334 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg);
Harald Weltedab544e2018-07-29 16:14:48 +0200335}
336
337/***********************************************************************
338 * Internal USSD Handlers
339 ***********************************************************************/
340
Neels Hofmeyr2f758032019-11-20 00:37:07 +0100341#include <osmocom/hlr/db.h>
Harald Weltedab544e2018-07-29 16:14:48 +0200342
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100343static int handle_ussd_own_msisdn(struct ss_session *ss,
Harald Weltedab544e2018-07-29 16:14:48 +0200344 const struct osmo_gsup_message *gsup, const struct ss_request *req)
345{
346 struct hlr_subscriber subscr;
347 char buf[GSM0480_USSD_7BIT_STRING_LEN+1];
348 int rc;
349
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700350 ss->state = OSMO_GSUP_SESSION_STATE_END;
351
Harald Weltedab544e2018-07-29 16:14:48 +0200352 rc = db_subscr_get_by_imsi(g_hlr->dbc, ss->imsi, &subscr);
353 switch (rc) {
354 case 0:
355 if (strlen(subscr.msisdn) == 0)
356 snprintf(buf, sizeof(buf), "You have no MSISDN!");
357 else
Mychaela N. Falconia0ead8472023-06-25 21:25:10 +0000358 snprintf(buf, sizeof(buf), "Your phone number is %s", subscr.msisdn);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700359 ss_tx_to_ms_ussd_7bit(ss, req->invoke_id, buf);
Harald Weltedab544e2018-07-29 16:14:48 +0200360 break;
361 case -ENOENT:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100362 ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_UNKNOWN_SUBSCRIBER);
Harald Weltedab544e2018-07-29 16:14:48 +0200363 break;
364 case -EIO:
365 default:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100366 ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE);
Harald Weltedab544e2018-07-29 16:14:48 +0200367 break;
368 }
369 return 0;
370}
371
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100372static int handle_ussd_own_imsi(struct ss_session *ss,
Harald Weltedab544e2018-07-29 16:14:48 +0200373 const struct osmo_gsup_message *gsup, const struct ss_request *req)
374{
375 char buf[GSM0480_USSD_7BIT_STRING_LEN+1];
Vadim Yanitskiy3adb33d2018-08-03 00:07:54 +0700376 snprintf(buf, sizeof(buf), "Your IMSI is %s", ss->imsi);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700377 ss->state = OSMO_GSUP_SESSION_STATE_END;
378 ss_tx_to_ms_ussd_7bit(ss, req->invoke_id, buf);
Harald Weltedab544e2018-07-29 16:14:48 +0200379 return 0;
380}
381
Vadim Yanitskiydac855e2020-11-17 04:17:46 +0700382/* This handler just keeps the session idle unless the guard timer expires. */
383static int handle_ussd_test_idle(struct ss_session *ss,
384 const struct osmo_gsup_message *gsup,
385 const struct ss_request *req)
386{
387 char buf[GSM0480_USSD_7BIT_STRING_LEN + 1];
388 snprintf(buf, sizeof(buf), "Keeping your session idle, it will expire "
389 "at most in %u seconds.", g_hlr->ncss_guard_timeout);
390 ss->state = OSMO_GSUP_SESSION_STATE_CONTINUE;
391 ss_tx_to_ms_ussd_7bit(ss, req->invoke_id, buf);
392 return 0;
393}
394
Harald Weltedab544e2018-07-29 16:14:48 +0200395
396static const struct hlr_iuse hlr_iuses[] = {
397 {
398 .name = "own-msisdn",
399 .handle_ussd = handle_ussd_own_msisdn,
400 },
401 {
402 .name = "own-imsi",
403 .handle_ussd = handle_ussd_own_imsi,
404 },
Vadim Yanitskiydac855e2020-11-17 04:17:46 +0700405 {
406 .name = "test-idle",
407 .handle_ussd = handle_ussd_test_idle,
408 },
Harald Weltedab544e2018-07-29 16:14:48 +0200409};
410
411const struct hlr_iuse *iuse_find(const char *name)
412{
413 unsigned int i;
414
415 for (i = 0; i < ARRAY_SIZE(hlr_iuses); i++) {
416 const struct hlr_iuse *iuse = &hlr_iuses[i];
417 if (!strcmp(name, iuse->name))
418 return iuse;
419 }
420 return NULL;
421}
Harald Welte72667312018-07-29 12:38:09 +0200422
423
424/***********************************************************************
Harald Weltebb779392018-06-16 20:21:10 +0200425 * handling functions for individual GSUP messages
426 ***********************************************************************/
427
428static bool ss_op_is_ussd(uint8_t opcode)
429{
430 switch (opcode) {
431 case GSM0480_OP_CODE_PROCESS_USS_DATA:
432 case GSM0480_OP_CODE_PROCESS_USS_REQ:
433 case GSM0480_OP_CODE_USS_REQUEST:
434 case GSM0480_OP_CODE_USS_NOTIFY:
435 return true;
436 default:
437 return false;
438 }
439}
440
441/* is this GSUP connection an EUSE (true) or not (false)? */
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100442static bool peer_name_is_euse(const struct osmo_cni_peer_id *peer_name)
Harald Weltebb779392018-06-16 20:21:10 +0200443{
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100444 if (peer_name->type != OSMO_CNI_PEER_ID_IPA_NAME)
Harald Weltebb779392018-06-16 20:21:10 +0200445 return false;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100446 if (peer_name->ipa_name.len <= 5)
Harald Weltebb779392018-06-16 20:21:10 +0200447 return false;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100448 return strncmp((char *)(peer_name->ipa_name.val), "EUSE-", 5) == 0;
Harald Weltebb779392018-06-16 20:21:10 +0200449}
450
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100451static struct hlr_euse *euse_by_name(const struct osmo_cni_peer_id *peer_name)
Harald Weltebb779392018-06-16 20:21:10 +0200452{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100453 if (!peer_name_is_euse(peer_name))
Harald Weltebb779392018-06-16 20:21:10 +0200454 return NULL;
455
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100456 /* above peer_name_is_euse() ensures this: */
457 OSMO_ASSERT(peer_name->type == OSMO_CNI_PEER_ID_IPA_NAME);
458
459 return euse_find(g_hlr, (const char*)(peer_name->ipa_name.val)+5);
Harald Weltebb779392018-06-16 20:21:10 +0200460}
461
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100462static int handle_ss(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup,
463 const struct ss_request *req)
Harald Weltebb779392018-06-16 20:21:10 +0200464{
465 uint8_t comp_type = gsup->ss_info[0];
466
Harald Welte7f32f5f2018-07-29 12:43:49 +0200467 LOGPSS(ss, LOGL_INFO, "SS CompType=%s, OpCode=%s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200468 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode));
Vadim Yanitskiy4a4bdcd2018-10-12 21:44:07 +0200469
470 /**
471 * FIXME: As we don't store any SS related information
472 * (e.g. call forwarding preferences) in the database,
473 * we don't handle "structured" SS requests at all.
474 */
475 LOGPSS(ss, LOGL_NOTICE, "Structured SS requests are not supported, rejecting...\n");
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100476 ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_FACILITY_NOT_SUPPORTED);
Vadim Yanitskiy4a4bdcd2018-10-12 21:44:07 +0200477 return -ENOTSUP;
Harald Weltebb779392018-06-16 20:21:10 +0200478}
479
Harald Weltedab544e2018-07-29 16:14:48 +0200480/* Handle a USSD GSUP message for a given SS Session received from VLR or EUSE */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100481static int handle_ussd(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup,
482 const struct ss_request *req)
Harald Weltebb779392018-06-16 20:21:10 +0200483{
484 uint8_t comp_type = gsup->ss_info[0];
485 struct msgb *msg_out;
Harald Weltebb779392018-06-16 20:21:10 +0200486
Harald Welte7f32f5f2018-07-29 12:43:49 +0200487 LOGPSS(ss, LOGL_INFO, "USSD CompType=%s, OpCode=%s '%s'\n",
Harald Weltebb779392018-06-16 20:21:10 +0200488 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode),
489 req->ussd_text);
490
Harald Weltedab544e2018-07-29 16:14:48 +0200491 if ((ss->is_external && !ss->u.euse) || !ss->u.iuse) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200492 LOGPSS(ss, LOGL_NOTICE, "USSD for unknown code '%s'\n", req->ussd_text);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100493 ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_SS_NOT_AVAILABLE);
Harald Weltebb779392018-06-16 20:21:10 +0200494 return 0;
495 }
496
497 if (is_euse_originated) {
498 /* Received from EUSE, Forward to VLR */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100499 /* Need a non-const osmo_gsup_message, because sending might modify some (routing related?) parts. */
500 struct osmo_gsup_message forward = *gsup;
501 ss_gsup_send_to_ms(ss, g_hlr->gs, &forward);
Harald Weltebb779392018-06-16 20:21:10 +0200502 } else {
Harald Weltedab544e2018-07-29 16:14:48 +0200503 /* Received from VLR (MS) */
504 if (ss->is_external) {
505 /* Forward to EUSE */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100506 struct osmo_ipa_name euse_name;
507 struct osmo_gsup_conn *conn;
508 osmo_ipa_name_set_str(&euse_name, "EUSE-%s", ss->u.euse->name);
509 conn = gsup_route_find_by_ipa_name(g_hlr->gs, &euse_name);
Harald Weltedab544e2018-07-29 16:14:48 +0200510 if (!conn) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100511 LOGPSS(ss, LOGL_ERROR, "Cannot find conn for EUSE %s\n",
512 osmo_ipa_name_to_str(&euse_name));
513 ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE);
Harald Weltedab544e2018-07-29 16:14:48 +0200514 } else {
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100515 msg_out = osmo_gsup_msgb_alloc("GSUP USSD FW");
Harald Weltedab544e2018-07-29 16:14:48 +0200516 osmo_gsup_encode(msg_out, gsup);
517 osmo_gsup_conn_send(conn, msg_out);
518 }
Harald Welte72667312018-07-29 12:38:09 +0200519 } else {
Harald Weltedab544e2018-07-29 16:14:48 +0200520 /* Handle internally */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100521 ss->u.iuse->handle_ussd(ss, gsup, req);
Vadim Yanitskiy6a6c7f82020-11-17 04:02:11 +0700522 /* Release session if the handler has changed its state to END */
523 if (ss->state == OSMO_GSUP_SESSION_STATE_END)
524 ss_session_free(ss);
Harald Weltebb779392018-06-16 20:21:10 +0200525 }
Harald Weltebb779392018-06-16 20:21:10 +0200526 }
527
528 return 0;
529}
530
531
532/* this function is called for any SS_REQ/SS_RESP messages from both the MSC/VLR side as well
533 * as from the EUSE side */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100534void rx_proc_ss_req(struct osmo_gsup_req *gsup_req)
Harald Weltebb779392018-06-16 20:21:10 +0200535{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100536 struct hlr *hlr = g_hlr;
Harald Weltebb779392018-06-16 20:21:10 +0200537 struct ss_session *ss;
538 struct ss_request req = {0};
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100539 const struct osmo_gsup_message *gsup = &gsup_req->gsup;
540 /* Remember whether this function should free the incoming gsup_req: if it is placed as ss->initial_req_from_*,
541 * do not free it here. If not, free it here. */
542 struct osmo_gsup_req *free_gsup_req = gsup_req;
543 bool is_euse_originated = peer_name_is_euse(&gsup_req->source_name);
Harald Weltebb779392018-06-16 20:21:10 +0200544
Harald Welte95b96d42018-07-29 12:47:39 +0200545 LOGP(DSS, LOGL_DEBUG, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id,
Harald Weltebb779392018-06-16 20:21:10 +0200546 osmo_gsup_session_state_name(gsup->session_state));
547
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100548 if (gsup_req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
549 LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to process SS request: Unsupported GSUP peer id type%s\n",
550 gsup->imsi, gsup->session_id,
551 osmo_cni_peer_id_type_name(gsup_req->source_name.type));
552 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_PROTO_ERR_UNSPEC, "error processing SS request");
553 return;
554 }
555
Harald Weltebb779392018-06-16 20:21:10 +0200556 /* decode and find out what kind of SS message it is */
557 if (gsup->ss_info && gsup->ss_info_len) {
558 if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
Harald Welte95b96d42018-07-29 12:47:39 +0200559 LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to parse SS request: %s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200560 gsup->imsi, gsup->session_id,
561 osmo_hexdump(gsup->ss_info, gsup->ss_info_len));
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100562 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "error parsing SS request");
563 return;
Harald Weltebb779392018-06-16 20:21:10 +0200564 }
Vadim Yanitskiy937f5832019-07-24 19:14:32 +0700565 } else if (gsup->session_state != OSMO_GSUP_SESSION_STATE_END) {
566 LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Missing SS payload for '%s'\n",
567 gsup->imsi, gsup->session_id,
568 osmo_gsup_session_state_name(gsup->session_state));
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100569 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "missing SS payload");
570 return;
Harald Weltebb779392018-06-16 20:21:10 +0200571 }
572
573 switch (gsup->session_state) {
574 case OSMO_GSUP_SESSION_STATE_BEGIN:
575 /* Check for overlapping Session ID usage */
576 if (ss_session_find(hlr, gsup->imsi, gsup->session_id)) {
Harald Welte95b96d42018-07-29 12:47:39 +0200577 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: BEGIN with non-unique session ID!\n",
Harald Weltebb779392018-06-16 20:21:10 +0200578 gsup->imsi, gsup->session_id);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100579 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "BEGIN with non-unique session ID");
580 return;
Harald Weltebb779392018-06-16 20:21:10 +0200581 }
582 ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id);
583 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200584 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unable to allocate SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200585 gsup->imsi, gsup->session_id);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100586 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_NET_FAIL, "Unable to allocate SS session");
587 return;
Harald Weltebb779392018-06-16 20:21:10 +0200588 }
Oliver Smith95abc2b2019-04-04 12:00:24 +0200589 /* Get IPA name from VLR conn and save as ss->vlr_number */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100590 if (!is_euse_originated) {
591 ss->initial_req_from_ms = gsup_req;
592 free_gsup_req = NULL;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100593 OSMO_ASSERT(gsup_req->source_name.type == OSMO_CNI_PEER_ID_IPA_NAME); /* checked above */
594 ss->vlr_name = gsup_req->source_name.ipa_name;
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100595 } else {
596 ss->initial_req_from_euse = gsup_req;
597 free_gsup_req = NULL;
Oliver Smith95abc2b2019-04-04 12:00:24 +0200598 }
Harald Weltebb779392018-06-16 20:21:10 +0200599 if (ss_op_is_ussd(req.opcode)) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100600 if (is_euse_originated) {
Harald Weltebb779392018-06-16 20:21:10 +0200601 /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100602 ss->u.euse = euse_by_name(&gsup_req->source_name);
Harald Weltebb779392018-06-16 20:21:10 +0200603 } else {
604 /* VLR->EUSE: MO USSD. VLR is known ('conn'), EUSE is to be resolved */
Harald Weltedab544e2018-07-29 16:14:48 +0200605 struct hlr_ussd_route *rt;
606 rt = ussd_route_lookup_7bit(hlr, (const char *) req.ussd_text);
607 if (rt) {
608 if (rt->is_external) {
609 ss->is_external = true;
610 ss->u.euse = rt->u.euse;
611 } else if (rt) {
612 ss->is_external = false;
613 ss->u.iuse = rt->u.iuse;
614 }
Harald Welte1eb98692018-08-08 08:57:26 +0200615 } else {
616 if (hlr->euse_default) {
617 ss->is_external = true;
618 ss->u.euse = hlr->euse_default;
619 }
Harald Weltedab544e2018-07-29 16:14:48 +0200620 }
Harald Weltebb779392018-06-16 20:21:10 +0200621 }
622 /* dispatch unstructured SS to routing */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100623 handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req);
Harald Weltebb779392018-06-16 20:21:10 +0200624 } else {
625 /* dispatch non-call SS to internal code */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100626 handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req);
Harald Weltebb779392018-06-16 20:21:10 +0200627 }
628 break;
629 case OSMO_GSUP_SESSION_STATE_CONTINUE:
630 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
631 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200632 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: CONTINUE for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200633 gsup->imsi, gsup->session_id);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100634 osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "CONTINUE for unknown SS session");
635 return;
Harald Weltebb779392018-06-16 20:21:10 +0200636 }
Vadim Yanitskiye6ce52b2018-12-01 00:16:44 +0700637
638 /* Reschedule self-destruction timer */
639 if (g_hlr->ncss_guard_timeout > 0)
640 osmo_timer_schedule(&ss->timeout, g_hlr->ncss_guard_timeout, 0);
641
Harald Weltebb779392018-06-16 20:21:10 +0200642 if (ss_op_is_ussd(req.opcode)) {
643 /* dispatch unstructured SS to routing */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100644 handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req);
Harald Weltebb779392018-06-16 20:21:10 +0200645 } else {
646 /* dispatch non-call SS to internal code */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100647 handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req);
Harald Weltebb779392018-06-16 20:21:10 +0200648 }
649 break;
650 case OSMO_GSUP_SESSION_STATE_END:
651 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
652 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200653 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: END for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200654 gsup->imsi, gsup->session_id);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100655 return;
Harald Weltebb779392018-06-16 20:21:10 +0200656 }
Vadim Yanitskiy937f5832019-07-24 19:14:32 +0700657
658 /* SS payload is optional for END */
659 if (gsup->ss_info && gsup->ss_info_len) {
660 if (ss_op_is_ussd(req.opcode)) {
661 /* dispatch unstructured SS to routing */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100662 handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req);
Vadim Yanitskiy937f5832019-07-24 19:14:32 +0700663 } else {
664 /* dispatch non-call SS to internal code */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100665 handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req);
Vadim Yanitskiy937f5832019-07-24 19:14:32 +0700666 }
Harald Weltebb779392018-06-16 20:21:10 +0200667 }
Vadim Yanitskiy937f5832019-07-24 19:14:32 +0700668
Harald Weltebb779392018-06-16 20:21:10 +0200669 ss_session_free(ss);
670 break;
671 default:
Harald Welte95b96d42018-07-29 12:47:39 +0200672 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unknown SS State %d\n", gsup->imsi,
Harald Welte7f32f5f2018-07-29 12:43:49 +0200673 gsup->session_id, gsup->session_state);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100674 break;
Harald Weltebb779392018-06-16 20:21:10 +0200675 }
676
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100677 if (free_gsup_req)
678 osmo_gsup_req_free(free_gsup_req);
Harald Weltebb779392018-06-16 20:21:10 +0200679}
680
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100681void rx_proc_ss_error(struct osmo_gsup_req *req)
Harald Weltebb779392018-06-16 20:21:10 +0200682{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100683 LOGP(DSS, LOGL_NOTICE, "%s/0x%08x: Process SS ERROR (%s)\n", req->gsup.imsi, req->gsup.session_id,
684 osmo_gsup_session_state_name(req->gsup.session_state));
Harald Weltebb779392018-06-16 20:21:10 +0200685}