blob: e85e9515b4601318e9cccc611a64851ac5090d1c [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>
40
Alexander Couzens951e1332020-09-22 13:21:46 +020041#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020042#include <osmocom/gprs/gprs_bssgp.h>
43
44#include <osmocom/sgsn/signal.h>
45#include <osmocom/sgsn/debug.h>
46#include <osmocom/sgsn/vty.h>
47#include <osmocom/sgsn/gb_proxy.h>
48
49#include <osmocom/ctrl/control_vty.h>
50#include <osmocom/ctrl/control_if.h>
51#include <osmocom/ctrl/ports.h>
52
53#include <osmocom/vty/command.h>
54#include <osmocom/vty/telnet_interface.h>
55#include <osmocom/vty/logging.h>
56#include <osmocom/vty/stats.h>
57#include <osmocom/vty/ports.h>
58#include <osmocom/vty/misc.h>
59
60#include "../../bscconfig.h"
61
62#define _GNU_SOURCE
63#include <getopt.h>
64
65void *tall_sgsn_ctx;
66
67const char *openbsc_copyright =
68 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
69 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
70 "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";
72
73#define CONFIG_FILE_DEFAULT "osmo-gbproxy.cfg"
74#define CONFIG_FILE_LEGACY "osmo_gbproxy.cfg"
75
76static char *config_file = NULL;
77struct gbproxy_config *gbcfg;
78static int daemonize = 0;
79
80/* Pointer to the SGSN peer */
81extern struct gbprox_peer *gbprox_peer_sgsn;
82
Pau Espin Pedrol821da082020-11-25 18:00:58 +010083static void signal_handler(int signum)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020084{
Pau Espin Pedrol821da082020-11-25 18:00:58 +010085 fprintf(stdout, "signal %u received\n", signum);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020086
Pau Espin Pedrol821da082020-11-25 18:00:58 +010087 switch (signum) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020088 case SIGINT:
89 case SIGTERM:
90 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
91 sleep(1);
92 exit(0);
93 break;
94 case SIGABRT:
Pau Espin Pedrol821da082020-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;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200106 case SIGUSR1:
107 talloc_report(tall_vty_ctx, stderr);
108 talloc_report_full(tall_sgsn_ctx, stderr);
109 break;
110 case SIGUSR2:
111 talloc_report_full(tall_vty_ctx, stderr);
112 break;
113 default:
114 break;
115 }
116}
117
118static 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");
128 printf(" -D --daemonize Fork the process into a background daemon\n");
129 printf(" -c --config-file filename The config file to use [%s]\n", CONFIG_FILE_DEFAULT);
130 printf(" -s --disable-color\n");
131 printf(" -T --timestamp Prefix every log line with a timestamp\n");
132 printf(" -V --version. Print the version.\n");
133 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' },
143 { "daemonize", 0, 0, 'D' },
144 { "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
152 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
153 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':
163 log_set_use_color(osmo_stderr_target, 0);
164 break;
165 case 'd':
166 log_parse_category_mask(osmo_stderr_target, optarg);
167 break;
168 case 'D':
169 daemonize = 1;
170 break;
171 case 'c':
172 config_file = optarg;
173 break;
174 case 'T':
175 log_set_print_timestamp(osmo_stderr_target, 1);
176 break;
177 case 'e':
178 log_set_log_level(osmo_stderr_target, atoi(optarg));
179 break;
180 case 'V':
181 print_version(1);
182 exit(0);
183 break;
184 default:
185 break;
186 }
187 }
Harald Welte92726d92019-12-03 22:28:19 +0100188
189 if (argc > optind) {
190 fprintf(stderr, "Unsupported positional arguments on command line\n");
191 exit(2);
192 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200193}
194
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200195static struct vty_app_info vty_info = {
196 .name = "OsmoGbProxy",
197 .version = PACKAGE_VERSION,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200198};
199
200/* 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 Weltecfc7e8e2020-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 },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200218};
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
226static bool file_exists(const char *path)
227{
228 struct stat sb;
229 return stat(path, &sb) ? false : true;
230}
231
Alexander Couzens951e1332020-09-22 13:21:46 +0200232int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg);
233
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200234int main(int argc, char **argv)
235{
236 int rc;
237 struct ctrl_handle *ctrl;
238
239 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;
242
243 signal(SIGINT, &signal_handler);
244 signal(SIGTERM, &signal_handler);
245 signal(SIGABRT, &signal_handler);
246 signal(SIGUSR1, &signal_handler);
247 signal(SIGUSR2, &signal_handler);
248 osmo_init_ignore_signals();
249
250 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
251
252 vty_info.copyright = openbsc_copyright;
253 vty_init(&vty_info);
254 logging_vty_add_cmds();
255 osmo_talloc_vty_add_cmds();
256 osmo_stats_vty_add_cmds();
Harald Welted10afca2020-12-12 17:32:52 +0100257 osmo_fsm_vty_add_cmds();
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200258 gbproxy_vty_init();
259
260 handle_options(argc, argv);
261
262 /* Backwards compatibility: for years, the default config file name was
263 * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a
264 * dash. To be able to use the new config file name without breaking
265 * previous setups that might rely on the legacy default config file
266 * name, we need to look for the old config file if no -c option was
267 * passed AND no file exists with the new default file name. */
268 if (!config_file) {
269 /* No -c option was passed */
270 if (file_exists(CONFIG_FILE_LEGACY)
271 && !file_exists(CONFIG_FILE_DEFAULT))
272 config_file = CONFIG_FILE_LEGACY;
273 else
274 config_file = CONFIG_FILE_DEFAULT;
275 }
276
277 rate_ctr_init(tall_sgsn_ctx);
278 osmo_stats_init(tall_sgsn_ctx);
279
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200280 gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config);
281 if (!gbcfg) {
282 LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
283 exit(1);
284 }
285 gbproxy_init_config(gbcfg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200286 gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg);
287 if (!gbcfg->nsi) {
288 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
289 exit(1);
290 }
291
292 gprs_ns2_vty_init(gbcfg->nsi, NULL);
Harald Welte4e15cf42020-12-10 15:18:50 +0100293 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp");
Alexander Couzens951e1332020-09-22 13:21:46 +0200294 gprs_ns2_dynamic_create_nse(gbcfg->nsi, true);
295
296 bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200297
298 rc = gbproxy_parse_config(config_file, gbcfg);
299 if (rc < 0) {
300 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file);
301 exit(2);
302 }
303
Alexander Couzens951e1332020-09-22 13:21:46 +0200304 gprs_ns2_vty_create();
305
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200306 /* start telnet after reading config for vty_get_bind_addr() */
307 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
308 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
309 if (rc < 0)
310 exit(1);
311
312 /* Start control interface after getting config for
313 * ctrl_vty_get_bind_addr() */
314 ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL);
315 if (!ctrl) {
316 LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n");
317 exit(1);
318 }
319
320 if (gb_ctrl_cmds_install() != 0) {
321 LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n");
322 exit(1);
323 }
324
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200325 if (daemonize) {
326 rc = osmo_daemonize();
327 if (rc < 0) {
328 perror("Error during daemonize");
329 exit(1);
330 }
331 }
332
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200333 while (1) {
334 rc = osmo_select_main(0);
335 if (rc < 0)
336 exit(3);
337 }
338
339 exit(0);
340}