blob: 479b5cfb9808bd3a4e3769626b5c77bcc91c361d [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>
26#include <osmocom/core/logging.h>
27#include <osmocom/core/application.h>
28#include <osmocom/gsm/gsup.h>
Neels Hofmeyr627de842016-12-19 13:16:06 +010029#include <osmocom/gsm/gsm48_ie.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>
Harald Weltee72cf552016-04-28 07:18:49 +020035
36#include "db.h"
Maxd4bebbd2017-03-02 12:00:19 +010037#include "hlr.h"
Max372868b2017-03-02 12:12:00 +010038#include "ctrl.h"
Harald Weltee72cf552016-04-28 07:18:49 +020039#include "logging.h"
40#include "gsup_server.h"
Harald Weltee687be52016-05-03 18:49:27 +020041#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020042#include "rand.h"
Maxea8b0d42017-02-14 16:53:04 +010043#include "luop.h"
Neels Hofmeyr7685a782017-01-30 23:30:26 +010044#include "hlr_vty.h"
Harald Weltee72cf552016-04-28 07:18:49 +020045
Maxd4bebbd2017-03-02 12:00:19 +010046static struct hlr *g_hlr;
Harald Weltee72cf552016-04-28 07:18:49 +020047
Stefan Sperlingf1622522018-04-09 11:39:16 +020048/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
49 *
50 * FIXME: In order to support large-scale networks this function should skip
51 * VLRs/SGSNs which do not currently serve the subscriber.
52 *
53 * \param[in] subscr A subscriber we have new data to send for.
54 */
55void
56osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
57{
58 struct osmo_gsup_conn *co;
59
60 if (g_hlr->gs == NULL)
61 return;
62
63 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperling93c5b102018-04-10 19:26:14 +020064 struct osmo_gsup_message gsup = {
65 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST
66 };
67 uint8_t *peer;
68 int peer_len;
69 uint8_t msisdn_enc[43]; /* TODO use constant; TS 24.008 10.5.4.7 */
70 int len;
71 struct msgb *msg_out;
72
73 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
74 if (peer_len < 0) {
Stefan Sperlingf1622522018-04-09 11:39:16 +020075 LOGP(DMAIN, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +020076 "IMSI='%s': Cannot notify GSUP client, cannot get peer name "
77 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +020078 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
79 co && co->conn && co->conn->server? co->conn->server->port : 0);
80 continue;
81 }
Stefan Sperling93c5b102018-04-10 19:26:14 +020082
83 osmo_strlcpy(gsup.imsi, subscr->imsi, GSM23003_IMSI_MAX_DIGITS + 1);
84
85 len = gsm48_encode_bcd_number(msisdn_enc, sizeof(msisdn_enc), 0, subscr->msisdn);
86 if (len < 1) {
87 LOGP(DMAIN, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n",
88 subscr->imsi, subscr->msisdn);
89 continue;
90 }
91 gsup.msisdn_enc = msisdn_enc;
92 gsup.msisdn_enc_len = len;
93
94 if (co->supports_ps) {
95 gsup.cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
96
97 /* FIXME: PDP infos - use more fine-grained access control
98 instead of wildcard APN */
99 osmo_gsup_configure_wildcard_apn(&gsup);
100 } else if (co->supports_cs) {
101 gsup.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
102 } else {
103 /* We have not yet received a location update from this subscriber .*/
104 continue;
105 }
106
107 /* Send ISD to MSC/SGSN */
108 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
109 if (msg_out == NULL) {
110 LOGP(DMAIN, LOGL_ERROR,
111 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
112 "for %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 osmo_gsup_encode(msg_out, &gsup);
119 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
120 LOGP(DMAIN, LOGL_ERROR,
121 "IMSI='%s': Cannot notify GSUP client; send operation failed "
122 "for %s:%u\n", subscr->imsi,
123 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
124 co && co->conn && co->conn->server? co->conn->server->port : 0);
125 continue;
126 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200127 }
128}
129
Harald Weltee687be52016-05-03 18:49:27 +0200130/***********************************************************************
131 * Send Auth Info handling
132 ***********************************************************************/
133
Harald Weltee72cf552016-04-28 07:18:49 +0200134/* process an incoming SAI request */
135static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100136 const struct osmo_gsup_message *gsup,
137 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200138{
139 struct osmo_gsup_message gsup_out;
140 struct msgb *msg_out;
141 int rc;
142
143 /* initialize return message structure */
144 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200145 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
146
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100147 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
148 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200149 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200150 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100151 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200152 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100153 switch (rc) {
154 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100155 /* 0 means "0 tuples generated", which shouldn't happen.
156 * Treat the same as "no auth data". */
157 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100158 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
159 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
160 gsup->imsi);
161 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
162 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100163 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100164 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100165 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
166 break;
167 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100168 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100169 gsup_out.cause = GMM_CAUSE_NET_FAIL;
170 break;
171 }
Harald Welte15db8262016-05-05 16:50:39 +0200172 } else {
173 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
174 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200175 }
176
Harald Weltee687be52016-05-03 18:49:27 +0200177 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200178 osmo_gsup_encode(msg_out, &gsup_out);
179 return osmo_gsup_conn_send(conn, msg_out);
180}
181
Harald Weltee687be52016-05-03 18:49:27 +0200182/***********************************************************************
183 * LU Operation State / Structure
184 ***********************************************************************/
185
186static LLIST_HEAD(g_lu_ops);
187
Harald Weltee687be52016-05-03 18:49:27 +0200188/*! Receive Cancel Location Result from old VLR/SGSN */
189void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100190 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200191{
192 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
193 /* FIXME: Check for spoofing */
194
195 osmo_timer_del(&luop->timer);
196
197 /* FIXME */
198
199 lu_op_tx_insert_subscr_data(luop);
200}
201
Harald Weltee687be52016-05-03 18:49:27 +0200202/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
203static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
204 const struct osmo_gsup_message *gsup)
205{
206 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
207 /* FIXME: Check for spoofing */
208
209 osmo_timer_del(&luop->timer);
210
211 /* Subscriber_Present_HLR */
212 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
213
214 /* Send final ACK towards inquiring VLR/SGSN */
215 lu_op_tx_ack(luop);
216}
217
218/*! Receive GSUP message for given \ref lu_operation */
219void lu_op_rx_gsup(struct lu_operation *luop,
220 const struct osmo_gsup_message *gsup)
221{
222 switch (gsup->message_type) {
223 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
224 /* FIXME */
225 break;
226 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
227 lu_op_rx_insert_subscr_data_ack(luop, gsup);
228 break;
229 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
230 /* FIXME */
231 break;
232 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
233 lu_op_rx_cancel_old_ack(luop, gsup);
234 break;
235 default:
236 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
237 gsup->message_type);
238 break;
239 }
240}
241
Harald Weltee687be52016-05-03 18:49:27 +0200242/*! Receive Update Location Request, creates new \ref lu_operation */
243static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
244 const struct osmo_gsup_message *gsup)
245{
Maxea8b0d42017-02-14 16:53:04 +0100246 struct lu_operation *luop = lu_op_alloc_conn(conn);
247 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200248 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100249 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200250 }
251
Harald Weltee687be52016-05-03 18:49:27 +0200252 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100253
Stefan Sperling93c5b102018-04-10 19:26:14 +0200254 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_CS)
255 conn->supports_cs = true;
256 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
257 conn->supports_ps = true;
Harald Weltee687be52016-05-03 18:49:27 +0200258 luop->is_ps = true;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200259 } else {
260 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
261 * accordance with the GSUP spec in osmo-hlr's user manual (section
262 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
263 * a request, the PS Domain is assumed." */
264 conn->supports_ps = true;
265 }
Harald Weltee687be52016-05-03 18:49:27 +0200266 llist_add(&luop->list, &g_lu_ops);
267
268 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
269
270 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100271 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200272 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900273 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200274 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
275 return 0;
276 }
277
Harald Welte99909272016-05-05 18:24:15 +0200278 /* Check if subscriber is generally permitted on CS or PS
279 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100280 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200281 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
282 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100283 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200284 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
285 return 0;
286 }
287
288 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
289
290#if 0
291 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
292 if (luop->is_ps == false &&
293 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200294 lu_op_tx_cancel_old(luop);
295 } else if (luop->is_ps == true &&
296 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200297 lu_op_tx_cancel_old(luop);
298 } else
299#endif
300 {
301 /* TODO: Subscriber allowed to roam in PLMN? */
302 /* TODO: Update RoutingInfo */
303 /* TODO: Reset Flag MS Purged (cs/ps) */
304 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
305 lu_op_tx_insert_subscr_data(luop);
306 }
307 return 0;
308}
309
Harald Welteb18f0e02016-05-05 21:03:03 +0200310static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
311 const struct osmo_gsup_message *gsup)
312{
313 struct osmo_gsup_message gsup_reply = {0};
314 struct msgb *msg_out;
315 bool is_ps = false;
316 int rc;
317
318 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
319 is_ps ? "PS" : "CS");
320
321 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
322
323 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
324 is_ps = true;
325
326 /* FIXME: check if the VLR that sends the purge is the same that
327 * we have on record. Only update if yes */
328
329 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200330 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200331
Harald Welte3f2a9a22018-03-01 23:35:35 +0100332 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200333 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100334 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200335 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
336 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
337 } else {
338 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
339 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
340 }
341
342 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
343 osmo_gsup_encode(msg_out, &gsup_reply);
344 return osmo_gsup_conn_send(conn, msg_out);
345}
346
Harald Weltee72cf552016-04-28 07:18:49 +0200347static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
348{
349 static struct osmo_gsup_message gsup;
350 int rc;
351
Harald Weltee687be52016-05-03 18:49:27 +0200352 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200353 if (rc < 0) {
354 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
355 return rc;
356 }
357
358 switch (gsup.message_type) {
359 /* requests sent to us */
360 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100361 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200362 break;
363 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200364 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200365 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200366 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
367 rx_purge_ms_req(conn, &gsup);
368 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200369 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100370 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
371 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
372 "for IMSI %s\n", gsup.imsi);
373 break;
374 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
375 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
376 gsup.imsi);
377 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200378 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200379 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200380 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
381 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
382 {
Maxea8b0d42017-02-14 16:53:04 +0100383 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
384 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200385 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100386 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
387 "unknown IMSI %s\n",
388 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200389 gsup.imsi);
390 break;
391 }
392 lu_op_rx_gsup(luop, &gsup);
393 }
Harald Weltee72cf552016-04-28 07:18:49 +0200394 break;
395 default:
Maxaa0fefd2017-02-16 12:25:22 +0100396 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
397 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200398 break;
399 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200400 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200401 return 0;
402}
403
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100404static void print_usage()
405{
406 printf("Usage: osmo-hlr\n");
407}
408
409static void print_help()
410{
411 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100412 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100413 printf(" -l --database db-name The database to use.\n");
414 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
415 printf(" -D --daemonize Fork the process into a background daemon.\n");
416 printf(" -s --disable-color Do not print ANSI colors in the log\n");
417 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
418 printf(" -e --log-level number Set a global loglevel.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100419 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100420}
421
422static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100423 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100424 const char *db_file;
425 bool daemonize;
426} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100427 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100428 .db_file = "hlr.db",
429 .daemonize = false,
430};
431
432static void handle_options(int argc, char **argv)
433{
434 while (1) {
435 int option_index = 0, c;
436 static struct option long_options[] = {
437 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100438 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100439 {"database", 1, 0, 'l'},
440 {"debug", 1, 0, 'd'},
441 {"daemonize", 0, 0, 'D'},
442 {"disable-color", 0, 0, 's'},
443 {"log-level", 1, 0, 'e'},
444 {"timestamp", 0, 0, 'T'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100445 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100446 {0, 0, 0, 0}
447 };
448
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100449 c = getopt_long(argc, argv, "hc:l:d:Dse:TV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100450 long_options, &option_index);
451 if (c == -1)
452 break;
453
454 switch (c) {
455 case 'h':
456 print_usage();
457 print_help();
458 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100459 case 'c':
460 cmdline_opts.config_file = optarg;
461 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100462 case 'l':
463 cmdline_opts.db_file = optarg;
464 break;
465 case 'd':
466 log_parse_category_mask(osmo_stderr_target, optarg);
467 break;
468 case 'D':
469 cmdline_opts.daemonize = 1;
470 break;
471 case 's':
472 log_set_use_color(osmo_stderr_target, 0);
473 break;
474 case 'e':
475 log_set_log_level(osmo_stderr_target, atoi(optarg));
476 break;
477 case 'T':
478 log_set_print_timestamp(osmo_stderr_target, 1);
479 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100480 case 'V':
481 print_version(1);
482 exit(0);
483 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100484 default:
485 /* catch unknown options *as well as* missing arguments. */
486 fprintf(stderr, "Error in command line options. Exiting.\n");
487 exit(-1);
488 break;
489 }
490 }
491}
492
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100493static void *hlr_ctx = NULL;
Harald Welteaabae9e2016-04-28 12:48:14 +0200494
495static void signal_hdlr(int signal)
496{
497 switch (signal) {
498 case SIGINT:
499 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
Maxd4bebbd2017-03-02 12:00:19 +0100500 osmo_gsup_server_destroy(g_hlr->gs);
501 db_close(g_hlr->dbc);
Harald Welteaabae9e2016-04-28 12:48:14 +0200502 log_fini();
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100503 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200504 exit(0);
505 break;
506 case SIGUSR1:
507 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100508 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200509 break;
510 }
511}
Harald Weltee72cf552016-04-28 07:18:49 +0200512
Max372868b2017-03-02 12:12:00 +0100513static const char vlr_copyright[] =
514 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
515 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
516 "This is free software: you are free to change and redistribute it.\r\n"
517 "There is NO WARRANTY, to the extent permitted by law.\r\n";
518
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100519static struct vty_app_info vty_info = {
520 .name = "OsmoHLR",
521 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100522 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100523 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200524 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100525};
526
Harald Weltee72cf552016-04-28 07:18:49 +0200527int main(int argc, char **argv)
528{
Harald Weltee72cf552016-04-28 07:18:49 +0200529 int rc;
530
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100531 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
532 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100533 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200534
Maxd4bebbd2017-03-02 12:00:19 +0100535 g_hlr = talloc_zero(hlr_ctx, struct hlr);
536
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200537 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200538 if (rc < 0) {
539 fprintf(stderr, "Error initializing logging\n");
540 exit(1);
541 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100542
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100543 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100544 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100545 handle_options(argc, argv);
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200546 hlr_vty_init(g_hlr, &hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100547
548 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
549 if (rc < 0) {
550 LOGP(DMAIN, LOGL_FATAL,
551 "Failed to parse the config file: '%s'\n",
552 cmdline_opts.config_file);
553 return rc;
554 }
555
556 /* start telnet after reading config for vty_get_bind_addr() */
557 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
558 OSMO_VTY_PORT_HLR);
559 if (rc < 0)
560 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100561
Harald Weltee72cf552016-04-28 07:18:49 +0200562 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
563
564 rc = rand_init();
565 if (rc < 0) {
566 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
567 exit(1);
568 }
569
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100570 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true);
Maxd4bebbd2017-03-02 12:00:19 +0100571 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200572 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
573 exit(1);
574 }
575
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200576 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200577 read_cb, &g_lu_ops);
Maxd4bebbd2017-03-02 12:00:19 +0100578 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200579 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
580 exit(1);
581 }
582
Max372868b2017-03-02 12:12:00 +0100583 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200584 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100585
Harald Welteaabae9e2016-04-28 12:48:14 +0200586 osmo_init_ignore_signals();
587 signal(SIGINT, &signal_hdlr);
588 signal(SIGUSR1, &signal_hdlr);
589
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100590 if (cmdline_opts.daemonize) {
591 rc = osmo_daemonize();
592 if (rc < 0) {
593 perror("Error during daemonize");
594 exit(1);
595 }
596 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200597
Harald Weltee72cf552016-04-28 07:18:49 +0200598 while (1) {
599 osmo_select_main(0);
600 }
601
Maxd4bebbd2017-03-02 12:00:19 +0100602 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200603
604 log_fini();
605
606 exit(0);
607}