blob: 7ef930246272c86f4676b0979650f2e5d15264fb [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
Alexander Couzens82182d02020-09-22 13:21:46 +020041#include <osmocom/gprs/gprs_ns2.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080042#include <osmocom/gprs/gprs_bssgp.h>
43
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020044#include <osmocom/sgsn/signal.h>
45#include <osmocom/sgsn/debug.h>
46#include <osmocom/sgsn/vty.h>
47#include <osmocom/sgsn/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020048
Daniel Willmannc977afa2018-06-01 07:21:20 +020049#include <osmocom/ctrl/control_vty.h>
50#include <osmocom/ctrl/control_if.h>
51#include <osmocom/ctrl/ports.h>
52
Harald Welte4b037e42010-05-19 19:45:32 +020053#include <osmocom/vty/command.h>
54#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010055#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010056#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020057#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010058#include <osmocom/vty/misc.h>
Harald Welte1353f962010-05-16 19:20:24 +020059
Harald Weltee2365962010-05-04 07:41:59 +020060#include "../../bscconfig.h"
Harald Welte9f75c352010-04-30 20:26:32 +020061
Harald Welte9f75c352010-04-30 20:26:32 +020062#define _GNU_SOURCE
63#include <getopt.h>
64
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010065void *tall_sgsn_ctx;
Harald Welte9f75c352010-04-30 20:26:32 +020066
Harald Welte9f75c352010-04-30 20:26:32 +020067const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080068 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010069 "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 +080070 "This is free software: you are free to change and redistribute it.\r\n"
71 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte9f75c352010-04-30 20:26:32 +020072
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +010073#define CONFIG_FILE_DEFAULT "osmo-gbproxy.cfg"
74#define CONFIG_FILE_LEGACY "osmo_gbproxy.cfg"
75
76static char *config_file = NULL;
Daniel Willmann3ab193c2018-06-01 07:21:08 +020077struct gbproxy_config *gbcfg;
Harald Welte2c869ef2010-08-25 19:43:54 +020078static int daemonize = 0;
Harald Welte9f75c352010-04-30 20:26:32 +020079
80/* Pointer to the SGSN peer */
81extern struct gbprox_peer *gbprox_peer_sgsn;
82
Pau Espin Pedrol826eaa32020-11-25 18:00:58 +010083static void signal_handler(int signum)
Harald Welte6df0c512010-05-12 18:40:01 +000084{
Pau Espin Pedrol826eaa32020-11-25 18:00:58 +010085 fprintf(stdout, "signal %u received\n", signum);
Harald Welte6df0c512010-05-12 18:40:01 +000086
Pau Espin Pedrol826eaa32020-11-25 18:00:58 +010087 switch (signum) {
Harald Welte6df0c512010-05-12 18:40:01 +000088 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +020089 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020090 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte6df0c512010-05-12 18:40:01 +000091 sleep(1);
92 exit(0);
93 break;
94 case SIGABRT:
Pau Espin Pedrol826eaa32020-11-25 18:00:58 +010095 /* in case of abort, we want to obtain a talloc report and
96 * then run default SIGABRT handler, who will generate coredump
97 * and abort the process. abort() should do this for us after we
98 * return, but program wouldn't exit if an external SIGABRT is
99 * received.
100 */
101 talloc_report(tall_vty_ctx, stderr);
102 talloc_report_full(tall_sgsn_ctx, stderr);
103 signal(SIGABRT, SIG_DFL);
104 raise(SIGABRT);
105 break;
Harald Welte6df0c512010-05-12 18:40:01 +0000106 case SIGUSR1:
107 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100108 talloc_report_full(tall_sgsn_ctx, stderr);
Harald Welte6df0c512010-05-12 18:40:01 +0000109 break;
110 case SIGUSR2:
111 talloc_report_full(tall_vty_ctx, stderr);
112 break;
113 default:
114 break;
115 }
116}
117
Harald Welte768f2872010-05-14 11:22:33 +0000118static void print_usage()
119{
120 printf("Usage: bsc_hack\n");
121}
122
123static void print_help()
124{
125 printf(" Some useful help...\n");
126 printf(" -h --help this text\n");
127 printf(" -d option --debug=DNS:DGPRS,0:0 enable debugging\n");
Harald Welte2c869ef2010-08-25 19:43:54 +0200128 printf(" -D --daemonize Fork the process into a background daemon\n");
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100129 printf(" -c --config-file filename The config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte768f2872010-05-14 11:22:33 +0000130 printf(" -s --disable-color\n");
131 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200132 printf(" -V --version. Print the version.\n");
Harald Welte768f2872010-05-14 11:22:33 +0000133 printf(" -e --log-level number. Set a global loglevel.\n");
134}
135
136static void handle_options(int argc, char **argv)
137{
138 while (1) {
139 int option_index = 0, c;
140 static struct option long_options[] = {
141 { "help", 0, 0, 'h' },
142 { "debug", 1, 0, 'd' },
Harald Welte2c869ef2010-08-25 19:43:54 +0200143 { "daemonize", 0, 0, 'D' },
Harald Welte768f2872010-05-14 11:22:33 +0000144 { "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
Harald Welte2c869ef2010-08-25 19:43:54 +0200152 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte768f2872010-05-14 11:22:33 +0000153 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':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200163 log_set_use_color(osmo_stderr_target, 0);
Harald Welte768f2872010-05-14 11:22:33 +0000164 break;
165 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200166 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte768f2872010-05-14 11:22:33 +0000167 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200168 case 'D':
169 daemonize = 1;
170 break;
Harald Welte768f2872010-05-14 11:22:33 +0000171 case 'c':
Holger Hans Peter Freytherabb34782015-01-26 10:26:36 +0100172 config_file = optarg;
Harald Welte768f2872010-05-14 11:22:33 +0000173 break;
174 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200175 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte768f2872010-05-14 11:22:33 +0000176 break;
177 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200178 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte768f2872010-05-14 11:22:33 +0000179 break;
180 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200181 print_version(1);
Harald Welte768f2872010-05-14 11:22:33 +0000182 exit(0);
183 break;
184 default:
185 break;
186 }
187 }
Harald Weltede67c002019-12-03 22:28:19 +0100188
189 if (argc > optind) {
190 fprintf(stderr, "Unsupported positional arguments on command line\n");
191 exit(2);
192 }
Harald Welte768f2872010-05-14 11:22:33 +0000193}
194
Harald Weltec31f4802010-05-25 23:31:39 +0200195static 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,
Harald Weltec31f4802010-05-25 23:31:39 +0200198};
199
Harald Welte11461a62012-06-17 12:58:46 +0800200/* default categories */
201static struct log_info_cat gprs_categories[] = {
202 [DGPRS] = {
203 .name = "DGPRS",
204 .description = "GPRS Packet Service",
205 .enabled = 1, .loglevel = LOGL_DEBUG,
206 },
207 [DNS] = {
208 .name = "DNS",
209 .description = "GPRS Network Service (NS)",
210 .enabled = 1, .loglevel = LOGL_INFO,
211 },
Harald Welte7cb76a42020-12-07 12:03:10 +0100212 [DOBJ] = {
213 .name = "DOBJ",
214 .description = "GbProxy object allocation/release",
215 .enabled = 1,
216 .color = "\033[38;5;121m"
217 },
Harald Welte11461a62012-06-17 12:58:46 +0800218};
219
220static const struct log_info gprs_log_info = {
221 .filter_fn = gprs_log_filter_fn,
222 .cat = gprs_categories,
223 .num_cat = ARRAY_SIZE(gprs_categories),
224};
225
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100226static bool file_exists(const char *path)
227{
228 struct stat sb;
229 return stat(path, &sb) ? false : true;
230}
231
Alexander Couzens82182d02020-09-22 13:21:46 +0200232int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg);
233
Harald Welte9f75c352010-04-30 20:26:32 +0200234int main(int argc, char **argv)
235{
Harald Welte9f75c352010-04-30 20:26:32 +0200236 int rc;
Daniel Willmannc977afa2018-06-01 07:21:20 +0200237 struct ctrl_handle *ctrl;
Harald Welte9f75c352010-04-30 20:26:32 +0200238
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100239 tall_sgsn_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
240 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
241 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte6df0c512010-05-12 18:40:01 +0000242
243 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200244 signal(SIGTERM, &signal_handler);
Harald Welte6df0c512010-05-12 18:40:01 +0000245 signal(SIGABRT, &signal_handler);
246 signal(SIGUSR1, &signal_handler);
247 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200248 osmo_init_ignore_signals();
Harald Welte9f75c352010-04-30 20:26:32 +0200249
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100250 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
Harald Welte9f75c352010-04-30 20:26:32 +0200251
Harald Weltec31f4802010-05-25 23:31:39 +0200252 vty_info.copyright = openbsc_copyright;
253 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200254 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100255 osmo_talloc_vty_add_cmds();
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200256 osmo_stats_vty_add_cmds();
Harald Welte7af49622010-05-19 14:04:23 +0200257 gbproxy_vty_init();
Harald Welte1353f962010-05-16 19:20:24 +0200258
Harald Welte768f2872010-05-14 11:22:33 +0000259 handle_options(argc, argv);
260
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100261 /* Backwards compatibility: for years, the default config file name was
262 * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a
263 * dash. To be able to use the new config file name without breaking
264 * previous setups that might rely on the legacy default config file
265 * name, we need to look for the old config file if no -c option was
266 * passed AND no file exists with the new default file name. */
267 if (!config_file) {
268 /* No -c option was passed */
269 if (file_exists(CONFIG_FILE_LEGACY)
270 && !file_exists(CONFIG_FILE_DEFAULT))
271 config_file = CONFIG_FILE_LEGACY;
272 else
273 config_file = CONFIG_FILE_DEFAULT;
274 }
275
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100276 rate_ctr_init(tall_sgsn_ctx);
277 osmo_stats_init(tall_sgsn_ctx);
Harald Welte9f75c352010-04-30 20:26:32 +0200278
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100279 gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config);
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200280 if (!gbcfg) {
281 LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
282 exit(1);
283 }
284 gbproxy_init_config(gbcfg);
Alexander Couzens82182d02020-09-22 13:21:46 +0200285 gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg);
286 if (!gbcfg->nsi) {
287 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
288 exit(1);
289 }
290
291 gprs_ns2_vty_init(gbcfg->nsi, NULL);
Harald Welte453a51d2020-12-10 15:18:50 +0100292 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp");
Alexander Couzens82182d02020-09-22 13:21:46 +0200293 gprs_ns2_dynamic_create_nse(gbcfg->nsi, true);
294
295 bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg);
Harald Welte1194b582010-05-12 15:55:23 +0000296
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200297 rc = gbproxy_parse_config(config_file, gbcfg);
Harald Welte1194b582010-05-12 15:55:23 +0000298 if (rc < 0) {
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100299 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file);
Harald Welte1194b582010-05-12 15:55:23 +0000300 exit(2);
301 }
302
Alexander Couzens82182d02020-09-22 13:21:46 +0200303 gprs_ns2_vty_create();
304
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100305 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100306 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100307 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
308 if (rc < 0)
309 exit(1);
310
Daniel Willmannc977afa2018-06-01 07:21:20 +0200311 /* Start control interface after getting config for
312 * ctrl_vty_get_bind_addr() */
313 ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL);
314 if (!ctrl) {
315 LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n");
316 exit(1);
317 }
318
319 if (gb_ctrl_cmds_install() != 0) {
320 LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n");
321 exit(1);
322 }
323
Harald Welte2c869ef2010-08-25 19:43:54 +0200324 if (daemonize) {
325 rc = osmo_daemonize();
326 if (rc < 0) {
327 perror("Error during daemonize");
328 exit(1);
329 }
330 }
331
Harald Welte9f75c352010-04-30 20:26:32 +0200332 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200333 rc = osmo_select_main(0);
Harald Welte9f75c352010-04-30 20:26:32 +0200334 if (rc < 0)
335 exit(3);
336 }
337
338 exit(0);
339}