blob: 3b5a1a89a2cc4fccef2744278e942e3f00d3d406 [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * 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/>.
19 *
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>
28#include <signal.h>
29#include <sys/fcntl.h>
30#include <sys/stat.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
35#include <osmocom/core/application.h>
36#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/rate_ctr.h>
39#include <osmocom/core/stats.h>
Oliver Smith29532c22021-01-29 11:13:00 +010040#include <osmocom/core/signal.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020041
Alexander Couzens951e1332020-09-22 13:21:46 +020042#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020043#include <osmocom/gprs/gprs_bssgp.h>
44
Oliver Smith29532c22021-01-29 11:13:00 +010045#include "debug.h"
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020046#include <osmocom/sgsn/gb_proxy.h>
47
48#include <osmocom/ctrl/control_vty.h>
49#include <osmocom/ctrl/control_if.h>
50#include <osmocom/ctrl/ports.h>
51
52#include <osmocom/vty/command.h>
53#include <osmocom/vty/telnet_interface.h>
54#include <osmocom/vty/logging.h>
55#include <osmocom/vty/stats.h>
56#include <osmocom/vty/ports.h>
57#include <osmocom/vty/misc.h>
58
Oliver Smith29532c22021-01-29 11:13:00 +010059#include "../bscconfig.h"
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020060
61#define _GNU_SOURCE
62#include <getopt.h>
63
64void *tall_sgsn_ctx;
65
66const char *openbsc_copyright =
67 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
68 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
69 "This is free software: you are free to change and redistribute it.\r\n"
70 "There is NO WARRANTY, to the extent permitted by law.\r\n";
71
72#define CONFIG_FILE_DEFAULT "osmo-gbproxy.cfg"
73#define CONFIG_FILE_LEGACY "osmo_gbproxy.cfg"
74
75static char *config_file = NULL;
76struct gbproxy_config *gbcfg;
77static int daemonize = 0;
78
79/* Pointer to the SGSN peer */
80extern struct gbprox_peer *gbprox_peer_sgsn;
81
Pau Espin Pedrol821da082020-11-25 18:00:58 +010082static void signal_handler(int signum)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020083{
Pau Espin Pedrol821da082020-11-25 18:00:58 +010084 fprintf(stdout, "signal %u received\n", signum);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020085
Pau Espin Pedrol821da082020-11-25 18:00:58 +010086 switch (signum) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020087 case SIGINT:
88 case SIGTERM:
89 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
90 sleep(1);
91 exit(0);
92 break;
93 case SIGABRT:
Pau Espin Pedrol821da082020-11-25 18:00:58 +010094 /* in case of abort, we want to obtain a talloc report and
95 * then run default SIGABRT handler, who will generate coredump
96 * and abort the process. abort() should do this for us after we
97 * return, but program wouldn't exit if an external SIGABRT is
98 * received.
99 */
100 talloc_report(tall_vty_ctx, stderr);
101 talloc_report_full(tall_sgsn_ctx, stderr);
102 signal(SIGABRT, SIG_DFL);
103 raise(SIGABRT);
104 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200105 case SIGUSR1:
106 talloc_report(tall_vty_ctx, stderr);
107 talloc_report_full(tall_sgsn_ctx, stderr);
108 break;
109 case SIGUSR2:
110 talloc_report_full(tall_vty_ctx, stderr);
111 break;
112 default:
113 break;
114 }
115}
116
117static void print_usage()
118{
119 printf("Usage: bsc_hack\n");
120}
121
122static void print_help()
123{
124 printf(" Some useful help...\n");
125 printf(" -h --help this text\n");
126 printf(" -d option --debug=DNS:DGPRS,0:0 enable debugging\n");
127 printf(" -D --daemonize Fork the process into a background daemon\n");
128 printf(" -c --config-file filename The config file to use [%s]\n", CONFIG_FILE_DEFAULT);
129 printf(" -s --disable-color\n");
130 printf(" -T --timestamp Prefix every log line with a timestamp\n");
131 printf(" -V --version. Print the version.\n");
132 printf(" -e --log-level number. Set a global loglevel.\n");
Harald Welte936dfd72021-01-31 19:36:06 +0100133
134 printf("\nVTY reference generation:\n");
135 printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n");
136 printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
137}
138
139static void handle_long_options(const char *prog_name, const int long_option)
140{
141 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
142
143 switch (long_option) {
144 case 1:
145 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
146 if (vty_ref_mode < 0) {
147 fprintf(stderr, "%s: Unknown VTY reference generation "
148 "mode '%s'\n", prog_name, optarg);
149 exit(2);
150 }
151 break;
152 case 2:
153 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
154 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
155 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
156 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
157 exit(0);
158 default:
159 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
160 exit(2);
161 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200162}
163
164static void handle_options(int argc, char **argv)
165{
166 while (1) {
167 int option_index = 0, c;
Harald Welte936dfd72021-01-31 19:36:06 +0100168 static int long_option = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200169 static struct option long_options[] = {
170 { "help", 0, 0, 'h' },
171 { "debug", 1, 0, 'd' },
172 { "daemonize", 0, 0, 'D' },
173 { "config-file", 1, 0, 'c' },
174 { "disable-color", 0, 0, 's' },
175 { "timestamp", 0, 0, 'T' },
176 { "version", 0, 0, 'V' },
177 { "log-level", 1, 0, 'e' },
Harald Welte936dfd72021-01-31 19:36:06 +0100178 { "vty-ref-mode", 1, &long_option, 1 },
179 { "vty-ref-xml", 0, &long_option, 2 },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200180 { 0, 0, 0, 0 }
181 };
182
183 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
184 long_options, &option_index);
185 if (c == -1)
186 break;
187
188 switch (c) {
189 case 'h':
190 print_usage();
191 print_help();
192 exit(0);
Harald Welte936dfd72021-01-31 19:36:06 +0100193 case 0:
194 handle_long_options(argv[0], long_option);
195 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200196 case 's':
197 log_set_use_color(osmo_stderr_target, 0);
198 break;
199 case 'd':
200 log_parse_category_mask(osmo_stderr_target, optarg);
201 break;
202 case 'D':
203 daemonize = 1;
204 break;
205 case 'c':
206 config_file = optarg;
207 break;
208 case 'T':
209 log_set_print_timestamp(osmo_stderr_target, 1);
210 break;
211 case 'e':
212 log_set_log_level(osmo_stderr_target, atoi(optarg));
213 break;
214 case 'V':
215 print_version(1);
216 exit(0);
217 break;
218 default:
219 break;
220 }
221 }
Harald Welte92726d92019-12-03 22:28:19 +0100222
223 if (argc > optind) {
224 fprintf(stderr, "Unsupported positional arguments on command line\n");
225 exit(2);
226 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200227}
228
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200229static struct vty_app_info vty_info = {
230 .name = "OsmoGbProxy",
231 .version = PACKAGE_VERSION,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200232};
233
234/* default categories */
235static struct log_info_cat gprs_categories[] = {
236 [DGPRS] = {
237 .name = "DGPRS",
238 .description = "GPRS Packet Service",
239 .enabled = 1, .loglevel = LOGL_DEBUG,
240 },
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100241 [DOBJ] = {
242 .name = "DOBJ",
243 .description = "GbProxy object allocation/release",
244 .enabled = 1,
245 .color = "\033[38;5;121m"
246 },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200247};
248
249static const struct log_info gprs_log_info = {
250 .filter_fn = gprs_log_filter_fn,
251 .cat = gprs_categories,
252 .num_cat = ARRAY_SIZE(gprs_categories),
253};
254
255static bool file_exists(const char *path)
256{
257 struct stat sb;
258 return stat(path, &sb) ? false : true;
259}
260
Alexander Couzens951e1332020-09-22 13:21:46 +0200261int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg);
262
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200263int main(int argc, char **argv)
264{
265 int rc;
266 struct ctrl_handle *ctrl;
267
268 tall_sgsn_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
269 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
270 vty_info.tall_ctx = tall_sgsn_ctx;
271
272 signal(SIGINT, &signal_handler);
273 signal(SIGTERM, &signal_handler);
274 signal(SIGABRT, &signal_handler);
275 signal(SIGUSR1, &signal_handler);
276 signal(SIGUSR2, &signal_handler);
277 osmo_init_ignore_signals();
278
279 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
280
281 vty_info.copyright = openbsc_copyright;
282 vty_init(&vty_info);
Daniel Willmanne64c92c2021-04-09 10:45:08 +0200283 ctrl_vty_init(tall_sgsn_ctx);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200284 logging_vty_add_cmds();
285 osmo_talloc_vty_add_cmds();
286 osmo_stats_vty_add_cmds();
Harald Welted10afca2020-12-12 17:32:52 +0100287 osmo_fsm_vty_add_cmds();
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200288 gbproxy_vty_init();
289
290 handle_options(argc, argv);
291
292 /* Backwards compatibility: for years, the default config file name was
293 * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a
294 * dash. To be able to use the new config file name without breaking
295 * previous setups that might rely on the legacy default config file
296 * name, we need to look for the old config file if no -c option was
297 * passed AND no file exists with the new default file name. */
298 if (!config_file) {
299 /* No -c option was passed */
300 if (file_exists(CONFIG_FILE_LEGACY)
301 && !file_exists(CONFIG_FILE_DEFAULT))
302 config_file = CONFIG_FILE_LEGACY;
303 else
304 config_file = CONFIG_FILE_DEFAULT;
305 }
306
307 rate_ctr_init(tall_sgsn_ctx);
308 osmo_stats_init(tall_sgsn_ctx);
309
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200310 gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config);
311 if (!gbcfg) {
312 LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
313 exit(1);
314 }
315 gbproxy_init_config(gbcfg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200316 gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg);
317 if (!gbcfg->nsi) {
318 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
319 exit(1);
320 }
Alexander Couzensb363e212021-01-27 21:01:42 +0100321 gprs_ns2_vty_init(gbcfg->nsi);
Harald Welte4e15cf42020-12-10 15:18:50 +0100322 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp");
Alexander Couzens951e1332020-09-22 13:21:46 +0200323
324 bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200325
326 rc = gbproxy_parse_config(config_file, gbcfg);
327 if (rc < 0) {
328 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file);
329 exit(2);
330 }
331
332 /* start telnet after reading config for vty_get_bind_addr() */
333 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
334 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
335 if (rc < 0)
336 exit(1);
337
338 /* Start control interface after getting config for
339 * ctrl_vty_get_bind_addr() */
340 ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL);
341 if (!ctrl) {
342 LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n");
343 exit(1);
344 }
345
346 if (gb_ctrl_cmds_install() != 0) {
347 LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n");
348 exit(1);
349 }
350
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200351 if (daemonize) {
352 rc = osmo_daemonize();
353 if (rc < 0) {
354 perror("Error during daemonize");
355 exit(1);
356 }
357 }
358
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200359 while (1) {
360 rc = osmo_select_main(0);
361 if (rc < 0)
362 exit(3);
363 }
364
365 exit(0);
366}