Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 1 | /* GPRS SGSN Implementation */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/stat.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 36 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/select.h> |
| 39 | #include <osmocom/core/rate_ctr.h> |
| 40 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 41 | #include <osmocom/core/stats.h> |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 42 | #include <osmocom/core/sockaddr_str.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 43 | |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 44 | #include <osmocom/gprs/gprs_ns2.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 45 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 9d16b14 | 2020-06-26 15:55:21 +0200 | [diff] [blame] | 46 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 47 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 48 | #include <osmocom/vty/telnet_interface.h> |
Pablo Neira Ayuso | 3848770 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 49 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 50 | #include <osmocom/vty/stats.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 51 | #include <osmocom/vty/ports.h> |
Harald Welte | 731930b | 2018-02-14 01:02:22 +0100 | [diff] [blame] | 52 | #include <osmocom/vty/misc.h> |
Pau Espin Pedrol | aae7daf | 2020-08-18 13:50:00 +0200 | [diff] [blame] | 53 | #include <osmocom/vty/cpu_sched_vty.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 54 | |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 55 | #include <osmocom/ctrl/control_vty.h> |
| 56 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 57 | #include <osmocom/sgsn/signal.h> |
| 58 | #include <osmocom/sgsn/debug.h> |
| 59 | #include <osmocom/sgsn/vty.h> |
| 60 | #include <osmocom/sgsn/sgsn.h> |
| 61 | #include <osmocom/sgsn/gprs_llc.h> |
Pau Espin Pedrol | df20336 | 2023-01-05 14:08:32 +0100 | [diff] [blame] | 62 | #include <osmocom/sgsn/gprs_sndcp.h> |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 63 | #include <osmocom/sgsn/gprs_gmm.h> |
Pau Espin Pedrol | 6dfb5fe | 2019-08-29 17:21:00 +0200 | [diff] [blame] | 64 | #include <osmocom/sgsn/gprs_ranap.h> |
Pau Espin Pedrol | 4398ac0 | 2022-12-23 17:12:39 +0100 | [diff] [blame] | 65 | #include <osmocom/sgsn/gprs_ns.h> |
Pau Espin Pedrol | 7a74ae4 | 2022-12-23 16:33:17 +0100 | [diff] [blame] | 66 | #include <osmocom/sgsn/gprs_bssgp.h> |
Pau Espin Pedrol | b61ab9b | 2023-01-05 14:03:18 +0100 | [diff] [blame] | 67 | #include <osmocom/sgsn/gprs_subscriber.h> |
Pau Espin Pedrol | 920c6c8 | 2023-01-05 14:13:25 +0100 | [diff] [blame] | 68 | #include <osmocom/sgsn/gtp.h> |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 69 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 70 | #include <gtp.h> |
Philipp Maier | 2ce050b | 2020-12-14 23:27:47 +0100 | [diff] [blame] | 71 | #include <osmocom/sgsn/sgsn_rim.h> |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 72 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 73 | #include "../../bscconfig.h" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 74 | |
Neels Hofmeyr | a7a3947 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 75 | #if BUILD_IU |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 76 | #include <osmocom/sigtran/osmo_ss7.h> |
| 77 | #include <osmocom/sigtran/protocol/m3ua.h> |
Neels Hofmeyr | a7a3947 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 78 | #include <osmocom/ranap/iu_client.h> |
| 79 | #endif |
| 80 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 81 | #define _GNU_SOURCE |
| 82 | #include <getopt.h> |
| 83 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 84 | void *tall_sgsn_ctx; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 85 | |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 86 | struct gprs_ns2_inst *sgsn_nsi; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 87 | static int daemonize = 0; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 88 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 89 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | 69ca8f0 | 2015-10-29 10:35:24 +0100 | [diff] [blame] | 90 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 91 | "This is free software: you are free to change and redistribute it.\r\n" |
| 92 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 93 | |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 94 | #define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg" |
| 95 | #define CONFIG_FILE_LEGACY "osmo_sgsn.cfg" |
| 96 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 97 | |
| 98 | struct sgsn_instance *sgsn; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 99 | |
Pau Espin Pedrol | 7a74ae4 | 2022-12-23 16:33:17 +0100 | [diff] [blame] | 100 | /* call-back function for the BSSGP protocol. |
| 101 | * Must be left here so that we can add a new one in tests/sgsn_test */ |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 102 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 103 | { |
Pau Espin Pedrol | 7a74ae4 | 2022-12-23 16:33:17 +0100 | [diff] [blame] | 104 | return sgsn_bssgp_rx_prim(oph); |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame] | 107 | static void signal_handler(int signum) |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 108 | { |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame] | 109 | fprintf(stdout, "signal %u received\n", signum); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 110 | |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame] | 111 | switch (signum) { |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 112 | case SIGINT: |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 113 | case SIGTERM: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 114 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 115 | sleep(1); |
| 116 | exit(0); |
| 117 | break; |
| 118 | case SIGABRT: |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame] | 119 | /* in case of abort, we want to obtain a talloc report and |
| 120 | * then run default SIGABRT handler, who will generate coredump |
| 121 | * and abort the process. abort() should do this for us after we |
| 122 | * return, but program wouldn't exit if an external SIGABRT is |
| 123 | * received. |
| 124 | */ |
| 125 | talloc_report(tall_vty_ctx, stderr); |
| 126 | talloc_report_full(tall_sgsn_ctx, stderr); |
| 127 | signal(SIGABRT, SIG_DFL); |
| 128 | raise(SIGABRT); |
| 129 | break; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 130 | case SIGUSR1: |
| 131 | talloc_report(tall_vty_ctx, stderr); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 132 | talloc_report_full(tall_sgsn_ctx, stderr); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 133 | break; |
| 134 | case SIGUSR2: |
| 135 | talloc_report_full(tall_vty_ctx, stderr); |
| 136 | break; |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
Pau Espin Pedrol | e5c8998 | 2021-05-03 18:16:42 +0200 | [diff] [blame] | 142 | static int sgsn_vty_go_parent(struct vty *vty) |
| 143 | { |
| 144 | switch (vty->node) { |
| 145 | case SGSN_NODE: |
| 146 | vty->node = CONFIG_NODE; |
| 147 | break; |
| 148 | case MME_NODE: |
| 149 | vty->node = SGSN_NODE; |
| 150 | vty->index = NULL; |
| 151 | break; |
| 152 | default: |
| 153 | #if BUILD_IU |
| 154 | osmo_ss7_vty_go_parent(vty); |
| 155 | #else |
| 156 | vty->node = CONFIG_NODE; |
| 157 | vty->index = NULL; |
| 158 | #endif |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | return vty->node; |
| 163 | } |
| 164 | |
Harald Welte | d6c7416 | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 165 | /* NSI that BSSGP uses when transmitting on NS */ |
| 166 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 167 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 168 | static struct vty_app_info vty_info = { |
Harald Welte | ec1921d | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 169 | .name = "OsmoSGSN", |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 170 | .version = PACKAGE_VERSION, |
Pau Espin Pedrol | e5c8998 | 2021-05-03 18:16:42 +0200 | [diff] [blame] | 171 | .go_parent_cb = sgsn_vty_go_parent, |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 174 | static void print_help(void) |
| 175 | { |
| 176 | printf("Some useful help...\n"); |
| 177 | printf(" -h --help\tthis text\n"); |
Daniel Willmann | 03a2ed5 | 2019-07-26 11:56:31 +0200 | [diff] [blame] | 178 | printf(" -V --version\tPrint the version\n"); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 179 | printf(" -D --daemonize\tFork the process into a background daemon\n"); |
| 180 | printf(" -d option --debug\tenable Debugging\n"); |
| 181 | printf(" -s --disable-color\n"); |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 182 | printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 183 | printf(" -e --log-level number\tSet a global log level\n"); |
Harald Welte | 999a776 | 2021-02-23 15:52:25 +0100 | [diff] [blame] | 184 | |
| 185 | printf("\nVTY reference generation:\n"); |
| 186 | printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"); |
| 187 | printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n"); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Harald Welte | 999a776 | 2021-02-23 15:52:25 +0100 | [diff] [blame] | 190 | static void handle_long_options(const char *prog_name, const int long_option) |
| 191 | { |
| 192 | static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT; |
| 193 | |
| 194 | switch (long_option) { |
| 195 | case 1: |
| 196 | vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg); |
| 197 | if (vty_ref_mode < 0) { |
| 198 | fprintf(stderr, "%s: Unknown VTY reference generation " |
| 199 | "mode '%s'\n", prog_name, optarg); |
| 200 | exit(2); |
| 201 | } |
| 202 | break; |
| 203 | case 2: |
| 204 | fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n", |
| 205 | get_value_string(vty_ref_gen_mode_names, vty_ref_mode), |
| 206 | get_value_string(vty_ref_gen_mode_desc, vty_ref_mode)); |
| 207 | vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode); |
| 208 | exit(0); |
| 209 | default: |
| 210 | fprintf(stderr, "%s: error parsing cmdline options\n", prog_name); |
| 211 | exit(2); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 216 | static void handle_options(int argc, char **argv) |
| 217 | { |
| 218 | while (1) { |
| 219 | int option_index = 0, c; |
Harald Welte | 999a776 | 2021-02-23 15:52:25 +0100 | [diff] [blame] | 220 | static int long_option = 0; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 221 | static struct option long_options[] = { |
| 222 | {"help", 0, 0, 'h'}, |
| 223 | {"debug", 1, 0, 'd'}, |
| 224 | {"daemonize", 0, 0, 'D'}, |
| 225 | {"config-file", 1, 0, 'c'}, |
| 226 | {"disable-color", 0, 0, 's'}, |
| 227 | {"timestamp", 0, 0, 'T'}, |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 228 | { "version", 0, 0, 'V' }, |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 229 | {"log-level", 1, 0, 'e'}, |
Harald Welte | 999a776 | 2021-02-23 15:52:25 +0100 | [diff] [blame] | 230 | {"vty-ref-mode", 1, &long_option, 1}, |
| 231 | {"vty-ref-xml", 0, &long_option, 2}, |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 232 | {NULL, 0, 0, 0} |
| 233 | }; |
| 234 | |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 235 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 236 | long_options, &option_index); |
| 237 | if (c == -1) |
| 238 | break; |
| 239 | |
| 240 | switch (c) { |
Harald Welte | 999a776 | 2021-02-23 15:52:25 +0100 | [diff] [blame] | 241 | case 0: |
| 242 | handle_long_options(argv[0], long_option); |
| 243 | break; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 244 | case 'h': |
| 245 | //print_usage(); |
| 246 | print_help(); |
| 247 | exit(0); |
| 248 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 249 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 250 | break; |
| 251 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 252 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 253 | break; |
| 254 | case 'D': |
| 255 | daemonize = 1; |
| 256 | break; |
| 257 | case 'c': |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 258 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 259 | break; |
| 260 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 261 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 262 | break; |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 263 | case 'V': |
| 264 | print_version(1); |
| 265 | exit(0); |
| 266 | break; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 267 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 268 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 269 | break; |
| 270 | default: |
| 271 | /* ignore */ |
| 272 | break; |
| 273 | } |
| 274 | } |
Harald Welte | de67c00 | 2019-12-03 22:28:19 +0100 | [diff] [blame] | 275 | |
| 276 | if (argc > optind) { |
| 277 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 278 | exit(2); |
| 279 | } |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 282 | /* default categories */ |
| 283 | static struct log_info_cat gprs_categories[] = { |
| 284 | [DMM] = { |
| 285 | .name = "DMM", |
| 286 | .description = "Layer3 Mobility Management (MM)", |
| 287 | .color = "\033[1;33m", |
| 288 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 289 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 290 | [DPAG] = { |
| 291 | .name = "DPAG", |
| 292 | .description = "Paging Subsystem", |
| 293 | .color = "\033[1;38m", |
| 294 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 295 | }, |
| 296 | [DMEAS] = { |
| 297 | .name = "DMEAS", |
| 298 | .description = "Radio Measurement Processing", |
| 299 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 300 | }, |
| 301 | [DREF] = { |
| 302 | .name = "DREF", |
| 303 | .description = "Reference Counting", |
| 304 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 305 | }, |
| 306 | [DGPRS] = { |
| 307 | .name = "DGPRS", |
| 308 | .description = "GPRS Packet Service", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 309 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 310 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 311 | [DLLC] = { |
| 312 | .name = "DLLC", |
| 313 | .description = "GPRS Logical Link Control Protocol (LLC)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 314 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 315 | }, |
| 316 | [DSNDCP] = { |
| 317 | .name = "DSNDCP", |
| 318 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 319 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 320 | }, |
Daniel Willmann | 35a65ed | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 321 | [DRANAP] = { |
| 322 | .name = "DRANAP", |
| 323 | .description = "RAN Application Part (RANAP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 324 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Daniel Willmann | 35a65ed | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 325 | }, |
Daniel Willmann | 3adb23c | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 326 | [DSUA] = { |
| 327 | .name = "DSUA", |
| 328 | .description = "SCCP User Adaptation (SUA)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 329 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Daniel Willmann | 3adb23c | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 330 | }, |
Philipp | 2c7f837 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 331 | [DSLHC] = { |
| 332 | .name = "DSLHC", |
| 333 | .description = "RFC1144 TCP/IP Header compression (SLHC)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 334 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Philipp | 2c7f837 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 335 | }, |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 336 | [DV42BIS] = { |
| 337 | .name = "DV42BIS", |
| 338 | .description = "V.42bis data compression (SNDCP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 339 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Pau Espin Pedrol | 6ec5dc2 | 2019-08-28 16:52:56 +0200 | [diff] [blame] | 340 | }, |
| 341 | [DGTP] = { |
| 342 | .name = "DGTP", |
| 343 | .description = "GPRS Tunnelling Protocol (GTP)", |
| 344 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 345 | }, |
Philipp Maier | 2ce050b | 2020-12-14 23:27:47 +0100 | [diff] [blame] | 346 | [DRIM] = { |
| 347 | .name = "DRIM", |
| 348 | .description = "RAN Information Management (RIM)", |
| 349 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 350 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | static const struct log_info gprs_log_info = { |
| 354 | .filter_fn = gprs_log_filter_fn, |
| 355 | .cat = gprs_categories, |
| 356 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 357 | }; |
| 358 | |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 359 | static bool file_exists(const char *path) |
| 360 | { |
| 361 | struct stat sb; |
| 362 | return stat(path, &sb) ? false : true; |
| 363 | } |
| 364 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 365 | int main(int argc, char **argv) |
| 366 | { |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 367 | int rc; |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 368 | #if BUILD_IU |
| 369 | struct osmo_sccp_instance *sccp; |
| 370 | #endif |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 371 | |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 372 | srand(time(NULL)); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 373 | tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 374 | sgsn = sgsn_instance_alloc(tall_sgsn_ctx); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 375 | msgb_talloc_ctx_init(tall_sgsn_ctx, 0); |
| 376 | vty_info.tall_ctx = tall_sgsn_ctx; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 377 | |
| 378 | signal(SIGINT, &signal_handler); |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 379 | signal(SIGTERM, &signal_handler); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 380 | signal(SIGABRT, &signal_handler); |
| 381 | signal(SIGUSR1, &signal_handler); |
| 382 | signal(SIGUSR2, &signal_handler); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 383 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 384 | osmo_init_ignore_signals(); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 385 | osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info); |
| 386 | osmo_stats_init(tall_sgsn_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 387 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 388 | vty_info.copyright = openbsc_copyright; |
| 389 | vty_init(&vty_info); |
Pau Espin Pedrol | e456e32 | 2019-08-05 16:03:39 +0200 | [diff] [blame] | 390 | logging_vty_add_cmds(); |
Harald Welte | 731930b | 2018-02-14 01:02:22 +0100 | [diff] [blame] | 391 | osmo_talloc_vty_add_cmds(); |
Pau Espin Pedrol | e456e32 | 2019-08-05 16:03:39 +0200 | [diff] [blame] | 392 | osmo_stats_vty_add_cmds(); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 393 | sgsn_vty_init(&sgsn->cfg); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 394 | ctrl_vty_init(tall_sgsn_ctx); |
Pau Espin Pedrol | aae7daf | 2020-08-18 13:50:00 +0200 | [diff] [blame] | 395 | osmo_cpu_sched_vty_init(tall_sgsn_ctx); |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 396 | |
| 397 | #if BUILD_IU |
Harald Welte | 61557f4 | 2019-12-01 13:25:16 +0100 | [diff] [blame] | 398 | OSMO_ASSERT(osmo_ss7_init() == 0); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 399 | osmo_ss7_vty_init_asp(tall_sgsn_ctx); |
Neels Hofmeyr | 395c558 | 2018-09-27 01:55:08 +0200 | [diff] [blame] | 400 | osmo_sccp_vty_init(); |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 401 | #endif |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 402 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 403 | rate_ctr_init(tall_sgsn_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 404 | |
Harald Welte | 453a51d | 2020-12-10 15:18:50 +0100 | [diff] [blame] | 405 | logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp"); |
Vadim Yanitskiy | 8de4be2 | 2021-03-14 20:58:58 +0100 | [diff] [blame] | 406 | logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "ns"); |
Harald Welte | 68d85d5 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 407 | |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 408 | sgsn_nsi = gprs_ns2_instantiate(tall_sgsn_ctx, &gprs_ns_prim_cb, NULL); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 409 | if (!sgsn_nsi) { |
| 410 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 411 | exit(1); |
| 412 | } |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 413 | sgsn->cfg.nsi = sgsn_nsi; |
Pau Espin Pedrol | 7a74ae4 | 2022-12-23 16:33:17 +0100 | [diff] [blame] | 414 | bssgp_set_bssgp_callback(sgsn_bssgp_dispatch_ns_unitdata_req_cb, sgsn_nsi); |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 415 | |
| 416 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 417 | |
Alexander Couzens | caf73b8 | 2021-01-27 21:02:13 +0100 | [diff] [blame] | 418 | gprs_ns2_vty_init(sgsn_nsi); |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 419 | bssgp_vty_init(); |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 420 | gprs_llc_vty_init(); |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 421 | gprs_sndcp_vty_init(); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 422 | |
Harald Welte | ebd3983 | 2021-02-23 16:41:04 +0100 | [diff] [blame] | 423 | handle_options(argc, argv); |
| 424 | |
| 425 | /* Backwards compatibility: for years, the default config file name was |
| 426 | * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a |
| 427 | * dash. To be able to use the new config file name without breaking |
| 428 | * previous setups that might rely on the legacy default config file |
| 429 | * name, we need to look for the old config file if no -c option was |
| 430 | * passed AND no file exists with the new default file name. */ |
| 431 | if (!sgsn->config_file) { |
| 432 | /* No -c option was passed */ |
| 433 | if (file_exists(CONFIG_FILE_LEGACY) |
| 434 | && !file_exists(CONFIG_FILE_DEFAULT)) |
| 435 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY); |
| 436 | else |
| 437 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT); |
| 438 | } |
| 439 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 440 | rc = sgsn_parse_config(sgsn->config_file); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 441 | if (rc < 0) { |
Neels Hofmeyr | 27355c9 | 2017-02-24 06:28:31 +0100 | [diff] [blame] | 442 | LOGP(DGPRS, LOGL_FATAL, "Error in config file\n"); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 443 | exit(2); |
| 444 | } |
| 445 | |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 446 | /* start telnet after reading config for vty_get_bind_addr() */ |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 447 | rc = telnet_init_dynif(tall_sgsn_ctx, NULL, |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 448 | vty_get_bind_addr(), OSMO_VTY_PORT_SGSN); |
| 449 | if (rc < 0) |
| 450 | exit(1); |
| 451 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 452 | rc = sgsn_gtp_init(sgsn); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 453 | if (rc) { |
| 454 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 455 | exit(2); |
Max | baabc68 | 2017-10-20 13:39:57 +0200 | [diff] [blame] | 456 | } else |
| 457 | LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version()); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 458 | |
Pau Espin Pedrol | e659f75 | 2023-01-05 17:20:37 +0100 | [diff] [blame] | 459 | rc = sgsn_inst_init(sgsn); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 460 | if (rc < 0) { |
Pau Espin Pedrol | e659f75 | 2023-01-05 17:20:37 +0100 | [diff] [blame] | 461 | LOGP(DGPRS, LOGL_FATAL, "Cannot set up SGSN\n"); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 462 | exit(2); |
| 463 | } |
| 464 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 465 | if (sgsn->cfg.dynamic_lookup) { |
| 466 | if (sgsn_ares_init(sgsn) != 0) { |
| 467 | LOGP(DGPRS, LOGL_FATAL, |
| 468 | "Failed to initialize c-ares(%d)\n", rc); |
| 469 | exit(4); |
| 470 | } |
| 471 | } |
| 472 | |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 473 | #if BUILD_IU |
Neels Hofmeyr | 1d047ed | 2017-11-24 04:33:08 +0100 | [diff] [blame] | 474 | /* Note that these are mostly defaults and can be overriden from the VTY */ |
Pau Espin Pedrol | 2c90899 | 2019-08-19 19:06:06 +0200 | [diff] [blame] | 475 | sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx, |
| 476 | sgsn->cfg.iu.cs7_instance, |
| 477 | "OsmoSGSN", |
Pau Espin Pedrol | f0f596f | 2019-08-19 17:54:16 +0200 | [diff] [blame] | 478 | (23 << 3) + 4, |
| 479 | OSMO_SS7_ASP_PROT_M3UA, |
Pau Espin Pedrol | e6c5b4a | 2020-08-20 12:35:07 +0200 | [diff] [blame] | 480 | 0, "localhost", |
| 481 | 0, "localhost"); |
Philipp Maier | efe85d3 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 482 | if (!sccp) { |
| 483 | printf("Setting up SCCP client failed.\n"); |
| 484 | return 8; |
| 485 | } |
| 486 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 487 | ranap_iu_init(tall_sgsn_ctx, DRANAP, "OsmoSGSN-IuPS", sccp, gsm0408_gprs_rcvmsg_iu, sgsn_ranap_iu_event); |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 488 | #endif |
| 489 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 490 | if (daemonize) { |
| 491 | rc = osmo_daemonize(); |
| 492 | if (rc < 0) { |
| 493 | perror("Error during daemonize"); |
| 494 | exit(1); |
| 495 | } |
| 496 | } |
| 497 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 498 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 499 | rc = osmo_select_main(0); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 500 | if (rc < 0) |
| 501 | exit(3); |
| 502 | } |
| 503 | |
Harald Welte | baf7700 | 2011-07-16 13:47:01 +0200 | [diff] [blame] | 504 | /* not reached */ |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 505 | exit(0); |
| 506 | } |