9
0
Fork 0

remove osmo-gsup-hlr

remove osmo-gsup-hlr, which was moving to its own osmo-hlr.git on
November 11, 2016.
This commit is contained in:
Harald Welte 2016-12-21 11:23:36 +01:00
parent 8076c87c6b
commit d54de7d353
22 changed files with 0 additions and 2107 deletions

View File

@ -1,4 +0,0 @@
*.o
*.db
src/hlr
src/db_test

View File

@ -1,70 +0,0 @@
--modelled roughly after TS 23.008 version 13.3.0
CREATE TABLE subscriber (
id INTEGER PRIMARY KEY,
-- Chapter 2.1.1.1
imsi VARCHAR(15) NOT NULL,
-- Chapter 2.1.2
msisdn VARCHAR(15),
-- Chapter 2.2.3: Most recent / current IMEI
imeisv VARCHAR,
-- Chapter 2.4.5
vlr_number VARCHAR(15),
-- Chapter 2.4.6
hlr_number VARCHAR(15),
-- Chapter 2.4.8.1
sgsn_number VARCHAR(15),
-- Chapter 2.13.10
sgsn_address VARCHAR,
-- Chapter 2.4.8.2
ggsn_number VARCHAR(15),
-- Chapter 2.4.9.2
gmlc_number VARCHAR(15),
-- Chapter 2.4.23
smsc_number VARCHAR(15),
-- Chapter 2.4.24
periodic_lu_tmr INTEGER,
-- Chapter 2.13.115
periodic_rau_tau_tmr INTEGER,
-- Chapter 2.1.1.2: network access mode
nam_cs BOOLEAN NOT NULL DEFAULT 1,
nam_ps BOOLEAN NOT NULL DEFAULT 1,
-- Chapter 2.1.8
lmsi INTEGER,
-- The below purged flags might not even be stored non-volatile,
-- refer to TS 23.012 Chapter 3.6.1.4
-- Chapter 2.7.5
ms_purged_cs BOOLEAN NOT NULL DEFAULT 0,
-- Chapter 2.7.6
ms_purged_ps BOOLEAN NOT NULL DEFAULT 0
);
CREATE TABLE subscriber_apn (
subscriber_id INTEGER, -- subscriber.id
apn VARCHAR(256) NOT NULL
);
-- Chapter 2.1.3
CREATE TABLE subscriber_multi_msisdn (
subscriber_id INTEGER, -- subscriber.id
msisdn VARCHAR(15) NOT NULL
);
CREATE TABLE auc_2g (
subscriber_id INTEGER PRIMARY KEY, -- subscriber.id
algo_id_2g INTEGER NOT NULL,
ki VARCHAR NOT NULL
);
CREATE TABLE auc_3g (
subscriber_id INTEGER PRIMARY KEY, -- subscrbier.id
algo_id_3g INTEGER NOT NULL,
k INTEGER NOT NULL,
op VARCHAR,
opc VARCHAR,
sqn INTEGER
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_subscr_imsi ON subscriber (imsi);
-- SELECT algo_id_2g, ki, algo_id_3g, k, op, opc, sqn FROM subscriber LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id WHERE imsi = ?

View File

@ -1,13 +0,0 @@
-- 2G only subscriber
INSERT INTO subscriber (id, imsi) VALUES (1, '901990000000001');
INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki) VALUES (1, 1, '000102030405060708090a0b0c0d0e0f');
-- 3G only subscriber
INSERT INTO subscriber (id, imsi) VALUES (2, '901990000000002');
INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, sqn) VALUES (2, 5, '000102030405060708090a0b0c0d0e0f', '101112131415161718191a1b1c1d1e1f', 0);
-- 2G + 3G subscriber
INSERT INTO subscriber (id, imsi) VALUES (3, '901990000000003');
INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki) VALUES (3, 1, '000102030405060708090a0b0c0d0e0f');
INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, sqn) VALUES (3, 5, '000102030405060708090a0b0c0d0e0f', '101112131415161718191a1b1c1d1e1f', 0);

View File

@ -1,18 +0,0 @@
LDFLAGS += -losmocore -losmogsm -losmoabis -lsqlite3 -ltalloc
CFLAGS += -g -Wall
OBJS = auc.o db.o db_auc.o db_hlr.o logging.o
all: db_test hlr
db_test: db_test.o rand_fake.o $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
hlr: hlr.o gsup_server.o gsup_router.o rand_urandom.o $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $^
clean:
rm -f *.o db_test

View File

@ -1,94 +0,0 @@
/* (C) 2015 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/>.
*
*/
#include <string.h>
#include <osmocom/core/utils.h>
#include <osmocom/crypt/auth.h>
#include "logging.h"
#include "rand.h"
/* compute given number of vectors using either aud2g or aud2g or a combination
* of both. Handles re-synchrnization if rand_auts and auts are set */
int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
struct osmo_sub_auth_data *aud2g,
struct osmo_sub_auth_data *aud3g,
const uint8_t *rand_auts, const uint8_t *auts)
{
unsigned int i;
uint8_t rand[16];
int rc;
if (aud2g->algo == OSMO_AUTH_ALG_NONE)
aud2g = NULL;
if (aud3g->algo == OSMO_AUTH_ALG_NONE)
aud3g = NULL;
if (!aud2g && !aud3g)
return -1;
/* compute quintuples */
for (i = 0; i < num_vec; i++) {
rc = rand_get(rand, sizeof(rand));
if (rc != sizeof(rand)) {
LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
"bytes: rc=%d\n", sizeof(rand), rc);
goto out;
}
if (aud2g && !aud3g) {
/* 2G only case: output directly to vec */
rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
if (rc < 0) {
LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
"generation: %d\n", rc);
goto out;
}
} else if (aud3g) {
/* 3G or 3G + 2G case */
if (rand_auts && auts)
rc = osmo_auth_gen_vec_auts(vec+i, aud3g,
rand_auts,
auts, rand);
else
rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
if (rc < 0) {
LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
"generation: %d\n", rc);
goto out;
}
}
if (aud2g && aud3g) {
/* separate 2G + 3G case: patch 2G into 3G */
struct osmo_auth_vector vtmp;
rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
if (rc < 0) {
LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
"generation: %d\n", rc);
goto out;
}
memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
}
}
out:
return i;
}

View File

@ -1,8 +0,0 @@
#pragma once
#include <osmocom/crypt/auth.h>
int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
struct osmo_sub_auth_data *aud2g,
struct osmo_sub_auth_data *aud3g,
const uint8_t *rand_auts, const uint8_t *auts);

