blob: 41661590c1f84bdad67053a4be159f9db6c22d70 [file] [log] [blame]
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001/* OsmoMSC - Circuit-Switched Core Network (MSC+VLR+HLR+SMSC) implementation
2 */
3
4/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
5 * All Rights Reserved
6 *
7 * Based on OsmoNITB:
8 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
9 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
10 * All Rights Reserved
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27#include <unistd.h>
28#include <time.h>
29#include <errno.h>
30#include <signal.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33
34#define _GNU_SOURCE
35#include <getopt.h>
36
37/* build switches from the configure script */
38#include "../../bscconfig.h"
39
40#include <openbsc/db.h>
41#include <osmocom/core/application.h>
42#include <osmocom/core/select.h>
43#include <osmocom/core/stats.h>
44#include <openbsc/debug.h>
45#include <osmocom/abis/abis.h>
46#include <osmocom/abis/e1_input.h>
47#include <osmocom/core/talloc.h>
48#include <openbsc/signal.h>
49#include <openbsc/osmo_msc.h>
50#include <openbsc/bsc_msc_data.h>
51#include <openbsc/sms_queue.h>
52#include <osmocom/vty/telnet_interface.h>
53#include <osmocom/vty/ports.h>
54#include <osmocom/vty/logging.h>
55#include <openbsc/vty.h>
56#include <openbsc/bss.h>
57#include <openbsc/mncc.h>
58#include <openbsc/handover_decision.h>
59#include <openbsc/rrlp.h>
60#include <osmocom/ctrl/control_if.h>
61#include <osmocom/ctrl/control_vty.h>
62#include <osmocom/ctrl/ports.h>
63#include <openbsc/ctrl.h>
64#include <openbsc/osmo_bsc_rf.h>
65#include <openbsc/smpp.h>
66#include <openbsc/mgcpgw_client.h>
67
68#include <openbsc/msc_ifaces.h>
69
70#include <openbsc/iu.h>
71#include <openbsc/iucs.h>
72#include <openbsc/iucs_ranap.h>
73
74static const char * const osmomsc_copyright =
75 "OsmoMSC - Osmocom Circuit-Switched Core Network implementation\r\n"
76 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
77 "Based on OsmoNITB:\r\n"
78 " (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>\r\n"
79 " (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>\r\n"
80 "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n"
81 "Dieter Spaar, Andreas Eversberg, Sylvain Munaut, Neels Hofmeyr\r\n\r\n"
82 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
83 "This is free software: you are free to change and redistribute it.\r\n"
84 "There is NO WARRANTY, to the extent permitted by law.\r\n";
85
86void *tall_msc_ctx = NULL;
87
88/* satisfy deps from libbsc legacy.
89 TODO double check these */
90void *tall_fle_ctx = NULL;
91void *tall_paging_ctx = NULL;
92void *tall_map_ctx = NULL;
93void *tall_upq_ctx = NULL;
94/* end deps from libbsc legacy. */
95
96static struct {
97 const char *database_name;
98 const char *config_file;
99 int daemonize;
100 const char *mncc_sock_path;
101 int use_db_counter;
102} msc_cmdline_config = {
103 "sms.db",
104 "osmo-msc.cfg",
105 0,
106 0,
107 1
108};
109
110/* timer to store statistics */
111#define DB_SYNC_INTERVAL 60, 0
112#define EXPIRE_INTERVAL 10, 0
113
114static struct osmo_timer_list db_sync_timer;
115
116static void create_pcap_file(char *file)
117{
118 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
119 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
120
121 if (fd < 0) {
122 perror("Failed to open file for pcap");
123 return;
124 }
125
126 e1_set_pcap_fd(fd);
127}
128
129static void print_usage()
130{
131 printf("Usage: osmo-nitb\n");
132}
133
134static void print_help()
135{
136 printf(" Some useful help...\n");
137 printf(" -h --help This text.\n");
138 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
139 printf(" -D --daemonize Fork the process into a background daemon.\n");
140 printf(" -c --config-file filename The config file to use.\n");
141 printf(" -s --disable-color\n");
142 printf(" -l --database db-name The database to use.\n");
143 printf(" -a --authorize-everyone Authorize every new subscriber. Dangerous!\n");
144 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
145 printf(" -V --version Print the version of OpenBSC.\n");
146 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC.\n");
147 printf(" -e --log-level number Set a global loglevel.\n");
148 printf(" -M --mncc-sock-path PATH Disable built-in MNCC handler and offer socket.\n");
149 printf(" -m --mncc-sock Same as `-M /tmp/bsc_mncc' (deprecated).\n");
150 printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n");
151 printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n");
152 printf(" -p --pcap PATH Write abis communication to pcap trace file.\n");
153}
154
155static void handle_options(int argc, char **argv)
156{
157 while (1) {
158 int option_index = 0, c;
159 static struct option long_options[] = {
160 {"help", 0, 0, 'h'},
161 {"debug", 1, 0, 'd'},
162 {"daemonize", 0, 0, 'D'},
163 {"config-file", 1, 0, 'c'},
164 {"disable-color", 0, 0, 's'},
165 {"database", 1, 0, 'l'},
166 {"authorize-everyone", 0, 0, 'a'},
167 {"pcap", 1, 0, 'p'},
168 {"timestamp", 0, 0, 'T'},
169 {"version", 0, 0, 'V' },
170 {"rtp-proxy", 0, 0, 'P'},
171 {"log-level", 1, 0, 'e'},
172 {"mncc-sock", 0, 0, 'm'},
173 {"mncc-sock-path", 1, 0, 'M'},
174 {"no-dbcounter", 0, 0, 'C'},
175 {0, 0, 0, 0}
176 };
177
178 c = getopt_long(argc, argv, "hd:Dsl:ap:TPVc:e:mCM:",
179 long_options, &option_index);
180 if (c == -1)
181 break;
182
183 switch (c) {
184 case 'h':
185 print_usage();
186 print_help();
187 exit(0);
188 case 's':
189 log_set_use_color(osmo_stderr_target, 0);
190 break;
191 case 'd':
192 log_parse_category_mask(osmo_stderr_target, optarg);
193 break;
194 case 'D':
195 msc_cmdline_config.daemonize = 1;
196 break;
197 case 'l':
198 msc_cmdline_config.database_name = optarg;
199 break;
200 case 'c':
201 msc_cmdline_config.config_file = optarg;
202 break;
203 case 'p':
204 create_pcap_file(optarg);
205 break;
206 case 'T':
207 log_set_print_timestamp(osmo_stderr_target, 1);
208 break;
209#if BEFORE_MSCSPLIT
210 case 'P':
211 ipacc_rtp_direct = 0;
212 break;
213#endif
214 case 'e':
215 log_set_log_level(osmo_stderr_target, atoi(optarg));
216 break;
217 case 'M':
218 msc_cmdline_config.mncc_sock_path = optarg;
219 break;
220 case 'm':
221 msc_cmdline_config.mncc_sock_path = "/tmp/bsc_mncc";
222 break;
223 case 'C':
224 msc_cmdline_config.use_db_counter = 0;
225 break;
226 case 'V':
227 print_version(1);
228 exit(0);
229 break;
230 default:
231 /* catch unknown options *as well as* missing arguments. */
232 fprintf(stderr, "Error in command line options. Exiting.\n");
233 exit(-1);
234 }
235 }
236}
237
238struct gsm_network *msc_network_alloc(void *ctx,
239 mncc_recv_cb_t mncc_recv)
240{
241 struct gsm_network *net = gsm_network_init(ctx, 1, 1, mncc_recv);
242 if (!net)
243 return NULL;
244
245 net->name_long = talloc_strdup(net, "OsmoMSC");
246 net->name_short = talloc_strdup(net, "OsmoMSC");
247
248 net->gsup_server_addr_str = talloc_strdup(net,
249 MSC_HLR_REMOTE_IP_DEFAULT);
250 net->gsup_server_port = MSC_HLR_REMOTE_PORT_DEFAULT;
251
252 mgcpgw_client_conf_init(&net->mgcpgw.conf);
253
254 return net;
255}
256
257void msc_network_shutdown(struct gsm_network *net)
258{
259 /* nothing here yet */
260}
261
262static struct gsm_network *msc_network = NULL;
263
264extern void *tall_vty_ctx;
265static void signal_handler(int signal)
266{
267 fprintf(stdout, "signal %u received\n", signal);
268
269 switch (signal) {
270 case SIGINT:
271 msc_network_shutdown(msc_network);
272 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
273 sleep(3);
274 exit(0);
275 break;
276 case SIGABRT:
277 osmo_generate_backtrace();
278 /* in case of abort, we want to obtain a talloc report
279 * and then return to the caller, who will abort the process */
280 case SIGUSR1:
281 talloc_report(tall_vty_ctx, stderr);
282 talloc_report_full(tall_msc_ctx, stderr);
283 break;
284 case SIGUSR2:
285 talloc_report_full(tall_vty_ctx, stderr);
286 break;
287 default:
288 break;
289 }
290}
291
292/* timer handling */
293static int _db_store_counter(struct osmo_counter *counter, void *data)
294{
295 return db_store_counter(counter);
296}
297
298static void db_sync_timer_cb(void *data)
299{
300 /* store counters to database and re-schedule */
301 osmo_counters_for_each(_db_store_counter, NULL);
302 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
303}
304
305extern int bsc_vty_go_parent(struct vty *vty);
306
307static struct vty_app_info msc_vty_info = {
308 .name = "OsmoMSC",
309 .version = PACKAGE_VERSION,
310 .go_parent_cb = bsc_vty_go_parent,
311 .is_config_node = bsc_vty_is_config_node,
312};
313
314static int rcvmsg_iu_cs(struct msgb *msg, struct gprs_ra_id *ra_id,
315 uint16_t *sai)
316{
317 DEBUGP(DIUCS, "got IuCS message"
318 " %d bytes: %s\n",
319 msg->len, osmo_hexdump(msg->data, msg->len));
320 if (ra_id) {
321 DEBUGP(DIUCS, "got IuCS message on"
322 " MNC %d MCC %d LAC %d RAC %d\n",
323 ra_id->mnc, ra_id->mcc, ra_id->lac, ra_id->rac);
324 }
325
326 return gsm0408_rcvmsg_iucs(msc_network, msg, ra_id? &ra_id->lac : NULL);
327}
328
329static int rx_iu_event(struct ue_conn_ctx *ctx, enum iu_event_type type,
330 void *data)
331{
332 DEBUGP(DIUCS, "got IuCS event %u: %s\n", type,
333 iu_event_type_str(type));
334
335 return iucs_rx_ranap_event(msc_network, ctx, type, data);
336}
337
338int main(int argc, char **argv)
339{
340 int rc;
341
342 msc_vty_info.copyright = osmomsc_copyright;
343
344 tall_msc_ctx = talloc_named_const(NULL, 1, "osmo_msc");
345 talloc_ctx_init(tall_msc_ctx);
346
347 osmo_init_logging(&log_info);
348 osmo_stats_init(tall_msc_ctx);
349
350 /* For --version, vty_init() must be called before handling options */
351 vty_init(&msc_vty_info);
352
353 /* Parse options */
354 handle_options(argc, argv);
355
356 /* Allocate global gsm_network struct; choose socket/internal MNCC */
357 msc_network = msc_network_alloc(tall_msc_ctx,
358 msc_cmdline_config.mncc_sock_path?
359 mncc_sock_from_cc
360 : int_mncc_recv);
361 if (!msc_network)
362 return -ENOMEM;
363
364 if (msc_vlr_alloc(msc_network)) {
365 fprintf(stderr, "Failed to allocate VLR\n");
366 exit(1);
367 }
368
369 ctrl_vty_init(tall_msc_ctx);
370 logging_vty_add_cmds(&log_info);
371 msc_vty_init(msc_network);
372 bsc_vty_init_extra();
373
374#ifdef BUILD_SMPP
375 if (smpp_openbsc_alloc_init(tall_msc_ctx) < 0)
376 return -1;
377#endif
378
379 /*
380 * For osmo-nitb, skip TCH/F for now, because otherwise dyn TS
381 * always imply the possibility to have a mix of TCH/F and
382 * TCH/H channels; if two phones request a TCH/F and a TCH/H,
383 * respectively, they cannot call each other. If we deny TCH/F,
384 * they will both fall back to TCH/H, and dynamic channels are
385 * usable. See OS#1778.
386 *
387 * A third-party MSC may well be able to handle a TCH/H TCH/F
388 * mismatch. Moreover, this option may be overwritten in the
389 * config file or in VTY.
390 */
391 msc_network->dyn_ts_allow_tch_f = false;
392
393 rc = vty_read_config_file(msc_cmdline_config.config_file, NULL);
394 if (rc < 0) {
395 LOGP(DNM, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
396 msc_cmdline_config.config_file);
397 return 1;
398 }
399
400 /* Initialize MNCC socket if appropriate */
401 if (msc_cmdline_config.mncc_sock_path) {
402 rc = mncc_sock_init(msc_network,
403 msc_cmdline_config.mncc_sock_path);
404 if (rc) {
405 fprintf(stderr, "MNCC socket initialization failed. exiting.\n");
406 exit(1);
407 }
408 } else
409 DEBUGP(DMNCC, "Using internal MNCC handler.\n");
410
411 /* start telnet after reading config for vty_get_bind_addr() */
412 rc = telnet_init_dynif(tall_msc_ctx, &msc_network,
413 vty_get_bind_addr(), OSMO_VTY_PORT_MSC);
414 if (rc < 0)
415 return 2;
416
417 /* BSC stuff is to be split behind an A-interface to be used with
418 * OsmoBSC, but there is no need to remove it yet. Most of the
419 * following code until iu_init() is legacy. */
420
421#ifdef BUILD_SMPP
422 smpp_openbsc_start(msc_network);
423#endif
424
425 /* start control interface after reading config for
426 * ctrl_vty_get_bind_addr() */
427 msc_network->ctrl = ctrl_interface_setup_dynip(msc_network, ctrl_vty_get_bind_addr(),
428 OSMO_CTRL_PORT_MSC, NULL);
429 if (!msc_network->ctrl) {
430 printf("Failed to initialize control interface. Exiting.\n");
431 return -1;
432 }
433
434#if 0
435TODO: we probably want some of the _net_ ctrl commands from bsc_base_ctrl_cmds_install().
436 if (bsc_base_ctrl_cmds_install() != 0) {
437 printf("Failed to initialize the BSC control commands.\n");
438 return -1;
439 }
440#endif
441
442 if (msc_ctrl_cmds_install(msc_network) != 0) {
443 printf("Failed to initialize the MSC control commands.\n");
444 return -1;
445 }
446
447 /* seed the PRNG */
448 srand(time(NULL));
449 /* TODO: is this used for crypto?? Improve randomness, at least we
450 * should try to use the nanoseconds part of the current time. */
451
452 if (db_init(msc_cmdline_config.database_name)) {
453 printf("DB: Failed to init database: %s\n",
454 msc_cmdline_config.database_name);
455 return 4;
456 }
457
458 osmo_fsm_log_addr(true);
459 if (msc_vlr_start(msc_network)) {
460 fprintf(stderr, "Failed to start VLR\n");
461 exit(1);
462 }
463
464 msc_subscr_conn_init();
465
466 if (db_prepare()) {
467 printf("DB: Failed to prepare database.\n");
468 return 5;
469 }
470
471 /* setup the timer */
472 osmo_timer_setup(&db_sync_timer, db_sync_timer_cb, NULL);
473 if (msc_cmdline_config.use_db_counter)
474 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
475
476 signal(SIGINT, &signal_handler);
477 signal(SIGABRT, &signal_handler);
478 signal(SIGUSR1, &signal_handler);
479 signal(SIGUSR2, &signal_handler);
480 osmo_init_ignore_signals();
481
482 /* start the SMS queue */
483 if (sms_queue_start(msc_network, 20) != 0)
484 return -1;
485
486 msc_network->mgcpgw.client = mgcpgw_client_init(
487 msc_network, &msc_network->mgcpgw.conf);
488
489 if (mgcpgw_client_connect(msc_network->mgcpgw.client)) {
490 printf("MGCPGW connect failed\n");
491 return 7;
492 }
493
494 /* Set up A-Interface */
495 /* TODO: implement A-Interface and remove above legacy stuff. */
496
497#ifdef BUILD_IU
498 /* Set up IuCS */
499 iu_init(tall_msc_ctx, "127.0.0.1", 14001, rcvmsg_iu_cs, rx_iu_event);
500#endif
501
502 if (msc_cmdline_config.daemonize) {
503 rc = osmo_daemonize();
504 if (rc < 0) {
505 perror("Error during daemonize");
506 return 6;
507 }
508 }
509
510 while (1) {
511 log_reset_context();
512 osmo_select_main(0);
513 }
514}