blob: de83186f3bde6d1b96f339be595c3082a59b66a6 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* 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 Welte6df0c512010-05-12 18:40:01 +000029#include <signal.h>
Harald Welte9f75c352010-04-30 20:26:32 +020030#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 Weltef2b4cd72010-05-13 11:45:07 +020039#include <osmocore/rate_ctr.h>
Harald Welte9f75c352010-04-30 20:26:32 +020040
41#include <openbsc/signal.h>
42#include <openbsc/debug.h>
43#include <openbsc/gprs_ns.h>
Harald Welte0a4050c2010-05-11 10:01:17 +020044#include <openbsc/gprs_bssgp.h>
Harald Welte9f75c352010-04-30 20:26:32 +020045#include <openbsc/telnet_interface.h>
46#include <openbsc/vty.h>
Harald Welteb77c6972010-05-01 11:28:43 +020047#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020048
Harald Weltee2365962010-05-04 07:41:59 +020049#include "../../bscconfig.h"
Harald Welte9f75c352010-04-30 20:26:32 +020050
51/* this is here for the vty... it will never be called */
52void subscr_put() { abort(); }
53
54#define _GNU_SOURCE
55#include <getopt.h>
56
57void *tall_bsc_ctx;
58
Harald Welte9f75c352010-04-30 20:26:32 +020059const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION;
60const 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 Welte768f2872010-05-14 11:22:33 +000068static struct log_target *stderr_target;
Harald Welte288be162010-05-01 16:48:27 +020069static char *config_file = "osmo_gbproxy.cfg";
Harald Welte672f5c42010-05-03 18:54:58 +020070struct gbproxy_config gbcfg;
Harald Welte9f75c352010-04-30 20:26:32 +020071
72/* Pointer to the SGSN peer */
73extern struct gbprox_peer *gbprox_peer_sgsn;
74
75/* call-back function for the NS protocol */
76static 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 Welte6df0c512010-05-12 18:40:01 +000095static 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 Welte768f2872010-05-14 11:22:33 +0000120static void print_usage()
121{
122 printf("Usage: bsc_hack\n");
123}
124
125static 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");
130 printf(" -c --config-file filename The config file to use.\n");
131 printf(" -s --disable-color\n");
132 printf(" -T --timestamp Prefix every log line with a timestamp\n");
133 printf(" -V --version. Print the version of OpenBSC.\n");
134 printf(" -e --log-level number. Set a global loglevel.\n");
135}
136
137static void handle_options(int argc, char **argv)
138{
139 while (1) {
140 int option_index = 0, c;
141 static struct option long_options[] = {
142 { "help", 0, 0, 'h' },
143 { "debug", 1, 0, 'd' },
144 { "config-file", 1, 0, 'c' },
145 { "disable-color", 0, 0, 's' },
146 { "timestamp", 0, 0, 'T' },
147 { "version", 0, 0, 'V' },
148 { "log-level", 1, 0, 'e' },
149 { 0, 0, 0, 0 }
150 };
151
152 c = getopt_long(argc, argv, "hd:c:sTVe:",
153 long_options, &option_index);
154 if (c == -1)
155 break;
156
157 switch (c) {
158 case 'h':
159 print_usage();
160 print_help();
161 exit(0);
162 case 's':
163 log_set_use_color(stderr_target, 0);
164 break;
165 case 'd':
166 log_parse_category_mask(stderr_target, optarg);
167 break;
168 case 'c':
169 config_file = strdup(optarg);
170 break;
171 case 'T':
172 log_set_print_timestamp(stderr_target, 1);
173 break;
174 case 'e':
175 log_set_log_level(stderr_target, atoi(optarg));
176 break;
177 case 'V':
178 printf("%s\n", openbsc_version);
179 printf("\n");
180 puts(openbsc_copyright);
181 exit(0);
182 break;
183 default:
184 break;
185 }
186 }
187}
188
Harald Welte6df0c512010-05-12 18:40:01 +0000189extern void *tall_msgb_ctx;
Harald Welte9f75c352010-04-30 20:26:32 +0200190
191int main(int argc, char **argv)
192{
193 struct gsm_network dummy_network;
Harald Welte9f75c352010-04-30 20:26:32 +0200194 struct sockaddr_in sin;
195 int rc;
196
197 tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
Harald Welte6df0c512010-05-12 18:40:01 +0000198 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
199
200 signal(SIGINT, &signal_handler);
201 signal(SIGABRT, &signal_handler);
202 signal(SIGUSR1, &signal_handler);
203 signal(SIGUSR2, &signal_handler);
204 signal(SIGPIPE, SIG_IGN);
Harald Welte9f75c352010-04-30 20:26:32 +0200205
206 log_init(&log_info);
207 stderr_target = log_target_create_stderr();
208 log_add_target(stderr_target);
209 log_set_all_filter(stderr_target, 1);
210
Harald Welte768f2872010-05-14 11:22:33 +0000211 handle_options(argc, argv);
212
Harald Weltef2b4cd72010-05-13 11:45:07 +0200213 rate_ctr_init(tall_bsc_ctx);
Harald Weltee69b2812010-05-11 18:32:37 +0200214 telnet_init(&dummy_network, 4246);
Harald Welte9f75c352010-04-30 20:26:32 +0200215
Harald Welte0a4050c2010-05-11 10:01:17 +0200216 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb);
217 if (!bssgp_nsi) {
Harald Welte9f75c352010-04-30 20:26:32 +0200218 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
219 exit(1);
220 }
Harald Welte0a4050c2010-05-11 10:01:17 +0200221 gbcfg.nsi = bssgp_nsi;
Harald Welte1194b582010-05-12 15:55:23 +0000222 gprs_ns_vty_init(bssgp_nsi);
Harald Weltec1c1dd22010-05-11 06:34:24 +0200223 register_signal_handler(SS_NS, &gbprox_signal, NULL);
Harald Welte1194b582010-05-12 15:55:23 +0000224
225 rc = gbproxy_parse_config(config_file, &gbcfg);
226 if (rc < 0) {
227 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
228 exit(2);
229 }
230
Harald Welte0a4050c2010-05-11 10:01:17 +0200231 nsip_listen(bssgp_nsi, gbcfg.nsip_listen_port);
Harald Welte9f75c352010-04-30 20:26:32 +0200232
233 /* 'establish' the outgoing connection to the SGSN */
Harald Welte7b8255c2010-04-30 20:42:42 +0200234 sin.sin_family = AF_INET;
Harald Welteb77c6972010-05-01 11:28:43 +0200235 sin.sin_port = htons(gbcfg.nsip_sgsn_port);
236 sin.sin_addr.s_addr = htonl(gbcfg.nsip_sgsn_ip);
Harald Welte0a4050c2010-05-11 10:01:17 +0200237 nsip_connect(bssgp_nsi, &sin, gbcfg.nsip_sgsn_nsei,
Harald Welte672f5c42010-05-03 18:54:58 +0200238 gbcfg.nsip_sgsn_nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200239
Harald Welte1ccbf442010-05-14 11:53:08 +0000240
241 /* Reset all the persistent NS-VCs that we've read from the config */
242 gbprox_reset_persistent_nsvcs(bssgp_nsi);
243
Harald Welte9f75c352010-04-30 20:26:32 +0200244 while (1) {
245 rc = bsc_select_main(0);
246 if (rc < 0)
247 exit(3);
248 }
249
250 exit(0);
251}
252
253struct gsm_network;
254int bsc_vty_init(struct gsm_network *dummy)
255{
256 cmd_init(1);
257 vty_init();
258
259 openbsc_vty_add_cmds();
Harald Welte799e0c92010-04-30 21:49:24 +0200260 gbproxy_vty_init();
Harald Welte9f75c352010-04-30 20:26:32 +0200261 return 0;
262}
263