View File

@ -1,130 +0,0 @@
/* (C) 2015 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/>.
*
*/
#include <osmocom/core/utils.h>
#include <sqlite3.h>
#include "logging.h"
#include "db.h"
static const char *stmt_sql[] = {
[SEL_BY_IMSI] = "SELECT id,imsi,msisdn,vlr_number,sgsn_number,sgsn_address,periodic_lu_tmr,periodic_rau_tau_tmr,nam_cs,nam_ps,lmsi,ms_purged_cs,ms_purged_ps FROM subscriber WHERE imsi = ?",
[UPD_VLR_BY_ID] = "UPDATE subscriber SET vlr_number = ? WHERE id = ?",
[UPD_SGSN_BY_ID] = "UPDATE subscriber SET sgsn_number = ? WHERE id = ?",
[AUC_BY_IMSI] = "SELECT id, algo_id_2g, ki, algo_id_3g, k, op, opc, sqn FROM subscriber LEFT JOIN auc_2g ON auc_2g.subscriber_id = subscriber.id LEFT JOIN auc_3g ON auc_3g.subscriber_id = subscriber.id WHERE imsi = ?",
[AUC_UPD_SQN] = "UPDATE auc_3g SET sqn = ? WHERE subscriber_id = ?",
[UPD_PURGE_CS_BY_IMSI] = "UPDATE subscriber SET ms_purged_cs=1 WHERE imsi = ?",
[UPD_PURGE_PS_BY_IMSI] = "UPDATE subscriber SET ms_purged_ps=1 WHERE imsi = ?",
};
static void sql3_error_log_cb(void *arg, int err_code, const char *msg)
{
LOGP(DDB, LOGL_ERROR, "(%d) %s\n", err_code, msg);
}
static void sql3_sql_log_cb(void *arg, sqlite3 *s3, const char *stmt, int type)
{
switch (type) {
case 0:
LOGP(DDB, LOGL_DEBUG, "Opened database\n");
break;
case 1:
LOGP(DDB, LOGL_DEBUG, stmt);
break;
case 2:
LOGP(DDB, LOGL_DEBUG, "Closed database\n");
break;
default:
LOGP(DDB, LOGL_DEBUG, "Unknown %d\n", type);
break;
}
}
void db_close(struct db_context *dbc)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
/* it is ok to call finalize on NULL */
sqlite3_finalize(dbc->stmt[i]);
}
sqlite3_close(dbc->db);
talloc_free(dbc);
}
struct db_context *db_open(void *ctx, const char *fname)
{
struct db_context *dbc = talloc_zero(ctx, struct db_context);
unsigned int i;
int rc;
LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
dbc->fname = talloc_strdup(dbc, fname);
for (i = 0; i < 0xfffff; i++) {
const char *o = sqlite3_compileoption_get(i);
if (!o)
break;
LOGP(DDB, LOGL_DEBUG, "SQlite3 compiled with '%s'\n", o);
}
rc = sqlite3_config(SQLITE_CONFIG_LOG, sql3_error_log_cb, NULL);
if (rc != SQLITE_OK)
LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 error log callback\n");
rc = sqlite3_config(SQLITE_CONFIG_SQLLOG, sql3_sql_log_cb, NULL);
if (rc != SQLITE_OK)
LOGP(DDB, LOGL_NOTICE, "Unable to set SQlite3 SQL statement log callback\n");
rc = sqlite3_open(dbc->fname, &dbc->db);
if (rc != SQLITE_OK) {
LOGP(DDB, LOGL_ERROR, "Unable to open DB; rc = %d\n", rc);
talloc_free(dbc);
return NULL;
}
/* enable extended result codes */
rc = sqlite3_extended_result_codes(dbc->db, 1);
if (rc != SQLITE_OK)
LOGP(DDB, LOGL_ERROR, "Unable to enable SQlite3 extended result codes\n");
char *err_msg;
rc = sqlite3_exec(dbc->db, "PRAGMA journal_mode=WAL; PRAGMA synchonous = NORMAL;", 0, 0, &err_msg);
if (rc != SQLITE_OK)
LOGP(DDB, LOGL_ERROR, "Unable to set Write-Ahead Logging: %s\n",
err_msg);
/* prepare all SQL statements */
for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
rc = sqlite3_prepare_v2(dbc->db, stmt_sql[i], -1,
&dbc->stmt[i], NULL);
if (rc != SQLITE_OK) {
LOGP(DDB, LOGL_ERROR, "Unable to prepare SQL statement '%s'\n", stmt_sql[i]);
goto out_free;
}
}
return dbc;
out_free:
db_close(dbc);
return NULL;
}

View File

@ -1,78 +0,0 @@
#pragma once
#include <stdbool.h>
#include <sqlite3.h>
enum stmt_idx {
SEL_BY_IMSI = 0,
UPD_VLR_BY_ID = 1,
UPD_SGSN_BY_ID = 2,
AUC_BY_IMSI = 3,
AUC_UPD_SQN = 4,
UPD_PURGE_CS_BY_IMSI,
UPD_PURGE_PS_BY_IMSI,
_NUM_STMT
};
struct db_context {
char *fname;
sqlite3 *db;
sqlite3_stmt *stmt[_NUM_STMT];
};
void db_close(struct db_context *dbc);
struct db_context *db_open(void *ctx, const char *fname);
#include <osmocom/crypt/auth.h>
/* obtain the authentication data for a given imsi */
int db_get_auth_data(struct db_context *dbc, const char *imsi,
struct osmo_sub_auth_data *aud2g,
struct osmo_sub_auth_data *aud3g,
uint64_t *suscr_id);
int db_update_sqn(struct db_context *dbc, uint64_t id,
uint64_t new_sqn);
int db_get_auc(struct db_context *dbc, const char *imsi,
struct osmo_auth_vector *vec, unsigned int num_vec,
const uint8_t *rand_auts, const uint8_t *auts);
#include <osmocom/core/linuxlist.h>
#include <osmocom/gsm/protocol/gsm_23_003.h>
/* TODO: Get this from somewhere? */
#define GT_MAX_DIGITS 15
struct hlr_subscriber {
struct llist_head list;
uint64_t id;
char imsi[GSM23003_IMSI_MAX_DIGITS+1];
char msisdn[GT_MAX_DIGITS+1];
/* imeisv? */
char vlr_number[GT_MAX_DIGITS+1];
char sgsn_number[GT_MAX_DIGITS+1];
char sgsn_address[GT_MAX_DIGITS+1];
/* ggsn number + address */
/* gmlc number */
/* smsc number */
uint32_t periodic_lu_timer;
uint32_t periodic_rau_tau_timer;
bool nam_cs;
bool nam_ps;
uint32_t lmsi;
bool ms_purged_cs;
bool ms_purged_ps;
};
int db_subscr_get(struct db_context *dbc, const char *imsi,
struct hlr_subscriber *subscr);
int db_subscr_lu(struct db_context *dbc,
const struct hlr_subscriber *subscr,
const char *vlr_or_sgsn_number,
bool lu_is_ps);
int db_subscr_purge(struct db_context *dbc,
const char *imsi, bool is_ps);

