From 94ec9bd9f311fc2ee94bf2d68e1fdfeb531ed5fa Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Wed, 4 Dec 2019 01:04:32 +0100 Subject: 2/2: fixup: add osmo_gsup_peer_id with type enum and union During code review it was requested to insert an ability to handle different kinds of peer id, in order to be able to add a Global Title in the future. Add this, but only in the publicly visible API. For osmo-hlr internal code, I intend to push implementing this into the future, when a different peer identification actually gets introduced. This way we don't need to implement it now in all osmo-hlr code paths (save time now), but still make all API users aware that this type may be extended in the future. Change-Id: Ide9dcdca283ab989240cfc6e53e9211862a199c5 --- include/Makefile.am | 2 +- include/osmocom/gsupclient/gsup_peer_id.h | 66 +++++++++++ include/osmocom/gsupclient/gsup_req.h | 10 +- include/osmocom/gsupclient/ipa_name.h | 38 ------ include/osmocom/hlr/db.h | 2 +- include/osmocom/hlr/gsup_server.h | 2 +- src/Makefile.am | 2 +- src/db_hlr.c | 2 +- src/gsup_server.c | 39 +++++-- src/gsupclient/Makefile.am | 2 +- src/gsupclient/gsup_peer_id.c | 187 ++++++++++++++++++++++++++++++ src/gsupclient/gsup_req.c | 13 ++- src/gsupclient/ipa_name.c | 97 ---------------- src/hlr.c | 2 +- src/hlr_ussd.c | 27 +++-- src/lu_fsm.c | 30 +++-- tests/db/Makefile.am | 2 +- tests/db/db_test.c | 2 +- tests/gsup_server/Makefile.am | 2 +- 19 files changed, 346 insertions(+), 181 deletions(-) create mode 100644 include/osmocom/gsupclient/gsup_peer_id.h delete mode 100644 include/osmocom/gsupclient/ipa_name.h create mode 100644 src/gsupclient/gsup_peer_id.c delete mode 100644 src/gsupclient/ipa_name.c diff --git a/include/Makefile.am b/include/Makefile.am index aeeb03f..cf95854 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = osmocom nobase_include_HEADERS = \ - osmocom/gsupclient/ipa_name.h \ + osmocom/gsupclient/gsup_peer_id.h \ osmocom/gsupclient/gsup_client.h \ osmocom/gsupclient/gsup_req.h \ osmocom/mslookup/mdns.h \ diff --git a/include/osmocom/gsupclient/gsup_peer_id.h b/include/osmocom/gsupclient/gsup_peer_id.h new file mode 100644 index 0000000..69c0776 --- /dev/null +++ b/include/osmocom/gsupclient/gsup_peer_id.h @@ -0,0 +1,66 @@ +/* Copyright 2019 by sysmocom s.f.m.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once +#include +#include +#include + +/*! IPA Name: Arbitrary length blob, not necessarily zero-terminated. + * In osmo-hlr, struct hlr_subscriber is mostly used as static reference and cannot serve as talloc context, which is + * why this is also implemented as a fixed-maximum-size buffer instead of a talloc'd arbitrary sized buffer. + * NOTE: The length of val may be extended in the future if it becomes necessary. + * At the time of writing, this holds IPA unit name strings of very limited length. + */ +struct osmo_ipa_name { + size_t len; + uint8_t val[128]; +}; + +bool osmo_ipa_name_is_empty(struct osmo_ipa_name *ipa_name); +int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len); +int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...); +int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b); +const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name); +const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name); + +enum osmo_gsup_peer_id_type { + OSMO_GSUP_PEER_ID_EMPTY=0, + OSMO_GSUP_PEER_ID_IPA_NAME, + /* OSMO_GSUP_PEER_ID_GLOBAL_TITLE, <-- currently not implemented, but likely future possibility */ +}; + +extern const struct value_string osmo_gsup_peer_id_type_names[]; +static inline const char *osmo_gsup_peer_id_type_name(enum osmo_gsup_peer_id_type val) +{ return get_value_string(osmo_gsup_peer_id_type_names, val); } + +struct osmo_gsup_peer_id { + enum osmo_gsup_peer_id_type type; + union { + struct osmo_ipa_name ipa_name; + }; +}; + +bool osmo_gsup_peer_id_is_empty(struct osmo_gsup_peer_id *gsup_peer_id); +int osmo_gsup_peer_id_set(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, + const uint8_t *val, size_t len); +int osmo_gsup_peer_id_set_str(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, + const char *str_fmt, ...); +int osmo_gsup_peer_id_cmp(const struct osmo_gsup_peer_id *a, const struct osmo_gsup_peer_id *b); +const char *osmo_gsup_peer_id_to_str(const struct osmo_gsup_peer_id *gsup_peer_id); +const char *osmo_gsup_peer_id_to_str_c(void *ctx, const struct osmo_gsup_peer_id *gsup_peer_id); diff --git a/include/osmocom/gsupclient/gsup_req.h b/include/osmocom/gsupclient/gsup_req.h index 09959f0..a8d74f5 100644 --- a/include/osmocom/gsupclient/gsup_req.h +++ b/include/osmocom/gsupclient/gsup_req.h @@ -19,14 +19,14 @@ #pragma once #include -#include +#include struct osmo_gsup_req; #define LOG_GSUP_REQ_CAT_SRC(req, subsys, level, file, line, fmt, args...) \ LOGPSRC(subsys, level, file, line, "GSUP %u: %s: IMSI-%s %s: " fmt, \ (req) ? (req)->nr : 0, \ - (req) ? osmo_ipa_name_to_str(&(req)->source_name) : "NULL", \ + (req) ? osmo_gsup_peer_id_to_str(&(req)->source_name) : "NULL", \ (req) ? (req)->gsup.imsi : "NULL", \ (req) ? osmo_gsup_message_type_name((req)->gsup.message_type) : "NULL", \ ##args) @@ -56,11 +56,11 @@ struct osmo_gsup_req { /* The ultimate source of this message: the source_name form the GSUP message, or, if not present, then the * immediate GSUP peer. GSUP messages going via a proxy reflect the initial source in the source_name. * This source_name is implicitly added to the routes for the conn the message was received on. */ - struct osmo_ipa_name source_name; + struct osmo_gsup_peer_id source_name; /* If the source_name is not an immediate GSUP peer, this is set to the closest intermediate peer between here * and source_name. */ - struct osmo_ipa_name via_proxy; + struct osmo_gsup_peer_id via_proxy; /* Identify this request by number, for logging. */ unsigned int nr; @@ -82,7 +82,7 @@ struct osmo_gsup_req { struct msgb *msg; }; -struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg, +struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_gsup_peer_id *from_peer, struct msgb *msg, osmo_gsup_req_send_response_t send_response_cb, void *cb_data, struct llist_head *add_to_list); void osmo_gsup_req_free(struct osmo_gsup_req *req); diff --git a/include/osmocom/gsupclient/ipa_name.h b/include/osmocom/gsupclient/ipa_name.h deleted file mode 100644 index 73cd1fb..0000000 --- a/include/osmocom/gsupclient/ipa_name.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2019 by sysmocom s.f.m.c. GmbH - * - * All Rights Reserved - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#pragma once -#include -#include - -/*! IPA Name: Arbitrary length blob, not necessarily zero-terminated. - * In osmo-hlr, struct hlr_subscriber is mostly used as static reference and cannot serve as talloc context, which is - * why this is also implemented as a fixed-maximum-size buffer instead of a talloc'd arbitrary sized buffer. - * NOTE: The length of val may be extended in the future if it becomes necessary. - * At the time of writing, this holds IPA unit name strings of very limited length. - */ -struct osmo_ipa_name { - size_t len; - uint8_t val[128]; -}; - -int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len); -int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...); -int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b); -const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name); -const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name); diff --git a/include/osmocom/hlr/db.h b/include/osmocom/hlr/db.h index 5c627be..02ab7e2 100644 --- a/include/osmocom/hlr/db.h +++ b/include/osmocom/hlr/db.h @@ -3,7 +3,7 @@ #include #include -#include +#include struct hlr; diff --git a/include/osmocom/hlr/gsup_server.h b/include/osmocom/hlr/gsup_server.h index 149971a..0ecf42c 100644 --- a/include/osmocom/hlr/gsup_server.h +++ b/include/osmocom/hlr/gsup_server.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #ifndef OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN diff --git a/src/Makefile.am b/src/Makefile.am index bfbe775..7841d42 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -72,7 +72,7 @@ osmo_hlr_db_tool_SOURCES = \ logging.c \ rand_urandom.c \ dbd_decode_binary.c \ - $(srcdir)/gsupclient/ipa_name.c \ + $(srcdir)/gsupclient/gsup_peer_id.c \ $(NULL) osmo_hlr_db_tool_LDADD = \ diff --git a/src/db_hlr.c b/src/db_hlr.c index fdac75f..ba13324 100644 --- a/src/db_hlr.c +++ b/src/db_hlr.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #define LOGHLR(imsi, level, fmt, args ...) LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args) diff --git a/src/gsup_server.c b/src/gsup_server.c index ba2d456..d4cdcca 100644 --- a/src/gsup_server.c +++ b/src/gsup_server.c @@ -65,13 +65,21 @@ int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg) static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response) { struct osmo_gsup_server *server = req->cb_data; - struct osmo_gsup_conn *conn; + struct osmo_gsup_conn *conn = NULL; struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx"); int rc; - conn = gsup_route_find_by_ipa_name(server, &req->source_name); + switch (req->source_name.type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + conn = gsup_route_find_by_ipa_name(server, &req->source_name.ipa_name); + break; + default: + LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n", + osmo_gsup_peer_id_type_name(req->source_name.type)); + break; + } if (!conn) { - LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request was disconnected, cannot respond\n"); + LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n"); msgb_free(msg); return; } @@ -91,19 +99,34 @@ static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg) { - struct osmo_gsup_req *req = osmo_gsup_req_new(conn->server, &conn->peer_name, msg, gsup_server_send_req_response, - conn->server, NULL); + struct osmo_gsup_req *req; + struct osmo_gsup_peer_id gpi = { + .type = OSMO_GSUP_PEER_ID_IPA_NAME, + .ipa_name = conn->peer_name, + }; + + req = osmo_gsup_req_new(conn->server, &gpi, msg, gsup_server_send_req_response, conn->server, NULL); if (!req) return NULL; - if (req->via_proxy.len) { + if (!osmo_gsup_peer_id_is_empty(&req->via_proxy)) { + switch (req->via_proxy.type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + break; + default: + LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n", + osmo_gsup_peer_id_type_name(req->source_name.type)); + osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); + return NULL; + } + /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that * source. Add it to the routes for this conn (so we can route responses back). */ - if (gsup_route_add_ipa_name(conn, &req->source_name)) { + if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) { LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP message received from %s via peer %s, but there already exists a" " different route to this source, message is not routable\n", - osmo_ipa_name_to_str(&req->source_name), + osmo_gsup_peer_id_to_str(&req->source_name), osmo_ipa_name_to_str(&conn->peer_name)); osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); return NULL; diff --git a/src/gsupclient/Makefile.am b/src/gsupclient/Makefile.am index 38b1582..a8674e4 100644 --- a/src/gsupclient/Makefile.am +++ b/src/gsupclient/Makefile.am @@ -9,7 +9,7 @@ AM_CFLAGS = -Wall $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/incl lib_LTLIBRARIES = libosmo-gsup-client.la libosmo_gsup_client_la_SOURCES = \ - ipa_name.c \ + gsup_peer_id.c \ gsup_client.c \ gsup_req.c \ $(NULL) diff --git a/src/gsupclient/gsup_peer_id.c b/src/gsupclient/gsup_peer_id.c new file mode 100644 index 0000000..7b3efd5 --- /dev/null +++ b/src/gsupclient/gsup_peer_id.c @@ -0,0 +1,187 @@ +/* Copyright 2019 by sysmocom s.f.m.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include +#include +#include + +bool osmo_ipa_name_is_empty(struct osmo_ipa_name *ipa_name) +{ + return (!ipa_name) || (!ipa_name->len); +} + +int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len) +{ + if (!val || !len) { + *ipa_name = (struct osmo_ipa_name){}; + return 0; + } + if (len > sizeof(ipa_name->val)) + return -ENOSPC; + ipa_name->len = len; + memcpy(ipa_name->val, val, len); + return 0; +} + +static int osmo_ipa_name_set_str_va(struct osmo_ipa_name *ipa_name, const char *str_fmt, va_list ap) +{ + if (!str_fmt) + return osmo_ipa_name_set(ipa_name, NULL, 0); + vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap); + ipa_name->len = strlen((char*)(ipa_name->val))+1; + return 0; +} + +int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...) +{ + va_list ap; + int rc; + va_start(ap, str_fmt); + rc = osmo_ipa_name_set_str_va(ipa_name, str_fmt, ap); + va_end(ap); + return rc; +} + +int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b) +{ + int cmp; + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + if (!a->len && !b->len) + return 0; + if (!a->len && b->len) + return -1; + if (!b->len && a->len) + return 1; + + if (a->len == b->len) + return memcmp(a->val, b->val, a->len); + else if (a->len < b->len) { + cmp = memcmp(a->val, b->val, a->len); + if (!cmp) + cmp = -1; + return cmp; + } else { + /* a->len > b->len */ + cmp = memcmp(a->val, b->val, b->len); + if (!cmp) + cmp = 1; + return cmp; + } +} + +/* Call osmo_ipa_name_to_str_c with OTC_SELECT. */ +const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name) +{ + return osmo_ipa_name_to_str_c(OTC_SELECT, ipa_name); +} + +/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ +const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name) +{ + size_t len = ipa_name->len; + if (!len) + return talloc_strdup(ctx, ""); + if (ipa_name->val[len-1] == '\0') + len--; + return osmo_escape_str_c(ctx, (char*)ipa_name->val, len); +} + +bool osmo_gsup_peer_id_is_empty(struct osmo_gsup_peer_id *gsup_peer_id) +{ + if (!gsup_peer_id) + return true; + switch (gsup_peer_id->type) { + case OSMO_GSUP_PEER_ID_EMPTY: + return true; + case OSMO_GSUP_PEER_ID_IPA_NAME: + return osmo_ipa_name_is_empty(&gsup_peer_id->ipa_name); + default: + return false; + } +} +int osmo_gsup_peer_id_set(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, + const uint8_t *val, size_t len) +{ + gsup_peer_id->type = type; + switch (type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + return osmo_ipa_name_set(&gsup_peer_id->ipa_name, val, len); + default: + return -EINVAL; + } +} + +int osmo_gsup_peer_id_set_str(struct osmo_gsup_peer_id *gsup_peer_id, enum osmo_gsup_peer_id_type type, + const char *str_fmt, ...) +{ + va_list ap; + int rc; + + *gsup_peer_id = (struct osmo_gsup_peer_id){}; + + switch (type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + gsup_peer_id->type = OSMO_GSUP_PEER_ID_IPA_NAME; + va_start(ap, str_fmt); + rc = osmo_ipa_name_set_str_va(&gsup_peer_id->ipa_name, str_fmt, ap); + va_end(ap); + return rc; + default: + return -EINVAL; + } +} + +int osmo_gsup_peer_id_cmp(const struct osmo_gsup_peer_id *a, const struct osmo_gsup_peer_id *b) +{ + if (a->type != b->type) + return OSMO_CMP(a->type, b->type); + switch (a->type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + return osmo_ipa_name_cmp(&a->ipa_name, &b->ipa_name); + default: + return -EINVAL; + } +} + +const struct value_string osmo_gsup_peer_id_type_names[] = { + { OSMO_GSUP_PEER_ID_IPA_NAME, "IPA-name" }, + {} +}; + +/* Call osmo_gsup_peer_id_to_str_c with OTC_SELECT */ +const char *osmo_gsup_peer_id_to_str(const struct osmo_gsup_peer_id *gpi) +{ + return osmo_gsup_peer_id_to_str_c(OTC_SELECT, gpi); +} + +/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ +const char *osmo_gsup_peer_id_to_str_c(void *ctx, const struct osmo_gsup_peer_id *gpi) +{ + switch (gpi->type) { + case OSMO_GSUP_PEER_ID_IPA_NAME: + return osmo_ipa_name_to_str_c(ctx, &gpi->ipa_name); + default: + return talloc_strdup(ctx, osmo_gsup_peer_id_type_name(gpi->type)); + } +} diff --git a/src/gsupclient/gsup_req.c b/src/gsupclient/gsup_req.c index df834eb..7cefbdf 100644 --- a/src/gsupclient/gsup_req.c +++ b/src/gsupclient/gsup_req.c @@ -44,7 +44,7 @@ * \param[inout] add_to_list List to which to append this request, or NULL for no list. * \return a newly allocated osmo_gsup_req, or NULL on error. */ -struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *from_peer, struct msgb *msg, +struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_gsup_peer_id *from_peer, struct msgb *msg, osmo_gsup_req_send_response_t send_response_cb, void *cb_data, struct llist_head *add_to_list) { @@ -54,7 +54,7 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f if (!msgb_l2(msg) || !msgb_l2len(msg)) { LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: missing or empty L2 data\n", - osmo_ipa_name_to_str(from_peer)); + osmo_gsup_peer_id_to_str(from_peer)); msgb_free(msg); return NULL; } @@ -70,7 +70,7 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f req->source_name = *from_peer; rc = osmo_gsup_decode(msgb_l2(req->msg), msgb_l2len(req->msg), (struct osmo_gsup_message*)&req->gsup); if (rc < 0) { - LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_ipa_name_to_str(from_peer), rc); + LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: cannot decode (rc=%d)\n", osmo_gsup_peer_id_to_str(from_peer), rc); osmo_gsup_req_free(req); return NULL; } @@ -78,17 +78,18 @@ struct osmo_gsup_req *osmo_gsup_req_new(void *ctx, const struct osmo_ipa_name *f LOG_GSUP_REQ(req, LOGL_DEBUG, "new request: {%s}\n", osmo_gsup_message_to_str_c(OTC_SELECT, &req->gsup)); if (req->gsup.source_name_len) { - if (osmo_ipa_name_set(&req->source_name, req->gsup.source_name, req->gsup.source_name_len)) { + if (osmo_gsup_peer_id_set(&req->source_name, OSMO_GSUP_PEER_ID_IPA_NAME, + req->gsup.source_name, req->gsup.source_name_len)) { LOGP(DLGSUP, LOGL_ERROR, "Rx GSUP from %s: failed to decode source_name, message is not routable\n", - osmo_ipa_name_to_str(from_peer)); + osmo_gsup_peer_id_to_str(from_peer)); osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true); return NULL; } /* The source of the GSUP message is not the immediate GSUP peer; the peer is our proxy for that source. */ - if (osmo_ipa_name_cmp(&req->source_name, from_peer)) + if (osmo_gsup_peer_id_cmp(&req->source_name, from_peer)) req->via_proxy = *from_peer; } diff --git a/src/gsupclient/ipa_name.c b/src/gsupclient/ipa_name.c deleted file mode 100644 index 2db069f..0000000 --- a/src/gsupclient/ipa_name.c +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2019 by sysmocom s.f.m.c. GmbH - * - * All Rights Reserved - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include -#include -#include -#include - -int osmo_ipa_name_set(struct osmo_ipa_name *ipa_name, const uint8_t *val, size_t len) -{ - if (!val || !len) { - *ipa_name = (struct osmo_ipa_name){}; - return 0; - } - if (len > sizeof(ipa_name->val)) - return -ENOSPC; - ipa_name->len = len; - memcpy(ipa_name->val, val, len); - return 0; -} - -int osmo_ipa_name_set_str(struct osmo_ipa_name *ipa_name, const char *str_fmt, ...) -{ - va_list ap; - if (!str_fmt) - return osmo_ipa_name_set(ipa_name, NULL, 0); - - va_start(ap, str_fmt); - vsnprintf((char*)(ipa_name->val), sizeof(ipa_name->val), str_fmt, ap); - va_end(ap); - ipa_name->len = strlen((char*)(ipa_name->val))+1; - return 0; -} - -int osmo_ipa_name_cmp(const struct osmo_ipa_name *a, const struct osmo_ipa_name *b) -{ - int cmp; - if (a == b) - return 0; - if (!a) - return -1; - if (!b) - return 1; - if (!a->len && !b->len) - return 0; - if (!a->len && b->len) - return -1; - if (!b->len && a->len) - return 1; - - if (a->len == b->len) - return memcmp(a->val, b->val, a->len); - else if (a->len < b->len) { - cmp = memcmp(a->val, b->val, a->len); - if (!cmp) - cmp = -1; - return cmp; - } else { - /* a->len > b->len */ - cmp = memcmp(a->val, b->val, b->len); - if (!cmp) - cmp = 1; - return cmp; - } -} - -/* Call osmo_ipa_name_to_str_c with OTC_SELECT. */ -const char *osmo_ipa_name_to_str(const struct osmo_ipa_name *ipa_name) -{ - return osmo_ipa_name_to_str_c(OTC_SELECT, ipa_name); -} - -/* Return an unquoted string, not including the terminating zero. Used for writing VTY config. */ -const char *osmo_ipa_name_to_str_c(void *ctx, const struct osmo_ipa_name *ipa_name) -{ - size_t len = ipa_name->len; - if (!len) - return talloc_strdup(ctx, ""); - if (ipa_name->val[len-1] == '\0') - len--; - return osmo_escape_str_c(ctx, (char*)ipa_name->val, len); -} diff --git a/src/hlr.c b/src/hlr.c index 47b3211..30d56f8 100644 --- a/src/hlr.c +++ b/src/hlr.c @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/hlr_ussd.c b/src/hlr_ussd.c index aa7614e..36b3022 100644 --- a/src/hlr_ussd.c +++ b/src/hlr_ussd.c @@ -416,22 +416,24 @@ static bool ss_op_is_ussd(uint8_t opcode) } /* is this GSUP connection an EUSE (true) or not (false)? */ -static bool peer_name_is_euse(const struct osmo_ipa_name *peer_name) +static bool peer_name_is_euse(const struct osmo_gsup_peer_id *peer_name) { - if (peer_name->len <= 5) + if (peer_name->type != OSMO_GSUP_PEER_ID_IPA_NAME) return false; - if (!strncmp((char *)(peer_name->val), "EUSE-", 5)) - return true; - else + if (peer_name->ipa_name.len <= 5) return false; + return strncmp((char *)(peer_name->ipa_name.val), "EUSE-", 5) == 0; } -static struct hlr_euse *euse_by_name(const struct osmo_ipa_name *peer_name) +static struct hlr_euse *euse_by_name(const struct osmo_gsup_peer_id *peer_name) { if (!peer_name_is_euse(peer_name)) return NULL; - return euse_find(g_hlr, (const char*)(peer_name->val)+5); + /* above peer_name_is_euse() ensures this: */ + OSMO_ASSERT(peer_name->type == OSMO_GSUP_PEER_ID_IPA_NAME); + + return euse_find(g_hlr, (const char*)(peer_name->ipa_name.val)+5); } static int handle_ss(struct ss_session *ss, bool is_euse_originated, const struct osmo_gsup_message *gsup, @@ -519,6 +521,14 @@ void rx_proc_ss_req(struct osmo_gsup_req *gsup_req) LOGP(DSS, LOGL_DEBUG, "%s/0x%08x: Process SS (%s)\n", gsup->imsi, gsup->session_id, osmo_gsup_session_state_name(gsup->session_state)); + if (gsup_req->source_name.type != OSMO_GSUP_PEER_ID_IPA_NAME) { + LOGP(DSS, LOGL_ERROR, "%s/0x%082x: Unable to process SS request: Unsupported GSUP peer id type%s\n", + gsup->imsi, gsup->session_id, + osmo_gsup_peer_id_type_name(gsup_req->source_name.type)); + osmo_gsup_req_respond_err(gsup_req, GMM_CAUSE_PROTO_ERR_UNSPEC, "error processing SS request"); + return; + } + /* decode and find out what kind of SS message it is */ if (gsup->ss_info && gsup->ss_info_len) { if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) { @@ -556,7 +566,8 @@ void rx_proc_ss_req(struct osmo_gsup_req *gsup_req) if (!is_euse_originated) { ss->initial_req_from_ms = gsup_req; free_gsup_req = NULL; - ss->vlr_name = gsup_req->source_name; + OSMO_ASSERT(gsup_req->source_name.type == OSMO_GSUP_PEER_ID_IPA_NAME); /* checked above */ + ss->vlr_name = gsup_req->source_name.ipa_name; } else { ss->initial_req_from_euse = gsup_req; free_gsup_req = NULL; diff --git a/src/lu_fsm.c b/src/lu_fsm.c index f430793..af3bed3 100644 --- a/src/lu_fsm.c +++ b/src/lu_fsm.c @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include #include @@ -52,11 +52,11 @@ struct lu { bool is_ps; /* VLR requesting the LU. */ - struct osmo_ipa_name vlr_name; + struct osmo_gsup_peer_id vlr_name; /* If the LU request was received via a proxy and not immediately from a local VLR, this indicates the closest * peer that forwarded the GSUP message. */ - struct osmo_ipa_name via_proxy; + struct osmo_gsup_peer_id via_proxy; }; LLIST_HEAD(g_all_lu); @@ -130,7 +130,7 @@ static void lu_start(struct osmo_gsup_req *update_location_req) osmo_fsm_inst_update_id_f_sanitize(fi, '_', "%s:IMSI-%s", lu->is_ps ? "PS" : "CS", update_location_req->gsup.imsi); - if (!lu->vlr_name.len) { + if (osmo_gsup_peer_id_is_empty(&lu->vlr_name)) { lu_failure(lu, GMM_CAUSE_NET_FAIL, "LU without a VLR"); return; } @@ -163,18 +163,30 @@ static void lu_start(struct osmo_gsup_req *update_location_req) #endif /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */ - if (lu->via_proxy.len) { + if (!osmo_gsup_peer_id_is_empty(&lu->via_proxy)) { LOG_GSUP_REQ(update_location_req, LOGL_DEBUG, "storing %s = %s, via proxy %s\n", lu->is_ps ? "SGSN number" : "VLR number", - osmo_ipa_name_to_str(&lu->vlr_name), - osmo_ipa_name_to_str(&lu->via_proxy)); + osmo_gsup_peer_id_to_str(&lu->vlr_name), + osmo_gsup_peer_id_to_str(&lu->via_proxy)); } else { LOG_GSUP_REQ(update_location_req, LOGL_DEBUG, "storing %s = %s\n", lu->is_ps ? "SGSN number" : "VLR number", - osmo_ipa_name_to_str(&lu->vlr_name)); + osmo_gsup_peer_id_to_str(&lu->vlr_name)); } - if (db_subscr_lu(g_hlr->dbc, lu->subscr.id, &lu->vlr_name, lu->is_ps, &lu->via_proxy)) { + if (osmo_gsup_peer_id_is_empty(&lu->vlr_name) + || (lu->vlr_name.type != OSMO_GSUP_PEER_ID_IPA_NAME)) { + lu_failure(lu, GMM_CAUSE_PROTO_ERR_UNSPEC, "Unsupported GSUP peer id type for vlr_name: %s", + osmo_gsup_peer_id_type_name(lu->vlr_name.type)); + return; + } + if (!osmo_gsup_peer_id_is_empty(&lu->via_proxy) && (lu->via_proxy.type != OSMO_GSUP_PEER_ID_IPA_NAME)) { + lu_failure(lu, GMM_CAUSE_PROTO_ERR_UNSPEC, "Unsupported GSUP peer id type for via_proxy: %s", + osmo_gsup_peer_id_type_name(lu->via_proxy.type)); + return; + } + if (db_subscr_lu(g_hlr->dbc, lu->subscr.id, &lu->vlr_name.ipa_name, lu->is_ps, + osmo_gsup_peer_id_is_empty(&lu->via_proxy)? NULL : &lu->via_proxy.ipa_name)) { lu_failure(lu, GMM_CAUSE_NET_FAIL, "Cannot update %s in the database", lu->is_ps ? "SGSN number" : "VLR number"); return; diff --git a/tests/db/Makefile.am b/tests/db/Makefile.am index 0194957..d1fdb48 100644 --- a/tests/db/Makefile.am +++ b/tests/db/Makefile.am @@ -30,7 +30,7 @@ db_test_LDADD = \ $(top_builddir)/src/db_auc.o \ $(top_builddir)/src/db_hlr.o \ $(top_builddir)/src/db.o \ - $(top_builddir)/src/ipa_name.o \ + $(top_builddir)/src/gsup_peer_id.o \ $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) \ $(LIBOSMOABIS_LIBS) \ diff --git a/tests/db/db_test.c b/tests/db/db_test.c index c53baed..4a0f3e8 100644 --- a/tests/db/db_test.c +++ b/tests/db/db_test.c @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include diff --git a/tests/gsup_server/Makefile.am b/tests/gsup_server/Makefile.am index 48fda88..6df538f 100644 --- a/tests/gsup_server/Makefile.am +++ b/tests/gsup_server/Makefile.am @@ -31,7 +31,7 @@ gsup_server_test_SOURCES = \ gsup_server_test_LDADD = \ $(top_srcdir)/src/gsup_server.c \ $(top_srcdir)/src/gsup_router.c \ - $(top_srcdir)/src/gsupclient/ipa_name.c \ + $(top_srcdir)/src/gsupclient/gsup_peer_id.c \ $(top_srcdir)/src/gsupclient/gsup_req.c \ $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) \ -- cgit v1.2.3