2010-11-13 18:45:09 +00:00
|
|
|
/* Main method of the layer2/3 stack */
|
|
|
|
|
|
|
|
/* (C) 2010 by Holger Hans Peter Freyther
|
|
|
|
* (C) 2010 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osmocom/bb/common/osmocom_data.h>
|
|
|
|
#include <osmocom/bb/common/logging.h>
|
2010-11-14 11:52:57 +00:00
|
|
|
#include <osmocom/bb/mobile/app_mobile.h>
|
2010-11-13 18:45:09 +00:00
|
|
|
|
2011-04-26 00:55:30 +00:00
|
|
|
#include <osmocom/core/talloc.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/signal.h>
|
2011-09-04 17:36:19 +00:00
|
|
|
#include <osmocom/core/application.h>
|
2010-11-13 18:45:09 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
2011-03-02 09:35:04 +00:00
|
|
|
#include <stdio.h>
|
2010-11-13 18:45:09 +00:00
|
|
|
#include <unistd.h>
|
2011-03-02 09:35:04 +00:00
|
|
|
#include <string.h>
|
2010-11-13 18:45:09 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2011-03-02 10:00:59 +00:00
|
|
|
#include <libgen.h>
|
2010-11-13 18:45:09 +00:00
|
|
|
|
|
|
|
struct log_target *stderr_target;
|
|
|
|
|
|
|
|
void *l23_ctx = NULL;
|
|
|
|
struct llist_head ms_list;
|
2011-05-22 10:36:55 +00:00
|
|
|
static char *gsmtap_ip = 0;
|
|
|
|
struct gsmtap_inst *gsmtap_inst = NULL;
|
2012-03-01 21:45:51 +00:00
|
|
|
static char *vty_ip = "127.0.0.1";
|
2010-11-13 18:45:09 +00:00
|
|
|
unsigned short vty_port = 4247;
|
2010-11-14 11:52:57 +00:00
|
|
|
int debug_set = 0;
|
2011-03-02 10:00:59 +00:00
|
|
|
char *config_dir = NULL;
|
2011-09-02 09:16:10 +00:00
|
|
|
int use_mncc_sock = 0;
|
2011-09-04 17:36:19 +00:00
|
|
|
int daemonize = 0;
|
2011-09-02 09:16:10 +00:00
|
|
|
|
|
|
|
int mncc_recv_socket(struct osmocom_ms *ms, int msg_type, void *arg);
|
2010-11-13 18:45:09 +00:00
|
|
|
|
|
|
|
int mobile_delete(struct osmocom_ms *ms, int force);
|
|
|
|
int mobile_signal_cb(unsigned int subsys, unsigned int signal,
|
|
|
|
void *handler_data, void *signal_data);
|
|
|
|
int mobile_work(struct osmocom_ms *ms);
|
|
|
|
int mobile_exit(struct osmocom_ms *ms, int force);
|
|
|
|
|
|
|
|
|
2011-10-31 17:14:03 +00:00
|
|
|
const char *debug_default =
|
2015-04-28 13:57:54 +00:00
|
|
|
"DCS:DNB:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DSS:DLSMS:DPAG:DSUM:DSAP";
|
2011-10-31 17:14:03 +00:00
|
|
|
|
2010-11-13 18:45:09 +00:00
|
|
|
const char *openbsc_copyright =
|
|
|
|
"Copyright (C) 2008-2010 ...\n"
|
|
|
|
"Contributions by ...\n\n"
|
|
|
|
"License GPLv2+: GNU GPL version 2 or later "
|
|
|
|
"<http://gnu.org/licenses/gpl.html>\n"
|
|
|
|
"This is free software: you are free to change and redistribute it.\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\n";
|
|
|
|
|
|
|
|
static void print_usage(const char *app)
|
|
|
|
{
|
|
|
|
printf("Usage: %s\n", app);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
|
|
|
printf(" Some help...\n");
|
|
|
|
printf(" -h --help this text\n");
|
|
|
|
printf(" -i --gsmtap-ip The destination IP used for GSMTAP.\n");
|
2012-03-01 21:45:51 +00:00
|
|
|
printf(" -u --vty-ip The VTY IP to telnet to. "
|
|
|
|
"(default %s)\n", vty_ip);
|
2010-11-13 18:45:09 +00:00
|
|
|
printf(" -v --vty-port The VTY port number to telnet to. "
|
|
|
|
"(default %u)\n", vty_port);
|
2011-10-31 17:14:03 +00:00
|
|
|
printf(" -d --debug Change debug flags. default: %s\n",
|
|
|
|
debug_default);
|
2011-09-04 17:36:19 +00:00
|
|
|
printf(" -D --daemonize Run as daemon\n");
|
2011-09-02 09:16:10 +00:00
|
|
|
printf(" -m --mncc-sock Disable built-in MNCC handler and "
|
|
|
|
"offer socket\n");
|
2010-11-13 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_options(int argc, char **argv)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"gsmtap-ip", 1, 0, 'i'},
|
2012-03-01 21:45:51 +00:00
|
|
|
{"vty-ip", 1, 0, 'u'},
|
2010-11-13 18:45:09 +00:00
|
|
|
{"vty-port", 1, 0, 'v'},
|
|
|
|
{"debug", 1, 0, 'd'},
|
2011-09-04 17:36:19 +00:00
|
|
|
{"daemonize", 0, 0, 'D'},
|
2011-09-02 09:16:10 +00:00
|
|
|
{"mncc-sock", 0, 0, 'm'},
|
2010-11-13 18:45:09 +00:00
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
2012-03-01 21:45:51 +00:00
|
|
|
c = getopt_long(argc, argv, "hi:u:v:d:Dm",
|
2010-11-13 18:45:09 +00:00
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_usage(argv[0]);
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'i':
|
2011-05-22 10:36:55 +00:00
|
|
|
gsmtap_ip = optarg;
|
2010-11-13 18:45:09 +00:00
|
|
|
break;
|
2012-03-01 21:45:51 +00:00
|
|
|
case 'u':
|
|
|
|
vty_ip = optarg;
|
|
|
|
break;
|
2010-11-13 18:45:09 +00:00
|
|
|
case 'v':
|
|
|
|
vty_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
log_parse_category_mask(stderr_target, optarg);
|
2010-11-14 11:52:57 +00:00
|
|
|
debug_set = 1;
|
2010-11-13 18:45:09 +00:00
|
|
|
break;
|
2011-09-04 17:36:19 +00:00
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
|
|
|
break;
|
2011-09-02 09:16:10 +00:00
|
|
|
case 'm':
|
|
|
|
use_mncc_sock = 1;
|
|
|
|
break;
|
2010-11-13 18:45:09 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sighandler(int sigset)
|
|
|
|
{
|
2013-01-07 09:39:59 +00:00
|
|
|
static uint8_t _quit = 1, count_int = 0;
|
|
|
|
|
2010-11-13 18:45:09 +00:00
|
|
|
if (sigset == SIGHUP || sigset == SIGPIPE)
|
|
|
|
return;
|
|
|
|
|
2013-01-07 09:39:59 +00:00
|
|
|
fprintf(stderr, "\nSignal %d received.\n", sigset);
|
2010-11-13 18:45:09 +00:00
|
|
|
|
2011-11-05 09:05:40 +00:00
|
|
|
switch (sigset) {
|
|
|
|
case SIGINT:
|
2013-01-07 09:39:59 +00:00
|
|
|
/* The first signal causes initiating of shutdown with detach
|
|
|
|
* procedure. The second signal causes initiating of shutdown
|
|
|
|
* without detach procedure. The third signal will exit process
|
|
|
|
* immidiately. (in case it hangs)
|
2011-11-05 09:05:40 +00:00
|
|
|
*/
|
2013-01-07 09:39:59 +00:00
|
|
|
if (count_int == 0) {
|
|
|
|
fprintf(stderr, "Performing shutdown with clean "
|
|
|
|
"detach, if possible...\n");
|
|
|
|
osmo_signal_dispatch(SS_GLOBAL, S_GLOBAL_SHUTDOWN,
|
|
|
|
NULL);
|
|
|
|
count_int = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (count_int == 2) {
|
|
|
|
fprintf(stderr, "Unclean exit, please turn off phone "
|
|
|
|
"to be sure it is not transmitting!\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case SIGTSTP:
|
|
|
|
count_int = 2;
|
|
|
|
fprintf(stderr, "Performing shutdown without detach...\n");
|
|
|
|
osmo_signal_dispatch(SS_GLOBAL, S_GLOBAL_SHUTDOWN, &_quit);
|
2011-11-05 09:05:40 +00:00
|
|
|
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(l23_ctx, stderr);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-13 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-11-14 11:52:57 +00:00
|
|
|
int quit = 0;
|
2010-11-13 18:45:09 +00:00
|
|
|
int rc;
|
2011-03-02 09:35:04 +00:00
|
|
|
char const * home;
|
|
|
|
size_t len;
|
|
|
|
const char osmocomcfg[] = ".osmocom/bb/mobile.cfg";
|
|
|
|
char *config_file = NULL;
|
2010-11-13 18:45:09 +00:00
|
|
|
|
|
|
|
printf("%s\n", openbsc_copyright);
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
INIT_LLIST_HEAD(&ms_list);
|
2011-07-28 19:28:41 +00:00
|
|
|
log_init(&log_info, NULL);
|
2010-11-13 18:45:09 +00:00
|
|
|
stderr_target = log_target_create_stderr();
|
|
|
|
log_add_target(stderr_target);
|
|
|
|
log_set_all_filter(stderr_target, 1);
|
|
|
|
|
|
|
|
l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
|
2011-11-05 09:05:40 +00:00
|
|
|
msgb_set_talloc_ctx(l23_ctx);
|
2010-11-13 18:45:09 +00:00
|
|
|
|
|
|
|
handle_options(argc, argv);
|
|
|
|
|
2010-11-14 11:52:57 +00:00
|
|
|
if (!debug_set)
|
2011-10-31 17:14:03 +00:00
|
|
|
log_parse_category_mask(stderr_target, debug_default);
|
2011-06-12 20:39:47 +00:00
|
|
|
log_set_log_level(stderr_target, LOGL_DEBUG);
|
2010-11-13 18:45:09 +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-11-13 18:45:09 +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-11-13 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 09:35:04 +00:00
|
|
|
home = getenv("HOME");
|
|
|
|
if (home != NULL) {
|
|
|
|
len = strlen(home) + 1 + sizeof(osmocomcfg);
|
|
|
|
config_file = talloc_size(l23_ctx, len);
|
|
|
|
if (config_file != NULL)
|
|
|
|
snprintf(config_file, len, "%s/%s", home, osmocomcfg);
|
|
|
|
}
|
2011-03-02 10:00:59 +00:00
|
|
|
/* save the config file directory name */
|
|
|
|
config_dir = talloc_strdup(l23_ctx, config_file);
|
|
|
|
config_dir = dirname(config_dir);
|
|
|
|
|
2011-09-02 09:16:10 +00:00
|
|
|
if (use_mncc_sock)
|
2012-03-01 21:45:51 +00:00
|
|
|
rc = l23_app_init(mncc_recv_socket, config_file, vty_ip, vty_port);
|
2011-09-02 09:16:10 +00:00
|
|
|
else
|
2012-03-01 21:45:51 +00:00
|
|
|
rc = l23_app_init(NULL, config_file, vty_ip, vty_port);
|
2010-11-20 08:19:38 +00:00
|
|
|
if (rc)
|
|
|
|
exit(rc);
|
2010-11-14 11:52:57 +00:00
|
|
|
|
2010-11-13 18:45:09 +00:00
|
|
|
signal(SIGINT, sighandler);
|
2013-01-07 09:39:59 +00:00
|
|
|
signal(SIGTSTP, sighandler);
|
2010-11-13 18:45:09 +00:00
|
|
|
signal(SIGHUP, sighandler);
|
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
signal(SIGPIPE, sighandler);
|
2011-11-05 09:05:40 +00:00
|
|
|
signal(SIGABRT, sighandler);
|
|
|
|
signal(SIGUSR1, sighandler);
|
|
|
|
signal(SIGUSR2, sighandler);
|
2010-11-13 18:45:09 +00:00
|
|
|
|
2011-09-04 17:36:19 +00:00
|
|
|
if (daemonize) {
|
|
|
|
printf("Running as daemon\n");
|
|
|
|
rc = osmo_daemonize();
|
|
|
|
if (rc)
|
|
|
|
fprintf(stderr, "Failed to run as daemon\n");
|
|
|
|
}
|
|
|
|
|
2010-11-13 18:45:09 +00:00
|
|
|
while (1) {
|
2010-11-14 11:52:57 +00:00
|
|
|
l23_app_work(&quit);
|
2010-11-13 18:45:09 +00:00
|
|
|
if (quit && llist_empty(&ms_list))
|
|
|
|
break;
|
2011-05-15 12:23:05 +00:00
|
|
|
osmo_select_main(0);
|
2010-11-13 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 11:52:57 +00:00
|
|
|
l23_app_exit();
|
2010-11-13 18:45:09 +00:00
|
|
|
|
2011-12-11 14:47:30 +00:00
|
|
|
talloc_free(config_file);
|
|
|
|
talloc_free(config_dir);
|
2011-11-05 09:05:40 +00:00
|
|
|
talloc_report_full(l23_ctx, stderr);
|
|
|
|
|
2013-01-07 09:39:59 +00:00
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGTSTP, SIG_DFL);
|
|
|
|
signal(SIGHUP, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
signal(SIGABRT, SIG_DFL);
|
|
|
|
signal(SIGUSR1, SIG_DFL);
|
|
|
|
signal(SIGUSR2, SIG_DFL);
|
|
|
|
|
2010-11-13 18:45:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|