View File

@ -1,225 +0,0 @@
/* (C) 2015 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/>.
*
*/
#include <string.h>
#include <osmocom/core/utils.h>
#include <osmocom/crypt/auth.h>
#include <sqlite3.h>
#include "logging.h"
#include "db.h"
#include "auc.h"
#include "rand.h"
#define LOGAUC(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args)
/* update the SQN for a given subscriber ID */
int db_update_sqn(struct db_context *dbc, uint64_t id,
uint64_t new_sqn)
{
sqlite3_stmt *stmt = dbc->stmt[AUC_UPD_SQN];
int rc;
/* bind new SQN and subscriber ID */
rc = sqlite3_bind_int64(stmt, 1, new_sqn);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding SQN: %d\n", rc);
return -1;
}
rc = sqlite3_bind_int64(stmt, 2, id);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding Subscrber ID: %d\n", rc);
return -1;
}
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
return -2;
}
/* remove bindings and reset statement to be re-executed */
rc = sqlite3_clear_bindings(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
}
rc = sqlite3_reset(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
}
return 0;
}
/* obtain the authentication data for a given imsi
* returns -1 in case of error, 0 for unknown IMSI, 1 for success */
int db_get_auth_data(struct db_context *dbc, const char *imsi,
struct osmo_sub_auth_data *aud2g,
struct osmo_sub_auth_data *aud3g,
uint64_t *subscr_id)
{
sqlite3_stmt *stmt = dbc->stmt[AUC_BY_IMSI];
int ret = 0;
int rc;
memset(aud2g, 0, sizeof(*aud2g));
memset(aud3g, 0, sizeof(*aud3g));
/* bind the IMSI value */
rc = sqlite3_bind_text(stmt, 1, imsi, -1,
SQLITE_STATIC);
if (rc != SQLITE_OK) {
LOGAUC(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc);
ret = -1;
goto out;
}
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc == SQLITE_DONE) {
LOGAUC(imsi, LOGL_INFO, "Unknown\n");
ret = 0;
goto out;
} else if (rc != SQLITE_ROW) {
LOGAUC(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
ret = -1;
goto out;
}
/* as an optimization, we retrieve the subscriber ID, to ensure we can
* update the SQN later without having to go back via a JOIN with the
* subscriber table. */
if (subscr_id)
*subscr_id = sqlite3_column_int64(stmt, 0);
/* obtain result values using sqlite3_column_*() */
if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) {
/* we do have some 2G authentication data */
const uint8_t *ki;
aud2g->algo = sqlite3_column_int(stmt, 1);
ki = sqlite3_column_text(stmt, 2);
#if 0
if (sqlite3_column_bytes(stmt, 2) != sizeof(aud2g->u.gsm.ki)) {
LOGAUC(imsi, LOGL_ERROR, "Error reading Ki: %d\n", rc);
goto end_2g;
}
#endif
osmo_hexparse(ki, &aud2g->u.gsm.ki, sizeof(aud2g->u.gsm.ki));
aud2g->type = OSMO_AUTH_TYPE_GSM;
} else
LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n");
//end_2g:
if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
/* we do have some 3G authentication data */
const uint8_t *k, *op, *opc;
aud3g->algo = sqlite3_column_int(stmt, 3);
k = sqlite3_column_text(stmt, 4);
if (!k) {
LOGAUC(imsi, LOGL_ERROR, "Error reading K: %d\n", rc);
goto out;
}
osmo_hexparse(k, &aud3g->u.umts.k, sizeof(aud3g->u.umts.k));
/* UMTS Subscribers can have either OP or OPC */
op = sqlite3_column_text(stmt, 5);
if (!op) {
opc = sqlite3_column_text(stmt, 6);
if (!opc) {
LOGAUC(imsi, LOGL_ERROR, "Error reading OPC: %d\n", rc);
goto out;
}
osmo_hexparse(opc, &aud3g->u.umts.opc,
sizeof(aud3g->u.umts.opc));
aud3g->u.umts.opc_is_op = 0;
} else {
osmo_hexparse(op, &aud3g->u.umts.opc,
sizeof(aud3g->u.umts.opc));
aud3g->u.umts.opc_is_op = 1;
}
aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7);
/* FIXME: amf? */
aud3g->type = OSMO_AUTH_TYPE_UMTS;
} else
LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n");
if (aud2g->type == 0 && aud3g->type == 0)
ret = -1;
else
ret = 1;
out:
/* remove bindings and reset statement to be re-executed */
rc = sqlite3_clear_bindings(stmt);
if (rc != SQLITE_OK) {
LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_clear_bindings(): %d\n", rc);
}
rc = sqlite3_reset(stmt);
if (rc != SQLITE_OK) {
LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_reset(): %d\n", rc);
}
return ret;
}
/* return -1 in case of error, 0 for unknown imsi, positive for number
* of vectors generated */
int db_get_auc(struct db_context *dbc, const char *imsi,
struct osmo_auth_vector *vec, unsigned int num_vec,
const uint8_t *rand_auts, const uint8_t *auts)
{
struct osmo_sub_auth_data aud2g, aud3g;
uint64_t subscr_id;
int ret = 0;
int rc;
rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id);
if (rc <= 0)
return rc;
LOGAUC(imsi, LOGL_INFO, "Calling to generate %u vectors\n", num_vec);
rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts);
if (rc < 0) {
num_vec = 0;
ret = -1;
} else {
num_vec = rc;
ret = num_vec;
}
LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec);
/* Update SQN in database, as needed */
if (aud3g.algo) {
LOGAUC(imsi, LOGL_DEBUG, "Updating SQN in DB\n");
rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn);
/* don't tell caller we generated any triplets in case of
* update error */
if (rc < 0) {
LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc);
num_vec = 0;
ret = -1;
}
}
return ret;
}

