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> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <signal.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #define _GNU_SOURCE |
| 31 | #include <getopt.h> |
| 32 | |
| 33 | #include <openbsc/db.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 34 | #include <osmocore/select.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/e1_input.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 37 | #include <osmocore/talloc.h> |
Harald Welte | d21adfe | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 39 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 40 | #include <osmocom/vty/command.h> |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 41 | |
| 42 | #include "../bscconfig.h" |
| 43 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 44 | /* MCC and MNC for the Location Area Identifier */ |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 45 | static struct log_target *stderr_target; |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 46 | struct gsm_network *bsc_gsmnet = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 47 | static const char *database_name = "hlr.sqlite3"; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 48 | static const char *config_file = "openbsc.cfg"; |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 49 | extern const char *openbsc_copyright; |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 50 | |
| 51 | /* timer to store statistics */ |
| 52 | #define DB_SYNC_INTERVAL 60, 0 |
| 53 | static struct timer_list db_sync_timer; |
| 54 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 55 | extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *), |
| 56 | const char *cfg_file); |
| 57 | extern int bsc_shutdown_net(struct gsm_network *net); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 58 | |
| 59 | static void create_pcap_file(char *file) |
| 60 | { |
| 61 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 62 | int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode); |
| 63 | |
| 64 | if (fd < 0) { |
| 65 | perror("Failed to open file for pcap"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | e1_set_pcap_fd(fd); |
| 70 | } |
| 71 | |
| 72 | static void print_usage() |
| 73 | { |
| 74 | printf("Usage: bsc_hack\n"); |
| 75 | } |
| 76 | |
| 77 | static void print_help() |
| 78 | { |
| 79 | printf(" Some useful help...\n"); |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 80 | printf(" -h --help this text\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 81 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 82 | 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] | 83 | printf(" -s --disable-color\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 84 | printf(" -l --database db-name The database to use\n"); |
Holger Hans Peter Freyther | b913c4d | 2010-03-25 02:41:38 +0100 | [diff] [blame] | 85 | printf(" -a --authorize-everyone. Authorize every new subscriber. Dangerous!.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 86 | printf(" -p --pcap file The filename of the pcap file\n"); |
Harald Welte | bea12fe | 2009-08-12 21:52:11 +0200 | [diff] [blame] | 87 | 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] | 88 | printf(" -V --version. Print the version of OpenBSC.\n"); |
Harald Welte | dae9a01 | 2009-12-18 15:05:02 +0100 | [diff] [blame] | 89 | 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] | 90 | printf(" -e --log-level number. Set a global loglevel.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void handle_options(int argc, char** argv) |
| 94 | { |
| 95 | while (1) { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 96 | int option_index = 0, c; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 97 | static struct option long_options[] = { |
| 98 | {"help", 0, 0, 'h'}, |
| 99 | {"debug", 1, 0, 'd'}, |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 100 | {"config-file", 1, 0, 'c'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 101 | {"disable-color", 0, 0, 's'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 102 | {"database", 1, 0, 'l'}, |
| 103 | {"authorize-everyone", 0, 0, 'a'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 104 | {"pcap", 1, 0, 'p'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 105 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 106 | {"version", 0, 0, 'V' }, |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 107 | {"rtp-proxy", 0, 0, 'P'}, |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 108 | {"log-level", 1, 0, 'e'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 109 | {0, 0, 0, 0} |
| 110 | }; |
| 111 | |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 112 | c = getopt_long(argc, argv, "hd:sl:ar:p:TPVc:e:", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 113 | long_options, &option_index); |
| 114 | if (c == -1) |
| 115 | break; |
| 116 | |
| 117 | switch (c) { |
| 118 | case 'h': |
| 119 | print_usage(); |
| 120 | print_help(); |
| 121 | exit(0); |
| 122 | case 's': |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 123 | log_set_use_color(stderr_target, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 124 | break; |
| 125 | case 'd': |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 126 | log_parse_category_mask(stderr_target, optarg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 127 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 128 | case 'l': |
| 129 | database_name = strdup(optarg); |
| 130 | break; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 131 | case 'c': |
| 132 | config_file = strdup(optarg); |
| 133 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 134 | case 'p': |
| 135 | create_pcap_file(optarg); |
| 136 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 137 | case 'T': |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 138 | log_set_print_timestamp(stderr_target, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 139 | break; |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 140 | case 'P': |
| 141 | ipacc_rtp_direct = 0; |
| 142 | break; |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 143 | case 'e': |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 144 | log_set_log_level(stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 38e15c0 | 2010-03-25 02:39:15 +0100 | [diff] [blame] | 145 | break; |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 146 | case 'V': |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 147 | print_version(1); |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 148 | exit(0); |
| 149 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 150 | default: |
| 151 | /* ignore */ |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 157 | extern void *tall_vty_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 158 | static void signal_handler(int signal) |
| 159 | { |
| 160 | fprintf(stdout, "signal %u received\n", signal); |
| 161 | |
| 162 | switch (signal) { |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 163 | case SIGINT: |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 164 | bsc_shutdown_net(bsc_gsmnet); |
Harald Welte | d21adfe | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 165 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 166 | sleep(3); |
| 167 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 168 | break; |
Harald Welte | 4d5e6d5 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 169 | case SIGABRT: |
| 170 | /* in case of abort, we want to obtain a talloc report |
| 171 | * and then return to the caller, who will abort the process */ |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 172 | case SIGUSR1: |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 173 | talloc_report(tall_vty_ctx, stderr); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 174 | talloc_report_full(tall_bsc_ctx, stderr); |
| 175 | break; |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 176 | case SIGUSR2: |
| 177 | talloc_report_full(tall_vty_ctx, stderr); |
| 178 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 184 | /* timer handling */ |
| 185 | static int _db_store_counter(struct counter *counter, void *data) |
| 186 | { |
| 187 | return db_store_counter(counter); |
| 188 | } |
| 189 | |
| 190 | static void db_sync_timer_cb(void *data) |
| 191 | { |
| 192 | /* store counters to database and re-schedule */ |
| 193 | counters_for_each(_db_store_counter, NULL); |
| 194 | bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); |
| 195 | } |
| 196 | |
Daniel Willmann | 2ac4edf | 2010-01-11 13:50:44 +0100 | [diff] [blame] | 197 | extern int bts_model_unknown_init(void); |
Harald Welte | 59698fb | 2010-01-10 18:01:52 +0100 | [diff] [blame] | 198 | extern int bts_model_bs11_init(void); |
| 199 | extern int bts_model_nanobts_init(void); |
| 200 | |
Holger Hans Peter Freyther | 3538c77 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 201 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 202 | |
| 203 | static struct vty_app_info vty_info = { |
| 204 | .name = "OpenBSC", |
| 205 | .version = PACKAGE_VERSION, |
| 206 | .go_parent_cb = bsc_vty_go_parent, |
| 207 | }; |
| 208 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 209 | int main(int argc, char **argv) |
| 210 | { |
| 211 | int rc; |
| 212 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 213 | vty_info.copyright = openbsc_copyright; |
| 214 | |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 215 | log_init(&log_info); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 216 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 217 | talloc_ctx_init(); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 218 | on_dso_load_token(); |
Harald Welte (local) | 9571378 | 2009-08-16 13:18:51 +0200 | [diff] [blame] | 219 | on_dso_load_rrlp(); |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 220 | on_dso_load_ho_dec(); |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 221 | stderr_target = log_target_create_stderr(); |
| 222 | log_add_target(stderr_target); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 223 | |
Daniel Willmann | 2ac4edf | 2010-01-11 13:50:44 +0100 | [diff] [blame] | 224 | bts_model_unknown_init(); |
Harald Welte | 59698fb | 2010-01-10 18:01:52 +0100 | [diff] [blame] | 225 | bts_model_bs11_init(); |
| 226 | bts_model_nanobts_init(); |
| 227 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 228 | /* enable filters */ |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 229 | log_set_all_filter(stderr_target, 1); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 230 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 231 | /* This needs to precede handle_options() */ |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 232 | vty_init(&vty_info); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 233 | bsc_vty_init(); |
| 234 | |
Holger Hans Peter Freyther | 4f8b9eb | 2010-05-31 21:42:53 +0800 | [diff] [blame] | 235 | /* parse options */ |
| 236 | handle_options(argc, argv); |
| 237 | |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 238 | rc = bsc_bootstrap_network(mncc_recv, config_file); |
| 239 | if (rc < 0) |
| 240 | exit(1); |
| 241 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 242 | /* seed the PRNG */ |
| 243 | srand(time(NULL)); |
| 244 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 245 | if (db_init(database_name)) { |
| 246 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | printf("DB: Database initialized.\n"); |
| 250 | |
| 251 | if (db_prepare()) { |
| 252 | printf("DB: Failed to prepare database.\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | printf("DB: Database prepared.\n"); |
| 256 | |
Holger Hans Peter Freyther | 76037f0 | 2009-12-23 05:29:04 +0100 | [diff] [blame] | 257 | /* setup the timer */ |
| 258 | db_sync_timer.cb = db_sync_timer_cb; |
| 259 | db_sync_timer.data = NULL; |
| 260 | bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); |
| 261 | |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 262 | signal(SIGINT, &signal_handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 263 | signal(SIGABRT, &signal_handler); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 264 | signal(SIGUSR1, &signal_handler); |
Harald Welte | 0c83660 | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 265 | signal(SIGUSR2, &signal_handler); |
Holger Hans Peter Freyther | 49b05d3 | 2009-11-24 20:06:41 +0100 | [diff] [blame] | 266 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 267 | |
| 268 | while (1) { |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 269 | bsc_upqueue(bsc_gsmnet); |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 270 | log_reset_context(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 271 | bsc_select_main(0); |
| 272 | } |
| 273 | } |