Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 1 | /* OsmoHLR SS/USSD implementation */ |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 2 | |
| 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 24 | #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 Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <string.h> |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 30 | #include <errno.h> |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 31 | |
| 32 | #include "hlr.h" |
| 33 | #include "hlr_ussd.h" |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 34 | #include "gsup_server.h" |
| 35 | #include "gsup_router.h" |
| 36 | #include "logging.h" |
| 37 | |
| 38 | /*********************************************************************** |
| 39 | * core data structures expressing config from VTY |
| 40 | ***********************************************************************/ |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 41 | |
| 42 | struct hlr_euse *euse_find(struct hlr *hlr, const char *name) |
| 43 | { |
| 44 | struct hlr_euse *euse; |
| 45 | |
| 46 | llist_for_each_entry(euse, &hlr->euse_list, list) { |
| 47 | if (!strcmp(euse->name, name)) |
| 48 | return euse; |
| 49 | } |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | struct hlr_euse *euse_alloc(struct hlr *hlr, const char *name) |
| 54 | { |
| 55 | struct hlr_euse *euse = euse_find(hlr, name); |
| 56 | if (euse) |
| 57 | return NULL; |
| 58 | |
| 59 | euse = talloc_zero(hlr, struct hlr_euse); |
| 60 | euse->name = talloc_strdup(euse, name); |
| 61 | euse->hlr = hlr; |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 62 | llist_add_tail(&euse->list, &hlr->euse_list); |
| 63 | |
| 64 | return euse; |
| 65 | } |
| 66 | |
| 67 | void euse_del(struct hlr_euse *euse) |
| 68 | { |
| 69 | llist_del(&euse->list); |
| 70 | talloc_free(euse); |
| 71 | } |
| 72 | |
| 73 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 74 | struct hlr_ussd_route *ussd_route_find_prefix(struct hlr *hlr, const char *prefix) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 75 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 76 | struct hlr_ussd_route *rt; |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 77 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 78 | llist_for_each_entry(rt, &hlr->ussd_routes, list) { |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 79 | if (!strcmp(rt->prefix, prefix)) |
| 80 | return rt; |
| 81 | } |
| 82 | return NULL; |
| 83 | } |
| 84 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 85 | struct hlr_ussd_route *ussd_route_prefix_alloc_int(struct hlr *hlr, const char *prefix, |
| 86 | const struct hlr_iuse *iuse) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 87 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 88 | struct hlr_ussd_route *rt; |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 89 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 90 | if (ussd_route_find_prefix(hlr, prefix)) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 91 | return NULL; |
| 92 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 93 | rt = talloc_zero(hlr, struct hlr_ussd_route); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 94 | rt->prefix = talloc_strdup(rt, prefix); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 95 | rt->u.iuse = iuse; |
| 96 | llist_add_tail(&rt->list, &hlr->ussd_routes); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 97 | |
| 98 | return rt; |
| 99 | } |
| 100 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 101 | struct hlr_ussd_route *ussd_route_prefix_alloc_ext(struct hlr *hlr, const char *prefix, |
| 102 | struct hlr_euse *euse) |
| 103 | { |
| 104 | struct hlr_ussd_route *rt; |
| 105 | |
| 106 | if (ussd_route_find_prefix(hlr, prefix)) |
| 107 | return NULL; |
| 108 | |
| 109 | rt = talloc_zero(hlr, struct hlr_ussd_route); |
| 110 | rt->prefix = talloc_strdup(rt, prefix); |
| 111 | rt->is_external = true; |
| 112 | rt->u.euse = euse; |
| 113 | llist_add_tail(&rt->list, &hlr->ussd_routes); |
| 114 | |
| 115 | return rt; |
| 116 | } |
| 117 | |
| 118 | void ussd_route_del(struct hlr_ussd_route *rt) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 119 | { |
| 120 | llist_del(&rt->list); |
| 121 | talloc_free(rt); |
| 122 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 123 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 124 | static struct hlr_ussd_route *ussd_route_lookup_7bit(struct hlr *hlr, const char *ussd_code) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 125 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 126 | struct hlr_ussd_route *rt; |
| 127 | llist_for_each_entry(rt, &hlr->ussd_routes, list) { |
| 128 | if (!strncmp(ussd_code, rt->prefix, strlen(rt->prefix))) { |
| 129 | LOGP(DSS, LOGL_DEBUG, "Found EUSE %s (prefix %s) for USSD Code '%s'\n", |
| 130 | rt->u.euse->name, rt->prefix, ussd_code); |
| 131 | return rt; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 135 | LOGP(DSS, LOGL_DEBUG, "Could not find Route for USSD Code '%s'\n", ussd_code); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | /*********************************************************************** |
| 140 | * handling functions for individual GSUP messages |
| 141 | ***********************************************************************/ |
| 142 | |
Harald Welte | 97bfb65 | 2018-07-29 12:28:11 +0200 | [diff] [blame] | 143 | #define LOGPSS(ss, lvl, fmt, args...) \ |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 144 | LOGP(DSS, lvl, "%s/0x%08x: " fmt, (ss)->imsi, (ss)->session_id, ## args) |
Harald Welte | 97bfb65 | 2018-07-29 12:28:11 +0200 | [diff] [blame] | 145 | |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 146 | struct ss_session { |
| 147 | /* link us to hlr->ss_sessions */ |
| 148 | struct llist_head list; |
| 149 | /* imsi of this session */ |
| 150 | char imsi[GSM23003_IMSI_MAX_DIGITS+2]; |
| 151 | /* ID of this session (unique per IMSI) */ |
| 152 | uint32_t session_id; |
| 153 | /* state of the session */ |
| 154 | enum osmo_gsup_session_state state; |
| 155 | /* time-out when we will delete the session */ |
| 156 | struct osmo_timer_list timeout; |
| 157 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 158 | /* is this USSD for an external handler (EUSE): true */ |
| 159 | bool is_external; |
| 160 | union { |
| 161 | /* external USSD Entity responsible for this session */ |
| 162 | struct hlr_euse *euse; |
| 163 | /* internal USSD Entity responsible for this session */ |
| 164 | const struct hlr_iuse *iuse; |
| 165 | } u; |
| 166 | |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 167 | /* we don't keep a pointer to the osmo_gsup_{route,conn} towards the MSC/VLR here, |
| 168 | * as this might change during inter-VLR hand-over, and we simply look-up the serving MSC/VLR |
| 169 | * every time we receive an USSD component from the EUSE */ |
| 170 | }; |
| 171 | |
| 172 | struct ss_session *ss_session_find(struct hlr *hlr, const char *imsi, uint32_t session_id) |
| 173 | { |
| 174 | struct ss_session *ss; |
| 175 | llist_for_each_entry(ss, &hlr->ss_sessions, list) { |
| 176 | if (!strcmp(ss->imsi, imsi) && ss->session_id == session_id) |
| 177 | return ss; |
| 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | void ss_session_free(struct ss_session *ss) |
| 183 | { |
| 184 | osmo_timer_del(&ss->timeout); |
| 185 | llist_del(&ss->list); |
| 186 | talloc_free(ss); |
| 187 | } |
| 188 | |
| 189 | static void ss_session_timeout(void *data) |
| 190 | { |
| 191 | struct ss_session *ss = data; |
| 192 | |
Harald Welte | 97bfb65 | 2018-07-29 12:28:11 +0200 | [diff] [blame] | 193 | LOGPSS(ss, LOGL_NOTICE, "SS Session Timeout, destroying\n"); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 194 | /* FIXME: should we send a ReturnError component to the MS? */ |
| 195 | ss_session_free(ss); |
| 196 | } |
| 197 | |
| 198 | struct ss_session *ss_session_alloc(struct hlr *hlr, const char *imsi, uint32_t session_id) |
| 199 | { |
| 200 | struct ss_session *ss; |
| 201 | |
| 202 | OSMO_ASSERT(!ss_session_find(hlr, imsi, session_id)); |
| 203 | |
| 204 | ss = talloc_zero(hlr, struct ss_session); |
| 205 | OSMO_ASSERT(ss); |
| 206 | |
| 207 | OSMO_STRLCPY_ARRAY(ss->imsi, imsi); |
| 208 | ss->session_id = session_id; |
| 209 | osmo_timer_setup(&ss->timeout, ss_session_timeout, ss); |
| 210 | /* NOTE: The timeout is currently global and not refreshed with subsequent messages |
| 211 | * within the SS/USSD session. So 30s after the initial SS message, the session will |
| 212 | * timeout! */ |
| 213 | osmo_timer_schedule(&ss->timeout, 30, 0); |
| 214 | |
| 215 | llist_add_tail(&ss->list, &hlr->ss_sessions); |
| 216 | return ss; |
| 217 | } |
| 218 | |
| 219 | /*********************************************************************** |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 220 | * handling functions for encoding SS messages + wrapping them in GSUP |
| 221 | ***********************************************************************/ |
| 222 | |
| 223 | static int ss_tx_to_ms(struct ss_session *ss, enum osmo_gsup_message_type gsup_msg_type, |
| 224 | bool final, struct msgb *ss_msg) |
| 225 | |
| 226 | { |
| 227 | struct osmo_gsup_message resp = {0}; |
| 228 | struct msgb *resp_msg; |
| 229 | |
| 230 | resp.message_type = gsup_msg_type; |
| 231 | OSMO_STRLCPY_ARRAY(resp.imsi, ss->imsi); |
| 232 | if (final) |
| 233 | resp.session_state = OSMO_GSUP_SESSION_STATE_END; |
| 234 | else |
| 235 | resp.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE; |
| 236 | resp.session_id = ss->session_id; |
| 237 | if (ss_msg) { |
| 238 | resp.ss_info = msgb_data(ss_msg); |
| 239 | resp.ss_info_len = msgb_length(ss_msg); |
| 240 | } |
| 241 | |
| 242 | resp_msg = gsm0480_msgb_alloc_name(__func__); |
| 243 | OSMO_ASSERT(resp_msg); |
| 244 | osmo_gsup_encode(resp_msg, &resp); |
| 245 | msgb_free(ss_msg); |
| 246 | |
| 247 | /* FIXME: resolve this based on the database vlr_addr */ |
| 248 | return osmo_gsup_addr_send(g_hlr->gs, (uint8_t *)"MSC-00-00-00-00-00-00", 22, resp_msg); |
| 249 | } |
| 250 | |
| 251 | #if 0 |
| 252 | static int ss_tx_reject(struct ss_session *ss, int invoke_id, uint8_t problem_tag, |
| 253 | uint8_t problem_code) |
| 254 | { |
| 255 | struct msgb *msg = gsm0480_gen_reject(invoke_id, problem_tag, problem_code); |
| 256 | LOGPSS(ss, LOGL_NOTICE, "Tx Reject(%u, 0x%02x, 0x%02x)\n", invoke_id, |
| 257 | problem_tag, problem_code); |
| 258 | OSMO_ASSERT(msg); |
| 259 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg); |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | static int ss_tx_error(struct ss_session *ss, uint8_t invoke_id, uint8_t error_code) |
| 264 | { |
| 265 | struct msgb *msg = gsm0480_gen_return_error(invoke_id, error_code); |
| 266 | LOGPSS(ss, LOGL_NOTICE, "Tx ReturnError(%u, 0x%02x)\n", invoke_id, error_code); |
| 267 | OSMO_ASSERT(msg); |
| 268 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg); |
| 269 | } |
| 270 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 271 | static int ss_tx_ussd_7bit(struct ss_session *ss, bool final, uint8_t invoke_id, const char *text) |
| 272 | { |
| 273 | struct msgb *msg = gsm0480_gen_ussd_resp_7bit(invoke_id, text); |
| 274 | LOGPSS(ss, LOGL_INFO, "Tx USSD '%s'\n", text); |
| 275 | OSMO_ASSERT(msg); |
| 276 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, final, msg); |
| 277 | } |
| 278 | |
| 279 | /*********************************************************************** |
| 280 | * Internal USSD Handlers |
| 281 | ***********************************************************************/ |
| 282 | |
| 283 | #include "db.h" |
| 284 | |
| 285 | static int handle_ussd_own_msisdn(struct osmo_gsup_conn *conn, struct ss_session *ss, |
| 286 | const struct osmo_gsup_message *gsup, const struct ss_request *req) |
| 287 | { |
| 288 | struct hlr_subscriber subscr; |
| 289 | char buf[GSM0480_USSD_7BIT_STRING_LEN+1]; |
| 290 | int rc; |
| 291 | |
| 292 | rc = db_subscr_get_by_imsi(g_hlr->dbc, ss->imsi, &subscr); |
| 293 | switch (rc) { |
| 294 | case 0: |
| 295 | if (strlen(subscr.msisdn) == 0) |
| 296 | snprintf(buf, sizeof(buf), "You have no MSISDN!"); |
| 297 | else |
| 298 | snprintf(buf, sizeof(buf), "Your extension is %s\r", subscr.msisdn); |
| 299 | ss_tx_ussd_7bit(ss, true, req->invoke_id, buf); |
| 300 | break; |
| 301 | case -ENOENT: |
| 302 | ss_tx_error(ss, true, GSM0480_ERR_CODE_UNKNOWN_SUBSCRIBER); |
| 303 | break; |
| 304 | case -EIO: |
| 305 | default: |
| 306 | ss_tx_error(ss, true, GSM0480_ERR_CODE_SYSTEM_FAILURE); |
| 307 | break; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int handle_ussd_own_imsi(struct osmo_gsup_conn *conn, struct ss_session *ss, |
| 313 | const struct osmo_gsup_message *gsup, const struct ss_request *req) |
| 314 | { |
| 315 | char buf[GSM0480_USSD_7BIT_STRING_LEN+1]; |
| 316 | snprintf(buf, sizeof(buf), "Your IMSI is %s!\n", ss->imsi); |
| 317 | ss_tx_ussd_7bit(ss, true, req->invoke_id, buf); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | static const struct hlr_iuse hlr_iuses[] = { |
| 323 | { |
| 324 | .name = "own-msisdn", |
| 325 | .handle_ussd = handle_ussd_own_msisdn, |
| 326 | }, |
| 327 | { |
| 328 | .name = "own-imsi", |
| 329 | .handle_ussd = handle_ussd_own_imsi, |
| 330 | }, |
| 331 | }; |
| 332 | |
| 333 | const struct hlr_iuse *iuse_find(const char *name) |
| 334 | { |
| 335 | unsigned int i; |
| 336 | |
| 337 | for (i = 0; i < ARRAY_SIZE(hlr_iuses); i++) { |
| 338 | const struct hlr_iuse *iuse = &hlr_iuses[i]; |
| 339 | if (!strcmp(name, iuse->name)) |
| 340 | return iuse; |
| 341 | } |
| 342 | return NULL; |
| 343 | } |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 344 | |
| 345 | |
| 346 | /*********************************************************************** |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 347 | * handling functions for individual GSUP messages |
| 348 | ***********************************************************************/ |
| 349 | |
| 350 | static bool ss_op_is_ussd(uint8_t opcode) |
| 351 | { |
| 352 | switch (opcode) { |
| 353 | case GSM0480_OP_CODE_PROCESS_USS_DATA: |
| 354 | case GSM0480_OP_CODE_PROCESS_USS_REQ: |
| 355 | case GSM0480_OP_CODE_USS_REQUEST: |
| 356 | case GSM0480_OP_CODE_USS_NOTIFY: |
| 357 | return true; |
| 358 | default: |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* is this GSUP connection an EUSE (true) or not (false)? */ |
| 364 | static bool conn_is_euse(struct osmo_gsup_conn *conn) |
| 365 | { |
| 366 | int rc; |
| 367 | uint8_t *addr; |
| 368 | |
| 369 | rc = osmo_gsup_conn_ccm_get(conn, &addr, IPAC_IDTAG_SERNR); |
| 370 | if (rc <= 5) |
| 371 | return false; |
| 372 | if (!strncmp((char *)addr, "EUSE-", 5)) |
| 373 | return true; |
| 374 | else |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | static struct hlr_euse *euse_by_conn(struct osmo_gsup_conn *conn) |
| 379 | { |
| 380 | int rc; |
| 381 | char *addr; |
| 382 | struct hlr *hlr = conn->server->priv; |
| 383 | |
| 384 | rc = osmo_gsup_conn_ccm_get(conn, (uint8_t **) &addr, IPAC_IDTAG_SERNR); |
| 385 | if (rc <= 5) |
| 386 | return NULL; |
| 387 | if (strncmp(addr, "EUSE-", 5)) |
| 388 | return NULL; |
| 389 | |
| 390 | return euse_find(hlr, addr+5); |
| 391 | } |
| 392 | |
| 393 | static int handle_ss(struct ss_session *ss, const struct osmo_gsup_message *gsup, |
| 394 | const struct ss_request *req) |
| 395 | { |
| 396 | uint8_t comp_type = gsup->ss_info[0]; |
| 397 | |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 398 | LOGPSS(ss, LOGL_INFO, "SS CompType=%s, OpCode=%s\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 399 | gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode)); |
| 400 | /* FIXME */ |
| 401 | return 0; |
| 402 | } |
| 403 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 404 | /* Handle a USSD GSUP message for a given SS Session received from VLR or EUSE */ |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 405 | static int handle_ussd(struct osmo_gsup_conn *conn, struct ss_session *ss, |
| 406 | const struct osmo_gsup_message *gsup, const struct ss_request *req) |
| 407 | { |
| 408 | uint8_t comp_type = gsup->ss_info[0]; |
| 409 | struct msgb *msg_out; |
| 410 | bool is_euse_originated = conn_is_euse(conn); |
| 411 | |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 412 | LOGPSS(ss, LOGL_INFO, "USSD CompType=%s, OpCode=%s '%s'\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 413 | gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode), |
| 414 | req->ussd_text); |
| 415 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 416 | if ((ss->is_external && !ss->u.euse) || !ss->u.iuse) { |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 417 | LOGPSS(ss, LOGL_NOTICE, "USSD for unknown code '%s'\n", req->ussd_text); |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 418 | ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SS_NOT_AVAILABLE); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | if (is_euse_originated) { |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 423 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW"); |
| 424 | OSMO_ASSERT(msg_out); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 425 | /* Received from EUSE, Forward to VLR */ |
| 426 | osmo_gsup_encode(msg_out, gsup); |
| 427 | /* FIXME: resolve this based on the database vlr_addr */ |
| 428 | osmo_gsup_addr_send(conn->server, (uint8_t *)"MSC-00-00-00-00-00-00", 22, msg_out); |
| 429 | } else { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 430 | /* Received from VLR (MS) */ |
| 431 | if (ss->is_external) { |
| 432 | /* Forward to EUSE */ |
| 433 | char addr[128]; |
| 434 | strcpy(addr, "EUSE-"); |
| 435 | osmo_strlcpy(addr+5, ss->u.euse->name, sizeof(addr)-5); |
| 436 | conn = gsup_route_find(conn->server, (uint8_t *)addr, strlen(addr)+1); |
| 437 | if (!conn) { |
| 438 | LOGPSS(ss, LOGL_ERROR, "Cannot find conn for EUSE %s\n", addr); |
| 439 | ss_tx_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE); |
| 440 | } else { |
| 441 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP USSD FW"); |
| 442 | OSMO_ASSERT(msg_out); |
| 443 | osmo_gsup_encode(msg_out, gsup); |
| 444 | osmo_gsup_conn_send(conn, msg_out); |
| 445 | } |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 446 | } else { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 447 | /* Handle internally */ |
| 448 | ss->u.iuse->handle_ussd(conn, ss, gsup, req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 449 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | /* this function is called for any SS_REQ/SS_RESP messages from both the MSC/VLR side as well |
| 457 | * as from the EUSE side */ |
| 458 | int rx_proc_ss_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup) |
| 459 | { |
| 460 | struct hlr *hlr = conn->server->priv; |
| 461 | struct ss_session *ss; |
| 462 | struct ss_request req = {0}; |
| 463 | |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 464 | LOGP(DSS, LOGL_DEBUG, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id, |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 465 | osmo_gsup_session_state_name(gsup->session_state)); |
| 466 | |
| 467 | /* decode and find out what kind of SS message it is */ |
| 468 | if (gsup->ss_info && gsup->ss_info_len) { |
| 469 | if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 470 | LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to parse SS request: %s\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 471 | gsup->imsi, gsup->session_id, |
| 472 | osmo_hexdump(gsup->ss_info, gsup->ss_info_len)); |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 473 | /* FIXME: Send a Reject component? */ |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 474 | goto out_err; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | switch (gsup->session_state) { |
| 479 | case OSMO_GSUP_SESSION_STATE_BEGIN: |
| 480 | /* Check for overlapping Session ID usage */ |
| 481 | if (ss_session_find(hlr, gsup->imsi, gsup->session_id)) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 482 | LOGP(DSS, LOGL_ERROR, "%s/0x%08x: BEGIN with non-unique session ID!\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 483 | gsup->imsi, gsup->session_id); |
| 484 | goto out_err; |
| 485 | } |
| 486 | ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id); |
| 487 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 488 | LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unable to allocate SS session\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 489 | gsup->imsi, gsup->session_id); |
| 490 | goto out_err; |
| 491 | } |
| 492 | if (ss_op_is_ussd(req.opcode)) { |
| 493 | if (conn_is_euse(conn)) { |
| 494 | /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */ |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 495 | ss->u.euse = euse_by_conn(conn); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 496 | } else { |
| 497 | /* VLR->EUSE: MO USSD. VLR is known ('conn'), EUSE is to be resolved */ |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 498 | struct hlr_ussd_route *rt; |
| 499 | rt = ussd_route_lookup_7bit(hlr, (const char *) req.ussd_text); |
| 500 | if (rt) { |
| 501 | if (rt->is_external) { |
| 502 | ss->is_external = true; |
| 503 | ss->u.euse = rt->u.euse; |
| 504 | } else if (rt) { |
| 505 | ss->is_external = false; |
| 506 | ss->u.iuse = rt->u.iuse; |
| 507 | } |
| 508 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 509 | } |
| 510 | /* dispatch unstructured SS to routing */ |
| 511 | handle_ussd(conn, ss, gsup, &req); |
| 512 | } else { |
| 513 | /* dispatch non-call SS to internal code */ |
| 514 | handle_ss(ss, gsup, &req); |
| 515 | } |
| 516 | break; |
| 517 | case OSMO_GSUP_SESSION_STATE_CONTINUE: |
| 518 | ss = ss_session_find(hlr, gsup->imsi, gsup->session_id); |
| 519 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 520 | LOGP(DSS, LOGL_ERROR, "%s/0x%08x: CONTINUE for unknown SS session\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 521 | gsup->imsi, gsup->session_id); |
| 522 | goto out_err; |
| 523 | } |
| 524 | if (ss_op_is_ussd(req.opcode)) { |
| 525 | /* dispatch unstructured SS to routing */ |
| 526 | handle_ussd(conn, ss, gsup, &req); |
| 527 | } else { |
| 528 | /* dispatch non-call SS to internal code */ |
| 529 | handle_ss(ss, gsup, &req); |
| 530 | } |
| 531 | break; |
| 532 | case OSMO_GSUP_SESSION_STATE_END: |
| 533 | ss = ss_session_find(hlr, gsup->imsi, gsup->session_id); |
| 534 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 535 | LOGP(DSS, LOGL_ERROR, "%s/0x%08x: END for unknown SS session\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 536 | gsup->imsi, gsup->session_id); |
| 537 | goto out_err; |
| 538 | } |
| 539 | if (ss_op_is_ussd(req.opcode)) { |
| 540 | /* dispatch unstructured SS to routing */ |
| 541 | handle_ussd(conn, ss, gsup, &req); |
| 542 | } else { |
| 543 | /* dispatch non-call SS to internal code */ |
| 544 | handle_ss(ss, gsup, &req); |
| 545 | } |
| 546 | ss_session_free(ss); |
| 547 | break; |
| 548 | default: |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 549 | LOGP(DSS, LOGL_ERROR, "%s/0x%08x: Unknown SS State %d\n", gsup->imsi, |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 550 | gsup->session_id, gsup->session_state); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 551 | goto out_err; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | |
| 556 | out_err: |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | int rx_proc_ss_error(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup) |
| 561 | { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 562 | LOGP(DSS, LOGL_NOTICE, "%s/0x%08x: Process SS ERROR (%s)\n", gsup->imsi, gsup->session_id, |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 563 | osmo_gsup_session_state_name(gsup->session_state)); |
| 564 | return 0; |
| 565 | } |