blob: 21585e59882850d6e9e323ff63254147f1cc1081 [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 Hans Peter Freyther2c9ea912012-05-11 23:28:19 +02004 * (C) 2009-2012 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>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010035#include <osmocom/core/stats.h>
Harald Welte702d8702008-12-26 20:25:35 +000036#include <openbsc/debug.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020037#include <osmocom/abis/abis.h>
38#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010039#include <osmocom/core/talloc.h>
Harald Welte3961fcc2009-11-30 19:37:18 +010040#include <openbsc/signal.h>
Holger Hans Peter Freytherec4bfdc2010-06-30 14:30:35 +080041#include <openbsc/osmo_msc.h>
Neels Hofmeyra42855f2017-02-23 21:49:55 +010042#include <openbsc/bsc_msc_data.h>
Harald Welte4d54d0b2011-02-19 16:48:17 +010043#include <openbsc/sms_queue.h>
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +080044#include <openbsc/vty.h>
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020045#include <openbsc/bss.h>
Harald Weltef142c972011-05-24 13:25:38 +020046#include <openbsc/mncc.h>
Alexander Huemer59947a02011-09-06 00:09:48 +020047#include <openbsc/handover_decision.h>
48#include <openbsc/rrlp.h>
Harald Welteba874b82014-08-20 23:47:15 +020049#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020050#include <osmocom/ctrl/ports.h>
Neels Hofmeyr73828152016-02-23 15:10:33 +010051#include <osmocom/ctrl/control_vty.h>
Harald Welteba874b82014-08-20 23:47:15 +020052#include <openbsc/ctrl.h>
Holger Hans Peter Freyther2c9ea912012-05-11 23:28:19 +020053#include <openbsc/osmo_bsc_rf.h>
Harald Welte7b1d25a2015-01-01 12:32:03 +010054#include <openbsc/smpp.h>
Harald Weltedcccb182010-05-16 20:52:23 +020055
Harald Weltec08e8be2011-03-04 13:53:51 +010056#include "../../bscconfig.h"
Harald Weltedcccb182010-05-16 20:52:23 +020057
Holger Freytherefde7fb2008-12-28 14:14:56 +000058/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020059struct gsm_network *bsc_gsmnet = 0;
Neels Hofmeyr9f52b062017-03-29 15:16:08 +020060static const char *database_name = "sms.db";
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020061static const char *config_file = "openbsc.cfg";
Neels Hofmeyrf4a31232016-02-23 14:57:22 +010062static const char *rf_ctrl_path = NULL;
Harald Weltedcccb182010-05-16 20:52:23 +020063extern const char *openbsc_copyright;
Harald Welte2c869ef2010-08-25 19:43:54 +020064static int daemonize = 0;
Neels Hofmeyr0ade6322016-02-23 14:55:17 +010065static const char *mncc_sock_path = NULL;
Harald Weltef7e94102011-11-13 19:02:06 +010066static int use_db_counter = 1;
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +010067
68/* timer to store statistics */
69#define DB_SYNC_INTERVAL 60, 0
Jan Luebbe1c201c62012-12-27 00:40:32 +010070#define EXPIRE_INTERVAL 10, 0
71
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020072static struct osmo_timer_list db_sync_timer;
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +010073
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000074static void create_pcap_file(char *file)
75{
76 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
77 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
78
79 if (fd < 0) {
80 perror("Failed to open file for pcap");
81 return;
82 }
83
Holger Freyther0469cf62009-03-31 12:14:16 +000084 e1_set_pcap_fd(fd);
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000085}
86
Holger Freytherb332f612008-12-27 12:46:51 +000087static void print_usage()
88{
Harald Welte19d87422013-11-27 01:28:21 +010089 printf("Usage: osmo-nitb\n");
Holger Freytherb332f612008-12-27 12:46:51 +000090}
91
92static void print_help()
93{
94 printf(" Some useful help...\n");
Neels Hofmeyr354d1f32015-09-15 11:50:50 +020095 printf(" -h --help This text.\n");
96 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
97 printf(" -D --daemonize Fork the process into a background daemon.\n");
98 printf(" -c --config-file filename The config file to use.\n");
Holger Hans Peter Freyther009ad612010-03-25 02:41:38 +010099 printf(" -s --disable-color\n");
Neels Hofmeyr354d1f32015-09-15 11:50:50 +0200100 printf(" -l --database db-name The database to use.\n");
101 printf(" -a --authorize-everyone Authorize every new subscriber. Dangerous!\n");
102 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
103 printf(" -V --version Print the version of OpenBSC.\n");
104 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC.\n");
105 printf(" -e --log-level number Set a global loglevel.\n");
Neels Hofmeyr0ade6322016-02-23 14:55:17 +0100106 printf(" -M --mncc-sock-path PATH Disable built-in MNCC handler and offer socket.\n");
107 printf(" -m --mncc-sock Same as `-M /tmp/bsc_mncc' (deprecated).\n");
Neels Hofmeyr354d1f32015-09-15 11:50:50 +0200108 printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n");
Neels Hofmeyrf4a31232016-02-23 14:57:22 +0100109 printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n");
Philipp5e5d94c2016-10-14 18:08:00 +0200110 printf(" -p --pcap PATH Write abis communication to pcap trace file.\n");
Holger Freytherb332f612008-12-27 12:46:51 +0000111}
112
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800113static void handle_options(int argc, char **argv)
Holger Freytherb332f612008-12-27 12:46:51 +0000114{
115 while (1) {
Harald Welte2cf161b2009-06-20 22:36:41 +0200116 int option_index = 0, c;
Holger Freytherb332f612008-12-27 12:46:51 +0000117 static struct option long_options[] = {
118 {"help", 0, 0, 'h'},
119 {"debug", 1, 0, 'd'},
Harald Welte2c869ef2010-08-25 19:43:54 +0200120 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200121 {"config-file", 1, 0, 'c'},
Holger Freytherefde7fb2008-12-28 14:14:56 +0000122 {"disable-color", 0, 0, 's'},
Holger Freytherbde36102008-12-28 22:51:39 +0000123 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +0000124 {"authorize-everyone", 0, 0, 'a'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000125 {"pcap", 1, 0, 'p'},
Harald Welted3ff51d2009-06-09 20:21:57 +0000126 {"timestamp", 0, 0, 'T'},
Harald Welte5a29c7f2010-03-23 00:09:32 +0800127 {"version", 0, 0, 'V' },
Harald Welte805f6442009-07-28 18:25:29 +0200128 {"rtp-proxy", 0, 0, 'P'},
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100129 {"log-level", 1, 0, 'e'},
Harald Welte02d99662010-12-23 01:31:07 +0100130 {"mncc-sock", 0, 0, 'm'},
Neels Hofmeyr0ade6322016-02-23 14:55:17 +0100131 {"mncc-sock-path", 1, 0, 'M'},
Harald Weltef7e94102011-11-13 19:02:06 +0100132 {"no-dbcounter", 0, 0, 'C'},
Holger Hans Peter Freyther2c9ea912012-05-11 23:28:19 +0200133 {"rf-ctl", 1, 0, 'r'},
Holger Freytherb332f612008-12-27 12:46:51 +0000134 {0, 0, 0, 0}
135 };
136
Neels Hofmeyr0ade6322016-02-23 14:55:17 +0100137 c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:mCr:M:",
Holger Freytherb332f612008-12-27 12:46:51 +0000138 long_options, &option_index);
139 if (c == -1)
140 break;
141
142 switch (c) {
143 case 'h':
144 print_usage();
145 print_help();
146 exit(0);
Holger Freytherefde7fb2008-12-28 14:14:56 +0000147 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200148 log_set_use_color(osmo_stderr_target, 0);
Holger Freytherb332f612008-12-27 12:46:51 +0000149 break;
150 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200151 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Freytherb332f612008-12-27 12:46:51 +0000152 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200153 case 'D':
154 daemonize = 1;
155 break;
Harald Welte8965da42009-01-06 18:09:02 +0000156 case 'l':
Daniel Willmann038f97a2014-05-21 15:46:44 +0200157 database_name = optarg;
Holger Freytherbde36102008-12-28 22:51:39 +0000158 break;
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200159 case 'c':
Daniel Willmann038f97a2014-05-21 15:46:44 +0200160 config_file = optarg;
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200161 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000162 case 'p':
163 create_pcap_file(optarg);
164 break;
Harald Welted3ff51d2009-06-09 20:21:57 +0000165 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200166 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welted3ff51d2009-06-09 20:21:57 +0000167 break;
Harald Welte805f6442009-07-28 18:25:29 +0200168 case 'P':
169 ipacc_rtp_direct = 0;
170 break;
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100171 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200172 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther829772d2010-03-25 02:39:15 +0100173 break;
Neels Hofmeyr0ade6322016-02-23 14:55:17 +0100174 case 'M':
175 mncc_sock_path = optarg;
176 break;
Harald Welte02d99662010-12-23 01:31:07 +0100177 case 'm':
Neels Hofmeyr0ade6322016-02-23 14:55:17 +0100178 mncc_sock_path = "/tmp/bsc_mncc";
Harald Welte02d99662010-12-23 01:31:07 +0100179 break;
Harald Weltef7e94102011-11-13 19:02:06 +0100180 case 'C':
181 use_db_counter = 0;
182 break;
Harald Welte5a29c7f2010-03-23 00:09:32 +0800183 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200184 print_version(1);
Harald Welte5a29c7f2010-03-23 00:09:32 +0800185 exit(0);
186 break;
Holger Hans Peter Freyther2c9ea912012-05-11 23:28:19 +0200187 case 'r':
Neels Hofmeyrf4a31232016-02-23 14:57:22 +0100188 rf_ctrl_path = optarg;
Holger Hans Peter Freyther2c9ea912012-05-11 23:28:19 +0200189 break;
Holger Freytherb332f612008-12-27 12:46:51 +0000190 default:
Neels Hofmeyrfc7add22016-02-23 14:59:43 +0100191 /* catch unknown options *as well as* missing arguments. */
192 fprintf(stderr, "Error in command line options. Exiting.\n");
193 exit(-1);
Holger Freytherb332f612008-12-27 12:46:51 +0000194 break;
195 }
196 }
197}
198
Harald Welte3cefa9a2009-12-24 10:51:56 +0100199extern void *tall_vty_ctx;
Harald Welted1252502009-01-01 01:50:32 +0000200static void signal_handler(int signal)
201{
202 fprintf(stdout, "signal %u received\n", signal);
203
204 switch (signal) {
Harald Weltef294f452009-08-06 17:43:50 +0200205 case SIGINT:
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200206 bsc_shutdown_net(bsc_gsmnet);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200207 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Weltef294f452009-08-06 17:43:50 +0200208 sleep(3);
209 exit(0);
Harald Welted1252502009-01-01 01:50:32 +0000210 break;
Harald Welte31c3d342009-08-07 00:29:44 +0200211 case SIGABRT:
Harald Welte7e82ad22016-08-25 16:35:26 +0900212 osmo_generate_backtrace();
Harald Welte31c3d342009-08-07 00:29:44 +0200213 /* in case of abort, we want to obtain a talloc report
214 * and then return to the caller, who will abort the process */
Harald Welte2cf161b2009-06-20 22:36:41 +0200215 case SIGUSR1:
Harald Welte3cefa9a2009-12-24 10:51:56 +0100216 talloc_report(tall_vty_ctx, stderr);
Harald Welte2cf161b2009-06-20 22:36:41 +0200217 talloc_report_full(tall_bsc_ctx, stderr);
218 break;
Harald Welte3cefa9a2009-12-24 10:51:56 +0100219 case SIGUSR2:
220 talloc_report_full(tall_vty_ctx, stderr);
221 break;
Harald Welted1252502009-01-01 01:50:32 +0000222 default:
223 break;
224 }
225}
226
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100227/* timer handling */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200228static int _db_store_counter(struct osmo_counter *counter, void *data)
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100229{
230 return db_store_counter(counter);
231}
232
233static void db_sync_timer_cb(void *data)
234{
235 /* store counters to database and re-schedule */
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200236 osmo_counters_for_each(_db_store_counter, NULL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200237 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100238}
239
Jan Luebbe1c201c62012-12-27 00:40:32 +0100240static void subscr_expire_cb(void *data)
241{
Harald Welte7b423ed2016-06-19 18:06:02 +0200242 /* TODO expire vlr_subscrs? */
Jan Luebbe1c201c62012-12-27 00:40:32 +0100243 osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL);
244}
245
Holger Hans Peter Freyther3e4e8cf2015-09-24 15:38:38 +0200246extern int bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200247
248static struct vty_app_info vty_info = {
249 .name = "OpenBSC",
250 .version = PACKAGE_VERSION,
251 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800252 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200253};
254
Harald Weltef6b7a902008-12-26 00:05:11 +0000255int main(int argc, char **argv)
256{
Harald Welte1fa60c82009-02-09 18:13:26 +0000257 int rc;
258
Harald Weltec31f4802010-05-25 23:31:39 +0200259 vty_info.copyright = openbsc_copyright;
260
Harald Welte2cf161b2009-06-20 22:36:41 +0200261 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Neels Hofmeyrc13e6872016-05-11 13:53:47 +0200262 talloc_ctx_init(tall_bsc_ctx);
Harald Welte (local)b8afe812009-08-16 13:18:51 +0200263 on_dso_load_rrlp();
Harald Welte8d77b952009-12-17 00:31:10 +0100264 on_dso_load_ho_dec();
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200265
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200266 libosmo_abis_init(tall_bsc_ctx);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200267 osmo_init_logging(&log_info);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100268 osmo_stats_init(tall_bsc_ctx);
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200269 bts_init();
Neels Hofmeyrfa9abac2016-12-28 18:56:19 +0100270 vty_init(&vty_info);
Harald Welte3016d9f2011-02-05 13:54:41 +0100271
Neels Hofmeyr2c05f752016-10-24 01:53:38 +0200272 /* Parse options */
273 handle_options(argc, argv);
274
275 /* Allocate global gsm_network struct; choose socket/internal MNCC */
276 rc = bsc_network_alloc(mncc_sock_path?
277 mncc_sock_from_cc : int_mncc_recv);
278 if (rc) {
279 fprintf(stderr, "Allocation failed. Exiting.\n");
280 exit(1);
281 }
282
283 /* Initialize VTY */
Maxdb0e3802017-01-12 19:35:11 +0100284 bsc_vty_init(bsc_gsmnet);
Neels Hofmeyr73828152016-02-23 15:10:33 +0100285 ctrl_vty_init(tall_bsc_ctx);
Harald Welte7b423ed2016-06-19 18:06:02 +0200286 if (msc_vlr_alloc(bsc_gsmnet)) {
287 fprintf(stderr, "Failed to allocate VLR\n");
288 exit(1);
289 }
Harald Weltedcccb182010-05-16 20:52:23 +0200290
Harald Welte338e3b32012-11-20 22:22:04 +0100291#ifdef BUILD_SMPP
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100292 if (smpp_openbsc_alloc_init(tall_bsc_ctx) < 0)
Harald Welte338e3b32012-11-20 22:22:04 +0100293 return -1;
294#endif
295
Neels Hofmeyr2c05f752016-10-24 01:53:38 +0200296 /* Initialize MNCC socket if appropriate */
Neels Hofmeyr50669be2016-05-09 21:56:42 +0200297 if (mncc_sock_path) {
298 rc = mncc_sock_init(bsc_gsmnet, mncc_sock_path);
299 if (rc) {
300 fprintf(stderr, "MNCC socket initialization failed. exiting.\n");
301 exit(1);
302 }
303 } else
Max8db12e42016-04-18 23:11:18 +0200304 DEBUGP(DMNCC, "Using internal MNCC handler.\n");
Neels Hofmeyr2c05f752016-10-24 01:53:38 +0200305
Vadim Yanitskiy7f3724e2017-03-31 23:27:44 +0700306 /*
307 * For osmo-nitb, skip TCH/F for now, because otherwise dyn TS
308 * always imply the possibility to have a mix of TCH/F and
309 * TCH/H channels; if two phones request a TCH/F and a TCH/H,
310 * respectively, they cannot call each other. If we deny TCH/F,
311 * they will both fall back to TCH/H, and dynamic channels are
312 * usable. See OS#1778.
313 *
314 * A third-party MSC may well be able to handle a TCH/H TCH/F
315 * mismatch. Moreover, this option may be overwritten in the
316 * config file or in VTY.
317 */
318 bsc_gsmnet->dyn_ts_allow_tch_f = false;
319
Neels Hofmeyr2c05f752016-10-24 01:53:38 +0200320 /* Read the config */
Neels Hofmeyre2354412016-05-11 14:12:00 +0200321 rc = bsc_network_configure(config_file);
322 if (rc < 0) {
323 fprintf(stderr, "Reading config failed. Exiting.\n");
Harald Welte1353f962010-05-16 19:20:24 +0200324 exit(1);
Neels Hofmeyre2354412016-05-11 14:12:00 +0200325 }
326
Harald Welte7159e8b2013-03-13 15:39:56 +0100327#ifdef BUILD_SMPP
Neels Hofmeyr1b0e5542016-02-24 19:15:39 +0100328 smpp_openbsc_start(bsc_gsmnet);
Harald Welte7159e8b2013-03-13 15:39:56 +0100329#endif
Holger Hans Peter Freytherec4bfdc2010-06-30 14:30:35 +0800330 bsc_api_init(bsc_gsmnet, msc_bsc_api());
Harald Welte1353f962010-05-16 19:20:24 +0200331
Neels Hofmeyr73828152016-02-23 15:10:33 +0100332 /* start control interface after reading config for
333 * ctrl_vty_get_bind_addr() */
Neels Hofmeyr73828152016-02-23 15:10:33 +0100334 bsc_gsmnet->ctrl = bsc_controlif_setup(bsc_gsmnet,
335 ctrl_vty_get_bind_addr(),
336 OSMO_CTRL_PORT_NITB_BSC);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200337 if (!bsc_gsmnet->ctrl) {
338 printf("Failed to initialize control interface. Exiting.\n");
339 return -1;
340 }
341
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +0100342 if (bsc_base_ctrl_cmds_install() != 0) {
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100343 printf("Failed to initialize the BSC control commands.\n");
344 return -1;
345 }
346
Harald Welte7b423ed2016-06-19 18:06:02 +0200347 if (msc_ctrl_cmds_install(bsc_gsmnet) != 0) {
Holger Hans Peter Freyther9dbc3f82014-03-23 12:06:36 +0100348 printf("Failed to initialize the MSC control commands.\n");
Holger Hans Peter Freytherf8c42192013-01-09 17:03:27 +0100349 return -1;
350 }
351
Harald Welte65ccf882009-02-24 22:36:20 +0000352 /* seed the PRNG */
353 srand(time(NULL));
354
Neels Hofmeyrf4a31232016-02-23 14:57:22 +0100355 bsc_gsmnet->bsc_data->rf_ctrl = osmo_bsc_rf_create(rf_ctrl_path, bsc_gsmnet);
Ciabyec6e4f82014-03-06 17:20:55 +0100356 if (!bsc_gsmnet->bsc_data->rf_ctrl) {
Holger Hans Peter Freyther362d10f2014-02-07 20:07:51 +0100357 fprintf(stderr, "Failed to create the RF service.\n");
358 exit(1);
Holger Hans Peter Freyther2c9ea912012-05-11 23:28:19 +0200359 }
360
Harald Welte7b423ed2016-06-19 18:06:02 +0200361 osmo_fsm_log_addr(true);
362 if (msc_vlr_start(bsc_gsmnet)) {
363 fprintf(stderr, "Failed to start VLR\n");
364 exit(1);
365 }
366
367 msc_subscr_conn_init();
368
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200369 if (db_init(database_name)) {
370 printf("DB: Failed to init database. Please check the option settings.\n");
371 return -1;
372 }
373 printf("DB: Database initialized.\n");
374
375 if (db_prepare()) {
376 printf("DB: Failed to prepare database.\n");
377 return -1;
378 }
379 printf("DB: Database prepared.\n");
380
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100381 /* setup the timer */
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200382 osmo_timer_setup(&db_sync_timer, db_sync_timer_cb, NULL);
Harald Weltef7e94102011-11-13 19:02:06 +0100383 if (use_db_counter)
384 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther079353a2009-12-23 05:29:04 +0100385
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200386 osmo_timer_setup(&bsc_gsmnet->subscr_expire_timer, subscr_expire_cb,
387 NULL);
Jan Luebbe1c201c62012-12-27 00:40:32 +0100388 osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL);
389
Harald Weltef294f452009-08-06 17:43:50 +0200390 signal(SIGINT, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +0000391 signal(SIGABRT, &signal_handler);
Harald Welte2cf161b2009-06-20 22:36:41 +0200392 signal(SIGUSR1, &signal_handler);
Harald Welte3cefa9a2009-12-24 10:51:56 +0100393 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200394 osmo_init_ignore_signals();
Harald Welted1252502009-01-01 01:50:32 +0000395
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100396 /* start the SMS queue */
Holger Hans Peter Freythera37e3bc2010-12-25 17:43:03 +0100397 if (sms_queue_start(bsc_gsmnet, 20) != 0)
Holger Hans Peter Freyther11b28f92010-12-24 13:48:27 +0100398 return -1;
Harald Welte338e3b32012-11-20 22:22:04 +0100399
Harald Welte2c869ef2010-08-25 19:43:54 +0200400 if (daemonize) {
401 rc = osmo_daemonize();
402 if (rc < 0) {
403 perror("Error during daemonize");
404 exit(1);
405 }
406 }
407
Harald Weltef6b7a902008-12-26 00:05:11 +0000408 while (1) {
Harald Weltedc5062b2010-03-26 21:28:59 +0800409 log_reset_context();
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200410 osmo_select_main(0);
Harald Weltef6b7a902008-12-26 00:05:11 +0000411 }
412}