2010-03-03 13:22:04 +00:00
|
|
|
/* Main method of the layer2/3 stack */
|
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
/* (C) 2010 by Holger Hans Peter Freyther
|
2010-03-02 17:47:01 +00:00
|
|
|
* (C) 2010 by Harald Welte <laforge@gnumonks.org>
|
2010-02-18 15:46:36 +00:00
|
|
|
*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-07-27 18:44:46 +00:00
|
|
|
#include <osmocom/bb/common/osmocom_data.h>
|
|
|
|
#include <osmocom/bb/common/l1ctl.h>
|
|
|
|
#include <osmocom/bb/common/l1l2_interface.h>
|
2010-09-11 10:42:29 +00:00
|
|
|
#include <osmocom/bb/common/sap_interface.h>
|
2010-07-27 18:44:46 +00:00
|
|
|
#include <osmocom/bb/misc/layer3.h>
|
|
|
|
#include <osmocom/bb/common/lapdm.h>
|
|
|
|
#include <osmocom/bb/common/logging.h>
|
|
|
|
#include <osmocom/bb/common/l23_app.h>
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2011-04-26 00:55:30 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
#include <osmocom/core/gsmtap_util.h>
|
2011-05-22 10:36:55 +00:00
|
|
|
#include <osmocom/core/gsmtap.h>
|
2011-04-26 00:55:30 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2010-04-29 18:46:11 +00:00
|
|
|
#include <signal.h>
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-05-02 17:13:47 +00:00
|
|
|
struct log_target *stderr_target;
|
2010-02-19 21:43:51 +00:00
|
|
|
|
2010-04-25 16:10:59 +00:00
|
|
|
void *l23_ctx = NULL;
|
2011-05-22 10:36:55 +00:00
|
|
|
|
2010-09-11 10:42:29 +00:00
|
|
|
static char *layer2_socket_path = "/tmp/osmocom_l2";
|
|
|
|
static char *sap_socket_path = "/tmp/osmocom_sap";
|
2010-05-09 16:01:49 +00:00
|
|
|
struct llist_head ms_list;
|
2010-02-18 15:46:36 +00:00
|
|
|
static struct osmocom_ms *ms = NULL;
|
2011-05-22 10:36:55 +00:00
|
|
|
static char *gsmtap_ip = NULL;
|
|
|
|
|
2010-06-26 22:27:38 +00:00
|
|
|
unsigned short vty_port = 4247;
|
2010-04-25 16:10:59 +00:00
|
|
|
int (*l23_app_work) (struct osmocom_ms *ms) = NULL;
|
2010-04-29 18:46:11 +00:00
|
|
|
int (*l23_app_exit) (struct osmocom_ms *ms) = NULL;
|
2010-05-24 00:21:24 +00:00
|
|
|
int quit = 0;
|
2011-05-22 10:36:55 +00:00
|
|
|
struct gsmtap_inst *gsmtap_inst;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-06-26 14:00:29 +00:00
|
|
|
const char *openbsc_copyright =
|
2010-12-26 18:15:48 +00:00
|
|
|
"%s"
|
|
|
|
"%s\n"
|
2010-06-26 22:27:38 +00:00
|
|
|
"License GPLv2+: GNU GPL version 2 or later "
|
|
|
|
"<http://gnu.org/licenses/gpl.html>\n"
|
2010-06-26 14:00:29 +00:00
|
|
|
"This is free software: you are free to change and redistribute it.\n"
|
2010-12-26 18:15:48 +00:00
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\n\n";
|
2010-06-26 14:00:29 +00:00
|
|
|
|
2010-04-25 16:28:24 +00:00
|
|
|
static void print_usage(const char *app)
|
2010-02-18 15:46:36 +00:00
|
|
|
{
|
2010-04-25 16:28:24 +00:00
|
|
|
printf("Usage: %s\n", app);
|
2010-02-18 15:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
2010-12-26 20:29:51 +00:00
|
|
|
int options = 0xff;
|
|
|
|
struct l23_app_info *app = l23_app_info();
|
|
|
|
|
|
|
|
if (app && app->cfg_supported != 0)
|
|
|
|
options = app->cfg_supported();
|
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
printf(" Some help...\n");
|
2010-03-04 14:41:11 +00:00
|
|
|
printf(" -h --help this text\n");
|
2010-06-26 22:27:38 +00:00
|
|
|
printf(" -s --socket /tmp/osmocom_l2. Path to the unix "
|
2010-09-11 10:42:29 +00:00
|
|
|
"domain socket (l2)\n");
|
2010-12-26 20:29:51 +00:00
|
|
|
|
|
|
|
if (options & L23_OPT_SAP)
|
|
|
|
printf(" -S --sap /tmp/osmocom_sap. Path to the "
|
|
|
|
"unix domain socket (BTSAP)\n");
|
|
|
|
|
|
|
|
if (options & L23_OPT_ARFCN)
|
|
|
|
printf(" -a --arfcn NR The ARFCN to be used for layer2.\n");
|
|
|
|
|
|
|
|
if (options & L23_OPT_TAP)
|
|
|
|
printf(" -i --gsmtap-ip The destination IP used for GSMTAP.\n");
|
|
|
|
|
|
|
|
if (options & L23_OPT_VTY)
|
|
|
|
printf(" -v --vty-port The VTY port number to telnet "
|
|
|
|
"to. (default %u)\n", vty_port);
|
|
|
|
|
|
|
|
if (options & L23_OPT_DBG)
|
|
|
|
printf(" -d --debug Change debug flags.\n");
|
2010-12-27 07:58:57 +00:00
|
|
|
|
|
|
|
if (app && app->cfg_print_help)
|
|
|
|
app->cfg_print_help();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void build_config(char **opt, struct option **option)
|
|
|
|
{
|
|
|
|
struct l23_app_info *app;
|
2010-12-27 13:51:13 +00:00
|
|
|
struct option *app_opp = NULL;
|
|
|
|
int app_len = 0, len;
|
|
|
|
|
2010-12-27 07:58:57 +00:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"socket", 1, 0, 's'},
|
|
|
|
{"sap", 1, 0, 'S'},
|
|
|
|
{"arfcn", 1, 0, 'a'},
|
|
|
|
{"gsmtap-ip", 1, 0, 'i'},
|
|
|
|
{"vty-port", 1, 0, 'v'},
|
|
|
|
{"debug", 1, 0, 'd'},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
app = l23_app_info();
|
|
|
|
*opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:%s",
|
|
|
|
app && app->getopt_string ? app->getopt_string : "");
|
2010-12-27 13:51:13 +00:00
|
|
|
|
|
|
|
len = ARRAY_SIZE(long_options);
|
|
|
|
if (app && app->cfg_getopt_opt)
|
|
|
|
app_len = app->cfg_getopt_opt(&app_opp);
|
|
|
|
|
|
|
|
*option = talloc_zero_array(l23_ctx, struct option, len + app_len + 1);
|
2010-12-27 07:58:57 +00:00
|
|
|
memcpy(*option, long_options, sizeof(long_options));
|
2010-12-27 13:51:13 +00:00
|
|
|
memcpy(*option + len, app_opp, app_len * sizeof(struct option));
|
2010-02-18 15:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_options(int argc, char **argv)
|
|
|
|
{
|
2010-12-27 07:58:57 +00:00
|
|
|
struct l23_app_info *app = l23_app_info();
|
|
|
|
struct option *long_options;
|
|
|
|
char *opt;
|
|
|
|
|
|
|
|
build_config(&opt, &long_options);
|
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
2010-12-27 07:58:57 +00:00
|
|
|
|
|
|
|
c = getopt_long(argc, argv, opt,
|
2010-02-18 15:46:36 +00:00
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
2010-04-25 16:28:24 +00:00
|
|
|
print_usage(argv[0]);
|
2010-02-18 15:46:36 +00:00
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 's':
|
2010-09-11 10:42:29 +00:00
|
|
|
layer2_socket_path = talloc_strdup(l23_ctx, optarg);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
sap_socket_path = talloc_strdup(l23_ctx, optarg);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2010-04-25 15:31:35 +00:00
|
|
|
ms->test_arfcn = atoi(optarg);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
2010-03-04 14:41:11 +00:00
|
|
|
case 'i':
|
2011-05-22 10:36:55 +00:00
|
|
|
gsmtap_ip = optarg;
|
2010-03-04 14:41:11 +00:00
|
|
|
break;
|
2010-06-26 22:27:38 +00:00
|
|
|
case 'v':
|
|
|
|
vty_port = atoi(optarg);
|
|
|
|
break;
|
2010-03-27 00:56:05 +00:00
|
|
|
case 'd':
|
|
|
|
log_parse_category_mask(stderr_target, optarg);
|
|
|
|
break;
|
2010-02-18 15:46:36 +00:00
|
|
|
default:
|
2010-12-27 07:58:57 +00:00
|
|
|
if (app && app->cfg_handle_opt)
|
|
|
|
app->cfg_handle_opt(c, optarg);
|
2010-02-18 15:46:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-27 07:58:57 +00:00
|
|
|
|
|
|
|
talloc_free(opt);
|
|
|
|
talloc_free(long_options);
|
2010-02-18 15:46:36 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:46:11 +00:00
|
|
|
void sighandler(int sigset)
|
|
|
|
{
|
2010-05-05 08:40:10 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2010-04-29 18:46:11 +00:00
|
|
|
if (sigset == SIGHUP || sigset == SIGPIPE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "Signal %d recevied.\n", sigset);
|
|
|
|
if (l23_app_exit)
|
2010-05-05 08:40:10 +00:00
|
|
|
rc = l23_app_exit(ms);
|
2010-04-29 18:46:11 +00:00
|
|
|
|
2010-05-05 08:40:10 +00:00
|
|
|
if (rc != -EBUSY)
|
|
|
|
exit (0);
|
2010-04-29 18:46:11 +00:00
|
|
|
}
|
|
|
|
|
2010-12-26 18:15:48 +00:00
|
|
|
static void print_copyright()
|
|
|
|
{
|
|
|
|
struct l23_app_info *app;
|
|
|
|
app = l23_app_info();
|
|
|
|
printf(openbsc_copyright,
|
|
|
|
app && app->copyright ? app->copyright : "",
|
|
|
|
app && app->contribution ? app->contribution : "");
|
|
|
|
}
|
|
|
|
|
2010-02-18 15:46:36 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2010-05-09 16:01:49 +00:00
|
|
|
INIT_LLIST_HEAD(&ms_list);
|
2010-03-27 00:56:05 +00:00
|
|
|
log_init(&log_info);
|
|
|
|
stderr_target = log_target_create_stderr();
|
|
|
|
log_add_target(stderr_target);
|
|
|
|
log_set_all_filter(stderr_target, 1);
|
|
|
|
|
2010-04-25 16:10:59 +00:00
|
|
|
l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-04-25 16:10:59 +00:00
|
|
|
ms = talloc_zero(l23_ctx, struct osmocom_ms);
|
2010-02-18 15:46:36 +00:00
|
|
|
if (!ms) {
|
|
|
|
fprintf(stderr, "Failed to allocate MS\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-12-26 18:15:48 +00:00
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
|
2010-05-09 16:01:49 +00:00
|
|
|
llist_add_tail(&ms->entity, &ms_list);
|
2010-02-18 15:46:36 +00:00
|
|
|
|
2010-04-25 16:10:59 +00:00
|
|
|
sprintf(ms->name, "1");
|
|
|
|
|
2010-04-25 15:31:35 +00:00
|
|
|
ms->test_arfcn = 871;
|
2010-02-18 15:46:36 +00:00
|
|
|
|
|
|
|
handle_options(argc, argv);
|
|
|
|
|
2010-09-11 10:42:29 +00:00
|
|
|
rc = layer2_open(ms, layer2_socket_path);
|
2010-02-18 15:46:36 +00:00
|
|
|
if (rc < 0) {
|
2010-05-22 10:46:20 +00:00
|
|
|
fprintf(stderr, "Failed during layer2_open()\n");
|
2010-02-18 15:46:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-09-11 10:42:29 +00:00
|
|
|
rc = sap_open(ms, sap_socket_path);
|
|
|
|
if (rc < 0)
|
|
|
|
fprintf(stderr, "Failed during sap_open(), no SIM reader\n");
|
|
|
|
|
2011-05-20 16:16:09 +00:00
|
|
|
ms->l2_entity.lapdm_dcch.l1_ctx = ms;
|
|
|
|
ms->l2_entity.lapdm_dcch.l3_ctx = ms;
|
|
|
|
lapdm_init(&ms->l2_entity.lapdm_dcch);
|
|
|
|
|
|
|
|
ms->l2_entity.lapdm_acch.l1_ctx = ms;
|
|
|
|
ms->l2_entity.lapdm_acch.l3_ctx = ms;
|
|
|
|
lapdm_init(&ms->l2_entity.lapdm_acch);
|
2010-04-05 11:26:09 +00:00
|
|
|
|
2010-05-13 12:51:22 +00:00
|
|
|
rc = l23_app_init(ms);
|
|
|
|
if (rc < 0)
|
|
|
|
exit(1);
|
2010-03-02 17:47:01 +00:00
|
|
|
|
2010-03-04 14:41:11 +00:00
|
|
|
if (gsmtap_ip) {
|
2011-05-22 10:36:55 +00:00
|
|
|
gsmtap_inst = gsmtap_source_init(gsmtap_ip, GSMTAP_UDP_PORT, 1);
|
|
|
|
if (!gsmtap_inst) {
|
2010-03-04 14:41:11 +00:00
|
|
|
fprintf(stderr, "Failed during gsmtap_init()\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-05-22 10:36:55 +00:00
|
|
|
gsmtap_source_add_sink(gsmtap_inst);
|
2010-02-19 21:43:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 18:46:11 +00:00
|
|
|
signal(SIGINT, sighandler);
|
|
|
|
signal(SIGHUP, sighandler);
|
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
signal(SIGPIPE, sighandler);
|
|
|
|
|
2010-05-24 00:21:24 +00:00
|
|
|
while (!quit) {
|
2010-04-25 16:10:59 +00:00
|
|
|
if (l23_app_work)
|
|
|
|
l23_app_work(ms);
|
2011-05-15 12:23:05 +00:00
|
|
|
osmo_select_main(0);
|
2010-02-18 15:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|