blob: 93cb90df23e7a1c66796856660abcb132a5e5ce7 [file] [log] [blame]
Harald Welte936f6722016-05-03 18:51:18 +02001/* (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 Welteaabae9e2016-04-28 12:48:14 +020020#include <signal.h>
Harald Weltee687be52016-05-03 18:49:27 +020021#include <errno.h>
Maxea8b0d42017-02-14 16:53:04 +010022#include <stdbool.h>
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +010023#include <getopt.h>
24
Harald Weltee72cf552016-04-28 07:18:49 +020025#include <osmocom/core/msgb.h>
Max20ddfdb2019-02-18 13:12:27 +010026#include <osmocom/core/stats.h>
Harald Weltee72cf552016-04-28 07:18:49 +020027#include <osmocom/core/logging.h>
28#include <osmocom/core/application.h>
29#include <osmocom/gsm/gsup.h>
Neels Hofmeyr7685a782017-01-30 23:30:26 +010030#include <osmocom/vty/vty.h>
31#include <osmocom/vty/command.h>
32#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/ports.h>
Max372868b2017-03-02 12:12:00 +010034#include <osmocom/ctrl/control_vty.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020035#include <osmocom/gsm/apn.h>
Oliver Smith783ac812018-12-17 11:34:51 +010036#include <osmocom/gsm/gsm48_ie.h>
Oliver Smithc7f17872019-03-04 15:10:44 +010037#include <osmocom/gsm/gsm_utils.h>
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010038#include <osmocom/gsm/gsm23003.h>
Harald Weltee72cf552016-04-28 07:18:49 +020039
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010040#include <osmocom/gsupclient/gsup_peer_id.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010041#include <osmocom/hlr/db.h>
42#include <osmocom/hlr/hlr.h>
43#include <osmocom/hlr/ctrl.h>
44#include <osmocom/hlr/logging.h>
45#include <osmocom/hlr/gsup_server.h>
46#include <osmocom/hlr/gsup_router.h>
47#include <osmocom/hlr/rand.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010048#include <osmocom/hlr/hlr_vty.h>
49#include <osmocom/hlr/hlr_ussd.h>
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010050#include <osmocom/hlr/lu_fsm.h>
Harald Weltee72cf552016-04-28 07:18:49 +020051
Harald Welted5807b82018-07-29 12:27:41 +020052struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010053static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070054static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020055
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010056struct osmo_tdef g_hlr_tdefs[] = {
57 /* 4222 is also the OSMO_GSUP_PORT */
58 { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" },
59 {}
60};
61
Stefan Sperlingf1622522018-04-09 11:39:16 +020062/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
63 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020064 * \param[in] subscr A subscriber we have new data to send for.
65 */
66void
67osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
68{
69 struct osmo_gsup_conn *co;
70
Neels Hofmeyre66e5252018-09-28 02:59:19 +020071 if (g_hlr->gs == NULL) {
72 LOGP(DLGSUP, LOGL_DEBUG,
73 "IMSI %s: NOT Notifying peers of subscriber data change,"
74 " there is no GSUP server\n",
75 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020076 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020077 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020078
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010079 /* FIXME: send only to current vlr_number and sgsn_number */
80
Stefan Sperlingf1622522018-04-09 11:39:16 +020081 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020082 struct osmo_gsup_message gsup = { };
83 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
84 uint8_t apn[APN_MAXLEN];
85 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020086 uint8_t *peer;
87 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020088 size_t peer_strlen;
89 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020090 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020091
Neels Hofmeyre66e5252018-09-28 02:59:19 +020092 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020093 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020094 peer_compare = subscr->sgsn_number;
95 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020096 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020097 peer_compare = subscr->vlr_number;
98 } else {
99 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200100 continue;
101 }
102
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200103 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
104 if (peer_len < 0) {
105 LOGP(DLGSUP, LOGL_ERROR,
106 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
107 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
108 co && co->conn && co->conn->server? co->conn->server->port : 0);
109 continue;
110 }
111
112 peer_strlen = strnlen((const char*)peer, peer_len);
113 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
114 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
115 /* I hope peer is always nul terminated... */
116 if (peer_strlen < peer_len)
117 LOGP(DLGSUP, LOGL_DEBUG,
118 "IMSI %s: subscriber change: skipping %s peer %s\n",
119 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
120 osmo_quote_str((char*)peer, -1));
121 continue;
122 }
123
124 LOGP(DLGSUP, LOGL_DEBUG,
125 "IMSI %s: subscriber change: notifying %s peer %s\n",
126 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
127 osmo_quote_str(peer_compare, -1));
128
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200129 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
130 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200131 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200132 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200133 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200134 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
135 co && co->conn && co->conn->server? co->conn->server->port : 0);
136 continue;
137 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200138
Stefan Sperling93c5b102018-04-10 19:26:14 +0200139 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100140 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200141 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200142 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200143 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
144 "for %s:%u\n", subscr->imsi,
145 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
146 co && co->conn && co->conn->server? co->conn->server->port : 0);
147 continue;
148 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200149 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200150
Stefan Sperling93c5b102018-04-10 19:26:14 +0200151 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
152 LOGP(DMAIN, LOGL_ERROR,
153 "IMSI='%s': Cannot notify GSUP client; send operation failed "
154 "for %s:%u\n", subscr->imsi,
155 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
156 co && co->conn && co->conn->server? co->conn->server->port : 0);
157 continue;
158 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200159 }
160}
161
Oliver Smithc7f17872019-03-04 15:10:44 +0100162static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
163{
164 int i, j, rc;
165 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
166
167 OSMO_ASSERT(len <= sizeof(rand_buf));
168
169 /* Generate a random unique MSISDN (with retry) */
170 for (i = 0; i < 10; i++) {
171 /* Get a random number (with retry) */
172 for (j = 0; j < 10; j++) {
173 rc = osmo_get_rand_id(rand_buf, len);
174 if (!rc)
175 break;
176 }
177 if (rc) {
178 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
179 " failed (rc=%d)\n", imsi, rc);
180 return rc;
181 }
182
183 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
184 for (j = 0; j < len; j++)
185 msisdn[j] = 48 + (rand_buf[j] % 10);
186 msisdn[j] = '\0';
187
188 /* Ensure there is no subscriber with such MSISDN */
189 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
190 return 0;
191 }
192
193 /* Failure */
194 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
195 "the length for the automatically assigned MSISDNs "
196 "(see 'subscriber-create-on-demand' command)\n", imsi);
197 return -1;
198}
199
200static int subscr_create_on_demand(const char *imsi)
201{
202 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
203 int rc;
204 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
205
206 if (!g_hlr->subscr_create_on_demand)
207 return -1;
208 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
209 return -1;
210 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
211 return -1;
212
213 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
214 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
215 if (rc) {
216 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
217 return rc;
218 }
219
220 if (!rand_msisdn_len)
221 return 0;
222
223 /* Update MSISDN of the new (just allocated) subscriber */
224 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
225 if (rc) {
226 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
227 return rc;
228 }
229 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
230
231 return 0;
232}
233
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100234/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
235 * \param[in,out] hlr Global hlr context.
236 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
237 * \param[in] nam_val True to enable CS/PS, false to disable.
238 * \param[in] is_ps True to enable/disable PS, false for CS.
239 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
240 * value on error.
241 */
242int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
243{
244 int rc;
245 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
246 struct osmo_ipa_name vlr_name;
247 struct osmo_gsup_message gsup_del_data = {
248 .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST,
249 };
250 OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi);
251
252 if (is_val == nam_val) {
253 LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n",
254 subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
255 return ENOEXEC;
256 }
257
258 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
259 if (rc)
260 return rc > 0? -rc : rc;
261
262 /* If we're disabling, send a notice out to the GSUP client that is
263 * responsible. Otherwise no need. */
264 if (nam_val)
265 return 0;
266
267 if (subscr->vlr_number && osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number))
268 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
269 if (subscr->sgsn_number && osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number))
270 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
271 return 0;
272}
273
Harald Weltee687be52016-05-03 18:49:27 +0200274/***********************************************************************
275 * Send Auth Info handling
276 ***********************************************************************/
277
Harald Weltee72cf552016-04-28 07:18:49 +0200278/* process an incoming SAI request */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100279static int rx_send_auth_info(unsigned int auc_3g_ind, struct osmo_gsup_req *req)
Harald Weltee72cf552016-04-28 07:18:49 +0200280{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100281 struct osmo_gsup_message gsup_out = {
282 .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT,
283 };
Harald Welte06f5af22019-08-21 20:01:31 +0200284 bool separation_bit = false;
Harald Weltee72cf552016-04-28 07:18:49 +0200285 int rc;
286
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100287 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100288
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100289 if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS)
Harald Welte06f5af22019-08-21 20:01:31 +0200290 separation_bit = true;
291
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100292 rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100293 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200294 ARRAY_SIZE(gsup_out.auth_vectors),
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100295 req->gsup.rand, req->gsup.auts, separation_bit);
296
Neels Hofmeyr671db902017-11-22 20:38:19 +0100297 if (rc <= 0) {
Neels Hofmeyr671db902017-11-22 20:38:19 +0100298 switch (rc) {
299 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100300 /* 0 means "0 tuples generated", which shouldn't happen.
301 * Treat the same as "no auth data". */
302 case -ENOKEY:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100303 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN,
304 "IMSI known, but has no auth data;"
305 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP");
306 return rc;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100307 case -ENOENT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100308 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
309 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100310 default:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100311 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db");
312 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100313 }
Harald Weltee72cf552016-04-28 07:18:49 +0200314 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100315 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200316
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100317 osmo_gsup_req_respond(req, &gsup_out, false, true);
318 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200319}
320
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100321/*! Receive Update Location Request, creates new lu_operation */
322static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req)
Harald Weltee687be52016-05-03 18:49:27 +0200323{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100324 switch (req->gsup.cn_domain) {
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200325 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200326 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200327 break;
328 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200329 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
330 * accordance with the GSUP spec in osmo-hlr's user manual (section
331 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
332 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200333 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200334 conn->supports_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200335 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200336 }
Harald Weltee687be52016-05-03 18:49:27 +0200337
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100338 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100339
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100340 lu_rx_gsup(req);
Harald Weltee687be52016-05-03 18:49:27 +0200341 return 0;
342}
343
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100344static int rx_purge_ms_req(struct osmo_gsup_req *req)
Harald Welteb18f0e02016-05-05 21:03:03 +0200345{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100346 bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS);
Harald Welteb18f0e02016-05-05 21:03:03 +0200347 int rc;
348
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100349 LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS");
Harald Welteb18f0e02016-05-05 21:03:03 +0200350
351 /* FIXME: check if the VLR that sends the purge is the same that
352 * we have on record. Only update if yes */
353
354 /* Perform the actual update of the DB */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100355 rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200356
Harald Welte3f2a9a22018-03-01 23:35:35 +0100357 if (rc == 0)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100358 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true);
359 else if (rc == -ENOENT)
360 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
361 else
362 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error");
363 return rc;
Harald Welteb18f0e02016-05-05 21:03:03 +0200364}
365
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100366static int rx_check_imei_req(struct osmo_gsup_req *req)
Harald Weltea1d3b042018-06-11 20:28:35 +0200367{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100368 struct osmo_gsup_message gsup_reply;
Oliver Smith103c11b2019-06-06 11:57:05 +0200369 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100370 const struct osmo_gsup_message *gsup = &req->gsup;
Oliver Smith103c11b2019-06-06 11:57:05 +0200371 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100372
Oliver Smith103c11b2019-06-06 11:57:05 +0200373 /* Require IMEI */
374 if (!gsup->imei_enc) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100375 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI");
Oliver Smith783ac812018-12-17 11:34:51 +0100376 return -1;
377 }
378
Oliver Smith103c11b2019-06-06 11:57:05 +0200379 /* Decode IMEI (fails if IMEI is too long) */
380 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
381 if (rc < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100382 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
383 "failed to decode IMEI %s (rc: %d)",
384 osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len),
385 rc);
Oliver Smith103c11b2019-06-06 11:57:05 +0200386 return -1;
387 }
388
389 /* Check if IMEI is too short */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100390 if (!osmo_imei_str_valid(imei, false)) {
391 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
392 "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1));
Oliver Smith783ac812018-12-17 11:34:51 +0100393 return -1;
394 }
395
Oliver Smithc7f17872019-03-04 15:10:44 +0100396 subscr_create_on_demand(gsup->imsi);
397
Oliver Smith851814a2019-01-11 15:30:21 +0100398 /* Save in DB if desired */
399 if (g_hlr->store_imei) {
400 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
401 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100402 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "Failed to store IMEI in HLR db");
Oliver Smith851814a2019-01-11 15:30:21 +0100403 return -1;
404 }
405 } else {
406 /* Check if subscriber exists and print IMEI */
407 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
408 struct hlr_subscriber subscr;
409 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100410 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "IMSI unknown");
Oliver Smith851814a2019-01-11 15:30:21 +0100411 return -1;
412 }
413 }
Oliver Smith783ac812018-12-17 11:34:51 +0100414
415 /* Accept all IMEIs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100416 gsup_reply = (struct osmo_gsup_message){
417 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
418 .imei_result = OSMO_GSUP_IMEI_RESULT_ACK,
419 };
420 return osmo_gsup_req_respond(req, &gsup_reply, false, true);
Oliver Smith783ac812018-12-17 11:34:51 +0100421}
422
Oliver Smith28f0af82019-02-22 11:46:33 +0100423static char namebuf[255];
424#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
425 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100426 osmo_gsup_message_type_name((gsup)->message_type), \
427 osmo_gsup_message_class_name((gsup)->message_class), \
428 (gsup)->imsi, \
429 osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \
430 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \
Oliver Smith28f0af82019-02-22 11:46:33 +0100431 ## args)
432
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100433static int read_cb_forward(struct osmo_gsup_req *req)
Oliver Smith28f0af82019-02-22 11:46:33 +0100434{
435 int ret = -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100436 const struct osmo_gsup_message *gsup = &req->gsup;
437 struct osmo_gsup_message gsup_err;
438 struct msgb *forward_msg;
439 struct osmo_ipa_name destination_name;
Oliver Smith28f0af82019-02-22 11:46:33 +0100440
441 /* Check for routing IEs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100442 if (!req->gsup.source_name[0] || !req->gsup.source_name_len
443 || !req->gsup.destination_name[0] || !req->gsup.destination_name_len) {
444 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n");
445 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100446 }
447
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100448 if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) {
449 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n");
450 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100451 }
452
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100453 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name));
Oliver Smith28f0af82019-02-22 11:46:33 +0100454
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100455 /* Forward message without re-encoding (so we don't remove unknown IEs).
456 * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */
457 forward_msg = osmo_gsup_msgb_alloc("GSUP forward");
458 forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg));
459 memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg));
460 ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg);
Oliver Smith28f0af82019-02-22 11:46:33 +0100461 if (ret) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100462 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n",
463 ret == -ENODEV ? "destination not connected" : "unknown error",
464 ret);
465 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100466 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100467 osmo_gsup_req_free(req);
468 return 0;
469
470routing_error:
471 gsup_err = (struct osmo_gsup_message){
472 .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR,
473 .source_name = gsup->destination_name,
474 .source_name_len = gsup->destination_name_len,
475 };
476 osmo_gsup_req_respond(req, &gsup_err, true, true);
477 return -1;
Oliver Smith28f0af82019-02-22 11:46:33 +0100478}
479
Harald Weltee72cf552016-04-28 07:18:49 +0200480static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
481{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100482 struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg);
483 if (!req)
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700484 return -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100485
486 /* If the GSUP recipient is other than this HLR, forward. */
487 if (req->gsup.destination_name_len) {
488 struct osmo_ipa_name destination_name;
489 struct osmo_ipa_name my_name;
490 osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno);
491 if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)
492 && osmo_ipa_name_cmp(&destination_name, &my_name)) {
493 return read_cb_forward(req);
494 }
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700495 }
496
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100497 switch (req->gsup.message_type) {
Harald Weltee72cf552016-04-28 07:18:49 +0200498 /* requests sent to us */
499 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100500 rx_send_auth_info(conn->auc_3g_ind, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200501 break;
502 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100503 rx_upd_loc_req(conn, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200504 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200505 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100506 rx_purge_ms_req(req);
Harald Welteb18f0e02016-05-05 21:03:03 +0200507 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200508 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100509 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100510 LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n");
511 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100512 break;
513 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100514 LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n");
515 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100516 break;
Harald Weltebb779392018-06-16 20:21:10 +0200517 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
518 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100519 rx_proc_ss_req(req);
Harald Weltebb779392018-06-16 20:21:10 +0200520 break;
521 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100522 rx_proc_ss_error(req);
Harald Weltebb779392018-06-16 20:21:10 +0200523 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200524 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200525 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200526 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
527 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100528 lu_rx_gsup(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200529 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100530 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100531 rx_check_imei_req(req);
Oliver Smith783ac812018-12-17 11:34:51 +0100532 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200533 default:
Maxaa0fefd2017-02-16 12:25:22 +0100534 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100535 osmo_gsup_message_type_name(req->gsup.message_type));
536 osmo_gsup_req_free(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200537 break;
538 }
539 return 0;
540}
541
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100542static void print_usage()
543{
544 printf("Usage: osmo-hlr\n");
545}
546
547static void print_help()
548{
549 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100550 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100551 printf(" -l --database db-name The database to use.\n");
552 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
553 printf(" -D --daemonize Fork the process into a background daemon.\n");
554 printf(" -s --disable-color Do not print ANSI colors in the log\n");
555 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
556 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100557 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100558 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100559 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100560}
561
562static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100563 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100564 const char *db_file;
565 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100566 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100567 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100568} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100569 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200570 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100571 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100572 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100573};
574
575static void handle_options(int argc, char **argv)
576{
577 while (1) {
578 int option_index = 0, c;
579 static struct option long_options[] = {
580 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100581 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100582 {"database", 1, 0, 'l'},
583 {"debug", 1, 0, 'd'},
584 {"daemonize", 0, 0, 'D'},
585 {"disable-color", 0, 0, 's'},
586 {"log-level", 1, 0, 'e'},
587 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100588 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100589 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100590 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100591 {0, 0, 0, 0}
592 };
593
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100594 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100595 long_options, &option_index);
596 if (c == -1)
597 break;
598
599 switch (c) {
600 case 'h':
601 print_usage();
602 print_help();
603 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100604 case 'c':
605 cmdline_opts.config_file = optarg;
606 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100607 case 'l':
608 cmdline_opts.db_file = optarg;
609 break;
610 case 'd':
611 log_parse_category_mask(osmo_stderr_target, optarg);
612 break;
613 case 'D':
614 cmdline_opts.daemonize = 1;
615 break;
616 case 's':
617 log_set_use_color(osmo_stderr_target, 0);
618 break;
619 case 'e':
620 log_set_log_level(osmo_stderr_target, atoi(optarg));
621 break;
622 case 'T':
623 log_set_print_timestamp(osmo_stderr_target, 1);
624 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100625 case 'U':
626 cmdline_opts.db_upgrade = true;
627 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100628 case 'C':
629 cmdline_opts.db_check = true;
630 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100631 case 'V':
632 print_version(1);
633 exit(0);
634 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100635 default:
636 /* catch unknown options *as well as* missing arguments. */
637 fprintf(stderr, "Error in command line options. Exiting.\n");
638 exit(-1);
639 break;
640 }
641 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100642
643 if (argc > optind) {
644 fprintf(stderr, "Unsupported positional arguments on command line\n");
645 exit(2);
646 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100647}
648
Harald Welteaabae9e2016-04-28 12:48:14 +0200649static void signal_hdlr(int signal)
650{
651 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700652 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200653 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700654 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700655 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200656 break;
657 case SIGUSR1:
658 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100659 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200660 break;
661 }
662}
Harald Weltee72cf552016-04-28 07:18:49 +0200663
Max372868b2017-03-02 12:12:00 +0100664static const char vlr_copyright[] =
665 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
666 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
667 "This is free software: you are free to change and redistribute it.\r\n"
668 "There is NO WARRANTY, to the extent permitted by law.\r\n";
669
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100670static struct vty_app_info vty_info = {
671 .name = "OsmoHLR",
672 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100673 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100674 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200675 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100676};
677
Harald Weltee72cf552016-04-28 07:18:49 +0200678int main(int argc, char **argv)
679{
Harald Weltee72cf552016-04-28 07:18:49 +0200680 int rc;
681
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700682 /* Track the use of talloc NULL memory contexts */
683 talloc_enable_null_tracking();
684
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100685 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
686 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100687 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200688
Maxd4bebbd2017-03-02 12:00:19 +0100689 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200690 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200691 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200692 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200693 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200694 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Maxd4bebbd2017-03-02 12:00:19 +0100695
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700696 /* Init default (call independent) SS session guard timeout value */
697 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
698
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200699 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200700 if (rc < 0) {
701 fprintf(stderr, "Error initializing logging\n");
702 exit(1);
703 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100704
Max20ddfdb2019-02-18 13:12:27 +0100705 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100706 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100707 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100708 handle_options(argc, argv);
Pau Espin Pedrole49391b2019-08-05 15:57:10 +0200709 hlr_vty_init();
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100710
711 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
712 if (rc < 0) {
713 LOGP(DMAIN, LOGL_FATAL,
714 "Failed to parse the config file: '%s'\n",
715 cmdline_opts.config_file);
716 return rc;
717 }
718
Harald Weltee72cf552016-04-28 07:18:49 +0200719 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
720
721 rc = rand_init();
722 if (rc < 0) {
723 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
724 exit(1);
725 }
726
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200727 if (cmdline_opts.db_file)
728 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
729
730 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100731 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200732 LOGP(DMAIN, LOGL_FATAL, "Error opening database %s\n", osmo_quote_str(g_hlr->db_file_path, -1));
Harald Weltee72cf552016-04-28 07:18:49 +0200733 exit(1);
734 }
735
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100736 if (cmdline_opts.db_check) {
737 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
738 db_close(g_hlr->dbc);
739 log_fini();
740 talloc_free(hlr_ctx);
741 talloc_free(tall_vty_ctx);
742 talloc_disable_null_tracking();
743 exit(0);
744 }
745
746 /* start telnet after reading config for vty_get_bind_addr() */
747 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
748 OSMO_VTY_PORT_HLR);
749 if (rc < 0)
750 return rc;
751
752
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200753 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100754 read_cb, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100755 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200756 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
757 exit(1);
758 }
759
Max372868b2017-03-02 12:12:00 +0100760 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200761 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100762
Harald Welteaabae9e2016-04-28 12:48:14 +0200763 osmo_init_ignore_signals();
764 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700765 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200766 signal(SIGUSR1, &signal_hdlr);
767
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100768 if (cmdline_opts.daemonize) {
769 rc = osmo_daemonize();
770 if (rc < 0) {
771 perror("Error during daemonize");
772 exit(1);
773 }
774 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200775
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700776 while (!quit)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100777 osmo_select_main_ctx(0);
778
Harald Weltee72cf552016-04-28 07:18:49 +0200779
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700780 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100781 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200782 log_fini();
783
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700784 /**
785 * Report the heap state of root context, then free,
786 * so both ASAN and Valgrind are happy...
787 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700788 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700789 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700790
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700791 /* FIXME: VTY code still uses NULL-context */
792 talloc_free(tall_vty_ctx);
793
794 /**
795 * Report the heap state of NULL context, then free,
796 * so both ASAN and Valgrind are happy...
797 */
798 talloc_report_full(NULL, stderr);
799 talloc_disable_null_tracking();
800
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700801 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200802}