blob: 6599d70019e444d46e425a07cb774100707f47d3 [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 Hofmeyrfd08e4d2019-12-16 17:15:40 +010054#include <osmocom/hlr/sms_over_gsup.h>
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +010055#include <osmocom/mslookup/mdns.h>
Harald Weltee72cf552016-04-28 07:18:49 +020056
Harald Welted5807b82018-07-29 12:27:41 +020057struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010058static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070059static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020060
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010061struct osmo_tdef g_hlr_tdefs[] = {
62 /* 4222 is also the OSMO_GSUP_PORT */
63 { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" },
64 {}
65};
66
Stefan Sperlingf1622522018-04-09 11:39:16 +020067/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
68 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020069 * \param[in] subscr A subscriber we have new data to send for.
70 */
71void
72osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
73{
74 struct osmo_gsup_conn *co;
75
Neels Hofmeyre66e5252018-09-28 02:59:19 +020076 if (g_hlr->gs == NULL) {
77 LOGP(DLGSUP, LOGL_DEBUG,
78 "IMSI %s: NOT Notifying peers of subscriber data change,"
79 " there is no GSUP server\n",
80 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020081 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020082 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020083
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +010084 /* FIXME: send only to current vlr_number and sgsn_number */
85
Stefan Sperlingf1622522018-04-09 11:39:16 +020086 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020087 struct osmo_gsup_message gsup = { };
88 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
89 uint8_t apn[APN_MAXLEN];
90 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020091 uint8_t *peer;
92 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020093 size_t peer_strlen;
94 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020095 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020096
Neels Hofmeyre66e5252018-09-28 02:59:19 +020097 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020098 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020099 peer_compare = subscr->sgsn_number;
100 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200101 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200102 peer_compare = subscr->vlr_number;
103 } else {
104 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200105 continue;
106 }
107
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200108 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
109 if (peer_len < 0) {
110 LOGP(DLGSUP, LOGL_ERROR,
111 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
112 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
113 co && co->conn && co->conn->server? co->conn->server->port : 0);
114 continue;
115 }
116
117 peer_strlen = strnlen((const char*)peer, peer_len);
118 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
119 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
120 /* I hope peer is always nul terminated... */
121 if (peer_strlen < peer_len)
122 LOGP(DLGSUP, LOGL_DEBUG,
123 "IMSI %s: subscriber change: skipping %s peer %s\n",
124 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
125 osmo_quote_str((char*)peer, -1));
126 continue;
127 }
128
129 LOGP(DLGSUP, LOGL_DEBUG,
130 "IMSI %s: subscriber change: notifying %s peer %s\n",
131 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
132 osmo_quote_str(peer_compare, -1));
133
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200134 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
135 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200136 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200137 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200138 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200139 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
140 co && co->conn && co->conn->server? co->conn->server->port : 0);
141 continue;
142 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200143
Stefan Sperling93c5b102018-04-10 19:26:14 +0200144 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100145 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200146 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200147 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200148 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
149 "for %s:%u\n", subscr->imsi,
150 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
151 co && co->conn && co->conn->server? co->conn->server->port : 0);
152 continue;
153 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200154 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200155
Stefan Sperling93c5b102018-04-10 19:26:14 +0200156 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
157 LOGP(DMAIN, LOGL_ERROR,
158 "IMSI='%s': Cannot notify GSUP client; send operation failed "
159 "for %s:%u\n", subscr->imsi,
160 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
161 co && co->conn && co->conn->server? co->conn->server->port : 0);
162 continue;
163 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200164 }
165}
166
Oliver Smithc7f17872019-03-04 15:10:44 +0100167static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
168{
169 int i, j, rc;
170 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
171
172 OSMO_ASSERT(len <= sizeof(rand_buf));
173
174 /* Generate a random unique MSISDN (with retry) */
175 for (i = 0; i < 10; i++) {
176 /* Get a random number (with retry) */
177 for (j = 0; j < 10; j++) {
178 rc = osmo_get_rand_id(rand_buf, len);
179 if (!rc)
180 break;
181 }
182 if (rc) {
183 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
184 " failed (rc=%d)\n", imsi, rc);
185 return rc;
186 }
187
188 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
189 for (j = 0; j < len; j++)
190 msisdn[j] = 48 + (rand_buf[j] % 10);
191 msisdn[j] = '\0';
192
193 /* Ensure there is no subscriber with such MSISDN */
194 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
195 return 0;
196 }
197
198 /* Failure */
199 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
200 "the length for the automatically assigned MSISDNs "
201 "(see 'subscriber-create-on-demand' command)\n", imsi);
202 return -1;
203}
204
205static int subscr_create_on_demand(const char *imsi)
206{
207 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
208 int rc;
209 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
210
211 if (!g_hlr->subscr_create_on_demand)
212 return -1;
213 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
214 return -1;
215 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
216 return -1;
217
218 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
219 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
220 if (rc) {
221 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
222 return rc;
223 }
224
225 if (!rand_msisdn_len)
226 return 0;
227
228 /* Update MSISDN of the new (just allocated) subscriber */
229 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
230 if (rc) {
231 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
232 return rc;
233 }
234 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
235
236 return 0;
237}
238
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100239/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
240 * \param[in,out] hlr Global hlr context.
241 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
242 * \param[in] nam_val True to enable CS/PS, false to disable.
243 * \param[in] is_ps True to enable/disable PS, false for CS.
244 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
245 * value on error.
246 */
247int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
248{
249 int rc;
250 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
251 struct osmo_ipa_name vlr_name;
252 struct osmo_gsup_message gsup_del_data = {
253 .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST,
254 };
255 OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi);
256
257 if (is_val == nam_val) {
258 LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n",
259 subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
260 return ENOEXEC;
261 }
262
263 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
264 if (rc)
265 return rc > 0? -rc : rc;
266
267 /* If we're disabling, send a notice out to the GSUP client that is
268 * responsible. Otherwise no need. */
269 if (nam_val)
270 return 0;
271
272 if (subscr->vlr_number && osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number))
273 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
274 if (subscr->sgsn_number && osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number))
275 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
276 return 0;
277}
278
Harald Weltee687be52016-05-03 18:49:27 +0200279/***********************************************************************
280 * Send Auth Info handling
281 ***********************************************************************/
282
Harald Weltee72cf552016-04-28 07:18:49 +0200283/* process an incoming SAI request */
Neels Hofmeyrba0b5bd2019-12-12 04:04:53 +0100284static int rx_send_auth_info(struct osmo_gsup_req *req)
Harald Weltee72cf552016-04-28 07:18:49 +0200285{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100286 struct osmo_gsup_message gsup_out = {
287 .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT,
288 };
Harald Welte06f5af22019-08-21 20:01:31 +0200289 bool separation_bit = false;
Neels Hofmeyrba0b5bd2019-12-12 04:04:53 +0100290 unsigned int auc_3g_ind;
Harald Weltee72cf552016-04-28 07:18:49 +0200291 int rc;
292
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100293 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100294
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100295 if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS)
Harald Welte06f5af22019-08-21 20:01:31 +0200296 separation_bit = true;
297
Neels Hofmeyrba0b5bd2019-12-12 04:04:53 +0100298 rc = db_ind(g_hlr->dbc, &req->source_name, &auc_3g_ind);
299 if (rc) {
300 LOG_GSUP_REQ(req, LOGL_ERROR,
301 "Unable to determine 3G auth IND for source %s (rc=%d),"
302 " generating tuples with IND = 0\n",
303 osmo_gsup_peer_id_to_str(&req->source_name), rc);
304 auc_3g_ind = 0;
305 }
306
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100307 rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100308 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200309 ARRAY_SIZE(gsup_out.auth_vectors),
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100310 req->gsup.rand, req->gsup.auts, separation_bit);
311
Neels Hofmeyr671db902017-11-22 20:38:19 +0100312 if (rc <= 0) {
Neels Hofmeyr671db902017-11-22 20:38:19 +0100313 switch (rc) {
314 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100315 /* 0 means "0 tuples generated", which shouldn't happen.
316 * Treat the same as "no auth data". */
317 case -ENOKEY:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100318 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN,
319 "IMSI known, but has no auth data;"
320 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP");
321 return rc;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100322 case -ENOENT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100323 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
324 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100325 default:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100326 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db");
327 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100328 }
Harald Weltee72cf552016-04-28 07:18:49 +0200329 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100330 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200331
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100332 osmo_gsup_req_respond(req, &gsup_out, false, true);
333 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200334}
335
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100336/*! Receive Update Location Request, creates new lu_operation */
337static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req)
Harald Weltee687be52016-05-03 18:49:27 +0200338{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100339 switch (req->gsup.cn_domain) {
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200340 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200341 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200342 break;
343 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200344 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
345 * accordance with the GSUP spec in osmo-hlr's user manual (section
346 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
347 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200348 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200349 conn->supports_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200350 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200351 }
Harald Weltee687be52016-05-03 18:49:27 +0200352
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100353 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100354
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100355 lu_rx_gsup(req);
Harald Weltee687be52016-05-03 18:49:27 +0200356 return 0;
357}
358
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100359static int rx_purge_ms_req(struct osmo_gsup_req *req)
Harald Welteb18f0e02016-05-05 21:03:03 +0200360{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100361 bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS);
Harald Welteb18f0e02016-05-05 21:03:03 +0200362 int rc;
363
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100364 LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS");
Harald Welteb18f0e02016-05-05 21:03:03 +0200365
366 /* FIXME: check if the VLR that sends the purge is the same that
367 * we have on record. Only update if yes */
368
369 /* Perform the actual update of the DB */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100370 rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200371
Harald Welte3f2a9a22018-03-01 23:35:35 +0100372 if (rc == 0)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100373 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true);
374 else if (rc == -ENOENT)
375 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
376 else
377 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error");
378 return rc;
Harald Welteb18f0e02016-05-05 21:03:03 +0200379}
380
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100381static int rx_check_imei_req(struct osmo_gsup_req *req)
Harald Weltea1d3b042018-06-11 20:28:35 +0200382{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100383 struct osmo_gsup_message gsup_reply;
Oliver Smith103c11b2019-06-06 11:57:05 +0200384 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100385 const struct osmo_gsup_message *gsup = &req->gsup;
Oliver Smith103c11b2019-06-06 11:57:05 +0200386 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100387
Oliver Smith103c11b2019-06-06 11:57:05 +0200388 /* Require IMEI */
389 if (!gsup->imei_enc) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100390 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI");
Oliver Smith783ac812018-12-17 11:34:51 +0100391 return -1;
392 }
393
Oliver Smith103c11b2019-06-06 11:57:05 +0200394 /* Decode IMEI (fails if IMEI is too long) */
395 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
396 if (rc < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100397 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
398 "failed to decode IMEI %s (rc: %d)",
399 osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len),
400 rc);
Oliver Smith103c11b2019-06-06 11:57:05 +0200401 return -1;
402 }
403
404 /* Check if IMEI is too short */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100405 if (!osmo_imei_str_valid(imei, false)) {
406 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
407 "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1));
Oliver Smith783ac812018-12-17 11:34:51 +0100408 return -1;
409 }
410
Oliver Smithc7f17872019-03-04 15:10:44 +0100411 subscr_create_on_demand(gsup->imsi);
412
Oliver Smith851814a2019-01-11 15:30:21 +0100413 /* Save in DB if desired */
414 if (g_hlr->store_imei) {
415 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
416 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100417 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 +0100418 return -1;
419 }
420 } else {
421 /* Check if subscriber exists and print IMEI */
422 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
423 struct hlr_subscriber subscr;
424 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100425 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "IMSI unknown");
Oliver Smith851814a2019-01-11 15:30:21 +0100426 return -1;
427 }
428 }
Oliver Smith783ac812018-12-17 11:34:51 +0100429
430 /* Accept all IMEIs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100431 gsup_reply = (struct osmo_gsup_message){
432 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
433 .imei_result = OSMO_GSUP_IMEI_RESULT_ACK,
434 };
435 return osmo_gsup_req_respond(req, &gsup_reply, false, true);
Oliver Smith783ac812018-12-17 11:34:51 +0100436}
437
Oliver Smith28f0af82019-02-22 11:46:33 +0100438static char namebuf[255];
439#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
440 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100441 osmo_gsup_message_type_name((gsup)->message_type), \
442 osmo_gsup_message_class_name((gsup)->message_class), \
443 (gsup)->imsi, \
444 osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \
445 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \
Oliver Smith28f0af82019-02-22 11:46:33 +0100446 ## args)
447
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100448static int read_cb_forward(struct osmo_gsup_req *req)
Oliver Smith28f0af82019-02-22 11:46:33 +0100449{
450 int ret = -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100451 const struct osmo_gsup_message *gsup = &req->gsup;
452 struct osmo_gsup_message gsup_err;
453 struct msgb *forward_msg;
454 struct osmo_ipa_name destination_name;
Oliver Smith28f0af82019-02-22 11:46:33 +0100455
456 /* Check for routing IEs */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100457 if (!req->gsup.source_name[0] || !req->gsup.source_name_len
458 || !req->gsup.destination_name[0] || !req->gsup.destination_name_len) {
459 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n");
460 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100461 }
462
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100463 if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) {
464 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n");
465 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100466 }
467
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100468 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name));
Oliver Smith28f0af82019-02-22 11:46:33 +0100469
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100470 /* Forward message without re-encoding (so we don't remove unknown IEs).
471 * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */
472 forward_msg = osmo_gsup_msgb_alloc("GSUP forward");
473 forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg));
474 memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg));
475 ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg);
Oliver Smith28f0af82019-02-22 11:46:33 +0100476 if (ret) {
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100477 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n",
478 ret == -ENODEV ? "destination not connected" : "unknown error",
479 ret);
480 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100481 }
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100482 osmo_gsup_req_free(req);
483 return 0;
484
485routing_error:
486 gsup_err = (struct osmo_gsup_message){
487 .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR,
488 .source_name = gsup->destination_name,
489 .source_name_len = gsup->destination_name_len,
490 };
491 osmo_gsup_req_respond(req, &gsup_err, true, true);
492 return -1;
Oliver Smith28f0af82019-02-22 11:46:33 +0100493}
494
Harald Weltee72cf552016-04-28 07:18:49 +0200495static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
496{
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100497 struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg);
498 if (!req)
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700499 return -EINVAL;
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100500
501 /* If the GSUP recipient is other than this HLR, forward. */
502 if (req->gsup.destination_name_len) {
503 struct osmo_ipa_name destination_name;
504 struct osmo_ipa_name my_name;
505 osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno);
506 if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)
507 && osmo_ipa_name_cmp(&destination_name, &my_name)) {
508 return read_cb_forward(req);
509 }
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700510 }
511
Neels Hofmeyrfd08e4d2019-12-16 17:15:40 +0100512 /* SMS over GSUP */
513 if (sms_over_gsup_check_handle_msg(req))
514 return 0;
515
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100516 /* Distributed GSM: check whether to proxy for / lookup a remote HLR.
517 * It would require less database hits to do this only if a local-only operation fails with "unknown IMSI", but
518 * it becomes semantically easier if we do this once-off ahead of time. */
519 if (osmo_mslookup_client_active(g_hlr->mslookup.client.client)
520 || osmo_sockaddr_str_is_nonzero(&g_hlr->mslookup.client.gsup_gateway_proxy)) {
521 if (dgsm_check_forward_gsup_msg(req))
522 return 0;
523 }
524
Neels Hofmeyre78241a2019-12-06 17:09:56 +0100525 /* HLR related messages that are handled at this HLR instance */
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100526 switch (req->gsup.message_type) {
Harald Weltee72cf552016-04-28 07:18:49 +0200527 /* requests sent to us */
528 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Neels Hofmeyrba0b5bd2019-12-12 04:04:53 +0100529 rx_send_auth_info(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200530 break;
531 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100532 rx_upd_loc_req(conn, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200533 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200534 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100535 rx_purge_ms_req(req);
Harald Welteb18f0e02016-05-05 21:03:03 +0200536 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200537 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100538 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100539 LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n");
540 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100541 break;
542 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100543 LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n");
544 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100545 break;
Harald Weltebb779392018-06-16 20:21:10 +0200546 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
547 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100548 rx_proc_ss_req(req);
Harald Weltebb779392018-06-16 20:21:10 +0200549 break;
550 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100551 rx_proc_ss_error(req);
Harald Weltebb779392018-06-16 20:21:10 +0200552 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200553 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200554 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200555 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
556 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100557 lu_rx_gsup(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200558 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100559 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100560 rx_check_imei_req(req);
Oliver Smith783ac812018-12-17 11:34:51 +0100561 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200562 default:
Maxaa0fefd2017-02-16 12:25:22 +0100563 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100564 osmo_gsup_message_type_name(req->gsup.message_type));
565 osmo_gsup_req_free(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200566 break;
567 }
568 return 0;
569}
570
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100571static void print_usage()
572{
573 printf("Usage: osmo-hlr\n");
574}
575
576static void print_help()
577{
578 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100579 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100580 printf(" -l --database db-name The database to use.\n");
581 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
582 printf(" -D --daemonize Fork the process into a background daemon.\n");
583 printf(" -s --disable-color Do not print ANSI colors in the log\n");
584 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
585 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100586 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100587 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100588 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100589}
590
591static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100592 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100593 const char *db_file;
594 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100595 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100596 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100597} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100598 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200599 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100600 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100601 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100602};
603
604static void handle_options(int argc, char **argv)
605{
606 while (1) {
607 int option_index = 0, c;
608 static struct option long_options[] = {
609 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100610 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100611 {"database", 1, 0, 'l'},
612 {"debug", 1, 0, 'd'},
613 {"daemonize", 0, 0, 'D'},
614 {"disable-color", 0, 0, 's'},
615 {"log-level", 1, 0, 'e'},
616 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100617 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100618 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100619 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100620 {0, 0, 0, 0}
621 };
622
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100623 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100624 long_options, &option_index);
625 if (c == -1)
626 break;
627
628 switch (c) {
629 case 'h':
630 print_usage();
631 print_help();
632 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100633 case 'c':
634 cmdline_opts.config_file = optarg;
635 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100636 case 'l':
637 cmdline_opts.db_file = optarg;
638 break;
639 case 'd':
640 log_parse_category_mask(osmo_stderr_target, optarg);
641 break;
642 case 'D':
643 cmdline_opts.daemonize = 1;
644 break;
645 case 's':
646 log_set_use_color(osmo_stderr_target, 0);
647 break;
648 case 'e':
649 log_set_log_level(osmo_stderr_target, atoi(optarg));
650 break;
651 case 'T':
652 log_set_print_timestamp(osmo_stderr_target, 1);
653 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100654 case 'U':
655 cmdline_opts.db_upgrade = true;
656 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100657 case 'C':
658 cmdline_opts.db_check = true;
659 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100660 case 'V':
661 print_version(1);
662 exit(0);
663 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100664 default:
665 /* catch unknown options *as well as* missing arguments. */
666 fprintf(stderr, "Error in command line options. Exiting.\n");
667 exit(-1);
668 break;
669 }
670 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100671
672 if (argc > optind) {
673 fprintf(stderr, "Unsupported positional arguments on command line\n");
674 exit(2);
675 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100676}
677
Harald Welteaabae9e2016-04-28 12:48:14 +0200678static void signal_hdlr(int signal)
679{
680 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700681 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200682 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700683 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700684 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200685 break;
686 case SIGUSR1:
687 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100688 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200689 break;
690 }
691}
Harald Weltee72cf552016-04-28 07:18:49 +0200692
Max372868b2017-03-02 12:12:00 +0100693static const char vlr_copyright[] =
694 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
695 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
696 "This is free software: you are free to change and redistribute it.\r\n"
697 "There is NO WARRANTY, to the extent permitted by law.\r\n";
698
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100699static struct vty_app_info vty_info = {
700 .name = "OsmoHLR",
701 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100702 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100703 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200704 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100705};
706
Harald Weltee72cf552016-04-28 07:18:49 +0200707int main(int argc, char **argv)
708{
Harald Weltee72cf552016-04-28 07:18:49 +0200709 int rc;
710
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700711 /* Track the use of talloc NULL memory contexts */
712 talloc_enable_null_tracking();
713
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100714 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
715 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100716 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200717
Maxd4bebbd2017-03-02 12:00:19 +0100718 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200719 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200720 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200721 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200722 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Neels Hofmeyr6aa871d2019-11-20 03:35:37 +0100723 INIT_LLIST_HEAD(&g_hlr->mslookup.server.local_site_services);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200724 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +0100725 g_hlr->mslookup.server.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100726 g_hlr->mslookup.client.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Maxd4bebbd2017-03-02 12:00:19 +0100727
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700728 /* Init default (call independent) SS session guard timeout value */
729 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
730
Neels Hofmeyrfd08e4d2019-12-16 17:15:40 +0100731 g_hlr->sms_over_gsup.try_direct_delivery = true;
732
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200733 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200734 if (rc < 0) {
735 fprintf(stderr, "Error initializing logging\n");
736 exit(1);
737 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100738
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100739 /* Set up llists and objects, startup is happening from VTY commands. */
740 dgsm_init(hlr_ctx);
741
Max20ddfdb2019-02-18 13:12:27 +0100742 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100743 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100744 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100745 handle_options(argc, argv);
Pau Espin Pedrole49391b2019-08-05 15:57:10 +0200746 hlr_vty_init();
Neels Hofmeyr3ad481a2019-11-20 03:35:37 +0100747 dgsm_vty_init();
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100748
749 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
750 if (rc < 0) {
751 LOGP(DMAIN, LOGL_FATAL,
752 "Failed to parse the config file: '%s'\n",
753 cmdline_opts.config_file);
754 return rc;
755 }
756
Harald Weltee72cf552016-04-28 07:18:49 +0200757 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
758
759 rc = rand_init();
760 if (rc < 0) {
761 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
762 exit(1);
763 }
764
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200765 if (cmdline_opts.db_file)
766 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
767
768 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100769 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200770 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 +0200771 exit(1);
772 }
773
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100774 if (cmdline_opts.db_check) {
775 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
776 db_close(g_hlr->dbc);
777 log_fini();
778 talloc_free(hlr_ctx);
779 talloc_free(tall_vty_ctx);
780 talloc_disable_null_tracking();
781 exit(0);
782 }
783
784 /* start telnet after reading config for vty_get_bind_addr() */
785 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
786 OSMO_VTY_PORT_HLR);
787 if (rc < 0)
788 return rc;
789
790
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200791 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100792 read_cb, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100793 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200794 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
795 exit(1);
796 }
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100797 proxy_init(g_hlr->gs);
Harald Weltee72cf552016-04-28 07:18:49 +0200798
Max372868b2017-03-02 12:12:00 +0100799 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200800 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100801
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100802 dgsm_start(hlr_ctx);
803
Harald Welteaabae9e2016-04-28 12:48:14 +0200804 osmo_init_ignore_signals();
805 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700806 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200807 signal(SIGUSR1, &signal_hdlr);
808
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100809 if (cmdline_opts.daemonize) {
810 rc = osmo_daemonize();
811 if (rc < 0) {
812 perror("Error during daemonize");
813 exit(1);
814 }
815 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200816
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700817 while (!quit)
Neels Hofmeyrf13a8bc2019-11-20 02:36:45 +0100818 osmo_select_main_ctx(0);
819
Neels Hofmeyr62d916f2019-11-20 03:35:37 +0100820 dgsm_stop();
Harald Weltee72cf552016-04-28 07:18:49 +0200821
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700822 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100823 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200824 log_fini();
825
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700826 /**
827 * Report the heap state of root context, then free,
828 * so both ASAN and Valgrind are happy...
829 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700830 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700831 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700832
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700833 /* FIXME: VTY code still uses NULL-context */
834 talloc_free(tall_vty_ctx);
835
836 /**
837 * Report the heap state of NULL context, then free,
838 * so both ASAN and Valgrind are happy...
839 */
840 talloc_report_full(NULL, stderr);
841 talloc_disable_null_tracking();
842
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700843 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200844}