blob: 79b50c2c70f0252a3d9c650c0d6f6d255d07ec9e [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>
Neels Hofmeyr62d916f2019-11-20 03:35:37 +010039#include <osmocom/mslookup/mslookup_client.h>
Harald Weltee72cf552016-04-28 07:18:49 +020040
Neels Hofmeyr008ce4b2019-12-04 01:04:32 +010041#include <osmocom/gsupclient/gsup_peer_id.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010042#include <osmocom/hlr/db.h>
43#include <osmocom/hlr/hlr.h>
44#include <osmocom/hlr/ctrl.h>
45#include <osmocom/hlr/logging.h>
46#include <osmocom/hlr/gsup_server.h>
47#include <osmocom/hlr/gsup_router.h>
48#include <osmocom/hlr/rand.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010049#include <osmocom/hlr/hlr_vty.h>
50#include <osmocom/hlr/hlr_ussd.h>
Neels Hofmeyr62d916f2019-11-20 03:35:37 +010051#include <osmocom/hlr/dgsm.h>
52#include <osmocom/hlr/proxy.h>
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010053#include <osmocom/hlr/lu_fsm.h>
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +010054#include <osmocom/mslookup/mdns.h>
Harald Weltee72cf552016-04-28 07:18:49 +020055
Harald Welted5807b82018-07-29 12:27:41 +020056struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010057static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070058static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020059
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010060struct osmo_tdef g_hlr_tdefs[] = {
61 /* 4222 is also the OSMO_GSUP_PORT */
62 { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" },
63 {}
64};
65
Stefan Sperlingf1622522018-04-09 11:39:16 +020066/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
67 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020068 * \param[in] subscr A subscriber we have new data to send for.
69 */
70void
71osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
72{
73 struct osmo_gsup_conn *co;
74
Neels Hofmeyre66e5252018-09-28 02:59:19 +020075 if (g_hlr->gs == NULL) {
76 LOGP(DLGSUP, LOGL_DEBUG,
77 "IMSI %s: NOT Notifying peers of subscriber data change,"
78 " there is no GSUP server\n",
79 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020080 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020081 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020082
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010083 /* FIXME: send only to current vlr_number and sgsn_number */
84
Stefan Sperlingf1622522018-04-09 11:39:16 +020085 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020086 struct osmo_gsup_message gsup = { };
87 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
88 uint8_t apn[APN_MAXLEN];
89 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020090 uint8_t *peer;
91 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020092 size_t peer_strlen;
93 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020094 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020095
Neels Hofmeyre66e5252018-09-28 02:59:19 +020096 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020097 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020098 peer_compare = subscr->sgsn_number;
99 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200100 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200101 peer_compare = subscr->vlr_number;
102 } else {
103 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200104 continue;
105 }
106
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200107 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
108 if (peer_len < 0) {
109 LOGP(DLGSUP, LOGL_ERROR,
110 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
111 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
112 co && co->conn && co->conn->server? co->conn->server->port : 0);
113 continue;
114 }
115
116 peer_strlen = strnlen((const char*)peer, peer_len);
117 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
118 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
119 /* I hope peer is always nul terminated... */
120 if (peer_strlen < peer_len)
121 LOGP(DLGSUP, LOGL_DEBUG,
122 "IMSI %s: subscriber change: skipping %s peer %s\n",
123 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
124 osmo_quote_str((char*)peer, -1));
125 continue;
126 }
127
128 LOGP(DLGSUP, LOGL_DEBUG,
129 "IMSI %s: subscriber change: notifying %s peer %s\n",
130 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
131 osmo_quote_str(peer_compare, -1));
132
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200133 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
134 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200135 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200136 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200137 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200138 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
139 co && co->conn && co->conn->server? co->conn->server->port : 0);
140 continue;
141 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200142
Stefan Sperling93c5b102018-04-10 19:26:14 +0200143 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100144 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200145 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200146 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200147 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
148 "for %s:%u\n", subscr->imsi,
149 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
150 co && co->conn && co->conn->server? co->conn->server->port : 0);
151 continue;
152 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200153 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200154
Stefan Sperling93c5b102018-04-10 19:26:14 +0200155 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
156 LOGP(DMAIN, LOGL_ERROR,
157 "IMSI='%s': Cannot notify GSUP client; send operation failed "
158 "for %s:%u\n", subscr->imsi,
159 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
160 co && co->conn && co->conn->server? co->conn->server->port : 0);
161 continue;
162 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200163 }
164}
165
Oliver Smithc7f17872019-03-04 15:10:44 +0100166static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
167{
168 int i, j, rc;
169 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
170
171 OSMO_ASSERT(len <= sizeof(rand_buf));
172
173 /* Generate a random unique MSISDN (with retry) */
174 for (i = 0; i < 10; i++) {
175 /* Get a random number (with retry) */
176 for (j = 0; j < 10; j++) {
177 rc = osmo_get_rand_id(rand_buf, len);
178 if (!rc)
179 break;
180 }
181 if (rc) {
182 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
183 " failed (rc=%d)\n", imsi, rc);
184 return rc;
185 }
186
187 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
188 for (j = 0; j < len; j++)
189 msisdn[j] = 48 + (rand_buf[j] % 10);
190 msisdn[j] = '\0';
191
192 /* Ensure there is no subscriber with such MSISDN */
193 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
194 return 0;
195 }
196
197 /* Failure */
198 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
199 "the length for the automatically assigned MSISDNs "
200 "(see 'subscriber-create-on-demand' command)\n", imsi);
201 return -1;
202}
203
204static int subscr_create_on_demand(const char *imsi)
205{
206 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
207 int rc;
208 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
209
210 if (!g_hlr->subscr_create_on_demand)
211 return -1;
212 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
213 return -1;
214 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
215 return -1;
216
217 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
218 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
219 if (rc) {
220 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
221 return rc;
222 }
223
224 if (!rand_msisdn_len)
225 return 0;
226
227 /* Update MSISDN of the new (just allocated) subscriber */
228 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
229 if (rc) {
230 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
231 return rc;
232 }
233 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
234
235 return 0;
236}
237
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100238/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
239 * \param[in,out] hlr Global hlr context.
240 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
241 * \param[in] nam_val True to enable CS/PS, false to disable.
242 * \param[in] is_ps True to enable/disable PS, false for CS.
243 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
244 * value on error.
245 */
246int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
247{
248 int rc;
249 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
250 struct osmo_ipa_name vlr_name;
251 struct osmo_gsup_message gsup_del_data = {
252 .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST,
253 };
254 OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi);
255
256 if (is_val == nam_val) {
257 LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n",
258 subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
259 return ENOEXEC;
260 }
261
262 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
263 if (rc)
264 return rc > 0? -rc : rc;
265
266 /* If we're disabling, send a notice out to the GSUP client that is
267 * responsible. Otherwise no need. */
268 if (nam_val)
269 return 0;
270
271 if (subscr->vlr_number && osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number))
272 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
273 if (subscr->sgsn_number && osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number))
274 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
275 return 0;
276}
277
Harald Weltee687be52016-05-03 18:49:27 +0200278/***********************************************************************
279 * Send Auth Info handling
280 ***********************************************************************/
281
Harald Weltee72cf552016-04-28 07:18:49 +0200282/* process an incoming SAI request */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100283static int rx_send_auth_info(unsigned int auc_3g_ind, struct osmo_gsup_req *req)
Harald Weltee72cf552016-04-28 07:18:49 +0200284{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100285 struct osmo_gsup_message gsup_out = {
286 .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT,
287 };
Harald Welte06f5af22019-08-21 20:01:31 +0200288 bool separation_bit = false;
Harald Weltee72cf552016-04-28 07:18:49 +0200289 int rc;
290
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100291 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100292
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100293 if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS)
Harald Welte06f5af22019-08-21 20:01:31 +0200294 separation_bit = true;
295
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100296 rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100297 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200298 ARRAY_SIZE(gsup_out.auth_vectors),
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100299 req->gsup.rand, req->gsup.auts, separation_bit);
300
Neels Hofmeyr671db902017-11-22 20:38:19 +0100301 if (rc <= 0) {
Neels Hofmeyr671db902017-11-22 20:38:19 +0100302 switch (rc) {
303 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100304 /* 0 means "0 tuples generated", which shouldn't happen.
305 * Treat the same as "no auth data". */
306 case -ENOKEY:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100307 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN,
308 "IMSI known, but has no auth data;"
309 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP");
310 return rc;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100311 case -ENOENT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100312 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
313 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100314 default:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100315 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db");
316 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100317 }
Harald Weltee72cf552016-04-28 07:18:49 +0200318 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100319 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200320
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100321 osmo_gsup_req_respond(req, &gsup_out, false, true);
322 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200323}
324
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100325/*! Receive Update Location Request, creates new lu_operation */
326static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req)
Harald Weltee687be52016-05-03 18:49:27 +0200327{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100328 switch (req->gsup.cn_domain) {
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200329 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200330 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200331 break;
332 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200333 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
334 * accordance with the GSUP spec in osmo-hlr's user manual (section
335 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
336 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200337 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200338 conn->supports_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200339 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200340 }
Harald Weltee687be52016-05-03 18:49:27 +0200341
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100342 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100343
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100344 lu_rx_gsup(req);
Harald Weltee687be52016-05-03 18:49:27 +0200345 return 0;
346}
347
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100348static int rx_purge_ms_req(struct osmo_gsup_req *req)
Harald Welteb18f0e02016-05-05 21:03:03 +0200349{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100350 bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS);
Harald Welteb18f0e02016-05-05 21:03:03 +0200351 int rc;
352
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100353 LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS");
Harald Welteb18f0e02016-05-05 21:03:03 +0200354
355 /* FIXME: check if the VLR that sends the purge is the same that
356 * we have on record. Only update if yes */
357
358 /* Perform the actual update of the DB */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100359 rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200360
Harald Welte3f2a9a22018-03-01 23:35:35 +0100361 if (rc == 0)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100362 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true);
363 else if (rc == -ENOENT)
364 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
365 else
366 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error");
367 return rc;
Harald Welteb18f0e02016-05-05 21:03:03 +0200368}
369
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100370static int rx_check_imei_req(struct osmo_gsup_req *req)
Harald Weltea1d3b042018-06-11 20:28:35 +0200371{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100372 struct osmo_gsup_message gsup_reply;
Oliver Smith103c11b2019-06-06 11:57:05 +0200373 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100374 const struct osmo_gsup_message *gsup = &req->gsup;
Oliver Smith103c11b2019-06-06 11:57:05 +0200375 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100376
Oliver Smith103c11b2019-06-06 11:57:05 +0200377 /* Require IMEI */
378 if (!gsup->imei_enc) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100379 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI");
Oliver Smith783ac812018-12-17 11:34:51 +0100380 return -1;
381 }
382
Oliver Smith103c11b2019-06-06 11:57:05 +0200383 /* Decode IMEI (fails if IMEI is too long) */
384 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
385 if (rc < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100386 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
387 "failed to decode IMEI %s (rc: %d)",
388 osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len),
389 rc);
Oliver Smith103c11b2019-06-06 11:57:05 +0200390 return -1;
391 }
392
393 /* Check if IMEI is too short */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100394 if (!osmo_imei_str_valid(imei, false)) {
395 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
396 "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1));
Oliver Smith783ac812018-12-17 11:34:51 +0100397 return -1;
398 }
399
Oliver Smithc7f17872019-03-04 15:10:44 +0100400 subscr_create_on_demand(gsup->imsi);
401
Oliver Smith851814a2019-01-11 15:30:21 +0100402 /* Save in DB if desired */
403 if (g_hlr->store_imei) {
404 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
405 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100406 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 +0100407 return -1;
408 }
409 } else {
410 /* Check if subscriber exists and print IMEI */
411 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
412 struct hlr_subscriber subscr;
413 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100414 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "IMSI unknown");
Oliver Smith851814a2019-01-11 15:30:21 +0100415 return -1;
416 }
417 }
Oliver Smith783ac812018-12-17 11:34:51 +0100418
419 /* Accept all IMEIs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100420 gsup_reply = (struct osmo_gsup_message){
421 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
422 .imei_result = OSMO_GSUP_IMEI_RESULT_ACK,
423 };
424 return osmo_gsup_req_respond(req, &gsup_reply, false, true);
Oliver Smith783ac812018-12-17 11:34:51 +0100425}
426
Oliver Smith28f0af82019-02-22 11:46:33 +0100427static char namebuf[255];
428#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
429 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100430 osmo_gsup_message_type_name((gsup)->message_type), \
431 osmo_gsup_message_class_name((gsup)->message_class), \
432 (gsup)->imsi, \
433 osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \
434 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \
Oliver Smith28f0af82019-02-22 11:46:33 +0100435 ## args)
436
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100437static int read_cb_forward(struct osmo_gsup_req *req)
Oliver Smith28f0af82019-02-22 11:46:33 +0100438{
439 int ret = -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100440 const struct osmo_gsup_message *gsup = &req->gsup;
441 struct osmo_gsup_message gsup_err;
442 struct msgb *forward_msg;
443 struct osmo_ipa_name destination_name;
Oliver Smith28f0af82019-02-22 11:46:33 +0100444
445 /* Check for routing IEs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100446 if (!req->gsup.source_name[0] || !req->gsup.source_name_len
447 || !req->gsup.destination_name[0] || !req->gsup.destination_name_len) {
448 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n");
449 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100450 }
451
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100452 if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) {
453 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n");
454 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100455 }
456
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100457 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name));
Oliver Smith28f0af82019-02-22 11:46:33 +0100458
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100459 /* Forward message without re-encoding (so we don't remove unknown IEs).
460 * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */
461 forward_msg = osmo_gsup_msgb_alloc("GSUP forward");
462 forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg));
463 memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg));
464 ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg);
Oliver Smith28f0af82019-02-22 11:46:33 +0100465 if (ret) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100466 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n",
467 ret == -ENODEV ? "destination not connected" : "unknown error",
468 ret);
469 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100470 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100471 osmo_gsup_req_free(req);
472 return 0;
473
474routing_error:
475 gsup_err = (struct osmo_gsup_message){
476 .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR,
477 .source_name = gsup->destination_name,
478 .source_name_len = gsup->destination_name_len,
479 };
480 osmo_gsup_req_respond(req, &gsup_err, true, true);
481 return -1;
Oliver Smith28f0af82019-02-22 11:46:33 +0100482}
483
Harald Weltee72cf552016-04-28 07:18:49 +0200484static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
485{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100486 struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg);
487 if (!req)
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700488 return -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100489
490 /* If the GSUP recipient is other than this HLR, forward. */
491 if (req->gsup.destination_name_len) {
492 struct osmo_ipa_name destination_name;
493 struct osmo_ipa_name my_name;
494 osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno);
495 if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)
496 && osmo_ipa_name_cmp(&destination_name, &my_name)) {
497 return read_cb_forward(req);
498 }
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700499 }
500
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100501 /* Distributed GSM: check whether to proxy for / lookup a remote HLR.
502 * It would require less database hits to do this only if a local-only operation fails with "unknown IMSI", but
503 * it becomes semantically easier if we do this once-off ahead of time. */
504 if (osmo_mslookup_client_active(g_hlr->mslookup.client.client)
505 || osmo_sockaddr_str_is_nonzero(&g_hlr->mslookup.client.gsup_gateway_proxy)) {
506 if (dgsm_check_forward_gsup_msg(req))
507 return 0;
508 }
509
Neels Hofmeyre78241a2019-12-06 17:09:56 +0100510 /* HLR related messages that are handled at this HLR instance */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100511 switch (req->gsup.message_type) {
Harald Weltee72cf552016-04-28 07:18:49 +0200512 /* requests sent to us */
513 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100514 rx_send_auth_info(conn->auc_3g_ind, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200515 break;
516 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100517 rx_upd_loc_req(conn, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200518 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200519 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100520 rx_purge_ms_req(req);
Harald Welteb18f0e02016-05-05 21:03:03 +0200521 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200522 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100523 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100524 LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n");
525 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100526 break;
527 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100528 LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n");
529 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100530 break;
Harald Weltebb779392018-06-16 20:21:10 +0200531 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
532 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100533 rx_proc_ss_req(req);
Harald Weltebb779392018-06-16 20:21:10 +0200534 break;
535 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100536 rx_proc_ss_error(req);
Harald Weltebb779392018-06-16 20:21:10 +0200537 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200538 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200539 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200540 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
541 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100542 lu_rx_gsup(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200543 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100544 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100545 rx_check_imei_req(req);
Oliver Smith783ac812018-12-17 11:34:51 +0100546 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200547 default:
Maxaa0fefd2017-02-16 12:25:22 +0100548 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100549 osmo_gsup_message_type_name(req->gsup.message_type));
550 osmo_gsup_req_free(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200551 break;
552 }
553 return 0;
554}
555
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100556static void print_usage()
557{
558 printf("Usage: osmo-hlr\n");
559}
560
561static void print_help()
562{
563 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100564 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100565 printf(" -l --database db-name The database to use.\n");
566 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
567 printf(" -D --daemonize Fork the process into a background daemon.\n");
568 printf(" -s --disable-color Do not print ANSI colors in the log\n");
569 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
570 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100571 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100572 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100573 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100574}
575
576static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100577 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100578 const char *db_file;
579 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100580 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100581 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100582} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100583 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200584 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100585 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100586 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100587};
588
589static void handle_options(int argc, char **argv)
590{
591 while (1) {
592 int option_index = 0, c;
593 static struct option long_options[] = {
594 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100595 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100596 {"database", 1, 0, 'l'},
597 {"debug", 1, 0, 'd'},
598 {"daemonize", 0, 0, 'D'},
599 {"disable-color", 0, 0, 's'},
600 {"log-level", 1, 0, 'e'},
601 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100602 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100603 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100604 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100605 {0, 0, 0, 0}
606 };
607
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100608 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100609 long_options, &option_index);
610 if (c == -1)
611 break;
612
613 switch (c) {
614 case 'h':
615 print_usage();
616 print_help();
617 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100618 case 'c':
619 cmdline_opts.config_file = optarg;
620 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100621 case 'l':
622 cmdline_opts.db_file = optarg;
623 break;
624 case 'd':
625 log_parse_category_mask(osmo_stderr_target, optarg);
626 break;
627 case 'D':
628 cmdline_opts.daemonize = 1;
629 break;
630 case 's':
631 log_set_use_color(osmo_stderr_target, 0);
632 break;
633 case 'e':
634 log_set_log_level(osmo_stderr_target, atoi(optarg));
635 break;
636 case 'T':
637 log_set_print_timestamp(osmo_stderr_target, 1);
638 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100639 case 'U':
640 cmdline_opts.db_upgrade = true;
641 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100642 case 'C':
643 cmdline_opts.db_check = true;
644 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100645 case 'V':
646 print_version(1);
647 exit(0);
648 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100649 default:
650 /* catch unknown options *as well as* missing arguments. */
651 fprintf(stderr, "Error in command line options. Exiting.\n");
652 exit(-1);
653 break;
654 }
655 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100656
657 if (argc > optind) {
658 fprintf(stderr, "Unsupported positional arguments on command line\n");
659 exit(2);
660 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100661}
662
Harald Welteaabae9e2016-04-28 12:48:14 +0200663static void signal_hdlr(int signal)
664{
665 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700666 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200667 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700668 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700669 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200670 break;
671 case SIGUSR1:
672 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100673 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200674 break;
675 }
676}
Harald Weltee72cf552016-04-28 07:18:49 +0200677
Max372868b2017-03-02 12:12:00 +0100678static const char vlr_copyright[] =
679 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
680 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
681 "This is free software: you are free to change and redistribute it.\r\n"
682 "There is NO WARRANTY, to the extent permitted by law.\r\n";
683
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100684static struct vty_app_info vty_info = {
685 .name = "OsmoHLR",
686 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100687 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100688 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200689 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100690};
691
Harald Weltee72cf552016-04-28 07:18:49 +0200692int main(int argc, char **argv)
693{
Harald Weltee72cf552016-04-28 07:18:49 +0200694 int rc;
695
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700696 /* Track the use of talloc NULL memory contexts */
697 talloc_enable_null_tracking();
698
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100699 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
700 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100701 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200702
Maxd4bebbd2017-03-02 12:00:19 +0100703 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200704 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200705 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200706 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200707 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Neels Hofmeyr6aa871d2019-11-20 03:35:37 +0100708 INIT_LLIST_HEAD(&g_hlr->mslookup.server.local_site_services);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200709 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +0100710 g_hlr->mslookup.server.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100711 g_hlr->mslookup.client.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Maxd4bebbd2017-03-02 12:00:19 +0100712
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700713 /* Init default (call independent) SS session guard timeout value */
714 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
715
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200716 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200717 if (rc < 0) {
718 fprintf(stderr, "Error initializing logging\n");
719 exit(1);
720 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100721
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100722 /* Set up llists and objects, startup is happening from VTY commands. */
723 dgsm_init(hlr_ctx);
724
Max20ddfdb2019-02-18 13:12:27 +0100725 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100726 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100727 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100728 handle_options(argc, argv);
Pau Espin Pedrole49391b2019-08-05 15:57:10 +0200729 hlr_vty_init();
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +0100730 dgsm_vty_init();
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100731
732 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
733 if (rc < 0) {
734 LOGP(DMAIN, LOGL_FATAL,
735 "Failed to parse the config file: '%s'\n",
736 cmdline_opts.config_file);
737 return rc;
738 }
739
Harald Weltee72cf552016-04-28 07:18:49 +0200740 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
741
742 rc = rand_init();
743 if (rc < 0) {
744 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
745 exit(1);
746 }
747
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200748 if (cmdline_opts.db_file)
749 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
750
751 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100752 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200753 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 +0200754 exit(1);
755 }
756
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100757 if (cmdline_opts.db_check) {
758 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
759 db_close(g_hlr->dbc);
760 log_fini();
761 talloc_free(hlr_ctx);
762 talloc_free(tall_vty_ctx);
763 talloc_disable_null_tracking();
764 exit(0);
765 }
766
767 /* start telnet after reading config for vty_get_bind_addr() */
768 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
769 OSMO_VTY_PORT_HLR);
770 if (rc < 0)
771 return rc;
772
773
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200774 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100775 read_cb, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100776 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200777 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
778 exit(1);
779 }
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100780 proxy_init(g_hlr->gs);
Harald Weltee72cf552016-04-28 07:18:49 +0200781
Max372868b2017-03-02 12:12:00 +0100782 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200783 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100784
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100785 dgsm_start(hlr_ctx);
786
Harald Welteaabae9e2016-04-28 12:48:14 +0200787 osmo_init_ignore_signals();
788 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700789 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200790 signal(SIGUSR1, &signal_hdlr);
791
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100792 if (cmdline_opts.daemonize) {
793 rc = osmo_daemonize();
794 if (rc < 0) {
795 perror("Error during daemonize");
796 exit(1);
797 }
798 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200799
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700800 while (!quit)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100801 osmo_select_main_ctx(0);
802
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100803 dgsm_stop();
Harald Weltee72cf552016-04-28 07:18:49 +0200804
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700805 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100806 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200807 log_fini();
808
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700809 /**
810 * Report the heap state of root context, then free,
811 * so both ASAN and Valgrind are happy...
812 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700813 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700814 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700815
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700816 /* FIXME: VTY code still uses NULL-context */
817 talloc_free(tall_vty_ctx);
818
819 /**
820 * Report the heap state of NULL context, then free,
821 * so both ASAN and Valgrind are happy...
822 */
823 talloc_report_full(NULL, stderr);
824 talloc_disable_null_tracking();
825
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700826 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200827}