blob: 38b9022fc297bd6732004e097fa8f33ee1da26d1 [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>
38#include <osmocom/gsm/protocol/gsm_23_003.h>
Harald Weltee72cf552016-04-28 07:18:49 +020039
Neels Hofmeyr2f758032019-11-20 00:37:07 +010040#include <osmocom/hlr/db.h>
41#include <osmocom/hlr/hlr.h>
42#include <osmocom/hlr/ctrl.h>
43#include <osmocom/hlr/logging.h>
44#include <osmocom/hlr/gsup_server.h>
45#include <osmocom/hlr/gsup_router.h>
46#include <osmocom/hlr/rand.h>
47#include <osmocom/hlr/luop.h>
48#include <osmocom/hlr/hlr_vty.h>
49#include <osmocom/hlr/hlr_ussd.h>
Harald Weltee72cf552016-04-28 07:18:49 +020050
Harald Welted5807b82018-07-29 12:27:41 +020051struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010052static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070053static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020054
Stefan Sperlingf1622522018-04-09 11:39:16 +020055/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
56 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020057 * \param[in] subscr A subscriber we have new data to send for.
58 */
59void
60osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
61{
62 struct osmo_gsup_conn *co;
63
Neels Hofmeyre66e5252018-09-28 02:59:19 +020064 if (g_hlr->gs == NULL) {
65 LOGP(DLGSUP, LOGL_DEBUG,
66 "IMSI %s: NOT Notifying peers of subscriber data change,"
67 " there is no GSUP server\n",
68 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020069 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020070 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020071
72 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020073 struct osmo_gsup_message gsup = { };
74 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
75 uint8_t apn[APN_MAXLEN];
76 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020077 uint8_t *peer;
78 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020079 size_t peer_strlen;
80 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020081 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020082
Neels Hofmeyre66e5252018-09-28 02:59:19 +020083 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020084 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020085 peer_compare = subscr->sgsn_number;
86 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020087 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020088 peer_compare = subscr->vlr_number;
89 } else {
90 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +020091 continue;
92 }
93
Neels Hofmeyre66e5252018-09-28 02:59:19 +020094 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
95 if (peer_len < 0) {
96 LOGP(DLGSUP, LOGL_ERROR,
97 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
98 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
99 co && co->conn && co->conn->server? co->conn->server->port : 0);
100 continue;
101 }
102
103 peer_strlen = strnlen((const char*)peer, peer_len);
104 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
105 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
106 /* I hope peer is always nul terminated... */
107 if (peer_strlen < peer_len)
108 LOGP(DLGSUP, LOGL_DEBUG,
109 "IMSI %s: subscriber change: skipping %s peer %s\n",
110 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
111 osmo_quote_str((char*)peer, -1));
112 continue;
113 }
114
115 LOGP(DLGSUP, LOGL_DEBUG,
116 "IMSI %s: subscriber change: notifying %s peer %s\n",
117 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
118 osmo_quote_str(peer_compare, -1));
119
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200120 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
121 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200122 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200123 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200124 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200125 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
126 co && co->conn && co->conn->server? co->conn->server->port : 0);
127 continue;
128 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200129
Stefan Sperling93c5b102018-04-10 19:26:14 +0200130 /* Send ISD to MSC/SGSN */
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100131 msg_out = osmo_gsup_msgb_alloc("GSUP ISD UPDATE");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200132 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200133 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200134 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
135 "for %s:%u\n", subscr->imsi,
136 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
137 co && co->conn && co->conn->server? co->conn->server->port : 0);
138 continue;
139 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200140 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200141
Stefan Sperling93c5b102018-04-10 19:26:14 +0200142 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
143 LOGP(DMAIN, LOGL_ERROR,
144 "IMSI='%s': Cannot notify GSUP client; send operation failed "
145 "for %s:%u\n", subscr->imsi,
146 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
147 co && co->conn && co->conn->server? co->conn->server->port : 0);
148 continue;
149 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200150 }
151}
152
Oliver Smithc7f17872019-03-04 15:10:44 +0100153static int generate_new_msisdn(char *msisdn, const char *imsi, unsigned int len)
154{
155 int i, j, rc;
156 uint8_t rand_buf[GSM23003_MSISDN_MAX_DIGITS];
157
158 OSMO_ASSERT(len <= sizeof(rand_buf));
159
160 /* Generate a random unique MSISDN (with retry) */
161 for (i = 0; i < 10; i++) {
162 /* Get a random number (with retry) */
163 for (j = 0; j < 10; j++) {
164 rc = osmo_get_rand_id(rand_buf, len);
165 if (!rc)
166 break;
167 }
168 if (rc) {
169 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate new MSISDN, random number generator"
170 " failed (rc=%d)\n", imsi, rc);
171 return rc;
172 }
173
174 /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
175 for (j = 0; j < len; j++)
176 msisdn[j] = 48 + (rand_buf[j] % 10);
177 msisdn[j] = '\0';
178
179 /* Ensure there is no subscriber with such MSISDN */
180 if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn) == -ENOENT)
181 return 0;
182 }
183
184 /* Failure */
185 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to generate a new MSISDN, consider increasing "
186 "the length for the automatically assigned MSISDNs "
187 "(see 'subscriber-create-on-demand' command)\n", imsi);
188 return -1;
189}
190
191static int subscr_create_on_demand(const char *imsi)
192{
193 char msisdn[GSM23003_MSISDN_MAX_DIGITS + 1];
194 int rc;
195 unsigned int rand_msisdn_len = g_hlr->subscr_create_on_demand_rand_msisdn_len;
196
197 if (!g_hlr->subscr_create_on_demand)
198 return -1;
199 if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
200 return -1;
201 if (rand_msisdn_len && generate_new_msisdn(msisdn, imsi, rand_msisdn_len) != 0)
202 return -1;
203
204 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", imsi);
205 rc = db_subscr_create(g_hlr->dbc, imsi, g_hlr->subscr_create_on_demand_flags);
206 if (rc) {
207 LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand (rc=%d): IMSI='%s'\n", rc, imsi);
208 return rc;
209 }
210
211 if (!rand_msisdn_len)
212 return 0;
213
214 /* Update MSISDN of the new (just allocated) subscriber */
215 rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
216 if (rc) {
217 LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign MSISDN='%s' (rc=%d)\n", imsi, msisdn, rc);
218 return rc;
219 }
220 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned MSISDN='%s'\n", imsi, msisdn);
221
222 return 0;
223}
224
Harald Weltee687be52016-05-03 18:49:27 +0200225/***********************************************************************
226 * Send Auth Info handling
227 ***********************************************************************/
228
Harald Weltee72cf552016-04-28 07:18:49 +0200229/* process an incoming SAI request */
230static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100231 const struct osmo_gsup_message *gsup,
232 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200233{
234 struct osmo_gsup_message gsup_out;
235 struct msgb *msg_out;
Harald Welte06f5af22019-08-21 20:01:31 +0200236 bool separation_bit = false;
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100237 int num_auth_vectors = OSMO_GSUP_MAX_NUM_AUTH_INFO;
Harald Weltee72cf552016-04-28 07:18:49 +0200238 int rc;
239
Oliver Smithc7f17872019-03-04 15:10:44 +0100240 subscr_create_on_demand(gsup->imsi);
241
Harald Weltee72cf552016-04-28 07:18:49 +0200242 /* initialize return message structure */
243 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200244 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
245
Harald Welte06f5af22019-08-21 20:01:31 +0200246 if (gsup->current_rat_type == OSMO_RAT_EUTRAN_SGS)
247 separation_bit = true;
248
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100249 if (gsup->num_auth_vectors > 0 &&
250 gsup->num_auth_vectors <= OSMO_GSUP_MAX_NUM_AUTH_INFO)
251 num_auth_vectors = gsup->num_auth_vectors;
252
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100253 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
254 gsup_out.auth_vectors,
Alexander Couzens81b92bb2020-01-07 19:05:13 +0100255 num_auth_vectors,
Harald Welte06f5af22019-08-21 20:01:31 +0200256 gsup->rand, gsup->auts, separation_bit);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100257 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200258 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100259 switch (rc) {
260 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100261 /* 0 means "0 tuples generated", which shouldn't happen.
262 * Treat the same as "no auth data". */
263 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100264 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
265 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
266 gsup->imsi);
267 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
268 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100269 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100270 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100271 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
272 break;
273 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100274 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100275 gsup_out.cause = GMM_CAUSE_NET_FAIL;
276 break;
277 }
Harald Welte15db8262016-05-05 16:50:39 +0200278 } else {
279 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
280 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200281 }
282
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100283 msg_out = osmo_gsup_msgb_alloc("GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200284 osmo_gsup_encode(msg_out, &gsup_out);
285 return osmo_gsup_conn_send(conn, msg_out);
286}
287
Harald Weltee687be52016-05-03 18:49:27 +0200288/***********************************************************************
289 * LU Operation State / Structure
290 ***********************************************************************/
291
292static LLIST_HEAD(g_lu_ops);
293
Harald Weltee687be52016-05-03 18:49:27 +0200294/*! Receive Cancel Location Result from old VLR/SGSN */
295void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100296 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200297{
298 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
299 /* FIXME: Check for spoofing */
300
301 osmo_timer_del(&luop->timer);
302
303 /* FIXME */
304
305 lu_op_tx_insert_subscr_data(luop);
306}
307
Harald Weltee687be52016-05-03 18:49:27 +0200308/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
309static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
310 const struct osmo_gsup_message *gsup)
311{
312 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
313 /* FIXME: Check for spoofing */
314
315 osmo_timer_del(&luop->timer);
316
317 /* Subscriber_Present_HLR */
318 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
319
320 /* Send final ACK towards inquiring VLR/SGSN */
321 lu_op_tx_ack(luop);
322}
323
324/*! Receive GSUP message for given \ref lu_operation */
325void lu_op_rx_gsup(struct lu_operation *luop,
326 const struct osmo_gsup_message *gsup)
327{
328 switch (gsup->message_type) {
329 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
330 /* FIXME */
331 break;
332 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
333 lu_op_rx_insert_subscr_data_ack(luop, gsup);
334 break;
335 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
336 /* FIXME */
337 break;
338 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
339 lu_op_rx_cancel_old_ack(luop, gsup);
340 break;
341 default:
342 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
343 gsup->message_type);
344 break;
345 }
346}
347
Harald Weltee687be52016-05-03 18:49:27 +0200348/*! Receive Update Location Request, creates new \ref lu_operation */
349static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
350 const struct osmo_gsup_message *gsup)
351{
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200352 struct hlr_subscriber *subscr;
Maxea8b0d42017-02-14 16:53:04 +0100353 struct lu_operation *luop = lu_op_alloc_conn(conn);
354 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200355 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100356 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200357 }
358
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200359 subscr = &luop->subscr;
360
Harald Weltee687be52016-05-03 18:49:27 +0200361 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100362
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200363 switch (gsup->cn_domain) {
364 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200365 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200366 break;
367 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200368 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
369 * accordance with the GSUP spec in osmo-hlr's user manual (section
370 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
371 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200372 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200373 conn->supports_ps = true;
Stefan Sperling1cb48922018-05-03 14:05:56 +0200374 luop->is_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200375 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200376 }
Harald Weltee687be52016-05-03 18:49:27 +0200377 llist_add(&luop->list, &g_lu_ops);
378
Oliver Smithc7f17872019-03-04 15:10:44 +0100379 subscr_create_on_demand(gsup->imsi);
380
Harald Weltee687be52016-05-03 18:49:27 +0200381 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
382
383 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100384 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200385 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900386 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200387 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
388 return 0;
389 }
390
Harald Welte99909272016-05-05 18:24:15 +0200391 /* Check if subscriber is generally permitted on CS or PS
392 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100393 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200394 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
395 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100396 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200397 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
398 return 0;
399 }
400
401 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
402
403#if 0
404 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
405 if (luop->is_ps == false &&
406 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200407 lu_op_tx_cancel_old(luop);
408 } else if (luop->is_ps == true &&
409 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200410 lu_op_tx_cancel_old(luop);
411 } else
412#endif
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200413
414 /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */
415 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n",
416 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number",
417 osmo_quote_str((const char*)luop->peer, -1));
418 if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps))
419 LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n",
420 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number");
421
Oliver Smith66106c02019-01-09 12:05:15 +0100422 /* TODO: Subscriber allowed to roam in PLMN? */
423 /* TODO: Update RoutingInfo */
424 /* TODO: Reset Flag MS Purged (cs/ps) */
425 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
426 lu_op_tx_insert_subscr_data(luop);
427
Harald Weltee687be52016-05-03 18:49:27 +0200428 return 0;
429}
430
Harald Welteb18f0e02016-05-05 21:03:03 +0200431static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
432 const struct osmo_gsup_message *gsup)
433{
434 struct osmo_gsup_message gsup_reply = {0};
435 struct msgb *msg_out;
436 bool is_ps = false;
437 int rc;
438
439 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
440 is_ps ? "PS" : "CS");
441
442 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
443
444 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
445 is_ps = true;
446
447 /* FIXME: check if the VLR that sends the purge is the same that
448 * we have on record. Only update if yes */
449
450 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200451 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200452
Harald Welte3f2a9a22018-03-01 23:35:35 +0100453 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200454 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100455 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200456 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
457 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
458 } else {
459 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
460 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
461 }
462
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100463 msg_out = osmo_gsup_msgb_alloc("GSUP AUC response");
Harald Welteb18f0e02016-05-05 21:03:03 +0200464 osmo_gsup_encode(msg_out, &gsup_reply);
465 return osmo_gsup_conn_send(conn, msg_out);
466}
467
Harald Weltea1d3b042018-06-11 20:28:35 +0200468static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
469 enum osmo_gsup_message_type type_in, uint8_t err_cause)
470{
Oliver Smithf9cf1802019-02-04 11:20:08 +0100471 int type_err = OSMO_GSUP_TO_MSGT_ERROR(type_in);
Harald Weltea1d3b042018-06-11 20:28:35 +0200472 struct osmo_gsup_message gsup_reply = {0};
473 struct msgb *msg_out;
474
Harald Weltea1d3b042018-06-11 20:28:35 +0200475 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
476 gsup_reply.message_type = type_err;
477 gsup_reply.cause = err_cause;
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100478 msg_out = osmo_gsup_msgb_alloc("GSUP ERR response");
Harald Weltea1d3b042018-06-11 20:28:35 +0200479 osmo_gsup_encode(msg_out, &gsup_reply);
480 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
481 return osmo_gsup_conn_send(conn, msg_out);
482}
483
Oliver Smith783ac812018-12-17 11:34:51 +0100484static int rx_check_imei_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
485{
486 struct osmo_gsup_message gsup_reply = {0};
487 struct msgb *msg_out;
Oliver Smith103c11b2019-06-06 11:57:05 +0200488 char imei[GSM23003_IMEI_NUM_DIGITS_NO_CHK+1] = {0};
489 int rc;
Oliver Smith783ac812018-12-17 11:34:51 +0100490
Oliver Smith103c11b2019-06-06 11:57:05 +0200491 /* Require IMEI */
492 if (!gsup->imei_enc) {
493 LOGP(DMAIN, LOGL_ERROR, "%s: missing IMEI\n", gsup->imsi);
Oliver Smith783ac812018-12-17 11:34:51 +0100494 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
495 return -1;
496 }
497
Oliver Smith103c11b2019-06-06 11:57:05 +0200498 /* Decode IMEI (fails if IMEI is too long) */
499 rc = gsm48_decode_bcd_number2(imei, sizeof(imei), gsup->imei_enc, gsup->imei_enc_len, 0);
500 if (rc < 0) {
501 LOGP(DMAIN, LOGL_ERROR, "%s: failed to decode IMEI (rc: %i)\n", gsup->imsi, rc);
502 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
503 return -1;
504 }
505
506 /* Check if IMEI is too short */
507 if (strlen(imei) != GSM23003_IMEI_NUM_DIGITS_NO_CHK) {
508 LOGP(DMAIN, LOGL_ERROR, "%s: wrong encoded IMEI length (IMEI: '%s', %lu, %i)\n", gsup->imsi, imei,
509 strlen(imei), GSM23003_IMEI_NUM_DIGITS_NO_CHK);
Oliver Smith783ac812018-12-17 11:34:51 +0100510 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
511 return -1;
512 }
513
Oliver Smithc7f17872019-03-04 15:10:44 +0100514 subscr_create_on_demand(gsup->imsi);
515
Oliver Smith851814a2019-01-11 15:30:21 +0100516 /* Save in DB if desired */
517 if (g_hlr->store_imei) {
518 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
519 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
520 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
521 return -1;
522 }
523 } else {
524 /* Check if subscriber exists and print IMEI */
525 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
526 struct hlr_subscriber subscr;
527 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
528 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
529 return -1;
530 }
531 }
Oliver Smith783ac812018-12-17 11:34:51 +0100532
533 /* Accept all IMEIs */
534 gsup_reply.imei_result = OSMO_GSUP_IMEI_RESULT_ACK;
535 gsup_reply.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT;
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100536 msg_out = osmo_gsup_msgb_alloc("GSUP Check_IMEI response");
Oliver Smith783ac812018-12-17 11:34:51 +0100537 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
538 osmo_gsup_encode(msg_out, &gsup_reply);
539 return osmo_gsup_conn_send(conn, msg_out);
540}
541
Oliver Smith28f0af82019-02-22 11:46:33 +0100542static char namebuf[255];
543#define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \
544 LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \
545 osmo_gsup_message_type_name(gsup->message_type), \
546 osmo_gsup_message_class_name(gsup->message_class), \
547 gsup->imsi, \
548 osmo_quote_str((const char *)gsup->source_name, gsup->source_name_len), \
549 osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)gsup->destination_name, gsup->destination_name_len), \
550 ## args)
551
552static int read_cb_forward(struct osmo_gsup_conn *conn, struct msgb *msg, const struct osmo_gsup_message *gsup)
553{
554 int ret = -EINVAL;
555 struct osmo_gsup_message *gsup_err;
556
557 /* FIXME: it would be better if the msgb never were deallocated immediately by osmo_gsup_addr_send(), which a
558 * select-loop volatile talloc context could facilitate. Then we would still be able to access gsup-> members
559 * (pointing into the msgb) even after sending failed, and we wouldn't need to copy this data before sending: */
560 /* Prepare error message (before IEs get deallocated) */
561 gsup_err = talloc_zero(hlr_ctx, struct osmo_gsup_message);
562 OSMO_STRLCPY_ARRAY(gsup_err->imsi, gsup->imsi);
563 gsup_err->message_class = gsup->message_class;
564 gsup_err->destination_name = talloc_memdup(gsup_err, gsup->destination_name, gsup->destination_name_len);
565 gsup_err->destination_name_len = gsup->destination_name_len;
Vadim Yanitskiy8625cda2019-05-07 21:09:01 +0700566 gsup_err->message_type = gsup->message_type;
Vadim Yanitskiy609978d2019-05-07 20:35:05 +0700567 gsup_err->session_state = gsup->session_state;
Oliver Smith28f0af82019-02-22 11:46:33 +0100568 gsup_err->session_id = gsup->session_id;
569 gsup_err->source_name = talloc_memdup(gsup_err, gsup->source_name, gsup->source_name_len);
570 gsup_err->source_name_len = gsup->source_name_len;
571
572 /* Check for routing IEs */
573 if (!gsup->source_name || !gsup->source_name_len || !gsup->destination_name || !gsup->destination_name_len) {
574 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "missing routing IEs\n");
575 goto end;
576 }
577
578 /* Verify source name (e.g. "MSC-00-00-00-00-00-00") */
579 if (gsup_route_find(conn->server, gsup->source_name, gsup->source_name_len) != conn) {
580 LOGP_GSUP_FWD(gsup, LOGL_ERROR, "mismatching source name\n");
581 goto end;
582 }
583
Oliver Smith28f0af82019-02-22 11:46:33 +0100584 /* Forward message without re-encoding (so we don't remove unknown IEs) */
585 LOGP_GSUP_FWD(gsup, LOGL_INFO, "checks passed, forwarding\n");
586
587 /* Remove incoming IPA header to be able to prepend an outgoing IPA header */
588 msgb_pull_to_l2(msg);
589 ret = osmo_gsup_addr_send(g_hlr->gs, gsup->destination_name, gsup->destination_name_len, msg);
590 /* AT THIS POINT, THE msg MAY BE DEALLOCATED and the data like gsup->imsi, gsup->source_name etc may all be
591 * invalid and cause segfaults. */
592 msg = NULL;
593 gsup = NULL;
594 if (ret == -ENODEV)
595 LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "destination not connected\n");
596 else if (ret)
597 LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "unknown error %i\n", ret);
598
599end:
600 /* Send error back to source */
601 if (ret) {
Neels Hofmeyra7d0f872019-10-30 02:08:28 +0100602 struct msgb *msg_err = osmo_gsup_msgb_alloc("GSUP forward ERR response");
Vadim Yanitskiy8625cda2019-05-07 21:09:01 +0700603 gsup_err->message_type = OSMO_GSUP_MSGT_E_ROUTING_ERROR;
Oliver Smith28f0af82019-02-22 11:46:33 +0100604 osmo_gsup_encode(msg_err, gsup_err);
605 LOGP_GSUP_FWD(gsup_err, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(gsup_err->message_type));
606 osmo_gsup_conn_send(conn, msg_err);
607 }
608 talloc_free(gsup_err);
609 if (msg)
610 msgb_free(msg);
611 return ret;
612}
613
Harald Weltee72cf552016-04-28 07:18:49 +0200614static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
615{
616 static struct osmo_gsup_message gsup;
617 int rc;
618
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700619 if (!msgb_l2(msg) || !msgb_l2len(msg)) {
620 LOGP(DMAIN, LOGL_ERROR, "missing or empty L2 data\n");
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700621 msgb_free(msg);
622 return -EINVAL;
Vadim Yanitskiyc69a18b2019-05-07 20:53:54 +0700623 }
624
Harald Weltee687be52016-05-03 18:49:27 +0200625 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200626 if (rc < 0) {
627 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700628 msgb_free(msg);
629 return rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200630 }
631
Harald Weltea1d3b042018-06-11 20:28:35 +0200632 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
633 * digits is impossible. Even 5 digits is a highly theoretical case */
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700634 if (strlen(gsup.imsi) < 5) { /* TODO: move this check to libosmogsm/gsup.c? */
Neels Hofmeyr9f6e5582019-04-03 18:16:50 +0200635 LOGP(DMAIN, LOGL_ERROR, "IMSI too short: %s\n", osmo_quote_str(gsup.imsi, -1));
Vadim Yanitskiyd9724f42019-05-07 21:05:18 +0700636 gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
637 msgb_free(msg);
638 return -EINVAL;
Neels Hofmeyr9f6e5582019-04-03 18:16:50 +0200639 }
Harald Weltea1d3b042018-06-11 20:28:35 +0200640
Oliver Smith28f0af82019-02-22 11:46:33 +0100641 if (gsup.destination_name_len)
642 return read_cb_forward(conn, msg, &gsup);
643
Harald Weltee72cf552016-04-28 07:18:49 +0200644 switch (gsup.message_type) {
645 /* requests sent to us */
646 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100647 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200648 break;
649 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200650 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200651 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200652 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
653 rx_purge_ms_req(conn, &gsup);
654 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200655 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100656 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
657 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
658 "for IMSI %s\n", gsup.imsi);
659 break;
660 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
661 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
662 gsup.imsi);
663 break;
Harald Weltebb779392018-06-16 20:21:10 +0200664 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
665 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
666 rx_proc_ss_req(conn, &gsup);
667 break;
668 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
669 rx_proc_ss_error(conn, &gsup);
670 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200671 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200672 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200673 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
674 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
675 {
Maxea8b0d42017-02-14 16:53:04 +0100676 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
677 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200678 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100679 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
680 "unknown IMSI %s\n",
681 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200682 gsup.imsi);
683 break;
684 }
685 lu_op_rx_gsup(luop, &gsup);
686 }
Harald Weltee72cf552016-04-28 07:18:49 +0200687 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100688 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
689 rx_check_imei_req(conn, &gsup);
690 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200691 default:
Maxaa0fefd2017-02-16 12:25:22 +0100692 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
693 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200694 break;
695 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200696 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200697 return 0;
698}
699
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100700static void print_usage()
701{
702 printf("Usage: osmo-hlr\n");
703}
704
705static void print_help()
706{
707 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100708 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100709 printf(" -l --database db-name The database to use.\n");
710 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
711 printf(" -D --daemonize Fork the process into a background daemon.\n");
712 printf(" -s --disable-color Do not print ANSI colors in the log\n");
713 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
714 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100715 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100716 printf(" -C --db-check Quit after opening (and upgrading) the database.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100717 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100718}
719
720static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100721 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100722 const char *db_file;
723 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100724 bool db_upgrade;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100725 bool db_check;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100726} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100727 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200728 .db_file = NULL,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100729 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100730 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100731};
732
733static void handle_options(int argc, char **argv)
734{
735 while (1) {
736 int option_index = 0, c;
737 static struct option long_options[] = {
738 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100739 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100740 {"database", 1, 0, 'l'},
741 {"debug", 1, 0, 'd'},
742 {"daemonize", 0, 0, 'D'},
743 {"disable-color", 0, 0, 's'},
744 {"log-level", 1, 0, 'e'},
745 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100746 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100747 {"db-check", 0, 0, 'C' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100748 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100749 {0, 0, 0, 0}
750 };
751
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100752 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100753 long_options, &option_index);
754 if (c == -1)
755 break;
756
757 switch (c) {
758 case 'h':
759 print_usage();
760 print_help();
761 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100762 case 'c':
763 cmdline_opts.config_file = optarg;
764 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100765 case 'l':
766 cmdline_opts.db_file = optarg;
767 break;
768 case 'd':
769 log_parse_category_mask(osmo_stderr_target, optarg);
770 break;
771 case 'D':
772 cmdline_opts.daemonize = 1;
773 break;
774 case 's':
775 log_set_use_color(osmo_stderr_target, 0);
776 break;
777 case 'e':
778 log_set_log_level(osmo_stderr_target, atoi(optarg));
779 break;
780 case 'T':
781 log_set_print_timestamp(osmo_stderr_target, 1);
782 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100783 case 'U':
784 cmdline_opts.db_upgrade = true;
785 break;
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100786 case 'C':
787 cmdline_opts.db_check = true;
788 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100789 case 'V':
790 print_version(1);
791 exit(0);
792 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100793 default:
794 /* catch unknown options *as well as* missing arguments. */
795 fprintf(stderr, "Error in command line options. Exiting.\n");
796 exit(-1);
797 break;
798 }
799 }
Harald Welte80dc9ae2019-12-03 22:16:39 +0100800
801 if (argc > optind) {
802 fprintf(stderr, "Unsupported positional arguments on command line\n");
803 exit(2);
804 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100805}
806
Harald Welteaabae9e2016-04-28 12:48:14 +0200807static void signal_hdlr(int signal)
808{
809 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700810 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200811 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700812 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700813 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200814 break;
815 case SIGUSR1:
816 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100817 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200818 break;
819 }
820}
Harald Weltee72cf552016-04-28 07:18:49 +0200821
Max372868b2017-03-02 12:12:00 +0100822static const char vlr_copyright[] =
823 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
824 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
825 "This is free software: you are free to change and redistribute it.\r\n"
826 "There is NO WARRANTY, to the extent permitted by law.\r\n";
827
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100828static struct vty_app_info vty_info = {
829 .name = "OsmoHLR",
830 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100831 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100832 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200833 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100834};
835
Harald Weltee72cf552016-04-28 07:18:49 +0200836int main(int argc, char **argv)
837{
Harald Weltee72cf552016-04-28 07:18:49 +0200838 int rc;
839
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700840 /* Track the use of talloc NULL memory contexts */
841 talloc_enable_null_tracking();
842
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100843 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
844 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100845 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200846
Maxd4bebbd2017-03-02 12:00:19 +0100847 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200848 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200849 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200850 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200851 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200852 g_hlr->db_file_path = talloc_strdup(g_hlr, HLR_DEFAULT_DB_FILE_PATH);
Maxd4bebbd2017-03-02 12:00:19 +0100853
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700854 /* Init default (call independent) SS session guard timeout value */
855 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
856
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200857 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200858 if (rc < 0) {
859 fprintf(stderr, "Error initializing logging\n");
860 exit(1);
861 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100862
Max20ddfdb2019-02-18 13:12:27 +0100863 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100864 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100865 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100866 handle_options(argc, argv);
Pau Espin Pedrole49391b2019-08-05 15:57:10 +0200867 hlr_vty_init();
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100868
869 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
870 if (rc < 0) {
871 LOGP(DMAIN, LOGL_FATAL,
872 "Failed to parse the config file: '%s'\n",
873 cmdline_opts.config_file);
874 return rc;
875 }
876
Harald Weltee72cf552016-04-28 07:18:49 +0200877 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
878
879 rc = rand_init();
880 if (rc < 0) {
881 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
882 exit(1);
883 }
884
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200885 if (cmdline_opts.db_file)
886 osmo_talloc_replace_string(g_hlr, &g_hlr->db_file_path, cmdline_opts.db_file);
887
888 g_hlr->dbc = db_open(hlr_ctx, g_hlr->db_file_path, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100889 if (!g_hlr->dbc) {
Neels Hofmeyr5857c592019-04-02 04:24:49 +0200890 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 +0200891 exit(1);
892 }
893
Neels Hofmeyrc3d40322019-10-31 01:39:05 +0100894 if (cmdline_opts.db_check) {
895 LOGP(DMAIN, LOGL_NOTICE, "Cmdline option --db-check: Database was opened successfully, quitting.\n");
896 db_close(g_hlr->dbc);
897 log_fini();
898 talloc_free(hlr_ctx);
899 talloc_free(tall_vty_ctx);
900 talloc_disable_null_tracking();
901 exit(0);
902 }
903
904 /* start telnet after reading config for vty_get_bind_addr() */
905 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
906 OSMO_VTY_PORT_HLR);
907 if (rc < 0)
908 return rc;
909
910
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200911 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200912 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100913 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200914 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
915 exit(1);
916 }
917
Max372868b2017-03-02 12:12:00 +0100918 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200919 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100920
Harald Welteaabae9e2016-04-28 12:48:14 +0200921 osmo_init_ignore_signals();
922 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700923 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200924 signal(SIGUSR1, &signal_hdlr);
925
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100926 if (cmdline_opts.daemonize) {
927 rc = osmo_daemonize();
928 if (rc < 0) {
929 perror("Error during daemonize");
930 exit(1);
931 }
932 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200933
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700934 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200935 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200936
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700937 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100938 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200939 log_fini();
940
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700941 /**
942 * Report the heap state of root context, then free,
943 * so both ASAN and Valgrind are happy...
944 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700945 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700946 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700947
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700948 /* FIXME: VTY code still uses NULL-context */
949 talloc_free(tall_vty_ctx);
950
951 /**
952 * Report the heap state of NULL context, then free,
953 * so both ASAN and Valgrind are happy...
954 */
955 talloc_report_full(NULL, stderr);
956 talloc_disable_null_tracking();
957
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700958 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200959}