blob: ea69cd923357fea758fde0172c89703444819846 [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
32#include "hlr.h"
33#include "hlr_ussd.h"
Harald Weltebb779392018-06-16 20:21:10 +020034#include "gsup_server.h"
35#include "gsup_router.h"
36#include "logging.h"
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +020037#include "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 */
153 char imsi[GSM23003_IMSI_MAX_DIGITS+2];
154 /* 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
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200170 /* subscriber's vlr_number, will be looked up once per session and cached here */
171 char vlr_number[32];
172
Harald Weltebb779392018-06-16 20:21:10 +0200173 /* we don't keep a pointer to the osmo_gsup_{route,conn} towards the MSC/VLR here,
174 * as this might change during inter-VLR hand-over, and we simply look-up the serving MSC/VLR
175 * every time we receive an USSD component from the EUSE */
176};
177
178struct ss_session *ss_session_find(struct hlr *hlr, const char *imsi, uint32_t session_id)
179{
180 struct ss_session *ss;
181 llist_for_each_entry(ss, &hlr->ss_sessions, list) {
182 if (!strcmp(ss->imsi, imsi) && ss->session_id == session_id)
183 return ss;
184 }
185 return NULL;
186}
187
188void ss_session_free(struct ss_session *ss)
189{
190 osmo_timer_del(&ss->timeout);
191 llist_del(&ss->list);
192 talloc_free(ss);
193}
194
195static void ss_session_timeout(void *data)
196{
197 struct ss_session *ss = data;
198
Harald Welte97bfb652018-07-29 12:28:11 +0200199 LOGPSS(ss, LOGL_NOTICE, "SS Session Timeout, destroying\n");
Harald Weltebb779392018-06-16 20:21:10 +0200200 /* FIXME: should we send a ReturnError component to the MS? */
201 ss_session_free(ss);
202}
203
204struct ss_session *ss_session_alloc(struct hlr *hlr, const char *imsi, uint32_t session_id)
205{
206 struct ss_session *ss;
207
208 OSMO_ASSERT(!ss_session_find(hlr, imsi, session_id));
209
210 ss = talloc_zero(hlr, struct ss_session);
211 OSMO_ASSERT(ss);
212
213 OSMO_STRLCPY_ARRAY(ss->imsi, imsi);
214 ss->session_id = session_id;
Vadim Yanitskiye6ce52b2018-12-01 00:16:44 +0700215
216 /* Schedule self-destruction timer */
Harald Weltebb779392018-06-16 20:21:10 +0200217 osmo_timer_setup(&ss->timeout, ss_session_timeout, ss);
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700218 if (g_hlr->ncss_guard_timeout > 0)
219 osmo_timer_schedule(&ss->timeout, g_hlr->ncss_guard_timeout, 0);
Harald Weltebb779392018-06-16 20:21:10 +0200220
221 llist_add_tail(&ss->list, &hlr->ss_sessions);
222 return ss;
223}
224
225/***********************************************************************
Harald Welte72667312018-07-29 12:38:09 +0200226 * handling functions for encoding SS messages + wrapping them in GSUP
227 ***********************************************************************/
228
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200229/* Resolve the target MSC by ss->imsi and send GSUP message. */
230static int ss_gsup_send(struct ss_session *ss, struct osmo_gsup_server *gs, struct msgb *msg)
231{
232 struct hlr_subscriber subscr = {};
233 int rc;
234
235 /* Use vlr_number as looked up by the caller, or look up now. */
236 if (!ss->vlr_number[0]) {
237 rc = db_subscr_get_by_imsi(g_hlr->dbc, ss->imsi, &subscr);
238 if (rc < 0) {
239 LOGPSS(ss, LOGL_ERROR, "Cannot find subscriber, cannot route GSUP message\n");
240 msgb_free(msg);
241 return -EINVAL;
242 }
243 osmo_strlcpy(ss->vlr_number, subscr.vlr_number, sizeof(ss->vlr_number));
244 }
245
246 if (!ss->vlr_number[0]) {
247 LOGPSS(ss, LOGL_ERROR, "Cannot send GSUP message, no VLR number stored for subscriber\n");
248 msgb_free(msg);
249 return -EINVAL;
250 }
251
252 LOGPSS(ss, LOGL_DEBUG, "Tx SS/USSD to VLR '%s'\n", ss->vlr_number);
253 return osmo_gsup_addr_send(gs, (uint8_t *)ss->vlr_number, strlen(ss->vlr_number) + 1, msg);
254}
255
Harald Welte72667312018-07-29 12:38:09 +0200256static int ss_tx_to_ms(struct ss_session *ss, enum osmo_gsup_message_type gsup_msg_type,
257 bool final, struct msgb *ss_msg)
258
259{
260 struct osmo_gsup_message resp = {0};
261 struct msgb *resp_msg;
262
263 resp.message_type = gsup_msg_type;
264 OSMO_STRLCPY_ARRAY(resp.imsi, ss->imsi);
265 if (final)
266 resp.session_state = OSMO_GSUP_SESSION_STATE_END;
267 else
268 resp.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
269 resp.session_id = ss->session_id;
270 if (ss_msg) {
271 resp.ss_info = msgb_data(ss_msg);
272 resp.ss_info_len = msgb_length(ss_msg);
273 }
274
275 resp_msg = gsm0480_msgb_alloc_name(__func__);
276 OSMO_ASSERT(resp_msg);
277 osmo_gsup_encode(resp_msg, &resp);
278 msgb_free(ss_msg);
279
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200280 return ss_gsup_send(ss, g_hlr->gs, resp_msg);
Harald Welte72667312018-07-29 12:38:09 +0200281}
282
283#if 0
284static int ss_tx_reject(struct ss_session *ss, int invoke_id, uint8_t problem_tag,
285 uint8_t problem_code)
286{
287 struct msgb *msg = gsm0480_gen_reject(invoke_id, problem_tag, problem_code);
288 LOGPSS(ss, LOGL_NOTICE, "Tx Reject(%u, 0x%02x, 0x%02x)\n", invoke_id,
289 problem_tag, problem_code);
290 OSMO_ASSERT(msg);
291 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg);
292}
293#endif
294
295static int ss_tx_error(struct ss_session *ss, uint8_t invoke_id, uint8_t error_code)
296{
297 struct msgb *msg = gsm0480_gen_return_error(invoke_id, error_code);
298 LOGPSS(ss, LOGL_NOTICE, "Tx ReturnError(%u, 0x%02x)\n", invoke_id, error_code);
299 OSMO_ASSERT(msg);
300 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg);
301}
302
Harald Weltedab544e2018-07-29 16:14:48 +0200303static int ss_tx_ussd_7bit(struct ss_session *ss, bool final, uint8_t invoke_id, const char *text)
304{
305 struct msgb *msg = gsm0480_gen_ussd_resp_7bit(invoke_id, text);
306 LOGPSS(ss, LOGL_INFO, "Tx USSD '%s'\n", text);
307 OSMO_ASSERT(msg);
308 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, final, msg);
309}
310
311/***********************************************************************
312 * Internal USSD Handlers
313 ***********************************************************************/
314
315#include "db.h"
316
317static int handle_ussd_own_msisdn(struct osmo_gsup_conn *conn, struct ss_session *ss,
318 const struct osmo_gsup_message *gsup, const struct ss_request *req)
319{
320 struct hlr_subscriber subscr;
321 char buf[GSM0480_USSD_7BIT_STRING_LEN+1];
322 int rc;
323
324 rc = db_subscr_get_by_imsi(g_hlr->dbc, ss->imsi, &subscr);
325 switch (rc) {
326 case 0:
327 if (strlen(subscr.msisdn) == 0)
328 snprintf(buf, sizeof(buf), "You have no MSISDN!");
329 else
Vadim Yanitskiy3adb33d2018-08-03 00:07:54 +0700330 snprintf(buf, sizeof(buf), "Your extension is %s", subscr.msisdn);
Harald Weltedab544e2018-07-29 16:14:48 +0200331 ss_tx_ussd_7bit(ss, true, req->invoke_id, buf);
332 break;
333 case -ENOENT:
334 ss_tx_error(ss, true, GSM0480_ERR_CODE_UNKNOWN_SUBSCRIBER);
335 break;
336 case -EIO:
337 default:
338 ss_tx_error(ss, true, GSM0480_ERR_CODE_SYSTEM_FAILURE);
339 break;
340 }
341 return 0;
342}
343
344static int handle_ussd_own_imsi(struct osmo_gsup_conn *conn, struct ss_session *ss,
345 const struct osmo_gsup_message *gsup, const struct ss_request *req)
346{
347 char buf[GSM0480_USSD_7BIT_STRING_LEN+1];
Vadim Yanitskiy3adb33d2018-08-03 00:07:54 +0700348 snprintf(buf, sizeof(buf), "Your IMSI is %s", ss->imsi);
Harald Weltedab544e2018-07-29 16:14:48 +0200349 ss_tx_ussd_7bit(ss, true, req->invoke_id, buf);
350 return 0;
351}
352
353
354static const struct hlr_iuse hlr_iuses[] = {
355 {
356 .name = "own-msisdn",
357 .handle_ussd = handle_ussd_own_msisdn,
358 },
359 {
360 .name = "own-imsi",
361 .handle_ussd = handle_ussd_own_imsi,
362 },
363};
364
365const struct hlr_iuse *iuse_find(const char *name)
366{
367 unsigned int i;
368
369 for (i = 0; i < ARRAY_SIZE(hlr_iuses); i++) {
370 const struct hlr_iuse *iuse = &hlr_iuses[i];
371 if (!strcmp(name, iuse->name))
372 return iuse;
373 }
374 return NULL;
375}
Harald Welte72667312018-07-29 12:38:09 +0200376
377
378/***********************************************************************
Harald Weltebb779392018-06-16 20:21:10 +0200379 * handling functions for individual GSUP messages
380 ***********************************************************************/
381
382static bool ss_op_is_ussd(uint8_t opcode)
383{
384 switch (opcode) {
385 case GSM0480_OP_CODE_PROCESS_USS_DATA:
386 case GSM0480_OP_CODE_PROCESS_USS_REQ:
387 case GSM0480_OP_CODE_USS_REQUEST:
388 case GSM0480_OP_CODE_USS_NOTIFY:
389 return true;
390 default:
391 return false;
392 }
393}
394
395/* is this GSUP connection an EUSE (true) or not (false)? */
396static bool conn_is_euse(struct osmo_gsup_conn *conn)
397{
398 int rc;
399 uint8_t *addr;
400
401 rc = osmo_gsup_conn_ccm_get(conn, &addr, IPAC_IDTAG_SERNR);
402 if (rc <= 5)
403 return false;
404 if (!strncmp((char *)addr, "EUSE-", 5))
405 return true;
406 else
407 return false;
408}
409
410static struct hlr_euse *euse_by_conn(struct osmo_gsup_conn *conn)
411{
412 int rc;
413 char *addr;
414 struct hlr *hlr = conn->server->priv;
415
416 rc = osmo_gsup_conn_ccm_get(conn, (uint8_t **) &addr, IPAC_IDTAG_SERNR);
417 if (rc <= 5)
418 return NULL;
419 if (strncmp(addr, "EUSE-", 5))
420 return NULL;
421
422 return euse_find(hlr, addr+5);
423}
424
425static int handle_ss(struct ss_session *ss, const struct osmo_gsup_message *gsup,
426 const struct ss_request *req)
427{
428 uint8_t comp_type = gsup->ss_info[0];
429
Harald Welte7f32f5f2018-07-29 12:43:49 +0200430 LOGPSS(ss, LOGL_INFO, "SS CompType=%s, OpCode=%s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200431 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode));
Vadim Yanitskiy4a4bdcd2018-10-12 21:44:07 +0200432
433 /**
434 * FIXME: As we don't store any SS related information
435 * (e.g. call forwarding preferences) in the database,
436 * we don't handle "structured" SS requests at all.
437 */
438 LOGPSS(ss, LOGL_NOTICE, "Structured SS requests are not supported, rejecting...\n");
439 ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_FACILITY_NOT_SUPPORTED);
440 return -ENOTSUP;
Harald Weltebb779392018-06-16 20:21:10 +0200441}
442
Harald Weltedab544e2018-07-29 16:14:48 +0200443/* Handle a USSD GSUP message for a given SS Session received from VLR or EUSE */
Harald Weltebb779392018-06-16 20:21:10 +0200444static int handle_ussd(struct osmo_gsup_conn *conn, struct ss_session *ss,
445 const struct osmo_gsup_message *gsup, const struct ss_request *req)
446{
447 uint8_t comp_type = gsup->ss_info[0];
448 struct msgb *msg_out;
449 bool is_euse_originated = conn_is_euse(conn);
450
Harald Welte7f32f5f2018-07-29 12:43:49 +0200451 LOGPSS(ss, LOGL_INFO, "USSD CompType=%s, OpCode=%s '%s'\n",
Harald Weltebb779392018-06-16 20:21:10 +0200452 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode),
453 req->ussd_text);
454
Harald Weltedab544e2018-07-29 16:14:48 +0200455 if ((ss->is_external && !ss->u.euse) || !ss->u.iuse) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200456 LOGPSS(ss, LOGL_NOTICE, "USSD for unknown code '%s'\n", req->ussd_text);
Harald Welte72667312018-07-29 12:38:09 +0200457 ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SS_NOT_AVAILABLE);
Harald Weltebb779392018-06-16 20:21:10 +0200458 return 0;
459 }
460
461 if (is_euse_originated) {
Harald Welte72667312018-07-29 12:38:09 +0200462 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW");
463 OSMO_ASSERT(msg_out);
Harald Weltebb779392018-06-16 20:21:10 +0200464 /* Received from EUSE, Forward to VLR */
465 osmo_gsup_encode(msg_out, gsup);
Neels Hofmeyrf1fe94c2019-04-02 04:26:55 +0200466 ss_gsup_send(ss, conn->server, msg_out);
Harald Weltebb779392018-06-16 20:21:10 +0200467 } else {
Harald Weltedab544e2018-07-29 16:14:48 +0200468 /* Received from VLR (MS) */
469 if (ss->is_external) {
470 /* Forward to EUSE */
471 char addr[128];
472 strcpy(addr, "EUSE-");
473 osmo_strlcpy(addr+5, ss->u.euse->name, sizeof(addr)-5);
474 conn = gsup_route_find(conn->server, (uint8_t *)addr, strlen(addr)+1);
475 if (!conn) {
476 LOGPSS(ss, LOGL_ERROR, "Cannot find conn for EUSE %s\n", addr);
477 ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE);
478 } else {
479 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW");
480 OSMO_ASSERT(msg_out);
481 osmo_gsup_encode(msg_out, gsup);
482 osmo_gsup_conn_send(conn, msg_out);
483 }
Harald Welte72667312018-07-29 12:38:09 +0200484 } else {
Harald Weltedab544e2018-07-29 16:14:48 +0200485 /* Handle internally */
486 ss->u.iuse->handle_ussd(conn, ss, gsup, req);
Vadim Yanitskiy9c8806a2018-11-30 08:11:19 +0700487 /* Release session immediately */
488 ss_session_free(ss);
Harald Weltebb779392018-06-16 20:21:10 +0200489 }
Harald Weltebb779392018-06-16 20:21:10 +0200490 }
491
492 return 0;
493}
494
495
496/* this function is called for any SS_REQ/SS_RESP messages from both the MSC/VLR side as well
497 * as from the EUSE side */
498int rx_proc_ss_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
499{
500 struct hlr *hlr = conn->server->priv;
501 struct ss_session *ss;
502 struct ss_request req = {0};
503
Harald Welte95b96d42018-07-29 12:47:39 +0200504 LOGP(DSS, LOGL_DEBUG, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id,
Harald Weltebb779392018-06-16 20:21:10 +0200505 osmo_gsup_session_state_name(gsup->session_state));
506
507 /* decode and find out what kind of SS message it is */
508 if (gsup->ss_info && gsup->ss_info_len) {
509 if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
Harald Welte95b96d42018-07-29 12:47:39 +0200510 LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to parse SS request: %s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200511 gsup->imsi, gsup->session_id,
512 osmo_hexdump(gsup->ss_info, gsup->ss_info_len));
Harald Welte72667312018-07-29 12:38:09 +0200513 /* FIXME: Send a Reject component? */
Harald Weltebb779392018-06-16 20:21:10 +0200514 goto out_err;
515 }
516 }
517
518 switch (gsup->session_state) {
519 case OSMO_GSUP_SESSION_STATE_BEGIN:
520 /* Check for overlapping Session ID usage */
521 if (ss_session_find(hlr, gsup->imsi, gsup->session_id)) {
Harald Welte95b96d42018-07-29 12:47:39 +0200522 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: BEGIN with non-unique session ID!\n",
Harald Weltebb779392018-06-16 20:21:10 +0200523 gsup->imsi, gsup->session_id);
524 goto out_err;
525 }
526 ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id);
527 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200528 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unable to allocate SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200529 gsup->imsi, gsup->session_id);
530 goto out_err;
531 }
532 if (ss_op_is_ussd(req.opcode)) {
533 if (conn_is_euse(conn)) {
534 /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */
Harald Weltedab544e2018-07-29 16:14:48 +0200535 ss->u.euse = euse_by_conn(conn);
Harald Weltebb779392018-06-16 20:21:10 +0200536 } else {
537 /* VLR->EUSE: MO USSD. VLR is known ('conn'), EUSE is to be resolved */
Harald Weltedab544e2018-07-29 16:14:48 +0200538 struct hlr_ussd_route *rt;
539 rt = ussd_route_lookup_7bit(hlr, (const char *) req.ussd_text);
540 if (rt) {
541 if (rt->is_external) {
542 ss->is_external = true;
543 ss->u.euse = rt->u.euse;
544 } else if (rt) {
545 ss->is_external = false;
546 ss->u.iuse = rt->u.iuse;
547 }
Harald Welte1eb98692018-08-08 08:57:26 +0200548 } else {
549 if (hlr->euse_default) {
550 ss->is_external = true;
551 ss->u.euse = hlr->euse_default;
552 }
Harald Weltedab544e2018-07-29 16:14:48 +0200553 }
Harald Weltebb779392018-06-16 20:21:10 +0200554 }
555 /* dispatch unstructured SS to routing */
556 handle_ussd(conn, ss, gsup, &req);
557 } else {
558 /* dispatch non-call SS to internal code */
559 handle_ss(ss, gsup, &req);
560 }
561 break;
562 case OSMO_GSUP_SESSION_STATE_CONTINUE:
563 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
564 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200565 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: CONTINUE for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200566 gsup->imsi, gsup->session_id);
567 goto out_err;
568 }
Vadim Yanitskiye6ce52b2018-12-01 00:16:44 +0700569
570 /* Reschedule self-destruction timer */
571 if (g_hlr->ncss_guard_timeout > 0)
572 osmo_timer_schedule(&ss->timeout, g_hlr->ncss_guard_timeout, 0);
573
Harald Weltebb779392018-06-16 20:21:10 +0200574 if (ss_op_is_ussd(req.opcode)) {
575 /* dispatch unstructured SS to routing */
576 handle_ussd(conn, ss, gsup, &req);
577 } else {
578 /* dispatch non-call SS to internal code */
579 handle_ss(ss, gsup, &req);
580 }
581 break;
582 case OSMO_GSUP_SESSION_STATE_END:
583 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
584 if (!ss) {
Harald Welte95b96d42018-07-29 12:47:39 +0200585 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: END for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200586 gsup->imsi, gsup->session_id);
587 goto out_err;
588 }
589 if (ss_op_is_ussd(req.opcode)) {
590 /* dispatch unstructured SS to routing */
591 handle_ussd(conn, ss, gsup, &req);
592 } else {
593 /* dispatch non-call SS to internal code */
594 handle_ss(ss, gsup, &req);
595 }
596 ss_session_free(ss);
597 break;
598 default:
Harald Welte95b96d42018-07-29 12:47:39 +0200599 LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unknown SS State %d\n", gsup->imsi,
Harald Welte7f32f5f2018-07-29 12:43:49 +0200600 gsup->session_id, gsup->session_state);
Harald Weltebb779392018-06-16 20:21:10 +0200601 goto out_err;
602 }
603
604 return 0;
605
606out_err:
607 return 0;
608}
609
610int rx_proc_ss_error(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
611{
Harald Welte95b96d42018-07-29 12:47:39 +0200612 LOGP(DSS, LOGL_NOTICE, "%s/0x%08x: Process SS ERROR (%s)\n", gsup->imsi, gsup->session_id,
Harald Weltebb779392018-06-16 20:21:10 +0200613 osmo_gsup_session_state_name(gsup->session_state));
614 return 0;
615}