View File

@ -1,176 +0,0 @@
/* (C) 2015 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/>.
*
*/
#include <string.h>
#include <osmocom/core/utils.h>
#include <osmocom/crypt/auth.h>
#include <sqlite3.h>
#include "logging.h"
#include "db.h"
#define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args)
#define SL3_TXT(x, stmt, idx) do { \
const char *_txt = (const char *) sqlite3_column_text(stmt, idx); \
if (_txt) \
strncpy(x, _txt, sizeof(x)); \
x[sizeof(x)-1] = '\0'; \
} while (0)
int db_subscr_get(struct db_context *dbc, const char *imsi,
struct hlr_subscriber *subscr)
{
sqlite3_stmt *stmt = dbc->stmt[SEL_BY_IMSI];
int rc, ret = 0;
rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) {
LOGHLR(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc);
return -1;
}
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) {
LOGHLR(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
ret = -2;
goto out;
}
/* obtain the various columns */
subscr->id = sqlite3_column_int64(stmt, 0);
SL3_TXT(subscr->imsi, stmt, 1);
SL3_TXT(subscr->msisdn, stmt, 2);
/* FIXME: These should all be BLOBs as they might contain NUL */
SL3_TXT(subscr->vlr_number, stmt, 3);
SL3_TXT(subscr->sgsn_number, stmt, 4);
SL3_TXT(subscr->sgsn_address, stmt, 5);
subscr->periodic_lu_timer = sqlite3_column_int(stmt, 6);
subscr->periodic_rau_tau_timer = sqlite3_column_int(stmt, 7);
subscr->nam_cs = sqlite3_column_int(stmt, 8);
subscr->nam_ps = sqlite3_column_int(stmt, 9);
subscr->lmsi = sqlite3_column_int(stmt, 10);
subscr->ms_purged_cs = sqlite3_column_int(stmt, 11);
subscr->ms_purged_ps = sqlite3_column_int(stmt, 12);
out:
/* remove bindings and reset statement to be re-executed */
rc = sqlite3_clear_bindings(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
}
rc = sqlite3_reset(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
}
return ret;
}
int db_subscr_lu(struct db_context *dbc,
const struct hlr_subscriber *subscr,
const char *vlr_or_sgsn_number, bool lu_is_ps)
{
sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID];
const char *txt;
int rc, ret = 0;
if (lu_is_ps) {
stmt = dbc->stmt[UPD_SGSN_BY_ID];
txt = subscr->sgsn_number;
} else {
stmt = dbc->stmt[UPD_VLR_BY_ID];
txt = subscr->vlr_number;
}
rc = sqlite3_bind_int64(stmt, 1, subscr->id);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding ID: %d\n", rc);
return -1;
}
rc = sqlite3_bind_text(stmt, 2, txt, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding VLR/SGSN Number: %d\n", rc);
ret = -2;
goto out;
}
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
ret = -3;
goto out;
}
out:
/* remove bindings and reset statement to be re-executed */
rc = sqlite3_clear_bindings(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc);
}
rc = sqlite3_reset(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
}
return ret;
}
int db_subscr_purge(struct db_context *dbc, const char *imsi, bool is_ps)
{
sqlite3_stmt *stmt = dbc->stmt[UPD_VLR_BY_ID];
int rc, ret = 1;
if (is_ps)
stmt = dbc->stmt[UPD_PURGE_PS_BY_IMSI];
else
stmt = dbc->stmt[UPD_PURGE_CS_BY_IMSI];
rc = sqlite3_bind_text(stmt, 1, imsi, -1, SQLITE_STATIC);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error binding IMSI %s: %d\n", imsi, rc);
ret = -1;
goto out;
}
/* execute the statement */
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
LOGP(DAUC, LOGL_ERROR, "Error setting Purged: %d\n", rc);
ret = -2;
goto out;
}
/* FIXME: return 0 in case IMSI not known */
out:
/* remove bindings and reset statement to be re-executed */
rc = sqlite3_clear_bindings(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error clearing bindings: %d\n", rc);
}
rc = sqlite3_reset(stmt);
if (rc != SQLITE_OK) {
LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc);
}
return ret;
}

View File

@ -1,84 +0,0 @@
#include <string.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/application.h>
#include "db.h"
#include "rand.h"
#include "logging.h"
static struct db_context *g_dbc;
static int test(const char *imsi)
{
struct osmo_auth_vector vec[3];
int rc, i;
/* initialize all vectors with a known token pattern */
memset(vec, 0x55, sizeof(vec));
for (i = 0; i < ARRAY_SIZE(vec); i++)
vec[i].res_len = 0;
rc = db_get_auc(g_dbc, imsi, vec, ARRAY_SIZE(vec), NULL, NULL);
if (rc <= 0) {
LOGP(DMAIN, LOGL_ERROR, "Cannot obtain auth tuples for '%s'\n", imsi);
return rc;
}
LOGP(DMAIN, LOGL_INFO, "Obtained %u tuples for subscriber IMSI %s\n",
rc, imsi);
for (i = 0; i < rc; i++) {
struct osmo_auth_vector *v = vec + i;
LOGP(DMAIN, LOGL_DEBUG, "Tuple %u, auth_types=0x%x\n", i, v->auth_types);
LOGP(DMAIN, LOGL_DEBUG, "RAND=%s\n", osmo_hexdump_nospc(v->rand, sizeof(v->rand)));
LOGP(DMAIN, LOGL_DEBUG, "AUTN=%s\n", osmo_hexdump_nospc(v->autn, sizeof(v->autn)));
LOGP(DMAIN, LOGL_DEBUG, "CK=%s\n", osmo_hexdump_nospc(v->ck, sizeof(v->ck)));
LOGP(DMAIN, LOGL_DEBUG, "IK=%s\n", osmo_hexdump_nospc(v->ik, sizeof(v->ik)));
LOGP(DMAIN, LOGL_DEBUG, "RES=%s\n", osmo_hexdump_nospc(v->res, v->res_len));
LOGP(DMAIN, LOGL_DEBUG, "Kc=%s\n", osmo_hexdump_nospc(v->kc, sizeof(v->kc)));
LOGP(DMAIN, LOGL_DEBUG, "SRES=%s\n", osmo_hexdump_nospc(v->sres, sizeof(v->sres)));
}
return rc;
}
int main(int argc, char **argv)
{
int rc;
rc = osmo_init_logging(&hlr_log_info);
if (rc < 0) {
fprintf(stderr, "Error initializing logging\n");
exit(1);
}
LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
rc = rand_init();
if (rc < 0) {
LOGP(DMAIN, LOGL_ERROR, "Error initializing random source\n");
exit(1);
}
g_dbc = db_open(NULL, "hlr.db");
if (!g_dbc) {
LOGP(DMAIN, LOGL_ERROR, "Error opening database\n");
exit(1);
}
/* non-existing subscriber */
rc = test("901990123456789");
/* 2G only AUC data (COMP128v1 / MILENAGE) */
rc = test("901990000000001");
/* 2G + 3G AUC data (COMP128v1 / MILENAGE) */
rc = test("901990000000002");
/* 3G AUC data (MILENAGE) */
rc = test("901990000000003");
LOGP(DMAIN, LOGL_NOTICE, "Exiting\n");
db_close(g_dbc);
log_fini();
exit(0);
}

