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