Add GTP hub initial code base.

First steps towards a new GTP hub. The aim is to mux GTP connections, so that
multiple SGSN <--> GGSN links can pass through a single point. Background:
allow having more than one SGSN, possibly in various remote locations.

The recent addition of OAP to GSUP is related to the same background idea.

(This is a collapsed patch of various changes that do not make sense to review
in chronological order anymore, since a lot of it has thorougly transmorphed
after it was first committed.)

Sponsored-by: On-Waves ehf
This commit is contained in:
Neels Hofmeyr 2015-09-24 17:32:30 +02:00
parent 65482c919f
commit c8a614d2e9
16 changed files with 3854 additions and 1 deletions

2
openbsc/.gitignore vendored
View File

@ -53,6 +53,7 @@ src/utils/isdnsync
src/nat/bsc_nat
src/gprs/osmo-sgsn
src/gprs/osmo-gbproxy
src/gprs/osmo-gtphub
src/osmo-bsc_nat/osmo-bsc_nat
#tests
@ -78,6 +79,7 @@ tests/mgcp/mgcp_transcoding_test
tests/sgsn/sgsn_test
tests/subscr/subscr_test
tests/oap/oap_test
tests/gtphub/gtphub_test
tests/atconfig
tests/atlocal

View File

@ -210,6 +210,7 @@ AC_OUTPUT(
tests/sgsn/Makefile
tests/subscr/Makefile
tests/oap/Makefile
tests/gtphub/Makefile
doc/Makefile
doc/examples/Makefile
Makefile)

View File

@ -18,6 +18,7 @@ noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \
gprs_gb_parse.h smpp.h meas_feed.h gprs_gsup_messages.h \
gprs_gsup_client.h bsc_msg_filter.h \
oap.h oap_messages.h
gtphub.h
openbsc_HEADERS = gsm_04_08.h meas_rep.h bsc_api.h
openbscdir = $(includedir)/openbsc

View File

@ -33,6 +33,7 @@ enum {
DCTRL,
DSMPP,
DFILTER,
DGTPHUB,
Debug_LastEntry,
};

View File

