Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | /* A hackish minimal BSC (+MSC +HLR) implementation */ |
| 2 | |
Harald Welte | 0677061 | 2010-01-12 10:46:27 +0100 | [diff] [blame] | 3 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 4 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 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 | 52b1f98 | 2008-12-23 20:25:15 +0000 | [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 | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 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 | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 23 | #include <time.h> |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 25 | #include <signal.h> |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <sys/stat.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 28 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 29 | #define _GNU_SOURCE |
| 30 | #include <getopt.h> |
| 31 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 32 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 33 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/select.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 35 | #include <osmocom/core/stats.h> |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | ed5cacb | 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 | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/talloc.h> |
Harald Welte | 3961fcc | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 40 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | ec4bfdc | 2010-06-30 14:30:35 +0800 | [diff] [blame] | 41 | #include <openbsc/osmo_msc.h> |
Ciaby | ec6e4f8 | 2014-03-06 17:20:55 +0100 | [diff] [blame] | 42 | #include <openbsc/osmo_msc_data.h> |
Harald Welte | 4d54d0b | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 43 | #include <openbsc/sms_queue.h> |
Holger Hans Peter Freyther | 7a2c86b | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 44 | #include <openbsc/vty.h> |
Pablo Neira Ayuso | 0d2881a | 2011-05-14 11:32:48 +0200 | [diff] [blame] | 45 | #include <openbsc/bss.h> |
Harald Welte | f142c97 | 2011-05-24 13:25:38 +0200 | [diff] [blame] | 46 | #include <openbsc/mncc.h> |
Alexander Huemer | 59947a0 | 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 | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 50 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 51 | #include <osmocom/ctrl/ports.h> |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 52 | #include <openbsc/ctrl.h> |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 53 | #include <openbsc/osmo_bsc_rf.h> |
Harald Welte | 7b1d25a | 2015-01-01 12:32:03 +0100 | [diff] [blame] | 54 | #include <openbsc/smpp.h> |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 55 | |
Harald Welte | c08e8be | 2011-03-04 13:53:51 +0100 | [diff] [blame] | 56 | #include "../../bscconfig.h" |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 57 | |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 58 | /* MCC and MNC for the Location Area Identifier */ |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 59 | struct gsm_network *bsc_gsmnet = 0; |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 60 | static const char *database_name = "hlr.sqlite3"; |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 61 | static const char *config_file = "openbsc.cfg"; |
Neels Hofmeyr | f4a3123 | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 62 | static const char *rf_ctrl_path = NULL; |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 63 | extern const char *openbsc_copyright; |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 64 | static int daemonize = 0; |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 65 | static const char *mncc_sock_path = NULL; |
Harald Welte | f7e9410 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 66 | static int use_db_counter = 1; |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 67 | |
| 68 | /* timer to store statistics */ |
| 69 | #define DB_SYNC_INTERVAL 60, 0 |
Jan Luebbe | 1c201c6 | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 70 | #define EXPIRE_INTERVAL 10, 0 |
| 71 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 72 | static struct osmo_timer_list db_sync_timer; |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 73 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 74 | static 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 Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 84 | e1_set_pcap_fd(fd); |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 87 | static void print_usage() |
| 88 | { |
Harald Welte | 19d8742 | 2013-11-27 01:28:21 +0100 | [diff] [blame] | 89 | printf("Usage: osmo-nitb\n"); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void print_help() |
| 93 | { |
| 94 | printf(" Some useful help...\n"); |
Neels Hofmeyr | 354d1f3 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 95 | 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 Freyther | 009ad61 | 2010-03-25 02:41:38 +0100 | [diff] [blame] | 99 | printf(" -s --disable-color\n"); |
Neels Hofmeyr | 354d1f3 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 100 | 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 Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 106 | 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 Hofmeyr | 354d1f3 | 2015-09-15 11:50:50 +0200 | [diff] [blame] | 108 | printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n"); |
Neels Hofmeyr | f4a3123 | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 109 | printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n"); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 112 | static void handle_options(int argc, char **argv) |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 113 | { |
| 114 | while (1) { |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 115 | int option_index = 0, c; |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 116 | static struct option long_options[] = { |
| 117 | {"help", 0, 0, 'h'}, |
| 118 | {"debug", 1, 0, 'd'}, |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 119 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 120 | {"config-file", 1, 0, 'c'}, |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 121 | {"disable-color", 0, 0, 's'}, |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 122 | {"database", 1, 0, 'l'}, |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 123 | {"authorize-everyone", 0, 0, 'a'}, |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 124 | {"pcap", 1, 0, 'p'}, |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 125 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 126 | {"version", 0, 0, 'V' }, |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 127 | {"rtp-proxy", 0, 0, 'P'}, |
Holger Hans Peter Freyther | 829772d | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 128 | {"log-level", 1, 0, 'e'}, |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 129 | {"mncc-sock", 0, 0, 'm'}, |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 130 | {"mncc-sock-path", 1, 0, 'M'}, |
Harald Welte | f7e9410 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 131 | {"no-dbcounter", 0, 0, 'C'}, |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 132 | {"rf-ctl", 1, 0, 'r'}, |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 133 | {0, 0, 0, 0} |
| 134 | }; |
| 135 | |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 136 | c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:mCr:M:", |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 137 | long_options, &option_index); |
| 138 | if (c == -1) |
| 139 | break; |
| 140 | |
| 141 | switch (c) { |
| 142 | case 'h': |
| 143 | print_usage(); |
| 144 | print_help(); |
| 145 | exit(0); |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 146 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 147 | log_set_use_color(osmo_stderr_target, 0); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 148 | break; |
| 149 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 150 | log_parse_category_mask(osmo_stderr_target, optarg); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 151 | break; |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 152 | case 'D': |
| 153 | daemonize = 1; |
| 154 | break; |
Harald Welte | 8965da4 | 2009-01-06 18:09:02 +0000 | [diff] [blame] | 155 | case 'l': |
Daniel Willmann | 038f97a | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 156 | database_name = optarg; |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 157 | break; |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 158 | case 'c': |
Daniel Willmann | 038f97a | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 159 | config_file = optarg; |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 160 | break; |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 161 | case 'p': |
| 162 | create_pcap_file(optarg); |
| 163 | break; |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 164 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 165 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 166 | break; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 167 | case 'P': |
| 168 | ipacc_rtp_direct = 0; |
| 169 | break; |
Holger Hans Peter Freyther | 829772d | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 170 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 171 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 829772d | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 172 | break; |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 173 | case 'M': |
| 174 | mncc_sock_path = optarg; |
| 175 | break; |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 176 | case 'm': |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 177 | mncc_sock_path = "/tmp/bsc_mncc"; |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 178 | break; |
Harald Welte | f7e9410 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 179 | case 'C': |
| 180 | use_db_counter = 0; |
| 181 | break; |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 182 | case 'V': |
Harald Welte | 1353f96 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 183 | print_version(1); |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 184 | exit(0); |
| 185 | break; |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 186 | case 'r': |
Neels Hofmeyr | f4a3123 | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 187 | rf_ctrl_path = optarg; |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 188 | break; |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 189 | default: |
Neels Hofmeyr | fc7add2 | 2016-02-23 14:59:43 +0100 | [diff] [blame^] | 190 | /* catch unknown options *as well as* missing arguments. */ |
| 191 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 192 | exit(-1); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 198 | extern void *tall_vty_ctx; |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 199 | static void signal_handler(int signal) |
| 200 | { |
| 201 | fprintf(stdout, "signal %u received\n", signal); |
| 202 | |
| 203 | switch (signal) { |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 204 | case SIGINT: |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 205 | bsc_shutdown_net(bsc_gsmnet); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 206 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 207 | sleep(3); |
| 208 | exit(0); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 209 | break; |
Harald Welte | 31c3d34 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 210 | case SIGABRT: |
| 211 | /* in case of abort, we want to obtain a talloc report |
| 212 | * and then return to the caller, who will abort the process */ |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 213 | case SIGUSR1: |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 214 | talloc_report(tall_vty_ctx, stderr); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 215 | talloc_report_full(tall_bsc_ctx, stderr); |
| 216 | break; |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 217 | case SIGUSR2: |
| 218 | talloc_report_full(tall_vty_ctx, stderr); |
| 219 | break; |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 225 | /* timer handling */ |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 226 | static int _db_store_counter(struct osmo_counter *counter, void *data) |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 227 | { |
| 228 | return db_store_counter(counter); |
| 229 | } |
| 230 | |
| 231 | static void db_sync_timer_cb(void *data) |
| 232 | { |
| 233 | /* store counters to database and re-schedule */ |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 234 | osmo_counters_for_each(_db_store_counter, NULL); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 235 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Jan Luebbe | 1c201c6 | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 238 | static void subscr_expire_cb(void *data) |
| 239 | { |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 240 | subscr_expire(bsc_gsmnet->subscr_group); |
Jan Luebbe | 1c201c6 | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 241 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 242 | } |
| 243 | |
Harald Welte | 4d54d0b | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 244 | void talloc_ctx_init(void); |
Harald Welte | 39315c4 | 2010-01-10 18:01:52 +0100 | [diff] [blame] | 245 | |
Holger Hans Peter Freyther | 3e4e8cf | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 246 | extern int bsc_vty_go_parent(struct vty *vty); |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 247 | |
| 248 | static struct vty_app_info vty_info = { |
| 249 | .name = "OpenBSC", |
| 250 | .version = PACKAGE_VERSION, |
| 251 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 7a2c86b | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 252 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 253 | }; |
| 254 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 255 | int main(int argc, char **argv) |
| 256 | { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 257 | int rc; |
| 258 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 259 | vty_info.copyright = openbsc_copyright; |
| 260 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 261 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 262 | talloc_ctx_init(); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 263 | on_dso_load_token(); |
Harald Welte (local) | b8afe81 | 2009-08-16 13:18:51 +0200 | [diff] [blame] | 264 | on_dso_load_rrlp(); |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 265 | on_dso_load_ho_dec(); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 266 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 267 | libosmo_abis_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 268 | osmo_init_logging(&log_info); |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 269 | osmo_stats_init(tall_bsc_ctx); |
Pablo Neira Ayuso | 0d2881a | 2011-05-14 11:32:48 +0200 | [diff] [blame] | 270 | bts_init(); |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 271 | |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 272 | /* This needs to precede handle_options() */ |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 273 | vty_init(&vty_info); |
Pablo Neira Ayuso | 739a566 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 274 | bsc_vty_init(&log_info); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 275 | |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 276 | #ifdef BUILD_SMPP |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 277 | if (smpp_openbsc_init(tall_bsc_ctx, 0) < 0) |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 278 | return -1; |
| 279 | #endif |
| 280 | |
Holger Hans Peter Freyther | 0c8aa73 | 2010-05-31 21:42:53 +0800 | [diff] [blame] | 281 | /* parse options */ |
| 282 | handle_options(argc, argv); |
| 283 | |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 284 | /* internal MNCC handler or MNCC socket? */ |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 285 | if (mncc_sock_path) { |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 286 | rc = bsc_bootstrap_network(mncc_sock_from_cc, config_file); |
| 287 | if (rc >= 0) |
Neels Hofmeyr | 0ade632 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 288 | mncc_sock_init(bsc_gsmnet, mncc_sock_path); |
Harald Welte | 02d9966 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 289 | } else |
| 290 | rc = bsc_bootstrap_network(int_mncc_recv, config_file); |
Harald Welte | 1353f96 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 291 | if (rc < 0) |
| 292 | exit(1); |
Harald Welte | 7159e8b | 2013-03-13 15:39:56 +0100 | [diff] [blame] | 293 | #ifdef BUILD_SMPP |
Harald Welte | 76afa16 | 2013-03-13 15:05:26 +0100 | [diff] [blame] | 294 | smpp_openbsc_set_net(bsc_gsmnet); |
Harald Welte | 7159e8b | 2013-03-13 15:39:56 +0100 | [diff] [blame] | 295 | #endif |
Holger Hans Peter Freyther | ec4bfdc | 2010-06-30 14:30:35 +0800 | [diff] [blame] | 296 | bsc_api_init(bsc_gsmnet, msc_bsc_api()); |
Harald Welte | 1353f96 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 297 | |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 298 | bsc_gsmnet->ctrl = bsc_controlif_setup(bsc_gsmnet, OSMO_CTRL_PORT_NITB_BSC); |
Daniel Willmann | 5e95f45 | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 299 | if (!bsc_gsmnet->ctrl) { |
| 300 | printf("Failed to initialize control interface. Exiting.\n"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
Holger Hans Peter Freyther | f8c4219 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 304 | if (bsc_base_ctrl_cmds_install() != 0) { |
Holger Hans Peter Freyther | 9dbc3f8 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 305 | printf("Failed to initialize the BSC control commands.\n"); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | if (msc_ctrl_cmds_install() != 0) { |
| 310 | printf("Failed to initialize the MSC control commands.\n"); |
Holger Hans Peter Freyther | f8c4219 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 311 | return -1; |
| 312 | } |
| 313 | |
Harald Welte | 65ccf88 | 2009-02-24 22:36:20 +0000 | [diff] [blame] | 314 | /* seed the PRNG */ |
| 315 | srand(time(NULL)); |
| 316 | |
Neels Hofmeyr | f4a3123 | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 317 | bsc_gsmnet->bsc_data->rf_ctrl = osmo_bsc_rf_create(rf_ctrl_path, bsc_gsmnet); |
Ciaby | ec6e4f8 | 2014-03-06 17:20:55 +0100 | [diff] [blame] | 318 | if (!bsc_gsmnet->bsc_data->rf_ctrl) { |
Holger Hans Peter Freyther | 362d10f | 2014-02-07 20:07:51 +0100 | [diff] [blame] | 319 | fprintf(stderr, "Failed to create the RF service.\n"); |
| 320 | exit(1); |
Holger Hans Peter Freyther | 2c9ea91 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 323 | if (db_init(database_name)) { |
| 324 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 325 | return -1; |
| 326 | } |
| 327 | printf("DB: Database initialized.\n"); |
| 328 | |
| 329 | if (db_prepare()) { |
| 330 | printf("DB: Failed to prepare database.\n"); |
| 331 | return -1; |
| 332 | } |
| 333 | printf("DB: Database prepared.\n"); |
| 334 | |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 335 | /* setup the timer */ |
| 336 | db_sync_timer.cb = db_sync_timer_cb; |
| 337 | db_sync_timer.data = NULL; |
Harald Welte | f7e9410 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 338 | if (use_db_counter) |
| 339 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 079353a | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 340 | |
Jan Luebbe | 1c201c6 | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 341 | bsc_gsmnet->subscr_expire_timer.cb = subscr_expire_cb; |
| 342 | bsc_gsmnet->subscr_expire_timer.data = NULL; |
| 343 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 344 | |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 345 | signal(SIGINT, &signal_handler); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 346 | signal(SIGABRT, &signal_handler); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 347 | signal(SIGUSR1, &signal_handler); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 348 | signal(SIGUSR2, &signal_handler); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 349 | osmo_init_ignore_signals(); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 350 | |
Holger Hans Peter Freyther | 11b28f9 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 351 | /* start the SMS queue */ |
Holger Hans Peter Freyther | a37e3bc | 2010-12-25 17:43:03 +0100 | [diff] [blame] | 352 | if (sms_queue_start(bsc_gsmnet, 20) != 0) |
Holger Hans Peter Freyther | 11b28f9 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 353 | return -1; |
Harald Welte | 338e3b3 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 354 | |
Harald Welte | 2c869ef | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 355 | if (daemonize) { |
| 356 | rc = osmo_daemonize(); |
| 357 | if (rc < 0) { |
| 358 | perror("Error during daemonize"); |
| 359 | exit(1); |
| 360 | } |
| 361 | } |
| 362 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 363 | while (1) { |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 364 | log_reset_context(); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 365 | osmo_select_main(0); |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 366 | } |
| 367 | } |