blob: 11499ab400da1e122ace57a81e496c78971d83f0 [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>
Harald Weltef4625b12010-02-20 16:24:02 +010033#include <osmocore/select.h>
Harald Weltec4ae1762010-08-25 19:43:54 +020034#include <osmocore/process.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>
Harald Weltef4625b12010-02-20 16:24:02 +010037#include <osmocore/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>
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +080040#include <openbsc/vty.h>
Harald Welte40152872010-05-16 20:52:23 +020041
42#include "../bscconfig.h"
43
Harald Welte59b04682009-06-10 05:40:52 +080044/* MCC and MNC for the Location Area Identifier */
Harald Welte51d2a592010-03-26 21:28:59 +080045static struct log_target *stderr_target;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020046struct gsm_network *bsc_gsmnet = 0;
Harald Welte59b04682009-06-10 05:40:52 +080047static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020048static const char *config_file = "openbsc.cfg";
Harald Welte40152872010-05-16 20:52:23 +020049extern const char *openbsc_copyright;
Harald Weltec4ae1762010-08-25 19:43:54 +020050static int daemonize = 0;
Harald Weltee532fb72010-12-23 01:31:07 +010051static int use_mncc_sock = 0;
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +010052
53/* timer to store statistics */
54#define DB_SYNC_INTERVAL 60, 0
55static struct timer_list db_sync_timer;
56
Holger Hans Peter Freyther7f661832011-01-06 14:13:44 +010057extern int bsc_bootstrap_network(int (*mncc_recv)(struct gsm_network *, struct msgb *),
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020058 const char *cfg_file);
59extern int bsc_shutdown_net(struct gsm_network *net);
Harald Welte59b04682009-06-10 05:40:52 +080060
61static void create_pcap_file(char *file)
62{
63 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
64 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
65
66 if (fd < 0) {
67 perror("Failed to open file for pcap");
68 return;
69 }
70
71 e1_set_pcap_fd(fd);
72}
73
74static void print_usage()
75{
76 printf("Usage: bsc_hack\n");
77}
78
79static void print_help()
80{
81 printf(" Some useful help...\n");
Harald Welte62868882009-08-08 16:12:58 +020082 printf(" -h --help this text\n");
Harald Welte59b04682009-06-10 05:40:52 +080083 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Weltec4ae1762010-08-25 19:43:54 +020084 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020085 printf(" -c --config-file filename The config file to use.\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010086 printf(" -s --disable-color\n");
Harald Welte59b04682009-06-10 05:40:52 +080087 printf(" -l --database db-name The database to use\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010088 printf(" -a --authorize-everyone. Authorize every new subscriber. Dangerous!.\n");
Harald Welte59b04682009-06-10 05:40:52 +080089 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltebea12fe2009-08-12 21:52:11 +020090 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +010091 printf(" -V --version. Print the version of OpenBSC.\n");
Harald Weltedae9a012009-12-18 15:05:02 +010092 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC\n");
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +010093 printf(" -e --log-level number. Set a global loglevel.\n");
Harald Weltee532fb72010-12-23 01:31:07 +010094 printf(" -m --mncc-sock Disable built-in MNCC handler and offer socket\n");
Harald Welte59b04682009-06-10 05:40:52 +080095}
96
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +080097static void handle_options(int argc, char **argv)
Harald Welte59b04682009-06-10 05:40:52 +080098{
99 while (1) {
Harald Weltea8379772009-06-20 22:36:41 +0200100 int option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +0800101 static struct option long_options[] = {
102 {"help", 0, 0, 'h'},
103 {"debug", 1, 0, 'd'},
Harald Weltec4ae1762010-08-25 19:43:54 +0200104 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200105 {"config-file", 1, 0, 'c'},
Harald Welte59b04682009-06-10 05:40:52 +0800106 {"disable-color", 0, 0, 's'},
Harald Welte59b04682009-06-10 05:40:52 +0800107 {"database", 1, 0, 'l'},
108 {"authorize-everyone", 0, 0, 'a'},
Harald Welte59b04682009-06-10 05:40:52 +0800109 {"pcap", 1, 0, 'p'},
Harald Welte59b04682009-06-10 05:40:52 +0800110 {"timestamp", 0, 0, 'T'},
Harald Weltefa13cad2010-03-23 00:09:32 +0800111 {"version", 0, 0, 'V' },
Harald Welte3c062072009-07-28 18:25:29 +0200112 {"rtp-proxy", 0, 0, 'P'},
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100113 {"log-level", 1, 0, 'e'},
Harald Weltee532fb72010-12-23 01:31:07 +0100114 {"mncc-sock", 0, 0, 'm'},
Harald Welte59b04682009-06-10 05:40:52 +0800115 {0, 0, 0, 0}
116 };
117
Harald Weltee532fb72010-12-23 01:31:07 +0100118 c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:m",
Harald Welte59b04682009-06-10 05:40:52 +0800119 long_options, &option_index);
120 if (c == -1)
121 break;
122
123 switch (c) {
124 case 'h':
125 print_usage();
126 print_help();
127 exit(0);
128 case 's':
Harald Welte51d2a592010-03-26 21:28:59 +0800129 log_set_use_color(stderr_target, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800130 break;
131 case 'd':
Harald Welte51d2a592010-03-26 21:28:59 +0800132 log_parse_category_mask(stderr_target, optarg);
Harald Welte59b04682009-06-10 05:40:52 +0800133 break;
Harald Weltec4ae1762010-08-25 19:43:54 +0200134 case 'D':
135 daemonize = 1;
136 break;
Harald Welte59b04682009-06-10 05:40:52 +0800137 case 'l':
138 database_name = strdup(optarg);
139 break;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200140 case 'c':
141 config_file = strdup(optarg);
142 break;
Harald Welte59b04682009-06-10 05:40:52 +0800143 case 'p':
144 create_pcap_file(optarg);
145 break;
Harald Welte59b04682009-06-10 05:40:52 +0800146 case 'T':
Harald Welte51d2a592010-03-26 21:28:59 +0800147 log_set_print_timestamp(stderr_target, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800148 break;
Harald Welte3c062072009-07-28 18:25:29 +0200149 case 'P':
150 ipacc_rtp_direct = 0;
151 break;
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100152 case 'e':
Harald Welte51d2a592010-03-26 21:28:59 +0800153 log_set_log_level(stderr_target, atoi(optarg));
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100154 break;
Harald Weltee532fb72010-12-23 01:31:07 +0100155 case 'm':
156 use_mncc_sock = 1;
157 break;
Harald Weltefa13cad2010-03-23 00:09:32 +0800158 case 'V':
Harald Welte10c29f62010-05-16 19:20:24 +0200159 print_version(1);
Harald Weltefa13cad2010-03-23 00:09:32 +0800160 exit(0);
161 break;
Harald Welte59b04682009-06-10 05:40:52 +0800162 default:
163 /* ignore */
164 break;
165 }
166 }
167}
168
Harald Welte0c836602009-12-24 10:51:56 +0100169extern void *tall_vty_ctx;
Harald Welte59b04682009-06-10 05:40:52 +0800170static void signal_handler(int signal)
171{
172 fprintf(stdout, "signal %u received\n", signal);
173
174 switch (signal) {
Harald Welte3b714502009-08-06 17:43:50 +0200175 case SIGINT:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200176 bsc_shutdown_net(bsc_gsmnet);
Harald Welted21adfe2009-11-30 19:37:18 +0100177 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
Harald Welte3b714502009-08-06 17:43:50 +0200178 sleep(3);
179 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800180 break;
Harald Welte4d5e6d52009-08-07 00:29:44 +0200181 case SIGABRT:
182 /* in case of abort, we want to obtain a talloc report
183 * and then return to the caller, who will abort the process */
Harald Weltea8379772009-06-20 22:36:41 +0200184 case SIGUSR1:
Harald Welte0c836602009-12-24 10:51:56 +0100185 talloc_report(tall_vty_ctx, stderr);
Harald Weltea8379772009-06-20 22:36:41 +0200186 talloc_report_full(tall_bsc_ctx, stderr);
187 break;
Harald Welte0c836602009-12-24 10:51:56 +0100188 case SIGUSR2:
189 talloc_report_full(tall_vty_ctx, stderr);
190 break;
Harald Welte59b04682009-06-10 05:40:52 +0800191 default:
192 break;
193 }
194}
195
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100196/* timer handling */
197static int _db_store_counter(struct counter *counter, void *data)
198{
199 return db_store_counter(counter);
200}
201
202static void db_sync_timer_cb(void *data)
203{
204 /* store counters to database and re-schedule */
205 counters_for_each(_db_store_counter, NULL);
206 bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
207}
208
Daniel Willmann2ac4edf2010-01-11 13:50:44 +0100209extern int bts_model_unknown_init(void);
Harald Welte59698fb2010-01-10 18:01:52 +0100210extern int bts_model_bs11_init(void);
211extern int bts_model_nanobts_init(void);
212
Holger Hans Peter Freyther3538c772010-06-08 16:11:06 +0800213extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Welte778c4b92010-05-25 23:31:39 +0200214
215static struct vty_app_info vty_info = {
216 .name = "OpenBSC",
217 .version = PACKAGE_VERSION,
218 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +0800219 .is_config_node = bsc_vty_is_config_node,
Harald Welte778c4b92010-05-25 23:31:39 +0200220};
221
Harald Welte59b04682009-06-10 05:40:52 +0800222int main(int argc, char **argv)
223{
224 int rc;
225
Harald Welte778c4b92010-05-25 23:31:39 +0200226 vty_info.copyright = openbsc_copyright;
227
Harald Welte51d2a592010-03-26 21:28:59 +0800228 log_init(&log_info);
Harald Weltea8379772009-06-20 22:36:41 +0200229 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)8751ee92009-08-15 02:30:58 +0200230 talloc_ctx_init();
Harald Welte (local)bd931f12009-08-13 13:52:14 +0200231 on_dso_load_token();
Harald Welte (local)95713782009-08-16 13:18:51 +0200232 on_dso_load_rrlp();
Harald Welteb90d7bd2009-12-17 00:31:10 +0100233 on_dso_load_ho_dec();
Harald Welte51d2a592010-03-26 21:28:59 +0800234 stderr_target = log_target_create_stderr();
235 log_add_target(stderr_target);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100236
Daniel Willmann2ac4edf2010-01-11 13:50:44 +0100237 bts_model_unknown_init();
Harald Welte59698fb2010-01-10 18:01:52 +0100238 bts_model_bs11_init();
239 bts_model_nanobts_init();
240
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100241 /* enable filters */
Harald Welte51d2a592010-03-26 21:28:59 +0800242 log_set_all_filter(stderr_target, 1);
Harald Weltea8379772009-06-20 22:36:41 +0200243
Harald Welte40152872010-05-16 20:52:23 +0200244 /* This needs to precede handle_options() */
Harald Welte778c4b92010-05-25 23:31:39 +0200245 vty_init(&vty_info);
Harald Welte40152872010-05-16 20:52:23 +0200246 bsc_vty_init();
247
Holger Hans Peter Freyther4f8b9eb2010-05-31 21:42:53 +0800248 /* parse options */
249 handle_options(argc, argv);
250
Harald Weltee532fb72010-12-23 01:31:07 +0100251 /* internal MNCC handler or MNCC socket? */
252 if (use_mncc_sock) {
253 rc = bsc_bootstrap_network(mncc_sock_from_cc, config_file);
254 if (rc >= 0)
255 mncc_sock_init(bsc_gsmnet);
256 } else
257 rc = bsc_bootstrap_network(int_mncc_recv, config_file);
Harald Welte10c29f62010-05-16 19:20:24 +0200258 if (rc < 0)
259 exit(1);
Holger Hans Peter Freytherc7efbe42010-06-30 14:30:35 +0800260 bsc_api_init(bsc_gsmnet, msc_bsc_api());
Harald Welte8e0b29a2010-12-23 00:16:34 +0100261 mncc_sock_init(bsc_gsmnet);
Harald Welte10c29f62010-05-16 19:20:24 +0200262
Harald Welte59b04682009-06-10 05:40:52 +0800263 /* seed the PRNG */
264 srand(time(NULL));
265
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200266 if (db_init(database_name)) {
267 printf("DB: Failed to init database. Please check the option settings.\n");
268 return -1;
269 }
270 printf("DB: Database initialized.\n");
271
272 if (db_prepare()) {
273 printf("DB: Failed to prepare database.\n");
274 return -1;
275 }
276 printf("DB: Database prepared.\n");
277
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100278 /* setup the timer */
279 db_sync_timer.cb = db_sync_timer_cb;
280 db_sync_timer.data = NULL;
281 bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL);
282
Harald Welte3b714502009-08-06 17:43:50 +0200283 signal(SIGINT, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800284 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +0200285 signal(SIGUSR1, &signal_handler);
Harald Welte0c836602009-12-24 10:51:56 +0100286 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther49b05d32009-11-24 20:06:41 +0100287 signal(SIGPIPE, SIG_IGN);
Harald Welte59b04682009-06-10 05:40:52 +0800288
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100289 /* start the SMS queue */
Holger Hans Peter Freyther48710462010-12-25 17:43:03 +0100290 if (sms_queue_start(bsc_gsmnet, 20) != 0)
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100291 return -1;
292
Harald Weltec4ae1762010-08-25 19:43:54 +0200293 if (daemonize) {
294 rc = osmo_daemonize();
295 if (rc < 0) {
296 perror("Error during daemonize");
297 exit(1);
298 }
299 }
300
Harald Welte59b04682009-06-10 05:40:52 +0800301 while (1) {
Harald Welte51d2a592010-03-26 21:28:59 +0800302 log_reset_context();
Harald Welte59b04682009-06-10 05:40:52 +0800303 bsc_select_main(0);
304 }
305}