Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1 | /* 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 Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 40 | #include <osmocom/core/signal.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 41 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 42 | #include <osmocom/gprs/gprs_ns2.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 43 | #include <osmocom/gprs/gprs_bssgp.h> |
| 44 | |
Oliver Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 45 | #include "debug.h" |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 46 | #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> |
Pau Espin Pedrol | 52461cf | 2021-03-03 16:05:10 +0100 | [diff] [blame] | 58 | #include <osmocom/vty/cpu_sched_vty.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 59 | |
Oliver Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 60 | #include "../bscconfig.h" |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 61 | |
| 62 | #define _GNU_SOURCE |
| 63 | #include <getopt.h> |
| 64 | |
| 65 | void *tall_sgsn_ctx; |
| 66 | |
| 67 | const 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 | |
| 76 | static char *config_file = NULL; |
| 77 | struct gbproxy_config *gbcfg; |
| 78 | static int daemonize = 0; |
| 79 | |
| 80 | /* Pointer to the SGSN peer */ |
| 81 | extern struct gbprox_peer *gbprox_peer_sgsn; |
| 82 | |
Pau Espin Pedrol | 821da08 | 2020-11-25 18:00:58 +0100 | [diff] [blame] | 83 | static void signal_handler(int signum) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 84 | { |
Pau Espin Pedrol | 821da08 | 2020-11-25 18:00:58 +0100 | [diff] [blame] | 85 | fprintf(stdout, "signal %u received\n", signum); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 86 | |
Pau Espin Pedrol | 821da08 | 2020-11-25 18:00:58 +0100 | [diff] [blame] | 87 | switch (signum) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 88 | 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 Pedrol | 821da08 | 2020-11-25 18:00:58 +0100 | [diff] [blame] | 95 | /* 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 Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 106 | 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 | |
| 118 | static void print_usage() |
| 119 | { |
Daniel Willmann | 349b78e | 2021-03-09 16:28:29 +0100 | [diff] [blame^] | 120 | printf("Usage: osmo-gbproxy\n"); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static 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"); |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 134 | |
| 135 | printf("\nVTY reference generation:\n"); |
| 136 | printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"); |
| 137 | printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n"); |
| 138 | } |
| 139 | |
| 140 | static void handle_long_options(const char *prog_name, const int long_option) |
| 141 | { |
| 142 | static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT; |
| 143 | |
| 144 | switch (long_option) { |
| 145 | case 1: |
| 146 | vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg); |
| 147 | if (vty_ref_mode < 0) { |
| 148 | fprintf(stderr, "%s: Unknown VTY reference generation " |
| 149 | "mode '%s'\n", prog_name, optarg); |
| 150 | exit(2); |
| 151 | } |
| 152 | break; |
| 153 | case 2: |
| 154 | fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n", |
| 155 | get_value_string(vty_ref_gen_mode_names, vty_ref_mode), |
| 156 | get_value_string(vty_ref_gen_mode_desc, vty_ref_mode)); |
| 157 | vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode); |
| 158 | exit(0); |
| 159 | default: |
| 160 | fprintf(stderr, "%s: error parsing cmdline options\n", prog_name); |
| 161 | exit(2); |
| 162 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void handle_options(int argc, char **argv) |
| 166 | { |
| 167 | while (1) { |
| 168 | int option_index = 0, c; |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 169 | static int long_option = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 170 | static struct option long_options[] = { |
| 171 | { "help", 0, 0, 'h' }, |
| 172 | { "debug", 1, 0, 'd' }, |
| 173 | { "daemonize", 0, 0, 'D' }, |
| 174 | { "config-file", 1, 0, 'c' }, |
| 175 | { "disable-color", 0, 0, 's' }, |
| 176 | { "timestamp", 0, 0, 'T' }, |
| 177 | { "version", 0, 0, 'V' }, |
| 178 | { "log-level", 1, 0, 'e' }, |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 179 | { "vty-ref-mode", 1, &long_option, 1 }, |
| 180 | { "vty-ref-xml", 0, &long_option, 2 }, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 181 | { 0, 0, 0, 0 } |
| 182 | }; |
| 183 | |
| 184 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
| 185 | long_options, &option_index); |
| 186 | if (c == -1) |
| 187 | break; |
| 188 | |
| 189 | switch (c) { |
| 190 | case 'h': |
| 191 | print_usage(); |
| 192 | print_help(); |
| 193 | exit(0); |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 194 | case 0: |
| 195 | handle_long_options(argv[0], long_option); |
| 196 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 197 | case 's': |
| 198 | log_set_use_color(osmo_stderr_target, 0); |
| 199 | break; |
| 200 | case 'd': |
| 201 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 202 | break; |
| 203 | case 'D': |
| 204 | daemonize = 1; |
| 205 | break; |
| 206 | case 'c': |
| 207 | config_file = optarg; |
| 208 | break; |
| 209 | case 'T': |
| 210 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 211 | break; |
| 212 | case 'e': |
| 213 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 214 | break; |
| 215 | case 'V': |
| 216 | print_version(1); |
| 217 | exit(0); |
| 218 | break; |
| 219 | default: |
| 220 | break; |
| 221 | } |
| 222 | } |
Harald Welte | 92726d9 | 2019-12-03 22:28:19 +0100 | [diff] [blame] | 223 | |
| 224 | if (argc > optind) { |
| 225 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 226 | exit(2); |
| 227 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 230 | static struct vty_app_info vty_info = { |
| 231 | .name = "OsmoGbProxy", |
| 232 | .version = PACKAGE_VERSION, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | /* default categories */ |
| 236 | static struct log_info_cat gprs_categories[] = { |
| 237 | [DGPRS] = { |
| 238 | .name = "DGPRS", |
| 239 | .description = "GPRS Packet Service", |
| 240 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 241 | }, |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 242 | [DOBJ] = { |
| 243 | .name = "DOBJ", |
| 244 | .description = "GbProxy object allocation/release", |
| 245 | .enabled = 1, |
| 246 | .color = "\033[38;5;121m" |
| 247 | }, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static const struct log_info gprs_log_info = { |
| 251 | .filter_fn = gprs_log_filter_fn, |
| 252 | .cat = gprs_categories, |
| 253 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 254 | }; |
| 255 | |
| 256 | static bool file_exists(const char *path) |
| 257 | { |
| 258 | struct stat sb; |
| 259 | return stat(path, &sb) ? false : true; |
| 260 | } |
| 261 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 262 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg); |
| 263 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 264 | int main(int argc, char **argv) |
| 265 | { |
| 266 | int rc; |
| 267 | struct ctrl_handle *ctrl; |
| 268 | |
Daniel Willmann | 349b78e | 2021-03-09 16:28:29 +0100 | [diff] [blame^] | 269 | tall_sgsn_ctx = talloc_named_const(NULL, 0, "gb_proxy"); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 270 | msgb_talloc_ctx_init(tall_sgsn_ctx, 0); |
| 271 | vty_info.tall_ctx = tall_sgsn_ctx; |
| 272 | |
| 273 | signal(SIGINT, &signal_handler); |
| 274 | signal(SIGTERM, &signal_handler); |
| 275 | signal(SIGABRT, &signal_handler); |
| 276 | signal(SIGUSR1, &signal_handler); |
| 277 | signal(SIGUSR2, &signal_handler); |
| 278 | osmo_init_ignore_signals(); |
| 279 | |
| 280 | osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info); |
| 281 | |
| 282 | vty_info.copyright = openbsc_copyright; |
| 283 | vty_init(&vty_info); |
| 284 | logging_vty_add_cmds(); |
| 285 | osmo_talloc_vty_add_cmds(); |
| 286 | osmo_stats_vty_add_cmds(); |
Harald Welte | d10afca | 2020-12-12 17:32:52 +0100 | [diff] [blame] | 287 | osmo_fsm_vty_add_cmds(); |
Pau Espin Pedrol | 52461cf | 2021-03-03 16:05:10 +0100 | [diff] [blame] | 288 | osmo_cpu_sched_vty_init(tall_sgsn_ctx); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 289 | gbproxy_vty_init(); |
| 290 | |
| 291 | handle_options(argc, argv); |
| 292 | |
| 293 | /* Backwards compatibility: for years, the default config file name was |
| 294 | * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a |
| 295 | * dash. To be able to use the new config file name without breaking |
| 296 | * previous setups that might rely on the legacy default config file |
| 297 | * name, we need to look for the old config file if no -c option was |
| 298 | * passed AND no file exists with the new default file name. */ |
| 299 | if (!config_file) { |
| 300 | /* No -c option was passed */ |
| 301 | if (file_exists(CONFIG_FILE_LEGACY) |
| 302 | && !file_exists(CONFIG_FILE_DEFAULT)) |
| 303 | config_file = CONFIG_FILE_LEGACY; |
| 304 | else |
| 305 | config_file = CONFIG_FILE_DEFAULT; |
| 306 | } |
| 307 | |
| 308 | rate_ctr_init(tall_sgsn_ctx); |
| 309 | osmo_stats_init(tall_sgsn_ctx); |
| 310 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 311 | gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config); |
| 312 | if (!gbcfg) { |
| 313 | LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n"); |
| 314 | exit(1); |
| 315 | } |
| 316 | gbproxy_init_config(gbcfg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 317 | gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg); |
| 318 | if (!gbcfg->nsi) { |
| 319 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 320 | exit(1); |
| 321 | } |
Alexander Couzens | b363e21 | 2021-01-27 21:01:42 +0100 | [diff] [blame] | 322 | gprs_ns2_vty_init(gbcfg->nsi); |
Harald Welte | 4e15cf4 | 2020-12-10 15:18:50 +0100 | [diff] [blame] | 323 | logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp"); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 324 | |
| 325 | bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 326 | |
| 327 | rc = gbproxy_parse_config(config_file, gbcfg); |
| 328 | if (rc < 0) { |
| 329 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file); |
| 330 | exit(2); |
| 331 | } |
| 332 | |
| 333 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 334 | rc = telnet_init_dynif(tall_sgsn_ctx, NULL, |
| 335 | vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY); |
| 336 | if (rc < 0) |
| 337 | exit(1); |
| 338 | |
| 339 | /* Start control interface after getting config for |
| 340 | * ctrl_vty_get_bind_addr() */ |
| 341 | ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL); |
| 342 | if (!ctrl) { |
| 343 | LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n"); |
| 344 | exit(1); |
| 345 | } |
| 346 | |
| 347 | if (gb_ctrl_cmds_install() != 0) { |
| 348 | LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n"); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 352 | if (daemonize) { |
| 353 | rc = osmo_daemonize(); |
| 354 | if (rc < 0) { |
| 355 | perror("Error during daemonize"); |
| 356 | exit(1); |
| 357 | } |
| 358 | } |
| 359 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 360 | while (1) { |
| 361 | rc = osmo_select_main(0); |
| 362 | if (rc < 0) |
| 363 | exit(3); |
| 364 | } |
| 365 | |
| 366 | exit(0); |
| 367 | } |