blob: 70bd2a5b4d46d67744798a46b21828930595725d [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 Hofmeyrb2553eb2019-11-20 02:36:45 +010038#include <osmocom/gsm/gsm23003.h>
Harald Weltee72cf552016-04-28 07:18:49 +020039
Neels Hofmeyr0d28d852019-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 Hofmeyrb2553eb2019-11-20 02:36:45 +010050#include <osmocom/hlr/lu_fsm.h>
Neels Hofmeyra75d1c22019-11-20 03:35:37 +010051#include <osmocom/mslookup/mdns.h>
Harald Weltee72cf552016-04-28 07:18:49 +020052
Harald Welted5807b82018-07-29 12:27:41 +020053struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010054static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070055static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020056
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +010057struct osmo_tdef g_hlr_tdefs[] = {
58 /* 4222 is also the OSMO_GSUP_PORT */
59 { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" },
60 {}
61};
62
Stefan Sperlingf1622522018-04-09 11:39:16 +020063/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
64 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020065 * \param[in] subscr A subscriber we have new data to send for.
66 */
67void
68osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
69{
70 struct osmo_gsup_conn *co;
71
Neels Hofmeyre66e5252018-09-28 02:59:19 +020072 if (g_hlr->gs == NULL) {
73 LOGP(DLGSUP, LOGL_DEBUG,
74 "IMSI %s: NOT Notifying peers of subscriber data change,"
75 " there is no GSUP server\n",
76 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020077 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020078 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020079
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +010080 /* FIXME: send only to current vlr_number and sgsn_number */
81
Stefan Sperlingf1622522018-04-09 11:39:16 +020082 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020083 struct osmo_gsup_message gsup = { };
84 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
85 uint8_t apn[APN_MAXLEN];
86 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020087 uint8_t *peer;
88 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020089 size_t peer_strlen;
90 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020091 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020092
Neels Hofmeyre66e5252018-09-28 02:59:19 +020093 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020094 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020095 peer_compare = subscr->sgsn_number;
96 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020097 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020098 peer_compare = subscr->vlr_number;
99 } else {
100 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200101 continue;
102 }
103
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200104 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
105 if (peer_len < 0) {
106 LOGP(DLGSUP, LOGL_ERROR,
107 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
108 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
109 co && co->conn && co->conn->server? co->conn->server->port : 0);
110 continue;
111 }
112
113 peer_strlen = strnlen((const char*)peer, peer_len);
114 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
115 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
116 /* I hope peer is always nul terminated... */
117 if (peer_strlen < peer_len)
118 LOGP(DLGSUP, LOGL_DEBUG,
119 "IMSI %s: subscriber change: skipping %s peer %s\n",
120 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
121 osmo_quote_str((char*)peer, -1));
122 continue;
123 }
124
125 LOGP(DLGSUP, LOGL_DEBUG,
126 "IMSI %s: subscriber change: notifying %s peer %s\n",
127 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
128 osmo_quote_str(peer_compare, -1));
129
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200130 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
131 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200132 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200133 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200134 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200135 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
136 co && co->conn && co->conn->server? co->conn->server->port : 0);
137 continue;
138 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200139
Stefan Sperling93c5b102018-04-10 19:26:14 +0200140 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100141 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200142 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200143 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200144 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
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 Sperling93c5b102018-04-10 19:26:14 +0200150 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200151
Stefan Sperling93c5b102018-04-10 19:26:14 +0200152 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
153 LOGP(DMAIN, LOGL_ERROR,
154 "IMSI='%s': Cannot notify GSUP client; send operation failed "
155 "for %s:%u\n", subscr->imsi,
156 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
157 co && co->conn && co->conn->server? co->conn->server->port : 0);
158 continue;
159 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200160 }
161}
162
Oliver Smithc7f17872019-03-04 15:10:44 +0100163static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
164{
165 int i, j, rc;
166 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
167
168 OSMO_ASSERT(len <= sizeof(rand_buf));
169
170 /* Generate a random unique MSISDN (with retry) */
171 for (i = 0; i < 10; i++) {
172 /* Get a random number (with retry) */
173 for (j = 0; j < 10; j++) {
174 rc = osmo_get_rand_id(rand_buf, len);
175 if (!rc)
176 break;
177 }
178 if (rc) {
179 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
180 " failed (rc=%d)\n", imsi, rc);
181 return rc;
182 }
183
184 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
185 for (j = 0; j < len; j++)
186 msisdn[j] = 48 + (rand_buf[j] % 10);
187 msisdn[j] = '\0';
188
189 /* Ensure there is no subscriber with such MSISDN */
190 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
191 return 0;
192 }
193
194 /* Failure */
195 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
196 "the length for the automatically assigned MSISDNs "
197 "(see 'subscriber-create-on-demand' command)\n", imsi);
198 return -1;
199}
200
201static int subscr_create_on_demand(const char *imsi)
202{
203 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
204 int rc;
205 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
206
207 if (!g_hlr->subscr_create_on_demand)
208 return -1;
209 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
210 return -1;
211 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
212 return -1;
213
214 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
215 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
216 if (rc) {
217 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
218 return rc;
219 }
220
221 if (!rand_msisdn_len)
222 return 0;
223
224 /* Update MSISDN of the new (just allocated) subscriber */
225 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
226 if (rc) {
227 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
228 return rc;
229 }
230 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
231
232 return 0;
233}
234
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100235/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
236 * \param[in,out] hlr Global hlr context.
237 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
238 * \param[in] nam_val True to enable CS/PS, false to disable.
239 * \param[in] is_ps True to enable/disable PS, false for CS.
240 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
241 * value on error.
242 */
243int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
244{
245 int rc;
246 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
247 struct osmo_ipa_name vlr_name;
248 struct osmo_gsup_message gsup_del_data = {
249 .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST,
250 };
251 OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi);
252
253 if (is_val == nam_val) {
254 LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n",
255 subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
256 return ENOEXEC;
257 }
258
259 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
260 if (rc)
261 return rc > 0? -rc : rc;
262
263 /* If we're disabling, send a notice out to the GSUP client that is
264 * responsible. Otherwise no need. */
265 if (nam_val)
266 return 0;
267
268 if (subscr->vlr_number && osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number))
269 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
270 if (subscr->sgsn_number && osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number))
271 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
272 return 0;
273}
274
Harald Weltee687be52016-05-03 18:49:27 +0200275/***********************************************************************
276 * Send Auth Info handling
277 ***********************************************************************/
278
Harald Weltee72cf552016-04-28 07:18:49 +0200279/* process an incoming SAI request */
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100280static int rx_send_auth_info(unsigned int auc_3g_ind, struct osmo_gsup_req *req)
Harald Weltee72cf552016-04-28 07:18:49 +0200281{
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100282 struct osmo_gsup_message gsup_out = {
283 .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT,
284 };
Harald Welte06f5af22019-08-21 20:01:31 +0200285 bool separation_bit = false;
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100286 int num_auth_vectors = OSMO_GSUP_MAX_NUM_AUTH_INFO;
Harald Weltee72cf552016-04-28 07:18:49 +0200287 int rc;
288
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100289 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100290
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100291 if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS)
Harald Welte06f5af22019-08-21 20:01:31 +0200292 separation_bit = true;
293
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100294 if (req->gsup.num_auth_vectors > 0 &&
295 req->gsup.num_auth_vectors <= OSMO_GSUP_MAX_NUM_AUTH_INFO)
296 num_auth_vectors = req->gsup.num_auth_vectors;
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100297
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100298 rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100299 gsup_out.auth_vectors,
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100300 num_auth_vectors,
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100301 req->gsup.rand, req->gsup.auts, separation_bit);
302
Neels Hofmeyr671db902017-11-22 20:38:19 +0100303 if (rc <= 0) {
Neels Hofmeyr671db902017-11-22 20:38:19 +0100304 switch (rc) {
305 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100306 /* 0 means "0 tuples generated", which shouldn't happen.
307 * Treat the same as "no auth data". */
308 case -ENOKEY:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100309 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN,
310 "IMSI known, but has no auth data;"
311 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP");
312 return rc;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100313 case -ENOENT:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100314 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
315 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100316 default:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100317 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db");
318 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100319 }
Harald Weltee72cf552016-04-28 07:18:49 +0200320 }
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100321 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200322
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100323 osmo_gsup_req_respond(req, &gsup_out, false, true);
324 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200325}
326
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100327/*! Receive Update Location Request, creates new lu_operation */
328static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req)
Harald Weltee687be52016-05-03 18:49:27 +0200329{
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100330 switch (req->gsup.cn_domain) {
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200331 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200332 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200333 break;
334 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200335 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
336 * accordance with the GSUP spec in osmo-hlr's user manual (section
337 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
338 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200339 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200340 conn->supports_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200341 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200342 }
Harald Weltee687be52016-05-03 18:49:27 +0200343
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100344 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100345
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100346 lu_rx_gsup(req);
Harald Weltee687be52016-05-03 18:49:27 +0200347 return 0;
348}
349
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100350static int rx_purge_ms_req(struct osmo_gsup_req *req)
Harald Welteb18f0e02016-05-05 21:03:03 +0200351{
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100352 bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS);
Harald Welteb18f0e02016-05-05 21:03:03 +0200353 int rc;
354
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100355 LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS");
Harald Welteb18f0e02016-05-05 21:03:03 +0200356
357 /* FIXME: check if the VLR that sends the purge is the same that
358 * we have on record. Only update if yes */
359
360 /* Perform the actual update of the DB */
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100361 rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200362
Harald Welte3f2a9a22018-03-01 23:35:35 +0100363 if (rc == 0)
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100364 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true);
365 else if (rc == -ENOENT)
366 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
367 else
368 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error");
369 return rc;
Harald Welteb18f0e02016-05-05 21:03:03 +0200370}
371
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100372static int rx_check_imei_req(struct osmo_gsup_req *req)
Harald Weltea1d3b042018-06-11 20:28:35 +0200373{
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100374 struct osmo_gsup_message gsup_reply;
Oliver Smith103c11b2019-06-06 11:57:05 +0200375 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100376 const struct osmo_gsup_message *gsup = &req->gsup;
Oliver Smith103c11b2019-06-06 11:57:05 +0200377 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100378
Oliver Smith103c11b2019-06-06 11:57:05 +0200379 /* Require IMEI */
380 if (!gsup->imei_enc) {
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100381 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI");
Oliver Smith783ac812018-12-17 11:34:51 +0100382 return -1;
383 }
384
Oliver Smith103c11b2019-06-06 11:57:05 +0200385 /* Decode IMEI (fails if IMEI is too long) */
386 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
387 if (rc < 0) {
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100388 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
389 "failed to decode IMEI %s (rc: %d)",
390 osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len),
391 rc);
Oliver Smith103c11b2019-06-06 11:57:05 +0200392 return -1;
393 }
394
395 /* Check if IMEI is too short */
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100396 if (!osmo_imei_str_valid(imei, false)) {
397 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
398 "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1));
Oliver Smith783ac812018-12-17 11:34:51 +0100399 return -1;
400 }
401
Oliver Smithc7f17872019-03-04 15:10:44 +0100402 subscr_create_on_demand(gsup->imsi);
403
Oliver Smith851814a2019-01-11 15:30:21 +0100404 /* Save in DB if desired */
405 if (g_hlr->store_imei) {
406 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
407 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100408 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 +0100409 return -1;
410 }
411 } else {
412 /* Check if subscriber exists and print IMEI */
413 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
414 struct hlr_subscriber subscr;
415 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100416 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "IMSI unknown");
Oliver Smith851814a2019-01-11 15:30:21 +0100417 return -1;
418 }
419 }
Oliver Smith783ac812018-12-17 11:34:51 +0100420
421 /* Accept all IMEIs */
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100422 gsup_reply = (struct osmo_gsup_message){
423 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
424 .imei_result = OSMO_GSUP_IMEI_RESULT_ACK,
425 };
426 return osmo_gsup_req_respond(req, &gsup_reply, false, true);
Oliver Smith783ac812018-12-17 11:34:51 +0100427}
428
Oliver Smith28f0af82019-02-22 11:46:33 +0100429static char namebuf[255];
430#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
431 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
Neels Hofmeyrfbe49292020-01-09 18:35:03 +0100432 osmo_gsup_message_type_name((gsup)->message_type), \
433 osmo_gsup_message_class_name((gsup)->message_class), \
434 (gsup)->imsi, \
435 osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \
436 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \
Oliver Smith28f0af82019-02-22 11:46:33 +0100437 ## args)
438
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100439static int read_cb_forward(struct osmo_gsup_req *req)
Oliver Smith28f0af82019-02-22 11:46:33 +0100440{
441 int ret = -EINVAL;
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100442 const struct osmo_gsup_message *gsup = &req->gsup;
443 struct osmo_gsup_message gsup_err;
444 struct msgb *forward_msg;
445 struct osmo_ipa_name destination_name;
Oliver Smith28f0af82019-02-22 11:46:33 +0100446
447 /* Check for routing IEs */
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100448 if (!req->gsup.source_name || !req->gsup.source_name_len
449 || !req->gsup.destination_name || !req->gsup.destination_name_len) {
450 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n");
451 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100452 }
453
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100454 if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) {
455 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n");
456 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100457 }
458
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100459 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name));
Oliver Smith28f0af82019-02-22 11:46:33 +0100460
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100461 /* Forward message without re-encoding (so we don't remove unknown IEs).
462 * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */
463 forward_msg = osmo_gsup_msgb_alloc("GSUP forward");
464 forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg));
465 memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg));
466 ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg);
Oliver Smith28f0af82019-02-22 11:46:33 +0100467 if (ret) {
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100468 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n",
469 ret == -ENODEV ? "destination not connected" : "unknown error",
470 ret);
471 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100472 }
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100473 osmo_gsup_req_free(req);
474 return 0;
475
476routing_error:
477 gsup_err = (struct osmo_gsup_message){
478 .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR,
479 .source_name = gsup->destination_name,
480 .source_name_len = gsup->destination_name_len,
481 };
482 osmo_gsup_req_respond(req, &gsup_err, true, true);
483 return -1;
Oliver Smith28f0af82019-02-22 11:46:33 +0100484}
485
Harald Weltee72cf552016-04-28 07:18:49 +0200486static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
487{
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100488 struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg);
489 if (!req)
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700490 return -EINVAL;
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100491
492 /* If the GSUP recipient is other than this HLR, forward. */
493 if (req->gsup.destination_name_len) {
494 struct osmo_ipa_name destination_name;
495 struct osmo_ipa_name my_name;
496 osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno);
497 if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)
498 && osmo_ipa_name_cmp(&destination_name, &my_name)) {
499 return read_cb_forward(req);
500 }
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700501 }
502
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100503 switch (req->gsup.message_type) {
Harald Weltee72cf552016-04-28 07:18:49 +0200504 /* requests sent to us */
505 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100506 rx_send_auth_info(conn->auc_3g_ind, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200507 break;
508 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100509 rx_upd_loc_req(conn, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200510 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200511 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100512 rx_purge_ms_req(req);
Harald Welteb18f0e02016-05-05 21:03:03 +0200513 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200514 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100515 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100516 LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n");
517 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100518 break;
519 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100520 LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n");
521 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100522 break;
Harald Weltebb779392018-06-16 20:21:10 +0200523 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
524 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100525 rx_proc_ss_req(req);
Harald Weltebb779392018-06-16 20:21:10 +0200526 break;
527 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100528 rx_proc_ss_error(req);
Harald Weltebb779392018-06-16 20:21:10 +0200529 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200530 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200531 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200532 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
533 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100534 lu_rx_gsup(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200535 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100536 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100537 rx_check_imei_req(req);
Oliver Smith783ac812018-12-17 11:34:51 +0100538 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200539 default:
Maxaa0fefd2017-02-16 12:25:22 +0100540 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100541 osmo_gsup_message_type_name(req->gsup.message_type));
542 osmo_gsup_req_free(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200543 break;
544 }
545 return 0;
546}
547
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100548static void print_usage()
549{
550 printf("Usage: osmo-hlr\n");
551}
552
553static void print_help()
554{
555 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100556 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100557 printf(" -l --database db-name The database to use.\n");
558 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
559 printf(" -D --daemonize Fork the process into a background daemon.\n");
560 printf(" -s --disable-color Do not print ANSI colors in the log\n");
561 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
562 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100563 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100564 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100565 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100566}
567
568static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100569 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100570 const char *db_file;
571 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100572 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100573 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100574} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100575 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200576 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100577 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100578 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100579};
580
581static void handle_options(int argc, char **argv)
582{
583 while (1) {
584 int option_index = 0, c;
585 static struct option long_options[] = {
586 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100587 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100588 {"database", 1, 0, 'l'},
589 {"debug", 1, 0, 'd'},
590 {"daemonize", 0, 0, 'D'},
591 {"disable-color", 0, 0, 's'},
592 {"log-level", 1, 0, 'e'},
593 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100594 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100595 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100596 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100597 {0, 0, 0, 0}
598 };
599
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100600 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100601 long_options, &option_index);
602 if (c == -1)
603 break;
604
605 switch (c) {
606 case 'h':
607 print_usage();
608 print_help();
609 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100610 case 'c':
611 cmdline_opts.config_file = optarg;
612 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100613 case 'l':
614 cmdline_opts.db_file = optarg;
615 break;
616 case 'd':
617 log_parse_category_mask(osmo_stderr_target, optarg);
618 break;
619 case 'D':
620 cmdline_opts.daemonize = 1;
621 break;
622 case 's':
623 log_set_use_color(osmo_stderr_target, 0);
624 break;
625 case 'e':
626 log_set_log_level(osmo_stderr_target, atoi(optarg));
627 break;
628 case 'T':
629 log_set_print_timestamp(osmo_stderr_target, 1);
630 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100631 case 'U':
632 cmdline_opts.db_upgrade = true;
633 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100634 case 'C':
635 cmdline_opts.db_check = true;
636 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100637 case 'V':
638 print_version(1);
639 exit(0);
640 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100641 default:
642 /* catch unknown options *as well as* missing arguments. */
643 fprintf(stderr, "Error in command line options. Exiting.\n");
644 exit(-1);
645 break;
646 }
647 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100648
649 if (argc > optind) {
650 fprintf(stderr, "Unsupported positional arguments on command line\n");
651 exit(2);
652 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100653}
654
Harald Welteaabae9e2016-04-28 12:48:14 +0200655static void signal_hdlr(int signal)
656{
657 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700658 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200659 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700660 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700661 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200662 break;
663 case SIGUSR1:
664 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100665 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200666 break;
667 }
668}
Harald Weltee72cf552016-04-28 07:18:49 +0200669
Max372868b2017-03-02 12:12:00 +0100670static const char vlr_copyright[] =
671 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
672 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
673 "This is free software: you are free to change and redistribute it.\r\n"
674 "There is NO WARRANTY, to the extent permitted by law.\r\n";
675
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100676static struct vty_app_info vty_info = {
677 .name = "OsmoHLR",
678 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100679 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100680 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200681 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100682};
683
Harald Weltee72cf552016-04-28 07:18:49 +0200684int main(int argc, char **argv)
685{
Harald Weltee72cf552016-04-28 07:18:49 +0200686 int rc;
687
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700688 /* Track the use of talloc NULL memory contexts */
689 talloc_enable_null_tracking();
690
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100691 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
692 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100693 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200694
Maxd4bebbd2017-03-02 12:00:19 +0100695 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200696 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200697 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200698 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Neels Hofmeyr32cc07a2019-11-20 03:35:37 +0100699 INIT_LLIST_HEAD(&g_hlr->mslookup.server.local_site_services);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200700 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Neels Hofmeyra75d1c22019-11-20 03:35:37 +0100701 g_hlr->mslookup.server.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Maxd4bebbd2017-03-02 12:00:19 +0100702
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700703 /* Init default (call independent) SS session guard timeout value */
704 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
705
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200706 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200707 if (rc < 0) {
708 fprintf(stderr, "Error initializing logging\n");
709 exit(1);
710 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100711
Max20ddfdb2019-02-18 13:12:27 +0100712 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100713 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100714 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100715 handle_options(argc, argv);
Pau Espin Pedrole49391b2019-08-05 15:57:10 +0200716 hlr_vty_init();
Neels Hofmeyra75d1c22019-11-20 03:35:37 +0100717 dgsm_vty_init();
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100718
719 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
720 if (rc < 0) {
721 LOGP(DMAIN, LOGL_FATAL,
722 "Failed to parse the config file: '%s'\n",
723 cmdline_opts.config_file);
724 return rc;
725 }
726
Harald Weltee72cf552016-04-28 07:18:49 +0200727 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
728
729 rc = rand_init();
730 if (rc < 0) {
731 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
732 exit(1);
733 }
734
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200735 if (cmdline_opts.db_file)
736 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
737
738 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100739 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200740 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 +0200741 exit(1);
742 }
743
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100744 if (cmdline_opts.db_check) {
745 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
746 db_close(g_hlr->dbc);
747 log_fini();
748 talloc_free(hlr_ctx);
749 talloc_free(tall_vty_ctx);
750 talloc_disable_null_tracking();
751 exit(0);
752 }
753
754 /* start telnet after reading config for vty_get_bind_addr() */
755 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
756 OSMO_VTY_PORT_HLR);
757 if (rc < 0)
758 return rc;
759
760
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200761 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100762 read_cb, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100763 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200764 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
765 exit(1);
766 }
767
Max372868b2017-03-02 12:12:00 +0100768 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200769 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100770
Harald Welteaabae9e2016-04-28 12:48:14 +0200771 osmo_init_ignore_signals();
772 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700773 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200774 signal(SIGUSR1, &signal_hdlr);
775
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100776 if (cmdline_opts.daemonize) {
777 rc = osmo_daemonize();
778 if (rc < 0) {
779 perror("Error during daemonize");
780 exit(1);
781 }
782 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200783
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700784 while (!quit)
Neels Hofmeyrb2553eb2019-11-20 02:36:45 +0100785 osmo_select_main_ctx(0);
786
Harald Weltee72cf552016-04-28 07:18:49 +0200787
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700788 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100789 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200790 log_fini();
791
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700792 /**
793 * Report the heap state of root context, then free,
794 * so both ASAN and Valgrind are happy...
795 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700796 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700797 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700798
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700799 /* FIXME: VTY code still uses NULL-context */
800 talloc_free(tall_vty_ctx);
801
802 /**
803 * Report the heap state of NULL context, then free,
804 * so both ASAN and Valgrind are happy...
805 */
806 talloc_report_full(NULL, stderr);
807 talloc_disable_null_tracking();
808
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700809 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200810}