@ -0,0 +1,424 @@
/* GTP Hub Implementation */
/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
* 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>
#include <sys/socket.h>
#include <osmocom/core/select.h>
#include <osmocom/core/timer.h>
/* support */
/* TODO move to osmocom/core/socket.c ? */
#include <netdb.h> /* for IPPROTO_* etc */
struct osmo_sockaddr {
struct sockaddr_storage a;
socklen_t l;
};
/* TODO move to osmocom/core/socket.c ? */
/*! \brief Initialize a sockaddr
* \param[out] addr Valid osmo_sockaddr pointer to write result to
* \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
* \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
* \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP
* \param[in] host Remote host name or IP address in string form
* \param[in] port Remote port number in host byte order
* \returns 0 on success, otherwise an error code (from getaddrinfo()).
*
* Copy the first result from a getaddrinfo() call with the given parameters to
* *addr and *addr_len. On error, do not change *addr and return nonzero.
*/
int osmo_sockaddr_init(struct osmo_sockaddr *addr,
uint16_t family, uint16_t type, uint8_t proto,
const char *host, uint16_t port);
/* Conveniently pass AF_UNSPEC, SOCK_DGRAM and IPPROTO_UDP to
* osmo_sockaddr_init(). */
static inline int osmo_sockaddr_init_udp(struct osmo_sockaddr *addr,
const char *host, uint16_t port)
{
return osmo_sockaddr_init(addr, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port);
}
/*! \brief convert sockaddr to human readable string.
* \param[out] addr_str Valid pointer to a buffer of length addr_str_len.
* \param[in] addr_str_len Size of buffer addr_str points at.
* \param[out] port_str Valid pointer to a buffer of length port_str_len.
* \param[in] port_str_len Size of buffer port_str points at.
* \param[in] addr Binary representation as returned by osmo_sockaddr_init().
* \param[in] flags flags as passed to getnameinfo().
* \returns 0 on success, an error code on error.
*
* Return the IPv4 or IPv6 address string and the port (a.k.a. service) string
* representations of the given struct osmo_sockaddr in two caller provided
* char buffers. Flags of (NI_NUMERICHOST | NI_NUMERICSERV) return numeric
* address and port. Either one of addr_str or port_str may be NULL, in which
* case nothing is returned there.
*
* See also osmo_sockaddr_to_str() (less flexible, but much more convenient). */
int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
char *port_str, size_t port_str_len,
const struct osmo_sockaddr *addr,
int flags);
/*! \brief conveniently concatenate the parts returned by osmo_sockaddr_to_strs().
* \param[in] addr Binary representation as returned by osmo_sockaddr_init().
* \param[in] buf A buffer to use for string operations.
* \param[in] buf_len Length of the buffer.
* \returns Address string (in buffer).
*
* Compose a string of the numeric IP-address and port represented by *addr of
* the form "<ip-addr> port <port>". The returned string is valid until the
* next invocation of this function.
*/
const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
char *buf, size_t buf_len);
/*! \brief conveniently return osmo_sockaddr_to_strb() in a static buffer.
* \param[in] addr Binary representation as returned by osmo_sockaddr_init().
* \returns Address string in static buffer.
*
* See osmo_sockaddr_to_strb().
*
* Note: only one osmo_sockaddr_to_str() call will work per print/log
* statement. For two or more, use osmo_sockaddr_to_strb() with a separate
* buffer each.
*/
const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr);
/*! \brief compare two osmo_sockaddr.
* \param[in] a The first address to compare.
* \param[in] b The other address to compare.
* \returns 0 if equal, otherwise -1 or 1.
*/
int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, const struct osmo_sockaddr *b);
/*! \brief Overwrite *dst with *src.
* Like memcpy(), but copy only the valid bytes. */
void osmo_sockaddr_copy(struct osmo_sockaddr *dst, const struct osmo_sockaddr *src);
/* general */
enum gtphub_plane_idx {
GTPH_PLANE_CTRL = 0,
GTPH_PLANE_USER = 1,
GTPH_PLANE_N
};
extern const char* const gtphub_plane_idx_names[GTPH_PLANE_N];
extern const uint16_t gtphub_plane_idx_default_port[GTPH_PLANE_N];
/* A host address in the form that is expected in the 7.7.32 GSN Address IE.
* len is either 4 (IPv4) or 16 (IPv6), any other value is invalid. If no
* address is set, len shall be 0. */
struct gsn_addr {
uint16_t len;
uint8_t buf[16];
};
void gsn_addr_copy(struct gsn_addr *gsna, const struct gsn_addr *src);
int gsn_addr_from_str(struct gsn_addr *gsna, const char *numeric_addr_str);
/* Return gsna in numeric string form, in a static buffer. */
const char *gsn_addr_to_str(const struct gsn_addr *gsna);
/* note: strbuf_len doesn't need to be larger than INET6_ADDRSTRLEN + 1. */
const char *gsn_addr_to_strb(const struct gsn_addr *gsna,
char *strbuf, int strbuf_len);
/* Return 1 on match, zero otherwise. */
int gsn_addr_same(const struct gsn_addr *a, const struct gsn_addr *b);
/* expiry */
struct expiring_item;
typedef void (*del_cb_t)(struct expiring_item *);
struct expiring_item {
struct llist_head entry;
time_t expiry;
del_cb_t del_cb;
};
struct expiry {
int expiry_in_seconds;
struct llist_head items;
};
/* Initialize an expiry queue. */
void expiry_init(struct expiry *exq, int expiry_in_seconds);
/* Add a new mapping, or restart the expiry timeout for an already listed mapping. */
void expiry_add(struct expiry *exq, struct expiring_item *mapping, time_t now);
/* Remove the given item from its expiry queue, and call item->del_cb, if set.
* This sets item->del_cb to NULL and is harmless when run a second time on the
* same item, so the del_cb may choose to call this function, too, to allow
* deleting items from several code paths. */
void expiring_item_del(struct expiring_item *item);
/* Carry out due expiry of mappings. Must be invoked regularly.
* 'now' is the current clock count in seconds and must correspond to the clock
* count passed to nr_map_add(). A monotonous clock counter should be used. */
int expiry_tick(struct expiry *exq, time_t now);
/* number map */
/* A number map assigns a "random" mapped number to each user provided number.
* If the same number is requested multiple times, the same mapped number is
* returned.
*
* Number maps plug into possibly shared pools and expiry queues, for example:
*
* mapA -----------+-> pool1 <-+-- mapB
* {10->1, 11->5} | {1, 2, 3, ...} | {10->2, 11->3}
* | |
* | |
* /-> \-> expiry1 <-/
* | (30 seconds)
* |
* mapC -------+-----> pool2 <-+-- mapD
* {10->1, 11->3} {1, 2, 3, ...} | {10->2, 11->5}
* |
* expiry2 <-/
* (60 seconds)
*
* A map contains mappings ("10->1"). Each map needs a number pool, which can
* be shared with other maps. Each new mapping receives a number from the pool,
* which is then unavailable to any other map using the same pool.
*
* A map may point at an expiry queue, in which case all mappings added to it
* are also appended to the expiry queue (using a separate llist entry in the
* mapping). Any number of maps may submit to the same expiry queue, if they
* desire the same expiry timeout. An expiry queue stores the mappings in
* chronological order, so that expiry checking is needed only from the start
* of the queue; hence only mappings with identical expiry timeout can be added
* to the same expiry queue. Upon expiry, a mapping is dropped from the map it
* was submitted at. expiry_tick() needs to be called regularly for each expiry
* queue.
*
* A nr_mapping can be embedded in a larger struct: each mapping can have a
* distinct destructor (del_cb), and each del_cb can figure out the container
* struct's address and free that upon expiry or manual deletion. So in expiry
* queues (and even maps), mappings of different container types can be mixed.
* This can help to drastically reduce the amount of unnecessary visits during
* expiry checking, for the case that no expiry is pending. An expiry queue
* always knows which mappings to expire next, because they are right at the
* start of its list.
*
* Mapping allocation and a del_cb are provided by the caller. If del_cb is
* NULL, no deallocation will be done (allowing statically allocated entries).
*/
/* TODO at some point I thought the allocation & del_cb complexity was
* needed/helpful, but by now it seems like overkill. Maybe lose that again. */
typedef int nr_t;
/* Generator for unused numbers. So far this counts upwards from zero, but the
* implementation may change in the future. Treat this like an opaque struct.
* If this becomes random, the tests need to be fixed. */
struct nr_pool {
nr_t last_nr;
/* TODO add min, max, for safe wrapping */
};
struct nr_mapping {
struct llist_head entry;
struct expiring_item expiry_entry;
void *origin;
nr_t orig;
nr_t repl;
};
struct nr_map {
struct nr_pool *pool; /* multiple nr_maps can share a nr_pool. */
struct expiry *add_items_to_expiry;
struct llist_head mappings;
};
void nr_pool_init(struct nr_pool *pool);
/* Return the next unused number from the nr_pool. */
nr_t nr_pool_next(struct nr_pool *pool);
/* Initialize the nr_mapping to zero/empty values. */
void nr_mapping_init(struct nr_mapping *mapping);
/* Remove the given mapping from its parent map and expiry queue, and call
* mapping->del_cb, if set. */
void nr_mapping_del(struct nr_mapping *mapping);
/* Initialize an (already allocated) nr_map, and set the map's number pool.
* Multiple nr_map instances may use the same nr_pool. Set the nr_map's expiry
* queue to exq, so that all added mappings are automatically expired after the
* time configured in exq. exq may be NULL to disable automatic expiry. */
void nr_map_init(struct nr_map *map, struct nr_pool *pool,
struct expiry *exq);
/* Add a new entry to the map. mapping->orig, mapping->origin and
* mapping->del_cb must be set before calling this function. The remaining
* fields of *mapping will be overwritten. mapping->repl is set to the next
* available mapped number from map->pool. 'now' is the current clock count in
* seconds; if no map->expiry is used, just pass 0 for 'now'. */
void nr_map_add(struct nr_map *map, struct nr_mapping *mapping,
time_t now);
/* Return a known mapping from nr_orig and the given origin. If nr_orig is
* unknown, return NULL. */
struct nr_mapping *nr_map_get(const struct nr_map *map,
void *origin, nr_t nr_orig);
/* Return a known mapping to nr_repl. If nr_repl is unknown, return NULL. */
struct nr_mapping *nr_map_get_inv(const struct nr_map *map, nr_t nr_repl);
/* Remove all mappings from map. */
void nr_map_clear(struct nr_map *map);
/* Return 1 if map has no entries, 0 otherwise. */
int nr_map_empty(const struct nr_map *map);
/* config */
static const int GTPH_SEQ_MAPPING_EXPIRY_SECS = 30; /* TODO is there a spec for this? */
static const int GTPH_TEI_MAPPING_EXPIRY_MINUTES = 6 * 60; /* TODO is there a spec for this? */
struct gtphub_cfg_addr {
const char *addr_str;
uint16_t port;
};
struct gtphub_cfg_bind {
struct gtphub_cfg_addr bind;
};
struct gtphub_cfg {
struct gtphub_cfg_bind to_sgsns[GTPH_PLANE_N];
struct gtphub_cfg_bind to_ggsns[GTPH_PLANE_N];
struct gtphub_cfg_addr sgsn_proxy[GTPH_PLANE_N];
struct gtphub_cfg_addr ggsn_proxy[GTPH_PLANE_N];
};
/* state */
struct gtphub_peer {
struct llist_head entry;
struct llist_head addresses; /* Alternatives, not load balancing. */
struct nr_pool seq_pool;
struct nr_map seq_map;
};
struct gtphub_peer_addr {
struct llist_head entry;
struct gtphub_peer *peer;
struct gsn_addr addr;
struct llist_head ports;
};
struct gtphub_peer_port {
struct llist_head entry;
struct gtphub_peer_addr *peer_addr;
uint16_t port;
unsigned int ref_count; /* references from other peers' seq_maps */
struct osmo_sockaddr sa;
};
struct gtphub_bind {
struct gsn_addr local_addr;
struct osmo_fd ofd;
/* list of struct gtphub_peer */
struct llist_head peers;
};
struct gtphub {
struct gtphub_bind to_sgsns[GTPH_PLANE_N];
struct gtphub_bind to_ggsns[GTPH_PLANE_N];
/* pointers to an entry of to_sgsns[x].peers */
struct gtphub_peer_port *sgsn_proxy[GTPH_PLANE_N];
/* pointers to an entry of to_ggsns[x].peers */
struct gtphub_peer_port *ggsn_proxy[GTPH_PLANE_N];
struct nr_map tei_map[GTPH_PLANE_N];
struct nr_pool tei_pool[GTPH_PLANE_N];
struct osmo_timer_list gc_timer;
struct expiry expire_seq_maps;
struct expiry expire_tei_maps;
};
struct gtp_packet_desc;
/* api */
int gtphub_vty_init(void);
int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file);
/* Initialize and start gtphub: bind to ports, run expiry timers. */
int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg);
time_t gtphub_now(void);
/* Remove expired items, empty peers, ... */
void gtphub_gc(struct gtphub *hub, time_t now);
/* Return the string of the first address for this peer. */
const char *gtphub_peer_str(struct gtphub_peer *peer);
/* Same with a different static buffer. We often want to print two peers. */
const char *gtphub_peer_str2(struct gtphub_peer *peer);
int gtphub_from_sgsns_handle_buf(struct gtphub *hub,
unsigned int port_idx,
const struct osmo_sockaddr *from_addr,
uint8_t *buf,
size_t received,
time_t now,
struct osmo_fd **to_ofd,
struct osmo_sockaddr *to_addr);
int gtphub_from_ggsns_handle_buf(struct gtphub *hub,
unsigned int port_idx,
const struct osmo_sockaddr *from_addr,
uint8_t *buf,
size_t received,
time_t now,
struct osmo_fd **to_ofd,
struct osmo_sockaddr *to_addr);
struct gtphub_peer_port *gtphub_port_find_sa(const struct gtphub_bind *bind,
const struct osmo_sockaddr *addr);

