Harald Welte | 9f75c35 | 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 | 6df0c51 | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Harald Welte | 9f75c35 | 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 | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 39 | #include <osmocore/rate_ctr.h> |
Harald Welte | 9f75c35 | 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 | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 44 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | #include <openbsc/telnet_interface.h> |
| 46 | #include <openbsc/vty.h> |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 47 | #include <openbsc/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 48 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 49 | #include "../../bscconfig.h" |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 50 | |
| 51 | /* this is here for the vty... it will never be called */ |
| 52 | void subscr_put() { abort(); } |
| 53 | |
| 54 | #define _GNU_SOURCE |
| 55 | #include <getopt.h> |
| 56 | |
| 57 | void *tall_bsc_ctx; |
| 58 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 59 | const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION; |
| 60 | const char *openbsc_copyright = |
| 61 | "Copyright (C) 2010 Harald Welte and On-Waves\n" |
| 62 | "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\n" |
| 63 | "Dieter Spaar, Andreas Eversberg, Holger Freyther\n\n" |
| 64 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 65 | "This is free software: you are free to change and redistribute it.\n" |
| 66 | "There is NO WARRANTY, to the extent permitted by law.\n"; |
| 67 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 68 | static char *config_file = "osmo_gbproxy.cfg"; |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 69 | struct gbproxy_config gbcfg; |
Harald Welte | 9f75c35 | 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 | 6df0c51 | 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 | |
| 119 | extern void *tall_msgb_ctx; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 120 | |
| 121 | int main(int argc, char **argv) |
| 122 | { |
| 123 | struct gsm_network dummy_network; |
| 124 | struct log_target *stderr_target; |
| 125 | struct sockaddr_in sin; |
| 126 | int rc; |
| 127 | |
| 128 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy"); |
Harald Welte | 6df0c51 | 2010-05-12 18:40:01 +0000 | [diff] [blame] | 129 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 130 | |
| 131 | signal(SIGINT, &signal_handler); |
| 132 | signal(SIGABRT, &signal_handler); |
| 133 | signal(SIGUSR1, &signal_handler); |
| 134 | signal(SIGUSR2, &signal_handler); |
| 135 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 136 | |
| 137 | log_init(&log_info); |
| 138 | stderr_target = log_target_create_stderr(); |
| 139 | log_add_target(stderr_target); |
| 140 | log_set_all_filter(stderr_target, 1); |
| 141 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 142 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | e69b281 | 2010-05-11 18:32:37 +0200 | [diff] [blame] | 143 | telnet_init(&dummy_network, 4246); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 144 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 145 | bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb); |
| 146 | if (!bssgp_nsi) { |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 147 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 148 | exit(1); |
| 149 | } |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 150 | gbcfg.nsi = bssgp_nsi; |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 151 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 152 | register_signal_handler(SS_NS, &gbprox_signal, NULL); |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 153 | |
| 154 | rc = gbproxy_parse_config(config_file, &gbcfg); |
| 155 | if (rc < 0) { |
| 156 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 157 | exit(2); |
| 158 | } |
| 159 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 160 | nsip_listen(bssgp_nsi, gbcfg.nsip_listen_port); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 161 | |
| 162 | /* 'establish' the outgoing connection to the SGSN */ |
Harald Welte | 7b8255c | 2010-04-30 20:42:42 +0200 | [diff] [blame] | 163 | sin.sin_family = AF_INET; |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 164 | sin.sin_port = htons(gbcfg.nsip_sgsn_port); |
| 165 | sin.sin_addr.s_addr = htonl(gbcfg.nsip_sgsn_ip); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 166 | nsip_connect(bssgp_nsi, &sin, gbcfg.nsip_sgsn_nsei, |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 167 | gbcfg.nsip_sgsn_nsvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 168 | |
| 169 | while (1) { |
| 170 | rc = bsc_select_main(0); |
| 171 | if (rc < 0) |
| 172 | exit(3); |
| 173 | } |
| 174 | |
| 175 | exit(0); |
| 176 | } |
| 177 | |
| 178 | struct gsm_network; |
| 179 | int bsc_vty_init(struct gsm_network *dummy) |
| 180 | { |
| 181 | cmd_init(1); |
| 182 | vty_init(); |
| 183 | |
| 184 | openbsc_vty_add_cmds(); |
Harald Welte | 799e0c9 | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 185 | gbproxy_vty_init(); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |