blob: cd713eee6feb5a2a0d2f71cada06400d5f178b87 [file] [log] [blame]
Neels Hofmeyr2188a772016-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>
Philipp Maierefe85d32017-04-09 12:32:51 +020066#include <osmocom/sigtran/osmo_ss7.h>
Neels Hofmeyr2188a772016-05-20 21:59:55 +020067#include <openbsc/mgcpgw_client.h>
68
69#include <openbsc/msc_ifaces.h>
70
71#include <openbsc/iu.h>
72#include <openbsc/iucs.h>
73#include <openbsc/iucs_ranap.h>
Philipp Maierefe85d32017-04-09 12:32:51 +020074#include <openbsc/a_iface.h>
Neels Hofmeyr2188a772016-05-20 21:59:55 +020075
76static const char * const osmomsc_copyright =
77 "OsmoMSC - Osmocom Circuit-Switched Core Network implementation\r\n"
78 "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
79 "Based on OsmoNITB:\r\n"
80 " (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>\r\n"
81 " (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>\r\n"
82 "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n"
83 "Dieter Spaar, Andreas Eversberg, Sylvain Munaut, Neels Hofmeyr\r\n\r\n"
84 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
85 "This is free software: you are free to change and redistribute it.\r\n"
86 "There is NO WARRANTY, to the extent permitted by law.\r\n";
87
88void *tall_msc_ctx = NULL;
89
90/* satisfy deps from libbsc legacy.
91 TODO double check these */
92void *tall_fle_ctx = NULL;
93void *tall_paging_ctx = NULL;
94void *tall_map_ctx = NULL;
95void *tall_upq_ctx = NULL;
96/* end deps from libbsc legacy. */
97
98static struct {
99 const char *database_name;
100 const char *config_file;
101 int daemonize;
102 const char *mncc_sock_path;
103 int use_db_counter;
104} msc_cmdline_config = {
105 "sms.db",
106 "osmo-msc.cfg",
107 0,
108 0,
109 1
110};
111
112/* timer to store statistics */
113#define DB_SYNC_INTERVAL 60, 0
114#define EXPIRE_INTERVAL 10, 0
115
116static struct osmo_timer_list db_sync_timer;
117
118static void create_pcap_file(char *file)
119{
120 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
121 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
122
123 if (fd < 0) {
124 perror("Failed to open file for pcap");
125 return;
126 }
127
128 e1_set_pcap_fd(fd);
129}
130
131static void print_usage()
132{
133 printf("Usage: osmo-nitb\n");
134}
135
136static void print_help()
137{
138 printf(" Some useful help...\n");
139 printf(" -h --help This text.\n");
140 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
141 printf(" -D --daemonize Fork the process into a background daemon.\n");
142 printf(" -c --config-file filename The config file to use.\n");
143 printf(" -s --disable-color\n");
144 printf(" -l --database db-name The database to use.\n");
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200145 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
146 printf(" -V --version Print the version of OpenBSC.\n");
147 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC.\n");
148 printf(" -e --log-level number Set a global loglevel.\n");
149 printf(" -M --mncc-sock-path PATH Disable built-in MNCC handler and offer socket.\n");
150 printf(" -m --mncc-sock Same as `-M /tmp/bsc_mncc' (deprecated).\n");
151 printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n");
152 printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n");
153 printf(" -p --pcap PATH Write abis communication to pcap trace file.\n");
154}
155
156static void handle_options(int argc, char **argv)
157{
158 while (1) {
159 int option_index = 0, c;
160 static struct option long_options[] = {
161 {"help", 0, 0, 'h'},
162 {"debug", 1, 0, 'd'},
163 {"daemonize", 0, 0, 'D'},
164 {"config-file", 1, 0, 'c'},
165 {"disable-color", 0, 0, 's'},
166 {"database", 1, 0, 'l'},
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200167 {"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:
Harald Welte6f750e62017-08-20 20:50:06 +0200271 case SIGTERM:
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200272 msc_network_shutdown(msc_network);
273 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
274 sleep(3);
275 exit(0);
276 break;
277 case SIGABRT:
278 osmo_generate_backtrace();
279 /* in case of abort, we want to obtain a talloc report
280 * and then return to the caller, who will abort the process */
281 case SIGUSR1:
282 talloc_report(tall_vty_ctx, stderr);
283 talloc_report_full(tall_msc_ctx, stderr);
284 break;
285 case SIGUSR2:
286 talloc_report_full(tall_vty_ctx, stderr);
287 break;
288 default:
289 break;
290 }
291}
292
293/* timer handling */
294static int _db_store_counter(struct osmo_counter *counter, void *data)
295{
296 return db_store_counter(counter);
297}
298
299static void db_sync_timer_cb(void *data)
300{
301 /* store counters to database and re-schedule */
302 osmo_counters_for_each(_db_store_counter, NULL);
303 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
304}
305
306extern int bsc_vty_go_parent(struct vty *vty);
307
308static struct vty_app_info msc_vty_info = {
309 .name = "OsmoMSC",
310 .version = PACKAGE_VERSION,
311 .go_parent_cb = bsc_vty_go_parent,
312 .is_config_node = bsc_vty_is_config_node,
313};
314
Philipp Maierefe85d32017-04-09 12:32:51 +0200315static int rcvmsg_iu_cs(struct msgb *msg, struct gprs_ra_id *ra_id, uint16_t *sai)
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200316{
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
Philipp Maierefe85d32017-04-09 12:32:51 +0200338#define DEFAULT_M3UA_REMOTE_IP "127.0.0.1"
339#define DEFAULT_PC_A "0.23.1"
340#define DEFAULT_PC_IU "0.23.2"
341#define DEFAULT_PC_A_IU DEFAULT_PC_A
342
343static struct osmo_sccp_instance *sccp_setup(void *ctx, uint32_t cs7_instance,
344 const char *label, const char *default_pc_str)
345{
346 int default_pc = osmo_ss7_pointcode_parse(NULL, default_pc_str);
347 if (default_pc < 0)
348 return NULL;
349
350 return osmo_sccp_simple_client_on_ss7_id(ctx, cs7_instance, label, default_pc,
351 OSMO_SS7_ASP_PROT_M3UA,
352 0, NULL, /* local: use arbitrary port and 0.0.0.0. */
353 0, /* remote: use protocol default port */
354 DEFAULT_M3UA_REMOTE_IP);
355 /* Note: If a differing remote IP is to be used, it was already entered in the vty config at
356 * 'cs7' / 'asp' / 'remote-ip', and this default remote IP has no effect.
357 * Similarly, 'cs7' / 'listen' can specify the local IP address. */
358}
359
360static int ss7_setup(void *ctx)
361{
362 uint32_t cs7_instance_a = msc_network->a.cs7_instance;
363#if BUILD_IU
364 uint32_t cs7_instance_iu = msc_network->iu.cs7_instance;
365
366 if (cs7_instance_a == cs7_instance_iu) {
367 /* Create one single SCCP instance which will be used for both,
368 * Iu and A at the same time, under the same point-code */
369 LOGP(DMSC, LOGL_NOTICE, "CS7 Instance identifiers: A = Iu = %u\n", cs7_instance_a);
370
371 msc_network->a.sccp = sccp_setup(ctx, cs7_instance_a, "OsmoMSC-A-Iu", DEFAULT_PC_A_IU);
372 if (!msc_network->a.sccp)
373 return -EINVAL;
374
375 msc_network->iu.sccp = msc_network->a.sccp;
376 } else {
377 /* Create two separate SCCP instances to run A and Iu independently on different
378 * pointcodes */
379 LOGP(DMSC, LOGL_NOTICE, "CS7 Instance identifiers: A = %u, Iu = %u\n",
380 cs7_instance_a, cs7_instance_iu);
381
382 msc_network->a.sccp = sccp_setup(ctx, cs7_instance_a, "OsmoMSC-A", DEFAULT_PC_A);
383 if (!msc_network->a.sccp)
384 return -EINVAL;
385
386 msc_network->iu.sccp = sccp_setup(ctx, cs7_instance_iu, "OsmoMSC-Iu", DEFAULT_PC_IU);
387 if (!msc_network->iu.sccp)
388 return -EINVAL;
389 }
390#else
391 /* No Iu support, just open up an A instance */
392 msc_network->a.sccp = sccp_setup(ctx, cs7_instance_a, "OsmoMSC-A", DEFAULT_PC_A);
393 if (!msc_network->a.sccp)
394 return -EINVAL;
395#endif
396
397 return 0;
398}
399
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200400int main(int argc, char **argv)
401{
402 int rc;
403
404 msc_vty_info.copyright = osmomsc_copyright;
405
406 tall_msc_ctx = talloc_named_const(NULL, 1, "osmo_msc");
407 talloc_ctx_init(tall_msc_ctx);
408
409 osmo_init_logging(&log_info);
410 osmo_stats_init(tall_msc_ctx);
411
412 /* For --version, vty_init() must be called before handling options */
413 vty_init(&msc_vty_info);
414
Philipp Maierefe85d32017-04-09 12:32:51 +0200415 osmo_ss7_init();
416 osmo_ss7_vty_init_asp(tall_msc_ctx);
417
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200418 /* Parse options */
419 handle_options(argc, argv);
420
421 /* Allocate global gsm_network struct; choose socket/internal MNCC */
422 msc_network = msc_network_alloc(tall_msc_ctx,
423 msc_cmdline_config.mncc_sock_path?
424 mncc_sock_from_cc
425 : int_mncc_recv);
426 if (!msc_network)
427 return -ENOMEM;
428
429 if (msc_vlr_alloc(msc_network)) {
430 fprintf(stderr, "Failed to allocate VLR\n");
431 exit(1);
432 }
433
434 ctrl_vty_init(tall_msc_ctx);
435 logging_vty_add_cmds(&log_info);
436 msc_vty_init(msc_network);
437 bsc_vty_init_extra();
438
439#ifdef BUILD_SMPP
440 if (smpp_openbsc_alloc_init(tall_msc_ctx) < 0)
441 return -1;
442#endif
443
444 /*
445 * For osmo-nitb, skip TCH/F for now, because otherwise dyn TS
446 * always imply the possibility to have a mix of TCH/F and
447 * TCH/H channels; if two phones request a TCH/F and a TCH/H,
448 * respectively, they cannot call each other. If we deny TCH/F,
449 * they will both fall back to TCH/H, and dynamic channels are
450 * usable. See OS#1778.
451 *
452 * A third-party MSC may well be able to handle a TCH/H TCH/F
453 * mismatch. Moreover, this option may be overwritten in the
454 * config file or in VTY.
455 */
456 msc_network->dyn_ts_allow_tch_f = false;
457
458 rc = vty_read_config_file(msc_cmdline_config.config_file, NULL);
459 if (rc < 0) {
460 LOGP(DNM, LOGL_FATAL, "Failed to parse the config file: '%s'\n",
461 msc_cmdline_config.config_file);
462 return 1;
463 }
464
465 /* Initialize MNCC socket if appropriate */
466 if (msc_cmdline_config.mncc_sock_path) {
467 rc = mncc_sock_init(msc_network,
468 msc_cmdline_config.mncc_sock_path);
469 if (rc) {
470 fprintf(stderr, "MNCC socket initialization failed. exiting.\n");
471 exit(1);
472 }
473 } else
474 DEBUGP(DMNCC, "Using internal MNCC handler.\n");
475
476 /* start telnet after reading config for vty_get_bind_addr() */
477 rc = telnet_init_dynif(tall_msc_ctx, &msc_network,
478 vty_get_bind_addr(), OSMO_VTY_PORT_MSC);
479 if (rc < 0)
480 return 2;
481
482 /* BSC stuff is to be split behind an A-interface to be used with
483 * OsmoBSC, but there is no need to remove it yet. Most of the
484 * following code until iu_init() is legacy. */
485
486#ifdef BUILD_SMPP
487 smpp_openbsc_start(msc_network);
488#endif
489
490 /* start control interface after reading config for
491 * ctrl_vty_get_bind_addr() */
492 msc_network->ctrl = ctrl_interface_setup_dynip(msc_network, ctrl_vty_get_bind_addr(),
493 OSMO_CTRL_PORT_MSC, NULL);
494 if (!msc_network->ctrl) {
495 printf("Failed to initialize control interface. Exiting.\n");
496 return -1;
497 }
498
499#if 0
500TODO: we probably want some of the _net_ ctrl commands from bsc_base_ctrl_cmds_install().
501 if (bsc_base_ctrl_cmds_install() != 0) {
502 printf("Failed to initialize the BSC control commands.\n");
503 return -1;
504 }
505#endif
506
507 if (msc_ctrl_cmds_install(msc_network) != 0) {
508 printf("Failed to initialize the MSC control commands.\n");
509 return -1;
510 }
511
512 /* seed the PRNG */
513 srand(time(NULL));
514 /* TODO: is this used for crypto?? Improve randomness, at least we
515 * should try to use the nanoseconds part of the current time. */
516
517 if (db_init(msc_cmdline_config.database_name)) {
518 printf("DB: Failed to init database: %s\n",
519 msc_cmdline_config.database_name);
520 return 4;
521 }
522
523 osmo_fsm_log_addr(true);
524 if (msc_vlr_start(msc_network)) {
525 fprintf(stderr, "Failed to start VLR\n");
526 exit(1);
527 }
528
529 msc_subscr_conn_init();
530
531 if (db_prepare()) {
532 printf("DB: Failed to prepare database.\n");
533 return 5;
534 }
535
536 /* setup the timer */
537 osmo_timer_setup(&db_sync_timer, db_sync_timer_cb, NULL);
538 if (msc_cmdline_config.use_db_counter)
539 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
540
541 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200542 signal(SIGTERM, &signal_handler);
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200543 signal(SIGABRT, &signal_handler);
544 signal(SIGUSR1, &signal_handler);
545 signal(SIGUSR2, &signal_handler);
546 osmo_init_ignore_signals();
547
548 /* start the SMS queue */
549 if (sms_queue_start(msc_network, 20) != 0)
550 return -1;
551
552 msc_network->mgcpgw.client = mgcpgw_client_init(
553 msc_network, &msc_network->mgcpgw.conf);
554
555 if (mgcpgw_client_connect(msc_network->mgcpgw.client)) {
556 printf("MGCPGW connect failed\n");
557 return 7;
558 }
559
Philipp Maierefe85d32017-04-09 12:32:51 +0200560 if (ss7_setup(tall_msc_ctx)) {
561 printf("Setting up SCCP client failed.\n");
562 return 8;
563 }
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200564
565#ifdef BUILD_IU
566 /* Set up IuCS */
Philipp Maierefe85d32017-04-09 12:32:51 +0200567 iu_init(tall_msc_ctx, msc_network->iu.sccp, rcvmsg_iu_cs, rx_iu_event);
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200568#endif
569
Philipp Maierefe85d32017-04-09 12:32:51 +0200570 /* Set up A interface */
571 a_init(msc_network->a.sccp, msc_network);
572
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200573 if (msc_cmdline_config.daemonize) {
574 rc = osmo_daemonize();
575 if (rc < 0) {
576 perror("Error during daemonize");
577 return 6;
578 }
579 }
580
581 while (1) {
582 log_reset_context();
583 osmo_select_main(0);
584 }
585}