View File

@ -36,6 +36,7 @@ enum bsc_vty_node {
BSC_NODE,
SMPP_NODE,
SMPP_ESME_NODE,
GTPHUB_NODE,
};
extern int bsc_vty_is_config_node(struct vty *vty, int node);

View File

@ -11,6 +11,7 @@ noinst_HEADERS = gprs_sndcp.h
bin_PROGRAMS = osmo-gbproxy
if HAVE_LIBGTP
bin_PROGRAMS += osmo-gtphub
if HAVE_LIBCARES
bin_PROGRAMS += osmo-sgsn
endif
@ -33,3 +34,8 @@ osmo_sgsn_LDADD = \
$(top_builddir)/src/libcommon/libcommon.a \
-lgtp $(OSMO_LIBS) $(LIBOSMOABIS_LIBS) $(LIBCARES_LIBS) \
$(LIBCRYPTO_LIBS) -lrt
osmo_gtphub_SOURCES = gtphub_main.c gtphub.c gtphub_ext.c gtphub_vty.c
osmo_gtphub_LDADD = \
$(top_builddir)/src/libcommon/libcommon.a \
-lgtp $(LIBOSMOCORE_LIBS) $(LIBOSMOVTY_LIBS) -lrt

2080
openbsc/src/gprs/gtphub.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,58 @@
/* GTP Hub Implementation */
/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
* All Rights Reserved
*
* gtphub_ext.c -- ext means extern. This file is kept separate so that these
* functions can be wrapped for gtphub_test.c. When a function and its callers
* are in the same compilational unit, the wrappability may be optimized away.
*
* 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 <openbsc/gtphub.h>
#include <osmocom/core/utils.h>
#define __llist_first(head) (((head)->next == (head)) ? NULL : (head)->next)
#define llist_first(head, type, entry) llist_entry(__llist_first(head), type, entry)
int gtphub_resolve_ggsn_addr(struct gtphub *hub,
struct osmo_sockaddr *result,
struct gtp_packet_desc *p)
{
/* TODO This is just hardcodedly returning the first known address.
* Should resolve from actual subscriber data. */
struct gtphub_peer *peer = llist_first(&hub->to_ggsns[GTPH_PLANE_CTRL].peers,
struct gtphub_peer, entry);
if (!peer)
return -1;
struct gtphub_peer_addr *pa = llist_first(&peer->addresses,
struct gtphub_peer_addr, entry);
if (!pa)
return -1;
struct gtphub_peer_port *pp = llist_first(&pa->ports,
struct gtphub_peer_port, entry);
if (!pp)
return -1;
*result = pp->sa;
return 0;
}

View File

