blob: 97e634206ab65c99b9783d86cabfcd73cd23dfc0 [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
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
65void *tall_bsc_ctx;
66
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
83/* call-back function for the NS protocol */
84static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 struct msgb *msg, uint16_t bvci)
Harald Welte9f75c352010-04-30 20:26:32 +020086{
87 int rc = 0;
88
89 switch (event) {
90 case GPRS_NS_EVT_UNIT_DATA:
Daniel Willmann3ab193c2018-06-01 07:21:08 +020091 rc = gbprox_rcvmsg(gbcfg, msg, nsvc->nsei, bvci, nsvc->nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +020092 break;
93 default:
94 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
95 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010096 msgb_free(msg);
Harald Welte9f75c352010-04-30 20:26:32 +020097 rc = -EIO;
98 break;
99 }
100 return rc;
101}
102
Harald Welte6df0c512010-05-12 18:40:01 +0000103static void signal_handler(int signal)
104{
105 fprintf(stdout, "signal %u received\n", signal);
106
107 switch (signal) {
108 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200109 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200110 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte6df0c512010-05-12 18:40:01 +0000111 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_bsc_ctx, stderr);
120 break;
121 case SIGUSR2:
122 talloc_report_full(tall_vty_ctx, stderr);
123 break;
124 default:
125 break;
126 }
127}
128
Harald Welte768f2872010-05-14 11:22:33 +0000129static 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");
Harald Welte2c869ef2010-08-25 19:43:54 +0200139 printf(" -D --daemonize Fork the process into a background daemon\n");
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100140 printf(" -c --config-file filename The config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte768f2872010-05-14 11:22:33 +0000141 printf(" -s --disable-color\n");
142 printf(" -T --timestamp Prefix every log line with a timestamp\n");
143 printf(" -V --version. Print the version of OpenBSC.\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' },
Harald Welte2c869ef2010-08-25 19:43:54 +0200154 { "daemonize", 0, 0, 'D' },
Harald Welte768f2872010-05-14 11:22:33 +0000155 { "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
Harald Welte2c869ef2010-08-25 19:43:54 +0200163 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte768f2872010-05-14 11:22:33 +0000164 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':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200174 log_set_use_color(osmo_stderr_target, 0);
Harald Welte768f2872010-05-14 11:22:33 +0000175 break;
176 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200177 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte768f2872010-05-14 11:22:33 +0000178 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200179 case 'D':
180 daemonize = 1;
181 break;
Harald Welte768f2872010-05-14 11:22:33 +0000182 case 'c':
Holger Hans Peter Freytherabb34782015-01-26 10:26:36 +0100183 config_file = optarg;
Harald Welte768f2872010-05-14 11:22:33 +0000184 break;
185 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200186 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte768f2872010-05-14 11:22:33 +0000187 break;
188 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200189 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte768f2872010-05-14 11:22:33 +0000190 break;
191 case 'V':
Harald Welte1353f962010-05-16 19:20:24 +0200192 print_version(1);
Harald Welte768f2872010-05-14 11:22:33 +0000193 exit(0);
194 break;
195 default:
196 break;
197 }
198 }
199}
200
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200201int gbproxy_vty_is_config_node(struct vty *vty, int node)
202{
203 switch (node) {
204 /* add items that are not config */
205 case CONFIG_NODE:
206 return 0;
207
208 default:
209 return 1;
210 }
211}
212
213int gbproxy_vty_go_parent(struct vty *vty)
214{
215 switch (vty->node) {
216 case GBPROXY_NODE:
217 default:
218 if (gbproxy_vty_is_config_node(vty, vty->node))
219 vty->node = CONFIG_NODE;
220 else
221 vty->node = ENABLE_NODE;
222
223 vty->index = NULL;
224 }
225
226 return vty->node;
227}
Harald Weltec31f4802010-05-25 23:31:39 +0200228
229static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100230 .name = "OsmoGbProxy",
Harald Weltec31f4802010-05-25 23:31:39 +0200231 .version = PACKAGE_VERSION,
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200232 .go_parent_cb = gbproxy_vty_go_parent,
233 .is_config_node = gbproxy_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200234};
235
Harald Welte11461a62012-06-17 12:58:46 +0800236/* default categories */
237static struct log_info_cat gprs_categories[] = {
238 [DGPRS] = {
239 .name = "DGPRS",
240 .description = "GPRS Packet Service",
241 .enabled = 1, .loglevel = LOGL_DEBUG,
242 },
243 [DNS] = {
244 .name = "DNS",
245 .description = "GPRS Network Service (NS)",
246 .enabled = 1, .loglevel = LOGL_INFO,
247 },
248 [DBSSGP] = {
249 .name = "DBSSGP",
250 .description = "GPRS BSS Gateway Protocol (BSSGP)",
251 .enabled = 1, .loglevel = LOGL_DEBUG,
252 },
253};
254
255static const struct log_info gprs_log_info = {
256 .filter_fn = gprs_log_filter_fn,
257 .cat = gprs_categories,
258 .num_cat = ARRAY_SIZE(gprs_categories),
259};
260
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100261static bool file_exists(const char *path)
262{
263 struct stat sb;
264 return stat(path, &sb) ? false : true;
265}
266
Harald Welte9f75c352010-04-30 20:26:32 +0200267int main(int argc, char **argv)
268{
Harald Welte9f75c352010-04-30 20:26:32 +0200269 int rc;
Daniel Willmannc977afa2018-06-01 07:21:20 +0200270 struct ctrl_handle *ctrl;
Harald Welte9f75c352010-04-30 20:26:32 +0200271
272 tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy");
Neels Hofmeyr39ae17f2016-09-16 01:49:08 +0200273 msgb_talloc_ctx_init(tall_bsc_ctx, 0);
Harald Welte731930b2018-02-14 01:02:22 +0100274 vty_info.tall_ctx = tall_bsc_ctx;
Harald Welte6df0c512010-05-12 18:40:01 +0000275
276 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200277 signal(SIGTERM, &signal_handler);
Harald Welte6df0c512010-05-12 18:40:01 +0000278 signal(SIGABRT, &signal_handler);
279 signal(SIGUSR1, &signal_handler);
280 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200281 osmo_init_ignore_signals();
Harald Welte9f75c352010-04-30 20:26:32 +0200282
Neels Hofmeyr5bd340e2018-04-16 00:57:10 +0200283 osmo_init_logging2(tall_bsc_ctx, &gprs_log_info);
Harald Welte9f75c352010-04-30 20:26:32 +0200284
Harald Weltec31f4802010-05-25 23:31:39 +0200285 vty_info.copyright = openbsc_copyright;
286 vty_init(&vty_info);
Maxdb0e3802017-01-12 19:35:11 +0100287 logging_vty_add_cmds(NULL);
Harald Welte731930b2018-02-14 01:02:22 +0100288 osmo_talloc_vty_add_cmds();
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100289 osmo_stats_vty_add_cmds(&gprs_log_info);
Harald Welte7af49622010-05-19 14:04:23 +0200290 gbproxy_vty_init();
Harald Welte1353f962010-05-16 19:20:24 +0200291
Harald Welte768f2872010-05-14 11:22:33 +0000292 handle_options(argc, argv);
293
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100294 /* Backwards compatibility: for years, the default config file name was
295 * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a
296 * dash. To be able to use the new config file name without breaking
297 * previous setups that might rely on the legacy default config file
298 * name, we need to look for the old config file if no -c option was
299 * passed AND no file exists with the new default file name. */
300 if (!config_file) {
301 /* No -c option was passed */
302 if (file_exists(CONFIG_FILE_LEGACY)
303 && !file_exists(CONFIG_FILE_DEFAULT))
304 config_file = CONFIG_FILE_LEGACY;
305 else
306 config_file = CONFIG_FILE_DEFAULT;
307 }
308
Harald Weltef2b4cd72010-05-13 11:45:07 +0200309 rate_ctr_init(tall_bsc_ctx);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100310 osmo_stats_init(tall_bsc_ctx);
Harald Welte9f75c352010-04-30 20:26:32 +0200311
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800312 bssgp_nsi = gprs_ns_instantiate(&proxy_ns_cb, tall_bsc_ctx);
Harald Welte0a4050c2010-05-11 10:01:17 +0200313 if (!bssgp_nsi) {
Harald Welte9f75c352010-04-30 20:26:32 +0200314 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
315 exit(1);
316 }
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200317
318 gbcfg = talloc_zero(tall_bsc_ctx, struct gbproxy_config);
319 if (!gbcfg) {
320 LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n");
321 exit(1);
322 }
323 gbproxy_init_config(gbcfg);
324 gbcfg->nsi = bssgp_nsi;
Harald Welte1194b582010-05-12 15:55:23 +0000325 gprs_ns_vty_init(bssgp_nsi);
Harald Welte68d85d52012-06-16 17:45:59 +0800326 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800327 bssgp_set_log_ss(DBSSGP);
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200328 osmo_signal_register_handler(SS_L_NS, &gbprox_signal, gbcfg);
Harald Welte1194b582010-05-12 15:55:23 +0000329
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200330 rc = gbproxy_parse_config(config_file, gbcfg);
Harald Welte1194b582010-05-12 15:55:23 +0000331 if (rc < 0) {
Neels Hofmeyr5343a8c2018-03-15 18:52:06 +0100332 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file);
Harald Welte1194b582010-05-12 15:55:23 +0000333 exit(2);
334 }
335
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100336 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200337 rc = telnet_init_dynif(tall_bsc_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100338 vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);
339 if (rc < 0)
340 exit(1);
341
Daniel Willmannc977afa2018-06-01 07:21:20 +0200342 /* Start control interface after getting config for
343 * ctrl_vty_get_bind_addr() */
344 ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL);
345 if (!ctrl) {
346 LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n");
347 exit(1);
348 }
349
350 if (gb_ctrl_cmds_install() != 0) {
351 LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n");
352 exit(1);
353 }
354
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200355 if (!gprs_nsvc_by_nsei(gbcfg->nsi, gbcfg->nsip_sgsn_nsei)) {
Harald Welte7af49622010-05-19 14:04:23 +0200356 LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSEI %u "
357 "without creating that NSEI before\n",
Daniel Willmann3ab193c2018-06-01 07:21:08 +0200358 gbcfg->nsip_sgsn_nsei);
Harald Welte7af49622010-05-19 14:04:23 +0200359 exit(2);
360 }
Harald Welte9f75c352010-04-30 20:26:32 +0200361
Harald Welteff3bde82010-05-19 15:09:09 +0200362 rc = gprs_ns_nsip_listen(bssgp_nsi);
Harald Welte7af49622010-05-19 14:04:23 +0200363 if (rc < 0) {
364 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
365 exit(2);
366 }
Harald Welte1ccbf442010-05-14 11:53:08 +0000367
Harald Welteff3bde82010-05-19 15:09:09 +0200368 rc = gprs_ns_frgre_listen(bssgp_nsi);
369 if (rc < 0) {
370 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
371 "socket. Do you have CAP_NET_RAW?\n");
372 exit(2);
Harald Welte5540c4c2010-05-19 14:38:50 +0200373 }
374
Harald Welte2c869ef2010-08-25 19:43:54 +0200375 if (daemonize) {
376 rc = osmo_daemonize();
377 if (rc < 0) {
378 perror("Error during daemonize");
379 exit(1);
380 }
381 }
382
Harald Welte1ccbf442010-05-14 11:53:08 +0000383 /* Reset all the persistent NS-VCs that we've read from the config */
384 gbprox_reset_persistent_nsvcs(bssgp_nsi);
385
Harald Welte9f75c352010-04-30 20:26:32 +0200386 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200387 rc = osmo_select_main(0);
Harald Welte9f75c352010-04-30 20:26:32 +0200388 if (rc < 0)
389 exit(3);
390 }
391
392 exit(0);
393}