8
0
Fork 0

remove osmo-gsup-hlr

remove osmo-gsup-hlr, which was moving to its own osmo-hlr.git on
November 11, 2016.
master
Harald Welte 6 years ago
parent 8076c87c6b
commit d54de7d353
  1. 4
      osmo-gsup-hlr/.gitignore
  2. 70
      osmo-gsup-hlr/sql/hlr.sql
  3. 13
      osmo-gsup-hlr/sql/hlr_data.sql
  4. 18
      osmo-gsup-hlr/src/Makefile
  5. 94
      osmo-gsup-hlr/src/auc.c
  6. 8
      osmo-gsup-hlr/src/auc.h
  7. 130
      osmo-gsup-hlr/src/db.c
  8. 78
      osmo-gsup-hlr/src/db.h
  9. 225
      osmo-gsup-hlr/src/db_auc.c
  10. 176
      osmo-gsup-hlr/src/db_hlr.c
  11. 84
      osmo-gsup-hlr/src/db_test.c
  12. 85
      osmo-gsup-hlr/src/gsup_router.c
  13. 8
      osmo-gsup-hlr/src/gsup_router.h
  14. 290
      osmo-gsup-hlr/src/gsup_server.c
  15. 42
      osmo-gsup-hlr/src/gsup_server.h
  16. 564
      osmo-gsup-hlr/src/hlr.c
  17. 34
      osmo-gsup-hlr/src/logging.c
  18. 12
      osmo-gsup-hlr/src/logging.h
  19. 75
      osmo-gsup-hlr/src/populate_hlr_db.pl
  20. 7
      osmo-gsup-hlr/src/rand.h
  21. 52
      osmo-gsup-hlr/src/rand_fake.c
  22. 38
      osmo-gsup-hlr/src/rand_urandom.c

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

@ -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 = ?

@ -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);

@ -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

@ -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;
}

@ -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);

@ -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;
}

@ -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);

@ -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;
}

@ -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;
}

@ -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);
}

@ -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;
}

@ -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);

@ -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);
}

@ -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);

@ -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