@ -0,0 +1,282 @@
/* GTP Hub main program */
/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
* 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 <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <osmocom/core/signal.h>
#include <osmocom/core/application.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/rate_ctr.h>
#include <osmocom/vty/logging.h>
#include <osmocom/vty/telnet_interface.h>
#include <openbsc/debug.h>
#include <openbsc/gtphub.h>
#include <openbsc/vty.h>
#include "../../bscconfig.h"
#define LOGERR(fmt, args...) \
LOGP(DGTPHUB, LOGL_ERROR, fmt, ##args)
#define LOG(fmt, args...) \
LOGP(DGTPHUB, LOGL_NOTICE, fmt, ##args)
#ifndef OSMO_VTY_PORT_GTPHUB
/* should come from libosmocore */
#define OSMO_VTY_PORT_GTPHUB 4253
#endif
extern void *osmo_gtphub_ctx;
const char *gtphub_copyright =
"Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n"
"License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
"This is free software: you are free to change and redistribute it.\r\n"
"There is NO WARRANTY, to the extent permitted by law.\r\n";
static struct log_info_cat gtphub_categories[] = {
[DGTPHUB] = {
.name = "DGTPHUB",
.description = "GTP Hub",
.color = "\033[1;33m",
.enabled = 1, .loglevel = LOGL_NOTICE,
},
};
int gtphub_log_filter_fn(const struct log_context *ctx,
struct log_target *tar)
{
return 0;
}
static const struct log_info gtphub_log_info = {
.filter_fn = gtphub_log_filter_fn,
.cat = gtphub_categories,
.num_cat = ARRAY_SIZE(gtphub_categories),
};
void log_cfg(struct gtphub_cfg *cfg)
{
struct gtphub_cfg_addr *a;
a = &cfg->to_sgsns[GTPH_PLANE_CTRL].bind;
LOG("to-SGSNs bind, Control: %s port %d\n",
a->addr_str, a->port);
a = &cfg->to_sgsns[GTPH_PLANE_USER].bind;
LOG("to-SGSNs bind, User: %s port %d\n",
a->addr_str, a->port);
a = &cfg->to_ggsns[GTPH_PLANE_CTRL].bind;
LOG("to-GGSNs bind, Control: %s port %d\n",
a->addr_str, a->port);
a = &cfg->to_ggsns[GTPH_PLANE_USER].bind;
LOG("to-GGSNs bind, User: %s port %d\n",
a->addr_str, a->port);
}
static void signal_handler(int signal)
{
fprintf(stdout, "signal %u received\n", signal);
switch (signal) {
case SIGINT:
osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
sleep(1);
exit(0);
break;
case SIGABRT:
/* in case of abort, we want to obtain a talloc report
* and then return to the caller, who will abort the process */
case SIGUSR1:
case SIGUSR2:
talloc_report_full(osmo_gtphub_ctx, stderr);
break;
default:
break;
}
}
extern int bsc_vty_go_parent(struct vty *vty);
static struct vty_app_info vty_info = {
.name = "OsmoGTPhub",
.version = PACKAGE_VERSION,
.go_parent_cb = bsc_vty_go_parent,
.is_config_node = bsc_vty_is_config_node,
};
struct cmdline_cfg {
const char *config_file;
int daemonize;
};
static void print_help(struct cmdline_cfg *ccfg)
{
printf("gtphub commandline options\n");
printf(" -h --help This text.\n");
printf(" -D --daemonize Fork the process into a background daemon.\n");
printf(" -d,--debug <cat> Enable Debugging for this category.\n");
printf(" Pass '-d list' to get a category listing.\n");
printf(" -s --disable-color");
printf(" -c --config-file The config file to use [%s].\n", ccfg->config_file);
printf(" -e,--log-level <nr> Set a global log level.\n");
}
static void list_categories(void)
{
printf("Avaliable debug categories:\n");
int i;
for (i = 0; i < gtphub_log_info.num_cat; ++i) {
if (!gtphub_log_info.cat[i].name)
continue;
printf("%s\n", gtphub_log_info.cat[i].name);
}
}
static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv)
{
while (1) {
int option_index = 0, c;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"debug", 1, 0, 'd'},
{"daemonize", 0, 0, 'D'},
{"config-file", 1, 0, 'c'},
{"disable-color", 0, 0, 's'},
{"timestamp", 0, 0, 'T'},
{"log-level", 1, 0, 'e'},
{NULL, 0, 0, 0}
};
c = getopt_long(argc, argv, "hd:Dc:sTe:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
//print_usage();
print_help(ccfg);
exit(0);
case 's':
log_set_use_color(osmo_stderr_target, 0);
break;
case 'd':
if (strcmp("list", optarg) == 0) {
list_categories();
exit(0);
} else
log_parse_category_mask(osmo_stderr_target, optarg);
break;
case 'D':
ccfg->daemonize = 1;
break;
case 'c':
ccfg->config_file = optarg;
break;
case 'T':
log_set_print_timestamp(osmo_stderr_target, 1);
break;
case 'e':
log_set_log_level(osmo_stderr_target, atoi(optarg));
break;
default:
/* ignore */
break;
}
}
}
int main(int argc, char **argv)
{
int rc;
osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
signal(SIGINT, &signal_handler);
signal(SIGABRT, &signal_handler);
signal(SIGUSR1, &signal_handler);
signal(SIGUSR2, &signal_handler);
osmo_init_ignore_signals();
osmo_init_logging(&gtphub_log_info);
vty_info.copyright = gtphub_copyright;
vty_init(&vty_info);
logging_vty_add_cmds(&gtphub_log_info);
gtphub_vty_init();
rate_ctr_init(osmo_gtphub_ctx);
rc = telnet_init(osmo_gtphub_ctx, 0, OSMO_VTY_PORT_GTPHUB);
if (rc < 0)
exit(1);
struct cmdline_cfg _ccfg;
struct cmdline_cfg *ccfg = &_ccfg;
memset(ccfg, '\0', sizeof(*ccfg));
ccfg->config_file = "./gtphub.conf";
struct gtphub_cfg _cfg;
struct gtphub_cfg *cfg = &_cfg;
memset(cfg, '\0', sizeof(*cfg));
struct gtphub _hub;
struct gtphub *hub = &_hub;
handle_options(ccfg, argc, argv);
rc = gtphub_cfg_read(cfg, ccfg->config_file);
if (rc < 0) {
LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n", ccfg->config_file);
exit(2);
}
if (gtphub_start(hub, cfg) != 0)
return -1;
log_cfg(cfg);
if (ccfg->daemonize) {
rc = osmo_daemonize();
if (rc < 0) {
LOGERR("Error during daemonize");
exit(1);
}
}
while (1) {
rc = osmo_select_main(0);
if (rc < 0)
exit(3);
}
/* not reached */
exit(0);
}

View File

