Neels Hofmeyr | 647c106 | 2019-11-20 03:35:37 +0100 | [diff] [blame^] | 1 | /* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <talloc.h> |
| 22 | #include <errno.h> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <osmocom/core/timer.h> |
| 26 | #include <osmocom/gsm/gsup.h> |
| 27 | #include <osmocom/gsm/gsm23003.h> |
| 28 | #include <osmocom/gsm/gsm48_ie.h> |
| 29 | #include <osmocom/gsupclient/gsup_client.h> |
| 30 | #include <osmocom/gsupclient/gsup_req.h> |
| 31 | |
| 32 | #include <osmocom/hlr/logging.h> |
| 33 | #include <osmocom/hlr/proxy.h> |
| 34 | #include <osmocom/hlr/remote_hlr.h> |
| 35 | #include <osmocom/hlr/gsup_server.h> |
| 36 | #include <osmocom/hlr/gsup_router.h> |
| 37 | |
| 38 | #define LOG_PROXY_SUBSCR(proxy_subscr, level, fmt, args...) \ |
| 39 | LOGP(DDGSM, level, "(Proxy IMSI-%s MSISDN-%s HLR-" OSMO_SOCKADDR_STR_FMT ") " fmt, \ |
| 40 | ((proxy_subscr) && *(proxy_subscr)->imsi)? (proxy_subscr)->imsi : "?", \ |
| 41 | ((proxy_subscr) && *(proxy_subscr)->msisdn)? (proxy_subscr)->msisdn : "?", \ |
| 42 | OSMO_SOCKADDR_STR_FMT_ARGS((proxy_subscr)? &(proxy_subscr)->remote_hlr_addr : NULL), \ |
| 43 | ##args) |
| 44 | |
| 45 | #define LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup_msg, level, fmt, args...) \ |
| 46 | LOG_PROXY_SUBSCR(proxy_subscr, level, "%s: " fmt, \ |
| 47 | (gsup_msg) ? osmo_gsup_message_type_name((gsup_msg)->message_type) : "NULL", \ |
| 48 | ##args) |
| 49 | |
| 50 | /* The proxy subscriber database. |
| 51 | * Why have a separate struct to add an llist_head entry? |
| 52 | * This is to keep the option open to store the proxy data in the database instead, without any visible effect outside |
| 53 | * of proxy.c. */ |
| 54 | struct proxy_subscr_listentry { |
| 55 | struct llist_head entry; |
| 56 | timestamp_t last_update; |
| 57 | struct proxy_subscr data; |
| 58 | }; |
| 59 | |
| 60 | struct proxy_pending_gsup_req { |
| 61 | struct llist_head entry; |
| 62 | struct osmo_gsup_req *req; |
| 63 | timestamp_t received_at; |
| 64 | }; |
| 65 | |
| 66 | /* Defer a GSUP message until we know a remote HLR to proxy to. |
| 67 | * Where to send this GSUP message is indicated by its IMSI: as soon as an MS lookup has yielded the IMSI's home HLR, |
| 68 | * that's where the message should go. */ |
| 69 | static void proxy_deferred_gsup_req_add(struct proxy *proxy, struct osmo_gsup_req *req) |
| 70 | { |
| 71 | struct proxy_pending_gsup_req *m; |
| 72 | |
| 73 | m = talloc_zero(proxy, struct proxy_pending_gsup_req); |
| 74 | OSMO_ASSERT(m); |
| 75 | m->req = req; |
| 76 | timestamp_update(&m->received_at); |
| 77 | llist_add_tail(&m->entry, &proxy->pending_gsup_reqs); |
| 78 | } |
| 79 | |
| 80 | static void proxy_pending_req_remote_hlr_connect_result(struct osmo_gsup_req *req, struct remote_hlr *remote_hlr) |
| 81 | { |
| 82 | if (!remote_hlr || !remote_hlr_is_up(remote_hlr)) { |
| 83 | osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "Proxy: Failed to connect to home HLR"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | remote_hlr_gsup_forward_to_remote_hlr(remote_hlr, req, NULL); |
| 88 | } |
| 89 | |
| 90 | static bool proxy_deferred_gsup_req_waiting(struct proxy *proxy, const char *imsi) |
| 91 | { |
| 92 | struct proxy_pending_gsup_req *p; |
| 93 | OSMO_ASSERT(imsi); |
| 94 | |
| 95 | llist_for_each_entry(p, &proxy->pending_gsup_reqs, entry) { |
| 96 | if (strcmp(p->req->gsup.imsi, imsi)) |
| 97 | continue; |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /* Result of looking for remote HLR. If it failed, pass remote_hlr as NULL. On failure, the remote_hlr may be passed |
| 104 | * NULL. */ |
| 105 | static void proxy_deferred_gsup_req_pop(struct proxy *proxy, const char *imsi, struct remote_hlr *remote_hlr) |
| 106 | { |
| 107 | struct proxy_pending_gsup_req *p, *n; |
| 108 | OSMO_ASSERT(imsi); |
| 109 | |
| 110 | llist_for_each_entry_safe(p, n, &proxy->pending_gsup_reqs, entry) { |
| 111 | if (strcmp(p->req->gsup.imsi, imsi)) |
| 112 | continue; |
| 113 | |
| 114 | proxy_pending_req_remote_hlr_connect_result(p->req, remote_hlr); |
| 115 | p->req = NULL; |
| 116 | llist_del(&p->entry); |
| 117 | talloc_free(p); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static bool proxy_subscr_matches_imsi(const struct proxy_subscr *proxy_subscr, const char *imsi) |
| 122 | { |
| 123 | if (!proxy_subscr || !imsi) |
| 124 | return false; |
| 125 | return strcmp(proxy_subscr->imsi, imsi) == 0; |
| 126 | } |
| 127 | |
| 128 | static bool proxy_subscr_matches_msisdn(const struct proxy_subscr *proxy_subscr, const char *msisdn) |
| 129 | { |
| 130 | if (!proxy_subscr || !msisdn) |
| 131 | return false; |
| 132 | return strcmp(proxy_subscr->msisdn, msisdn) == 0; |
| 133 | } |
| 134 | |
| 135 | static struct proxy_subscr_listentry *_proxy_get_by_imsi(struct proxy *proxy, const char *imsi) |
| 136 | { |
| 137 | struct proxy_subscr_listentry *e; |
| 138 | if (!proxy) |
| 139 | return NULL; |
| 140 | llist_for_each_entry(e, &proxy->subscr_list, entry) { |
| 141 | if (proxy_subscr_matches_imsi(&e->data, imsi)) |
| 142 | return e; |
| 143 | } |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static struct proxy_subscr_listentry *_proxy_get_by_msisdn(struct proxy *proxy, const char *msisdn) |
| 148 | { |
| 149 | struct proxy_subscr_listentry *e; |
| 150 | if (!proxy) |
| 151 | return NULL; |
| 152 | llist_for_each_entry(e, &proxy->subscr_list, entry) { |
| 153 | if (proxy_subscr_matches_msisdn(&e->data, msisdn)) |
| 154 | return e; |
| 155 | } |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | int proxy_subscr_get_by_imsi(struct proxy_subscr *dst, struct proxy *proxy, const char *imsi) |
| 160 | { |
| 161 | struct proxy_subscr_listentry *e = _proxy_get_by_imsi(proxy, imsi); |
| 162 | if (!e) |
| 163 | return -ENOENT; |
| 164 | *dst = e->data; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int proxy_subscr_get_by_msisdn(struct proxy_subscr *dst, struct proxy *proxy, const char *msisdn) |
| 169 | { |
| 170 | struct proxy_subscr_listentry *e = _proxy_get_by_msisdn(proxy, msisdn); |
| 171 | if (!e) |
| 172 | return -ENOENT; |
| 173 | *dst = e->data; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int proxy_subscr_create_or_update(struct proxy *proxy, const struct proxy_subscr *proxy_subscr) |
| 178 | { |
| 179 | struct proxy_subscr_listentry *e = _proxy_get_by_imsi(proxy, proxy_subscr->imsi); |
| 180 | if (!e) { |
| 181 | /* Does not exist yet */ |
| 182 | e = talloc_zero(proxy, struct proxy_subscr_listentry); |
| 183 | llist_add(&e->entry, &proxy->subscr_list); |
| 184 | } |
| 185 | e->data = *proxy_subscr; |
| 186 | timestamp_update(&e->last_update); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int _proxy_subscr_del(struct proxy_subscr_listentry *e) |
| 191 | { |
| 192 | llist_del(&e->entry); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int proxy_subscr_del(struct proxy *proxy, const char *imsi) |
| 197 | { |
| 198 | struct proxy_subscr_listentry *e; |
| 199 | proxy_deferred_gsup_req_pop(proxy, imsi, NULL); |
| 200 | e = _proxy_get_by_imsi(proxy, imsi); |
| 201 | if (!e) |
| 202 | return -ENOENT; |
| 203 | return _proxy_subscr_del(e); |
| 204 | } |
| 205 | |
| 206 | /* Discard stale proxy entries. */ |
| 207 | static void proxy_cleanup(void *proxy_v) |
| 208 | { |
| 209 | struct proxy *proxy = proxy_v; |
| 210 | struct proxy_subscr_listentry *e, *n; |
| 211 | uint32_t age; |
| 212 | llist_for_each_entry_safe(e, n, &proxy->subscr_list, entry) { |
| 213 | if (!timestamp_age(&e->last_update, &age)) |
| 214 | LOGP(DDGSM, LOGL_ERROR, "Invalid timestamp, deleting proxy entry\n"); |
| 215 | else if (age <= proxy->fresh_time) |
| 216 | continue; |
| 217 | LOG_PROXY_SUBSCR(&e->data, LOGL_INFO, "proxy entry timed out, deleting\n"); |
| 218 | _proxy_subscr_del(e); |
| 219 | } |
| 220 | if (proxy->gc_period) |
| 221 | osmo_timer_schedule(&proxy->gc_timer, proxy->gc_period, 0); |
| 222 | else |
| 223 | LOGP(DDGSM, LOGL_NOTICE, "Proxy cleanup is switched off (gc_period == 0)\n"); |
| 224 | } |
| 225 | |
| 226 | void proxy_set_gc_period(struct proxy *proxy, uint32_t gc_period) |
| 227 | { |
| 228 | proxy->gc_period = gc_period; |
| 229 | proxy_cleanup(proxy); |
| 230 | } |
| 231 | |
| 232 | void proxy_init(struct osmo_gsup_server *gsup_server_to_vlr) |
| 233 | { |
| 234 | OSMO_ASSERT(!gsup_server_to_vlr->proxy); |
| 235 | struct proxy *proxy = talloc_zero(gsup_server_to_vlr, struct proxy); |
| 236 | *proxy = (struct proxy){ |
| 237 | .gsup_server_to_vlr = gsup_server_to_vlr, |
| 238 | .fresh_time = 60*60, |
| 239 | .gc_period = 60, |
| 240 | }; |
| 241 | INIT_LLIST_HEAD(&proxy->subscr_list); |
| 242 | INIT_LLIST_HEAD(&proxy->pending_gsup_reqs); |
| 243 | |
| 244 | osmo_timer_setup(&proxy->gc_timer, proxy_cleanup, proxy); |
| 245 | /* Invoke to trigger the first timer schedule */ |
| 246 | proxy_set_gc_period(proxy, proxy->gc_period); |
| 247 | gsup_server_to_vlr->proxy = proxy; |
| 248 | } |
| 249 | |
| 250 | void proxy_del(struct proxy *proxy) |
| 251 | { |
| 252 | osmo_timer_del(&proxy->gc_timer); |
| 253 | talloc_free(proxy); |
| 254 | } |
| 255 | |
| 256 | /* All GSUP messages sent to the remote HLR pass through this function, to modify the subscriber state or disallow |
| 257 | * sending the message. Return 0 to allow sending the message. */ |
| 258 | static int proxy_acknowledge_gsup_to_remote_hlr(struct proxy *proxy, const struct proxy_subscr *proxy_subscr, |
| 259 | const struct osmo_gsup_req *req) |
| 260 | { |
| 261 | struct proxy_subscr proxy_subscr_new = *proxy_subscr; |
| 262 | bool ps; |
| 263 | bool cs; |
| 264 | int rc; |
| 265 | |
| 266 | if (req->source_name.type != OSMO_GSUP_PEER_ID_IPA_NAME) { |
| 267 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, LOGL_ERROR, |
| 268 | "Unsupported GSUP peer id type: %s\n", |
| 269 | osmo_gsup_peer_id_type_name(req->source_name.type)); |
| 270 | return -ENOTSUP; |
| 271 | } |
| 272 | |
| 273 | switch (req->gsup.message_type) { |
| 274 | |
| 275 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
| 276 | /* Store the CS and PS VLR name in vlr_name_preliminary to later update the right {cs,ps} LU timestamp |
| 277 | * when receiving an OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT. Store in vlr_name_preliminary so that in |
| 278 | * case the LU fails, we keep the vlr_name intact. */ |
| 279 | switch (req->gsup.cn_domain) { |
| 280 | case OSMO_GSUP_CN_DOMAIN_CS: |
| 281 | proxy_subscr_new.cs.vlr_name_preliminary = req->source_name.ipa_name; |
| 282 | break; |
| 283 | case OSMO_GSUP_CN_DOMAIN_PS: |
| 284 | proxy_subscr_new.ps.vlr_name_preliminary = req->source_name.ipa_name; |
| 285 | break; |
| 286 | default: |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | ps = cs = false; |
| 291 | if (osmo_ipa_name_cmp(&proxy_subscr_new.cs.vlr_name_preliminary, &proxy_subscr->cs.vlr_name_preliminary)) |
| 292 | cs = true; |
| 293 | if (osmo_ipa_name_cmp(&proxy_subscr_new.ps.vlr_name_preliminary, &proxy_subscr->ps.vlr_name_preliminary)) |
| 294 | ps = true; |
| 295 | |
| 296 | if (!(cs || ps)) { |
| 297 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, LOGL_DEBUG, "VLR names remain unchanged\n"); |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | rc = proxy_subscr_create_or_update(proxy, &proxy_subscr_new); |
| 302 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, rc ? LOGL_ERROR : LOGL_INFO, |
| 303 | "%s: preliminary VLR name for%s%s to %s\n", |
| 304 | rc ? "failed to update" : "updated", |
| 305 | cs ? " CS" : "", ps ? " PS" : "", |
| 306 | osmo_gsup_peer_id_to_str(&req->source_name)); |
| 307 | break; |
| 308 | /* TODO: delete proxy entry in case of a Purge Request? */ |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* All GSUP messages received from the remote HLR to be sent to a local MSC pass through this function, to modify the |
| 316 | * subscriber state or disallow sending the message. Return 0 to allow sending the message. |
| 317 | * The local MSC shall be indicated by gsup.destination_name. */ |
| 318 | static int proxy_acknowledge_gsup_from_remote_hlr(struct proxy *proxy, const struct proxy_subscr *proxy_subscr, |
| 319 | const struct osmo_gsup_message *gsup, |
| 320 | struct remote_hlr *from_remote_hlr, |
| 321 | const struct osmo_ipa_name *destination, |
| 322 | const struct osmo_ipa_name *via_peer) |
| 323 | { |
| 324 | struct proxy_subscr proxy_subscr_new = *proxy_subscr; |
| 325 | bool ps; |
| 326 | bool cs; |
| 327 | bool vlr_name_changed_cs = false; |
| 328 | bool vlr_name_changed_ps = false; |
| 329 | int rc; |
| 330 | struct osmo_ipa_name via_proxy = {}; |
| 331 | if (osmo_ipa_name_cmp(via_peer, destination)) |
| 332 | via_proxy = *via_peer; |
| 333 | |
| 334 | switch (gsup->message_type) { |
| 335 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
| 336 | /* Remember the MSISDN of the subscriber. This does not need to be a preliminary record, because when |
| 337 | * the HLR tells us about subscriber data, it is definitive info and there is no ambiguity (like there |
| 338 | * would be with failed LU attempts from various sources). */ |
| 339 | if (!gsup->msisdn_enc_len) |
| 340 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_DEBUG, "No MSISDN in this Insert Data Request\n"); |
| 341 | else if (gsm48_decode_bcd_number2(proxy_subscr_new.msisdn, sizeof(proxy_subscr_new.msisdn), |
| 342 | gsup->msisdn_enc, gsup->msisdn_enc_len, 0)) |
| 343 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, "Failed to decode MSISDN\n"); |
| 344 | else if (!osmo_msisdn_str_valid(proxy_subscr_new.msisdn)) |
| 345 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, "invalid MSISDN: %s\n", |
| 346 | osmo_quote_str_c(OTC_SELECT, proxy_subscr_new.msisdn, -1)); |
| 347 | else if (!strcmp(proxy_subscr->msisdn, proxy_subscr_new.msisdn)) |
| 348 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_DEBUG, "already have MSISDN = %s\n", |
| 349 | proxy_subscr_new.msisdn); |
| 350 | else if (proxy_subscr_create_or_update(proxy, &proxy_subscr_new)) |
| 351 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, "failed to update MSISDN to %s\n", |
| 352 | proxy_subscr_new.msisdn); |
| 353 | else |
| 354 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_INFO, "stored MSISDN=%s\n", |
| 355 | proxy_subscr_new.msisdn); |
| 356 | break; |
| 357 | |
| 358 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
| 359 | /* Update the Location Updating timestamp */ |
| 360 | cs = ps = false; |
| 361 | if (!osmo_ipa_name_cmp(destination, &proxy_subscr->cs.vlr_name_preliminary)) { |
| 362 | timestamp_update(&proxy_subscr_new.cs.last_lu); |
| 363 | proxy_subscr_new.cs.vlr_name_preliminary = (struct osmo_ipa_name){}; |
| 364 | vlr_name_changed_cs = |
| 365 | osmo_ipa_name_cmp(&proxy_subscr->cs.vlr_name, destination) |
| 366 | || osmo_ipa_name_cmp(&proxy_subscr->cs.vlr_via_proxy, &via_proxy); |
| 367 | proxy_subscr_new.cs.vlr_name = *destination; |
| 368 | proxy_subscr_new.cs.vlr_via_proxy = via_proxy; |
| 369 | cs = true; |
| 370 | } |
| 371 | if (!osmo_ipa_name_cmp(destination, &proxy_subscr->ps.vlr_name_preliminary)) { |
| 372 | timestamp_update(&proxy_subscr_new.ps.last_lu); |
| 373 | proxy_subscr_new.ps.vlr_name_preliminary = (struct osmo_ipa_name){}; |
| 374 | proxy_subscr_new.ps.vlr_name = *destination; |
| 375 | vlr_name_changed_ps = |
| 376 | osmo_ipa_name_cmp(&proxy_subscr->ps.vlr_name, destination) |
| 377 | || osmo_ipa_name_cmp(&proxy_subscr->ps.vlr_via_proxy, &via_proxy); |
| 378 | proxy_subscr_new.ps.vlr_via_proxy = via_proxy; |
| 379 | ps = true; |
| 380 | } |
| 381 | if (!(cs || ps)) { |
| 382 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, |
| 383 | "destination is neither CS nor PS VLR: %s\n", |
| 384 | osmo_ipa_name_to_str(destination)); |
| 385 | return GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 386 | } |
| 387 | rc = proxy_subscr_create_or_update(proxy, &proxy_subscr_new); |
| 388 | |
| 389 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, rc ? LOGL_ERROR : LOGL_INFO, |
| 390 | "%s LU: timestamp for%s%s%s%s%s%s%s%s%s%s\n", |
| 391 | rc ? "failed to update" : "updated", |
| 392 | cs ? " CS" : "", ps ? " PS" : "", |
| 393 | vlr_name_changed_cs? ", CS VLR=" : "", |
| 394 | vlr_name_changed_cs? osmo_ipa_name_to_str(&proxy_subscr_new.cs.vlr_name) : "", |
| 395 | proxy_subscr_new.cs.vlr_via_proxy.len ? " via proxy " : "", |
| 396 | proxy_subscr_new.cs.vlr_via_proxy.len ? |
| 397 | osmo_ipa_name_to_str(&proxy_subscr_new.cs.vlr_via_proxy) : "", |
| 398 | vlr_name_changed_ps? ", PS VLR=" : "", |
| 399 | vlr_name_changed_ps? osmo_ipa_name_to_str(&proxy_subscr_new.ps.vlr_name) : "", |
| 400 | proxy_subscr_new.ps.vlr_via_proxy.len ? " via proxy " : "", |
| 401 | proxy_subscr_new.ps.vlr_via_proxy.len ? |
| 402 | osmo_ipa_name_to_str(&proxy_subscr_new.ps.vlr_via_proxy) : "" |
| 403 | ); |
| 404 | break; |
| 405 | |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void proxy_remote_hlr_connect_result_cb(const struct osmo_sockaddr_str *addr, struct remote_hlr *remote_hlr, |
| 414 | void *data) |
| 415 | { |
| 416 | struct proxy *proxy = data; |
| 417 | struct proxy_subscr_listentry *e; |
| 418 | if (!proxy) |
| 419 | return; |
| 420 | llist_for_each_entry(e, &proxy->subscr_list, entry) { |
| 421 | if (!osmo_sockaddr_str_cmp(addr, &e->data.remote_hlr_addr)) { |
| 422 | proxy_deferred_gsup_req_pop(proxy, e->data.imsi, remote_hlr); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Store the remote HLR's GSUP address for this proxy subscriber. |
| 428 | * This can be set before the remote_hlr is connected, or after. |
| 429 | * And, this can be set before the gsup_req has been queued for this HLR, or after. |
| 430 | */ |
| 431 | void proxy_subscr_remote_hlr_resolved(struct proxy *proxy, const struct proxy_subscr *proxy_subscr, |
| 432 | const struct osmo_sockaddr_str *remote_hlr_addr) |
| 433 | { |
| 434 | struct proxy_subscr proxy_subscr_new; |
| 435 | |
| 436 | if (osmo_sockaddr_str_is_nonzero(&proxy_subscr->remote_hlr_addr)) { |
| 437 | if (!osmo_sockaddr_str_cmp(remote_hlr_addr, &proxy_subscr->remote_hlr_addr)) { |
| 438 | /* Already have this remote address */ |
| 439 | return; |
| 440 | } else { |
| 441 | LOG_PROXY_SUBSCR(proxy_subscr, LOGL_NOTICE, |
| 442 | "Remote HLR address changes to " OSMO_SOCKADDR_STR_FMT "\n", |
| 443 | OSMO_SOCKADDR_STR_FMT_ARGS(remote_hlr_addr)); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /* Store the address. Make a copy to modify. */ |
| 448 | proxy_subscr_new = *proxy_subscr; |
| 449 | proxy_subscr = &proxy_subscr_new; |
| 450 | proxy_subscr_new.remote_hlr_addr = *remote_hlr_addr; |
| 451 | |
| 452 | if (proxy_subscr_create_or_update(proxy, proxy_subscr)) { |
| 453 | LOG_PROXY_SUBSCR(proxy_subscr, LOGL_ERROR, "Failed to store proxy entry for remote HLR\n"); |
| 454 | /* If no remote HLR is known for the IMSI, the proxy entry is pointless. */ |
| 455 | proxy_subscr_del(proxy, proxy_subscr->imsi); |
| 456 | return; |
| 457 | } |
| 458 | LOG_PROXY_SUBSCR(proxy_subscr, LOGL_DEBUG, "Remote HLR resolved, stored address\n"); |
| 459 | |
| 460 | /* If any messages for this HLR are already spooled, connect now. Otherwise wait for |
| 461 | * proxy_subscr_forward_to_remote_hlr() to connect then. */ |
| 462 | if (proxy_deferred_gsup_req_waiting(proxy, proxy_subscr->imsi)) |
| 463 | remote_hlr_get_or_connect(&proxy_subscr->remote_hlr_addr, true, |
| 464 | proxy_remote_hlr_connect_result_cb, proxy); |
| 465 | } |
| 466 | |
| 467 | int proxy_subscr_forward_to_remote_hlr(struct proxy *proxy, const struct proxy_subscr *proxy_subscr, struct osmo_gsup_req *req) |
| 468 | { |
| 469 | struct remote_hlr *remote_hlr; |
| 470 | int rc; |
| 471 | |
| 472 | rc = proxy_acknowledge_gsup_to_remote_hlr(proxy, proxy_subscr, req); |
| 473 | if (rc) { |
| 474 | osmo_gsup_req_respond_err(req, GMM_CAUSE_PROTO_ERR_UNSPEC, "Proxy does not allow this message"); |
| 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | if (!osmo_sockaddr_str_is_nonzero(&proxy_subscr->remote_hlr_addr)) { |
| 479 | /* We don't know the remote target yet. Still waiting for an MS lookup response, which will end up |
| 480 | * calling proxy_subscr_remote_hlr_resolved(). See dgsm.c. */ |
| 481 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, LOGL_DEBUG, "deferring until remote HLR is known\n"); |
| 482 | proxy_deferred_gsup_req_add(proxy, req); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | if (!osmo_gsup_peer_id_is_empty(&req->via_proxy)) { |
| 487 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, LOGL_INFO, "VLR->HLR: forwarding from %s via proxy %s\n", |
| 488 | osmo_gsup_peer_id_to_str(&req->source_name), |
| 489 | osmo_gsup_peer_id_to_str(&req->via_proxy)); |
| 490 | } else { |
| 491 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, &req->gsup, LOGL_INFO, "VLR->HLR: forwarding from %s\n", |
| 492 | osmo_gsup_peer_id_to_str(&req->source_name)); |
| 493 | } |
| 494 | |
| 495 | /* We could always store in the defer queue and empty the queue if the connection is already up. |
| 496 | * Slight optimisation: if the remote_hlr is already up and running, skip the defer queue. |
| 497 | * First ask for an existing remote_hlr. */ |
| 498 | remote_hlr = remote_hlr_get_or_connect(&proxy_subscr->remote_hlr_addr, false, NULL, NULL); |
| 499 | if (remote_hlr && remote_hlr_is_up(remote_hlr)) { |
| 500 | proxy_pending_req_remote_hlr_connect_result(req, remote_hlr); |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /* Not existing or not up. Defer req and ask to be notified when it is up. |
| 505 | * If the remote_hlr exists but is not connected yet, there should actually already be a pending |
| 506 | * proxy_remote_hlr_connect_result_cb queued, but it doesn't hurt to do that more often. */ |
| 507 | proxy_deferred_gsup_req_add(proxy, req); |
| 508 | remote_hlr_get_or_connect(&proxy_subscr->remote_hlr_addr, true, |
| 509 | proxy_remote_hlr_connect_result_cb, proxy); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int proxy_subscr_forward_to_vlr(struct proxy *proxy, const struct proxy_subscr *proxy_subscr, |
| 514 | const struct osmo_gsup_message *gsup, struct remote_hlr *from_remote_hlr) |
| 515 | { |
| 516 | struct osmo_ipa_name destination; |
| 517 | struct osmo_gsup_conn *vlr_conn; |
| 518 | struct msgb *msg; |
| 519 | |
| 520 | if (osmo_ipa_name_set(&destination, gsup->destination_name, gsup->destination_name_len)) { |
| 521 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, |
| 522 | "no valid Destination Name IE, cannot route to VLR.\n"); |
| 523 | return GMM_CAUSE_INV_MAND_INFO; |
| 524 | } |
| 525 | |
| 526 | /* Route to MSC/SGSN that we're proxying for */ |
| 527 | vlr_conn = gsup_route_find_by_ipa_name(proxy->gsup_server_to_vlr, &destination); |
| 528 | if (!vlr_conn) { |
| 529 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, |
| 530 | "Destination VLR unreachable: %s\n", osmo_ipa_name_to_str(&destination)); |
| 531 | return GMM_CAUSE_MSC_TEMP_NOTREACH; |
| 532 | } |
| 533 | |
| 534 | if (proxy_acknowledge_gsup_from_remote_hlr(proxy, proxy_subscr, gsup, from_remote_hlr, &destination, |
| 535 | &vlr_conn->peer_name)) { |
| 536 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, |
| 537 | "Proxy does not allow forwarding this message\n"); |
| 538 | return GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 539 | } |
| 540 | |
| 541 | msg = osmo_gsup_msgb_alloc("GSUP proxy to VLR"); |
| 542 | if (osmo_gsup_encode(msg, gsup)) { |
| 543 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_ERROR, |
| 544 | "Failed to re-encode GSUP message, cannot forward\n"); |
| 545 | return GMM_CAUSE_INV_MAND_INFO; |
| 546 | } |
| 547 | |
| 548 | LOG_PROXY_SUBSCR_MSG(proxy_subscr, gsup, LOGL_INFO, "VLR<-HLR: forwarding to %s%s%s\n", |
| 549 | osmo_ipa_name_to_str(&destination), |
| 550 | osmo_ipa_name_cmp(&destination, &vlr_conn->peer_name) ? " via " : "", |
| 551 | osmo_ipa_name_cmp(&destination, &vlr_conn->peer_name) ? |
| 552 | osmo_ipa_name_to_str(&vlr_conn->peer_name) : ""); |
| 553 | return osmo_gsup_conn_send(vlr_conn, msg); |
| 554 | } |