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"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 113 | static void handle_options(int argc, char **argv) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 114 | { |
| 115 | while (1) { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 116 | int option_index = 0, c; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 117 | static struct option long_options[] = { |
| 118 | {"help", 0, 0, 'h'}, |
| 119 | {"debug", 1, 0, 'd'}, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 120 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 121 | {"config-file", 1, 0, 'c'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 122 | {"disable-color", 0, 0, 's'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 123 | {"database", 1, 0, 'l'}, |
| 124 | {"authorize-everyone", 0, 0, 'a'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 125 | {"pcap", 1, 0, 'p'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 126 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 127 | {"version", 0, 0, 'V' }, |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 128 | {"rtp-proxy", 0, 0, 'P'}, |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 129 | {"log-level", 1, 0, 'e'}, |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 130 | {"mncc-sock", 0, 0, 'm'}, |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 131 | {"mncc-sock-path", 1, 0, 'M'}, |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 132 | {"no-dbcounter", 0, 0, 'C'}, |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 133 | {"rf-ctl", 1, 0, 'r'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 134 | {0, 0, 0, 0} |
| 135 | }; |
| 136 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 137 | 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] | 138 | 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); |
| 147 | case 's': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 148 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 149 | break; |
| 150 | case 'd': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 151 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 152 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 153 | case 'D': |
| 154 | daemonize = 1; |
| 155 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 156 | case 'l': |
Daniel Willmann | c415da5 | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 157 | database_name = optarg; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 158 | break; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 159 | case 'c': |
Daniel Willmann | c415da5 | 2014-05-21 15:46:44 +0200 | [diff] [blame] | 160 | config_file = optarg; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 161 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 162 | case 'p': |
| 163 | create_pcap_file(optarg); |
| 164 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 165 | case 'T': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 166 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 167 | break; |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 168 | case 'P': |
| 169 | ipacc_rtp_direct = 0; |
| 170 | break; |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 171 | case 'e': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 172 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 173 | break; |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 174 | case 'M': |
| 175 | mncc_sock_path = optarg; |
| 176 | break; |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 177 | case 'm': |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 178 | mncc_sock_path = "/tmp/bsc_mncc"; |
Harald Welte | e532fb7 | 2010-12-23 01:31:07 +0100 | [diff] [blame] | 179 | break; |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 180 | case 'C': |
| 181 | use_db_counter = 0; |
| 182 | break; |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 183 | case 'V': |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 184 | print_version(1); |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 185 | exit(0); |
| 186 | break; |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 187 | case 'r': |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 188 | rf_ctrl_path = optarg; |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 189 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 190 | default: |
Neels Hofmeyr | deb5675 | 2016-02-23 14:59:43 +0100 | [diff] [blame] | 191 | /* catch unknown options *as well as* missing arguments. */ |
| 192 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 193 | exit(-1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 199 | extern void *tall_vty_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 200 | static void signal_handler(int signal) |
| 201 | { |
| 202 | fprintf(stdout, "signal %u received\n", signal); |
| 203 | |
| 204 | switch (signal) { |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 205 | case SIGINT: |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 206 | bsc_shutdown_net(bsc_gsmnet); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 207 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 208 | sleep(3); |
| 209 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 210 | break; |
Harald Welte | 4d5e6d5 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 211 | case SIGABRT: |
| 212 | /* in case of abort, we want to obtain a talloc report |
| 213 | * and then return to the caller, who will abort the process */ |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 214 | case SIGUSR1: |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 215 | talloc_report(tall_vty_ctx, stderr); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 216 | talloc_report_full(tall_bsc_ctx, stderr); |
| 217 | break; |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 218 | case SIGUSR2: |
| 219 | talloc_report_full(tall_vty_ctx, stderr); |
| 220 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 226 | /* timer handling */ |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 227 | 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] | 228 | { |
| 229 | return db_store_counter(counter); |
| 230 | } |
| 231 | |
| 232 | static void db_sync_timer_cb(void *data) |
| 233 | { |
| 234 | /* store counters to database and re-schedule */ |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 235 | osmo_counters_for_each(_db_store_counter, NULL); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 236 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 239 | static void subscr_expire_cb(void *data) |
| 240 | { |
Jacob Erlbeck | da8770b | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 241 | subscr_expire(bsc_gsmnet->subscr_group); |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 242 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 243 | } |
| 244 | |
Harald Welte | 30f93fb | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 245 | void talloc_ctx_init(void); |
Harald Welte | 59698fb | 2010-01-10 18:01:52 +0100 | [diff] [blame] | 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"); |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 263 | talloc_ctx_init(); |
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); |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 275 | bsc_vty_init(&log_info); |
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() */ |
| 317 | LOGP(DNM, LOGL_NOTICE, "CTRL at %s %d\n", |
| 318 | ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_NITB_BSC); |
| 319 | bsc_gsmnet->ctrl = bsc_controlif_setup(bsc_gsmnet, |
| 320 | ctrl_vty_get_bind_addr(), |
| 321 | OSMO_CTRL_PORT_NITB_BSC); |
Daniel Willmann | 6eb756f | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 322 | if (!bsc_gsmnet->ctrl) { |
| 323 | printf("Failed to initialize control interface. Exiting.\n"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
Holger Hans Peter Freyther | 1ba8fa6 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 327 | if (bsc_base_ctrl_cmds_install() != 0) { |
Holger Hans Peter Freyther | fba0316 | 2014-03-23 12:06:36 +0100 | [diff] [blame] | 328 | printf("Failed to initialize the BSC control commands.\n"); |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | if (msc_ctrl_cmds_install() != 0) { |
| 333 | printf("Failed to initialize the MSC control commands.\n"); |
Holger Hans Peter Freyther | 1ba8fa6 | 2013-01-09 17:03:27 +0100 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 337 | /* seed the PRNG */ |
| 338 | srand(time(NULL)); |
| 339 | |
Neels Hofmeyr | f428d9a | 2016-02-23 14:57:22 +0100 | [diff] [blame] | 340 | 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] | 341 | if (!bsc_gsmnet->bsc_data->rf_ctrl) { |
Holger Hans Peter Freyther | bcfa0b6 | 2014-02-07 20:07:51 +0100 | [diff] [blame] | 342 | fprintf(stderr, "Failed to create the RF service.\n"); |
| 343 | exit(1); |
Holger Hans Peter Freyther | 5faf1d3 | 2012-05-11 23:28:19 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 346 | if (db_init(database_name)) { |
| 347 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 348 | return -1; |
| 349 | } |
| 350 | printf("DB: Database initialized.\n"); |
| 351 | |
| 352 | if (db_prepare()) { |
| 353 | printf("DB: Failed to prepare database.\n"); |
| 354 | return -1; |
| 355 | } |
| 356 | printf("DB: Database prepared.\n"); |
| 357 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 358 | /* setup the timer */ |
| 359 | db_sync_timer.cb = db_sync_timer_cb; |
| 360 | db_sync_timer.data = NULL; |
Harald Welte | cfa2685 | 2011-11-13 19:02:06 +0100 | [diff] [blame] | 361 | if (use_db_counter) |
| 362 | osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL); |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 363 | |
Jan Luebbe | 0f1f95e | 2012-12-27 00:40:32 +0100 | [diff] [blame] | 364 | bsc_gsmnet->subscr_expire_timer.cb = subscr_expire_cb; |
| 365 | bsc_gsmnet->subscr_expire_timer.data = NULL; |
| 366 | osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL); |
| 367 | |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 368 | signal(SIGINT, &signal_handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 369 | signal(SIGABRT, &signal_handler); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 370 | signal(SIGUSR1, &signal_handler); |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 371 | signal(SIGUSR2, &signal_handler); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 372 | osmo_init_ignore_signals(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 373 | |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 374 | /* start the SMS queue */ |
Holger Hans Peter Freyther | 4871046 | 2010-12-25 17:43:03 +0100 | [diff] [blame] | 375 | if (sms_queue_start(bsc_gsmnet, 20) != 0) |
Holger Hans Peter Freyther | 575d2e1 | 2010-12-24 13:48:27 +0100 | [diff] [blame] | 376 | return -1; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 377 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 378 | if (daemonize) { |
| 379 | rc = osmo_daemonize(); |
| 380 | if (rc < 0) { |
| 381 | perror("Error during daemonize"); |
| 382 | exit(1); |
| 383 | } |
| 384 | } |
| 385 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 386 | while (1) { |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 387 | log_reset_context(); |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 388 | osmo_select_main(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 389 | } |
| 390 | } |