blob: 001d8f9a494b243da601987cc0d39f01a9142bf0 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte06770612010-01-12 10:46:27 +01003/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Freyther219518d2009-01-02 22:04:43 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte52b1f982008-12-23 20:25:15 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-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 Welte52b1f982008-12-23 20:25:15 +000010 * (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 Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000016 *
Harald Welte9af6ddf2011-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 Welte52b1f982008-12-23 20:25:15 +000019 *
20 */
21
Harald Weltef6b7a902008-12-26 00:05:11 +000022#include <unistd.h>
Harald Weltef6b7a902008-12-26 00:05:11 +000023#include <time.h>
Harald Weltead384642008-12-26 10:20:07 +000024#include <errno.h>
Harald Welted1252502009-01-01 01:50:32 +000025#include <signal.h>
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000026#include <fcntl.h>
27#include <sys/stat.h>
Harald Welte52b1f982008-12-23 20:25:15 +000028
Holger Freytherb332f612008-12-27 12:46:51 +000029#define _GNU_SOURCE
30#include <getopt.h>
31
Harald Welte255539c2008-12-28 02:26:27 +000032#include <openbsc/db.h>
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020033#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/select.h>
Harald Welte702d8702008-12-26 20:25:35 +000035#include <openbsc/debug.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020036#include <osmocom/abis/abis.h>
37#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010038#include <osmocom/core/talloc.h>
Harald Welte3961fcc2009-11-30 19:37:18 +010039#include <openbsc/signal.h>
Holger Hans Peter Freytherec4bfdc2010-06-30 14:30:35 +080040#include <openbsc/osmo_msc.h>
Harald Welte4d54d0b2011-02-19 16:48:17 +010041#include <openbsc/sms_queue.h>
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +080042#include <openbsc/vty.h>
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020043#include <openbsc/bss.h>
Harald Weltef142c972011-05-24 13:25:38 +020044#include <openbsc/mncc.h>
Alexander Huemer59947a02011-09-06 00:09:48 +020045#include <openbsc/token_auth.h>
46#include <openbsc/handover_decision.h>
47#include <openbsc/rrlp.h>
48#include <openbsc/control_if.h>
Harald Weltedcccb182010-05-16 20:52:23 +020049
Harald Weltec08e8be2011-03-04 13:53:51 +010050#include "../../bscconfig.h"
Harald Weltedcccb182010-05-16 20:52:23 +020051
Holger Freytherefde7fb2008-12-28 14:14:56 +000052/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020053struct gsm_network *bsc_gsmnet = 0;
Holger Freytherbde36102008-12-28 22:51:39 +000054static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020055static const char *config_file = "openbsc.cfg";
Harald Weltedcccb182010-05-16 20:52:23 +020056extern const char *openbsc_copyright;
Harald Welte2c869ef2010-08-25 19:43:54 +020057static int daemonize = 0;
Harald Welte02d99662010-12-23 01:31:07 +010058static int use_mncc_sock = 0;
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +010059
60/* timer to store statistics */
61#define DB_SYNC_INTERVAL 60, 0
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020062static struct osmo_timer_list db_sync_timer;
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +010063
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000064static void create_pcap_file(char *file)
65{
66 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
67 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
68
69 if (fd < 0) {
70 perror("Failed to open file for pcap");
71 return;
72 }
73
Holger Freyther0469cf62009-03-31 12:14:16 +000074 e1_set_pcap_fd(fd);
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000075}
76
Holger Freytherb332f612008-12-27 12:46:51 +000077static void print_usage()
78{
79 printf("Usage: bsc_hack\n");
80}
81
82static void print_help()
83{
84 printf(" Some useful help...\n");
Harald Welte42581822009-08-08 16:12:58 +020085 printf(" -h --help this text\n");
Holger Freytherb332f612008-12-27 12:46:51 +000086 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +020087 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020088 printf(" -c --config-file filename The config file to use.\n");
Holger Hans Peter Freyther009ad612010-03-25 02:41:38 +010089 printf(" -s --disable-color\n");
Holger Freytherbde36102008-12-28 22:51:39 +000090 printf(" -l --database db-name The database to use\n");
Holger Hans Peter Freyther009ad612010-03-25 02:41:38 +010091 printf(" -a --authorize-everyone. Authorize every new subscriber. Dangerous!.\n");
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000092 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltec70979a2009-08-12 21:52:11 +020093 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Holger Hans Peter Freyther009ad612010-03-25 02:41:38 +010094 printf(" -V --version. Print the version of OpenBSC.\n");
Harald Weltef314f682009-12-18 15:05:02 +010095 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC\n");
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +010096 printf(" -e --log-level number. Set a global loglevel.\n");
Harald Welte02d99662010-12-23 01:31:07 +010097 printf(" -m --mncc-sock Disable built-in MNCC handler and offer socket\n");
Holger Freytherb332f612008-12-27 12:46:51 +000098}
99
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800100static void handle_options(int argc, char **argv)
Holger Freytherb332f612008-12-27 12:46:51 +0000101{
102 while (1) {
Harald Welte2cf161b2009-06-20 22:36:41 +0200103 int option_index = 0, c;
Holger Freytherb332f612008-12-27 12:46:51 +0000104 static struct option long_options[] = {
105 {"help", 0, 0, 'h'},
106 {"debug", 1, 0, 'd'},
Harald Welte2c869ef2010-08-25 19:43:54 +0200107 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200108 {"config-file", 1, 0, 'c'},
Holger Freytherefde7fb2008-12-28 14:14:56 +0000109 {"disable-color", 0, 0, 's'},
Holger Freytherbde36102008-12-28 22:51:39 +0000110 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +0000111 {"authorize-everyone", 0, 0, 'a'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000112 {"pcap", 1, 0, 'p'},
Harald Welted3ff51d2009-06-09 20:21:57 +0000113 {"timestamp", 0, 0, 'T'},
Harald Welte5a29c7f2010-03-23 00:09:32 +0800114 {"version", 0, 0, 'V' },
Harald Welte805f6442009-07-28 18:25:29 +0200115 {"rtp-proxy", 0, 0, 'P'},
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100116 {"log-level", 1, 0, 'e'},
Harald Welte02d99662010-12-23 01:31:07 +0100117 {"mncc-sock", 0, 0, 'm'},
Holger Freytherb332f612008-12-27 12:46:51 +0000118 {0, 0, 0, 0}
119 };
120
Harald Welte02d99662010-12-23 01:31:07 +0100121 c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:m",
Holger Freytherb332f612008-12-27 12:46:51 +0000122 long_options, &option_index);
123 if (c == -1)
124 break;
125
126 switch (c) {
127 case 'h':
128 print_usage();
129 print_help();
130 exit(0);
Holger Freytherefde7fb2008-12-28 14:14:56 +0000131 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200132 log_set_use_color(osmo_stderr_target, 0);
Holger Freytherb332f612008-12-27 12:46:51 +0000133 break;
134 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200135 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Freytherb332f612008-12-27 12:46:51 +0000136 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200137 case 'D':
138 daemonize = 1;
139 break;
Harald Welte8965da42009-01-06 18:09:02 +0000140 case 'l':
Holger Freytherbde36102008-12-28 22:51:39 +0000141 database_name = strdup(optarg);
142 break;
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200143 case 'c':
144 config_file = strdup(optarg);
145 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000146 case 'p':
147 create_pcap_file(optarg);
148 break;
Harald Welted3ff51d2009-06-09 20:21:57 +0000149 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200150 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welted3ff51d2009-06-09 20:21:57 +0000151 break;
Harald Welte805f6442009-07-28 18:25:29 +0200152 case 'P':
153 ipacc_rtp_direct = 0;
154 break;
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100155 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200156 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100157 break;
Harald Welte02d99662010-12-23 01:31:07 +0100158 case 'm':
159 use_mncc_sock = 1;
160 break;
Harald Welte5a29c7f2010-03-23 00:09:32 +0800161 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200162 print_version(1);
Harald Welte5a29c7f2010-03-23 00:09:32 +0800163 exit(0);
164 break;
Holger Freytherb332f612008-12-27 12:46:51 +0000165 default:
166 /* ignore */
167 break;
168 }
169 }
170}
171
Harald Welte3cefa9a2009-12-24 10:51:56 +0100172extern void *tall_vty_ctx;
Harald Welted1252502009-01-01 01:50:32 +0000173static void signal_handler(int signal)
174{
175 fprintf(stdout, "signal %u received\n", signal);
176
177 switch (signal) {
Harald Weltef294f452009-08-06 17:43:50 +0200178 case SIGINT:
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200179 bsc_shutdown_net(bsc_gsmnet);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200180 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Weltef294f452009-08-06 17:43:50 +0200181 sleep(3);
182 exit(0);
Harald Welted1252502009-01-01 01:50:32 +0000183 break;
Harald Welte31c3d342009-08-07 00:29:44 +0200184 case SIGABRT:
185 /* in case of abort, we want to obtain a talloc report
186 * and then return to the caller, who will abort the process */
Harald Welte2cf161b2009-06-20 22:36:41 +0200187 case SIGUSR1:
Harald Welte3cefa9a2009-12-24 10:51:56 +0100188 talloc_report(tall_vty_ctx, stderr);
Harald Welte2cf161b2009-06-20 22:36:41 +0200189 talloc_report_full(tall_bsc_ctx, stderr);
190 break;
Harald Welte3cefa9a2009-12-24 10:51:56 +0100191 case SIGUSR2:
192 talloc_report_full(tall_vty_ctx, stderr);
193 break;
Harald Welted1252502009-01-01 01:50:32 +0000194 default:
195 break;
196 }
197}
198
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100199/* timer handling */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200200static int _db_store_counter(struct osmo_counter *counter, void *data)
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100201{
202 return db_store_counter(counter);
203}
204
205static void db_sync_timer_cb(void *data)
206{
207 /* store counters to database and re-schedule */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200208 osmo_counters_for_each(_db_store_counter, NULL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200209 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100210}
211
Harald Welte4d54d0b2011-02-19 16:48:17 +0100212void talloc_ctx_init(void);
Harald Welte39315c42010-01-10 18:01:52 +0100213
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800214extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200215
216static struct vty_app_info vty_info = {
217 .name = "OpenBSC",
218 .version = PACKAGE_VERSION,
219 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800220 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200221};
222
Harald Weltef6b7a902008-12-26 00:05:11 +0000223int main(int argc, char **argv)
224{
Harald Welte1fa60c82009-02-09 18:13:26 +0000225 int rc;
226
Harald Weltec31f4802010-05-25 23:31:39 +0200227 vty_info.copyright = openbsc_copyright;
228
Harald Welte2cf161b2009-06-20 22:36:41 +0200229 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)d19e58b2009-08-15 02:30:58 +0200230 talloc_ctx_init();
Harald Welte (local)50d12712009-08-13 13:52:14 +0200231 on_dso_load_token();
Harald Welte (local)b8afe812009-08-16 13:18:51 +0200232 on_dso_load_rrlp();
Harald Welte8d77b952009-12-17 00:31:10 +0100233 on_dso_load_ho_dec();
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200234
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200235 libosmo_abis_init(tall_bsc_ctx);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200236 osmo_init_logging(&log_info);
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200237 bts_init();
Harald Welte3016d9f2011-02-05 13:54:41 +0100238
Harald Weltedcccb182010-05-16 20:52:23 +0200239 /* This needs to precede handle_options() */
Harald Weltec31f4802010-05-25 23:31:39 +0200240 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +0100241 bsc_vty_init(&log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200242
Holger Hans Peter Freyther0c8aa732010-05-31 21:42:53 +0800243 /* parse options */
244 handle_options(argc, argv);
245
Harald Welte02d99662010-12-23 01:31:07 +0100246 /* internal MNCC handler or MNCC socket? */
247 if (use_mncc_sock) {
248 rc = bsc_bootstrap_network(mncc_sock_from_cc, config_file);
249 if (rc >= 0)
250 mncc_sock_init(bsc_gsmnet);
251 } else
252 rc = bsc_bootstrap_network(int_mncc_recv, config_file);
Harald Welte1353f962010-05-16 19:20:24 +0200253 if (rc < 0)
254 exit(1);
Holger Hans Peter Freytherec4bfdc2010-06-30 14:30:35 +0800255 bsc_api_init(bsc_gsmnet, msc_bsc_api());
Harald Welte1353f962010-05-16 19:20:24 +0200256
Daniel Willmanne8aef2a2011-02-18 16:17:58 +0100257 controlif_setup(bsc_gsmnet, 4249);
Harald Welte65ccf882009-02-24 22:36:20 +0000258 /* seed the PRNG */
259 srand(time(NULL));
260
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200261 if (db_init(database_name)) {
262 printf("DB: Failed to init database. Please check the option settings.\n");
263 return -1;
264 }
265 printf("DB: Database initialized.\n");
266
267 if (db_prepare()) {
268 printf("DB: Failed to prepare database.\n");
269 return -1;
270 }
271 printf("DB: Database prepared.\n");
272
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100273 /* setup the timer */
274 db_sync_timer.cb = db_sync_timer_cb;
275 db_sync_timer.data = NULL;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200276 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100277
Harald Weltef294f452009-08-06 17:43:50 +0200278 signal(SIGINT, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +0000279 signal(SIGABRT, &signal_handler);
Harald Welte2cf161b2009-06-20 22:36:41 +0200280 signal(SIGUSR1, &signal_handler);
Harald Welte3cefa9a2009-12-24 10:51:56 +0100281 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200282 osmo_init_ignore_signals();
Harald Welted1252502009-01-01 01:50:32 +0000283
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100284 /* start the SMS queue */
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100285 if (sms_queue_start(bsc_gsmnet, 20) != 0)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100286 return -1;
287
Harald Welte2c869ef2010-08-25 19:43:54 +0200288 if (daemonize) {
289 rc = osmo_daemonize();
290 if (rc < 0) {
291 perror("Error during daemonize");
292 exit(1);
293 }
294 }
295
Harald Weltef6b7a902008-12-26 00:05:11 +0000296 while (1) {
Harald Weltedc5062b2010-03-26 21:28:59 +0800297 log_reset_context();
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200298 osmo_select_main(0);
Harald Weltef6b7a902008-12-26 00:05:11 +0000299 }
300}