blob: 8c8398043431eb388d0f10bd8062c8d85a369e37 [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
41#include <osmocom/gprs/gprs_ns.h>
42#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
83/* call-back function for the NS protocol */
84static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
85 struct msgb *msg, uint16_t bvci)
86{
87 int rc = 0;
88
89 switch (event) {
90 case GPRS_NS_EVT_UNIT_DATA:
91 rc = gbprox_rcvmsg(gbcfg, msg, nsvc->nsei, bvci, nsvc->nsvci);
92 break;
93 default:
94 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
95 if (msg)
96 msgb_free(msg);
97 rc = -EIO;
98 break;
99 }
100 return rc;
101}
102
103static void signal_handler(int signal)
104{
105 fprintf(stdout, "signal %u received\n", signal);
106
107 switch (signal) {
108 case SIGINT:
109 case SIGTERM:
110 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
111 sleep(1);
112 exit(0);
113 break;
114 case SIGABRT:
115 /* in case of abort, we want to obtain a talloc report
116 * and then return to the caller, who will abort the process */
117 case SIGUSR1:
118 talloc_report(tall_vty_ctx, stderr);
119 talloc_report_full(tall_sgsn_ctx, stderr);
120 break;
121 case SIGUSR2:
122 talloc_report_full(tall_vty_ctx, stderr);
123 break;
124 default:
125 break;
126 }
127}
128
129static void print_usage()
130{
131 printf("Usage: bsc_hack\n");
132}
133
134static void print_help()
135{
136 printf(" Some useful help...\n");
137 printf(" -h --help this text\n");
138 printf(" -d option --debug=DNS:DGPRS,0:0 enable debugging\n");
139 printf(" -D --daemonize Fork the process into a background daemon\n");
140 printf(" -c --config-file filename The config file to use [%s]\n", CONFIG_FILE_DEFAULT);
141 printf(" -s --disable-color\n");
142 printf(" -T --timestamp Prefix every log line with a timestamp\n");
143 printf(" -V --version. Print the version.\n");
144 printf(" -e --log-level number. Set a global loglevel.\n");
145}
146
147static void handle_options(int argc, char **argv)
148{
149 while (1) {
150 int option_index = 0, c;
151 static struct option long_options[] = {
152 { "help", 0, 0, 'h' },
153 { "debug", 1, 0, 'd' },
154 { "daemonize", 0, 0, 'D' },
155 { "config-file", 1, 0, 'c' },
156 { "disable-color", 0, 0, 's' },
157 { "timestamp", 0, 0, 'T' },
158 { "version", 0, 0, 'V' },
159 { "log-level", 1, 0, 'e' },
160 { 0, 0, 0, 0 }
161 };
162
163 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
164 long_options, &option_index);
165 if (c == -1)
166 break;
167
168 switch (c) {
169 case 'h':
170 print_usage();
171 print_help();
172 exit(0);
173 case 's':
174 log_set_use_color(osmo_stderr_target, 0);
175 break;
176 case 'd':
177 log_parse_category_mask(osmo_stderr_target, optarg);
178 break;
179 case 'D':
180 daemonize = 1;
181 break;
182 case 'c':
183 config_file = optarg;
184 break;
185 case 'T':
186 log_set_print_timestamp(osmo_stderr_target, 1);
187 break;
188 case 'e':
189 log_set_log_level(osmo_stderr_target, atoi(optarg));
190 break;
191 case 'V':
192 print_version(1);
193 exit(0);
194 break;
195 default:
196 break;
197 }
198 }
Harald Welte92726d92019-12-03 22:28:19 +0100199
200 if (argc > optind) {
201 fprintf(stderr, "Unsupported positional arguments on command line\n");
202 exit(2);
203 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200204}
205
206int gbproxy_vty_is_config_node(struct vty *vty, int node)
207{
208 switch (node) {
209 /* add items that are not config */
210 case CONFIG_NODE:
211 return 0;
212
213 default:
214 return 1;
215 }
216}
217
218int gbproxy_vty_go_parent(struct vty *vty)
219{
220 switch (vty->node) {
221 case GBPROXY_NODE:
222 default:
223 if (gbproxy_vty_is_config_node(vty, vty->node))
224 vty->node = CONFIG_NODE;
225 else
226 vty->node = ENABLE_NODE;
227
228 vty->index = NULL;
229 }
230
231 return vty->node;
232}
233
234static struct vty_app_info vty_info = {
235 .name = "OsmoGbProxy",
236 .version = PACKAGE_VERSION,
237 .go_parent_cb = gbproxy_vty_go_parent,
238 .is_config_node = gbproxy_vty_is_config_node,
239};
240
241/* default categories */
242static struct log_info_cat gprs_categories[] = {
243 [DGPRS] = {
244 .name = "DGPRS",
245 .description = "GPRS Packet Service",
246 .enabled = 1, .loglevel = LOGL_DEBUG,
247 },
248 [DNS] = {
249 .name = "DNS",
250 .description = "GPRS Network Service (NS)",
251 .enabled = 1, .loglevel = LOGL_INFO,
252 },
253 [DBSSGP] = {
254 .name = "DBSSGP",
255 .description = "GPRS BSS Gateway Protocol (BSSGP)",
256 .enabled = 1, .loglevel = LOGL_DEBUG,
257 },
258};
259
260static const struct log_info gprs_log_info = {
261 .filter_fn = gprs_log_filter_fn,
262 .cat = gprs_categories,
263 .num_cat = ARRAY_SIZE(gprs_categories),
264};
265
266static bool file_exists(const char *path)
267{
268 struct stat sb;
269 return stat(path, &sb) ? false : true;
270}
271
272int main(int argc, char **argv)
273{
274 int rc;
275 struct ctrl_handle *ctrl;
276
277 tall_sgsn_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
278 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
279 vty_info.tall_ctx = tall_sgsn_ctx;
280
281 signal(SIGINT, &signal_handler);
282 signal(SIGTERM, &signal_handler);
283 signal(SIGABRT, &signal_handler);
284 signal(SIGUSR1, &signal_handler);
285 signal(SIGUSR2, &signal_handler);
286 osmo_init_ignore_signals();
287
288 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
289
290 vty_info.copyright = openbsc_copyright;
291 vty_init(&vty_info);
292 logging_vty_add_cmds();
293 osmo_talloc_vty_add_cmds();
294 osmo_stats_vty_add_cmds();
295 gbproxy_vty_init();
296
297 handle_options(argc, argv);
298
299 /* Backwards compatibility: for years, the default config file name was
300 * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a
301 * dash. To be able to use the new config file name without breaking
302 * previous setups that might rely on the legacy default config file
303 * name, we need to look for the old config file if no -c option was
304 * passed AND no file exists with the new default file name. */
305 if (!config_file) {
306 /* No -c option was passed */
307 if (file_exists(CONFIG_FILE_LEGACY)
308 && !file_exists(CONFIG_FILE_DEFAULT))
309 config_file = CONFIG_FILE_LEGACY;
310 else
311 config_file = CONFIG_FILE_DEFAULT;
312 }
313
314 rate_ctr_init(tall_sgsn_ctx);
315 osmo_stats_init(tall_sgsn_ctx);
316
317 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb, tall_sgsn_ctx);
318 if (!bssgp_nsi) {
319 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
320 exit(1);
321 }
322
323 gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config);
324 if (!gbcfg) {
325 LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
326 exit(1);
327 }
328 gbproxy_init_config(gbcfg);
329 gbcfg->nsi = bssgp_nsi;
330 gprs_ns_vty_init(bssgp_nsi);
331 gprs_ns_set_log_ss(DNS);
332 bssgp_set_log_ss(DBSSGP);
333 osmo_signal_register_handler(SS_L_NS, &gbprox_signal, gbcfg);
334
335 rc = gbproxy_parse_config(config_file, gbcfg);
336 if (rc < 0) {
337 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file);
338 exit(2);
339 }
340
341 /* start telnet after reading config for vty_get_bind_addr() */
342 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
343 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
344 if (rc < 0)
345 exit(1);
346
347 /* Start control interface after getting config for
348 * ctrl_vty_get_bind_addr() */
349 ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL);
350 if (!ctrl) {
351 LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n");
352 exit(1);
353 }
354
355 if (gb_ctrl_cmds_install() != 0) {
356 LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n");
357 exit(1);
358 }
359
360 if (!gprs_nsvc_by_nsei(gbcfg->nsi, gbcfg->nsip_sgsn_nsei)) {
361 LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u "
362 "without creating that NSEI before\n",
363 gbcfg->nsip_sgsn_nsei);
364 exit(2);
365 }
366
367 rc = gprs_ns_nsip_listen(bssgp_nsi);
368 if (rc < 0) {
369 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
370 exit(2);
371 }
372
373 rc = gprs_ns_frgre_listen(bssgp_nsi);
374 if (rc < 0) {
375 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
376 "socket. Do you have CAP_NET_RAW?\n");
377 exit(2);
378 }
379
380 if (daemonize) {
381 rc = osmo_daemonize();
382 if (rc < 0) {
383 perror("Error during daemonize");
384 exit(1);
385 }
386 }
387
388 /* Reset all the persistent NS-VCs that we've read from the config */
389 gbprox_reset_persistent_nsvcs(bssgp_nsi);
390
391 while (1) {
392 rc = osmo_select_main(0);
393 if (rc < 0)
394 exit(3);
395 }
396
397 exit(0);
398}