blob: c66350c90fd58fbc7bedc291306e8cff74540c1f [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>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08004 * (C) 2010 by On-Waves
Harald Welte9f75c352010-04-30 20:26:32 +02005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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 Welte9f75c352010-04-30 20:26:32 +020010 * (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 Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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 Welte9f75c352010-04-30 20:26:32 +020019 *
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 Welte6df0c512010-05-12 18:40:01 +000028#include <signal.h>
Harald Welte9f75c352010-04-30 20:26:32 +020029#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte9f75c352010-04-30 20:26:32 +020031#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020035#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/rate_ctr.h>
Harald Welte9f75c352010-04-30 20:26:32 +020039
40#include <openbsc/signal.h>
41#include <openbsc/debug.h>
42#include <openbsc/gprs_ns.h>
Harald Welte0a4050c2010-05-11 10:01:17 +020043#include <openbsc/gprs_bssgp.h>
Harald Welte9f75c352010-04-30 20:26:32 +020044#include <openbsc/vty.h>
Harald Welteb77c6972010-05-01 11:28:43 +020045#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020046
Harald Welte4b037e42010-05-19 19:45:32 +020047#include <osmocom/vty/command.h>
48#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010049#include <osmocom/vty/logging.h>
Harald Welte1353f962010-05-16 19:20:24 +020050
Harald Weltee2365962010-05-04 07:41:59 +020051#include "../../bscconfig.h"
Harald Welte9f75c352010-04-30 20:26:32 +020052
53/* this is here for the vty... it will never be called */
54void subscr_put() { abort(); }
55
56#define _GNU_SOURCE
57#include <getopt.h>
58
59void *tall_bsc_ctx;
60
Harald Welte9f75c352010-04-30 20:26:32 +020061const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080062 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010063 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080064 "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 Welte9f75c352010-04-30 20:26:32 +020066
Harald Welte288be162010-05-01 16:48:27 +020067static char *config_file = "osmo_gbproxy.cfg";
Harald Welte672f5c42010-05-03 18:54:58 +020068struct gbproxy_config gbcfg;
Harald Welte2c869ef2010-08-25 19:43:54 +020069static int daemonize = 0;
Harald Welte9f75c352010-04-30 20:26:32 +020070
71/* Pointer to the SGSN peer */
72extern struct gbprox_peer *gbprox_peer_sgsn;
73
74/* call-back function for the NS protocol */
75static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020076 struct msgb *msg, uint16_t bvci)
Harald Welte9f75c352010-04-30 20:26:32 +020077{
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 Welte6df0c512010-05-12 18:40:01 +000094static void signal_handler(int signal)
95{
96 fprintf(stdout, "signal %u received\n", signal);
97
98 switch (signal) {
99 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200100 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte6df0c512010-05-12 18:40:01 +0000101 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 Welte768f2872010-05-14 11:22:33 +0000119static void print_usage()
120{
121 printf("Usage: bsc_hack\n");
122}
123
124static 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");
Harald Welte2c869ef2010-08-25 19:43:54 +0200129 printf(" -D --daemonize Fork the process into a background daemon\n");
Harald Welte768f2872010-05-14 11:22:33 +0000130 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' },
Harald Welte2c869ef2010-08-25 19:43:54 +0200144 { "daemonize", 0, 0, 'D' },
Harald Welte768f2872010-05-14 11:22:33 +0000145 { "config-file", 1, 0, 'c' },
146 { "disable-color", 0, 0, 's' },
147 { "timestamp", 0, 0, 'T' },
148 { "version", 0, 0, 'V' },
149 { "log-level", 1, 0, 'e' },
150 { 0, 0, 0, 0 }
151 };
152
Harald Welte2c869ef2010-08-25 19:43:54 +0200153 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte768f2872010-05-14 11:22:33 +0000154 long_options, &option_index);
155 if (c == -1)
156 break;
157
158 switch (c) {
159 case 'h':
160 print_usage();
161 print_help();
162 exit(0);
163 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200164 log_set_use_color(osmo_stderr_target, 0);
Harald Welte768f2872010-05-14 11:22:33 +0000165 break;
166 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200167 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte768f2872010-05-14 11:22:33 +0000168 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200169 case 'D':
170 daemonize = 1;
171 break;
Harald Welte768f2872010-05-14 11:22:33 +0000172 case 'c':
173 config_file = strdup(optarg);
174 break;
175 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200176 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte768f2872010-05-14 11:22:33 +0000177 break;
178 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200179 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte768f2872010-05-14 11:22:33 +0000180 break;
181 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200182 print_version(1);
Harald Welte768f2872010-05-14 11:22:33 +0000183 exit(0);
184 break;
185 default:
186 break;
187 }
188 }
189}
190
Harald Welte6df0c512010-05-12 18:40:01 +0000191extern void *tall_msgb_ctx;
Harald Welte9f75c352010-04-30 20:26:32 +0200192
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800193extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200194
195static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100196 .name = "OsmoGbProxy",
Harald Weltec31f4802010-05-25 23:31:39 +0200197 .version = PACKAGE_VERSION,
198 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800199 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200200};
201
Harald Welte9f75c352010-04-30 20:26:32 +0200202int main(int argc, char **argv)
203{
204 struct gsm_network dummy_network;
Harald Welte9f75c352010-04-30 20:26:32 +0200205 int rc;
206
207 tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
Harald Welte6df0c512010-05-12 18:40:01 +0000208 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
209
210 signal(SIGINT, &signal_handler);
211 signal(SIGABRT, &signal_handler);
212 signal(SIGUSR1, &signal_handler);
213 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200214 osmo_init_ignore_signals();
Harald Welte9f75c352010-04-30 20:26:32 +0200215
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200216 osmo_init_logging(&log_info);
Harald Welte9f75c352010-04-30 20:26:32 +0200217
Harald Weltec31f4802010-05-25 23:31:39 +0200218 vty_info.copyright = openbsc_copyright;
219 vty_init(&vty_info);
Pablo Neira Ayuso38487702011-03-23 08:49:00 +0100220 logging_vty_add_cmds(&log_info);
Harald Welte7af49622010-05-19 14:04:23 +0200221 gbproxy_vty_init();
Harald Welte1353f962010-05-16 19:20:24 +0200222
Harald Welte768f2872010-05-14 11:22:33 +0000223 handle_options(argc, argv);
224
Harald Weltef2b4cd72010-05-13 11:45:07 +0200225 rate_ctr_init(tall_bsc_ctx);
Harald Welte9f75c352010-04-30 20:26:32 +0200226
Harald Weltedcccb182010-05-16 20:52:23 +0200227 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4246);
228 if (rc < 0)
229 exit(1);
230
Harald Welte0a4050c2010-05-11 10:01:17 +0200231 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb);
232 if (!bssgp_nsi) {
Harald Welte9f75c352010-04-30 20:26:32 +0200233 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
234 exit(1);
235 }
Harald Welte0a4050c2010-05-11 10:01:17 +0200236 gbcfg.nsi = bssgp_nsi;
Harald Welte1194b582010-05-12 15:55:23 +0000237 gprs_ns_vty_init(bssgp_nsi);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200238 osmo_signal_register_handler(SS_NS, &gbprox_signal, NULL);
Harald Welte1194b582010-05-12 15:55:23 +0000239
240 rc = gbproxy_parse_config(config_file, &gbcfg);
241 if (rc < 0) {
242 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
243 exit(2);
244 }
245
Harald Welte7af49622010-05-19 14:04:23 +0200246 if (!nsvc_by_nsei(gbcfg.nsi, gbcfg.nsip_sgsn_nsei)) {
247 LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u "
248 "without creating that NSEI before\n",
249 gbcfg.nsip_sgsn_nsei);
250 exit(2);
251 }
Harald Welte9f75c352010-04-30 20:26:32 +0200252
Harald Welteff3bde82010-05-19 15:09:09 +0200253 rc = gprs_ns_nsip_listen(bssgp_nsi);
Harald Welte7af49622010-05-19 14:04:23 +0200254 if (rc < 0) {
255 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
256 exit(2);
257 }
Harald Welte1ccbf442010-05-14 11:53:08 +0000258
Harald Welteff3bde82010-05-19 15:09:09 +0200259 rc = gprs_ns_frgre_listen(bssgp_nsi);
260 if (rc < 0) {
261 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
262 "socket. Do you have CAP_NET_RAW?\n");
263 exit(2);
Harald Welte5540c4c2010-05-19 14:38:50 +0200264 }
265
Harald Welte2c869ef2010-08-25 19:43:54 +0200266 if (daemonize) {
267 rc = osmo_daemonize();
268 if (rc < 0) {
269 perror("Error during daemonize");
270 exit(1);
271 }
272 }
273
Harald Welte1ccbf442010-05-14 11:53:08 +0000274 /* Reset all the persistent NS-VCs that we've read from the config */
275 gbprox_reset_persistent_nsvcs(bssgp_nsi);
276
Harald Welte9f75c352010-04-30 20:26:32 +0200277 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200278 rc = osmo_select_main(0);
Harald Welte9f75c352010-04-30 20:26:32 +0200279 if (rc < 0)
280 exit(3);
281 }
282
283 exit(0);
284}