blob: 8ce83f1a1a0402d07a1efc75ce0a92705f6e7ee9 [file] [log] [blame]
Harald Welted1991e72010-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 Welte32c806e2010-05-12 18:40:01 +000029#include <signal.h>
Harald Welted1991e72010-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 Weltec80bb282010-05-13 11:45:07 +020039#include <osmocore/rate_ctr.h>
Harald Weltec4ae1762010-08-25 19:43:54 +020040#include <osmocore/process.h>
Harald Welted1991e72010-04-30 20:26:32 +020041
42#include <openbsc/signal.h>
43#include <openbsc/debug.h>
44#include <openbsc/gprs_ns.h>
Harald Welte874acec2010-05-11 10:01:17 +020045#include <openbsc/gprs_bssgp.h>
Harald Welted1991e72010-04-30 20:26:32 +020046#include <openbsc/vty.h>
Harald Welte323e4f62010-05-01 11:28:43 +020047#include <openbsc/gb_proxy.h>
Harald Welted1991e72010-04-30 20:26:32 +020048
Harald Weltebd9591f2010-05-19 19:45:32 +020049#include <osmocom/vty/command.h>
50#include <osmocom/vty/telnet_interface.h>
Harald Welte10c29f62010-05-16 19:20:24 +020051
Harald Welte48030cf2010-05-04 07:41:59 +020052#include "../../bscconfig.h"
Harald Welted1991e72010-04-30 20:26:32 +020053
54/* this is here for the vty... it will never be called */
55void subscr_put() { abort(); }
56
57#define _GNU_SOURCE
58#include <getopt.h>
59
60void *tall_bsc_ctx;
61
Harald Welted1991e72010-04-30 20:26:32 +020062const char *openbsc_copyright =
63 "Copyright (C) 2010 Harald Welte and On-Waves\n"
Harald Welted1991e72010-04-30 20:26:32 +020064 "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 Welte17f83c52010-05-14 11:22:33 +000068static struct log_target *stderr_target;
Harald Welte55fe0552010-05-01 16:48:27 +020069static char *config_file = "osmo_gbproxy.cfg";
Harald Weltef01709d2010-05-03 18:54:58 +020070struct gbproxy_config gbcfg;
Harald Weltec4ae1762010-08-25 19:43:54 +020071static int daemonize = 0;
Harald Welted1991e72010-04-30 20:26:32 +020072
73/* Pointer to the SGSN peer */
74extern struct gbprox_peer *gbprox_peer_sgsn;
75
76/* call-back function for the NS protocol */
77static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
78 struct msgb *msg, u_int16_t bvci)
79{
80 int rc = 0;
81
82 switch (event) {
83 case GPRS_NS_EVT_UNIT_DATA:
84 rc = gbprox_rcvmsg(msg, nsvc, bvci);
85 break;
86 default:
87 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
88 if (msg)
89 talloc_free(msg);
90 rc = -EIO;
91 break;
92 }
93 return rc;
94}
95
Harald Welte32c806e2010-05-12 18:40:01 +000096static void signal_handler(int signal)
97{
98 fprintf(stdout, "signal %u received\n", signal);
99
100 switch (signal) {
101 case SIGINT:
102 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
103 sleep(1);
104 exit(0);
105 break;
106 case SIGABRT:
107 /* in case of abort, we want to obtain a talloc report
108 * and then return to the caller, who will abort the process */
109 case SIGUSR1:
110 talloc_report(tall_vty_ctx, stderr);
111 talloc_report_full(tall_bsc_ctx, stderr);
112 break;
113 case SIGUSR2:
114 talloc_report_full(tall_vty_ctx, stderr);
115 break;
116 default:
117 break;
118 }
119}
120
Harald Welte17f83c52010-05-14 11:22:33 +0000121static void print_usage()
122{
123 printf("Usage: bsc_hack\n");
124}
125
126static void print_help()
127{
128 printf(" Some useful help...\n");
129 printf(" -h --help this text\n");
130 printf(" -d option --debug=DNS:DGPRS,0:0 enable debugging\n");
Harald Weltec4ae1762010-08-25 19:43:54 +0200131 printf(" -D --daemonize Fork the process into a background daemon\n");
Harald Welte17f83c52010-05-14 11:22:33 +0000132 printf(" -c --config-file filename The config file to use.\n");
133 printf(" -s --disable-color\n");
134 printf(" -T --timestamp Prefix every log line with a timestamp\n");
135 printf(" -V --version. Print the version of OpenBSC.\n");
136 printf(" -e --log-level number. Set a global loglevel.\n");
137}
138
139static void handle_options(int argc, char **argv)
140{
141 while (1) {
142 int option_index = 0, c;
143 static struct option long_options[] = {
144 { "help", 0, 0, 'h' },
145 { "debug", 1, 0, 'd' },
Harald Weltec4ae1762010-08-25 19:43:54 +0200146 { "daemonize", 0, 0, 'D' },
Harald Welte17f83c52010-05-14 11:22:33 +0000147 { "config-file", 1, 0, 'c' },
148 { "disable-color", 0, 0, 's' },
149 { "timestamp", 0, 0, 'T' },
150 { "version", 0, 0, 'V' },
151 { "log-level", 1, 0, 'e' },
152 { 0, 0, 0, 0 }
153 };
154
Harald Weltec4ae1762010-08-25 19:43:54 +0200155 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte17f83c52010-05-14 11:22:33 +0000156 long_options, &option_index);
157 if (c == -1)
158 break;
159
160 switch (c) {
161 case 'h':
162 print_usage();
163 print_help();
164 exit(0);
165 case 's':
166 log_set_use_color(stderr_target, 0);
167 break;
168 case 'd':
169 log_parse_category_mask(stderr_target, optarg);
170 break;
Harald Weltec4ae1762010-08-25 19:43:54 +0200171 case 'D':
172 daemonize = 1;
173 break;
Harald Welte17f83c52010-05-14 11:22:33 +0000174 case 'c':
175 config_file = strdup(optarg);
176 break;
177 case 'T':
178 log_set_print_timestamp(stderr_target, 1);
179 break;
180 case 'e':
181 log_set_log_level(stderr_target, atoi(optarg));
182 break;
183 case 'V':
Harald Welte10c29f62010-05-16 19:20:24 +0200184 print_version(1);
Harald Welte17f83c52010-05-14 11:22:33 +0000185 exit(0);
186 break;
187 default:
188 break;
189 }
190 }
191}
192
Harald Welte32c806e2010-05-12 18:40:01 +0000193extern void *tall_msgb_ctx;
Harald Welted1991e72010-04-30 20:26:32 +0200194
Holger Hans Peter Freyther3538c772010-06-08 16:11:06 +0800195extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Welte778c4b92010-05-25 23:31:39 +0200196
197static struct vty_app_info vty_info = {
198 .name = "Osmocom Gb Proxy",
199 .version = PACKAGE_VERSION,
200 .go_parent_cb = bsc_vty_go_parent,
201};
202
Harald Welted1991e72010-04-30 20:26:32 +0200203int main(int argc, char **argv)
204{
205 struct gsm_network dummy_network;
Harald Welted1991e72010-04-30 20:26:32 +0200206 int rc;
207
208 tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
Harald Welte32c806e2010-05-12 18:40:01 +0000209 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 Welted1991e72010-04-30 20:26:32 +0200216
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 Welte778c4b92010-05-25 23:31:39 +0200222 vty_info.copyright = openbsc_copyright;
223 vty_init(&vty_info);
Harald Welte682ee5f2010-05-16 22:02:16 +0200224 logging_vty_add_cmds();
Harald Welte2ef79c52010-05-19 14:04:23 +0200225 gbproxy_vty_init();
Harald Welte10c29f62010-05-16 19:20:24 +0200226
Harald Welte17f83c52010-05-14 11:22:33 +0000227 handle_options(argc, argv);
228
Harald Weltec80bb282010-05-13 11:45:07 +0200229 rate_ctr_init(tall_bsc_ctx);
Harald Welted1991e72010-04-30 20:26:32 +0200230
Harald Welte40152872010-05-16 20:52:23 +0200231 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4246);
232 if (rc < 0)
233 exit(1);
234
Harald Welte874acec2010-05-11 10:01:17 +0200235 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb);
236 if (!bssgp_nsi) {
Harald Welted1991e72010-04-30 20:26:32 +0200237 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
238 exit(1);
239 }
Harald Welte874acec2010-05-11 10:01:17 +0200240 gbcfg.nsi = bssgp_nsi;
Harald Welte51cce2a2010-05-12 15:55:23 +0000241 gprs_ns_vty_init(bssgp_nsi);
Harald Welte5bb0d362010-05-11 06:34:24 +0200242 register_signal_handler(SS_NS, &gbprox_signal, NULL);
Harald Welte51cce2a2010-05-12 15:55:23 +0000243
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 Welte2ef79c52010-05-19 14:04:23 +0200250 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 Welted1991e72010-04-30 20:26:32 +0200256
Harald Welte31f0a232010-05-19 15:09:09 +0200257 rc = gprs_ns_nsip_listen(bssgp_nsi);
Harald Welte2ef79c52010-05-19 14:04:23 +0200258 if (rc < 0) {
259 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
260 exit(2);
261 }
Harald Welte9ee404a2010-05-14 11:53:08 +0000262
Harald Welte31f0a232010-05-19 15:09:09 +0200263 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 Welteba1f9312010-05-19 14:38:50 +0200268 }
269
Harald Weltec4ae1762010-08-25 19:43:54 +0200270 if (daemonize) {
271 rc = osmo_daemonize();
272 if (rc < 0) {
273 perror("Error during daemonize");
274 exit(1);
275 }
276 }
277
Harald Welte9ee404a2010-05-14 11:53:08 +0000278 /* Reset all the persistent NS-VCs that we've read from the config */
279 gbprox_reset_persistent_nsvcs(bssgp_nsi);
280
Harald Welted1991e72010-04-30 20:26:32 +0200281 while (1) {
282 rc = bsc_select_main(0);
283 if (rc < 0)
284 exit(3);
285 }
286
287 exit(0);
288}