View File

@ -1,85 +0,0 @@
/* (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/>.
*
*/
#include <errno.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include "gsup_server.h"
struct gsup_route {
struct llist_head list;
uint8_t *addr;
struct osmo_gsup_conn *conn;
};
/* find a route for the given address */
struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs,
const uint8_t *addr, size_t addrlen)
{
struct gsup_route *gr;
llist_for_each_entry(gr, &gs->routes, list) {
if (talloc_total_size(gr->addr) == addrlen &&
!memcmp(gr->addr, addr, addrlen))
return gr->conn;
}
return NULL;
}
/* add a new route for the given address to the given conn */
int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen)
{
struct gsup_route *gr;
/* Check if we already have a route for this address */
if (gsup_route_find(conn->server, addr, addrlen))
return -EEXIST;
/* allocate new route and populate it */
gr = talloc_zero(conn->server, struct gsup_route);
if (!gr)
return -ENOMEM;
gr->addr = talloc_memdup(gr, addr, addrlen);
gr->conn = conn;
llist_add_tail(&gr->list, &conn->server->routes);
return 0;
}
/* delete all routes for the given connection */
int gsup_route_del_conn(struct osmo_gsup_conn *conn)
{
struct gsup_route *gr, *gr2;
unsigned int num_deleted = 0;
llist_for_each_entry_safe(gr, gr2, &conn->server->routes, list) {
if (gr->conn == conn) {
llist_del(&gr->list);
talloc_free(gr);
num_deleted++;
}
}
return num_deleted;
}

View File

@ -1,8 +0,0 @@
struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs,
const uint8_t *addr, size_t addrlen);
/* add a new route for the given address to the given conn */
int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen);
/* delete all routes for the given connection */
int gsup_route_del_conn(struct osmo_gsup_conn *conn);

View File

@ -1,290 +0,0 @@
/* (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/>.
*
*/
#include <errno.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/abis/ipa.h>
#include <osmocom/abis/ipaccess.h>
#include "logging.h"
#include "gsup_server.h"
static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
int proto_ext, struct msgb *msg_tx)
{
ipa_prepend_header_ext(msg_tx, proto_ext);
ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
ipa_server_conn_send(conn->conn, msg_tx);
}
int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
{
if (!conn) {
msgb_free(msg);
return -ENOTCONN;
}
osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
return 0;
}
static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
struct msgb *msg_rx)
{
int rc;
struct msgb *msg_tx;
#if 0
rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
msgb_free(msg_rx);
if (rc < 0)
return rc;
if (msg_tx)
osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
#endif
return 0;
}
/* Data from a given client has arrived over the socket */
static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
struct msgb *msg)
{
struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
int rc;
msg->l2h = &hh->data[0];
if (hh->proto == IPAC_PROTO_IPACCESS) {
rc = ipa_server_conn_ccm(conn, msg);
if (rc < 0) {
/* conn is already invalid here! */
return -1;
}
msgb_free(msg);
return 0;
}
if (hh->proto != IPAC_PROTO_OSMO) {
LOGP(DGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
hh->proto);
goto invalid;
}
if (!he || msgb_l2len(msg) < sizeof(*he)) {
LOGP(DGSUP, LOGL_NOTICE, "short IPA message\n");
goto invalid;
}
msg->l2h = &he->data[0];
if (he->proto == IPAC_PROTO_EXT_GSUP) {
OSMO_ASSERT(clnt->server->read_cb != NULL);
clnt->server->read_cb(clnt, msg);
/* expecting read_cb() to free msg */
} else if (he->proto == IPAC_PROTO_EXT_OAP) {
return osmo_gsup_conn_oap_handle(clnt, msg);
/* osmo_gsup_client_oap_handle frees msg */
} else {
LOGP(DGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
hh->proto);
goto invalid;
}
return 0;
invalid:
LOGP(DGSUP, LOGL_NOTICE,
"GSUP received an invalid IPA message from %s:%d: %s\n",
conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
msgb_free(msg);
return -1;
}
static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
int subsys, int level)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
if (!TLVP_PRESENT(tlvp, i))
continue;
LOGP(subsys, level, "%u: %s\n", i,
TLVP_VAL(tlvp, i));
LOGP(subsys, level, "%u: %s\n", i,
osmo_hexdump(TLVP_VAL(tlvp, i),
TLVP_LEN(tlvp, i)));
}
}
/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
if (!TLVP_PRESENT(in, i)) {
if (TLVP_PRESENT(out, i)) {
talloc_free((void *) out->lv[i].val);
out->lv[i].val = NULL;
out->lv[i].len = 0;
}
continue;
}
out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
out->lv[i].len = in->lv[i].len;
}
}
int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
uint8_t tag)
{
if (!TLVP_PRESENT(&clnt->ccm, tag))
return -ENODEV;
*addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
return TLVP_LEN(&clnt->ccm, tag);
}
static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
struct msgb *msg, struct tlv_parsed *tlvp,
struct ipaccess_unit *unit)
{
struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
uint8_t *addr;
size_t addr_len;
LOGP(DGSUP, LOGL_INFO, "CCM Callback\n");
/* FIXME: should this be parrt of ipas_server handling, not
* GSUP? */
tlvp_copy(clnt, &clnt->ccm, tlvp);
osmo_tlvp_dump(tlvp, DGSUP, LOGL_INFO);
addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
if (addr_len)
gsup_route_add(clnt, addr, addr_len);
return 0;
}
static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
{
struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
LOGP(DGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
conn->addr, conn->port);
gsup_route_del_conn(clnt);
llist_del(&clnt->list);
talloc_free(clnt);
return 0;
}
/* a client has connected to the server socket and we have accept()ed it */
static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
{
struct osmo_gsup_conn *conn;
struct osmo_gsup_server *gsups =
(struct osmo_gsup_server *) link->data;
int rc;
conn = talloc_zero(gsups, struct osmo_gsup_conn);
OSMO_ASSERT(conn);
conn->conn = ipa_server_conn_create(gsups, link, fd,
osmo_gsup_server_read_cb,
osmo_gsup_server_closed_cb, conn);
conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
OSMO_ASSERT(conn->conn);
/* link data structure with server structure */
conn->server = gsups;
llist_add_tail(&conn->list, &gsups->clients);
LOGP(DGSUP, LOGL_INFO, "New GSUP client %s:%d\n",
conn->conn->addr, conn->conn->port);
/* request the identity of the client */
rc = ipa_ccm_send_id_req(fd);
if (rc < 0)
goto failed;
#if 0
rc = oap_init(&gsups->oap_config, &conn->oap_state);
if (rc != 0)
goto failed;
#endif
return 0;
failed:
ipa_server_conn_destroy(conn->conn);
return -1;
}
struct osmo_gsup_server *
osmo_gsup_server_create(void *ctx, const char *ip_addr,
uint16_t tcp_port,
osmo_gsup_read_cb_t read_cb)
{
struct osmo_gsup_server *gsups;
int rc;
gsups = talloc_zero(ctx, struct osmo_gsup_server);
OSMO_ASSERT(gsups);
INIT_LLIST_HEAD(&gsups->clients);
INIT_LLIST_HEAD(&gsups->routes);
gsups->link = ipa_server_link_create(gsups,
/* no e1inp */ NULL,
ip_addr, tcp_port,
osmo_gsup_server_accept_cb,
gsups);
if (!gsups->link)
goto failed;
gsups->read_cb = read_cb;
rc = ipa_server_link_open(gsups->link);
if (rc < 0)
goto failed;
return gsups;
failed:
osmo_gsup_server_destroy(gsups);
return NULL;
}
void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
{
if (gsups->link) {
ipa_server_link_close(gsups->link);
ipa_server_link_destroy(gsups->link);
gsups->link = NULL;
}
talloc_free(gsups);
}

