blob: 8732587048e919ce2785ca21261238951e26eaef [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 Hofmeyr7685a782017-01-30 23:30:26 +010029#include <osmocom/vty/vty.h>
30#include <osmocom/vty/command.h>
31#include <osmocom/vty/telnet_interface.h>
32#include <osmocom/vty/ports.h>
Max372868b2017-03-02 12:12:00 +010033#include <osmocom/ctrl/control_vty.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020034#include <osmocom/gsm/apn.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;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070047static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020048
Stefan Sperlingf1622522018-04-09 11:39:16 +020049/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
50 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020051 * \param[in] subscr A subscriber we have new data to send for.
52 */
53void
54osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
55{
Harald Welteb85f6042018-06-15 18:01:14 +020056 /* FIXME: the below code can only be re-enabled after we make sure that an ISD
57 * is only sent tot the currently serving VLR and/or SGSN (if there are any).
58 * We cannot blindly flood the entire PLMN with this, as it would create subscriber
59 * state in every VLR/SGSN out there, even those that have never seen the subscriber.
60 * See https://osmocom.org/issues/3154 for details. */
61#if 0
Stefan Sperlingf1622522018-04-09 11:39:16 +020062 struct osmo_gsup_conn *co;
63
64 if (g_hlr->gs == NULL)
65 return;
66
67 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020068 struct osmo_gsup_message gsup = { };
69 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
70 uint8_t apn[APN_MAXLEN];
71 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020072 uint8_t *peer;
73 int peer_len;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020074 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020075
Stefan Sperlingf83432c2018-05-03 14:26:59 +020076 if (co->supports_ps)
77 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
78 else if (co->supports_cs)
79 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
80 else {
81 /* We have not yet received a location update from this subscriber .*/
82 continue;
83 }
84
85 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
86 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Stefan Sperlingf1622522018-04-09 11:39:16 +020087 LOGP(DMAIN, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +020088 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +020089 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +020090 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
91 co && co->conn && co->conn->server? co->conn->server->port : 0);
92 continue;
93 }
Stefan Sperling93c5b102018-04-10 19:26:14 +020094
Stefan Sperling93c5b102018-04-10 19:26:14 +020095 /* Send ISD to MSC/SGSN */
96 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
97 if (msg_out == NULL) {
98 LOGP(DMAIN, LOGL_ERROR,
99 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
100 "for %s:%u\n", subscr->imsi,
101 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
102 co && co->conn && co->conn->server? co->conn->server->port : 0);
103 continue;
104 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200105 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200106
107 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
108 if (peer_len < 0) {
109 LOGP(DMAIN, LOGL_ERROR,
110 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
111 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
112 co && co->conn && co->conn->server? co->conn->server->port : 0);
113 continue;
114 }
115
Stefan Sperling93c5b102018-04-10 19:26:14 +0200116 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
117 LOGP(DMAIN, LOGL_ERROR,
118 "IMSI='%s': Cannot notify GSUP client; send operation failed "
119 "for %s:%u\n", subscr->imsi,
120 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
121 co && co->conn && co->conn->server? co->conn->server->port : 0);
122 continue;
123 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200124 }
Harald Welteb85f6042018-06-15 18:01:14 +0200125#endif
Stefan Sperlingf1622522018-04-09 11:39:16 +0200126}
127
Harald Weltee687be52016-05-03 18:49:27 +0200128/***********************************************************************
129 * Send Auth Info handling
130 ***********************************************************************/
131
Harald Weltee72cf552016-04-28 07:18:49 +0200132/* process an incoming SAI request */
133static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100134 const struct osmo_gsup_message *gsup,
135 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200136{
137 struct osmo_gsup_message gsup_out;
138 struct msgb *msg_out;
139 int rc;
140
141 /* initialize return message structure */
142 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200143 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
144
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100145 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
146 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200147 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200148 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100149 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200150 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100151 switch (rc) {
152 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100153 /* 0 means "0 tuples generated", which shouldn't happen.
154 * Treat the same as "no auth data". */
155 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100156 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
157 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
158 gsup->imsi);
159 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
160 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100161 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100162 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100163 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
164 break;
165 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100166 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100167 gsup_out.cause = GMM_CAUSE_NET_FAIL;
168 break;
169 }
Harald Welte15db8262016-05-05 16:50:39 +0200170 } else {
171 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
172 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200173 }
174
Harald Weltee687be52016-05-03 18:49:27 +0200175 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200176 osmo_gsup_encode(msg_out, &gsup_out);
177 return osmo_gsup_conn_send(conn, msg_out);
178}
179
Harald Weltee687be52016-05-03 18:49:27 +0200180/***********************************************************************
181 * LU Operation State / Structure
182 ***********************************************************************/
183
184static LLIST_HEAD(g_lu_ops);
185
Harald Weltee687be52016-05-03 18:49:27 +0200186/*! Receive Cancel Location Result from old VLR/SGSN */
187void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100188 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200189{
190 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
191 /* FIXME: Check for spoofing */
192
193 osmo_timer_del(&luop->timer);
194
195 /* FIXME */
196
197 lu_op_tx_insert_subscr_data(luop);
198}
199
Harald Weltee687be52016-05-03 18:49:27 +0200200/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
201static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
202 const struct osmo_gsup_message *gsup)
203{
204 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
205 /* FIXME: Check for spoofing */
206
207 osmo_timer_del(&luop->timer);
208
209 /* Subscriber_Present_HLR */
210 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
211
212 /* Send final ACK towards inquiring VLR/SGSN */
213 lu_op_tx_ack(luop);
214}
215
216/*! Receive GSUP message for given \ref lu_operation */
217void lu_op_rx_gsup(struct lu_operation *luop,
218 const struct osmo_gsup_message *gsup)
219{
220 switch (gsup->message_type) {
221 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
222 /* FIXME */
223 break;
224 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
225 lu_op_rx_insert_subscr_data_ack(luop, gsup);
226 break;
227 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
228 /* FIXME */
229 break;
230 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
231 lu_op_rx_cancel_old_ack(luop, gsup);
232 break;
233 default:
234 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
235 gsup->message_type);
236 break;
237 }
238}
239
Harald Weltee687be52016-05-03 18:49:27 +0200240/*! Receive Update Location Request, creates new \ref lu_operation */
241static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
242 const struct osmo_gsup_message *gsup)
243{
Maxea8b0d42017-02-14 16:53:04 +0100244 struct lu_operation *luop = lu_op_alloc_conn(conn);
245 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200246 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100247 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200248 }
249
Harald Weltee687be52016-05-03 18:49:27 +0200250 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100251
Stefan Sperling93c5b102018-04-10 19:26:14 +0200252 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_CS)
253 conn->supports_cs = true;
254 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
255 conn->supports_ps = true;
Harald Weltee687be52016-05-03 18:49:27 +0200256 luop->is_ps = true;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200257 } else {
258 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
259 * accordance with the GSUP spec in osmo-hlr's user manual (section
260 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
261 * a request, the PS Domain is assumed." */
262 conn->supports_ps = true;
Stefan Sperling1cb48922018-05-03 14:05:56 +0200263 luop->is_ps = true;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200264 }
Harald Weltee687be52016-05-03 18:49:27 +0200265 llist_add(&luop->list, &g_lu_ops);
266
267 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
268
269 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100270 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200271 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900272 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200273 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
274 return 0;
275 }
276
Harald Welte99909272016-05-05 18:24:15 +0200277 /* Check if subscriber is generally permitted on CS or PS
278 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100279 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200280 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
281 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100282 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200283 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
284 return 0;
285 }
286
287 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
288
289#if 0
290 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
291 if (luop->is_ps == false &&
292 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200293 lu_op_tx_cancel_old(luop);
294 } else if (luop->is_ps == true &&
295 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200296 lu_op_tx_cancel_old(luop);
297 } else
298#endif
299 {
300 /* TODO: Subscriber allowed to roam in PLMN? */
301 /* TODO: Update RoutingInfo */
302 /* TODO: Reset Flag MS Purged (cs/ps) */
303 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
304 lu_op_tx_insert_subscr_data(luop);
305 }
306 return 0;
307}
308
Harald Welteb18f0e02016-05-05 21:03:03 +0200309static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
310 const struct osmo_gsup_message *gsup)
311{
312 struct osmo_gsup_message gsup_reply = {0};
313 struct msgb *msg_out;
314 bool is_ps = false;
315 int rc;
316
317 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
318 is_ps ? "PS" : "CS");
319
320 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
321
322 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
323 is_ps = true;
324
325 /* FIXME: check if the VLR that sends the purge is the same that
326 * we have on record. Only update if yes */
327
328 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200329 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200330
Harald Welte3f2a9a22018-03-01 23:35:35 +0100331 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200332 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100333 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200334 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
335 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
336 } else {
337 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
338 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
339 }
340
341 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
342 osmo_gsup_encode(msg_out, &gsup_reply);
343 return osmo_gsup_conn_send(conn, msg_out);
344}
345
Harald Weltea1d3b042018-06-11 20:28:35 +0200346static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
347 enum osmo_gsup_message_type type_in, uint8_t err_cause)
348{
349 int type_err = osmo_gsup_get_err_msg_type(type_in);
350 struct osmo_gsup_message gsup_reply = {0};
351 struct msgb *msg_out;
352
353 if (type_err < 0) {
354 LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
355 osmo_gsup_message_type_name(type_in));
356 return type_err;
357 }
358
359 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
360 gsup_reply.message_type = type_err;
361 gsup_reply.cause = err_cause;
362 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
363 OSMO_ASSERT(msg_out);
364 osmo_gsup_encode(msg_out, &gsup_reply);
365 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
366 return osmo_gsup_conn_send(conn, msg_out);
367}
368
Harald Weltee72cf552016-04-28 07:18:49 +0200369static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
370{
371 static struct osmo_gsup_message gsup;
372 int rc;
373
Harald Weltee687be52016-05-03 18:49:27 +0200374 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200375 if (rc < 0) {
376 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
377 return rc;
378 }
379
Harald Weltea1d3b042018-06-11 20:28:35 +0200380 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
381 * digits is impossible. Even 5 digits is a highly theoretical case */
382 if (strlen(gsup.imsi) < 5)
383 return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
384
Harald Weltee72cf552016-04-28 07:18:49 +0200385 switch (gsup.message_type) {
386 /* requests sent to us */
387 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100388 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200389 break;
390 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200391 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200392 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200393 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
394 rx_purge_ms_req(conn, &gsup);
395 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200396 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100397 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
398 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
399 "for IMSI %s\n", gsup.imsi);
400 break;
401 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
402 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
403 gsup.imsi);
404 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200405 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200406 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200407 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
408 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
409 {
Maxea8b0d42017-02-14 16:53:04 +0100410 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
411 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200412 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100413 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
414 "unknown IMSI %s\n",
415 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200416 gsup.imsi);
417 break;
418 }
419 lu_op_rx_gsup(luop, &gsup);
420 }
Harald Weltee72cf552016-04-28 07:18:49 +0200421 break;
422 default:
Maxaa0fefd2017-02-16 12:25:22 +0100423 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
424 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200425 break;
426 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200427 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200428 return 0;
429}
430
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100431static void print_usage()
432{
433 printf("Usage: osmo-hlr\n");
434}
435
436static void print_help()
437{
438 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100439 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100440 printf(" -l --database db-name The database to use.\n");
441 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
442 printf(" -D --daemonize Fork the process into a background daemon.\n");
443 printf(" -s --disable-color Do not print ANSI colors in the log\n");
444 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
445 printf(" -e --log-level number Set a global loglevel.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100446 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100447}
448
449static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100450 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100451 const char *db_file;
452 bool daemonize;
453} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100454 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100455 .db_file = "hlr.db",
456 .daemonize = false,
457};
458
459static void handle_options(int argc, char **argv)
460{
461 while (1) {
462 int option_index = 0, c;
463 static struct option long_options[] = {
464 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100465 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100466 {"database", 1, 0, 'l'},
467 {"debug", 1, 0, 'd'},
468 {"daemonize", 0, 0, 'D'},
469 {"disable-color", 0, 0, 's'},
470 {"log-level", 1, 0, 'e'},
471 {"timestamp", 0, 0, 'T'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100472 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100473 {0, 0, 0, 0}
474 };
475
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100476 c = getopt_long(argc, argv, "hc:l:d:Dse:TV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100477 long_options, &option_index);
478 if (c == -1)
479 break;
480
481 switch (c) {
482 case 'h':
483 print_usage();
484 print_help();
485 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100486 case 'c':
487 cmdline_opts.config_file = optarg;
488 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100489 case 'l':
490 cmdline_opts.db_file = optarg;
491 break;
492 case 'd':
493 log_parse_category_mask(osmo_stderr_target, optarg);
494 break;
495 case 'D':
496 cmdline_opts.daemonize = 1;
497 break;
498 case 's':
499 log_set_use_color(osmo_stderr_target, 0);
500 break;
501 case 'e':
502 log_set_log_level(osmo_stderr_target, atoi(optarg));
503 break;
504 case 'T':
505 log_set_print_timestamp(osmo_stderr_target, 1);
506 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100507 case 'V':
508 print_version(1);
509 exit(0);
510 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100511 default:
512 /* catch unknown options *as well as* missing arguments. */
513 fprintf(stderr, "Error in command line options. Exiting.\n");
514 exit(-1);
515 break;
516 }
517 }
518}
519
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100520static void *hlr_ctx = NULL;
Harald Welteaabae9e2016-04-28 12:48:14 +0200521
522static void signal_hdlr(int signal)
523{
524 switch (signal) {
525 case SIGINT:
526 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700527 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200528 break;
529 case SIGUSR1:
530 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100531 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200532 break;
533 }
534}
Harald Weltee72cf552016-04-28 07:18:49 +0200535
Max372868b2017-03-02 12:12:00 +0100536static const char vlr_copyright[] =
537 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
538 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
539 "This is free software: you are free to change and redistribute it.\r\n"
540 "There is NO WARRANTY, to the extent permitted by law.\r\n";
541
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100542static struct vty_app_info vty_info = {
543 .name = "OsmoHLR",
544 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100545 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100546 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200547 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100548};
549
Harald Weltee72cf552016-04-28 07:18:49 +0200550int main(int argc, char **argv)
551{
Harald Weltee72cf552016-04-28 07:18:49 +0200552 int rc;
553
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700554 /* Track the use of talloc NULL memory contexts */
555 talloc_enable_null_tracking();
556
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100557 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
558 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100559 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200560
Maxd4bebbd2017-03-02 12:00:19 +0100561 g_hlr = talloc_zero(hlr_ctx, struct hlr);
562
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200563 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200564 if (rc < 0) {
565 fprintf(stderr, "Error initializing logging\n");
566 exit(1);
567 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100568
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100569 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100570 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100571 handle_options(argc, argv);
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200572 hlr_vty_init(g_hlr, &hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100573
574 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
575 if (rc < 0) {
576 LOGP(DMAIN, LOGL_FATAL,
577 "Failed to parse the config file: '%s'\n",
578 cmdline_opts.config_file);
579 return rc;
580 }
581
582 /* start telnet after reading config for vty_get_bind_addr() */
583 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
584 OSMO_VTY_PORT_HLR);
585 if (rc < 0)
586 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100587
Harald Weltee72cf552016-04-28 07:18:49 +0200588 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
589
590 rc = rand_init();
591 if (rc < 0) {
592 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
593 exit(1);
594 }
595
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100596 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true);
Maxd4bebbd2017-03-02 12:00:19 +0100597 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200598 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
599 exit(1);
600 }
601
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200602 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200603 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100604 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200605 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
606 exit(1);
607 }
608
Max372868b2017-03-02 12:12:00 +0100609 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200610 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100611
Harald Welteaabae9e2016-04-28 12:48:14 +0200612 osmo_init_ignore_signals();
613 signal(SIGINT, &signal_hdlr);
614 signal(SIGUSR1, &signal_hdlr);
615
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100616 if (cmdline_opts.daemonize) {
617 rc = osmo_daemonize();
618 if (rc < 0) {
619 perror("Error during daemonize");
620 exit(1);
621 }
622 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200623
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700624 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200625 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200626
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700627 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100628 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200629 log_fini();
630
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700631 /**
632 * Report the heap state of root context, then free,
633 * so both ASAN and Valgrind are happy...
634 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700635 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700636 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700637
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700638 /* FIXME: VTY code still uses NULL-context */
639 talloc_free(tall_vty_ctx);
640
641 /**
642 * Report the heap state of NULL context, then free,
643 * so both ASAN and Valgrind are happy...
644 */
645 talloc_report_full(NULL, stderr);
646 talloc_disable_null_tracking();
647
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700648 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200649}