blob: 8710061f477bf73acabf679f485a8a028ef570f3 [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>
30
31#include "hlr.h"
32#include "hlr_ussd.h"
Harald Weltebb779392018-06-16 20:21:10 +020033#include "gsup_server.h"
34#include "gsup_router.h"
35#include "logging.h"
36
37/***********************************************************************
38 * core data structures expressing config from VTY
39 ***********************************************************************/
Harald Welte4956ae12018-06-15 22:04:28 +020040
41struct hlr_euse *euse_find(struct hlr *hlr, const char *name)
42{
43 struct hlr_euse *euse;
44
45 llist_for_each_entry(euse, &hlr->euse_list, list) {
46 if (!strcmp(euse->name, name))
47 return euse;
48 }
49 return NULL;
50}
51
52struct hlr_euse *euse_alloc(struct hlr *hlr, const char *name)
53{
54 struct hlr_euse *euse = euse_find(hlr, name);
55 if (euse)
56 return NULL;
57
58 euse = talloc_zero(hlr, struct hlr_euse);
59 euse->name = talloc_strdup(euse, name);
60 euse->hlr = hlr;
61 INIT_LLIST_HEAD(&euse->routes);
62 llist_add_tail(&euse->list, &hlr->euse_list);
63
64 return euse;
65}
66
67void euse_del(struct hlr_euse *euse)
68{
69 llist_del(&euse->list);
70 talloc_free(euse);
71}
72
73
74struct hlr_euse_route *euse_route_find(struct hlr_euse *euse, const char *prefix)
75{
76 struct hlr_euse_route *rt;
77
78 llist_for_each_entry(rt, &euse->routes, list) {
79 if (!strcmp(rt->prefix, prefix))
80 return rt;
81 }
82 return NULL;
83}
84
85struct hlr_euse_route *euse_route_prefix_alloc(struct hlr_euse *euse, const char *prefix)
86{
87 struct hlr_euse_route *rt;
88
89 if (euse_route_find(euse, prefix))
90 return NULL;
91
92 rt = talloc_zero(euse, struct hlr_euse_route);
93 rt->prefix = talloc_strdup(rt, prefix);
94 rt->euse = euse;
95 llist_add_tail(&rt->list, &euse->routes);
96
97 return rt;
98}
99
100void euse_route_del(struct hlr_euse_route *rt)
101{
102 llist_del(&rt->list);
103 talloc_free(rt);
104}
Harald Weltebb779392018-06-16 20:21:10 +0200105
106struct hlr_euse *ussd_euse_find_7bit_gsm(struct hlr *hlr, const char *ussd_code)
107{
108 struct hlr_euse *euse;
109
110 llist_for_each_entry(euse, &hlr->euse_list, list) {
111 struct hlr_euse_route *rt;
112 llist_for_each_entry(rt, &euse->routes, list) {
113 if (!strncmp(ussd_code, rt->prefix, strlen(rt->prefix))) {
114 LOGP(DMAIN, LOGL_DEBUG, "Found EUSE %s (prefix %s) for USSD Code '%s'\n",
115 rt->euse->name, rt->prefix, ussd_code);
116 return rt->euse;
117 }
118 }
119 }
120
121 LOGP(DMAIN, LOGL_DEBUG, "Could not find Route/EUSE for USSD Code '%s'\n", ussd_code);
122 return NULL;
123}
124
125/***********************************************************************
126 * handling functions for individual GSUP messages
127 ***********************************************************************/
128
Harald Welte97bfb652018-07-29 12:28:11 +0200129#define LOGPSS(ss, lvl, fmt, args...) \
130 LOGP(DMAIN, lvl, "%s/0x%08x: " fmt, (ss)->imsi, (ss)->session_id, ## args)
131
Harald Weltebb779392018-06-16 20:21:10 +0200132struct ss_session {
133 /* link us to hlr->ss_sessions */
134 struct llist_head list;
135 /* imsi of this session */
136 char imsi[GSM23003_IMSI_MAX_DIGITS+2];
137 /* ID of this session (unique per IMSI) */
138 uint32_t session_id;
139 /* state of the session */
140 enum osmo_gsup_session_state state;
141 /* time-out when we will delete the session */
142 struct osmo_timer_list timeout;
143
144 /* external USSD Entity responsible for this session */
145 struct hlr_euse *euse;
146 /* we don't keep a pointer to the osmo_gsup_{route,conn} towards the MSC/VLR here,
147 * as this might change during inter-VLR hand-over, and we simply look-up the serving MSC/VLR
148 * every time we receive an USSD component from the EUSE */
149};
150
151struct ss_session *ss_session_find(struct hlr *hlr, const char *imsi, uint32_t session_id)
152{
153 struct ss_session *ss;
154 llist_for_each_entry(ss, &hlr->ss_sessions, list) {
155 if (!strcmp(ss->imsi, imsi) && ss->session_id == session_id)
156 return ss;
157 }
158 return NULL;
159}
160
161void ss_session_free(struct ss_session *ss)
162{
163 osmo_timer_del(&ss->timeout);
164 llist_del(&ss->list);
165 talloc_free(ss);
166}
167
168static void ss_session_timeout(void *data)
169{
170 struct ss_session *ss = data;
171
Harald Welte97bfb652018-07-29 12:28:11 +0200172 LOGPSS(ss, LOGL_NOTICE, "SS Session Timeout, destroying\n");
Harald Weltebb779392018-06-16 20:21:10 +0200173 /* FIXME: should we send a ReturnError component to the MS? */
174 ss_session_free(ss);
175}
176
177struct ss_session *ss_session_alloc(struct hlr *hlr, const char *imsi, uint32_t session_id)
178{
179 struct ss_session *ss;
180
181 OSMO_ASSERT(!ss_session_find(hlr, imsi, session_id));
182
183 ss = talloc_zero(hlr, struct ss_session);
184 OSMO_ASSERT(ss);
185
186 OSMO_STRLCPY_ARRAY(ss->imsi, imsi);
187 ss->session_id = session_id;
188 osmo_timer_setup(&ss->timeout, ss_session_timeout, ss);
189 /* NOTE: The timeout is currently global and not refreshed with subsequent messages
190 * within the SS/USSD session. So 30s after the initial SS message, the session will
191 * timeout! */
192 osmo_timer_schedule(&ss->timeout, 30, 0);
193
194 llist_add_tail(&ss->list, &hlr->ss_sessions);
195 return ss;
196}
197
198/***********************************************************************
Harald Welte72667312018-07-29 12:38:09 +0200199 * handling functions for encoding SS messages + wrapping them in GSUP
200 ***********************************************************************/
201
202static int ss_tx_to_ms(struct ss_session *ss, enum osmo_gsup_message_type gsup_msg_type,
203 bool final, struct msgb *ss_msg)
204
205{
206 struct osmo_gsup_message resp = {0};
207 struct msgb *resp_msg;
208
209 resp.message_type = gsup_msg_type;
210 OSMO_STRLCPY_ARRAY(resp.imsi, ss->imsi);
211 if (final)
212 resp.session_state = OSMO_GSUP_SESSION_STATE_END;
213 else
214 resp.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
215 resp.session_id = ss->session_id;
216 if (ss_msg) {
217 resp.ss_info = msgb_data(ss_msg);
218 resp.ss_info_len = msgb_length(ss_msg);
219 }
220
221 resp_msg = gsm0480_msgb_alloc_name(__func__);
222 OSMO_ASSERT(resp_msg);
223 osmo_gsup_encode(resp_msg, &resp);
224 msgb_free(ss_msg);
225
226 /* FIXME: resolve this based on the database vlr_addr */
227 return osmo_gsup_addr_send(g_hlr->gs, (uint8_t *)"MSC-00-00-00-00-00-00", 22, resp_msg);
228}
229
230#if 0
231static int ss_tx_reject(struct ss_session *ss, int invoke_id, uint8_t problem_tag,
232 uint8_t problem_code)
233{
234 struct msgb *msg = gsm0480_gen_reject(invoke_id, problem_tag, problem_code);
235 LOGPSS(ss, LOGL_NOTICE, "Tx Reject(%u, 0x%02x, 0x%02x)\n", invoke_id,
236 problem_tag, problem_code);
237 OSMO_ASSERT(msg);
238 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg);
239}
240#endif
241
242static int ss_tx_error(struct ss_session *ss, uint8_t invoke_id, uint8_t error_code)
243{
244 struct msgb *msg = gsm0480_gen_return_error(invoke_id, error_code);
245 LOGPSS(ss, LOGL_NOTICE, "Tx ReturnError(%u, 0x%02x)\n", invoke_id, error_code);
246 OSMO_ASSERT(msg);
247 return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg);
248}
249
250
251
252/***********************************************************************
Harald Weltebb779392018-06-16 20:21:10 +0200253 * handling functions for individual GSUP messages
254 ***********************************************************************/
255
256static bool ss_op_is_ussd(uint8_t opcode)
257{
258 switch (opcode) {
259 case GSM0480_OP_CODE_PROCESS_USS_DATA:
260 case GSM0480_OP_CODE_PROCESS_USS_REQ:
261 case GSM0480_OP_CODE_USS_REQUEST:
262 case GSM0480_OP_CODE_USS_NOTIFY:
263 return true;
264 default:
265 return false;
266 }
267}
268
269/* is this GSUP connection an EUSE (true) or not (false)? */
270static bool conn_is_euse(struct osmo_gsup_conn *conn)
271{
272 int rc;
273 uint8_t *addr;
274
275 rc = osmo_gsup_conn_ccm_get(conn, &addr, IPAC_IDTAG_SERNR);
276 if (rc <= 5)
277 return false;
278 if (!strncmp((char *)addr, "EUSE-", 5))
279 return true;
280 else
281 return false;
282}
283
284static struct hlr_euse *euse_by_conn(struct osmo_gsup_conn *conn)
285{
286 int rc;
287 char *addr;
288 struct hlr *hlr = conn->server->priv;
289
290 rc = osmo_gsup_conn_ccm_get(conn, (uint8_t **) &addr, IPAC_IDTAG_SERNR);
291 if (rc <= 5)
292 return NULL;
293 if (strncmp(addr, "EUSE-", 5))
294 return NULL;
295
296 return euse_find(hlr, addr+5);
297}
298
299static int handle_ss(struct ss_session *ss, const struct osmo_gsup_message *gsup,
300 const struct ss_request *req)
301{
302 uint8_t comp_type = gsup->ss_info[0];
303
Harald Welte7f32f5f2018-07-29 12:43:49 +0200304 LOGPSS(ss, LOGL_INFO, "SS CompType=%s, OpCode=%s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200305 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode));
306 /* FIXME */
307 return 0;
308}
309
310static int handle_ussd(struct osmo_gsup_conn *conn, struct ss_session *ss,
311 const struct osmo_gsup_message *gsup, const struct ss_request *req)
312{
313 uint8_t comp_type = gsup->ss_info[0];
314 struct msgb *msg_out;
315 bool is_euse_originated = conn_is_euse(conn);
316
Harald Welte7f32f5f2018-07-29 12:43:49 +0200317 LOGPSS(ss, LOGL_INFO, "USSD CompType=%s, OpCode=%s '%s'\n",
Harald Weltebb779392018-06-16 20:21:10 +0200318 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode),
319 req->ussd_text);
320
Harald Weltebb779392018-06-16 20:21:10 +0200321
322 if (!ss->euse) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200323 LOGPSS(ss, LOGL_NOTICE, "USSD for unknown code '%s'\n", req->ussd_text);
Harald Welte72667312018-07-29 12:38:09 +0200324 ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SS_NOT_AVAILABLE);
Harald Weltebb779392018-06-16 20:21:10 +0200325 return 0;
326 }
327
328 if (is_euse_originated) {
Harald Welte72667312018-07-29 12:38:09 +0200329 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW");
330 OSMO_ASSERT(msg_out);
Harald Weltebb779392018-06-16 20:21:10 +0200331 /* Received from EUSE, Forward to VLR */
332 osmo_gsup_encode(msg_out, gsup);
333 /* FIXME: resolve this based on the database vlr_addr */
334 osmo_gsup_addr_send(conn->server, (uint8_t *)"MSC-00-00-00-00-00-00", 22, msg_out);
335 } else {
336 /* Received from VLR, Forward to EUSE */
337 char addr[128];
338 strcpy(addr, "EUSE-");
339 osmo_strlcpy(addr+5, ss->euse->name, sizeof(addr)-5);
340 conn = gsup_route_find(conn->server, (uint8_t *)addr, strlen(addr)+1);
341 if (!conn) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200342 LOGPSS(ss, LOGL_ERROR, "Cannot find conn for EUSE %s\n", addr);
Harald Welte72667312018-07-29 12:38:09 +0200343 ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE);
344 } else {
345 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW");
346 OSMO_ASSERT(msg_out);
347 osmo_gsup_encode(msg_out, gsup);
348 osmo_gsup_conn_send(conn, msg_out);
Harald Weltebb779392018-06-16 20:21:10 +0200349 }
Harald Weltebb779392018-06-16 20:21:10 +0200350 }
351
352 return 0;
353}
354
355
356/* this function is called for any SS_REQ/SS_RESP messages from both the MSC/VLR side as well
357 * as from the EUSE side */
358int rx_proc_ss_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
359{
360 struct hlr *hlr = conn->server->priv;
361 struct ss_session *ss;
362 struct ss_request req = {0};
363
Harald Welte7f32f5f2018-07-29 12:43:49 +0200364 LOGP(DMAIN, LOGL_INFO, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id,
Harald Weltebb779392018-06-16 20:21:10 +0200365 osmo_gsup_session_state_name(gsup->session_state));
366
367 /* decode and find out what kind of SS message it is */
368 if (gsup->ss_info && gsup->ss_info_len) {
369 if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200370 LOGP(DMAIN, LOGL_ERROR, "%s/0x%082x: Unable to parse SS request: %s\n",
Harald Weltebb779392018-06-16 20:21:10 +0200371 gsup->imsi, gsup->session_id,
372 osmo_hexdump(gsup->ss_info, gsup->ss_info_len));
Harald Welte72667312018-07-29 12:38:09 +0200373 /* FIXME: Send a Reject component? */
Harald Weltebb779392018-06-16 20:21:10 +0200374 goto out_err;
375 }
376 }
377
378 switch (gsup->session_state) {
379 case OSMO_GSUP_SESSION_STATE_BEGIN:
380 /* Check for overlapping Session ID usage */
381 if (ss_session_find(hlr, gsup->imsi, gsup->session_id)) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200382 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: BEGIN with non-unique session ID!\n",
Harald Weltebb779392018-06-16 20:21:10 +0200383 gsup->imsi, gsup->session_id);
384 goto out_err;
385 }
386 ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id);
387 if (!ss) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200388 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: Unable to allocate SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200389 gsup->imsi, gsup->session_id);
390 goto out_err;
391 }
392 if (ss_op_is_ussd(req.opcode)) {
393 if (conn_is_euse(conn)) {
394 /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */
395 ss->euse = euse_by_conn(conn);
396 } else {
397 /* VLR->EUSE: MO USSD. VLR is known ('conn'), EUSE is to be resolved */
398 ss->euse = ussd_euse_find_7bit_gsm(hlr, (const char *) req.ussd_text);
399 }
400 /* dispatch unstructured SS to routing */
401 handle_ussd(conn, ss, gsup, &req);
402 } else {
403 /* dispatch non-call SS to internal code */
404 handle_ss(ss, gsup, &req);
405 }
406 break;
407 case OSMO_GSUP_SESSION_STATE_CONTINUE:
408 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
409 if (!ss) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200410 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: CONTINUE for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200411 gsup->imsi, gsup->session_id);
412 goto out_err;
413 }
414 if (ss_op_is_ussd(req.opcode)) {
415 /* dispatch unstructured SS to routing */
416 handle_ussd(conn, ss, gsup, &req);
417 } else {
418 /* dispatch non-call SS to internal code */
419 handle_ss(ss, gsup, &req);
420 }
421 break;
422 case OSMO_GSUP_SESSION_STATE_END:
423 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
424 if (!ss) {
Harald Welte7f32f5f2018-07-29 12:43:49 +0200425 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: END for unknown SS session\n",
Harald Weltebb779392018-06-16 20:21:10 +0200426 gsup->imsi, gsup->session_id);
427 goto out_err;
428 }
429 if (ss_op_is_ussd(req.opcode)) {
430 /* dispatch unstructured SS to routing */
431 handle_ussd(conn, ss, gsup, &req);
432 } else {
433 /* dispatch non-call SS to internal code */
434 handle_ss(ss, gsup, &req);
435 }
436 ss_session_free(ss);
437 break;
438 default:
Harald Welte7f32f5f2018-07-29 12:43:49 +0200439 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: Unknown SS State %d\n", gsup->imsi,
440 gsup->session_id, gsup->session_state);
Harald Weltebb779392018-06-16 20:21:10 +0200441 goto out_err;
442 }
443
444 return 0;
445
446out_err:
447 return 0;
448}
449
450int rx_proc_ss_error(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
451{
Harald Welte7f32f5f2018-07-29 12:43:49 +0200452 LOGP(DMAIN, LOGL_NOTICE, "%s/0x%08x: Process SS ERROR (%s)\n", gsup->imsi, gsup->session_id,
Harald Weltebb779392018-06-16 20:21:10 +0200453 osmo_gsup_session_state_name(gsup->session_state));
454 return 0;
455}