@ -0,0 +1,258 @@
/* (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 <osmocom/core/talloc.h>
#include <osmocom/vty/command.h>
#include <openbsc/vty.h>
#include <openbsc/gtphub.h>
static struct gtphub_cfg *g_cfg = 0;
static struct cmd_node gtphub_node = {
GTPHUB_NODE,
"%s(config-gtphub)# ",
1,
};
#define GTPH_DEFAULT_CONTROL_PORT 2123
#define GTPH_DEFAULT_USER_PORT 2152
static void write_addrs(struct vty *vty, const char *name,
struct gtphub_cfg_addr *c, struct gtphub_cfg_addr *u)
{
if ((c->port == GTPH_DEFAULT_CONTROL_PORT)
&& (u->port == GTPH_DEFAULT_USER_PORT)
&& (strcmp(c->addr_str, u->addr_str) == 0)) {
/* Default port numbers and same IP address: write "short"
* variant. */
vty_out(vty, " %s %s%s",
name,
c->addr_str,
VTY_NEWLINE);
return;
}
vty_out(vty, " %s ctrl %s %d user %s %d%s",
name,
c->addr_str, (int)c->port,
u->addr_str, (int)u->port,
VTY_NEWLINE);
}
static int config_write_gtphub(struct vty *vty)
{
vty_out(vty, "gtphub%s", VTY_NEWLINE);
write_addrs(vty, "bind-to-sgsns",
&g_cfg->to_sgsns[GTPH_PLANE_CTRL].bind,
&g_cfg->to_sgsns[GTPH_PLANE_USER].bind);
write_addrs(vty, "bind-to-ggsns",
&g_cfg->to_ggsns[GTPH_PLANE_CTRL].bind,
&g_cfg->to_ggsns[GTPH_PLANE_USER].bind);
if (g_cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str) {
write_addrs(vty, "ggsn-proxy",
&g_cfg->ggsn_proxy[GTPH_PLANE_CTRL],
&g_cfg->ggsn_proxy[GTPH_PLANE_USER]);
}
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub, cfg_gtphub_cmd,
"gtphub",
"Configure the GTP hub")
{
vty->node = GTPHUB_NODE;
return CMD_SUCCESS;
}
#define BIND_ARGS "ctrl ADDR <0-65535> user ADDR <0-65535>"
#define BIND_DOCS \
"Set GTP-C bind\n" \
"GTP-C local IP address (v4 or v6)\n" \
"GTP-C local port\n" \
"Set GTP-U bind\n" \
"GTP-U local IP address (v4 or v6)\n" \
"GTP-U local port\n"
DEFUN(cfg_gtphub_bind_to_sgsns_short, cfg_gtphub_bind_to_sgsns_short_cmd,
"bind-to-sgsns ADDR",
"GTP Hub Parameters\n"
"Set the local bind address to listen for SGSNs, for both GTP-C and GTP-U\n"
"Local IP address (v4 or v6)\n"
)
{
int i;
for (i = 0; i < GTPH_PLANE_N; i++)
g_cfg->to_sgsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->to_sgsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->to_sgsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT;
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub_bind_to_ggsns_short, cfg_gtphub_bind_to_ggsns_short_cmd,
"bind-to-ggsns ADDR",
"GTP Hub Parameters\n"
"Set the local bind address to listen for GGSNs, for both GTP-C and GTP-U\n"
"Local IP address (v4 or v6)\n"
)
{
int i;
for (i = 0; i < GTPH_PLANE_N; i++)
g_cfg->to_ggsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->to_ggsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->to_ggsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT;
return CMD_SUCCESS;
}
static int handle_binds(struct gtphub_cfg_bind *b, const char **argv)
{
b[GTPH_PLANE_CTRL].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
b[GTPH_PLANE_CTRL].bind.port = atoi(argv[1]);
b[GTPH_PLANE_USER].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[2]);
b[GTPH_PLANE_USER].bind.port = atoi(argv[3]);
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub_bind_to_sgsns, cfg_gtphub_bind_to_sgsns_cmd,
"bind-to-sgsns " BIND_ARGS,
"GTP Hub Parameters\n"
"Set the local bind addresses and ports to listen for SGSNs\n"
BIND_DOCS
)
{
return handle_binds(g_cfg->to_sgsns, argv);
}
DEFUN(cfg_gtphub_bind_to_ggsns, cfg_gtphub_bind_to_ggsns_cmd,
"bind-to-ggsns " BIND_ARGS,
"GTP Hub Parameters\n"
"Set the local bind addresses and ports to listen for GGSNs\n"
BIND_DOCS
)
{
return handle_binds(g_cfg->to_ggsns, argv);
}
DEFUN(cfg_gtphub_ggsn_proxy_short, cfg_gtphub_ggsn_proxy_short_cmd,
"ggsn-proxy ADDR",
"GTP Hub Parameters\n"
"Redirect all GGSN bound traffic to default ports on this address (another gtphub)\n"
"Remote IP address (v4 or v6)\n"
)
{
g_cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->ggsn_proxy[GTPH_PLANE_CTRL].port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->ggsn_proxy[GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->ggsn_proxy[GTPH_PLANE_USER].port = GTPH_DEFAULT_USER_PORT;
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub_ggsn_proxy, cfg_gtphub_ggsn_proxy_cmd,
"ggsn-proxy " BIND_ARGS,
"GTP Hub Parameters\n"
"Redirect all GGSN bound traffic to these addresses and ports (another gtphub)\n"
BIND_DOCS
)
{
g_cfg->ggsn_proxy[GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->ggsn_proxy[GTPH_PLANE_CTRL].port = atoi(argv[1]);
g_cfg->ggsn_proxy[GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[2]);
g_cfg->ggsn_proxy[GTPH_PLANE_USER].port = atoi(argv[3]);
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub_sgsn_proxy_short, cfg_gtphub_sgsn_proxy_short_cmd,
"sgsn-proxy ADDR",
"GTP Hub Parameters\n"
"Redirect all SGSN bound traffic to default ports on this address (another gtphub)\n"
"Remote IP address (v4 or v6)\n"
)
{
g_cfg->sgsn_proxy[GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->sgsn_proxy[GTPH_PLANE_CTRL].port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->sgsn_proxy[GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->sgsn_proxy[GTPH_PLANE_USER].port = GTPH_DEFAULT_USER_PORT;
return CMD_SUCCESS;
}
DEFUN(cfg_gtphub_sgsn_proxy, cfg_gtphub_sgsn_proxy_cmd,
"sgsn-proxy " BIND_ARGS,
"GTP Hub Parameters\n"
"Redirect all SGSN bound traffic to these addresses and ports (another gtphub)\n"
BIND_DOCS
)
{
g_cfg->sgsn_proxy[GTPH_PLANE_CTRL].addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->sgsn_proxy[GTPH_PLANE_CTRL].port = atoi(argv[1]);
g_cfg->sgsn_proxy[GTPH_PLANE_USER].addr_str = talloc_strdup(tall_vty_ctx, argv[2]);
g_cfg->sgsn_proxy[GTPH_PLANE_USER].port = atoi(argv[3]);
return CMD_SUCCESS;
}
DEFUN(show_gtphub, show_gtphub_cmd, "show gtphub",
SHOW_STR "Display information about the GTP hub")
{
vty_out(vty, "gtphub has nothing to say yet%s", VTY_NEWLINE);
return CMD_SUCCESS;
}
int gtphub_vty_init(void)
{
install_element_ve(&show_gtphub_cmd);
install_element(CONFIG_NODE, &cfg_gtphub_cmd);
install_node(&gtphub_node, config_write_gtphub);
vty_install_default(GTPHUB_NODE);
install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_sgsns_short_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_sgsns_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_ggsns_short_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_bind_to_ggsns_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_ggsn_proxy_short_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_ggsn_proxy_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_sgsn_proxy_short_cmd);
install_element(GTPHUB_NODE, &cfg_gtphub_sgsn_proxy_cmd);
return 0;
}
int gtphub_cfg_read(struct gtphub_cfg *cfg, const char *config_file)
{
int rc;
g_cfg = cfg;
rc = vty_read_config_file(config_file, NULL);
if (rc < 0) {
fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
return rc;
}
return 0;
}

View File

@ -1,4 +1,4 @@
SUBDIRS = gsm0408 db channel mgcp gprs abis gbproxy trau subscr oap
SUBDIRS = gsm0408 db channel mgcp gprs abis gbproxy trau subscr oap gtphub
if BUILD_NAT
SUBDIRS += bsc-nat bsc-nat-trie

View File

@ -0,0 +1,20 @@
AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
AM_CFLAGS=-Wall -ggdb3 $(LIBOSMOCORE_CFLAGS)
EXTRA_DIST = \
gtphub_test.ok \
gtphub_nc_test.sh \
gtphub_nc_test.ok \
hex2bin.py
noinst_PROGRAMS = gtphub_test
gtphub_test_SOURCES = gtphub_test.c
gtphub_test_LDFLAGS = \
-Wl,--wrap=gtphub_resolve_ggsn_addr
gtphub_test_LDADD = \
$(top_builddir)/src/gprs/gtphub.o \
$(LIBOSMOCORE_LIBS) \
-lgtp -lrt

View File

@ -0,0 +1,709 @@
/* Test the GTP hub */
/* (C) 2015 by sysmocom s.f.m.c. GmbH
* All Rights Reserved
*
* Author: Neels Hofmeyr <nhofmeyr@sysmcom.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/application.h>
#include <openbsc/debug.h>
#include <openbsc/gtphub.h>
#include <gtp.h>
#include <gtpie.h>
#define EXPIRE_ALL ((60 * GTPH_TEI_MAPPING_EXPIRY_MINUTES) + 1)
/* Make non-public API accessible */
void gtphub_init(struct gtphub *hub);
void *osmo_gtphub_ctx;
/* TODO copied from libosmo-abis/src/subchan_demux.c, remove dup */
static int llist_len(struct llist_head *head)
{
struct llist_head *entry;
int i = 0;
llist_for_each(entry, head)
i++;
return i;
}
static void nr_mapping_free(struct expiring_item *e)
{
struct nr_mapping *m = container_of(e, struct nr_mapping,
expiry_entry);
nr_mapping_del(m);
talloc_free(m);
}
static struct nr_mapping *nr_mapping_alloc(void)
{
struct nr_mapping *m;
m = talloc(osmo_gtphub_ctx, struct nr_mapping);
nr_mapping_init(m);
m->expiry_entry.del_cb = nr_mapping_free;
return m;
}
static struct nr_mapping *nr_map_have(struct nr_map *map, void *origin, nr_t orig, time_t now)
{
struct nr_mapping *mapping;
mapping = nr_map_get(map, origin, orig);
if (!mapping) {
mapping = nr_mapping_alloc();
mapping->origin = origin;
mapping->orig = orig;
nr_map_add(map, mapping, now);
}
return mapping;
}
static nr_t nr_map_verify(const struct nr_map *map, void *origin, nr_t orig, nr_t expect_repl)
{
struct nr_mapping *m;
m = nr_map_get(map, origin, orig);
if (!m) {
printf("mapping not found for %p %d\n", origin, orig);
return 0;
}
if (m->repl != expect_repl) {
printf("mapping found, but nr mismatches: expect %d, got %d\n",
(int)expect_repl, (int)m->repl);
return 0;
}
return 1;
}
static int nr_map_verify_inv(const struct nr_map *map, nr_t repl,
void *expect_origin, nr_t expect_orig)
{
struct nr_mapping *m;
m = nr_map_get_inv(map, repl);
if (!m) {
printf("mapping not found for %d\n", (int)repl);
return 0;
}
if (m->origin != expect_origin) {
printf("mapping found, but origin mismatches: expect %p, got %p\n",
expect_origin, m->origin);
return 0;
}
if (m->orig != expect_orig) {
printf("mapping found, but nr mismatches: expect %d, got %d\n",
(int)expect_orig, (int)m->orig);
return 0;
}
return 1;
}
static void test_nr_map_basic(void)
{
struct nr_pool _pool;
struct nr_pool *pool = &_pool;
struct nr_map _map;
struct nr_map *map = &_map;
nr_pool_init(pool);
nr_map_init(map, pool, NULL);
OSMO_ASSERT(llist_empty(&map->mappings));
#define TEST_N_HALF 100
#define TEST_N (2*TEST_N_HALF)
#define TEST_I 123
uint32_t i, check_i;
uint32_t m[TEST_N];
struct nr_mapping *mapping;
/* create half of TEST_N mappings from one origin */
void *origin1 = (void*)0x1234;
for (i = 0; i < TEST_N_HALF; i++) {
nr_t orig = TEST_I + i;
mapping = nr_map_have(map, origin1, orig, 0);
m[i] = mapping->repl;
OSMO_ASSERT(m[i] != 0);
OSMO_ASSERT(llist_len(&map->mappings) == (i+1));
for (check_i = 0; check_i < i; check_i++)
OSMO_ASSERT(m[check_i] != m[i]);
}
OSMO_ASSERT(llist_len(&map->mappings) == TEST_N_HALF);
/* create another TEST_N mappings with the same original numbers, but
* from a different origin */
void *origin2 = (void*)0x5678;
for (i = 0; i < TEST_N_HALF; i++) {
int i2 = TEST_N_HALF + i;
nr_t orig = TEST_I + i;
mapping = nr_map_have(map, origin2, orig, 0);
m[i2] = mapping->repl;
OSMO_ASSERT(m[i2] != 0);
OSMO_ASSERT(llist_len(&map->mappings) == (i2+1));
for (check_i = 0; check_i < i2; check_i++)
OSMO_ASSERT(m[check_i] != m[i2]);
}
OSMO_ASSERT(llist_len(&map->mappings) == TEST_N);
/* verify mappings */
for (i = 0; i < TEST_N_HALF; i++) {
nr_t orig = TEST_I + i;
{
OSMO_ASSERT(nr_map_verify(map, origin1, orig, m[i]));
OSMO_ASSERT(nr_map_verify_inv(map, m[i], origin1, orig));
}
{
int i2 = TEST_N_HALF + i;
OSMO_ASSERT(nr_map_verify(map, origin2, orig, m[i2]));
OSMO_ASSERT(nr_map_verify_inv(map, m[i2], origin2, orig));
}
}
/* remove all mappings */
for (i = 0; i < TEST_N_HALF; i++) {
OSMO_ASSERT(llist_len(&map->mappings) == (TEST_N - 2*i));
nr_t orig = TEST_I + i;
nr_mapping_del(nr_map_get(map, origin1, orig));
nr_mapping_del(nr_map_get(map, origin2, orig));
}
OSMO_ASSERT(llist_empty(&map->mappings));
#undef TEST_N
#undef TEST_I
}
static int nr_map_is(struct nr_map *map, const char *str)
{
static char buf[4096];
char *pos = buf;
size_t len = sizeof(buf);
struct nr_mapping *m;
llist_for_each_entry(m, &map->mappings, entry) {
size_t wrote = snprintf(pos, len, "(%d->%d@%d), ",
(int)m->orig,
(int)m->repl,
(int)m->expiry_entry.expiry);
OSMO_ASSERT(wrote < len);
pos += wrote;
len -= wrote;
}
*pos = '\0';
if (strncmp(buf, str, sizeof(buf)) != 0) {
printf("FAILURE: nr_map_is() mismatches expected value:\n"
"expected: \"%s\"\n"
"is: \"%s\"\n",
str, buf);
return 0;
}
return 1;
}
static void test_expiry(void)
{
struct expiry expiry;
struct nr_pool pool;
struct nr_map map;
int i;
expiry_init(&expiry, 30);
nr_pool_init(&pool);
nr_map_init(&map, &pool, &expiry);
OSMO_ASSERT(nr_map_is(&map, ""));
/* tick on empty map */
OSMO_ASSERT(expiry_tick(&expiry, 10000) == 0);
OSMO_ASSERT(nr_map_is(&map, ""));
#define MAP1 \
"(10->1@10040), " \
""
#define MAP2 \
"(20->2@10050), " \
"(21->3@10051), " \
"(22->4@10052), " \
"(23->5@10053), " \
"(24->6@10054), " \
"(25->7@10055), " \
"(26->8@10056), " \
"(27->9@10057), " \
""
#define MAP3 \
"(420->10@10072), " \
"(421->11@10072), " \
"(422->12@10072), " \
"(423->13@10072), " \
"(424->14@10072), " \
"(425->15@10072), " \
"(426->16@10072), " \
"(427->17@10072), " \
""
/* add mapping at time 10010. */
nr_map_have(&map, 0, 10, 10010);
OSMO_ASSERT(nr_map_is(&map, MAP1));
/* tick on unexpired item. */
OSMO_ASSERT(expiry_tick(&expiry, 10010) == 0);
OSMO_ASSERT(expiry_tick(&expiry, 10011) == 0);
OSMO_ASSERT(nr_map_is(&map, MAP1));
/* Spread mappings at 10020, 10021, ... 10027. */
for (i = 0; i < 8; i++)
nr_map_have(&map, 0, 20 + i, 10020 + i);
OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2));
/* tick on unexpired items. */
OSMO_ASSERT(expiry_tick(&expiry, 10030) == 0);
OSMO_ASSERT(expiry_tick(&expiry, 10039) == 0);
OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2));
/* expire the first item (from 10010). */
OSMO_ASSERT(expiry_tick(&expiry, 10010 + 30) == 1);
OSMO_ASSERT(nr_map_is(&map, MAP2));
/* again nothing to expire */
OSMO_ASSERT(expiry_tick(&expiry, 10041) == 0);
OSMO_ASSERT(nr_map_is(&map, MAP2));
/* Mappings all at the same time. */
for (i = 0; i < 8; i++)
nr_map_have(&map, 0, 420 + i, 10042);
OSMO_ASSERT(nr_map_is(&map, MAP2 MAP3));
/* Eight to expire, were added further above to be chronologically
* correct, at 10020..10027. */
OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 8);
OSMO_ASSERT(nr_map_is(&map, MAP3));
/* again nothing to expire */
OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 0);
OSMO_ASSERT(nr_map_is(&map, MAP3));
/* Eight to expire, from 10042. Now at 10042 + 30: */
OSMO_ASSERT(expiry_tick(&expiry, 10042 + 30) == 8);
OSMO_ASSERT(nr_map_is(&map, ""));
#undef MAP1
#undef MAP2
#undef MAP3
}
/* override, requires '-Wl,--wrap=gtphub_resolve_ggsn_addr' */
int __real_gtphub_resolve_ggsn_addr(struct gtphub *hub,
struct osmo_sockaddr *result,
struct gtp_packet_desc *p);
struct osmo_sockaddr resolved_ggsn_addr = {.l = 0};
int __wrap_gtphub_resolve_ggsn_addr(struct gtphub *hub,
struct osmo_sockaddr *result,
struct gtp_packet_desc *p)
{
osmo_sockaddr_copy(result, &resolved_ggsn_addr);
printf("Wrap: returning GGSN addr: %s\n",
osmo_sockaddr_to_str(result));
return (resolved_ggsn_addr.l != 0)? 0 : -1;
}
#define buf_len 1024
static uint8_t buf[buf_len];
static unsigned int msg(const char *hex)
{
unsigned int l = osmo_hexparse(hex, buf, buf_len);
OSMO_ASSERT(l > 0);
return l;
}
/* Compare static buf to given string constant. The amount of bytes is obtained
* from parsing the GTP header in buf. hex must match an osmo_hexdump() of the
* desired message. Return 1 if size and content match. */
#define msg_is(MSG) _msg_is(MSG, __FILE__, __LINE__)
static int _msg_is(const char *hex, const char *file, int line)
{
struct gtp1_header_long *h = (void*)buf;
int len = ntoh16(h->length) + 8;
const char *dump = osmo_hexdump_nospc(buf, len);
int cmp = strcmp(dump, hex);
if (cmp != 0) {
printf("\n%s:%d: msg_is(): MISMATCH\n"
" expecting:\n'%s'\n"
" got:\n'%s'\n\n",
file,
line,
hex, dump);
int i;
int l = strlen(hex);
int m = strlen(dump);
if (m < l)
l = m;
for (i = 0; i < l; i++) {
if (hex[i] != dump[i]) {
printf("First mismatch at position %d:\n"
" %s\n %s\n", i, hex + i, dump + i);
break;
}
}
}
return cmp == 0;
}
#define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__)
static int _same_addr(const struct osmo_sockaddr *got,
const struct osmo_sockaddr *expected,
const char *file, int line)
{
int cmp = osmo_sockaddr_cmp(got, expected);
if (!cmp)
return 1;
char buf[256];
printf("\n%s:%d: addr_is(): MISMATCH\n"
" expecting: '%s'\n"
" got: '%s'\n\n",
file, line,
osmo_sockaddr_to_str(expected),
osmo_sockaddr_to_strb(got, buf, sizeof(buf)));
return 0;
}
static void test_echo(void)
{
struct gtphub _hub;
struct gtphub *hub = &_hub;
time_t now = 123;
gtphub_init(hub);
const char *gtp_ping_from_sgsn =
"32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */
"01" /* type 01: Echo request */
"0004" /* length of 4 after header TEI */
"00000000" /* header TEI == 0 in Echo */
"abcd" /* some 16 octet sequence nr */
"0000" /* N-PDU 0, no extension header (why is this here?) */
;
/* Same with mapped sequence number */
const char *gtp_ping_to_ggsn =
"32" "01" "0004" "00000000"
"6d31" /* mapped seq */
"00" "00";
const char *gtp_pong_from_ggsn =
"32"
"02" /* type 02: Echo response */
"0006" /* len */
"00000000" /* tei */
"6d31" /* mapped seq */
"0000" /* ext */
"0e01" /* 0e: Recovery, val == 1 */
;
/* Same with unmapped sequence number */
const char *gtp_pong_to_sgsn =
"32" "02" "0006" "00000000"
"abcd" /* unmapped seq */
"00" "00" "0e01";
/* Set the GGSN address that gtphub is forced to resolve to. */
OSMO_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_addr,
"192.168.43.34", 434)
== 0);
/* according to spec, we'd always send to port 2123 instead...
struct osmo_sockaddr ggsn_standard_port;
OSMO_ASSERT(osmo_sockaddr_init_udp(&ggsn_standard_port,
"192.168.43.34", 2123)
== 0);
*/
struct osmo_sockaddr orig_sgsn_addr;
OSMO_ASSERT(osmo_sockaddr_init(&orig_sgsn_addr,
AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
"192.168.42.23", 423) == 0);
struct osmo_fd *ggsn_ofd = NULL;
struct osmo_sockaddr ggsn_addr;
int send;
send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr,
buf, msg(gtp_ping_from_sgsn), now,
&ggsn_ofd, &ggsn_addr);
OSMO_ASSERT(send > 0);
OSMO_ASSERT(ggsn_addr.l);
OSMO_ASSERT(same_addr(&ggsn_addr, &resolved_ggsn_addr));
OSMO_ASSERT(msg_is(gtp_ping_to_ggsn));
struct osmo_fd *sgsn_ofd;
struct osmo_sockaddr sgsn_addr;
send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_addr,
buf, msg(gtp_pong_from_ggsn), now,
&sgsn_ofd, &sgsn_addr);
OSMO_ASSERT(send > 0);
OSMO_ASSERT(same_addr(&sgsn_addr, &orig_sgsn_addr));
OSMO_ASSERT(msg_is(gtp_pong_to_sgsn));
struct gtphub_peer_port *ggsn_port =
gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL],
&resolved_ggsn_addr);
OSMO_ASSERT(ggsn_port);
struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer;
/* now == 123; now + 30 == 153. */
OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@153), "));
OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], ""));
OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], ""));
gtphub_gc(hub, now + EXPIRE_ALL);
}
static void test_create_pdp_ctx(void)
{
struct gtphub _hub;
struct gtphub *hub = &_hub;
time_t now = 345;
gtphub_init(hub);
/* Tell this mock gtphub its local address for this test. */
OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_CTRL].local_addr,
"127.0.1.1") == 0);
OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_USER].local_addr,
"127.0.1.2") == 0);
OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_CTRL].local_addr,
"127.0.2.1") == 0);
OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_USER].local_addr,
"127.0.2.2") == 0);
/* This is copied from a packet that sgsnemu sends. */
const char *gtp_req_from_sgsn =
"32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */
"10" /* type 16: Create PDP Context Request */
"0067" /* length = 8 + 103 */
"00000000" /* No TEI yet */
"abcd" /* Sequence nr */
"00" /* N-PDU 0 */
"00" /* No extensions */
/* IEs */
"02" /* 2 = IMSI */
"42000121436587f9"
"0e" "60" /* 14: Recovery = 96 */
"0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/
"10" /* 16: TEI Data I */
"00000123"
"11" /* 17: TEI Control Plane */
"00000321"
"1400" /* 20: NSAPI = 0*/
"1a" /* 26: Charging Characteristics */
"0800"
"80" /* 128: End User Address */
"0002" /* length = 2: empty PDP Address */
"f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */
"83" /* 131: Access Point Name */
"0008" /* length = 8 */
"696e7465726e6574" /* "internet" */
"84" /* 132: Protocol Configuration Options */
"0015" /* length = 21 */
"80c0231101010011036d69670868656d6d656c6967"
"85" /* 133: GSN Address */
"0004" /* length */
"abcdef00"
"85" /* 133: GSN Address (second entry) */
"0004" /* length */
"fedcba00"
"86" /* 134: MS International PSTN/ISDN Number (MSISDN) */
"0007" /* length */
"916407123254f6" /* 1946702123456(f) */
"87" /* 135: Quality of Service (QoS) Profile */
"0004" /* length */
"00" /* priority */
"0b921f" /* QoS profile data */
;
const char *gtp_req_to_ggsn =
"32" "10" "0067" "00000000"
"6d31" /* mapped seq ("abcd") */
"00" "00" "02" "42000121436587f9" "0e60" "0f01"
"10" "00000001" /* mapped TEI Data I ("123") */
"11" "00000001" /* mapped TEI Control ("321") */
"1400" "1a" "0800" "80" "0002" "f121" "83"
"0008" "696e7465726e6574" "84" "0015"
"80c0231101010011036d69670868656d6d656c6967" "85" "0004"
"7f000201" /* replaced with gtphub's address ggsn ctrl */
"85" "0004"
"7f000202" /* replaced with gtphub's address ggsn user */
"86" "0007" "916407123254f6"
"87" "0004" "00" "0b921f"
;
const char *gtp_resp_from_ggsn =
"32"
"11" /* Create PDP Context Response */
"004e" /* length = 78 + 8 */
"00000001" /* destination TEI (sent in req above) */
"6d31" /* mapped seq */
"00" "00"
/* IEs */
"01" /* 1: Cause */
"80" /* value = 0b10000000 = response, no rejection. */
"08" /* 8: Reordering Required */
"00" /* not required. */
"0e" "01" /* 14: Recovery = 1 */
"10" /* 16: TEI Data I */
"00000567"
"11" /* 17: TEI Control */
"00000765"
"7f" /* 127: Charging ID */
"00000001"
"80" /* 128: End User Address */
"0006" /* length = 6 */
"f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */
"7f000002"
"84" /* 132: Protocol Configuration Options */
"0014" /* len = 20 */
"8080211002000010810608080808830600000000"
"85" /* 133: GSN Address (Ctrl) */
"0004" /* length */
"7f000002"
"85" /* 133: GSN Address (User) */
"0004" /* length */
"7f000002"
"87" /* 135: Quality of Service (QoS) Profile */
"0004" /* length */
"00" /* priority */
"0b921f" /* QoS profile data */
;
const char *gtp_resp_to_sgsn =
"32" "11" "004e"
"00000321" /* unmapped TEI ("001") */
"abcd" /* unmapped seq ("6d31") */
"00" "00" "01" "80" "08" "00" "0e" "01"
"10" "00000002" /* mapped TEI from GGSN ("567") */
"11" "00000002" /* mapped TEI from GGSN ("765") */
"7f" "00000001" "80" "0006" "f121" "7f000002" "84" "0014"
"8080211002000010810608080808830600000000"
"85" "0004"
"7f000101" /* gtphub's address towards SGSNs (Ctrl) */
"85" "0004"
"7f000102" /* gtphub's address towards SGSNs (User) */
"87" "0004" "00" "0b921f"
;
/* Set the GGSN address that gtphub is forced to resolve to. */
OSMO_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_addr,
"192.168.43.34", 434)
== 0);
struct osmo_sockaddr orig_sgsn_addr;
OSMO_ASSERT(osmo_sockaddr_init(&orig_sgsn_addr,
AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
"192.168.42.23", 423) == 0);
struct osmo_fd *ggsn_ofd = NULL;
struct osmo_sockaddr ggsn_addr;
int send;
send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr,
buf, msg(gtp_req_from_sgsn), now,
&ggsn_ofd, &ggsn_addr);
OSMO_ASSERT(send > 0);
OSMO_ASSERT(same_addr(&ggsn_addr, &resolved_ggsn_addr));
OSMO_ASSERT(msg_is(gtp_req_to_ggsn));
struct osmo_fd *sgsn_ofd;
struct osmo_sockaddr sgsn_addr;
send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_addr,
buf, msg(gtp_resp_from_ggsn), now,
&sgsn_ofd, &sgsn_addr);
OSMO_ASSERT(send > 0);
OSMO_ASSERT(same_addr(&sgsn_addr, &orig_sgsn_addr));
OSMO_ASSERT(msg_is(gtp_resp_to_sgsn));
struct gtphub_peer_port *ggsn_port =
gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL],
&resolved_ggsn_addr);
OSMO_ASSERT(ggsn_port);
struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer;
/* now == 345; now + 30 == 375.
* seq mapping from above:
* 0xabcd == 43981 (sent in the packet)
* 0x6d31 == 27953 (harcoded seq mapping start val) */
OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), "));
/* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945.
* 0x00000321 == 801 (TEI from SGSN Ctrl)
* 0x00000123 == 291 (TEI from SGSN User)
* 0x00000765 == 1893 (TEI from GGSN Ctrl)
* 0x00000567 == 1383 (TEI from GGSN User)
* Mapped TEIs should be 1 and 2. */
OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], "(801->1@21945), (1893->2@21945), "));
OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], "(291->1@21945), (1383->2@21945), "));
gtphub_gc(hub, now + EXPIRE_ALL);
}
static struct log_info_cat gtphub_categories[] = {
[DGTPHUB] = {
.name = "DGTPHUB",
.description = "GTP Hub",
.color = "\033[1;33m",
.enabled = 1, .loglevel = LOGL_NOTICE,
},
};
static struct log_info info = {
.cat = gtphub_categories,
.num_cat = ARRAY_SIZE(gtphub_categories),
};
int main(int argc, char **argv)
{
osmo_init_logging(&info);
osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
test_nr_map_basic();
test_expiry();
test_echo();
test_create_pdp_ctx();
printf("Done\n");
talloc_report_full(osmo_gtphub_ctx, stderr);
OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1);
return 0;
}

View File

@ -0,0 +1,3 @@
Wrap: returning GGSN addr: 192.168.43.34 port 434
Wrap: returning GGSN addr: 192.168.43.34 port 434
Done

View File

@ -110,3 +110,10 @@ AT_CHECK([test "$enable_oap_test" != no || exit 77])
cat $abs_srcdir/oap/oap_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/oap/oap_test], [], [expout], [ignore])
AT_CLEANUP
AT_SETUP([gtphub])
AT_KEYWORDS([gtphub])
AT_CHECK([test "$enable_gtphub_test" != no || exit 77])
cat $abs_srcdir/gtphub/gtphub_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/gtphub/gtphub_test], [], [expout], [ignore])
AT_CLEANUP