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 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 32 | #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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 38 | |
| 39 | /*********************************************************************** |
| 40 | * core data structures expressing config from VTY |
| 41 | ***********************************************************************/ |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 42 | |
| 43 | struct 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 | |
| 54 | struct 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 Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 63 | llist_add_tail(&euse->list, &hlr->euse_list); |
| 64 | |
| 65 | return euse; |
| 66 | } |
| 67 | |
| 68 | void euse_del(struct hlr_euse *euse) |
| 69 | { |
| 70 | llist_del(&euse->list); |
| 71 | talloc_free(euse); |
| 72 | } |
| 73 | |
| 74 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 75 | 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] | 76 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 77 | struct hlr_ussd_route *rt; |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 78 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 79 | llist_for_each_entry(rt, &hlr->ussd_routes, list) { |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 80 | if (!strcmp(rt->prefix, prefix)) |
| 81 | return rt; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 86 | struct hlr_ussd_route *ussd_route_prefix_alloc_int(struct hlr *hlr, const char *prefix, |
| 87 | const struct hlr_iuse *iuse) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 88 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 89 | struct hlr_ussd_route *rt; |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 90 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 91 | if (ussd_route_find_prefix(hlr, prefix)) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 92 | return NULL; |
| 93 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 94 | rt = talloc_zero(hlr, struct hlr_ussd_route); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 95 | rt->prefix = talloc_strdup(rt, prefix); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 96 | rt->u.iuse = iuse; |
| 97 | llist_add_tail(&rt->list, &hlr->ussd_routes); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 98 | |
| 99 | return rt; |
| 100 | } |
| 101 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 102 | struct 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 | |
| 119 | void ussd_route_del(struct hlr_ussd_route *rt) |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 120 | { |
| 121 | llist_del(&rt->list); |
| 122 | talloc_free(rt); |
| 123 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 124 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 125 | 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] | 126 | { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 127 | 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 Yanitskiy | e6c839e | 2018-08-02 23:59:51 +0700 | [diff] [blame] | 130 | 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 Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 134 | return rt; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 138 | 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] | 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | /*********************************************************************** |
| 143 | * handling functions for individual GSUP messages |
| 144 | ***********************************************************************/ |
| 145 | |
Harald Welte | 97bfb65 | 2018-07-29 12:28:11 +0200 | [diff] [blame] | 146 | #define LOGPSS(ss, lvl, fmt, args...) \ |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 147 | 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] | 148 | |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 149 | struct ss_session { |
| 150 | /* link us to hlr->ss_sessions */ |
| 151 | struct llist_head list; |
| 152 | /* imsi of this session */ |
Neels Hofmeyr | e21b45a | 2019-03-18 21:04:23 +0100 | [diff] [blame] | 153 | char imsi[OSMO_IMSI_BUF_SIZE]; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 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 Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 161 | /* 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 Smith | 95abc2b | 2019-04-04 12:00:24 +0200 | [diff] [blame] | 170 | /* subscriber's vlr_number |
| 171 | * MO USSD: originating MSC's vlr_number |
| 172 | * MT USSD: looked up once per session and cached here */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 173 | struct osmo_ipa_name vlr_name; |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 174 | |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 175 | /* 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 178 | |
| 179 | struct osmo_gsup_req *initial_req_from_ms; |
| 180 | struct osmo_gsup_req *initial_req_from_euse; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | struct 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 | |
| 193 | void ss_session_free(struct ss_session *ss) |
| 194 | { |
| 195 | osmo_timer_del(&ss->timeout); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 196 | 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 200 | llist_del(&ss->list); |
| 201 | talloc_free(ss); |
| 202 | } |
| 203 | |
| 204 | static void ss_session_timeout(void *data) |
| 205 | { |
| 206 | struct ss_session *ss = data; |
| 207 | |
Harald Welte | 97bfb65 | 2018-07-29 12:28:11 +0200 | [diff] [blame] | 208 | LOGPSS(ss, LOGL_NOTICE, "SS Session Timeout, destroying\n"); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 209 | /* FIXME: should we send a ReturnError component to the MS? */ |
| 210 | ss_session_free(ss); |
| 211 | } |
| 212 | |
| 213 | struct 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 Yanitskiy | e6ce52b | 2018-12-01 00:16:44 +0700 | [diff] [blame] | 224 | |
| 225 | /* Schedule self-destruction timer */ |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 226 | osmo_timer_setup(&ss->timeout, ss_session_timeout, ss); |
Vadim Yanitskiy | d157a56 | 2018-12-01 00:03:39 +0700 | [diff] [blame] | 227 | if (g_hlr->ncss_guard_timeout > 0) |
| 228 | osmo_timer_schedule(&ss->timeout, g_hlr->ncss_guard_timeout, 0); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 229 | |
| 230 | llist_add_tail(&ss->list, &hlr->ss_sessions); |
| 231 | return ss; |
| 232 | } |
| 233 | |
| 234 | /*********************************************************************** |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 235 | * handling functions for encoding SS messages + wrapping them in GSUP |
| 236 | ***********************************************************************/ |
| 237 | |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 238 | /* Resolve the target MSC by ss->imsi and send GSUP message. */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 239 | static int ss_gsup_send_to_ms(struct ss_session *ss, struct osmo_gsup_server *gs, struct osmo_gsup_message *gsup) |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 240 | { |
| 241 | struct hlr_subscriber subscr = {}; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 242 | struct msgb *msg; |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 243 | int rc; |
| 244 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 245 | 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 Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 259 | /* Use vlr_number as looked up by the caller, or look up now. */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 260 | if (!ss->vlr_name.len) { |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 261 | 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 267 | osmo_ipa_name_set_str(&ss->vlr_name, subscr.vlr_number); |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Oliver Smith | 95abc2b | 2019-04-04 12:00:24 +0200 | [diff] [blame] | 270 | /* Check for empty string (all vlr_number strings end in "\0", because otherwise gsup_route_find() fails) */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 271 | if (ss->vlr_name.len <= 1) { |
Neels Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 272 | 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 277 | 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 Hofmeyr | f1fe94c | 2019-04-02 04:26:55 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 281 | static int ss_tx_to_ms(struct ss_session *ss, enum osmo_gsup_message_type gsup_msg_type, |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 282 | struct msgb *ss_msg) |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 283 | |
| 284 | { |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 285 | struct osmo_gsup_message resp; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 286 | int rc; |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 287 | |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 288 | 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 Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 294 | OSMO_STRLCPY_ARRAY(resp.imsi, ss->imsi); |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 295 | |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 296 | if (ss_msg) { |
| 297 | resp.ss_info = msgb_data(ss_msg); |
| 298 | resp.ss_info_len = msgb_length(ss_msg); |
| 299 | } |
| 300 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 301 | rc = ss_gsup_send_to_ms(ss, g_hlr->gs, &resp); |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 302 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 303 | msgb_free(ss_msg); |
| 304 | return rc; |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | #if 0 |
| 308 | static 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 Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 315 | ss->state = OSMO_GSUP_SESSION_STATE_END; |
| 316 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg); |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 317 | } |
| 318 | #endif |
| 319 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 320 | static int ss_tx_to_ms_error(struct ss_session *ss, uint8_t invoke_id, uint8_t error_code) |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 321 | { |
| 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 Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 325 | ss->state = OSMO_GSUP_SESSION_STATE_END; |
| 326 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg); |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 329 | static int ss_tx_to_ms_ussd_7bit(struct ss_session *ss, uint8_t invoke_id, const char *text) |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 330 | { |
| 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 Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 334 | return ss_tx_to_ms(ss, OSMO_GSUP_MSGT_PROC_SS_RESULT, msg); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /*********************************************************************** |
| 338 | * Internal USSD Handlers |
| 339 | ***********************************************************************/ |
| 340 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 341 | #include <osmocom/hlr/db.h> |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 342 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 343 | static int handle_ussd_own_msisdn(struct ss_session *ss, |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 344 | 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 Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 350 | ss->state = OSMO_GSUP_SESSION_STATE_END; |
| 351 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 352 | 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 |
Vadim Yanitskiy | 3adb33d | 2018-08-03 00:07:54 +0700 | [diff] [blame] | 358 | snprintf(buf, sizeof(buf), "Your extension is %s", subscr.msisdn); |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 359 | ss_tx_to_ms_ussd_7bit(ss, req->invoke_id, buf); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 360 | break; |
| 361 | case -ENOENT: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 362 | ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_UNKNOWN_SUBSCRIBER); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 363 | break; |
| 364 | case -EIO: |
| 365 | default: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 366 | ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_SYSTEM_FAILURE); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | return 0; |
| 370 | } |
| 371 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 372 | static int handle_ussd_own_imsi(struct ss_session *ss, |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 373 | const struct osmo_gsup_message *gsup, const struct ss_request *req) |
| 374 | { |
| 375 | char buf[GSM0480_USSD_7BIT_STRING_LEN+1]; |
Vadim Yanitskiy | 3adb33d | 2018-08-03 00:07:54 +0700 | [diff] [blame] | 376 | snprintf(buf, sizeof(buf), "Your IMSI is %s", ss->imsi); |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 377 | ss->state = OSMO_GSUP_SESSION_STATE_END; |
| 378 | ss_tx_to_ms_ussd_7bit(ss, req->invoke_id, buf); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Vadim Yanitskiy | dac855e | 2020-11-17 04:17:46 +0700 | [diff] [blame] | 382 | /* This handler just keeps the session idle unless the guard timer expires. */ |
| 383 | static 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 Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 395 | |
| 396 | static 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 Yanitskiy | dac855e | 2020-11-17 04:17:46 +0700 | [diff] [blame] | 405 | { |
| 406 | .name = "test-idle", |
| 407 | .handle_ussd = handle_ussd_test_idle, |
| 408 | }, |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | const 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 Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 422 | |
| 423 | |
| 424 | /*********************************************************************** |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 425 | * handling functions for individual GSUP messages |
| 426 | ***********************************************************************/ |
| 427 | |
| 428 | static 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 Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 442 | static bool peer_name_is_euse(const struct osmo_cni_peer_id *peer_name) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 443 | { |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 444 | if (peer_name->type != OSMO_CNI_PEER_ID_IPA_NAME) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 445 | return false; |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 446 | if (peer_name->ipa_name.len <= 5) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 447 | return false; |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 448 | return strncmp((char *)(peer_name->ipa_name.val), "EUSE-", 5) == 0; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 451 | static struct hlr_euse *euse_by_name(const struct osmo_cni_peer_id *peer_name) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 452 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 453 | if (!peer_name_is_euse(peer_name)) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 454 | return NULL; |
| 455 | |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 456 | /* 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 462 | static int handle_ss(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup, |
| 463 | const struct ss_request *req) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 464 | { |
| 465 | uint8_t comp_type = gsup->ss_info[0]; |
| 466 | |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 467 | LOGPSS(ss, LOGL_INFO, "SS CompType=%s, OpCode=%s\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 468 | gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode)); |
Vadim Yanitskiy | 4a4bdcd | 2018-10-12 21:44:07 +0200 | [diff] [blame] | 469 | |
| 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 476 | ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_FACILITY_NOT_SUPPORTED); |
Vadim Yanitskiy | 4a4bdcd | 2018-10-12 21:44:07 +0200 | [diff] [blame] | 477 | return -ENOTSUP; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 480 | /* Handle a USSD GSUP message for a given SS Session received from VLR or EUSE */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 481 | static int handle_ussd(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup, |
| 482 | const struct ss_request *req) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 483 | { |
| 484 | uint8_t comp_type = gsup->ss_info[0]; |
| 485 | struct msgb *msg_out; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 486 | |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 487 | LOGPSS(ss, LOGL_INFO, "USSD CompType=%s, OpCode=%s '%s'\n", |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 488 | gsm0480_comp_type_name(comp_type), gsm0480_op_code_name(req->opcode), |
| 489 | req->ussd_text); |
| 490 | |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 491 | if ((ss->is_external && !ss->u.euse) || !ss->u.iuse) { |
Harald Welte | 7f32f5f | 2018-07-29 12:43:49 +0200 | [diff] [blame] | 492 | LOGPSS(ss, LOGL_NOTICE, "USSD for unknown code '%s'\n", req->ussd_text); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 493 | ss_tx_to_ms_error(ss, req->invoke_id, GSM0480_ERR_CODE_SS_NOT_AVAILABLE); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | if (is_euse_originated) { |
| 498 | /* Received from EUSE, Forward to VLR */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 499 | /* 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 502 | } else { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 503 | /* Received from VLR (MS) */ |
| 504 | if (ss->is_external) { |
| 505 | /* Forward to EUSE */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 506 | 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 Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 510 | if (!conn) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 511 | 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 Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 514 | } else { |
Neels Hofmeyr | a7d0f87 | 2019-10-30 02:08:28 +0100 | [diff] [blame] | 515 | msg_out = osmo_gsup_msgb_alloc("GSUP USSD FW"); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 516 | osmo_gsup_encode(msg_out, gsup); |
| 517 | osmo_gsup_conn_send(conn, msg_out); |
| 518 | } |
Harald Welte | 7266731 | 2018-07-29 12:38:09 +0200 | [diff] [blame] | 519 | } else { |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 520 | /* Handle internally */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 521 | ss->u.iuse->handle_ussd(ss, gsup, req); |
Vadim Yanitskiy | 6a6c7f8 | 2020-11-17 04:02:11 +0700 | [diff] [blame] | 522 | /* 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 525 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 526 | } |
| 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 534 | void rx_proc_ss_req(struct osmo_gsup_req *gsup_req) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 535 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 536 | struct hlr *hlr = g_hlr; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 537 | struct ss_session *ss; |
| 538 | struct ss_request req = {0}; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 539 | 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 544 | |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 545 | 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] | 546 | osmo_gsup_session_state_name(gsup->session_state)); |
| 547 | |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 548 | 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 556 | /* 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 Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 559 | 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] | 560 | gsup->imsi, gsup->session_id, |
| 561 | osmo_hexdump(gsup->ss_info, gsup->ss_info_len)); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 562 | osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "error parsing SS request"); |
| 563 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 564 | } |
Vadim Yanitskiy | 937f583 | 2019-07-24 19:14:32 +0700 | [diff] [blame] | 565 | } 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 569 | osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "missing SS payload"); |
| 570 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 571 | } |
| 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 Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 577 | 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] | 578 | gsup->imsi, gsup->session_id); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 579 | osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "BEGIN with non-unique session ID"); |
| 580 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 581 | } |
| 582 | ss = ss_session_alloc(hlr, gsup->imsi, gsup->session_id); |
| 583 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 584 | 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] | 585 | gsup->imsi, gsup->session_id); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 586 | osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_NET_FAIL, "Unable to allocate SS session"); |
| 587 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 588 | } |
Oliver Smith | 95abc2b | 2019-04-04 12:00:24 +0200 | [diff] [blame] | 589 | /* Get IPA name from VLR conn and save as ss->vlr_number */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 590 | if (!is_euse_originated) { |
| 591 | ss->initial_req_from_ms = gsup_req; |
| 592 | free_gsup_req = NULL; |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 593 | 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 595 | } else { |
| 596 | ss->initial_req_from_euse = gsup_req; |
| 597 | free_gsup_req = NULL; |
Oliver Smith | 95abc2b | 2019-04-04 12:00:24 +0200 | [diff] [blame] | 598 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 599 | if (ss_op_is_ussd(req.opcode)) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 600 | if (is_euse_originated) { |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 601 | /* EUSE->VLR: MT USSD. EUSE is known ('conn'), VLR is to be resolved */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 602 | ss->u.euse = euse_by_name(&gsup_req->source_name); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 603 | } else { |
| 604 | /* 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] | 605 | 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 Welte | 1eb9869 | 2018-08-08 08:57:26 +0200 | [diff] [blame] | 615 | } else { |
| 616 | if (hlr->euse_default) { |
| 617 | ss->is_external = true; |
| 618 | ss->u.euse = hlr->euse_default; |
| 619 | } |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 620 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 621 | } |
| 622 | /* dispatch unstructured SS to routing */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 623 | handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 624 | } else { |
| 625 | /* dispatch non-call SS to internal code */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 626 | handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 627 | } |
| 628 | break; |
| 629 | case OSMO_GSUP_SESSION_STATE_CONTINUE: |
| 630 | ss = ss_session_find(hlr, gsup->imsi, gsup->session_id); |
| 631 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 632 | 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] | 633 | gsup->imsi, gsup->session_id); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 634 | osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_INV_MAND_INFO, "CONTINUE for unknown SS session"); |
| 635 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 636 | } |
Vadim Yanitskiy | e6ce52b | 2018-12-01 00:16:44 +0700 | [diff] [blame] | 637 | |
| 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 642 | if (ss_op_is_ussd(req.opcode)) { |
| 643 | /* dispatch unstructured SS to routing */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 644 | handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 645 | } else { |
| 646 | /* dispatch non-call SS to internal code */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 647 | handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 648 | } |
| 649 | break; |
| 650 | case OSMO_GSUP_SESSION_STATE_END: |
| 651 | ss = ss_session_find(hlr, gsup->imsi, gsup->session_id); |
| 652 | if (!ss) { |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 653 | 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] | 654 | gsup->imsi, gsup->session_id); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 655 | return; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 656 | } |
Vadim Yanitskiy | 937f583 | 2019-07-24 19:14:32 +0700 | [diff] [blame] | 657 | |
| 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 Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 662 | handle_ussd(ss, is_euse_originated, &gsup_req->gsup, &req); |
Vadim Yanitskiy | 937f583 | 2019-07-24 19:14:32 +0700 | [diff] [blame] | 663 | } else { |
| 664 | /* dispatch non-call SS to internal code */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 665 | handle_ss(ss, is_euse_originated, &gsup_req->gsup, &req); |
Vadim Yanitskiy | 937f583 | 2019-07-24 19:14:32 +0700 | [diff] [blame] | 666 | } |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 667 | } |
Vadim Yanitskiy | 937f583 | 2019-07-24 19:14:32 +0700 | [diff] [blame] | 668 | |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 669 | ss_session_free(ss); |
| 670 | break; |
| 671 | default: |
Harald Welte | 95b96d4 | 2018-07-29 12:47:39 +0200 | [diff] [blame] | 672 | 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] | 673 | gsup->session_id, gsup->session_state); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 674 | break; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 677 | if (free_gsup_req) |
| 678 | osmo_gsup_req_free(free_gsup_req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 681 | void rx_proc_ss_error(struct osmo_gsup_req *req) |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 682 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 683 | 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 Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 685 | } |