Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 5180844 | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [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 | d1991e7 | 2010-04-30 20:26:32 +0200 | [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 | d1991e7 | 2010-04-30 20:26:32 +0200 | [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 | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <getopt.h> |
| 27 | #include <errno.h> |
Harald Welte | 32c806e | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 28 | #include <signal.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
| 36 | #include <osmocore/talloc.h> |
| 37 | #include <osmocore/select.h> |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 38 | #include <osmocore/rate_ctr.h> |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 39 | #include <osmocore/process.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 40 | |
| 41 | #include <openbsc/signal.h> |
| 42 | #include <openbsc/debug.h> |
| 43 | #include <openbsc/gprs_ns.h> |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 44 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | #include <openbsc/vty.h> |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 46 | #include <openbsc/gb_proxy.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 47 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 48 | #include <osmocom/vty/command.h> |
| 49 | #include <osmocom/vty/telnet_interface.h> |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 50 | |
Harald Welte | 48030cf | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 51 | #include "../../bscconfig.h" |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 52 | |
| 53 | /* this is here for the vty... it will never be called */ |
| 54 | void subscr_put() { abort(); } |
| 55 | |
| 56 | #define _GNU_SOURCE |
| 57 | #include <getopt.h> |
| 58 | |
| 59 | void *tall_bsc_ctx; |
| 60 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 61 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 62 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 63 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 64 | "This is free software: you are free to change and redistribute it.\r\n" |
| 65 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 66 | |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 67 | static struct log_target *stderr_target; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 68 | static char *config_file = "osmo_gbproxy.cfg"; |
Harald Welte | f01709d | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 69 | struct gbproxy_config gbcfg; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 70 | static int daemonize = 0; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 71 | |
| 72 | /* Pointer to the SGSN peer */ |
| 73 | extern struct gbprox_peer *gbprox_peer_sgsn; |
| 74 | |
| 75 | /* call-back function for the NS protocol */ |
| 76 | static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
| 77 | struct msgb *msg, u_int16_t bvci) |
| 78 | { |
| 79 | int rc = 0; |
| 80 | |
| 81 | switch (event) { |
| 82 | case GPRS_NS_EVT_UNIT_DATA: |
| 83 | rc = gbprox_rcvmsg(msg, nsvc, bvci); |
| 84 | break; |
| 85 | default: |
| 86 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 87 | if (msg) |
| 88 | talloc_free(msg); |
| 89 | rc = -EIO; |
| 90 | break; |
| 91 | } |
| 92 | return rc; |
| 93 | } |
| 94 | |
Harald Welte | 32c806e | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 95 | static void signal_handler(int signal) |
| 96 | { |
| 97 | fprintf(stdout, "signal %u received\n", signal); |
| 98 | |
| 99 | switch (signal) { |
| 100 | case SIGINT: |
| 101 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
| 102 | sleep(1); |
| 103 | exit(0); |
| 104 | break; |
| 105 | case SIGABRT: |
| 106 | /* in case of abort, we want to obtain a talloc report |
| 107 | * and then return to the caller, who will abort the process */ |
| 108 | case SIGUSR1: |
| 109 | talloc_report(tall_vty_ctx, stderr); |
| 110 | talloc_report_full(tall_bsc_ctx, stderr); |
| 111 | break; |
| 112 | case SIGUSR2: |
| 113 | talloc_report_full(tall_vty_ctx, stderr); |
| 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 120 | static void print_usage() |
| 121 | { |
| 122 | printf("Usage: bsc_hack\n"); |
| 123 | } |
| 124 | |
| 125 | static void print_help() |
| 126 | { |
| 127 | printf(" Some useful help...\n"); |
| 128 | printf(" -h --help this text\n"); |
| 129 | printf(" -d option --debug=DNS:DGPRS,0:0 enable debugging\n"); |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 130 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 131 | printf(" -c --config-file filename The config file to use.\n"); |
| 132 | printf(" -s --disable-color\n"); |
| 133 | printf(" -T --timestamp Prefix every log line with a timestamp\n"); |
| 134 | printf(" -V --version. Print the version of OpenBSC.\n"); |
| 135 | printf(" -e --log-level number. Set a global loglevel.\n"); |
| 136 | } |
| 137 | |
| 138 | static void handle_options(int argc, char **argv) |
| 139 | { |
| 140 | while (1) { |
| 141 | int option_index = 0, c; |
| 142 | static struct option long_options[] = { |
| 143 | { "help", 0, 0, 'h' }, |
| 144 | { "debug", 1, 0, 'd' }, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 145 | { "daemonize", 0, 0, 'D' }, |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 146 | { "config-file", 1, 0, 'c' }, |
| 147 | { "disable-color", 0, 0, 's' }, |
| 148 | { "timestamp", 0, 0, 'T' }, |
| 149 | { "version", 0, 0, 'V' }, |
| 150 | { "log-level", 1, 0, 'e' }, |
| 151 | { 0, 0, 0, 0 } |
| 152 | }; |
| 153 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 154 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 155 | long_options, &option_index); |
| 156 | if (c == -1) |
| 157 | break; |
| 158 | |
| 159 | switch (c) { |
| 160 | case 'h': |
| 161 | print_usage(); |
| 162 | print_help(); |
| 163 | exit(0); |
| 164 | case 's': |
| 165 | log_set_use_color(stderr_target, 0); |
| 166 | break; |
| 167 | case 'd': |
| 168 | log_parse_category_mask(stderr_target, optarg); |
| 169 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 170 | case 'D': |
| 171 | daemonize = 1; |
| 172 | break; |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 173 | case 'c': |
| 174 | config_file = strdup(optarg); |
| 175 | break; |
| 176 | case 'T': |
| 177 | log_set_print_timestamp(stderr_target, 1); |
| 178 | break; |
| 179 | case 'e': |
| 180 | log_set_log_level(stderr_target, atoi(optarg)); |
| 181 | break; |
| 182 | case 'V': |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 183 | print_version(1); |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 184 | exit(0); |
| 185 | break; |
| 186 | default: |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Harald Welte | 32c806e | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 192 | extern void *tall_msgb_ctx; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 193 | |
Holger Hans Peter Freyther | 3538c77 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 194 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 195 | |
| 196 | static struct vty_app_info vty_info = { |
Harald Welte | 9af42c3 | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 197 | .name = "OsmoGbProxy", |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 198 | .version = PACKAGE_VERSION, |
| 199 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 200 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 203 | int main(int argc, char **argv) |
| 204 | { |
| 205 | struct gsm_network dummy_network; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 206 | int rc; |
| 207 | |
| 208 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy"); |
Harald Welte | 32c806e | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 209 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 210 | |
| 211 | signal(SIGINT, &signal_handler); |
| 212 | signal(SIGABRT, &signal_handler); |
| 213 | signal(SIGUSR1, &signal_handler); |
| 214 | signal(SIGUSR2, &signal_handler); |
| 215 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 216 | |
| 217 | log_init(&log_info); |
| 218 | stderr_target = log_target_create_stderr(); |
| 219 | log_add_target(stderr_target); |
| 220 | log_set_all_filter(stderr_target, 1); |
| 221 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 222 | vty_info.copyright = openbsc_copyright; |
| 223 | vty_init(&vty_info); |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 224 | logging_vty_add_cmds(); |
Harald Welte | 2ef79c5 | 2010-05-19 14:04:23 +0200 | [diff] [blame] | 225 | gbproxy_vty_init(); |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 226 | |
Harald Welte | 17f83c5 | 2010-05-14 11:22:33 +0000 | [diff] [blame] | 227 | handle_options(argc, argv); |
| 228 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 229 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 230 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 231 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4246); |
| 232 | if (rc < 0) |
| 233 | exit(1); |
| 234 | |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 235 | bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb); |
| 236 | if (!bssgp_nsi) { |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 237 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 238 | exit(1); |
| 239 | } |
Harald Welte | 874acec | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 240 | gbcfg.nsi = bssgp_nsi; |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 241 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | 5bb0d36 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 242 | register_signal_handler(SS_NS, &gbprox_signal, NULL); |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 243 | |
| 244 | rc = gbproxy_parse_config(config_file, &gbcfg); |
| 245 | if (rc < 0) { |
| 246 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 247 | exit(2); |
| 248 | } |
| 249 | |
Harald Welte | 2ef79c5 | 2010-05-19 14:04:23 +0200 | [diff] [blame] | 250 | if (!nsvc_by_nsei(gbcfg.nsi, gbcfg.nsip_sgsn_nsei)) { |
| 251 | LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u " |
| 252 | "without creating that NSEI before\n", |
| 253 | gbcfg.nsip_sgsn_nsei); |
| 254 | exit(2); |
| 255 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 256 | |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 257 | rc = gprs_ns_nsip_listen(bssgp_nsi); |
Harald Welte | 2ef79c5 | 2010-05-19 14:04:23 +0200 | [diff] [blame] | 258 | if (rc < 0) { |
| 259 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
| 260 | exit(2); |
| 261 | } |
Harald Welte | 9ee404a | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 262 | |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 263 | rc = gprs_ns_frgre_listen(bssgp_nsi); |
| 264 | if (rc < 0) { |
| 265 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 266 | "socket. Do you have CAP_NET_RAW?\n"); |
| 267 | exit(2); |
Harald Welte | ba1f931 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 270 | if (daemonize) { |
| 271 | rc = osmo_daemonize(); |
| 272 | if (rc < 0) { |
| 273 | perror("Error during daemonize"); |
| 274 | exit(1); |
| 275 | } |
| 276 | } |
| 277 | |
Harald Welte | 9ee404a | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 278 | /* Reset all the persistent NS-VCs that we've read from the config */ |
| 279 | gbprox_reset_persistent_nsvcs(bssgp_nsi); |
| 280 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 281 | while (1) { |
| 282 | rc = bsc_select_main(0); |
| 283 | if (rc < 0) |
| 284 | exit(3); |
| 285 | } |
| 286 | |
| 287 | exit(0); |
| 288 | } |