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> |
| 40 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 41 | #include <osmocom/gprs/gprs_ns2.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 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 | |
| 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 | { |
| 120 | printf("Usage: bsc_hack\n"); |
| 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"); |
| 134 | } |
| 135 | |
| 136 | static 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 Welte | 92726d9 | 2019-12-03 22:28:19 +0100 | [diff] [blame] | 188 | |
| 189 | if (argc > optind) { |
| 190 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 191 | exit(2); |
| 192 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 195 | static struct vty_app_info vty_info = { |
| 196 | .name = "OsmoGbProxy", |
| 197 | .version = PACKAGE_VERSION, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | /* default categories */ |
| 201 | static 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 | }, |
| 212 | [DBSSGP] = { |
| 213 | .name = "DBSSGP", |
| 214 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
| 215 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 216 | }, |
| 217 | }; |
| 218 | |
| 219 | static const struct log_info gprs_log_info = { |
| 220 | .filter_fn = gprs_log_filter_fn, |
| 221 | .cat = gprs_categories, |
| 222 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 223 | }; |
| 224 | |
| 225 | static bool file_exists(const char *path) |
| 226 | { |
| 227 | struct stat sb; |
| 228 | return stat(path, &sb) ? false : true; |
| 229 | } |
| 230 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 231 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg); |
| 232 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 233 | int main(int argc, char **argv) |
| 234 | { |
| 235 | int rc; |
| 236 | struct ctrl_handle *ctrl; |
| 237 | |
| 238 | tall_sgsn_ctx = talloc_named_const(NULL, 0, "nsip_proxy"); |
| 239 | msgb_talloc_ctx_init(tall_sgsn_ctx, 0); |
| 240 | vty_info.tall_ctx = tall_sgsn_ctx; |
| 241 | |
| 242 | signal(SIGINT, &signal_handler); |
| 243 | signal(SIGTERM, &signal_handler); |
| 244 | signal(SIGABRT, &signal_handler); |
| 245 | signal(SIGUSR1, &signal_handler); |
| 246 | signal(SIGUSR2, &signal_handler); |
| 247 | osmo_init_ignore_signals(); |
| 248 | |
| 249 | osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info); |
| 250 | |
| 251 | vty_info.copyright = openbsc_copyright; |
| 252 | vty_init(&vty_info); |
| 253 | logging_vty_add_cmds(); |
| 254 | osmo_talloc_vty_add_cmds(); |
| 255 | osmo_stats_vty_add_cmds(); |
| 256 | gbproxy_vty_init(); |
| 257 | |
| 258 | handle_options(argc, argv); |
| 259 | |
| 260 | /* Backwards compatibility: for years, the default config file name was |
| 261 | * osmo_gbproxy.cfg. All other Osmocom programs use osmo-*.cfg with a |
| 262 | * dash. To be able to use the new config file name without breaking |
| 263 | * previous setups that might rely on the legacy default config file |
| 264 | * name, we need to look for the old config file if no -c option was |
| 265 | * passed AND no file exists with the new default file name. */ |
| 266 | if (!config_file) { |
| 267 | /* No -c option was passed */ |
| 268 | if (file_exists(CONFIG_FILE_LEGACY) |
| 269 | && !file_exists(CONFIG_FILE_DEFAULT)) |
| 270 | config_file = CONFIG_FILE_LEGACY; |
| 271 | else |
| 272 | config_file = CONFIG_FILE_DEFAULT; |
| 273 | } |
| 274 | |
| 275 | rate_ctr_init(tall_sgsn_ctx); |
| 276 | osmo_stats_init(tall_sgsn_ctx); |
| 277 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 278 | gbcfg = talloc_zero(tall_sgsn_ctx, struct gbproxy_config); |
| 279 | if (!gbcfg) { |
| 280 | LOGP(DGPRS, LOGL_FATAL, "Unable to allocate config\n"); |
| 281 | exit(1); |
| 282 | } |
| 283 | gbproxy_init_config(gbcfg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 284 | gbcfg->nsi = gprs_ns2_instantiate(tall_sgsn_ctx, gprs_ns2_prim_cb, gbcfg); |
| 285 | if (!gbcfg->nsi) { |
| 286 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 287 | exit(1); |
| 288 | } |
| 289 | |
| 290 | gprs_ns2_vty_init(gbcfg->nsi, NULL); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 291 | bssgp_set_log_ss(DBSSGP); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 292 | gprs_ns2_dynamic_create_nse(gbcfg->nsi, true); |
| 293 | |
| 294 | bssgp_set_bssgp_callback(gbprox_bssgp_send_cb, gbcfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 295 | |
| 296 | rc = gbproxy_parse_config(config_file, gbcfg); |
| 297 | if (rc < 0) { |
| 298 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file '%s'\n", config_file); |
| 299 | exit(2); |
| 300 | } |
| 301 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 302 | gprs_ns2_vty_create(); |
| 303 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 304 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 305 | rc = telnet_init_dynif(tall_sgsn_ctx, NULL, |
| 306 | vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY); |
| 307 | if (rc < 0) |
| 308 | exit(1); |
| 309 | |
| 310 | /* Start control interface after getting config for |
| 311 | * ctrl_vty_get_bind_addr() */ |
| 312 | ctrl = ctrl_interface_setup_dynip(gbcfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_GBPROXY, NULL); |
| 313 | if (!ctrl) { |
| 314 | LOGP(DGPRS, LOGL_FATAL, "Failed to create CTRL interface.\n"); |
| 315 | exit(1); |
| 316 | } |
| 317 | |
| 318 | if (gb_ctrl_cmds_install() != 0) { |
| 319 | LOGP(DGPRS, LOGL_FATAL, "Failed to install CTRL commands.\n"); |
| 320 | exit(1); |
| 321 | } |
| 322 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 323 | if (!gprs_ns2_nse_by_nsei(gbcfg->nsi, gbcfg->nsip_sgsn_nsei)) { |
Daniel Willmann | 3696dce | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 324 | LOGP(DGPRS, LOGL_FATAL, "You cannot proxy to NSE(%05u) " |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 325 | "without creating that NSEI before\n", |
| 326 | gbcfg->nsip_sgsn_nsei); |
| 327 | exit(2); |
| 328 | } |
| 329 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 330 | if (daemonize) { |
| 331 | rc = osmo_daemonize(); |
| 332 | if (rc < 0) { |
| 333 | perror("Error during daemonize"); |
| 334 | exit(1); |
| 335 | } |
| 336 | } |
| 337 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 338 | while (1) { |
| 339 | rc = osmo_select_main(0); |
| 340 | if (rc < 0) |
| 341 | exit(3); |
| 342 | } |
| 343 | |
| 344 | exit(0); |
| 345 | } |