Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | /* A hackish minimal BSC (+MSC +HLR) implementation */ |
| 2 | |
Harald Welte | 32201c1 | 2009-03-10 12:15:10 +0000 | [diff] [blame] | 3 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 4 | * (C) 2009 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 |
| 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 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 26 | #include <signal.h> |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <sys/stat.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 29 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 30 | #define _GNU_SOURCE |
| 31 | #include <getopt.h> |
| 32 | |
Harald Welte | 255539c | 2008-12-28 02:26:27 +0000 | [diff] [blame] | 33 | #include <openbsc/db.h> |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 34 | #include <openbsc/select.h> |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 36 | #include <openbsc/e1_input.h> |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 37 | #include <openbsc/talloc.h> |
| 38 | |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 39 | /* MCC and MNC for the Location Area Identifier */ |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 40 | struct gsm_network *bsc_gsmnet = 0; |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 41 | static const char *database_name = "hlr.sqlite3"; |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 42 | static const char *config_file = "openbsc.cfg"; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 43 | extern int ipacc_rtp_direct; |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 45 | extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *), |
| 46 | const char *cfg_file); |
| 47 | extern int bsc_shutdown_net(struct gsm_network *net); |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 48 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 49 | static void create_pcap_file(char *file) |
| 50 | { |
| 51 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 52 | int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode); |
| 53 | |
| 54 | if (fd < 0) { |
| 55 | perror("Failed to open file for pcap"); |
| 56 | return; |
| 57 | } |
| 58 | |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 59 | e1_set_pcap_fd(fd); |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 62 | static void print_usage() |
| 63 | { |
| 64 | printf("Usage: bsc_hack\n"); |
| 65 | } |
| 66 | |
| 67 | static void print_help() |
| 68 | { |
| 69 | printf(" Some useful help...\n"); |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 70 | printf(" -h --help this text\n"); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 71 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 72 | printf(" -s --disable-color\n"); |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 73 | printf(" -c --config-file filename The config file to use.\n"); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 74 | printf(" -l --database db-name The database to use\n"); |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 75 | printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n"); |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 76 | printf(" -p --pcap file The filename of the pcap file\n"); |
Harald Welte | c70979a | 2009-08-12 21:52:11 +0200 | [diff] [blame] | 77 | printf(" -T --timestamp Prefix every log line with a timestamp\n"); |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void handle_options(int argc, char** argv) |
| 81 | { |
| 82 | while (1) { |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 83 | int option_index = 0, c; |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 84 | static struct option long_options[] = { |
| 85 | {"help", 0, 0, 'h'}, |
| 86 | {"debug", 1, 0, 'd'}, |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 87 | {"config-file", 1, 0, 'c'}, |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 88 | {"disable-color", 0, 0, 's'}, |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 89 | {"database", 1, 0, 'l'}, |
Holger Freyther | 89824fc | 2008-12-30 16:18:18 +0000 | [diff] [blame] | 90 | {"authorize-everyone", 0, 0, 'a'}, |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 91 | {"reject-cause", 1, 0, 'r'}, |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 92 | {"pcap", 1, 0, 'p'}, |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 93 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 94 | {"rtp-proxy", 0, 0, 'P'}, |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 95 | {0, 0, 0, 0} |
| 96 | }; |
| 97 | |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 98 | c = getopt_long(argc, argv, "hd:sl:ar:p:TPc:", |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 99 | long_options, &option_index); |
| 100 | if (c == -1) |
| 101 | break; |
| 102 | |
| 103 | switch (c) { |
| 104 | case 'h': |
| 105 | print_usage(); |
| 106 | print_help(); |
| 107 | exit(0); |
Holger Freyther | efde7fb | 2008-12-28 14:14:56 +0000 | [diff] [blame] | 108 | case 's': |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 109 | debug_use_color(0); |
| 110 | break; |
| 111 | case 'd': |
| 112 | debug_parse_category_mask(optarg); |
| 113 | break; |
Harald Welte | 8965da4 | 2009-01-06 18:09:02 +0000 | [diff] [blame] | 114 | case 'l': |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 115 | database_name = strdup(optarg); |
| 116 | break; |
Holger Hans Peter Freyther | d5d1cef | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 117 | case 'c': |
| 118 | config_file = strdup(optarg); |
| 119 | break; |
Holger Freyther | e97f7fb | 2008-12-31 18:52:11 +0000 | [diff] [blame] | 120 | case 'r': |
| 121 | gsm0408_set_reject_cause(atoi(optarg)); |
| 122 | break; |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 123 | case 'p': |
| 124 | create_pcap_file(optarg); |
| 125 | break; |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 126 | case 'T': |
| 127 | debug_timestamp(1); |
| 128 | break; |
Harald Welte | 805f644 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 129 | case 'P': |
| 130 | ipacc_rtp_direct = 0; |
| 131 | break; |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 132 | default: |
| 133 | /* ignore */ |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 139 | static void signal_handler(int signal) |
| 140 | { |
| 141 | fprintf(stdout, "signal %u received\n", signal); |
| 142 | |
| 143 | switch (signal) { |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 144 | case SIGINT: |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 145 | bsc_shutdown_net(bsc_gsmnet); |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 146 | sleep(3); |
| 147 | exit(0); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 148 | break; |
Harald Welte | 31c3d34 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 149 | case SIGABRT: |
| 150 | /* in case of abort, we want to obtain a talloc report |
| 151 | * and then return to the caller, who will abort the process */ |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 152 | case SIGUSR1: |
| 153 | talloc_report_full(tall_bsc_ctx, stderr); |
| 154 | break; |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 155 | default: |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 160 | int main(int argc, char **argv) |
| 161 | { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 162 | int rc; |
| 163 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 164 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 165 | talloc_ctx_init(); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 166 | on_dso_load_token(); |
Harald Welte (local) | b8afe81 | 2009-08-16 13:18:51 +0200 | [diff] [blame] | 167 | on_dso_load_rrlp(); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 168 | |
Holger Freyther | b332f61 | 2008-12-27 12:46:51 +0000 | [diff] [blame] | 169 | /* parse options */ |
| 170 | handle_options(argc, argv); |
| 171 | |
Harald Welte | 65ccf88 | 2009-02-24 22:36:20 +0000 | [diff] [blame] | 172 | /* seed the PRNG */ |
| 173 | srand(time(NULL)); |
| 174 | |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 175 | if (db_init(database_name)) { |
| 176 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | printf("DB: Database initialized.\n"); |
| 180 | |
| 181 | if (db_prepare()) { |
| 182 | printf("DB: Failed to prepare database.\n"); |
| 183 | return -1; |
| 184 | } |
| 185 | printf("DB: Database prepared.\n"); |
| 186 | |
| 187 | rc = bsc_bootstrap_network(mncc_recv, config_file); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 188 | if (rc < 0) |
| 189 | exit(1); |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 190 | |
Harald Welte | f294f45 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 191 | signal(SIGINT, &signal_handler); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 192 | signal(SIGABRT, &signal_handler); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 193 | signal(SIGUSR1, &signal_handler); |
Harald Welte | d125250 | 2009-01-01 01:50:32 +0000 | [diff] [blame] | 194 | |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 195 | while (1) { |
Holger Hans Peter Freyther | 8b457fb | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 196 | bsc_upqueue(bsc_gsmnet); |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 197 | bsc_select_main(0); |
Harald Welte | f6b7a90 | 2008-12-26 00:05:11 +0000 | [diff] [blame] | 198 | } |
| 199 | } |