blob: 72533489f02d8b9d0a022096cfcfc4e35b5051e9 [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/* GPRS SGSN Implementation */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08004 * (C) 2010 by On-Waves
Harald Welte288be162010-05-01 16:48:27 +02005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte288be162010-05-01 16:48:27 +020010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte288be162010-05-01 16:48:27 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte288be162010-05-01 16:48:27 +020019 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
Max4011e722016-07-05 15:19:12 +020024#include <time.h>
Harald Welte288be162010-05-01 16:48:27 +020025#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
Harald Welte66f793a2010-05-13 11:50:04 +020029#include <signal.h>
Harald Welte288be162010-05-01 16:48:27 +020030#include <sys/fcntl.h>
31#include <sys/stat.h>
Harald Welte288be162010-05-01 16:48:27 +020032#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020036#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#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 Erlbeck64630cc2015-10-26 16:25:37 +010041#include <osmocom/core/stats.h>
Alexander Couzensf23e2db2020-07-27 22:39:58 +020042#include <osmocom/core/sockaddr_str.h>
Harald Welte288be162010-05-01 16:48:27 +020043
Alexander Couzensf23e2db2020-07-27 22:39:58 +020044#include <osmocom/gprs/gprs_ns2.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080045#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte9d16b142020-06-26 15:55:21 +020046#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080047
Harald Welte4b037e42010-05-19 19:45:32 +020048#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010049#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010050#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020051#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010052#include <osmocom/vty/misc.h>
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +020053#include <osmocom/vty/cpu_sched_vty.h>
Harald Welte4b037e42010-05-19 19:45:32 +020054
Neels Hofmeyr73828152016-02-23 15:10:33 +010055#include <osmocom/ctrl/control_vty.h>
56
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020057#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 Pedroldf203362023-01-05 14:08:32 +010062#include <osmocom/sgsn/gprs_sndcp.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020063#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020064#include <osmocom/sgsn/gprs_ranap.h>
Pau Espin Pedrol4398ac02022-12-23 17:12:39 +010065#include <osmocom/sgsn/gprs_ns.h>
Pau Espin Pedrol7a74ae42022-12-23 16:33:17 +010066#include <osmocom/sgsn/gprs_bssgp.h>
Pau Espin Pedrolb61ab9b2023-01-05 14:03:18 +010067#include <osmocom/sgsn/gprs_subscriber.h>
Pau Espin Pedrol920c6c82023-01-05 14:13:25 +010068#include <osmocom/sgsn/gtp.h>
Daniel Willmann6292c8d2016-05-21 17:35:57 +020069
Harald Welte2720e732010-05-17 00:44:57 +020070#include <gtp.h>
Philipp Maier2ce050b2020-12-14 23:27:47 +010071#include <osmocom/sgsn/sgsn_rim.h>
Harald Welte2720e732010-05-17 00:44:57 +020072
Harald Weltee2365962010-05-04 07:41:59 +020073#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020074
Neels Hofmeyra7a39472017-07-05 15:19:52 +020075#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020076#include <osmocom/sigtran/osmo_ss7.h>
77#include <osmocom/sigtran/protocol/m3ua.h>
Neels Hofmeyra7a39472017-07-05 15:19:52 +020078#include <osmocom/ranap/iu_client.h>
79#endif
80
Harald Welte288be162010-05-01 16:48:27 +020081#define _GNU_SOURCE
82#include <getopt.h>
83
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010084void *tall_sgsn_ctx;
Harald Welte288be162010-05-01 16:48:27 +020085
Alexander Couzensf23e2db2020-07-27 22:39:58 +020086struct gprs_ns2_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020087static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020088const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080089 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte69ca8f02015-10-29 10:35:24 +010090 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080091 "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 Welte288be162010-05-01 16:48:27 +020093
Neels Hofmeyrfea06092017-10-31 01:00:37 +010094#define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg"
95#define CONFIG_FILE_LEGACY "osmo_sgsn.cfg"
96
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +010097
98struct sgsn_instance *sgsn;
Harald Welte288be162010-05-01 16:48:27 +020099
Pau Espin Pedrol7a74ae42022-12-23 16:33:17 +0100100/* 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 Welteb1fd9022012-06-17 12:16:31 +0800102int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
103{
Pau Espin Pedrol7a74ae42022-12-23 16:33:17 +0100104 return sgsn_bssgp_rx_prim(oph);
Harald Welteb1fd9022012-06-17 12:16:31 +0800105}
106
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100107static void signal_handler(int signum)
Harald Welte66f793a2010-05-13 11:50:04 +0200108{
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100109 fprintf(stdout, "signal %u received\n", signum);
Harald Welte66f793a2010-05-13 11:50:04 +0200110
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100111 switch (signum) {
Harald Welte66f793a2010-05-13 11:50:04 +0200112 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200113 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200114 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200115 sleep(1);
116 exit(0);
117 break;
118 case SIGABRT:
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100119 /* 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 Welte66f793a2010-05-13 11:50:04 +0200130 case SIGUSR1:
131 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100132 talloc_report_full(tall_sgsn_ctx, stderr);
Harald Welte66f793a2010-05-13 11:50:04 +0200133 break;
134 case SIGUSR2:
135 talloc_report_full(tall_vty_ctx, stderr);
136 break;
137 default:
138 break;
139 }
140}
141
Pau Espin Pedrole5c89982021-05-03 18:16:42 +0200142static 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 Welted6c74162010-05-02 21:29:17 +0200165/* NSI that BSSGP uses when transmitting on NS */
166extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte288be162010-05-01 16:48:27 +0200167
Harald Weltec31f4802010-05-25 23:31:39 +0200168static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100169 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200170 .version = PACKAGE_VERSION,
Pau Espin Pedrole5c89982021-05-03 18:16:42 +0200171 .go_parent_cb = sgsn_vty_go_parent,
Harald Weltec31f4802010-05-25 23:31:39 +0200172};
173
Harald Welte9ae28a12010-08-27 09:26:44 +0200174static void print_help(void)
175{
176 printf("Some useful help...\n");
177 printf(" -h --help\tthis text\n");
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200178 printf(" -V --version\tPrint the version\n");
Harald Welte9ae28a12010-08-27 09:26:44 +0200179 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 Hofmeyrfea06092017-10-31 01:00:37 +0100182 printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte9ae28a12010-08-27 09:26:44 +0200183 printf(" -e --log-level number\tSet a global log level\n");
Harald Welte999a7762021-02-23 15:52:25 +0100184
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 Welte9ae28a12010-08-27 09:26:44 +0200188}
189
Harald Welte999a7762021-02-23 15:52:25 +0100190static 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 Welte9ae28a12010-08-27 09:26:44 +0200216static void handle_options(int argc, char **argv)
217{
218 while (1) {
219 int option_index = 0, c;
Harald Welte999a7762021-02-23 15:52:25 +0100220 static int long_option = 0;
Harald Welte9ae28a12010-08-27 09:26:44 +0200221 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 Maier99c97072017-03-14 17:32:35 +0100228 { "version", 0, 0, 'V' },
Harald Welte9ae28a12010-08-27 09:26:44 +0200229 {"log-level", 1, 0, 'e'},
Harald Welte999a7762021-02-23 15:52:25 +0100230 {"vty-ref-mode", 1, &long_option, 1},
231 {"vty-ref-xml", 0, &long_option, 2},
Harald Welte9ae28a12010-08-27 09:26:44 +0200232 {NULL, 0, 0, 0}
233 };
234
Philipp Maier99c97072017-03-14 17:32:35 +0100235 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte9ae28a12010-08-27 09:26:44 +0200236 long_options, &option_index);
237 if (c == -1)
238 break;
239
240 switch (c) {
Harald Welte999a7762021-02-23 15:52:25 +0100241 case 0:
242 handle_long_options(argv[0], long_option);
243 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200244 case 'h':
245 //print_usage();
246 print_help();
247 exit(0);
248 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200249 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200250 break;
251 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200252 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200253 break;
254 case 'D':
255 daemonize = 1;
256 break;
257 case 'c':
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100258 osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200259 break;
260 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200261 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200262 break;
Philipp Maier99c97072017-03-14 17:32:35 +0100263 case 'V':
264 print_version(1);
265 exit(0);
266 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200267 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200268 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200269 break;
270 default:
271 /* ignore */
272 break;
273 }
274 }
Harald Weltede67c002019-12-03 22:28:19 +0100275
276 if (argc > optind) {
277 fprintf(stderr, "Unsupported positional arguments on command line\n");
278 exit(2);
279 }
Harald Welte9ae28a12010-08-27 09:26:44 +0200280}
281
Harald Welte11461a62012-06-17 12:58:46 +0800282/* default categories */
283static 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 Welte11461a62012-06-17 12:58:46 +0800290 [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 Maier56143542017-10-19 18:20:33 +0200309 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800310 },
Harald Welte11461a62012-06-17 12:58:46 +0800311 [DLLC] = {
312 .name = "DLLC",
313 .description = "GPRS Logical Link Control Protocol (LLC)",
Philipp Maier56143542017-10-19 18:20:33 +0200314 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800315 },
316 [DSNDCP] = {
317 .name = "DSNDCP",
318 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200319 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800320 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200321 [DRANAP] = {
322 .name = "DRANAP",
323 .description = "RAN Application Part (RANAP)",
Philipp Maier56143542017-10-19 18:20:33 +0200324 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200325 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200326 [DSUA] = {
327 .name = "DSUA",
328 .description = "SCCP User Adaptation (SUA)",
Philipp Maier56143542017-10-19 18:20:33 +0200329 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200330 },
Philipp2c7f8372016-08-26 16:58:41 +0200331 [DSLHC] = {
332 .name = "DSLHC",
333 .description = "RFC1144 TCP/IP Header compression (SLHC)",
Philipp Maier56143542017-10-19 18:20:33 +0200334 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp2c7f8372016-08-26 16:58:41 +0200335 },
Philipp73f83d52016-09-02 13:38:01 +0200336 [DV42BIS] = {
337 .name = "DV42BIS",
338 .description = "V.42bis data compression (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200339 .enabled = 1, .loglevel = LOGL_NOTICE,
Pau Espin Pedrol6ec5dc22019-08-28 16:52:56 +0200340 },
341 [DGTP] = {
342 .name = "DGTP",
343 .description = "GPRS Tunnelling Protocol (GTP)",
344 .enabled = 1, .loglevel = LOGL_NOTICE,
345 },
Philipp Maier2ce050b2020-12-14 23:27:47 +0100346 [DRIM] = {
347 .name = "DRIM",
348 .description = "RAN Information Management (RIM)",
349 .enabled = 1, .loglevel = LOGL_NOTICE,
350 },
Harald Welte11461a62012-06-17 12:58:46 +0800351};
352
353static 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 Hofmeyrfea06092017-10-31 01:00:37 +0100359static bool file_exists(const char *path)
360{
361 struct stat sb;
362 return stat(path, &sb) ? false : true;
363}
364
Harald Welte288be162010-05-01 16:48:27 +0200365int main(int argc, char **argv)
366{
Harald Welte288be162010-05-01 16:48:27 +0200367 int rc;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200368#if BUILD_IU
369 struct osmo_sccp_instance *sccp;
370#endif
Harald Welte288be162010-05-01 16:48:27 +0200371
Max4011e722016-07-05 15:19:12 +0200372 srand(time(NULL));
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100373 tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100374 sgsn = sgsn_instance_alloc(tall_sgsn_ctx);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100375 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
376 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte66f793a2010-05-13 11:50:04 +0200377
378 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200379 signal(SIGTERM, &signal_handler);
Harald Welte66f793a2010-05-13 11:50:04 +0200380 signal(SIGABRT, &signal_handler);
381 signal(SIGUSR1, &signal_handler);
382 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200383
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200384 osmo_init_ignore_signals();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100385 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
386 osmo_stats_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200387
Harald Weltec31f4802010-05-25 23:31:39 +0200388 vty_info.copyright = openbsc_copyright;
389 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200390 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100391 osmo_talloc_vty_add_cmds();
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200392 osmo_stats_vty_add_cmds();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100393 sgsn_vty_init(&sgsn->cfg);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100394 ctrl_vty_init(tall_sgsn_ctx);
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +0200395 osmo_cpu_sched_vty_init(tall_sgsn_ctx);
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200396
397#if BUILD_IU
Harald Welte61557f42019-12-01 13:25:16 +0100398 OSMO_ASSERT(osmo_ss7_init() == 0);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100399 osmo_ss7_vty_init_asp(tall_sgsn_ctx);
Neels Hofmeyr395c5582018-09-27 01:55:08 +0200400 osmo_sccp_vty_init();
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200401#endif
Harald Weltedcccb182010-05-16 20:52:23 +0200402
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100403 rate_ctr_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200404
Harald Welte453a51d2020-12-10 15:18:50 +0100405 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp");
Vadim Yanitskiy8de4be22021-03-14 20:58:58 +0100406 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "ns");
Harald Welte68d85d52012-06-16 17:45:59 +0800407
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200408 sgsn_nsi = gprs_ns2_instantiate(tall_sgsn_ctx, &gprs_ns_prim_cb, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200409 if (!sgsn_nsi) {
410 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
411 exit(1);
412 }
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200413 sgsn->cfg.nsi = sgsn_nsi;
Pau Espin Pedrol7a74ae42022-12-23 16:33:17 +0100414 bssgp_set_bssgp_callback(sgsn_bssgp_dispatch_ns_unitdata_req_cb, sgsn_nsi);
Harald Welte496aee42010-06-30 19:59:55 +0200415
416 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200417
Alexander Couzenscaf73b82021-01-27 21:02:13 +0100418 gprs_ns2_vty_init(sgsn_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800419 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200420 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200421 gprs_sndcp_vty_init();
Harald Welte66f793a2010-05-13 11:50:04 +0200422
Harald Welteebd39832021-02-23 16:41:04 +0100423 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 Pedrol73b2bf32018-10-30 18:00:57 +0100440 rc = sgsn_parse_config(sgsn->config_file);
Harald Welte66f793a2010-05-13 11:50:04 +0200441 if (rc < 0) {
Neels Hofmeyr27355c92017-02-24 06:28:31 +0100442 LOGP(DGPRS, LOGL_FATAL, "Error in config file\n");
Harald Welte66f793a2010-05-13 11:50:04 +0200443 exit(2);
444 }
445
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100446 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100447 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100448 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
449 if (rc < 0)
450 exit(1);
451
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100452 rc = sgsn_gtp_init(sgsn);
Harald Welte6efc1762010-05-19 15:46:31 +0200453 if (rc) {
454 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200455 exit(2);
Maxbaabc682017-10-20 13:39:57 +0200456 } else
457 LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
Harald Welte269ae752010-05-18 14:44:31 +0200458
Pau Espin Pedrole659f752023-01-05 17:20:37 +0100459 rc = sgsn_inst_init(sgsn);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100460 if (rc < 0) {
Pau Espin Pedrole659f752023-01-05 17:20:37 +0100461 LOGP(DGPRS, LOGL_FATAL, "Cannot set up SGSN\n");
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100462 exit(2);
463 }
464
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800465 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 Hofmeyree6cfdc2017-07-13 02:03:50 +0200473#if BUILD_IU
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100474 /* Note that these are mostly defaults and can be overriden from the VTY */
Pau Espin Pedrol2c908992019-08-19 19:06:06 +0200475 sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx,
476 sgsn->cfg.iu.cs7_instance,
477 "OsmoSGSN",
Pau Espin Pedrolf0f596f2019-08-19 17:54:16 +0200478 (23 << 3) + 4,
479 OSMO_SS7_ASP_PROT_M3UA,
Pau Espin Pedrole6c5b4a2020-08-20 12:35:07 +0200480 0, "localhost",
481 0, "localhost");
Philipp Maierefe85d32017-04-09 12:32:51 +0200482 if (!sccp) {
483 printf("Setting up SCCP client failed.\n");
484 return 8;
485 }
486
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100487 ranap_iu_init(tall_sgsn_ctx, DRANAP, "OsmoSGSN-IuPS", sccp, gsm0408_gprs_rcvmsg_iu, sgsn_ranap_iu_event);
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200488#endif
489
Harald Welte9ae28a12010-08-27 09:26:44 +0200490 if (daemonize) {
491 rc = osmo_daemonize();
492 if (rc < 0) {
493 perror("Error during daemonize");
494 exit(1);
495 }
496 }
497
Harald Welte288be162010-05-01 16:48:27 +0200498 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200499 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200500 if (rc < 0)
501 exit(3);
502 }
503
Harald Weltebaf77002011-07-16 13:47:01 +0200504 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200505 exit(0);
506}