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> |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 34 | #include <osmocom/ctrl/control_vty.h> |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 35 | #include <osmocom/gsm/apn.h> |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/gsm48_ie.h> |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 37 | #include <osmocom/gsm/gsm_utils.h> |
| 38 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 39 | |
| 40 | #include "db.h" |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 41 | #include "hlr.h" |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 42 | #include "ctrl.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 43 | #include "logging.h" |
| 44 | #include "gsup_server.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 45 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 46 | #include "rand.h" |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 47 | #include "luop.h" |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 48 | #include "hlr_vty.h" |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 49 | #include "hlr_ussd.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 50 | |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 51 | struct hlr *g_hlr; |
Oliver Smith | c5044cf | 2019-02-26 16:32:57 +0100 | [diff] [blame] | 52 | static void *hlr_ctx = NULL; |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 53 | static int quit = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 54 | |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 55 | /* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients. |
| 56 | * |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 57 | * \param[in] subscr A subscriber we have new data to send for. |
| 58 | */ |
| 59 | void |
| 60 | osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr) |
| 61 | { |
| 62 | struct osmo_gsup_conn *co; |
| 63 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 64 | if (g_hlr->gs == NULL) { |
| 65 | LOGP(DLGSUP, LOGL_DEBUG, |
| 66 | "IMSI %s: NOT Notifying peers of subscriber data change," |
| 67 | " there is no GSUP server\n", |
| 68 | subscr->imsi); |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 69 | return; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 70 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 71 | |
| 72 | llist_for_each_entry(co, &g_hlr->gs->clients, list) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 73 | struct osmo_gsup_message gsup = { }; |
| 74 | uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN]; |
| 75 | uint8_t apn[APN_MAXLEN]; |
| 76 | struct msgb *msg_out; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 77 | uint8_t *peer; |
| 78 | int peer_len; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 79 | size_t peer_strlen; |
| 80 | const char *peer_compare; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 81 | enum osmo_gsup_cn_domain cn_domain; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 82 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 83 | if (co->supports_ps) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 84 | cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 85 | peer_compare = subscr->sgsn_number; |
| 86 | } else if (co->supports_cs) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 87 | cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 88 | peer_compare = subscr->vlr_number; |
| 89 | } else { |
| 90 | /* We have not yet received a location update from this GSUP client.*/ |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 94 | peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR); |
| 95 | if (peer_len < 0) { |
| 96 | LOGP(DLGSUP, LOGL_ERROR, |
| 97 | "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi, |
| 98 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 99 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | peer_strlen = strnlen((const char*)peer, peer_len); |
| 104 | if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) { |
| 105 | /* Mismatch. The subscriber is not subscribed with this GSUP client. */ |
| 106 | /* I hope peer is always nul terminated... */ |
| 107 | if (peer_strlen < peer_len) |
| 108 | LOGP(DLGSUP, LOGL_DEBUG, |
| 109 | "IMSI %s: subscriber change: skipping %s peer %s\n", |
| 110 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 111 | osmo_quote_str((char*)peer, -1)); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | LOGP(DLGSUP, LOGL_DEBUG, |
| 116 | "IMSI %s: subscriber change: notifying %s peer %s\n", |
| 117 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 118 | osmo_quote_str(peer_compare, -1)); |
| 119 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 120 | if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc, |
| 121 | sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 122 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 123 | "IMSI='%s': Cannot notify GSUP client; could not create gsup message " |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 124 | "for %s:%u\n", subscr->imsi, |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 125 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 126 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 127 | continue; |
| 128 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 129 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 130 | /* Send ISD to MSC/SGSN */ |
| 131 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE"); |
| 132 | if (msg_out == NULL) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 133 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 134 | "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer " |
| 135 | "for %s:%u\n", subscr->imsi, |
| 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 | osmo_gsup_encode(msg_out, &gsup); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 141 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 142 | if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) { |
| 143 | LOGP(DMAIN, LOGL_ERROR, |
| 144 | "IMSI='%s': Cannot notify GSUP client; send operation failed " |
| 145 | "for %s:%u\n", subscr->imsi, |
| 146 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 147 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 148 | continue; |
| 149 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 153 | static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len) |
| 154 | { |
| 155 | int i, j, rc; |
| 156 | uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS]; |
| 157 | |
| 158 | OSMO_ASSERT(len <= sizeof(rand_buf)); |
| 159 | |
| 160 | /* Generate a random unique MSISDN (with retry) */ |
| 161 | for (i = 0; i < 10; i++) { |
| 162 | /* Get a random number (with retry) */ |
| 163 | for (j = 0; j < 10; j++) { |
| 164 | rc = osmo_get_rand_id(rand_buf, len); |
| 165 | if (!rc) |
| 166 | break; |
| 167 | } |
| 168 | if (rc) { |
| 169 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator" |
| 170 | " failed (rc=%d)\n", imsi, rc); |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */ |
| 175 | for (j = 0; j < len; j++) |
| 176 | msisdn[j] = 48 + (rand_buf[j] % 10); |
| 177 | msisdn[j] = '\0'; |
| 178 | |
| 179 | /* Ensure there is no subscriber with such MSISDN */ |
| 180 | if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT) |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* Failure */ |
| 185 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing " |
| 186 | "the length for the automatically assigned MSISDNs " |
| 187 | "(see 'subscriber-create-on-demand' command)\n", imsi); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | static int subscr_create_on_demand(const char *imsi) |
| 192 | { |
| 193 | char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1]; |
| 194 | int rc; |
| 195 | unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len; |
| 196 | |
| 197 | if (!g_hlr->subscr_create_on_demand) |
| 198 | return -1; |
| 199 | if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0) |
| 200 | return -1; |
| 201 | if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0) |
| 202 | return -1; |
| 203 | |
| 204 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi); |
| 205 | rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags); |
| 206 | if (rc) { |
| 207 | LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi); |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | if (!rand_msisdn_len) |
| 212 | return 0; |
| 213 | |
| 214 | /* Update MSISDN of the new (just allocated) subscriber */ |
| 215 | rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn); |
| 216 | if (rc) { |
| 217 | LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc); |
| 218 | return rc; |
| 219 | } |
| 220 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 225 | /*********************************************************************** |
| 226 | * Send Auth Info handling |
| 227 | ***********************************************************************/ |
| 228 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 229 | /* process an incoming SAI request */ |
| 230 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 231 | const struct osmo_gsup_message *gsup, |
| 232 | struct db_context *dbc) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 233 | { |
| 234 | struct osmo_gsup_message gsup_out; |
| 235 | struct msgb *msg_out; |
Harald Welte | 15f8810 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 236 | bool separation_bit = false; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 237 | int rc; |
| 238 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 239 | subscr_create_on_demand(gsup->imsi); |
| 240 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 241 | /* initialize return message structure */ |
| 242 | memset(&gsup_out, 0, sizeof(gsup_out)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 243 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 244 | |
Harald Welte | 15f8810 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 245 | if (gsup->rat_types_len >= 1 && gsup->rat_types[0] == OSMO_RAT_EUTRAN_SGS) |
| 246 | separation_bit = true; |
| 247 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 248 | rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind, |
| 249 | gsup_out.auth_vectors, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 250 | ARRAY_SIZE(gsup_out.auth_vectors), |
Harald Welte | 15f8810 | 2019-08-21 20:01:31 +0200 | [diff] [blame] | 251 | gsup->rand, gsup->auts, separation_bit); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 252 | if (rc <= 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 253 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 254 | switch (rc) { |
| 255 | case 0: |
Neels Hofmeyr | bd1dca0 | 2017-11-23 15:25:30 +0100 | [diff] [blame] | 256 | /* 0 means "0 tuples generated", which shouldn't happen. |
| 257 | * Treat the same as "no auth data". */ |
| 258 | case -ENOKEY: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 259 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;" |
| 260 | " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n", |
| 261 | gsup->imsi); |
| 262 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 263 | break; |
Neels Hofmeyr | 33cbde9 | 2017-11-22 20:39:59 +0100 | [diff] [blame] | 264 | case -ENOENT: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 265 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi); |
gsmevent admin | 9eb10ef | 2019-08-17 21:43:07 +0200 | [diff] [blame] | 266 | gsup_out.cause = GMM_CAUSE_ROAMING_NOTALLOWED; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 267 | break; |
| 268 | default: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 269 | LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 270 | gsup_out.cause = GMM_CAUSE_NET_FAIL; |
| 271 | break; |
| 272 | } |
Harald Welte | 15db826 | 2016-05-05 16:50:39 +0200 | [diff] [blame] | 273 | } else { |
| 274 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 275 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 278 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 279 | osmo_gsup_encode(msg_out, &gsup_out); |
| 280 | return osmo_gsup_conn_send(conn, msg_out); |
| 281 | } |
| 282 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 283 | /*********************************************************************** |
| 284 | * LU Operation State / Structure |
| 285 | ***********************************************************************/ |
| 286 | |
| 287 | static LLIST_HEAD(g_lu_ops); |
| 288 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 289 | /*! Receive Cancel Location Result from old VLR/SGSN */ |
| 290 | void lu_op_rx_cancel_old_ack(struct lu_operation *luop, |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 291 | const struct osmo_gsup_message *gsup) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 292 | { |
| 293 | OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT); |
| 294 | /* FIXME: Check for spoofing */ |
| 295 | |
| 296 | osmo_timer_del(&luop->timer); |
| 297 | |
| 298 | /* FIXME */ |
| 299 | |
| 300 | lu_op_tx_insert_subscr_data(luop); |
| 301 | } |
| 302 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 303 | /*! Receive Insert Subscriber Data Result from new VLR/SGSN */ |
| 304 | static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop, |
| 305 | const struct osmo_gsup_message *gsup) |
| 306 | { |
| 307 | OSMO_ASSERT(luop->state == LU_S_ISD_SENT); |
| 308 | /* FIXME: Check for spoofing */ |
| 309 | |
| 310 | osmo_timer_del(&luop->timer); |
| 311 | |
| 312 | /* Subscriber_Present_HLR */ |
| 313 | /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */ |
| 314 | |
| 315 | /* Send final ACK towards inquiring VLR/SGSN */ |
| 316 | lu_op_tx_ack(luop); |
| 317 | } |
| 318 | |
| 319 | /*! Receive GSUP message for given \ref lu_operation */ |
| 320 | void lu_op_rx_gsup(struct lu_operation *luop, |
| 321 | const struct osmo_gsup_message *gsup) |
| 322 | { |
| 323 | switch (gsup->message_type) { |
| 324 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 325 | /* FIXME */ |
| 326 | break; |
| 327 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 328 | lu_op_rx_insert_subscr_data_ack(luop, gsup); |
| 329 | break; |
| 330 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 331 | /* FIXME */ |
| 332 | break; |
| 333 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 334 | lu_op_rx_cancel_old_ack(luop, gsup); |
| 335 | break; |
| 336 | default: |
| 337 | LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n", |
| 338 | gsup->message_type); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 343 | /*! Receive Update Location Request, creates new \ref lu_operation */ |
| 344 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, |
| 345 | const struct osmo_gsup_message *gsup) |
| 346 | { |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 347 | struct hlr_subscriber *subscr; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 348 | struct lu_operation *luop = lu_op_alloc_conn(conn); |
Neels Hofmeyr | f1949f7 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 349 | int i; |
| 350 | bool allowed; |
| 351 | |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 352 | if (!luop) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 353 | LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n"); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 354 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 357 | subscr = &luop->subscr; |
| 358 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 359 | lu_op_statechg(luop, LU_S_LU_RECEIVED); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 360 | |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 361 | switch (gsup->cn_domain) { |
| 362 | case OSMO_GSUP_CN_DOMAIN_CS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 363 | conn->supports_cs = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 364 | break; |
| 365 | default: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 366 | /* The client didn't send a CN_DOMAIN IE; assume packet-switched in |
| 367 | * accordance with the GSUP spec in osmo-hlr's user manual (section |
| 368 | * 11.6.15 "CN Domain" says "if no CN Domain IE is present within |
| 369 | * a request, the PS Domain is assumed." */ |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 370 | case OSMO_GSUP_CN_DOMAIN_PS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 371 | conn->supports_ps = true; |
Stefan Sperling | 1cb4892 | 2018-05-03 14:05:56 +0200 | [diff] [blame] | 372 | luop->is_ps = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 373 | break; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 374 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 375 | llist_add(&luop->list, &g_lu_ops); |
| 376 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 377 | subscr_create_on_demand(gsup->imsi); |
| 378 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 379 | /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */ |
| 380 | |
| 381 | /* check if subscriber is known at all */ |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 382 | if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 383 | /* Send Error back: Subscriber Unknown in HLR */ |
Harald Welte | bd0d5bf | 2017-11-06 03:55:02 +0900 | [diff] [blame] | 384 | osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi)); |
gsmevent admin | 9eb10ef | 2019-08-17 21:43:07 +0200 | [diff] [blame] | 385 | lu_op_tx_error(luop, GMM_CAUSE_ROAMING_NOTALLOWED); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 389 | /* Check if subscriber is generally permitted on CS or PS |
| 390 | * service (as requested) */ |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 391 | if (!luop->is_ps && !luop->subscr.nam_cs) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 392 | lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED); |
| 393 | return 0; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 394 | } else if (luop->is_ps && !luop->subscr.nam_ps) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 395 | lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED); |
| 396 | return 0; |
| 397 | } |
| 398 | |
Neels Hofmeyr | f1949f7 | 2018-12-26 01:49:53 +0100 | [diff] [blame] | 399 | /* Check if any available RAT type is allowed. See 3GPP TS 29.010 3.2 'Routeing area updating' and 3.8 'Location |
| 400 | * update' for the "No Suitable cells in location area" error code. */ |
| 401 | allowed = false; |
| 402 | LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' on %s sent RAT types: %zu\n", subscr->imsi, |
| 403 | gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_CS ? "CS" : "PS", gsup->rat_types_len); |
| 404 | for (i = 0; i < gsup->rat_types_len; i++) { |
| 405 | enum osmo_rat_type rat = gsup->rat_types[i]; |
| 406 | if (rat <= 0 || rat >= OSMO_RAT_COUNT) { |
| 407 | lu_op_tx_error(luop, GMM_CAUSE_COND_IE_ERR); |
| 408 | return 0; |
| 409 | } |
| 410 | if (luop->subscr.rat_types[rat]) { |
| 411 | allowed = true; |
| 412 | LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' allowed on %s\n", |
| 413 | subscr->imsi, osmo_rat_type_name(rat)); |
| 414 | } else { |
| 415 | LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' not allowed on %s\n", |
| 416 | subscr->imsi, osmo_rat_type_name(rat)); |
| 417 | } |
| 418 | } |
| 419 | if (!allowed && gsup->rat_types_len > 0) { |
| 420 | LOGP(DAUC, LOGL_DEBUG, "ISMI='%s' not allowed on %s%s\n", |
| 421 | subscr->imsi, osmo_rat_type_name(gsup->rat_types[0]), |
| 422 | gsup->rat_types_len > 1 ? " (nor on the other available RAT types)" : ""); |
| 423 | lu_op_tx_error(luop, GMM_CAUSE_NO_SUIT_CELL_IN_LA); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 427 | /* TODO: Set subscriber tracing = deactive in VLR/SGSN */ |
| 428 | |
| 429 | #if 0 |
| 430 | /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */ |
| 431 | if (luop->is_ps == false && |
| 432 | strcmp(subscr->vlr_number, vlr_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 433 | lu_op_tx_cancel_old(luop); |
| 434 | } else if (luop->is_ps == true && |
| 435 | strcmp(subscr->sgsn_number, sgsn_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 436 | lu_op_tx_cancel_old(luop); |
| 437 | } else |
| 438 | #endif |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 439 | |
| 440 | /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */ |
| 441 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n", |
| 442 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number", |
| 443 | osmo_quote_str((const char*)luop->peer, -1)); |
Neels Hofmeyr | b984a6c | 2018-12-29 03:28:38 +0100 | [diff] [blame] | 444 | if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps, |
| 445 | gsup->rat_types, gsup->rat_types_len)) |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 446 | LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n", |
| 447 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number"); |
| 448 | |
Oliver Smith | 66106c0 | 2019-01-09 12:05:15 +0100 | [diff] [blame] | 449 | /* TODO: Subscriber allowed to roam in PLMN? */ |
| 450 | /* TODO: Update RoutingInfo */ |
| 451 | /* TODO: Reset Flag MS Purged (cs/ps) */ |
| 452 | /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */ |
| 453 | lu_op_tx_insert_subscr_data(luop); |
| 454 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 458 | static int rx_purge_ms_req(struct osmo_gsup_conn *conn, |
| 459 | const struct osmo_gsup_message *gsup) |
| 460 | { |
| 461 | struct osmo_gsup_message gsup_reply = {0}; |
| 462 | struct msgb *msg_out; |
| 463 | bool is_ps = false; |
| 464 | int rc; |
| 465 | |
| 466 | LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi, |
| 467 | is_ps ? "PS" : "CS"); |
| 468 | |
| 469 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 470 | |
| 471 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 472 | is_ps = true; |
| 473 | |
| 474 | /* FIXME: check if the VLR that sends the purge is the same that |
| 475 | * we have on record. Only update if yes */ |
| 476 | |
| 477 | /* Perform the actual update of the DB */ |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 478 | rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 479 | |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 480 | if (rc == 0) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 481 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 482 | else if (rc == -ENOENT) { |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 483 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 484 | gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 485 | } else { |
| 486 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 487 | gsup_reply.cause = GMM_CAUSE_NET_FAIL; |
| 488 | } |
| 489 | |
| 490 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
| 491 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 492 | return osmo_gsup_conn_send(conn, msg_out); |
| 493 | } |
| 494 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 495 | static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi, |
| 496 | enum osmo_gsup_message_type type_in, uint8_t err_cause) |
| 497 | { |
Oliver Smith | f9cf180 | 2019-02-04 11:20:08 +0100 | [diff] [blame] | 498 | int type_err = OSMO_GSUP_TO_MSGT_ERROR(type_in); |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 499 | struct osmo_gsup_message gsup_reply = {0}; |
| 500 | struct msgb *msg_out; |
| 501 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 502 | OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi); |
| 503 | gsup_reply.message_type = type_err; |
| 504 | gsup_reply.cause = err_cause; |
| 505 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response"); |
| 506 | OSMO_ASSERT(msg_out); |
| 507 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 508 | LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err)); |
| 509 | return osmo_gsup_conn_send(conn, msg_out); |
| 510 | } |
| 511 | |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 512 | static int rx_check_imei_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup) |
| 513 | { |
| 514 | struct osmo_gsup_message gsup_reply = {0}; |
| 515 | struct msgb *msg_out; |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 516 | char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0}; |
| 517 | int rc; |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 518 | |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 519 | /* Require IMEI */ |
| 520 | if (!gsup->imei_enc) { |
| 521 | LOGP(DMAIN, LOGL_ERROR, "%s: missing IMEI\n", gsup->imsi); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 522 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 523 | return -1; |
| 524 | } |
| 525 | |
Oliver Smith | 103c11b | 2019-06-06 11:57:05 +0200 | [diff] [blame] | 526 | /* Decode IMEI (fails if IMEI is too long) */ |
| 527 | rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0); |
| 528 | if (rc < 0) { |
| 529 | LOGP(DMAIN, LOGL_ERROR, "%s: failed to decode IMEI (rc: %i)\n", gsup->imsi, rc); |
| 530 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /* Check if IMEI is too short */ |
| 535 | if (strlen(imei) != GSM23003_IMEI_NUM_DIGITS_NO_CHK) { |
| 536 | LOGP(DMAIN, LOGL_ERROR, "%s: wrong encoded IMEI length (IMEI: '%s', %lu, %i)\n", gsup->imsi, imei, |
| 537 | strlen(imei), GSM23003_IMEI_NUM_DIGITS_NO_CHK); |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 538 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 539 | return -1; |
| 540 | } |
| 541 | |
Oliver Smith | c7f1787 | 2019-03-04 15:10:44 +0100 | [diff] [blame] | 542 | subscr_create_on_demand(gsup->imsi); |
| 543 | |
Oliver Smith | 851814a | 2019-01-11 15:30:21 +0100 | [diff] [blame] | 544 | /* Save in DB if desired */ |
| 545 | if (g_hlr->store_imei) { |
| 546 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei); |
| 547 | if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) { |
| 548 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 549 | return -1; |
| 550 | } |
| 551 | } else { |
| 552 | /* Check if subscriber exists and print IMEI */ |
| 553 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei); |
| 554 | struct hlr_subscriber subscr; |
| 555 | if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) { |
| 556 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 557 | return -1; |
| 558 | } |
| 559 | } |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 560 | |
| 561 | /* Accept all IMEIs */ |
| 562 | gsup_reply.imei_result = OSMO_GSUP_IMEI_RESULT_ACK; |
| 563 | gsup_reply.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT; |
| 564 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP Check_IMEI response"); |
| 565 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 566 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 567 | return osmo_gsup_conn_send(conn, msg_out); |
| 568 | } |
| 569 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 570 | static char namebuf[255]; |
| 571 | #define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \ |
| 572 | LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \ |
| 573 | osmo_gsup_message_type_name(gsup->message_type), \ |
| 574 | osmo_gsup_message_class_name(gsup->message_class), \ |
| 575 | gsup->imsi, \ |
| 576 | osmo_quote_str((const char *)gsup->source_name, gsup->source_name_len), \ |
| 577 | osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)gsup->destination_name, gsup->destination_name_len), \ |
| 578 | ## args) |
| 579 | |
| 580 | static int read_cb_forward(struct osmo_gsup_conn *conn, struct msgb *msg, const struct osmo_gsup_message *gsup) |
| 581 | { |
| 582 | int ret = -EINVAL; |
| 583 | struct osmo_gsup_message *gsup_err; |
| 584 | |
| 585 | /* FIXME: it would be better if the msgb never were deallocated immediately by osmo_gsup_addr_send(), which a |
| 586 | * select-loop volatile talloc context could facilitate. Then we would still be able to access gsup-> members |
| 587 | * (pointing into the msgb) even after sending failed, and we wouldn't need to copy this data before sending: */ |
| 588 | /* Prepare error message (before IEs get deallocated) */ |
| 589 | gsup_err = talloc_zero(hlr_ctx, struct osmo_gsup_message); |
| 590 | OSMO_STRLCPY_ARRAY(gsup_err->imsi, gsup->imsi); |
| 591 | gsup_err->message_class = gsup->message_class; |
| 592 | gsup_err->destination_name = talloc_memdup(gsup_err, gsup->destination_name, gsup->destination_name_len); |
| 593 | gsup_err->destination_name_len = gsup->destination_name_len; |
Vadim Yanitskiy | 8625cda | 2019-05-07 21:09:01 +0700 | [diff] [blame] | 594 | gsup_err->message_type = gsup->message_type; |
Vadim Yanitskiy | 609978d | 2019-05-07 20:35:05 +0700 | [diff] [blame] | 595 | gsup_err->session_state = gsup->session_state; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 596 | gsup_err->session_id = gsup->session_id; |
| 597 | gsup_err->source_name = talloc_memdup(gsup_err, gsup->source_name, gsup->source_name_len); |
| 598 | gsup_err->source_name_len = gsup->source_name_len; |
| 599 | |
| 600 | /* Check for routing IEs */ |
| 601 | if (!gsup->source_name || !gsup->source_name_len || !gsup->destination_name || !gsup->destination_name_len) { |
| 602 | LOGP_GSUP_FWD(gsup, LOGL_ERROR, "missing routing IEs\n"); |
| 603 | goto end; |
| 604 | } |
| 605 | |
| 606 | /* Verify source name (e.g. "MSC-00-00-00-00-00-00") */ |
| 607 | if (gsup_route_find(conn->server, gsup->source_name, gsup->source_name_len) != conn) { |
| 608 | LOGP_GSUP_FWD(gsup, LOGL_ERROR, "mismatching source name\n"); |
| 609 | goto end; |
| 610 | } |
| 611 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 612 | /* Forward message without re-encoding (so we don't remove unknown IEs) */ |
| 613 | LOGP_GSUP_FWD(gsup, LOGL_INFO, "checks passed, forwarding\n"); |
| 614 | |
| 615 | /* Remove incoming IPA header to be able to prepend an outgoing IPA header */ |
| 616 | msgb_pull_to_l2(msg); |
| 617 | ret = osmo_gsup_addr_send(g_hlr->gs, gsup->destination_name, gsup->destination_name_len, msg); |
| 618 | /* AT THIS POINT, THE msg MAY BE DEALLOCATED and the data like gsup->imsi, gsup->source_name etc may all be |
| 619 | * invalid and cause segfaults. */ |
| 620 | msg = NULL; |
| 621 | gsup = NULL; |
| 622 | if (ret == -ENODEV) |
| 623 | LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "destination not connected\n"); |
| 624 | else if (ret) |
| 625 | LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "unknown error %i\n", ret); |
| 626 | |
| 627 | end: |
| 628 | /* Send error back to source */ |
| 629 | if (ret) { |
| 630 | struct msgb *msg_err = msgb_alloc_headroom(1024+16, 16, "GSUP forward ERR response"); |
| 631 | OSMO_ASSERT(msg_err); |
Vadim Yanitskiy | 8625cda | 2019-05-07 21:09:01 +0700 | [diff] [blame] | 632 | gsup_err->message_type = OSMO_GSUP_MSGT_E_ROUTING_ERROR; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 633 | osmo_gsup_encode(msg_err, gsup_err); |
| 634 | LOGP_GSUP_FWD(gsup_err, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(gsup_err->message_type)); |
| 635 | osmo_gsup_conn_send(conn, msg_err); |
| 636 | } |
| 637 | talloc_free(gsup_err); |
| 638 | if (msg) |
| 639 | msgb_free(msg); |
| 640 | return ret; |
| 641 | } |
| 642 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 643 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 644 | { |
| 645 | static struct osmo_gsup_message gsup; |
| 646 | int rc; |
| 647 | |
Vadim Yanitskiy | c69a18b | 2019-05-07 20:53:54 +0700 | [diff] [blame] | 648 | if (!msgb_l2(msg) || !msgb_l2len(msg)) { |
| 649 | LOGP(DMAIN, LOGL_ERROR, "missing or empty L2 data\n"); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame] | 650 | msgb_free(msg); |
| 651 | return -EINVAL; |
Vadim Yanitskiy | c69a18b | 2019-05-07 20:53:54 +0700 | [diff] [blame] | 652 | } |
| 653 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 654 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 655 | if (rc < 0) { |
| 656 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame] | 657 | msgb_free(msg); |
| 658 | return rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 661 | /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5 |
| 662 | * digits is impossible. Even 5 digits is a highly theoretical case */ |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame] | 663 | if (strlen(gsup.imsi) < 5) { /* TODO: move this check to libosmogsm/gsup.c? */ |
Neels Hofmeyr | 9f6e558 | 2019-04-03 18:16:50 +0200 | [diff] [blame] | 664 | LOGP(DMAIN, LOGL_ERROR, "IMSI too short: %s\n", osmo_quote_str(gsup.imsi, -1)); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame] | 665 | gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO); |
| 666 | msgb_free(msg); |
| 667 | return -EINVAL; |
Neels Hofmeyr | 9f6e558 | 2019-04-03 18:16:50 +0200 | [diff] [blame] | 668 | } |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 669 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 670 | if (gsup.destination_name_len) |
| 671 | return read_cb_forward(conn, msg, &gsup); |
| 672 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 673 | switch (gsup.message_type) { |
| 674 | /* requests sent to us */ |
| 675 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 676 | rx_send_auth_info(conn, &gsup, g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 677 | break; |
| 678 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 679 | rx_upd_loc_req(conn, &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 680 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 681 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
| 682 | rx_purge_ms_req(conn, &gsup); |
| 683 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 684 | /* responses to requests sent by us */ |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 685 | case OSMO_GSUP_MSGT_DELETE_DATA_ERROR: |
| 686 | LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data " |
| 687 | "for IMSI %s\n", gsup.imsi); |
| 688 | break; |
| 689 | case OSMO_GSUP_MSGT_DELETE_DATA_RESULT: |
| 690 | LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n", |
| 691 | gsup.imsi); |
| 692 | break; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 693 | case OSMO_GSUP_MSGT_PROC_SS_REQUEST: |
| 694 | case OSMO_GSUP_MSGT_PROC_SS_RESULT: |
| 695 | rx_proc_ss_req(conn, &gsup); |
| 696 | break; |
| 697 | case OSMO_GSUP_MSGT_PROC_SS_ERROR: |
| 698 | rx_proc_ss_error(conn, &gsup); |
| 699 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 700 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 701 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 702 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 703 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 704 | { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 705 | struct lu_operation *luop = lu_op_by_imsi(gsup.imsi, |
| 706 | &g_lu_ops); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 707 | if (!luop) { |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 708 | LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for " |
| 709 | "unknown IMSI %s\n", |
| 710 | osmo_gsup_message_type_name(gsup.message_type), |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 711 | gsup.imsi); |
| 712 | break; |
| 713 | } |
| 714 | lu_op_rx_gsup(luop, &gsup); |
| 715 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 716 | break; |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 717 | case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST: |
| 718 | rx_check_imei_req(conn, &gsup); |
| 719 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 720 | default: |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 721 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n", |
| 722 | osmo_gsup_message_type_name(gsup.message_type)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 723 | break; |
| 724 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 725 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 729 | static void print_usage() |
| 730 | { |
| 731 | printf("Usage: osmo-hlr\n"); |
| 732 | } |
| 733 | |
| 734 | static void print_help() |
| 735 | { |
| 736 | printf(" -h --help This text.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 737 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 738 | printf(" -l --database db-name The database to use.\n"); |
| 739 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 740 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 741 | printf(" -s --disable-color Do not print ANSI colors in the log\n"); |
| 742 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 743 | printf(" -e --log-level number Set a global loglevel.\n"); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 744 | printf(" -U --db-upgrade Allow HLR database schema upgrades.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 745 | printf(" -V --version Print the version of OsmoHLR.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static struct { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 749 | const char *config_file; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 750 | const char *db_file; |
| 751 | bool daemonize; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 752 | bool db_upgrade; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 753 | } cmdline_opts = { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 754 | .config_file = "osmo-hlr.cfg", |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 755 | .db_file = NULL, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 756 | .daemonize = false, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 757 | .db_upgrade = false, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 758 | }; |
| 759 | |
| 760 | static void handle_options(int argc, char **argv) |
| 761 | { |
| 762 | while (1) { |
| 763 | int option_index = 0, c; |
| 764 | static struct option long_options[] = { |
| 765 | {"help", 0, 0, 'h'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 766 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 767 | {"database", 1, 0, 'l'}, |
| 768 | {"debug", 1, 0, 'd'}, |
| 769 | {"daemonize", 0, 0, 'D'}, |
| 770 | {"disable-color", 0, 0, 's'}, |
| 771 | {"log-level", 1, 0, 'e'}, |
| 772 | {"timestamp", 0, 0, 'T'}, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 773 | {"db-upgrade", 0, 0, 'U' }, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 774 | {"version", 0, 0, 'V' }, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 775 | {0, 0, 0, 0} |
| 776 | }; |
| 777 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 778 | c = getopt_long(argc, argv, "hc:l:d:Dse:TUV", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 779 | long_options, &option_index); |
| 780 | if (c == -1) |
| 781 | break; |
| 782 | |
| 783 | switch (c) { |
| 784 | case 'h': |
| 785 | print_usage(); |
| 786 | print_help(); |
| 787 | exit(0); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 788 | case 'c': |
| 789 | cmdline_opts.config_file = optarg; |
| 790 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 791 | case 'l': |
| 792 | cmdline_opts.db_file = optarg; |
| 793 | break; |
| 794 | case 'd': |
| 795 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 796 | break; |
| 797 | case 'D': |
| 798 | cmdline_opts.daemonize = 1; |
| 799 | break; |
| 800 | case 's': |
| 801 | log_set_use_color(osmo_stderr_target, 0); |
| 802 | break; |
| 803 | case 'e': |
| 804 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 805 | break; |
| 806 | case 'T': |
| 807 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 808 | break; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 809 | case 'U': |
| 810 | cmdline_opts.db_upgrade = true; |
| 811 | break; |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 812 | case 'V': |
| 813 | print_version(1); |
| 814 | exit(0); |
| 815 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 816 | default: |
| 817 | /* catch unknown options *as well as* missing arguments. */ |
| 818 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 819 | exit(-1); |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 825 | static void signal_hdlr(int signal) |
| 826 | { |
| 827 | switch (signal) { |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 828 | case SIGTERM: |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 829 | case SIGINT: |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 830 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 831 | quit++; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 832 | break; |
| 833 | case SIGUSR1: |
| 834 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 835 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 839 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 840 | static const char vlr_copyright[] = |
| 841 | "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n" |
| 842 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 843 | "This is free software: you are free to change and redistribute it.\r\n" |
| 844 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 845 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 846 | static struct vty_app_info vty_info = { |
| 847 | .name = "OsmoHLR", |
| 848 | .version = PACKAGE_VERSION, |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 849 | .copyright = vlr_copyright, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 850 | .is_config_node = hlr_vty_is_config_node, |
Pau Espin Pedrol | ce9bc40 | 2017-05-31 13:19:22 +0200 | [diff] [blame] | 851 | .go_parent_cb = hlr_vty_go_parent, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 852 | }; |
| 853 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 854 | int main(int argc, char **argv) |
| 855 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 856 | int rc; |
| 857 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 858 | /* Track the use of talloc NULL memory contexts */ |
| 859 | talloc_enable_null_tracking(); |
| 860 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 861 | hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR"); |
| 862 | msgb_talloc_ctx_init(hlr_ctx, 0); |
Harald Welte | 7ee6e55 | 2018-02-14 00:52:05 +0100 | [diff] [blame] | 863 | vty_info.tall_ctx = hlr_ctx; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 864 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 865 | g_hlr = talloc_zero(hlr_ctx, struct hlr); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 866 | INIT_LLIST_HEAD(&g_hlr->euse_list); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 867 | INIT_LLIST_HEAD(&g_hlr->iuse_list); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 868 | INIT_LLIST_HEAD(&g_hlr->ss_sessions); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 869 | INIT_LLIST_HEAD(&g_hlr->ussd_routes); |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 870 | g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 871 | |
Vadim Yanitskiy | d157a56 | 2018-12-01 00:03:39 +0700 | [diff] [blame] | 872 | /* Init default (call independent) SS session guard timeout value */ |
| 873 | g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT; |
| 874 | |
Pau Espin Pedrol | 5153031 | 2018-04-17 15:07:06 +0200 | [diff] [blame] | 875 | rc = osmo_init_logging2(hlr_ctx, &hlr_log_info); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 876 | if (rc < 0) { |
| 877 | fprintf(stderr, "Error initializing logging\n"); |
| 878 | exit(1); |
| 879 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 880 | |
Max | 20ddfdb | 2019-02-18 13:12:27 +0100 | [diff] [blame] | 881 | osmo_stats_init(hlr_ctx); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 882 | vty_init(&vty_info); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 883 | ctrl_vty_init(hlr_ctx); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 884 | handle_options(argc, argv); |
Pau Espin Pedrol | e49391b | 2019-08-05 15:57:10 +0200 | [diff] [blame] | 885 | hlr_vty_init(); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 886 | |
| 887 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
| 888 | if (rc < 0) { |
| 889 | LOGP(DMAIN, LOGL_FATAL, |
| 890 | "Failed to parse the config file: '%s'\n", |
| 891 | cmdline_opts.config_file); |
| 892 | return rc; |
| 893 | } |
| 894 | |
| 895 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 896 | rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(), |
| 897 | OSMO_VTY_PORT_HLR); |
| 898 | if (rc < 0) |
| 899 | return rc; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 900 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 901 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 902 | |
| 903 | rc = rand_init(); |
| 904 | if (rc < 0) { |
| 905 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 906 | exit(1); |
| 907 | } |
| 908 | |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 909 | if (cmdline_opts.db_file) |
| 910 | osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file); |
| 911 | |
| 912 | 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] | 913 | if (!g_hlr->dbc) { |
Neels Hofmeyr | 5857c59 | 2019-04-02 04:24:49 +0200 | [diff] [blame] | 914 | 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] | 915 | exit(1); |
| 916 | } |
| 917 | |
Neels Hofmeyr | 84201d3 | 2017-07-21 16:00:32 +0200 | [diff] [blame] | 918 | g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT, |
Harald Welte | 32acace | 2018-06-16 17:07:28 +0200 | [diff] [blame] | 919 | read_cb, &g_lu_ops, g_hlr); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 920 | if (!g_hlr->gs) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 921 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 922 | exit(1); |
| 923 | } |
| 924 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 925 | g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr(); |
Neels Hofmeyr | 234f9cb | 2017-10-24 17:23:04 +0200 | [diff] [blame] | 926 | g_hlr->ctrl = hlr_controlif_setup(g_hlr); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 927 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 928 | osmo_init_ignore_signals(); |
| 929 | signal(SIGINT, &signal_hdlr); |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 930 | signal(SIGTERM, &signal_hdlr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 931 | signal(SIGUSR1, &signal_hdlr); |
| 932 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 933 | if (cmdline_opts.daemonize) { |
| 934 | rc = osmo_daemonize(); |
| 935 | if (rc < 0) { |
| 936 | perror("Error during daemonize"); |
| 937 | exit(1); |
| 938 | } |
| 939 | } |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 940 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 941 | while (!quit) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 942 | osmo_select_main(0); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 943 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 944 | osmo_gsup_server_destroy(g_hlr->gs); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 945 | db_close(g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 946 | log_fini(); |
| 947 | |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 948 | /** |
| 949 | * Report the heap state of root context, then free, |
| 950 | * so both ASAN and Valgrind are happy... |
| 951 | */ |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 952 | talloc_report_full(hlr_ctx, stderr); |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 953 | talloc_free(hlr_ctx); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 954 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 955 | /* FIXME: VTY code still uses NULL-context */ |
| 956 | talloc_free(tall_vty_ctx); |
| 957 | |
| 958 | /** |
| 959 | * Report the heap state of NULL context, then free, |
| 960 | * so both ASAN and Valgrind are happy... |
| 961 | */ |
| 962 | talloc_report_full(NULL, stderr); |
| 963 | talloc_disable_null_tracking(); |
| 964 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 965 | return 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 966 | } |