blob: ef4063c49bea5f40ed42ef3efeee1bfd8d3418f9 [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/***********************************************************************
199 * handling functions for individual GSUP messages
200 ***********************************************************************/
201
202static bool ss_op_is_ussd(uint8_t opcode)
203{
204 switch (opcode) {
205 case GSM0480_OP_CODE_PROCESS_USS_DATA:
206 case GSM0480_OP_CODE_PROCESS_USS_REQ:
207 case GSM0480_OP_CODE_USS_REQUEST:
208 case GSM0480_OP_CODE_USS_NOTIFY:
209 return true;
210 default:
211 return false;
212 }
213}
214
215/* is this GSUP connection an EUSE (true) or not (false)? */
216static bool conn_is_euse(struct osmo_gsup_conn *conn)
217{
218 int rc;
219 uint8_t *addr;
220
221 rc = osmo_gsup_conn_ccm_get(conn, &addr, IPAC_IDTAG_SERNR);
222 if (rc <= 5)
223 return false;
224 if (!strncmp((char *)addr, "EUSE-", 5))
225 return true;
226 else
227 return false;
228}
229
230static struct hlr_euse *euse_by_conn(struct osmo_gsup_conn *conn)
231{
232 int rc;
233 char *addr;
234 struct hlr *hlr = conn->server->priv;
235
236 rc = osmo_gsup_conn_ccm_get(conn, (uint8_t **) &addr, IPAC_IDTAG_SERNR);
237 if (rc <= 5)
238 return NULL;
239 if (strncmp(addr, "EUSE-", 5))
240 return NULL;
241
242 return euse_find(hlr, addr+5);
243}
244
245static int handle_ss(struct ss_session *ss, const struct osmo_gsup_message *gsup,
246 const struct ss_request *req)
247{
248 uint8_t comp_type = gsup->ss_info[0];
249
250 LOGP(DMAIN, LOGL_INFO, "%s: SS CompType=%s, OpCode=%s\n", gsup->imsi,
251 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode));
252 /* FIXME */
253 return 0;
254}
255
256static int handle_ussd(struct osmo_gsup_conn *conn, struct ss_session *ss,
257 const struct osmo_gsup_message *gsup, const struct ss_request *req)
258{
259 uint8_t comp_type = gsup->ss_info[0];
260 struct msgb *msg_out;
261 bool is_euse_originated = conn_is_euse(conn);
262
263 LOGP(DMAIN, LOGL_INFO, "%s: USSD CompType=%s, OpCode=%s '%s'\n", gsup->imsi,
264 gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode),
265 req->ussd_text);
266
267 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW");
268 OSMO_ASSERT(msg_out);
269
270 if (!ss->euse) {
271 LOGP(DMAIN, LOGL_NOTICE, "%s: USSD for unknown code '%s'\n", gsup->imsi, req->ussd_text);
272 /* FIXME: send proper error */
273 return 0;
274 }
275
276 if (is_euse_originated) {
277 /* Received from EUSE, Forward to VLR */
278 osmo_gsup_encode(msg_out, gsup);
279 /* FIXME: resolve this based on the database vlr_addr */
280 osmo_gsup_addr_send(conn->server, (uint8_t *)"MSC-00-00-00-00-00-00", 22, msg_out);
281 } else {
282 /* Received from VLR, Forward to EUSE */
283 char addr[128];
284 strcpy(addr, "EUSE-");
285 osmo_strlcpy(addr+5, ss->euse->name, sizeof(addr)-5);
286 conn = gsup_route_find(conn->server, (uint8_t *)addr, strlen(addr)+1);
287 if (!conn) {
288 LOGP(DMAIN, LOGL_ERROR, "Cannot find conn for EUSE %s\n", addr);
289 /* FIXME: send proper error */
290 return -1;
291 }
292 osmo_gsup_encode(msg_out, gsup);
293 osmo_gsup_conn_send(conn, msg_out);
294 }
295
296 return 0;
297}
298
299
300/* this function is called for any SS_REQ/SS_RESP messages from both the MSC/VLR side as well
301 * as from the EUSE side */
302int rx_proc_ss_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
303{
304 struct hlr *hlr = conn->server->priv;
305 struct ss_session *ss;
306 struct ss_request req = {0};
307
308 LOGP(DMAIN, LOGL_INFO, "%s: Process SS (0x%08x, %s)\n", gsup->imsi, gsup->session_id,
309 osmo_gsup_session_state_name(gsup->session_state));
310
311 /* decode and find out what kind of SS message it is */
312 if (gsup->ss_info && gsup->ss_info_len) {
313 if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
314 LOGP(DMAIN, LOGL_ERROR, "%s: Unable to parse SS request for 0x%08x: %s\n",
315 gsup->imsi, gsup->session_id,
316 osmo_hexdump(gsup->ss_info, gsup->ss_info_len));
317 goto out_err;
318 }
319 }
320
321 switch (gsup->session_state) {
322 case OSMO_GSUP_SESSION_STATE_BEGIN:
323 /* Check for overlapping Session ID usage */
324 if (ss_session_find(hlr, gsup->imsi, gsup->session_id)) {
325 LOGP(DMAIN, LOGL_ERROR, "%s/0x%08x: BEGIN with non-uinque session ID!\n",
326 gsup->imsi, gsup->session_id);
327 goto out_err;
328 }
329 ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id);
330 if (!ss) {
331 LOGP(DMAIN, LOGL_ERROR, "%s: Unable to allocate SS session for 0x%08x\n",
332 gsup->imsi, gsup->session_id);
333 goto out_err;
334 }
335 if (ss_op_is_ussd(req.opcode)) {
336 if (conn_is_euse(conn)) {
337 /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */
338 ss->euse = euse_by_conn(conn);
339 } else {
340 /* VLR->EUSE: MO USSD. VLR is known ('conn'), EUSE is to be resolved */
341 ss->euse = ussd_euse_find_7bit_gsm(hlr, (const char *) req.ussd_text);
342 }
343 /* dispatch unstructured SS to routing */
344 handle_ussd(conn, ss, gsup, &req);
345 } else {
346 /* dispatch non-call SS to internal code */
347 handle_ss(ss, gsup, &req);
348 }
349 break;
350 case OSMO_GSUP_SESSION_STATE_CONTINUE:
351 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
352 if (!ss) {
353 LOGP(DMAIN, LOGL_ERROR, "%s: CONTINUE for unknwon SS session 0x%08x\n",
354 gsup->imsi, gsup->session_id);
355 goto out_err;
356 }
357 if (ss_op_is_ussd(req.opcode)) {
358 /* dispatch unstructured SS to routing */
359 handle_ussd(conn, ss, gsup, &req);
360 } else {
361 /* dispatch non-call SS to internal code */
362 handle_ss(ss, gsup, &req);
363 }
364 break;
365 case OSMO_GSUP_SESSION_STATE_END:
366 ss = ss_session_find(hlr, gsup->imsi, gsup->session_id);
367 if (!ss) {
368 LOGP(DMAIN, LOGL_ERROR, "%s: END for unknwon SS session 0x%08x\n",
369 gsup->imsi, gsup->session_id);
370 goto out_err;
371 }
372 if (ss_op_is_ussd(req.opcode)) {
373 /* dispatch unstructured SS to routing */
374 handle_ussd(conn, ss, gsup, &req);
375 } else {
376 /* dispatch non-call SS to internal code */
377 handle_ss(ss, gsup, &req);
378 }
379 ss_session_free(ss);
380 break;
381 default:
382 LOGP(DMAIN, LOGL_ERROR, "%s: Unknown SS State %d\n", gsup->imsi, gsup->session_state);
383 goto out_err;
384 }
385
386 return 0;
387
388out_err:
389 return 0;
390}
391
392int rx_proc_ss_error(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
393{
394 LOGP(DMAIN, LOGL_NOTICE, "%s: Process SS ERROR (0x%08x, %s)\n", gsup->imsi, gsup->session_id,
395 osmo_gsup_session_state_name(gsup->session_state));
396 return 0;
397}