blob: 043189c6152011163083721362c7d2c8d03a5968 [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>
Pau Espin Pedroled18fa92020-08-18 12:58:36 +020034#include <osmocom/vty/cpu_sched_vty.h>
Max372868b2017-03-02 12:12:00 +010035#include <osmocom/ctrl/control_vty.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020036#include <osmocom/gsm/apn.h>
Oliver Smith783ac812018-12-17 11:34:51 +010037#include <osmocom/gsm/gsm48_ie.h>
Oliver Smithc7f17872019-03-04 15:10:44 +010038#include <osmocom/gsm/gsm_utils.h>
Neels Hofmeyrad868e22019-11-20 02:36:45 +010039#include <osmocom/gsm/gsm23003.h>
Neels Hofmeyr76328bd2019-11-20 03:35:37 +010040#include <osmocom/mslookup/mslookup_client.h>
Harald Weltee72cf552016-04-28 07:18:49 +020041
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010042#include <osmocom/gsupclient/cni_peer_id.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010043#include <osmocom/hlr/db.h>
44#include <osmocom/hlr/hlr.h>
45#include <osmocom/hlr/ctrl.h>
46#include <osmocom/hlr/logging.h>
47#include <osmocom/hlr/gsup_server.h>
48#include <osmocom/hlr/gsup_router.h>
49#include <osmocom/hlr/rand.h>
Neels Hofmeyr2f758032019-11-20 00:37:07 +010050#include <osmocom/hlr/hlr_vty.h>
51#include <osmocom/hlr/hlr_ussd.h>
Mychaela N. Falconia46d354b2023-08-25 21:15:23 +000052#include <osmocom/hlr/hlr_sms.h>
Neels Hofmeyr76328bd2019-11-20 03:35:37 +010053#include <osmocom/hlr/dgsm.h>
54#include <osmocom/hlr/proxy.h>
Neels Hofmeyrad868e22019-11-20 02:36:45 +010055#include <osmocom/hlr/lu_fsm.h>
Neels Hofmeyr407925d2019-11-20 03:35:37 +010056#include <osmocom/mslookup/mdns.h>
Harald Weltee72cf552016-04-28 07:18:49 +020057
Harald Welted5807b82018-07-29 12:27:41 +020058struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010059static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070060static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020061
Neels Hofmeyrad868e22019-11-20 02:36:45 +010062struct osmo_tdef g_hlr_tdefs[] = {
63 /* 4222 is also the OSMO_GSUP_PORT */
64 { .T = -4222, .default_val = 30, .desc = "GSUP Update Location timeout" },
65 {}
66};
67
Stefan Sperlingf1622522018-04-09 11:39:16 +020068/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
69 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020070 * \param[in] subscr A subscriber we have new data to send for.
71 */
72void
73osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
74{
75 struct osmo_gsup_conn *co;
76
Neels Hofmeyre66e5252018-09-28 02:59:19 +020077 if (g_hlr->gs == NULL) {
78 LOGP(DLGSUP, LOGL_DEBUG,
79 "IMSI %s: NOT Notifying peers of subscriber data change,"
80 " there is no GSUP server\n",
81 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020082 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020083 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020084
Neels Hofmeyrad868e22019-11-20 02:36:45 +010085 /* FIXME: send only to current vlr_number and sgsn_number */
86
Stefan Sperlingf1622522018-04-09 11:39:16 +020087 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020088 struct osmo_gsup_message gsup = { };
89 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
90 uint8_t apn[APN_MAXLEN];
91 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020092 uint8_t *peer;
93 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020094 size_t peer_strlen;
95 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020096 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020097
Neels Hofmeyre66e5252018-09-28 02:59:19 +020098 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020099 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200100 peer_compare = subscr->sgsn_number;
101 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200102 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200103 peer_compare = subscr->vlr_number;
104 } else {
105 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200106 continue;
107 }
108
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200109 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
110 if (peer_len < 0) {
111 LOGP(DLGSUP, LOGL_ERROR,
112 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
113 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
114 co && co->conn && co->conn->server? co->conn->server->port : 0);
115 continue;
116 }
117
118 peer_strlen = strnlen((const char*)peer, peer_len);
119 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
120 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
121 /* I hope peer is always nul terminated... */
122 if (peer_strlen < peer_len)
123 LOGP(DLGSUP, LOGL_DEBUG,
124 "IMSI %s: subscriber change: skipping %s peer %s\n",
125 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
126 osmo_quote_str((char*)peer, -1));
127 continue;
128 }
129
130 LOGP(DLGSUP, LOGL_DEBUG,
131 "IMSI %s: subscriber change: notifying %s peer %s\n",
132 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
133 osmo_quote_str(peer_compare, -1));
134
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200135 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
136 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200137 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200138 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200139 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200140 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
141 co && co->conn && co->conn->server? co->conn->server->port : 0);
142 continue;
143 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200144
Stefan Sperling93c5b102018-04-10 19:26:14 +0200145 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100146 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200147 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200148 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200149 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
150 "for %s:%u\n", subscr->imsi,
151 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
152 co && co->conn && co->conn->server? co->conn->server->port : 0);
153 continue;
154 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200155 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200156
Stefan Sperling93c5b102018-04-10 19:26:14 +0200157 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
158 LOGP(DMAIN, LOGL_ERROR,
159 "IMSI='%s': Cannot notify GSUP client; send operation failed "
160 "for %s:%u\n", subscr->imsi,
161 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
162 co && co->conn && co->conn->server? co->conn->server->port : 0);
163 continue;
164 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200165 }
166}
167
Oliver Smithc7f17872019-03-04 15:10:44 +0100168static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
169{
170 int i, j, rc;
171 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
172
173 OSMO_ASSERT(len <= sizeof(rand_buf));
174
175 /* Generate a random unique MSISDN (with retry) */
176 for (i = 0; i < 10; i++) {
177 /* Get a random number (with retry) */
178 for (j = 0; j < 10; j++) {
179 rc = osmo_get_rand_id(rand_buf, len);
180 if (!rc)
181 break;
182 }
183 if (rc) {
184 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
185 " failed (rc=%d)\n", imsi, rc);
186 return rc;
187 }
188
189 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
190 for (j = 0; j < len; j++)
191 msisdn[j] = 48 + (rand_buf[j] % 10);
192 msisdn[j] = '\0';
193
194 /* Ensure there is no subscriber with such MSISDN */
195 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
196 return 0;
197 }
198
199 /* Failure */
200 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
201 "the length for the automatically assigned MSISDNs "
202 "(see 'subscriber-create-on-demand' command)\n", imsi);
203 return -1;
204}
205
206static int subscr_create_on_demand(const char *imsi)
207{
208 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
209 int rc;
210 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
211
212 if (!g_hlr->subscr_create_on_demand)
213 return -1;
214 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
215 return -1;
216 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
217 return -1;
218
219 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
220 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
221 if (rc) {
222 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
223 return rc;
224 }
225
226 if (!rand_msisdn_len)
227 return 0;
228
229 /* Update MSISDN of the new (just allocated) subscriber */
230 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
231 if (rc) {
232 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
233 return rc;
234 }
235 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
236
237 return 0;
238}
239
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100240/*! Update nam_cs/nam_ps in the db and trigger notifications to GSUP clients.
241 * \param[in,out] hlr Global hlr context.
242 * \param[in] subscr Subscriber from a fresh db_subscr_get_by_*() call.
243 * \param[in] nam_val True to enable CS/PS, false to disable.
244 * \param[in] is_ps True to enable/disable PS, false for CS.
245 * \returns 0 on success, ENOEXEC if there is no need to change, a negative
246 * value on error.
247 */
248int hlr_subscr_nam(struct hlr *hlr, struct hlr_subscriber *subscr, bool nam_val, bool is_ps)
249{
250 int rc;
251 bool is_val = is_ps? subscr->nam_ps : subscr->nam_cs;
252 struct osmo_ipa_name vlr_name;
253 struct osmo_gsup_message gsup_del_data = {
254 .message_type = OSMO_GSUP_MSGT_DELETE_DATA_REQUEST,
255 };
256 OSMO_STRLCPY_ARRAY(gsup_del_data.imsi, subscr->imsi);
257
258 if (is_val == nam_val) {
259 LOGP(DAUC, LOGL_DEBUG, "IMSI-%s: Already has the requested value when asked to %s %s\n",
260 subscr->imsi, nam_val ? "enable" : "disable", is_ps ? "PS" : "CS");
261 return ENOEXEC;
262 }
263
264 rc = db_subscr_nam(hlr->dbc, subscr->imsi, nam_val, is_ps);
265 if (rc)
266 return rc > 0? -rc : rc;
267
268 /* If we're disabling, send a notice out to the GSUP client that is
269 * responsible. Otherwise no need. */
270 if (nam_val)
271 return 0;
272
Neels Hofmeyr9b8e7b42020-05-04 19:20:23 +0200273 if (subscr->vlr_number[0] && !osmo_ipa_name_set_str(&vlr_name, subscr->vlr_number))
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100274 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
Neels Hofmeyr9b8e7b42020-05-04 19:20:23 +0200275 if (subscr->sgsn_number[0] && !osmo_ipa_name_set_str(&vlr_name, subscr->sgsn_number))
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100276 osmo_gsup_enc_send_to_ipa_name(g_hlr->gs, &vlr_name, &gsup_del_data);
277 return 0;
278}
279
Harald Weltee687be52016-05-03 18:49:27 +0200280/***********************************************************************
281 * Send Auth Info handling
282 ***********************************************************************/
283
Harald Weltee72cf552016-04-28 07:18:49 +0200284/* process an incoming SAI request */
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100285static int rx_send_auth_info(struct osmo_gsup_req *req)
Harald Weltee72cf552016-04-28 07:18:49 +0200286{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100287 struct osmo_gsup_message gsup_out = {
288 .message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT,
289 };
Harald Welte06f5af22019-08-21 20:01:31 +0200290 bool separation_bit = false;
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100291 int num_auth_vectors = OSMO_GSUP_MAX_NUM_AUTH_INFO;
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100292 unsigned int auc_3g_ind;
Harald Weltee72cf552016-04-28 07:18:49 +0200293 int rc;
294
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100295 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100296
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100297 if (req->gsup.current_rat_type == OSMO_RAT_EUTRAN_SGS)
Harald Welte06f5af22019-08-21 20:01:31 +0200298 separation_bit = true;
299
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100300 if (req->gsup.num_auth_vectors > 0 &&
301 req->gsup.num_auth_vectors <= OSMO_GSUP_MAX_NUM_AUTH_INFO)
302 num_auth_vectors = req->gsup.num_auth_vectors;
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100303 rc = db_ind(g_hlr->dbc, &req->source_name, &auc_3g_ind);
304 if (rc) {
305 LOG_GSUP_REQ(req, LOGL_ERROR,
306 "Unable to determine 3G auth IND for source %s (rc=%d),"
307 " generating tuples with IND = 0\n",
308 osmo_cni_peer_id_to_str(&req->source_name), rc);
309 auc_3g_ind = 0;
310 }
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100311
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100312 rc = db_get_auc(g_hlr->dbc, req->gsup.imsi, auc_3g_ind,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100313 gsup_out.auth_vectors,
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100314 num_auth_vectors,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100315 req->gsup.rand, req->gsup.auts, separation_bit);
316
Neels Hofmeyr671db902017-11-22 20:38:19 +0100317 if (rc <= 0) {
Neels Hofmeyr671db902017-11-22 20:38:19 +0100318 switch (rc) {
319 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100320 /* 0 means "0 tuples generated", which shouldn't happen.
321 * Treat the same as "no auth data". */
322 case -ENOKEY:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100323 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN,
324 "IMSI known, but has no auth data;"
325 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP");
326 return rc;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100327 case -ENOENT:
Alexander Couzens268a33e2020-01-12 00:48:07 +0100328 osmo_gsup_req_respond_err(req, g_hlr->reject_cause, "IMSI unknown");
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100329 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100330 default:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100331 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "failure to look up IMSI in db");
332 return rc;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100333 }
Harald Weltee72cf552016-04-28 07:18:49 +0200334 }
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100335 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200336
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100337 osmo_gsup_req_respond(req, &gsup_out, false, true);
338 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200339}
340
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100341/*! Receive Update Location Request, creates new lu_operation */
342static int rx_upd_loc_req(struct osmo_gsup_conn *conn, struct osmo_gsup_req *req)
Harald Weltee687be52016-05-03 18:49:27 +0200343{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100344 switch (req->gsup.cn_domain) {
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200345 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200346 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200347 break;
348 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200349 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
350 * accordance with the GSUP spec in osmo-hlr's user manual (section
351 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
352 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200353 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200354 conn->supports_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200355 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200356 }
Harald Weltee687be52016-05-03 18:49:27 +0200357
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100358 subscr_create_on_demand(req->gsup.imsi);
Oliver Smithc7f17872019-03-04 15:10:44 +0100359
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100360 lu_rx_gsup(req);
Harald Weltee687be52016-05-03 18:49:27 +0200361 return 0;
362}
363
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100364static int rx_purge_ms_req(struct osmo_gsup_req *req)
Harald Welteb18f0e02016-05-05 21:03:03 +0200365{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100366 bool is_ps = (req->gsup.cn_domain != OSMO_GSUP_CN_DOMAIN_CS);
Harald Welteb18f0e02016-05-05 21:03:03 +0200367 int rc;
368
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100369 LOG_GSUP_REQ_CAT(req, DAUC, LOGL_INFO, "Purge MS (%s)\n", is_ps ? "PS" : "CS");
Harald Welteb18f0e02016-05-05 21:03:03 +0200370
371 /* FIXME: check if the VLR that sends the purge is the same that
372 * we have on record. Only update if yes */
373
374 /* Perform the actual update of the DB */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100375 rc = db_subscr_purge(g_hlr->dbc, req->gsup.imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200376
Harald Welte3f2a9a22018-03-01 23:35:35 +0100377 if (rc == 0)
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100378 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_PURGE_MS_RESULT, true);
379 else if (rc == -ENOENT)
380 osmo_gsup_req_respond_err(req, GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown");
381 else
382 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "db error");
383 return rc;
Harald Welteb18f0e02016-05-05 21:03:03 +0200384}
385
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100386static int rx_check_imei_req(struct osmo_gsup_req *req)
Harald Weltea1d3b042018-06-11 20:28:35 +0200387{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100388 struct osmo_gsup_message gsup_reply;
Oliver Smith103c11b2019-06-06 11:57:05 +0200389 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100390 const struct osmo_gsup_message *gsup = &req->gsup;
Oliver Smith103c11b2019-06-06 11:57:05 +0200391 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100392
Oliver Smith103c11b2019-06-06 11:57:05 +0200393 /* Require IMEI */
394 if (!gsup->imei_enc) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100395 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO, "missing IMEI");
Oliver Smith783ac812018-12-17 11:34:51 +0100396 return -1;
397 }
398
Oliver Smith103c11b2019-06-06 11:57:05 +0200399 /* Decode IMEI (fails if IMEI is too long) */
400 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
401 if (rc < 0) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100402 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
403 "failed to decode IMEI %s (rc: %d)",
404 osmo_hexdump_c(OTC_SELECT, gsup->imei_enc, gsup->imei_enc_len),
405 rc);
Oliver Smith103c11b2019-06-06 11:57:05 +0200406 return -1;
407 }
408
409 /* Check if IMEI is too short */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100410 if (!osmo_imei_str_valid(imei, false)) {
411 osmo_gsup_req_respond_err(req, GMM_CAUSE_INV_MAND_INFO,
412 "invalid IMEI: %s", osmo_quote_str_c(OTC_SELECT, imei, -1));
Oliver Smith783ac812018-12-17 11:34:51 +0100413 return -1;
414 }
415
Oliver Smithc7f17872019-03-04 15:10:44 +0100416 subscr_create_on_demand(gsup->imsi);
417
Oliver Smith851814a2019-01-11 15:30:21 +0100418 /* Save in DB if desired */
419 if (g_hlr->store_imei) {
420 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
421 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100422 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 +0100423 return -1;
424 }
425 } else {
Mychaela N. Falconiaeeac7282023-06-26 06:48:03 +0000426 /* ThemWi change: report IMSI-IMEI correspondence in syslog */
427 LOGP(DMAIN, LOGL_NOTICE, "IMEI REPORT: IMSI=%s has IMEI=%s\n",
428 gsup->imsi, imei);
Oliver Smith851814a2019-01-11 15:30:21 +0100429 }
Oliver Smith783ac812018-12-17 11:34:51 +0100430
431 /* Accept all IMEIs */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100432 gsup_reply = (struct osmo_gsup_message){
433 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
434 .imei_result = OSMO_GSUP_IMEI_RESULT_ACK,
435 };
436 return osmo_gsup_req_respond(req, &gsup_reply, false, true);
Oliver Smith783ac812018-12-17 11:34:51 +0100437}
438
Oliver Smith28f0af82019-02-22 11:46:33 +0100439static char namebuf[255];
440#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
441 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
Neels Hofmeyrfbe49292020-01-09 18:35:03 +0100442 osmo_gsup_message_type_name((gsup)->message_type), \
443 osmo_gsup_message_class_name((gsup)->message_class), \
444 (gsup)->imsi, \
445 osmo_quote_str((const char *)(gsup)->source_name, (gsup)->source_name_len), \
446 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)(gsup)->destination_name, (gsup)->destination_name_len), \
Oliver Smith28f0af82019-02-22 11:46:33 +0100447 ## args)
448
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100449static int read_cb_forward(struct osmo_gsup_req *req)
Oliver Smith28f0af82019-02-22 11:46:33 +0100450{
451 int ret = -EINVAL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100452 const struct osmo_gsup_message *gsup = &req->gsup;
453 struct osmo_gsup_message gsup_err;
454 struct msgb *forward_msg;
455 struct osmo_ipa_name destination_name;
Oliver Smith28f0af82019-02-22 11:46:33 +0100456
457 /* Check for routing IEs */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100458 if (!req->gsup.source_name || !req->gsup.source_name_len
459 || !req->gsup.destination_name || !req->gsup.destination_name_len) {
460 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "missing routing IEs\n");
461 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100462 }
463
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100464 if (osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)) {
465 LOGP_GSUP_FWD(&req->gsup, LOGL_ERROR, "invalid destination name\n");
466 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100467 }
468
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100469 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_ipa_name_to_str(&destination_name));
Oliver Smith28f0af82019-02-22 11:46:33 +0100470
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100471 /* Forward message without re-encoding (so we don't remove unknown IEs).
472 * Copy GSUP part to forward, removing incoming IPA header to be able to prepend an outgoing IPA header */
473 forward_msg = osmo_gsup_msgb_alloc("GSUP forward");
474 forward_msg->l2h = msgb_put(forward_msg, msgb_l2len(req->msg));
475 memcpy(forward_msg->l2h, msgb_l2(req->msg), msgb_l2len(req->msg));
476 ret = osmo_gsup_send_to_ipa_name(g_hlr->gs, &destination_name, forward_msg);
Oliver Smith28f0af82019-02-22 11:46:33 +0100477 if (ret) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100478 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "%s (rc=%d)\n",
479 ret == -ENODEV ? "destination not connected" : "unknown error",
480 ret);
481 goto routing_error;
Oliver Smith28f0af82019-02-22 11:46:33 +0100482 }
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100483 osmo_gsup_req_free(req);
484 return 0;
485
486routing_error:
487 gsup_err = (struct osmo_gsup_message){
488 .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR,
489 .source_name = gsup->destination_name,
490 .source_name_len = gsup->destination_name_len,
491 };
492 osmo_gsup_req_respond(req, &gsup_err, true, true);
493 return -1;
Oliver Smith28f0af82019-02-22 11:46:33 +0100494}
495
Harald Weltee72cf552016-04-28 07:18:49 +0200496static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
497{
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100498 struct osmo_gsup_req *req = osmo_gsup_conn_rx(conn, msg);
499 if (!req)
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700500 return -EINVAL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100501
502 /* If the GSUP recipient is other than this HLR, forward. */
503 if (req->gsup.destination_name_len) {
504 struct osmo_ipa_name destination_name;
505 struct osmo_ipa_name my_name;
506 osmo_ipa_name_set_str(&my_name, g_hlr->gsup_unit_name.serno);
507 if (!osmo_ipa_name_set(&destination_name, req->gsup.destination_name, req->gsup.destination_name_len)
508 && osmo_ipa_name_cmp(&destination_name, &my_name)) {
509 return read_cb_forward(req);
510 }
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700511 }
512
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100513 /* Distributed GSM: check whether to proxy for / lookup a remote HLR.
514 * It would require less database hits to do this only if a local-only operation fails with "unknown IMSI", but
515 * it becomes semantically easier if we do this once-off ahead of time. */
516 if (osmo_mslookup_client_active(g_hlr->mslookup.client.client)
517 || osmo_sockaddr_str_is_nonzero(&g_hlr->mslookup.client.gsup_gateway_proxy)) {
518 if (dgsm_check_forward_gsup_msg(req))
519 return 0;
520 }
521
522 /* HLR related messages that are handled at this HLR instance */
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100523 switch (req->gsup.message_type) {
Harald Weltee72cf552016-04-28 07:18:49 +0200524 /* requests sent to us */
525 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100526 rx_send_auth_info(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200527 break;
528 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100529 rx_upd_loc_req(conn, req);
Harald Weltee72cf552016-04-28 07:18:49 +0200530 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200531 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100532 rx_purge_ms_req(req);
Harald Welteb18f0e02016-05-05 21:03:03 +0200533 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200534 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100535 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100536 LOG_GSUP_REQ(req, LOGL_ERROR, "Peer responds with: Error while deleting subscriber data\n");
537 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100538 break;
539 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100540 LOG_GSUP_REQ(req, LOGL_DEBUG, "Peer responds with: Subscriber data deleted\n");
541 osmo_gsup_req_free(req);
Max9cacb6f2017-02-20 17:22:56 +0100542 break;
Harald Weltebb779392018-06-16 20:21:10 +0200543 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
544 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100545 rx_proc_ss_req(req);
Harald Weltebb779392018-06-16 20:21:10 +0200546 break;
547 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100548 rx_proc_ss_error(req);
Harald Weltebb779392018-06-16 20:21:10 +0200549 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200550 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200551 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200552 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
553 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100554 lu_rx_gsup(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200555 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100556 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100557 rx_check_imei_req(req);
Oliver Smith783ac812018-12-17 11:34:51 +0100558 break;
Mychaela N. Falconia46d354b2023-08-25 21:15:23 +0000559 case OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST:
560 forward_mo_sms(req);
561 break;
Mychaela N. Falconia4f2cde32023-08-26 01:06:57 +0000562 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
563 forward_mt_sms(req);
564 break;
Mychaela N. Falconia5b5a9ff2023-08-28 18:46:41 +0000565 case OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST:
566 rx_ready_for_sm_req(req);
567 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200568 default:
Maxaa0fefd2017-02-16 12:25:22 +0100569 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100570 osmo_gsup_message_type_name(req->gsup.message_type));
571 osmo_gsup_req_free(req);
Harald Weltee72cf552016-04-28 07:18:49 +0200572 break;
573 }
574 return 0;
575}
576
Harald Welte7a476532022-11-03 11:38:41 +0100577static void print_usage(void)
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100578{
579 printf("Usage: osmo-hlr\n");
580}
581
Harald Welte7a476532022-11-03 11:38:41 +0100582static void print_help(void)
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100583{
584 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100585 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100586 printf(" -l --database db-name The database to use.\n");
587 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
588 printf(" -D --daemonize Fork the process into a background daemon.\n");
589 printf(" -s --disable-color Do not print ANSI colors in the log\n");
590 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
591 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100592 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100593 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100594 printf(" -V --version Print the version of OsmoHLR.\n");
Harald Welte2bd1a452021-02-23 17:23:30 +0100595
596 printf("\nVTY reference generation:\n");
597 printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n");
598 printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100599}
600
601static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100602 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100603 const char *db_file;
604 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100605 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100606 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100607} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100608 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200609 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100610 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100611 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100612};
613
Harald Welte2bd1a452021-02-23 17:23:30 +0100614static void handle_long_options(const char *prog_name, const int long_option)
615{
616 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
617
618 switch (long_option) {
619 case 1:
620 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
621 if (vty_ref_mode < 0) {
622 fprintf(stderr, "%s: Unknown VTY reference generation "
623 "mode '%s'\n", prog_name, optarg);
624 exit(2);
625 }
626 break;
627 case 2:
628 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
629 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
630 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
631 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
632 exit(0);
633 default:
634 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
635 exit(2);
636 }
637
638}
639
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100640static void handle_options(int argc, char **argv)
641{
642 while (1) {
643 int option_index = 0, c;
Harald Welte2bd1a452021-02-23 17:23:30 +0100644 static int long_option = 0;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100645 static struct option long_options[] = {
646 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100647 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100648 {"database", 1, 0, 'l'},
649 {"debug", 1, 0, 'd'},
650 {"daemonize", 0, 0, 'D'},
651 {"disable-color", 0, 0, 's'},
652 {"log-level", 1, 0, 'e'},
653 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100654 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100655 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100656 {"version", 0, 0, 'V' },
Harald Welte2bd1a452021-02-23 17:23:30 +0100657 {"vty-ref-mode", 1, &long_option, 1},
658 {"vty-ref-xml", 0, &long_option, 2},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100659 {0, 0, 0, 0}
660 };
661
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100662 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100663 long_options, &option_index);
664 if (c == -1)
665 break;
666
667 switch (c) {
Harald Welte2bd1a452021-02-23 17:23:30 +0100668 case 0:
669 handle_long_options(argv[0], long_option);
670 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100671 case 'h':
672 print_usage();
673 print_help();
674 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100675 case 'c':
676 cmdline_opts.config_file = optarg;
677 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100678 case 'l':
679 cmdline_opts.db_file = optarg;
680 break;
681 case 'd':
682 log_parse_category_mask(osmo_stderr_target, optarg);
683 break;
684 case 'D':
685 cmdline_opts.daemonize = 1;
686 break;
687 case 's':
688 log_set_use_color(osmo_stderr_target, 0);
689 break;
690 case 'e':
691 log_set_log_level(osmo_stderr_target, atoi(optarg));
692 break;
693 case 'T':
694 log_set_print_timestamp(osmo_stderr_target, 1);
695 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100696 case 'U':
697 cmdline_opts.db_upgrade = true;
698 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100699 case 'C':
700 cmdline_opts.db_check = true;
701 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100702 case 'V':
703 print_version(1);
704 exit(0);
705 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100706 default:
707 /* catch unknown options *as well as* missing arguments. */
708 fprintf(stderr, "Error in command line options. Exiting.\n");
709 exit(-1);
710 break;
711 }
712 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100713
714 if (argc > optind) {
715 fprintf(stderr, "Unsupported positional arguments on command line\n");
716 exit(2);
717 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100718}
719
Harald Welteaabae9e2016-04-28 12:48:14 +0200720static void signal_hdlr(int signal)
721{
722 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700723 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200724 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700725 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700726 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200727 break;
728 case SIGUSR1:
729 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100730 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200731 break;
732 }
733}
Harald Weltee72cf552016-04-28 07:18:49 +0200734
Max372868b2017-03-02 12:12:00 +0100735static const char vlr_copyright[] =
736 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
737 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
738 "This is free software: you are free to change and redistribute it.\r\n"
739 "There is NO WARRANTY, to the extent permitted by law.\r\n";
740
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100741static struct vty_app_info vty_info = {
742 .name = "OsmoHLR",
743 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100744 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100745 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200746 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100747};
748
Harald Weltee72cf552016-04-28 07:18:49 +0200749int main(int argc, char **argv)
750{
Harald Weltee72cf552016-04-28 07:18:49 +0200751 int rc;
752
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700753 /* Track the use of talloc NULL memory contexts */
754 talloc_enable_null_tracking();
755
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100756 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
757 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100758 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200759
Maxd4bebbd2017-03-02 12:00:19 +0100760 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200761 INIT_LLIST_HEAD(&g_hlr->euse_list);
Mychaela N. Falconia9f5f5dc2023-08-25 18:33:18 +0000762 INIT_LLIST_HEAD(&g_hlr->smsc_list);
Harald Weltebb779392018-06-16 20:21:10 +0200763 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200764 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Mychaela N. Falconia9f5f5dc2023-08-25 18:33:18 +0000765 INIT_LLIST_HEAD(&g_hlr->smsc_routes);
Neels Hofmeyrab7dc402019-11-20 03:35:37 +0100766 INIT_LLIST_HEAD(&g_hlr->mslookup.server.local_site_services);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200767 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Neels Hofmeyr407925d2019-11-20 03:35:37 +0100768 g_hlr->mslookup.server.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100769 g_hlr->mslookup.client.mdns.domain_suffix = talloc_strdup(g_hlr, OSMO_MDNS_DOMAIN_SUFFIX_DEFAULT);
Alexander Couzens268a33e2020-01-12 00:48:07 +0100770 g_hlr->reject_cause = GMM_CAUSE_IMSI_UNKNOWN;
771 g_hlr->no_proxy_reject_cause = GMM_CAUSE_IMSI_UNKNOWN;
Maxd4bebbd2017-03-02 12:00:19 +0100772
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700773 /* Init default (call independent) SS session guard timeout value */
774 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
775
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200776 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200777 if (rc < 0) {
778 fprintf(stderr, "Error initializing logging\n");
779 exit(1);
780 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100781
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100782 /* Set up llists and objects, startup is happening from VTY commands. */
783 dgsm_init(hlr_ctx);
784
Max20ddfdb2019-02-18 13:12:27 +0100785 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100786 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100787 ctrl_vty_init(hlr_ctx);
Alexander Couzens268a33e2020-01-12 00:48:07 +0100788 hlr_vty_init(hlr_ctx);
Neels Hofmeyr407925d2019-11-20 03:35:37 +0100789 dgsm_vty_init();
Pau Espin Pedroled18fa92020-08-18 12:58:36 +0200790 osmo_cpu_sched_vty_init(hlr_ctx);
Harald Welte2bd1a452021-02-23 17:23:30 +0100791 handle_options(argc, argv);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100792
793 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
794 if (rc < 0) {
795 LOGP(DMAIN, LOGL_FATAL,
796 "Failed to parse the config file: '%s'\n",
797 cmdline_opts.config_file);
798 return rc;
799 }
800
Harald Weltee72cf552016-04-28 07:18:49 +0200801 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
802
803 rc = rand_init();
804 if (rc < 0) {
805 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
806 exit(1);
807 }
808
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200809 if (cmdline_opts.db_file)
810 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
811
812 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100813 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200814 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 +0200815 exit(1);
816 }
817
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100818 if (cmdline_opts.db_check) {
819 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
820 db_close(g_hlr->dbc);
821 log_fini();
822 talloc_free(hlr_ctx);
823 talloc_free(tall_vty_ctx);
824 talloc_disable_null_tracking();
825 exit(0);
826 }
827
828 /* start telnet after reading config for vty_get_bind_addr() */
arehbeine4143232022-12-19 21:28:53 +0100829 rc = telnet_init_default(hlr_ctx, NULL, OSMO_VTY_PORT_HLR);
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100830 if (rc < 0)
831 return rc;
832
833
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200834 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100835 read_cb, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100836 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200837 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
838 exit(1);
839 }
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100840 proxy_init(g_hlr->gs);
Harald Weltee72cf552016-04-28 07:18:49 +0200841
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200842 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100843
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100844 dgsm_start(hlr_ctx);
845
Harald Welteaabae9e2016-04-28 12:48:14 +0200846 osmo_init_ignore_signals();
847 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700848 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200849 signal(SIGUSR1, &signal_hdlr);
850
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100851 if (cmdline_opts.daemonize) {
852 rc = osmo_daemonize();
853 if (rc < 0) {
854 perror("Error during daemonize");
855 exit(1);
856 }
857 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200858
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700859 while (!quit)
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100860 osmo_select_main_ctx(0);
861
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100862 dgsm_stop();
Harald Weltee72cf552016-04-28 07:18:49 +0200863
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700864 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100865 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200866 log_fini();
867
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700868 /**
869 * Report the heap state of root context, then free,
870 * so both ASAN and Valgrind are happy...
871 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700872 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700873 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700874
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700875 /* FIXME: VTY code still uses NULL-context */
876 talloc_free(tall_vty_ctx);
877
878 /**
879 * Report the heap state of NULL context, then free,
880 * so both ASAN and Valgrind are happy...
881 */
882 talloc_report_full(NULL, stderr);
883 talloc_disable_null_tracking();
884
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700885 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200886}