blob: caff27f6f4c5a749a9563ee4c6aaaf531c1aceba [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>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010039#include <osmocom/core/stats.h>
Harald Welte9f75c352010-04-30 20:26:32 +020040
Harald Welteea34a4e2012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_ns.h>
42#include <osmocom/gprs/gprs_bssgp.h>
43
Harald Welte9f75c352010-04-30 20:26:32 +020044#include <openbsc/signal.h>
45#include <openbsc/debug.h>
Harald Welte9f75c352010-04-30 20:26:32 +020046#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 Welte4b037e42010-05-19 19:45:32 +020049#include <osmocom/vty/command.h>
50#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010051#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010052#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020053#include <osmocom/vty/ports.h>
Harald Welte1353f962010-05-16 19:20:24 +020054
Harald Weltee2365962010-05-04 07:41:59 +020055#include "../../bscconfig.h"
Harald Welte9f75c352010-04-30 20:26:32 +020056
Harald Welte9f75c352010-04-30 20:26:32 +020057#define _GNU_SOURCE
58#include <getopt.h>
59
60void *tall_bsc_ctx;
61
Harald Welte9f75c352010-04-30 20:26:32 +020062const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080063 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010064 "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 +080065 "This is free software: you are free to change and redistribute it.\r\n"
66 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte9f75c352010-04-30 20:26:32 +020067
Harald Welte288be162010-05-01 16:48:27 +020068static char *config_file = "osmo_gbproxy.cfg";
Jacob Erlbeck67a44452014-05-19 10:14:58 +020069struct gbproxy_config gbcfg = {0};
Harald Welte2c869ef2010-08-25 19:43:54 +020070static int daemonize = 0;
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,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 struct msgb *msg, uint16_t bvci)
Harald Welte9f75c352010-04-30 20:26:32 +020078{
79 int rc = 0;
80
81 switch (event) {
82 case GPRS_NS_EVT_UNIT_DATA:
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +020083 rc = gbprox_rcvmsg(&gbcfg, msg, nsvc->nsei, bvci, nsvc->nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +020084 break;
85 default:
86 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
87 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010088 msgb_free(msg);
Harald Welte9f75c352010-04-30 20:26:32 +020089 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:
Harald Welte28cd9442017-08-20 20:50:06 +0200101 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200102 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte6df0c512010-05-12 18:40:01 +0000103 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 Welte768f2872010-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 Welte2c869ef2010-08-25 19:43:54 +0200131 printf(" -D --daemonize Fork the process into a background daemon\n");
Harald Welte768f2872010-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 Welte2c869ef2010-08-25 19:43:54 +0200146 { "daemonize", 0, 0, 'D' },
Harald Welte768f2872010-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 Welte2c869ef2010-08-25 19:43:54 +0200155 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte768f2872010-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':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200166 log_set_use_color(osmo_stderr_target, 0);
Harald Welte768f2872010-05-14 11:22:33 +0000167 break;
168 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200169 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte768f2872010-05-14 11:22:33 +0000170 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200171 case 'D':
172 daemonize = 1;
173 break;
Harald Welte768f2872010-05-14 11:22:33 +0000174 case 'c':
Holger Hans Peter Freytherabb34782015-01-26 10:26:36 +0100175 config_file = optarg;
Harald Welte768f2872010-05-14 11:22:33 +0000176 break;
177 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200178 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte768f2872010-05-14 11:22:33 +0000179 break;
180 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200181 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte768f2872010-05-14 11:22:33 +0000182 break;
183 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200184 print_version(1);
Harald Welte768f2872010-05-14 11:22:33 +0000185 exit(0);
186 break;
187 default:
188 break;
189 }
190 }
191}
192
Holger Hans Peter Freyther3e4e8cf2015-09-24 15:38:38 +0200193extern int 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 Welte11461a62012-06-17 12:58:46 +0800202/* default categories */
203static struct log_info_cat gprs_categories[] = {
204 [DGPRS] = {
205 .name = "DGPRS",
206 .description = "GPRS Packet Service",
207 .enabled = 1, .loglevel = LOGL_DEBUG,
208 },
209 [DNS] = {
210 .name = "DNS",
211 .description = "GPRS Network Service (NS)",
212 .enabled = 1, .loglevel = LOGL_INFO,
213 },
214 [DBSSGP] = {
215 .name = "DBSSGP",
216 .description = "GPRS BSS Gateway Protocol (BSSGP)",
217 .enabled = 1, .loglevel = LOGL_DEBUG,
218 },
219};
220
221static const struct log_info gprs_log_info = {
222 .filter_fn = gprs_log_filter_fn,
223 .cat = gprs_categories,
224 .num_cat = ARRAY_SIZE(gprs_categories),
225};
226
Harald Welte9f75c352010-04-30 20:26:32 +0200227int main(int argc, char **argv)
228{
229 struct gsm_network dummy_network;
Harald Welte9f75c352010-04-30 20:26:32 +0200230 int rc;
231
232 tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
Neels Hofmeyr39ae17f2016-09-16 01:49:08 +0200233 msgb_talloc_ctx_init(tall_bsc_ctx, 0);
Harald Welte6df0c512010-05-12 18:40:01 +0000234
235 signal(SIGINT, &signal_handler);
Harald Welte28cd9442017-08-20 20:50:06 +0200236 signal(SIGTERM, &signal_handler);
Harald Welte6df0c512010-05-12 18:40:01 +0000237 signal(SIGABRT, &signal_handler);
238 signal(SIGUSR1, &signal_handler);
239 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200240 osmo_init_ignore_signals();
Harald Welte9f75c352010-04-30 20:26:32 +0200241
Harald Welte11461a62012-06-17 12:58:46 +0800242 osmo_init_logging(&gprs_log_info);
Harald Welte9f75c352010-04-30 20:26:32 +0200243
Harald Weltec31f4802010-05-25 23:31:39 +0200244 vty_info.copyright = openbsc_copyright;
245 vty_init(&vty_info);
Maxdb0e3802017-01-12 19:35:11 +0100246 logging_vty_add_cmds(NULL);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100247 osmo_stats_vty_add_cmds(&gprs_log_info);
Harald Welte7af49622010-05-19 14:04:23 +0200248 gbproxy_vty_init();
Harald Welte1353f962010-05-16 19:20:24 +0200249
Harald Welte768f2872010-05-14 11:22:33 +0000250 handle_options(argc, argv);
251
Harald Weltef2b4cd72010-05-13 11:45:07 +0200252 rate_ctr_init(tall_bsc_ctx);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100253 osmo_stats_init(tall_bsc_ctx);
Harald Welte9f75c352010-04-30 20:26:32 +0200254
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800255 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb, tall_bsc_ctx);
Harald Welte0a4050c2010-05-11 10:01:17 +0200256 if (!bssgp_nsi) {
Harald Welte9f75c352010-04-30 20:26:32 +0200257 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
258 exit(1);
259 }
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200260 gbproxy_init_config(&gbcfg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200261 gbcfg.nsi = bssgp_nsi;
Harald Welte1194b582010-05-12 15:55:23 +0000262 gprs_ns_vty_init(bssgp_nsi);
Harald Welte68d85d52012-06-16 17:45:59 +0800263 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800264 bssgp_set_log_ss(DBSSGP);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200265 osmo_signal_register_handler(SS_L_NS, &gbprox_signal, &gbcfg);
Harald Welte1194b582010-05-12 15:55:23 +0000266
267 rc = gbproxy_parse_config(config_file, &gbcfg);
268 if (rc < 0) {
269 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
270 exit(2);
271 }
272
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100273 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100274 rc = telnet_init_dynif(tall_bsc_ctx, &dummy_network,
275 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
276 if (rc < 0)
277 exit(1);
278
Harald Weltee6599ee2012-06-17 12:25:53 +0800279 if (!gprs_nsvc_by_nsei(gbcfg.nsi, gbcfg.nsip_sgsn_nsei)) {
Harald Welte7af49622010-05-19 14:04:23 +0200280 LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u "
281 "without creating that NSEI before\n",
282 gbcfg.nsip_sgsn_nsei);
283 exit(2);
284 }
Harald Welte9f75c352010-04-30 20:26:32 +0200285
Harald Welteff3bde82010-05-19 15:09:09 +0200286 rc = gprs_ns_nsip_listen(bssgp_nsi);
Harald Welte7af49622010-05-19 14:04:23 +0200287 if (rc < 0) {
288 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
289 exit(2);
290 }
Harald Welte1ccbf442010-05-14 11:53:08 +0000291
Harald Welteff3bde82010-05-19 15:09:09 +0200292 rc = gprs_ns_frgre_listen(bssgp_nsi);
293 if (rc < 0) {
294 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
295 "socket. Do you have CAP_NET_RAW?\n");
296 exit(2);
Harald Welte5540c4c2010-05-19 14:38:50 +0200297 }
298
Harald Welte2c869ef2010-08-25 19:43:54 +0200299 if (daemonize) {
300 rc = osmo_daemonize();
301 if (rc < 0) {
302 perror("Error during daemonize");
303 exit(1);
304 }
305 }
306
Harald Welte1ccbf442010-05-14 11:53:08 +0000307 /* Reset all the persistent NS-VCs that we've read from the config */
308 gbprox_reset_persistent_nsvcs(bssgp_nsi);
309
Harald Welte9f75c352010-04-30 20:26:32 +0200310 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200311 rc = osmo_select_main(0);
Harald Welte9f75c352010-04-30 20:26:32 +0200312 if (rc < 0)
313 exit(3);
314 }
315
316 exit(0);
317}