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