View File

@ -1,42 +0,0 @@
#pragma once
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/msgb.h>
#include <osmocom/abis/ipa.h>
#include <osmocom/abis/ipaccess.h>
struct osmo_gsup_conn;
/* Expects message in msg->l2h */
typedef int (*osmo_gsup_read_cb_t)(struct osmo_gsup_conn *conn, struct msgb *msg);
struct osmo_gsup_server {
/* list of osmo_gsup_conn */
struct llist_head clients;
struct ipa_server_link *link;
osmo_gsup_read_cb_t read_cb;
struct llist_head routes;
};
/* a single connection to a given client (SGSN, MSC) */
struct osmo_gsup_conn {
struct llist_head list;
struct osmo_gsup_server *server;
struct ipa_server_conn *conn;
//struct oap_state oap_state;
struct tlv_parsed ccm;
};
int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg);
struct osmo_gsup_server *osmo_gsup_server_create(void *ctx,
const char *ip_addr,
uint16_t tcp_port,
osmo_gsup_read_cb_t read_cb);
void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups);

View File

@ -1,564 +0,0 @@
/* (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/>.
*
*/
#include <signal.h>
#include <errno.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/application.h>
#include <osmocom/gsm/gsup.h>
#include <osmocom/gsm/apn.h>
#include "db.h"
#include "logging.h"
#include "gsup_server.h"
#include "gsup_router.h"
#include "rand.h"
static struct db_context *g_dbc;
/***********************************************************************
* Send Auth Info handling
***********************************************************************/
/* process an incoming SAI request */
static int rx_send_auth_info(struct osmo_gsup_conn *conn,
const struct osmo_gsup_message *gsup)
{
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));
rc = db_get_auc(g_dbc, gsup->imsi, gsup_out.auth_vectors,
ARRAY_SIZE(gsup_out.auth_vectors),
gsup->rand, gsup->auts);
if (rc < 0) {
gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
gsup_out.cause = GMM_CAUSE_NET_FAIL;
} else if (rc == 0) {
gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
} else {
gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
gsup_out.num_auth_vectors = rc;
}
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
osmo_gsup_encode(msg_out, &gsup_out);
return osmo_gsup_conn_send(conn, msg_out);
}
/***********************************************************************
* LU Operation State / Structure
***********************************************************************/
static LLIST_HEAD(g_lu_ops);
#define CANCEL_TIMEOUT_SECS 30
#define ISD_TIMEOUT_SECS 30
enum lu_state {
LU_S_NULL,
LU_S_LU_RECEIVED,
LU_S_CANCEL_SENT,
LU_S_CANCEL_ACK_RECEIVED,
LU_S_ISD_SENT,
LU_S_ISD_ACK_RECEIVED,
LU_S_COMPLETE,
};
static const struct value_string lu_state_names[] = {
{ LU_S_NULL, "NULL" },
{ LU_S_LU_RECEIVED, "LU RECEIVED" },
{ LU_S_CANCEL_SENT, "CANCEL SENT" },
{ LU_S_CANCEL_ACK_RECEIVED, "CANCEL-ACK RECEIVED" },
{ LU_S_ISD_SENT, "ISD SENT" },
{ LU_S_ISD_ACK_RECEIVED, "ISD-ACK RECEIVED" },
{ LU_S_COMPLETE, "COMPLETE" },
{ 0, NULL }
};
struct lu_operation {
/*! entry in global list of location update operations */
struct llist_head list;
/*! to which gsup_server do we belong */
struct osmo_gsup_server *gsup_server;
/*! state of the location update */
enum lu_state state;
/*! CS (false) or PS (true) Location Update? */
bool is_ps;
/*! currently running timer */
struct osmo_timer_list timer;
/*! subscriber related to this operation */
struct hlr_subscriber subscr;
/*! peer VLR/SGSN starting the request */
uint8_t *peer;
};
void lu_op_statechg(struct lu_operation *luop, enum lu_state new_state)
{
enum lu_state old_state = luop->state;
DEBUGP(DMAIN, "LU OP state change: %s -> ",
get_value_string(lu_state_names, old_state));
DEBUGPC(DMAIN, "%s\n",
get_value_string(lu_state_names, new_state));
luop->state = new_state;
}
struct lu_operation *lu_op_by_imsi(const char *imsi)
{
struct lu_operation *luop;
llist_for_each_entry(luop, &g_lu_ops, list) {
if (!strcmp(imsi, luop->subscr.imsi))
return luop;
}
return NULL;
}
/* Send a msgb to a given address using routing */
int osmo_gsup_addr_send(struct osmo_gsup_server *gs,
const uint8_t *addr, size_t addrlen,
struct msgb *msg)
{
struct osmo_gsup_conn *conn;
conn = gsup_route_find(gs, addr, addrlen);
if (!conn) {
DEBUGP(DMAIN, "Cannot find route for addr %s\n", addr);
msgb_free(msg);
return -ENODEV;
}
return osmo_gsup_conn_send(conn, msg);
}
/* Transmit a given GSUP message for the given LU operation */
static void _luop_tx_gsup(struct lu_operation *luop,
const struct osmo_gsup_message *gsup)
{
struct msgb *msg_out;
msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP LUOP");
osmo_gsup_encode(msg_out, gsup);
osmo_gsup_addr_send(luop->gsup_server, luop->peer,
talloc_total_size(luop->peer),
msg_out);
}
/*! Transmit UPD_LOC_ERROR and destroy lu_operation */
void lu_op_tx_error(struct lu_operation *luop, enum gsm48_gmm_cause cause)
{
struct osmo_gsup_message gsup;
DEBUGP(DMAIN, "%s: LU OP Tx Error (cause=%u)\n",
luop->subscr.imsi, cause);
memset(&gsup, 0, sizeof(gsup));
gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR;
strncpy(&gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi));
gsup.imsi[sizeof(gsup.imsi)-1] = '\0';
gsup.cause = cause;
_luop_tx_gsup(luop, &gsup);
llist_del(&luop->list);
talloc_free(luop);
}
/* timer call-back in case LU operation doesn't receive an response */
static void lu_op_timer_cb(void *data)
{
struct lu_operation *luop = data;
DEBUGP(DMAIN, "LU OP timer expired in state %s\n",
get_value_string(lu_state_names, luop->state));
switch (luop->state) {
case LU_S_CANCEL_SENT:
break;
case LU_S_ISD_SENT:
break;
default:
break;
}
lu_op_tx_error(luop, GMM_CAUSE_NET_FAIL);
}
/*! Transmit UPD_LOC_RESULT and destroy lu_operation */
void lu_op_tx_ack(struct lu_operation *luop)
{
struct osmo_gsup_message gsup;
memset(&gsup, 0, sizeof(gsup));
gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT;
strncpy(gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi)-1);
//FIXME gsup.hlr_enc;
_luop_tx_gsup(luop, &gsup);
llist_del(&luop->list);
talloc_free(luop);
}
/*! Send Cancel Location to old VLR/SGSN */
void lu_op_tx_cancel_old(struct lu_operation *luop)
{
struct osmo_gsup_message gsup;
OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED);
memset(&gsup, 0, sizeof(gsup));
gsup.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST;
//gsup.cause = FIXME;
//gsup.cancel_type = FIXME;
_luop_tx_gsup(luop, &gsup);
lu_op_statechg(luop, LU_S_CANCEL_SENT);
osmo_timer_schedule(&luop->timer, CANCEL_TIMEOUT_SECS, 0);
}
/*! Receive Cancel Location Result from old VLR/SGSN */
void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
const struct osmo_gsup_message *gsup)
{
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);
}
/*! Transmit Insert Subscriber Data to new VLR/SGSN */
void lu_op_tx_insert_subscr_data(struct lu_operation *luop)
{
struct osmo_gsup_message gsup;
OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED ||
luop->state == LU_S_CANCEL_ACK_RECEIVED);
memset(&gsup, 0, sizeof(gsup));
gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST;
strncpy(gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi)-1);
/* FIXME: deal with encoding the following data */
gsup.msisdn_enc;
gsup.hlr_enc;
if (luop->is_ps) {
/* FIXME: PDP infos - use more fine-grained access control
instead of wildcard */
uint8_t apn[APN_MAXLEN];
gsup.pdp_infos[0].have_info = 1;
gsup.pdp_infos[0].context_id = 1; // FIXME: use real value
gsup.pdp_infos[0].apn_enc = apn;
gsup.pdp_infos[0].apn_enc_len =
osmo_apn_from_str(gsup.pdp_infos[0].apn_enc, 2, "*");
gsup.num_pdp_infos = 1;
}
/* Send ISD to new VLR/SGSN */
_luop_tx_gsup(luop, &gsup);
lu_op_statechg(luop, LU_S_ISD_SENT);
osmo_timer_schedule(&luop->timer, ISD_TIMEOUT_SECS, 0);
}
/*! 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;
}
}
static struct lu_operation *lu_op_alloc(struct osmo_gsup_server *srv)
{
struct lu_operation *luop;
luop = talloc_zero(srv, struct lu_operation);
OSMO_ASSERT(luop);
luop->gsup_server = srv;
luop->timer.cb = lu_op_timer_cb;
luop->timer.data = luop;
return luop;
}
/*! 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)
{
int rc;
struct lu_operation *luop;
struct hlr_subscriber *subscr;
uint8_t *peer_addr;
rc = osmo_gsup_conn_ccm_get(conn, &peer_addr, IPAC_IDTAG_SERNR);
if (rc < 0) {
LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
return rc;
}
luop = lu_op_alloc(conn->server);
luop->peer = talloc_memdup(luop, peer_addr, rc);
lu_op_statechg(luop, LU_S_LU_RECEIVED);
subscr = &luop->subscr;
if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
luop->is_ps = true;
llist_add(&luop->list, &g_lu_ops);
/* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
/* check if subscriber is known at all */
rc = db_subscr_get(g_dbc, gsup->imsi, subscr);
if (rc < 0) {
/* Send Error back: Subscriber Unknown in HLR */
strcpy(luop->subscr.imsi, gsup->imsi);
lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
return 0;
}
/* Check if subscriber is generally permitted on CS or PS
* service (as requested) */
if (!luop->is_ps && !subscr->nam_cs) {
lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
return 0;
} else if (luop->is_ps && !subscr->nam_ps) {
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;
}
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 */
rc = db_subscr_purge(g_dbc, gsup->imsi, is_ps);
if (rc == 1)
gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
else if (rc == 0) {
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);
}
static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
{
static struct osmo_gsup_message gsup;
int rc;
rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
if (rc < 0) {
LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
return rc;
}
switch (gsup.message_type) {
/* requests sent to us */
case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
rx_send_auth_info(conn, &gsup);
break;
case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
rx_upd_loc_req(conn, &gsup);
break;
case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
rx_purge_ms_req(conn, &gsup);
break;
/* responses to requests sent by us */
case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
{
struct lu_operation *luop = lu_op_by_imsi(gsup.imsi);
if (!luop) {
LOGP(DMAIN, LOGL_ERROR, "GSUP message %u for "
"unknown IMSI %s\n", gsup.message_type,
gsup.imsi);
break;
}
lu_op_rx_gsup(luop, &gsup);
}
break;
default:
LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %u\n",
gsup.message_type);
break;
}
msgb_free(msg);
return 0;
}
static struct osmo_gsup_server *gs;
static void signal_hdlr(int signal)
{
switch (signal) {
case SIGINT:
LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
osmo_gsup_server_destroy(gs);
db_close(g_dbc);
log_fini();
exit(0);
break;
case SIGUSR1:
LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
talloc_report_full(NULL, stderr);
break;
}
}
int main(int argc, char **argv)
{
int rc;
talloc_enable_leak_report_full();
rc = osmo_init_logging(&hlr_log_info);
if (rc < 0) {
fprintf(stderr, "Error initializing logging\n");
exit(1);
}
LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
rc = rand_init();
if (rc < 0) {
LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
exit(1);
}
g_dbc = db_open(NULL, "hlr.db");
if (!g_dbc) {
LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
exit(1);
}
gs = osmo_gsup_server_create(NULL, NULL, 2222, read_cb);
if (!gs) {
LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
exit(1);
}
osmo_init_ignore_signals();
signal(SIGINT, &signal_hdlr);
signal(SIGUSR1, &signal_hdlr);
//osmo_daemonize();
while (1) {
osmo_select_main(0);
}
db_close(g_dbc);
log_fini();
exit(0);
}

