blob: 9693dc4766c45809e164b6e22a8734fdbbd72140 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte1c369472010-01-12 10:46:27 +01003/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte59b04682009-06-10 05:40:52 +08004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte59b04682009-06-10 05:40:52 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte59b04682009-06-10 05:40:52 +080019 *
20 */
21
22#include <unistd.h>
Harald Welte59b04682009-06-10 05:40:52 +080023#include <time.h>
Harald Welte59b04682009-06-10 05:40:52 +080024#include <errno.h>
25#include <signal.h>
26#include <fcntl.h>
27#include <sys/stat.h>
28
29#define _GNU_SOURCE
30#include <getopt.h>
31
32#include <openbsc/db.h>
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +020033#include <osmocom/core/application.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010034#include <osmocom/core/select.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/debug.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010037#include <osmocom/core/talloc.h>
Harald Welted21adfe2009-11-30 19:37:18 +010038#include <openbsc/signal.h>
Holger Hans Peter Freytherc7efbe42010-06-30 14:30:35 +080039#include <openbsc/osmo_msc.h>
Harald Welte30f93fb2011-02-19 16:48:17 +010040#include <openbsc/sms_queue.h>
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +080041#include <openbsc/vty.h>
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +020042#include <openbsc/bss.h>
Harald Welte28dce912011-05-24 13:25:38 +020043#include <openbsc/mncc.h>
Harald Welte40152872010-05-16 20:52:23 +020044
Harald Welted9dea592011-03-04 13:53:51 +010045#include "../../bscconfig.h"
Harald Welte40152872010-05-16 20:52:23 +020046
Harald Welte59b04682009-06-10 05:40:52 +080047/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020048struct gsm_network *bsc_gsmnet = 0;
Harald Welte59b04682009-06-10 05:40:52 +080049static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020050static const char *config_file = "openbsc.cfg";
Harald Welte40152872010-05-16 20:52:23 +020051extern const char *openbsc_copyright;
Harald Weltec4ae1762010-08-25 19:43:54 +020052static int daemonize = 0;
Harald Weltee532fb72010-12-23 01:31:07 +010053static int use_mncc_sock = 0;
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +010054
55/* timer to store statistics */
56#define DB_SYNC_INTERVAL 60, 0
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +020057static struct osmo_timer_list db_sync_timer;
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +010058
Harald Welte59b04682009-06-10 05:40:52 +080059static void create_pcap_file(char *file)
60{
61 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
62 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
63
64 if (fd < 0) {
65 perror("Failed to open file for pcap");
66 return;
67 }
68
69 e1_set_pcap_fd(fd);
70}
71
72static void print_usage()
73{
74 printf("Usage: bsc_hack\n");
75}
76
77static void print_help()
78{
79 printf(" Some useful help...\n");
Harald Welte62868882009-08-08 16:12:58 +020080 printf(" -h --help this text\n");
Harald Welte59b04682009-06-10 05:40:52 +080081 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Weltec4ae1762010-08-25 19:43:54 +020082 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020083 printf(" -c --config-file filename The config file to use.\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010084 printf(" -s --disable-color\n");
Harald Welte59b04682009-06-10 05:40:52 +080085 printf(" -l --database db-name The database to use\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010086 printf(" -a --authorize-everyone. Authorize every new subscriber. Dangerous!.\n");
Harald Welte59b04682009-06-10 05:40:52 +080087 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltebea12fe2009-08-12 21:52:11 +020088 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010089 printf(" -V --version. Print the version of OpenBSC.\n");
Harald Weltedae9a012009-12-18 15:05:02 +010090 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC\n");
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +010091 printf(" -e --log-level number. Set a global loglevel.\n");
Harald Weltee532fb72010-12-23 01:31:07 +010092 printf(" -m --mncc-sock Disable built-in MNCC handler and offer socket\n");
Harald Welte59b04682009-06-10 05:40:52 +080093}
94
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +080095static void handle_options(int argc, char **argv)
Harald Welte59b04682009-06-10 05:40:52 +080096{
97 while (1) {
Harald Weltea8379772009-06-20 22:36:41 +020098 int option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +080099 static struct option long_options[] = {
100 {"help", 0, 0, 'h'},
101 {"debug", 1, 0, 'd'},
Harald Weltec4ae1762010-08-25 19:43:54 +0200102 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200103 {"config-file", 1, 0, 'c'},
Harald Welte59b04682009-06-10 05:40:52 +0800104 {"disable-color", 0, 0, 's'},
Harald Welte59b04682009-06-10 05:40:52 +0800105 {"database", 1, 0, 'l'},
106 {"authorize-everyone", 0, 0, 'a'},
Harald Welte59b04682009-06-10 05:40:52 +0800107 {"pcap", 1, 0, 'p'},
Harald Welte59b04682009-06-10 05:40:52 +0800108 {"timestamp", 0, 0, 'T'},
Harald Weltefa13cad2010-03-23 00:09:32 +0800109 {"version", 0, 0, 'V' },
Harald Welte3c062072009-07-28 18:25:29 +0200110 {"rtp-proxy", 0, 0, 'P'},
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100111 {"log-level", 1, 0, 'e'},
Harald Weltee532fb72010-12-23 01:31:07 +0100112 {"mncc-sock", 0, 0, 'm'},
Harald Welte59b04682009-06-10 05:40:52 +0800113 {0, 0, 0, 0}
114 };
115
Harald Weltee532fb72010-12-23 01:31:07 +0100116 c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:m",
Harald Welte59b04682009-06-10 05:40:52 +0800117 long_options, &option_index);
118 if (c == -1)
119 break;
120
121 switch (c) {
122 case 'h':
123 print_usage();
124 print_help();
125 exit(0);
126 case 's':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200127 log_set_use_color(osmo_stderr_target, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800128 break;
129 case 'd':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200130 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte59b04682009-06-10 05:40:52 +0800131 break;
Harald Weltec4ae1762010-08-25 19:43:54 +0200132 case 'D':
133 daemonize = 1;
134 break;
Harald Welte59b04682009-06-10 05:40:52 +0800135 case 'l':
136 database_name = strdup(optarg);
137 break;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200138 case 'c':
139 config_file = strdup(optarg);
140 break;
Harald Welte59b04682009-06-10 05:40:52 +0800141 case 'p':
142 create_pcap_file(optarg);
143 break;
Harald Welte59b04682009-06-10 05:40:52 +0800144 case 'T':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200145 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800146 break;
Harald Welte3c062072009-07-28 18:25:29 +0200147 case 'P':
148 ipacc_rtp_direct = 0;
149 break;
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100150 case 'e':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200151 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100152 break;
Harald Weltee532fb72010-12-23 01:31:07 +0100153 case 'm':
154 use_mncc_sock = 1;
155 break;
Harald Weltefa13cad2010-03-23 00:09:32 +0800156 case 'V':
Harald Welte10c29f62010-05-16 19:20:24 +0200157 print_version(1);
Harald Weltefa13cad2010-03-23 00:09:32 +0800158 exit(0);
159 break;
Harald Welte59b04682009-06-10 05:40:52 +0800160 default:
161 /* ignore */
162 break;
163 }
164 }
165}
166
Harald Welte0c836602009-12-24 10:51:56 +0100167extern void *tall_vty_ctx;
Harald Welte59b04682009-06-10 05:40:52 +0800168static void signal_handler(int signal)
169{
170 fprintf(stdout, "signal %u received\n", signal);
171
172 switch (signal) {
Harald Welte3b714502009-08-06 17:43:50 +0200173 case SIGINT:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200174 bsc_shutdown_net(bsc_gsmnet);
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200175 osmo_signal_dispatch(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
Harald Welte3b714502009-08-06 17:43:50 +0200176 sleep(3);
177 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800178 break;
Harald Welte4d5e6d52009-08-07 00:29:44 +0200179 case SIGABRT:
180 /* in case of abort, we want to obtain a talloc report
181 * and then return to the caller, who will abort the process */
Harald Weltea8379772009-06-20 22:36:41 +0200182 case SIGUSR1:
Harald Welte0c836602009-12-24 10:51:56 +0100183 talloc_report(tall_vty_ctx, stderr);
Harald Weltea8379772009-06-20 22:36:41 +0200184 talloc_report_full(tall_bsc_ctx, stderr);
185 break;
Harald Welte0c836602009-12-24 10:51:56 +0100186 case SIGUSR2:
187 talloc_report_full(tall_vty_ctx, stderr);
188 break;
Harald Welte59b04682009-06-10 05:40:52 +0800189 default:
190 break;
191 }
192}
193
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100194/* timer handling */
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200195static int _db_store_counter(struct osmo_counter *counter, void *data)
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100196{
197 return db_store_counter(counter);
198}
199
200static void db_sync_timer_cb(void *data)
201{
202 /* store counters to database and re-schedule */
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200203 osmo_counters_for_each(_db_store_counter, NULL);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200204 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100205}
206
Harald Welte30f93fb2011-02-19 16:48:17 +0100207void talloc_ctx_init(void);
Harald Welte59698fb2010-01-10 18:01:52 +0100208
Holger Hans Peter Freyther3538c772010-06-08 16:11:06 +0800209extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Welte778c4b92010-05-25 23:31:39 +0200210
211static struct vty_app_info vty_info = {
212 .name = "OpenBSC",
213 .version = PACKAGE_VERSION,
214 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +0800215 .is_config_node = bsc_vty_is_config_node,
Harald Welte778c4b92010-05-25 23:31:39 +0200216};
217
Harald Welte59b04682009-06-10 05:40:52 +0800218int main(int argc, char **argv)
219{
220 int rc;
221
Harald Welte778c4b92010-05-25 23:31:39 +0200222 vty_info.copyright = openbsc_copyright;
223
Harald Weltea8379772009-06-20 22:36:41 +0200224 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)8751ee92009-08-15 02:30:58 +0200225 talloc_ctx_init();
Harald Welte (local)bd931f12009-08-13 13:52:14 +0200226 on_dso_load_token();
Harald Welte (local)95713782009-08-16 13:18:51 +0200227 on_dso_load_rrlp();
Harald Welteb90d7bd2009-12-17 00:31:10 +0100228 on_dso_load_ho_dec();
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200229
230 osmo_init_logging(&log_info);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100231
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +0200232 bts_init();
Harald Weltee76bea02011-02-05 13:54:41 +0100233 e1inp_init();
234
Harald Welte40152872010-05-16 20:52:23 +0200235 /* This needs to precede handle_options() */
Harald Welte778c4b92010-05-25 23:31:39 +0200236 vty_init(&vty_info);
Pablo Neira Ayuso36ad9a42011-03-09 13:36:32 +0100237 bsc_vty_init(&log_info);
Harald Welte40152872010-05-16 20:52:23 +0200238
Holger Hans Peter Freyther4f8b9eb2010-05-31 21:42:53 +0800239 /* parse options */
240 handle_options(argc, argv);
241
Harald Weltee532fb72010-12-23 01:31:07 +0100242 /* internal MNCC handler or MNCC socket? */
243 if (use_mncc_sock) {
244 rc = bsc_bootstrap_network(mncc_sock_from_cc, config_file);
245 if (rc >= 0)
246 mncc_sock_init(bsc_gsmnet);
247 } else
248 rc = bsc_bootstrap_network(int_mncc_recv, config_file);
Harald Welte10c29f62010-05-16 19:20:24 +0200249 if (rc < 0)
250 exit(1);
Holger Hans Peter Freytherc7efbe42010-06-30 14:30:35 +0800251 bsc_api_init(bsc_gsmnet, msc_bsc_api());
Harald Welte10c29f62010-05-16 19:20:24 +0200252
Daniel Willmann74aa6b42011-02-18 16:17:58 +0100253 controlif_setup(bsc_gsmnet, 4249);
Harald Welte59b04682009-06-10 05:40:52 +0800254 /* seed the PRNG */
255 srand(time(NULL));
256
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200257 if (db_init(database_name)) {
258 printf("DB: Failed to init database. Please check the option settings.\n");
259 return -1;
260 }
261 printf("DB: Database initialized.\n");
262
263 if (db_prepare()) {
264 printf("DB: Failed to prepare database.\n");
265 return -1;
266 }
267 printf("DB: Database prepared.\n");
268
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100269 /* setup the timer */
270 db_sync_timer.cb = db_sync_timer_cb;
271 db_sync_timer.data = NULL;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200272 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100273
Harald Welte3b714502009-08-06 17:43:50 +0200274 signal(SIGINT, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800275 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +0200276 signal(SIGUSR1, &signal_handler);
Harald Welte0c836602009-12-24 10:51:56 +0100277 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200278 osmo_init_ignore_signals();
Harald Welte59b04682009-06-10 05:40:52 +0800279
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100280 /* start the SMS queue */
Holger Hans Peter Freyther48710462010-12-25 17:43:03 +0100281 if (sms_queue_start(bsc_gsmnet, 20) != 0)
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100282 return -1;
283
Harald Weltec4ae1762010-08-25 19:43:54 +0200284 if (daemonize) {
285 rc = osmo_daemonize();
286 if (rc < 0) {
287 perror("Error during daemonize");
288 exit(1);
289 }
290 }
291
Harald Welte59b04682009-06-10 05:40:52 +0800292 while (1) {
Harald Welte51d2a592010-03-26 21:28:59 +0800293 log_reset_context();
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200294 osmo_select_main(0);
Harald Welte59b04682009-06-10 05:40:52 +0800295 }
296}