2016-05-03 16:51:18 +00:00
|
|
|
/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-04-28 10:48:14 +00:00
|
|
|
#include <signal.h>
|
2016-05-03 16:49:27 +00:00
|
|
|
#include <errno.h>
|
2017-02-14 15:53:04 +00:00
|
|
|
#include <stdbool.h>
|
2017-01-30 12:30:47 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/application.h>
|
|
|
|
#include <osmocom/gsm/gsup.h>
|
2017-01-30 22:30:26 +00:00
|
|
|
#include <osmocom/vty/vty.h>
|
|
|
|
#include <osmocom/vty/command.h>
|
|
|
|
#include <osmocom/vty/telnet_interface.h>
|
|
|
|
#include <osmocom/vty/ports.h>
|
2017-03-02 11:12:00 +00:00
|
|
|
#include <osmocom/ctrl/control_vty.h>
|
2018-05-04 14:02:44 +00:00
|
|
|
#include <osmocom/gsm/apn.h>
|
2016-04-28 05:18:49 +00:00
|
|
|
|
|
|
|
#include "db.h"
|
2017-03-02 11:00:19 +00:00
|
|
|
#include "hlr.h"
|
2017-03-02 11:12:00 +00:00
|
|
|
#include "ctrl.h"
|
2016-04-28 05:18:49 +00:00
|
|
|
#include "logging.h"
|
|
|
|
#include "gsup_server.h"
|
2016-05-03 16:49:27 +00:00
|
|
|
#include "gsup_router.h"
|
2016-04-28 05:18:49 +00:00
|
|
|
#include "rand.h"
|
2017-02-14 15:53:04 +00:00
|
|
|
#include "luop.h"
|
2017-01-30 22:30:26 +00:00
|
|
|
#include "hlr_vty.h"
|
2016-04-28 05:18:49 +00:00
|
|
|
|
2017-03-02 11:00:19 +00:00
|
|
|
static struct hlr *g_hlr;
|
2016-04-28 05:18:49 +00:00
|
|
|
|
2018-04-09 09:39:16 +00:00
|
|
|
/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
|
|
|
|
*
|
|
|
|
* \param[in] subscr A subscriber we have new data to send for.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
|
|
|
|
{
|
2018-06-15 16:01:14 +00:00
|
|
|
/* FIXME: the below code can only be re-enabled after we make sure that an ISD
|
|
|
|
* is only sent tot the currently serving VLR and/or SGSN (if there are any).
|
|
|
|
* We cannot blindly flood the entire PLMN with this, as it would create subscriber
|
|
|
|
* state in every VLR/SGSN out there, even those that have never seen the subscriber.
|
|
|
|
* See https://osmocom.org/issues/3154 for details. */
|
|
|
|
#if 0
|
2018-04-09 09:39:16 +00:00
|
|
|
struct osmo_gsup_conn *co;
|
|
|
|
|
|
|
|
if (g_hlr->gs == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
llist_for_each_entry(co, &g_hlr->gs->clients, list) {
|
2018-05-03 12:26:59 +00:00
|
|
|
struct osmo_gsup_message gsup = { };
|
|
|
|
uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
|
2018-05-04 14:02:44 +00:00
|
|
|
uint8_t apn[APN_MAXLEN];
|
2018-04-10 17:26:14 +00:00
|
|
|
struct msgb *msg_out;
|
2018-05-03 12:26:59 +00:00
|
|
|
uint8_t *peer;
|
|
|
|
int peer_len;
|
|
|
|
enum osmo_gsup_cn_domain cn_domain;
|
2018-04-10 17:26:14 +00:00
|
|
|
|
2018-05-03 12:26:59 +00:00
|
|
|
if (co->supports_ps)
|
|
|
|
cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
|
|
|
|
else if (co->supports_cs)
|
|
|
|
cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
|
|
|
|
else {
|
|
|
|
/* We have not yet received a location update from this subscriber .*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
|
|
|
|
sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
|
2018-04-09 09:39:16 +00:00
|
|
|
LOGP(DMAIN, LOGL_ERROR,
|
2018-05-03 12:26:59 +00:00
|
|
|
"IMSI='%s': Cannot notify GSUP client; could not create gsup message "
|
2018-04-10 17:26:14 +00:00
|
|
|
"for %s:%u\n", subscr->imsi,
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->addr : "unset",
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->port : 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send ISD to MSC/SGSN */
|
|
|
|
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
|
|
|
|
if (msg_out == NULL) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR,
|
|
|
|
"IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
|
|
|
|
"for %s:%u\n", subscr->imsi,
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->addr : "unset",
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->port : 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
osmo_gsup_encode(msg_out, &gsup);
|
2018-05-03 12:26:59 +00:00
|
|
|
|
|
|
|
peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
|
|
|
|
if (peer_len < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR,
|
|
|
|
"IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->addr : "unset",
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->port : 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:26:14 +00:00
|
|
|
if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR,
|
|
|
|
"IMSI='%s': Cannot notify GSUP client; send operation failed "
|
|
|
|
"for %s:%u\n", subscr->imsi,
|
2018-04-09 09:39:16 +00:00
|
|
|
co && co->conn && co->conn->server? co->conn->server->addr : "unset",
|
|
|
|
co && co->conn && co->conn->server? co->conn->server->port : 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 16:01:14 +00:00
|
|
|
#endif
|
2018-04-09 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 16:49:27 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Send Auth Info handling
|
|
|
|
***********************************************************************/
|
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
/* process an incoming SAI request */
|
|
|
|
static int rx_send_auth_info(struct osmo_gsup_conn *conn,
|
2017-03-02 11:00:19 +00:00
|
|
|
const struct osmo_gsup_message *gsup,
|
|
|
|
struct db_context *dbc)
|
2016-04-28 05:18:49 +00:00
|
|
|
{
|
|
|
|
struct osmo_gsup_message gsup_out;
|
|
|
|
struct msgb *msg_out;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* initialize return message structure */
|
|
|
|
memset(&gsup_out, 0, sizeof(gsup_out));
|
|
|
|
memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
|
|
|
|
|
2017-03-14 23:07:43 +00:00
|
|
|
rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
|
|
|
|
gsup_out.auth_vectors,
|
2016-04-28 05:18:49 +00:00
|
|
|
ARRAY_SIZE(gsup_out.auth_vectors),
|
2016-06-10 15:34:02 +00:00
|
|
|
gsup->rand, gsup->auts);
|
2017-11-22 19:38:19 +00:00
|
|
|
if (rc <= 0) {
|
2016-04-28 05:18:49 +00:00
|
|
|
gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
|
2017-11-22 19:38:19 +00:00
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
2017-11-23 14:25:30 +00:00
|
|
|
/* 0 means "0 tuples generated", which shouldn't happen.
|
|
|
|
* Treat the same as "no auth data". */
|
|
|
|
case -ENOKEY:
|
2017-11-23 14:31:12 +00:00
|
|
|
LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
|
|
|
|
" Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
|
|
|
|
gsup->imsi);
|
|
|
|
gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
|
|
|
|
break;
|
2017-11-22 19:39:59 +00:00
|
|
|
case -ENOENT:
|
2017-11-23 14:31:12 +00:00
|
|
|
LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
|
2017-11-22 19:38:19 +00:00
|
|
|
gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
|
|
|
|
break;
|
|
|
|
default:
|
2017-11-23 14:31:12 +00:00
|
|
|
LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
|
2017-11-22 19:38:19 +00:00
|
|
|
gsup_out.cause = GMM_CAUSE_NET_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-05 14:50:39 +00:00
|
|
|
} else {
|
|
|
|
gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
|
|
|
|
gsup_out.num_auth_vectors = rc;
|
2016-04-28 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 16:49:27 +00:00
|
|
|
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
|
2016-04-28 05:18:49 +00:00
|
|
|
osmo_gsup_encode(msg_out, &gsup_out);
|
|
|
|
return osmo_gsup_conn_send(conn, msg_out);
|
|
|
|
}
|
|
|
|
|
2016-05-03 16:49:27 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* LU Operation State / Structure
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
static LLIST_HEAD(g_lu_ops);
|
|
|
|
|
|
|
|
/*! Receive Cancel Location Result from old VLR/SGSN */
|
|
|
|
void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
|
2017-02-14 15:53:04 +00:00
|
|
|
const struct osmo_gsup_message *gsup)
|
2016-05-03 16:49:27 +00:00
|
|
|
{
|
|
|
|
OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
|
|
|
|
/* FIXME: Check for spoofing */
|
|
|
|
|
|
|
|
osmo_timer_del(&luop->timer);
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
|
|
|
|
lu_op_tx_insert_subscr_data(luop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
|
|
|
|
static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
|
|
|
|
const struct osmo_gsup_message *gsup)
|
|
|
|
{
|
|
|
|
OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
|
|
|
|
/* FIXME: Check for spoofing */
|
|
|
|
|
|
|
|
osmo_timer_del(&luop->timer);
|
|
|
|
|
|
|
|
/* Subscriber_Present_HLR */
|
|
|
|
/* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
|
|
|
|
|
|
|
|
/* Send final ACK towards inquiring VLR/SGSN */
|
|
|
|
lu_op_tx_ack(luop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Receive GSUP message for given \ref lu_operation */
|
|
|
|
void lu_op_rx_gsup(struct lu_operation *luop,
|
|
|
|
const struct osmo_gsup_message *gsup)
|
|
|
|
{
|
|
|
|
switch (gsup->message_type) {
|
|
|
|
case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
|
|
|
|
/* FIXME */
|
|
|
|
break;
|
|
|
|
case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
|
|
|
|
lu_op_rx_insert_subscr_data_ack(luop, gsup);
|
|
|
|
break;
|
|
|
|
case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
|
|
|
|
/* FIXME */
|
|
|
|
break;
|
|
|
|
case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
|
|
|
|
lu_op_rx_cancel_old_ack(luop, gsup);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
|
|
|
|
gsup->message_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Receive Update Location Request, creates new \ref lu_operation */
|
|
|
|
static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
|
|
|
|
const struct osmo_gsup_message *gsup)
|
|
|
|
{
|
2017-02-14 15:53:04 +00:00
|
|
|
struct lu_operation *luop = lu_op_alloc_conn(conn);
|
|
|
|
if (!luop) {
|
2016-05-03 16:49:27 +00:00
|
|
|
LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
|
2017-02-14 15:53:04 +00:00
|
|
|
return -EINVAL;
|
2016-05-03 16:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lu_op_statechg(luop, LU_S_LU_RECEIVED);
|
2017-02-14 15:53:04 +00:00
|
|
|
|
2018-04-10 17:26:14 +00:00
|
|
|
if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_CS)
|
|
|
|
conn->supports_cs = true;
|
|
|
|
if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
|
|
|
|
conn->supports_ps = true;
|
2016-05-03 16:49:27 +00:00
|
|
|
luop->is_ps = true;
|
2018-04-10 17:26:14 +00:00
|
|
|
} else {
|
|
|
|
/* The client didn't send a CN_DOMAIN IE; assume packet-switched in
|
|
|
|
* accordance with the GSUP spec in osmo-hlr's user manual (section
|
|
|
|
* 11.6.15 "CN Domain" says "if no CN Domain IE is present within
|
|
|
|
* a request, the PS Domain is assumed." */
|
|
|
|
conn->supports_ps = true;
|
2018-05-03 12:05:56 +00:00
|
|
|
luop->is_ps = true;
|
2018-04-10 17:26:14 +00:00
|
|
|
}
|
2016-05-03 16:49:27 +00:00
|
|
|
llist_add(&luop->list, &g_lu_ops);
|
|
|
|
|
|
|
|
/* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
|
|
|
|
|
|
|
|
/* check if subscriber is known at all */
|
2017-03-02 11:00:19 +00:00
|
|
|
if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
|
2016-05-03 16:49:27 +00:00
|
|
|
/* Send Error back: Subscriber Unknown in HLR */
|
2017-11-05 18:55:02 +00:00
|
|
|
osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
|
2016-05-03 16:49:27 +00:00
|
|
|
lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 16:24:15 +00:00
|
|
|
/* Check if subscriber is generally permitted on CS or PS
|
|
|
|
* service (as requested) */
|
2017-02-14 15:53:04 +00:00
|
|
|
if (!luop->is_ps && !luop->subscr.nam_cs) {
|
2016-05-03 16:49:27 +00:00
|
|
|
lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
|
|
|
|
return 0;
|
2017-02-14 15:53:04 +00:00
|
|
|
} else if (luop->is_ps && !luop->subscr.nam_ps) {
|
2016-05-03 16:49:27 +00:00
|
|
|
lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Set subscriber tracing = deactive in VLR/SGSN */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
|
|
|
|
if (luop->is_ps == false &&
|
|
|
|
strcmp(subscr->vlr_number, vlr_number)) {
|
|
|
|
lu_op_tx_cancel_old(luop);
|
|
|
|
} else if (luop->is_ps == true &&
|
|
|
|
strcmp(subscr->sgsn_number, sgsn_number)) {
|
|
|
|
lu_op_tx_cancel_old(luop);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* TODO: Subscriber allowed to roam in PLMN? */
|
|
|
|
/* TODO: Update RoutingInfo */
|
|
|
|
/* TODO: Reset Flag MS Purged (cs/ps) */
|
|
|
|
/* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
|
|
|
|
lu_op_tx_insert_subscr_data(luop);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:03:03 +00:00
|
|
|
static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
|
|
|
|
const struct osmo_gsup_message *gsup)
|
|
|
|
{
|
|
|
|
struct osmo_gsup_message gsup_reply = {0};
|
|
|
|
struct msgb *msg_out;
|
|
|
|
bool is_ps = false;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
|
|
|
|
is_ps ? "PS" : "CS");
|
|
|
|
|
|
|
|
memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
|
|
|
|
|
|
|
|
if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
|
|
|
|
is_ps = true;
|
|
|
|
|
|
|
|
/* FIXME: check if the VLR that sends the purge is the same that
|
|
|
|
* we have on record. Only update if yes */
|
|
|
|
|
|
|
|
/* Perform the actual update of the DB */
|
2017-10-09 15:48:51 +00:00
|
|
|
rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
|
2016-05-05 19:03:03 +00:00
|
|
|
|
2018-03-01 22:35:35 +00:00
|
|
|
if (rc == 0)
|
2016-05-05 19:03:03 +00:00
|
|
|
gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
|
2018-03-01 22:35:35 +00:00
|
|
|
else if (rc == -ENOENT) {
|
2016-05-05 19:03:03 +00:00
|
|
|
gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
|
|
|
|
gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
|
|
|
|
} else {
|
|
|
|
gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
|
|
|
|
gsup_reply.cause = GMM_CAUSE_NET_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
|
|
|
|
osmo_gsup_encode(msg_out, &gsup_reply);
|
|
|
|
return osmo_gsup_conn_send(conn, msg_out);
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:28:35 +00:00
|
|
|
static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
|
|
|
|
enum osmo_gsup_message_type type_in, uint8_t err_cause)
|
|
|
|
{
|
|
|
|
int type_err = osmo_gsup_get_err_msg_type(type_in);
|
|
|
|
struct osmo_gsup_message gsup_reply = {0};
|
|
|
|
struct msgb *msg_out;
|
|
|
|
|
|
|
|
if (type_err < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
|
|
|
|
osmo_gsup_message_type_name(type_in));
|
|
|
|
return type_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
|
|
|
|
gsup_reply.message_type = type_err;
|
|
|
|
gsup_reply.cause = err_cause;
|
|
|
|
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
|
|
|
|
OSMO_ASSERT(msg_out);
|
|
|
|
osmo_gsup_encode(msg_out, &gsup_reply);
|
|
|
|
LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
|
|
|
|
return osmo_gsup_conn_send(conn, msg_out);
|
|
|
|
}
|
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
|
|
|
|
{
|
|
|
|
static struct osmo_gsup_message gsup;
|
|
|
|
int rc;
|
|
|
|
|
2016-05-03 16:49:27 +00:00
|
|
|
rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
|
2016-04-28 05:18:49 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:28:35 +00:00
|
|
|
/* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
|
|
|
|
* digits is impossible. Even 5 digits is a highly theoretical case */
|
|
|
|
if (strlen(gsup.imsi) < 5)
|
|
|
|
return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
|
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
switch (gsup.message_type) {
|
|
|
|
/* requests sent to us */
|
|
|
|
case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
|
2017-03-02 11:00:19 +00:00
|
|
|
rx_send_auth_info(conn, &gsup, g_hlr->dbc);
|
2016-04-28 05:18:49 +00:00
|
|
|
break;
|
|
|
|
case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
|
2016-05-03 16:49:27 +00:00
|
|
|
rx_upd_loc_req(conn, &gsup);
|
2016-04-28 05:18:49 +00:00
|
|
|
break;
|
2016-05-05 19:03:03 +00:00
|
|
|
case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
|
|
|
|
rx_purge_ms_req(conn, &gsup);
|
|
|
|
break;
|
2016-04-28 05:18:49 +00:00
|
|
|
/* responses to requests sent by us */
|
2017-02-20 16:22:56 +00:00
|
|
|
case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
|
|
|
|
"for IMSI %s\n", gsup.imsi);
|
|
|
|
break;
|
|
|
|
case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
|
|
|
|
LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
|
|
|
|
gsup.imsi);
|
|
|
|
break;
|
2016-04-28 05:18:49 +00:00
|
|
|
case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
|
|
|
|
case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
|
2016-05-03 16:49:27 +00:00
|
|
|
case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
|
|
|
|
case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
|
|
|
|
{
|
2017-02-14 15:53:04 +00:00
|
|
|
struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
|
|
|
|
&g_lu_ops);
|
2016-05-03 16:49:27 +00:00
|
|
|
if (!luop) {
|
2017-02-16 11:25:22 +00:00
|
|
|
LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
|
|
|
|
"unknown IMSI %s\n",
|
|
|
|
osmo_gsup_message_type_name(gsup.message_type),
|
2016-05-03 16:49:27 +00:00
|
|
|
gsup.imsi);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lu_op_rx_gsup(luop, &gsup);
|
|
|
|
}
|
2016-04-28 05:18:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-02-16 11:25:22 +00:00
|
|
|
LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
|
|
|
|
osmo_gsup_message_type_name(gsup.message_type));
|
2016-04-28 05:18:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-28 10:48:39 +00:00
|
|
|
msgb_free(msg);
|
2016-04-28 05:18:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-30 12:30:47 +00:00
|
|
|
static void print_usage()
|
|
|
|
{
|
|
|
|
printf("Usage: osmo-hlr\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
|
|
|
printf(" -h --help This text.\n");
|
2017-01-30 22:30:26 +00:00
|
|
|
printf(" -c --config-file filename The config file to use.\n");
|
2017-01-30 12:30:47 +00:00
|
|
|
printf(" -l --database db-name The database to use.\n");
|
|
|
|
printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
|
|
|
|
printf(" -D --daemonize Fork the process into a background daemon.\n");
|
|
|
|
printf(" -s --disable-color Do not print ANSI colors in the log\n");
|
|
|
|
printf(" -T --timestamp Prefix every log line with a timestamp.\n");
|
|
|
|
printf(" -e --log-level number Set a global loglevel.\n");
|
2017-01-30 22:30:26 +00:00
|
|
|
printf(" -V --version Print the version of OsmoHLR.\n");
|
2017-01-30 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
2017-01-30 22:30:26 +00:00
|
|
|
const char *config_file;
|
2017-01-30 12:30:47 +00:00
|
|
|
const char *db_file;
|
|
|
|
bool daemonize;
|
|
|
|
} cmdline_opts = {
|
2017-01-30 22:30:26 +00:00
|
|
|
.config_file = "osmo-hlr.cfg",
|
2017-01-30 12:30:47 +00:00
|
|
|
.db_file = "hlr.db",
|
|
|
|
.daemonize = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
2017-01-30 22:30:26 +00:00
|
|
|
{"config-file", 1, 0, 'c'},
|
2017-01-30 12:30:47 +00:00
|
|
|
{"database", 1, 0, 'l'},
|
|
|
|
{"debug", 1, 0, 'd'},
|
|
|
|
{"daemonize", 0, 0, 'D'},
|
|
|
|
{"disable-color", 0, 0, 's'},
|
|
|
|
{"log-level", 1, 0, 'e'},
|
|
|
|
{"timestamp", 0, 0, 'T'},
|
2017-01-30 22:30:26 +00:00
|
|
|
{"version", 0, 0, 'V' },
|
2017-01-30 12:30:47 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2017-01-30 22:30:26 +00:00
|
|
|
c = getopt_long(argc, argv, "hc:l:d:Dse:TV",
|
2017-01-30 12:30:47 +00:00
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_usage();
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
2017-01-30 22:30:26 +00:00
|
|
|
case 'c':
|
|
|
|
cmdline_opts.config_file = optarg;
|
|
|
|
break;
|
2017-01-30 12:30:47 +00:00
|
|
|
case 'l':
|
|
|
|
cmdline_opts.db_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
log_parse_category_mask(osmo_stderr_target, optarg);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
cmdline_opts.daemonize = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
log_set_use_color(osmo_stderr_target, 0);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
log_set_log_level(osmo_stderr_target, atoi(optarg));
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
log_set_print_timestamp(osmo_stderr_target, 1);
|
|
|
|
break;
|
2017-01-30 22:30:26 +00:00
|
|
|
case 'V':
|
|
|
|
print_version(1);
|
|
|
|
exit(0);
|
|
|
|
break;
|
2017-01-30 12:30:47 +00:00
|
|
|
default:
|
|
|
|
/* catch unknown options *as well as* missing arguments. */
|
|
|
|
fprintf(stderr, "Error in command line options. Exiting.\n");
|
|
|
|
exit(-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 12:18:23 +00:00
|
|
|
static void *hlr_ctx = NULL;
|
2016-04-28 10:48:14 +00:00
|
|
|
|
|
|
|
static void signal_hdlr(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGINT:
|
|
|
|
LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
|
2017-03-02 11:00:19 +00:00
|
|
|
osmo_gsup_server_destroy(g_hlr->gs);
|
|
|
|
db_close(g_hlr->dbc);
|
2016-04-28 10:48:14 +00:00
|
|
|
log_fini();
|
2017-01-30 12:18:23 +00:00
|
|
|
talloc_report_full(hlr_ctx, stderr);
|
2016-04-28 10:48:14 +00:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case SIGUSR1:
|
|
|
|
LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
|
2017-01-30 12:18:23 +00:00
|
|
|
talloc_report_full(hlr_ctx, stderr);
|
2016-04-28 10:48:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 05:18:49 +00:00
|
|
|
|
2017-03-02 11:12:00 +00:00
|
|
|
static const char vlr_copyright[] =
|
|
|
|
"Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
|
|
|
|
"License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
|
|
|
|
"This is free software: you are free to change and redistribute it.\r\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\r\n";
|
|
|
|
|
2017-01-30 22:30:26 +00:00
|
|
|
static struct vty_app_info vty_info = {
|
|
|
|
.name = "OsmoHLR",
|
|
|
|
.version = PACKAGE_VERSION,
|
2017-03-02 11:12:00 +00:00
|
|
|
.copyright = vlr_copyright,
|
2017-01-30 22:30:26 +00:00
|
|
|
.is_config_node = hlr_vty_is_config_node,
|
2017-05-31 11:19:22 +00:00
|
|
|
.go_parent_cb = hlr_vty_go_parent,
|
2017-01-30 22:30:26 +00:00
|
|
|
};
|
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-01-30 12:18:23 +00:00
|
|
|
hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
|
|
|
|
msgb_talloc_ctx_init(hlr_ctx, 0);
|
2018-02-13 23:52:05 +00:00
|
|
|
vty_info.tall_ctx = hlr_ctx;
|
2016-04-28 10:48:14 +00:00
|
|
|
|
2017-03-02 11:00:19 +00:00
|
|
|
g_hlr = talloc_zero(hlr_ctx, struct hlr);
|
|
|
|
|
2018-04-17 13:07:06 +00:00
|
|
|
rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
|
2016-04-28 05:18:49 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Error initializing logging\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-01-30 12:30:47 +00:00
|
|
|
|
2017-01-30 22:30:26 +00:00
|
|
|
vty_init(&vty_info);
|
2017-03-02 11:12:00 +00:00
|
|
|
ctrl_vty_init(hlr_ctx);
|
2017-01-30 12:30:47 +00:00
|
|
|
handle_options(argc, argv);
|
2017-05-31 11:19:22 +00:00
|
|
|
hlr_vty_init(g_hlr, &hlr_log_info);
|
2017-01-30 22:30:26 +00:00
|
|
|
|
|
|
|
rc = vty_read_config_file(cmdline_opts.config_file, NULL);
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_FATAL,
|
|
|
|
"Failed to parse the config file: '%s'\n",
|
|
|
|
cmdline_opts.config_file);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start telnet after reading config for vty_get_bind_addr() */
|
|
|
|
rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
|
|
|
|
OSMO_VTY_PORT_HLR);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2017-01-30 12:30:47 +00:00
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
|
|
|
|
|
|
|
|
rc = rand_init();
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:28:07 +00:00
|
|
|
g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true);
|
2017-03-02 11:00:19 +00:00
|
|
|
if (!g_hlr->dbc) {
|
2016-04-28 05:18:49 +00:00
|
|
|
LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-07-21 14:00:32 +00:00
|
|
|
g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
|
2018-06-16 15:07:28 +00:00
|
|
|
read_cb, &g_lu_ops, g_hlr);
|
2017-03-02 11:00:19 +00:00
|
|
|
if (!g_hlr->gs) {
|
2016-04-28 05:18:49 +00:00
|
|
|
LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:12:00 +00:00
|
|
|
g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
|
2017-10-24 15:23:04 +00:00
|
|
|
g_hlr->ctrl = hlr_controlif_setup(g_hlr);
|
2017-03-02 11:12:00 +00:00
|
|
|
|
2016-04-28 10:48:14 +00:00
|
|
|
osmo_init_ignore_signals();
|
|
|
|
signal(SIGINT, &signal_hdlr);
|
|
|
|
signal(SIGUSR1, &signal_hdlr);
|
|
|
|
|
2017-01-30 12:30:47 +00:00
|
|
|
if (cmdline_opts.daemonize) {
|
|
|
|
rc = osmo_daemonize();
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error during daemonize");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 10:48:14 +00:00
|
|
|
|
2016-04-28 05:18:49 +00:00
|
|
|
while (1) {
|
|
|
|
osmo_select_main(0);
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:00:19 +00:00
|
|
|
db_close(g_hlr->dbc);
|
2016-04-28 05:18:49 +00:00
|
|
|
|
|
|
|
log_fini();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|