blob: f631d523185c7a14321ae565da6e70cb85a7869f [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 Weltebb779392018-06-16 20:21:10 +020045#include "hlr_ussd.h"
Harald Weltee72cf552016-04-28 07:18:49 +020046
Harald Welted5807b82018-07-29 12:27:41 +020047struct hlr *g_hlr;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070048static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020049
Stefan Sperlingf1622522018-04-09 11:39:16 +020050/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
51 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020052 * \param[in] subscr A subscriber we have new data to send for.
53 */
54void
55osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
56{
Harald Welteb85f6042018-06-15 18:01:14 +020057 /* FIXME: the below code can only be re-enabled after we make sure that an ISD
58 * is only sent tot the currently serving VLR and/or SGSN (if there are any).
59 * We cannot blindly flood the entire PLMN with this, as it would create subscriber
60 * state in every VLR/SGSN out there, even those that have never seen the subscriber.
61 * See https://osmocom.org/issues/3154 for details. */
62#if 0
Stefan Sperlingf1622522018-04-09 11:39:16 +020063 struct osmo_gsup_conn *co;
64
65 if (g_hlr->gs == NULL)
66 return;
67
68 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020069 struct osmo_gsup_message gsup = { };
70 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
71 uint8_t apn[APN_MAXLEN];
72 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020073 uint8_t *peer;
74 int peer_len;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020075 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020076
Stefan Sperlingf83432c2018-05-03 14:26:59 +020077 if (co->supports_ps)
78 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
79 else if (co->supports_cs)
80 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
81 else {
82 /* We have not yet received a location update from this subscriber .*/
83 continue;
84 }
85
86 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
87 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Stefan Sperlingf1622522018-04-09 11:39:16 +020088 LOGP(DMAIN, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +020089 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +020090 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +020091 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
92 co && co->conn && co->conn->server? co->conn->server->port : 0);
93 continue;
94 }
Stefan Sperling93c5b102018-04-10 19:26:14 +020095
Stefan Sperling93c5b102018-04-10 19:26:14 +020096 /* Send ISD to MSC/SGSN */
97 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
98 if (msg_out == NULL) {
99 LOGP(DMAIN, LOGL_ERROR,
100 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
101 "for %s:%u\n", subscr->imsi,
102 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
103 co && co->conn && co->conn->server? co->conn->server->port : 0);
104 continue;
105 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200106 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200107
108 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
109 if (peer_len < 0) {
110 LOGP(DMAIN, LOGL_ERROR,
111 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
112 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
113 co && co->conn && co->conn->server? co->conn->server->port : 0);
114 continue;
115 }
116
Stefan Sperling93c5b102018-04-10 19:26:14 +0200117 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
118 LOGP(DMAIN, LOGL_ERROR,
119 "IMSI='%s': Cannot notify GSUP client; send operation failed "
120 "for %s:%u\n", subscr->imsi,
121 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
122 co && co->conn && co->conn->server? co->conn->server->port : 0);
123 continue;
124 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200125 }
Harald Welteb85f6042018-06-15 18:01:14 +0200126#endif
Stefan Sperlingf1622522018-04-09 11:39:16 +0200127}
128
Harald Weltee687be52016-05-03 18:49:27 +0200129/***********************************************************************
130 * Send Auth Info handling
131 ***********************************************************************/
132
Harald Weltee72cf552016-04-28 07:18:49 +0200133/* process an incoming SAI request */
134static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100135 const struct osmo_gsup_message *gsup,
136 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200137{
138 struct osmo_gsup_message gsup_out;
139 struct msgb *msg_out;
140 int rc;
141
142 /* initialize return message structure */
143 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200144 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
145
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100146 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
147 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200148 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200149 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100150 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200151 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100152 switch (rc) {
153 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100154 /* 0 means "0 tuples generated", which shouldn't happen.
155 * Treat the same as "no auth data". */
156 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100157 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
158 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
159 gsup->imsi);
160 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
161 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100162 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100163 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100164 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
165 break;
166 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100167 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100168 gsup_out.cause = GMM_CAUSE_NET_FAIL;
169 break;
170 }
Harald Welte15db8262016-05-05 16:50:39 +0200171 } else {
172 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
173 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200174 }
175
Harald Weltee687be52016-05-03 18:49:27 +0200176 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200177 osmo_gsup_encode(msg_out, &gsup_out);
178 return osmo_gsup_conn_send(conn, msg_out);
179}
180
Harald Weltee687be52016-05-03 18:49:27 +0200181/***********************************************************************
182 * LU Operation State / Structure
183 ***********************************************************************/
184
185static LLIST_HEAD(g_lu_ops);
186
Harald Weltee687be52016-05-03 18:49:27 +0200187/*! Receive Cancel Location Result from old VLR/SGSN */
188void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100189 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200190{
191 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
192 /* FIXME: Check for spoofing */
193
194 osmo_timer_del(&luop->timer);
195
196 /* FIXME */
197
198 lu_op_tx_insert_subscr_data(luop);
199}
200
Harald Weltee687be52016-05-03 18:49:27 +0200201/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
202static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
203 const struct osmo_gsup_message *gsup)
204{
205 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
206 /* FIXME: Check for spoofing */
207
208 osmo_timer_del(&luop->timer);
209
210 /* Subscriber_Present_HLR */
211 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
212
213 /* Send final ACK towards inquiring VLR/SGSN */
214 lu_op_tx_ack(luop);
215}
216
217/*! Receive GSUP message for given \ref lu_operation */
218void lu_op_rx_gsup(struct lu_operation *luop,
219 const struct osmo_gsup_message *gsup)
220{
221 switch (gsup->message_type) {
222 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
223 /* FIXME */
224 break;
225 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
226 lu_op_rx_insert_subscr_data_ack(luop, gsup);
227 break;
228 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
229 /* FIXME */
230 break;
231 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
232 lu_op_rx_cancel_old_ack(luop, gsup);
233 break;
234 default:
235 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
236 gsup->message_type);
237 break;
238 }
239}
240
Harald Weltee687be52016-05-03 18:49:27 +0200241/*! Receive Update Location Request, creates new \ref lu_operation */
242static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
243 const struct osmo_gsup_message *gsup)
244{
Maxea8b0d42017-02-14 16:53:04 +0100245 struct lu_operation *luop = lu_op_alloc_conn(conn);
246 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200247 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100248 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200249 }
250
Harald Weltee687be52016-05-03 18:49:27 +0200251 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100252
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200253 switch (gsup->cn_domain) {
254 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200255 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200256 break;
257 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200258 /* 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." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200262 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200263 conn->supports_ps = true;
Stefan Sperling1cb48922018-05-03 14:05:56 +0200264 luop->is_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200265 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200266 }
Harald Weltee687be52016-05-03 18:49:27 +0200267 llist_add(&luop->list, &g_lu_ops);
268
269 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
270
271 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100272 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200273 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900274 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200275 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
276 return 0;
277 }
278
Harald Welte99909272016-05-05 18:24:15 +0200279 /* Check if subscriber is generally permitted on CS or PS
280 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100281 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200282 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
283 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100284 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200285 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
286 return 0;
287 }
288
289 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
290
291#if 0
292 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
293 if (luop->is_ps == false &&
294 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200295 lu_op_tx_cancel_old(luop);
296 } else if (luop->is_ps == true &&
297 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200298 lu_op_tx_cancel_old(luop);
299 } else
300#endif
301 {
302 /* TODO: Subscriber allowed to roam in PLMN? */
303 /* TODO: Update RoutingInfo */
304 /* TODO: Reset Flag MS Purged (cs/ps) */
305 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
306 lu_op_tx_insert_subscr_data(luop);
307 }
308 return 0;
309}
310
Harald Welteb18f0e02016-05-05 21:03:03 +0200311static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
312 const struct osmo_gsup_message *gsup)
313{
314 struct osmo_gsup_message gsup_reply = {0};
315 struct msgb *msg_out;
316 bool is_ps = false;
317 int rc;
318
319 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
320 is_ps ? "PS" : "CS");
321
322 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
323
324 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
325 is_ps = true;
326
327 /* FIXME: check if the VLR that sends the purge is the same that
328 * we have on record. Only update if yes */
329
330 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200331 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200332
Harald Welte3f2a9a22018-03-01 23:35:35 +0100333 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200334 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100335 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200336 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
337 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
338 } else {
339 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
340 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
341 }
342
343 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
344 osmo_gsup_encode(msg_out, &gsup_reply);
345 return osmo_gsup_conn_send(conn, msg_out);
346}
347
Harald Weltea1d3b042018-06-11 20:28:35 +0200348static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
349 enum osmo_gsup_message_type type_in, uint8_t err_cause)
350{
351 int type_err = osmo_gsup_get_err_msg_type(type_in);
352 struct osmo_gsup_message gsup_reply = {0};
353 struct msgb *msg_out;
354
355 if (type_err < 0) {
356 LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
357 osmo_gsup_message_type_name(type_in));
358 return type_err;
359 }
360
361 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
362 gsup_reply.message_type = type_err;
363 gsup_reply.cause = err_cause;
364 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
365 OSMO_ASSERT(msg_out);
366 osmo_gsup_encode(msg_out, &gsup_reply);
367 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
368 return osmo_gsup_conn_send(conn, msg_out);
369}
370
Harald Weltee72cf552016-04-28 07:18:49 +0200371static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
372{
373 static struct osmo_gsup_message gsup;
374 int rc;
375
Harald Weltee687be52016-05-03 18:49:27 +0200376 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200377 if (rc < 0) {
378 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
379 return rc;
380 }
381
Harald Weltea1d3b042018-06-11 20:28:35 +0200382 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
383 * digits is impossible. Even 5 digits is a highly theoretical case */
384 if (strlen(gsup.imsi) < 5)
385 return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
386
Harald Weltee72cf552016-04-28 07:18:49 +0200387 switch (gsup.message_type) {
388 /* requests sent to us */
389 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100390 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200391 break;
392 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200393 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200394 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200395 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
396 rx_purge_ms_req(conn, &gsup);
397 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200398 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100399 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
400 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
401 "for IMSI %s\n", gsup.imsi);
402 break;
403 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
404 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
405 gsup.imsi);
406 break;
Harald Weltebb779392018-06-16 20:21:10 +0200407 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
408 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
409 rx_proc_ss_req(conn, &gsup);
410 break;
411 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
412 rx_proc_ss_error(conn, &gsup);
413 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200414 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200415 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200416 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
417 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
418 {
Maxea8b0d42017-02-14 16:53:04 +0100419 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
420 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200421 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100422 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
423 "unknown IMSI %s\n",
424 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200425 gsup.imsi);
426 break;
427 }
428 lu_op_rx_gsup(luop, &gsup);
429 }
Harald Weltee72cf552016-04-28 07:18:49 +0200430 break;
431 default:
Maxaa0fefd2017-02-16 12:25:22 +0100432 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
433 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200434 break;
435 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200436 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200437 return 0;
438}
439
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100440static void print_usage()
441{
442 printf("Usage: osmo-hlr\n");
443}
444
445static void print_help()
446{
447 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100448 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100449 printf(" -l --database db-name The database to use.\n");
450 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
451 printf(" -D --daemonize Fork the process into a background daemon.\n");
452 printf(" -s --disable-color Do not print ANSI colors in the log\n");
453 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
454 printf(" -e --log-level number Set a global loglevel.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100455 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100456}
457
458static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100459 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100460 const char *db_file;
461 bool daemonize;
462} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100463 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100464 .db_file = "hlr.db",
465 .daemonize = false,
466};
467
468static void handle_options(int argc, char **argv)
469{
470 while (1) {
471 int option_index = 0, c;
472 static struct option long_options[] = {
473 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100474 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100475 {"database", 1, 0, 'l'},
476 {"debug", 1, 0, 'd'},
477 {"daemonize", 0, 0, 'D'},
478 {"disable-color", 0, 0, 's'},
479 {"log-level", 1, 0, 'e'},
480 {"timestamp", 0, 0, 'T'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100481 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100482 {0, 0, 0, 0}
483 };
484
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100485 c = getopt_long(argc, argv, "hc:l:d:Dse:TV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100486 long_options, &option_index);
487 if (c == -1)
488 break;
489
490 switch (c) {
491 case 'h':
492 print_usage();
493 print_help();
494 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100495 case 'c':
496 cmdline_opts.config_file = optarg;
497 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100498 case 'l':
499 cmdline_opts.db_file = optarg;
500 break;
501 case 'd':
502 log_parse_category_mask(osmo_stderr_target, optarg);
503 break;
504 case 'D':
505 cmdline_opts.daemonize = 1;
506 break;
507 case 's':
508 log_set_use_color(osmo_stderr_target, 0);
509 break;
510 case 'e':
511 log_set_log_level(osmo_stderr_target, atoi(optarg));
512 break;
513 case 'T':
514 log_set_print_timestamp(osmo_stderr_target, 1);
515 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100516 case 'V':
517 print_version(1);
518 exit(0);
519 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100520 default:
521 /* catch unknown options *as well as* missing arguments. */
522 fprintf(stderr, "Error in command line options. Exiting.\n");
523 exit(-1);
524 break;
525 }
526 }
527}
528
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100529static void *hlr_ctx = NULL;
Harald Welteaabae9e2016-04-28 12:48:14 +0200530
531static void signal_hdlr(int signal)
532{
533 switch (signal) {
534 case SIGINT:
535 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700536 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200537 break;
538 case SIGUSR1:
539 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100540 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200541 break;
542 }
543}
Harald Weltee72cf552016-04-28 07:18:49 +0200544
Max372868b2017-03-02 12:12:00 +0100545static const char vlr_copyright[] =
546 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
547 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
548 "This is free software: you are free to change and redistribute it.\r\n"
549 "There is NO WARRANTY, to the extent permitted by law.\r\n";
550
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100551static struct vty_app_info vty_info = {
552 .name = "OsmoHLR",
553 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100554 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100555 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200556 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100557};
558
Harald Weltee72cf552016-04-28 07:18:49 +0200559int main(int argc, char **argv)
560{
Harald Weltee72cf552016-04-28 07:18:49 +0200561 int rc;
562
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700563 /* Track the use of talloc NULL memory contexts */
564 talloc_enable_null_tracking();
565
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100566 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
567 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100568 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200569
Maxd4bebbd2017-03-02 12:00:19 +0100570 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200571 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200572 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200573 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200574 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Maxd4bebbd2017-03-02 12:00:19 +0100575
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200576 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200577 if (rc < 0) {
578 fprintf(stderr, "Error initializing logging\n");
579 exit(1);
580 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100581
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100582 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100583 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100584 handle_options(argc, argv);
Harald Welted5807b82018-07-29 12:27:41 +0200585 hlr_vty_init(&hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100586
587 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
588 if (rc < 0) {
589 LOGP(DMAIN, LOGL_FATAL,
590 "Failed to parse the config file: '%s'\n",
591 cmdline_opts.config_file);
592 return rc;
593 }
594
595 /* start telnet after reading config for vty_get_bind_addr() */
596 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
597 OSMO_VTY_PORT_HLR);
598 if (rc < 0)
599 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100600
Harald Weltee72cf552016-04-28 07:18:49 +0200601 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
602
603 rc = rand_init();
604 if (rc < 0) {
605 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
606 exit(1);
607 }
608
Neels Hofmeyrd3814b92017-11-21 12:28:07 +0100609 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true);
Maxd4bebbd2017-03-02 12:00:19 +0100610 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200611 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
612 exit(1);
613 }
614
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200615 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200616 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100617 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200618 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
619 exit(1);
620 }
621
Max372868b2017-03-02 12:12:00 +0100622 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200623 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100624
Harald Welteaabae9e2016-04-28 12:48:14 +0200625 osmo_init_ignore_signals();
626 signal(SIGINT, &signal_hdlr);
627 signal(SIGUSR1, &signal_hdlr);
628
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100629 if (cmdline_opts.daemonize) {
630 rc = osmo_daemonize();
631 if (rc < 0) {
632 perror("Error during daemonize");
633 exit(1);
634 }
635 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200636
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700637 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200638 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200639
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700640 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100641 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200642 log_fini();
643
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700644 /**
645 * Report the heap state of root context, then free,
646 * so both ASAN and Valgrind are happy...
647 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700648 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700649 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700650
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700651 /* FIXME: VTY code still uses NULL-context */
652 talloc_free(tall_vty_ctx);
653
654 /**
655 * Report the heap state of NULL context, then free,
656 * so both ASAN and Valgrind are happy...
657 */
658 talloc_report_full(NULL, stderr);
659 talloc_disable_null_tracking();
660
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700661 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200662}