Harald Welte | 936f672 | 2016-05-03 18:51:18 +0200 | [diff] [blame] | 1 | /* (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 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 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 20 | #include <signal.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 22 | #include <stdbool.h> |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 23 | #include <getopt.h> |
| 24 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
Max | 20ddfdb | 2019-02-18 13:12:27 +0100 | [diff] [blame] | 26 | #include <osmocom/core/stats.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 27 | #include <osmocom/core/logging.h> |
| 28 | #include <osmocom/core/application.h> |
| 29 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 30 | #include <osmocom/vty/vty.h> |
| 31 | #include <osmocom/vty/command.h> |
| 32 | #include <osmocom/vty/telnet_interface.h> |
| 33 | #include <osmocom/vty/ports.h> |
Pau Espin Pedrol | ed18fa9 | 2020-08-18 12:58:36 +0200 | [diff] [blame] | 34 | #include <osmocom/vty/cpu_sched_vty.h> |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 35 | #include <osmocom/ctrl/control_vty.h> |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 36 | #include <osmocom/gsm/apn.h> |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 37 | #include <osmocom/gsm/gsm48_ie.h> |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 38 | #include <osmocom/gsm/gsm_utils.h> |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 39 | #include <osmocom/gsm/gsm23003.h> |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 40 | #include <osmocom/mslookup/mslookup_client.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | c79bcde | 2019-12-04 01:04:32 +0100 | [diff] [blame] | 42 | #include <osmocom/gsupclient/cni_peer_id.h> |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 43 | #include <osmocom/hlr/db.h> |
| 44 | #include <osmocom/hlr/hlr.h> |
| 45 | #include <osmocom/hlr/ctrl.h> |
| 46 | #include <osmocom/hlr/logging.h> |
| 47 | #include <osmocom/hlr/gsup_server.h> |
| 48 | #include <osmocom/hlr/gsup_router.h> |
| 49 | #include <osmocom/hlr/rand.h> |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 50 | #include <osmocom/hlr/hlr_vty.h> |
| 51 | #include <osmocom/hlr/hlr_ussd.h> |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 52 | #include <osmocom/hlr/dgsm.h> |
| 53 | #include <osmocom/hlr/proxy.h> |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 54 | #include <osmocom/hlr/lu_fsm.h> |
Neels Hofmeyr | 407925d | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 55 | #include <osmocom/mslookup/mdns.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 56 | |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 57 | struct hlr *g_hlr; |
Oliver Smith | c5044cf | 2019-02-26 16:32:57 +0100 | [diff] [blame] | 58 | static void *hlr_ctx = NULL; |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 59 | static int quit = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 60 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 61 | struct osmo_tdef g_hlr_tdefs[] = { |
| 62 | /* 4222 is also the OSMO_GSUP_PORT */ |
| 63 | { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" }, |
| 64 | {} |
| 65 | }; |
| 66 | |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 67 | /* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients. |
| 68 | * |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 69 | * \param[in] subscr A subscriber we have new data to send for. |
| 70 | */ |
| 71 | void |
| 72 | osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr) |
| 73 | { |
| 74 | struct osmo_gsup_conn *co; |
| 75 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 76 | if (g_hlr->gs == NULL) { |
| 77 | LOGP(DLGSUP, LOGL_DEBUG, |
| 78 | "IMSI %s: NOT Notifying peers of subscriber data change," |
| 79 | " there is no GSUP server\n", |
| 80 | subscr->imsi); |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 81 | return; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 82 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 83 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 84 | /* FIXME: send only to current vlr_number and sgsn_number */ |
| 85 | |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 86 | llist_for_each_entry(co, &g_hlr->gs->clients, list) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 87 | struct osmo_gsup_message gsup = { }; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 88 | struct msgb *msg_out; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 89 | uint8_t *peer; |
| 90 | int peer_len; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 91 | size_t peer_strlen; |
| 92 | const char *peer_compare; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 93 | enum osmo_gsup_cn_domain cn_domain; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 94 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 95 | if (co->supports_ps) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 96 | cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 97 | peer_compare = subscr->sgsn_number; |
| 98 | } else if (co->supports_cs) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 99 | cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 100 | peer_compare = subscr->vlr_number; |
| 101 | } else { |
| 102 | /* We have not yet received a location update from this GSUP client.*/ |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 106 | peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR); |
| 107 | if (peer_len < 0) { |
| 108 | LOGP(DLGSUP, LOGL_ERROR, |
| 109 | "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi, |
| 110 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 111 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | peer_strlen = strnlen((const char*)peer, peer_len); |
| 116 | if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) { |
| 117 | /* Mismatch. The subscriber is not subscribed with this GSUP client. */ |
| 118 | /* I hope peer is always nul terminated... */ |
| 119 | if (peer_strlen < peer_len) |
| 120 | LOGP(DLGSUP, LOGL_DEBUG, |
| 121 | "IMSI %s: subscriber change: skipping %s peer %s\n", |
| 122 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 123 | osmo_quote_str((char*)peer, -1)); |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | LOGP(DLGSUP, LOGL_DEBUG, |
| 128 | "IMSI %s: subscriber change: notifying %s peer %s\n", |
| 129 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 130 | osmo_quote_str(peer_compare, -1)); |
| 131 | |
Alexander Couzens | 2989873 | 2023-04-11 17:53:33 +0200 | [diff] [blame] | 132 | if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, cn_domain, OTC_SELECT) != 0) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 133 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 134 | "IMSI='%s': Cannot notify GSUP client; could not create gsup message " |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 135 | "for %s:%u\n", subscr->imsi, |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 136 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 137 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 138 | continue; |
| 139 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 140 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 141 | /* Send ISD to MSC/SGSN */ |
Neels Hofmeyr | a7d0f87 | 2019-10-30 02:08:28 +0100 | [diff] [blame] | 142 | msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE"); |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 143 | if (msg_out == NULL) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 144 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 145 | "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer " |
| 146 | "for %s:%u\n", subscr->imsi, |
| 147 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 148 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 149 | continue; |
| 150 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 151 | osmo_gsup_encode(msg_out, &gsup); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 152 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 153 | if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) { |
| 154 | LOGP(DMAIN, LOGL_ERROR, |
| 155 | "IMSI='%s': Cannot notify GSUP client; send operation failed " |
| 156 | "for %s:%u\n", subscr->imsi, |
| 157 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 158 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 159 | continue; |
| 160 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 164 | static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len) |
| 165 | { |
| 166 | int i, j, rc; |
| 167 | uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS]; |
| 168 | |
| 169 | OSMO_ASSERT(len <= sizeof(rand_buf)); |
| 170 | |
| 171 | /* Generate a random unique MSISDN (with retry) */ |
| 172 | for (i = 0; i < 10; i++) { |
| 173 | /* Get a random number (with retry) */ |
| 174 | for (j = 0; j < 10; j++) { |
| 175 | rc = osmo_get_rand_id(rand_buf, len); |
| 176 | if (!rc) |
| 177 | break; |
| 178 | } |
| 179 | if (rc) { |
| 180 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator" |
| 181 | " failed (rc=%d)\n", imsi, rc); |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */ |
| 186 | for (j = 0; j < len; j++) |
| 187 | msisdn[j] = 48 + (rand_buf[j] % 10); |
| 188 | msisdn[j] = '\0'; |
| 189 | |
| 190 | /* Ensure there is no subscriber with such MSISDN */ |
| 191 | if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT) |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* Failure */ |
| 196 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing " |
| 197 | "the length for the automatically assigned MSISDNs " |
| 198 | "(see 'subscriber-create-on-demand' command)\n", imsi); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | static int subscr_create_on_demand(const char *imsi) |
| 203 | { |
| 204 | char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1]; |
| 205 | int rc; |
| 206 | unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len; |
| 207 | |
| 208 | if (!g_hlr->subscr_create_on_demand) |
| 209 | return -1; |
| 210 | if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0) |
| 211 | return -1; |
| 212 | if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0) |
| 213 | return -1; |
| 214 | |
| 215 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi); |
| 216 | rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags); |
| 217 | if (rc) { |
| 218 | LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | if (!rand_msisdn_len) |
| 223 | return 0; |
| 224 | |
| 225 | /* Update MSISDN of the new (just allocated) subscriber */ |
| 226 | rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn); |
| 227 | if (rc) { |
| 228 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc); |
| 229 | return rc; |
| 230 | } |
| 231 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 236 | /*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients. |
| 237 | * \param[in,out] hlr Global hlr context. |
| 238 | * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call. |
| 239 | * \param[in] nam_val True to enable CS/PS, false to disable. |
| 240 | * \param[in] is_ps True to enable/disable PS, false for CS. |
| 241 | * \returns 0 on success, ENOEXEC if there is no need to change, a negative |
| 242 | * value on error. |
| 243 | */ |
| 244 | int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps) |
| 245 | { |
| 246 | int rc; |
| 247 | bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs; |
| 248 | struct osmo_ipa_name vlr_name; |
| 249 | struct osmo_gsup_message gsup_del_data = { |
| 250 | .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST, |
| 251 | }; |
| 252 | OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi); |
| 253 | |
| 254 | if (is_val == nam_val) { |
| 255 | LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n", |
| 256 | subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS"); |
| 257 | return ENOEXEC; |
| 258 | } |
| 259 | |
| 260 | rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps); |
| 261 | if (rc) |
| 262 | return rc > 0? -rc : rc; |
| 263 | |
| 264 | /* If we're disabling, send a notice out to the GSUP client that is |
| 265 | * responsible. Otherwise no need. */ |
| 266 | if (nam_val) |
| 267 | return 0; |
| 268 | |
Neels Hofmeyr | 9b8e7b4 | 2020-05-04 19:20:23 +0200 | [diff] [blame] | 269 | if (subscr->vlr_number[0] && !osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number)) |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 270 | osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data); |
Neels Hofmeyr | 9b8e7b4 | 2020-05-04 19:20:23 +0200 | [diff] [blame] | 271 | if (subscr->sgsn_number[0] && !osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number)) |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 272 | osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data); |
| 273 | return 0; |
| 274 | } |
| 275 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 276 | /*********************************************************************** |
| 277 | * Send Auth Info handling |
| 278 | ***********************************************************************/ |
| 279 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 280 | /* process an incoming SAI request */ |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 281 | static int rx_send_auth_info(struct osmo_gsup_req *req) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 282 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 283 | struct osmo_gsup_message gsup_out = { |
| 284 | .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT, |
| 285 | }; |
Harald Welte | 06f5af2 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 286 | bool separation_bit = false; |
Alexander Couzens | 81b92bb | 2020-01-07 19:05:13 +0100 | [diff] [blame] | 287 | int num_auth_vectors = OSMO_GSUP_MAX_NUM_AUTH_INFO; |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 288 | unsigned int auc_3g_ind; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 289 | int rc; |
| 290 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 291 | subscr_create_on_demand(req->gsup.imsi); |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 292 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 293 | if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS) |
Harald Welte | 06f5af2 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 294 | separation_bit = true; |
| 295 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 296 | if (req->gsup.num_auth_vectors > 0 && |
| 297 | req->gsup.num_auth_vectors <= OSMO_GSUP_MAX_NUM_AUTH_INFO) |
| 298 | num_auth_vectors = req->gsup.num_auth_vectors; |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 299 | rc = db_ind(g_hlr->dbc, &req->source_name, &auc_3g_ind); |
| 300 | if (rc) { |
| 301 | LOG_GSUP_REQ(req, LOGL_ERROR, |
| 302 | "Unable to determine 3G auth IND for source %s (rc=%d)," |
| 303 | " generating tuples with IND = 0\n", |
| 304 | osmo_cni_peer_id_to_str(&req->source_name), rc); |
| 305 | auc_3g_ind = 0; |
| 306 | } |
Alexander Couzens | 81b92bb | 2020-01-07 19:05:13 +0100 | [diff] [blame] | 307 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 308 | rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind, |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 309 | gsup_out.auth_vectors, |
Alexander Couzens | 81b92bb | 2020-01-07 19:05:13 +0100 | [diff] [blame] | 310 | num_auth_vectors, |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 311 | req->gsup.rand, req->gsup.auts, separation_bit); |
| 312 | |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 313 | if (rc <= 0) { |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 314 | switch (rc) { |
| 315 | case 0: |
Neels Hofmeyr | bd1dca0 | 2017-11-23 15:25:30 +0100 | [diff] [blame] | 316 | /* 0 means "0 tuples generated", which shouldn't happen. |
| 317 | * Treat the same as "no auth data". */ |
| 318 | case -ENOKEY: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 319 | osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, |
| 320 | "IMSI known, but has no auth data;" |
| 321 | " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP"); |
| 322 | return rc; |
Neels Hofmeyr | 33cbde9 | 2017-11-22 20:39:59 +0100 | [diff] [blame] | 323 | case -ENOENT: |
Alexander Couzens | 268a33e | 2020-01-12 00:48:07 +0100 | [diff] [blame] | 324 | osmo_gsup_req_respond_err(req, g_hlr->reject_cause, "IMSI unknown"); |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 325 | return rc; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 326 | default: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 327 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db"); |
| 328 | return rc; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 329 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 330 | } |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 331 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 332 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 333 | osmo_gsup_req_respond(req, &gsup_out, false, true); |
| 334 | return 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 337 | /*! Receive Update Location Request, creates new lu_operation */ |
| 338 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 339 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 340 | switch (req->gsup.cn_domain) { |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 341 | case OSMO_GSUP_CN_DOMAIN_CS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 342 | conn->supports_cs = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 343 | break; |
| 344 | default: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 345 | /* The client didn't send a CN_DOMAIN IE; assume packet-switched in |
| 346 | * accordance with the GSUP spec in osmo-hlr's user manual (section |
| 347 | * 11.6.15 "CN Domain" says "if no CN Domain IE is present within |
| 348 | * a request, the PS Domain is assumed." */ |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 349 | case OSMO_GSUP_CN_DOMAIN_PS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 350 | conn->supports_ps = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 351 | break; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 352 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 353 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 354 | subscr_create_on_demand(req->gsup.imsi); |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 355 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 356 | lu_rx_gsup(req); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 360 | static int rx_purge_ms_req(struct osmo_gsup_req *req) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 361 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 362 | bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 363 | int rc; |
| 364 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 365 | LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS"); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 366 | |
| 367 | /* FIXME: check if the VLR that sends the purge is the same that |
| 368 | * we have on record. Only update if yes */ |
| 369 | |
| 370 | /* Perform the actual update of the DB */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 371 | rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 372 | |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 373 | if (rc == 0) |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 374 | osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true); |
| 375 | else if (rc == -ENOENT) |
| 376 | osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown"); |
| 377 | else |
| 378 | osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error"); |
| 379 | return rc; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 382 | static int rx_check_imei_req(struct osmo_gsup_req *req) |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 383 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 384 | struct osmo_gsup_message gsup_reply; |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 385 | char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0}; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 386 | const struct osmo_gsup_message *gsup = &req->gsup; |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 387 | int rc; |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 388 | |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 389 | /* Require IMEI */ |
| 390 | if (!gsup->imei_enc) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 391 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI"); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 392 | return -1; |
| 393 | } |
| 394 | |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 395 | /* Decode IMEI (fails if IMEI is too long) */ |
| 396 | rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0); |
| 397 | if (rc < 0) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 398 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, |
| 399 | "failed to decode IMEI %s (rc: %d)", |
| 400 | osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len), |
| 401 | rc); |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | /* Check if IMEI is too short */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 406 | if (!osmo_imei_str_valid(imei, false)) { |
| 407 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, |
| 408 | "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1)); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 412 | subscr_create_on_demand(gsup->imsi); |
| 413 | |
Oliver Smith | 851814a | 2019-01-11 15:30:21 +0100 | [diff] [blame] | 414 | /* Save in DB if desired */ |
| 415 | if (g_hlr->store_imei) { |
| 416 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei); |
| 417 | if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 418 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "Failed to store IMEI in HLR db"); |
Oliver Smith | 851814a | 2019-01-11 15:30:21 +0100 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | } else { |
| 422 | /* Check if subscriber exists and print IMEI */ |
| 423 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei); |
| 424 | struct hlr_subscriber subscr; |
| 425 | if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 426 | osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "IMSI unknown"); |
Oliver Smith | 851814a | 2019-01-11 15:30:21 +0100 | [diff] [blame] | 427 | return -1; |
| 428 | } |
| 429 | } |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 430 | |
| 431 | /* Accept all IMEIs */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 432 | gsup_reply = (struct osmo_gsup_message){ |
| 433 | .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT, |
| 434 | .imei_result = OSMO_GSUP_IMEI_RESULT_ACK, |
| 435 | }; |
| 436 | return osmo_gsup_req_respond(req, &gsup_reply, false, true); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 439 | static char namebuf[255]; |
| 440 | #define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \ |
| 441 | LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \ |
Neels Hofmeyr | fbe4929 | 2020-01-09 18:35:03 +0100 | [diff] [blame] | 442 | osmo_gsup_message_type_name((gsup)->message_type), \ |
| 443 | osmo_gsup_message_class_name((gsup)->message_class), \ |
| 444 | (gsup)->imsi, \ |
| 445 | osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \ |
| 446 | osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \ |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 447 | ## args) |
| 448 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 449 | static int read_cb_forward(struct osmo_gsup_req *req) |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 450 | { |
| 451 | int ret = -EINVAL; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 452 | const struct osmo_gsup_message *gsup = &req->gsup; |
| 453 | struct osmo_gsup_message gsup_err; |
| 454 | struct msgb *forward_msg; |
| 455 | struct osmo_ipa_name destination_name; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 456 | |
| 457 | /* Check for routing IEs */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 458 | if (!req->gsup.source_name || !req->gsup.source_name_len |
| 459 | || !req->gsup.destination_name || !req->gsup.destination_name_len) { |
| 460 | LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n"); |
| 461 | goto routing_error; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 464 | if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) { |
| 465 | LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n"); |
| 466 | goto routing_error; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 469 | LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name)); |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 470 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 471 | /* Forward message without re-encoding (so we don't remove unknown IEs). |
| 472 | * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */ |
| 473 | forward_msg = osmo_gsup_msgb_alloc("GSUP forward"); |
| 474 | forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg)); |
| 475 | memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg)); |
| 476 | ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg); |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 477 | if (ret) { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 478 | LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n", |
| 479 | ret == -ENODEV ? "destination not connected" : "unknown error", |
| 480 | ret); |
| 481 | goto routing_error; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 482 | } |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 483 | osmo_gsup_req_free(req); |
| 484 | return 0; |
| 485 | |
| 486 | routing_error: |
| 487 | gsup_err = (struct osmo_gsup_message){ |
| 488 | .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR, |
| 489 | .source_name = gsup->destination_name, |
| 490 | .source_name_len = gsup->destination_name_len, |
| 491 | }; |
| 492 | osmo_gsup_req_respond(req, &gsup_err, true, true); |
| 493 | return -1; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 496 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 497 | { |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 498 | struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg); |
| 499 | if (!req) |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame] | 500 | return -EINVAL; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 501 | |
| 502 | /* If the GSUP recipient is other than this HLR, forward. */ |
| 503 | if (req->gsup.destination_name_len) { |
| 504 | struct osmo_ipa_name destination_name; |
| 505 | struct osmo_ipa_name my_name; |
| 506 | osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno); |
| 507 | if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len) |
| 508 | && osmo_ipa_name_cmp(&destination_name, &my_name)) { |
| 509 | return read_cb_forward(req); |
| 510 | } |
Vadim Yanitskiy | c69a18b | 2019-05-07 20:53:54 +0700 | [diff] [blame] | 511 | } |
| 512 | |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 513 | /* Distributed GSM: check whether to proxy for / lookup a remote HLR. |
| 514 | * It would require less database hits to do this only if a local-only operation fails with "unknown IMSI", but |
| 515 | * it becomes semantically easier if we do this once-off ahead of time. */ |
| 516 | if (osmo_mslookup_client_active(g_hlr->mslookup.client.client) |
| 517 | || osmo_sockaddr_str_is_nonzero(&g_hlr->mslookup.client.gsup_gateway_proxy)) { |
| 518 | if (dgsm_check_forward_gsup_msg(req)) |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* HLR related messages that are handled at this HLR instance */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 523 | switch (req->gsup.message_type) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 524 | /* requests sent to us */ |
| 525 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
Neels Hofmeyr | 3f9d197 | 2019-12-12 04:04:53 +0100 | [diff] [blame] | 526 | rx_send_auth_info(req); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 527 | break; |
| 528 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 529 | rx_upd_loc_req(conn, req); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 530 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 531 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 532 | rx_purge_ms_req(req); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 533 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 534 | /* responses to requests sent by us */ |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 535 | case OSMO_GSUP_MSGT_DELETE_DATA_ERROR: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 536 | LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n"); |
| 537 | osmo_gsup_req_free(req); |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 538 | break; |
| 539 | case OSMO_GSUP_MSGT_DELETE_DATA_RESULT: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 540 | LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n"); |
| 541 | osmo_gsup_req_free(req); |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 542 | break; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 543 | case OSMO_GSUP_MSGT_PROC_SS_REQUEST: |
| 544 | case OSMO_GSUP_MSGT_PROC_SS_RESULT: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 545 | rx_proc_ss_req(req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 546 | break; |
| 547 | case OSMO_GSUP_MSGT_PROC_SS_ERROR: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 548 | rx_proc_ss_error(req); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 549 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 550 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 551 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 552 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 553 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 554 | lu_rx_gsup(req); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 555 | break; |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 556 | case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST: |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 557 | rx_check_imei_req(req); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 558 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 559 | default: |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 560 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n", |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 561 | osmo_gsup_message_type_name(req->gsup.message_type)); |
| 562 | osmo_gsup_req_free(req); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
Harald Welte | 7a47653 | 2022-11-03 11:38:41 +0100 | [diff] [blame] | 568 | static void print_usage(void) |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 569 | { |
| 570 | printf("Usage: osmo-hlr\n"); |
| 571 | } |
| 572 | |
Harald Welte | 7a47653 | 2022-11-03 11:38:41 +0100 | [diff] [blame] | 573 | static void print_help(void) |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 574 | { |
| 575 | printf(" -h --help This text.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 576 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 577 | printf(" -l --database db-name The database to use.\n"); |
| 578 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 579 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 580 | printf(" -s --disable-color Do not print ANSI colors in the log\n"); |
| 581 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 582 | printf(" -e --log-level number Set a global loglevel.\n"); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 583 | printf(" -U --db-upgrade Allow HLR database schema upgrades.\n"); |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 584 | printf(" -C --db-check Quit after opening (and upgrading) the database.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 585 | printf(" -V --version Print the version of OsmoHLR.\n"); |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 586 | |
| 587 | printf("\nVTY reference generation:\n"); |
| 588 | printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"); |
| 589 | printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static struct { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 593 | const char *config_file; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 594 | const char *db_file; |
| 595 | bool daemonize; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 596 | bool db_upgrade; |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 597 | bool db_check; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 598 | } cmdline_opts = { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 599 | .config_file = "osmo-hlr.cfg", |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 600 | .db_file = NULL, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 601 | .daemonize = false, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 602 | .db_upgrade = false, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 603 | }; |
| 604 | |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 605 | static void handle_long_options(const char *prog_name, const int long_option) |
| 606 | { |
| 607 | static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT; |
| 608 | |
| 609 | switch (long_option) { |
| 610 | case 1: |
| 611 | vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg); |
| 612 | if (vty_ref_mode < 0) { |
| 613 | fprintf(stderr, "%s: Unknown VTY reference generation " |
| 614 | "mode '%s'\n", prog_name, optarg); |
| 615 | exit(2); |
| 616 | } |
| 617 | break; |
| 618 | case 2: |
| 619 | fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n", |
| 620 | get_value_string(vty_ref_gen_mode_names, vty_ref_mode), |
| 621 | get_value_string(vty_ref_gen_mode_desc, vty_ref_mode)); |
| 622 | vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode); |
| 623 | exit(0); |
| 624 | default: |
| 625 | fprintf(stderr, "%s: error parsing cmdline options\n", prog_name); |
| 626 | exit(2); |
| 627 | } |
| 628 | |
| 629 | } |
| 630 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 631 | static void handle_options(int argc, char **argv) |
| 632 | { |
| 633 | while (1) { |
| 634 | int option_index = 0, c; |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 635 | static int long_option = 0; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 636 | static struct option long_options[] = { |
| 637 | {"help", 0, 0, 'h'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 638 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 639 | {"database", 1, 0, 'l'}, |
| 640 | {"debug", 1, 0, 'd'}, |
| 641 | {"daemonize", 0, 0, 'D'}, |
| 642 | {"disable-color", 0, 0, 's'}, |
| 643 | {"log-level", 1, 0, 'e'}, |
| 644 | {"timestamp", 0, 0, 'T'}, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 645 | {"db-upgrade", 0, 0, 'U' }, |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 646 | {"db-check", 0, 0, 'C' }, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 647 | {"version", 0, 0, 'V' }, |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 648 | {"vty-ref-mode", 1, &long_option, 1}, |
| 649 | {"vty-ref-xml", 0, &long_option, 2}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 650 | {0, 0, 0, 0} |
| 651 | }; |
| 652 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 653 | c = getopt_long(argc, argv, "hc:l:d:Dse:TUV", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 654 | long_options, &option_index); |
| 655 | if (c == -1) |
| 656 | break; |
| 657 | |
| 658 | switch (c) { |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 659 | case 0: |
| 660 | handle_long_options(argv[0], long_option); |
| 661 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 662 | case 'h': |
| 663 | print_usage(); |
| 664 | print_help(); |
| 665 | exit(0); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 666 | case 'c': |
| 667 | cmdline_opts.config_file = optarg; |
| 668 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 669 | case 'l': |
| 670 | cmdline_opts.db_file = optarg; |
| 671 | break; |
| 672 | case 'd': |
| 673 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 674 | break; |
| 675 | case 'D': |
| 676 | cmdline_opts.daemonize = 1; |
| 677 | break; |
| 678 | case 's': |
| 679 | log_set_use_color(osmo_stderr_target, 0); |
| 680 | break; |
| 681 | case 'e': |
| 682 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 683 | break; |
| 684 | case 'T': |
| 685 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 686 | break; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 687 | case 'U': |
| 688 | cmdline_opts.db_upgrade = true; |
| 689 | break; |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 690 | case 'C': |
| 691 | cmdline_opts.db_check = true; |
| 692 | break; |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 693 | case 'V': |
| 694 | print_version(1); |
| 695 | exit(0); |
| 696 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 697 | default: |
| 698 | /* catch unknown options *as well as* missing arguments. */ |
| 699 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 700 | exit(-1); |
| 701 | break; |
| 702 | } |
| 703 | } |
Harald Welte | 80dc9ae | 2019-12-03 22:16:39 +0100 | [diff] [blame] | 704 | |
| 705 | if (argc > optind) { |
| 706 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 707 | exit(2); |
| 708 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 711 | static void signal_hdlr(int signal) |
| 712 | { |
| 713 | switch (signal) { |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 714 | case SIGTERM: |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 715 | case SIGINT: |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 716 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 717 | quit++; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 718 | break; |
| 719 | case SIGUSR1: |
| 720 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 721 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 722 | break; |
| 723 | } |
| 724 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 725 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 726 | static const char vlr_copyright[] = |
Harald Welte | a854b48 | 2023-05-30 17:27:32 +0200 | [diff] [blame] | 727 | "Copyright (C) 2016-2023 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n" |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 728 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 729 | "This is free software: you are free to change and redistribute it.\r\n" |
| 730 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 731 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 732 | static struct vty_app_info vty_info = { |
| 733 | .name = "OsmoHLR", |
| 734 | .version = PACKAGE_VERSION, |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 735 | .copyright = vlr_copyright, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 736 | .is_config_node = hlr_vty_is_config_node, |
Pau Espin Pedrol | ce9bc40 | 2017-05-31 13:19:22 +0200 | [diff] [blame] | 737 | .go_parent_cb = hlr_vty_go_parent, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 738 | }; |
| 739 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 740 | int main(int argc, char **argv) |
| 741 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 742 | int rc; |
| 743 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 744 | /* Track the use of talloc NULL memory contexts */ |
| 745 | talloc_enable_null_tracking(); |
| 746 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 747 | hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR"); |
| 748 | msgb_talloc_ctx_init(hlr_ctx, 0); |
Harald Welte | 7ee6e55 | 2018-02-14 00:52:05 +0100 | [diff] [blame] | 749 | vty_info.tall_ctx = hlr_ctx; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 750 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 751 | g_hlr = talloc_zero(hlr_ctx, struct hlr); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 752 | INIT_LLIST_HEAD(&g_hlr->euse_list); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 753 | INIT_LLIST_HEAD(&g_hlr->ss_sessions); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 754 | INIT_LLIST_HEAD(&g_hlr->ussd_routes); |
Neels Hofmeyr | ab7dc40 | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 755 | INIT_LLIST_HEAD(&g_hlr->mslookup.server.local_site_services); |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 756 | g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH); |
Neels Hofmeyr | 407925d | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 757 | g_hlr->mslookup.server.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT); |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 758 | g_hlr->mslookup.client.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT); |
Alexander Couzens | 268a33e | 2020-01-12 00:48:07 +0100 | [diff] [blame] | 759 | g_hlr->reject_cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 760 | g_hlr->no_proxy_reject_cause = GMM_CAUSE_IMSI_UNKNOWN; |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 761 | |
Vadim Yanitskiy | d157a56 | 2018-12-01 00:03:39 +0700 | [diff] [blame] | 762 | /* Init default (call independent) SS session guard timeout value */ |
| 763 | g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT; |
| 764 | |
Pau Espin Pedrol | 5153031 | 2018-04-17 15:07:06 +0200 | [diff] [blame] | 765 | rc = osmo_init_logging2(hlr_ctx, &hlr_log_info); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 766 | if (rc < 0) { |
| 767 | fprintf(stderr, "Error initializing logging\n"); |
| 768 | exit(1); |
| 769 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 770 | |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 771 | /* Set up llists and objects, startup is happening from VTY commands. */ |
| 772 | dgsm_init(hlr_ctx); |
| 773 | |
Max | 20ddfdb | 2019-02-18 13:12:27 +0100 | [diff] [blame] | 774 | osmo_stats_init(hlr_ctx); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 775 | vty_init(&vty_info); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 776 | ctrl_vty_init(hlr_ctx); |
Alexander Couzens | 268a33e | 2020-01-12 00:48:07 +0100 | [diff] [blame] | 777 | hlr_vty_init(hlr_ctx); |
Neels Hofmeyr | 407925d | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 778 | dgsm_vty_init(); |
Pau Espin Pedrol | ed18fa9 | 2020-08-18 12:58:36 +0200 | [diff] [blame] | 779 | osmo_cpu_sched_vty_init(hlr_ctx); |
Harald Welte | 2bd1a45 | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 780 | handle_options(argc, argv); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 781 | |
| 782 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
| 783 | if (rc < 0) { |
| 784 | LOGP(DMAIN, LOGL_FATAL, |
| 785 | "Failed to parse the config file: '%s'\n", |
| 786 | cmdline_opts.config_file); |
| 787 | return rc; |
| 788 | } |
| 789 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 790 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 791 | |
| 792 | rc = rand_init(); |
| 793 | if (rc < 0) { |
| 794 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 795 | exit(1); |
| 796 | } |
| 797 | |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 798 | if (cmdline_opts.db_file) |
| 799 | osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file); |
| 800 | |
| 801 | g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 802 | if (!g_hlr->dbc) { |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 803 | LOGP(DMAIN, LOGL_FATAL, "Error opening database %s\n", osmo_quote_str(g_hlr->db_file_path, -1)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 804 | exit(1); |
| 805 | } |
| 806 | |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 807 | if (cmdline_opts.db_check) { |
| 808 | LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n"); |
| 809 | db_close(g_hlr->dbc); |
| 810 | log_fini(); |
| 811 | talloc_free(hlr_ctx); |
| 812 | talloc_free(tall_vty_ctx); |
| 813 | talloc_disable_null_tracking(); |
| 814 | exit(0); |
| 815 | } |
| 816 | |
| 817 | /* start telnet after reading config for vty_get_bind_addr() */ |
arehbein | e414323 | 2022-12-19 21:28:53 +0100 | [diff] [blame] | 818 | rc = telnet_init_default(hlr_ctx, NULL, OSMO_VTY_PORT_HLR); |
Neels Hofmeyr | c3d4032 | 2019-10-31 01:39:05 +0100 | [diff] [blame] | 819 | if (rc < 0) |
| 820 | return rc; |
| 821 | |
| 822 | |
Neels Hofmeyr | 84201d3 | 2017-07-21 16:00:32 +0200 | [diff] [blame] | 823 | g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT, |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 824 | read_cb, g_hlr); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 825 | if (!g_hlr->gs) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 826 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 827 | exit(1); |
| 828 | } |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 829 | proxy_init(g_hlr->gs); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 830 | |
Neels Hofmeyr | 234f9cb | 2017-10-24 17:23:04 +0200 | [diff] [blame] | 831 | g_hlr->ctrl = hlr_controlif_setup(g_hlr); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 832 | |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 833 | dgsm_start(hlr_ctx); |
| 834 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 835 | osmo_init_ignore_signals(); |
| 836 | signal(SIGINT, &signal_hdlr); |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 837 | signal(SIGTERM, &signal_hdlr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 838 | signal(SIGUSR1, &signal_hdlr); |
| 839 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 840 | if (cmdline_opts.daemonize) { |
| 841 | rc = osmo_daemonize(); |
| 842 | if (rc < 0) { |
| 843 | perror("Error during daemonize"); |
| 844 | exit(1); |
| 845 | } |
| 846 | } |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 847 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 848 | while (!quit) |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 849 | osmo_select_main_ctx(0); |
| 850 | |
Neels Hofmeyr | 76328bd | 2019-11-20 03:35:37 +0100 | [diff] [blame] | 851 | dgsm_stop(); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 852 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 853 | osmo_gsup_server_destroy(g_hlr->gs); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 854 | db_close(g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 855 | log_fini(); |
| 856 | |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 857 | /** |
| 858 | * Report the heap state of root context, then free, |
| 859 | * so both ASAN and Valgrind are happy... |
| 860 | */ |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 861 | talloc_report_full(hlr_ctx, stderr); |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 862 | talloc_free(hlr_ctx); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 863 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 864 | /* FIXME: VTY code still uses NULL-context */ |
| 865 | talloc_free(tall_vty_ctx); |
| 866 | |
| 867 | /** |
| 868 | * Report the heap state of NULL context, then free, |
| 869 | * so both ASAN and Valgrind are happy... |
| 870 | */ |
| 871 | talloc_report_full(NULL, stderr); |
| 872 | talloc_disable_null_tracking(); |
| 873 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 874 | return 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 875 | } |