This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Harald Welte gerrit-no-reply at lists.osmocom.orgHarald Welte has uploaded this change for review. ( https://gerrit.osmocom.org/10109 Change subject: import gsup_client + oap_client as new libosmo-gsup-client ...................................................................... import gsup_client + oap_client as new libosmo-gsup-client This imports the code from osmo-msc 6afef893e17bce67e4d4119acd34d480ed03ba77 with minimal changes to make it compile. Symbol renaming ot osmo_ prefix is done separately in a follow-up patch to have a as-clean-as-possible import first. Change-Id: Ief50054ad135551625b684ed8a0486f7af0b2940 --- M Makefile.am M configure.ac M include/Makefile.am A include/osmocom/gsupclient/gsup_client.h A include/osmocom/gsupclient/oap_client.h A src/gsupclient/Makefile.am A src/gsupclient/gsup_client.c A src/gsupclient/gsup_test_client.c A src/gsupclient/oap_client.c 9 files changed, 1,123 insertions(+), 1 deletion(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/09/10109/1 diff --git a/Makefile.am b/Makefile.am index de5eb35..3fe61b2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ ACLOCAL_AMFLAGS = -I m4 AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc utils tests +SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc src/gsupclient utils tests pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \ diff --git a/configure.ac b/configure.ac index dd5f15b..c2b12ea 100644 --- a/configure.ac +++ b/configure.ac @@ -228,6 +228,14 @@ [enable_ctrl=$enableval], [enable_ctrl="yes"]) AM_CONDITIONAL(ENABLE_CTRL, test x"$enable_ctrl" = x"yes") +AC_ARG_ENABLE(gsup-client, + [AS_HELP_STRING( + [--disable-gsup-client], + [Disable building GSUP client library], + )], + [enable_gsupclient=$enableval], [enable_gsupclient="yes"]) +AM_CONDITIONAL(ENABLE_GSUP_CLIENT, test x"$enable_gsupclient" = x"yes") + AC_ARG_ENABLE(pseudotalloc, [AS_HELP_STRING( [--enable-pseudotalloc], @@ -364,6 +372,7 @@ src/gb/Makefile src/ctrl/Makefile src/pseudotalloc/Makefile + src/gsupclient/Makefile tests/Makefile tests/atlocal utils/Makefile diff --git a/include/Makefile.am b/include/Makefile.am index 38ba14c..a65f8a5 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -135,6 +135,10 @@ nobase_include_HEADERS += osmocom/core/serial.h endif +if ENABLE_GSUP_CLIENT +nobase_include_HEADERS += osmocom/gsupclient/oap_client.h \ + osmocom/gsupclient/gsup_client.h +endif if ENABLE_VTY nobase_include_HEADERS += \ diff --git a/include/osmocom/gsupclient/gsup_client.h b/include/osmocom/gsupclient/gsup_client.h new file mode 100644 index 0000000..bc0f35a --- /dev/null +++ b/include/osmocom/gsupclient/gsup_client.h @@ -0,0 +1,66 @@ +/* GPRS Subscriber Update Protocol client */ + +/* (C) 2014 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Jacob Erlbeck + * + * 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/>. + * + */ +#pragma once + +#include <osmocom/core/timer.h> +#include <osmocom/gsupclient/oap_client.h> + +/* a loss of GSUP between MSC and HLR is considered quite serious, let's try to recover as quickly as + * possible. Even one new connection attempt per second should be quite acceptable until the link is + * re-established */ +#define GSUP_CLIENT_RECONNECT_INTERVAL 1 +#define GSUP_CLIENT_PING_INTERVAL 20 + +struct msgb; +struct ipa_client_conn; +struct gsup_client; + +/* Expects message in msg->l2h */ +typedef int (*gsup_client_read_cb_t)(struct gsup_client *gsupc, + struct msgb *msg); + +struct gsup_client { + const char *unit_name; + + struct ipa_client_conn *link; + gsup_client_read_cb_t read_cb; + void *data; + + struct oap_client_state oap_state; + + struct osmo_timer_list ping_timer; + struct osmo_timer_list connect_timer; + int is_connected; + int got_ipa_pong; +}; + +struct gsup_client *gsup_client_create(void *talloc_ctx, + const char *unit_name, + const char *ip_addr, + unsigned int tcp_port, + gsup_client_read_cb_t read_cb, + struct oap_client_config *oapc_config); + +void gsup_client_destroy(struct gsup_client *gsupc); +int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg); +struct msgb *gsup_client_msgb_alloc(void); + diff --git a/include/osmocom/gsupclient/oap_client.h b/include/osmocom/gsupclient/oap_client.h new file mode 100644 index 0000000..80c86d5 --- /dev/null +++ b/include/osmocom/gsupclient/oap_client.h @@ -0,0 +1,82 @@ +/* Osmocom Authentication Protocol API */ + +/* (C) 2015 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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/>. + * + */ + +#pragma once + +#include <stdint.h> + +struct msgb; +struct osmo_oap_message; + +/* This is the config part for vty. It is essentially copied in + * oap_client_state, where values are copied over once the config is + * considered valid. */ +struct oap_client_config { + uint16_t client_id; + int secret_k_present; + uint8_t secret_k[16]; + int secret_opc_present; + uint8_t secret_opc[16]; +}; + +/* The runtime state of the OAP client. client_id and the secrets are in fact + * duplicated from oap_client_config, so that a separate validation of the + * config data is possible, and so that only a struct oap_client_state* is + * passed around. */ +struct oap_client_state { + enum { + OAP_UNINITIALIZED = 0, /* just allocated. */ + OAP_DISABLED, /* disabled by config. */ + OAP_INITIALIZED, /* enabled, config is valid. */ + OAP_REQUESTED_CHALLENGE, + OAP_SENT_CHALLENGE_RESULT, + OAP_REGISTERED + } state; + uint16_t client_id; + uint8_t secret_k[16]; + uint8_t secret_opc[16]; + int registration_failures; +}; + +/* From config, initialize state. Return 0 on success. */ +int oap_client_init(struct oap_client_config *config, + struct oap_client_state *state); + +/* Construct an OAP registration message and return in *msg_tx. Use + * state->client_id and update state->state. + * Return 0 on success, or a negative value on error. + * If an error is returned, *msg_tx is guaranteed to be NULL. */ +int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx); + +/* Decode and act on a received OAP message msg_rx. Update state->state. If a + * non-NULL pointer is returned in *msg_tx, that msgb should be sent to the OAP + * server (and freed) by the caller. The received msg_rx is not freed. + * Return 0 on success, or a negative value on error. + * If an error is returned, *msg_tx is guaranteed to be NULL. */ +int oap_client_handle(struct oap_client_state *state, + const struct msgb *msg_rx, struct msgb **msg_tx); + +/* Allocate a msgb and in it, return the encoded oap_client_msg. Return + * NULL on error. (Like oap_client_encode(), but also allocates a msgb.) + * About the name: the idea is do_something(oap_client_encoded(my_struct)) + */ +struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_client_msg); diff --git a/src/gsupclient/Makefile.am b/src/gsupclient/Makefile.am new file mode 100644 index 0000000..a2a3cde --- /dev/null +++ b/src/gsupclient/Makefile.am @@ -0,0 +1,16 @@ +# This is _NOT_ the library release version, it's an API version. +# Please read chapter "Library interface versions" of the libtool documentation +# before making any modifications: https://www.gnu.org/software/libtool/manual/html_node/Versioning.html +LIBVERSION=0:0:0 + +AM_CFLAGS = -Wall $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include $(TALLOC_CFLAGS) + +if ENABLE_GSUP_CLIENT +lib_LTLIBRARIES = libosmo-gsup-client.la + +libosmo_gsup_client_la_SOURCES = gsup_client.c oap_client.c + +libosmo_gsup_client_la_LDFLAGS = -version-info $(LIBVERSION) -no-undefined +libosmo_gsup_client_la_LIBADD = $(TALLOC_LIBS) \ + $(top_builddir)/src/libosmocore.la +endif diff --git a/src/gsupclient/gsup_client.c b/src/gsupclient/gsup_client.c new file mode 100644 index 0000000..7c2bb49 --- /dev/null +++ b/src/gsupclient/gsup_client.c @@ -0,0 +1,344 @@ +/* Generic Subscriber Update Protocol client */ + +/* (C) 2014-2016 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Jacob Erlbeck + * Author: Neels Hofmeyr + * + * 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/gsupclient/gsup_client.h> + +#include <osmocom/abis/ipa.h> +#include <osmocom/gsm/protocol/ipaccess.h> +#include <osmocom/core/msgb.h> +#include <osmocom/core/logging.h> + +#include <errno.h> +#include <string.h> + +static void start_test_procedure(struct gsup_client *gsupc); + +static void gsup_client_send_ping(struct gsup_client *gsupc) +{ + struct msgb *msg = gsup_client_msgb_alloc(); + + msg->l2h = msgb_put(msg, 1); + msg->l2h[0] = IPAC_MSGT_PING; + ipa_msg_push_header(msg, IPAC_PROTO_IPACCESS); + ipa_client_conn_send(gsupc->link, msg); +} + +static int gsup_client_connect(struct gsup_client *gsupc) +{ + int rc; + + if (gsupc->is_connected) + return 0; + + if (osmo_timer_pending(&gsupc->connect_timer)) { + LOGP(DLGSUP, LOGL_DEBUG, + "GSUP connect: connect timer already running\n"); + osmo_timer_del(&gsupc->connect_timer); + } + + if (osmo_timer_pending(&gsupc->ping_timer)) { + LOGP(DLGSUP, LOGL_DEBUG, + "GSUP connect: ping timer already running\n"); + osmo_timer_del(&gsupc->ping_timer); + } + + if (ipa_client_conn_clear_queue(gsupc->link) > 0) + LOGP(DLGSUP, LOGL_DEBUG, "GSUP connect: discarded stored messages\n"); + + rc = ipa_client_conn_open(gsupc->link); + + if (rc >= 0) { + LOGP(DLGSUP, LOGL_NOTICE, "GSUP connecting to %s:%d\n", + gsupc->link->addr, gsupc->link->port); + return 0; + } + + LOGP(DLGSUP, LOGL_ERROR, "GSUP failed to connect to %s:%d: %s\n", + gsupc->link->addr, gsupc->link->port, strerror(-rc)); + + if (rc == -EBADF || rc == -ENOTSOCK || rc == -EAFNOSUPPORT || + rc == -EINVAL) + return rc; + + osmo_timer_schedule(&gsupc->connect_timer, + GSUP_CLIENT_RECONNECT_INTERVAL, 0); + + LOGP(DLGSUP, LOGL_INFO, "Scheduled timer to retry GSUP connect to %s:%d\n", + gsupc->link->addr, gsupc->link->port); + + return 0; +} + +static void connect_timer_cb(void *gsupc_) +{ + struct gsup_client *gsupc = gsupc_; + + if (gsupc->is_connected) + return; + + gsup_client_connect(gsupc); +} + +static void client_send(struct gsup_client *gsupc, 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_client_conn_send(gsupc->link, msg_tx); + /* msg_tx is now queued and will be freed. */ +} + +static void gsup_client_oap_register(struct gsup_client *gsupc) +{ + struct msgb *msg_tx; + int rc; + rc = oap_client_register(&gsupc->oap_state, &msg_tx); + + if ((rc < 0) || (!msg_tx)) { + LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); + return; + } + + client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); +} + +static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) +{ + struct gsup_client *gsupc = link->data; + + LOGP(DLGSUP, LOGL_INFO, "GSUP link to %s:%d %s\n", + link->addr, link->port, up ? "UP" : "DOWN"); + + gsupc->is_connected = up; + + if (up) { + start_test_procedure(gsupc); + + if (gsupc->oap_state.state == OAP_INITIALIZED) + gsup_client_oap_register(gsupc); + + osmo_timer_del(&gsupc->connect_timer); + } else { + osmo_timer_del(&gsupc->ping_timer); + + osmo_timer_schedule(&gsupc->connect_timer, + GSUP_CLIENT_RECONNECT_INTERVAL, 0); + } +} + +static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx) +{ + int rc; + struct msgb *msg_tx; + + /* If the oap_state is disabled, this will reject the messages. */ + rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); + msgb_free(msg_rx); + if (rc < 0) + return rc; + + if (msg_tx) + client_send(gsupc, IPAC_PROTO_EXT_OAP, msg_tx); + + return 0; +} + +static int gsup_client_read_cb(struct ipa_client_conn *link, 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 gsup_client *gsupc = (struct gsup_client *)link->data; + int rc; + struct ipaccess_unit ipa_dev = { + /* see gsup_client_create() on const vs non-const */ + .unit_name = (char*)gsupc->unit_name, + }; + + OSMO_ASSERT(ipa_dev.unit_name); + + msg->l2h = &hh->data[0]; + + rc = ipaccess_bts_handle_ccm(link, &ipa_dev, msg); + + if (rc < 0) { + LOGP(DLGSUP, LOGL_NOTICE, + "GSUP received an invalid IPA/CCM message from %s:%d\n", + link->addr, link->port); + /* Link has been closed */ + gsupc->is_connected = 0; + msgb_free(msg); + return -1; + } + + if (rc == 1) { + uint8_t msg_type = *(msg->l2h); + /* CCM message */ + if (msg_type == IPAC_MSGT_PONG) { + LOGP(DLGSUP, LOGL_DEBUG, "GSUP receiving PONG\n"); + gsupc->got_ipa_pong = 1; + } + + msgb_free(msg); + return 0; + } + + if (hh->proto != IPAC_PROTO_OSMO) + goto invalid; + + if (!he || msgb_l2len(msg) < sizeof(*he)) + goto invalid; + + msg->l2h = &he->data[0]; + + if (he->proto == IPAC_PROTO_EXT_GSUP) { + OSMO_ASSERT(gsupc->read_cb != NULL); + gsupc->read_cb(gsupc, msg); + /* expecting read_cb() to free msg */ + } else if (he->proto == IPAC_PROTO_EXT_OAP) { + return gsup_client_oap_handle(gsupc, msg); + /* gsup_client_oap_handle frees msg */ + } else + goto invalid; + + return 0; + +invalid: + LOGP(DLGSUP, LOGL_NOTICE, + "GSUP received an invalid IPA message from %s:%d, size = %d\n", + link->addr, link->port, msgb_length(msg)); + + msgb_free(msg); + return -1; +} + +static void ping_timer_cb(void *gsupc_) +{ + struct gsup_client *gsupc = gsupc_; + + LOGP(DLGSUP, LOGL_INFO, "GSUP ping callback (%s, %s PONG)\n", + gsupc->is_connected ? "connected" : "not connected", + gsupc->got_ipa_pong ? "got" : "didn't get"); + + if (gsupc->got_ipa_pong) { + start_test_procedure(gsupc); + return; + } + + LOGP(DLGSUP, LOGL_NOTICE, "GSUP ping timed out, reconnecting\n"); + ipa_client_conn_close(gsupc->link); + gsupc->is_connected = 0; + + gsup_client_connect(gsupc); +} + +static void start_test_procedure(struct gsup_client *gsupc) +{ + osmo_timer_setup(&gsupc->ping_timer, ping_timer_cb, gsupc); + + gsupc->got_ipa_pong = 0; + osmo_timer_schedule(&gsupc->ping_timer, GSUP_CLIENT_PING_INTERVAL, 0); + LOGP(DLGSUP, LOGL_DEBUG, "GSUP sending PING\n"); + gsup_client_send_ping(gsupc); +} + +struct gsup_client *gsup_client_create(void *talloc_ctx, + const char *unit_name, + const char *ip_addr, + unsigned int tcp_port, + gsup_client_read_cb_t read_cb, + struct oap_client_config *oapc_config) +{ + struct gsup_client *gsupc; + int rc; + + gsupc = talloc_zero(talloc_ctx, struct gsup_client); + OSMO_ASSERT(gsupc); + + /* struct ipaccess_unit has a non-const unit_name, so let's copy to be + * able to have a non-const unit_name here as well. To not taint the + * public gsup_client API, let's store it in a const char* anyway. */ + gsupc->unit_name = talloc_strdup(gsupc, unit_name); + OSMO_ASSERT(gsupc->unit_name); + + /* a NULL oapc_config will mark oap_state disabled. */ + rc = oap_client_init(oapc_config, &gsupc->oap_state); + if (rc != 0) + goto failed; + + gsupc->link = ipa_client_conn_create(gsupc, + /* no e1inp */ NULL, + 0, + ip_addr, tcp_port, + gsup_client_updown_cb, + gsup_client_read_cb, + /* default write_cb */ NULL, + gsupc); + if (!gsupc->link) + goto failed; + + osmo_timer_setup(&gsupc->connect_timer, connect_timer_cb, gsupc); + + rc = gsup_client_connect(gsupc); + + if (rc < 0) + goto failed; + + gsupc->read_cb = read_cb; + + return gsupc; + +failed: + gsup_client_destroy(gsupc); + return NULL; +} + +void gsup_client_destroy(struct gsup_client *gsupc) +{ + osmo_timer_del(&gsupc->connect_timer); + osmo_timer_del(&gsupc->ping_timer); + + if (gsupc->link) { + ipa_client_conn_close(gsupc->link); + ipa_client_conn_destroy(gsupc->link); + gsupc->link = NULL; + } + talloc_free(gsupc); +} + +int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg) +{ + if (!gsupc || !gsupc->is_connected) { + LOGP(DLGSUP, LOGL_ERROR, "GSUP not connected, unable to send %s\n", msgb_hexdump(msg)); + msgb_free(msg); + return -ENOTCONN; + } + + client_send(gsupc, IPAC_PROTO_EXT_GSUP, msg); + + return 0; +} + +struct msgb *gsup_client_msgb_alloc(void) +{ + return msgb_alloc_headroom(4000, 64, __func__); +} diff --git a/src/gsupclient/gsup_test_client.c b/src/gsupclient/gsup_test_client.c new file mode 100644 index 0000000..0ebcd4a --- /dev/null +++ b/src/gsupclient/gsup_test_client.c @@ -0,0 +1,321 @@ +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include <signal.h> + +#include <osmocom/core/linuxlist.h> +#include <osmocom/core/msgb.h> +#include <osmocom/core/select.h> +#include <osmocom/core/application.h> +#include <osmocom/core/utils.h> +#include <osmocom/core/logging.h> +#include <osmocom/gsm/gsup.h> + +#include <osmocom/msc/gsup_client.h> + +static struct gsup_client *g_gc; + + +/*********************************************************************** + * IMSI Operation + ***********************************************************************/ +static LLIST_HEAD(g_imsi_ops); + +struct imsi_op_stats { + uint32_t num_alloc; + uint32_t num_released; + uint32_t num_rx_success; + uint32_t num_rx_error; + uint32_t num_timeout; +}; + +enum imsi_op_type { + IMSI_OP_SAI, + IMSI_OP_LU, + IMSI_OP_ISD, + _NUM_IMSI_OP +}; + +static const struct value_string imsi_op_names[] = { + { IMSI_OP_SAI, "SAI" }, + { IMSI_OP_LU, "LU" }, + { IMSI_OP_ISD, "ISD" }, + { 0, NULL } +}; + +static struct imsi_op_stats imsi_op_stats[_NUM_IMSI_OP]; + +struct imsi_op { + struct llist_head list; + char imsi[17]; + enum imsi_op_type type; + struct osmo_timer_list timer; +}; + +static struct imsi_op *imsi_op_find(const char *imsi, + enum imsi_op_type type) +{ + struct imsi_op *io; + + llist_for_each_entry(io, &g_imsi_ops, list) { + if (!strcmp(io->imsi, imsi) && io->type == type) + return io; + } + return NULL; +} + +static void imsi_op_timer_cb(void *data); + +static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi, + enum imsi_op_type type) +{ + struct imsi_op *io; + + if (imsi_op_find(imsi, type)) + return NULL; + + io = talloc_zero(ctx, struct imsi_op); + OSMO_STRLCPY_ARRAY(io->imsi, imsi); + io->type = type; + osmo_timer_setup(&io->timer, imsi_op_timer_cb, io); + llist_add(&io->list, &g_imsi_ops); + imsi_op_stats[type].num_alloc++; + + return io; +} + +static void imsi_op_release(struct imsi_op *io) +{ + osmo_timer_del(&io->timer); + llist_del(&io->list); + imsi_op_stats[io->type].num_released++; + talloc_free(io); +} + +static void imsi_op_timer_cb(void *data) +{ + struct imsi_op *io = data; + printf("%s: Timer expiration\n", io->imsi); + imsi_op_stats[io->type].num_timeout++; + imsi_op_release(io); +} + +/* allocate + generate + send Send-Auth-Info */ +static int req_auth_info(const char *imsi) +{ + struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_SAI); + struct osmo_gsup_message gsup = {0}; + struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); + int rc; + + OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi); + gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; + + rc = osmo_gsup_encode(msg, &gsup); + if (rc < 0) { + printf("%s: encoding failure (%s)\n", imsi, strerror(-rc)); + return rc; + } + + return gsup_client_send(g_gc, msg); +} + +/* allocate + generate + send Send-Auth-Info */ +static int req_loc_upd(const char *imsi) +{ + struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU); + struct osmo_gsup_message gsup = {0}; + struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); + int rc; + + OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi); + gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; + + rc = osmo_gsup_encode(msg, &gsup); + if (rc < 0) { + printf("%s: encoding failure (%s)\n", imsi, strerror(-rc)); + return rc; + } + + return gsup_client_send(g_gc, msg); +} + +static int resp_isd(struct imsi_op *io) +{ + struct osmo_gsup_message gsup = {0}; + struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); + int rc; + + OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi); + gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; + + rc = osmo_gsup_encode(msg, &gsup); + if (rc < 0) { + printf("%s: encoding failure (%s)\n", io->imsi, strerror(-rc)); + return rc; + } + + imsi_op_release(io); + + return gsup_client_send(g_gc, msg); +} + +/* receive an incoming GSUP message */ +static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message *gsup) +{ + int is_error = 0, rc; + + if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) { + imsi_op_stats[io->type].num_rx_error++; + is_error = 1; + } else + imsi_op_stats[io->type].num_rx_success++; + + switch (io->type) { + case IMSI_OP_SAI: + printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : ""); + /* now that we have auth tuples, request LU */ + rc = req_loc_upd(io->imsi); + if (rc < 0) + printf("Failed to request Location Update for %s\n", io->imsi); + imsi_op_release(io); + break; + case IMSI_OP_LU: + printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : ""); + imsi_op_release(io); + break; + case IMSI_OP_ISD: + printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : ""); + rc = resp_isd(io); + if (rc < 0) + printf("Failed to insert subscriber data for %s\n", io->imsi); + break; + default: + printf("%s: Unknown\n", io->imsi); + imsi_op_release(io); + break; + } +} + +static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type) +{ + switch (msg_type) { + case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: + case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: + return IMSI_OP_SAI; + case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: + case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: + return IMSI_OP_LU; + case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: + return IMSI_OP_ISD; + default: + printf("Unknown GSUP msg_type %u\n", msg_type); + return -1; + } +} + +static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg) +{ + struct osmo_gsup_message gsup_msg = {0}; + struct imsi_op *io = NULL; + int rc; + + DEBUGP(DLGSUP, "Rx GSUP %s\n", msgb_hexdump(msg)); + + rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg); + if (rc < 0) + return rc; + + if (!gsup_msg.imsi[0]) + return -1; + + rc = op_type_by_gsup_msgt(gsup_msg.message_type); + if (rc < 0) + return rc; + + switch (rc) { + case IMSI_OP_SAI: + case IMSI_OP_LU: + io = imsi_op_find(gsup_msg.imsi, rc); + break; + case IMSI_OP_ISD: + /* ISD is an inbound transaction */ + io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD); + break; + } + if (!io) + return -1; + + imsi_op_rx_gsup(io, &gsup_msg); + msgb_free(msg); + + return 0; +} + +static void print_report(void) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) { + struct imsi_op_stats *st = &imsi_op_stats[i]; + const char *name = get_value_string(imsi_op_names, i); + printf("%s: %u alloc, %u released, %u success, %u error , %u tout\n", + name, st->num_alloc, st->num_released, st->num_rx_success, + st->num_rx_error, st->num_timeout); + } +} + +static void sig_cb(int sig) +{ + switch (sig) { + case SIGINT: + print_report(); + exit(0); + break; + } +} + +/* default categories */ +static struct log_info_cat default_categories[] = { +}; + +static const struct log_info gsup_test_client_log_info = { + .cat = default_categories, + .num_cat = ARRAY_SIZE(default_categories), +}; + +int main(int argc, char **argv) +{ + unsigned long long i; + char *server_host = "127.0.0.1"; + uint16_t server_port = OSMO_GSUP_PORT; + void *ctx = talloc_named_const(NULL, 0, "gsup_test_client"); + + osmo_init_logging2(ctx, &gsup_test_client_log_info); + + g_gc = gsup_client_create(ctx, "GSUPTEST", server_host, server_port, + gsupc_read_cb, NULL); + + + signal(SIGINT, sig_cb); + + for (i = 0; i < 10000; i++) { + unsigned long long imsi = 901790000000000 + i; + char imsi_buf[17] = { 0 }; + int rc; + + snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi); + rc = req_auth_info(imsi_buf); + if (rc < 0) + printf("Failed to request Auth Info for %s\n", imsi_buf); + + osmo_select_main(0); + } + + while (1) { + osmo_select_main(0); + } + + print_report(); + exit(0); +} diff --git a/src/gsupclient/oap_client.c b/src/gsupclient/oap_client.c new file mode 100644 index 0000000..9a0b60f --- /dev/null +++ b/src/gsupclient/oap_client.c @@ -0,0 +1,280 @@ +/* Osmocom Authentication Protocol API */ + +/* (C) 2015 by Sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Author: Neels Hofmeyr + * + * 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 <errno.h> + +#include <osmocom/core/utils.h> +#include <osmocom/core/logging.h> +#include <osmocom/crypt/auth.h> +#include <osmocom/gsm/oap.h> + +#include <osmocom/gsupclient/oap_client.h> + +int oap_client_init(struct oap_client_config *config, + struct oap_client_state *state) +{ + OSMO_ASSERT(state->state == OAP_UNINITIALIZED); + + if (!config) + goto disable; + + if (config->client_id == 0) + goto disable; + + if (config->secret_k_present == 0) { + LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret K missing.\n"); + goto disable; + } + + if (config->secret_opc_present == 0) { + LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret OPC missing.\n"); + goto disable; + } + + state->client_id = config->client_id; + memcpy(state->secret_k, config->secret_k, sizeof(state->secret_k)); + memcpy(state->secret_opc, config->secret_opc, sizeof(state->secret_opc)); + state->state = OAP_INITIALIZED; + return 0; + +disable: + state->state = OAP_DISABLED; + return 0; +} + +/* From the given state and received RAND and AUTN octets, validate the + * server's authenticity and formulate the matching milenage reply octets in + * *tx_xres. The state is not modified. + * On success, and if tx_res is not NULL, exactly 8 octets will be written to + * *tx_res. If not NULL, tx_res must point at allocated memory of at least 8 + * octets. The caller will want to send XRES back to the server in a challenge + * response message and update the state. + * Return 0 on success; -1 if OAP is disabled; -2 if rx_random and rx_autn fail + * the authentication check; -3 for any other errors. */ +static int oap_evaluate_challenge(const struct oap_client_state *state, + const uint8_t *rx_random, + const uint8_t *rx_autn, + uint8_t *tx_xres) +{ + struct osmo_auth_vector vec; + + struct osmo_sub_auth_data auth = { + .type = OSMO_AUTH_TYPE_UMTS, + .algo = OSMO_AUTH_ALG_MILENAGE, + }; + + osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.k) + == sizeof(state->secret_k), _secret_k_size_match); + osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.opc) + == sizeof(state->secret_opc), _secret_opc_size_match); + + switch (state->state) { + case OAP_UNINITIALIZED: + case OAP_DISABLED: + return -1; + default: + break; + } + + memcpy(auth.u.umts.k, state->secret_k, sizeof(auth.u.umts.k)); + memcpy(auth.u.umts.opc, state->secret_opc, sizeof(auth.u.umts.opc)); + memset(auth.u.umts.amf, '\0', sizeof(auth.u.umts.amf)); + auth.u.umts.sqn = 41; /* TODO use incrementing sequence nr */ + + memset(&vec, 0, sizeof(vec)); + osmo_auth_gen_vec(&vec, &auth, rx_random); + + if (vec.res_len != 8) { + LOGP(DLOAP, LOGL_ERROR, "OAP: Expected XRES to be 8 octets, got %d\n", + vec.res_len); + return -3; + } + + if (osmo_constant_time_cmp(vec.autn, rx_autn, sizeof(vec.autn)) != 0) { + LOGP(DLOAP, LOGL_ERROR, "OAP: AUTN mismatch!\n"); + LOGP(DLOAP, LOGL_INFO, "OAP: AUTN from server: %s\n", + osmo_hexdump_nospc(rx_autn, sizeof(vec.autn))); + LOGP(DLOAP, LOGL_INFO, "OAP: AUTN expected: %s\n", + osmo_hexdump_nospc(vec.autn, sizeof(vec.autn))); + return -2; + } + + if (tx_xres != NULL) + memcpy(tx_xres, vec.res, 8); + return 0; +} + +struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_msg) +{ + struct msgb *msg = msgb_alloc_headroom(1000, 64, __func__); + OSMO_ASSERT(msg); + osmo_oap_encode(msg, oap_msg); + return msg; +} + +/* Create a new msgb containing an OAP registration message. + * On error, return NULL. */ +static struct msgb* oap_msg_register(uint16_t client_id) +{ + struct osmo_oap_message oap_msg = {0}; + + if (client_id < 1) { + LOGP(DLOAP, LOGL_ERROR, "OAP: Invalid client ID: %d\n", client_id); + return NULL; + } + + oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST; + oap_msg.client_id = client_id; + return oap_client_encoded(&oap_msg); +} + +int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx) +{ + *msg_tx = oap_msg_register(state->client_id); + if (!(*msg_tx)) + return -1; + + state->state = OAP_REQUESTED_CHALLENGE; + return 0; +} + +/* Create a new msgb containing an OAP challenge response message. + * xres must point at 8 octets to return as challenge response. + * On error, return NULL. */ +static struct msgb* oap_msg_challenge_response(uint8_t *xres) +{ + struct osmo_oap_message oap_reply = {0}; + + oap_reply.message_type = OAP_MSGT_CHALLENGE_RESULT; + memcpy(oap_reply.xres, xres, sizeof(oap_reply.xres)); + oap_reply.xres_present = 1; + return oap_client_encoded(&oap_reply); +} + +static int handle_challenge(struct oap_client_state *state, + struct osmo_oap_message *oap_rx, + struct msgb **msg_tx) +{ + int rc; + uint8_t xres[8]; + + if (!(oap_rx->rand_present && oap_rx->autn_present)) { + LOGP(DLOAP, LOGL_ERROR, + "OAP challenge incomplete (rand_present: %d, autn_present: %d)\n", + oap_rx->rand_present, oap_rx->autn_present); + rc = -2; + goto failure; + } + + rc = oap_evaluate_challenge(state, + oap_rx->rand, + oap_rx->autn, + xres); + if (rc < 0) + goto failure; + + *msg_tx = oap_msg_challenge_response(xres); + if ((*msg_tx) == NULL) { + rc = -1; + goto failure; + } + + state->state = OAP_SENT_CHALLENGE_RESULT; + return 0; + +failure: + OSMO_ASSERT(rc < 0); + state->state = OAP_INITIALIZED; + return rc; +} + +int oap_client_handle(struct oap_client_state *state, + const struct msgb *msg_rx, struct msgb **msg_tx) +{ + uint8_t *data = msgb_l2(msg_rx); + size_t data_len = msgb_l2len(msg_rx); + struct osmo_oap_message oap_msg = {0}; + int rc = 0; + + *msg_tx = NULL; + + OSMO_ASSERT(data); + + rc = osmo_oap_decode(&oap_msg, data, data_len); + if (rc < 0) { + LOGP(DLOAP, LOGL_ERROR, + "Decoding OAP message failed with error '%s' (%d)\n", + get_value_string(gsm48_gmm_cause_names, -rc), -rc); + return -10; + } + + switch (state->state) { + case OAP_UNINITIALIZED: + LOGP(DLOAP, LOGL_ERROR, + "Received OAP message %d, but the OAP client is" + " not initialized\n", oap_msg.message_type); + return -ENOTCONN; + case OAP_DISABLED: + LOGP(DLOAP, LOGL_ERROR, + "Received OAP message %d, but the OAP client is" + " disabled\n", oap_msg.message_type); + return -ENOTCONN; + default: + break; + } + + switch (oap_msg.message_type) { + case OAP_MSGT_CHALLENGE_REQUEST: + return handle_challenge(state, &oap_msg, msg_tx); + + case OAP_MSGT_REGISTER_RESULT: + /* successfully registered */ + state->state = OAP_REGISTERED; + break; + + case OAP_MSGT_REGISTER_ERROR: + LOGP(DLOAP, LOGL_ERROR, + "OAP registration failed\n"); + state->state = OAP_INITIALIZED; + if (state->registration_failures < 3) { + state->registration_failures++; + return oap_client_register(state, msg_tx); + } + return -11; + + case OAP_MSGT_REGISTER_REQUEST: + case OAP_MSGT_CHALLENGE_RESULT: + LOGP(DLOAP, LOGL_ERROR, + "Received invalid OAP message type for OAP client side: %d\n", + (int)oap_msg.message_type); + return -12; + + default: + LOGP(DLOAP, LOGL_ERROR, + "Unknown OAP message type: %d\n", + (int)oap_msg.message_type); + return -13; + } + + return 0; +} -- To view, visit https://gerrit.osmocom.org/10109 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: Ief50054ad135551625b684ed8a0486f7af0b2940 Gerrit-Change-Number: 10109 Gerrit-PatchSet: 1 Gerrit-Owner: Harald Welte <laforge at gnumonks.org> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180723/fd1f85ee/attachment.htm>