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> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 43 | #include <osmocom/gprs/gprs_ns.h> |
| 44 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 9d16b14 | 2020-06-26 15:55:21 +0200 | [diff] [blame] | 45 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 46 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 47 | #include <osmocom/vty/telnet_interface.h> |
Pablo Neira Ayuso | 3848770 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 48 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 49 | #include <osmocom/vty/stats.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 50 | #include <osmocom/vty/ports.h> |
Harald Welte | 731930b | 2018-02-14 01:02:22 +0100 | [diff] [blame] | 51 | #include <osmocom/vty/misc.h> |
Pau Espin Pedrol | aae7daf | 2020-08-18 13:50:00 +0200 | [diff] [blame] | 52 | #include <osmocom/vty/cpu_sched_vty.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 53 | |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 54 | #include <osmocom/ctrl/control_vty.h> |
| 55 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 56 | #include <osmocom/sgsn/signal.h> |
| 57 | #include <osmocom/sgsn/debug.h> |
| 58 | #include <osmocom/sgsn/vty.h> |
| 59 | #include <osmocom/sgsn/sgsn.h> |
| 60 | #include <osmocom/sgsn/gprs_llc.h> |
| 61 | #include <osmocom/sgsn/gprs_gmm.h> |
Pau Espin Pedrol | 6dfb5fe | 2019-08-29 17:21:00 +0200 | [diff] [blame] | 62 | #include <osmocom/sgsn/gprs_ranap.h> |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 63 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 64 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 65 | #include <osmocom/ctrl/ports.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 66 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 67 | #include <gtp.h> |
| 68 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 69 | #include "../../bscconfig.h" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 70 | |
Neels Hofmeyr | a7a3947 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 71 | #if BUILD_IU |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 72 | #include <osmocom/sigtran/osmo_ss7.h> |
| 73 | #include <osmocom/sigtran/protocol/m3ua.h> |
Neels Hofmeyr | a7a3947 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 74 | #include <osmocom/ranap/iu_client.h> |
| 75 | #endif |
| 76 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 77 | #define _GNU_SOURCE |
| 78 | #include <getopt.h> |
| 79 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 80 | void *tall_sgsn_ctx; |
Pau Espin Pedrol | 2e9ea50 | 2017-11-29 14:01:35 +0100 | [diff] [blame] | 81 | struct ctrl_handle *g_ctrlh; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 82 | |
| 83 | struct gprs_ns_inst *sgsn_nsi; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 84 | static int daemonize = 0; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 85 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 86 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | 69ca8f0 | 2015-10-29 10:35:24 +0100 | [diff] [blame] | 87 | "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] | 88 | "This is free software: you are free to change and redistribute it.\r\n" |
| 89 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 90 | |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 91 | #define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg" |
| 92 | #define CONFIG_FILE_LEGACY "osmo_sgsn.cfg" |
| 93 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 94 | |
| 95 | struct sgsn_instance *sgsn; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 96 | |
| 97 | /* call-back function for the NS protocol */ |
| 98 | static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 99 | struct msgb *msg, uint16_t bvci) |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 100 | { |
| 101 | int rc = 0; |
| 102 | |
| 103 | switch (event) { |
| 104 | case GPRS_NS_EVT_UNIT_DATA: |
| 105 | /* hand the message into the BSSGP implementation */ |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 106 | rc = bssgp_rcvmsg(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 110 | if (msg) |
Holger Hans Peter Freyther | a8ddb08 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 111 | msgb_free(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 112 | rc = -EIO; |
| 113 | break; |
| 114 | } |
| 115 | return rc; |
| 116 | } |
| 117 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 118 | /* call-back function for the BSSGP protocol */ |
| 119 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 120 | { |
| 121 | struct osmo_bssgp_prim *bp; |
| 122 | bp = container_of(oph, struct osmo_bssgp_prim, oph); |
| 123 | |
| 124 | switch (oph->sap) { |
| 125 | case SAP_BSSGP_LL: |
| 126 | switch (oph->primitive) { |
| 127 | case PRIM_BSSGP_UL_UD: |
| 128 | return gprs_llc_rcvmsg(oph->msg, bp->tp); |
| 129 | } |
| 130 | break; |
| 131 | case SAP_BSSGP_GMM: |
| 132 | switch (oph->primitive) { |
| 133 | case PRIM_BSSGP_GMM_SUSPEND: |
| 134 | return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli); |
| 135 | case PRIM_BSSGP_GMM_RESUME: |
| 136 | return gprs_gmm_rx_resume(bp->ra_id, bp->tlli, |
Holger Hans Peter Freyther | 9b5db28 | 2012-08-03 10:01:57 +0200 | [diff] [blame] | 137 | bp->u.resume.suspend_ref); |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 138 | } |
| 139 | break; |
| 140 | case SAP_BSSGP_NM: |
| 141 | break; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame^] | 146 | static void signal_handler(int signum) |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 147 | { |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame^] | 148 | fprintf(stdout, "signal %u received\n", signum); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 149 | |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame^] | 150 | switch (signum) { |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 151 | case SIGINT: |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 152 | case SIGTERM: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 153 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 154 | sleep(1); |
| 155 | exit(0); |
| 156 | break; |
| 157 | case SIGABRT: |
Pau Espin Pedrol | bcd7709 | 2020-11-25 18:28:55 +0100 | [diff] [blame^] | 158 | /* in case of abort, we want to obtain a talloc report and |
| 159 | * then run default SIGABRT handler, who will generate coredump |
| 160 | * and abort the process. abort() should do this for us after we |
| 161 | * return, but program wouldn't exit if an external SIGABRT is |
| 162 | * received. |
| 163 | */ |
| 164 | talloc_report(tall_vty_ctx, stderr); |
| 165 | talloc_report_full(tall_sgsn_ctx, stderr); |
| 166 | signal(SIGABRT, SIG_DFL); |
| 167 | raise(SIGABRT); |
| 168 | break; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 169 | case SIGUSR1: |
| 170 | talloc_report(tall_vty_ctx, stderr); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 171 | talloc_report_full(tall_sgsn_ctx, stderr); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 172 | break; |
| 173 | case SIGUSR2: |
| 174 | talloc_report_full(tall_vty_ctx, stderr); |
| 175 | break; |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
Harald Welte | d6c7416 | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 181 | /* NSI that BSSGP uses when transmitting on NS */ |
| 182 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 183 | |
Harald Welte | 9d16b14 | 2020-06-26 15:55:21 +0200 | [diff] [blame] | 184 | static void bvc_reset_persistent_nsvcs(void) |
| 185 | { |
| 186 | /* Send BVC-RESET on all persistent NSVCs */ |
| 187 | struct gprs_nsvc *nsvc; |
| 188 | |
| 189 | llist_for_each_entry(nsvc, &sgsn_nsi->gprs_nsvcs, list) { |
| 190 | struct bssgp_bvc_ctx bctx = { |
| 191 | .nsei = nsvc->nsei, |
| 192 | }; |
| 193 | if (!nsvc->persistent) |
| 194 | continue; |
| 195 | /* if it is not marked ALIVE, we cannot send any data over it. */ |
| 196 | nsvc->state |= NSE_S_ALIVE; |
| 197 | bssgp_tx_bvc_reset2(&bctx, BVCI_SIGNALLING, BSSGP_CAUSE_EQUIP_FAIL, false); |
| 198 | } |
| 199 | } |
| 200 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 201 | static struct vty_app_info vty_info = { |
Harald Welte | ec1921d | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 202 | .name = "OsmoSGSN", |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 203 | .version = PACKAGE_VERSION, |
Daniel Willmann | 62fa619 | 2020-11-03 09:07:45 +0100 | [diff] [blame] | 204 | #if BUILD_IU |
| 205 | .go_parent_cb = osmo_ss7_vty_go_parent, |
| 206 | #endif |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 209 | static void print_help(void) |
| 210 | { |
| 211 | printf("Some useful help...\n"); |
| 212 | printf(" -h --help\tthis text\n"); |
Daniel Willmann | 03a2ed5 | 2019-07-26 11:56:31 +0200 | [diff] [blame] | 213 | printf(" -V --version\tPrint the version\n"); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 214 | printf(" -D --daemonize\tFork the process into a background daemon\n"); |
| 215 | printf(" -d option --debug\tenable Debugging\n"); |
| 216 | printf(" -s --disable-color\n"); |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 217 | 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] | 218 | printf(" -e --log-level number\tSet a global log level\n"); |
| 219 | } |
| 220 | |
| 221 | static void handle_options(int argc, char **argv) |
| 222 | { |
| 223 | while (1) { |
| 224 | int option_index = 0, c; |
| 225 | static struct option long_options[] = { |
| 226 | {"help", 0, 0, 'h'}, |
| 227 | {"debug", 1, 0, 'd'}, |
| 228 | {"daemonize", 0, 0, 'D'}, |
| 229 | {"config-file", 1, 0, 'c'}, |
| 230 | {"disable-color", 0, 0, 's'}, |
| 231 | {"timestamp", 0, 0, 'T'}, |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 232 | { "version", 0, 0, 'V' }, |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 233 | {"log-level", 1, 0, 'e'}, |
| 234 | {NULL, 0, 0, 0} |
| 235 | }; |
| 236 | |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 237 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 238 | long_options, &option_index); |
| 239 | if (c == -1) |
| 240 | break; |
| 241 | |
| 242 | switch (c) { |
| 243 | case 'h': |
| 244 | //print_usage(); |
| 245 | print_help(); |
| 246 | exit(0); |
| 247 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 248 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 249 | break; |
| 250 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 251 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 252 | break; |
| 253 | case 'D': |
| 254 | daemonize = 1; |
| 255 | break; |
| 256 | case 'c': |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 257 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 258 | break; |
| 259 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 260 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 261 | break; |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 262 | case 'V': |
| 263 | print_version(1); |
| 264 | exit(0); |
| 265 | break; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 266 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 267 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 268 | break; |
| 269 | default: |
| 270 | /* ignore */ |
| 271 | break; |
| 272 | } |
| 273 | } |
Harald Welte | de67c00 | 2019-12-03 22:28:19 +0100 | [diff] [blame] | 274 | |
| 275 | if (argc > optind) { |
| 276 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 277 | exit(2); |
| 278 | } |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 281 | /* default categories */ |
| 282 | static struct log_info_cat gprs_categories[] = { |
| 283 | [DMM] = { |
| 284 | .name = "DMM", |
| 285 | .description = "Layer3 Mobility Management (MM)", |
| 286 | .color = "\033[1;33m", |
| 287 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 288 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 289 | [DPAG] = { |
| 290 | .name = "DPAG", |
| 291 | .description = "Paging Subsystem", |
| 292 | .color = "\033[1;38m", |
| 293 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 294 | }, |
| 295 | [DMEAS] = { |
| 296 | .name = "DMEAS", |
| 297 | .description = "Radio Measurement Processing", |
| 298 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 299 | }, |
| 300 | [DREF] = { |
| 301 | .name = "DREF", |
| 302 | .description = "Reference Counting", |
| 303 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 304 | }, |
| 305 | [DGPRS] = { |
| 306 | .name = "DGPRS", |
| 307 | .description = "GPRS Packet Service", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 308 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 309 | }, |
| 310 | [DNS] = { |
| 311 | .name = "DNS", |
| 312 | .description = "GPRS Network Service (NS)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 313 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 314 | }, |
| 315 | [DBSSGP] = { |
| 316 | .name = "DBSSGP", |
| 317 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 318 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 319 | }, |
| 320 | [DLLC] = { |
| 321 | .name = "DLLC", |
| 322 | .description = "GPRS Logical Link Control Protocol (LLC)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 323 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 324 | }, |
| 325 | [DSNDCP] = { |
| 326 | .name = "DSNDCP", |
| 327 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 328 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 329 | }, |
Daniel Willmann | 35a65ed | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 330 | [DRANAP] = { |
| 331 | .name = "DRANAP", |
| 332 | .description = "RAN Application Part (RANAP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 333 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Daniel Willmann | 35a65ed | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 334 | }, |
Daniel Willmann | 3adb23c | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 335 | [DSUA] = { |
| 336 | .name = "DSUA", |
| 337 | .description = "SCCP User Adaptation (SUA)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 338 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Daniel Willmann | 3adb23c | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 339 | }, |
Philipp | 2c7f837 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 340 | [DSLHC] = { |
| 341 | .name = "DSLHC", |
| 342 | .description = "RFC1144 TCP/IP Header compression (SLHC)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 343 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Philipp | 2c7f837 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 344 | }, |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 345 | [DV42BIS] = { |
| 346 | .name = "DV42BIS", |
| 347 | .description = "V.42bis data compression (SNDCP)", |
Philipp Maier | 5614354 | 2017-10-19 18:20:33 +0200 | [diff] [blame] | 348 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Pau Espin Pedrol | 6ec5dc2 | 2019-08-28 16:52:56 +0200 | [diff] [blame] | 349 | }, |
| 350 | [DGTP] = { |
| 351 | .name = "DGTP", |
| 352 | .description = "GPRS Tunnelling Protocol (GTP)", |
| 353 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 354 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | static const struct log_info gprs_log_info = { |
| 358 | .filter_fn = gprs_log_filter_fn, |
| 359 | .cat = gprs_categories, |
| 360 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 361 | }; |
| 362 | |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 363 | static bool file_exists(const char *path) |
| 364 | { |
| 365 | struct stat sb; |
| 366 | return stat(path, &sb) ? false : true; |
| 367 | } |
| 368 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 369 | int main(int argc, char **argv) |
| 370 | { |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 371 | int rc; |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 372 | #if BUILD_IU |
| 373 | struct osmo_sccp_instance *sccp; |
| 374 | #endif |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 375 | |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 376 | srand(time(NULL)); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 377 | tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 378 | sgsn = sgsn_instance_alloc(tall_sgsn_ctx); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 379 | msgb_talloc_ctx_init(tall_sgsn_ctx, 0); |
| 380 | vty_info.tall_ctx = tall_sgsn_ctx; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 381 | |
| 382 | signal(SIGINT, &signal_handler); |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 383 | signal(SIGTERM, &signal_handler); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 384 | signal(SIGABRT, &signal_handler); |
| 385 | signal(SIGUSR1, &signal_handler); |
| 386 | signal(SIGUSR2, &signal_handler); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 387 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 388 | osmo_init_ignore_signals(); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 389 | osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info); |
| 390 | osmo_stats_init(tall_sgsn_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 391 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 392 | vty_info.copyright = openbsc_copyright; |
| 393 | vty_init(&vty_info); |
Pau Espin Pedrol | e456e32 | 2019-08-05 16:03:39 +0200 | [diff] [blame] | 394 | logging_vty_add_cmds(); |
Harald Welte | 731930b | 2018-02-14 01:02:22 +0100 | [diff] [blame] | 395 | osmo_talloc_vty_add_cmds(); |
Pau Espin Pedrol | e456e32 | 2019-08-05 16:03:39 +0200 | [diff] [blame] | 396 | osmo_stats_vty_add_cmds(); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 397 | sgsn_vty_init(&sgsn->cfg); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 398 | ctrl_vty_init(tall_sgsn_ctx); |
Pau Espin Pedrol | aae7daf | 2020-08-18 13:50:00 +0200 | [diff] [blame] | 399 | osmo_cpu_sched_vty_init(tall_sgsn_ctx); |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 400 | |
| 401 | #if BUILD_IU |
Harald Welte | 61557f4 | 2019-12-01 13:25:16 +0100 | [diff] [blame] | 402 | OSMO_ASSERT(osmo_ss7_init() == 0); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 403 | osmo_ss7_vty_init_asp(tall_sgsn_ctx); |
Neels Hofmeyr | 395c558 | 2018-09-27 01:55:08 +0200 | [diff] [blame] | 404 | osmo_sccp_vty_init(); |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 405 | #endif |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 406 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 407 | handle_options(argc, argv); |
| 408 | |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 409 | /* Backwards compatibility: for years, the default config file name was |
| 410 | * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a |
| 411 | * dash. To be able to use the new config file name without breaking |
| 412 | * previous setups that might rely on the legacy default config file |
| 413 | * name, we need to look for the old config file if no -c option was |
| 414 | * passed AND no file exists with the new default file name. */ |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 415 | if (!sgsn->config_file) { |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 416 | /* No -c option was passed */ |
| 417 | if (file_exists(CONFIG_FILE_LEGACY) |
| 418 | && !file_exists(CONFIG_FILE_DEFAULT)) |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 419 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY); |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 420 | else |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 421 | osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT); |
Neels Hofmeyr | fea0609 | 2017-10-31 01:00:37 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 424 | rate_ctr_init(tall_sgsn_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 425 | |
Harald Welte | 68d85d5 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 426 | gprs_ns_set_log_ss(DNS); |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 427 | bssgp_set_log_ss(DBSSGP); |
Harald Welte | 68d85d5 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 428 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 429 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_sgsn_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 430 | if (!sgsn_nsi) { |
| 431 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 432 | exit(1); |
| 433 | } |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 434 | bssgp_nsi = sgsn->cfg.nsi = sgsn_nsi; |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 435 | |
| 436 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 437 | sgsn_rate_ctr_init(); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 438 | sgsn_inst_init(sgsn); |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 439 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 440 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 441 | bssgp_vty_init(); |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 442 | gprs_llc_vty_init(); |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 443 | gprs_sndcp_vty_init(); |
Max | 2e48576 | 2018-12-10 18:01:47 +0100 | [diff] [blame] | 444 | sgsn_auth_init(sgsn); |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 445 | sgsn_cdr_init(sgsn); |
Harald Welte | a6a20b4 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 446 | /* FIXME: register signal handler for SS_L_NS */ |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 447 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 448 | rc = sgsn_parse_config(sgsn->config_file); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 449 | if (rc < 0) { |
Neels Hofmeyr | 27355c9 | 2017-02-24 06:28:31 +0100 | [diff] [blame] | 450 | LOGP(DGPRS, LOGL_FATAL, "Error in config file\n"); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 451 | exit(2); |
| 452 | } |
| 453 | |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 454 | /* start telnet after reading config for vty_get_bind_addr() */ |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 455 | rc = telnet_init_dynif(tall_sgsn_ctx, NULL, |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 456 | vty_get_bind_addr(), OSMO_VTY_PORT_SGSN); |
| 457 | if (rc < 0) |
| 458 | exit(1); |
| 459 | |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 460 | /* start control interface after reading config for |
| 461 | * ctrl_vty_get_bind_addr() */ |
Stefan Sperling | 2599644 | 2018-12-11 14:35:43 +0100 | [diff] [blame] | 462 | g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(), |
| 463 | OSMO_CTRL_PORT_SGSN, NULL); |
Pau Espin Pedrol | 2e9ea50 | 2017-11-29 14:01:35 +0100 | [diff] [blame] | 464 | if (!g_ctrlh) { |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 465 | LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n"); |
| 466 | exit(1); |
| 467 | } |
| 468 | |
| 469 | if (sgsn_ctrl_cmds_install() != 0) { |
| 470 | LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n"); |
| 471 | exit(1); |
| 472 | } |
| 473 | |
| 474 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 475 | rc = sgsn_gtp_init(sgsn); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 476 | if (rc) { |
| 477 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 478 | exit(2); |
Max | baabc68 | 2017-10-20 13:39:57 +0200 | [diff] [blame] | 479 | } else |
| 480 | LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version()); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 481 | |
Pau Espin Pedrol | 73b2bf3 | 2018-10-30 18:00:57 +0100 | [diff] [blame] | 482 | rc = gprs_subscr_init(sgsn); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 483 | if (rc < 0) { |
| 484 | LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n"); |
| 485 | exit(2); |
| 486 | } |
| 487 | |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 488 | rc = gprs_ns_nsip_listen(sgsn_nsi); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 489 | if (rc < 0) { |
| 490 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 491 | exit(2); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | rc = gprs_ns_frgre_listen(sgsn_nsi); |
| 495 | if (rc < 0) { |
| 496 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 497 | "socket. Do you have CAP_NET_RAW?\n"); |
| 498 | exit(2); |
| 499 | } |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 500 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 501 | if (sgsn->cfg.dynamic_lookup) { |
| 502 | if (sgsn_ares_init(sgsn) != 0) { |
| 503 | LOGP(DGPRS, LOGL_FATAL, |
| 504 | "Failed to initialize c-ares(%d)\n", rc); |
| 505 | exit(4); |
| 506 | } |
| 507 | } |
| 508 | |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 509 | #if BUILD_IU |
Neels Hofmeyr | 1d047ed | 2017-11-24 04:33:08 +0100 | [diff] [blame] | 510 | /* 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] | 511 | sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx, |
| 512 | sgsn->cfg.iu.cs7_instance, |
| 513 | "OsmoSGSN", |
Pau Espin Pedrol | f0f596f | 2019-08-19 17:54:16 +0200 | [diff] [blame] | 514 | (23 << 3) + 4, |
| 515 | OSMO_SS7_ASP_PROT_M3UA, |
Pau Espin Pedrol | e6c5b4a | 2020-08-20 12:35:07 +0200 | [diff] [blame] | 516 | 0, "localhost", |
| 517 | 0, "localhost"); |
Philipp Maier | efe85d3 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 518 | if (!sccp) { |
| 519 | printf("Setting up SCCP client failed.\n"); |
| 520 | return 8; |
| 521 | } |
| 522 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 523 | 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] | 524 | #endif |
| 525 | |
Harald Welte | 9d16b14 | 2020-06-26 15:55:21 +0200 | [diff] [blame] | 526 | bvc_reset_persistent_nsvcs(); |
| 527 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 528 | if (daemonize) { |
| 529 | rc = osmo_daemonize(); |
| 530 | if (rc < 0) { |
| 531 | perror("Error during daemonize"); |
| 532 | exit(1); |
| 533 | } |
| 534 | } |
| 535 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 536 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 537 | rc = osmo_select_main(0); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 538 | if (rc < 0) |
| 539 | exit(3); |
| 540 | } |
| 541 | |
Harald Welte | baf7700 | 2011-07-16 13:47:01 +0200 | [diff] [blame] | 542 | /* not reached */ |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 543 | exit(0); |
| 544 | } |