Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* A hackish minimal BSC (+MSC +HLR) implementation */ |
| 2 | |
Harald Welte | 1c36947 | 2010-01-12 10:46:27 +0100 | [diff] [blame] | 3 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 4 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 10 | * (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 Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 23 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #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> |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 33 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/select.h> |
Jacob Erlbeck | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 35 | #include <osmocom/core/stats.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 37 | #include <osmocom/abis/abis.h> |
| 38 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/talloc.h> |
Harald Welte | d21adfe | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 40 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | c7efbe4 | 2010-06-30 14:30:35 +0800 | [diff] [blame] | 41 | #include <openbsc/osmo_msc.h> |
Ciaby | 38abd7b | 2014-03-06 17:20:55 +0100 | [diff] [blame] | 42 | #include <openbsc/osmo_msc_data.h> |
Harald Welte | 30f93fb | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 43 | #include <openbsc/sms_queue.h> |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 44 | #include <openbsc/vty.h> |
Pablo Neira Ayuso | 5bd96a7 | 2011-05-14 11:32:48 +0200 | [diff] [blame] | 45 | #include <openbsc/bss.h> |
Harald Welte | 28dce91 | 2011-05-24 13:25:38 +0200 | [diff] [blame] | 46 | #include <openbsc/mncc.h> |
Alexander Huemer | ee8e7ee | 2011-09-06 00:09:48 +0200 | [diff] [blame] | 47 | #include <openbsc/token_auth.h> |
| 48 | #include <openbsc/handover_decision.h> |
| 49 | #include <openbsc/rrlp.h> |
Harald Welte | 5919b6a | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 50 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 6bcd303 | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 51 | #include <osmocom/ctrl/ports.h> |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 52 | #include <osmocom/ctrl/control_vty.h> |
Harald Welte | 5919b6a | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 53 | #include <openbsc/ctrl.h> |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 54 | #include <openbsc/osmo_bsc_rf.h> |
Harald Welte | bad8ffc | 2015-01-01 12:32:03 +0100 | [diff] [blame] | 55 | #include <openbsc/smpp.h> |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 56 | |
Harald Welte | d9dea59 | 2011-03-04 13:53:51 +0100 | [diff] [blame] | 57 | #include "../../bscconfig.h" |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 58 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 59 | /* MCC and MNC for the Location Area Identifier */ |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 60 | struct gsm_network *bsc_gsmnet = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 61 | static const char *database_name = "hlr.sqlite3"; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 62 | static const char *config_file = "openbsc.cfg"; |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 63 | static const char *rf_ctrl_path = NULL; |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 64 | extern const char *openbsc_copyright; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 65 | static int daemonize = 0; |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 66 | static const char *mncc_sock_path = NULL; |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 67 | static int use_db_counter = 1; |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 68 | |
| 69 | /* timer to store statistics */ |
| 70 | #define DB_SYNC_INTERVAL 60, 0 |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 71 | #define EXPIRE_INTERVAL 10, 0 |
| 72 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 73 | static struct osmo_timer_list db_sync_timer; |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 74 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 75 | static void create_pcap_file(char *file) |
| 76 | { |
| 77 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 78 | int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode); |
| 79 | |
| 80 | if (fd < 0) { |
| 81 | perror("Failed to open file for pcap"); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | e1_set_pcap_fd(fd); |
| 86 | } |
| 87 | |
| 88 | static void print_usage() |
| 89 | { |
Harald Welte | faae5b8 | 2013-11-27 01:28:21 +0100 | [diff] [blame] | 90 | printf("Usage: osmo-nitb\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void print_help() |
| 94 | { |
| 95 | printf(" Some useful help...\n"); |
Neels Hofmeyr | e1aed09 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 96 | printf(" -h --help This text.\n"); |
| 97 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 98 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 99 | printf(" -c --config-file filename The config file to use.\n"); |
Holger Hans Peter Freyther | b913c4d | 2010-03-25 02:41:38 +0100 | [diff] [blame] | 100 | printf(" -s --disable-color\n"); |
Neels Hofmeyr | e1aed09 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 101 | printf(" -l --database db-name The database to use.\n"); |
| 102 | printf(" -a --authorize-everyone Authorize every new subscriber. Dangerous!\n"); |
| 103 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 104 | printf(" -V --version Print the version of OpenBSC.\n"); |
| 105 | printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC.\n"); |
| 106 | printf(" -e --log-level number Set a global loglevel.\n"); |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 107 | printf(" -M --mncc-sock-path PATH Disable built-in MNCC handler and offer socket.\n"); |
| 108 | printf(" -m --mncc-sock Same as `-M /tmp/bsc_mncc' (deprecated).\n"); |
Neels Hofmeyr | e1aed09 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 109 | printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n"); |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 110 | printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n"); |
Philipp | b9abf30 | 2016-10-14 18:08:00 +0200 | [diff] [blame] | 111 | printf(" -p --pcap PATH Write abis communication to pcap trace file.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 114 | static void handle_options(int argc, char **argv) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 115 | { |
| 116 | while (1) { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 117 | int option_index = 0, c; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 118 | static struct option long_options[] = { |
| 119 | {"help", 0, 0, 'h'}, |
| 120 | {"debug", 1, 0, 'd'}, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 121 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 122 | {"config-file", 1, 0, 'c'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 123 | {"disable-color", 0, 0, 's'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 124 | {"database", 1, 0, 'l'}, |
| 125 | {"authorize-everyone", 0, 0, 'a'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 126 | {"pcap", 1, 0, 'p'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 127 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 128 | {"version", 0, 0, 'V' }, |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 129 | {"rtp-proxy", 0, 0, 'P'}, |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 130 | {"log-level", 1, 0, 'e'}, |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 131 | {"mncc-sock", 0, 0, 'm'}, |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 132 | {"mncc-sock-path", 1, 0, 'M'}, |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 133 | {"no-dbcounter", 0, 0, 'C'}, |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 134 | {"rf-ctl", 1, 0, 'r'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 135 | {0, 0, 0, 0} |
| 136 | }; |
| 137 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 138 | c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:mCr:M:", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 139 | long_options, &option_index); |
| 140 | if (c == -1) |
| 141 | break; |
| 142 | |
| 143 | switch (c) { |
| 144 | case 'h': |
| 145 | print_usage(); |
| 146 | print_help(); |
| 147 | exit(0); |
| 148 | case 's': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 149 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 150 | break; |
| 151 | case 'd': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 152 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 153 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 154 | case 'D': |
| 155 | daemonize = 1; |
| 156 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 157 | case 'l': |
Daniel Willmann | c415da5 | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 158 | database_name = optarg; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 159 | break; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 160 | case 'c': |
Daniel Willmann | c415da5 | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 161 | config_file = optarg; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 162 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 163 | case 'p': |
| 164 | create_pcap_file(optarg); |
| 165 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 166 | case 'T': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 167 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 168 | break; |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 169 | case 'P': |
| 170 | ipacc_rtp_direct = 0; |
| 171 | break; |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 172 | case 'e': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 173 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 174 | break; |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 175 | case 'M': |
| 176 | mncc_sock_path = optarg; |
| 177 | break; |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 178 | case 'm': |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 179 | mncc_sock_path = "/tmp/bsc_mncc"; |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 180 | break; |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 181 | case 'C': |
| 182 | use_db_counter = 0; |
| 183 | break; |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 184 | case 'V': |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 185 | print_version(1); |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 186 | exit(0); |
| 187 | break; |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 188 | case 'r': |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 189 | rf_ctrl_path = optarg; |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 190 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 191 | default: |
Neels Hofmeyr | deb5675 | 2016-02-23 14:59:43 +0100 | [diff] [blame] | 192 | /* catch unknown options *as well as* missing arguments. */ |
| 193 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 194 | exit(-1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 200 | extern void *tall_vty_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 201 | static void signal_handler(int signal) |
| 202 | { |
| 203 | fprintf(stdout, "signal %u received\n", signal); |
| 204 | |
| 205 | switch (signal) { |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 206 | case SIGINT: |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 207 | bsc_shutdown_net(bsc_gsmnet); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 208 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 209 | sleep(3); |
| 210 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 211 | break; |
Harald Welte | 4d5e6d5 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 212 | case SIGABRT: |
Harald Welte | 8428211 | 2016-08-25 16:35:26 +0900 | [diff] [blame] | 213 | osmo_generate_backtrace(); |
Harald Welte | 4d5e6d5 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 214 | /* in case of abort, we want to obtain a talloc report |
| 215 | * and then return to the caller, who will abort the process */ |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 216 | case SIGUSR1: |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 217 | talloc_report(tall_vty_ctx, stderr); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 218 | talloc_report_full(tall_bsc_ctx, stderr); |
| 219 | break; |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 220 | case SIGUSR2: |
| 221 | talloc_report_full(tall_vty_ctx, stderr); |
| 222 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 228 | /* timer handling */ |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 229 | static int _db_store_counter(struct osmo_counter *counter, void *data) |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 230 | { |
| 231 | return db_store_counter(counter); |
| 232 | } |
| 233 | |
| 234 | static void db_sync_timer_cb(void *data) |
| 235 | { |
| 236 | /* store counters to database and re-schedule */ |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 237 | osmo_counters_for_each(_db_store_counter, NULL); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 238 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 241 | static void subscr_expire_cb(void *data) |
| 242 | { |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 243 | subscr_expire(bsc_gsmnet->subscr_group); |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 244 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 245 | } |
| 246 | |
Holger Hans Peter Freyther | 4ac8cd1 | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 247 | extern int bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 248 | |
| 249 | static struct vty_app_info vty_info = { |
| 250 | .name = "OpenBSC", |
| 251 | .version = PACKAGE_VERSION, |
| 252 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 253 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 254 | }; |
| 255 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 256 | int main(int argc, char **argv) |
| 257 | { |
| 258 | int rc; |
| 259 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 260 | vty_info.copyright = openbsc_copyright; |
| 261 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 262 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Neels Hofmeyr | a50599e | 2016-05-11 13:53:47 +0200 | [diff] [blame] | 263 | talloc_ctx_init(tall_bsc_ctx); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 264 | on_dso_load_token(); |
Harald Welte (local) | 9571378 | 2009-08-16 13:18:51 +0200 | [diff] [blame] | 265 | on_dso_load_rrlp(); |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 266 | on_dso_load_ho_dec(); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 267 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 268 | libosmo_abis_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 269 | osmo_init_logging(&log_info); |
Jacob Erlbeck | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 270 | osmo_stats_init(tall_bsc_ctx); |
Pablo Neira Ayuso | 5bd96a7 | 2011-05-14 11:32:48 +0200 | [diff] [blame] | 271 | bts_init(); |
Harald Welte | e76bea0 | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 272 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 273 | /* This needs to precede handle_options() */ |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 274 | vty_init(&vty_info); |
Neels Hofmeyr | d5cd265 | 2016-05-10 13:40:19 +0200 | [diff] [blame] | 275 | bsc_vty_init(&log_info, bsc_gsmnet); |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 276 | ctrl_vty_init(tall_bsc_ctx); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 277 | |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 278 | #ifdef BUILD_SMPP |
Neels Hofmeyr | abc6465 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 279 | if (smpp_openbsc_alloc_init(tall_bsc_ctx) < 0) |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 280 | return -1; |
| 281 | #endif |
| 282 | |
Holger Hans Peter Freyther | 4f8b9eb | 2010-05-31 21:42:53 +0800 | [diff] [blame] | 283 | /* parse options */ |
| 284 | handle_options(argc, argv); |
| 285 | |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 286 | /* internal MNCC handler or MNCC socket? */ |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 287 | if (mncc_sock_path) { |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 288 | rc = bsc_bootstrap_network(mncc_sock_from_cc, config_file); |
| 289 | if (rc >= 0) |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 290 | mncc_sock_init(bsc_gsmnet, mncc_sock_path); |
Max | 3a816e8 | 2016-04-18 23:11:18 +0200 | [diff] [blame] | 291 | } else { |
| 292 | DEBUGP(DMNCC, "Using internal MNCC handler.\n"); |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 293 | rc = bsc_bootstrap_network(int_mncc_recv, config_file); |
Max | 3a816e8 | 2016-04-18 23:11:18 +0200 | [diff] [blame] | 294 | } |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 295 | if (rc < 0) |
| 296 | exit(1); |
Harald Welte | 784be94 | 2013-03-13 15:39:56 +0100 | [diff] [blame] | 297 | #ifdef BUILD_SMPP |
Neels Hofmeyr | abc6465 | 2016-02-24 19:15:39 +0100 | [diff] [blame] | 298 | smpp_openbsc_start(bsc_gsmnet); |
Harald Welte | 784be94 | 2013-03-13 15:39:56 +0100 | [diff] [blame] | 299 | #endif |
Holger Hans Peter Freyther | c7efbe4 | 2010-06-30 14:30:35 +0800 | [diff] [blame] | 300 | bsc_api_init(bsc_gsmnet, msc_bsc_api()); |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 301 | |
Neels Hofmeyr | f8d6053 | 2016-07-23 20:15:28 +0200 | [diff] [blame] | 302 | /* |
| 303 | * For osmo-nitb, skip TCH/F for now, because otherwise dyn TS |
| 304 | * always imply the possibility to have a mix of TCH/F and |
| 305 | * TCH/H channels; if two phones request a TCH/F and a TCH/H, |
| 306 | * respectively, they cannot call each other. If we deny TCH/F, |
| 307 | * they will both fall back to TCH/H, and dynamic channels are |
| 308 | * usable. See http://osmocom.org/issues/1778. |
| 309 | * |
| 310 | * A third-party MSC may well be able to handle a TCH/H TCH/F |
| 311 | * mismatch. |
| 312 | */ |
| 313 | bsc_gsmnet->dyn_ts_allow_tch_f = false; |
| 314 | |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 315 | /* start control interface after reading config for |
| 316 | * ctrl_vty_get_bind_addr() */ |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 317 | bsc_gsmnet->ctrl = bsc_controlif_setup(bsc_gsmnet, |
| 318 | ctrl_vty_get_bind_addr(), |
| 319 | OSMO_CTRL_PORT_NITB_BSC); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 320 | if (!bsc_gsmnet->ctrl) { |
| 321 | printf("Failed to initialize control interface. Exiting.\n"); |
| 322 | return -1; |
| 323 | } |
| 324 | |
Holger Hans Peter Freyther | 1ba8fa6 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 325 | if (bsc_base_ctrl_cmds_install() != 0) { |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 326 | printf("Failed to initialize the BSC control commands.\n"); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | if (msc_ctrl_cmds_install() != 0) { |
| 331 | printf("Failed to initialize the MSC control commands.\n"); |
Holger Hans Peter Freyther | 1ba8fa6 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 335 | /* seed the PRNG */ |
| 336 | srand(time(NULL)); |
| 337 | |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 338 | bsc_gsmnet->bsc_data->rf_ctrl = osmo_bsc_rf_create(rf_ctrl_path, bsc_gsmnet); |
Ciaby | 38abd7b | 2014-03-06 17:20:55 +0100 | [diff] [blame] | 339 | if (!bsc_gsmnet->bsc_data->rf_ctrl) { |
Holger Hans Peter Freyther | bcfa0b6 | 2014-02-07 20:07:51 +0100 | [diff] [blame] | 340 | fprintf(stderr, "Failed to create the RF service.\n"); |
| 341 | exit(1); |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 344 | if (db_init(database_name)) { |
| 345 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 346 | return -1; |
| 347 | } |
| 348 | printf("DB: Database initialized.\n"); |
| 349 | |
| 350 | if (db_prepare()) { |
| 351 | printf("DB: Failed to prepare database.\n"); |
| 352 | return -1; |
| 353 | } |
| 354 | printf("DB: Database prepared.\n"); |
| 355 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 356 | /* setup the timer */ |
| 357 | db_sync_timer.cb = db_sync_timer_cb; |
| 358 | db_sync_timer.data = NULL; |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 359 | if (use_db_counter) |
| 360 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 361 | |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 362 | bsc_gsmnet->subscr_expire_timer.cb = subscr_expire_cb; |
| 363 | bsc_gsmnet->subscr_expire_timer.data = NULL; |
| 364 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 365 | |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 366 | signal(SIGINT, &signal_handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 367 | signal(SIGABRT, &signal_handler); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 368 | signal(SIGUSR1, &signal_handler); |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 369 | signal(SIGUSR2, &signal_handler); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 370 | osmo_init_ignore_signals(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 371 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 372 | /* start the SMS queue */ |
Holger Hans Peter Freyther | 4871046 | 2010-12-25 17:43:03 +0100 | [diff] [blame] | 373 | if (sms_queue_start(bsc_gsmnet, 20) != 0) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 374 | return -1; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 375 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 376 | if (daemonize) { |
| 377 | rc = osmo_daemonize(); |
| 378 | if (rc < 0) { |
| 379 | perror("Error during daemonize"); |
| 380 | exit(1); |
| 381 | } |
| 382 | } |
| 383 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 384 | while (1) { |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 385 | log_reset_context(); |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 386 | osmo_select_main(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 387 | } |
| 388 | } |