View File

@ -1,34 +0,0 @@
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>
#include "logging.h"
const struct log_info_cat hlr_log_info_cat[] = {
[DMAIN] = {
.name = "DMAIN",
.description = "Main Program",
.enabled = 1, .loglevel = LOGL_DEBUG,
},
[DDB] = {
.name = "DDB",
.description = "Database Layer",
.color = "\033[1;31m",
.enabled = 1, .loglevel = LOGL_DEBUG,
},
[DGSUP] = {
.name = "DGSUP",
.description = "GSUP Protocol",
.color = "\033[1;32m",
.enabled = 1, .loglevel = LOGL_INFO,
},
[DAUC] = {
.name = "DAUC",
.description = "Authentication Center",
.color = "\033[1;33m",
.enabled = 1, .loglevel = LOGL_DEBUG,
},
};
const struct log_info hlr_log_info = {
.cat = hlr_log_info_cat,
.num_cat = ARRAY_SIZE(hlr_log_info_cat),
};

View File

@ -1,12 +0,0 @@
#pragma once
#include <osmocom/core/logging.h>
enum {
DMAIN,
DDB,
DGSUP,
DAUC,
};
extern const struct log_info hlr_log_info;

View File

@ -1,75 +0,0 @@
#!/usr/bin/perl
#
use strict;
use DBI;
my $dbh = DBI->connect("dbi:SQLite:dbname=hlr.db","","");
my $sth_subscr_base = $dbh->prepare("INSERT INTO subscriber (imsi, msisdn) VALUES (?, ?)");
my $sth_subscr_get_id = $dbh->prepare("SELECT * FROM subscriber WHERE imsi = ?");
my $sth_auc_3g = $dbh->prepare("INSERT INTO auc_3g (subscriber_id, algo_id_3g, k, op, sqn) VALUES (?, ?, ?, ?, ?)");
my $sth_auc_2g = $dbh->prepare("INSERT INTO auc_2g (subscriber_id, algo_id_2g, ki) VALUES (?, ?, ?)");
sub create_subscr_base($)
{
my ($imsi) = @_;
my $suffix = substr($imsi, 5);
my $msisdn = "49" . $suffix;
return $sth_subscr_base->execute($imsi, $msisdn);
}
sub create_auc_2g($)
{
my ($id) = @_;
my $ki = "000102030405060708090a0b0c0d0e0f";
$sth_auc_2g->execute($id, 1, $ki);
}
sub create_auc_3g($)
{
my ($id) = @_;
my $k = "000102030405060708090a0b0c0d0e0f";
my $op = "00102030405060708090a0b0c0d0e0f0";
$sth_auc_3g->execute($id, 5, $k, $op, 0);
}
sub create_subscr($$$)
{
my ($imsi, $is_2g, $is_3g) = @_;
my $suffix = substr($imsi, 5);
create_subscr_base($imsi);
my $id = $dbh->sqlite_last_insert_rowid();
#$sth_subscr_get_id->execute($imsi);
#my @arr = $sth_subscr_get_id->fetchrow_array();
#my $id = $arr[0];
if ($is_3g) {
create_auc_3g($id);
}
if ($is_2g) {
create_auc_2g($id);
}
}
my $prefix = "90179";
$dbh->{AutoCommit} = 0;
$dbh->do("PRAGMA synchronous = OFF");
for (my $i = 0; $i < 1000000; $i++) {
my $imsi = sprintf("%s%010u", $prefix, $i);
if ($i % 1000 == 0) {
printf("Creating subscriber IMSI %s\n", $imsi);
}
create_subscr($imsi, 1, 1);
}
$dbh->commit;

View File

@ -1,7 +0,0 @@
#pragma once
#include <stdint.h>
int rand_init(void);
int rand_get(uint8_t *rand, unsigned int len);

View File

@ -1,52 +0,0 @@
/* (C) 2012 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/>.
*
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
static uint8_t ctr = 0;
static void print_msg(void)
{
static int printed = 0;
if (!printed) {
fprintf(stderr, "Using fake random generator for deterministic "
"test results. NEVER USE THIS IN PRODUCTION\n");
printed = 1;
}
}
int rand_init(void)
{
print_msg();
return 0;
}
int rand_get(uint8_t *rand, unsigned int len)
{
print_msg();
memset(rand, ctr, len);
ctr++;
return len;
}

View File

@ -1,38 +0,0 @@
/* (C) 2012 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/>.
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
static int rand_fd = -1;
int rand_init(void)
{
rand_fd = open("/dev/urandom", O_RDONLY);
return rand_fd;
}
int rand_get(uint8_t *rand, unsigned int len)
{
return read(rand_fd, rand, len);
}